]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lsblk: move mount stuff to lsblk-mnt.c
authorKarel Zak <kzak@redhat.com>
Fri, 14 Sep 2018 13:41:39 +0000 (15:41 +0200)
committerKarel Zak <kzak@redhat.com>
Fri, 14 Sep 2018 13:41:39 +0000 (15:41 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
misc-utils/Makemodule.am
misc-utils/lsblk-mnt.c [new file with mode: 0644]
misc-utils/lsblk.c
misc-utils/lsblk.h

index 3fb3114ed8697bfdee8149cd140287305b355f38..8b58fd5dfd3b9c35b02d38fb3476b9c6b7d43441 100644 (file)
@@ -79,6 +79,7 @@ bin_PROGRAMS += lsblk
 dist_man_MANS += misc-utils/lsblk.8
 lsblk_SOURCES = \
        misc-utils/lsblk.c \
+       misc-utils/lsblk-mnt.c \
        misc-utils/lsblk.h
 lsblk_LDADD = $(LDADD) libblkid.la libmount.la libcommon.la libsmartcols.la
 lsblk_CFLAGS = $(AM_CFLAGS) -I$(ul_libblkid_incdir) -I$(ul_libmount_incdir) -I$(ul_libsmartcols_incdir)
diff --git a/misc-utils/lsblk-mnt.c b/misc-utils/lsblk-mnt.c
new file mode 100644 (file)
index 0000000..4e1e328
--- /dev/null
@@ -0,0 +1,111 @@
+#include "c.h"
+#include "pathnames.h"
+#include "xalloc.h"
+#include "nls.h"
+
+#include <libmount.h>
+
+#include "lsblk.h"
+
+static struct libmnt_table *mtab, *swaps;
+static struct libmnt_cache *mntcache;
+
+static int table_parser_errcb(struct libmnt_table *tb __attribute__((__unused__)),
+                       const char *filename, int line)
+{
+       if (filename)
+               warnx(_("%s: parse error at line %d -- ignored"), filename, line);
+       return 1;
+}
+
+static int is_active_swap(const char *filename)
+{
+       if (!swaps) {
+               swaps = mnt_new_table();
+               if (!swaps)
+                       return 0;
+               if (!mntcache)
+                       mntcache = mnt_new_cache();
+
+               mnt_table_set_parser_errcb(swaps, table_parser_errcb);
+               mnt_table_set_cache(swaps, mntcache);
+
+               if (!lsblk->sysroot)
+                       mnt_table_parse_swaps(swaps, NULL);
+               else {
+                       char buf[PATH_MAX];
+                       snprintf(buf, sizeof(buf), "%s" _PATH_PROC_SWAPS, lsblk->sysroot);
+                       mnt_table_parse_swaps(swaps, buf);
+               }
+       }
+
+       return mnt_table_find_srcpath(swaps, filename, MNT_ITER_BACKWARD) != NULL;
+}
+
+char *get_device_mountpoint(struct blkdev_cxt *cxt)
+{
+       struct libmnt_fs *fs;
+       const char *fsroot;
+
+       assert(cxt);
+       assert(cxt->filename);
+
+       if (cxt->is_mounted)
+               return cxt->mountpoint;
+
+       if (!mtab) {
+               mtab = mnt_new_table();
+               if (!mtab)
+                       return NULL;
+               if (!mntcache)
+                       mntcache = mnt_new_cache();
+
+               mnt_table_set_parser_errcb(mtab, table_parser_errcb);
+               mnt_table_set_cache(mtab, mntcache);
+
+               if (!lsblk->sysroot)
+                       mnt_table_parse_mtab(mtab, NULL);
+               else {
+                       char buf[PATH_MAX];
+                       snprintf(buf, sizeof(buf), "%s" _PATH_PROC_MOUNTINFO, lsblk->sysroot);
+                       mnt_table_parse_mtab(mtab, buf);
+               }
+       }
+
+       /* Note that maj:min in /proc/self/mountinfo does not have to match with
+        * devno as returned by stat(), so we have to try devname too
+        */
+       fs = mnt_table_find_devno(mtab, makedev(cxt->maj, cxt->min), MNT_ITER_BACKWARD);
+       if (!fs)
+               fs = mnt_table_find_srcpath(mtab, cxt->filename, MNT_ITER_BACKWARD);
+       if (!fs) {
+               cxt->mountpoint = is_active_swap(cxt->filename) ? xstrdup("[SWAP]") : NULL;
+               cxt->is_mounted = 1;
+               return cxt->mountpoint;
+       }
+
+       /* found */
+       fsroot = mnt_fs_get_root(fs);
+       if (fsroot && strcmp(fsroot, "/") != 0) {
+               /* hmm.. we found bind mount or btrfs subvolume, let's try to
+                * get real FS root mountpoint */
+               struct libmnt_fs *rfs;
+               struct libmnt_iter *itr = mnt_new_iter(MNT_ITER_BACKWARD);
+
+               mnt_table_set_iter(mtab, itr, fs);
+               while (mnt_table_next_fs(mtab, itr, &rfs) == 0) {
+                       fsroot = mnt_fs_get_root(rfs);
+                       if ((!fsroot || strcmp(fsroot, "/") == 0)
+                           && mnt_fs_match_source(rfs, cxt->filename, mntcache)) {
+                               fs = rfs;
+                               break;
+                       }
+               }
+               mnt_free_iter(itr);
+       }
+
+       DBG(DEV, ul_debugobj(cxt, "mountpoint: %s", mnt_fs_get_target(fs)));
+       cxt->mountpoint = xstrdup(mnt_fs_get_target(fs));
+       cxt->is_mounted = 1;
+       return cxt->mountpoint;
+}
index 75ddd70ee57e334a2d39ac64d7285fb92e18e54d..de31f38329a00d4eef21f4a53eff364c2a497519 100644 (file)
@@ -245,9 +245,6 @@ static size_t nexcludes;
 static int includes[256];
 static size_t nincludes;
 
-static struct libmnt_table *mtab, *swaps;
-static struct libmnt_cache *mntcache;
-
 #ifdef HAVE_LIBUDEV
 static struct udev *udev;
 #endif
@@ -396,106 +393,6 @@ static char *get_device_path(struct blkdev_cxt *cxt)
        return xstrdup(path);
 }
 
