Line data Source code
1 : /*-
2 : * Copyright (c) 2011-2012 Baptiste Daroussin <bapt@FreeBSD.org>
3 : * Copyright (c) 2011-2012 Marin Atanasov Nikolov <dnaeon@gmail.com>
4 : * Copyright (c) 2014 Matthew Seaman <matthew@FreeBSD.org>
5 : * All rights reserved.
6 : *
7 : * Redistribution and use in source and binary forms, with or without
8 : * modification, are permitted provided that the following conditions
9 : * are met:
10 : * 1. Redistributions of source code must retain the above copyright
11 : * notice, this list of conditions and the following disclaimer
12 : * in this position and unchanged.
13 : * 2. Redistributions in binary form must reproduce the above copyright
14 : * notice, this list of conditions and the following disclaimer in the
15 : * documentation and/or other materials provided with the distribution.
16 : *
17 : * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
18 : * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 : * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 : * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
21 : * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 : * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 : * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 : * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 : * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 : * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 : */
28 :
29 : #include <pkg.h>
30 : #include <getopt.h>
31 : #include <sysexits.h>
32 : #include <unistd.h>
33 :
34 : #include "pkgcli.h"
35 :
36 : void
37 0 : usage_backup(void)
38 : {
39 0 : fprintf(stderr, "Usage: pkg backup [-q] -d <dest_file>\n");
40 0 : fprintf(stderr, " pkg backup [-q] -r <src_file>\n\n");
41 0 : fprintf(stderr, "For more information see 'pkg help backup'.\n");
42 0 : }
43 :
44 : int
45 0 : exec_backup(int argc, char **argv)
46 : {
47 0 : struct pkgdb *db = NULL;
48 0 : char *backup_file = NULL;
49 0 : bool dump = false;
50 0 : bool restore = false;
51 : int ch;
52 :
53 0 : struct option longopts[] = {
54 : { "dump", required_argument, NULL, 'd' },
55 : { "quiet", no_argument, NULL, 'q' },
56 : { "restore", required_argument, NULL, 'r' },
57 : { NULL, 0, NULL, 0 },
58 : };
59 :
60 0 : while ((ch = getopt_long(argc, argv, "+d:qr:", longopts, NULL)) != -1) {
61 0 : switch (ch) {
62 : case 'd':
63 0 : dump = true;
64 0 : backup_file = optarg;
65 0 : break;
66 : case 'q':
67 0 : quiet = true;
68 0 : break;
69 : case 'r':
70 0 : restore = true;
71 0 : backup_file = optarg;
72 0 : break;
73 : default:
74 0 : usage_backup();
75 0 : return (EX_USAGE);
76 : }
77 : }
78 0 : argc -= optind;
79 0 : argv += optind;
80 :
81 0 : if ( dump == restore ) {
82 0 : usage_backup();
83 0 : return (EX_USAGE);
84 : }
85 :
86 0 : if (pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK)
87 0 : return (EX_IOERR);
88 :
89 0 : if (dump) {
90 0 : if (!quiet)
91 0 : printf("Dumping database:\n");
92 0 : if (pkgdb_dump(db, backup_file) == EPKG_FATAL)
93 0 : return (EX_IOERR);
94 : }
95 :
96 0 : if (restore) {
97 0 : if (!quiet)
98 0 : printf("Restoring database:\n");
99 0 : if (pkgdb_load(db, backup_file) == EPKG_FATAL)
100 0 : return (EX_IOERR);
101 : }
102 :
103 0 : pkgdb_close(db);
104 :
105 0 : return (EX_OK);
106 : }
|