Commit Graph

47 Commits

Author SHA1 Message Date
Fabio Scotto di Santolo
4b1d0b02b2 Document the iterative walker in the README 2026-09-13 23:35:38 +02:00
Fabio Scotto di Santolo
4e14e91c11 Merge pull request #1 from fscotto/feature/walk-dfs
Feature/walk dfs
2026-09-13 23:34:13 +02:00
Fabio Scotto di Santolo
3c6785532a Walk iteratively with an explicit stack 2026-09-13 23:33:29 +02:00
Fabio Scotto di Santolo
f2817f3ca5 Fix stack implementation 2026-09-13 16:04:11 +02:00
Fabio Scotto di Santolo
be86f05d58 Implement stack data structure WIP 2026-08-27 00:11:35 +02:00
Fabio Scotto di Santolo
8c34ab4f4e Add a make valgrind target
Runs the existing suite with every fsnap invocation under memcheck,
rather than duplicating a list of commands in the Makefile: tests/run.sh
already funnels each call through fsnap(), so a command prefix is all it
takes. VALGRIND is empty for a plain `make test`.

-q keeps stderr clean, which matters because several tests compare it,
and --error-exitcode=99 turns a finding into a failing test instead of a
note nobody reads. The per-invocation timeout goes to 300s, since
memcheck costs an order of magnitude: the whole suite takes about 25
seconds against a fraction of a second without it.

Checked the same way as the suite itself. Dropping the free() of the
getline buffer in list keeps `make test` green at 91 passed while `make
valgrind` fails, which is the whole point of having it. Replacing the
calloc in RecordObjectNew with malloc is caught too, and named precisely:
free() on an uninitialised value at types.c:420. Baseline green either
side, and the missing-valgrind guard exits 2 with a clear message.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018KyCHPAxWzqa3vHKdXJkgx
2026-08-26 16:19:50 +02:00
Fabio Scotto di Santolo
aad607e5f0 Add a test suite and a make test target
91 black-box checks in tests/run.sh, driving the built binary because
that is the only interface the tool has. POSIX sh, no dependencies, no
framework; TAP-ish output and a non-zero exit when anything fails.

Covers the tree shapes SPEC.md section 22 asks for -- regular and empty
files, nested directories, symlinks, broken symlinks, symlinks to
directories, hard links, FIFOs, sockets, unusual filenames and permission
errors -- the parser cases from sections 14 and 15, the diff
classifications, argument handling, and the resource rules from sections
9 and 11. What the environment cannot provide is skipped, not failed.

Every bug fixed in this branch is pinned by a test: the 2^32 comparison
truncation, the symlink checksum, target-before-fingerprint ordering, the
mkstemp template unlink, errno surviving a cleanup failure, the empty
snapshot in diff, and create over each entry type that used to abort it.

Invocations run under timeout(1) where it exists. Without it a walker
regression makes create block on a FIFO forever, which hangs the suite
instead of failing it -- found by reintroducing exactly that bug.

Checked by mutation: seven deliberate regressions, one per fix, each
caught by the test written for it, with the baseline green either side.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018KyCHPAxWzqa3vHKdXJkgx
2026-08-26 16:13:27 +02:00
Fabio Scotto di Santolo
0417bd825a Checksum a symlink over its target string
e2b45e6 left symlinks with fingerprint 0 on the grounds that the target
field already describes them. Restore a real checksum for them, computed
over the target string readlink reports -- which is what the link
actually holds on disk -- rather than over the referent's contents, which
is what the code did before e2b45e6 and what made `create /etc` fail with
EISDIR on the 13 links there that point at directories.

Adds crc32_buffer for the in-memory case. Values agree with zlib.crc32
of the same string.

Directories keep 0, and so do FIFOs, sockets and devices: those cannot be
read at all rather than merely being awkward to read. A FIFO blocks until
a writer appears, which is an indefinite hang, not an error.

Also reorder RecordObjectCompare to test target before fingerprint. Since
a link's fingerprint is now a function of its target, the old order
reported a changed target as a changed checksum, and diff printed M where
it should print L. The fields are now compared in the order the record
stores them.

