files: make a backup only when requested, not an unrequested failsafe one

After thinking about it, I don't like it that nano makes an unrequested
failsafe backup: in the vast, vast majority of cases it is a waste both
of time and of disk wear.  If the user is worried about data loss, they
can use --backup.  The fsync() after writing out the buffer (added in
commit a84cdaaa) already reduces the chances on data loss somewhat.
master
Benno Schulenberg 2020-07-15 10:05:21 +02:00
parent 5f90e067f1
commit f7682730bd
1 changed files with 1 additions and 15 deletions

View File

@ -1605,7 +1605,6 @@ bool backup_file(char *realname, char **backupname)
statusbar(_("Making backup...")); statusbar(_("Making backup..."));
if (ISSET(MAKE_BACKUP)) {
/* If no backup directory was specified, we make a simple backup /* If no backup directory was specified, we make a simple backup
* by appending a tilde to the original file name. Otherwise, * by appending a tilde to the original file name. Otherwise,
* we create a numbered backup in the specified directory. */ * we create a numbered backup in the specified directory. */
@ -1652,12 +1651,6 @@ bool backup_file(char *realname, char **backupname)
/* Create the backup file (or truncate the existing one). */ /* Create the backup file (or truncate the existing one). */
backup_fd = open(*backupname, backup_cflags, S_IRUSR|S_IWUSR); backup_fd = open(*backupname, backup_cflags, S_IRUSR|S_IWUSR);
} else {
*backupname = charalloc(strlen(realname) + 8);
sprintf(*backupname, "%s~XXXXXX", realname);
backup_fd = mkstemp(*backupname);
}
retry_backup: retry_backup:
if (backup_fd >= 0) if (backup_fd >= 0)
@ -1718,11 +1711,9 @@ bool backup_file(char *realname, char **backupname)
unlink(*backupname); unlink(*backupname);
free(*backupname); free(*backupname);
if (ISSET(MAKE_BACKUP)) {
warn_and_briefly_pause(_("Cannot make regular backup")); warn_and_briefly_pause(_("Cannot make regular backup"));
warn_and_briefly_pause(_("Trying again in your home directory")); warn_and_briefly_pause(_("Trying again in your home directory"));
currmenu = MMOST; currmenu = MMOST;
}
*backupname = charalloc(strlen(homedir) + strlen(tail(realname)) + 9); *backupname = charalloc(strlen(homedir) + strlen(tail(realname)) + 9);
sprintf(*backupname, "%s/%s~XXXXXX", homedir, tail(realname)); sprintf(*backupname, "%s/%s~XXXXXX", homedir, tail(realname));
@ -1797,7 +1788,7 @@ bool write_file(const char *name, FILE *thefile, bool tmp,
/* When the user requested a backup, we do this only if the file exists and /* When the user requested a backup, we do this only if the file exists and
* isn't temporary AND the file has not been modified by someone else since * isn't temporary AND the file has not been modified by someone else since
* we opened it (or we are appending/prepending or writing a selection). */ * we opened it (or we are appending/prepending or writing a selection). */
if (is_existing_file && !ISSET(RESTRICTED) && openfile->statinfo && if (ISSET(MAKE_BACKUP) && is_existing_file && openfile->statinfo &&
(openfile->statinfo->st_mtime == st.st_mtime || (openfile->statinfo->st_mtime == st.st_mtime ||
method != OVERWRITE || openfile->mark)) { method != OVERWRITE || openfile->mark)) {
if (!backup_file(realname, &backupname)) if (!backup_file(realname, &backupname))
@ -1971,11 +1962,6 @@ bool write_file(const char *name, FILE *thefile, bool tmp,
goto cleanup_and_exit; goto cleanup_and_exit;
} }
/* If no backups were requested, delete the temporary backup file
* that was created just to ensure a failsafe replacement. */
if (!ISSET(MAKE_BACKUP) && backupname != NULL)
unlink(backupname);
/* When having written an entire buffer, update some administrivia. */ /* When having written an entire buffer, update some administrivia. */
if (fullbuffer && method == OVERWRITE && !tmp) { if (fullbuffer && method == OVERWRITE && !tmp) {
/* If the filename was changed, write a new lockfile when needed, /* If the filename was changed, write a new lockfile when needed,