From 9df9223b63b604ca95bd01959e978d52793d8137 Mon Sep 17 00:00:00 2001 From: Fabio Scotto di Santolo Date: Fri, 21 Aug 2026 00:00:23 +0200 Subject: [PATCH] Extract snapshot parser into a reusable types module Splits RecordObject, unpack, release, field and field_name out of list.c into types.c/types.h; declares field extern in the header with a single definition in types.c; adds src/types.c to SOURCES. --- Makefile | 1 + include/list.h | 4 - include/types.h | 23 +++++ src/list.c | 262 +----------------------------------------------- src/types.c | 256 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 281 insertions(+), 265 deletions(-) create mode 100644 include/types.h create mode 100644 src/types.c diff --git a/Makefile b/Makefile index 16eabf5..39b8edd 100644 --- a/Makefile +++ b/Makefile @@ -11,6 +11,7 @@ TARGET := $(BUILD_DIR)/fsnap SOURCES := \ src/main.c \ src/utility.c \ + src/types.c \ src/walk.c \ src/scan.c \ src/create.c \ diff --git a/include/list.h b/include/list.h index 5d3263a..8efc4fd 100644 --- a/include/list.h +++ b/include/list.h @@ -1,10 +1,6 @@ #ifndef LIST_H #define LIST_H -struct RecordObject; -struct RecordObject *unpack(char *s); -int release(struct RecordObject *obj); - int list(const char *snapshot); #endif diff --git a/include/types.h b/include/types.h new file mode 100644 index 0000000..1954405 --- /dev/null +++ b/include/types.h @@ -0,0 +1,23 @@ +#ifndef TYPE_H +#define TYPE_H + +enum Fields { + FILE_TYPE, + PERMISSIONS, + UID, + GID, + SIZE, + TIME, + PATH, + NONE, +}; + +extern enum Fields field; + +const char *field_name(enum Fields f); + +struct RecordObject; +struct RecordObject *unpack(char *s); +int release(struct RecordObject *obj); + +#endif diff --git a/src/list.c b/src/list.c index 5ee67e4..e8da06a 100644 --- a/src/list.c +++ b/src/list.c @@ -1,272 +1,12 @@ #define _POSIX_C_SOURCE 200809L #include "list.h" +#include "types.h" #include #include #include #include #include -#define NFIELDS 7 - -enum Fields { - FILE_TYPE, - PERMISSIONS, - UID, - GID, - SIZE, - TIME, - PATH, - NONE, -}; - -static const char *field_names[] = {"file type", "permissions", "uid", "gid", - "size", "time", "path", "none"}; - -static enum Fields field = NONE; - -static const char *field_name(enum Fields f) { - return (f <= NONE) ? field_names[f] : "unknown"; -} - -static int split_record(char *line, char *fields[NFIELDS], char delim) { - char *p = line; - - line[strcspn(line, "\n")] = '\0'; - - for (size_t n = 0; n < NFIELDS - 1; n++) { - fields[n] = p; - - char *sep = strchr(p, delim); - if (sep == NULL) - return -1; - - *sep = '\0'; - p = sep + 1; - } - - /* Only the path may contain an escaped delimiter, and it is the last field, - so the six separators found above are always the real ones. Validating - what is left is the unescaper's job. */ - fields[NFIELDS - 1] = p; - - return 0; -} - -/* Undo the escaping create applies: \\ -> \, \| -> |, \n -> newline. A bare - delimiter, an unknown escape, or a trailing backslash means the record is - malformed; that is reported with EINVAL to keep it apart from allocation - failure. */ -static char *unescape_path(const char *s, char delim) { - const size_t len = strlen(s); - - char *out = calloc(len + 1, sizeof(char)); - if (out == NULL) - return NULL; - - size_t j = 0; - for (size_t i = 0; i < len; i++) { - if (s[i] == delim) - goto malformed; - - if (s[i] != '\\') { - out[j++] = s[i]; - continue; - } - - /* s[len] is the terminator, so a trailing backslash lands on the - default case rather than reading past the end. */ - i++; - switch (s[i]) { - case '\\': - out[j++] = '\\'; - break; - case 'n': - out[j++] = '\n'; - break; - default: - if (s[i] != delim) - goto malformed; - out[j++] = delim; - break; - } - } - - return out; - -malformed: - free(out); - errno = EINVAL; - return NULL; -} - -static int parse_file_type(const char *s, char *out) { - if (s == NULL || s[0] == '\0' || s[1] != '\0') - return -1; - - char c = s[0]; - switch (c) { - case 'F': - case 'D': - case 'L': - case 'P': - case 'C': - case 'B': - case 'S': - case '?': - *out = c; - return 0; - default: - return -1; - } -} - -/* strtol and friends accept leading whitespace, a sign, and wrap negative - input around for the unsigned variants. Reject anything that is not a bare - run of decimal digits before converting. */ -static int only_digits(const char *s, int base) { - const char last = (base == 8) ? '7' : '9'; - - if (s == NULL || *s == '\0') - return 0; - - for (const char *p = s; *p != '\0'; p++) { - if (*p < '0' || *p > last) - return 0; - } - return 1; -} - -static int parse_perm(const char *s, unsigned int *out) { - char *end = NULL; - - if (s == NULL || strlen(s) != 4 || !only_digits(s, 8)) - return -1; - - errno = 0; - unsigned long v = strtoul(s, &end, 8); - if (*end != '\0' || errno == ERANGE || v > 07777) - return -1; - - *out = (unsigned int)v; - return 0; -} - -static int parse_uintmax(const char *s, uintmax_t *out) { - char *end = NULL; - - if (!only_digits(s, 10)) - return -1; - - errno = 0; - uintmax_t v = strtoumax(s, &end, 10); - if (*end != '\0' || errno == ERANGE) - return -1; - - *out = v; - return 0; -} - -static int parse_intmax_nonnegative(const char *s, intmax_t *out) { - char *end = NULL; - - if (!only_digits(s, 10)) - return -1; - - errno = 0; - intmax_t v = strtoimax(s, &end, 10); - if (*end != '\0' || errno == ERANGE || v < 0) - return -1; - - *out = v; - return 0; -} - -struct RecordObject { - char file_type; - unsigned int perm; - uintmax_t uid; - uintmax_t gid; - intmax_t size; - intmax_t time; - char *path; -}; - -struct RecordObject *unpack(char *s) { - char *fields[NFIELDS]; - if (split_record(s, fields, '|') == -1) - return NULL; - - struct RecordObject *objp = calloc(1, sizeof(*objp)); - if (objp == NULL) { - return NULL; - } - - field = NONE; - - if (parse_file_type(fields[0], &objp->file_type) == -1) { - field = FILE_TYPE; - return objp; - } - - if (parse_perm(fields[1], &objp->perm) == -1) { - field = PERMISSIONS; - return objp; - } - - if (parse_uintmax(fields[2], &objp->uid) == -1) { - field = UID; - return objp; - } - - if (parse_uintmax(fields[3], &objp->gid) == -1) { - field = GID; - return objp; - } - - if (parse_intmax_nonnegative(fields[4], &objp->size) == -1) { - field = SIZE; - return objp; - } - - if (parse_intmax_nonnegative(fields[5], &objp->time) == -1) { - field = TIME; - return objp; - } - - if (fields[6][0] == '\0') { - field = PATH; - return objp; - } - - errno = 0; - char *path = unescape_path(fields[6], '|'); - if (path == NULL) { - if (errno == EINVAL) { - field = PATH; - return objp; - } - release(objp); - return NULL; - } - - objp->path = path; - - return objp; -} - -int release(struct RecordObject *obj) { - if (obj == NULL) { - return 0; - } - if (obj->path != NULL) { - free(obj->path); - obj->path = NULL; - } - - free(obj); - return 0; -} - static int count_lines(const char *file) { FILE *f = fopen(file, "r"); if (f == NULL) { diff --git a/src/types.c b/src/types.c new file mode 100644 index 0000000..8c860c5 --- /dev/null +++ b/src/types.c @@ -0,0 +1,256 @@ +#include "types.h" +#include +#include +#include +#include +#include + +#define NFIELDS 7 + +enum Fields field = NONE; + +static const char *field_names[] = {"file type", "permissions", "uid", "gid", + "size", "time", "path", "none"}; + +const char *field_name(enum Fields f) { + return (f <= NONE) ? field_names[f] : "unknown"; +} + +static int split_record(char *line, char *fields[NFIELDS], char delim) { + char *p = line; + + line[strcspn(line, "\n")] = '\0'; + + for (size_t n = 0; n < NFIELDS - 1; n++) { + fields[n] = p; + + char *sep = strchr(p, delim); + if (sep == NULL) + return -1; + + *sep = '\0'; + p = sep + 1; + } + + /* Only the path may contain an escaped delimiter, and it is the last field, + so the six separators found above are always the real ones. Validating + what is left is the unescaper's job. */ + fields[NFIELDS - 1] = p; + + return 0; +} + +/* Undo the escaping create applies: \\ -> \, \| -> |, \n -> newline. A bare + delimiter, an unknown escape, or a trailing backslash means the record is + malformed; that is reported with EINVAL to keep it apart from allocation + failure. */ +static char *unescape_path(const char *s, char delim) { + const size_t len = strlen(s); + + char *out = calloc(len + 1, sizeof(char)); + if (out == NULL) + return NULL; + + size_t j = 0; + for (size_t i = 0; i < len; i++) { + if (s[i] == delim) + goto malformed; + + if (s[i] != '\\') { + out[j++] = s[i]; + continue; + } + + /* s[len] is the terminator, so a trailing backslash lands on the + default case rather than reading past the end. */ + i++; + switch (s[i]) { + case '\\': + out[j++] = '\\'; + break; + case 'n': + out[j++] = '\n'; + break; + default: + if (s[i] != delim) + goto malformed; + out[j++] = delim; + break; + } + } + + return out; + +malformed: + free(out); + errno = EINVAL; + return NULL; +} + +static int parse_file_type(const char *s, char *out) { + if (s == NULL || s[0] == '\0' || s[1] != '\0') + return -1; + + char c = s[0]; + switch (c) { + case 'F': + case 'D': + case 'L': + case 'P': + case 'C': + case 'B': + case 'S': + case '?': + *out = c; + return 0; + default: + return -1; + } +} + +/* strtol and friends accept leading whitespace, a sign, and wrap negative + input around for the unsigned variants. Reject anything that is not a bare + run of decimal digits before converting. */ +static int only_digits(const char *s, int base) { + const char last = (base == 8) ? '7' : '9'; + + if (s == NULL || *s == '\0') + return 0; + + for (const char *p = s; *p != '\0'; p++) { + if (*p < '0' || *p > last) + return 0; + } + return 1; +} + +static int parse_perm(const char *s, unsigned int *out) { + char *end = NULL; + + if (s == NULL || strlen(s) != 4 || !only_digits(s, 8)) + return -1; + + errno = 0; + unsigned long v = strtoul(s, &end, 8); + if (*end != '\0' || errno == ERANGE || v > 07777) + return -1; + + *out = (unsigned int)v; + return 0; +} + +static int parse_uintmax(const char *s, uintmax_t *out) { + char *end = NULL; + + if (!only_digits(s, 10)) + return -1; + + errno = 0; + uintmax_t v = strtoumax(s, &end, 10); + if (*end != '\0' || errno == ERANGE) + return -1; + + *out = v; + return 0; +} + +static int parse_intmax_nonnegative(const char *s, intmax_t *out) { + char *end = NULL; + + if (!only_digits(s, 10)) + return -1; + + errno = 0; + intmax_t v = strtoimax(s, &end, 10); + if (*end != '\0' || errno == ERANGE || v < 0) + return -1; + + *out = v; + return 0; +} + +struct RecordObject { + char file_type; + unsigned int perm; + uintmax_t uid; + uintmax_t gid; + intmax_t size; + intmax_t time; + char *path; +}; + +struct RecordObject *unpack(char *s) { + char *fields[NFIELDS]; + if (split_record(s, fields, '|') == -1) + return NULL; + + struct RecordObject *objp = calloc(1, sizeof(*objp)); + if (objp == NULL) { + return NULL; + } + + field = NONE; + + if (parse_file_type(fields[0], &objp->file_type) == -1) { + field = FILE_TYPE; + return objp; + } + + if (parse_perm(fields[1], &objp->perm) == -1) { + field = PERMISSIONS; + return objp; + } + + if (parse_uintmax(fields[2], &objp->uid) == -1) { + field = UID; + return objp; + } + + if (parse_uintmax(fields[3], &objp->gid) == -1) { + field = GID; + return objp; + } + + if (parse_intmax_nonnegative(fields[4], &objp->size) == -1) { + field = SIZE; + return objp; + } + + if (parse_intmax_nonnegative(fields[5], &objp->time) == -1) { + field = TIME; + return objp; + } + + if (fields[6][0] == '\0') { + field = PATH; + return objp; + } + + errno = 0; + char *path = unescape_path(fields[6], '|'); + if (path == NULL) { + if (errno == EINVAL) { + field = PATH; + return objp; + } + release(objp); + return NULL; + } + + objp->path = path; + + return objp; +} + +int release(struct RecordObject *obj) { + if (obj == NULL) { + return 0; + } + if (obj->path != NULL) { + free(obj->path); + obj->path = NULL; + } + + free(obj); + return 0; +}