Verified: all 1236 symlinks in a tree shaped like /etc get a non-zero
fingerprint and the walk completes; L, M and P classifications confirmed
individually; valgrind clean.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018KyCHPAxWzqa3vHKdXJkgx
2026-08-26 16:01:06 +02:00
Fabio Scotto di Santolo
e2b45e6ca8 Fingerprint regular files only
create checksummed every entry that was not a directory, which meant it
called fopen on symlinks, FIFOs, sockets and devices. fopen follows
symlinks, so a link to a directory failed with EISDIR -- which is what
`fsnap create /etc` hits, /etc holding 13 of them:

  $ fsnap create /etc snapshot.fsnap
  create failed: Is a directory

Each of the other types failed its own way: a broken symlink with ENOENT,
a socket with ENXIO, and a FIFO by blocking forever waiting for a writer.

Only a regular file has contents worth checksumming, and only a regular
file is safe to open. A symlink is already fully described by its target
field, so checksumming its referent was answering a different question
anyway. Everything that is not a regular file now keeps fingerprint 0.

This changes recorded fingerprints for symlinks, from the referent's
checksum to 0. The format carries no version yet, so old snapshots will
diff as modified against new ones.

Verified over a tree rebuilt with the exact shape of /etc -- 1597 regular
files, 1236 symlinks including 13 pointing at directories, 449
directories -- which now completes with all 3282 records valid, and over
the four failing types in isolation.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018KyCHPAxWzqa3vHKdXJkgx
2026-08-26 15:51:18 +02:00
Fabio Scotto di Santolo
25b328ba79 Add install and uninstall targets
install copies the binary to $(BINDIR), defaulting to /usr/local/bin.
PREFIX, BINDIR and INSTALL are overridable, and DESTDIR is honoured so a
packager can stage into a temporary root; it is deliberately left
undefaulted rather than set to empty.

uninstall removes what install placed, and is idempotent.

Both use install(1) rather than cp, so the mode is set explicitly and the
target directory is created in the same step. The flags used, -d and -m,
are common to GNU and BSD install, which SPEC.md section 19 asks for.

Verified: staged install under DESTDIR, PREFIX and BINDIR overrides,
uninstall in both plain and staged form, uninstall with nothing to
remove, and install from a clean tree rebuilding first.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018KyCHPAxWzqa3vHKdXJkgx
2026-08-26 15:46:05 +02:00
Fabio Scotto di Santolo
4e56e3e37c Update README now that the sources declare no feature test macros
The Building section described _GNU_SOURCE as an open gap against
SPEC.md section 19. It was closed in e193783: the macros moved to the
Makefile and nothing behind them is outside POSIX.1-2008.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018KyCHPAxWzqa3vHKdXJkgx
2026-08-26 15:38:20 +02:00
Fabio Scotto di Santolo
93e78fc7b3 Cosmetic cleanups across the tree
No behavioural change; verified byte-identical scan output over
/usr/include and /etc and byte-identical snapshots against the previous
build, with valgrind clean on every command.

- free() accepts NULL, so drop six guards around it in types.c.
- RecordObjectRelease returned an int no caller ever read; make it void,
  which is the idiom for a free-like function.
- RecordObjectFieldName bounded its index only from above. The underlying
  type of an enum is the compiler's choice, so compare as unsigned and
  catch a negative value too.
- copy() opened the destination "w+" but never reads it back; "w" says
  what it does.
- Testing strlen(s) == 0 walks the whole string to answer a question
  about its first byte.
- Two comments contradicted the code: split_record finds eight
  separators, not six, and strconcat explains itself in terms of a strcat
  it does not call.
- Drop includes nothing uses: <inttypes.h> from list.c and create.c, and
  <string.h> from list.c and from scan.c, which stopped needing it once
  the strlen above was gone.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018KyCHPAxWzqa3vHKdXJkgx
2026-08-26 15:37:57 +02:00
Fabio Scotto di Santolo
381f7b242d Report crc32 failure through the return value, not a sentinel
crc32 returned 0 both for a read error and as a genuine checksum, so the
caller had to remember to test ferror separately. That is precisely how
the descriptor leak fixed in 9e65819 got in.

The ambiguity is not hypothetical: an empty file checksums to exactly 0,
and create writes one for every empty file it walks.

Take a uint32_t out parameter and return 0/-1 instead.

