linux-f2fs-devel.lists.sourceforge.net archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] fsck.f2fs: support -p without argument
@ 2017-01-21  6:00 Jaegeuk Kim
  2017-01-21  6:00 ` [PATCH 2/2] fsck.f2fs: show parse errors neatly Jaegeuk Kim
  0 siblings, 1 reply; 2+ messages in thread
From: Jaegeuk Kim @ 2017-01-21  6:00 UTC (permalink / raw)
  To: linux-f2fs-devel; +Cc: Jaegeuk Kim

This patch allows fsck run -p without argument. So we could use -p as
-p, -p 0, -p 1. '-p' and '-p 0' have the same meaning as '-a'. '-p 1'
check more meta data than '-a'.

Reported-by: KARBOWSKI Piotr <piotr.karbowski@gmail.com>
Signed-off-by: Sheng Yong <shengyong1@huawei.com>
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
---
 fsck/main.c | 99 +++++++++++++++++++++++++++++++++++++++++++++++++------------
 1 file changed, 80 insertions(+), 19 deletions(-)

diff --git a/fsck/main.c b/fsck/main.c
index 39ef8d3..0ac1711 100644
--- a/fsck/main.c
+++ b/fsck/main.c
@@ -17,6 +17,7 @@
  */
 #include "fsck.h"
 #include <libgen.h>
+#include <ctype.h>
 
 struct f2fs_fsck gfsck;
 
@@ -77,13 +78,54 @@ void sload_usage()
 	exit(1);
 }
 
+static void __handle_fsck_args(int optopt)
+{
+	switch (optopt) {
+	case 'p':
+		MSG(0, "Info: Use default preen mode\n");
+		c.preen_mode = PREEN_MODE_0;
+		c.auto_fix = 1;
+		break;
+	default:
+		MSG(0, "\tError: Need argument for -%c\n", optopt);
+		fsck_usage();
+	}
+}
+
+static int is_digits(char *optarg)
+{
+	int i;
+
+	for (i = 0; i < strlen(optarg); i++)
+		if (!isdigit(optarg[i]))
+			break;
+	return i == strlen(optarg);
+}
+
 void f2fs_parse_options(int argc, char *argv[])
 {
 	int option = 0;
 	char *prog = basename(argv[0]);
+	int err = 0;
+
+	if (argc < 2) {
+		MSG(0, "\tError: Device not specified\n");
+		if (c.func == FSCK)
+			fsck_usage();
+		else if (c.func == DUMP)
+			dump_usage();
+		else if (c.func == DEFRAG)
+			defrag_usage();
+		else if (c.func == RESIZE)
+			resize_usage();
+		else if (c.func == SLOAD)
+			sload_usage();
+	}
+	c.devices[0].path = strdup(argv[argc - 1]);
+	argv[argc-- - 1] = 0;
 
 	if (!strcmp("fsck.f2fs", prog)) {
-		const char *option_string = "ad:fp:t";
+		const char *option_string = ":ad:fp:t";
 
 		c.func = FSCK;
 		while ((option = getopt(argc, argv, option_string)) != EOF) {
@@ -97,6 +139,16 @@ void f2fs_parse_options(int argc, char *argv[])
 				 *  0: default level, the same as -a
 				 *  1: check meta
 				 */
+				if (optarg[0] == '-') {
+					c.preen_mode = PREEN_MODE_0;
+					optind--;
+					break;
+				} else if (!is_digits(optarg)) {
+					MSG(0, "\tError: Wrong option -%c %s\n",
+							option, optarg);
+					err = 1;
+					break;
+				}
 				c.preen_mode = atoi(optarg);
 				if (c.preen_mode < 0)
 					c.preen_mode = PREEN_MODE_0;
@@ -108,9 +160,19 @@ void f2fs_parse_options(int argc, char *argv[])
 					"preen mode %d\n", c.preen_mode);
 				break;
 			case 'd':
+				if (optarg[0] == '-') {
+					MSG(0, "\tError: Need argument for -%c\n",
+							option);
+					err = 1;
+					break;
+				} else if (!is_digits(optarg)) {
+					MSG(0, "\tError: Wrong option -%c %s\n",
+							option, optarg);
+					err = 1;
+					break;
+				}
 				c.dbg_lv = atoi(optarg);
-				MSG(0, "Info: Debug level = %d\n",
-							c.dbg_lv);
+				MSG(0, "Info: Debug level = %d\n", c.dbg_lv);
 				break;
 			case 'f':
 				c.fix_on = 1;
@@ -119,11 +181,25 @@ void f2fs_parse_options(int argc, char *argv[])
 			case 't':
 				c.dbg_lv = -1;
 				break;
+			case ':':
+				__handle_fsck_args(optopt);
+				break;
+			case '?':
+				MSG(0, "\tError: Unknown option %c\n", optopt);
+				err = 1;
+				break;
 			default:
 				MSG(0, "\tError: Unknown option %c\n", option);
-				fsck_usage();
+				err = 1;
 				break;
 			}
+			if (err)
+				fsck_usage();
+		}
+		if (argc > optind) {
+			c.dbg_lv = 0;
+			MSG(0, "\tError: Unknown argument %s\n", argv[optind]);
+			fsck_usage();
 		}
 	} else if (!strcmp("dump.f2fs", prog)) {
 		const char *option_string = "d:i:n:s:a:b:";
@@ -287,21 +363,6 @@ void f2fs_parse_options(int argc, char *argv[])
 			}
 		}
 	}
-
-	if ((optind + 1) != argc) {
-		MSG(0, "\tError: Device not specified\n");
-		if (c.func == FSCK)
-			fsck_usage();
-		else if (c.func == DUMP)
-			dump_usage();
-		else if (c.func == DEFRAG)
-			defrag_usage();
-		else if (c.func == RESIZE)
-			resize_usage();
-		else if (c.func == SLOAD)
-			sload_usage();
-	}
-	c.devices[0].path = strdup(argv[optind]);
 }
 
 static void do_fsck(struct f2fs_sb_info *sbi)
-- 
2.11.0


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot

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

* [PATCH 2/2] fsck.f2fs: show parse errors neatly
  2017-01-21  6:00 [PATCH 1/2] fsck.f2fs: support -p without argument Jaegeuk Kim
@ 2017-01-21  6:00 ` Jaegeuk Kim
  0 siblings, 0 replies; 2+ messages in thread
