Source-Changes-HG archive

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

[src/trunk]: src/sbin/gpt eliminate static globals so that commands can be re...



details:   https://anonhg.NetBSD.org/src/rev/84131ae6a80f
branches:  trunk
changeset: 812159:84131ae6a80f
user:      christos <christos%NetBSD.org@localhost>
date:      Thu Dec 03 01:07:28 2015 +0000

description:
eliminate static globals so that commands can be re-used.

diffstat:

 sbin/gpt/add.c        |   21 ++++---
 sbin/gpt/backup.c     |   17 +++---
 sbin/gpt/biosboot.c   |   67 ++++++++++++--------------
 sbin/gpt/create.c     |   16 ++---
 sbin/gpt/destroy.c    |   12 ++--
 sbin/gpt/header.c     |    4 +-
 sbin/gpt/label.c      |    7 +-
 sbin/gpt/main.c       |   10 ++--
 sbin/gpt/migrate.c    |   17 ++----
 sbin/gpt/recover.c    |   11 ++--
 sbin/gpt/remove.c     |    6 +-
 sbin/gpt/resize.c     |   13 ++--
 sbin/gpt/resizedisk.c |   10 ++--
 sbin/gpt/restore.c    |   17 +++---
 sbin/gpt/set.c        |    4 +-
 sbin/gpt/show.c       |  122 ++++++++++++++++++++++++-------------------------
 sbin/gpt/type.c       |    7 +-
 sbin/gpt/unset.c      |    4 +-
 18 files changed, 178 insertions(+), 187 deletions(-)

diffs (truncated from 1081 to 300 lines):

diff -r 49d7d8499d41 -r 84131ae6a80f sbin/gpt/add.c
--- a/sbin/gpt/add.c    Thu Dec 03 00:28:55 2015 +0000
+++ b/sbin/gpt/add.c    Thu Dec 03 01:07:28 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.37 2015/12/02 11:20:34 jnemeth Exp $");
+__RCSID("$NetBSD: add.c,v 1.38 2015/12/03 01:07:28 christos Exp $");
 #endif
 
 #include <sys/types.h>
@@ -51,15 +51,11 @@
 #include "gpt.h"
 #include "gpt_private.h"
 
-static gpt_uuid_t type;
-static off_t alignment, block, sectors, size;
-static unsigned int entry;
-static uint8_t *name;
 static int cmd_add(gpt_t, int, char *[]);
 
 static const char *addhelp[] = {
-    "[-a alignment] [-b blocknr] [-i index] [-l label]",
-    "[-s size] [-t type]",
+       "[-a alignment] [-b blocknr] [-i index] [-l label]",
+       "[-s size] [-t type]",
 };
 
 struct gpt_cmd c_add = {
@@ -84,7 +80,8 @@
 }
 
 static int
