From 4f5995f5526cbeacbea2ef9f4184036047a8363c Mon Sep 17 00:00:00 2001 From: Viorel Bota Date: Mon, 18 Sep 2017 23:52:59 +0300 Subject: [PATCH] fix for: "bug #51040: when using 'savefile' or --tempfile" Signed-off-by: Viorel Bota --- src/files.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/files.c b/src/files.c index 37f61e3..9fd3101 100644 --- a/src/files.c +++ b/src/files.c @@ -2047,6 +2047,48 @@ int do_writeout(bool exiting) as_an_at = FALSE; if (exiting && ISSET(TEMP_FILE) && openfile->filename[0] != '\0') { + bool name_exists; + char *full_filename; + char *short_filename = NULL; + struct stat st; + + short_filename = mallocstrcpy(short_filename, openfile->filename); + + /* If short_filename is null, + * return 0 to indicate writing error */ + if ( NULL==short_filename ) + return 0; + + full_filename = get_full_path(short_filename); + name_exists = (stat((full_filename == NULL) ? + short_filename : full_filename, &st) != -1); + + free(full_filename); + free(short_filename); + + /* Complain if the file exists, the name hasn't changed, + * and the stat information we had before does not match + * what we have now. */ + if (name_exists && openfile->current_stat && + (openfile->current_stat->st_mtime < st.st_mtime || + openfile->current_stat->st_dev != st.st_dev || + openfile->current_stat->st_ino != st.st_ino)) { + + warn_and_shortly_pause(_("File on disk has changed")); + + int response = do_yesno_prompt(FALSE, _("File was modified since " + "you opened it; continue saving? ")); + + /* If Cancel option was selected, + * return 0 to indicate writing error */ + if ( 0>response ) + return 0; + /* If No option was selected, + * return 2 to discard buffer */ + else if ( 0==response ) + return 2; + } + if (write_file(openfile->filename, NULL, FALSE, OVERWRITE, FALSE)) return 1; /* If writing the file failed, go on to prompt for a new name. */ -- 2.7.4