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.
This commit is contained in:
Fabio Scotto di Santolo
2026-08-23 17:47:09 +02:00
parent 9df9223b63
commit e5f5d559bc
6 changed files with 137 additions and 7 deletions

View File

@@ -12,6 +12,7 @@ SOURCES := \
src/main.c \ src/main.c \
src/utility.c \ src/utility.c \
src/types.c \ src/types.c \
src/hash.c \
src/walk.c \ src/walk.c \
src/scan.c \ src/scan.c \
src/create.c \ src/create.c \

9
include/hash.h Normal file
View File

@@ -0,0 +1,9 @@
#ifndef HASH_H
#define HASH_H
#include <stdint.h>
#include <stdio.h>
uint32_t crc32(FILE *stream);
#endif

View File

@@ -8,6 +8,8 @@ enum Fields {
GID, GID,
SIZE, SIZE,
TIME, TIME,
TARGET,
FINGERPRINT,
PATH, PATH,
NONE, NONE,
}; };

View File

@@ -1,11 +1,14 @@
#define _POSIX_C_SOURCE 1
#define _DEFAULT_SOURCE #define _DEFAULT_SOURCE
#define _XOPEN_SOURCE 700 #define _XOPEN_SOURCE 700
#include "create.h" #include "create.h"
#include "hash.h"
#include "utility.h" #include "utility.h"
#include "walk.h" #include "walk.h"
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <inttypes.h> #include <inttypes.h>
#include <limits.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -90,12 +93,54 @@ static int write_record(const char *path, void *context) {
if (s == NULL) if (s == NULL)
return -1; return -1;
int ret = fprintf(snapshot, "%c|%04o|%ju|%ju|%jd|%jd|%s\n", type, perm, uid, char *target = NULL;
gid, size, time, s); if (type == 'L') {
size_t bufsiz = ((size_t)(st.st_size + 1));
if (st.st_size == 0)
bufsiz = (size_t)sysconf(_PC_PATH_MAX);
target = calloc(bufsiz, sizeof(char));
if (target == NULL)
goto failure;
if (readlink(path, target, bufsiz) == -1)
goto failure;
}
FILE *f = NULL;
uint32_t hash = 0;
if (!S_ISDIR(st.st_mode)) {
if ((f = fopen(path, "r")) == NULL)
goto failure;
hash = crc32(f);
}
int ret =
fprintf(snapshot, "%c|%04o|%ju|%ju|%jd|%jd|%s|%u|%s\n", type, perm, uid,
gid, size, time, (target == NULL ? "" : target), hash, s);
free(s); free(s);
if (target != NULL)
free(target);
if (f != NULL)
fclose(f);
return ret < 0 ? -1 : 0; return ret < 0 ? -1 : 0;
failure:
free(s);
if (target != NULL)
free(target);
if (f != NULL)
fclose(f);
return -1;
} }
int create(const char *directory, const char *output_file) { int create(const char *directory, const char *output_file) {
@@ -138,6 +183,7 @@ int create(const char *directory, const char *output_file) {
goto out; goto out;
} }
// FIXME: It doesn't atomic copy, using rename
if (copy(template, output_file) == -1) { if (copy(template, output_file) == -1) {
rc = -1; rc = -1;
goto out; goto out;

47
src/hash.c Normal file
View File

@@ -0,0 +1,47 @@
#include "hash.h"
#define CRC32_POLY 0xEDB88320
#define BUFFER_SIZE 4096
uint32_t crc32_table[256];
int table_computed = 0;
static void init_crc32_table() {
for (uint32_t i = 0; i < 256; i++) {
uint32_t crc = i;
for (int j = 0; j < 8; j++) {
if (crc & 1) {
crc = (crc >> 1) ^ CRC32_POLY;
} else {
crc >>= 1;
}
}
crc32_table[i] = crc;
}
table_computed = 1;
}
static uint32_t crc32_update(uint32_t crc, const uint8_t *data, size_t length) {
if (!table_computed) {
init_crc32_table();
}
for (size_t i = 0; i < length; i++) {
uint8_t lookup_index = (crc ^ data[i]) & 0xFF;
crc = (crc >> 8) ^ crc32_table[lookup_index];
}
return crc;
}
uint32_t crc32(FILE *stream) {
uint8_t buffer[BUFFER_SIZE];
size_t bytes_read;
uint32_t crc = 0xFFFFFFFF;
while ((bytes_read = fread(buffer, 1, BUFFER_SIZE, stream)) > 0) {
crc = crc32_update(crc, buffer, bytes_read);
}
return crc ^ 0xFFFFFFFF;
}

View File

@@ -5,12 +5,13 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#define NFIELDS 7 #define NFIELDS 9
enum Fields field = NONE; enum Fields field = NONE;
static const char *field_names[] = {"file type", "permissions", "uid", "gid", static const char *field_names[] = {
"size", "time", "path", "none"}; "file type", "permissions", "uid", "gid", "size",
"time", "target", "fingerprint", "path", "none"};
const char *field_name(enum Fields f) { const char *field_name(enum Fields f) {
return (f <= NONE) ? field_names[f] : "unknown"; return (f <= NONE) ? field_names[f] : "unknown";
@@ -177,6 +178,8 @@ struct RecordObject {
intmax_t size; intmax_t size;
intmax_t time; intmax_t time;
char *path; char *path;
char *target; /* only for symlink */
uintmax_t fingerprint;
}; };
struct RecordObject *unpack(char *s) { struct RecordObject *unpack(char *s) {
@@ -221,13 +224,31 @@ struct RecordObject *unpack(char *s) {
return objp; return objp;
} }
if (fields[6][0] == '\0') { errno = 0;
char *target = unescape_path(fields[6], '|');
if (target == NULL) {
if (errno == EINVAL) {
field = TARGET;
return objp;
}
release(objp);
return NULL;
}
objp->target = target;
if (parse_uintmax(fields[7], &objp->fingerprint) == -1) {
field = FINGERPRINT;
return objp;
}
if (fields[8][0] == '\0') {
field = PATH; field = PATH;
return objp; return objp;
} }
errno = 0; errno = 0;
char *path = unescape_path(fields[6], '|'); char *path = unescape_path(fields[8], '|');
if (path == NULL) { if (path == NULL) {
if (errno == EINVAL) { if (errno == EINVAL) {
field = PATH; field = PATH;
@@ -250,6 +271,10 @@ int release(struct RecordObject *obj) {
free(obj->path); free(obj->path);
obj->path = NULL; obj->path = NULL;
} }
if (obj->target != NULL) {
free(obj->target);
obj->target = NULL;
}
free(obj); free(obj);
return 0; return 0;