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