]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/commitdiff
xfs_fsr: refactor mountpoint finding to use libfrog paths functions
authorDarrick J. Wong <darrick.wong@oracle.com>
Tue, 27 Mar 2018 02:27:31 +0000 (21:27 -0500)
committerEric Sandeen <sandeen@redhat.com>
Tue, 27 Mar 2018 02:27:31 +0000 (21:27 -0500)
Refactor the mount-point finding code in fsr to use the libfrog helpers
instead of open-coding yet another routine.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Reviewed-by: Eric Sandeen <sandeen@redhat.com>
Signed-off-by: Eric Sandeen <sandeen@sandeen.net>
fsr/Makefile
fsr/xfs_fsr.c
include/path.h
libfrog/paths.c

index d3521b23642e1041c6a48a48d8338e2490387a61..4201b388bd7fe9a0a00d53c4388e81dee41eacf0 100644 (file)
@@ -7,7 +7,9 @@ include $(TOPDIR)/include/builddefs
 
 LTCOMMAND = xfs_fsr
 CFILES = xfs_fsr.c
-LLDLIBS = $(LIBHANDLE)
+LLDLIBS = $(LIBHANDLE) $(LIBFROG) $(LIBPTHREAD) $(LIBBLKID)
+LTDEPENDENCIES = $(LIBHANDLE) $(LIBFROG)
+LLDFLAGS = -static-libtool-libs
 
 ifeq ($(HAVE_GETMNTENT),yes)
 LCFLAGS += -DHAVE_GETMNTENT
index 2a18ce082b93a7ca17407e2a1740f6f7fa9c1c81..b74a70b59df6dec5e97576fa53b8d77ee3207d44 100644 (file)
@@ -22,6 +22,7 @@
 #include "jdm.h"
 #include "xfs_bmap_btree.h"
 #include "xfs_attr_sf.h"
+#include "path.h"
 
 #include <fcntl.h>
 #include <errno.h>
@@ -167,73 +168,13 @@ aborter(int unused)
        exit(1);
 }
 
