Files
fsnap/src/main.c
Fabio Scotto di Santolo 691988e505 Add SPEC.md and bring list, create and scan in line with it
SPEC.md records the design the project has been following: goals, staged
plan, and the rules for parsing, error handling and resource ownership.

Conformance fixes it made visible:

- list: accept the `?` file type that create legitimately writes; read
  records with getline so long paths are no longer truncated at a fixed
  buffer size (SPEC 13); reject numeric fields that are not bare digits
  and check ERANGE, so out-of-range values and negative uid/gid no
  longer wrap silently (SPEC 14); release the record when the path
  allocation fails (SPEC 11).
- list, create: keep the errno of the failure that actually occurred.
  An error raised before the cleanup owns errno and is restored; one
  raised by fclose owns it only when nothing had failed yet, which is
  the case a buffered write can hit for the first time at close
  (SPEC 8, SPEC 10).
- scan: propagate printf failures and check fflush(stdout), so a write
  error to a redirected stdout is no longer silently ignored (SPEC 8).
- An empty snapshot is a valid snapshot with zero records, not an
  error: create produces one for an empty directory.

README.md now documents list, points at SPEC.md where the two differ,
and drops the roadmap entry that proposed renaming list to verify --
SPEC 12 defines list as the parser and SPEC 18 reserves verify for
comparing a snapshot against the live filesystem.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F3NNME8GifswDYLr9VpBzs
2026-08-16 11:03:31 +02:00

64 lines
1.7 KiB
C

#include "command.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "%s: command not found [scan|create|list]\n", argv[0]);
return EXIT_FAILURE;
}
// I check the argument command exists
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;
fprintf(stderr, "scan failed: %s\n", strerror(err));
return EXIT_FAILURE;
}
} 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;
}
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;
}
} else if (strcmp(cmd, "list") == 0) {
if (argc < 3) {
fprintf(stderr, "%s: command %s expected param [snapshot_file]", argv[0],
cmd);
return EXIT_FAILURE;
}
const char *snapshot = argv[2];
int ret = list(snapshot);
if (ret == -1) {
const int err = errno;
fprintf(stderr, "list failed: %s\n", strerror(err));
return EXIT_FAILURE;
} else if (ret > 0) {
return EXIT_FAILURE;
}
} else {
fprintf(stderr, "command %s is not valid\n", cmd);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}