From 94a7020ad8c9efd9c5818eb3422ff4cb66a1b278 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Tue, 13 Aug 2024 01:01:28 -0700 Subject: vmon: use temp name and rename() for snapshots This switches to constructing the WIP png in a temporary dot-file derived from the final name, then an atomic rename from the dot-file to the final name. The temporary name is placed in the same directory as the final one. --- src/vmon.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/vmon.c b/src/vmon.c index d69154e..3977be0 100644 --- a/src/vmon.c +++ b/src/vmon.c @@ -764,7 +764,7 @@ static int vmon_snapshot(vmon_t *vmon) struct tm *t; char t_str[32]; char *name = NULL; - char path[4096]; + char path[4096], tmp_path[4096]; FILE *output; int r; @@ -794,20 +794,31 @@ static int vmon_snapshot(vmon_t *vmon) name ? "-" : "", t_str, vmon->n_snapshots++); + snprintf(tmp_path, sizeof(tmp_path), "%s/.%s%s%s-%u.png-WIP", + vmon->output_dir, + name ? name : "", + name ? "-" : "", + t_str, + vmon->n_snapshots); free(name); - output = fopen(path, "w+"); + output = fopen(tmp_path, "w+"); if (!output) return -errno; r = vmon_snapshot_as_png(vmon, output); if (r < 0) { - fclose(output); + (void) unlink(tmp_path); + (void) fclose(output); return r; } + fflush(output); fsync(fileno(output)); fclose(output); + r = rename(tmp_path, path); + if (r < 0) + return -errno; return 0; } -- cgit v1.2.3