summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVito Caputo <vcaputo@pengaru.com>2024-08-13 01:01:28 -0700
committerVito Caputo <vcaputo@pengaru.com>2024-08-13 01:01:28 -0700
commit94a7020ad8c9efd9c5818eb3422ff4cb66a1b278 (patch)
tree938392c9a067e099ce6cdfc1bee2d23f8a2eae7d
parent922406f5df68a2014bff05d366bb3057ab6e4d17 (diff)
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.
-rw-r--r--src/vmon.c17
1 files changed, 14 insertions, 3 deletions
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;
}
© All Rights Reserved