files: write a lock file also for a new file and when the name changed

(When the new or changed name is that of an existing file AND this
file is being edited by some other editor (or nano itself), then the
behavior is rather strange: nano asks whether to continue, but the
file has already been overwritten and there is nothing the user can
do to cancel or revert things.  A later commit should improve this.)

This fixes https://savannah.gnu.org/bugs/?47975,
and fixes https://savannah.gnu.org/bugs/?53883.
master
Benno Schulenberg 2020-01-13 19:10:23 +01:00
parent 4687322398
commit 497f126bb2
1 changed files with 10 additions and 1 deletions

View File

@ -1867,8 +1867,17 @@ bool write_file(const char *name, FILE *thefile, bool tmp,
/* When having written an entire buffer, update some administrivia. */
if (fullbuffer && method == OVERWRITE && !tmp) {
/* If the filename was changed, check if this means a new syntax. */
/* If the filename was changed, write a new lockfile when needed,
* and check whether it means a different syntax gets used. */
if (strcmp(openfile->filename, realname) != 0) {
#ifndef NANO_TINY
if (openfile->lock_filename != NULL) {
delete_lockfile(openfile->lock_filename);
free(openfile->lock_filename);
openfile->lock_filename = NULL;
do_lockfile(realname);
}
#endif
#ifdef ENABLE_COLOR
const char *oldname, *newname;