mirror of
https://github.com/fscotto/fsnap.git
synced 2026-11-26 03:09:51 +00:00
Add create command
This commit is contained in:
77
src/create.c
Normal file
77
src/create.c
Normal file
@@ -0,0 +1,77 @@
|
||||
#define _XOPEN_SOURCE 500
|
||||
#include "create.h"
|
||||
#include "utility.h"
|
||||
#include "walk.h"
|
||||
#include <fcntl.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.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 int write_record(const char *path, void *context) {
|
||||
FILE *snapshot = (FILE *)context;
|
||||
|
||||
struct stat st;
|
||||
if (lstat(path, &st) == -1) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
// Write record in the stream file
|
||||
fprintf(snapshot, "%c|%04o|%ju|%ju|%jd|%jd|%s\n", type, perm, uid, gid, size,
|
||||
time, path);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int create(const char *directory, const char *output_file) {
|
||||
if (directory == NULL || strlen(directory) == 0) {
|
||||
return -1;
|
||||
}
|
||||
if (output_file == NULL || strlen(output_file) == 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char *resolved = resolve_path(directory);
|
||||
if (resolved == NULL) {
|
||||
return -1;
|
||||
}
|
||||
FILE *f = fopen(output_file, "w+");
|
||||
if (f == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (walk(resolved, write_record, (void *)f) == -1) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (fclose(f) != 0) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
17
src/main.c
17
src/main.c
@@ -1,12 +1,13 @@
|
||||
#include "scan.h"
|
||||
#include "create.h"
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc < 2) {
|
||||
fprintf(stderr, "command not found [scan]\n");
|
||||
fprintf(stderr, "%s: command not found [scan|create]\n", argv[0]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
@@ -20,6 +21,18 @@ int main(int argc, char *argv[]) {
|
||||
fprintf(stderr, "scan failed: %s\n", strerror(err));
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
} else if (strcmp(cmd, "create") == 0) {
|
||||
if (argc < 4) {
|
||||
fprintf(stderr, "%s: command %s expected params [directory output_file]\n", argv[0], cmd);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
const char *directory = argv[2];
|
||||
const char *output_file = argv[3];
|
||||
if (create(directory, output_file) == -1) {
|
||||
const int err = errno;
|
||||
fprintf(stderr, "create failed: %s\n", strerror(err));
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "command %s is not valid\n", cmd);
|
||||
return EXIT_FAILURE;
|
||||
|
||||
18
src/scan.c
18
src/scan.c
@@ -1,16 +1,9 @@
|
||||
#define _XOPEN_SOURCE 500
|
||||
#include "scan.h"
|
||||
#include "utility.h"
|
||||
#include "walk.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef PATH_MAX
|
||||
static long pathmax = PATH_MAX;
|
||||
#else
|
||||
static long pathmax = 1024;
|
||||
#endif
|
||||
|
||||
static int print_file(const char *file, void *context) {
|
||||
(void)context;
|
||||
printf("[%s]\n", file);
|
||||
@@ -21,12 +14,9 @@ static int print_file(const char *file, void *context) {
|
||||
int scan(const char *directory) {
|
||||
if (directory == NULL || strlen(directory) == 0)
|
||||
return -1;
|
||||
char path[pathmax];
|
||||
const char *dir = NULL;
|
||||
if ((dir = realpath(directory, path)) == NULL) {
|
||||
perror("realpath");
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char *dir = resolve_path(directory);
|
||||
if (dir == NULL)
|
||||
return -1;
|
||||
return walk(dir, print_file, NULL);
|
||||
}
|
||||
|
||||
47
src/utility.c
Normal file
47
src/utility.c
Normal file
@@ -0,0 +1,47 @@
|
||||
#define _XOPEN_SOURCE 500
|
||||
#include "utility.h"
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
char *strconcat(const char *src, int n, ...) {
|
||||
va_list ap;
|
||||
|
||||
/* First pass: total length. strcat never reallocates, so the buffer has to
|
||||
be sized up front. */
|
||||
size_t len = strlen(src);
|
||||
va_start(ap, n);
|
||||
for (int i = 0; i < n; i++) {
|
||||
len += strlen(va_arg(ap, char *));
|
||||
}
|
||||
va_end(ap);
|
||||
|
||||
char *path = malloc(len + 1);
|
||||
if (path == NULL)
|
||||
return NULL;
|
||||
|
||||
size_t off = strlen(src);
|
||||
memcpy(path, src, off);
|
||||
va_start(ap, n);
|
||||
for (int i = 0; i < n; i++) {
|
||||
const char *s = va_arg(ap, char *);
|
||||
size_t l = strlen(s);
|
||||
memcpy(path + off, s, l);
|
||||
off += l;
|
||||
}
|
||||
va_end(ap);
|
||||
path[off] = '\0';
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
char *resolve_path(const char *path) {
|
||||
char resolved[pathmax];
|
||||
char *dir = NULL;
|
||||
if ((dir = realpath(path, resolved)) == NULL) {
|
||||
perror("realpath");
|
||||
return NULL;
|
||||
}
|
||||
return dir;
|
||||
}
|
||||
36
src/walk.c
36
src/walk.c
@@ -1,48 +1,18 @@
|
||||
#define _DEFAULT_SOURCE
|
||||
#define _GNU_SOURCE
|
||||
#include "walk.h"
|
||||
#include "utility.h"
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
static char *blacklist[] = {".", ".."};
|
||||
|
||||
static char *strconcat(const char *src, int n, ...) {
|
||||
va_list ap;
|
||||
|
||||
/* First pass: total length. strcat never reallocates, so the buffer has to
|
||||
be sized up front. */
|
||||
size_t len = strlen(src);
|
||||
va_start(ap, n);
|
||||
for (int i = 0; i < n; i++) {
|
||||
len += strlen(va_arg(ap, char *));
|
||||
}
|
||||
va_end(ap);
|
||||
|
||||
char *path = malloc(len + 1);
|
||||
if (path == NULL)
|
||||
return NULL;
|
||||
|
||||
size_t off = strlen(src);
|
||||
memcpy(path, src, off);
|
||||
va_start(ap, n);
|
||||
for (int i = 0; i < n; i++) {
|
||||
const char *s = va_arg(ap, char *);
|
||||
size_t l = strlen(s);
|
||||
memcpy(path + off, s, l);
|
||||
off += l;
|
||||
}
|
||||
va_end(ap);
|
||||
path[off] = '\0';
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
static int ignore_file(const char *name) {
|
||||
for (long unsigned int i = 0; i < sizeof(blacklist) / sizeof(char *); i++) {
|
||||
if (strcmp(name, blacklist[i]) == 0)
|
||||
if (strcmp(name, blacklist[i]) == 0 || strcasestr(name, ".fsnap"))
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user