main: factor out code to parse layout for raid10 and faulty.
authorNeilBrown <neilb@suse.de>
Tue, 14 Jul 2009 01:29:20 +0000 (11:29 +1000)
committerNeilBrown <neilb@suse.de>
Tue, 14 Jul 2009 01:29:20 +0000 (11:29 +1000)
This will soon be called from multiple places.

Signed-off-by: NeilBrown <neilb@suse.de>
mdadm.c
mdadm.h
util.c

diff --git a/mdadm.c b/mdadm.c
index 2417f10..82e2d43 100644 (file)
--- 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 (file)
--- 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 (file)
--- 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.