linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
To: <dsterba@suse.cz>
Cc: <linux-btrfs@vger.kernel.org>, Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Subject: [PATCH v2] btrfs-progs: use check_argc_* to check arg number for all tools
Date: Wed, 16 Jul 2014 11:44:19 +0800	[thread overview]
Message-ID: <1405482259-9912-1-git-send-email-guihc.fnst@cn.fujitsu.com> (raw)
In-Reply-To: <1404954405-16290-1-git-send-email-guihc.fnst@cn.fujitsu.com>

Since this patch:
	btrfs-progs: move the check_argc_* functions into utils.c

All tools including the independent tools(e.g. btrfs-image, btrfs-convert)
can share the convenience of the check_argc_* functions, so this patch
adopt the argc check functions globally.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
---
changelog
	v1->v2: make show-super, check and send cmds work correctly
---
 btrfs-calc-size.c     | 4 +++-
 btrfs-convert.c       | 3 ++-
 btrfs-corrupt-block.c | 3 ++-
 btrfs-crc.c           | 7 ++++---
 btrfs-debug-tree.c    | 3 ++-
 btrfs-find-root.c     | 4 +++-
 btrfs-fragments.c     | 8 +++++---
 btrfs-map-logical.c   | 3 ++-
 btrfs-select-super.c  | 3 ++-
 btrfs-show-super.c    | 4 +++-
 btrfs-zero-log.c      | 3 ++-
 btrfstune.c           | 3 ++-
 cmds-check.c          | 3 ++-
 cmds-restore.c        | 5 +++--
 cmds-send.c           | 4 +++-
 15 files changed, 40 insertions(+), 20 deletions(-)

diff --git a/btrfs-calc-size.c b/btrfs-calc-size.c
index 5eabdfc..501111c 100644
--- a/btrfs-calc-size.c
+++ b/btrfs-calc-size.c
@@ -452,7 +452,9 @@ int main(int argc, char **argv)
 		}
 	}
 
-	if (optind >= argc) {
+	set_argv0(argv);
+	argc = argc - optind;
+	if (check_argc_min(argc, 1)) {
 		usage();
 		exit(1);
 	}
diff --git a/btrfs-convert.c b/btrfs-convert.c
index d62d4f8..952e3e6 100644
--- a/btrfs-convert.c
+++ b/btrfs-convert.c
@@ -2723,7 +2723,8 @@ int main(int argc, char *argv[])
 		}
 	}
 	argc = argc - optind;
-	if (argc != 1) {
+	set_argv0(argv);
+	if (check_argc_exact(argc, 1)) {
 		print_usage();
 		return 1;
 	}
diff --git a/btrfs-corrupt-block.c b/btrfs-corrupt-block.c
index 6ecbe47..1a3ac35 100644
--- a/btrfs-corrupt-block.c
+++ b/btrfs-corrupt-block.c
@@ -865,8 +865,9 @@ int main(int ac, char **av)
 				print_usage();
 		}
 	}
+	set_argv0(av);
 	ac = ac - optind;
-	if (ac == 0)
+	if (check_argc_min(ac, 1))
 		print_usage();
 	dev = av[optind];
 
diff --git a/btrfs-crc.c b/btrfs-crc.c
index 1990534..723e0b7 100644
--- a/btrfs-crc.c
+++ b/btrfs-crc.c
@@ -20,6 +20,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include "crc32c.h"
+#include "utils.h"
 
 void usage(void)
 {
@@ -62,13 +63,13 @@ int main(int argc, char **argv)
 		}
 	}
 
+	set_argv0(argv);
 	str = argv[optind];
 
 	if (!loop) {
-		if (optind >= argc) {
-			fprintf(stderr, "not enough arguments\n");
+		if (check_argc_min(argc - optind, 1))
 			return 255;
-		}
+
 		printf("%12u - %s\n", crc32c(~1, str, strlen(str)), str);
 		return 0;
 	}
