]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libmount: add mnt_table_is_empty(), improve table list usage
authorKarel Zak <kzak@redhat.com>
Tue, 20 Aug 2013 12:35:13 +0000 (14:35 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 20 Aug 2013 12:35:13 +0000 (14:35 +0200)
Currently you have to use mnt_table_remove_fs() + mnt_free_fs() to
destroy the list in the table. This is complicated in same situations.
This patch allows to use mnt_free_fs() only.

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/tab.c
sys-utils/swapon.c

index d5083216e242611e7ef1851a5669e88a4ad56233..f7004fcd4b1131941624dbee97ce3f1847d2e196 100644 (file)
@@ -303,6 +303,7 @@ mnt_table_get_nents
 mnt_table_get_root_fs
 mnt_table_get_trailing_comment
 mnt_table_get_userdata
+mnt_table_is_empty
 mnt_table_is_fs_mounted
 mnt_table_last_fs
 mnt_table_next_child_fs
index 0a4f57a6c8cfa44547fd333e8f9c46a6325bc576..e09f472ab24a23463a4ac0753e4339c4f5329dea 100644 (file)
@@ -412,6 +412,7 @@ extern void mnt_free_table(struct libmnt_table *tb);
 
 extern int mnt_reset_table(struct libmnt_table *tb);
 extern int mnt_table_get_nents(struct libmnt_table *tb);
+extern int mnt_table_is_empty(struct libmnt_table *tb);
 
 extern int mnt_table_set_userdata(struct libmnt_table *tb, void *data);
 extern void *mnt_table_get_userdata(struct libmnt_table *tb);
index f9870e3c4112bd4a6e1d8bf58cc74c62e35efc9a..be140000b0198d869346e8580664ab6e6327cc62 100644 (file)
@@ -269,6 +269,7 @@ global:
        mnt_table_get_intro_comment;
        mnt_table_get_trailing_comment;
        mnt_table_get_userdata;
+       mnt_table_is_empty;
        mnt_table_last_fs;
        mnt_table_replace_file;
        mnt_table_set_intro_comment;
index 1ba7d5c4896a1d1f7e7c0527bd5cddc167939419..61b872eda09347083f33df8fd4d8bf1cf3591214 100644 (file)
@@ -266,7 +266,6 @@ struct libmnt_fs {
  */
 struct libmnt_table {
        int             fmt;            /* MNT_FMT_* file format */
-       int             nents;          /* number of valid entries */
        int             comms;          /* enable/disable comment parsing */
        char            *comm_intro;    /* First comment in file */
        char            *comm_tail;     /* Last comment in file */
index 2b258c7daf60fa43d240cdbd32171bd31791cec2..098bf368c5f96300aa381f319906bb716950b9bf 100644 (file)
@@ -92,7 +92,6 @@ int mnt_reset_table(struct libmnt_table *tb)
                mnt_free_fs(fs);
        }
 
-       tb->nents = 0;
        return 0;
 }
 
@@ -122,9 +121,30 @@ void mnt_free_table(struct libmnt_table *tb)
  * Returns: number of valid entries in tab.
  */
 int mnt_table_get_nents(struct libmnt_table *tb)
+{
+       struct list_head *p;
+       int i = 0;
+
+       assert(tb);
+       if (!tb)
+               return -EINVAL;
+       if (list_empty(&tb->ents))
+               return 0;
+       list_for_each(p, &tb->ents)
+               i++;
+       return i;
+}
+
+/**
+ * mnt_table_is_empty:
+ * @tb: pointer to tab
+ *
+ * Returns: 1 if the table is without filesystems, or 0.
+ */
+int mnt_table_is_empty(struct libmnt_table *tb)
 {
        assert(tb);
-       return tb ? tb->nents : 0;
+       return tb == NULL || list_empty(&tb->ents) ? 1 : 0;
 }
 
 /**
@@ -372,7 +392,6 @@ int mnt_table_add_fs(struct libmnt_table *tb, struct libmnt_fs *fs)
 
        DBG(TAB, mnt_debug_h(tb, "add entry: %s %s",
                        mnt_fs_get_source(fs), mnt_fs_get_target(fs)));
-       tb->nents++;
        return 0;
 }
 
@@ -391,7 +410,6 @@ int mnt_table_remove_fs(struct libmnt_table *tb, struct libmnt_fs *fs)
        if (!tb || !fs)
                return -EINVAL;
        list_del(&fs->ents);
-       tb->nents--;
        return 0;
 }
 
@@ -792,7 +810,7 @@ struct libmnt_fs *mnt_table_find_srcpath(struct libmnt_table *tb, const char *pa
 {
        struct libmnt_iter itr;
        struct libmnt_fs *fs = NULL;
-       int ntags = 0;
+       int ntags = 0, nents;
        char *cn;
        const char *p;
 
@@ -818,8 +836,10 @@ struct libmnt_fs *mnt_table_find_srcpath(struct libmnt_table *tb, const char *pa
 
        DBG(TAB, mnt_debug_h(tb, "lookup canonical SRCPATH: '%s'", cn));
 
+       nents = mnt_table_get_nents(tb);
+
        /* canonicalized paths in struct libmnt_table */
-       if (ntags < mnt_table_get_nents(tb)) {
+       if (ntags < nents) {
                mnt_reset_iter(&itr, direction);
                while(mnt_table_next_fs(tb, &itr, &fs) == 0) {
                        if (mnt_fs_streq_srcpath(fs, cn))
@@ -862,7 +882,7 @@ struct libmnt_fs *mnt_table_find_srcpath(struct libmnt_table *tb, const char *pa
        }
 
        /* non-canonicalized paths in struct libmnt_table */
-       if (ntags <= mnt_table_get_nents(tb)) {
+       if (ntags <= nents) {
                mnt_reset_iter(&itr, direction);
                while(mnt_table_next_fs(tb, &itr, &fs) == 0) {
                        if (mnt_fs_is_netfs(fs) || mnt_fs_is_pseudofs(fs))
@@ -1210,7 +1230,7 @@ int mnt_table_is_fs_mounted(struct libmnt_table *tb, struct libmnt_fs *fstab_fs)
        DBG(FS, mnt_debug_h(fstab_fs, "is FS mounted? [target=%s]",
                                mnt_fs_get_target(fstab_fs)));
 
-       if (mnt_fs_is_swaparea(fstab_fs) || mnt_table_get_nents(tb) == 0) {
+       if (mnt_fs_is_swaparea(fstab_fs) || mnt_table_is_empty(tb)) {
                DBG(FS, mnt_debug_h(fstab_fs, "- ignore (swap or no data)"));
                return 0;
        }
index f1e2433755a10a884470059b3a89aec7a8e6cfe2..4b4b7160cb82d6bd5b5bbefbeecb237e2ecd3c9d 100644 (file)
@@ -188,12 +188,14 @@ static int display_summary(void)
        if (!st)
                return -1;
 
+       if (mnt_table_is_empty(st))
+               return 0;
+
        itr = mnt_new_iter(MNT_ITER_FORWARD);
        if (!itr)
                err(EXIT_FAILURE, _("failed to initialize libmount iterator"));
 
-       if (mnt_table_get_nents(st) > 0)
-               printf(_("%s\t\t\t\tType\t\tSize\tUsed\tPriority\n"), _("Filename"));
+       printf(_("%s\t\t\t\tType\t\tSize\tUsed\tPriority\n"), _("Filename"));
 
        while (mnt_table_next_fs(st, itr, &fs) == 0) {
                printf("%-39s\t%s\t%jd\t%jd\t%d\n",