6112288211e37682df668ff0729802a9f69b4ae6
[dragonfly.git] / sbin / restore / main.c
1 /*
2  * Copyright (c) 1983, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#) Copyright (c) 1983, 1993 The Regents of the University of California.  All rights reserved.
34  * @(#)main.c   8.6 (Berkeley) 5/4/95
35  * $FreeBSD: src/sbin/restore/main.c,v 1.10.2.3 2001/10/02 08:30:17 cjc Exp $
36  * $DragonFly: src/sbin/restore/main.c,v 1.2 2003/06/17 04:27:34 dillon Exp $
37  */
38
39 #include <sys/param.h>
40 #include <sys/stat.h>
41
42 #include <ufs/ufs/dinode.h>
43 #include <protocols/dumprestore.h>
44
45 #include <err.h>
46 #include <paths.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51
52 #include "restore.h"
53 #include "extern.h"
54
55 int     bflag = 0, cvtflag = 0, dflag = 0, vflag = 0, yflag = 0;
56 int     hflag = 1, mflag = 1, Nflag = 0;
57 int     uflag = 0;
58 int     dokerberos = 0;
59 char    command = '\0';
60 long    dumpnum = 1;
61 long    volno = 0;
62 long    ntrec;
63 char    *dumpmap;
64 char    *usedinomap;
65 ino_t   maxino;
66 time_t  dumptime;
67 time_t  dumpdate;
68 FILE    *terminal;
69
70 static void obsolete __P((int *, char **[]));
71 static void usage __P((void));
72
73 int
74 main(argc, argv)
75         int argc;
76         char *argv[];
77 {
78         int ch;
79         ino_t ino;
80         char *inputdev;
81         char *symtbl = "./restoresymtable";
82         char *p, name[MAXPATHLEN];
83
84         /* Temp files should *not* be readable.  We set permissions later. */
85         (void) umask(077);
86
87         if (argc < 2)
88                 usage();
89
90         if ((inputdev = getenv("TAPE")) == NULL)
91                 inputdev = _PATH_DEFTAPE;
92         obsolete(&argc, &argv);
93 #ifdef KERBEROS
94 #define optlist "b:cdf:hikmNRrs:tuvxy"
95 #else
96 #define optlist "b:cdf:himNRrs:tuvxy"
97 #endif
98         while ((ch = getopt(argc, argv, optlist)) != -1)
99                 switch(ch) {
100                 case 'b':
101                         /* Change default tape blocksize. */
102                         bflag = 1;
103                         ntrec = strtol(optarg, &p, 10);
104                         if (*p)
105                                 errx(1, "illegal blocksize -- %s", optarg);
106                         if (ntrec <= 0)
107                                 errx(1, "block size must be greater than 0");
108                         break;
109                 case 'c':
110                         cvtflag = 1;
111                         break;
112                 case 'd':
113                         dflag = 1;
114                         break;
115                 case 'f':
116                         inputdev = optarg;
117                         break;
118                 case 'h':
119                         hflag = 0;
120                         break;
121 #ifdef KERBEROS
122                 case 'k':
123                         dokerberos = 1;
124                         break;
125 #endif
126                 case 'i':
127                 case 'R':
128                 case 'r':
129                 case 't':
130                 case 'x':
131                         if (command != '\0')
132                                 errx(1,
133                                     "%c and %c options are mutually exclusive",
134                                     ch, command);
135                         command = ch;
136                         break;
137                 case 'm':
138                         mflag = 0;
139                         break;
140                 case 'N':
141                         Nflag = 1;
142                         break;
143                 case 's':
144                         /* Dumpnum (skip to) for multifile dump tapes. */
145                         dumpnum = strtol(optarg, &p, 10);
146                         if (*p)
147                                 errx(1, "illegal dump number -- %s", optarg);
148                         if (dumpnum <= 0)
149                                 errx(1, "dump number must be greater than 0");
150                         break;
151                 case 'u':
152                         uflag = 1;
153                         break;
154                 case 'v':
155                         vflag = 1;
156                         break;
157                 case 'y':
158                         yflag = 1;
159                         break;
160                 default:
161                         usage();
162                 }
163         argc -= optind;
164         argv += optind;
165
166         if (command == '\0')
167                 errx(1, "none of i, R, r, t or x options specified");
168
169         if (signal(SIGINT, onintr) == SIG_IGN)
170                 (void) signal(SIGINT, SIG_IGN);
171         if (signal(SIGTERM, onintr) == SIG_IGN)
172                 (void) signal(SIGTERM, SIG_IGN);
173         setlinebuf(stderr);
174
175         setinput(inputdev);
176
177         if (argc == 0) {
178                 argc = 1;
179                 *--argv = ".";
180         }
181
182         switch (command) {
183         /*
184          * Interactive mode.
185          */
186         case 'i':
187                 setup();
188                 extractdirs(1);
189                 initsymtable(NULL);
190                 runcmdshell();
191                 break;
192         /*
193          * Incremental restoration of a file system.
194          */
195         case 'r':
196                 setup();
197                 if (dumptime > 0) {
198                         /*
199                          * This is an incremental dump tape.
200                          */
201                         vprintf(stdout, "Begin incremental restore\n");
202                         initsymtable(symtbl);
203                         extractdirs(1);
204                         removeoldleaves();
205                         vprintf(stdout, "Calculate node updates.\n");
206                         treescan(".", ROOTINO, nodeupdates);
207                         findunreflinks();
208                         removeoldnodes();
209                 } else {
210                         /*
211                          * This is a level zero dump tape.
212                          */
213                         vprintf(stdout, "Begin level 0 restore\n");
214                         initsymtable((char *)0);
215                         extractdirs(1);
216                         vprintf(stdout, "Calculate extraction list.\n");
217                         treescan(".", ROOTINO, nodeupdates);
218                 }
219                 createleaves(symtbl);
220                 createlinks();
221                 setdirmodes(FORCE);
222                 checkrestore();
223                 if (dflag) {
224                         vprintf(stdout, "Verify the directory structure\n");
225                         treescan(".", ROOTINO, verifyfile);
226                 }
227                 dumpsymtable(symtbl, (long)1);
228                 break;
229         /*
230          * Resume an incremental file system restoration.
231          */
232         case 'R':
233                 initsymtable(symtbl);
234                 skipmaps();
235                 skipdirs();
236                 createleaves(symtbl);
237                 createlinks();
238                 setdirmodes(FORCE);
239                 checkrestore();
240                 dumpsymtable(symtbl, (long)1);
241                 break;
242         /*
243          * List contents of tape.
244          */
245         case 't':
246                 setup();
247                 extractdirs(0);
248                 initsymtable((char *)0);
249                 while (argc--) {
250                         canon(*argv++, name, sizeof(name));
251                         ino = dirlookup(name);
252                         if (ino == 0)
253                                 continue;
254                         treescan(name, ino, listfile);
255                 }
256                 break;
257         /*
258          * Batch extraction of tape contents.
259          */
260         case 'x':
261                 setup();
262                 extractdirs(1);
263                 initsymtable((char *)0);
264                 while (argc--) {
265                         canon(*argv++, name, sizeof(name));
266                         ino = dirlookup(name);
267                         if (ino == 0)
268                                 continue;
269                         if (mflag)
270                                 pathcheck(name);
271                         treescan(name, ino, addfile);
272                 }
273                 createfiles();
274                 createlinks();
275                 setdirmodes(0);
276                 if (dflag)
277                         checkrestore();
278                 break;
279         }
280         done(0);
281         /* NOTREACHED */
282 }
283
284 static void
285 usage()
286 {
287         (void)fprintf(stderr, "usage:\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n",
288           "restore -i [-cdhkmNuvy] [-b blocksize] [-f file] [-s fileno]",
289           "restore -r [-cdkNuvy] [-b blocksize] [-f file] [-s fileno]",
290           "restore -R [-cdkNuvy] [-b blocksize] [-f file] [-s fileno]",
291           "restore -x [-cdhkmNuvy] [-b blocksize] [-f file] [-s fileno] [file ...]",
292           "restore -t [-cdhkNuvy] [-b blocksize] [-f file] [-s fileno] [file ...]");
293         done(1);
294 }
295
296 /*
297  * obsolete --
298  *      Change set of key letters and ordered arguments into something
299  *      getopt(3) will like.
300  */
301 static void
302 obsolete(argcp, argvp)
303         int *argcp;
304         char **argvp[];
305 {
306         int argc, flags;
307         char *ap, **argv, *flagsp, **nargv, *p;
308
309         /* Setup. */
310         argv = *argvp;
311         argc = *argcp;
312
313         /* Return if no arguments or first argument has leading dash. */
314         ap = argv[1];
315         if (argc == 1 || *ap == '-')
316                 return;
317
318         /* Allocate space for new arguments. */
319         if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
320             (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
321                 err(1, NULL);
322
323         *nargv++ = *argv;
324         argv += 2, argc -= 2;
325
326         for (flags = 0; *ap; ++ap) {
327                 switch (*ap) {
328                 case 'b':
329                 case 'f':
330                 case 's':
331                         if (*argv == NULL) {
332                                 warnx("option requires an argument -- %c", *ap);
333                                 usage();
334                         }
335                         if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
336                                 err(1, NULL);
337                         nargv[0][0] = '-';
338                         nargv[0][1] = *ap;
339                         (void)strcpy(&nargv[0][2], *argv);
340                         ++argv;
341                         ++nargv;
342                         break;
343                 default:
344                         if (!flags) {
345                                 *p++ = '-';
346                                 flags = 1;
347                         }
348                         *p++ = *ap;
349                         break;
350                 }
351         }
352
353         /* Terminate flags. */
354         if (flags) {
355                 *p = '\0';
356                 *nargv++ = flagsp;
357         }
358
359         /* Copy remaining arguments. */
360         while ((*nargv++ = *argv++));
361
362         /* Update argument count. */
363         *argcp = nargv - *argvp - 1;
364 }