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
This commit is contained in:
Fabio Scotto di Santolo
2026-08-26 15:51:18 +02:00
parent 25b328ba79
commit e2b45e6ca8
2 changed files with 17 additions and 10 deletions

View File

@@ -108,7 +108,7 @@ type|permissions|uid|gid|size|time|target|fingerprint|path
| `size` | Size in bytes, as reported by `lstat` |
| `time` | Modification time, seconds since the Unix epoch |
| `target` | Symlink target returned by `readlink`; empty for non-symlink entries |
| `fingerprint` | CRC32 checksum read through `fopen(path, "r")` for every non-directory entry; `0` for directories |
| `fingerprint` | CRC32 checksum of the contents, for regular files only; `0` for every other type, including symlinks, whose contents are the `target` field |
| `path` | Absolute path: the input root is resolved with `realpath`, then child names are appended; it is escaped |
### Escaping rules
@@ -140,10 +140,10 @@ returns `128` internally; `main` maps it to exit status `1`.
## Behaviour worth knowing
- **Symlinks are not followed by the walker.** Their metadata comes from
`lstat()` and their target is read using `readlink()`. The subsequent
fingerprint step opens the symlink target, however, so `create` fails for a
broken symlink and fingerprints the referent rather than the link target.
- **Symlinks are not followed, anywhere.** Their metadata comes from `lstat()`
and their target from `readlink()`. Nothing opens the path afterwards, so a
symlink is recorded as itself rather than as its referent, and a broken one
snapshots like any other.
- **Paths are absolute.** The input directory is resolved through `realpath()`
before traversal; snapshots are tied to the machine and location where they
were taken.
@@ -185,10 +185,11 @@ These are actual, reproduced problems, not hypotheticals:
is unreadable, the result is an empty snapshot reported as success.
- **No hard-link or inode information** is recorded, so hard links cannot be
detected and identical files cannot be correlated.
- **`create` is unsafe for several non-regular entry types.** It attempts to
open every non-directory entry to fingerprint it. A FIFO can block forever;
devices, sockets, unreadable files, and broken symlinks can make creation
fail.
- **An unreadable regular file aborts the whole run.** `create` opens every
regular file to checksum it, and a failure there stops the walk and leaves no
snapshot, even though the same run tolerates a directory it cannot enter.
Running `fsnap create /etc` as an ordinary user fails on the first of the 39
files it may not read.
- **Symlink targets are not escaped by the writer.** A target containing `|`, a
newline, or a backslash can make the generated record unparsable by `list`.
- **`list` counts the lines in a separate pass over the file.** If the snapshot

View File

@@ -453,8 +453,14 @@ int RecordObjectWrite(struct RecordObject *self, const char *path, FILE *out) {
self->target = target;
target = NULL;
/* Only a regular file has contents worth checksumming, and only a regular
file is safe to open: fopen follows symlinks, so a link to a directory
fails with EISDIR and a broken one with ENOENT; a FIFO blocks until a
writer appears; a socket fails with ENXIO; and opening a device is not
something a metadata walk should ever do. A symlink is already fully
described by the target field. Everything else keeps fingerprint 0. */
uint32_t hash = 0;
if (!S_ISDIR(st.st_mode)) {
if (S_ISREG(st.st_mode)) {
if ((f = fopen(path, "r")) == NULL)
goto cleanup;