]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
mountpoint-util: introduce name_to_handle_at_u64() and friends
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 3 Feb 2026 15:27:10 +0000 (00:27 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 3 Feb 2026 17:45:06 +0000 (02:45 +0900)
They will be used later.

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

index 3bb82ef239bfa1351dc07aeee21b2a3b0c3f2378..547183395e4e61db7343f5048bd09e134b51cb17 100644 (file)
@@ -17,6 +17,7 @@
 #include "stat-util.h"
 #include "string-util.h"
 #include "strv.h"
+#include "unaligned.h"
 
 /* This is the original MAX_HANDLE_SZ definition from the kernel, when the API was introduced. We use that in place of
  * any more currently defined value to future-proof things: if the size is increased in the API headers, and our code
@@ -138,6 +139,27 @@ int name_to_handle_at_try_fid(
         return name_to_handle_at_loop(fd, path, ret_handle, ret_mnt_id, flags & ~AT_HANDLE_FID);
 }
 
+int name_to_handle_at_u64(int fd, const char *path, uint64_t *ret) {
+        _cleanup_free_ struct file_handle *h = NULL;
+        int r;
+
+        assert(fd >= 0 || fd == AT_FDCWD);
+
+        /* This provides the first 64bit of the file handle. */
+
+        r = name_to_handle_at_loop(fd, path, &h, /* ret_mnt_id= */ NULL, /* flags= */ 0);
+        if (r < 0)
+                return r;
+        if (h->handle_bytes < sizeof(uint64_t))
+                return -EBADMSG;
+
+        if (ret)
+                /* Note, "struct file_handle" is 32bit aligned usually, but we need to read a 64bit value from it */
+                *ret = unaligned_read_ne64(h->f_handle);
+
+        return 0;
+}
+
 bool file_handle_equal(const struct file_handle *a, const struct file_handle *b) {
         if (a == b)
                 return true;
index 659f0385560a83c9e126f096e4c75606b152f48b..8799224f3d436b4ac2fb49f8c490c1b6b6a0df69 100644 (file)
@@ -36,6 +36,13 @@ bool is_name_to_handle_at_fatal_error(int err);
 
 int name_to_handle_at_loop(int fd, const char *path, struct file_handle **ret_handle, int *ret_mnt_id, int flags);
 int name_to_handle_at_try_fid(int fd, const char *path, struct file_handle **ret_handle, int *ret_mnt_id, int flags);
+int name_to_handle_at_u64(int fd, const char *path, uint64_t *ret);
+static inline int path_to_handle_u64(const char *path, uint64_t *ret) {
+        return name_to_handle_at_u64(AT_FDCWD, path, ret);
+}
+static inline int fd_to_handle_u64(int fd, uint64_t *ret) {
+        return name_to_handle_at_u64(fd, NULL, ret);
+}
 
 bool file_handle_equal(const struct file_handle *a, const struct file_handle *b);