From: Luca Boccassi Date: Wed, 3 Aug 2022 13:20:41 +0000 (+0100) Subject: glob: add glob_first(), returns first match X-Git-Tag: v252-rc1~357^2~3 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=f52faaf923acfe6fe3f0955d1ad66840b13babfc;p=thirdparty%2Fsystemd.git glob: add glob_first(), returns first match Note that which match is returned depends on the system and is not guaranteed to be stable --- diff --git a/src/basic/glob-util.c b/src/basic/glob-util.c index e026b29478e..fd60a6eda2d 100644 --- a/src/basic/glob-util.c +++ b/src/basic/glob-util.c @@ -47,17 +47,28 @@ int safe_glob(const char *path, int flags, glob_t *pglob) { return 0; } -int glob_exists(const char *path) { +int glob_first(const char *path, char **ret_first) { _cleanup_globfree_ glob_t g = {}; int k; assert(path); k = safe_glob(path, GLOB_NOSORT|GLOB_BRACE, &g); - if (k == -ENOENT) + if (k == -ENOENT) { + if (ret_first) + *ret_first = NULL; return false; + } if (k < 0) return k; + + if (ret_first) { + char *first = strdup(g.gl_pathv[0]); + if (!first) + return log_oom_debug(); + *ret_first = first; + } + return true; } diff --git a/src/basic/glob-util.h b/src/basic/glob-util.h index fc86e990dd7..7ca26cc27f7 100644 --- a/src/basic/glob-util.h +++ b/src/basic/glob-util.h @@ -10,7 +10,9 @@ /* Note: this function modifies pglob to set various functions. */ int safe_glob(const char *path, int flags, glob_t *pglob); -int glob_exists(const char *path); +/* Note: which match is returned depends on the implementation/system and not guaranteed to be stable */ +int glob_first(const char *path, char **ret_first); +#define glob_exists(path) glob_first(path, NULL) int glob_extend(char ***strv, const char *path, int flags); int glob_non_glob_prefix(const char *path, char **ret); diff --git a/src/test/test-glob-util.c b/src/test/test-glob-util.c index ec8b74f48f3..566b68b5892 100644 --- a/src/test/test-glob-util.c +++ b/src/test/test-glob-util.c @@ -13,6 +13,27 @@ #include "tests.h" #include "tmpfile-util.h" +TEST(glob_first) { + char *first, name[] = "/tmp/test-glob_first.XXXXXX"; + int fd = -1; + int r; + + fd = mkostemp_safe(name); + assert_se(fd >= 0); + close(fd); + + r = glob_first("/tmp/test-glob_first*", &first); + assert_se(r == 1); + assert_se(streq(name, first)); + first = mfree(first); + + r = unlink(name); + assert_se(r == 0); + r = glob_first("/tmp/test-glob_first*", &first); + assert_se(r == 0); + assert_se(first == NULL); +} + TEST(glob_exists) { char name[] = "/tmp/test-glob_exists.XXXXXX"; int fd = -1;