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