Verified byte-identical snapshots against the previous build, and the
values still agree with zlib.crc32 for both an empty and a 100KB file.
A read failure still surfaces: create over a symlink to a directory
reports "Is a directory".

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018KyCHPAxWzqa3vHKdXJkgx
2026-08-26 15:36:29 +02:00
Fabio Scotto di Santolo
cf7db11099 Name the parse-error status instead of repeating 128
The literal appeared in list.c and diff.c, was spelled out in prose in
both their headers, and collided with an unrelated 128 in
RecordObjectCompare, where it is an ordering sentinel for a NULL argument
rather than a status.

Introduce include/status.h with FSNAP_EPARSE, use it in both commands,
and say in types.h that the value there means something else.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018KyCHPAxWzqa3vHKdXJkgx
2026-08-26 15:33:31 +02:00
Fabio Scotto di Santolo
e19378308d Declare feature test macros once in the build, not per source file
Nine files had drifted into four different combinations: _GNU_SOURCE in
walk.c, _XOPEN_SOURCE 700 in utility.c, _DEFAULT_SOURCE in scan.c,
_POSIX_C_SOURCE 200809L in list.c and diff.c, and all three at once in
create.c and types.c, where _XOPEN_SOURCE 700 already implies POSIX 2008.

_GNU_SOURCE was also wider than anything the code asks for: walk needs
dirent.d_type, which _DEFAULT_SOURCE provides. Closing that gap is what
SPEC.md section 19 asks for and what the README flags as the one thing
keeping the tree from being POSIX-only.

CPPFLAGS now carries -D_POSIX_C_SOURCE=200809L -D_DEFAULT_SOURCE, and the
sources declare nothing. Confirmed DT_UNKNOWN is still visible under that
pair, so the d_type fast path stays active rather than silently falling
back to lstat.

Verified against the previous build: byte-identical scan output over
/usr/include, /usr/share/doc and /etc (17820 entries), and byte-identical
snapshots from create.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018KyCHPAxWzqa3vHKdXJkgx
2026-08-26 15:32:35 +02:00
Fabio Scotto di Santolo
14f6d8aca8 Stop reporting an allocation failure as a malformed record
RecordObjectUnpack returned -1 both for a bad record and for a failed
allocation inside itself, and only the first case set *err. list() read
that as "no field identified, so the line is unparsable" and printed a
parse error for a perfectly valid snapshot, exiting 1 instead of
reporting a system error. The errno == EINVAL protocol that already told
the two apart inside unescape_path stopped at the function boundary.

With calloc made to fail on a valid two-record snapshot:

  before:  good.fsnap:1: unparsable line
  after:   list failed: Cannot allocate memory

Give every malformed-record exit errno EINVAL through a small malformed()
helper, set ENOMEM explicitly where calloc fails (the C standard does not
require calloc to set it, and RecordObjectNew returned NULL without it),
and have both callers branch on errno. Document the contract in types.h.

Verified by injecting calloc failures at every allocation site in list
and diff: all of them now surface as ENOMEM, and the parse diagnostics
for the SPEC section 15 cases are unchanged.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018KyCHPAxWzqa3vHKdXJkgx
2026-08-26 15:20:20 +02:00
Fabio Scotto di Santolo
594dcfe62c Reset errno adjacent to readdir, not at the bottom of the loop
readdir returns NULL both at end of directory and on error, so the loop
tells the two apart with a post-loop errno test. The reset feeding that
test sat at the bottom of the body, where the `continue` on "." and ".."
skips it -- and those are typically the first two entries.

POSIX allows readdir to set errno on a successful call, so a stale value
could survive to the final NULL and make walk report a failure that never
happened. Move the reset next to the call it guards.

Verified unchanged against the previous build: identical scan output over
a 270-entry tree, 4125 entries over /usr/include, and the EACCES,
empty-directory and deep-nesting paths all behave as before.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018KyCHPAxWzqa3vHKdXJkgx
2026-08-26 15:18:27 +02:00
Fabio Scotto di Santolo
c6ffc7fbb9 Only unlink the temporary file when mkstemp actually created it
The cleanup path ran unlink(template) unconditionally. Until mkstemp
succeeds, template still holds the literal "/tmp/snapshot_XXXXXX", so any
earlier failure -- an unresolvable source directory, or mkstemp itself --
deleted whatever file happened to carry that name:

  $ echo victim > /tmp/snapshot_XXXXXX
  $ fsnap create /nonexistent out.fsnap
  create failed: No such file or directory
  $ test -f /tmp/snapshot_XXXXXX || echo gone
  gone

