linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/4] btrfs-progs: Use common function to parse unit arguments
@ 2015-08-27 13:38 Zhao Lei
  2015-08-27 13:38 ` [PATCH 1/4] btrfs-progs: Introduce get_unit_mode_from_arg for common use Zhao Lei
                   ` (4 more replies)
  0 siblings, 5 replies; 7+ messages in thread
From: Zhao Lei @ 2015-08-27 13:38 UTC (permalink / raw)
  To: linux-btrfs; +Cc: Zhao Lei

We are using separate code for parse unit mode in current code,
result is each command have different argument for unit mode:

 # btrfs filesystem show --help
   ...
   --raw              raw numbers in bytes
   --human-readable   human friendly numbers, base 1024 (default)
   --iec              use 1024 as a base (KiB, MiB, GiB, TiB)
   --si               use 1000 as a base (kB, MB, GB, TB)
   --kbytes           show sizes in KiB, or kB with --si
   --mbytes           show sizes in MiB, or MB with --si
   --gbytes           show sizes in GiB, or GB with --si
   --tbytes           show sizes in TiB, or TB with --si
   ...
 #
 # btrfs filesystem df --help
   ...
   -b|--raw           raw numbers in bytes
   -h|--human-readable
                      human friendly numbers, base 1024 (default)
   -H                 human friendly numbers, base 1000
   --iec              use 1024 as a base (KiB, MiB, GiB, TiB)
   --si               use 1000 as a base (kB, MB, GB, TB)
   -k|--kbytes        show sizes in KiB, or kB with --si
   -m|--mbytes        show sizes in MiB, or MB with --si
   -g|--gbytes        show sizes in GiB, or GB with --si
   -t|--tbytes        show sizes in TiB, or TB with --si
   ...
 #

This patchset introduce common function to parse arguments for setting
unit: get_unit_mode_from_arg()
and common help message for unit argument,
to make every tool in btrfs have same interface for setting unit.

The merit are:
1: Unify current each tool's arguments for unit
2: Make tools in future easy to implement such argument
3: Changes(enhancement) in common function have effect on all
   relative tools

Zhao Lei (4):
  btrfs-progs: Introduce get_unit_mode_from_arg for common use
  btrfs-progs: Use common unit parser for btrfs filesystem command
  btrfs-progs: Use common unit parser for btrfs device command
  btrfs-progs: Use common unit parser for btrfs qgroup command

 cmds-device.c     |  74 ++++-----------------------------
 cmds-fi-usage.c   |  79 ++++-------------------------------
 cmds-filesystem.c | 121 ++++++------------------------------------------------
 cmds-qgroup.c     |  47 ++-------------------
 utils.c           |  70 +++++++++++++++++++++++++++++++
 utils.h           |  14 +++++++
 6 files changed, 114 insertions(+), 291 deletions(-)

-- 
1.8.5.1


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 1/4] btrfs-progs: Introduce get_unit_mode_from_arg for common use
  2015-08-27 13:38 [PATCH 0/4] btrfs-progs: Use common function to parse unit arguments Zhao Lei
@ 2015-08-27 13:38 ` Zhao Lei
  2015-08-27 13:38 ` [PATCH 2/4] btrfs-progs: Use common unit parser for btrfs filesystem command Zhao Lei
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Zhao Lei @ 2015-08-27 13:38 UTC (permalink / raw)
  To: linux-btrfs; +Cc: Zhao Lei

We are using separate code for parse unit mode in current code,
result is each command have different argument for unit mode:

 # btrfs filesystem show --help
   ...
   --raw              raw numbers in bytes
   --human-readable   human friendly numbers, base 1024 (default)
   --iec              use 1024 as a base (KiB, MiB, GiB, TiB)
   --si               use 1000 as a base (kB, MB, GB, TB)
   --kbytes           show sizes in KiB, or kB with --si
   --mbytes           show sizes in MiB, or MB with --si
   --gbytes           show sizes in GiB, or GB with --si
   --tbytes           show sizes in TiB, or TB with --si
   ...
 #
 # btrfs filesystem df --help
   ...
   -b|--raw           raw numbers in bytes
   -h|--human-readable
                      human friendly numbers, base 1024 (default)
   -H                 human friendly numbers, base 1000
   --iec              use 1024 as a base (KiB, MiB, GiB, TiB)
   --si               use 1000 as a base (kB, MB, GB, TB)
   -k|--kbytes        show sizes in KiB, or kB with --si
   -m|--mbytes        show sizes in MiB, or MB with --si
   -g|--gbytes        show sizes in GiB, or GB with --si
   -t|--tbytes        show sizes in TiB, or TB with --si
   ...
 #

This patch introduce common function for to arguments for setting
unit, and a common help message, to make every tool in btrfs having
same unit argument.

