From 14f3480af1f5b5884827977fedecf0d7bb61ebab Mon Sep 17 00:00:00 2001 From: Franck Bui Date: Thu, 24 May 2018 14:17:07 +0200 Subject: [PATCH] tmpfiles: stat file in item_do() rather than in its callers This a slight simplification since all callers of item_do() (glob_item_recursively() and item_do() itself) stat the file descriptor only for passing it to item_do(). --- src/tmpfiles/tmpfiles.c | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c index 52f4d90fba1..31d4c923a27 100644 --- a/src/tmpfiles/tmpfiles.c +++ b/src/tmpfiles/tmpfiles.c @@ -1295,18 +1295,23 @@ static int write_one_file(Item *i, const char *path) { typedef int (*action_t)(Item *, const char *); typedef int (*fdaction_t)(Item *, int fd, const struct stat *st); -static int item_do(Item *i, int fd, const struct stat *st, fdaction_t action) { +static int item_do(Item *i, int fd, fdaction_t action) { + struct stat st; int r = 0, q; assert(i); assert(fd >= 0); - assert(st); + + if (fstat(fd, &st) < 0) { + r = -errno; + goto finish; + } /* This returns the first error we run into, but nevertheless * tries to go on */ - r = action(i, fd, st); + r = action(i, fd, &st); - if (S_ISDIR(st->st_mode)) { + if (S_ISDIR(st.st_mode)) { char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)]; _cleanup_closedir_ DIR *d = NULL; struct dirent *de; @@ -1322,16 +1327,15 @@ static int item_do(Item *i, int fd, const struct stat *st, fdaction_t action) { } FOREACH_DIRENT_ALL(de, d, q = -errno; goto finish) { - struct stat de_st; int de_fd; if (dot_or_dot_dot(de->d_name)) continue; de_fd = openat(fd, de->d_name, O_NOFOLLOW|O_CLOEXEC|O_PATH); - if (de_fd >= 0 && fstat(de_fd, &de_st) >= 0) + if (de_fd >= 0) /* pass ownership of dirent fd over */ - q = item_do(i, de_fd, &de_st, action); + q = item_do(i, de_fd, action); else q = -errno; @@ -1377,7 +1381,6 @@ static int glob_item_recursively(Item *i, fdaction_t action) { STRV_FOREACH(fn, g.gl_pathv) { _cleanup_close_ int fd = -1; - struct stat st; /* Make sure we won't trigger/follow file object (such as * device nodes, automounts, ...) pointed out by 'fn' with @@ -1390,12 +1393,7 @@ static int glob_item_recursively(Item *i, fdaction_t action) { continue; } - if (fstat(fd, &st) < 0) { - r = r ?: -errno; - continue; - } - - k = item_do(i, fd, &st, action); + k = item_do(i, fd, action); if (k < 0 && r == 0) r = k; -- 2.39.2