Unify error handling: output parameter, goto cleanup, errno preservation

- Replace global field variable with output parameter enum Fields *err
- Adopt single goto cleanup pattern across all functions
- Add errno preservation around cleanup in all modules
- Fix early return bug in create.c cleanup block
- Add ferror check in crc32() for silent read errors
- Document error contracts in all public headers
This commit is contained in:
Fabio Scotto di Santolo
2026-08-26 14:20:00 +02:00
parent 464bb58c8c
commit c397728912
15 changed files with 237 additions and 195 deletions

View File

@@ -1,6 +1,8 @@
#ifndef CREATE_H
#define CREATE_H
/* Create a snapshot of a directory tree.
Returns 0 on success, -1 on failure. */
int create(const char *directory, const char *output_file);
#endif

View File

@@ -1,6 +1,8 @@
#ifndef DIFF_H
#define DIFF_H
/* Compare two snapshot files and print added/deleted/modified entries.
Returns 0 on success, -1 on failure. */
int diff(const char *file1, const char *file2);
#endif

View File

@@ -4,6 +4,8 @@
#include <stdint.h>
#include <stdio.h>
/* Compute CRC-32 checksum of stream contents.
Returns 0 on read error. */
uint32_t crc32(FILE *stream);
#endif

View File

@@ -1,6 +1,8 @@
#ifndef LIST_H
#define LIST_H
/* List all entries in a snapshot file.
Returns 0 on success, -1 on system/IO error, 128 on parse error. */
int list(const char *snapshot);
#endif

View File

@@ -1,6 +1,8 @@
#ifndef SCAN_H
#define SCAN_H
int scan(const char *);
/* Recursively scan a directory, printing all entries.
Returns 0 on success, -1 on failure. */
int scan(const char *directory);
#endif

View File

@@ -17,26 +17,41 @@ enum Fields {
NONE,
};
extern enum Fields field;
const char *RecordObjectFieldName(enum Fields f);
struct RecordObject;
/* Methods */
struct RecordObject *RecordObjectNew();
int RecordObjectUnpack(struct RecordObject *, char *);
/* Allocate a new RecordObject. Returns NULL on allocation failure. */
struct RecordObject *RecordObjectNew(void);
/* Unpack a pipe-delimited record string into self.
Returns 0 on success, -1 on failure. If err is non-NULL, sets *err to
the field that failed to parse. */
int RecordObjectUnpack(struct RecordObject *self, char *s, enum Fields *err);
/* Populate self by stat-ing the file at path and writing to out.
Returns the number of bytes written on success, -1 on failure. */
int RecordObjectWrite(struct RecordObject *self, const char *path, FILE *out);
int RecordObjectCompare(const struct RecordObject *, const struct RecordObject *);
char RecordObjectFileType(const struct RecordObject *);
unsigned int RecordObjectPermissions(const struct RecordObject *);
uintmax_t RecordObjectUid(const struct RecordObject *);
uintmax_t RecordObjectGid(const struct RecordObject *);
intmax_t RecordObjectSize(const struct RecordObject *);
intmax_t RecordObjectTime(const struct RecordObject *);
const char *RecordObjectPath(const struct RecordObject *);
const char *RecordObjectTarget(const struct RecordObject *);
uintmax_t RecordObjectFingerPrint(const struct RecordObject *);
int RecordObjectRelease(struct RecordObject *);
/* Compare two RecordObjects field by field.
Returns 0 if equal, a non-zero difference otherwise.
On NULL self returns 128, on NULL other returns 1.
If err is non-NULL, sets *err to the first differing field. */
int RecordObjectCompare(const struct RecordObject *self,
const struct RecordObject *other, enum Fields *err);
/* Accessors */
char RecordObjectFileType(const struct RecordObject *self);
unsigned int RecordObjectPermissions(const struct RecordObject *self);
uintmax_t RecordObjectUid(const struct RecordObject *self);
uintmax_t RecordObjectGid(const struct RecordObject *self);
intmax_t RecordObjectSize(const struct RecordObject *self);
intmax_t RecordObjectTime(const struct RecordObject *self);
const char *RecordObjectPath(const struct RecordObject *self);
const char *RecordObjectTarget(const struct RecordObject *self);
uintmax_t RecordObjectFingerPrint(const struct RecordObject *self);
/* Free self and all owned resources. Returns 0, or -1 if self is NULL. */
int RecordObjectRelease(struct RecordObject *self);
#endif

