btrfs-progs: add getopt stubs where needed
[platform/upstream/btrfs-progs.git] / cmds-inspect.c
index a13a170..dbc6656 100644 (file)
@@ -31,6 +31,8 @@
 #include "disk-io.h"
 #include "commands.h"
 #include "btrfs-list.h"
+#include "cmds-inspect-dump-tree.h"
+#include "cmds-inspect-dump-super.h"
 
 static const char * const inspect_cmd_group_usage[] = {
        "btrfs inspect-internal <command> <args>",
@@ -42,19 +44,15 @@ static int __ino_to_path_fd(u64 inum, int fd, int verbose, const char *prepend)
        int ret;
        int i;
        struct btrfs_ioctl_ino_path_args ipa;
-       struct btrfs_data_container *fspath;
-
-       fspath = malloc(4096);
-       if (!fspath)
-               return -ENOMEM;
+       struct btrfs_data_container fspath[PATH_MAX];
 
        memset(fspath, 0, sizeof(*fspath));
        ipa.inum = inum;
-       ipa.size = 4096;
+       ipa.size = PATH_MAX;
        ipa.fspath = ptr_to_u64(fspath);
 
        ret = ioctl(fd, BTRFS_IOC_INO_PATHS, &ipa);
-       if (ret) {
+       if (ret < 0) {
                printf("ioctl ret=%d, error: %s\n", ret, strerror(errno));
                goto out;
        }
@@ -79,7 +77,6 @@ static int __ino_to_path_fd(u64 inum, int fd, int verbose, const char *prepend)
        }
 
 out:
-       free(fspath);
        return !!ret;
 }
 
@@ -116,11 +113,9 @@ static int cmd_inspect_inode_resolve(int argc, char **argv)
        if (check_argc_exact(argc - optind, 2))
                usage(cmd_inspect_inode_resolve_usage);
 
-       fd = open_file_or_dir(argv[optind+1], &dirstream);
-       if (fd < 0) {
-               fprintf(stderr, "ERROR: can't access '%s'\n", argv[optind+1]);
+       fd = btrfs_open_dir(argv[optind + 1], &dirstream, 1);
+       if (fd < 0)
                return 1;
-       }
 
        ret = __ino_to_path_fd(arg_strtou64(argv[optind]), fd, verbose,
                               argv[optind+1]);
@@ -189,15 +184,14 @@ static int cmd_inspect_logical_resolve(int argc, char **argv)
        loi.size = size;
        loi.inodes = ptr_to_u64(inodes);
 
-       fd = open_file_or_dir(argv[optind+1], &dirstream);
+       fd = btrfs_open_dir(argv[optind + 1], &dirstream, 1);
        if (fd < 0) {
-               fprintf(stderr, "ERROR: can't access '%s'\n", argv[optind+1]);
                ret = 12;
                goto out;
        }
 
        ret = ioctl(fd, BTRFS_IOC_LOGICAL_INO, &loi);
-       if (ret) {
+       if (ret < 0) {
                printf("ioctl ret=%d, error: %s\n", ret, strerror(errno));
                goto out;
        }
@@ -239,10 +233,8 @@ static int cmd_inspect_logical_resolve(int argc, char **argv)
                                                name);
                                BUG_ON(ret >= bytes_left);
                                free(name);
-                               path_fd = open_file_or_dir(full_path, &dirs);
+                               path_fd = btrfs_open_dir(full_path, &dirs, 1);
                                if (path_fd < 0) {
-                                       fprintf(stderr, "ERROR: can't access "
-                                               "'%s'\n", full_path);
                                        ret = -ENOENT;
                                        goto out;
                                }
@@ -276,23 +268,24 @@ static int cmd_inspect_subvolid_resolve(int argc, char **argv)
        char path[PATH_MAX];
        DIR *dirstream = NULL;
 
-       if (check_argc_exact(argc, 3))
+       clean_args_no_options(argc, argv, cmd_inspect_subvolid_resolve_usage);
+
+       if (check_argc_exact(argc - optind, 2))
                usage(cmd_inspect_subvolid_resolve_usage);
 
-       fd = open_file_or_dir(argv[2], &dirstream);
+       fd = btrfs_open_dir(argv[optind], &dirstream, 1);
        if (fd < 0) {
-               fprintf(stderr, "ERROR: can't access '%s'\n", argv[2]);
                ret = -ENOENT;
                goto out;
        }
 
