]> git.wh0rd.org Git - nano.git/commitdiff
in do_writeout(), fix problem where a file could sometimes be
authorDavid Lawrence Ramsey <pooka109@gmail.com>
Tue, 7 Nov 2006 22:57:13 +0000 (22:57 +0000)
committerDavid Lawrence Ramsey <pooka109@gmail.com>
Tue, 7 Nov 2006 22:57:13 +0000 (22:57 +0000)
overwritten without a warning prompt

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@3942 35c25a1d-7b9e-4130-9fde-d3aeb78583b8

ChangeLog
src/files.c

index f971bb156180a7a67c47d469e0ee046eda699c36..2bdf16d2ed800bf37f96b0137f5f25dfd96dea98 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -8,6 +8,8 @@ CVS code -
        - For consistency, when saving a file with no name, don't
          allow overwriting an existing file when in restricted
          mode. (DLR)
+       - Fix problem where a file could sometimes be overwritten
+         without a warning prompt. (DLR)
 
 GNU nano 2.0.0 - 2006.11.06
 - General:
index 25c3362002ce23aebfcea1b10d0089f6ea2b9317..210ae60c6509ad64f84ec43a3a6b8a8beb66f38f 100644 (file)
@@ -1867,42 +1867,53 @@ int do_writeout(bool exiting)
                break;
            }
 #endif
-           if (append == OVERWRITE && strcmp(answer,
-               openfile->filename) != 0) {
-               struct stat st;
 
-               if (stat(answer, &st) != -1) {
-                   /* If we're using restricted mode, we aren't allowed
-                    * to save a new file under the name of an existing
-                    * file.  In this case, show a "File exists"
-                    * error. */
-                   if (ISSET(RESTRICTED)) {
-                       errno = EEXIST;
-                       statusbar(_("Error writing %s: %s"), answer,
+           if (append == OVERWRITE) {
+               char *full_answer = get_full_path(answer);
+               char *full_filename = get_full_path(openfile->filename);
+               bool different_name = (strcmp(full_answer,
+                       full_filename) != 0);
+               struct stat st;
+               bool name_exists = (stat(full_answer, &st) != -1);
+
+               free(full_filename);
+               free(full_answer);
+
+               if (different_name) {
+                   if (name_exists) {
+                       /* If we're using restricted mode, we aren't
+                        * allowed to save a new file under the name of
+                        * an existing file.  In this case, show a "File
+                        * exists" error. */
+                       if (ISSET(RESTRICTED)) {
+                           errno = EEXIST;
+                           statusbar(_("Error writing %s: %s"), answer,
                                strerror(errno));
-                       retval = -1;
-                       break;
-                   } else {
-                       i = do_yesno_prompt(FALSE,
+                           retval = -1;
+                           break;
+                       } else {
+                           i = do_yesno_prompt(FALSE,
                                _("File exists, OVERWRITE ? "));
-                       if (i == 0 || i == -1)
-                           continue;
-                   }
-               /* If we're using restricted mode, we aren't allowed to
-                * change the name of a file once it has one, because
-                * that would allow reading from or writing to files not
-                * specified on the command line.  In this case, don't
-                * bother showing the "Different Name" prompt. */
-               } else if (!ISSET(RESTRICTED) &&
+                           if (i == 0 || i == -1)
+                               continue;
+                       }
+                   /* If we're using restricted mode, we aren't allowed
+                    * to change the name of a file once it has one,
+                    * because that would allow reading from or writing
+                    * to files not specified on the command line.  In
+                    * this case, don't bother showing the "Different
+                    * Name" prompt. */
+                   } else if (!ISSET(RESTRICTED) &&
                        openfile->filename[0] != '\0'
 #ifndef NANO_TINY
                        && (exiting || !openfile->mark_set)
 #endif
                        ) {
-                   i = do_yesno_prompt(FALSE,
-                       _("Save file under DIFFERENT NAME ? "));
-                   if (i == 0 || i == -1)
-                       continue;
+                       i = do_yesno_prompt(FALSE,
+                               _("Save file under DIFFERENT NAME ? "));
+                       if (i == 0 || i == -1)
+                           continue;
+                   }
                }
            }