1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include "dirent-util.h"
11 #include "glob-util.h"
13 #include "path-util.h"
16 static void closedir_wrapper(void* v
) {
20 int safe_glob(const char *path
, int flags
, glob_t
*pglob
) {
23 /* We want to set GLOB_ALTDIRFUNC ourselves, don't allow it to be set. */
24 assert(!(flags
& GLOB_ALTDIRFUNC
));
26 if (!pglob
->gl_closedir
)
27 pglob
->gl_closedir
= closedir_wrapper
;
28 if (!pglob
->gl_readdir
)
29 pglob
->gl_readdir
= (struct dirent
*(*)(void *)) readdir_no_dot
;
30 if (!pglob
->gl_opendir
)
31 pglob
->gl_opendir
= (void *(*)(const char *)) opendir
;
33 pglob
->gl_lstat
= lstat
;
35 pglob
->gl_stat
= stat
;
38 k
= glob(path
, flags
| GLOB_ALTDIRFUNC
, NULL
, pglob
);
40 if (k
== GLOB_NOMATCH
)
42 if (k
== GLOB_NOSPACE
)
45 return errno
> 0 ? -errno
: -EIO
;
46 if (strv_isempty(pglob
->gl_pathv
))
52 int glob_exists(const char *path
) {
53 _cleanup_globfree_ glob_t g
= {};
58 k
= safe_glob(path
, GLOB_NOSORT
|GLOB_BRACE
, &g
);
66 int glob_extend(char ***strv
, const char *path
) {
67 _cleanup_globfree_ glob_t g
= {};
70 k
= safe_glob(path
, GLOB_NOSORT
|GLOB_BRACE
, &g
);
74 return strv_extend_strv(strv
, g
.gl_pathv
, false);