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
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
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
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
- 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
- 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
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.
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.
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.
- 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