]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
fd-util: Add path_is_root_at()
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Thu, 1 Jun 2023 12:26:35 +0000 (14:26 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 6 Jun 2023 12:42:03 +0000 (14:42 +0200)
A generalization of dir_fd_is_root() that allows passing a path
component.

src/basic/fd-util.c
src/basic/fd-util.h

index 907bfeb60024b9d4252ee1506a9f0e9039146cb3..ecbe58a9f83fca94233ae2932f33e0799af2c428 100644 (file)
@@ -891,12 +891,21 @@ int fd_get_diskseq(int fd, uint64_t *ret) {
         return 0;
 }
 
-int dir_fd_is_root(int dir_fd) {
+int path_is_root_at(int dir_fd, const char *path) {
         STRUCT_NEW_STATX_DEFINE(st);
         STRUCT_NEW_STATX_DEFINE(pst);
+        _cleanup_close_ int fd = -EBADF;
         int r;
 
-        assert(dir_fd >= 0);
+        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
+
+        if (!isempty(path)) {
+                fd = openat(dir_fd, path, O_PATH|O_CLOEXEC);
+                if (fd < 0)
+                        return -errno;
+
+                dir_fd = fd;
+        }
 
         r = statx_fallback(dir_fd, ".", 0, STATX_TYPE|STATX_INO|STATX_MNT_ID, &st.sx);
         if (r == -ENOTDIR)
index 2f59e334c5c12051405b3bfa94ef3ac2fbe80aa7..c870a1b8990d6ca21d870c260b0e51913b12958d 100644 (file)
@@ -101,9 +101,12 @@ int fd_is_opath(int fd);
 int read_nr_open(void);
 int fd_get_diskseq(int fd, uint64_t *ret);
 
-int dir_fd_is_root(int dir_fd);
+int path_is_root_at(int dir_fd, const char *path);
+static inline int dir_fd_is_root(int dir_fd) {
+        return path_is_root_at(dir_fd, NULL);
+}
 static inline int dir_fd_is_root_or_cwd(int dir_fd) {
-        return dir_fd == AT_FDCWD ? true : dir_fd_is_root(dir_fd);
+        return dir_fd == AT_FDCWD ? true : path_is_root_at(dir_fd, NULL);
 }
 
 /* The maximum length a buffer for a /proc/self/fd/<fd> path needs */