-       subvol_id = arg_strtou64(argv[1]);
+       subvol_id = arg_strtou64(argv[optind]);
        ret = btrfs_subvolid_resolve(fd, path, sizeof(path), subvol_id);
 
        if (ret) {
                fprintf(stderr,
-                       "%s: btrfs_subvolid_resolve(subvol_id %llu) failed with ret=%d\n",
-                       argv[0], (unsigned long long)subvol_id, ret);
+                       "btrfs_subvolid_resolve(subvol_id %llu) failed with ret=%d\n",
+                       (unsigned long long)subvol_id, ret);
                goto out;
        }
 
@@ -317,20 +310,20 @@ static int cmd_inspect_rootid(int argc, char **argv)
        u64 rootid;
        DIR *dirstream = NULL;
 
-       if (check_argc_exact(argc, 2))
+       clean_args_no_options(argc, argv, cmd_inspect_rootid_usage);
+
+       if (check_argc_exact(argc - optind, 1))
                usage(cmd_inspect_rootid_usage);
 
-       fd = open_file_or_dir(argv[1], &dirstream);
+       fd = btrfs_open_dir(argv[optind], &dirstream, 1);
        if (fd < 0) {
-               fprintf(stderr, "ERROR: can't access '%s'\n", argv[1]);
                ret = -ENOENT;
                goto out;
        }
 
        ret = lookup_ino_rootid(fd, &rootid);
        if (ret) {
-               fprintf(stderr, "%s: rootid failed with ret=%d\n",
-                       argv[0], ret);
+               fprintf(stderr, "rootid failed with ret=%d\n", ret);
                goto out;
        }
 
@@ -341,6 +334,14 @@ out:
        return !!ret;
 }
 
+static const char* const cmd_inspect_min_dev_size_usage[] = {
+       "btrfs inspect-internal min-dev-size [options] <path>",
+       "Get the minimum size the device can be shrunk to. The",
+       "device id 1 is used by default.",
+       "--id DEVID   specify the device id to query",
+       NULL
+};
+
 struct dev_extent_elem {
        u64 start;
        /* inclusive end */
@@ -581,14 +582,6 @@ out:
        return ret;
 }
 
-static const char* const cmd_inspect_min_dev_size_usage[] = {
-       "btrfs inspect-internal min-dev-size [options] <path>",
-       "Get the minimum size the device can be shrunk to. The",
-       "device id 1 is used by default.",
-       "--id DEVID   specify the device id to query",
-       NULL
-};
-
 static int cmd_inspect_min_dev_size(int argc, char **argv)
 {
        int ret;
@@ -619,9 +612,8 @@ static int cmd_inspect_min_dev_size(int argc, char **argv)
        if (check_argc_exact(argc - optind, 1))
                usage(cmd_inspect_min_dev_size_usage);
 
-       fd = open_file_or_dir(argv[optind], &dirstream);
+       fd = btrfs_open_dir(argv[optind], &dirstream, 1);
        if (fd < 0) {
-               fprintf(stderr, "ERROR: can't access '%s'\n", argv[optind]);
                ret = -ENOENT;
                goto out;
        }
@@ -632,6 +624,16 @@ out:
        return !!ret;
 }
 
+static int cmd_inspect_dump_tree_hook(int ac, char **av)
+{
+       return cmd_inspect_dump_tree(ac, av);
+}
+
+static int cmd_inspect_dump_super_hook(int ac, char **av)
+{
+       return cmd_inspect_dump_super(ac, av);
+}
+
 static const char inspect_cmd_group_info[] =
 "query various internal information";
 
@@ -647,6 +649,10 @@ const struct cmd_group inspect_cmd_group = {
                        0 },
                { "min-dev-size", cmd_inspect_min_dev_size,
                        cmd_inspect_min_dev_size_usage, NULL, 0 },
+               { "dump-tree", cmd_inspect_dump_tree_hook,
+                               cmd_inspect_dump_tree_usage, NULL, 0 },
+               { "dump-super", cmd_inspect_dump_super_hook,
+                               cmd_inspect_dump_super_usage, NULL, 0 },
                NULL_CMD_STRUCT
        }
 };