]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libmount: add mnt_table_find_[uniq]_id() function
authorKarel Zak <kzak@redhat.com>
Mon, 11 Nov 2024 11:45:00 +0000 (12:45 +0100)
committerKarel Zak <kzak@redhat.com>
Wed, 8 Jan 2025 12:57:43 +0000 (13:57 +0100)
Addresses: https://github.com/util-linux/util-linux/issues/3275
Signed-off-by: Karel Zak <kzak@redhat.com>
libmount/docs/libmount-sections.txt
libmount/src/libmount.h.in
libmount/src/libmount.sym
libmount/src/tab.c
libmount/src/utils.c

index 13259fa510c3ad7d948d4a88394747a02ed4ad3d..d2aa76ede9aeb8d5422c069d92c08115e73eb5f2 100644 (file)
@@ -373,6 +373,7 @@ mnt_table_enable_noautofs
 mnt_table_fetch_listmount
 mnt_table_find_devno
 mnt_table_find_fs
+mnt_table_find_id
 mnt_table_find_mountpoint
 mnt_table_find_next_fs
 mnt_table_find_pair
@@ -381,6 +382,7 @@ mnt_table_find_srcpath
 mnt_table_find_tag
 mnt_table_find_target
 mnt_table_find_target_with_option
+mnt_table_find_uniq_id
 mnt_table_first_fs
 mnt_table_get_cache
 mnt_table_get_intro_comment
index 555c7be343d5aa409d841a01bba6323aeef44e14..6779de7c601d7df0be901de606cd9f676afe2159 100644 (file)
@@ -681,6 +681,8 @@ extern struct libmnt_fs *mnt_table_find_pair(struct libmnt_table *tb,
                                const char *target, int direction);
 extern struct libmnt_fs *mnt_table_find_devno(struct libmnt_table *tb,
                                dev_t devno, int direction);
+extern struct libmnt_fs *mnt_table_find_uniq_id(struct libmnt_table *tb, uint64_t id);
+extern struct libmnt_fs *mnt_table_find_id(struct libmnt_table *tb, int id);
 
 extern int mnt_table_find_next_fs(struct libmnt_table *tb,
                        struct libmnt_iter *itr,
index d09b426351bc4a4e986d595c93c8c515fd31fe5b..8de169b6ed64e4efd889d2312fc3442c204492f3 100644 (file)
@@ -398,6 +398,8 @@ MOUNT_2_41 {
        mnt_statmnt_set_mask;
        mnt_table_enable_listmount;
        mnt_table_fetch_listmount;
+       mnt_table_find_id;
+       mnt_table_find_uniq_id;
        mnt_table_listmount_set_id;
        mnt_table_listmount_set_ns;
        mnt_table_listmount_set_stepsiz;
index ba4eef850ad0e14cd8fda70c67dedd46eba63af0..8508715eab2768b6fca0b0e507f9e6bbdbb03334 100644 (file)
@@ -1541,6 +1541,67 @@ struct libmnt_fs *mnt_table_find_devno(struct libmnt_table *tb,
        return NULL;
 }
 
+/**
+ * mnt_table_find_id:
+ * @tb: mount table
+ * @id: classic mount ID
+ *
+ * See also mnt_id_from_path().
+ *
+ * Returns: a tab entry or NULL.
+ *
+ * Since: 2.41
+ */
+struct libmnt_fs *mnt_table_find_id(struct libmnt_table *tb, int id)
+{
+       struct libmnt_fs *fs = NULL;
+       struct libmnt_iter itr;
+
+       if (!tb)
+               return NULL;
+
+       DBG(TAB, ul_debugobj(tb, "lookup ID: %d", id));
+       mnt_reset_iter(&itr, MNT_ITER_BACKWARD);
+
+       while (mnt_table_next_fs(tb, &itr, &fs) == 0) {
+               if (mnt_fs_get_id(fs) == id)
+                       return fs;
+       }
+
+       return NULL;
+}
+
+/**
+ * mnt_table_find_uniq_id:
+ * @tb: mount table
+ * @id: uniqie 64-bit mount ID
+ *
+ * See also mnt_id_from_path().
+ *
+ * Returns: a tab entry or NULL.
+ *
+ * Since: 2.41
+ */
+struct libmnt_fs *mnt_table_find_uniq_id(struct libmnt_table *tb, uint64_t id)
+{
+       struct libmnt_fs *fs = NULL;
+       struct libmnt_iter itr;
+
+       if (!tb)
+               return NULL;
+
+       DBG(TAB, ul_debugobj(tb, "lookup uniq-ID: %" PRIu64, id));
+       mnt_reset_iter(&itr, MNT_ITER_BACKWARD);
+
+       while (mnt_table_next_fs(tb, &itr, &fs) == 0) {
+               if (mnt_fs_get_uniq_id(fs) == id)
+                       return fs;
+       }
+
+       return NULL;
+}
+
+
 static char *remove_mountpoint_from_path(const char *path, const char *mnt)
 {
         char *res;
index a090373728bb910bc442350cf76777d386c88699..0601def97c2c9819788f785d9817e5550fa8a395 100644 (file)
@@ -319,8 +319,8 @@ int mnt_id_from_fd(int fd, uint64_t *uniq_id, int *id)
 /**
  * mnt_id_from_path:
  * @path: mountpoint
- * @uniq_id: returns STATX_MNT_ID_UNIQUE
- * @id: returns STATX_MNT_ID
+ * @uniq_id: returns STATX_MNT_ID_UNIQUE (optional)
+ * @id: returns STATX_MNT_ID (optional)
  *
  * Converts @path to ID.
  *