Commit Graph

21 Commits

Author SHA1 Message Date
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
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
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
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
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
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
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
b542113252 Add list command [WIP] 2026-08-05 22:07:21 +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