Skip to content

Commit

Permalink
tmpfiles: stat file in item_do() rather than in its callers
Browse files Browse the repository at this point in the history
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().
  • Loading branch information
fbuihuu committed Jul 30, 2018
1 parent 1c57fa9 commit 14f3480
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions src/tmpfiles/tmpfiles.c
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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
Expand All @@ -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;

Expand Down

0 comments on commit 14f3480

Please sign in to comment.