]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libmount: add API to read ID by statx()
authorKarel Zak <kzak@redhat.com>
Wed, 14 Aug 2024 14:28:48 +0000 (16:28 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 8 Jan 2025 12:57:42 +0000 (13:57 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
libmount/docs/libmount-sections.txt
libmount/src/libmount.h.in
libmount/src/libmount.sym
libmount/src/mountP.h
libmount/src/utils.c

index a8a503144c0a070dca934562462f0cf8a4e24889..b72229c585bd1f592a00b46eeeb619b3ee18e4ce 100644 (file)
@@ -430,6 +430,7 @@ mnt_get_mtab_path
 mnt_get_swaps_path
 mnt_guess_system_root
 mnt_has_regular_mtab
+mnt_id_from_path
 mnt_mangle
 mnt_match_fstype
 mnt_tag_is_valid
index 90f6a66c0eb9c069e0e5543e1e9563643b52eabc..146240630b2679d281009d9f97c9fe0cb2e35958 100644 (file)
@@ -369,6 +369,8 @@ extern char *mnt_get_mountpoint(const char *path)
 extern int mnt_guess_system_root(dev_t devno, struct libmnt_cache *cache, char **path)
                        __ul_attribute__((nonnull(3)));
 
+extern int mnt_id_from_path(const char *path, uint64_t *uniq_id, int *id);
+
 /* cache.c */
 extern struct libmnt_cache *mnt_new_cache(void)
                        __ul_attribute__((warn_unused_result));
index cdd76ba3283e376164595ff4c7c85b87205d963b..af7b2ac21a62b06f448ab4ab377a8729fd1f8692 100644 (file)
@@ -386,4 +386,5 @@ MOUNT_2_41 {
        mnt_fs_get_uniq_id;
        mnt_fs_get_parent_uniq_id;
        mnt_fs_set_uniq_id;
+       mnt_id_from_path;
 } MOUNT_2_40;
index 370bcd9e74472e17e0cfcdf711ecd1710f7075b0..b1374ceb226030f50bc831dceaf05da0c42b6e68 100644 (file)
@@ -136,6 +136,8 @@ extern int mnt_is_path(const char *target);
 extern int mnt_tmptgt_unshare(int *old_ns_fd);
 extern int mnt_tmptgt_cleanup(int old_ns_fd);
 
+extern int mnt_id_from_fd(int fd, uint64_t *uniq_id, int *id);
+
 /* tab.c */
 extern int is_mountinfo(struct libmnt_table *tb);
 extern int mnt_table_set_parser_fltrcb(        struct libmnt_table *tb,
index 94a877cf051b131dfb9602cb68b9faa5fb117a0e..e5e37662921efc6838c4ae2539bcb75857be359f 100644 (file)
@@ -266,6 +266,63 @@ int mnt_is_readonly(const char *path)
        return 0;
 }
 
+static int get_mnt_id( int fd, const char *path,
+                       uint64_t *uniq_id, int *id)
+{
+#if defined(HAVE_STATX) && defined(HAVE_STRUCT_STATX) && defined(HAVE_STRUCT_STATX_STX_MNT_ID)
+       int rc;
+       struct statx sx = { 0 };
+       int flags = AT_STATX_DONT_SYNC | AT_NO_AUTOMOUNT;
+
+       if (!path || !*path)
+               flags |= AT_EMPTY_PATH;
+
+       if (id) {
+               rc = statx(fd, path ? path : "", flags,
+                               STATX_MNT_ID, &sx);
+               if (rc)
+                       return rc;
+               *id = sx.stx_mnt_id;
+       }
+       if (uniq_id) {
+               errno = 0;
+               rc = statx(fd, path ? path : "", flags,
+                               STATX_MNT_ID_UNIQUE, &sx);
+
+               if (rc && errno == EINVAL)
+                       return -ENOSYS;         /* *_ID_UNIQUE unsupported? */
+               if (rc)
+                       return rc;
+               *uniq_id = sx.stx_mnt_id;
+       }
+       return 0;
+#else
+       return -ENOSYS;
+#endif
+}
+
+int mnt_id_from_fd(int fd, uint64_t *uniq_id, int *id)
+{
+       return get_mnt_id(fd, NULL, uniq_id, id);
+}
+
+/**
+ * mnt_id_from_path:
+ * @path: mountpoint
+ * @uniq_id: returns STATX_MNT_ID_UNIQUE
+ * @id: returns STATX_MNT_ID
+ *
+ * Converts @path to ID.
+ *
+ * Returns: 0 on success, <0 on error
+ *
+ * Since: 2.41
+ */
+int mnt_id_from_path(const char *path, uint64_t *uniq_id, int *id)
+{
+       return get_mnt_id(-1, path, uniq_id, id);
+}
+
 /**
  * mnt_mangle:
  * @str: string