btrfs-progs: accept --help as option in the standalone utilities

Signed-off-by: David Sterba <dsterba@suse.cz>
master
David Sterba 2015-06-11 00:04:19 +02:00
parent 250a58f34d
commit 4647ab887a
8 changed files with 82 additions and 44 deletions

View File

@ -2869,6 +2869,7 @@ int main(int argc, char *argv[])
{ "label", required_argument, NULL, 'l' }, { "label", required_argument, NULL, 'l' },
{ "copy-label", no_argument, NULL, 'L' }, { "copy-label", no_argument, NULL, 'L' },
{ "nodesize", required_argument, NULL, 'N' }, { "nodesize", required_argument, NULL, 'N' },
{ "help", no_argument, NULL, GETOPT_VAL_HELP},
{ NULL, 0, NULL, 0 } { NULL, 0, NULL, 0 }
}; };
int c = getopt_long(argc, argv, "dinN:rl:LpO:", long_options, NULL); 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: case GETOPT_VAL_NO_PROGRESS:
progress = 0; progress = 0;
break; break;
case GETOPT_VAL_HELP:
default: default:
print_usage(); print_usage();
return 1; return c != GETOPT_VAL_HELP;
} }
} }
argc = argc - optind; argc = argc - optind;

View File

@ -21,6 +21,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <getopt.h> #include <getopt.h>
#include "kerncompat.h" #include "kerncompat.h"
#include "ctree.h" #include "ctree.h"
#include "volumes.h" #include "volumes.h"
@ -85,7 +86,7 @@ struct extent_buffer *debug_corrupt_block(struct btrfs_root *root, u64 bytenr,
return eb; return eb;
} }
static void print_usage(void) static void print_usage(int ret)
{ {
fprintf(stderr, "usage: btrfs-corrupt-block [options] device\n"); fprintf(stderr, "usage: btrfs-corrupt-block [options] device\n");
fprintf(stderr, "\t-l Logical extent to be corrupted\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 " fprintf(stderr, "\t-C Delete a csum for the specified bytenr. When "
"used with -b it'll delete that many bytes, otherwise it's " "used with -b it'll delete that many bytes, otherwise it's "
"just sectorsize\n"); "just sectorsize\n");
exit(1); exit(ret);
} }
static void corrupt_keys(struct btrfs_trans_handle *trans, static void corrupt_keys(struct btrfs_trans_handle *trans,
@ -1061,6 +1062,7 @@ int main(int ac, char **av)
{ "delete", no_argument, NULL, 'd'}, { "delete", no_argument, NULL, 'd'},
{ "root", no_argument, NULL, 'r'}, { "root", no_argument, NULL, 'r'},
{ "csum", required_argument, NULL, 'C'}, { "csum", required_argument, NULL, 'C'},
{ "help", no_argument, NULL, GETOPT_VAL_HELP},
{ NULL, 0, NULL, 0 } { NULL, 0, NULL, 0 }
}; };
@ -1113,7 +1115,7 @@ int main(int ac, char **av)
if (ret != 3) { if (ret != 3) {
fprintf(stderr, "error reading key " fprintf(stderr, "error reading key "
"%d\n", errno); "%d\n", errno);
print_usage(); print_usage(1);
} }
break; break;
case 'D': case 'D':
@ -1131,14 +1133,15 @@ int main(int ac, char **av)
case 'C': case 'C':
csum_bytenr = arg_strtou64(optarg); csum_bytenr = arg_strtou64(optarg);
break; break;
case GETOPT_VAL_HELP:
default: default:
print_usage(); print_usage(c != GETOPT_VAL_HELP);
} }
} }
set_argv0(av); set_argv0(av);
ac = ac - optind; ac = ac - optind;
if (check_argc_min(ac, 1)) if (check_argc_min(ac, 1))
print_usage(); print_usage(1);
dev = av[optind]; dev = av[optind];
radix_tree_init(); radix_tree_init();
@ -1153,7 +1156,7 @@ int main(int ac, char **av)
struct btrfs_trans_handle *trans; struct btrfs_trans_handle *trans;
if (logical == (u64)-1) if (logical == (u64)-1)
print_usage(); print_usage(1);
trans = btrfs_start_transaction(root, 1); trans = btrfs_start_transaction(root, 1);
ret = corrupt_extent (trans, root, logical, 0); ret = corrupt_extent (trans, root, logical, 0);
btrfs_commit_transaction(trans, root); btrfs_commit_transaction(trans, root);
@ -1173,7 +1176,7 @@ int main(int ac, char **av)
int del; int del;
if (logical == (u64)-1) if (logical == (u64)-1)
print_usage(); print_usage(1);
del = rand() % 3; del = rand() % 3;
path = btrfs_alloc_path(); path = btrfs_alloc_path();
if (!path) { if (!path) {
@ -1207,7 +1210,7 @@ int main(int ac, char **av)
struct btrfs_trans_handle *trans; struct btrfs_trans_handle *trans;
if (!strlen(field)) if (!strlen(field))
print_usage(); print_usage(1);
trans = btrfs_start_transaction(root, 1); trans = btrfs_start_transaction(root, 1);
if (file_extent == (u64)-1) { if (file_extent == (u64)-1) {
@ -1223,13 +1226,13 @@ int main(int ac, char **av)
} }
if (metadata_block) { if (metadata_block) {
if (!strlen(field)) if (!strlen(field))
print_usage(); print_usage(1);
ret = corrupt_metadata_block(root, metadata_block, field); ret = corrupt_metadata_block(root, metadata_block, field);
goto out_close; goto out_close;
} }
if (corrupt_di) { if (corrupt_di) {
if (!key.objectid || !strlen(field)) if (!key.objectid || !strlen(field))
print_usage(); print_usage(1);
ret = corrupt_dir_item(root, &key, field); ret = corrupt_dir_item(root, &key, field);
goto out_close; goto out_close;
} }
@ -1239,14 +1242,14 @@ int main(int ac, char **av)
} }
if (corrupt_item) { if (corrupt_item) {
if (!key.objectid) if (!key.objectid)
print_usage(); print_usage(1);
ret = corrupt_btrfs_item(root, &key, field); ret = corrupt_btrfs_item(root, &key, field);
} }
if (delete) { if (delete) {
struct btrfs_root *target = root; struct btrfs_root *target = root;
if (!key.objectid) if (!key.objectid)
print_usage(); print_usage(1);
if (root_objectid) { if (root_objectid) {
struct btrfs_key root_key; struct btrfs_key root_key;
@ -1258,7 +1261,7 @@ int main(int ac, char **av)
if (IS_ERR(target)) { if (IS_ERR(target)) {
fprintf(stderr, "Couldn't find root %llu\n", fprintf(stderr, "Couldn't find root %llu\n",
(unsigned long long)root_objectid); (unsigned long long)root_objectid);
print_usage(); print_usage(1);
} }
} }
ret = delete_item(target, &key); ret = delete_item(target, &key);
@ -1266,7 +1269,7 @@ int main(int ac, char **av)
} }
if (key.objectid || key.offset || key.type) { if (key.objectid || key.offset || key.type) {
if (!strlen(field)) if (!strlen(field))
print_usage(); print_usage(1);
ret = corrupt_key(root, &key, field); ret = corrupt_key(root, &key, field);
goto out_close; goto out_close;
} }
@ -1275,10 +1278,10 @@ int main(int ac, char **av)
* inode and we're screwed. * inode and we're screwed.
*/ */
if (file_extent != (u64)-1) if (file_extent != (u64)-1)
print_usage(); print_usage(1);
if (logical == (u64)-1) if (logical == (u64)-1)
print_usage(); print_usage(1);
if (bytes == 0) if (bytes == 0)
bytes = root->sectorsize; bytes = root->sectorsize;

