]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
chattr-util: move O_PATH conversion from read_attr_at() to read_attr_fd()
authorLennart Poettering <lennart@poettering.net>
Mon, 9 Dec 2024 12:50:48 +0000 (13:50 +0100)
committerLennart Poettering <lennart@poettering.net>
Tue, 17 Dec 2024 10:19:55 +0000 (11:19 +0100)
src/basic/chattr-util.c

index 9677e9a35a99b1952f34aae99789810edadcd76a..4aabcc68183318d981b96c52439df2c50ea3167f 100644 (file)
@@ -145,6 +145,11 @@ int read_attr_fd(int fd, unsigned *ret) {
         if (!S_ISDIR(st.st_mode) && !S_ISREG(st.st_mode))
                 return -ENOTTY;
 
+        _cleanup_close_ int fd_close = -EBADF;
+        fd = fd_reopen_condition(fd, O_RDONLY|O_CLOEXEC|O_NOCTTY, O_PATH, &fd_close); /* drop O_PATH if it is set */
+        if (fd < 0)
+                return fd;
+
         return RET_NERRNO(ioctl(fd, FS_IOC_GETFLAGS, ret));
 }
 
@@ -155,11 +160,9 @@ int read_attr_at(int dir_fd, const char *path, unsigned *ret) {
         assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
         assert(ret);
 
-        if (isempty(path)) {
-                fd = fd_reopen_condition(dir_fd, O_RDONLY|O_CLOEXEC|O_NOCTTY, O_PATH, &fd_close); /* drop O_PATH if it is set */
-                if (fd < 0)
-                        return fd;
-        } else {
+        if (isempty(path) && dir_fd != AT_FDCWD)
+                fd = dir_fd;
+        else {
                 fd_close = xopenat(dir_fd, path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
                 if (fd_close < 0)
                         return fd_close;