vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
commit 3bb0c003c48b50a13b7c20a9de6f7a436860f27d parent 778d21bac348477097fd73c3b04a47552da9a5c9 Author: Marc André Tanner <mat@brain-dump.org> Date: Sun, 23 Feb 2020 11:07:48 +0100 sam: fix modification time comparision when writing file The modification time should only be compared when dealing with the same file i.e. the following should work without a warning: $ touch foo; sleep 1; touch bar $ vis foo :w bar Also switch from path to inode based file equality testing. Diffstat:
| M | sam.c | | | 19 | +++++++++++++------ |
1 file changed, 13 insertions(+), 6 deletions(-)
diff --git a/sam.c b/sam.c @@ -1647,10 +1647,15 @@ static bool cmd_write(Vis *vis, Win *win, Command *cmd, const char *argv[], Sele return false; struct stat meta; - if (cmd->flags != '!' && file->stat.st_mtime && stat(path, &meta) == 0 && - file->stat.st_mtime < meta.st_mtime) { - vis_info_show(vis, "WARNING: file has been changed since reading it"); - goto err; + bool existing_file = !stat(path, &meta); + bool same_file = existing_file && file->name && + file->stat.st_dev == meta.st_dev && file->stat.st_ino == meta.st_ino; + + if (cmd->flags != '!') { + if (same_file && file->stat.st_mtime && file->stat.st_mtime < meta.st_mtime) { + vis_info_show(vis, "WARNING: file has been changed since reading it"); + goto err; + } } if (!vis_event_emit(vis, VIS_EVENT_FILE_SAVE_PRE, file, path) && cmd->flags != '!') { @@ -1689,9 +1694,11 @@ static bool cmd_write(Vis *vis, Win *win, Command *cmd, const char *argv[], Sele goto err; } - if (!file->name) + if (!file->name) { file_name_set(file, path); - if (strcmp(file->name, path) == 0) + same_file = true; + } + if (same_file) file->stat = text_stat(text); vis_event_emit(vis, VIS_EVENT_FILE_SAVE_POST, file, path); free(path);