From 0417bd825a0939aa2252307b45484ee5a8090c80 Mon Sep 17 00:00:00 2001 From: Fabio Scotto di Santolo Date: Wed, 26 Aug 2026 16:01:06 +0200 Subject: [PATCH] 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 Claude-Session: https://claude.ai/code/session_018KyCHPAxWzqa3vHKdXJkgx --- README.md | 12 ++++++------ include/hash.h | 6 ++++++ src/hash.c | 7 ++++++- src/types.c | 35 +++++++++++++++++++++-------------- 4 files changed, 39 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index d96b5f1..9281eb5 100644 --- a/README.md +++ b/README.md @@ -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 of the contents, for regular files only; `0` for every other type, including symlinks, whose contents are the `target` field | +| `fingerprint` | CRC32 of the contents for a regular file, and of the target string for a symlink, which is what the link holds. `0` for a directory and for the types that cannot be read at all (FIFO, socket, device) | | `path` | Absolute path: the input root is resolved with `realpath`, then child names are appended; it is escaped | ### Escaping rules @@ -131,7 +131,7 @@ pathname: | `A` | Present only in the new snapshot | | `D` | Present only in the old snapshot | | `P` | Permissions are the first differing field | -| `L` | Symlink target is the only differing field after the other compared fields match | +| `L` | The symlink target changed. Checked before the fingerprint, which for a link is derived from the target, so the specific answer wins over the generic one | | `M` | Any other difference, including type, owner, size, time, or fingerprint | `diff` reports a malformed input the way `list` does, as @@ -140,10 +140,10 @@ returns `128` internally; `main` maps it to exit status `1`. ## Behaviour worth knowing -- **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. +- **Symlinks are not followed, anywhere.** Their metadata comes from `lstat()`, + their target from `readlink()`, and their fingerprint from that target string + rather than from whatever it points at. Nothing opens the path, so a link to a + directory and a broken link both snapshot like any other entry. - **Paths are absolute.** The input directory is resolved through `realpath()` before traversal; snapshots are tied to the machine and location where they were taken. diff --git a/include/hash.h b/include/hash.h index dfe83ce..a26bee6 100644 --- a/include/hash.h +++ b/include/hash.h @@ -1,9 +1,15 @@ #ifndef HASH_H #define HASH_H +#include #include #include +/* Compute the CRC-32 checksum of a memory buffer. Cannot fail, so the result + is returned directly. Used for a symlink, whose content is the target string + readlink reports rather than anything reachable through the filesystem. */ +uint32_t crc32_buffer(const void *data, size_t length); + /* Compute the CRC-32 checksum of the stream's remaining contents into *out. Returns 0 on success, -1 on read error, leaving *out untouched. The result is reported through an out parameter because 0 is a perfectly good checksum diff --git a/src/hash.c b/src/hash.c index d0d0137..01f3bc5 100644 --- a/src/hash.c +++ b/src/hash.c @@ -21,7 +21,8 @@ static void init_crc32_table(void) { table_computed = 1; } -static uint32_t crc32_update(uint32_t crc, const uint8_t *data, size_t length) { +static uint32_t crc32_update(uint32_t crc, const void *bytes, size_t length) { + const uint8_t *data = bytes; if (!table_computed) { init_crc32_table(); } @@ -34,6 +35,10 @@ static uint32_t crc32_update(uint32_t crc, const uint8_t *data, size_t length) { return crc; } +uint32_t crc32_buffer(const void *data, size_t length) { + return crc32_update(0xFFFFFFFF, data, length) ^ 0xFFFFFFFF; +} + int crc32(FILE *stream, uint32_t *out) { uint8_t buffer[BUFFER_SIZE]; size_t bytes_read; diff --git a/src/types.c b/src/types.c index 9c662f3..d02c6bb 100644 --- a/src/types.c +++ b/src/types.c @@ -346,6 +346,9 @@ int RecordObjectCompare(const struct RecordObject *self, if (other == NULL) return 1; + const int target_cmp = + strcmp(or_empty(self->target), or_empty(other->target)); + int exit_code = 0; if (self->file_type != other->file_type) { exit_code = cmp_intmax(self->file_type, other->file_type); @@ -371,6 +374,14 @@ int RecordObjectCompare(const struct RecordObject *self, exit_code = cmp_intmax(self->time, other->time); if (err) *err = TIME; + } else if (target_cmp != 0) { + /* Checked before the fingerprint, which for a symlink is derived from this + very string: comparing the other way round would answer "the checksum + changed" when the specific and useful answer is "the target changed". + The fields are now compared in the order the record stores them. */ + exit_code = target_cmp; + if (err) + *err = TARGET; } else if (self->fingerprint != other->fingerprint) { exit_code = cmp_uintmax(self->fingerprint, other->fingerprint); if (err) @@ -381,13 +392,6 @@ int RecordObjectCompare(const struct RecordObject *self, exit_code = retp; if (err) *err = PATH; - } else { - int rett = strcmp(or_empty(self->target), or_empty(other->target)); - if (rett != 0) { - exit_code = rett; - if (err) - *err = TARGET; - } } } @@ -453,14 +457,17 @@ 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. */ + /* A regular file is checksummed by reading it. A symlink is checksummed over + the target string, which is what the link actually holds: opening the path + instead would follow it, and a link to a directory then fails with EISDIR, + a broken one with ENOENT. Directories have no contents to speak of, and + the remaining types cannot be read at all -- 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 -- so all of those keep 0. */ uint32_t hash = 0; - if (S_ISREG(st.st_mode)) { + if (S_ISLNK(st.st_mode)) { + hash = crc32_buffer(self->target, strlen(self->target)); + } else if (S_ISREG(st.st_mode)) { if ((f = fopen(path, "r")) == NULL) goto cleanup;