NeilBrown: main: factor out code to parse layout for raid10 and faulty.

Martin F. Krafft madduck at alioth.debian.org
Wed Jan 27 02:02:57 UTC 2010


Module: mdadm
Branch: upstream
Commit: 4a06e2c270948bdcfaa447c8a0e80abb0978b5c4
URL:    http://git.debian.org/?p=pkg-mdadm/mdadm.git;a=commit;h=4a06e2c270948bdcfaa447c8a0e80abb0978b5c4

Author: NeilBrown <neilb at suse.de>
Date:   Tue Jul 14 11:29:20 2009 +1000

main: factor out code to parse layout for raid10 and faulty.

This will soon be called from multiple places.

Signed-off-by: NeilBrown <neilb at suse.de>

---

 mdadm.c |   27 +++++----------------------
 mdadm.h |    2 ++
 util.c  |   34 ++++++++++++++++++++++++++++++++++
 3 files changed, 41 insertions(+), 22 deletions(-)

diff --git a/mdadm.c b/mdadm.c
index 2417f10..82e2d43 100644
--- a/mdadm.c
+++ b/mdadm.c
@@ -107,7 +107,6 @@ int main(int argc, char *argv[])
 	int rebuild_map = 0;
 	int auto_update_home = 0;
 
-	int copies;
 	int print_help = 0;
 	FILE *outf;
 
@@ -478,38 +477,22 @@ int main(int argc, char *argv[])
 				break;
 
 			case 10:
-				/* 'f', 'o' or 'n' followed by a number <= raid_disks */
-				if ((optarg[0] !=  'n' && optarg[0] != 'f' && optarg[0] != 'o') ||
-				    (copies = strtoul(optarg+1, &cp, 10)) < 1 ||
-				    copies > 200 ||
-				    *cp) {
+				layout = parse_layout_10(optarg);
+				if (layout < 0) {
 					fprintf(stderr, Name ": layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg);
 					exit(2);
 				}
-				if (optarg[0] == 'n')
-					layout = 256 + copies;
-				else if (optarg[0] == 'o')
-					layout = 0x10000 + (copies<<8) + 1;
-				else
-					layout = 1 + (copies<<8);
 				break;
 			case -5: /* Faulty
 				  * modeNNN
 				  */
-
-			{
-				int ln = strcspn(optarg, "0123456789");
-				char *m = strdup(optarg);
-				int mode;
-				m[ln] = 0;
-				mode = map_name(faultylayout, m);
-				if (mode == UnSet) {
+				layout = parse_layout_faulty(optarg);
+				if (layout == -1) {
 					fprintf(stderr, Name ": layout %s not understood for faulty.\n",
 						optarg);
 					exit(2);
 				}
-				layout = mode | (atoi(optarg+ln)<< ModeShift);
-			}
+				break;
 			}
 			continue;
 
diff --git a/mdadm.h b/mdadm.h
index bf7e59d..b0a840b 100644
--- a/mdadm.h
+++ b/mdadm.h
@@ -776,6 +776,8 @@ extern int md_get_version(int fd);
 extern int get_linux_version(void);
 extern long long parse_size(char *size);
 extern int parse_uuid(char *str, int uuid[4]);
+extern int parse_layout_10(char *layout);
+extern int parse_layout_faulty(char *layout);
 extern int check_ext2(int fd, char *name);
 extern int check_reiser(int fd, char *name);
 extern int check_raid(int fd, char *name);
diff --git a/util.c b/util.c
index 080decc..c0c3f0c 100644
--- a/util.c
+++ b/util.c
@@ -185,6 +185,40 @@ long long parse_size(char *size)
 	return s;
 }
 
+int parse_layout_10(char *layout)
+{
+	int copies, rv;
+	char *cp;
+	/* Parse the layout string for raid10 */
+	/* 'f', 'o' or 'n' followed by a number <= raid_disks */
+	if ((layout[0] !=  'n' && layout[0] != 'f' && layout[0] != 'o') ||
+	    (copies = strtoul(layout+1, &cp, 10)) < 1 ||
+	    copies > 200 ||
+	    *cp)
+		return -1;
+	if (layout[0] == 'n')
+		rv = 256 + copies;
+	else if (layout[0] == 'o')
+		rv = 0x10000 + (copies<<8) + 1;
+	else
+		rv = 1 + (copies<<8);
+	return rv;
+}
+
+int parse_layout_faulty(char *layout)
+{
+	/* Parse the layout string for 'faulty' */
+	int ln = strcspn(layout, "0123456789");
+	char *m = strdup(layout);
+	int mode;
+	m[ln] = 0;
+	mode = map_name(faultylayout, m);
+	if (mode == UnSet)
+		return -1;
+
+	return mode | (atoi(layout+ln)<< ModeShift);
+}
+
 void remove_partitions(int fd)
 {
 	/* remove partitions from this block devices.




More information about the pkg-mdadm-commits mailing list