From 8bd76d8302af33340c1ca102cb4775124b6d16dc Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Wed, 20 Jan 2016 15:14:52 +0000 Subject: [PATCH] Verifying that the containing directory of the given filename exists. Original patch by Rishabh Dave, edited by Benno. git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@5572 35c25a1d-7b9e-4130-9fde-d3aeb78583b8 --- ChangeLog | 6 ++++++ src/files.c | 43 +++++++++++++++++++++++++++++-------------- src/global.c | 2 ++ src/proto.h | 2 ++ 4 files changed, 39 insertions(+), 14 deletions(-) diff --git a/ChangeLog b/ChangeLog index a8578590..81f4f566 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2016-01-20 Rishabh Dave + * src/files.c (verify_path, open_buffer): When opening a new buffer, + verify that the containing directory of the given filename exists. + * src/files.c (do_lockfile): Remove the existence check on the + directory, as this is now covered by verify_path(). + 2016-01-17 Benno Schulenberg * src/global.c: Fix typo in #ifndef symbol. Reported by Frank. * doc/syntax/nanorc.nanorc: Remove '+' as only one menu is allowed. diff --git a/src/files.c b/src/files.c index a2104ada..310c3000 100644 --- a/src/files.c +++ b/src/files.c @@ -33,6 +33,29 @@ #include #include +/* Determine whether the containing directory of the given filename exists. + * Pass the result back in the global variable valid_path. */ +void verify_path(const char *filename) +{ + char *parentdir; + struct stat parentinfo; + + if (strrchr(filename, '/') == NULL) + parentdir = mallocstrcpy(NULL, "."); + else + parentdir = dirname(mallocstrcpy(NULL, filename)); + + if (stat(parentdir, &parentinfo) != -1 && S_ISDIR(parentinfo.st_mode)) + valid_path = TRUE; + else { + statusbar(_("Directory '%s' does not exist"), parentdir); + valid_path = FALSE; + beep(); + } + + free(parentdir); +} + /* Add an entry to the openfile openfilestruct. This should only be * called from open_buffer(). */ void make_new_buffer(void) @@ -112,7 +135,7 @@ void set_modified(void) titlebar(NULL); #ifndef NANO_TINY - if (!ISSET(LOCKING) || openfile->filename[0] == '\0') + if (!ISSET(LOCKING) || openfile->filename[0] == '\0' || !valid_path) return; if (openfile->lock_filename == NULL) { @@ -282,7 +305,6 @@ int do_lockfile(const char *filename) size_t locknamesize = strlen(filename) + strlen(locking_prefix) + strlen(locking_suffix) + 3; char *lockfilename = charalloc(locknamesize); - char *lockfiledir = NULL; static char lockprog[11], lockuser[17]; struct stat fileinfo; int lockfd, lockpid; @@ -331,18 +353,7 @@ int do_lockfile(const char *filename) blank_statusbar(); return -1; } - } else { - lockfiledir = mallocstrcpy(NULL, lockfilename); - lockfiledir = dirname(lockfiledir); - if (stat(lockfiledir, &fileinfo) == -1) { - statusbar(_("Error writing lock file: Directory \'%s\' doesn't exist"), - lockfiledir); - free(lockfiledir); - return 0; - } - free(lockfiledir); } - return write_lockfile(lockfilename, filename, FALSE); } #endif /* !NANO_TINY */ @@ -393,6 +404,9 @@ bool open_buffer(const char *filename, bool undoable) if (new_buffer) { make_new_buffer(); + verify_path(filename); + + if (valid_path) { #ifndef NANO_TINY if (ISSET(LOCKING) && filename[0] != '\0') { int lockstatus = do_lockfile(filename); @@ -408,6 +422,7 @@ bool open_buffer(const char *filename, bool undoable) } } #endif + } } /* If the filename isn't blank, and we are not in NOREAD_MODE, @@ -963,7 +978,7 @@ int open_file(const char *filename, bool newfie, bool quiet, FILE **f) } if (newfie) { - if (!quiet) + if (!quiet && valid_path) statusbar(_("New File")); return -2; } diff --git a/src/global.c b/src/global.c index 829c19bf..b252f64a 100644 --- a/src/global.c +++ b/src/global.c @@ -40,6 +40,8 @@ bool func_key; /* Whether the current keystroke is an extended keypad value. */ bool focusing = FALSE; /* Whether an update of the edit window should center the cursor. */ +bool valid_path; + /* Whether the containing directory of a specified file exists. */ #ifndef NANO_TINY int controlleft = CONTROL_LEFT; diff --git a/src/proto.h b/src/proto.h index e0842b48..1056b76d 100644 --- a/src/proto.h +++ b/src/proto.h @@ -34,6 +34,7 @@ extern volatile sig_atomic_t sigwinch_counter; extern bool meta_key; extern bool func_key; extern bool focusing; +extern bool valid_path; #ifndef NANO_TINY extern int controlleft; @@ -281,6 +282,7 @@ void do_cut_till_eof(void); void do_uncut_text(void); /* All functions in files.c. */ +void verify_path(const char *filename); void make_new_buffer(void); void initialize_buffer_text(void); bool open_buffer(const char *filename, bool undoable); -- 2.39.5