aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2020-02-23 11:07:48 +0100
committerMarc André Tanner <mat@brain-dump.org>2020-02-23 11:59:14 +0100
commit9d789adf02d80001bbd2f2af7f48526e08ccffe8 (patch)
tree0d5c5b08cace6e4c20a4a7a358ccefd9085569cf
parent390024fad1248a437173b4246de05bea4576476c (diff)
downloadvis-9d789adf02d80001bbd2f2af7f48526e08ccffe8.tar.gz
vis-9d789adf02d80001bbd2f2af7f48526e08ccffe8.tar.xz
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.
-rw-r--r--sam.c19
1 files 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);