Guard it with fd != -1, which is true exactly when mkstemp turned the
suffix into a real name.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018KyCHPAxWzqa3vHKdXJkgx
2026-08-26 15:17:52 +02:00
Fabio Scotto di Santolo
3cce196045 Stop RecordObjectCompare from dereferencing a NULL target
RecordObjectWrite leaves target NULL for every entry that is not a
symlink, while RecordObjectUnpack always produces a string, empty in that
case. RecordObjectCompare handed both straight to strcmp.

Nothing reaches it today because diff only compares records parsed from
files, but comparing a record built from the live filesystem against the
one stored for the same path is exactly what verify() will do, and it
crashes as soon as the earlier fields match:

  Segmentation fault (core dumped)

Treat NULL and "" as the same value for both path and target.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018KyCHPAxWzqa3vHKdXJkgx
2026-08-26 15:16:33 +02:00
Fabio Scotto di Santolo
0e890aecdc Update README where the fixes changed documented behaviour
diff now reports snapshot:line: field for a malformed record instead of
failing with a bare errno, and accepts an empty snapshot. The build
already uses -O2, so drop it from the roadmap.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018KyCHPAxWzqa3vHKdXJkgx
2026-08-26 15:13:46 +02:00
Fabio Scotto di Santolo
1302d5a946 Build with -O2 and a stricter warning set, and fix what it found
Without optimisation GCC skips the flow analysis most of these warnings
are derived from, so -O2 is what makes the existing -W flags do their job.

The new flags found three real defects:

  -Wstrict-prototypes/-Wold-style-definition: init_crc32_table() and
  RecordObjectNew() were declared with (), which in C11 means "no
  prototype", not "no arguments". RecordObjectNew even contradicted its
  own header, which declares (void).

  -Wwrite-strings: walk's blacklist held string literals in a
  non-const char *[], so they were writable through the array.

Also make ignore_file size its loop with sizeof(blacklist[0]) instead of
repeating the element type, and drop the order-only build/ prerequisite
on the link rule, which the %.o rule's mkdir -p already covers.

The tree builds clean under all twelve flags.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018KyCHPAxWzqa3vHKdXJkgx
2026-08-26 15:13:08 +02:00
Fabio Scotto di Santolo
4aaaa8dd9c Accept an empty snapshot in diff and report malformed records properly
Three problems in load_in_memory, all visible from the command line:

An empty snapshot was treated as an error, though create() produces one
for an empty directory and list() accepts it. Diffing against it should
report every record of the other side as added or deleted.

A malformed record jumped to cleanup without setting errno and without
naming the offending line, so main() printed strerror() of whatever was
left over, normally 0:

  $ fsnap diff old.fsnap malformed.fsnap
  diff failed: Success

The enum Fields it passed to RecordObjectUnpack was uninitialised and
then discarded, even though list() uses the same out-parameter to say
which field failed.

Report parse failures the way list() does, with snapshot:line: field, and
give them the same distinct 128 status so main() does not append a second,
meaningless message. Guard qsort against the now-reachable NULL array, and
check fflush(stdout) as scan() already does.

  $ fsnap diff old.fsnap malformed.fsnap
  malformed.fsnap:3: unparsable line

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018KyCHPAxWzqa3vHKdXJkgx
2026-08-26 15:12:14 +02:00
Fabio Scotto di Santolo
7f0c18df76 Let a cleanup failure keep its own errno in copy and create
Both cleanup blocks saved errno on entry and restored it whenever rc had
become -1, including when it was the cleanup itself that had just failed.
On the success path the saved value is meaningless, so a genuine fclose
error was reported through a stale errno, usually 0:

  $ fsnap create dir /dev/full
  create failed: Success

A buffered write may succeed and only fail at flush or close, which is
exactly the case SPEC.md section 8 asks not to lose. Adopt the scheme
already used in list(): a pre-existing error keeps errno, a cleanup error
keeps its own, and either one yields -1.

  $ fsnap create dir /dev/full
  create failed: No space left on device

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018KyCHPAxWzqa3vHKdXJkgx
2026-08-26 15:11:06 +02:00
Fabio Scotto di Santolo
9e6581986e Close the fingerprint stream even when the read failed
RecordObjectWrite tested `ferror(f) || fclose(f) != 0`. When ferror was
true the short circuit skipped fclose, and the branch then set f to NULL,
disarming the cleanup that would have closed it. Every unreadable entry
leaked a file descriptor inside a walk that already holds one DIR* per
recursion level.

