mirror of
https://github.com/fscotto/fsnap.git
synced 2026-11-26 11:19:52 +00:00
Checksum a symlink over its target string
e2b45e6left 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 beforee2b45e6and 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
This commit is contained in:
12
README.md
12
README.md
@@ -108,7 +108,7 @@ type|permissions|uid|gid|size|time|target|fingerprint|path
|
|||||||
| `size` | Size in bytes, as reported by `lstat` |
|
| `size` | Size in bytes, as reported by `lstat` |
|
||||||
| `time` | Modification time, seconds since the Unix epoch |
|
| `time` | Modification time, seconds since the Unix epoch |
|
||||||
| `target` | Symlink target returned by `readlink`; empty for non-symlink entries |
|
| `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 |
|
| `path` | Absolute path: the input root is resolved with `realpath`, then child names are appended; it is escaped |
|
||||||
|
|
||||||
### Escaping rules
|
### Escaping rules
|
||||||
@@ -131,7 +131,7 @@ pathname:
|
|||||||
| `A` | Present only in the new snapshot |
|
| `A` | Present only in the new snapshot |
|
||||||
| `D` | Present only in the old snapshot |
|
| `D` | Present only in the old snapshot |
|
||||||
| `P` | Permissions are the first differing field |
|
| `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 |
|
| `M` | Any other difference, including type, owner, size, time, or fingerprint |
|
||||||
|
|
||||||
`diff` reports a malformed input the way `list` does, as
|
`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
|
## Behaviour worth knowing
|
||||||
|
|
||||||
- **Symlinks are not followed, anywhere.** Their metadata comes from `lstat()`
|
- **Symlinks are not followed, anywhere.** Their metadata comes from `lstat()`,
|
||||||
and their target from `readlink()`. Nothing opens the path afterwards, so a
|
their target from `readlink()`, and their fingerprint from that target string
|
||||||
symlink is recorded as itself rather than as its referent, and a broken one
|
rather than from whatever it points at. Nothing opens the path, so a link to a
|
||||||
snapshots like any other.
|
directory and a broken link both snapshot like any other entry.
|
||||||
- **Paths are absolute.** The input directory is resolved through `realpath()`
|
- **Paths are absolute.** The input directory is resolved through `realpath()`
|
||||||
before traversal; snapshots are tied to the machine and location where they
|
before traversal; snapshots are tied to the machine and location where they
|
||||||
were taken.
|
were taken.
|
||||||
|
|||||||
@@ -1,9 +1,15 @@
|
|||||||
#ifndef HASH_H
|
#ifndef HASH_H
|
||||||
#define HASH_H
|
#define HASH_H
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
/* 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.
|
/* 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
|
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
|
is reported through an out parameter because 0 is a perfectly good checksum
|
||||||
|
|||||||
@@ -21,7 +21,8 @@ static void init_crc32_table(void) {
|
|||||||
table_computed = 1;
|
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) {
|
if (!table_computed) {
|
||||||
init_crc32_table();
|
init_crc32_table();
|
||||||
}
|
}
|
||||||
@@ -34,6 +35,10 @@ static uint32_t crc32_update(uint32_t crc, const uint8_t *data, size_t length) {
|
|||||||
return crc;
|
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) {
|
int crc32(FILE *stream, uint32_t *out) {
|
||||||
uint8_t buffer[BUFFER_SIZE];
|
uint8_t buffer[BUFFER_SIZE];
|
||||||
size_t bytes_read;
|
size_t bytes_read;
|
||||||
|
|||||||
35
src/types.c
35
src/types.c
@@ -346,6 +346,9 @@ int RecordObjectCompare(const struct RecordObject *self,
|
|||||||
if (other == NULL)
|
if (other == NULL)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
|
const int target_cmp =
|
||||||
|
strcmp(or_empty(self->target), or_empty(other->target));
|
||||||
|
|
||||||
int exit_code = 0;
|
int exit_code = 0;
|
||||||
if (self->file_type != other->file_type) {
|
if (self->file_type != other->file_type) {
|
||||||
exit_code = cmp_intmax(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);
|
exit_code = cmp_intmax(self->time, other->time);
|
||||||
if (err)
|
if (err)
|
||||||
*err = TIME;
|
*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) {
|
} else if (self->fingerprint != other->fingerprint) {
|
||||||
exit_code = cmp_uintmax(self->fingerprint, other->fingerprint);
|
exit_code = cmp_uintmax(self->fingerprint, other->fingerprint);
|
||||||
if (err)
|
if (err)
|
||||||
@@ -381,13 +392,6 @@ int RecordObjectCompare(const struct RecordObject *self,
|
|||||||
exit_code = retp;
|
exit_code = retp;
|
||||||
if (err)
|
if (err)
|
||||||
*err = PATH;
|
*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;
|
self->target = target;
|
||||||
target = NULL;
|
target = NULL;
|
||||||
|
|
||||||
/* Only a regular file has contents worth checksumming, and only a regular
|
/* A regular file is checksummed by reading it. A symlink is checksummed over
|
||||||
file is safe to open: fopen follows symlinks, so a link to a directory
|
the target string, which is what the link actually holds: opening the path
|
||||||
fails with EISDIR and a broken one with ENOENT; a FIFO blocks until a
|
instead would follow it, and a link to a directory then fails with EISDIR,
|
||||||
writer appears; a socket fails with ENXIO; and opening a device is not
|
a broken one with ENOENT. Directories have no contents to speak of, and
|
||||||
something a metadata walk should ever do. A symlink is already fully
|
the remaining types cannot be read at all -- a FIFO blocks until a writer
|
||||||
described by the target field. Everything else keeps fingerprint 0. */
|
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;
|
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)
|
if ((f = fopen(path, "r")) == NULL)
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user