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:
187
src/diff.c
Normal file
187
src/diff.c
Normal file
@@ -0,0 +1,187 @@
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include "diff.h"
|
||||
#include "types.h"
|
||||
#include "utility.h"
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static int load_in_memory(const char *file, struct RecordObject ***records,
|
||||
int *count) {
|
||||
int exit_failure = -1;
|
||||
FILE *stream = NULL;
|
||||
char *buf = NULL;
|
||||
|
||||
if ((stream = fopen(file, "r")) == NULL)
|
||||
goto failure;
|
||||
|
||||
*count = count_lines(file);
|
||||
if (*count == 0)
|
||||
goto success;
|
||||
else if (*count < 0)
|
||||
goto failure;
|
||||
|
||||
*records = calloc((size_t)*count, sizeof(**records));
|
||||
if (*records == NULL) {
|
||||
errno = ENOMEM;
|
||||
goto failure;
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
size_t size = 0;
|
||||
while (getline(&buf, &size, stream) != -1 && i < *count) {
|
||||
struct RecordObject *objp = NewRecordObject();
|
||||
if (objp == NULL) {
|
||||
errno = ENOMEM;
|
||||
goto failure;
|
||||
}
|
||||
|
||||
int ret = Unpack(objp, buf);
|
||||
if (ret == -1) {
|
||||
errno = EINVAL;
|
||||
Release(objp);
|
||||
goto failure;
|
||||
}
|
||||
|
||||
(*records)[i] = objp;
|
||||
i++;
|
||||
}
|
||||
|
||||
if (ferror(stream))
|
||||
goto failure;
|
||||
|
||||
success:
|
||||
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;
|
||||
}
|
||||
|
||||
static int dealloc(struct RecordObject **records, int size) {
|
||||
for (int j = 0; j < size; j++) {
|
||||
Release(records[j]);
|
||||
records[j] = NULL;
|
||||
}
|
||||
free(records);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int order_by_path(const void *o1, const void *o2) {
|
||||
const struct RecordObject *r1 = *(const struct RecordObject *const *)o1;
|
||||
const struct RecordObject *r2 = *(const struct RecordObject *const *)o2;
|
||||
return strcmp(GetPath(r1), GetPath(r2));
|
||||
}
|
||||
|
||||
int diff(const char *file1, const char *file2) {
|
||||
int exit_failure = -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;
|
||||
} else if (ret1 > 0) {
|
||||
exit_failure = ret1;
|
||||
goto failure;
|
||||
}
|
||||
|
||||
int len2 = 0;
|
||||
int ret2 = load_in_memory(file2, &records2, &len2);
|
||||
if (ret2 == -1) {
|
||||
goto failure;
|
||||
} else if (ret2 > 0) {
|
||||
exit_failure = ret2;
|
||||
goto failure;
|
||||
}
|
||||
|
||||
qsort(records1, (size_t)len1, sizeof(records1[0]), order_by_path);
|
||||
qsort(records2, (size_t)len2, sizeof(records2[0]), order_by_path);
|
||||
|
||||
int m = 0, n = 0;
|
||||
while (m < len1 && n < len2) {
|
||||
struct RecordObject *r1 = records1[m];
|
||||
struct RecordObject *r2 = records2[n];
|
||||
|
||||
char code = 0;
|
||||
const char *path = NULL;
|
||||
int c = strcmp(GetPath(r1), GetPath(r2));
|
||||
if (c < 0) {
|
||||
code = 'D';
|
||||
path = GetPath(r1);
|
||||
m++;
|
||||
} else if (c > 0) {
|
||||
code = 'A';
|
||||
path = GetPath(r2);
|
||||
n++;
|
||||
} else if (Compare(r1, r2) != 0) {
|
||||
switch (field) {
|
||||
case TARGET:
|
||||
code = 'L';
|
||||
break;
|
||||
case PERMISSIONS:
|
||||
code = 'P';
|
||||
break;
|
||||
default:
|
||||
code = 'M';
|
||||
break;
|
||||
}
|
||||
path = GetPath(r1);
|
||||
m++;
|
||||
n++;
|
||||
} else {
|
||||
m++;
|
||||
n++;
|
||||
}
|
||||
|
||||
if (code != 0) {
|
||||
fprintf(stdout, "%c\t%s\n", code, path);
|
||||
}
|
||||
}
|
||||
|
||||
while (m < len1) {
|
||||
const struct RecordObject *r = records1[m];
|
||||
fprintf(stdout, "D\t%s\n", GetPath(r));
|
||||
m++;
|
||||
}
|
||||
|
||||
while (n < len2) {
|
||||
const struct RecordObject *r = records2[n];
|
||||
fprintf(stdout, "A\t%s\n", GetPath(r));
|
||||
n++;
|
||||
}
|
||||
|
||||
if (records1 != NULL) {
|
||||
dealloc(records1, len1);
|
||||
records1 = 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;
|
||||
}
|
||||
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);
|
||||
|
||||
21
src/main.c
21
src/main.c
@@ -6,7 +6,7 @@
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc < 2) {
|
||||
fprintf(stderr, "%s: command not found [scan|create|list]\n", argv[0]);
|
||||
fprintf(stderr, "%s: command not found [scan|create|list|diff]\n", argv[0]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ int main(int argc, char *argv[]) {
|
||||
}
|
||||
} else if (strcmp(cmd, "list") == 0) {
|
||||
if (argc < 3) {
|
||||
fprintf(stderr, "%s: command %s expected param [snapshot_file]", argv[0],
|
||||
fprintf(stderr, "%s: command %s expected param [snapshot_file]\n", argv[0],
|
||||
cmd);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
@@ -54,6 +54,23 @@ int main(int argc, char *argv[]) {
|
||||
} else if (ret > 0) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
} else if (strcmp(cmd, "diff") == 0) {
|
||||
if (argc < 4) {
|
||||
fprintf(stderr,
|
||||
"%s: command %s expected params [snapshot_file1 snapshot_file2]\n",
|
||||
argv[0], cmd);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
const char *snapshot1 = argv[2];
|
||||
const char *snapshot2 = argv[3];
|
||||
int ret = diff(snapshot1, snapshot2);
|
||||
if (ret == -1) {
|
||||
const int err = errno;
|
||||
fprintf(stderr, "diff failed: %s\n", strerror(err));
|
||||
return EXIT_FAILURE;
|
||||
} else if (ret > 0) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "command %s is not valid\n", cmd);
|
||||
return EXIT_FAILURE;
|
||||
|
||||
165
src/types.c
165
src/types.c
@@ -1,6 +1,5 @@
|
||||
#include "types.h"
|
||||
#include <errno.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -13,10 +12,12 @@ static const char *field_names[] = {
|
||||
"file type", "permissions", "uid", "gid", "size",
|
||||
"time", "target", "fingerprint", "path", "none"};
|
||||
|
||||
const char *field_name(enum Fields f) {
|
||||
const char *FieldName(enum Fields f) {
|
||||
return (f <= NONE) ? field_names[f] : "unknown";
|
||||
}
|
||||
|
||||
//====================== static functions =====================================
|
||||
|
||||
static int split_record(char *line, char *fields[NFIELDS], char delim) {
|
||||
char *p = line;
|
||||
|
||||
@@ -170,6 +171,8 @@ static int parse_intmax_nonnegative(const char *s, intmax_t *out) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
//========================= RecordObject =================================
|
||||
|
||||
struct RecordObject {
|
||||
char file_type;
|
||||
unsigned int perm;
|
||||
@@ -182,46 +185,50 @@ struct RecordObject {
|
||||
uintmax_t fingerprint;
|
||||
};
|
||||
|
||||
struct RecordObject *unpack(char *s) {
|
||||
struct RecordObject *NewRecordObject() {
|
||||
struct RecordObject *ret = calloc(1, sizeof(*ret));
|
||||
if (ret == NULL)
|
||||
return NULL;
|
||||
ret->path = NULL;
|
||||
ret->target = NULL;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int Unpack(struct RecordObject *self, char *s) {
|
||||
char *fields[NFIELDS];
|
||||
if (split_record(s, fields, '|') == -1)
|
||||
return NULL;
|
||||
|
||||
struct RecordObject *objp = calloc(1, sizeof(*objp));
|
||||
if (objp == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
return -1;
|
||||
|
||||
field = NONE;
|
||||
|
||||
if (parse_file_type(fields[0], &objp->file_type) == -1) {
|
||||
if (parse_file_type(fields[0], &self->file_type) == -1) {
|
||||
field = FILE_TYPE;
|
||||
return objp;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (parse_perm(fields[1], &objp->perm) == -1) {
|
||||
if (parse_perm(fields[1], &self->perm) == -1) {
|
||||
field = PERMISSIONS;
|
||||
return objp;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (parse_uintmax(fields[2], &objp->uid) == -1) {
|
||||
if (parse_uintmax(fields[2], &self->uid) == -1) {
|
||||
field = UID;
|
||||
return objp;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (parse_uintmax(fields[3], &objp->gid) == -1) {
|
||||
if (parse_uintmax(fields[3], &self->gid) == -1) {
|
||||
field = GID;
|
||||
return objp;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (parse_intmax_nonnegative(fields[4], &objp->size) == -1) {
|
||||
if (parse_intmax_nonnegative(fields[4], &self->size) == -1) {
|
||||
field = SIZE;
|
||||
return objp;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (parse_intmax_nonnegative(fields[5], &objp->time) == -1) {
|
||||
if (parse_intmax_nonnegative(fields[5], &self->time) == -1) {
|
||||
field = TIME;
|
||||
return objp;
|
||||
return -1;
|
||||
}
|
||||
|
||||
errno = 0;
|
||||
@@ -229,22 +236,21 @@ struct RecordObject *unpack(char *s) {
|
||||
if (target == NULL) {
|
||||
if (errno == EINVAL) {
|
||||
field = TARGET;
|
||||
return objp;
|
||||
return -1;
|
||||
}
|
||||
release(objp);
|
||||
return NULL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
objp->target = target;
|
||||
self->target = target;
|
||||
|
||||
if (parse_uintmax(fields[7], &objp->fingerprint) == -1) {
|
||||
if (parse_uintmax(fields[7], &self->fingerprint) == -1) {
|
||||
field = FINGERPRINT;
|
||||
return objp;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (fields[8][0] == '\0') {
|
||||
field = PATH;
|
||||
return objp;
|
||||
return -1;
|
||||
}
|
||||
|
||||
errno = 0;
|
||||
@@ -252,30 +258,91 @@ struct RecordObject *unpack(char *s) {
|
||||
if (path == NULL) {
|
||||
if (errno == EINVAL) {
|
||||
field = PATH;
|
||||
return objp;
|
||||
return -1;
|
||||
}
|
||||
release(objp);
|
||||
return NULL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
objp->path = path;
|
||||
self->path = path;
|
||||
|
||||
return objp;
|
||||
}
|
||||
|
||||
int release(struct RecordObject *obj) {
|
||||
if (obj == NULL) {
|
||||
return 0;
|
||||
}
|
||||
if (obj->path != NULL) {
|
||||
free(obj->path);
|
||||
obj->path = NULL;
|
||||
}
|
||||
if (obj->target != NULL) {
|
||||
free(obj->target);
|
||||
obj->target = NULL;
|
||||
}
|
||||
|
||||
free(obj);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Compare(struct RecordObject *self, struct RecordObject *other) {
|
||||
field = NONE;
|
||||
|
||||
if (self == NULL)
|
||||
return 128;
|
||||
if (other == NULL)
|
||||
return 1;
|
||||
|
||||
int exit_code = 0;
|
||||
if (self->file_type != other->file_type) {
|
||||
exit_code = ((int)(self->file_type - other->file_type));
|
||||
field = FILE_TYPE;
|
||||
} else if (self->perm != other->perm) {
|
||||
exit_code = ((int)(self->perm - other->perm));
|
||||
field = PERMISSIONS;
|
||||
} else if (self->uid != other->uid) {
|
||||
exit_code = ((int)(self->uid - other->uid));
|
||||
field = UID;
|
||||
} else if (self->gid != other->gid) {
|
||||
exit_code = ((int)(self->gid - other->gid));
|
||||
field = GID;
|
||||
} else if (self->size != other->size) {
|
||||
exit_code = ((int)(self->size - other->size));
|
||||
field = SIZE;
|
||||
} else if (self->time != other->time) {
|
||||
exit_code = ((int)(self->time - other->time));
|
||||
field = TIME;
|
||||
} else if (self->fingerprint != other->fingerprint) {
|
||||
exit_code = ((int)(self->fingerprint - other->fingerprint));
|
||||
field = FINGERPRINT;
|
||||
} else {
|
||||
int retp = strcmp(self->path, other->path);
|
||||
if (retp != 0) {
|
||||
exit_code = retp;
|
||||
field = PATH;
|
||||
} else {
|
||||
int rett = strcmp(self->target, other->target);
|
||||
if (rett != 0) {
|
||||
exit_code = rett;
|
||||
field = TARGET;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return exit_code;
|
||||
}
|
||||
|
||||
int Release(struct RecordObject *self) {
|
||||
if (self == NULL) {
|
||||
return 0;
|
||||
}
|
||||
if (self->path != NULL) {
|
||||
free(self->path);
|
||||
self->path = NULL;
|
||||
}
|
||||
if (self->target != NULL) {
|
||||
free(self->target);
|
||||
self->target = NULL;
|
||||
}
|
||||
|
||||
free(self);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Accessors
|
||||
char GetFileType(const struct RecordObject *self) { return self->file_type; }
|
||||
unsigned int GetPermissions(const struct RecordObject *self) {
|
||||
return self->perm;
|
||||
}
|
||||
uintmax_t GetUid(const struct RecordObject *self) { return self->uid; }
|
||||
uintmax_t GetGid(const struct RecordObject *self) { return self->gid; }
|
||||
intmax_t GetSize(const struct RecordObject *self) { return self->size; }
|
||||
intmax_t GetTime(const struct RecordObject *self) { return self->time; }
|
||||
const char *GetPath(const struct RecordObject *self) { return self->path; }
|
||||
const char *GetTarget(const struct RecordObject *self) { return self->target; }
|
||||
uintmax_t GetFingerPrint(const struct RecordObject *self) {
|
||||
return self->fingerprint;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#define _XOPEN_SOURCE 700
|
||||
#include "utility.h"
|
||||
#include <errno.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -81,3 +82,31 @@ int copy(const char *src, const char *dst) {
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user