]> git.wh0rd.org - dump.git/blob - restore/main.c
Added the -X option to restore (read names of files to be
[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.10 2000/03/08 11:25:58 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
92 #ifdef __linux__
93 char *__progname;
94 #endif
95
96 static void obsolete __P((int *, char **[]));
97 static void usage __P((void));
98
99 int
100 main(int argc, char *argv[])
101 {
102 int ch;
103 ino_t ino;
104 char *inputdev = _PATH_DEFTAPE;
105 char *symtbl = "./restoresymtable";
106 char *p, name[MAXPATHLEN];
107 FILE *filelist = NULL;
108 char fname[MAXPATHLEN];
109
110 /* Temp files should *not* be readable. We set permissions later. */
111 (void) umask(077);
112 filesys[0] = '\0';
113 #ifdef __linux__
114 __progname = argv[0];
115 #endif
116
117 if (argc < 2)
118 usage();
119
120 if ((inputdev = getenv("TAPE")) == NULL)
121 inputdev = _PATH_DEFTAPE;
122 if ((tmpdir = getenv("TMPDIR")) == NULL)
123 tmpdir = _PATH_TMP;
124 if ((tmpdir = strdup(tmpdir)) == NULL)
125 err(1, "malloc tmpdir");
126 for (p = tmpdir + strlen(tmpdir) - 1; p >= tmpdir && *p == '/'; p--)
127 ;
128 obsolete(&argc, &argv);
129 #ifdef KERBEROS
130 #define optlist "b:CcdD:f:hikmMNRrs:tT:uvxX:y"
131 #else
132 #define optlist "b:CcdD:f:himMNRrs:tT:uvxX:y"
133 #endif
134 while ((ch = getopt(argc, argv, optlist)) != -1)
135 switch(ch) {
136 case 'b':
137 /* Change default tape blocksize. */
138 bflag = 1;
139 ntrec = strtol(optarg, &p, 10);
140 if (*p)
141 errx(1, "illegal blocksize -- %s", optarg);
142 if (ntrec <= 0)
143 errx(1, "block size must be greater than 0");
144 break;
145 case 'c':
146 cvtflag = 1;
147 break;
148 case 'D':
149 strncpy(filesys, optarg, NAMELEN);
150 filesys[NAMELEN - 1] = '\0';
151 break;
152 case 'T':
153 tmpdir = optarg;
154 break;
155 case 'd':
156 dflag = 1;
157 break;
158 case 'f':
159 inputdev = optarg;
160 break;
161 case 'h':
162 hflag = 0;
163 break;
164 #ifdef KERBEROS
165 case 'k':
166 dokerberos = 1;
167 break;
168 #endif
169 case 'C':
170 case 'i':
171 case 'R':
172 case 'r':
173 case 't':
174 case 'x':
175 if (command != '\0')
176 errx(1,
177 "%c and %c options are mutually exclusive",
178 ch, command);
179 command = ch;
180 break;
181 case 'm':
182 mflag = 0;
183 break;
184 case 'M':
185 Mflag = 1;
186 break;
187 case 'N':
188 Nflag = 1;
189 break;
190 case 's':
191 /* Dumpnum (skip to) for multifile dump tapes. */
192 dumpnum = strtol(optarg, &p, 10);
193 if (*p)
194 errx(1, "illegal dump number -- %s", optarg);
195 if (dumpnum <= 0)
196 errx(1, "dump number must be greater than 0");
197 break;
198 case 'u':
199 uflag = 1;
200 break;
201 case 'v':
202 vflag = 1;
203 break;
204 case 'X':
205 if ( !(filelist=fopen(optarg,"r")) )
206 errx(1, "can't open file for reading -- %s", optarg);
207 break;
208 case 'y':
209 yflag = 1;
210 break;
211 default:
212 usage();
213 }
214 argc -= optind;
215 argv += optind;
216
217 if (command == '\0')
218 errx(1, "none of C, i, R, r, t or x options specified");
219
220 if (signal(SIGINT, onintr) == SIG_IGN)
221 (void) signal(SIGINT, SIG_IGN);
222 if (signal(SIGTERM, onintr) == SIG_IGN)
223 (void) signal(SIGTERM, SIG_IGN);
224 setlinebuf(stderr);
225
226 atexit(cleanup);
227
228 setinput(inputdev);
229
230 if (argc == 0) {
231 argc = 1;
232 *--argv = ".";
233 }
234
235 switch (command) {
236 /*
237 * Compare contents of tape.
238 */
239 case 'C': {
240 struct stat stbuf;
241
242 Vprintf(stdout, "Begin compare restore\n");
243 compare_ignore_not_found = 0;
244 setup();
245 printf("filesys = %s\n", filesys);
246 if (stat(filesys, &stbuf) < 0)
247 err(1, "cannot stat directory %s", filesys);
248 if (chdir(filesys) < 0)
249 err(1, "cannot cd to %s", filesys);
250 compare_ignore_not_found = dumptime > 0;
251 initsymtable((char *)0);
252 extractdirs(0);
253 treescan(".", ROOTINO, nodeupdates);
254 compareleaves();
255 checkrestore();
256 break;
257 }
258
259 /*
260 * Interactive mode.
261 */
262 case 'i':
263 setup();
264 extractdirs(1);
265 initsymtable(NULL);
266 runcmdshell();
267 break;
268 /*
269 * Incremental restoration of a file system.
270 */
271 case 'r':
272 setup();
273 if (dumptime > 0) {
274 /*
275 * This is an incremental dump tape.
276 */
277 Vprintf(stdout, "Begin incremental restore\n");
278 initsymtable(symtbl);
279 extractdirs(1);
280 removeoldleaves();
281 Vprintf(stdout, "Calculate node updates.\n");
282 treescan(".", ROOTINO, nodeupdates);
283 findunreflinks();
284 removeoldnodes();
285 } else {
286 /*
287 * This is a level zero dump tape.
288 */
289 Vprintf(stdout, "Begin level 0 restore\n");
290 initsymtable((char *)0);
291 extractdirs(1);
292 Vprintf(stdout, "Calculate extraction list.\n");
293 treescan(".", ROOTINO, nodeupdates);
294 }
295 createleaves(symtbl);
296 createlinks();
297 setdirmodes(FORCE);
298 checkrestore();
299 if (dflag) {
300 Vprintf(stdout, "Verify the directory structure\n");
301 treescan(".", ROOTINO, verifyfile);
302 }
303 dumpsymtable(symtbl, (long)1);
304 break;
305 /*
306 * Resume an incremental file system restoration.
307 */
308 case 'R':
309 initsymtable(symtbl);
310 skipmaps();
311 skipdirs();
312 createleaves(symtbl);
313 createlinks();
314 setdirmodes(FORCE);
315 checkrestore();
316 dumpsymtable(symtbl, (long)1);
317 break;
318
319 #define NEXTFILE(p) \
320 if (filelist) { \
321 if ((p = fgets(fname, MAXPATHLEN, filelist))) \
322 *(p + strlen(p) - 1) = '\0'; \
323 } \
324 else { \
325 if (argc--) \
326 p = *argv++; \
327 else \
328 p = NULL; \
329 }
330
331 /*
332 * List contents of tape.
333 */
334 case 't':
335 setup();
336 extractdirs(0);
337 initsymtable((char *)0);
338 for (;;) {
339 NEXTFILE(p);
340 if (!p)
341 break;
342 canon(p, name, sizeof(name));
343 ino = dirlookup(name);
344 if (ino == 0)
345 continue;
346 treescan(name, ino, listfile);
347 }
348 break;
349 /*
350 * Batch extraction of tape contents.
351 */
352 case 'x':
353 setup();
354 extractdirs(1);
355 initsymtable((char *)0);
356 for (;;) {
357 NEXTFILE(p);
358 if (!p)
359 break;
360 canon(p, name, sizeof(name));
361 ino = dirlookup(name);
362 if (ino == 0)
363 continue;
364 if (mflag)
365 pathcheck(name);
366 treescan(name, ino, addfile);
367 }
368 createfiles();
369 createlinks();
370 setdirmodes(0);
371 if (dflag)
372 checkrestore();
373 break;
374 }
375 exit(0);
376 /* NOTREACHED */
377 return 0; /* gcc shut up */
378 }
379
380 static void
381 usage(void)
382 {
383 #ifdef KERBEROS
384 #define kerbflag "k"
385 #else
386 #define kerbflag
387 #endif
388 (void)fprintf(stderr,
389 "%s %s\n", __progname, _DUMP_VERSION);
390 (void)fprintf(stderr,
391 "usage:\t%s%s\n\t%s%s\n\t%s%s\n\t%s%s\n\t%s%s\n\t%s%s\n\t%s%s\n",
392 __progname, " -i [-ch" kerbflag "mMuvy] [-b blocksize] [-f file] [-s fileno]",
393 __progname, " -r [-c" kerbflag "Muvy] [-b blocksize] [-f file] [-s fileno]",
394 __progname, " -R [-c" kerbflag "Muvy] [-b blocksize] [-f file] [-s fileno]",
395 __progname, " -x [-ch" kerbflag "mMuvy] [-b blocksize] [-f file] [-s fileno] [file ...]",
396 __progname, " -x [-ch" kerbflag "mMuvy] [-b blocksize] [-f file] [-s fileno] [-X filelist]",
397 __progname, " -t [-ch" kerbflag "Muvy] [-b blocksize] [-f file] [-s fileno] [file ...]",
398 __progname, " -t [-ch" kerbflag "Muvy] [-b blocksize] [-f file] [-s fileno] [-X filelist]");
399 exit(1);
400 }
401
402 /*
403 * obsolete --
404 * Change set of key letters and ordered arguments into something
405 * getopt(3) will like.
406 */
407 static void
408 obsolete(int *argcp, char **argvp[])
409 {
410 int argc, flags;
411 char *ap, **argv, *flagsp = NULL, **nargv, *p = NULL;
412
413 /* Setup. */
414 argv = *argvp;
415 argc = *argcp;
416
417 /* Return if no arguments or first argument has leading dash. */
418 ap = argv[1];
419 if (argc == 1 || *ap == '-')
420 return;
421
422 /* Allocate space for new arguments. */
423 if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
424 (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
425 err(1, "malloc args");
426
427 *nargv++ = *argv;
428 argv += 2, argc -= 2;
429
430 for (flags = 0; *ap; ++ap) {
431 switch (*ap) {
432 case 'b':
433 case 'D':
434 case 'f':
435 case 's':
436 case 'T':
437 case 'X':
438 if (*argv == NULL) {
439 warnx("option requires an argument -- %c", *ap);
440 usage();
441 }
442 if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
443 err(1, "malloc arg");
444 nargv[0][0] = '-';
445 nargv[0][1] = *ap;
446 (void)strcpy(&nargv[0][2], *argv);
447 ++argv;
448 ++nargv;
449 break;
450 default:
451 if (!flags) {
452 *p++ = '-';
453 flags = 1;
454 }
455 *p++ = *ap;
456 break;
457 }
458 }
459
460 /* Terminate flags. */
461 if (flags) {
462 *p = '\0';
463 *nargv++ = flagsp;
464 }
465
466 /* Copy remaining arguments. */
467 while ((*nargv++ = *argv++));
468
469 /* Update argument count. */
470 *argcp = nargv - *argvp - 1;
471 }