View File

@ -20,6 +20,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <uuid/uuid.h> #include <uuid/uuid.h>
#include <getopt.h>
#include "kerncompat.h" #include "kerncompat.h"
#include "radix-tree.h" #include "radix-tree.h"
#include "ctree.h" #include "ctree.h"
@ -28,7 +30,7 @@
#include "transaction.h" #include "transaction.h"
#include "utils.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, "usage: btrfs-debug-tree [-e] [-d] [-r] [-R] [-u]\n");
fprintf(stderr, " [-b block_num ] device\n"); fprintf(stderr, " [-b block_num ] device\n");
@ -43,7 +45,7 @@ static int print_usage(void)
fprintf(stderr, fprintf(stderr,
"\t-t tree_id : print only the tree with the given id\n"); "\t-t tree_id : print only the tree with the given id\n");
fprintf(stderr, "%s\n", PACKAGE_STRING); fprintf(stderr, "%s\n", PACKAGE_STRING);
exit(1); exit(ret);
} }
static void print_extents(struct btrfs_root *root, struct extent_buffer *eb) static void print_extents(struct btrfs_root *root, struct extent_buffer *eb)
@ -145,7 +147,12 @@ int main(int ac, char **av)
while(1) { while(1) {
int c; 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) if (c < 0)
break; break;
switch(c) { switch(c) {
@ -171,14 +178,15 @@ int main(int ac, char **av)
case 't': case 't':
tree_id = arg_strtou64(optarg); tree_id = arg_strtou64(optarg);
break; break;
case GETOPT_VAL_HELP:
default: default:
print_usage(); print_usage(c != GETOPT_VAL_HELP);
} }
} }
set_argv0(av); set_argv0(av);
ac = ac - optind; ac = ac - optind;
if (check_argc_exact(ac, 1)) if (check_argc_exact(ac, 1))
print_usage(); print_usage(1);
ret = check_arg_type(av[optind]); ret = check_arg_type(av[optind]);
if (ret != BTRFS_ARG_BLKDEV && ret != BTRFS_ARG_REG) { if (ret != BTRFS_ARG_BLKDEV && ret != BTRFS_ARG_REG) {

View File

@ -22,6 +22,8 @@
#include <fcntl.h> #include <fcntl.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <zlib.h> #include <zlib.h>
#include <getopt.h>
#include "kerncompat.h" #include "kerncompat.h"
#include "ctree.h" #include "ctree.h"
#include "disk-io.h" #include "disk-io.h"
@ -142,15 +144,23 @@ int main(int argc, char **argv)
struct btrfs_find_root_filter filter = {0}; struct btrfs_find_root_filter filter = {0};
struct cache_tree result; struct cache_tree result;
struct cache_extent *found; struct cache_extent *found;
int opt;
int ret; int ret;
/* Default to search root tree */ /* Default to search root tree */
filter.objectid = BTRFS_ROOT_TREE_OBJECTID; filter.objectid = BTRFS_ROOT_TREE_OBJECTID;
filter.match_gen = (u64)-1; filter.match_gen = (u64)-1;
filter.match_level = (u8)-1; filter.match_level = (u8)-1;
while ((opt = getopt(argc, argv, "al:o:g:")) != -1) { while (1) {
switch(opt) { 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': case 'a':
filter.search_all = 1; filter.search_all = 1;
break; break;
@ -163,9 +173,10 @@ int main(int argc, char **argv)
case 'l': case 'l':
filter.level = arg_strtou64(optarg); filter.level = arg_strtou64(optarg);
break; break;
case GETOPT_VAL_HELP:
default: default:
usage(); usage();
exit(1); exit(c != GETOPT_VAL_HELP);
} }
} }

View File

@ -25,6 +25,8 @@
#include <unistd.h> #include <unistd.h>
#include <dirent.h> #include <dirent.h>
#include <zlib.h> #include <zlib.h>
#include <getopt.h>
#include "kerncompat.h" #include "kerncompat.h"
#include "crc32c.h" #include "crc32c.h"
#include "ctree.h" #include "ctree.h"
@ -148,7 +150,6 @@ struct mdrestore_struct {
struct btrfs_fs_info *info; struct btrfs_fs_info *info;
}; };
static void print_usage(void) __attribute__((noreturn));
static int search_for_chunk_blocks(struct mdrestore_struct *mdres, static int search_for_chunk_blocks(struct mdrestore_struct *mdres,
u64 search, u64 cluster_bytenr); u64 search, u64 cluster_bytenr);
static struct extent_buffer *alloc_dummy_eb(u64 bytenr, u32 size); static struct extent_buffer *alloc_dummy_eb(u64 bytenr, u32 size);
@ -2689,7 +2690,7 @@ out:
return 0; return 0;
} }
static void print_usage(void) static void print_usage(int ret)
{ {
fprintf(stderr, "usage: btrfs-image [options] source target\n"); fprintf(stderr, "usage: btrfs-image [options] source target\n");
fprintf(stderr, "\t-r \trestore metadump image\n"); fprintf(stderr, "\t-r \trestore metadump image\n");
@ -2702,7 +2703,7 @@ static void print_usage(void)
fprintf(stderr, "\n"); 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 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"); 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[]) int main(int argc, char *argv[])
@ -2722,7 +2723,11 @@ int main(int argc, char *argv[])
FILE *out; FILE *out;
while (1) { 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) if (c < 0)
break; break;
switch (c) { switch (c) {
@ -2732,12 +2737,12 @@ int main(int argc, char *argv[])
case 't': case 't':
num_threads = arg_strtou64(optarg); num_threads = arg_strtou64(optarg);
if (num_threads > 32) if (num_threads > 32)
print_usage(); print_usage(1);
break; break;
case 'c': case 'c':
compress_level = arg_strtou64(optarg); compress_level = arg_strtou64(optarg);
if (compress_level > 9) if (compress_level > 9)
print_usage(); print_usage(1);
break; break;
case 'o': case 'o':
old_restore = 1; old_restore = 1;
@ -2752,15 +2757,16 @@ int main(int argc, char *argv[])
create = 0; create = 0;
multi_devices = 1; multi_devices = 1;
break; break;
case GETOPT_VAL_HELP:
default: default:
print_usage(); print_usage(c != GETOPT_VAL_HELP);
} }
} }
argc = argc - optind; argc = argc - optind;
set_argv0(argv); set_argv0(argv);
if (check_argc_min(argc, 2)) if (check_argc_min(argc, 2))
print_usage(); print_usage(1);
dev_cnt = argc - 1; dev_cnt = argc - 1;
@ -2785,7 +2791,7 @@ int main(int argc, char *argv[])
} }
if (usage_error) if (usage_error)
print_usage(); print_usage(1);
source = argv[optind]; source = argv[optind];
target = argv[optind + 1]; target = argv[optind + 1];

