]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
utils: add development helper to quickly dump a directories contents
authorChristian Brauner <christian.brauner@ubuntu.com>
Mon, 15 Feb 2021 11:29:47 +0000 (12:29 +0100)
committerChristian Brauner <christian.brauner@ubuntu.com>
Mon, 15 Feb 2021 11:29:47 +0000 (12:29 +0100)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/utils.c
src/lxc/utils.h

index ce00104ed9ceb26867aabd9533cf8228e8b87fcc..d64fbf6b1c08211bf83c50db844a3128f452ac34 100644 (file)
@@ -1855,3 +1855,53 @@ bool multiply_overflow(int64_t base, uint64_t mult, int64_t *res)
        *res = base * mult;
        return true;
 }
+
+int print_r(int fd, const char *path)
+{
+       __do_close int dfd = -EBADF;
+       __do_closedir DIR *dir = NULL;
+       int ret = 0;
+       struct dirent *direntp;
+       struct stat st;
+
+       if (is_empty_string(path))
+               dfd = dup(fd);
+       else
+               dfd = openat(fd, path, O_CLOEXEC | O_DIRECTORY);
+       if (dfd < 0)
+               return -1;
+
+       dir = fdopendir(dfd);
+       if (!dir)
+               return -1;
+       /* Transfer ownership to fdopendir(). */
+       move_fd(dfd);
+
+       while ((direntp = readdir(dir))) {
+               if (!strcmp(direntp->d_name, ".") ||
+                   !strcmp(direntp->d_name, ".."))
+                       continue;
+
+               ret = fstatat(dfd, direntp->d_name, &st, AT_SYMLINK_NOFOLLOW);
+               if (ret < 0 && errno != ENOENT)
+                       break;
+
+               ret = 0;
+               if (S_ISDIR(st.st_mode))
+                       ret = print_r(dfd, direntp->d_name);
+               else
+                       INFO("mode(%o):uid(%d):gid(%d) -> %s/%s\n",
+                            (st.st_mode & ~S_IFMT), st.st_uid, st.st_gid, path,
+                            direntp->d_name);
+               if (ret < 0 && errno != ENOENT)
+                       break;
+       }
+
+       ret = fstatat(fd, path, &st, AT_SYMLINK_NOFOLLOW);
+       if (ret)
+               return -1;
+       else
+               INFO("mode(%o):uid(%d):gid(%d) -> %s",
+                    (st.st_mode & ~S_IFMT), st.st_uid, st.st_gid, path);
+       return ret;
+}
index b8f23994c88e2e08296d65499052d402486aa4a2..dd51afdc4c8d2918f77b71828a78b424d2dcefac 100644 (file)
@@ -243,5 +243,6 @@ __hidden extern int safe_mount_beneath(const char *beneath, const char *src, con
                                       const char *fstype, unsigned int flags, const void *data);
 __hidden extern int safe_mount_beneath_at(int beneat_fd, const char *src, const char *dst,
                                          const char *fstype, unsigned int flags, const void *data);
+__hidden __lxc_unused int print_r(int fd, const char *path);
 
 #endif /* __LXC_UTILS_H */