-add(gpt_t gpt)
+add(gpt_t gpt, off_t alignment, off_t block, off_t sectors, off_t size,
+    u_int entry, uint8_t *name, gpt_uuid_t type)
 {
        map_t map;
        struct gpt_hdr *hdr;
@@ -157,6 +154,12 @@
 cmd_add(gpt_t gpt, int argc, char *argv[])
 {
        int ch;
+       off_t alignment = 0, block = 0, sectors = 0, size = 0;
+       unsigned int entry = 0;
+       uint8_t *name = NULL;
+       gpt_uuid_t type;
+
+       gpt_uuid_copy(type, gpt_uuid_nil);
 
        while ((ch = getopt(argc, argv, GPT_AIS "b:l:t:")) != -1) {
                switch(ch) {
@@ -193,5 +196,5 @@
        if ((sectors = gpt_check_ais(gpt, alignment, ~0, size)) == -1)
                return -1;
 
-       return add(gpt);
+       return add(gpt, alignment, block, sectors, size, entry, name, type);
 }
diff -r 49d7d8499d41 -r 84131ae6a80f sbin/gpt/backup.c
--- a/sbin/gpt/backup.c Thu Dec 03 00:28:55 2015 +0000
+++ b/sbin/gpt/backup.c Thu Dec 03 01:07:28 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.13 2015/12/02 12:36:53 christos Exp $");
+__RCSID("$NetBSD: backup.c,v 1.14 2015/12/03 01:07:28 christos Exp $");
 #endif
 
 #include <sys/bootblock.h>
@@ -51,10 +51,8 @@
 #include "gpt.h"
 #include "gpt_private.h"
 
-static const char *outfile = "/dev/stdout";
-
 static const char *backuphelp[] = {
-    "[-o outfile]",
+       "[-o outfile]",
 };
 
 static int cmd_backup(gpt_t, int, char *[]);
@@ -241,7 +239,7 @@
 }
 
 static int
-backup(gpt_t gpt)
+backup(gpt_t gpt, const char *outfile)
 {
        map_t m;
        struct mbr *mbr;
@@ -309,12 +307,14 @@
        propext = prop_dictionary_externalize(props);
        PROP_ERR(propext);
        prop_object_release(props);
-       if ((fp = fopen(outfile, "w")) == NULL) {
+       fp = strcmp(outfile, "-") == 0 ? stdout : fopen(outfile, "w");
+       if (fp == NULL) {
                gpt_warn(gpt, "Can't open `%s'", outfile);
                return -1;
        }
        fputs(propext, fp);
-       fclose(fp);
+       if (fp != stdin)
+               fclose(fp);
        free(propext);
        return 0;
 }
@@ -323,6 +323,7 @@
 cmd_backup(gpt_t gpt, int argc, char *argv[])
 {
        int ch;
+       const char *outfile = "-";
 
        while ((ch = getopt(argc, argv, "o:")) != -1) {
                switch(ch) {
@@ -336,5 +337,5 @@
        if (argc != optind)
                return usage();
 
-       return backup(gpt);
+       return backup(gpt, outfile);
 }
diff -r 49d7d8499d41 -r 84131ae6a80f sbin/gpt/biosboot.c
--- a/sbin/gpt/biosboot.c       Thu Dec 03 00:28:55 2015 +0000
+++ b/sbin/gpt/biosboot.c       Thu Dec 03 01:07:28 2015 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: biosboot.c,v 1.20 2015/12/02 12:24:02 christos Exp $ */
+/*     $NetBSD: biosboot.c,v 1.21 2015/12/03 01:07:28 christos Exp $ */
 
 /*
  * Copyright (c) 2009 The NetBSD Foundation, Inc.
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #ifdef __RCSID
-__RCSID("$NetBSD: biosboot.c,v 1.20 2015/12/02 12:24:02 christos Exp $");
+__RCSID("$NetBSD: biosboot.c,v 1.21 2015/12/03 01:07:28 christos Exp $");
 #endif
 
 #include <sys/stat.h>
@@ -65,20 +65,13 @@
 #define DEFAULT_BOOTDIR                "/usr/mdec"
 #define DEFAULT_BOOTCODE       "gptmbr.bin"
 
-static daddr_t start;
-static uint64_t size;
-
-static char *bootpath;
-static unsigned int entry;
-static uint8_t *label;
-
 static int cmd_biosboot(gpt_t, int, char *[]);
 
 static const char *biosboothelp[] = {
-    "[-c bootcode] [-i index] [-L label]",
+       "[-c bootcode] [-i index] [-L label]",
 #if notyet
-    "[-a alignment] [-b blocknr] [-i index] [-l label]",
-    "[-s size] [-t type]",
+       "[-a alignment] [-b blocknr] [-i index] [-l label]",
+       "[-s size] [-t type]",
 #endif
 };
 
@@ -92,25 +85,26 @@
 #define usage() gpt_usage(NULL, &c_biosboot)
 
 static struct mbr*
-read_boot(gpt_t gpt)
+read_boot(gpt_t gpt, const char *bootpath)
 {
-       int bfd, ret = 0;
+       int bfd, ret = -1;
        struct mbr *buf;
        struct stat st;
+       char *bp;
 
        buf = NULL;
        bfd = -1;
 
        if (bootpath == NULL)
-               bootpath = strdup(DEFAULT_BOOTDIR "/" DEFAULT_BOOTCODE);
+               bp = strdup(DEFAULT_BOOTDIR "/" DEFAULT_BOOTCODE);
        else if (*bootpath == '/')
-               bootpath = strdup(bootpath);
+               bp = strdup(bootpath);
        else {
-               if (asprintf(&bootpath, "%s/%s", DEFAULT_BOOTDIR, bootpath) < 0)
-                       bootpath = NULL;
+               if (asprintf(&bp, "%s/%s", DEFAULT_BOOTDIR, bootpath) < 0)
+                       bp = NULL;
        }
 
-       if (bootpath == NULL) {
+       if (bp == NULL) {
                gpt_warn(gpt, "Can't allocate memory for bootpath");
                goto fail;
        }
@@ -121,31 +115,31 @@
        }
 
 
-       if ((bfd = open(bootpath, O_RDONLY)) < 0 || fstat(bfd, &st) == -1) {
-               gpt_warn(gpt, "Can't open `%s'", bootpath);
+       if ((bfd = open(bp, O_RDONLY)) < 0 || fstat(bfd, &st) == -1) {
+               gpt_warn(gpt, "Can't open `%s'", bp);
                goto fail;
        }
 
        if (st.st_size != MBR_DSN_OFFSET) {
                gpt_warnx(gpt, "The bootcode in `%s' does not match the"
-                   " expected size %u", bootpath, MBR_DSN_OFFSET);
+                   " expected size %u", bp, MBR_DSN_OFFSET);
                goto fail;
        }
 
        if (read(bfd, buf, st.st_size) != st.st_size) {
-               gpt_warn(gpt, "Error reading from `%s'", bootpath);
+               gpt_warn(gpt, "Error reading from `%s'", bp);
                goto fail;
        }
 
-       ret++;
-
-    fail:
+       ret = 0;
+fail:
        if (bfd != -1)
                close(bfd);
-       if (ret == 0) {
+       if (ret == -1) {
                free(buf);
                buf = NULL;
        }
+       free(bp);
        return buf;
 }
 
@@ -169,7 +163,8 @@
 }
 
 static int
-biosboot(gpt_t gpt)
+biosboot(gpt_t gpt, daddr_t start, uint64_t size, u_int entry, uint8_t *label,
+    const char *bootpath)
 {
        map_t mbrmap, m;
        struct mbr *mbr, *bootcode;
@@ -194,7 +189,7 @@
        /*
         * Update the boot code
         */
-       if ((bootcode = read_boot(gpt)) == NULL) {
+       if ((bootcode = read_boot(gpt, bootpath)) == NULL) {
                gpt_warnx(gpt, "Error reading bootcode");
                return -1;
        }
@@ -256,9 +251,13 @@
 #ifdef DIOCGWEDGEINFO
        struct dkwedge_info dkw;
 #endif
-       char *dev;
        int ch;
        gpt_t ngpt = gpt;
+       daddr_t start = 0;
+       uint64_t size = 0;
+       unsigned int entry = 0;
+       uint8_t *label = NULL;
+       const char *bootpath = NULL;
 
        while ((ch = getopt(argc, argv, "c:i:L:")) != -1) {
                switch(ch) {
@@ -282,25 +281,21 @@
        if (argc != optind)
                return usage();
 
-       start = 0;
-       size = 0;
-
 #ifdef DIOCGWEDGEINFO
        if ((gpt->sb.st_mode & S_IFMT) != S_IFREG &&
            ioctl(gpt->fd, DIOCGWEDGEINFO, &dkw) != -1) {
                if (entry > 0)
                        /* wedges and indexes are mutually exclusive */
                        return usage();
-               dev = dkw.dkw_parent;
                start = dkw.dkw_offset;
                size = dkw.dkw_size;
-               ngpt = gpt_open(dev, gpt->flags, gpt->verbose,
+               ngpt = gpt_open(dkw.dkw_parent, gpt->flags, gpt->verbose,
                    gpt->mediasz, gpt->secsz);
                if (ngpt == NULL)
                        return -1;
        }
 #endif
-       biosboot(ngpt);
+       biosboot(ngpt, start, size, entry, label, bootpath);



Home | Main Index | Thread Index | Old Index