diff options
| author | Aargh Rai <aargh.rai+git@gmail.com> | 2026-10-20 11:20:15 +0530 |
|---|---|---|
| committer | Aargh Rai <aargh.rai+git@gmail.com> | 2026-10-20 11:20:15 +0530 |
| commit | 0f3997d6ff2d25c0941824ec3daa3c8e767bd73e (patch) | |
| tree | d6110910cfa976b5bfd57139e3810d2924bbba9e /src/main.c | |
| parent | 790131205281884585d4418ae7093636d6e61c97 (diff) | |
wrote the entire thing using libgit2
Diffstat (limited to 'src/main.c')
| -rw-r--r--[-rwxr-xr-x] | src/main.c | 143 |
1 files changed, 31 insertions, 112 deletions
diff --git a/src/main.c b/src/main.c index 7d33850..1d419fa 100755..100644 --- a/src/main.c +++ b/src/main.c @@ -1,125 +1,44 @@ -#include "lib.h" +#include <unistd.h> +#include <assert.h> -#define VERSION "1.0" +#include "lib.h" -void help() { - printf( -"Backup your files\n" -"Usage: Backbencher [OPTIONS] [input]\n" -"Arguments:\n" -"\t[input]... The path of the folder to backup\n" -"Options:\n" -"\t-i --ignore-file <filename> Specify which files to exclude\n\t\t(default = ~/.config/backbencher/.backupignore)\n" -"\t-h --help \tPrint help\n" -"\t-v --verbose \tVerbose logging\n" -"\t-V --version \tPrint version\n" - ); -} +int main(int argc, char **argv) { + (void)argv; -void assign(char* to, char* from) { - int i = 0; - for (; from[i] != 0; i++) { - to[i] = from[i]; - } - to[i] = 0; -} + debug = argc > 1; + if (check_for_backupignore()) return 1; + debugf(".backupignore found\n"); -int source_main(int argc, char** argv) { - shift(&argc, &argv); + struct tmp_repo repo = tmp_repo_init(); + assert(repo.repo != NULL); + debugf("%s initialized\n", repo.path); - char* ignore_file = get_default_config(); - char* target = NULL_STR; - int VERBOSE = 0; + copy_over_backupignore(repo); + debugf("ignore file loaded\n"); - while (1) { - char* segment = shift(&argc, &argv); - if (segment == NULL_STR) { - break; - } - else if (segment[0] == '-' && segment[1] == '-') { - if (strcmp(&segment[2], "ignore-file") == 0) { - assign(&ignore_file[0], shift(&argc, &argv)); - if (ignore_file == NULL_STR) { - fprintf(stderr, "Missing <filename> for --ignore-file\n"); - return 1; - } - } - else if (strcmp(&segment[2], "help") == 0) { - help(); - return 0; - } - else if (strcmp(&segment[2], "verbose") == 0) { - VERBOSE = 1; - } - else if (strcmp(&segment[2], "version") == 0) { - printf(VERSION); - printf("\n"); - return 0; - } - } else if (segment[0] == '-') { - if (segment[1] == 'i') { - assign(&ignore_file[0], shift(&argc, &argv)); - if (ignore_file == NULL_STR) { - fprintf(stderr, "Missing <filename> for --ignore-file\n"); - return 1; - } - } - else if (segment[1] == 'h') { - help(); - return 0; - } - else if (segment[1] == 'v') { - VERBOSE = 1; - } - else if (segment[1] == 'V') { - printf(VERSION); - printf("\n"); - return 0; - } - } else { - target = segment; - } - } + char *cwd = getcwd(NULL, 0); + assert(cwd != NULL); + struct backup_streamer streamer = backup_streamer_init(cwd); + free(cwd); - if (target == NULL_STR) { - fprintf(stderr, "Missing [input] - The folder to backup\n"); - return 1; - } + struct recursive_reader reader = recursive_reader_init(); + while (recursive_reader_read(&reader) != -1) { + if (is_ignored(repo, reader.line)) continue; + if (is_dir(reader.line)) continue; - if (VERBOSE) { - printf("Target folder: %s\n", target); - printf("Using ignore file: %s\n", ignore_file); - printf("Using verbosity: %d\n", VERBOSE); + get_backup_destination(&streamer, reader.line); + debugf("cp %s %s\n", reader.line, streamer.cursor); + backup_file(reader.line, streamer.cursor); } - char* ignore_instructions = get_ignore_file(ignore_file, VERBOSE); - if (ignore_instructions == NULL) { - fprintf(stderr, "I don't have permissions to read the ignore file\n"); - return 1; - } - if (VERBOSE) { - printf("Ignoring rules:\n%s\n", ignore_instructions); - } - - backup(target, ignore_instructions, VERBOSE); - - free(ignore_instructions); + recursive_reader_deinit(reader); + tmp_repo_deinit(repo); + git_repository_free(repo.repo); + git_libgit2_shutdown(); return 0; } -int main(int argc, char** argv) { - int result = source_main(argc, argv); - if (result != 0) { - printf("\n"); - help(); - } - return result; -} - -char* shift(int* argc, char*** argv) { - if (argc == 0) return NULL_STR; - --(*argc); - char* output = **argv; - ++(*argv); - return output; -} +#include "tmp_repo.c" +#include "recursive_reader.c" +#include "backup_streamer.c" |