The merit are:
1: Unify current each tool's arguments for unit
2: Make tools in future easy to implement such argument
3: Changes(enhancement) in common function have effect on all
   relative tools

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
---
 utils.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 utils.h | 14 +++++++++++++
 2 files changed, 84 insertions(+)

diff --git a/utils.c b/utils.c
index aa9c2c9..6af840b 100644
--- a/utils.c
+++ b/utils.c
@@ -2946,3 +2946,73 @@ int arg_copy_path(char *dest, const char *src, int destlen)
 
 	return 0;
 }
+
+unsigned int get_unit_mode_from_arg(int *argc, char *argv[])
+{
+	unsigned int unit_mode = UNITS_DEFAULT;
+	int arg_i;
+	int arg_end;
+
+	for (arg_i = 0; arg_i < *argc; arg_i++) {
+		if (!strcmp(argv[arg_i], "--raw")) {
+			unit_mode = UNITS_RAW;
+			argv[arg_i] = NULL;
+			continue;
+		}
+
+		if (!strcmp(argv[arg_i], "-h") ||
+		    !strcmp(argv[arg_i], "--human-readable")) {
+			unit_mode = UNITS_HUMAN_BINARY;
+			argv[arg_i] = NULL;
+			continue;
+		}
+		if (!strcmp(argv[arg_i], "-H")) {
+			unit_mode = UNITS_HUMAN_DECIMAL;
+			argv[arg_i] = NULL;
+			continue;
+		}
+
+		if (!strcmp(argv[arg_i], "--iec")) {
+			units_set_mode(&unit_mode, UNITS_BINARY);
+			argv[arg_i] = NULL;
+			continue;
+		}
+		if (!strcmp(argv[arg_i], "--si")) {
+			units_set_mode(&unit_mode, UNITS_DECIMAL);
+			argv[arg_i] = NULL;
+			continue;
+		}
+
+		if (!strcmp(argv[arg_i], "--kbytes")) {
+			units_set_base(&unit_mode, UNITS_KBYTES);
+			argv[arg_i] = NULL;
+			continue;
+		}
+		if (!strcmp(argv[arg_i], "--mbytes")) {
+			units_set_base(&unit_mode, UNITS_MBYTES);
+			argv[arg_i] = NULL;
+			continue;
+		}
+		if (!strcmp(argv[arg_i], "--gbytes")) {
+			units_set_base(&unit_mode, UNITS_GBYTES);
+			argv[arg_i] = NULL;
+			continue;
+		}
+		if (!strcmp(argv[arg_i], "--tbytes")) {
+			units_set_base(&unit_mode, UNITS_TBYTES);
+			argv[arg_i] = NULL;
+			continue;
+		}
+	}
+
+	for (arg_i = 0, arg_end = 0; arg_i < *argc; arg_i++) {
+		if (!argv[arg_i])
+			continue;
+		argv[arg_end] = argv[arg_i];
+		arg_end++;
+	}
+
+	*argc = arg_end;
+
+	return unit_mode;
+}
diff --git a/utils.h b/utils.h
index 10d68e9..7b5c72b 100644
--- a/utils.h
+++ b/utils.h
@@ -245,4 +245,18 @@ int btrfs_check_nodesize(u32 nodesize, u32 sectorsize);
 
 const char *get_argv0_buf(void);
 
+#define HELPINFO_OUTPUT_UNIT \
+	"--raw              raw numbers in bytes", \
+	"-h|--human-readable", \
+	"                   human friendly numbers, base 1024 (default)", \
+	"-H                 human friendly numbers, base 1000", \
+	"--iec              use 1024 as a base (KiB, MiB, GiB, TiB)", \
+	"--si               use 1000 as a base (kB, MB, GB, TB)", \
+	"--kbytes           show sizes in KiB, or kB with --si", \
+	"--mbytes           show sizes in MiB, or MB with --si", \
+	"--gbytes           show sizes in GiB, or GB with --si", \
+	"--tbytes           show sizes in TiB, or TB with --si"
+
+unsigned int get_unit_mode_from_arg(int *argc, char *argv[]);
+
 #endif
