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