in do_writeout(), fix problem where a file could sometimes be

overwritten without a warning prompt


git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@3942 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
master
David Lawrence Ramsey 2006-11-07 22:57:13 +00:00
parent 2bf7e975eb
commit b24cb0cd19
2 changed files with 41 additions and 28 deletions

View File

@ -8,6 +8,8 @@ CVS code -
- For consistency, when saving a file with no name, don't - For consistency, when saving a file with no name, don't
allow overwriting an existing file when in restricted allow overwriting an existing file when in restricted
mode. (DLR) mode. (DLR)
- Fix problem where a file could sometimes be overwritten
without a warning prompt. (DLR)
GNU nano 2.0.0 - 2006.11.06 GNU nano 2.0.0 - 2006.11.06
- General: - General:

View File

@ -1867,42 +1867,53 @@ int do_writeout(bool exiting)
break; break;
} }
#endif #endif
if (append == OVERWRITE && strcmp(answer,
openfile->filename) != 0) {
struct stat st;
if (stat(answer, &st) != -1) { if (append == OVERWRITE) {
/* If we're using restricted mode, we aren't allowed char *full_answer = get_full_path(answer);
* to save a new file under the name of an existing char *full_filename = get_full_path(openfile->filename);
* file. In this case, show a "File exists" bool different_name = (strcmp(full_answer,
* error. */ full_filename) != 0);
if (ISSET(RESTRICTED)) { struct stat st;
errno = EEXIST; bool name_exists = (stat(full_answer, &st) != -1);
statusbar(_("Error writing %s: %s"), answer,
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)); strerror(errno));
retval = -1; retval = -1;
break; break;
} else { } else {
i = do_yesno_prompt(FALSE, i = do_yesno_prompt(FALSE,
_("File exists, OVERWRITE ? ")); _("File exists, OVERWRITE ? "));
if (i == 0 || i == -1) if (i == 0 || i == -1)
continue; continue;
} }
/* If we're using restricted mode, we aren't allowed to /* If we're using restricted mode, we aren't allowed
* change the name of a file once it has one, because * to change the name of a file once it has one,
* that would allow reading from or writing to files not * because that would allow reading from or writing
* specified on the command line. In this case, don't * to files not specified on the command line. In
* bother showing the "Different Name" prompt. */ * this case, don't bother showing the "Different
} else if (!ISSET(RESTRICTED) && * Name" prompt. */
} else if (!ISSET(RESTRICTED) &&
openfile->filename[0] != '\0' openfile->filename[0] != '\0'
#ifndef NANO_TINY #ifndef NANO_TINY
&& (exiting || !openfile->mark_set) && (exiting || !openfile->mark_set)
#endif #endif
) { ) {
i = do_yesno_prompt(FALSE, i = do_yesno_prompt(FALSE,
_("Save file under DIFFERENT NAME ? ")); _("Save file under DIFFERENT NAME ? "));
if (i == 0 || i == -1) if (i == 0 || i == -1)
continue; continue;
}
} }
} }