From: Jaegeuk Kim @ 2017-01-21  6:00 UTC (permalink / raw)
  To: linux-f2fs-devel; +Cc: Jaegeuk Kim

This patch is just to clean up.

Cc: Sheng Yong <shengyong1@huawei.com>
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
---
 fsck/fsck.h |   8 ++++
 fsck/main.c | 140 ++++++++++++++++++++++++++++++++++++------------------------
 2 files changed, 93 insertions(+), 55 deletions(-)

diff --git a/fsck/fsck.h b/fsck/fsck.h
index f21e199..5a6f018 100644
--- a/fsck/fsck.h
+++ b/fsck/fsck.h
@@ -21,6 +21,14 @@ enum {
 	PREEN_MODE_MAX
 };
 
+enum {
+	NOERROR,
+	EWRONG_OPT,
+	ENEED_ARG,
+	EUNKNOWN_OPT,
+	EUNKNOWN_ARG,
+};
+
 /* fsck.c */
 struct orphan_info {
 	u32 nr_inodes;
diff --git a/fsck/main.c b/fsck/main.c
index 0ac1711..6c94a70 100644
--- a/fsck/main.c
+++ b/fsck/main.c
@@ -78,20 +78,6 @@ void sload_usage()
 	exit(1);
 }
 
-static void __handle_fsck_args(int optopt)
-{
-	switch (optopt) {
-	case 'p':
-		MSG(0, "Info: Use default preen mode\n");
-		c.preen_mode = PREEN_MODE_0;
-		c.auto_fix = 1;
-		break;
-	default:
-		MSG(0, "\tError: Need argument for -%c\n", optopt);
-		fsck_usage();
-	}
-}
-
 static int is_digits(char *optarg)
 {
 	int i;
@@ -102,24 +88,29 @@ static int is_digits(char *optarg)
 	return i == strlen(optarg);
 }
 
