btrfs-progs: add getopt stubs where needed
[platform/upstream/btrfs-progs.git] / cmds-subvolume.c
index d5ec886..02e1dec 100644 (file)
 #include <sys/ioctl.h>
 #include <errno.h>
 #include <sys/stat.h>
+#include <sys/vfs.h>
 #include <libgen.h>
 #include <limits.h>
 #include <getopt.h>
 #include <uuid/uuid.h>
+#include <linux/magic.h>
 
 #include "kerncompat.h"
 #include "ioctl.h"
@@ -232,14 +234,21 @@ out:
 int test_issubvolume(const char *path)
 {
        struct stat     st;
+       struct statfs stfs;
        int             res;
 
        res = stat(path, &st);
        if (res < 0)
                return -errno;
 
-       return (st.st_ino == BTRFS_FIRST_FREE_OBJECTID)
-               && S_ISDIR(st.st_mode);
+       if (st.st_ino != BTRFS_FIRST_FREE_OBJECTID || !S_ISDIR(st.st_mode))
+               return 0;
+
+       res = statfs(path, &stfs);
+       if (res < 0)
+               return -errno;
+
+       return (int)stfs.f_type == BTRFS_SUPER_MAGIC;
 }
 
 static int wait_for_commit(int fd)
@@ -779,7 +788,9 @@ static int cmd_subvol_get_default(int argc, char **argv)
        u64 default_id;
        DIR *dirstream = NULL;
 
-       if (check_argc_exact(argc, 2))
+       clean_args_no_options(argc, argv, cmd_subvol_get_default_usage);
+
+       if (check_argc_exact(argc - optind, 2))
                usage(cmd_subvol_get_default_usage);
 
        subvol = argv[1];
@@ -841,11 +852,13 @@ static int cmd_subvol_set_default(int argc, char **argv)
        char    *subvolid;
        DIR     *dirstream = NULL;
 
-       if (check_argc_exact(argc, 3))
+       clean_args_no_options(argc, argv, cmd_subvol_set_default_usage);
+
+       if (check_argc_exact(argc - optind, 2))
                usage(cmd_subvol_set_default_usage);
 
-       subvolid = argv[1];
-       path = argv[2];
+       subvolid = argv[optind];
+       path = argv[optind + 1];
 
        objectid = arg_strtou64(subvolid);
 
@@ -878,11 +891,13 @@ static int cmd_subvol_find_new(int argc, char **argv)
        u64 last_gen;
        DIR *dirstream = NULL;
 
-       if (check_argc_exact(argc, 3))
+       clean_args_no_options(argc, argv, cmd_subvol_find_new_usage);
+
+       if (check_argc_exact(argc - optind, 2))
                usage(cmd_subvol_find_new_usage);
 
-       subvol = argv[1];
-       last_gen = arg_strtou64(argv[2]);
+       subvol = argv[optind];
+       last_gen = arg_strtou64(argv[optind + 1]);
 
        ret = test_issubvolume(subvol);
        if (ret < 0) {
@@ -930,20 +945,7 @@ static int cmd_subvol_show(int argc, char **argv)
        int ret = 1;
        DIR *dirstream1 = NULL, *dirstream2 = NULL;
 
-       while (1) {
-               static const struct option long_options[] = {
-                       {NULL, 0, NULL, 0}
-               };
-               int c = getopt_long(argc, argv, "", long_options, NULL);
-
-               if (c < 0)
-                       break;
-
-               switch (c) {
-               default:
-                       usage(cmd_subvol_show_usage);
-               }
-       }
+       clean_args_no_options(argc, argv, cmd_subvol_show_usage);
 
        if (check_argc_exact(argc - optind, 1))
                usage(cmd_subvol_show_usage);