View File

@ -24,6 +24,7 @@
#include <unistd.h> #include <unistd.h>
#include <dirent.h> #include <dirent.h>
#include <uuid/uuid.h> #include <uuid/uuid.h>
#include <getopt.h>
#include "kerncompat.h" #include "kerncompat.h"
#include "ctree.h" #include "ctree.h"
@ -422,7 +423,12 @@ int main(int argc, char *argv[])
optind = 1; optind = 1;
while(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) if (c < 0)
break; break;
switch(c) { switch(c) {
@ -450,9 +456,10 @@ int main(int argc, char *argv[])
ctree_flags |= OPEN_CTREE_IGNORE_FSID_MISMATCH; ctree_flags |= OPEN_CTREE_IGNORE_FSID_MISMATCH;
random_fsid = 1; random_fsid = 1;
break; break;
case GETOPT_VAL_HELP:
default: default:
print_usage(); print_usage();
return 1; return c != GETOPT_VAL_HELP;
} }
} }

13
mkfs.c
View File

@ -290,9 +290,7 @@ static int create_data_reloc_tree(struct btrfs_trans_handle *trans,
return 0; return 0;
} }
static void print_usage(int ret)
static void print_usage(void) __attribute__((noreturn));
static void print_usage(void)
{ {
fprintf(stderr, "usage: mkfs.btrfs [options] dev [ dev ... ]\n"); fprintf(stderr, "usage: mkfs.btrfs [options] dev [ dev ... ]\n");
fprintf(stderr, "options:\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-q|--quiet no messages except errors\n");
fprintf(stderr, "\t-V|--version print the mkfs.btrfs version and exit\n"); fprintf(stderr, "\t-V|--version print the mkfs.btrfs version and exit\n");
fprintf(stderr, "%s\n", PACKAGE_STRING); fprintf(stderr, "%s\n", PACKAGE_STRING);
exit(1); exit(ret);
} }
static void print_version(void) __attribute__((noreturn)); static void print_version(void) __attribute__((noreturn));
@ -341,7 +339,6 @@ static u64 parse_profile(char *s)
return 0; return 0;
} else { } else {
fprintf(stderr, "Unknown profile %s\n", s); fprintf(stderr, "Unknown profile %s\n", s);
print_usage();
} }
/* not reached */ /* not reached */
return 0; return 0;
@ -1222,6 +1219,7 @@ int main(int ac, char **av)
{ "features", required_argument, NULL, 'O' }, { "features", required_argument, NULL, 'O' },
{ "uuid", required_argument, NULL, 'U' }, { "uuid", required_argument, NULL, 'U' },
{ "quiet", 0, NULL, 'q' }, { "quiet", 0, NULL, 'q' },
{ "help", no_argument, NULL, GETOPT_VAL_HELP },
{ NULL, 0, NULL, 0} { NULL, 0, NULL, 0}
}; };
@ -1302,8 +1300,9 @@ int main(int ac, char **av)
case 'q': case 'q':
verbose = 0; verbose = 0;
break; break;
case GETOPT_VAL_HELP:
default: default:
print_usage(); print_usage(c != GETOPT_VAL_HELP);
} }
} }
sectorsize = max(sectorsize, (u32)sysconf(_SC_PAGESIZE)); sectorsize = max(sectorsize, (u32)sysconf(_SC_PAGESIZE));
@ -1312,7 +1311,7 @@ int main(int ac, char **av)
saved_optind = optind; saved_optind = optind;
dev_cnt = ac - optind; dev_cnt = ac - optind;
if (dev_cnt == 0) if (dev_cnt == 0)
print_usage(); print_usage(1);
if (source_dir_set && dev_cnt > 1) { if (source_dir_set && dev_cnt > 1) {
fprintf(stderr, fprintf(stderr,

View File

@ -69,6 +69,8 @@
#define GETOPT_VAL_GBYTES 262 #define GETOPT_VAL_GBYTES 262
#define GETOPT_VAL_TBYTES 263 #define GETOPT_VAL_TBYTES 263
#define GETOPT_VAL_HELP 270
int check_argc_exact(int nargs, int expected); int check_argc_exact(int nargs, int expected);
int check_argc_min(int nargs, int expected); int check_argc_min(int nargs, int expected);
int check_argc_max(int nargs, int expected); int check_argc_max(int nargs, int expected);