]> git.seodisparate.com - SimpleArchiver/commitdiff
WIP white/black-list: Setup args in parser
authorStephen Seo <seo.disparate@gmail.com>
Mon, 17 Feb 2025 03:33:43 +0000 (12:33 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Mon, 17 Feb 2025 04:33:50 +0000 (13:33 +0900)
src/parser.c
src/parser.h

index c4b36954601bac96b838df15929ea9d231df931a..4ef9813e87b5b52f34e64edc4d96767e9a939424 100644 (file)
@@ -243,6 +243,30 @@ void simple_archiver_print_usage(void) {
           "--force-empty-dir-permissions <3-octal-values> : Force set EMPTY "
           "dir permissions. Like \"--force-dir-permissions\", but for empty "
           "directories.\n");
+  fprintf(stderr,
+          "--whitelist-contains <text> : Whitelist entries to contain "
+          "\"<text>\", specify multiple times to require multiple \"<text>\" "
+          "entries at once.\n");
+  fprintf(stderr,
+          "--whitelist-begins-with <text> : Whitelist entries to start with "
+          "\"<text>\", specify multiple times to allow different entries to "
+          "start with different \"<text>\" entries.\n");
+  fprintf(stderr,
+          "--whitelist-ends-with <text> : Whitelist entries to end with "
+          "\"<text>\", specify multiple times to allow different entries to end"
+          " with different \"<text>\" entries.\n");
+  fprintf(stderr,
+          "--blacklist-contains <text> : blacklist entries that contains "
+          "\"<text>\", specify multiple times to deny multiple \"<text>\" "
+          "entries at once.\n");
+  fprintf(stderr,
+          "--blacklist-begins-with <text> : blacklist entries that starts with "
+          "\"<text>\", specify multiple times to deny multiple entries "
+          "starting with different \"<text>\" entries.\n");
+  fprintf(stderr,
+          "--blacklist-ends-with <text> : blacklist entries that ends with "
+          "\"<text>\", specify multiple times to deny multiple entries ending "
+          "with different \"<text>\" entries.\n");
   fprintf(stderr, "--version : prints version and exits\n");
   fprintf(stderr,
           "-- : specifies remaining arguments are files to archive/extract\n");
@@ -285,6 +309,12 @@ SDArchiverParsed simple_archiver_create_parsed(void) {
   parsed.mappings.GidToGid = simple_archiver_hash_map_init();
   parsed.mappings.GnameToGname = simple_archiver_hash_map_init();
   parsed.prefix = NULL;
+  parsed.whitelist_contains = NULL;
+  parsed.whitelist_begins = NULL;
+  parsed.whitelist_ends = NULL;
+  parsed.blacklist_contains = NULL;
+  parsed.blacklist_begins = NULL;
+  parsed.blacklist_ends = NULL;
 
   return parsed;
 }
@@ -683,6 +713,112 @@ int simple_archiver_parse_args(int argc, const char **argv,
 
         out->flags |= 0x10000;
 
+        --argc;
+        ++argv;
+      } else if (strcmp(argv[0], "--whitelist-contains") == 0) {
+        if (argc < 2) {
+          fprintf(stderr, "ERROR: --whitelist-contains expects an argument!\n");
+          simple_archiver_print_usage();
+          return 1;
+        }
+
+        if (!out->whitelist_contains) {
+          out->whitelist_contains = simple_archiver_list_init();
+        }
+        simple_archiver_list_add(
+          out->whitelist_contains,
+          (void *)argv[1],
+          simple_archiver_helper_datastructure_cleanup_nop);
+
+        --argc;
+        ++argv;
+      } else if (strcmp(argv[0], "--whitelist-begins-with") == 0) {
+        if (argc < 2) {
+          fprintf(stderr,
+                  "ERROR: --whitelist-begins-with expects an argument!\n");
+          simple_archiver_print_usage();
+          return 1;
+        }
+
+        if (!out->whitelist_begins) {
+          out->whitelist_begins = simple_archiver_list_init();
+        }
+        simple_archiver_list_add(
+          out->whitelist_begins,
+          (void *)argv[1],
+          simple_archiver_helper_datastructure_cleanup_nop);
+
+        --argc;
+        ++argv;
+      } else if (strcmp(argv[0], "--whitelist-ends-with") == 0) {
+        if (argc < 2) {
+          fprintf(stderr,
+                  "ERROR: --whitelist-ends-with expects an argument!\n");
+          simple_archiver_print_usage();
+          return 1;
+        }
+
+        if (!out->whitelist_ends) {
+          out->whitelist_ends = simple_archiver_list_init();
+        }
+        simple_archiver_list_add(
+          out->whitelist_ends,
+          (void *)argv[1],
+          simple_archiver_helper_datastructure_cleanup_nop);
+
+        --argc;
+        ++argv;
+      } else if (strcmp(argv[0], "--blacklist-contains") == 0) {
+        if (argc < 2) {
+          fprintf(stderr, "ERROR: --blacklist-contains expects an argument!\n");
+          simple_archiver_print_usage();
+          return 1;
+        }
+
+        if (!out->blacklist_contains) {
+          out->blacklist_contains = simple_archiver_list_init();
+        }
+        simple_archiver_list_add(
+          out->blacklist_contains,
+          (void *)argv[1],
+          simple_archiver_helper_datastructure_cleanup_nop);
+
+        --argc;
+        ++argv;
+      } else if (strcmp(argv[0], "--blacklist-begins-with") == 0) {
+        if (argc < 2) {
+          fprintf(stderr,
+                  "ERROR: --blacklist-begins-with expects an argument!\n");
+          simple_archiver_print_usage();
+          return 1;
+        }
+
+        if (!out->blacklist_begins) {
+          out->blacklist_begins = simple_archiver_list_init();
+        }
+        simple_archiver_list_add(
+          out->blacklist_begins,
+          (void *)argv[1],
+          simple_archiver_helper_datastructure_cleanup_nop);
+
+        --argc;
+        ++argv;
+      } else if (strcmp(argv[0], "--blacklist-ends-with") == 0) {
+        if (argc < 2) {
+          fprintf(stderr,
+                  "ERROR: --blacklist-ends-with expects an argument!\n");
+          simple_archiver_print_usage();
+          return 1;
+        }
+
+        if (!out->blacklist_ends) {
+          out->blacklist_ends = simple_archiver_list_init();
+        }
+        simple_archiver_list_add(
+          out->blacklist_ends,
+          (void *)argv[1],
+          simple_archiver_helper_datastructure_cleanup_nop);
+
         --argc;
         ++argv;
       } else if (strcmp(argv[0], "--version") == 0) {
@@ -797,6 +933,25 @@ void simple_archiver_free_parsed(SDArchiverParsed *parsed) {
   if (parsed->prefix) {
     free(parsed->prefix);
   }
+
+  if (parsed->whitelist_contains) {
+    simple_archiver_list_free(&parsed->whitelist_contains);
+  }
+  if (parsed->whitelist_begins) {
+    simple_archiver_list_free(&parsed->whitelist_begins);
+  }
+  if (parsed->whitelist_ends) {
+    simple_archiver_list_free(&parsed->whitelist_ends);
+  }
+  if (parsed->blacklist_contains) {
+    simple_archiver_list_free(&parsed->blacklist_contains);
+  }
+  if (parsed->blacklist_begins) {
+    simple_archiver_list_free(&parsed->blacklist_begins);
+  }
+  if (parsed->blacklist_ends) {
+    simple_archiver_list_free(&parsed->blacklist_ends);
+  }
 }
 
 SDArchiverLinkedList *simple_archiver_parsed_to_filenames(
index ff0755df8ac62778ea3ff40c856ea0c001ad2888..bfecf6206cf60ce6eadf7dacadbd8b9188356435 100644 (file)
@@ -97,6 +97,12 @@ typedef struct SDArchiverParsed {
   SDA_UGMapping mappings;
   /// Prefix for archived/extracted paths.
   char *prefix;
+  SDArchiverLinkedList *whitelist_contains;
+  SDArchiverLinkedList *whitelist_begins;
+  SDArchiverLinkedList *whitelist_ends;
+  SDArchiverLinkedList *blacklist_contains;
+  SDArchiverLinkedList *blacklist_begins;
+  SDArchiverLinkedList *blacklist_ends;
 } SDArchiverParsed;
 
 typedef struct SDArchiverFileInfo {