]> git.seodisparate.com - SimpleArchiver/commitdiff
WIP white/black-list: change "contains" to any/all
authorStephen Seo <seo.disparate@gmail.com>
Tue, 18 Feb 2025 04:24:39 +0000 (13:24 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Tue, 18 Feb 2025 04:24:39 +0000 (13:24 +0900)
Changed `--whitelist-contains` and `--blacklist-contains` into:

`--whitelist-contains-any`
`--whitelist-contains-all`
`--blacklist-contains-any`
`--blacklist-contains-all`

README.md
src/helpers.c
src/parser.c
src/parser.h

index 0eaab95a12e6bea2c2c4a8f91e2e5cb37b68d89d..e9852dd4d5e3c5cf15808916479e8c15e3c3ee8f 100644 (file)
--- a/README.md
+++ b/README.md
@@ -53,10 +53,12 @@ API calls.
     --force-dir-permissions <3-octal-values> : Force set permissions for directories on archive creation/extraction
       Must be three octal characters like "755" or "440"
     --force-empty-dir-permissions <3-octal-values> : Force set EMPTY dir permissions. Like "--force-dir-permissions", but for empty directories.
-    --whitelist-contains <text> : Whitelist entries to contain "<text>", specify multiple times to require multiple "<text>" entries at once.
+    --whitelist-contains-any <text> : Whitelist entries to contain "<text>", specify multiple times to allow entries that contain any of the specified "<text>"s.
+    --whitelist-contains-all <text> : Whitelist entries to contain "<text>", specify multiple times to allow entries that contain all of the specified "<text>"s.
     --whitelist-begins-with <text> : Whitelist entries to start with "<text>", specify multiple times to allow different entries to start with different "<text>" entries.
     --whitelist-ends-with <text> : Whitelist entries to end with "<text>", specify multiple times to allow different entries to end with different "<text>" entries.
-    --blacklist-contains <text> : blacklist entries that contains "<text>", specify multiple times to deny entries that contain all the specified "<text>"s.
+    --blacklist-contains-any <text> : blacklist entries that contains "<text>", specify multiple times to deny entries that contain any of the specified "<text>"s.
+    --blacklist-contains-all <text> : blacklist entries that contains "<text>", specify multiple times to deny entries that contain all of the specified "<text>"s.
     --blacklist-begins-with <text> : blacklist entries that starts with "<text>", specify multiple times to deny multiple entries starting with different "<text>" entries.
     --blacklist-ends-with <text> : blacklist entries that ends with "<text>", specify multiple times to deny multiple entries ending with different "<text>" entries.
     --wb-case-insensitive : Makes white/black-list checking case insensitive.
index d8e1068aca7be30a4ec2a8461367879acac62c50..fc17982d84f69c69d9cd4194a59b2f7e7bcfcfe4 100644 (file)
@@ -723,9 +723,26 @@ uint_fast8_t simple_archiver_helper_string_allowed_lists(
     const char *cstring,
     uint_fast8_t case_i,
     const SDArchiverParsed *parsed) {
-  if (parsed->whitelist_contains) {
-    for (const SDArchiverLLNode *node = parsed->whitelist_contains->head->next;
-        node != parsed->whitelist_contains->tail;
+  if (parsed->whitelist_contains_any) {
+    uint_fast8_t contains_any = 0;
+    for (const SDArchiverLLNode *node = parsed->whitelist_contains_any->head->next;
+        node != parsed->whitelist_contains_any->tail;
+        node = node->next) {
+      if (node->data) {
+        if (simple_archiver_helper_string_contains(
+            cstring, node->data, case_i)) {
+          contains_any = 1;
+          break;
+        }
+      }
+    }
+    if (!contains_any) {
+      return 0;
+    }
+  }
+  if (parsed->whitelist_contains_all) {
+    for (const SDArchiverLLNode *node = parsed->whitelist_contains_all->head->next;
+        node != parsed->whitelist_contains_all->tail;
         node = node->next) {
       if (node->data) {
         if (!simple_archiver_helper_string_contains(
@@ -759,10 +776,22 @@ uint_fast8_t simple_archiver_helper_string_allowed_lists(
     }
   }
 
-  if (parsed->blacklist_contains) {
+  if (parsed->blacklist_contains_any) {
+    for (const SDArchiverLLNode *node = parsed->blacklist_contains_any->head->next;
+        node != parsed->blacklist_contains_any->tail;
+        node = node->next) {
+      if (node->data) {
+        if (simple_archiver_helper_string_contains(
+            cstring, node->data, case_i)) {
+          return 0;
+        }
+      }
+    }
+  }
+  if (parsed->blacklist_contains_all) {
     uint_fast8_t contains_all = 1;
-    for (const SDArchiverLLNode *node = parsed->blacklist_contains->head->next;
-        node != parsed->blacklist_contains->tail;
+    for (const SDArchiverLLNode *node = parsed->blacklist_contains_all->head->next;
+        node != parsed->blacklist_contains_all->tail;
         node = node->next) {
       if (node->data) {
         if (!simple_archiver_helper_string_contains(
index 7a52ccd2479a20c6a806b5d66c95e2cd1d69f6ac..8374f2d11e1cba72afb449326be5e9c38e7d06dd 100644 (file)
@@ -244,9 +244,13 @@ void simple_archiver_print_usage(void) {
           "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");
+          "--whitelist-contains-any <text> : Whitelist entries to contain "
+          "\"<text>\", specify multiple times to allow entries that contain "
+          "any of the specified \"<text>\"s.\n");
+  fprintf(stderr,
+          "--whitelist-contains-all <text> : Whitelist entries to contain "
+          "\"<text>\", specify multiple times to allow entries that contain "
+          "all of the specified \"<text>\"s.\n");
   fprintf(stderr,
           "--whitelist-begins-with <text> : Whitelist entries to start with "
           "\"<text>\", specify multiple times to allow different entries to "
@@ -256,9 +260,13 @@ void simple_archiver_print_usage(void) {
           "\"<text>\", specify multiple times to allow different entries to end"
           " with different \"<text>\" entries.\n");
   fprintf(stderr,
-          "--blacklist-contains <text> : blacklist entries that contains "
+          "--blacklist-contains-any <text> : blacklist entries that contains "
+          "\"<text>\", specify multiple times to deny entries that contain any"
+          " of the specified \"<text>\"s.\n");
+  fprintf(stderr,
+          "--blacklist-contains-all <text> : blacklist entries that contains "
           "\"<text>\", specify multiple times to deny entries that contain all"
-          " the specified \"<text>\"s.\n");
+          " of the specified \"<text>\"s.\n");
   fprintf(stderr,
           "--blacklist-begins-with <text> : blacklist entries that starts with "
           "\"<text>\", specify multiple times to deny multiple entries "
@@ -312,10 +320,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_contains_any = NULL;
+  parsed.whitelist_contains_all = NULL;
   parsed.whitelist_begins = NULL;
   parsed.whitelist_ends = NULL;
-  parsed.blacklist_contains = NULL;
+  parsed.blacklist_contains_any = NULL;
+  parsed.blacklist_contains_all = NULL;
   parsed.blacklist_begins = NULL;
   parsed.blacklist_ends = NULL;
 
@@ -718,18 +728,37 @@ int simple_archiver_parse_args(int argc, const char **argv,
 
         --argc;
         ++argv;
-      } else if (strcmp(argv[0], "--whitelist-contains") == 0) {
+      } else if (strcmp(argv[0], "--whitelist-contains-any") == 0) {
+        if (argc < 2) {
+          fprintf(stderr,
+                  "ERROR: --whitelist-contains-any expects an argument!\n");
+          simple_archiver_print_usage();
+          return 1;
+        }
+
+        if (!out->whitelist_contains_any) {
+          out->whitelist_contains_any = simple_archiver_list_init();
+        }
+        simple_archiver_list_add(
+          out->whitelist_contains_any,
+          (void *)argv[1],
+          simple_archiver_helper_datastructure_cleanup_nop);
+
+        --argc;
+        ++argv;
+      } else if (strcmp(argv[0], "--whitelist-contains-all") == 0) {
         if (argc < 2) {
-          fprintf(stderr, "ERROR: --whitelist-contains expects an argument!\n");
+          fprintf(stderr,
+                  "ERROR: --whitelist-contains-all expects an argument!\n");
           simple_archiver_print_usage();
           return 1;
         }
 
-        if (!out->whitelist_contains) {
-          out->whitelist_contains = simple_archiver_list_init();
+        if (!out->whitelist_contains_all) {
+          out->whitelist_contains_all = simple_archiver_list_init();
         }
         simple_archiver_list_add(
-          out->whitelist_contains,
+          out->whitelist_contains_all,
           (void *)argv[1],
           simple_archiver_helper_datastructure_cleanup_nop);
 
@@ -771,18 +800,37 @@ int simple_archiver_parse_args(int argc, const char **argv,
 
         --argc;
         ++argv;
-      } else if (strcmp(argv[0], "--blacklist-contains") == 0) {
+      } else if (strcmp(argv[0], "--blacklist-contains-any") == 0) {
         if (argc < 2) {
-          fprintf(stderr, "ERROR: --blacklist-contains expects an argument!\n");
+          fprintf(stderr,
+                  "ERROR: --blacklist-contains-any expects an argument!\n");
+          simple_archiver_print_usage();
+          return 1;
+        }
+
+        if (!out->blacklist_contains_any) {
+          out->blacklist_contains_any = simple_archiver_list_init();
+        }
+        simple_archiver_list_add(
+          out->blacklist_contains_any,
+          (void *)argv[1],
+          simple_archiver_helper_datastructure_cleanup_nop);
+
+        --argc;
+        ++argv;
+      } else if (strcmp(argv[0], "--blacklist-contains-all") == 0) {
+        if (argc < 2) {
+          fprintf(stderr,
+                  "ERROR: --blacklist-contains-all expects an argument!\n");
           simple_archiver_print_usage();
           return 1;
         }
 
-        if (!out->blacklist_contains) {
-          out->blacklist_contains = simple_archiver_list_init();
+        if (!out->blacklist_contains_all) {
+          out->blacklist_contains_all = simple_archiver_list_init();
         }
         simple_archiver_list_add(
-          out->blacklist_contains,
+          out->blacklist_contains_all,
           (void *)argv[1],
           simple_archiver_helper_datastructure_cleanup_nop);
 
@@ -939,8 +987,11 @@ void simple_archiver_free_parsed(SDArchiverParsed *parsed) {
     free(parsed->prefix);
   }
 
-  if (parsed->whitelist_contains) {
-    simple_archiver_list_free(&parsed->whitelist_contains);
+  if (parsed->whitelist_contains_any) {
+    simple_archiver_list_free(&parsed->whitelist_contains_any);
+  }
+  if (parsed->whitelist_contains_all) {
+    simple_archiver_list_free(&parsed->whitelist_contains_all);
   }
   if (parsed->whitelist_begins) {
     simple_archiver_list_free(&parsed->whitelist_begins);
@@ -948,8 +999,11 @@ void simple_archiver_free_parsed(SDArchiverParsed *parsed) {
   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_contains_any) {
+    simple_archiver_list_free(&parsed->blacklist_contains_any);
+  }
+  if (parsed->blacklist_contains_all) {
+    simple_archiver_list_free(&parsed->blacklist_contains_all);
   }
   if (parsed->blacklist_begins) {
     simple_archiver_list_free(&parsed->blacklist_begins);
index 241d595f7315ecb36fa8315894a84912f12c563b..7c6a9c5ba447961f045f6c1127e5c48321acf9f8 100644 (file)
@@ -99,10 +99,12 @@ typedef struct SDArchiverParsed {
   SDA_UGMapping mappings;
   /// Prefix for archived/extracted paths.
   char *prefix;
-  SDArchiverLinkedList *whitelist_contains;
+  SDArchiverLinkedList *whitelist_contains_any;
+  SDArchiverLinkedList *whitelist_contains_all;
   SDArchiverLinkedList *whitelist_begins;
   SDArchiverLinkedList *whitelist_ends;
-  SDArchiverLinkedList *blacklist_contains;
+  SDArchiverLinkedList *blacklist_contains_any;
+  SDArchiverLinkedList *blacklist_contains_all;
   SDArchiverLinkedList *blacklist_begins;
   SDArchiverLinkedList *blacklist_ends;
 } SDArchiverParsed;