From: Mike Yuan Date: Sun, 18 Jan 2026 23:53:48 +0000 (+0100) Subject: fs-util: optimize xopenat(XAT_FDROOT, ...) a bit X-Git-Tag: v260-rc1~363^2~6 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=02bf2b7cdfb26af99e13dded572b1ff44f04049a;p=thirdparty%2Fsystemd.git fs-util: optimize xopenat(XAT_FDROOT, ...) a bit Follow-up for ef582ab201c2942813960ad3c451df3642d85558 * Open / with O_PATH * Shortcut to fd_reopen() directly if path is empty --- diff --git a/src/basic/fs-util.c b/src/basic/fs-util.c index 0159934ddb3..f790ca4e136 100644 --- a/src/basic/fs-util.c +++ b/src/basic/fs-util.c @@ -1158,19 +1158,6 @@ int xopenat_full(int dir_fd, const char *path, int open_flags, XOpenFlags xopen_ * • The dir fd can be passed as XAT_FDROOT, in which case any relative paths will be taken relative to the root fs. */ - _cleanup_close_ int _dir_fd = -EBADF; - if (dir_fd == XAT_FDROOT) { - if (path_is_absolute(path)) - dir_fd = AT_FDCWD; - else { - _dir_fd = open("/", O_CLOEXEC|O_DIRECTORY|O_RDONLY); - if (_dir_fd < 0) - return -errno; - - dir_fd = _dir_fd; - } - } - if (mode == MODE_INVALID) mode = (open_flags & O_DIRECTORY) ? 0755 : 0644; @@ -1186,6 +1173,19 @@ int xopenat_full(int dir_fd, const char *path, int open_flags, XOpenFlags xopen_ return fd_reopen(dir_fd, open_flags & ~O_NOFOLLOW); } + _cleanup_close_ int _dir_fd = -EBADF; + if (dir_fd == XAT_FDROOT) { + if (path_is_absolute(path)) + dir_fd = AT_FDCWD; + else { + _dir_fd = open("/", O_CLOEXEC|O_DIRECTORY|O_PATH); + if (_dir_fd < 0) + return -errno; + + dir_fd = _dir_fd; + } + } + bool call_label_ops_post = false; if (FLAGS_SET(open_flags, O_CREAT) && FLAGS_SET(xopen_flags, XO_LABEL)) {