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