-- 
1.8.5.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 2/4] btrfs-progs: Use common unit parser for btrfs filesystem command
  2015-08-27 13:38 [PATCH 0/4] btrfs-progs: Use common function to parse unit arguments Zhao Lei
  2015-08-27 13:38 ` [PATCH 1/4] btrfs-progs: Introduce get_unit_mode_from_arg for common use Zhao Lei
@ 2015-08-27 13:38 ` Zhao Lei
  2015-08-27 13:38 ` [PATCH 3/4] btrfs-progs: Use common unit parser for btrfs device command Zhao Lei
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Zhao Lei @ 2015-08-27 13:38 UTC (permalink / raw)
  To: linux-btrfs; +Cc: Zhao Lei

Move to use get_unit_mode_from_arg() for cmds-filesystem.c,
to make "btrfs filesystem df/show/usage"'s unit argument same.

Also have cleanup effect: 18 insertions(+), 178 deletions(-)

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
---
 cmds-fi-usage.c   |  79 ++++-------------------------------
 cmds-filesystem.c | 121 ++++++------------------------------------------------
 2 files changed, 19 insertions(+), 181 deletions(-)

diff --git a/cmds-fi-usage.c b/cmds-fi-usage.c
index adf1c27..40e0f31 100644
--- a/cmds-fi-usage.c
+++ b/cmds-fi-usage.c
@@ -859,88 +859,23 @@ out:
 const char * const cmd_filesystem_usage_usage[] = {
 	"btrfs filesystem usage [options] <path> [<path>..]",
 	"Show detailed information about internal filesystem usage .",
-	"-b|--raw           raw numbers in bytes",
-	"-h|--human-readable",
-	"                   human friendly numbers, base 1024 (default)",
-	"-H                 human friendly numbers, base 1000",
-	"--iec              use 1024 as a base (KiB, MiB, GiB, TiB)",
-	"--si               use 1000 as a base (kB, MB, GB, TB)",
-	"-k|--kbytes        show sizes in KiB, or kB with --si",
-	"-m|--mbytes        show sizes in MiB, or MB with --si",
-	"-g|--gbytes        show sizes in GiB, or GB with --si",
-	"-t|--tbytes        show sizes in TiB, or TB with --si",
-	"-T                 show data in tabular format",
+	HELPINFO_OUTPUT_UNIT,
 	NULL
 };
 
 int cmd_filesystem_usage(int argc, char **argv)
 {
-	unsigned unit_mode = UNITS_DEFAULT;
+	unsigned unit_mode;
 	int ret = 0;
-	int	i, more_than_one = 0;
-	int	tabular = 0;
+	int i, more_than_one = 0;
+	int tabular = 0;
 
-	optind = 1;
-	while (1) {
-		int c;
-		static const struct option long_options[] = {
-			{ "raw", no_argument, NULL, 'b'},
-			{ "kbytes", no_argument, NULL, 'k'},
-			{ "mbytes", no_argument, NULL, 'm'},
-			{ "gbytes", no_argument, NULL, 'g'},
-			{ "tbytes", no_argument, NULL, 't'},
-			{ "si", no_argument, NULL, GETOPT_VAL_SI},
-			{ "iec", no_argument, NULL, GETOPT_VAL_IEC},
-			{ "human-readable", no_argument, NULL,
-				GETOPT_VAL_HUMAN_READABLE},
-			{ NULL, 0, NULL, 0 }
-		};
-
-		c = getopt_long(argc, argv, "bhHkmgtT", long_options, NULL);
-
-		if (c < 0)
-			break;
-		switch (c) {
-		case 'b':
-			unit_mode = UNITS_RAW;
-			break;
-		case 'k':
-			units_set_base(&unit_mode, UNITS_KBYTES);
-			break;
-		case 'm':
-			units_set_base(&unit_mode, UNITS_MBYTES);
-			break;
-		case 'g':
-			units_set_base(&unit_mode, UNITS_GBYTES);
-			break;
-		case 't':
-			units_set_base(&unit_mode, UNITS_TBYTES);
-			break;
-		case GETOPT_VAL_HUMAN_READABLE:
-		case 'h':
-			unit_mode = UNITS_HUMAN_BINARY;
-			break;
-		case 'H':
-			unit_mode = UNITS_HUMAN_DECIMAL;
-			break;
-		case GETOPT_VAL_SI:
-			units_set_mode(&unit_mode, UNITS_DECIMAL);
-			break;
-		case GETOPT_VAL_IEC:
-			units_set_mode(&unit_mode, UNITS_BINARY);
-			break;
-		case 'T':
-			tabular = 1;
-			break;
-		default:
-			usage(cmd_filesystem_usage_usage);
-		}
-	}
+	unit_mode = get_unit_mode_from_arg(&argc, argv);
 
-	if (check_argc_min(argc - optind, 1))
+	if (check_argc_min(argc, 2) || argv[1][0] == '-')
 		usage(cmd_filesystem_usage_usage);
 
-	for (i = optind; i < argc; i++) {
+	for (i = 1; i < argc; i++) {
 		int fd;
 		DIR *dirstream = NULL;
 		struct chunk_info *chunkinfo = NULL;
diff --git a/cmds-filesystem.c b/cmds-filesystem.c
index fa555b0..6a4dc8f 100644
--- a/cmds-filesystem.c
+++ b/cmds-filesystem.c
@@ -121,19 +121,10 @@ static const char * const filesystem_cmd_group_usage[] = {
 };
 
 static const char * const cmd_filesystem_df_usage[] = {
-       "btrfs filesystem df [options] <path>",
-       "Show space usage information for a mount point",
-	"-b|--raw           raw numbers in bytes",
-	"-h|--human-readable",
-	"                   human friendly numbers, base 1024 (default)",
-	"-H                 human friendly numbers, base 1000",
-	"--iec              use 1024 as a base (KiB, MiB, GiB, TiB)",
-	"--si               use 1000 as a base (kB, MB, GB, TB)",
-	"-k|--kbytes        show sizes in KiB, or kB with --si",
-	"-m|--mbytes        show sizes in MiB, or MB with --si",
-	"-g|--gbytes        show sizes in GiB, or GB with --si",
-	"-t|--tbytes        show sizes in TiB, or TB with --si",
-       NULL
+	"btrfs filesystem df [options] <path>",
+	"Show space usage information for a mount point",
+	HELPINFO_OUTPUT_UNIT,
+	NULL
 };
 
 static int get_df(int fd, struct btrfs_ioctl_space_args **sargs_ret)
@@ -205,64 +196,14 @@ static int cmd_filesystem_df(int argc, char **argv)
 	int fd;
 	char *path;
 	DIR *dirstream = NULL;
-	unsigned unit_mode = UNITS_DEFAULT;
+	unsigned unit_mode;
 
-	while (1) {
-		int c;
-		static const struct option long_options[] = {
-			{ "raw", no_argument, NULL, 'b'},
-			{ "kbytes", no_argument, NULL, 'k'},
-			{ "mbytes", no_argument, NULL, 'm'},
-			{ "gbytes", no_argument, NULL, 'g'},
-			{ "tbytes", no_argument, NULL, 't'},
-			{ "si", no_argument, NULL, GETOPT_VAL_SI},
-			{ "iec", no_argument, NULL, GETOPT_VAL_IEC},
-			{ "human-readable", no_argument, NULL,
-				GETOPT_VAL_HUMAN_READABLE},
-			{ NULL, 0, NULL, 0 }
-		};
-
-		c = getopt_long(argc, argv, "bhHkmgt", long_options, NULL);
-		if (c < 0)
-			break;
-		switch (c) {
-		case 'b':
-			unit_mode = UNITS_RAW;
-			break;
-		case 'k':
-			units_set_base(&unit_mode, UNITS_KBYTES);
-			break;
-		case 'm':
-			units_set_base(&unit_mode, UNITS_MBYTES);
-			break;
-		case 'g':
-			units_set_base(&unit_mode, UNITS_GBYTES);
-			break;
-		case 't':
-			units_set_base(&unit_mode, UNITS_TBYTES);
-			break;
-		case GETOPT_VAL_HUMAN_READABLE:
-		case 'h':
-			unit_mode = UNITS_HUMAN_BINARY;
-			break;
-		case 'H':
-			unit_mode = UNITS_HUMAN_DECIMAL;
-			break;
-		case GETOPT_VAL_SI:
-			units_set_mode(&unit_mode, UNITS_DECIMAL);
-			break;
-		case GETOPT_VAL_IEC:
-			units_set_mode(&unit_mode, UNITS_BINARY);
-			break;
-		default:
-			usage(cmd_filesystem_df_usage);
-		}
-	}
+	unit_mode = get_unit_mode_from_arg(&argc, argv);
 
-	if (check_argc_exact(argc, optind + 1))
+	if (argc != 2 || argv[1][0] == '-')
 		usage(cmd_filesystem_df_usage);
 
-	path = argv[optind];
+	path = argv[1];
 
 	fd = open_file_or_dir(path, &dirstream);
 	if (fd < 0) {
@@ -820,14 +761,7 @@ static const char * const cmd_filesystem_show_usage[] = {
 	"Show the structure of a filesystem",
 	"-d|--all-devices   show only disks under /dev containing btrfs filesystem",
 	"-m|--mounted       show only mounted btrfs",
-	"--raw              raw numbers in bytes",
-	"--human-readable   human friendly numbers, base 1024 (default)",
-	"--iec              use 1024 as a base (KiB, MiB, GiB, TiB)",
-	"--si               use 1000 as a base (kB, MB, GB, TB)",
-	"--kbytes           show sizes in KiB, or kB with --si",
-	"--mbytes           show sizes in MiB, or MB with --si",
-	"--gbytes           show sizes in GiB, or GB with --si",
-	"--tbytes           show sizes in TiB, or TB with --si",
+	HELPINFO_OUTPUT_UNIT,
 	"If no argument is given, structure of all present filesystems is shown.",
 	NULL
 };
@@ -845,23 +779,16 @@ static int cmd_filesystem_show(int argc, char **argv)
 	char path[PATH_MAX];
 	__u8 fsid[BTRFS_FSID_SIZE];
 	char uuid_buf[BTRFS_UUID_UNPARSED_SIZE];
-	unsigned unit_mode = UNITS_DEFAULT;
+	unsigned unit_mode;
 	int found = 0;
 
+	unit_mode = get_unit_mode_from_arg(&argc, argv);
+
 	while (1) {
 		int c;
 		static const struct option long_options[] = {
 			{ "all-devices", no_argument, NULL, 'd'},
 			{ "mounted", no_argument, NULL, 'm'},
-			{ "raw", no_argument, NULL, GETOPT_VAL_RAW},
-			{ "kbytes", no_argument, NULL, GETOPT_VAL_KBYTES},
-			{ "mbytes", no_argument, NULL, GETOPT_VAL_MBYTES},
-			{ "gbytes", no_argument, NULL, GETOPT_VAL_GBYTES},
-			{ "tbytes", no_argument, NULL, GETOPT_VAL_TBYTES},
-			{ "si", no_argument, NULL, GETOPT_VAL_SI},
-			{ "iec", no_argument, NULL, GETOPT_VAL_IEC},
-			{ "human-readable", no_argument, NULL,
-				GETOPT_VAL_HUMAN_READABLE},
 			{ NULL, 0, NULL, 0 }
 		};
 
@@ -875,30 +802,6 @@ static int cmd_filesystem_show(int argc, char **argv)
 		case 'm':
 			where = BTRFS_SCAN_MOUNTED;
 			break;
-		case GETOPT_VAL_RAW:
-			units_set_mode(&unit_mode, UNITS_RAW);
-			break;
-		case GETOPT_VAL_KBYTES:
-			units_set_base(&unit_mode, UNITS_KBYTES);
-			break;
-		case GETOPT_VAL_MBYTES:
-			units_set_base(&unit_mode, UNITS_MBYTES);
-			break;
-		case GETOPT_VAL_GBYTES:
-			units_set_base(&unit_mode, UNITS_GBYTES);
-			break;
-		case GETOPT_VAL_TBYTES:
-			units_set_base(&unit_mode, UNITS_TBYTES);
-			break;
-		case GETOPT_VAL_SI:
-			units_set_mode(&unit_mode, UNITS_DECIMAL);
-			break;
-		case GETOPT_VAL_IEC:
-			units_set_mode(&unit_mode, UNITS_BINARY);
-			break;
-		case GETOPT_VAL_HUMAN_READABLE:
-			units_set_mode(&unit_mode, UNITS_HUMAN_BINARY);
-			break;
 		default:
 			usage(cmd_filesystem_show_usage);
 		}
-- 
1.8.5.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 3/4] btrfs-progs: Use common unit parser for btrfs device command
  2015-08-27 13:38 [PATCH 0/4] btrfs-progs: Use common function to parse unit arguments Zhao Lei
  2015-08-27 13:38 ` [PATCH 1/4] btrfs-progs: Introduce get_unit_mode_from_arg for common use Zhao Lei
  2015-08-27 13:38 ` [PATCH 2/4] btrfs-progs: Use common unit parser for btrfs filesystem command Zhao Lei
@ 2015-08-27 13:38 ` Zhao Lei
  2015-08-27 13:38 ` [PATCH 4/4] btrfs-progs: Use common unit parser for btrfs qgroup command Zhao Lei
  2015-08-28 17:24 ` [PATCH 0/4] btrfs-progs: Use common function to parse unit arguments David Sterba
  4 siblings, 0 replies; 7+ messages in thread
