]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - cmd_format.c
convert main() from C to Rust
[bcachefs-tools-debian] / cmd_format.c
index 57aa496cc305e5a2eb4caaa6b45899ea2c700e62..45c44e3229d0f5ff50cf5b21d3bd6d1042267f1c 100644 (file)
@@ -24,6 +24,7 @@
 #include "libbcachefs.h"
 #include "crypto.h"
 #include "libbcachefs/darray.h"
+#include "libbcachefs/errcode.h"
 #include "libbcachefs/opts.h"
 #include "libbcachefs/super-io.h"
 #include "libbcachefs/util.h"
@@ -116,8 +117,9 @@ int cmd_format(int argc, char *argv[])
        DARRAY(struct dev_opts) devices = { 0 };
        DARRAY(char *) device_paths = { 0 };
        struct format_opts opts = format_opts_default();
-       struct dev_opts dev_opts = dev_opts_default(), *dev;
+       struct dev_opts dev_opts = dev_opts_default();
        bool force = false, no_passphrase = false, quiet = false, initialize = true, verbose = false;
+       bool unconsumed_dev_option = false;
        unsigned v;
        int opt;
 
@@ -161,6 +163,7 @@ int cmd_format(int argc, char *argv[])
                case O_fs_size:
                        if (bch2_strtoull_h(optarg, &dev_opts.size))
                                die("invalid filesystem size");
+                       unconsumed_dev_option = true;
                        break;
                case O_superblock_size:
                        if (bch2_strtouint_h(optarg, &opts.superblock_size))
@@ -171,23 +174,28 @@ int cmd_format(int argc, char *argv[])
                case O_bucket_size:
                        if (bch2_strtoull_h(optarg, &dev_opts.bucket_size))
                                die("bad bucket_size %s", optarg);
+                       unconsumed_dev_option = true;
                        break;
                case O_label:
                case 'l':
                        dev_opts.label = optarg;
+                       unconsumed_dev_option = true;
                        break;
                case O_discard:
                        dev_opts.discard = true;
+                       unconsumed_dev_option = true;
                        break;
                case O_data_allowed:
                        dev_opts.data_allowed =
                                read_flag_list_or_die(optarg,
                                        bch2_data_types, "data type");
+                       unconsumed_dev_option = true;
                        break;
                case O_durability:
                        if (kstrtouint(optarg, 10, &dev_opts.durability) ||
                            dev_opts.durability > BCH_REPLICAS_MAX)
                                die("invalid durability");
+                       unconsumed_dev_option = true;
                        break;
                case O_version:
                        if (kstrtouint(optarg, 10, &opts.version))
@@ -201,6 +209,7 @@ int cmd_format(int argc, char *argv[])
                        dev_opts.path = optarg;
                        darray_push(&devices, dev_opts);
                        dev_opts.size = 0;
+                       unconsumed_dev_option = false;
                        break;
                case O_quiet:
                case 'q':
@@ -218,6 +227,12 @@ int cmd_format(int argc, char *argv[])
                        break;
                }
 
+       if (unconsumed_dev_option)
+               die("Options for devices apply to subsequent devices; got a device option with no device");
+
+       if (opts.version != bcachefs_metadata_version_current)
+               initialize = false;
+
        if (!devices.nr)
                die("Please supply a device");
 
@@ -226,8 +241,11 @@ int cmd_format(int argc, char *argv[])
                initialize = false;
        }
 
-       darray_for_each(devices, dev)
-               dev->fd = open_for_format(dev->path, force);
+       darray_for_each(devices, dev) {
+               int ret = open_for_format(dev, force);
+               if (ret)
+                       die("Error opening %s: %s", dev_opts.path, strerror(-ret));
+       }
 
        struct bch_sb *sb =
                bch2_format(fs_opt_strs,
@@ -239,9 +257,9 @@ int cmd_format(int argc, char *argv[])
        if (!quiet) {
                struct printbuf buf = PRINTBUF;
 
-               buf.units = PRINTBUF_UNITS_HUMAN_READABLE;
+               buf.human_readable_units = true;
 
-               bch2_sb_to_text(&buf, sb, false, 1 << BCH_SB_FIELD_members);
+               bch2_sb_to_text(&buf, sb, false, 1 << BCH_SB_FIELD_members_v2);
                printf("%s", buf.buf);
 
                printbuf_exit(&buf);
@@ -270,7 +288,7 @@ int cmd_format(int argc, char *argv[])
                                                mount_opts);
                if (IS_ERR(c))
                        die("error opening %s: %s", device_paths.data[0],
-                           strerror(-PTR_ERR(c)));
+                           bch2_err_str(PTR_ERR(c)));
 
                bch2_fs_stop(c);
        }
@@ -287,6 +305,7 @@ static void show_super_usage(void)
             "\n"
             "Options:\n"
             "  -f, --fields=(fields)       list of sections to print\n"
+            "      --field-only=fiel)      print superblock section only, no header\n"
             "  -l, --layout                print superblock layout\n"
             "  -h, --help                  display this help and exit\n"
             "Report bugs to <linux-bcachefs@vger.kernel.org>");
@@ -297,12 +316,15 @@ int cmd_show_super(int argc, char *argv[])
 {
        static const struct option longopts[] = {
                { "fields",                     1, NULL, 'f' },
+               { "field-only",                 1, NULL, 'F' },
                { "layout",                     0, NULL, 'l' },
                { "help",                       0, NULL, 'h' },
                { NULL }
        };
-       unsigned fields = 1 << BCH_SB_FIELD_members;
+       unsigned fields = 0;
+       int field_only = -1;
        bool print_layout = false;
+       bool print_default_fields = true;
        int opt;
 
        while ((opt = getopt_long(argc, argv, "f:lh", longopts, NULL)) != -1)
@@ -312,6 +334,12 @@ int cmd_show_super(int argc, char *argv[])
                                ? ~0
                                : read_flag_list_or_die(optarg,
                                        bch2_sb_fields, "superblock field");
+                       print_default_fields = false;
+                       break;
+               case 'F':
+                       field_only = read_string_list_or_die(optarg,
+                                       bch2_sb_fields, "superblock field");
+                       print_default_fields = false;
                        break;
                case 'l':
                        print_layout = true;
@@ -336,13 +364,27 @@ int cmd_show_super(int argc, char *argv[])
        struct bch_sb_handle sb;
        int ret = bch2_read_super(dev, &opts, &sb);
        if (ret)
-               die("Error opening %s: %s", dev, strerror(-ret));
+               die("Error opening %s: %s", dev, bch2_err_str(ret));
+
+       if (print_default_fields) {
+               fields |= bch2_sb_field_get(sb.sb, members_v2)
+                       ? 1 << BCH_SB_FIELD_members_v2
+                       : 1 << BCH_SB_FIELD_members_v1;
+               fields |= 1 << BCH_SB_FIELD_errors;
+       }
 
        struct printbuf buf = PRINTBUF;
 
-       buf.units = PRINTBUF_UNITS_HUMAN_READABLE;
+       buf.human_readable_units = true;
 
-       bch2_sb_to_text(&buf, sb.sb, print_layout, fields);
+       if (field_only >= 0) {
+               struct bch_sb_field *f = bch2_sb_field_get_id(sb.sb, field_only);
+
+               if (f)
+                       __bch2_sb_field_to_text(&buf, sb.sb, f);
+       } else {
+               bch2_sb_to_text(&buf, sb.sb, print_layout, fields);
+       }
        printf("%s", buf.buf);
 
        bch2_free_super(&sb);