]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
recurse-dir: switch to FOREACH_ARRAY
authorMike Yuan <me@yhndnzj.com>
Wed, 9 Jul 2025 07:55:15 +0000 (09:55 +0200)
committerMike Yuan <me@yhndnzj.com>
Wed, 9 Jul 2025 08:13:59 +0000 (10:13 +0200)
src/basic/recurse-dir.c

index 05a2cfa2e0639cb5470e5a760c195ca0c6a8e539..2f63aa48f93c4a846d6ea28a7dde35dbd028714f 100644 (file)
@@ -191,7 +191,8 @@ int recurse_dir(
         if (r < 0)
                 return r;
 
-        for (size_t i = 0; i < de->n_entries; i++) {
+        FOREACH_ARRAY(entry, de->entries, de->n_entries) {
+                struct dirent *i = *entry;
                 _cleanup_close_ int inode_fd = -EBADF, subdir_fd = -EBADF;
                 _cleanup_free_ char *joined = NULL;
                 struct statx sx;
@@ -206,16 +207,16 @@ int recurse_dir(
                  */
 
                 if (path) {
-                        joined = path_join(path, de->entries[i]->d_name);
+                        joined = path_join(path, i->d_name);
                         if (!joined)
                                 return -ENOMEM;
 
                         p = joined;
                 } else
-                        p = de->entries[i]->d_name;
+                        p = i->d_name;
 
-                if (IN_SET(de->entries[i]->d_type, DT_UNKNOWN, DT_DIR)) {
-                        subdir_fd = openat(dir_fd, de->entries[i]->d_name, O_DIRECTORY|O_NOFOLLOW|O_CLOEXEC);
+                if (IN_SET(i->d_type, DT_UNKNOWN, DT_DIR)) {
+                        subdir_fd = openat(dir_fd, i->d_name, O_DIRECTORY|O_NOFOLLOW|O_CLOEXEC);
                         if (subdir_fd < 0) {
                                 if (errno == ENOENT) /* Vanished by now, go for next file immediately */
                                         continue;
@@ -230,7 +231,7 @@ int recurse_dir(
                                                  p,
                                                  dir_fd,
                                                  /* inode_fd = */ -EBADF,
-                                                 de->entries[i],
+                                                 i,
                                                  /* sx = */ NULL,
                                                  userdata);
                                         if (r == RECURSE_DIR_LEAVE_DIRECTORY)
@@ -245,7 +246,7 @@ int recurse_dir(
 
                         } else {
                                 /* If we managed to get a DIR* off the inode, it's definitely a directory. */
-                                de->entries[i]->d_type = DT_DIR;
+                                i->d_type = DT_DIR;
 
                                 if (statx_mask != 0 || (flags & RECURSE_DIR_SAME_MOUNT)) {
                                         if (statx(subdir_fd, "", AT_EMPTY_PATH, statx_mask, &sx) < 0)
@@ -261,7 +262,7 @@ int recurse_dir(
 
                         if (flags & RECURSE_DIR_INODE_FD) {
 
-                                inode_fd = openat(dir_fd, de->entries[i]->d_name, O_PATH|O_NOFOLLOW|O_CLOEXEC);
+                                inode_fd = openat(dir_fd, i->d_name, O_PATH|O_NOFOLLOW|O_CLOEXEC);
                                 if (inode_fd < 0) {
                                         if (errno == ENOENT) /* Vanished by now, go for next file immediately */
                                                 continue;
@@ -274,7 +275,7 @@ int recurse_dir(
                                                  p,
                                                  dir_fd,
                                                  /* inode_fd = */ -EBADF,
-                                                 de->entries[i],
+                                                 i,
                                                  /* sx = */ NULL,
                                                  userdata);
                                         if (r == RECURSE_DIR_LEAVE_DIRECTORY)
@@ -308,9 +309,9 @@ int recurse_dir(
                                         inode_fd = safe_close(inode_fd);
                                 }
 
-                        } else if (statx_mask != 0 || (de->entries[i]->d_type == DT_UNKNOWN && (flags & RECURSE_DIR_ENSURE_TYPE))) {
+                        } else if (statx_mask != 0 || (i->d_type == DT_UNKNOWN && (flags & RECURSE_DIR_ENSURE_TYPE))) {
 
-                                if (statx(dir_fd, de->entries[i]->d_name, AT_SYMLINK_NOFOLLOW, statx_mask | STATX_TYPE, &sx) < 0) {
+                                if (statx(dir_fd, i->d_name, AT_SYMLINK_NOFOLLOW, statx_mask | STATX_TYPE, &sx) < 0) {
                                         if (errno == ENOENT) /* Vanished by now? Go for next file immediately */
                                                 continue;
 
@@ -322,7 +323,7 @@ int recurse_dir(
                                                  p,
                                                  dir_fd,
                                                  /* inode_fd = */ -EBADF,
-                                                 de->entries[i],
+                                                 i,
                                                  /* sx = */ NULL,
                                                  userdata);
                                         if (r == RECURSE_DIR_LEAVE_DIRECTORY)
@@ -352,7 +353,7 @@ int recurse_dir(
                                                  p,
                                                  dir_fd,
                                                  /* inode_fd = */ -EBADF,
-                                                 de->entries[i],
+                                                 i,
                                                  /* sx = */ NULL,
                                                  userdata);
                                         if (r == RECURSE_DIR_LEAVE_DIRECTORY)
@@ -369,11 +370,11 @@ int recurse_dir(
                         /* Copy over the data we acquired through statx() if we acquired any */
                         if (sx.stx_mask & STATX_TYPE) {
                                 assert((subdir_fd < 0) == !S_ISDIR(sx.stx_mode));
-                                de->entries[i]->d_type = IFTODT(sx.stx_mode);
+                                i->d_type = IFTODT(sx.stx_mode);
                         }
 
                         if (sx.stx_mask & STATX_INO)
-                                de->entries[i]->d_ino = sx.stx_ino;
+                                i->d_ino = sx.stx_ino;
                 }
 
                 if (subdir_fd >= 0) {
@@ -383,7 +384,7 @@ int recurse_dir(
                                 if (sx_valid && FLAGS_SET(sx.stx_attributes_mask, STATX_ATTR_MOUNT_ROOT))
                                         is_mount = FLAGS_SET(sx.stx_attributes, STATX_ATTR_MOUNT_ROOT);
                                 else {
-                                        r = is_mount_point_at(dir_fd, de->entries[i]->d_name, 0);
+                                        r = is_mount_point_at(dir_fd, i->d_name, /* flags = */ 0);
                                         if (r < 0)
                                                 log_debug_errno(r, "Failed to determine whether %s is a submount, assuming not: %m", p);
 
@@ -395,7 +396,7 @@ int recurse_dir(
                                                  p,
                                                  dir_fd,
                                                  subdir_fd,
-                                                 de->entries[i],
+                                                 i,
                                                  statx_mask != 0 ? &sx : NULL, /* only pass sx if user asked for it */
                                                  userdata);
                                         if (r == RECURSE_DIR_LEAVE_DIRECTORY)
@@ -414,7 +415,7 @@ int recurse_dir(
                                          p,
                                          dir_fd,
                                          subdir_fd,
-                                         de->entries[i],
+                                         i,
                                          statx_mask != 0 ? &sx : NULL, /* only pass sx if user asked for it */
                                          userdata);
                                 if (r == RECURSE_DIR_LEAVE_DIRECTORY)
@@ -429,7 +430,7 @@ int recurse_dir(
                                  p,
                                  dir_fd,
                                  subdir_fd,
-                                 de->entries[i],
+                                 i,
                                  statx_mask != 0 ? &sx : NULL, /* only pass sx if user asked for it */
                                  userdata);
                         if (r == RECURSE_DIR_LEAVE_DIRECTORY)
@@ -443,7 +444,7 @@ int recurse_dir(
                                         p,
                                         statx_mask,
                                         n_depth_max - 1,
-                                        flags &RECURSE_DIR_TOPLEVEL, /* we already called the callback for this entry */
+                                        flags & ~RECURSE_DIR_TOPLEVEL, /* we already called the callback for this entry */
                                         func,
                                         userdata);
                         if (r != 0)
@@ -453,7 +454,7 @@ int recurse_dir(
                                  p,
                                  dir_fd,
                                  subdir_fd,
-                                 de->entries[i],
+                                 i,
                                  statx_mask != 0 ? &sx : NULL, /* only pass sx if user asked for it */
                                  userdata);
                 } else
@@ -462,7 +463,7 @@ int recurse_dir(
                                  p,
                                  dir_fd,
                                  inode_fd,
-                                 de->entries[i],
+                                 i,
                                  statx_mask != 0 ? &sx : NULL, /* only pass sx if user asked for it */
                                  userdata);