]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
fd-util: do not call statx() twice when it does not provide mount ID 28487/head
authorYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 21 Jul 2023 16:31:16 +0000 (01:31 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 21 Jul 2023 17:22:06 +0000 (02:22 +0900)
Previously, in path_is_root_at(), if statx() does not provide mount ID,
path_get_mnt_id_at() was called, but it also calls statx(). Let's avoid
the second trial.

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

index 1c67e785aa3fb535fc64e07714a5acf5e4955e7c..1730c9ab2d266f76eb9a1be728a4214413a108a0 100644 (file)
@@ -935,7 +935,7 @@ int path_is_root_at(int dir_fd, const char *path) {
         if (!FLAGS_SET(st.nsx.stx_mask, STATX_MNT_ID)) {
                 int mntid;
 
-                r = path_get_mnt_id_at(dir_fd, "", &mntid);
+                r = path_get_mnt_id_at_fallback(dir_fd, "", &mntid);
                 if (r < 0) {
                         if (ERRNO_IS_NOT_SUPPORTED(r))
                                 return true; /* skip the mount ID check */
@@ -950,7 +950,7 @@ int path_is_root_at(int dir_fd, const char *path) {
         if (!FLAGS_SET(pst.nsx.stx_mask, STATX_MNT_ID)) {
                 int mntid;
 
-                r = path_get_mnt_id_at(dir_fd, "..", &mntid);
+                r = path_get_mnt_id_at_fallback(dir_fd, "..", &mntid);
                 if (r < 0) {
                         if (ERRNO_IS_NOT_SUPPORTED(r))
                                 return true; /* skip the mount ID check */
index d7b182f6dd0084170815fad510e9383b8e466a77..5af0896ad69053a08eb5e7895757b46b60250d67 100644 (file)
@@ -359,9 +359,21 @@ int path_is_mount_point(const char *t, const char *root, int flags) {
         return fd_is_mount_point(fd, last_path_component(t), flags);
 }
 
+int path_get_mnt_id_at_fallback(int dir_fd, const char *path, int *ret) {
+        int r;
+
+        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
+        assert(ret);
+
+        r = name_to_handle_at_loop(dir_fd, path, NULL, ret, isempty(path) ? AT_EMPTY_PATH : 0);
+        if (r == 0 || is_name_to_handle_at_fatal_error(r))
+                return r;
+
+        return fd_fdinfo_mnt_id(dir_fd, path, isempty(path) ? AT_EMPTY_PATH : 0, ret);
+}
+
 int path_get_mnt_id_at(int dir_fd, const char *path, int *ret) {
         STRUCT_NEW_STATX_DEFINE(buf);
-        int r;
 
         assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
         assert(ret);
@@ -386,11 +398,7 @@ int path_get_mnt_id_at(int dir_fd, const char *path, int *ret) {
                 return 0;
         }
 
-        r = name_to_handle_at_loop(dir_fd, path, NULL, ret, isempty(path) ? AT_EMPTY_PATH : 0);
-        if (r == 0 || is_name_to_handle_at_fatal_error(r))
-                return r;
-
-        return fd_fdinfo_mnt_id(dir_fd, path, isempty(path) ? AT_EMPTY_PATH : 0, ret);
+        return path_get_mnt_id_at_fallback(dir_fd, path, ret);
 }
 
 bool fstype_is_network(const char *fstype) {
index 8c4cee2f544b085ca7ebf461ad545c3bd66ba0a4..846015c43c71a52c9d5d05cfda4089644068be2f 100644 (file)
@@ -37,6 +37,7 @@
 
 int name_to_handle_at_loop(int fd, const char *path, struct file_handle **ret_handle, int *ret_mnt_id, int flags);
 
+int path_get_mnt_id_at_fallback(int dir_fd, const char *path, int *ret);
 int path_get_mnt_id_at(int dir_fd, const char *path, int *ret);
 static inline int path_get_mnt_id(const char *path, int *ret) {
         return path_get_mnt_id_at(AT_FDCWD, path, ret);