Patchwork [3/5] libe2p: teach parse_num_blocks2() to return bytes if log_block_size < 0

login
register
mail settings
Submitter Theodore Ts'o
Date Jan. 15, 2013, 12:37 a.m.
Message ID <1358210232-30578-3-git-send-email-tytso@mit.edu>
Download mbox | patch
Permalink /patch/211963/
State Accepted
Headers show

Comments

Theodore Ts'o - Jan. 15, 2013, 12:37 a.m.
Previously the behavior of parse_num_block2 was undefined if
log_block_size was less than zero.  It will now return a number in
units of bytes.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
---
 lib/e2p/parse_num.c | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)
Zheng Liu - Jan. 15, 2013, 3:23 p.m.
On Mon, Jan 14, 2013 at 07:37:10PM -0500, Theodore Ts'o wrote:
> Previously the behavior of parse_num_block2 was undefined if
> log_block_size was less than zero.  It will now return a number in
> units of bytes.
> 
> Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
Reviewed-by: Zheng Liu <wenqing.lz@taobao.com>
Regards,
                                                - Zheng
> ---
>  lib/e2p/parse_num.c | 24 ++++++++++++++++++++----
>  1 file changed, 20 insertions(+), 4 deletions(-)
> 
> diff --git a/lib/e2p/parse_num.c b/lib/e2p/parse_num.c
> index d9ad3e7..cb0dc5b 100644
> --- a/lib/e2p/parse_num.c
> +++ b/lib/e2p/parse_num.c
> @@ -35,10 +35,16 @@ unsigned long long parse_num_blocks2(const char *arg, int log_block_size)
>  		num <<= 10;
>  		/* fallthrough */
>  	case 'K': case 'k':
> -		num >>= log_block_size;
> +		if (log_block_size < 0)
> +			num <<= 10;
> +		else
> +			num >>= log_block_size;
>  		break;
>  	case 's':
> -		num >>= (1+log_block_size);
> +		if (log_block_size < 0)
> +			num << 1;
> +		else
> +			num >>= (1+log_block_size);
>  		break;
>  	case '\0':
>  		break;
> @@ -62,11 +68,21 @@ main(int argc, char **argv)
>  	unsigned long num;
>  	int log_block_size = 0;
>  
> -	if (argc != 2) {
> -		fprintf(stderr, "Usage: %s arg\n", argv[0]);
> +	if (argc != 2 && argc != 3) {
> +		fprintf(stderr, "Usage: %s arg [log_block_size]\n", argv[0]);
>  		exit(1);
>  	}
>  
> +	if (argc == 3) {
> +		char *p;
> +
> +		log_block_size = strtol(argv[2], &p, 0);
> +		if (*p) {
> +			fprintf(stderr, "Bad log_block_size: %s\n", argv[2]);
> +			exit(1);
> +		}
> +	}
> +
>  	num = parse_num_blocks(argv[1], log_block_size);
>  
>  	printf("Parsed number: %lu\n", num);
> -- 
> 1.7.12.rc0.22.gcdd159b
> 
--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Patch

diff --git a/lib/e2p/parse_num.c b/lib/e2p/parse_num.c
index d9ad3e7..cb0dc5b 100644
--- a/lib/e2p/parse_num.c
+++ b/lib/e2p/parse_num.c
@@ -35,10 +35,16 @@  unsigned long long parse_num_blocks2(const char *arg, int log_block_size)
 		num <<= 10;
 		/* fallthrough */
 	case 'K': case 'k':
-		num >>= log_block_size;
+		if (log_block_size < 0)
+			num <<= 10;
+		else
+			num >>= log_block_size;
 		break;
 	case 's':
-		num >>= (1+log_block_size);
+		if (log_block_size < 0)
+			num << 1;
+		else
+			num >>= (1+log_block_size);
 		break;
 	case '\0':
 		break;
@@ -62,11 +68,21 @@  main(int argc, char **argv)
 	unsigned long num;
 	int log_block_size = 0;
 
-	if (argc != 2) {
-		fprintf(stderr, "Usage: %s arg\n", argv[0]);
+	if (argc != 2 && argc != 3) {
+		fprintf(stderr, "Usage: %s arg [log_block_size]\n", argv[0]);
 		exit(1);
 	}
 
+	if (argc == 3) {
+		char *p;
+
+		log_block_size = strtol(argv[2], &p, 0);
+		if (*p) {
+			fprintf(stderr, "Bad log_block_size: %s\n", argv[2]);
+			exit(1);
+		}
+	}
+
 	num = parse_num_blocks(argv[1], log_block_size);
 
 	printf("Parsed number: %lu\n", num);