From: Zhao Lei @ 2015-08-27 13:38 UTC (permalink / raw)
  To: linux-btrfs; +Cc: Zhao Lei

Move to use get_unit_mode_from_arg() for cmds-device.c,
to make "btrfs device usage"'s unit argument same with other
tools.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
---
 cmds-device.c | 74 ++++++-----------------------------------------------------
 1 file changed, 7 insertions(+), 67 deletions(-)

diff --git a/cmds-device.c b/cmds-device.c
index 844289a..25e8c39 100644
--- a/cmds-device.c
+++ b/cmds-device.c
@@ -474,16 +474,7 @@ out:
 const char * const cmd_device_usage_usage[] = {
 	"btrfs device usage [options] <path> [<path>..]",
 	"Show detailed information about internal allocations in devices.",
-	"-b|--raw           raw numbers in bytes",
-	"-h|--human-readable",
-	"                   human friendly numbers, base 1024 (default)",
-	"-H                 human friendly numbers, base 1000",
-	"--iec              use 1024 as a base (KiB, MiB, GiB, TiB)",
-	"--si               use 1000 as a base (kB, MB, GB, TB)",
-	"-k|--kbytes        show sizes in KiB, or kB with --si",
-	"-m|--mbytes        show sizes in MiB, or MB with --si",
-	"-g|--gbytes        show sizes in GiB, or GB with --si",
-	"-t|--tbytes        show sizes in TiB, or TB with --si",
+	HELPINFO_OUTPUT_UNIT,
 	NULL
 };
 
