* [PATCH 1/6] btrfs-progs: fix btrfs-image old_restore fsck failure
@ 2014-06-26 2:53 Gui Hecheng
2014-06-26 2:53 ` [PATCH 2/6] btrfs-progs: deal with malloc failure in btrfs-image Gui Hecheng
` (4 more replies)
0 siblings, 5 replies; 19+ messages in thread
From: Gui Hecheng @ 2014-06-26 2:53 UTC (permalink / raw)
To: linux-btrfs; +Cc: Gui Hecheng
Steps to reproduce:
# mkfs.btrfs -f <dev1>
# btrfs-image <dev1> <image_file>
# btrfs-image -r -o <image_file> <dev2>
# btrfs check <dev2>
btrfs check output:
: read block failed check_tree_block
: Couldn't read tree root
: Couldn't open file system
The btrfs-image should not mess with the chunk tree under the old_restore way.
The new restore way was introduced by:
commit d6f7e3da0dae7b60cb7565f8a47c3b9045c52d1d
Btrfs-progs: make btrfs-image restore with a valid chunk tree V2
...
And the following commit enhanced the new restore on the valid chunk tree
building stuff:
commit ef2a8889ef813ba77061f6a92f4954d047a78932
Btrfs-progs: make image restore with the original device offsets
...
But the second commit should not effect the old_restore way since the
old_restore way doesn't try to build a valid chunk tree at all.
---
btrfs-image.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/btrfs-image.c b/btrfs-image.c
index 02ae5d6..c90bca8 100644
--- a/btrfs-image.c
+++ b/btrfs-image.c
@@ -1668,7 +1668,7 @@ static void *restore_worker(void *data)
if (!mdres->fixup_offset) {
while (size) {
u64 chunk_size = size;
- if (!mdres->multi_devices)
+ if (!mdres->multi_devices && !mdres->old_restore)
bytenr = logical_to_physical(mdres,
async->start + offset,
&chunk_size);
@@ -2282,7 +2282,7 @@ static int __restore_metadump(const char *input, FILE *out, int old_restore,
goto failed_cluster;
}
- if (!multi_devices) {
+ if (!multi_devices && !old_restore) {
ret = build_chunk_tree(&mdrestore, cluster);
if (ret)
goto out;
--
1.8.1.4
^ permalink raw reply related [flat|nested] 19+ messages in thread* [PATCH 2/6] btrfs-progs: deal with malloc failure in btrfs-image 2014-06-26 2:53 [PATCH 1/6] btrfs-progs: fix btrfs-image old_restore fsck failure Gui Hecheng @ 2014-06-26 2:53 ` Gui Hecheng 2014-06-26 2:53 ` [PATCH 3/6] btrfs-progs: cleanup unnecessary free if malloc fails " Gui Hecheng ` (3 subsequent siblings) 4 siblings, 0 replies; 19+ messages in thread From: Gui Hecheng @ 2014-06-26 2:53 UTC (permalink / raw) To: linux-btrfs; +Cc: Gui Hecheng Handle the malloc failure for dump_worker in the same way as the restore worker. Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com> --- btrfs-image.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/btrfs-image.c b/btrfs-image.c index c90bca8..7131001 100644 --- a/btrfs-image.c +++ b/btrfs-image.c @@ -107,6 +107,8 @@ struct metadump_struct { int done; int data; int sanitize_names; + + int error; }; struct name { @@ -602,6 +604,14 @@ static void *dump_worker(void *data) async->bufsize = compressBound(async->size); async->buffer = malloc(async->bufsize); + if (!async->buffer) { + fprintf(stderr, "Error allocing buffer\n"); + pthread_mutex_lock(&md->mutex); + if (!md->error) + md->error = -ENOMEM; + pthread_mutex_unlock(&md->mutex); + pthread_exit(NULL); + } ret = compress2(async->buffer, (unsigned long *)&async->bufsize, @@ -736,7 +746,7 @@ static int write_buffers(struct metadump_struct *md, u64 *next) goto out; /* wait until all buffers are compressed */ - while (md->num_items > md->num_ready) { + while (!err && md->num_items > md->num_ready) { struct timespec ts = { .tv_sec = 0, .tv_nsec = 10000000, @@ -744,6 +754,13 @@ static int write_buffers(struct metadump_struct *md, u64 *next) pthread_mutex_unlock(&md->mutex); nanosleep(&ts, NULL); pthread_mutex_lock(&md->mutex); + err = md->error; + } + + if (err) { + fprintf(stderr, "One of the threads errored out %s\n", + strerror(err)); + goto out; } /* setup and write index block */ -- 1.8.1.4 ^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH 3/6] btrfs-progs: cleanup unnecessary free if malloc fails in btrfs-image 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 ` Gui Hecheng 2014-06-26 2:53 ` [PATCH 4/6] btrfs-progs: cleanup possible silent failure " Gui Hecheng ` (2 subsequent siblings) 4 siblings, 0 replies; 19+ messages in thread From: Gui Hecheng @ 2014-06-26 2:53 UTC (permalink / raw) To: linux-btrfs; +Cc: Gui Hecheng Don't bother free the buffer if the malloc failed. Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com> --- btrfs-image.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/btrfs-image.c b/btrfs-image.c index 7131001..2d482c3 100644 --- a/btrfs-image.c +++ b/btrfs-image.c @@ -1631,7 +1631,7 @@ static void *restore_worker(void *data) if (!mdres->error) mdres->error = -ENOMEM; pthread_mutex_unlock(&mdres->mutex); - goto out; + pthread_exit(NULL); } while (1) { -- 1.8.1.4 ^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH 4/6] btrfs-progs: cleanup possible silent failure in btrfs-image 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 ` Gui Hecheng 2014-06-26 2:53 ` [PATCH 5/6] btrfs-progs: limit minimal num of args for btrfs-image 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 4 siblings, 0 replies; 19+ messages in thread From: Gui Hecheng @ 2014-06-26 2:53 UTC (permalink / raw) To: linux-btrfs; +Cc: Gui Hecheng If the malloc above fails, the btrfs-image will exit directly without any error messages. Now just return the ENOMEM errno and let the caller prompt the error message. Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com> --- btrfs-image.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/btrfs-image.c b/btrfs-image.c index 2d482c3..e0aabfd 100644 --- a/btrfs-image.c +++ b/btrfs-image.c @@ -2421,7 +2421,8 @@ static int update_disk_super_on_device(struct btrfs_fs_info *info, buf = malloc(BTRFS_SUPER_INFO_SIZE); if (!buf) { ret = -ENOMEM; - exit(1); + close(fp); + return ret; } memcpy(buf, info->super_copy, BTRFS_SUPER_INFO_SIZE); -- 1.8.1.4 ^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH 5/6] btrfs-progs: limit minimal num of args for btrfs-image 2014-06-26 2:53 [PATCH 1/6] btrfs-progs: fix btrfs-image old_restore fsck failure Gui Hecheng ` (2 preceding siblings ...) 2014-06-26 2:53 ` [PATCH 4/6] btrfs-progs: cleanup possible silent failure " Gui Hecheng @ 2014-06-26 2:53 ` Gui Hecheng 2014-06-27 12:35 ` David Sterba 2014-06-26 2:53 ` [PATCH 6/6] btrfs-progs: use BTRFS_SUPER_INFO_SIZE to replace raw 4096 in btrfs-image Gui Hecheng 4 siblings, 1 reply; 19+ messages in thread From: Gui Hecheng @ 2014-06-26 2:53 UTC (permalink / raw) To: linux-btrfs; +Cc: Gui Hecheng The btrfs-image requires at least 2 args to run, one for the source dev/file, the other for the target dev/file. Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com> --- btrfs-image.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/btrfs-image.c b/btrfs-image.c index e0aabfd..b13f236 100644 --- a/btrfs-image.c +++ b/btrfs-image.c @@ -2521,6 +2521,9 @@ int main(int argc, char *argv[]) } argc = argc - optind; + if (argc < 2) + print_usage(); + dev_cnt = argc - 1; if (create) { -- 1.8.1.4 ^ permalink raw reply related [flat|nested] 19+ messages in thread
* Re: [PATCH 5/6] btrfs-progs: limit minimal num of args for btrfs-image 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 0 siblings, 2 replies; 19+ messages in thread From: David Sterba @ 2014-06-27 12:35 UTC (permalink / raw) To: Gui Hecheng; +Cc: linux-btrfs On Thu, Jun 26, 2014 at 10:53:05AM +0800, Gui Hecheng wrote: > @@ -2521,6 +2521,9 @@ int main(int argc, char *argv[]) > } > > argc = argc - optind; > + if (argc < 2) Please use the check_argc_min helper instead. Thanks. > + print_usage(); > + > dev_cnt = argc - 1; > > if (create) { ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [PATCH 5/6] btrfs-progs: limit minimal num of args for btrfs-image 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 1 sibling, 0 replies; 19+ messages in thread From: Gui Hecheng @ 2014-06-30 1:47 UTC (permalink / raw) To: dsterba; +Cc: linux-btrfs On Fri, 2014-06-27 at 14:35 +0200, David Sterba wrote: > On Thu, Jun 26, 2014 at 10:53:05AM +0800, Gui Hecheng wrote: > > @@ -2521,6 +2521,9 @@ int main(int argc, char *argv[]) > > } > > > > argc = argc - optind; > > + if (argc < 2) > > Please use the check_argc_min helper instead. Thanks. Yes, actually I just wanted to prevent including "btrfs.c" in btrfs-image.c. I will move the check_argc_* functions into utils.c and resend. Thanks, David. -Gui > > > + print_usage(); > > + > > dev_cnt = argc - 1; > > > > if (create) { ^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH 1/2] btrfs-progs: move the check_argc_* functions into utils.c 2014-06-27 12:35 ` David Sterba 2014-06-30 1:47 ` Gui Hecheng @ 2014-06-30 3:54 ` 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 1 sibling, 2 replies; 19+ messages in thread From: Gui Hecheng @ 2014-06-30 3:54 UTC (permalink / raw) To: dsterba; +Cc: linux-btrfs, Gui Hecheng To let the independent tools(e.g. btrfs-image, btrfs-convert, etc.) share the convenience of check_argc_* functions, just move it into utils.c. Also add a new function "set_argv0" to set the correct tool name: *btrfs-image*: too few arguments The original btrfs* tools work as before. Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com> --- btrfs.c | 41 +---------------------------------------- utils.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ utils.h | 7 +++++++ 3 files changed, 55 insertions(+), 40 deletions(-) diff --git a/btrfs.c b/btrfs.c index 25257b6..685455f 100644 --- a/btrfs.c +++ b/btrfs.c @@ -22,6 +22,7 @@ #include "crc32c.h" #include "commands.h" #include "version.h" +#include "utils.h" static const char * const btrfs_cmd_group_usage[] = { "btrfs [--help] [--version] <group> [<group>...] <command> [<args>]", @@ -31,8 +32,6 @@ static const char * const btrfs_cmd_group_usage[] = { static const char btrfs_cmd_group_info[] = "Use --help as an argument for information on a specific group or command."; -static char argv0_buf[ARGV0_BUF_SIZE] = "btrfs"; - static inline const char *skip_prefix(const char *str, const char *prefix) { size_t len = strlen(prefix); @@ -125,14 +124,6 @@ static void handle_help_options_next_level(const struct cmd_struct *cmd, } } -static void fixup_argv0(char **argv, const char *token) -{ - int len = strlen(argv0_buf); - - snprintf(argv0_buf + len, sizeof(argv0_buf) - len, " %s", token); - argv[0] = argv0_buf; -} - int handle_command_group(const struct cmd_group *grp, int argc, char **argv) @@ -154,36 +145,6 @@ int handle_command_group(const struct cmd_group *grp, int argc, return cmd->fn(argc, argv); } -int check_argc_exact(int nargs, int expected) -{ - if (nargs < expected) - fprintf(stderr, "%s: too few arguments\n", argv0_buf); - if (nargs > expected) - fprintf(stderr, "%s: too many arguments\n", argv0_buf); - - return nargs != expected; -} - -int check_argc_min(int nargs, int expected) -{ - if (nargs < expected) { - fprintf(stderr, "%s: too few arguments\n", argv0_buf); - return 1; - } - - return 0; -} - -int check_argc_max(int nargs, int expected) -{ - if (nargs > expected) { - fprintf(stderr, "%s: too many arguments\n", argv0_buf); - return 1; - } - - return 0; -} - static const struct cmd_group btrfs_cmd_group; static const char * const cmd_help_usage[] = { diff --git a/utils.c b/utils.c index 8ce3be9..fd59f58 100644 --- a/utils.c +++ b/utils.c @@ -50,11 +50,58 @@ #include "volumes.h" #include "ioctl.h" #include "btrfs-list.h" +#include "commands.h" #ifndef BLKDISCARD #define BLKDISCARD _IO(0x12,119) #endif +static char argv0_buf[ARGV0_BUF_SIZE] = "btrfs"; + +void fixup_argv0(char **argv, const char *token) +{ + int len = strlen(argv0_buf); + + snprintf(argv0_buf + len, sizeof(argv0_buf) - len, " %s", token); + argv[0] = argv0_buf; +} + +void set_argv0(char **argv) +{ + sprintf(argv0_buf, "%s", argv[0]); +} + +int check_argc_exact(int nargs, int expected) +{ + if (nargs < expected) + fprintf(stderr, "%s: too few arguments\n", argv0_buf); + if (nargs > expected) + fprintf(stderr, "%s: too many arguments\n", argv0_buf); + + return nargs != expected; +} + +int check_argc_min(int nargs, int expected) +{ + if (nargs < expected) { + fprintf(stderr, "%s: too few arguments\n", argv0_buf); + return 1; + } + + return 0; +} + +int check_argc_max(int nargs, int expected) +{ + if (nargs > expected) { + fprintf(stderr, "%s: too many arguments\n", argv0_buf); + return 1; + } + + return 0; +} + + /* * Discard the given range in one go */ diff --git a/utils.h b/utils.h index ad772c2..f05477a 100644 --- a/utils.h +++ b/utils.h @@ -47,6 +47,13 @@ #define UNITS_DECIMAL (3) #define UNITS_HUMAN UNITS_BINARY +int check_argc_exact(int nargs, int expected); +int check_argc_min(int nargs, int expected); +int check_argc_max(int nargs, int expected); + +void fixup_argv0(char **argv, const char *token); +void set_argv0(char **argv); + int make_btrfs(int fd, const char *device, const char *label, char *fs_uuid, u64 blocks[6], u64 num_bytes, u32 nodesize, u32 leafsize, u32 sectorsize, u32 stripesize, u64 features); -- 1.8.1.4 ^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH v2 2/2] btrfs-progs: limit minimal num of args for btrfs-image 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 ` Gui Hecheng 2014-07-01 23:11 ` [PATCH 1/2] btrfs-progs: move the check_argc_* functions into utils.c David Sterba 1 sibling, 0 replies; 19+ messages in thread From: Gui Hecheng @ 2014-06-30 3:54 UTC (permalink / raw) To: dsterba; +Cc: linux-btrfs, Gui Hecheng The btrfs-image requires at least 2 args to run, one for the source dev/file, the other for the target dev/file. This patch depends on patch: btrfs-progs: move the check_argc_* functions into utils.c Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com> --- changelog v1->v2: use check_argc_min instead --- btrfs-image.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/btrfs-image.c b/btrfs-image.c index e0aabfd..3b27155 100644 --- a/btrfs-image.c +++ b/btrfs-image.c @@ -2521,6 +2521,10 @@ int main(int argc, char *argv[]) } argc = argc - optind; + set_argv0(argv); + if (check_argc_min(argc, 2)) + print_usage(); + dev_cnt = argc - 1; if (create) { -- 1.8.1.4 ^ permalink raw reply related [flat|nested] 19+ messages in thread
* Re: [PATCH 1/2] btrfs-progs: move the check_argc_* functions into utils.c 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 ` David Sterba 2014-07-02 0:20 ` WorMzy Tykashi ` (2 more replies) 1 sibling, 3 replies; 19+ messages in thread From: David Sterba @ 2014-07-01 23:11 UTC (permalink / raw) To: Gui Hecheng; +Cc: dsterba, linux-btrfs On Mon, Jun 30, 2014 at 11:54:11AM +0800, Gui Hecheng wrote: > To let the independent tools(e.g. btrfs-image, btrfs-convert, etc.) > share the convenience of check_argc_* functions, just move it into > utils.c. > Also add a new function "set_argv0" to set the correct tool name: > *btrfs-image*: too few arguments > > The original btrfs* tools work as before. Good cleanup, but I don't like to see the utils.c pull the commands.h, the argv0 or argument helpers are selfcontained, so the relevant definitions shloud be moved to utils.h . Moved ARGV0_BUF_SIZE -> utils.h Removed check_argc_* protoypes from command.h Trivial changes, I made them myself, no need to resend the patch. The diff: --- a/commands.h +++ b/commands.h @@ -14,8 +14,6 @@ * Boston, MA 021110-1307, USA. */ -#define ARGV0_BUF_SIZE 64 - struct cmd_struct { const char *token; int (*fn)(int, char **); @@ -62,10 +60,6 @@ struct cmd_group { /* btrfs.c */ int prefixcmp(const char *str, const char *prefix); -int check_argc_exact(int nargs, int expected); -int check_argc_min(int nargs, int expected); -int check_argc_max(int nargs, int expected); - int handle_command_group(const struct cmd_group *grp, int argc, char **argv); diff --git a/utils.h b/utils.h index 20e5a2200052..e29ab936cd81 100644 --- a/utils.h +++ b/utils.h @@ -39,6 +39,8 @@ #define BTRFS_UUID_UNPARSED_SIZE 37 +#define ARGV0_BUF_SIZE 64 + int check_argc_exact(int nargs, int expected); int check_argc_min(int nargs, int expected); int check_argc_max(int nargs, int expected); --- ^ permalink raw reply related [flat|nested] 19+ messages in thread
* Re: [PATCH 1/2] btrfs-progs: move the check_argc_* functions into utils.c 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 1:13 ` Gui Hecheng 2014-07-10 1:06 ` [PATCH] btrfs-progs: use check_argc_* to check arg number for all tools Gui Hecheng 2 siblings, 1 reply; 19+ messages in thread From: WorMzy Tykashi @ 2014-07-02 0:20 UTC (permalink / raw) To: dsterba, Gui Hecheng, linux-btrfs@vger.kernel.org On 2 July 2014 00:11, David Sterba <dsterba@suse.cz> wrote: > On Mon, Jun 30, 2014 at 11:54:11AM +0800, Gui Hecheng wrote: >> To let the independent tools(e.g. btrfs-image, btrfs-convert, etc.) >> share the convenience of check_argc_* functions, just move it into >> utils.c. >> Also add a new function "set_argv0" to set the correct tool name: >> *btrfs-image*: too few arguments >> >> The original btrfs* tools work as before. > > Good cleanup, but I don't like to see the utils.c pull the commands.h, > the argv0 or argument helpers are selfcontained, so the relevant > definitions shloud be moved to utils.h . > > Moved ARGV0_BUF_SIZE -> utils.h > Removed check_argc_* protoypes from command.h > > Trivial changes, I made them myself, no need to resend the patch. > > The diff: > --- a/commands.h > +++ b/commands.h > @@ -14,8 +14,6 @@ > * Boston, MA 021110-1307, USA. > */ > > -#define ARGV0_BUF_SIZE 64 > - > struct cmd_struct { > const char *token; > int (*fn)(int, char **); > @@ -62,10 +60,6 @@ struct cmd_group { > /* btrfs.c */ > int prefixcmp(const char *str, const char *prefix); > > -int check_argc_exact(int nargs, int expected); > -int check_argc_min(int nargs, int expected); > -int check_argc_max(int nargs, int expected); > - > int handle_command_group(const struct cmd_group *grp, int argc, > char **argv); > > diff --git a/utils.h b/utils.h > index 20e5a2200052..e29ab936cd81 100644 > --- a/utils.h > +++ b/utils.h > @@ -39,6 +39,8 @@ > > #define BTRFS_UUID_UNPARSED_SIZE 37 > > +#define ARGV0_BUF_SIZE 64 > + > int check_argc_exact(int nargs, int expected); > int check_argc_min(int nargs, int expected); > int check_argc_max(int nargs, int expected); > --- > -- > To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in > the body of a message to majordomo@vger.kernel.org > More majordomo info at http://vger.kernel.org/majordomo-info.html Hi David, I'm getting a build failure in integration-20140701 in regard to these changes: --------- [CC] help.o help.c:23:23: error: 'ARGV0_BUF_SIZE' undeclared here (not in a function) static char argv0_buf[ARGV0_BUF_SIZE]; ^ help.c:23:13: warning: 'argv0_buf' defined but not used [-Wunused-variable] static char argv0_buf[ARGV0_BUF_SIZE]; ^ Makefile:114: recipe for target 'help.o' failed make: *** [help.o] Error 1 --------- Looks like help.c just need to include utils.h now too. I tested with the following patch, and it builds again. --- a/help.c 2014-07-02 01:14:02.365340224 +0100 +++ b/help.c 2014-07-02 01:13:49.995276342 +0100 @@ -19,6 +19,7 @@ #include <string.h> #include "commands.h" +#include "utils.h" static char argv0_buf[ARGV0_BUF_SIZE]; Cheers, WorMzy ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [PATCH 1/2] btrfs-progs: move the check_argc_* functions into utils.c 2014-07-02 0:20 ` WorMzy Tykashi @ 2014-07-02 9:34 ` David Sterba 2014-07-02 10:50 ` WorMzy Tykashi 0 siblings, 1 reply; 19+ messages in thread From: David Sterba @ 2014-07-02 9:34 UTC (permalink / raw) To: WorMzy Tykashi; +Cc: Gui Hecheng, linux-btrfs@vger.kernel.org On Wed, Jul 02, 2014 at 01:20:41AM +0100, WorMzy Tykashi wrote: > I'm getting a build failure in integration-20140701 in regard to these changes: > > --------- > [CC] help.o > help.c:23:23: error: 'ARGV0_BUF_SIZE' undeclared here (not in a function) > static char argv0_buf[ARGV0_BUF_SIZE]; > ^ > help.c:23:13: warning: 'argv0_buf' defined but not used [-Wunused-variable] > static char argv0_buf[ARGV0_BUF_SIZE]; > ^ > Makefile:114: recipe for target 'help.o' failed > make: *** [help.o] Error 1 > --------- Oh sorry, fixed branch pushed. I've added the utils.h inlucde into cmds-property.c as well, fixes "implicit declaration of function" warnings. ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [PATCH 1/2] btrfs-progs: move the check_argc_* functions into utils.c 2014-07-02 9:34 ` David Sterba @ 2014-07-02 10:50 ` WorMzy Tykashi 0 siblings, 0 replies; 19+ messages in thread From: WorMzy Tykashi @ 2014-07-02 10:50 UTC (permalink / raw) To: dsterba, WorMzy Tykashi, Gui Hecheng, linux-btrfs@vger.kernel.org On 2 July 2014 10:34, David Sterba <dsterba@suse.cz> wrote: > > Oh sorry, fixed branch pushed. I've added the utils.h inlucde into > cmds-property.c as well, fixes "implicit declaration of function" > warnings. Great stuff, thanks. WorMzy ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [PATCH 1/2] btrfs-progs: move the check_argc_* functions into utils.c 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 1:13 ` Gui Hecheng 2014-07-10 1:06 ` [PATCH] btrfs-progs: use check_argc_* to check arg number for all tools Gui Hecheng 2 siblings, 0 replies; 19+ messages in thread From: Gui Hecheng @ 2014-07-02 1:13 UTC (permalink / raw) To: dsterba; +Cc: linux-btrfs On Wed, 2014-07-02 at 01:11 +0200, David Sterba wrote: > On Mon, Jun 30, 2014 at 11:54:11AM +0800, Gui Hecheng wrote: > > To let the independent tools(e.g. btrfs-image, btrfs-convert, etc.) > > share the convenience of check_argc_* functions, just move it into > > utils.c. > > Also add a new function "set_argv0" to set the correct tool name: > > *btrfs-image*: too few arguments > > > > The original btrfs* tools work as before. > > Good cleanup, but I don't like to see the utils.c pull the commands.h, > the argv0 or argument helpers are selfcontained, so the relevant > definitions shloud be moved to utils.h . > > Moved ARGV0_BUF_SIZE -> utils.h > Removed check_argc_* protoypes from command.h > > Trivial changes, I made them myself, no need to resend the patch. Thanks very much David. > The diff: > --- a/commands.h > +++ b/commands.h > @@ -14,8 +14,6 @@ > * Boston, MA 021110-1307, USA. > */ > > -#define ARGV0_BUF_SIZE 64 > - > struct cmd_struct { > const char *token; > int (*fn)(int, char **); > @@ -62,10 +60,6 @@ struct cmd_group { > /* btrfs.c */ > int prefixcmp(const char *str, const char *prefix); > > -int check_argc_exact(int nargs, int expected); > -int check_argc_min(int nargs, int expected); > -int check_argc_max(int nargs, int expected); > - > int handle_command_group(const struct cmd_group *grp, int argc, > char **argv); > > diff --git a/utils.h b/utils.h > index 20e5a2200052..e29ab936cd81 100644 > --- a/utils.h > +++ b/utils.h > @@ -39,6 +39,8 @@ > > #define BTRFS_UUID_UNPARSED_SIZE 37 > > +#define ARGV0_BUF_SIZE 64 > + > int check_argc_exact(int nargs, int expected); > int check_argc_min(int nargs, int expected); > int check_argc_max(int nargs, int expected); > --- ^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH] btrfs-progs: use check_argc_* to check arg number for all tools 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 1:13 ` Gui Hecheng @ 2014-07-10 1:06 ` Gui Hecheng 2014-07-16 3:44 ` [PATCH v2] " Gui Hecheng 2014-07-16 3:59 ` Gui Hecheng 2 siblings, 2 replies; 19+ messages in thread From: Gui Hecheng @ 2014-07-10 1:06 UTC (permalink / raw) To: dsterba; +Cc: linux-btrfs, Gui Hecheng 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> --- 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 ^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH v2] btrfs-progs: use check_argc_* to check arg number for all tools 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 2014-07-16 3:58 ` Gui Hecheng 2014-07-16 3:59 ` Gui Hecheng 1 sibling, 1 reply; 19+ messages in thread From: Gui Hecheng @ 2014-07-16 3:44 UTC (permalink / raw) To: dsterba; +Cc: linux-btrfs, Gui Hecheng 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 ^ permalink raw reply related [flat|nested] 19+ messages in thread
* Re: [PATCH v2] btrfs-progs: use check_argc_* to check arg number for all tools 2014-07-16 3:44 ` [PATCH v2] " Gui Hecheng @ 2014-07-16 3:58 ` Gui Hecheng 0 siblings, 0 replies; 19+ messages in thread From: Gui Hecheng @ 2014-07-16 3:58 UTC (permalink / raw) To: dsterba; +Cc: linux-btrfs On Wed, 2014-07-16 at 11:44 +0800, Gui Hecheng wrote: Oh, very sorry for the noise, this is not the new one, please *ignore* this one. And also, for you to check, the v1 patch makes 2 mistakes: o mistakenly change the value of argc, which influence the following procedures. o mistakenly use set_argv0 for send & check cmd, which is not neccessary. > 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 && ^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v2] btrfs-progs: use check_argc_* to check arg number for all tools 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 ` [PATCH v2] " Gui Hecheng @ 2014-07-16 3:59 ` Gui Hecheng 1 sibling, 0 replies; 19+ messages in thread From: Gui Hecheng @ 2014-07-16 3:59 UTC (permalink / raw) To: dsterba; +Cc: linux-btrfs, Gui Hecheng 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 | 3 ++- btrfs-zero-log.c | 3 ++- btrfstune.c | 3 ++- cmds-check.c | 2 +- cmds-restore.c | 5 +++-- cmds-send.c | 2 +- 15 files changed, 36 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..38c5d26 100644 --- a/btrfs-show-super.c +++ b/btrfs-show-super.c @@ -95,7 +95,8 @@ int main(int argc, char **argv) } } - if (argc < optind + 1) { + set_argv0(argv); + if (check_argc_min(argc - optind, 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..078882b 100644 --- a/cmds-check.c +++ b/cmds-check.c @@ -7020,7 +7020,7 @@ int cmd_check(int argc, char **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..cbc21f8 100644 --- a/cmds-send.c +++ b/cmds-send.c @@ -556,7 +556,7 @@ int cmd_send(int argc, char **argv) } } - if (optind == argc) + if (check_argc_min(argc - optind, 1)) usage(cmd_send_usage); if (g_total_data_size && -- 1.8.1.4 ^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH 6/6] btrfs-progs: use BTRFS_SUPER_INFO_SIZE to replace raw 4096 in btrfs-image 2014-06-26 2:53 [PATCH 1/6] btrfs-progs: fix btrfs-image old_restore fsck failure Gui Hecheng ` (3 preceding siblings ...) 2014-06-26 2:53 ` [PATCH 5/6] btrfs-progs: limit minimal num of args for btrfs-image Gui Hecheng @ 2014-06-26 2:53 ` Gui Hecheng 4 siblings, 0 replies; 19+ messages in thread From: Gui Hecheng @ 2014-06-26 2:53 UTC (permalink / raw) To: linux-btrfs; +Cc: Gui Hecheng Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com> --- btrfs-image.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/btrfs-image.c b/btrfs-image.c index b13f236..ed0b6ec 100644 --- a/btrfs-image.c +++ b/btrfs-image.c @@ -1163,7 +1163,7 @@ static int copy_from_extent_tree(struct metadump_struct *metadump, int ret; extent_root = metadump->root->fs_info->extent_root; - bytenr = BTRFS_SUPER_INFO_OFFSET + 4096; + bytenr = BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE; key.objectid = bytenr; key.type = BTRFS_EXTENT_ITEM_KEY; key.offset = 0; @@ -1278,7 +1278,8 @@ static int create_metadump(const char *input, FILE *out, int num_threads, return ret; } - ret = add_extent(BTRFS_SUPER_INFO_OFFSET, 4096, &metadump, 0); + ret = add_extent(BTRFS_SUPER_INFO_OFFSET, BTRFS_SUPER_INFO_SIZE, + &metadump, 0); if (ret) { fprintf(stderr, "Error adding metadata %d\n", ret); err = ret; @@ -1368,7 +1369,7 @@ static void update_super_old(u8 *buffer) btrfs_set_stack_stripe_offset(&chunk->stripe, 0); memcpy(chunk->stripe.dev_uuid, super->dev_item.uuid, BTRFS_UUID_SIZE); btrfs_set_super_sys_array_size(super, sizeof(*key) + sizeof(*chunk)); - csum_block(buffer, 4096); + csum_block(buffer, BTRFS_SUPER_INFO_SIZE); } static int update_super(u8 *buffer) @@ -1422,7 +1423,7 @@ static int update_super(u8 *buffer) } btrfs_set_super_sys_array_size(super, new_array_size); - csum_block(buffer, 4096); + csum_block(buffer, BTRFS_SUPER_INFO_SIZE); return 0; } @@ -1569,12 +1570,12 @@ static void write_backup_supers(int fd, u8 *buf) for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) { bytenr = btrfs_sb_offset(i); - if (bytenr + 4096 > size) + if (bytenr + BTRFS_SUPER_INFO_SIZE > size) break; btrfs_set_super_bytenr(super, bytenr); - csum_block(buf, 4096); - ret = pwrite64(fd, buf, 4096, bytenr); - if (ret < 4096) { + csum_block(buf, BTRFS_SUPER_INFO_SIZE); + ret = pwrite64(fd, buf, BTRFS_SUPER_INFO_SIZE, bytenr); + if (ret < BTRFS_SUPER_INFO_SIZE) { if (ret < 0) fprintf(stderr, "Problem writing out backup " "super block %d, err %d\n", i, errno); -- 1.8.1.4 ^ permalink raw reply related [flat|nested] 19+ messages in thread
end of thread, other threads:[~2014-07-16 4:05 UTC | newest] Thread overview: 19+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 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 ` [PATCH v2] " Gui Hecheng 2014-07-16 3:58 ` 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
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).