+static void error_out(void)
+{
+	if (c.func == FSCK)
+		fsck_usage();
+	else if (c.func == DUMP)
+		dump_usage();
+	else if (c.func == DEFRAG)
+		defrag_usage();
+	else if (c.func == RESIZE)
+		resize_usage();
+	else if (c.func == SLOAD)
+		sload_usage();
+}
+
 void f2fs_parse_options(int argc, char *argv[])
 {
 	int option = 0;
 	char *prog = basename(argv[0]);
-	int err = 0;
+	int err = NOERROR;
 
 	if (argc < 2) {
 		MSG(0, "\tError: Device not specified\n");
-		if (c.func == FSCK)
-			fsck_usage();
-		else if (c.func == DUMP)
-			dump_usage();
-		else if (c.func == DEFRAG)
-			defrag_usage();
-		else if (c.func == RESIZE)
-			resize_usage();
-		else if (c.func == SLOAD)
-			sload_usage();
+		error_out();
 	}
 	c.devices[0].path = strdup(argv[argc - 1]);
 	argv[argc-- - 1] = 0;
@@ -144,9 +135,7 @@ void f2fs_parse_options(int argc, char *argv[])
 					optind--;
 					break;
 				} else if (!is_digits(optarg)) {
-					MSG(0, "\tError: Wrong option -%c %s\n",
-							option, optarg);
-					err = 1;
+					err = EWRONG_OPT;
 					break;
 				}
 				c.preen_mode = atoi(optarg);
@@ -161,14 +150,10 @@ void f2fs_parse_options(int argc, char *argv[])
 				break;
 			case 'd':
 				if (optarg[0] == '-') {
-					MSG(0, "\tError: Need argument for -%c\n",
-							option);
-					err = 1;
+					err = ENEED_ARG;
 					break;
 				} else if (!is_digits(optarg)) {
-					MSG(0, "\tError: Wrong option -%c %s\n",
-							option, optarg);
-					err = 1;
+					err = EWRONG_OPT;
 					break;
 				}
 				c.dbg_lv = atoi(optarg);
@@ -181,25 +166,27 @@ void f2fs_parse_options(int argc, char *argv[])
 			case 't':
 				c.dbg_lv = -1;
 				break;
+
+
 			case ':':
-				__handle_fsck_args(optopt);
+				if (optopt == 'p') {
+					MSG(0, "Info: Use default preen mode\n");
+					c.preen_mode = PREEN_MODE_0;
+					c.auto_fix = 1;
+				} else {
+					option = optopt;
+					err = ENEED_ARG;
+					break;
+				}
 				break;
 			case '?':
-				MSG(0, "\tError: Unknown option %c\n", optopt);
-				err = 1;
-				break;
+				option = optopt;
 			default:
-				MSG(0, "\tError: Unknown option %c\n", option);
-				err = 1;
+				err = EUNKNOWN_OPT;
 				break;
 			}
-			if (err)
-				fsck_usage();
-		}
-		if (argc > optind) {
-			c.dbg_lv = 0;
-			MSG(0, "\tError: Unknown argument %s\n", argv[optind]);
-			fsck_usage();
+			if (err != NOERROR)
+				break;
 		}
 	} else if (!strcmp("dump.f2fs", prog)) {
 		const char *option_string = "d:i:n:s:a:b:";
@@ -220,6 +207,10 @@ void f2fs_parse_options(int argc, char *argv[])
 
 			switch (option) {
 			case 'd':
+				if (!is_digits(optarg)) {
+					err = EWRONG_OPT;
+					break;
+				}
 				c.dbg_lv = atoi(optarg);
 				MSG(0, "Info: Debug level = %d\n",
 							c.dbg_lv);
@@ -256,11 +247,12 @@ void f2fs_parse_options(int argc, char *argv[])
 							&dump_opt.blk_addr);
 				break;
 			default:
-				MSG(0, "\tError: Unknown option %c\n", option);
-				dump_usage();
+				err = EUNKNOWN_OPT;
 				break;
 			}
 			ASSERT(ret >= 0);
