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:
Fabio Scotto di Santolo
2026-08-01 18:08:26 +02:00
parent 3eae528f54
commit 777f14a9e9
6 changed files with 148 additions and 55 deletions

View File

@@ -1,7 +1,10 @@
#define _DEFAULT_SOURCE
#include "scan.h"
#include "utility.h"
#include "walk.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int print_file(const char *file, void *context) {
@@ -12,11 +15,19 @@ static int print_file(const char *file, void *context) {
/* Scan recursively directory, printing all entry */
int scan(const char *directory) {
if (directory == NULL || strlen(directory) == 0)
if (directory == NULL || strlen(directory) == 0) {
errno = EINVAL;
return -1;
}
const char *dir = resolve_path(directory);
char *dir = resolve_path(directory);
if (dir == NULL)
return -1;
return walk(dir, print_file, NULL);
if (walk(dir, print_file, NULL) == -1) {
free(dir);
return -1;
}
free(dir);
return 0;
}