1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include "dirent-util.h"
11 #include "path-util.h"
14 static void closedir_wrapper(void* v
) {
18 int safe_glob(const char *path
, int flags
, glob_t
*pglob
) {
21 /* We want to set GLOB_ALTDIRFUNC ourselves, don't allow it to be set. */
22 assert(!(flags
& GLOB_ALTDIRFUNC
));
24 if (!pglob
->gl_closedir
)
25 pglob
->gl_closedir
= closedir_wrapper
;
26 if (!pglob
->gl_readdir
)
27 pglob
->gl_readdir
= (struct dirent
*(*)(void *)) readdir_no_dot
;
28 if (!pglob
->gl_opendir
)
29 pglob
->gl_opendir
= (void *(*)(const char *)) opendir
;
31 pglob
->gl_lstat
= lstat
;
33 pglob
->gl_stat
= stat
;
36 k
= glob(path
, flags
| GLOB_ALTDIRFUNC
, NULL
, pglob
);
38 if (k
== GLOB_NOMATCH
)
40 if (k
== GLOB_NOSPACE
)
43 return errno
> 0 ? -errno
: -EIO
;
44 if (strv_isempty(pglob
->gl_pathv
))
50 int glob_exists(const char *path
) {
51 _cleanup_globfree_ glob_t g
= {};
56 k
= safe_glob(path
, GLOB_NOSORT
|GLOB_BRACE
, &g
);
64 int glob_extend(char ***strv
, const char *path
) {
65 _cleanup_globfree_ glob_t g
= {};
68 k
= safe_glob(path
, GLOB_NOSORT
|GLOB_BRACE
, &g
);
72 return strv_extend_strv(strv
, g
.gl_pathv
, false);