+			if (err != NOERROR)
+				break;
 		}
 
 		c.private = &dump_opt;
@@ -273,6 +265,10 @@ void f2fs_parse_options(int argc, char *argv[])
 
 			switch (option) {
 			case 'd':
+				if (!is_digits(optarg)) {
+					err = EWRONG_OPT;
+					break;
+				}
 				c.dbg_lv = atoi(optarg);
 				MSG(0, "Info: Debug level = %d\n",
 							c.dbg_lv);
@@ -305,11 +301,12 @@ void f2fs_parse_options(int argc, char *argv[])
 				c.defrag_shrink = 1;
 				break;
 			default:
-				MSG(0, "\tError: Unknown option %c\n", option);
-				defrag_usage();
+				err = EUNKNOWN_OPT;
 				break;
 			}
 			ASSERT(ret >= 0);
+			if (err != NOERROR)
+				break;
 		}
 	} else if (!strcmp("resize.f2fs", prog)) {
 		const char *option_string = "d:t:";
@@ -320,6 +317,10 @@ void f2fs_parse_options(int argc, char *argv[])
 
 			switch (option) {
 			case 'd':
+				if (!is_digits(optarg)) {
+					err = EWRONG_OPT;
+					break;
+				}
 				c.dbg_lv = atoi(optarg);
 				MSG(0, "Info: Debug level = %d\n",
 							c.dbg_lv);
@@ -333,11 +334,12 @@ void f2fs_parse_options(int argc, char *argv[])
 							&c.target_sectors);
 				break;
 			default:
-				MSG(0, "\tError: Unknown option %c\n", option);
-				resize_usage();
+				err = EUNKNOWN_OPT;
 				break;
 			}
 			ASSERT(ret >= 0);
+			if (err != NOERROR)
+				break;
 		}
 	} else if (!strcmp("sload.f2fs", prog)) {
 		const char *option_string = "d:f:t:";
@@ -346,6 +348,10 @@ void f2fs_parse_options(int argc, char *argv[])
 		while ((option = getopt(argc, argv, option_string)) != EOF) {
 			switch (option) {
 			case 'd':
+				if (!is_digits(optarg)) {
+					err = EWRONG_OPT;
+					break;
+				}
 				c.dbg_lv = atoi(optarg);
 				MSG(0, "Info: Debug level = %d\n",
 						c.dbg_lv);
@@ -357,12 +363,36 @@ void f2fs_parse_options(int argc, char *argv[])
 				c.mount_point = (char *)optarg;
 				break;
 			default:
-				MSG(0, "\tError: Unknown option %c\n", option);
-				sload_usage();
+				err = EUNKNOWN_OPT;
 				break;
 			}
+			if (err != NOERROR)
+				break;
 		}
 	}
+	if (argc > optind) {
+		c.dbg_lv = 0;
+		err = EUNKNOWN_ARG;
+	}
+	if (err == NOERROR)
+		return;
+
+	/* print out error */
+	switch (err) {
+	case EWRONG_OPT:
+		MSG(0, "\tError: Wrong option -%c %s\n", option, optarg);
+		break;
+	case ENEED_ARG:
+		MSG(0, "\tError: Need argument for -%c\n", option);
+		break;
+	case EUNKNOWN_OPT:
+		MSG(0, "\tError: Unknown option %c\n", option);
+		break;
+	case EUNKNOWN_ARG:
+		MSG(0, "\tError: Unknown argument %s\n", argv[optind]);
+		break;
+	}
+	error_out();
 }
 
 static void do_fsck(struct f2fs_sb_info *sbi)
-- 
2.11.0


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot

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

end of thread, other threads:[~2017-01-21  6:01 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-01-21  6:00 [PATCH 1/2] fsck.f2fs: support -p without argument Jaegeuk Kim
2017-01-21  6:00 ` [PATCH 2/2] fsck.f2fs: show parse errors neatly Jaegeuk Kim

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