mirror of
https://github.com/fscotto/fsnap.git
synced 2026-11-26 03:09:51 +00:00
Implement snapshot diff command
Add fsnap diff <old> <new> that compares two snapshots and reports added (A), deleted (D), metadata changed (M), permissions changed (P), and symlink target changed (L) records. Both snapshots are loaded into memory, sorted by path using qsort, and merged with a two-pointer algorithm. Residual records after the merge are emitted as A or D. Escape sequences in paths are decoded on output. Also add GetTime accessor, set errno on load_in_memory failures for meaningful error messages, and fix unused-variable warnings.
This commit is contained in:
53
src/list.c
53
src/list.c
@@ -1,40 +1,13 @@
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include "list.h"
|
||||
#include "types.h"
|
||||
#include "utility.h"
|
||||
#include <errno.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static int count_lines(const char *file) {
|
||||
FILE *f = fopen(file, "r");
|
||||
if (f == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
size_t size = 0;
|
||||
int count = 0;
|
||||
char *buf = NULL;
|
||||
while (getline(&buf, &size, f) != -1)
|
||||
count++;
|
||||
|
||||
if (ferror(f)) {
|
||||
/* Capture before the cleanup: free and fclose may both touch errno. */
|
||||
const int saved_errno = errno;
|
||||
free(buf);
|
||||
fclose(f);
|
||||
errno = saved_errno;
|
||||
return -1;
|
||||
}
|
||||
|
||||
free(buf);
|
||||
if (fclose(f) != 0) {
|
||||
return -1;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
int list(const char *snapshot) {
|
||||
FILE *snapshot_file = fopen(snapshot, "r");
|
||||
if (snapshot_file == NULL) {
|
||||
@@ -60,20 +33,26 @@ int list(const char *snapshot) {
|
||||
goto out;
|
||||
}
|
||||
while (getline(&buf, &size, snapshot_file) != -1 && i < arr_size) {
|
||||
struct RecordObject *objp = unpack(buf);
|
||||
struct RecordObject *objp = NewRecordObject();
|
||||
if (objp == NULL) {
|
||||
rc = -1;
|
||||
goto out;
|
||||
}
|
||||
int ret = Unpack(objp, buf);
|
||||
if (field != NONE) {
|
||||
fprintf(stderr, "%s:%d: invalid %s\n", snapshot, i + 1, FieldName(field));
|
||||
rc = 128;
|
||||
Release(objp);
|
||||
break;
|
||||
}
|
||||
|
||||
if (ret == -1) {
|
||||
fprintf(stderr, "%s:%d: unparsable line\n", snapshot, i + 1);
|
||||
Release(objp);
|
||||
rc = 128;
|
||||
break;
|
||||
}
|
||||
|
||||
if (field != NONE) {
|
||||
fprintf(stderr, "%s:%d: invalid %s\n", snapshot, i + 1,
|
||||
field_name(field));
|
||||
rc = 128;
|
||||
release(objp);
|
||||
break;
|
||||
}
|
||||
records[i] = objp;
|
||||
i++;
|
||||
}
|
||||
@@ -95,7 +74,7 @@ out:;
|
||||
free(buf);
|
||||
if (records != NULL) {
|
||||
for (int j = 0; j < arr_size; j++) {
|
||||
release(records[j]);
|
||||
Release(records[j]);
|
||||
records[j] = NULL;
|
||||
}
|
||||
free(records);
|
||||
|
||||
Reference in New Issue
Block a user