glob-util: rework safe_glob()

Currently, callers of safe_glob() set an empty glob_t or glob_t with
opendir func, and all other components are always zero.
So, let's introduce safe_glob_full() which optionally takes opendir
function, rather than glob_t, and returns result strv, rather than
storing results in glob_t.
Also, introduce safe_glob() which is a trivial wrapper of
safe_glob_full() without opendir func.

No functional change, just refactoring.
This commit is contained in:
Yu Watanabe
2025-06-23 23:11:44 +09:00
parent 1b0c39458b
commit 2a5f950e56
6 changed files with 78 additions and 81 deletions
+3 -3
View File
@@ -539,20 +539,20 @@ static int load_credential_glob(
assert(search_path);
STRV_FOREACH(d, search_path) {
_cleanup_globfree_ glob_t pglob = {};
_cleanup_strv_free_ char **paths = NULL;
_cleanup_free_ char *j = NULL;
j = path_join(*d, ic->glob);
if (!j)
return -ENOMEM;
r = safe_glob(j, 0, &pglob);
r = safe_glob(j, /* flags = */ 0, &paths);
if (r == -ENOENT)
continue;
if (r < 0)
return r;
FOREACH_ARRAY(p, pglob.gl_pathv, pglob.gl_pathc) {
STRV_FOREACH(p, paths) {
_cleanup_free_ char *fn = NULL;
_cleanup_(erase_and_freep) char *data = NULL;
size_t size;
+4 -4
View File
@@ -911,7 +911,7 @@ static int exec_context_load_environment(const Unit *unit, const ExecContext *c,
assert(ret);
STRV_FOREACH(i, c->environment_files) {
_cleanup_globfree_ glob_t pglob = {};
_cleanup_strv_free_ char **paths = NULL;
bool ignore = false;
char *fn = *i;
@@ -927,7 +927,7 @@ static int exec_context_load_environment(const Unit *unit, const ExecContext *c,
}
/* Filename supports globbing, take all matching files */
r = safe_glob(fn, 0, &pglob);
r = safe_glob(fn, /* flags = */ 0, &paths);
if (r < 0) {
if (ignore)
continue;
@@ -935,9 +935,9 @@ static int exec_context_load_environment(const Unit *unit, const ExecContext *c,
}
/* When we don't match anything, -ENOENT should be returned */
assert(pglob.gl_pathc > 0);
assert(!strv_isempty(paths));
FOREACH_ARRAY(path, pglob.gl_pathv, pglob.gl_pathc) {
STRV_FOREACH(path, paths) {
_cleanup_strv_free_ char **p = NULL;
r = load_env_file(NULL, *path, &p);