diff --git a/btrfs-debug-tree.c b/btrfs-debug-tree.c
index 36e1115..e46500d 100644
--- a/btrfs-debug-tree.c
+++ b/btrfs-debug-tree.c
@@ -174,8 +174,9 @@ int main(int ac, char **av)
 				print_usage();
 		}
 	}
+	set_argv0(av);
 	ac = ac - optind;
-	if (ac != 1)
+	if (check_argc_exact(ac, 1))
 		print_usage();
 
 	info = open_ctree_fs_info(av[optind], 0, 0, OPEN_CTREE_PARTIAL);
diff --git a/btrfs-find-root.c b/btrfs-find-root.c
index 7932308..6fe4b7b 100644
--- a/btrfs-find-root.c
+++ b/btrfs-find-root.c
@@ -305,7 +305,9 @@ int main(int argc, char **argv)
 		}
 	}
 
-	if (optind >= argc) {
+	set_argv0(argv);
+	argc = argc - optind;
+	if (check_argc_min(argc, 1)) {
 		usage();
 		exit(1);
 	}
diff --git a/btrfs-fragments.c b/btrfs-fragments.c
index 160429a..d03c2c3 100644
--- a/btrfs-fragments.c
+++ b/btrfs-fragments.c
@@ -425,13 +425,15 @@ int main(int argc, char **argv)
 		}
 	}
 
-	if (optind < argc) {
-		path = argv[optind++];
-	} else {
+	set_argv0(argv);
+	argc = argc - optind;
+	if (check_argc_min(argc, 1)) {
 		usage();
 		exit(1);
 	}
 
+	path = argv[optind++];
+
 	fd = open_file_or_dir(path, &dirstream);
 	if (fd < 0) {
 		fprintf(stderr, "ERROR: can't access '%s'\n", path);
diff --git a/btrfs-map-logical.c b/btrfs-map-logical.c
index e47a1ae..6b475fc 100644
--- a/btrfs-map-logical.c
+++ b/btrfs-map-logical.c
@@ -148,8 +148,9 @@ int main(int ac, char **av)
 				print_usage();
 		}
 	}
+	set_argv0(av);
 	ac = ac - optind;
-	if (ac == 0)
+	if (check_argc_min(ac, 1))
 		print_usage();
 	if (logical == 0)
 		print_usage();
diff --git a/btrfs-select-super.c b/btrfs-select-super.c
index d7cd187..6231d42 100644
--- a/btrfs-select-super.c
+++ b/btrfs-select-super.c
@@ -66,9 +66,10 @@ int main(int ac, char **av)
 				print_usage();
 		}
 	}
+	set_argv0(av);
 	ac = ac - optind;
 
-	if (ac != 1)
+	if (check_argc_exact(ac, 1))
 		print_usage();
 
 	if (bytenr == 0) {
diff --git a/btrfs-show-super.c b/btrfs-show-super.c
index ed0311f..0401344 100644
--- a/btrfs-show-super.c
+++ b/btrfs-show-super.c
@@ -95,7 +95,9 @@ int main(int argc, char **argv)
 		}
 	}
 
-	if (argc < optind + 1) {
+	set_argv0(argv);
+	argc = argc - optind;
+	if (check_argc_min(argc, 1)) {
 		print_usage();
 		exit(1);
 	}
diff --git a/btrfs-zero-log.c b/btrfs-zero-log.c
index ab7f418..88998e9 100644
--- a/btrfs-zero-log.c
+++ b/btrfs-zero-log.c
@@ -46,7 +46,8 @@ int main(int ac, char **av)
 	struct btrfs_trans_handle *trans;
 	int ret;
 
-	if (ac != 2)
+	set_argv0(av);
+	if (check_argc_exact(ac, 2))
 		print_usage();
 
 	radix_tree_init();
diff --git a/btrfstune.c b/btrfstune.c
index 3f2f0cd..b43c2f0 100644
--- a/btrfstune.c
+++ b/btrfstune.c
@@ -137,9 +137,10 @@ int main(int argc, char *argv[])
 		}
 	}
 
+	set_argv0(argv);
 	argc = argc - optind;
 	device = argv[optind];
