]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
Merge pull request #2633 from stoeckmann/match_cleanup
authorTim Kientzle <kientzle@acm.org>
Sat, 24 May 2025 17:37:02 +0000 (10:37 -0700)
committerMartin Matuska <martin@matuska.de>
Sat, 31 May 2025 19:25:32 +0000 (21:25 +0200)
archive_match: Simplify and clean up code

(cherry picked from commit 6dbea2df3bc8288d52c65cb3d543a1e883162a20)

libarchive/archive_match.c

index 20880d3d39af88a161d1ab3758d0618f9c7b45cd..51a0e3fd57789e63cd3cd570bc73838555d8fa99 100644 (file)
@@ -35,6 +35,9 @@
 #ifdef HAVE_STRING_H
 #include <string.h>
 #endif
+#ifdef HAVE_LIMITS_H
+#include <limits.h>
+#endif
 
 #include "archive.h"
 #include "archive_private.h"
@@ -53,8 +56,7 @@ struct match {
 struct match_list {
        struct match            *first;
        struct match            **last;
-       int                      count;
-       int                      unmatched_count;
+       size_t                   unmatched_count;
        struct match            *unmatched_next;
        int                      unmatched_eof;
 };
@@ -73,7 +75,6 @@ struct match_file {
 struct entry_list {
        struct match_file       *first;
        struct match_file       **last;
-       int                      count;
 };
 
 struct id_array {
@@ -144,12 +145,15 @@ static int        add_pattern_mbs(struct archive_match *, struct match_list *,
                    const char *);
 static int     add_pattern_wcs(struct archive_match *, struct match_list *,
                    const wchar_t *);
+#if !defined(_WIN32) || defined(__CYGWIN__)
 static int     cmp_key_mbs(const struct archive_rb_node *, const void *);
-static int     cmp_key_wcs(const struct archive_rb_node *, const void *);
 static int     cmp_node_mbs(const struct archive_rb_node *,
                    const struct archive_rb_node *);
+#else
+static int     cmp_key_wcs(const struct archive_rb_node *, const void *);
 static int     cmp_node_wcs(const struct archive_rb_node *,
                    const struct archive_rb_node *);
+#endif
 static void    entry_list_add(struct entry_list *, struct match_file *);
 static void    entry_list_free(struct entry_list *);
 static void    entry_list_init(struct entry_list *);
@@ -189,12 +193,12 @@ static int        validate_time_flag(struct archive *, int, const char *);
 
 #define get_date archive_parse_date
 
-static const struct archive_rb_tree_ops rb_ops_mbs = {
+static const struct archive_rb_tree_ops rb_ops = {
+#if !defined(_WIN32) || defined(__CYGWIN__)
        cmp_node_mbs, cmp_key_mbs
-};
-
-static const struct archive_rb_tree_ops rb_ops_wcs = {
+#else
        cmp_node_wcs, cmp_key_wcs
+#endif
 };
 
 /*
@@ -228,7 +232,7 @@ archive_match_new(void)
        a->recursive_include = 1;
        match_list_init(&(a->inclusions));
        match_list_init(&(a->exclusions));
-       __archive_rb_tree_init(&(a->exclusion_tree), &rb_ops_mbs);
+       __archive_rb_tree_init(&(a->exclusion_tree), &rb_ops);
        entry_list_init(&(a->exclusion_entry_list));
        match_list_init(&(a->inclusion_unames));
        match_list_init(&(a->inclusion_gnames));
@@ -507,7 +511,9 @@ archive_match_path_unmatched_inclusions(struct archive *_a)
            ARCHIVE_STATE_NEW, "archive_match_unmatched_inclusions");
        a = (struct archive_match *)_a;
 
-       return (a->inclusions.unmatched_count);
+       if (a->inclusions.unmatched_count > (size_t)INT_MAX)
+               return INT_MAX;
+       return (int)(a->inclusions.unmatched_count);
 }
 
 int
@@ -650,7 +656,7 @@ add_pattern_from_file(struct archive_match *a, struct match_list *mlist,
                                                break;
                                        }
                                } else {
-                                       if (*b == 0x0d || *b == 0x0a) {
+                                       if (*b == 0x0d || *b == 0x0a) {
                                                found_separator = 1;
                                                break;
                                        }
@@ -735,7 +741,7 @@ path_excluded(struct archive_match *a, int mbs, const void *pathname)
                }
        }
 
-       /* Exclusions take priority */
+       /* Exclusions take priority. */
        for (match = a->exclusions.first; match != NULL;
            match = match->next){
                r = match_path_exclusion(a, match, mbs, pathname);
@@ -834,7 +840,6 @@ match_list_init(struct match_list *list)
 {
        list->first = NULL;
        list->last = &(list->first);
-       list->count = 0;
 }
 
 static void
@@ -855,7 +860,6 @@ match_list_add(struct match_list *list, struct match *m)
 {
        *list->last = m;
        list->last = &(m->next);
-       list->count++;
        list->unmatched_count++;
 }
 
@@ -1275,6 +1279,7 @@ set_timefilter_pathname_wcs(struct archive_match *a, int timetype,
 /*
  * Call back functions for archive_rb.
  */
+#if !defined(_WIN32) || defined(__CYGWIN__)
 static int
 cmp_node_mbs(const struct archive_rb_node *n1,
     const struct archive_rb_node *n2)
@@ -1291,7 +1296,7 @@ cmp_node_mbs(const struct archive_rb_node *n1,
                return (-1);
        return (strcmp(p1, p2));
 }
-        
+
 static int
 cmp_key_mbs(const struct archive_rb_node *n, const void *key)
 {
@@ -1303,7 +1308,7 @@ cmp_key_mbs(const struct archive_rb_node *n, const void *key)
                return (-1);
        return (strcmp(p, (const char *)key));
 }
-
+#else
 static int
 cmp_node_wcs(const struct archive_rb_node *n1,
     const struct archive_rb_node *n2)
@@ -1320,7 +1325,7 @@ cmp_node_wcs(const struct archive_rb_node *n1,
                return (-1);
        return (wcscmp(p1, p2));
 }
-        
+
 static int
 cmp_key_wcs(const struct archive_rb_node *n, const void *key)
 {
@@ -1332,13 +1337,13 @@ cmp_key_wcs(const struct archive_rb_node *n, const void *key)
                return (-1);
        return (wcscmp(p, (const wchar_t *)key));
 }
+#endif
 
 static void
 entry_list_init(struct entry_list *list)
 {
        list->first = NULL;
        list->last = &(list->first);
-       list->count = 0;
 }
 
 static void
@@ -1359,7 +1364,6 @@ entry_list_add(struct entry_list *list, struct match_file *file)
 {
        *list->last = file;
        list->last = &(file->next);
-       list->count++;
 }
 
 static int
@@ -1382,9 +1386,7 @@ add_entry(struct archive_match *a, int flag,
                return (ARCHIVE_FAILED);
        }
        archive_mstring_copy_wcs(&(f->pathname), pathname);
-       a->exclusion_tree.rbt_ops = &rb_ops_wcs;
 #else
-       (void)rb_ops_wcs;
        pathname = archive_entry_pathname(entry);
        if (pathname == NULL) {
                free(f);
@@ -1392,7 +1394,6 @@ add_entry(struct archive_match *a, int flag,
                return (ARCHIVE_FAILED);
        }
        archive_mstring_copy_mbs(&(f->pathname), pathname);
-       a->exclusion_tree.rbt_ops = &rb_ops_mbs;
 #endif
        f->flag = flag;
        f->mtime_sec = archive_entry_mtime(entry);
@@ -1517,16 +1518,13 @@ time_excluded(struct archive_match *a, struct archive_entry *entry)
        }
 
        /* If there is no exclusion list, include the file. */
-       if (a->exclusion_entry_list.count == 0)
+       if (a->exclusion_entry_list.first == NULL)
                return (0);
 
 #if defined(_WIN32) && !defined(__CYGWIN__)
        pathname = archive_entry_pathname_w(entry);
-       a->exclusion_tree.rbt_ops = &rb_ops_wcs;
 #else
-       (void)rb_ops_wcs;
        pathname = archive_entry_pathname(entry);
-       a->exclusion_tree.rbt_ops = &rb_ops_mbs;
 #endif
        if (pathname == NULL)
                return (0);
@@ -1682,7 +1680,7 @@ archive_match_owner_excluded(struct archive *_a,
 static int
 add_owner_id(struct archive_match *a, struct id_array *ids, int64_t id)
 {
-       unsigned i;
+       size_t i;
 
        if (ids->count + 1 >= ids->size) {
                void *p;
@@ -1719,10 +1717,10 @@ add_owner_id(struct archive_match *a, struct id_array *ids, int64_t id)
 static int
 match_owner_id(struct id_array *ids, int64_t id)
 {
-       unsigned b, m, t;
+       size_t b, m, t;
 
        t = 0;
-       b = (unsigned)ids->count;
+       b = ids->count;
        while (t < b) {
                m = (t + b)>>1;
                if (ids->ids[m] == id)
@@ -1817,7 +1815,7 @@ owner_excluded(struct archive_match *a, struct archive_entry *entry)
                        return (1);
        }
 
-       if (a->inclusion_unames.count) {
+       if (a->inclusion_unames.first != NULL) {
 #if defined(_WIN32) && !defined(__CYGWIN__)
                r = match_owner_name_wcs(a, &(a->inclusion_unames),
                        archive_entry_uname_w(entry));
@@ -1831,7 +1829,7 @@ owner_excluded(struct archive_match *a, struct archive_entry *entry)
                        return (r);
        }
 
-       if (a->inclusion_gnames.count) {
+       if (a->inclusion_gnames.first != NULL) {
 #if defined(_WIN32) && !defined(__CYGWIN__)
                r = match_owner_name_wcs(a, &(a->inclusion_gnames),
                        archive_entry_gname_w(entry));