Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sbin/gpt refactor more duplicated code.



details:   https://anonhg.NetBSD.org/src/rev/dd30ac08ff58
branches:  trunk
changeset: 341983:dd30ac08ff58
user:      christos <christos%NetBSD.org@localhost>
date:      Tue Dec 01 19:25:24 2015 +0000

description:
refactor more duplicated code.

diffstat:

 sbin/gpt/add.c        |   63 +------
 sbin/gpt/create.c     |  120 +-------------
 sbin/gpt/gpt.c        |  408 +++++++++++++++++++++++++++++++++++++++++++++++--
 sbin/gpt/gpt.h        |   33 +++-
 sbin/gpt/label.c      |  126 ++-------------
 sbin/gpt/migrate.c    |  101 +-----------
 sbin/gpt/remove.c     |  120 +-------------
 sbin/gpt/resize.c     |   58 +------
 sbin/gpt/resizedisk.c |   33 +---
 sbin/gpt/set.c        |   68 +-------
 sbin/gpt/type.c       |  125 +-------------
 sbin/gpt/unset.c      |   66 +-------
 12 files changed, 514 insertions(+), 807 deletions(-)

diffs (truncated from 1787 to 300 lines):

diff -r 106da1c94bf3 -r dd30ac08ff58 sbin/gpt/add.c
--- a/sbin/gpt/add.c    Tue Dec 01 17:48:41 2015 +0000
+++ b/sbin/gpt/add.c    Tue Dec 01 19:25:24 2015 +0000
@@ -33,7 +33,7 @@
 __FBSDID("$FreeBSD: src/sbin/gpt/add.c,v 1.14 2006/06/22 22:05:28 marcel Exp $");
 #endif
 #ifdef __RCSID
-__RCSID("$NetBSD: add.c,v 1.32 2015/12/01 16:32:19 christos Exp $");
+__RCSID("$NetBSD: add.c,v 1.33 2015/12/01 19:25:24 christos Exp $");
 #endif
 
 #include <sys/types.h>
@@ -79,6 +79,7 @@
        struct gpt_ent *ent, e;
        unsigned int i;
        off_t alignsecs;
+       char buf[128];
        
        if ((hdr = gpt_hdr(gpt)) == NULL)
                return -1;
@@ -141,29 +142,20 @@
        memcpy(ent, &e, sizeof(e));
        gpt_write_backup(gpt);
 