@@ -518,69 +509,18 @@ out:
 
 int cmd_device_usage(int argc, char **argv)
 {
-	unsigned unit_mode = UNITS_DEFAULT;
+	unsigned unit_mode;
 	int ret = 0;
-	int	i, more_than_one = 0;
+	int i, more_than_one = 0;
 
-	optind = 1;
-	while (1) {
-		int c;
-		static const struct option long_options[] = {
-			{ "raw", no_argument, NULL, 'b'},
-			{ "kbytes", no_argument, NULL, 'k'},
-			{ "mbytes", no_argument, NULL, 'm'},
-			{ "gbytes", no_argument, NULL, 'g'},
-			{ "tbytes", no_argument, NULL, 't'},
-			{ "si", no_argument, NULL, GETOPT_VAL_SI},
-			{ "iec", no_argument, NULL, GETOPT_VAL_IEC},
-			{ "human-readable", no_argument, NULL,
-				GETOPT_VAL_HUMAN_READABLE},
-			{ NULL, 0, NULL, 0 }
-		};
-
-		c = getopt_long(argc, argv, "bhHkmgt", long_options, NULL);
-		if (c < 0)
-			break;
-		switch (c) {
-		case 'b':
-			unit_mode = UNITS_RAW;
-			break;
-		case 'k':
-			units_set_base(&unit_mode, UNITS_KBYTES);
-			break;
-		case 'm':
-			units_set_base(&unit_mode, UNITS_MBYTES);
-			break;
-		case 'g':
-			units_set_base(&unit_mode, UNITS_GBYTES);
-			break;
-		case 't':
-			units_set_base(&unit_mode, UNITS_TBYTES);
-			break;
-		case GETOPT_VAL_HUMAN_READABLE:
-		case 'h':
-			unit_mode = UNITS_HUMAN_BINARY;
-			break;
-		case 'H':
-			unit_mode = UNITS_HUMAN_DECIMAL;
-			break;
-		case GETOPT_VAL_SI:
-			units_set_mode(&unit_mode, UNITS_DECIMAL);
-			break;
-		case GETOPT_VAL_IEC:
-			units_set_mode(&unit_mode, UNITS_BINARY);
-			break;
-		default:
-			usage(cmd_device_usage_usage);
-		}
-	}
+	unit_mode = get_unit_mode_from_arg(&argc, argv);
 
-	if (check_argc_min(argc - optind, 1))
+	if (check_argc_min(argc, 2) || argv[1][0] == '-')
 		usage(cmd_device_usage_usage);
 
-	for (i = optind; i < argc ; i++) {
+	for (i = 1; i < argc; i++) {
 		int fd;
-		DIR	*dirstream = NULL;
+		DIR *dirstream = NULL;
 		if (more_than_one)
 			printf("\n");
 
-- 
1.8.5.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 4/4] btrfs-progs: Use common unit parser for btrfs qgroup command
  2015-08-27 13:38 [PATCH 0/4] btrfs-progs: Use common function to parse unit arguments Zhao Lei
                   ` (2 preceding siblings ...)
  2015-08-27 13:38 ` [PATCH 3/4] btrfs-progs: Use common unit parser for btrfs device command Zhao Lei
@ 2015-08-27 13:38 ` Zhao Lei
  2015-08-28 17:24 ` [PATCH 0/4] btrfs-progs: Use common function to parse unit arguments David Sterba
  4 siblings, 0 replies; 7+ messages in thread
From: Zhao Lei @ 2015-08-27 13:38 UTC (permalink / raw)
  To: linux-btrfs; +Cc: Zhao Lei

Move to use get_unit_mode_from_arg() for btrfs qgroup command,
to make "btrfs qgroup show"'s unit argument same with other
tools.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
---
 cmds-qgroup.c | 47 ++++-------------------------------------------
 1 file changed, 4 insertions(+), 43 deletions(-)

diff --git a/cmds-qgroup.c b/cmds-qgroup.c
index d58df5e..1505c03 100644
--- a/cmds-qgroup.c
+++ b/cmds-qgroup.c
@@ -275,15 +275,7 @@ static const char * const cmd_qgroup_show_usage[] = {
 	"               (including ancestral qgroups)",
 	"-f             list all qgroups which impact the given path",
 	"               (excluding ancestral qgroups)",
-	"--raw          raw numbers in bytes",
-	"--human-readable",
-	"               human firendly numbers in given base, 1024 by default",
-	"--iec          use 1024 as a base (KiB, MiB, GiB, TiB)",
-	"--si           use 1000 as a base (kB, MB, GB, TB)",
-	"--kbytes       show sizes in KiB, or kB with --si",
-	"--mbytes       show sizes in MiB, or MB with --si",
-	"--gbytes       show sizes in GiB, or GB with --si",
-	"--tbytes       show sizes in TiB, or TB with --si",
+	HELPINFO_OUTPUT_UNIT,
 	"--sort=qgroupid,rfer,excl,max_rfer,max_excl",
 	"               list qgroups sorted by specified items",
 	"               you can use '+' or '-' in front of each item.",
@@ -300,27 +292,20 @@ static int cmd_qgroup_show(int argc, char **argv)
 	DIR *dirstream = NULL;
 	u64 qgroupid;
 	int filter_flag = 0;
-	unsigned unit_mode = UNITS_DEFAULT;
+	unsigned unit_mode;
 
 	struct btrfs_qgroup_comparer_set *comparer_set;
 	struct btrfs_qgroup_filter_set *filter_set;
 	filter_set = btrfs_qgroup_alloc_filter_set();
 	comparer_set = btrfs_qgroup_alloc_comparer_set();
 
+	unit_mode = get_unit_mode_from_arg(&argc, argv);
+
 	optind = 1;
 	while (1) {
 		int c;
 		static const struct option long_options[] = {
 			{"sort", required_argument, NULL, 'S'},
-			{"raw", no_argument, NULL, GETOPT_VAL_RAW},
-			{"kbytes", no_argument, NULL, GETOPT_VAL_KBYTES},
-			{"mbytes", no_argument, NULL, GETOPT_VAL_MBYTES},
-			{"gbytes", no_argument, NULL, GETOPT_VAL_GBYTES},
-			{"tbytes", no_argument, NULL, GETOPT_VAL_TBYTES},
-			{"si", no_argument, NULL, GETOPT_VAL_SI},
-			{"iec", no_argument, NULL, GETOPT_VAL_IEC},
-			{ "human-readable", no_argument, NULL,
-				GETOPT_VAL_HUMAN_READABLE},
 			{ NULL, 0, NULL, 0 }
 		};
 
@@ -356,30 +341,6 @@ static int cmd_qgroup_show(int argc, char **argv)
 			if (ret)
 				usage(cmd_qgroup_show_usage);
 			break;
-		case GETOPT_VAL_RAW:
-			unit_mode = UNITS_RAW;
-			break;
-		case GETOPT_VAL_KBYTES:
-			units_set_base(&unit_mode, UNITS_KBYTES);
-			break;
-		case GETOPT_VAL_MBYTES:
-			units_set_base(&unit_mode, UNITS_MBYTES);
-			break;
-		case GETOPT_VAL_GBYTES:
-			units_set_base(&unit_mode, UNITS_GBYTES);
-			break;
-		case GETOPT_VAL_TBYTES:
-			units_set_base(&unit_mode, UNITS_TBYTES);
-			break;
-		case GETOPT_VAL_SI:
-			units_set_mode(&unit_mode, UNITS_DECIMAL);
-			break;
-		case GETOPT_VAL_IEC:
-			units_set_mode(&unit_mode, UNITS_BINARY);
-			break;
-		case GETOPT_VAL_HUMAN_READABLE:
-			unit_mode = UNITS_HUMAN_BINARY;
-			break;
 		default:
 			usage(cmd_qgroup_show_usage);
 		}
-- 
1.8.5.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH 0/4] btrfs-progs: Use common function to parse unit arguments
  2015-08-27 13:38 [PATCH 0/4] btrfs-progs: Use common function to parse unit arguments Zhao Lei
                   ` (3 preceding siblings ...)
  2015-08-27 13:38 ` [PATCH 4/4] btrfs-progs: Use common unit parser for btrfs qgroup command Zhao Lei
@ 2015-08-28 17:24 ` David Sterba
  2015-08-31  1:56   ` Zhao Lei
  4 siblings, 1 reply; 7+ messages in thread
From: David Sterba @ 2015-08-28 17:24 UTC (permalink / raw)
  To: Zhao Lei; +Cc: linux-btrfs

On Thu, Aug 27, 2015 at 09:38:19PM +0800, Zhao Lei wrote:
> We are using separate code for parse unit mode in current code,
> result is each command have different argument for unit mode:
> 
>  # btrfs filesystem show --help
>    ...
>    --raw              raw numbers in bytes
>    --human-readable   human friendly numbers, base 1024 (default)
>    --iec              use 1024 as a base (KiB, MiB, GiB, TiB)
>    --si               use 1000 as a base (kB, MB, GB, TB)
>    --kbytes           show sizes in KiB, or kB with --si
>    --mbytes           show sizes in MiB, or MB with --si
>    --gbytes           show sizes in GiB, or GB with --si
>    --tbytes           show sizes in TiB, or TB with --si
>    ...
>  #
>  # btrfs filesystem df --help
>    ...
>    -b|--raw           raw numbers in bytes
>    -h|--human-readable
>                       human friendly numbers, base 1024 (default)
>    -H                 human friendly numbers, base 1000
>    --iec              use 1024 as a base (KiB, MiB, GiB, TiB)
>    --si               use 1000 as a base (kB, MB, GB, TB)
>    -k|--kbytes        show sizes in KiB, or kB with --si
>    -m|--mbytes        show sizes in MiB, or MB with --si
>    -g|--gbytes        show sizes in GiB, or GB with --si
>    -t|--tbytes        show sizes in TiB, or TB with --si
>    ...
>  #
> 
> This patchset introduce common function to parse arguments for setting
> unit: get_unit_mode_from_arg()
> and common help message for unit argument,
> to make every tool in btrfs have same interface for setting unit.
> 
> The merit are:
> 1: Unify current each tool's arguments for unit
> 2: Make tools in future easy to implement such argument
> 3: Changes(enhancement) in common function have effect on all
>    relative tools

I like this kind of unification and code simplification. However, the
'filesystem df' command keeps some option compatibility with the
standalone 'df' commmand while the other btrfs commands use only long
versions of the options. So we have to treat 'fi df' differently and not
remove the existing short options. But otherwise looks good, thanks.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: [PATCH 0/4] btrfs-progs: Use common function to parse unit arguments
  2015-08-28 17:24 ` [PATCH 0/4] btrfs-progs: Use common function to parse unit arguments David Sterba
@ 2015-08-31  1:56   ` Zhao Lei
  0 siblings, 0 replies; 7+ messages in thread
From: Zhao Lei @ 2015-08-31  1:56 UTC (permalink / raw)
  To: dsterba; +Cc: linux-btrfs

Hi, David Sterba

> -----Original Message-----
> From: David Sterba [mailto:dsterba@suse.cz]
> Sent: Saturday, August 29, 2015 1:24 AM
> To: Zhao Lei <zhaolei@cn.fujitsu.com>
> Cc: linux-btrfs@vger.kernel.org
> Subject: Re: [PATCH 0/4] btrfs-progs: Use common function to parse unit
> arguments
> 
> On Thu, Aug 27, 2015 at 09:38:19PM +0800, Zhao Lei wrote:
> > We are using separate code for parse unit mode in current code, result
> > is each command have different argument for unit mode:
> >
> >  # btrfs filesystem show --help
> >    ...
> >    --raw              raw numbers in bytes
> >    --human-readable   human friendly numbers, base 1024 (default)
> >    --iec              use 1024 as a base (KiB, MiB, GiB, TiB)
> >    --si               use 1000 as a base (kB, MB, GB, TB)
> >    --kbytes           show sizes in KiB, or kB with --si
> >    --mbytes           show sizes in MiB, or MB with --si
> >    --gbytes           show sizes in GiB, or GB with --si
> >    --tbytes           show sizes in TiB, or TB with --si
> >    ...
> >  #
> >  # btrfs filesystem df --help
> >    ...
> >    -b|--raw           raw numbers in bytes
> >    -h|--human-readable
> >                       human friendly numbers, base 1024 (default)
> >    -H                 human friendly numbers, base 1000
> >    --iec              use 1024 as a base (KiB, MiB, GiB, TiB)
> >    --si               use 1000 as a base (kB, MB, GB, TB)
> >    -k|--kbytes        show sizes in KiB, or kB with --si
> >    -m|--mbytes        show sizes in MiB, or MB with --si
> >    -g|--gbytes        show sizes in GiB, or GB with --si
> >    -t|--tbytes        show sizes in TiB, or TB with --si
> >    ...
> >  #
> >
> > This patchset introduce common function to parse arguments for setting
> > unit: get_unit_mode_from_arg()
> > and common help message for unit argument, to make every tool in btrfs
> > have same interface for setting unit.
> >
> > The merit are:
> > 1: Unify current each tool's arguments for unit
> > 2: Make tools in future easy to implement such argument
> > 3: Changes(enhancement) in common function have effect on all
> >    relative tools
> 
> I like this kind of unification and code simplification. However, the 'filesystem df'
> command keeps some option compatibility with the standalone 'df' commmand
> while the other btrfs commands use only long versions of the options. So we
> have to treat 'fi df' differently and not remove the existing short options. But
> otherwise looks good, thanks.

Thanks for explain why we use short option for df.
I'll send v2.

Thanks
Zhaolei


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2015-08-31  1:57 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-08-27 13:38 [PATCH 0/4] btrfs-progs: Use common function to parse unit arguments Zhao Lei
2015-08-27 13:38 ` [PATCH 1/4] btrfs-progs: Introduce get_unit_mode_from_arg for common use Zhao Lei
2015-08-27 13:38 ` [PATCH 2/4] btrfs-progs: Use common unit parser for btrfs filesystem command Zhao Lei
2015-08-27 13:38 ` [PATCH 3/4] btrfs-progs: Use common unit parser for btrfs device command Zhao Lei
2015-08-27 13:38 ` [PATCH 4/4] btrfs-progs: Use common unit parser for btrfs qgroup command Zhao Lei
2015-08-28 17:24 ` [PATCH 0/4] btrfs-progs: Use common function to parse unit arguments David Sterba
2015-08-31  1:56   ` Zhao Lei

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).