Measured on 200 records whose fopen succeeds but whose read fails
(a symlink to a directory): 207 descriptors held before, 7 after.

Sample ferror and close unconditionally, then report either failure.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018KyCHPAxWzqa3vHKdXJkgx
2026-08-26 15:10:05 +02:00
Fabio Scotto di Santolo
695a0f37d7 Read symlink targets with pathconf, and detect truncation
_PC_PATH_MAX belongs to pathconf, not sysconf. On glibc _PC_PATH_MAX and
_SC_OPEN_MAX are both 4, so sysconf(_PC_PATH_MAX) silently returned the
open-file limit (524288 here) and the fallback allocated 512KB per link.
Where the two constants do not collide sysconf returns -1, which becomes
SIZE_MAX and makes calloc fail, so create would break on every symlink
whose st_size is 0.

readlink also does not terminate the buffer and truncates silently when
the target does not fit, so check the returned length rather than only
testing for -1.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018KyCHPAxWzqa3vHKdXJkgx
2026-08-26 15:09:33 +02:00
Fabio Scotto di Santolo
4db8e32547 Compare record fields by ordering, not by subtraction
RecordObjectCompare computed its result as (int)(a - b) on uintmax_t and
intmax_t fields. That truncates: a difference which is an exact multiple
of 2^32 yields 0, so two records already known to differ compared equal
and diff dropped the change without any output.

  $ fsnap diff old.fsnap new.fsnap   # size 0 vs 4294967296
  $ echo $?
  0

The subtraction on intmax_t was also undefined on overflow. The explicit
casts are what kept -Wconversion quiet about all of it.

Replace them with cmp_intmax/cmp_uintmax helpers returning -1/0/1.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018KyCHPAxWzqa3vHKdXJkgx
2026-08-26 15:08:33 +02:00
Fabio Scotto di Santolo
c397728912 Unify error handling: output parameter, goto cleanup, errno preservation
- Replace global field variable with output parameter enum Fields *err
- Adopt single goto cleanup pattern across all functions
- Add errno preservation around cleanup in all modules
- Fix early return bug in create.c cleanup block
- Add ferror check in crc32() for silent read errors
- Document error contracts in all public headers
2026-08-26 14:20:00 +02:00
Fabio Scotto di Santolo
464bb58c8c Fix remaining naming and style inconsistencies
- Add const to RecordObjectCompare parameters
- Add static to crc32_table and table_computed in hash.c
- Rename NewRecordObject to RecordObjectNew
- Replace magic number 8192 with BUFFER_SIZE in utility.c
- Unify comment style to /* */ across all files
- Standardize _POSIX_C_SOURCE to 200809L
- Remove unreachable dead code in diff.c
2026-08-26 00:03:13 +02:00
Fabio Scotto di Santolo
a7f13e408b Fix naming inconsistencies: header guard, FieldName prefix, Operation typedef 2026-08-25 23:55:42 +02:00
Fabio Scotto di Santolo
ad062794d8 Apply RecordObject namespace to all methods 2026-08-25 23:45:56 +02:00
Fabio Scotto di Santolo
53e13e9c51 Refactor create command using RecordObject 2026-08-25 23:25:55 +02:00
Fabio Scotto di Santolo
dcd17889ac Implement snapshot diff command
Add fsnap diff <old> <new> that compares two snapshots and reports
added (A), deleted (D), metadata changed (M), permissions changed (P),
and symlink target changed (L) records.

Both snapshots are loaded into memory, sorted by path using qsort,
and merged with a two-pointer algorithm. Residual records after the
merge are emitted as A or D. Escape sequences in paths are decoded
on output.

