]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
fstrim: de-duplicate list of filesystems
authorKarel Zak <kzak@redhat.com>
Wed, 11 Dec 2013 16:01:23 +0000 (17:01 +0100)
committerKarel Zak <kzak@redhat.com>
Wed, 11 Dec 2013 16:01:23 +0000 (17:01 +0100)
Reported-by: Pádraig Brady <P@draigBrady.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
sys-utils/fstrim.c

index 747e6bb23cc55ebb6e44a074ddf2a37a5a5a71f1..f58b6d4c35c4c7b32a4193af0d877b8840502b0e 100644 (file)
@@ -139,6 +139,15 @@ static int has_discard(const char *devname, struct sysfs_cxt *wholedisk)
        return rc == 0 && dg > 0;
 }
 
+
+static int uniq_fs_target_cmp(
+               struct libmnt_table *tb __attribute__((__unused__)),
+               struct libmnt_fs *a,
+               struct libmnt_fs *b)
+{
+       return !mnt_fs_streq_target(a, mnt_fs_get_target(b));
+}
+
 /*
  * fstrim --all follows "mount -a" return codes:
  *
@@ -154,6 +163,8 @@ static int fstrim_all(struct fstrim_range *rangetpl, int verbose)
        struct sysfs_cxt wholedisk = UL_SYSFSCXT_EMPTY;
        int cnt = 0, cnt_err = 0;
 
+       mnt_init_debug(0);
+
        itr = mnt_new_iter(MNT_ITER_BACKWARD);
        if (!itr)
                err(MOUNT_EX_FAIL, _("failed to initialize libmount iterator"));
@@ -162,6 +173,9 @@ static int fstrim_all(struct fstrim_range *rangetpl, int verbose)
        if (!tab)
                err(MOUNT_EX_FAIL, _("failed to parse %s"), _PATH_PROC_MOUNTINFO);
 
+       /* de-duplicate the table */
+       mnt_table_uniq_fs(tab, 0, uniq_fs_target_cmp);
+
        while (mnt_table_next_fs(tab, itr, &fs) == 0) {
                const char *src = mnt_fs_get_srcpath(fs),
                           *tgt = mnt_fs_get_target(fs);
@@ -199,7 +213,7 @@ static int fstrim_all(struct fstrim_range *rangetpl, int verbose)
        }
 
        sysfs_deinit(&wholedisk);
-       mnt_free_table(tab);
+       mnt_unref_table(tab);
 
        if (cnt && cnt == cnt_err)
                return MOUNT_EX_FAIL;           /* all failed */