Source-Changes-HG archive

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

[src/trunk]: src/sbin/gpt Firs step of refactoring, remove all globals, facto...



details:   https://anonhg.NetBSD.org/src/rev/a2d3e5486207
branches:  trunk
changeset: 341968:a2d3e5486207
user:      christos <christos%NetBSD.org@localhost>
date:      Tue Dec 01 09:05:33 2015 +0000

description:
Firs step of refactoring, remove all globals, factor out some common code,
handle alternate usage but not advertise it.

diffstat:

 sbin/gpt/Makefile      |    7 +-
 sbin/gpt/add.c         |  160 +++++-----------
 sbin/gpt/backup.c      |   47 ++---
 sbin/gpt/biosboot.c    |  246 ++++++++++---------------
 sbin/gpt/create.c      |  200 ++++++++++----------
 sbin/gpt/destroy.c     |   65 +++---
 sbin/gpt/gpt.8         |   36 ++-
 sbin/gpt/gpt.c         |  451 ++++++++++++++++++++++++++++++++----------------
 sbin/gpt/gpt.h         |   70 ++++---
 sbin/gpt/gpt_private.h |   45 ++++
 sbin/gpt/header.c      |   75 +++----
 sbin/gpt/label.c       |   99 ++-------
 sbin/gpt/main.c        |   70 +++++--
 sbin/gpt/map.c         |   93 ++++-----
 sbin/gpt/map.h         |   24 +-
 sbin/gpt/migrate.c     |  224 +++++++++++------------
 sbin/gpt/recover.c     |  183 +++++++++----------
 sbin/gpt/remove.c      |  118 ++++--------
 sbin/gpt/resize.c      |  174 ++++++------------
 sbin/gpt/resizedisk.c  |  197 +++++++++-----------
 sbin/gpt/restore.c     |  186 +++++++++----------
 sbin/gpt/set.c         |  100 +++-------
 sbin/gpt/show.c        |   73 +++----
 sbin/gpt/type.c        |  124 ++++---------
 sbin/gpt/unset.c       |  106 +++--------
 25 files changed, 1472 insertions(+), 1701 deletions(-)

diffs (truncated from 5450 to 300 lines):

diff -r 9dbce1caeabd -r a2d3e5486207 sbin/gpt/Makefile
--- a/sbin/gpt/Makefile Tue Dec 01 08:40:34 2015 +0000
+++ b/sbin/gpt/Makefile Tue Dec 01 09:05:33 2015 +0000
@@ -1,12 +1,17 @@
-# $NetBSD: Makefile,v 1.16 2015/11/30 19:59:34 christos Exp $
+# $NetBSD: Makefile,v 1.17 2015/12/01 09:05:33 christos Exp $
 # $FreeBSD: src/sbin/gpt/Makefile,v 1.7 2005/09/01 02:49:20 marcel Exp $
 
+.include <bsd.own.mk>
+
 PROG=  gpt
 SRCS=  add.c biosboot.c create.c destroy.c gpt.c header.c label.c map.c \
        main.c migrate.c recover.c remove.c resize.c resizedisk.c \
        set.c show.c type.c unset.c gpt_uuid.c
 MAN=   gpt.8
 
+#LINKS=  ${BINDIR}/gpt ${BINDIR}/gptlabel
+#MLINKS= gpt.8 gptlabel.8
+
 .if (${HOSTPROG:U} == "")
 SRCS+= backup.c restore.c
 LDADD+=        -lprop -lutil
diff -r 9dbce1caeabd -r a2d3e5486207 sbin/gpt/add.c
--- a/sbin/gpt/add.c    Tue Dec 01 08:40:34 2015 +0000
+++ b/sbin/gpt/add.c    Tue Dec 01 09:05:33 2015 +0000
@@ -33,10 +33,12 @@
 __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.30 2015/12/01 02:03:55 christos Exp $");
+__RCSID("$NetBSD: add.c,v 1.31 2015/12/01 09:05:33 christos Exp $");
 #endif
 
 #include <sys/types.h>
+#include <sys/param.h>
+#include <sys/stat.h>
 
 #include <err.h>
 #include <stddef.h>
@@ -47,6 +49,7 @@
 
 #include "map.h"
 #include "gpt.h"
+#include "gpt_private.h"
 
 static gpt_uuid_t type;
 static off_t alignment, block, sectors, size;
@@ -54,9 +57,9 @@
 static uint8_t *name;
 
 const char addmsg1[] = "add [-a alignment] [-b blocknr] [-i index] [-l label]";
