]> git.seodisparate.com - SimpleArchiver/commitdiff
WIP white/black-list: refactoring
authorStephen Seo <seo.disparate@gmail.com>
Mon, 17 Feb 2025 04:53:27 +0000 (13:53 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Mon, 17 Feb 2025 04:53:27 +0000 (13:53 +0900)
src/archiver.c
src/helpers.c
src/helpers.h

index 59b6797bf6ea4c81db4d7b8eab1c6f2b827c71fb..ea07a74bef2128b46d443f92480c0f74e6e55d87 100644 (file)
@@ -1704,86 +1704,15 @@ int symlinks_and_files_from_files(void *data, void *ud) {
 
   if (file_info->filename) {
     // Check white/black lists.
-    if (state->parsed->whitelist_contains) {
-      for (SDArchiverLLNode *node =
-            state->parsed->whitelist_contains->head->next;
-          node != state->parsed->whitelist_contains->tail;
-          node = node->next) {
-        if (node->data) {
-          if (!simple_archiver_helper_string_contains(file_info->filename,
-                                                      node->data)) {
-            // Skipping since filename does not contain whitelist text.
-            return 0;
-          }
-        }
-      }
-    }
-    if (state->parsed->whitelist_begins) {
-      for (SDArchiverLLNode *node = state->parsed->whitelist_begins->head->next;
-          node != state->parsed->whitelist_begins->tail;
-          node = node->next) {
-        if (node->data) {
-          if (!simple_archiver_helper_string_starts(file_info->filename,
-                                                    node->data)) {
-            // Skipping since filename does not start with whitelist text.
-            return 0;
-          }
-        }
-      }
-    }
-    if (state->parsed->whitelist_ends) {
-      for (SDArchiverLLNode *node = state->parsed->whitelist_ends->head->next;
-          node != state->parsed->whitelist_ends->tail;
-          node = node->next) {
-        if (node->data) {
-          if (!simple_archiver_helper_string_ends(file_info->filename,
-                                                  node->data)) {
-            // Skipping since filename does not end with whitelist text.
-            return 0;
-          }
-        }
-      }
-    }
-
-    if (state->parsed->blacklist_contains) {
-      for (SDArchiverLLNode *node =
-            state->parsed->blacklist_contains->head->next;
-          node != state->parsed->blacklist_contains->tail;
-          node = node->next) {
-        if (node->data) {
-          if (simple_archiver_helper_string_contains(file_info->filename,
-                                                     node->data)) {
-            // Skipping since filename contains blacklist text.
-            return 0;
-          }
-        }
-      }
-    }
-    if (state->parsed->blacklist_begins) {
-      for (SDArchiverLLNode *node = state->parsed->blacklist_begins->head->next;
-          node != state->parsed->blacklist_begins->tail;
-          node = node->next) {
-        if (node->data) {
-          if (simple_archiver_helper_string_starts(file_info->filename,
-                                                   node->data)) {
-            // Skipping since filename starts with blacklist text.
-            return 0;
-          }
-        }
-      }
-    }
-    if (state->parsed->blacklist_ends) {
-      for (SDArchiverLLNode *node = state->parsed->blacklist_ends->head->next;
-          node != state->parsed->blacklist_ends->tail;
-          node = node->next) {
-        if (node->data) {
-          if (simple_archiver_helper_string_ends(file_info->filename,
-                                                 node->data)) {
-            // Skipping since filename ends with blacklist text.
-            return 0;
-          }
-        }
-      }
+    if (!simple_archiver_helper_string_allowed_lists(
+        file_info->filename,
+        state->parsed->whitelist_contains,
+        state->parsed->whitelist_begins,
+        state->parsed->whitelist_ends,
+        state->parsed->blacklist_contains,
+        state->parsed->blacklist_begins,
+        state->parsed->blacklist_ends)) {
+      return 0;
     }
 
     if (file_info->link_dest) {
index 6c65f27419ec3ccd85a35d7e414ddb0cc4e28d88..5d102e0bfc1f32ea41b0d24e0eb2d9f05f8c6039 100644 (file)
@@ -682,3 +682,82 @@ uint_fast8_t simple_archiver_helper_string_ends(const char *cstring,
 
   return 0;
 }
+
+uint_fast8_t simple_archiver_helper_string_allowed_lists(
+    const char *cstring,
+    const SDArchiverLinkedList *w_contains,
+    const SDArchiverLinkedList *w_begins,
+    const SDArchiverLinkedList *w_ends,
+    const SDArchiverLinkedList *b_contains,
+    const SDArchiverLinkedList *b_begins,
+    const SDArchiverLinkedList *b_ends) {
+  if (w_contains) {
+    for (const SDArchiverLLNode *node = w_contains->head->next;
+        node != w_contains->tail;
+        node = node->next) {
+      if (node->data) {
+        if (!simple_archiver_helper_string_contains(cstring, node->data)) {
+          return 0;
+        }
+      }
+    }
+  }
+  if (w_begins) {
+    for (const SDArchiverLLNode *node = w_begins->head->next;
+        node != w_begins->tail;
+        node = node->next) {
+      if (node->data) {
+        if (!simple_archiver_helper_string_starts(cstring, node->data)) {
+          return 0;
+        }
+      }
+    }
+  }
+  if (w_ends) {
+    for (const SDArchiverLLNode *node = w_ends->head->next;
+        node != w_ends->tail;
+        node = node->next) {
+      if (node->data) {
+        if (!simple_archiver_helper_string_ends(cstring, node->data)) {
+          return 0;
+        }
+      }
+    }
+  }
+
+  if (b_contains) {
+    for (const SDArchiverLLNode *node = b_contains->head->next;
+        node != b_contains->tail;
+        node = node->next) {
+      if (node->data) {
+        if (simple_archiver_helper_string_contains(cstring, node->data)) {
+          return 0;
+        }
+      }
+    }
+  }
+  if (b_begins) {
+    for (const SDArchiverLLNode *node = b_begins->head->next;
+        node != b_begins->tail;
+        node = node->next) {
+      if (node->data) {
+        if (simple_archiver_helper_string_starts(cstring, node->data)) {
+          return 0;
+        }
+      }
+    }
+  }
+  if (b_ends) {
+    for (const SDArchiverLLNode *node = b_ends->head->next;
+        node != b_ends->tail;
+        node = node->next) {
+      if (node->data) {
+        if (simple_archiver_helper_string_ends(cstring, node->data)) {
+          return 0;
+        }
+      }
+    }
+  }
+
+  return 1;
+}
index 8a5f18e03f5b3104998152ddd469c23e3dc7e1d7..37d549379de807719d66ca1851722ec00b53c71b 100644 (file)
@@ -131,4 +131,14 @@ uint_fast8_t simple_archiver_helper_string_starts(const char *cstring,
 uint_fast8_t simple_archiver_helper_string_ends(const char *cstring,
                                                 const char *ends);
 
+// Returns non-zero if "cstring" is allowed by lists.
+uint_fast8_t simple_archiver_helper_string_allowed_lists(
+  const char *cstring,
+  const SDArchiverLinkedList *w_contains,
+  const SDArchiverLinkedList *w_begins,
+  const SDArchiverLinkedList *w_ends,
+  const SDArchiverLinkedList *b_contains,
+  const SDArchiverLinkedList *b_begins,
+  const SDArchiverLinkedList *b_ends);
+
 #endif