]> git.wh0rd.org - dump.git/blob - restore/main.c
updates to the -X option...
[dump.git] / restore / main.c
1 /*
2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
4 * Remy Card <card@Linux.EU.Org>, 1994-1997
5 * Stelian Pop <pop@cybercable.fr>, 1999-2000
6 */
7
8 /*
9 * Copyright (c) 1983, 1993
10 * The Regents of the University of California. All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 */
40
41 #ifndef lint
42 static const char rcsid[] =
43 "$Id: main.c,v 1.11 2000/03/09 13:12:31 stelian Exp $";
44 #endif /* not lint */
45
46 #include <sys/param.h>
47 #include <sys/stat.h>
48 #include <errno.h>
49
50 #ifdef __linux__
51 #include <linux/ext2_fs.h>
52 #include <bsdcompat.h>
53 #include <signal.h>
54 #include <string.h>
55 #else /* __linux__ */
56 #include <ufs/ufs/dinode.h>
57 #endif /* __linux__ */
58 #include <protocols/dumprestore.h>
59
60 #include <compaterr.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <unistd.h>
64
65 #ifdef __linux__
66 #include <ext2fs/ext2fs.h>
67 #include <getopt.h>
68 #endif
69
70 #include "pathnames.h"
71 #include "restore.h"
72 #include "extern.h"
73
74 int bflag = 0, cvtflag = 0, dflag = 0, vflag = 0, yflag = 0;
75 int hflag = 1, mflag = 1, Nflag = 0;
76 int uflag = 0;
77 int dokerberos = 0;
78 char command = '\0';
79 long dumpnum = 1;
80 long volno = 0;
81 long ntrec;
82 char *dumpmap;
83 char *usedinomap;
84 ino_t maxino;
85 time_t dumptime;
86 time_t dumpdate;
87 FILE *terminal;
88 char *tmpdir;
89 int compare_ignore_not_found;
90 char filesys[NAMELEN];
91 static const char *stdin_opt = NULL;
92
93 #ifdef __linux__
94 char *__progname;
95 #endif
96
97 static void obsolete __P((int *, char **[]));
98 static void usage __P((void));
99 static void use_stdin __P((const char *));
100
101 int
102 main(int argc, char *argv[])
103 {
104 int ch;
105 ino_t ino;
106 char *inputdev = _PATH_DEFTAPE;
107 char *symtbl = "./restoresymtable";
108 char *p, name[MAXPATHLEN];
109 FILE *filelist = NULL;
110 char fname[MAXPATHLEN];
111
112 /* Temp files should *not* be readable. We set permissions later. */
113 (void) umask(077);
114 filesys[0] = '\0';
115 #ifdef __linux__
116 __progname = argv[0];
117 #endif
118
119 if (argc < 2)
120 usage();
121
122 if ((inputdev = getenv("TAPE")) == NULL)
123 inputdev = _PATH_DEFTAPE;
124 if ((tmpdir = getenv("TMPDIR")) == NULL)
125 tmpdir = _PATH_TMP;
126 if ((tmpdir = strdup(tmpdir)) == NULL)
127 err(1, "malloc tmpdir");
128 for (p = tmpdir + strlen(tmpdir) - 1; p >= tmpdir && *p == '/'; p--)
129 ;
130 obsolete(&argc, &argv);
131 #ifdef KERBEROS
132 #define optlist "b:CcdD:f:hikmMNRrs:tT:uvxX:y"
133 #else
134 #define optlist "b:CcdD:f:himMNRrs:tT:uvxX:y"
135 #endif
136 while ((ch = getopt(argc, argv, optlist)) != -1)
137 switch(ch) {
138 case 'b':
139 /* Change default tape blocksize. */
140 bflag = 1;
141 ntrec = strtol(optarg, &p, 10);
142 if (*p)
143 errx(1, "illegal blocksize -- %s", optarg);
144 if (ntrec <= 0)
145 errx(1, "block size must be greater than 0");
146 break;
147 case 'c':
148 cvtflag = 1;
149 break;
150 case 'D':
151 strncpy(filesys, optarg, NAMELEN);
152 filesys[NAMELEN - 1] = '\0';
153 break;
154 case 'T':
155 tmpdir = optarg;
156 break;
157 case 'd':
158 dflag = 1;
159 break;
160 case 'f':
161 if( !strcmp(optarg,"-") )
162 use_stdin("-f");
163 inputdev = optarg;
164 break;
165 case 'h':
166 hflag = 0;
167 break;
168 #ifdef KERBEROS
169 case 'k':
170 dokerberos = 1;
171 break;
172 #endif
173 case 'C':
174 case 'i':
175 case 'R':
176 case 'r':
177 case 't':
178 case 'x':
179 if (command != '\0')
180 errx(1,
181 "%c and %c options are mutually exclusive",
182 ch, command);
183 command = ch;
184 break;
185 case 'm':
186 mflag = 0;
187 break;
188 case 'M':
189 Mflag = 1;
190 break;
191 case 'N':
192 Nflag = 1;
193 break;
194 case 's':
195 /* Dumpnum (skip to) for multifile dump tapes. */
196 dumpnum = strtol(optarg, &p, 10);
197 if (*p)
198 errx(1, "illegal dump number -- %s", optarg);
199 if (dumpnum <= 0)
200 errx(1, "dump number must be greater than 0");
201 break;
202 case 'u':
203 uflag = 1;
204 break;
205 case 'v':
206 vflag = 1;
207 break;
208 case 'X':
209 if( !strcmp(optarg,"-") ) {
210 use_stdin("-X");
211 filelist = stdin;
212 }
213 else
214 if ( !(filelist = fopen(optarg,"r")) )
215 errx(1, "can't open file for reading -- %s", optarg);
216 break;
217 case 'y':
218 yflag = 1;
219 break;
220 default:
221 usage();
222 }
223 argc -= optind;
224 argv += optind;
225
226 if (command == '\0')
227 errx(1, "none of C, i, R, r, t or x options specified");
228
229 if (signal(SIGINT, onintr) == SIG_IGN)
230 (void) signal(SIGINT, SIG_IGN);
231 if (signal(SIGTERM, onintr) == SIG_IGN)
232 (void) signal(SIGTERM, SIG_IGN);
233 setlinebuf(stderr);
234
235 atexit(cleanup);
236
237 setinput(inputdev);
238
239 if (argc == 0 && !filelist) {
240 argc = 1;
241 *--argv = ".";
242 }
243
244 switch (command) {
245 /*
246 * Compare contents of tape.
247 */
248 case 'C': {
249 struct stat stbuf;
250
251 Vprintf(stdout, "Begin compare restore\n");
252 compare_ignore_not_found = 0;
253 setup();
254 printf("filesys = %s\n", filesys);
255 if (stat(filesys, &stbuf) < 0)
256 err(1, "cannot stat directory %s", filesys);
257 if (chdir(filesys) < 0)
258 err(1, "cannot cd to %s", filesys);
259 compare_ignore_not_found = dumptime > 0;
260 initsymtable((char *)0);
261 extractdirs(0);
262 treescan(".", ROOTINO, nodeupdates);
263 compareleaves();
264 checkrestore();
265 break;
266 }
267
268 /*
269 * Interactive mode.
270 */
271 case 'i':
272 setup();
273 extractdirs(1);
274 initsymtable(NULL);
275 runcmdshell();
276 break;
277 /*
278 * Incremental restoration of a file system.
279 */
280 case 'r':
281 setup();
282 if (dumptime > 0) {
283 /*
284 * This is an incremental dump tape.
285 */
286 Vprintf(stdout, "Begin incremental restore\n");
287 initsymtable(symtbl);
288 extractdirs(1);
289 removeoldleaves();
290 Vprintf(stdout, "Calculate node updates.\n");
291 treescan(".", ROOTINO, nodeupdates);
292 findunreflinks();
293 removeoldnodes();
294 } else {
295 /*
296 * This is a level zero dump tape.
297 */
298 Vprintf(stdout, "Begin level 0 restore\n");
299 initsymtable((char *)0);
300 extractdirs(1);
301 Vprintf(stdout, "Calculate extraction list.\n");
302 treescan(".", ROOTINO, nodeupdates);
303 }
304 createleaves(symtbl);
305 createlinks();
306 setdirmodes(FORCE);
307 checkrestore();
308 if (dflag) {
309 Vprintf(stdout, "Verify the directory structure\n");
310 treescan(".", ROOTINO, verifyfile);
311 }
312 dumpsymtable(symtbl, (long)1);
313 break;
314 /*
315 * Resume an incremental file system restoration.
316 */
317 case 'R':
318 initsymtable(symtbl);
319 skipmaps();
320 skipdirs();
321 createleaves(symtbl);
322 createlinks();
323 setdirmodes(FORCE);
324 checkrestore();
325 dumpsymtable(symtbl, (long)1);
326 break;
327
328 /* handle file names from either text file (-X) or the command line */
329 #define NEXTFILE(p) \
330 p = NULL; \
331 if (argc) { \
332 --argc; \
333 p = *argv++; \
334 } \
335 else if (filelist) { \
336 if ((p = fgets(fname, MAXPATHLEN, filelist))) { \
337 if ( *p && *(p + strlen(p) - 1) == '\n' ) /* possible null string */ \
338 *(p + strlen(p) - 1) = '\0'; \
339 if ( !*p ) /* skip empty lines */ \
340 continue; \
341 } \
342 }
343
344 /*
345 * List contents of tape.
346 */
347 case 't':
348 setup();
349 extractdirs(0);
350 initsymtable((char *)0);
351 for (;;) {
352 NEXTFILE(p);
353 if (!p)
354 break;
355 canon(p, name, sizeof(name));
356 ino = dirlookup(name);
357 if (ino == 0)
358 continue;
359 treescan(name, ino, listfile);
360 }
361 break;
362 /*
363 * Batch extraction of tape contents.
364 */
365 case 'x':
366 setup();
367 extractdirs(1);
368 initsymtable((char *)0);
369 for (;;) {
370 NEXTFILE(p);
371 if (!p)
372 break;
373 canon(p, name, sizeof(name));
374 ino = dirlookup(name);
375 if (ino == 0)
376 continue;
377 if (mflag)
378 pathcheck(name);
379 treescan(name, ino, addfile);
380 }
381 createfiles();
382 createlinks();
383 setdirmodes(0);
384 if (dflag)
385 checkrestore();
386 break;
387 }
388 exit(0);
389 /* NOTREACHED */
390 return 0; /* gcc shut up */
391 }
392
393 static void
394 usage(void)
395 {
396 #ifdef KERBEROS
397 #define kerbflag "k"
398 #else
399 #define kerbflag
400 #endif
401 (void)fprintf(stderr,
402 "%s %s\n", __progname, _DUMP_VERSION);
403 (void)fprintf(stderr,
404 "usage:\t%s%s\n\t%s%s\n\t%s%s\n\t%s%s\n\t%s%s\n",
405 __progname, " -i [-ch" kerbflag "mMuvy] [-b blocksize] [-f file] [-s fileno]",
406 __progname, " -r [-c" kerbflag "Muvy] [-b blocksize] [-f file] [-s fileno]",
407 __progname, " -R [-c" kerbflag "Muvy] [-b blocksize] [-f file] [-s fileno]",
408 __progname, " -x [-ch" kerbflag "mMuvy] [-b blocksize] [-f file] [-s fileno] [-X filelist] [file ...]",
409 __progname, " -t [-ch" kerbflag "Muvy] [-b blocksize] [-f file] [-s fileno] [-X filelist] [file ...]");
410 exit(1);
411 }
412
413 /*
414 * obsolete --
415 * Change set of key letters and ordered arguments into something
416 * getopt(3) will like.
417 */
418 static void
419 obsolete(int *argcp, char **argvp[])
420 {
421 int argc, flags;
422 char *ap, **argv, *flagsp = NULL, **nargv, *p = NULL;
423
424 /* Setup. */
425 argv = *argvp;
426 argc = *argcp;
427
428 /* Return if no arguments or first argument has leading dash. */
429 ap = argv[1];
430 if (argc == 1 || *ap == '-')
431 return;
432
433 /* Allocate space for new arguments. */
434 if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
435 (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
436 err(1, "malloc args");
437
438 *nargv++ = *argv;
439 argv += 2, argc -= 2;
440
441 for (flags = 0; *ap; ++ap) {
442 switch (*ap) {
443 case 'b':
444 case 'D':
445 case 'f':
446 case 's':
447 case 'T':
448 case 'X':
449 if (*argv == NULL) {
450 warnx("option requires an argument -- %c", *ap);
451 usage();
452 }
453 if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
454 err(1, "malloc arg");
455 nargv[0][0] = '-';
456 nargv[0][1] = *ap;
457 (void)strcpy(&nargv[0][2], *argv);
458 ++argv;
459 ++nargv;
460 break;
461 default:
462 if (!flags) {
463 *p++ = '-';
464 flags = 1;
465 }
466 *p++ = *ap;
467 break;
468 }
469 }
470
471 /* Terminate flags. */
472 if (flags) {
473 *p = '\0';
474 *nargv++ = flagsp;
475 }
476
477 /* Copy remaining arguments. */
478 while ((*nargv++ = *argv++));
479
480 /* Update argument count. */
481 *argcp = nargv - *argvp - 1;
482 }
483
484 /*
485 * use_stdin --
486 * reserve stdin for opt (avoid conflicts)
487 */
488 void
489 use_stdin(const char *opt)
490 {
491 if (stdin_opt)
492 errx(1, "can't handle standard input for both %s and %s",
493 stdin_opt, opt);
494 stdin_opt = opt;
495 }