From 9d789adf02d80001bbd2f2af7f48526e08ccffe8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Sun, 23 Feb 2020 11:07:48 +0100 Subject: 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. --- sam.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/sam.c b/sam.c index 3ea7aee..2816403 100644 --- 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); -- cgit v1.2.3