mirror of
https://github.com/fscotto/fsnap.git
synced 2026-11-26 03:09:51 +00:00
Harden create command and directory walk
- create: write the snapshot to a temporary file and copy it into place only after the walk succeeds; every failure now propagates through rc instead of being reported as success, and the temporary is unlinked on all exit paths - create: skip snapshot files by matching the ".fsnap" suffix against the basename, so entries below a directory whose name contains ".fsnap" are no longer dropped from the snapshot - walk: emit a record for directories as well as leaf files, honour the return value of the operation callback, and free the path and close the directory handle on every exit path - utility: replace the fixed-size realpath buffer with realpath(NULL) and add copy(), which reports read, write and close errors instead of silently truncating the destination - scan, create: set errno to EINVAL on invalid arguments so main can print a meaningful message Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Mbj1ZPUEhxqMYKsK7Y7jkt
This commit is contained in:
19
src/main.c
19
src/main.c
@@ -1,5 +1,5 @@
|
||||
#include "scan.h"
|
||||
#include "create.h"
|
||||
#include "scan.h"
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -15,6 +15,11 @@ int main(int argc, char *argv[]) {
|
||||
errno = 0;
|
||||
const char *cmd = argv[1];
|
||||
if (strcmp(cmd, "scan") == 0) {
|
||||
if (argc < 3) {
|
||||
fprintf(stderr, "%s: command %s expected param [directory]\n", argv[0],
|
||||
cmd);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
const char *directory = argv[2];
|
||||
if (scan(directory) == -1) {
|
||||
const int err = errno;
|
||||
@@ -23,15 +28,17 @@ int main(int argc, char *argv[]) {
|
||||
}
|
||||
} else if (strcmp(cmd, "create") == 0) {
|
||||
if (argc < 4) {
|
||||
fprintf(stderr, "%s: command %s expected params [directory output_file]\n", argv[0], cmd);
|
||||
return EXIT_FAILURE;
|
||||
fprintf(stderr,
|
||||
"%s: command %s expected params [directory output_file]\n",
|
||||
argv[0], cmd);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
const char *directory = argv[2];
|
||||
const char *output_file = argv[3];
|
||||
if (create(directory, output_file) == -1) {
|
||||
const int err = errno;
|
||||
fprintf(stderr, "create failed: %s\n", strerror(err));
|
||||
return EXIT_FAILURE;
|
||||
const int err = errno;
|
||||
fprintf(stderr, "create failed: %s\n", strerror(err));
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "command %s is not valid\n", cmd);
|
||||
|
||||
Reference in New Issue
Block a user