]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libmount: add mnt_table_over_fs()
authorKarel Zak <kzak@redhat.com>
Mon, 22 Mar 2021 10:24:49 +0000 (11:24 +0100)
committerKarel Zak <kzak@redhat.com>
Mon, 22 Mar 2021 10:24:49 +0000 (11:24 +0100)
The function returns the first over-mount for specified filesystem.

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

index 911dc0a0f0c4c7f5580272f71976985e1529e4d5..89b2b1e8baf0a2e3eee9d2612e79e7296253ec8b 100644 (file)
@@ -365,6 +365,7 @@ mnt_table_last_fs
 mnt_table_move_fs
 mnt_table_next_child_fs
 mnt_table_next_fs
+mnt_table_over_fs
 mnt_table_parse_dir
 mnt_table_parse_file
 mnt_table_parse_fstab
index b7b278d0e3f265bd51e0beb670e24e3cf3d42cc8..2d4d044e951f5722b6a1ab46d14cbac5d557d02d 100644 (file)
@@ -570,6 +570,8 @@ extern int mnt_table_move_fs(struct libmnt_table *src, struct libmnt_table *dst,
 extern int mnt_table_remove_fs(struct libmnt_table *tb, struct libmnt_fs *fs);
 extern int mnt_table_first_fs(struct libmnt_table *tb, struct libmnt_fs **fs);
 extern int mnt_table_last_fs(struct libmnt_table *tb, struct libmnt_fs **fs);
+extern int mnt_table_over_fs(struct libmnt_table *tb, struct libmnt_fs *parent,
+                             struct libmnt_fs **child);
 extern int mnt_table_next_fs(struct libmnt_table *tb, struct libmnt_iter *itr,
                             struct libmnt_fs **fs);
 extern int mnt_table_next_child_fs(struct libmnt_table *tb, struct libmnt_iter *itr,
index e98e44b9c6302d1c3565700702ba0fbc7a7905ad..85e89ad02084ed7b46a25235b9fc391521dd9396 100644 (file)
@@ -359,4 +359,5 @@ MOUNT_2_35 {
 
 MOUNT_2_37 {
        mnt_fs_get_vfs_options_all;
+       mnt_table_over_fs;
 } MOUNT_2_35;
index e91254a712ee490387034e8445e703439b93fd67..465708f61818e5d06cc584b232b4b36c0bf23aa6 100644 (file)
@@ -706,6 +706,55 @@ int mnt_table_next_child_fs(struct libmnt_table *tb, struct libmnt_iter *itr,
        return 0;
 }
 
+/**
+ * mnt_table_over_fs:
+ * @tb: tab pointer
+ * @parent: pointer to parental FS
+ * @child: returns pointer to FS which over-mounting parent
+ *
+ * This function returns by @child the first filesystenm which is over-mounted
+ * on @parent. It means the mountpoint of @child is the same as for @parent and
+ * parent->id is the same as child->parent_id.
+ *
+ * Note that you need to call this function in loop until it returns 1 to get
+ * the highest filesystem.
+ *
+ * Example:
+ * <informalexample>
+ *   <programlisting>
+ *     while (mnt_table_over_fs(tb, cur, &over) == 0) {
+ *             printf("%s overmounted by %d\n", mnt_fs_get_target(cur), mnt_fs_get_id(over));
+ *             cur = over;
+ *     }
+ *   </programlisting>
+ * </informalexample>
+ *
+ * Returns: 0 on success, negative number in case of error or 1 at the end of list.
+ */
+int mnt_table_over_fs(struct libmnt_table *tb, struct libmnt_fs *parent,
+                     struct libmnt_fs **child)
+{
+       struct libmnt_iter itr;
+       int id;
+       const char *tgt;
+
+       if (!tb || !parent || !is_mountinfo(tb))
+               return -EINVAL;
+
+       mnt_reset_iter(&itr, MNT_ITER_FORWARD);
+       id = mnt_fs_get_id(parent);
+       tgt = mnt_fs_get_target(parent);
+
+       while (mnt_table_next_fs(tb, &itr, child) == 0) {
+               if (mnt_fs_get_parent_id(*child) == id &&
+                   mnt_fs_streq_target(*child, tgt) == 1)
+                       return 0;
+       }
+
+       *child = NULL;
+       return 1;       /* nothing */
+}
+
 /**
  * mnt_table_next_fs:
  * @tb: tab pointer