mirror of
https://github.com/systemd/systemd.git
synced 2024-10-30 06:25:37 +03:00
set: introduce set_fnmatch()
This commit is contained in:
parent
cf393c5f44
commit
d25d4f189c
@ -1,6 +1,7 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <errno.h>
|
||||
#include <fnmatch.h>
|
||||
#include <pthread.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
@ -2070,3 +2071,27 @@ bool set_equal(Set *a, Set *b) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool set_fnmatch_one(Set *patterns, const char *needle) {
|
||||
const char *p;
|
||||
|
||||
assert(needle);
|
||||
|
||||
SET_FOREACH(p, patterns)
|
||||
if (fnmatch(p, needle, 0) == 0)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool set_fnmatch(Set *include_patterns, Set *exclude_patterns, const char *needle) {
|
||||
assert(needle);
|
||||
|
||||
if (set_fnmatch_one(exclude_patterns, needle))
|
||||
return false;
|
||||
|
||||
if (set_isempty(include_patterns))
|
||||
return true;
|
||||
|
||||
return set_fnmatch_one(include_patterns, needle);
|
||||
}
|
||||
|
@ -153,3 +153,5 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(Set*, set_free_free);
|
||||
int set_strjoin(Set *s, const char *separator, bool wrap_with_separator, char **ret);
|
||||
|
||||
bool set_equal(Set *a, Set *b);
|
||||
|
||||
bool set_fnmatch(Set *include_patterns, Set *exclude_patterns, const char *needle);
|
||||
|
@ -330,4 +330,50 @@ TEST(set_equal) {
|
||||
assert_se(set_equal(b, a));
|
||||
}
|
||||
|
||||
TEST(set_fnmatch) {
|
||||
_cleanup_set_free_ Set *match = NULL, *nomatch = NULL;
|
||||
|
||||
assert_se(set_put_strdup(&match, "aaa") >= 0);
|
||||
assert_se(set_put_strdup(&match, "bbb*") >= 0);
|
||||
assert_se(set_put_strdup(&match, "*ccc") >= 0);
|
||||
|
||||
assert_se(set_put_strdup(&nomatch, "a*") >= 0);
|
||||
assert_se(set_put_strdup(&nomatch, "bbb") >= 0);
|
||||
assert_se(set_put_strdup(&nomatch, "ccc*") >= 0);
|
||||
|
||||
assert_se(set_fnmatch(NULL, NULL, ""));
|
||||
assert_se(set_fnmatch(NULL, NULL, "hoge"));
|
||||
|
||||
assert_se(set_fnmatch(match, NULL, "aaa"));
|
||||
assert_se(set_fnmatch(match, NULL, "bbb"));
|
||||
assert_se(set_fnmatch(match, NULL, "bbbXXX"));
|
||||
assert_se(set_fnmatch(match, NULL, "ccc"));
|
||||
assert_se(set_fnmatch(match, NULL, "XXXccc"));
|
||||
assert_se(!set_fnmatch(match, NULL, ""));
|
||||
assert_se(!set_fnmatch(match, NULL, "aaaa"));
|
||||
assert_se(!set_fnmatch(match, NULL, "XXbbb"));
|
||||
assert_se(!set_fnmatch(match, NULL, "cccXX"));
|
||||
|
||||
assert_se(set_fnmatch(NULL, nomatch, ""));
|
||||
assert_se(set_fnmatch(NULL, nomatch, "Xa"));
|
||||
assert_se(set_fnmatch(NULL, nomatch, "bbbb"));
|
||||
assert_se(set_fnmatch(NULL, nomatch, "XXXccc"));
|
||||
assert_se(!set_fnmatch(NULL, nomatch, "a"));
|
||||
assert_se(!set_fnmatch(NULL, nomatch, "aXXXX"));
|
||||
assert_se(!set_fnmatch(NULL, nomatch, "bbb"));
|
||||
assert_se(!set_fnmatch(NULL, nomatch, "ccc"));
|
||||
assert_se(!set_fnmatch(NULL, nomatch, "cccXXX"));
|
||||
|
||||
assert_se(set_fnmatch(match, nomatch, "bbbbb"));
|
||||
assert_se(set_fnmatch(match, nomatch, "XXccc"));
|
||||
assert_se(!set_fnmatch(match, nomatch, ""));
|
||||
assert_se(!set_fnmatch(match, nomatch, "a"));
|
||||
assert_se(!set_fnmatch(match, nomatch, "aaa"));
|
||||
assert_se(!set_fnmatch(match, nomatch, "b"));
|
||||
assert_se(!set_fnmatch(match, nomatch, "bbb"));
|
||||
assert_se(!set_fnmatch(match, nomatch, "ccc"));
|
||||
assert_se(!set_fnmatch(match, nomatch, "ccccc"));
|
||||
assert_se(!set_fnmatch(match, nomatch, "cccXX"));
|
||||
}
|
||||
|
||||
DEFINE_TEST_MAIN(LOG_INFO);
|
||||
|
Loading…
Reference in New Issue
Block a user