From 978f3d70b9047608789e40a0b67e91d4fd19e60a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Mon, 27 Jan 2020 10:35:11 +0100 Subject: vis: pass absolute path to pre/post save events --- sam.c | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) (limited to 'sam.c') diff --git a/sam.c b/sam.c index b34ccb5..16f811c 100644 --- a/sam.c +++ b/sam.c @@ -1623,23 +1623,28 @@ static bool cmd_write(Vis *vis, Win *win, Command *cmd, const char *argv[], Sele } for (const char **name = argv[1] ? &argv[1] : (const char*[]){ filename, NULL }; *name; name++) { + + char *path = absolute_path(*name); + if (!path) + return false; + struct stat meta; - if (cmd->flags != '!' && file->stat.st_mtime && stat(*name, &meta) == 0 && + 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"); - return false; + goto err; } - if (!vis_event_emit(vis, VIS_EVENT_FILE_SAVE_PRE, file, *name) && cmd->flags != '!') { - vis_info_show(vis, "Rejected write to `%s' by pre-save hook", *name); - return false; + if (!vis_event_emit(vis, VIS_EVENT_FILE_SAVE_PRE, file, path) && cmd->flags != '!') { + vis_info_show(vis, "Rejected write to `%s' by pre-save hook", path); + goto err; } - TextSave *ctx = text_save_begin(text, *name, file->save_method); + TextSave *ctx = text_save_begin(text, path, file->save_method); if (!ctx) { const char *msg = errno ? strerror(errno) : "try changing `:set savemethod`"; - vis_info_show(vis, "Can't write `%s': %s", *name, msg); - return false; + vis_info_show(vis, "Can't write `%s': %s", path, msg); + goto err; } bool failure = false; @@ -1659,15 +1664,21 @@ static bool cmd_write(Vis *vis, Win *win, Command *cmd, const char *argv[], Sele } if (failure || !text_save_commit(ctx)) { - vis_info_show(vis, "Can't write `%s': %s", *name, strerror(errno)); - return false; + vis_info_show(vis, "Can't write `%s': %s", path, strerror(errno)); + goto err; } if (!file->name) - file_name_set(file, *name); - if (strcmp(file->name, *name) == 0) + file_name_set(file, path); + if (strcmp(file->name, path) == 0) file->stat = text_stat(text); - vis_event_emit(vis, VIS_EVENT_FILE_SAVE_POST, file, *name); + vis_event_emit(vis, VIS_EVENT_FILE_SAVE_POST, file, path); + free(path); + continue; + + err: + free(path); + return false; } return true; } -- cgit v1.2.3