mirror of
https://github.com/fscotto/fsnap.git
synced 2026-11-26 11:19:52 +00:00
Refactor create command using RecordObject
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
#define TYPE_H
|
#define TYPE_H
|
||||||
|
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
enum Fields {
|
enum Fields {
|
||||||
FILE_TYPE,
|
FILE_TYPE,
|
||||||
@@ -25,6 +26,7 @@ struct RecordObject;
|
|||||||
// Methods
|
// Methods
|
||||||
struct RecordObject *NewRecordObject();
|
struct RecordObject *NewRecordObject();
|
||||||
int Unpack(struct RecordObject *, char *);
|
int Unpack(struct RecordObject *, char *);
|
||||||
|
int RecordObjectWrite(struct RecordObject *self, const char *path, FILE *out);
|
||||||
int Compare(struct RecordObject *, struct RecordObject *);
|
int Compare(struct RecordObject *, struct RecordObject *);
|
||||||
char GetFileType(const struct RecordObject *);
|
char GetFileType(const struct RecordObject *);
|
||||||
unsigned int GetPermissions(const struct RecordObject *);
|
unsigned int GetPermissions(const struct RecordObject *);
|
||||||
|
|||||||
124
src/create.c
124
src/create.c
@@ -2,73 +2,17 @@
|
|||||||
#define _DEFAULT_SOURCE
|
#define _DEFAULT_SOURCE
|
||||||
#define _XOPEN_SOURCE 700
|
#define _XOPEN_SOURCE 700
|
||||||
#include "create.h"
|
#include "create.h"
|
||||||
#include "hash.h"
|
#include "types.h"
|
||||||
#include "utility.h"
|
#include "utility.h"
|
||||||
#include "walk.h"
|
#include "walk.h"
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <limits.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <strings.h>
|
#include <strings.h>
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
static char file_type(mode_t mode) {
|
|
||||||
if (S_ISREG(mode))
|
|
||||||
return 'F';
|
|
||||||
if (S_ISDIR(mode))
|
|
||||||
return 'D';
|
|
||||||
if (S_ISLNK(mode))
|
|
||||||
return 'L';
|
|
||||||
if (S_ISFIFO(mode))
|
|
||||||
return 'P';
|
|
||||||
if (S_ISCHR(mode))
|
|
||||||
return 'C';
|
|
||||||
if (S_ISBLK(mode))
|
|
||||||
return 'B';
|
|
||||||
if (S_ISSOCK(mode))
|
|
||||||
return 'S';
|
|
||||||
|
|
||||||
return '?';
|
|
||||||
}
|
|
||||||
|
|
||||||
static char *sanitize_path(const char *path) {
|
|
||||||
size_t len = strlen(path);
|
|
||||||
size_t escapes = 0;
|
|
||||||
for (size_t i = 0; i < len; i++) {
|
|
||||||
if (path[i] == '|' || path[i] == '\\' || path[i] == '\n')
|
|
||||||
escapes++;
|
|
||||||
}
|
|
||||||
|
|
||||||
char *s = calloc(len + escapes + 1, sizeof(char));
|
|
||||||
if (s == NULL)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
for (size_t i = 0, j = 0; i < len; i++) {
|
|
||||||
switch (path[i]) {
|
|
||||||
case '\\':
|
|
||||||
s[j++] = '\\';
|
|
||||||
s[j++] = '\\';
|
|
||||||
break;
|
|
||||||
case '|':
|
|
||||||
s[j++] = '\\';
|
|
||||||
s[j++] = '|';
|
|
||||||
break;
|
|
||||||
case '\n':
|
|
||||||
s[j++] = '\\';
|
|
||||||
s[j++] = 'n';
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
s[j++] = path[i];
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int write_record(const char *path, void *context) {
|
static int write_record(const char *path, void *context) {
|
||||||
// skip snapshot files
|
// skip snapshot files
|
||||||
const char *base = strrchr(path, '/');
|
const char *base = strrchr(path, '/');
|
||||||
@@ -77,70 +21,12 @@ static int write_record(const char *path, void *context) {
|
|||||||
if (n >= 6 && strcasecmp(base + n - 6, ".fsnap") == 0)
|
if (n >= 6 && strcasecmp(base + n - 6, ".fsnap") == 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
FILE *snapshot = (FILE *)context;
|
struct RecordObject *r = NewRecordObject();
|
||||||
struct stat st;
|
if (r == NULL)
|
||||||
if (lstat(path, &st) == -1) {
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
int ret = RecordObjectWrite(r, path, (FILE *)context);
|
||||||
|
Release(r);
|
||||||
char type = file_type(st.st_mode);
|
|
||||||
unsigned int perm = (unsigned int)(st.st_mode & 07777);
|
|
||||||
uintmax_t uid = (uintmax_t)st.st_uid;
|
|
||||||
uintmax_t gid = (uintmax_t)st.st_gid;
|
|
||||||
intmax_t size = (intmax_t)st.st_size;
|
|
||||||
intmax_t time = (intmax_t)st.st_mtime;
|
|
||||||
char *s = sanitize_path(path);
|
|
||||||
if (s == NULL)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
char *target = NULL;
|
|
||||||
if (type == 'L') {
|
|
||||||
size_t bufsiz = ((size_t)(st.st_size + 1));
|
|
||||||
|
|
||||||
if (st.st_size == 0)
|
|
||||||
bufsiz = (size_t)sysconf(_PC_PATH_MAX);
|
|
||||||
|
|
||||||
target = calloc(bufsiz, sizeof(char));
|
|
||||||
if (target == NULL)
|
|
||||||
goto failure;
|
|
||||||
|
|
||||||
if (readlink(path, target, bufsiz) == -1)
|
|
||||||
goto failure;
|
|
||||||
}
|
|
||||||
|
|
||||||
FILE *f = NULL;
|
|
||||||
uint32_t hash = 0;
|
|
||||||
if (!S_ISDIR(st.st_mode)) {
|
|
||||||
if ((f = fopen(path, "r")) == NULL)
|
|
||||||
goto failure;
|
|
||||||
|
|
||||||
hash = crc32(f);
|
|
||||||
}
|
|
||||||
|
|
||||||
int ret =
|
|
||||||
fprintf(snapshot, "%c|%04o|%ju|%ju|%jd|%jd|%s|%u|%s\n", type, perm, uid,
|
|
||||||
gid, size, time, (target == NULL ? "" : target), hash, s);
|
|
||||||
|
|
||||||
free(s);
|
|
||||||
|
|
||||||
if (target != NULL)
|
|
||||||
free(target);
|
|
||||||
|
|
||||||
if (f != NULL)
|
|
||||||
fclose(f);
|
|
||||||
|
|
||||||
return ret < 0 ? -1 : 0;
|
return ret < 0 ? -1 : 0;
|
||||||
|
|
||||||
failure:
|
|
||||||
free(s);
|
|
||||||
|
|
||||||
if (target != NULL)
|
|
||||||
free(target);
|
|
||||||
|
|
||||||
if (f != NULL)
|
|
||||||
fclose(f);
|
|
||||||
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int create(const char *directory, const char *output_file) {
|
int create(const char *directory, const char *output_file) {
|
||||||
|
|||||||
124
src/types.c
124
src/types.c
@@ -1,8 +1,16 @@
|
|||||||
|
#define _POSIX_C_SOURCE 1
|
||||||
|
#define _DEFAULT_SOURCE
|
||||||
|
#define _XOPEN_SOURCE 700
|
||||||
|
#include "hash.h"
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <limits.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#define NFIELDS 9
|
#define NFIELDS 9
|
||||||
|
|
||||||
@@ -171,6 +179,59 @@ static int parse_intmax_nonnegative(const char *s, intmax_t *out) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static char file_type(mode_t mode) {
|
||||||
|
if (S_ISREG(mode))
|
||||||
|
return 'F';
|
||||||
|
if (S_ISDIR(mode))
|
||||||
|
return 'D';
|
||||||
|
if (S_ISLNK(mode))
|
||||||
|
return 'L';
|
||||||
|
if (S_ISFIFO(mode))
|
||||||
|
return 'P';
|
||||||
|
if (S_ISCHR(mode))
|
||||||
|
return 'C';
|
||||||
|
if (S_ISBLK(mode))
|
||||||
|
return 'B';
|
||||||
|
if (S_ISSOCK(mode))
|
||||||
|
return 'S';
|
||||||
|
|
||||||
|
return '?';
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *sanitize_path(const char *path) {
|
||||||
|
size_t len = strlen(path);
|
||||||
|
size_t escapes = 0;
|
||||||
|
for (size_t i = 0; i < len; i++) {
|
||||||
|
if (path[i] == '|' || path[i] == '\\' || path[i] == '\n')
|
||||||
|
escapes++;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *s = calloc(len + escapes + 1, sizeof(char));
|
||||||
|
if (s == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
for (size_t i = 0, j = 0; i < len; i++) {
|
||||||
|
switch (path[i]) {
|
||||||
|
case '\\':
|
||||||
|
s[j++] = '\\';
|
||||||
|
s[j++] = '\\';
|
||||||
|
break;
|
||||||
|
case '|':
|
||||||
|
s[j++] = '\\';
|
||||||
|
s[j++] = '|';
|
||||||
|
break;
|
||||||
|
case '\n':
|
||||||
|
s[j++] = '\\';
|
||||||
|
s[j++] = 'n';
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
s[j++] = path[i];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
//========================= RecordObject =================================
|
//========================= RecordObject =================================
|
||||||
|
|
||||||
struct RecordObject {
|
struct RecordObject {
|
||||||
@@ -315,10 +376,66 @@ int Compare(struct RecordObject *self, struct RecordObject *other) {
|
|||||||
return exit_code;
|
return exit_code;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Release(struct RecordObject *self) {
|
int RecordObjectWrite(struct RecordObject *self, const char *path, FILE *out) {
|
||||||
if (self == NULL) {
|
struct stat st;
|
||||||
return 0;
|
if (lstat(path, &st) == -1) {
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self->file_type = file_type(st.st_mode);
|
||||||
|
self->perm = (unsigned int)(st.st_mode & 07777);
|
||||||
|
self->uid = (uintmax_t)st.st_uid;
|
||||||
|
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);
|
||||||
|
if (s == NULL)
|
||||||
|
return -1;
|
||||||
|
if (self->path != NULL)
|
||||||
|
free(self->path);
|
||||||
|
self->path = s;
|
||||||
|
|
||||||
|
char *target = NULL;
|
||||||
|
if (self->file_type == 'L') {
|
||||||
|
size_t bufsiz = ((size_t)(st.st_size + 1));
|
||||||
|
|
||||||
|
if (st.st_size == 0)
|
||||||
|
bufsiz = (size_t)sysconf(_PC_PATH_MAX);
|
||||||
|
|
||||||
|
target = calloc(bufsiz, sizeof(char));
|
||||||
|
if (target == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if (readlink(path, target, bufsiz) == -1) {
|
||||||
|
free(target);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (self->target != NULL)
|
||||||
|
free(self->target);
|
||||||
|
self->target = target;
|
||||||
|
|
||||||
|
FILE *f = NULL;
|
||||||
|
uint32_t hash = 0;
|
||||||
|
if (!S_ISDIR(st.st_mode)) {
|
||||||
|
if ((f = fopen(path, "r")) == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
hash = crc32(f);
|
||||||
|
if (fclose(f) != 0)
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
int Release(struct RecordObject *self) {
|
||||||
|
if (self == NULL)
|
||||||
|
return -1;
|
||||||
if (self->path != NULL) {
|
if (self->path != NULL) {
|
||||||
free(self->path);
|
free(self->path);
|
||||||
self->path = NULL;
|
self->path = NULL;
|
||||||
@@ -327,7 +444,6 @@ int Release(struct RecordObject *self) {
|
|||||||
free(self->target);
|
free(self->target);
|
||||||
self->target = NULL;
|
self->target = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
free(self);
|
free(self);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user