View File

@@ -1,12 +1,20 @@
#ifndef UTILITY_H
#define UTILITY_H
/* Concatenate src with n additional strings. Returns a newly allocated
string, or NULL on allocation failure. */
char *strconcat(const char *src, int n, ...);
/* Resolve path to an absolute canonical form. Returns a newly allocated
string, or NULL on failure (errno set by realpath). */
char *resolve_path(const char *path);
/* Copy file contents from src to dst.
Returns 0 on success, -1 on failure. */
int copy(const char *src, const char *dst);
/* Count lines in a file.
Returns the line count on success, -1 on failure. */
int count_lines(const char *file);
#endif

View File

@@ -1,8 +1,11 @@
#ifndef WALK_H
#define WALK_H
typedef int(*WalkOp)(const char *, void *);
typedef int (*WalkOp)(const char *, void *);
/* Recursively walk a directory, calling op for each entry.
Returns 0 on success, -1 on failure. Silently skips directories
where access is denied. */
int walk(const char *directory, WalkOp op, void *context);
#endif

View File

@@ -39,50 +39,44 @@ int create(const char *directory, const char *output_file) {
return -1;
}
char *resolved = resolve_path(directory);
if (resolved == NULL) {
return -1;
}
int rc = 0;
int rc = -1;
char *resolved = NULL;
char template[] = "/tmp/snapshot_XXXXXX";
int fd = mkstemp(template);
if (fd == -1) {
free(resolved);
return -1;
}
int fd = -1;
FILE *tmp = NULL;
FILE *tmp = fdopen(fd, "w+");
resolved = resolve_path(directory);
if (resolved == NULL)
goto cleanup;
fd = mkstemp(template);
if (fd == -1)
goto cleanup;
tmp = fdopen(fd, "w+");
if (tmp == NULL) {
close(fd);
rc = -1;
goto out;
goto cleanup;
}
if (walk(resolved, write_record, (void *)tmp) == -1) {
rc = -1;
goto out;
}
if (walk(resolved, write_record, (void *)tmp) == -1)
goto cleanup;
if (fflush(tmp) == EOF) {
rc = -1;
goto out;
}
if (fflush(tmp) == EOF)
goto cleanup;
/* FIXME: It doesn't atomic copy, using rename */
if (copy(template, output_file) == -1) {
rc = -1;
goto out;
}
if (copy(template, output_file) == -1)
goto cleanup;
out:;
/* A buffered write can fail for the first time here, so fclose owns errno
when nothing had failed before it. */
rc = 0;
cleanup:;
const int saved_errno = errno;
free(resolved);
unlink(template);
if (tmp != NULL && fclose(tmp) != 0 && rc == 0)
return -1;
rc = -1;
if (rc == -1)
errno = saved_errno;
return rc;

View File

