From: Karel Zak Date: Wed, 11 Dec 2013 16:01:23 +0000 (+0100) Subject: fstrim: de-duplicate list of filesystems X-Git-Tag: v2.25-rc1~675 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e05a3400e79e8cba6aca8725fd53d2b9753f0cf3;p=thirdparty%2Futil-linux.git fstrim: de-duplicate list of filesystems Reported-by: Pádraig Brady Signed-off-by: Karel Zak --- diff --git a/sys-utils/fstrim.c b/sys-utils/fstrim.c index 747e6bb23c..f58b6d4c35 100644 --- a/sys-utils/fstrim.c +++ b/sys-utils/fstrim.c @@ -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 */