diff --git a/Makefile b/Makefile index 39b8edd..e7d4b84 100644 --- a/Makefile +++ b/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 \ diff --git a/include/hash.h b/include/hash.h new file mode 100644 index 0000000..1b6451b --- /dev/null +++ b/include/hash.h @@ -0,0 +1,9 @@ +#ifndef HASH_H +#define HASH_H + +#include +#include + +uint32_t crc32(FILE *stream); + +#endif diff --git a/include/types.h b/include/types.h index 1954405..a0d5ba1 100644 --- a/include/types.h +++ b/include/types.h @@ -8,6 +8,8 @@ enum Fields { GID, SIZE, TIME, + TARGET, + FINGERPRINT, PATH, NONE, }; diff --git a/src/create.c b/src/create.c index 3248eba..1ca6e06 100644 --- a/src/create.c +++ b/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 #include #include +#include #include #include #include @@ -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; diff --git a/src/hash.c b/src/hash.c new file mode 100644 index 0000000..53b7696 --- /dev/null +++ b/src/hash.c @@ -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; +} diff --git a/src/types.c b/src/types.c index 8c860c5..7e5e9dd 100644 --- a/src/types.c +++ b/src/types.c @@ -5,12 +5,13 @@ #include #include -#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;