Also add GetTime accessor, set errno on load_in_memory failures
for meaningful error messages, and fix unused-variable warnings.
2026-08-25 18:13:29 +02:00
Fabio Scotto di Santolo
f08692679f Update README.md 2026-08-23 18:43:04 +02:00
Fabio Scotto di Santolo
e5f5d559bc Add CRC-32 fingerprinting of symlink targets
Hash the followed target content with CRC-32 (hash.c/hash.h) and emit a FINGERPRINT field for non-directory records in create.c. Schema: TYPE|MODE|UID|GID|MTIME|SIZE|FINGERPRINT|TARGET per SPEC §17/§18.
2026-08-23 17:47:09 +02:00
Fabio Scotto di Santolo
9df9223b63 Extract snapshot parser into a reusable types module
Splits RecordObject, unpack, release, field and field_name out of list.c into types.c/types.h; declares field extern in the header with a single definition in types.c; adds src/types.c to SOURCES.
2026-08-21 00:00:23 +02:00
Fabio Scotto di Santolo
3d63af4202 Escape the path field so unusual filenames survive a round trip
A filename may contain any byte except NUL and '/', so it may contain the
field delimiter or a newline. Until now create wrote such paths verbatim:
a name with '|' shifted the field layout and a name with a newline split
the record in two, and list could only reject the result.

create now escapes the path before writing it -- '\' as '\\', '|' as
'\|', and a newline as '\n' -- and list decodes it back. The backslash
has to be escaped as well: without it '\|' would be ambiguous between an
escaped delimiter and a literal backslash followed by a delimiter.

Splitting stays escape-unaware on purpose. Only the path may hold an
escaped delimiter and it is the last of the seven fields, so the six
separators found while splitting are always the real ones; the check that
used to reject a delimiter in the last field moves into the unescaper,
which has the context to tell a valid escape from a malformed one. It
rejects a bare delimiter, an unknown escape, and a trailing backslash,
and reports them as an invalid path field rather than an unparsable line.

walk guards the d_type fast path with #ifdef DT_UNKNOWN and falls back to
the lstat path when the constants are unavailable, which SPEC 4.1 already
requires and which POSIX-only feature test macros need, since d_type is a
BSD extension rather than part of POSIX.

Verified by decoding every record of three trees -- one with pipes,
backslashes and a newline in the names, the SPEC 22 tree, and this
repository -- and confirming lstat finds all 315 decoded paths.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F3NNME8GifswDYLr9VpBzs
2026-08-16 22:47:59 +02:00
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
Fabio Scotto di Santolo
ca660bc9e7 Merge branch 'feature/list-validation' 2026-08-06 23:35:29 +02:00
Fabio Scotto di Santolo
60d2af64e2 New feature list with record validation 2026-08-06 23:34:56 +02:00
Fabio Scotto di Santolo
b542113252 Add list command [WIP] 2026-08-05 22:07:21 +02:00
Fabio Scotto di Santolo
bb049d57a9 Rewrite README to describe the tool as it actually is
The previous text advertised inode data, hard-link tracking, snapshot
comparison and verification, none of which exist. Replace it with what the
code does today: the scan and create commands, the seven-field record
format, and the behaviour around symlinks, absolute paths, unreadable
directories and the .fsnap filter.

State plainly that this is a hobby project and not production ready, list
the limitations that are known and reproducible — non-atomic output, one
directory handle per recursion level, no exit code for a partial snapshot,
no quoting for paths containing a pipe or a newline, no hard-link data, no
tests — and record the intended work as a roadmap.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Mbj1ZPUEhxqMYKsK7Y7jkt
2026-08-01 22:55:06 +02:00
Fabio Scotto di Santolo
1ddc085160 Skip unreadable directories instead of aborting the snapshot
opendir() failing with EACCES now warns on stderr and lets the walk carry
on with the rest of the tree. The record for the directory itself is still
written, since the operation runs before the recursion, so its metadata
survives even when its contents cannot be listed. Every other opendir
error still propagates.

Also check the return value of fprintf when writing a record, close the
descriptor when fdopen fails, and move the strings.h include to the file
that actually calls strcasecmp.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Mbj1ZPUEhxqMYKsK7Y7jkt
2026-08-01 21:26:09 +02:00
Fabio Scotto di Santolo
777f14a9e9 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
2026-08-01 18:08:26 +02:00
Fabio Scotto di Santolo
3eae528f54 Add create command 2026-08-01 14:27:52 +02:00
Fabio Scotto di Santolo
462aa6e5bf Refactor operation function definition 2026-08-01 08:54:20 +02:00
Fabio Scotto di Santolo
078b95dc4b Add function scan directory 2026-07-31 23:54:45 +02:00
Fabio Scotto di Santolo
af56ec524d Initial commit 2026-07-31 14:40:53 +02:00