-/*
- * Check if the argument is either the device name or mountpoint of an XFS
- * filesystem.  Note that we do not care about bind mounted regular files
- * here - the code that handles defragmentation of invidual files takes care
- * of that.
- */
-static char *
-find_mountpoint_check(struct stat *sb, struct mntent *t)
-{
-       struct stat ms;
-
-       if (S_ISDIR(sb->st_mode)) {             /* mount point */
-               if (stat(t->mnt_dir, &ms) < 0)
-                       return NULL;
-               if (sb->st_ino != ms.st_ino)
-                       return NULL;
-               if (sb->st_dev != ms.st_dev)
-                       return NULL;
-               if (strcmp(t->mnt_type, MNTTYPE_XFS) != 0)
-                       return NULL;
-       } else {                                /* device */
-               if (stat(t->mnt_fsname, &ms) < 0)
-                       return NULL;
-               if (sb->st_rdev != ms.st_rdev)
-                       return NULL;
-               if (strcmp(t->mnt_type, MNTTYPE_XFS) != 0)
-                       return NULL;
-               /*
-                * Make sure the mountpoint given by mtab is accessible
-                * before using it.
-                */
-               if (stat(t->mnt_dir, &ms) < 0)
-                       return NULL;
-       }
-
-       return t->mnt_dir;
-}
-
-static char *
-find_mountpoint(char *mtab, char *argname, struct stat *sb)
-{
-       struct mntent_cursor cursor;
-       struct mntent *t = NULL;
-       char *mntp = NULL;
-
-       if (platform_mntent_open(&cursor, mtab) != 0){
-               fprintf(stderr, "Error: can't get mntent entries.\n");
-               exit(1);
-       }
-
-       while ((t = platform_mntent_next(&cursor)) != NULL) {
-               mntp = find_mountpoint_check(sb, t);
-               if (mntp == NULL)
-                       continue;
-               break;
-       }
-       platform_mntent_close(&cursor);
-       return mntp;
-}
-
 int
 main(int argc, char **argv)
 {
        struct stat sb;
        char *argname;
        int c;
-       char *mntp;
+       struct fs_path  *fsp;
        char *mtab = NULL;
 
        setlinebuf(stdout);
@@ -322,7 +263,7 @@ main(int argc, char **argv)
        RealUid = getuid();
 
        pagesize = getpagesize();
-
+       fs_table_initialise(0, NULL, 0, NULL);
        if (optind < argc) {
                for (; optind < argc; optind++) {
                        argname = argv[optind];
@@ -343,9 +284,11 @@ main(int argc, char **argv)
                                sb = sb2;
                        }
 
-                       mntp = find_mountpoint(mtab, argname, &sb);
-                       if (mntp != NULL) {
-                               fsrfs(mntp, 0, 100);
+                       fsp = fs_table_lookup_mount(argname);
+                       if (!fsp)
+                               fsp = fs_table_lookup_blkdev(argname);
+                       if (fsp != NULL) {
+                               fsrfs(fsp->fs_dir, 0, 100);
                        } else if (S_ISCHR(sb.st_mode)) {
                                fprintf(stderr, _(
                                        "%s: char special not supported: %s\n"),
index 1d3a902e07b6ec0d2f8bf023b39211d177b97132..88dc44b66cbd54941620973657d95aa0ead867b7 100644 (file)
@@ -57,6 +57,7 @@ extern void fs_table_insert_project_path(char *__dir, uint __projid);
 
 extern fs_path_t *fs_table_lookup(const char *__dir, uint __flags);
 extern fs_path_t *fs_table_lookup_mount(const char *__dir);
+extern fs_path_t *fs_table_lookup_blkdev(const char *bdev);
 
 typedef struct fs_cursor {
        uint            count;          /* total count of mount entries */
index 19ee1ea9e4d9fba1f0f51f1a510ddd85d84b906a..318b48f99e1d2bd42e44e5a32c68dab7c2781786 100644 (file)
@@ -89,30 +89,26 @@ fs_table_lookup(
        return NULL;
 }
 
-/*
- * Find the FS table entry describing an actual mount for the given path.
- * Unlike fs_table_lookup(), fs_table_lookup_mount() compares the "dir"
- * argument to actual mount point entries in the table. Accordingly, it
- * will find matches only if the "dir" argument is indeed mounted.
- */
-struct fs_path *
-fs_table_lookup_mount(
-       const char      *dir)
+static struct fs_path *
+__fs_table_lookup_mount(
+       const char      *dir,
+       const char      *blkdev)
 {
        uint            i;
-       dev_t           dev = 0;
        char            rpath[PATH_MAX];
        char            dpath[PATH_MAX];
 
-       if (fs_device_number(dir, &dev))
+       if (dir && !realpath(dir, dpath))
                return NULL;
-       if (!realpath(dir, dpath))
+       if (blkdev && !realpath(blkdev, dpath))
                return NULL;
 
        for (i = 0; i < fs_count; i++) {
                if (fs_table[i].fs_flags != FS_MOUNT_POINT)
                        continue;
-               if (!realpath(fs_table[i].fs_dir, rpath))
+               if (dir && !realpath(fs_table[i].fs_dir, rpath))
+                       continue;
+               if (blkdev && !realpath(fs_table[i].fs_name, rpath))
                        continue;
                if (strcmp(rpath, dpath) == 0)
                        return &fs_table[i];
@@ -120,6 +116,32 @@ fs_table_lookup_mount(
        return NULL;
 }
 
+/*
+ * Find the FS table entry describing an actual mount for the given path.
+ * Unlike fs_table_lookup(), fs_table_lookup_mount() compares the "dir"
+ * argument to actual mount point entries in the table. Accordingly, it
+ * will find matches only if the "dir" argument is indeed mounted.
+ */
+struct fs_path *
+fs_table_lookup_mount(
+       const char      *dir)
+{
+       return __fs_table_lookup_mount(dir, NULL);
+}
+
+/*
+ * Find the FS table entry describing an actual mount for the block device.
+ * Unlike fs_table_lookup(), fs_table_lookup_blkdev() compares the "bdev"
+ * argument to actual mount point names in the table. Accordingly, it
+ * will find matches only if the "bdev" argument is indeed mounted.
+ */
+struct fs_path *
+fs_table_lookup_blkdev(
+       const char      *bdev)
+{
+       return __fs_table_lookup_mount(NULL, bdev);
+}
+
 static int
 fs_table_insert(
        char            *dir,