chattr-util: Optimize read_attr_at()
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Mon, 26 Aug 2024 12:14:07 +0000 (14:14 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Wed, 4 Sep 2024 16:51:53 +0000 (18:51 +0200)
Let's make sure we only reopen O_PATH file descriptors.

src/basic/chattr-util.c

index 49831a26cee32a9e8250686602a59bb9c32a58d6..13d46f26aaafdf94e3cadff89d5779d2116176aa 100644 (file)
@@ -149,14 +149,23 @@ int read_attr_fd(int fd, unsigned *ret) {
 }
 
 int read_attr_at(int dir_fd, const char *path, unsigned *ret) {
-        _cleanup_close_ int fd = -EBADF;
+        _cleanup_close_ int fd_close = -EBADF;
+        int fd;
 
         assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
         assert(ret);
 
-        fd = xopenat(dir_fd, path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
-        if (fd < 0)
-                return fd;
+        if (isempty(path)) {
+                fd = fd_reopen_condition(dir_fd, O_RDONLY|O_CLOEXEC, O_PATH, &fd_close); /* drop O_PATH if it is set */
+                if (fd < 0)
+                        return fd;
+        } else {
+                fd_close = xopenat(dir_fd, path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
+                if (fd_close < 0)
+                        return fd_close;
+
+                fd = fd_close;
+        }
 
         return read_attr_fd(fd, ret);
 }