mirror of
https://github.com/fscotto/fsnap.git
synced 2026-11-26 19:29:50 +00:00
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:
1
Makefile
1
Makefile
@@ -12,6 +12,7 @@ SOURCES := \
|
||||
src/main.c \
|
||||
src/utility.c \
|
||||
src/types.c \
|
||||
src/hash.c \
|
||||
src/walk.c \
|
||||
src/scan.c \
|
||||
src/create.c \
|
||||
|
||||
9
include/hash.h
Normal file
9
include/hash.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#ifndef HASH_H
|
||||
#define HASH_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
uint32_t crc32(FILE *stream);
|
||||
|
||||
#endif
|
||||
@@ -8,6 +8,8 @@ enum Fields {
|
||||
GID,
|
||||
SIZE,
|
||||
TIME,
|
||||
TARGET,
|
||||
FINGERPRINT,
|
||||
PATH,
|
||||
NONE,
|
||||
};
|
||||
|
||||
50
src/create.c
50
src/create.c
@@ -1,11 +1,14 @@
|
||||
#define _POSIX_C_SOURCE 1
|
||||
#define _DEFAULT_SOURCE
|
||||
#define _XOPEN_SOURCE 700
|
||||
#include "create.h"
|
||||
#include "hash.h"
|
||||
#include "utility.h"
|
||||
#include "walk.h"
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <inttypes.h>
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -90,12 +93,54 @@ static int write_record(const char *path, void *context) {
|
||||
if (s == NULL)
|
||||
return -1;
|
||||
|
||||
int ret = fprintf(snapshot, "%c|%04o|%ju|%ju|%jd|%jd|%s\n", type, perm, uid,
|
||||
gid, size, time, s);
|
||||
char *target = NULL;
|
||||
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);
|
||||
|
||||
if (target != NULL)
|
||||
free(target);
|
||||
|
||||
if (f != NULL)
|
||||
fclose(f);
|
||||
|
||||
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) {
|
||||
@@ -138,6 +183,7 @@ int create(const char *directory, const char *output_file) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
// FIXME: It doesn't atomic copy, using rename
|
||||
if (copy(template, output_file) == -1) {
|
||||
rc = -1;
|
||||
goto out;
|
||||
|
||||
47
src/hash.c
Normal file
47
src/hash.c
Normal 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;
|
||||
}
|
||||
35
src/types.c
35
src/types.c
@@ -5,12 +5,13 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define NFIELDS 7
|
||||
#define NFIELDS 9
|
||||
|
||||
enum Fields field = NONE;
|
||||
|
||||
static const char *field_names[] = {"file type", "permissions", "uid", "gid",
|
||||
"size", "time", "path", "none"};
|
||||
static const char *field_names[] = {
|
||||
"file type", "permissions", "uid", "gid", "size",
|
||||
"time", "target", "fingerprint", "path", "none"};
|
||||
|
||||
const char *field_name(enum Fields f) {
|
||||
return (f <= NONE) ? field_names[f] : "unknown";
|
||||
@@ -177,6 +178,8 @@ struct RecordObject {
|
||||
intmax_t size;
|
||||
intmax_t time;
|
||||
char *path;
|
||||
char *target; /* only for symlink */
|
||||
uintmax_t fingerprint;
|
||||
};
|
||||
|
||||
struct RecordObject *unpack(char *s) {
|
||||
@@ -221,13 +224,31 @@ struct RecordObject *unpack(char *s) {
|
||||
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;
|
||||
return objp;
|
||||
}
|
||||
|
||||
errno = 0;
|
||||
char *path = unescape_path(fields[6], '|');
|
||||
char *path = unescape_path(fields[8], '|');
|
||||
if (path == NULL) {
|
||||
if (errno == EINVAL) {
|
||||
field = PATH;
|
||||
@@ -250,6 +271,10 @@ int release(struct RecordObject *obj) {
|
||||
free(obj->path);
|
||||
obj->path = NULL;
|
||||
}
|
||||
if (obj->target != NULL) {
|
||||
free(obj->target);
|
||||
obj->target = NULL;
|
||||
}
|
||||
|
||||
free(obj);
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user