-       gpt_msg(gpt, "Partition %d added: %s %" PRIu64 " %" PRIu64 "\n", i + 1,
-           type, map->map_start, map->map_size);
+       gpt_uuid_snprintf(buf, sizeof(buf), "%d", type);
+       gpt_msg(gpt, "Partition %d added: %s %" PRIu64 " %" PRIu64, i + 1,
+           buf, map->map_start, map->map_size);
        return 0;
 }
 
 static int
 cmd_add(gpt_t gpt, int argc, char *argv[])
 {
-       char *p;
        int ch;
        int64_t human_num;
 
-       while ((ch = getopt(argc, argv, "a:b:i:l:s:t:")) != -1) {
+       while ((ch = getopt(argc, argv, GPT_AIS "bl:t:")) != -1) {
                switch(ch) {
-               case 'a':
-                       if (alignment > 0)
-                               return usage();
-                       if (dehumanize_number(optarg, &human_num) < 0)
-                               return usage();
-                       alignment = human_num;
-                       if (alignment < 1)
-                               return usage();
-                       break;
                case 'b':
                        if (block > 0)
                                return usage();
@@ -173,45 +165,11 @@
                        if (block < 1)
                                return usage();
                        break;
-               case 'i':
-                       if (entry > 0)
-                               usage();
-                       entry = strtoul(optarg, &p, 10);
-                       if (*p != 0 || entry < 1)
-                               return usage();
-                       break;
                case 'l':
                        if (name != NULL)
                                return usage();
                        name = (uint8_t *)strdup(optarg);
                        break;
-               case 's':
-                       if (sectors > 0 || size > 0)
-                               return usage();
-                       sectors = strtoll(optarg, &p, 10);
-                       if (sectors < 1)
-                               return usage();
-                       if (*p == '\0')
-                               break;
-                       if (*p == 's' || *p == 'S') {
-                               if (*(p + 1) == '\0')
-                                       break;
-                               else
-                                       return usage();
-                       }
-                       if (*p == 'b' || *p == 'B') {
-                               if (*(p + 1) == '\0') {
-                                       size = sectors;
-                                       sectors = 0;
-                                       break;
-                               } else
-                                       return usage();
-                       }
-                       if (dehumanize_number(optarg, &human_num) < 0)
-                               return usage();
-                       size = human_num;
-                       sectors = 0;
-                       break;
                case 't':
                        if (!gpt_uuid_is_nil(type))
                                return usage();
@@ -219,11 +177,14 @@
                                return usage();
                        break;
                default:
-                       return usage();
+                       if (gpt_add_ais(gpt, &alignment, &entry, &size, ch)
+                           == -1)
+                               return usage();
+                       break;
                }
        }
 
-       if (argc == optind)
+       if (argc != optind)
                return usage();
 
        /* Create NetBSD FFS partitions by default. */
@@ -234,7 +195,7 @@
        if (optind != argc)
                return usage();
 
-       if ((sectors = gpt_check(gpt, alignment, size)) == -1)
+       if ((sectors = gpt_check_ais(gpt, alignment, entry, size)) == -1)
                return -1;
 
        return add(gpt);
diff -r 106da1c94bf3 -r dd30ac08ff58 sbin/gpt/create.c
--- a/sbin/gpt/create.c Tue Dec 01 17:48:41 2015 +0000
+++ b/sbin/gpt/create.c Tue Dec 01 19:25:24 2015 +0000
@@ -33,7 +33,7 @@
 __FBSDID("$FreeBSD: src/sbin/gpt/create.c,v 1.11 2005/08/31 01:47:19 marcel Exp $");
 #endif
 #ifdef __RCSID
-__RCSID("$NetBSD: create.c,v 1.15 2015/12/01 16:32:19 christos Exp $");
+__RCSID("$NetBSD: create.c,v 1.16 2015/12/01 19:25:24 christos Exp $");
 #endif
 
 #include <sys/types.h>
@@ -74,21 +74,10 @@
 static int
 create(gpt_t gpt)
 {
-       off_t blocks, last;
+       off_t last = gpt_last(gpt);
        map_t map;
        struct mbr *mbr;
-       struct gpt_hdr *hdr;
-       struct gpt_ent *ent;
-       unsigned int i;
-       void *p;
 
-       last = gpt->mediasz / gpt->secsz - 1LL;
-
-       if (map_find(gpt, MAP_TYPE_PRI_GPT_HDR) != NULL ||
-           map_find(gpt, MAP_TYPE_SEC_GPT_HDR) != NULL) {
-               gpt_warnx(gpt, "Device already contains a GPT");
-               return -1;
-       }
        map = map_find(gpt, MAP_TYPE_MBR);
        if (map != NULL) {
                if (!force) {
@@ -117,110 +106,21 @@
                gpt_create_pmbr_part(mbr->mbr_part, last);
 
                map = map_add(gpt, 0LL, 1LL, MAP_TYPE_PMBR, mbr);
-               gpt_write(gpt, map);
-       }
-
-       /* Get the amount of free space after the MBR */
-       blocks = map_free(gpt, 1LL, 0LL);
-       if (blocks == 0LL) {
-               gpt_warnx(gpt, "No room for the GPT header");
-               return -1;
-       }
-
-       /* Don't create more than parts entries. */
-       if ((uint64_t)(blocks - 1) * gpt->secsz >
-           parts * sizeof(struct gpt_ent)) {
-               blocks = (parts * sizeof(struct gpt_ent)) / gpt->secsz;
-               if ((parts * sizeof(struct gpt_ent)) % gpt->secsz)
-                       blocks++;
-               blocks++;               /* Don't forget the header itself */
-       }
-
-       /* Never cross the median of the device. */
-       if ((blocks + 1LL) > ((last + 1LL) >> 1))
-               blocks = ((last + 1LL) >> 1) - 1LL;
-
-       /*
-        * Get the amount of free space at the end of the device and
-        * calculate the size for the GPT structures.
-        */
-       map = map_last(gpt);
-       if (map->map_type != MAP_TYPE_UNUSED) {
-               gpt_warnx(gpt, "No room for the backup header");
-               return -1;
-       }
-
-       if (map->map_size < blocks)
-               blocks = map->map_size;
-       if (blocks == 1LL) {
-               gpt_warnx(gpt, "No room for the GPT table");
-               return -1;
+               if (gpt_write(gpt, map) == -1) {
+                       gpt_warn(gpt, "Can't write PMBR");
+                       return -1;
+               }
        }
 
-       if ((p = calloc(1, gpt->secsz)) == NULL) {
-               gpt_warnx(gpt, "Can't allocate the GPT");
-               return -1;
-       }
-       if ((gpt->gpt = map_add(gpt, 1LL, 1LL,
-           MAP_TYPE_PRI_GPT_HDR, p)) == NULL) {
-               free(p);
-               gpt_warnx(gpt, "Can't add the GPT");
-               return -1;
-       }
-
-       blocks--;               /* Number of blocks in the GPT table. */
-       if ((p = calloc(blocks, gpt->secsz)) == NULL) {
-               gpt_warnx(gpt, "Can't allocate the GPT table");
-               return -1;
-       }
-       if ((gpt->tbl = map_add(gpt, 2LL, blocks,
-           MAP_TYPE_PRI_GPT_TBL, p)) == NULL) {
-               free(p);
-               gpt_warnx(gpt, "Can't add the GPT table");
+       if (gpt_create(gpt, last, parts, primary_only) == -1)
                return -1;
-       }
-
-       hdr = gpt->gpt->map_data;
-       memcpy(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig));
-       hdr->hdr_revision = htole32(GPT_HDR_REVISION);
-       hdr->hdr_size = htole32(GPT_HDR_SIZE);
-       hdr->hdr_lba_self = htole64(gpt->gpt->map_start);
-       hdr->hdr_lba_alt = htole64(last);
-       hdr->hdr_lba_start = htole64(gpt->tbl->map_start + blocks);
-       hdr->hdr_lba_end = htole64(last - blocks - 1LL);
-       gpt_uuid_generate(hdr->hdr_guid);
-       hdr->hdr_lba_table = htole64(gpt->tbl->map_start);
-       hdr->hdr_entries = htole32((blocks * gpt->secsz) /
-           sizeof(struct gpt_ent));
-       if (le32toh(hdr->hdr_entries) > parts)
-               hdr->hdr_entries = htole32(parts);
-       hdr->hdr_entsz = htole32(sizeof(struct gpt_ent));
-
-       ent = gpt->tbl->map_data;
-       for (i = 0; i < le32toh(hdr->hdr_entries); i++) {
-               gpt_uuid_generate(ent[i].ent_guid);
-       }
 
        if (gpt_write_primary(gpt) == -1)
                return -1;
 
-       /*
-        * Create backup GPT if the user didn't suppress it.
-        */
-       if (!primary_only) {
-               // XXX: error checks 
-               gpt->tpg = map_add(gpt, last, 1LL, MAP_TYPE_SEC_GPT_HDR,
-                   calloc(1, gpt->secsz));
-               gpt->lbt = map_add(gpt, last - blocks, blocks,
-                   MAP_TYPE_SEC_GPT_TBL, gpt->tbl->map_data);
-               memcpy(gpt->tpg->map_data, gpt->gpt->map_data, gpt->secsz);
-               hdr = gpt->tpg->map_data;
-               hdr->hdr_lba_self = htole64(gpt->tpg->map_start);
-               hdr->hdr_lba_alt = htole64(gpt->gpt->map_start);
-               hdr->hdr_lba_table = htole64(gpt->lbt->map_start);
-               if (gpt_write_backup(gpt) == -1)
-                       return -1;
-       }
+       if (!primary_only && gpt_write_backup(gpt) == -1)
+               return -1;
+
        return 0;
 }
 
diff -r 106da1c94bf3 -r dd30ac08ff58 sbin/gpt/gpt.c
--- a/sbin/gpt/gpt.c    Tue Dec 01 17:48:41 2015 +0000
+++ b/sbin/gpt/gpt.c    Tue Dec 01 19:25:24 2015 +0000
@@ -35,7 +35,7 @@
 __FBSDID("$FreeBSD: src/sbin/gpt/gpt.c,v 1.16 2006/07/07 02:44:23 marcel Exp $");
 #endif
 #ifdef __RCSID
-__RCSID("$NetBSD: gpt.c,v 1.50 2015/12/01 16:32:19 christos Exp $");
+__RCSID("$NetBSD: gpt.c,v 1.51 2015/12/01 19:25:24 christos Exp $");
 #endif
 
 #include <sys/param.h>
@@ -712,24 +712,7 @@
        }
 }
 
-off_t
-gpt_check(gpt_t gpt, off_t alignment, off_t size)
-{
-       if (alignment % gpt->secsz != 0) {



Home | Main Index | Thread Index | Old Index