]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
fileio: modernize xopendirat() a bit
authorLennart Poettering <lennart@poettering.net>
Mon, 25 Aug 2025 09:05:48 +0000 (11:05 +0200)
committerLennart Poettering <lennart@poettering.net>
Fri, 19 Sep 2025 06:39:32 +0000 (08:39 +0200)
src/basic/chase.c
src/basic/fileio.c
src/basic/fileio.h
src/udev/udev-node.c

index ddef19448741dc2e2eac9a9a849608e08116cc6b..3443721daac51b28ef6d020cbcd50537c4990574 100644 (file)
@@ -842,7 +842,7 @@ int chase_and_opendir(const char *path, const char *root, ChaseFlags chase_flags
                 return r;
         assert(path_fd >= 0);
 
-        d = xopendirat(path_fd, ".", O_NOFOLLOW);
+        d = xopendirat(path_fd, /* path= */ NULL, /* flags= */ 0);
         if (!d)
                 return -errno;
 
@@ -1045,7 +1045,7 @@ int chase_and_opendirat(int dir_fd, const char *path, ChaseFlags chase_flags, ch
                 return r;
         assert(path_fd >= 0);
 
-        d = xopendirat(path_fd, ".", O_NOFOLLOW);
+        d = xopendirat(path_fd, /* path= */ NULL, /* flags= */ 0);
         if (!d)
                 return -errno;
 
index a2fb5c7649c92de9567c728db3cf7c1d4542c84f..ca763a315b3421860367bc2b5abec689661d99ee 100644 (file)
@@ -928,17 +928,22 @@ int get_proc_field(const char *path, const char *key, char **ret) {
         }
 }
 
-DIR* xopendirat(int dir_fd, const char *name, int flags) {
+DIR* xopendirat(int dir_fd, const char *path, int flags) {
         _cleanup_close_ int fd = -EBADF;
 
         assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
-        assert(name);
         assert(!(flags & (O_CREAT|O_TMPFILE)));
 
-        if (dir_fd == AT_FDCWD && flags == 0)
-                return opendir(name);
+        if ((dir_fd == AT_FDCWD || path_is_absolute(path)) &&
+            (flags &~ O_DIRECTORY) == 0)
+                return opendir(path);
 
-        fd = openat(dir_fd, name, O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|flags);
+        if (isempty(path)) {
+                path = ".";
+                flags |= O_NOFOLLOW;
+        }
+
+        fd = openat(dir_fd, path, O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|flags);
         if (fd < 0)
                 return NULL;
 
index 0297cd07e985f0f894fdde315413d21195f592aa..4d798e7e646f62e8d1a6a2bfd55a0c7bf753acda 100644 (file)
@@ -92,7 +92,7 @@ int script_get_shebang_interpreter(const char *path, char **ret);
 
 int get_proc_field(const char *path, const char *key, char **ret);
 
-DIR* xopendirat(int dir_fd, const char *name, int flags);
+DIR* xopendirat(int dir_fd, const char *path, int flags);
 
 typedef enum XfopenFlags {
         XFOPEN_UNLOCKED = 1 << 0, /* call __fsetlocking(FSETLOCKING_BYCALLER) after opened */
index 412775b38b95fee6b8be52be35ce6ce332dda8a1..5abaf1eff39b93b6d2e8ce4b6fc3923649901b42 100644 (file)
@@ -190,7 +190,7 @@ static int stack_directory_find_prioritized_devnode(sd_device *dev, int dirfd, b
                         return -ENOMEM;
         }
 
-        dir = xopendirat(dirfd, ".", O_NOFOLLOW);
+        dir = xopendirat(dirfd, /* path= */ NULL, /* flags= */ 0);
         if (!dir)
                 return -errno;