files: do not silently ignore an invalid backup directory, but die

It would be horrible if the user expects to find numbered backups
of all the files that were changed but they are NOT there because
the config file contains a typo or the relevant directory was moved
or renamed or something.  So... if the specified backup directory
is not usable, nano should complain and simply not start up.
master
Benno Schulenberg 2017-08-05 21:55:03 +02:00
parent ee3254b614
commit 751e7f0fbe
2 changed files with 13 additions and 18 deletions

View File

@ -1487,23 +1487,18 @@ int prompt_failed_backupwrite(const char *filename)
return response; return response;
} }
/* Transform the specified backup directory to an absolute path. */ /* Transform the specified backup directory to an absolute path,
* and verify that it is usable. */
void init_backup_dir(void) void init_backup_dir(void)
{ {
char *full_backup_dir = get_full_path(backup_dir); backup_dir = free_and_assign(backup_dir, get_full_path(backup_dir));
/* When we can't get an absolute path, or it's not a directory, /* If we can't get an absolute path (which means it doesn't exist or
* cancel the making of backups. */ isn't accessible), or it's not a directory, fail. */
if (full_backup_dir == NULL || if (backup_dir == NULL || backup_dir[strlen(backup_dir) - 1] != '/')
full_backup_dir[strlen(full_backup_dir) - 1] != '/') { die(_("Invalid backup directory\n"));
free(full_backup_dir);
free(backup_dir); snuggly_fit(&backup_dir);
backup_dir = NULL;
} else {
free(backup_dir);
backup_dir = full_backup_dir;
snuggly_fit(&backup_dir);
}
} }
#endif /* !NANO_TINY */ #endif /* !NANO_TINY */

View File

@ -2375,10 +2375,10 @@ int main(int argc, char **argv)
#endif /* !DISABLE_HISTORIES */ #endif /* !DISABLE_HISTORIES */
#ifndef NANO_TINY #ifndef NANO_TINY
/* If a backup directory was specified and we're not in restricted mode, /* If backups are enabled and a backup directory was specified and
* make sure the path exists and is a directory, so that backup files can * we're not in restricted mode, make sure the path exists and is
* be saved there. */ * a directory, so that backup files can be saved there. */
if (backup_dir != NULL && !ISSET(RESTRICTED)) if (ISSET(BACKUP_FILE) && backup_dir != NULL && !ISSET(RESTRICTED))
init_backup_dir(); init_backup_dir();
#endif #endif