Improve snapshot handling and CLI workflows

This commit is contained in:
2026-11-01 16:52:17 +01:00
parent c63530da34
commit 2a1051980b
23 changed files with 944 additions and 495 deletions

View File

@@ -2,7 +2,8 @@
#define CREATE_H
/* Create a snapshot of a directory tree.
Returns 0 on success, -1 on failure. */
Returns 0 on success, FSNAP_WARNINGS (2) if entries were skipped,
-1 on failure. */
int create(const char *directory, const char *output_file);
#endif

View File

@@ -2,7 +2,8 @@
#define SCAN_H
/* Recursively scan a directory, printing all entries.
Returns 0 on success, -1 on failure. */
Returns 0 on success, FSNAP_WARNINGS (2) if entries were skipped,
-1 on failure. */
int scan(const char *directory);
#endif

13
include/snapshot.h Normal file
View File

@@ -0,0 +1,13 @@
#ifndef SNAPSHOT_H
#define SNAPSHOT_H
#include "types.h"
#include "status.h"
/* Read every record in a single pass. Supports FSNAP|1 and legacy headerless
snapshots. On success ownership passes to the caller; on failure outputs
remain empty. A regular file changed during the read fails with EAGAIN. */
int snapshot_load(const char *file, struct RecordObject ***out, int *count);
void snapshot_release(struct RecordObject **records, int count);
#endif

View File

@@ -1,13 +1,8 @@
#ifndef STATUS_H
#define STATUS_H
/* Command return values.
0 is success and -1 a system error, with errno set. FSNAP_EPARSE says the
input itself was malformed, which is the snapshot's problem rather than the
system's: errno carries nothing useful and the command has already written a
snapshot:line: diagnostic to stderr. main maps every failure to
EXIT_FAILURE, so the distinction is internal for now. */
enum { FSNAP_EPARSE = 128 };
/* Public status 2 means the command completed with skipped entries. Parse
errors stay internal: main maps them to public failure (1). */
enum { FSNAP_WARNINGS = 2, FSNAP_EPARSE = 128 };
#endif

View File

@@ -14,6 +14,9 @@ enum Fields {
TARGET,
FINGERPRINT,
PATH,
DEVICE,
INODE,
LINKS,
NONE,
};
@@ -25,13 +28,15 @@ struct RecordObject;
with errno set to ENOMEM. */
struct RecordObject *RecordObjectNew(void);
/* Unpack a pipe-delimited record string into self.
/* Unpack a pipe-delimited record string into self. version is 0 for legacy
nine-field records or 1 for the twelve-field versioned format.
Returns 0 on success, -1 on failure. On failure errno is EINVAL when the
record is malformed and something else, ENOMEM in practice, when the
failure is the parser's own; only in the first case is *err meaningful.
If err is non-NULL and the offending field could be identified, sets *err
to that field. */
int RecordObjectUnpack(struct RecordObject *self, char *s, enum Fields *err);
int RecordObjectUnpack(struct RecordObject *self, char *s, enum Fields *err,
unsigned version);
/* Populate self by stat-ing the file at path and writing to out.
Returns the number of bytes written on success, -1 on failure. */
@@ -47,6 +52,10 @@ int RecordObjectCompare(const struct RecordObject *self,
const struct RecordObject *other, enum Fields *err);
/* Accessors */
unsigned RecordObjectVersion(const struct RecordObject *self);
uintmax_t RecordObjectDevice(const struct RecordObject *self);
uintmax_t RecordObjectInode(const struct RecordObject *self);
uintmax_t RecordObjectLinks(const struct RecordObject *self);
char RecordObjectFileType(const struct RecordObject *self);
unsigned int RecordObjectPermissions(const struct RecordObject *self);
uintmax_t RecordObjectUid(const struct RecordObject *self);

View File

@@ -13,8 +13,4 @@ char *resolve_path(const char *path);
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

@@ -5,7 +5,8 @@
/* Compare a snapshot with the live tree rooted at root_fs.
Returns 0 when they match, 1 when differences were found and reported on
stdout, -1 on a system/IO error, and FSNAP_EPARSE on a malformed snapshot. */
stdout, FSNAP_WARNINGS (2) if traversal was incomplete but no differences
were found, -1 on a system/IO error, and FSNAP_EPARSE on a malformed snapshot. */
int verify(const char *snapshot, const char *root_fs);
#endif

View File

@@ -1,11 +1,14 @@
#ifndef WALK_H
#define WALK_H
#include "status.h"
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. */
Returns 0 on success, FSNAP_WARNINGS when a callback warns or a child
directory is skipped with EACCES, -1 on failure. An unreadable root fails.
A callback returns 0, FSNAP_WARNINGS, or -1. */
int walk(const char *directory, WalkOp op, void *context);
#endif