Source-Changes-HG archive

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

[src/trunk]: src/sbin/gpt Allow backup and restore to operate on files.



details:   https://anonhg.NetBSD.org/src/rev/83af82aeddb4
branches:  trunk
changeset: 812150:83af82aeddb4
user:      christos <christos%NetBSD.org@localhost>
date:      Wed Dec 02 12:36:53 2015 +0000

description:
Allow backup and restore to operate on files.

diffstat:

 sbin/gpt/backup.c  |  24 +++++++++++++++++++++---
 sbin/gpt/gpt.8     |  16 ++++++++++------
 sbin/gpt/restore.c |  13 +++++++++----
 3 files changed, 40 insertions(+), 13 deletions(-)

diffs (156 lines):

diff -r a9d1e5bfd39c -r 83af82aeddb4 sbin/gpt/backup.c
--- a/sbin/gpt/backup.c Wed Dec 02 12:24:02 2015 +0000
+++ b/sbin/gpt/backup.c Wed Dec 02 12:36:53 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.12 2015/12/02 04:06:47 christos Exp $");
+__RCSID("$NetBSD: backup.c,v 1.13 2015/12/02 12:36:53 christos Exp $");
 #endif
 
 #include <sys/bootblock.h>
@@ -51,9 +51,10 @@
 #include "gpt.h"
 #include "gpt_private.h"
 
+static const char *outfile = "/dev/stdout";
 
 static const char *backuphelp[] = {
-    "",
+    "[-o outfile]",
 };
 
 static int cmd_backup(gpt_t, int, char *[]);
@@ -251,6 +252,7 @@
        prop_number_t propnum;
        char *propext;
        bool rc;
+       FILE *fp;
 
        props = prop_dictionary_create();
        PROP_ERR(props);
@@ -307,7 +309,12 @@
        propext = prop_dictionary_externalize(props);
        PROP_ERR(propext);
        prop_object_release(props);
-       fputs(propext, stdout);
+       if ((fp = fopen(outfile, "w")) == NULL) {
+               gpt_warn(gpt, "Can't open `%s'", outfile);
+               return -1;
+       }
+       fputs(propext, fp);
+       fclose(fp);
        free(propext);
        return 0;
 }
@@ -315,6 +322,17 @@
 static int
 cmd_backup(gpt_t gpt, int argc, char *argv[])
 {
+       int ch;
+
+       while ((ch = getopt(argc, argv, "o:")) != -1) {
+               switch(ch) {
+               case 'o':
+                       outfile = optarg;
+                       break;
+               default:
+                       return usage();
+               }
+       }
        if (argc != optind)
                return usage();
 
diff -r a9d1e5bfd39c -r 83af82aeddb4 sbin/gpt/gpt.8
--- a/sbin/gpt/gpt.8    Wed Dec 02 12:24:02 2015 +0000
+++ b/sbin/gpt/gpt.8    Wed Dec 02 12:36:53 2015 +0000
@@ -1,4 +1,4 @@
-.\" $NetBSD: gpt.8,v 1.40 2015/12/01 22:49:25 christos Exp $
+.\" $NetBSD: gpt.8,v 1.41 2015/12/02 12:36:53 christos Exp $
 .\"
 .\" Copyright (c) 2002 Marcel Moolenaar
 .\" All rights reserved.
@@ -199,11 +199,13 @@
 .El
 as aliases for the most commonly used partition types.
 .\" ==== backup ====
-.It Nm Ic backup
+.It Nm Ic backup Oo Fl o Ar outfile Oc
 The
 .Ic backup
 command dumps the MBR or (PMBR) and GPT partition tables to standard
-output in a format to be used by the
+output or to a file specified by the
+.Ar outfile
+argument in a format to be used by the
 .Ic restore
 command.
 The format is a plist.
@@ -465,14 +467,16 @@
 option allows you to move the backup copy prior to resizing the medium.
 This is primarily useful when shrinking the medium.
 .\" ==== restore ====
-.It Nm Ic restore Oo Fl F Oc
+.It Nm Ic restore Oo Fl F Oc Oo Fl i Ar infile Oc
 The
 .Ic restore
 command restores a partition table that was previously saved using the
 .Ic backup
 command.
-The partition table is read from standard input and is expected to be in
-the format of a plist.
+The partition table is read from standard input or a file specified in
+the
+.Ar infile
+argument and is expected to be in the format of a plist.
 It assumes an empty disk.
 The
 .Fl F
diff -r a9d1e5bfd39c -r 83af82aeddb4 sbin/gpt/restore.c
--- a/sbin/gpt/restore.c        Wed Dec 02 12:24:02 2015 +0000
+++ b/sbin/gpt/restore.c        Wed Dec 02 12:36:53 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: restore.c,v 1.12 2015/12/02 04:07:11 christos Exp $");
+__RCSID("$NetBSD: restore.c,v 1.13 2015/12/02 12:36:53 christos Exp $");
 #endif
 
 #include <sys/types.h>
@@ -57,9 +57,11 @@
 static int cmd_restore(gpt_t, int, char *[]);
 
 static const char *restorehelp[] = {
-    "[-F]",
+    "[-F] [-i <infile>]",
 };
 
+static const char *infile = "/dev/stdin";
+
 struct gpt_cmd c_restore = {
        "restore",
        cmd_restore,
@@ -113,7 +115,7 @@
                map->map_type = MAP_TYPE_UNUSED;
        }
 
-       props = prop_dictionary_internalize_from_file("/dev/stdin");
+       props = prop_dictionary_internalize_from_file(infile);
        if (props == NULL) {
                gpt_warnx(gpt, "Unable to read/parse backup file");
                return -1;
@@ -396,8 +398,11 @@
 {
        int ch;
 
-       while ((ch = getopt(argc, argv, "F")) != -1) {
+       while ((ch = getopt(argc, argv, "Fi:")) != -1) {
                switch(ch) {
+               case 'i':
+                       infile = optarg;
+                       break;
                case 'F':
                        force = 1;
                        break;



Home | Main Index | Thread Index | Old Index