-static int table_parser_errcb(struct libmnt_table *tb __attribute__((__unused__)),
-                       const char *filename, int line)
-{
-       if (filename)
-               warnx(_("%s: parse error at line %d -- ignored"), filename, line);
-       return 1;
-}
-
-static int is_active_swap(const char *filename)
-{
-       if (!swaps) {
-               swaps = mnt_new_table();
-               if (!swaps)
-                       return 0;
-               if (!mntcache)
-                       mntcache = mnt_new_cache();
-
-               mnt_table_set_parser_errcb(swaps, table_parser_errcb);
-               mnt_table_set_cache(swaps, mntcache);
-
-               if (!lsblk->sysroot)
-                       mnt_table_parse_swaps(swaps, NULL);
-               else {
-                       char buf[PATH_MAX];
-                       snprintf(buf, sizeof(buf), "%s" _PATH_PROC_SWAPS, lsblk->sysroot);
-                       mnt_table_parse_swaps(swaps, buf);
-               }
-       }
-
-       return mnt_table_find_srcpath(swaps, filename, MNT_ITER_BACKWARD) != NULL;
-}
-
-static char *get_device_mountpoint(struct blkdev_cxt *cxt)
-{
-       struct libmnt_fs *fs;
-       const char *fsroot;
-
-       assert(cxt);
-       assert(cxt->filename);
-
-       if (cxt->is_mounted)
-               return cxt->mountpoint;
-
-       if (!mtab) {
-               mtab = mnt_new_table();
-               if (!mtab)
-                       return NULL;
-               if (!mntcache)
-                       mntcache = mnt_new_cache();
-
-               mnt_table_set_parser_errcb(mtab, table_parser_errcb);
-               mnt_table_set_cache(mtab, mntcache);
-
-               if (!lsblk->sysroot)
-                       mnt_table_parse_mtab(mtab, NULL);
-               else {
-                       char buf[PATH_MAX];
-                       snprintf(buf, sizeof(buf), "%s" _PATH_PROC_MOUNTINFO, lsblk->sysroot);
-                       mnt_table_parse_mtab(mtab, buf);
-               }
-       }
-
-       /* Note that maj:min in /proc/self/mountinfo does not have to match with
-        * devno as returned by stat(), so we have to try devname too
-        */
-       fs = mnt_table_find_devno(mtab, makedev(cxt->maj, cxt->min), MNT_ITER_BACKWARD);
-       if (!fs)
-               fs = mnt_table_find_srcpath(mtab, cxt->filename, MNT_ITER_BACKWARD);
-       if (!fs) {
-               cxt->mountpoint = is_active_swap(cxt->filename) ? xstrdup("[SWAP]") : NULL;
-               cxt->is_mounted = 1;
-               return cxt->mountpoint;
-       }
-
-       /* found */
-       fsroot = mnt_fs_get_root(fs);
-       if (fsroot && strcmp(fsroot, "/") != 0) {
-               /* hmm.. we found bind mount or btrfs subvolume, let's try to
-                * get real FS root mountpoint */
-               struct libmnt_fs *rfs;
-               struct libmnt_iter *itr = mnt_new_iter(MNT_ITER_BACKWARD);
-
-               mnt_table_set_iter(mtab, itr, fs);
-               while (mnt_table_next_fs(mtab, itr, &rfs) == 0) {
-                       fsroot = mnt_fs_get_root(rfs);
-                       if ((!fsroot || strcmp(fsroot, "/") == 0)
-                           && mnt_fs_match_source(rfs, cxt->filename, mntcache)) {
-                               fs = rfs;
-                               break;
-                       }
-               }
-               mnt_free_iter(itr);
-       }
-
-       DBG(DEV, ul_debugobj(cxt, "mountpoint: %s", mnt_fs_get_target(fs)));
-       cxt->mountpoint = xstrdup(mnt_fs_get_target(fs));
-       cxt->is_mounted = 1;
-       return cxt->mountpoint;
-}
-
 #ifndef HAVE_LIBUDEV
 static int get_udev_properties(struct blkdev_cxt *cxt
                                __attribute__((__unused__)))
@@ -1970,7 +1867,6 @@ int main(int argc, char *argv[])
                lsblk->sort_hidden = 1;
        }
 
-       mnt_init_debug(0);
        scols_init_debug(0);
        ul_path_init_debug();
 
@@ -2059,9 +1955,6 @@ leave:
 
        scols_unref_table(lsblk->table);
 
-       mnt_unref_table(mtab);
-       mnt_unref_table(swaps);
-       mnt_unref_cache(mntcache);
 #ifdef HAVE_LIBUDEV
        udev_unref(udev);
 #endif
index 5608c45cf298b9235f930d8d2c509aac8bea7c57..0ab7c599582d459989f42e4dacb2987417e01603 100644 (file)
@@ -92,4 +92,6 @@ struct blkdev_cxt {
        unsigned int    is_mounted : 1;
 };
 
+extern char *get_device_mountpoint(struct blkdev_cxt *cxt);
+
 #endif /* UTIL_LINUX_LSBLK_H */