-	if (argc != 1) {
+	if (check_argc_exact(argc, 1)) {
 		print_usage();
 		return 1;
 	}
diff --git a/cmds-check.c b/cmds-check.c
index 6d3388f..18bebb0 100644
--- a/cmds-check.c
+++ b/cmds-check.c
@@ -7018,9 +7018,10 @@ int cmd_check(int argc, char **argv)
 			check_data_csum = 1;
 		}
 	}
+	set_argv0(argv);
 	argc = argc - optind;
 
-	if (argc != 1)
+	if (check_argc_exact(argc, 1))
 		usage(cmd_check_usage);
 
 	radix_tree_init();
diff --git a/cmds-restore.c b/cmds-restore.c
index 3465f84..8e8afc5 100644
--- a/cmds-restore.c
+++ b/cmds-restore.c
@@ -1214,9 +1214,10 @@ int cmd_restore(int argc, char **argv)
 		}
 	}
 
-	if (!list_roots && optind + 1 >= argc)
+	set_argv0(argv);
+	if (!list_roots && check_argc_min(argc - optind, 2))
 		usage(cmd_restore_usage);
-	else if (list_roots && optind >= argc)
+	else if (list_roots && check_argc_min(argc - optind, 1))
 		usage(cmd_restore_usage);
 
 	if (fs_location && root_objectid) {
diff --git a/cmds-send.c b/cmds-send.c
index 9a73b32..8263f72 100644
--- a/cmds-send.c
+++ b/cmds-send.c
@@ -556,7 +556,9 @@ int cmd_send(int argc, char **argv)
 		}
 	}
 
-	if (optind == argc)
+	set_argv0(argv);
+	argc = argc - optind;
+	if (check_argc_min(argc, 1))
 		usage(cmd_send_usage);
 
 	if (g_total_data_size &&
-- 
1.8.1.4


  reply	other threads:[~2014-07-16  3:50 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-06-26  2:53 [PATCH 1/6] btrfs-progs: fix btrfs-image old_restore fsck failure Gui Hecheng
2014-06-26  2:53 ` [PATCH 2/6] btrfs-progs: deal with malloc failure in btrfs-image Gui Hecheng
2014-06-26  2:53 ` [PATCH 3/6] btrfs-progs: cleanup unnecessary free if malloc fails " Gui Hecheng
2014-06-26  2:53 ` [PATCH 4/6] btrfs-progs: cleanup possible silent failure " Gui Hecheng
2014-06-26  2:53 ` [PATCH 5/6] btrfs-progs: limit minimal num of args for btrfs-image Gui Hecheng
2014-06-27 12:35   ` David Sterba
2014-06-30  1:47     ` Gui Hecheng
2014-06-30  3:54     ` [PATCH 1/2] btrfs-progs: move the check_argc_* functions into utils.c Gui Hecheng
2014-06-30  3:54       ` [PATCH v2 2/2] btrfs-progs: limit minimal num of args for btrfs-image Gui Hecheng
2014-07-01 23:11       ` [PATCH 1/2] btrfs-progs: move the check_argc_* functions into utils.c David Sterba
2014-07-02  0:20         ` WorMzy Tykashi
2014-07-02  9:34           ` David Sterba
2014-07-02 10:50             ` WorMzy Tykashi
2014-07-02  1:13         ` Gui Hecheng
2014-07-10  1:06         ` [PATCH] btrfs-progs: use check_argc_* to check arg number for all tools Gui Hecheng
2014-07-16  3:44           ` Gui Hecheng [this message]
2014-07-16  3:58             ` [PATCH v2] " Gui Hecheng
2014-07-16  3:59           ` Gui Hecheng
2014-06-26  2:53 ` [PATCH 6/6] btrfs-progs: use BTRFS_SUPER_INFO_SIZE to replace raw 4096 in btrfs-image Gui Hecheng

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1405482259-9912-1-git-send-email-guihc.fnst@cn.fujitsu.com \
    --to=guihc.fnst@cn.fujitsu.com \
    --cc=dsterba@suse.cz \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).