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
+6 -10
View File
@@ -2570,23 +2570,21 @@ finish:
}
static int glob_item(Context *c, Item *i, action_t action) {
_cleanup_globfree_ glob_t g = {
.gl_opendir = (void *(*)(const char *)) opendir_nomod,
};
_cleanup_strv_free_ char **paths = NULL;
int r;
assert(c);
assert(i);
assert(action);
r = safe_glob(i->path, GLOB_NOSORT|GLOB_BRACE, &g);
r = safe_glob_full(i->path, GLOB_NOSORT|GLOB_BRACE, opendir_nomod, &paths);
if (r == -ENOENT)
return 0;
if (r < 0)
return log_error_errno(r, "Failed to glob '%s': %m", i->path);
r = 0;
STRV_FOREACH(fn, g.gl_pathv)
STRV_FOREACH(fn, paths)
/* We pass CREATION_EXISTING here, since if we are globbing for it, it always has to exist */
RET_GATHER(r, action(c, i, *fn, CREATION_EXISTING));
@@ -2598,23 +2596,21 @@ static int glob_item_recursively(
Item *i,
fdaction_t action) {
_cleanup_globfree_ glob_t g = {
.gl_opendir = (void *(*)(const char *)) opendir_nomod,
};
_cleanup_strv_free_ char **paths = NULL;
int r;
assert(c);
assert(i);
assert(action);
r = safe_glob(i->path, GLOB_NOSORT|GLOB_BRACE, &g);
r = safe_glob_full(i->path, GLOB_NOSORT|GLOB_BRACE, opendir_nomod, &paths);
if (r == -ENOENT)
return 0;
if (r < 0)
return log_error_errno(r, "Failed to glob '%s': %m", i->path);
r = 0;
STRV_FOREACH(fn, g.gl_pathv) {
STRV_FOREACH(fn, paths) {
_cleanup_close_ int fd = -EBADF;
/* Make sure we won't trigger/follow file object (such as device nodes, automounts, ...)