From 4647ab887a592516841f74fa47586771bbcad81b Mon Sep 17 00:00:00 2001 From: David Sterba Date: Thu, 11 Jun 2015 00:04:19 +0200 Subject: [PATCH] btrfs-progs: accept --help as option in the standalone utilities Signed-off-by: David Sterba --- btrfs-convert.c | 4 +++- btrfs-corrupt-block.c | 35 +++++++++++++++++++---------------- btrfs-debug-tree.c | 18 +++++++++++++----- btrfs-find-root.c | 19 +++++++++++++++---- btrfs-image.c | 24 +++++++++++++++--------- btrfstune.c | 11 +++++++++-- mkfs.c | 13 ++++++------- utils.h | 2 ++ 8 files changed, 82 insertions(+), 44 deletions(-) diff --git a/btrfs-convert.c b/btrfs-convert.c index a452c2ba..6cbba237 100644 --- a/btrfs-convert.c +++ b/btrfs-convert.c @@ -2869,6 +2869,7 @@ int main(int argc, char *argv[]) { "label", required_argument, NULL, 'l' }, { "copy-label", no_argument, NULL, 'L' }, { "nodesize", required_argument, NULL, 'N' }, + { "help", no_argument, NULL, GETOPT_VAL_HELP}, { NULL, 0, NULL, 0 } }; int c = getopt_long(argc, argv, "dinN:rl:LpO:", long_options, NULL); @@ -2941,9 +2942,10 @@ int main(int argc, char *argv[]) case GETOPT_VAL_NO_PROGRESS: progress = 0; break; + case GETOPT_VAL_HELP: default: print_usage(); - return 1; + return c != GETOPT_VAL_HELP; } } argc = argc - optind; diff --git a/btrfs-corrupt-block.c b/btrfs-corrupt-block.c index 6c335b95..6eb04a7f 100644 --- a/btrfs-corrupt-block.c +++ b/btrfs-corrupt-block.c @@ -21,6 +21,7 @@ #include #include #include + #include "kerncompat.h" #include "ctree.h" #include "volumes.h" @@ -85,7 +86,7 @@ struct extent_buffer *debug_corrupt_block(struct btrfs_root *root, u64 bytenr, return eb; } -static void print_usage(void) +static void print_usage(int ret) { fprintf(stderr, "usage: btrfs-corrupt-block [options] device\n"); fprintf(stderr, "\t-l Logical extent to be corrupted\n"); @@ -113,7 +114,7 @@ static void print_usage(void) fprintf(stderr, "\t-C Delete a csum for the specified bytenr. When " "used with -b it'll delete that many bytes, otherwise it's " "just sectorsize\n"); - exit(1); + exit(ret); } static void corrupt_keys(struct btrfs_trans_handle *trans, @@ -1061,6 +1062,7 @@ int main(int ac, char **av) { "delete", no_argument, NULL, 'd'}, { "root", no_argument, NULL, 'r'}, { "csum", required_argument, NULL, 'C'}, + { "help", no_argument, NULL, GETOPT_VAL_HELP}, { NULL, 0, NULL, 0 } }; @@ -1113,7 +1115,7 @@ int main(int ac, char **av) if (ret != 3) { fprintf(stderr, "error reading key " "%d\n", errno); - print_usage(); + print_usage(1); } break; case 'D': @@ -1131,14 +1133,15 @@ int main(int ac, char **av) case 'C': csum_bytenr = arg_strtou64(optarg); break; + case GETOPT_VAL_HELP: default: - print_usage(); + print_usage(c != GETOPT_VAL_HELP); } } set_argv0(av); ac = ac - optind; if (check_argc_min(ac, 1)) - print_usage(); + print_usage(1); dev = av[optind]; radix_tree_init(); @@ -1153,7 +1156,7 @@ int main(int ac, char **av) struct btrfs_trans_handle *trans; if (logical == (u64)-1) - print_usage(); + print_usage(1); trans = btrfs_start_transaction(root, 1); ret = corrupt_extent (trans, root, logical, 0); btrfs_commit_transaction(trans, root); @@ -1173,7 +1176,7 @@ int main(int ac, char **av) int del; if (logical == (u64)-1) - print_usage(); + print_usage(1); del = rand() % 3; path = btrfs_alloc_path(); if (!path) { @@ -1207,7 +1210,7 @@ int main(int ac, char **av) struct btrfs_trans_handle *trans; if (!strlen(field)) - print_usage(); + print_usage(1); trans = btrfs_start_transaction(root, 1); if (file_extent == (u64)-1) { @@ -1223,13 +1226,13 @@ int main(int ac, char **av) } if (metadata_block) { if (!strlen(field)) - print_usage(); + print_usage(1); ret = corrupt_metadata_block(root, metadata_block, field); goto out_close; } if (corrupt_di) { if (!key.objectid || !strlen(field)) - print_usage(); + print_usage(1); ret = corrupt_dir_item(root, &key, field); goto out_close; } @@ -1239,14 +1242,14 @@ int main(int ac, char **av) } if (corrupt_item) { if (!key.objectid) - print_usage(); + print_usage(1); ret = corrupt_btrfs_item(root, &key, field); } if (delete) { struct btrfs_root *target = root; if (!key.objectid) - print_usage(); + print_usage(1); if (root_objectid) { struct btrfs_key root_key; @@ -1258,7 +1261,7 @@ int main(int ac, char **av) if (IS_ERR(target)) { fprintf(stderr, "Couldn't find root %llu\n", (unsigned long long)root_objectid); - print_usage(); + print_usage(1); } } ret = delete_item(target, &key); @@ -1266,7 +1269,7 @@ int main(int ac, char **av) } if (key.objectid || key.offset || key.type) { if (!strlen(field)) - print_usage(); + print_usage(1); ret = corrupt_key(root, &key, field); goto out_close; } @@ -1275,10 +1278,10 @@ int main(int ac, char **av) * inode and we're screwed. */ if (file_extent != (u64)-1) - print_usage(); + print_usage(1); if (logical == (u64)-1) - print_usage(); + print_usage(1); if (bytes == 0) bytes = root->sectorsize; diff --git a/btrfs-debug-tree.c b/btrfs-debug-tree.c index 610624e4..7d8e876f 100644 --- a/btrfs-debug-tree.c +++ b/btrfs-debug-tree.c @@ -20,6 +20,8 @@ #include #include #include +#include + #include "kerncompat.h" #include "radix-tree.h" #include "ctree.h" @@ -28,7 +30,7 @@ #include "transaction.h" #include "utils.h" -static int print_usage(void) +static int print_usage(int ret) { fprintf(stderr, "usage: btrfs-debug-tree [-e] [-d] [-r] [-R] [-u]\n"); fprintf(stderr, " [-b block_num ] device\n"); @@ -43,7 +45,7 @@ static int print_usage(void) fprintf(stderr, "\t-t tree_id : print only the tree with the given id\n"); fprintf(stderr, "%s\n", PACKAGE_STRING); - exit(1); + exit(ret); } static void print_extents(struct btrfs_root *root, struct extent_buffer *eb) @@ -145,7 +147,12 @@ int main(int ac, char **av) while(1) { int c; - c = getopt(ac, av, "deb:rRut:"); + static const struct option long_options[] = { + { "help", no_argument, NULL, GETOPT_VAL_HELP}, + { NULL, 0, NULL, 0 } + }; + + c = getopt_long(ac, av, "deb:rRut:", long_options, NULL); if (c < 0) break; switch(c) { @@ -171,14 +178,15 @@ int main(int ac, char **av) case 't': tree_id = arg_strtou64(optarg); break; + case GETOPT_VAL_HELP: default: - print_usage(); + print_usage(c != GETOPT_VAL_HELP); } } set_argv0(av); ac = ac - optind; if (check_argc_exact(ac, 1)) - print_usage(); + print_usage(1); ret = check_arg_type(av[optind]); if (ret != BTRFS_ARG_BLKDEV && ret != BTRFS_ARG_REG) { diff --git a/btrfs-find-root.c b/btrfs-find-root.c index 9e56c81d..1cb3085d 100644 --- a/btrfs-find-root.c +++ b/btrfs-find-root.c @@ -22,6 +22,8 @@ #include #include #include +#include + #include "kerncompat.h" #include "ctree.h" #include "disk-io.h" @@ -142,15 +144,23 @@ int main(int argc, char **argv) struct btrfs_find_root_filter filter = {0}; struct cache_tree result; struct cache_extent *found; - int opt; int ret; /* Default to search root tree */ filter.objectid = BTRFS_ROOT_TREE_OBJECTID; filter.match_gen = (u64)-1; filter.match_level = (u8)-1; - while ((opt = getopt(argc, argv, "al:o:g:")) != -1) { - switch(opt) { + while (1) { + static const struct option long_options[] = { + { "help", no_argument, NULL, GETOPT_VAL_HELP}, + { NULL, 0, NULL, 0 } + }; + int c = getopt_long(argc, argv, "al:o:g:", long_options, NULL); + + if (c < 0) + break; + + switch (c) { case 'a': filter.search_all = 1; break; @@ -163,9 +173,10 @@ int main(int argc, char **argv) case 'l': filter.level = arg_strtou64(optarg); break; + case GETOPT_VAL_HELP: default: usage(); - exit(1); + exit(c != GETOPT_VAL_HELP); } } diff --git a/btrfs-image.c b/btrfs-image.c index 04ec4734..3684a05b 100644 --- a/btrfs-image.c +++ b/btrfs-image.c @@ -25,6 +25,8 @@ #include #include #include +#include + #include "kerncompat.h" #include "crc32c.h" #include "ctree.h" @@ -148,7 +150,6 @@ struct mdrestore_struct { struct btrfs_fs_info *info; }; -static void print_usage(void) __attribute__((noreturn)); static int search_for_chunk_blocks(struct mdrestore_struct *mdres, u64 search, u64 cluster_bytenr); static struct extent_buffer *alloc_dummy_eb(u64 bytenr, u32 size); @@ -2689,7 +2690,7 @@ out: return 0; } -static void print_usage(void) +static void print_usage(int ret) { fprintf(stderr, "usage: btrfs-image [options] source target\n"); fprintf(stderr, "\t-r \trestore metadump image\n"); @@ -2702,7 +2703,7 @@ static void print_usage(void) fprintf(stderr, "\n"); fprintf(stderr, "\tIn the dump mode, source is the btrfs device and target is the output file (use '-' for stdout).\n"); fprintf(stderr, "\tIn the restore mode, source is the dumped image and target is the btrfs device/file.\n"); - exit(1); + exit(ret); } int main(int argc, char *argv[]) @@ -2722,7 +2723,11 @@ int main(int argc, char *argv[]) FILE *out; while (1) { - int c = getopt(argc, argv, "rc:t:oswm"); + static const struct option long_options[] = { + { "help", no_argument, NULL, GETOPT_VAL_HELP}, + { NULL, 0, NULL, 0 } + }; + int c = getopt_long(argc, argv, "rc:t:oswm", long_options, NULL); if (c < 0) break; switch (c) { @@ -2732,12 +2737,12 @@ int main(int argc, char *argv[]) case 't': num_threads = arg_strtou64(optarg); if (num_threads > 32) - print_usage(); + print_usage(1); break; case 'c': compress_level = arg_strtou64(optarg); if (compress_level > 9) - print_usage(); + print_usage(1); break; case 'o': old_restore = 1; @@ -2752,15 +2757,16 @@ int main(int argc, char *argv[]) create = 0; multi_devices = 1; break; + case GETOPT_VAL_HELP: default: - print_usage(); + print_usage(c != GETOPT_VAL_HELP); } } argc = argc - optind; set_argv0(argv); if (check_argc_min(argc, 2)) - print_usage(); + print_usage(1); dev_cnt = argc - 1; @@ -2785,7 +2791,7 @@ int main(int argc, char *argv[]) } if (usage_error) - print_usage(); + print_usage(1); source = argv[optind]; target = argv[optind + 1]; diff --git a/btrfstune.c b/btrfstune.c index 7b178b19..bc8d53ca 100644 --- a/btrfstune.c +++ b/btrfstune.c @@ -24,6 +24,7 @@ #include #include #include +#include #include "kerncompat.h" #include "ctree.h" @@ -422,7 +423,12 @@ int main(int argc, char *argv[]) optind = 1; while(1) { - int c = getopt(argc, argv, "S:rxfuU:n"); + static const struct option long_options[] = { + { "help", no_argument, NULL, GETOPT_VAL_HELP}, + { NULL, 0, NULL, 0 } + }; + int c = getopt_long(argc, argv, "S:rxfuU:n", long_options, NULL); + if (c < 0) break; switch(c) { @@ -450,9 +456,10 @@ int main(int argc, char *argv[]) ctree_flags |= OPEN_CTREE_IGNORE_FSID_MISMATCH; random_fsid = 1; break; + case GETOPT_VAL_HELP: default: print_usage(); - return 1; + return c != GETOPT_VAL_HELP; } } diff --git a/mkfs.c b/mkfs.c index f622f2f6..14b75bce 100644 --- a/mkfs.c +++ b/mkfs.c @@ -290,9 +290,7 @@ static int create_data_reloc_tree(struct btrfs_trans_handle *trans, return 0; } - -static void print_usage(void) __attribute__((noreturn)); -static void print_usage(void) +static void print_usage(int ret) { fprintf(stderr, "usage: mkfs.btrfs [options] dev [ dev ... ]\n"); fprintf(stderr, "options:\n"); @@ -313,7 +311,7 @@ static void print_usage(void) fprintf(stderr, "\t-q|--quiet no messages except errors\n"); fprintf(stderr, "\t-V|--version print the mkfs.btrfs version and exit\n"); fprintf(stderr, "%s\n", PACKAGE_STRING); - exit(1); + exit(ret); } static void print_version(void) __attribute__((noreturn)); @@ -341,7 +339,6 @@ static u64 parse_profile(char *s) return 0; } else { fprintf(stderr, "Unknown profile %s\n", s); - print_usage(); } /* not reached */ return 0; @@ -1222,6 +1219,7 @@ int main(int ac, char **av) { "features", required_argument, NULL, 'O' }, { "uuid", required_argument, NULL, 'U' }, { "quiet", 0, NULL, 'q' }, + { "help", no_argument, NULL, GETOPT_VAL_HELP }, { NULL, 0, NULL, 0} }; @@ -1302,8 +1300,9 @@ int main(int ac, char **av) case 'q': verbose = 0; break; + case GETOPT_VAL_HELP: default: - print_usage(); + print_usage(c != GETOPT_VAL_HELP); } } sectorsize = max(sectorsize, (u32)sysconf(_SC_PAGESIZE)); @@ -1312,7 +1311,7 @@ int main(int ac, char **av) saved_optind = optind; dev_cnt = ac - optind; if (dev_cnt == 0) - print_usage(); + print_usage(1); if (source_dir_set && dev_cnt > 1) { fprintf(stderr, diff --git a/utils.h b/utils.h index e7714821..50d18533 100644 --- a/utils.h +++ b/utils.h @@ -69,6 +69,8 @@ #define GETOPT_VAL_GBYTES 262 #define GETOPT_VAL_TBYTES 263 +#define GETOPT_VAL_HELP 270 + int check_argc_exact(int nargs, int expected); int check_argc_min(int nargs, int expected); int check_argc_max(int nargs, int expected);