-const char addmsg2[] = "    [-s size] [-t type] device ...";
+const char addmsg2[] = "    [-s size] [-t type]";
 
-__dead static void
+static int
 usage_add(void)
 {
 
@@ -64,131 +67,89 @@
            "usage: %s %s\n"
            "       %*s %s\n", getprogname(), addmsg1,
            (int)strlen(getprogname()), "", addmsg2);
-       exit(1);
+       return -1;
 }
 
-static void
-add(int fd)
+static int
+add(gpt_t gpt)
 {
-       map_t *gpt, *tpg;
-       map_t *tbl, *lbt;
-       map_t *map;
+       map_t map;
        struct gpt_hdr *hdr;
-       struct gpt_ent *ent;
+       struct gpt_ent *ent, e;
        unsigned int i;
        off_t alignsecs;
        
+       if ((hdr = gpt_hdr(gpt)) == NULL)
+               return -1;
 
-       gpt = map_find(MAP_TYPE_PRI_GPT_HDR);
        ent = NULL;
-       if (gpt == NULL) {
-               warnx("%s: error: no primary GPT header; run create or recover",
-                   device_name);
-               return;
-       }
 
-       tpg = map_find(MAP_TYPE_SEC_GPT_HDR);
-       if (tpg == NULL) {
-               warnx("%s: error: no secondary GPT header; run recover",
-                   device_name);
-               return;
-       }
-
-       tbl = map_find(MAP_TYPE_PRI_GPT_TBL);
-       lbt = map_find(MAP_TYPE_SEC_GPT_TBL);
-       if (tbl == NULL || lbt == NULL) {
-               warnx("%s: error: run recover -- trust me", device_name);
-               return;
-       }
-
-       hdr = gpt->map_data;
        if (entry > le32toh(hdr->hdr_entries)) {
-               warnx("%s: error: index %u out of range (%u max)", device_name,
+               gpt_warnx(gpt, "index %u out of range (%u max)",
                    entry, le32toh(hdr->hdr_entries));
-               return;
+               return -1;
        }
 
        if (entry > 0) {
                i = entry - 1;
-               ent = (void*)((char*)tbl->map_data + i *
-                   le32toh(hdr->hdr_entsz));
+               ent = gpt_ent_primary(gpt, i);
                if (!gpt_uuid_is_nil(ent->ent_type)) {
-                       warnx("%s: error: entry at index %u is not free",
-                           device_name, entry);
-                       return;
+                       gpt_warnx(gpt, "Entry at index %u is not free", entry);
+                       return -1;
                }
        } else {
                /* Find empty slot in GPT table. */
                for (i = 0; i < le32toh(hdr->hdr_entries); i++) {
-                       ent = (void*)((char*)tbl->map_data + i *
-                           le32toh(hdr->hdr_entsz));
+                       ent = gpt_ent_primary(gpt, i);
                        if (gpt_uuid_is_nil(ent->ent_type))
                                break;
                }
                if (i == le32toh(hdr->hdr_entries)) {
-                       warnx("%s: error: no available table entries",
-                           device_name);
-                       return;
+                       gpt_warnx(gpt, "No available table entries");
+                       return -1;
                }
        }
 
        if (alignment > 0) {
-               alignsecs = alignment / secsz;
-               map = map_alloc(block, sectors, alignsecs);
+               alignsecs = alignment / gpt->secsz;
+               map = map_alloc(gpt, block, sectors, alignsecs);
                if (map == NULL) {
-                       warnx("%s: error: not enough space available on "
-                             "device for an aligned partition", device_name);
-                       return;
+                       gpt_warnx(gpt, "Not enough space available on "
+                             "device for an aligned partition");
+                       return -1;
                }
        } else {
-               map = map_alloc(block, sectors, 0);
+               map = map_alloc(gpt, block, sectors, 0);
                if (map == NULL) {
-                       warnx("%s: error: not enough space available on "
-                             "device", device_name);
-                       return;
+                       gpt_warnx(gpt, "Not enough space available on device");
+                       return -1;
                }
        }
 
-       gpt_uuid_copy(ent->ent_type, type);
-       ent->ent_lba_start = htole64(map->map_start);
-       ent->ent_lba_end = htole64(map->map_start + map->map_size - 1LL);
+       memset(&e, 0, sizeof(e));
+       gpt_uuid_copy(e.ent_type, type);
+       e.ent_lba_start = htole64(map->map_start);
+       e.ent_lba_end = htole64(map->map_start + map->map_size - 1LL);
        if (name != NULL)
-               utf8_to_utf16(name, ent->ent_name, 36);
-
-       hdr->hdr_crc_table = htole32(crc32(tbl->map_data,
-           le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
-       hdr->hdr_crc_self = 0;
-       hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
-
-       gpt_write(fd, gpt);
-       gpt_write(fd, tbl);
-
-       hdr = tpg->map_data;
-       ent = (void*)((char*)lbt->map_data + i * le32toh(hdr->hdr_entsz));
+               utf8_to_utf16(name, e.ent_name, 36);
 
-       gpt_uuid_copy(ent->ent_type, type);
-       ent->ent_lba_start = htole64(map->map_start);
-       ent->ent_lba_end = htole64(map->map_start + map->map_size - 1LL);
-       if (name != NULL)
-               utf8_to_utf16(name, ent->ent_name, 36);
+       memcpy(ent, &e, sizeof(e));
+       gpt_write_primary(gpt);
 
-       hdr->hdr_crc_table = htole32(crc32(lbt->map_data,
-           le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
-       hdr->hdr_crc_self = 0;
-       hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
+       ent = gpt_ent_backup(gpt, i);
+       memcpy(ent, &e, sizeof(e));
+       gpt_write_backup(gpt);
 
-       gpt_write(fd, lbt);
-       gpt_write(fd, tpg);
-
-       gpt_msg("Partition %d added: %s %" PRIu64 " %" PRIu64 "\n", i + 1,
+       gpt_msg(gpt, "Partition %d added: %s %" PRIu64 " %" PRIu64 "\n", i + 1,
            type, map->map_start, map->map_size);
+       return 0;
 }
 
 int
-cmd_add(int argc, char *argv[])
+cmd_add(gpt_t gpt, int argc, char *argv[])
 {
        char *p;
-       int ch, fd;
+       int ch;
        int64_t human_num;
 
        while ((ch = getopt(argc, argv, "a:b:i:l:s:t:")) != -1) {
@@ -257,44 +218,23 @@
                                usage_add();
                        break;
                default:
-                       usage_add();
+                       return usage_add();
                }
        }
 
        if (argc == optind)
-               usage_add();
+               return usage_add();
 
        /* Create NetBSD FFS partitions by default. */
        if (gpt_uuid_is_nil(type)) {
                gpt_uuid_create(GPT_TYPE_NETBSD_FFS, type, NULL, 0);
        }
 
-       while (optind < argc) {
-               fd = gpt_open(argv[optind++], 0);
-               if (fd == -1)
-                       continue;
-
-               if (alignment % secsz != 0) {
-                       warnx("Alignment must be a multiple of sector size;");
-                       warnx("the sector size for %s is %d bytes.",
-                           device_name, secsz);
-                       continue;
-               }
+       if (optind != argc)
+               return usage_add();
 
-               if (size % secsz != 0) {
-                       warnx("Size in bytes must be a multiple of sector "
-                             "size;");
-                       warnx("the sector size for %s is %d bytes.",
-                           device_name, secsz);
-                       continue;
-               }
-               if (size > 0)
-                       sectors = size / secsz;
+       if ((sectors = gpt_check(gpt, alignment, size)) == -1)
+               return -1;
 
-               add(fd);
-
-               gpt_close(fd);
-       }
-
-       return (0);
+       return add(gpt);
 }
diff -r 9dbce1caeabd -r a2d3e5486207 sbin/gpt/backup.c
--- a/sbin/gpt/backup.c Tue Dec 01 08:40:34 2015 +0000
+++ b/sbin/gpt/backup.c Tue Dec 01 09:05:33 2015 +0000
@@ -33,7 +33,7 @@
 __FBSDID("$FreeBSD: src/sbin/gpt/show.c,v 1.14 2006/06/22 22:22:32 marcel Exp $");
 #endif
 #ifdef __RCSID
-__RCSID("$NetBSD: backup.c,v 1.9 2015/11/29 00:14:46 christos Exp $");
+__RCSID("$NetBSD: backup.c,v 1.10 2015/12/01 09:05:33 christos Exp $");
 #endif
 
 #include <sys/bootblock.h>
@@ -49,31 +49,32 @@
 
 #include "map.h"
 #include "gpt.h"
+#include "gpt_private.h"
 
 
-const char backupmsg[] = "backup device ...";



Home | Main Index | Thread Index | Old Index