From 53e13e9c51865c8273aa629fe33c5e5f358d73e4 Mon Sep 17 00:00:00 2001 From: Fabio Scotto di Santolo Date: Tue, 25 Aug 2026 23:25:55 +0200 Subject: [PATCH] Refactor create command using RecordObject --- include/types.h | 2 + src/create.c | 124 ++---------------------------------------------- src/types.c | 124 ++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 127 insertions(+), 123 deletions(-) diff --git a/include/types.h b/include/types.h index ceb6a34..3aa7143 100644 --- a/include/types.h +++ b/include/types.h @@ -2,6 +2,7 @@ #define TYPE_H #include +#include enum Fields { FILE_TYPE, @@ -25,6 +26,7 @@ struct RecordObject; // Methods struct RecordObject *NewRecordObject(); int Unpack(struct RecordObject *, char *); +int RecordObjectWrite(struct RecordObject *self, const char *path, FILE *out); int Compare(struct RecordObject *, struct RecordObject *); char GetFileType(const struct RecordObject *); unsigned int GetPermissions(const struct RecordObject *); diff --git a/src/create.c b/src/create.c index 1ca6e06..b3f3f79 100644 --- a/src/create.c +++ b/src/create.c @@ -2,73 +2,17 @@ #define _DEFAULT_SOURCE #define _XOPEN_SOURCE 700 #include "create.h" -#include "hash.h" +#include "types.h" #include "utility.h" #include "walk.h" #include -#include #include -#include #include #include #include #include -#include #include -static char file_type(mode_t mode) { - if (S_ISREG(mode)) - return 'F'; - if (S_ISDIR(mode)) - return 'D'; - if (S_ISLNK(mode)) - return 'L'; - if (S_ISFIFO(mode)) - return 'P'; - if (S_ISCHR(mode)) - return 'C'; - if (S_ISBLK(mode)) - return 'B'; - if (S_ISSOCK(mode)) - return 'S'; - - return '?'; -} - -static char *sanitize_path(const char *path) { - size_t len = strlen(path); - size_t escapes = 0; - for (size_t i = 0; i < len; i++) { - if (path[i] == '|' || path[i] == '\\' || path[i] == '\n') - escapes++; - } - - char *s = calloc(len + escapes + 1, sizeof(char)); - if (s == NULL) - return NULL; - - for (size_t i = 0, j = 0; i < len; i++) { - switch (path[i]) { - case '\\': - s[j++] = '\\'; - s[j++] = '\\'; - break; - case '|': - s[j++] = '\\'; - s[j++] = '|'; - break; - case '\n': - s[j++] = '\\'; - s[j++] = 'n'; - break; - default: - s[j++] = path[i]; - break; - } - } - return s; -} - static int write_record(const char *path, void *context) { // skip snapshot files const char *base = strrchr(path, '/'); @@ -77,70 +21,12 @@ static int write_record(const char *path, void *context) { if (n >= 6 && strcasecmp(base + n - 6, ".fsnap") == 0) return 0; - FILE *snapshot = (FILE *)context; - struct stat st; - if (lstat(path, &st) == -1) { + struct RecordObject *r = NewRecordObject(); + if (r == NULL) return -1; - } - - char type = file_type(st.st_mode); - unsigned int perm = (unsigned int)(st.st_mode & 07777); - uintmax_t uid = (uintmax_t)st.st_uid; - uintmax_t gid = (uintmax_t)st.st_gid; - intmax_t size = (intmax_t)st.st_size; - intmax_t time = (intmax_t)st.st_mtime; - char *s = sanitize_path(path); - if (s == NULL) - return -1; - - 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); - + int ret = RecordObjectWrite(r, path, (FILE *)context); + Release(r); 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) { diff --git a/src/types.c b/src/types.c index 9caa089..b36bc90 100644 --- a/src/types.c +++ b/src/types.c @@ -1,8 +1,16 @@ +#define _POSIX_C_SOURCE 1 +#define _DEFAULT_SOURCE +#define _XOPEN_SOURCE 700 +#include "hash.h" #include "types.h" #include +#include +#include #include #include #include +#include +#include #define NFIELDS 9 @@ -171,6 +179,59 @@ static int parse_intmax_nonnegative(const char *s, intmax_t *out) { return 0; } +static char file_type(mode_t mode) { + if (S_ISREG(mode)) + return 'F'; + if (S_ISDIR(mode)) + return 'D'; + if (S_ISLNK(mode)) + return 'L'; + if (S_ISFIFO(mode)) + return 'P'; + if (S_ISCHR(mode)) + return 'C'; + if (S_ISBLK(mode)) + return 'B'; + if (S_ISSOCK(mode)) + return 'S'; + + return '?'; +} + +static char *sanitize_path(const char *path) { + size_t len = strlen(path); + size_t escapes = 0; + for (size_t i = 0; i < len; i++) { + if (path[i] == '|' || path[i] == '\\' || path[i] == '\n') + escapes++; + } + + char *s = calloc(len + escapes + 1, sizeof(char)); + if (s == NULL) + return NULL; + + for (size_t i = 0, j = 0; i < len; i++) { + switch (path[i]) { + case '\\': + s[j++] = '\\'; + s[j++] = '\\'; + break; + case '|': + s[j++] = '\\'; + s[j++] = '|'; + break; + case '\n': + s[j++] = '\\'; + s[j++] = 'n'; + break; + default: + s[j++] = path[i]; + break; + } + } + return s; +} + //========================= RecordObject ================================= struct RecordObject { @@ -315,10 +376,66 @@ int Compare(struct RecordObject *self, struct RecordObject *other) { return exit_code; } -int Release(struct RecordObject *self) { - if (self == NULL) { - return 0; +int RecordObjectWrite(struct RecordObject *self, const char *path, FILE *out) { + struct stat st; + if (lstat(path, &st) == -1) { + return -1; } + + self->file_type = file_type(st.st_mode); + self->perm = (unsigned int)(st.st_mode & 07777); + self->uid = (uintmax_t)st.st_uid; + self->gid = (uintmax_t)st.st_gid; + self->size = (intmax_t)st.st_size; + self->time = (intmax_t)st.st_mtime; + char *s = sanitize_path(path); + if (s == NULL) + return -1; + if (self->path != NULL) + free(self->path); + self->path = s; + + char *target = NULL; + if (self->file_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) + return -1; + + if (readlink(path, target, bufsiz) == -1) { + free(target); + return -1; + } + } + if (self->target != NULL) + free(self->target); + self->target = target; + + FILE *f = NULL; + uint32_t hash = 0; + if (!S_ISDIR(st.st_mode)) { + if ((f = fopen(path, "r")) == NULL) + return -1; + + hash = crc32(f); + if (fclose(f) != 0) + return -1; + } + self->fingerprint = hash; + + return fprintf(out, "%c|%04o|%ju|%ju|%jd|%jd|%s|%ju|%s\n", self->file_type, + self->perm, self->uid, self->gid, self->size, self->time, + self->target == NULL ? "" : self->target, self->fingerprint, + self->path); +} + +int Release(struct RecordObject *self) { + if (self == NULL) + return -1; if (self->path != NULL) { free(self->path); self->path = NULL; @@ -327,7 +444,6 @@ int Release(struct RecordObject *self) { free(self->target); self->target = NULL; } - free(self); return 0; }