@@ -8,24 +8,24 @@
#include <string.h>
static int load_in_memory(const char *file, struct RecordObject ***records,
int *count) {
int exit_failure = -1;
int *count) {
int rc = -1;
FILE *stream = NULL;
char *buf = NULL;
if ((stream = fopen(file, "r")) == NULL)
goto failure;
goto cleanup;
*count = count_lines(file);
if (*count == 0)
goto success;
else if (*count < 0)
goto failure;
goto cleanup;
if (*count < 0)
goto cleanup;
*records = calloc((size_t)*count, sizeof(**records));
if (*records == NULL) {
errno = ENOMEM;
goto failure;
goto cleanup;
}
int i = 0;
@@ -34,14 +34,14 @@ static int load_in_memory(const char *file, struct RecordObject ***records,
struct RecordObject *objp = RecordObjectNew();
if (objp == NULL) {
errno = ENOMEM;
goto failure;
goto cleanup;
}
int ret = RecordObjectUnpack(objp, buf);
enum Fields err;
int ret = RecordObjectUnpack(objp, buf, &err);
if (ret == -1) {
errno = EINVAL;
RecordObjectRelease(objp);
goto failure;
goto cleanup;
}
(*records)[i] = objp;
@@ -49,32 +49,26 @@ static int load_in_memory(const char *file, struct RecordObject ***records,
}
if (ferror(stream))
goto failure;
goto cleanup;
success:
rc = 0;
cleanup:;
const int saved_errno = errno;
if (stream != NULL)
fclose(stream);
if (buf != NULL)
free(buf);
return 0;
failure:
if (stream != NULL)
fclose(stream);
if (buf != NULL)
free(buf);
return exit_failure;
errno = saved_errno;
return rc;
}
static int dealloc(struct RecordObject **records, int size) {
static void dealloc(struct RecordObject **records, int size) {
for (int j = 0; j < size; j++) {
RecordObjectRelease(records[j]);
records[j] = NULL;
}
free(records);
return 0;
}
static int order_by_path(const void *o1, const void *o2) {
@@ -84,21 +78,17 @@ static int order_by_path(const void *o1, const void *o2) {
}
int diff(const char *file1, const char *file2) {
int exit_failure = -1;
int rc = -1;
struct RecordObject **records1 = NULL;
struct RecordObject **records2 = NULL;
int len1 = 0;
int ret1 = load_in_memory(file1, &records1, &len1);
if (ret1 == -1) {
goto failure;
}
if (load_in_memory(file1, &records1, &len1) == -1)
goto cleanup;
int len2 = 0;
int ret2 = load_in_memory(file2, &records2, &len2);
if (ret2 == -1) {
goto failure;
}
if (load_in_memory(file2, &records2, &len2) == -1)
goto cleanup;
qsort(records1, (size_t)len1, sizeof(records1[0]), order_by_path);
qsort(records2, (size_t)len2, sizeof(records2[0]), order_by_path);
@@ -119,24 +109,27 @@ int diff(const char *file1, const char *file2) {
code = 'A';
path = RecordObjectPath(r2);
n++;
} else if (RecordObjectCompare(r1, r2) != 0) {
switch (field) {
case TARGET:
code = 'L';
break;
case PERMISSIONS:
code = 'P';
break;
default:
code = 'M';
break;
}
path = RecordObjectPath(r1);
m++;
n++;
} else {
m++;
n++;
enum Fields err = NONE;
if (RecordObjectCompare(r1, r2, &err) != 0) {
switch (err) {
case TARGET:
code = 'L';
break;
case PERMISSIONS:
code = 'P';
break;
default:
code = 'M';
break;
}
path = RecordObjectPath(r1);
m++;
n++;
} else {
m++;
n++;
}
}
if (code != 0) {
@@ -156,26 +149,12 @@ int diff(const char *file1, const char *file2) {
n++;
}
if (records1 != NULL) {
rc = 0;
cleanup:
if (records1 != NULL)
dealloc(records1, len1);
records1 = NULL;
}
if (records2 != NULL) {
if (records2 != NULL)
dealloc(records2, len2);
records2 = NULL;
}
return 0;
failure:
if (records1 != NULL) {
dealloc(records1, len1);
records1 = NULL;
}
if (records2 != NULL) {
dealloc(records2, len2);
records2 = NULL;
}
return exit_failure;
return rc;
}

View File

@@ -43,5 +43,8 @@ uint32_t crc32(FILE *stream) {
crc = crc32_update(crc, buffer, bytes_read);
}
if (ferror(stream))
return 0;
return crc ^ 0xFFFFFFFF;
}

View File

@@ -38,9 +38,11 @@ int list(const char *snapshot) {
rc = -1;
goto out;
}
int ret = RecordObjectUnpack(objp, buf);
if (field != NONE) {
fprintf(stderr, "%s:%d: invalid %s\n", snapshot, i + 1, RecordObjectFieldName(field));
enum Fields err = NONE;
int ret = RecordObjectUnpack(objp, buf, &err);
if (err != NONE) {
fprintf(stderr, "%s:%d: invalid %s\n", snapshot, i + 1,
RecordObjectFieldName(err));
rc = 128;
RecordObjectRelease(objp);
break;

View File

@@ -14,8 +14,6 @@
#define NFIELDS 9
enum Fields field = NONE;
static const char *field_names[] = {
"file type", "permissions", "uid", "gid", "size",
"time", "target", "fingerprint", "path", "none"};
@@ -255,40 +253,44 @@ struct RecordObject *RecordObjectNew() {
return ret;
}
int RecordObjectUnpack(struct RecordObject *self, char *s) {
int RecordObjectUnpack(struct RecordObject *self, char *s, enum Fields *err) {
char *fields[NFIELDS];
if (split_record(s, fields, '|') == -1)
return -1;
field = NONE;
if (parse_file_type(fields[0], &self->file_type) == -1) {
field = FILE_TYPE;
if (err)
*err = FILE_TYPE;
return -1;
}
if (parse_perm(fields[1], &self->perm) == -1) {
field = PERMISSIONS;
if (err)
*err = PERMISSIONS;
return -1;
}
if (parse_uintmax(fields[2], &self->uid) == -1) {
field = UID;
if (err)
*err = UID;
return -1;
}
if (parse_uintmax(fields[3], &self->gid) == -1) {
field = GID;
if (err)
*err = GID;
return -1;
}
if (parse_intmax_nonnegative(fields[4], &self->size) == -1) {
field = SIZE;
if (err)
*err = SIZE;
return -1;
}
if (parse_intmax_nonnegative(fields[5], &self->time) == -1) {
field = TIME;
if (err)
*err = TIME;
return -1;
}
@@ -296,7 +298,8 @@ int RecordObjectUnpack(struct RecordObject *self, char *s) {
char *target = unescape_path(fields[6], '|');
if (target == NULL) {
if (errno == EINVAL) {
field = TARGET;
if (err)
*err = TARGET;
return -1;
}
return -1;
@@ -305,12 +308,14 @@ int RecordObjectUnpack(struct RecordObject *self, char *s) {
self->target = target;
if (parse_uintmax(fields[7], &self->fingerprint) == -1) {
field = FINGERPRINT;
if (err)
*err = FINGERPRINT;
return -1;
}
if (fields[8][0] == '\0') {
field = PATH;
if (err)
*err = PATH;
return -1;
}
@@ -318,7 +323,8 @@ int RecordObjectUnpack(struct RecordObject *self, char *s) {
char *path = unescape_path(fields[8], '|');
if (path == NULL) {
if (errno == EINVAL) {
field = PATH;
if (err)
*err = PATH;
return -1;
}
return -1;
@@ -329,9 +335,9 @@ int RecordObjectUnpack(struct RecordObject *self, char *s) {
return 0;
}
int RecordObjectCompare(const struct RecordObject *self, const struct RecordObject *other) {
field = NONE;
int RecordObjectCompare(const struct RecordObject *self,
const struct RecordObject *other,
enum Fields *err) {
if (self == NULL)
return 128;
if (other == NULL)
@@ -340,35 +346,44 @@ int RecordObjectCompare(const struct RecordObject *self, const struct RecordObje
int exit_code = 0;
if (self->file_type != other->file_type) {
exit_code = ((int)(self->file_type - other->file_type));
field = FILE_TYPE;
if (err)
*err = FILE_TYPE;
} else if (self->perm != other->perm) {
exit_code = ((int)(self->perm - other->perm));
field = PERMISSIONS;
if (err)
*err = PERMISSIONS;
} else if (self->uid != other->uid) {
exit_code = ((int)(self->uid - other->uid));
field = UID;
if (err)
*err = UID;
} else if (self->gid != other->gid) {
exit_code = ((int)(self->gid - other->gid));
field = GID;
if (err)
*err = GID;
} else if (self->size != other->size) {
exit_code = ((int)(self->size - other->size));
field = SIZE;
if (err)
*err = SIZE;
} else if (self->time != other->time) {
exit_code = ((int)(self->time - other->time));
field = TIME;
if (err)
*err = TIME;
} else if (self->fingerprint != other->fingerprint) {
exit_code = ((int)(self->fingerprint - other->fingerprint));
field = FINGERPRINT;
if (err)
*err = FINGERPRINT;
} else {
int retp = strcmp(self->path, other->path);
if (retp != 0) {
exit_code = retp;
field = PATH;
if (err)
*err = PATH;
} else {
int rett = strcmp(self->target, other->target);
if (rett != 0) {
exit_code = rett;
field = TARGET;
if (err)
*err = TARGET;
}
}
}
@@ -377,10 +392,14 @@ int RecordObjectCompare(const struct RecordObject *self, const struct RecordObje
}
int RecordObjectWrite(struct RecordObject *self, const char *path, FILE *out) {
int rc = -1;
struct stat st;
if (lstat(path, &st) == -1) {
return -1;
}
char *s = NULL;
char *target = NULL;
FILE *f = NULL;
if (lstat(path, &st) == -1)
goto cleanup;
self->file_type = file_type(st.st_mode);
self->perm = (unsigned int)(st.st_mode & 07777);
@@ -388,14 +407,14 @@ int RecordObjectWrite(struct RecordObject *self, const char *path, FILE *out) {
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);
s = sanitize_path(path);
if (s == NULL)
return -1;
goto cleanup;
if (self->path != NULL)
free(self->path);
self->path = s;
s = NULL;
char *target = NULL;
if (self->file_type == 'L') {
size_t bufsiz = ((size_t)(st.st_size + 1));
@@ -404,33 +423,43 @@ int RecordObjectWrite(struct RecordObject *self, const char *path, FILE *out) {
target = calloc(bufsiz, sizeof(char));
if (target == NULL)
return -1;
goto cleanup;
if (readlink(path, target, bufsiz) == -1) {
free(target);
return -1;
}
if (readlink(path, target, bufsiz) == -1)
goto cleanup;
}
if (self->target != NULL)
free(self->target);
self->target = target;
target = NULL;
FILE *f = NULL;
uint32_t hash = 0;
if (!S_ISDIR(st.st_mode)) {
if ((f = fopen(path, "r")) == NULL)
return -1;
goto cleanup;
hash = crc32(f);
if (fclose(f) != 0)
return -1;
if (ferror(f) || fclose(f) != 0) {
f = NULL;
goto cleanup;
}
f = NULL;
}
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);
rc = 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);
cleanup:
if (s != NULL)
free(s);
if (target != NULL)
free(target);
if (f != NULL)
fclose(f);
return rc;
}
int RecordObjectRelease(struct RecordObject *self) {

View File

@@ -48,40 +48,39 @@ char *resolve_path(const char *path) {
}
int copy(const char *src, const char *dst) {
FILE *file1 = fopen(src, "r");
if (file1 == NULL) {
return -1;
}
int rc = -1;
FILE *file1 = NULL;
FILE *file2 = NULL;
FILE *file2 = fopen(dst, "w+");
if (file2 == NULL) {
fclose(file1);
return -1;
}
file1 = fopen(src, "r");
if (file1 == NULL)
goto cleanup;
file2 = fopen(dst, "w+");
if (file2 == NULL)
goto cleanup;
char buffer[BUFFER_SIZE];
size_t bytes;
while ((bytes = fread(buffer, 1, sizeof(buffer), file1)) > 0) {
if (fwrite(buffer, 1, bytes, file2) != bytes) {
fclose(file1);
fclose(file2);
return -1;
}
if (fwrite(buffer, 1, bytes, file2) != bytes)
goto cleanup;
}
if (ferror(file1)) {
fclose(file1);
fclose(file2);
return -1;
}
if (ferror(file1))
goto cleanup;
int rc = 0;
if (fclose(file1) != 0)
rc = -1;
if (fclose(file2) != 0)
rc = -1;
rc = 0;
cleanup:;
const int saved_errno = errno;
if (file1 != NULL && fclose(file1) != 0 && rc == 0)
rc = -1;
if (file2 != NULL && fclose(file2) != 0 && rc == 0)
rc = -1;
if (rc == -1)
errno = saved_errno;
return rc;
}
@@ -98,7 +97,6 @@ int count_lines(const char *file) {
count++;
if (ferror(f)) {
/* Capture before the cleanup: free and fclose may both touch errno. */
const int saved_errno = errno;
free(buf);
fclose(f);
@@ -107,8 +105,7 @@ int count_lines(const char *file) {
}
free(buf);
if (fclose(f) != 0) {
if (fclose(f) != 0)
return -1;
}
return count;
}

View File

@@ -96,7 +96,9 @@ int walk(const char *directory, WalkOp op, void *context) {
out:
free(path);
const int saved_errno = errno;
if (closedir(dirp) == -1)
rc = -1;
errno = saved_errno;
return rc;
}