]> git.wh0rd.org - dump.git/blame - restore/main.c
Added the -X option to restore (read names of files to be
[dump.git] / restore / main.c
CommitLineData
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
42static const char rcsid[] =
08db2b86 43 "$Id: main.c,v 1.10 2000/03/08 11:25:58 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
74int bflag = 0, cvtflag = 0, dflag = 0, vflag = 0, yflag = 0;
75int hflag = 1, mflag = 1, Nflag = 0;
b45f51d6
SP
76int uflag = 0;
77int dokerberos = 0;
1227625a
SP
78char command = '\0';
79long dumpnum = 1;
80long volno = 0;
81long ntrec;
82char *dumpmap;
83char *usedinomap;
84ino_t maxino;
85time_t dumptime;
86time_t dumpdate;
87FILE *terminal;
ddd2ef55 88char *tmpdir;
1227625a 89int compare_ignore_not_found;
0d7af9c5 90char filesys[NAMELEN];
1227625a
SP
91
92#ifdef __linux__
93char *__progname;
94#endif
95
96static void obsolete __P((int *, char **[]));
97static void usage __P((void));
98
99int
ddd2ef55 100main(int argc, char *argv[])
1227625a
SP
101{
102 int ch;
103 ino_t ino;
ddd2ef55 104 char *inputdev = _PATH_DEFTAPE;
1227625a
SP
105 char *symtbl = "./restoresymtable";
106 char *p, name[MAXPATHLEN];
08db2b86
SP
107 FILE *filelist = NULL;
108 char fname[MAXPATHLEN];
1227625a 109
b45f51d6
SP
110 /* Temp files should *not* be readable. We set permissions later. */
111 (void) umask(077);
0d7af9c5 112 filesys[0] = '\0';
1227625a
SP
113#ifdef __linux__
114 __progname = argv[0];
115#endif
116
b45f51d6
SP
117 if (argc < 2)
118 usage();
119
120 if ((inputdev = getenv("TAPE")) == NULL)
121 inputdev = _PATH_DEFTAPE;
ddd2ef55
SP
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 ;
1227625a 128 obsolete(&argc, &argv);
b45f51d6 129#ifdef KERBEROS
08db2b86 130#define optlist "b:CcdD:f:hikmMNRrs:tT:uvxX:y"
b45f51d6 131#else
08db2b86 132#define optlist "b:CcdD:f:himMNRrs:tT:uvxX:y"
b45f51d6
SP
133#endif
134 while ((ch = getopt(argc, argv, optlist)) != -1)
1227625a
SP
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':
0d7af9c5
SP
149 strncpy(filesys, optarg, NAMELEN);
150 filesys[NAMELEN - 1] = '\0';
1227625a
SP
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;
b45f51d6
SP
164#ifdef KERBEROS
165 case 'k':
166 dokerberos = 1;
167 break;
168#endif
1227625a
SP
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;
394240ce
SP
184 case 'M':
185 Mflag = 1;
186 break;
1227625a
SP
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;
b45f51d6
SP
198 case 'u':
199 uflag = 1;
200 break;
1227625a
SP
201 case 'v':
202 vflag = 1;
203 break;
08db2b86
SP
204 case 'X':
205 if ( !(filelist=fopen(optarg,"r")) )
206 errx(1, "can't open file for reading -- %s", optarg);
207 break;
1227625a
SP
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
ddd2ef55
SP
226 atexit(cleanup);
227
1227625a
SP
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
ddd2ef55 242 Vprintf(stdout, "Begin compare restore\n");
1227625a
SP
243 compare_ignore_not_found = 0;
244 setup();
245 printf("filesys = %s\n", filesys);
ddd2ef55
SP
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);
1227625a
SP
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 */
ddd2ef55 277 Vprintf(stdout, "Begin incremental restore\n");
1227625a
SP
278 initsymtable(symtbl);
279 extractdirs(1);
280 removeoldleaves();
ddd2ef55 281 Vprintf(stdout, "Calculate node updates.\n");
1227625a
SP
282 treescan(".", ROOTINO, nodeupdates);
283 findunreflinks();
284 removeoldnodes();
285 } else {
286 /*
287 * This is a level zero dump tape.
288 */
ddd2ef55 289 Vprintf(stdout, "Begin level 0 restore\n");
1227625a
SP
290 initsymtable((char *)0);
291 extractdirs(1);
ddd2ef55 292 Vprintf(stdout, "Calculate extraction list.\n");
1227625a
SP
293 treescan(".", ROOTINO, nodeupdates);
294 }
295 createleaves(symtbl);
296 createlinks();
297 setdirmodes(FORCE);
298 checkrestore();
299 if (dflag) {
ddd2ef55 300 Vprintf(stdout, "Verify the directory structure\n");
1227625a
SP
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;
08db2b86
SP
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
1227625a
SP
331 /*
332 * List contents of tape.
333 */
334 case 't':
335 setup();
336 extractdirs(0);
337 initsymtable((char *)0);
08db2b86
SP
338 for (;;) {
339 NEXTFILE(p);
340 if (!p)
341 break;
342 canon(p, name, sizeof(name));
1227625a
SP
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);
08db2b86
SP
356 for (;;) {
357 NEXTFILE(p);
358 if (!p)
359 break;
360 canon(p, name, sizeof(name));
1227625a
SP
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 }
ddd2ef55 375 exit(0);
1227625a 376 /* NOTREACHED */
ddd2ef55 377 return 0; /* gcc shut up */
1227625a
SP
378}
379
380static void
ddd2ef55 381usage(void)
1227625a 382{
8d4197bb
SP
383#ifdef KERBEROS
384#define kerbflag "k"
385#else
386#define kerbflag
387#endif
df9ae507
SP
388 (void)fprintf(stderr,
389 "%s %s\n", __progname, _DUMP_VERSION);
390 (void)fprintf(stderr,
08db2b86 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",
394240ce
SP
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 ...]",
08db2b86
SP
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]");
ddd2ef55 399 exit(1);
1227625a
SP
400}
401
402/*
403 * obsolete --
404 * Change set of key letters and ordered arguments into something
405 * getopt(3) will like.
406 */
407static void
ddd2ef55 408obsolete(int *argcp, char **argvp[])
1227625a
SP
409{
410 int argc, flags;
ddd2ef55 411 char *ap, **argv, *flagsp = NULL, **nargv, *p = NULL;
1227625a
SP
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)
ddd2ef55 425 err(1, "malloc args");
1227625a
SP
426
427 *nargv++ = *argv;
b45f51d6 428 argv += 2, argc -= 2;
1227625a
SP
429
430 for (flags = 0; *ap; ++ap) {
431 switch (*ap) {
432 case 'b':
0d7af9c5 433 case 'D':
1227625a
SP
434 case 'f':
435 case 's':
0d7af9c5 436 case 'T':
08db2b86 437 case 'X':
1227625a
SP
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)
ddd2ef55 443 err(1, "malloc arg");
1227625a
SP
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. */
b45f51d6 467 while ((*nargv++ = *argv++));
1227625a
SP
468
469 /* Update argument count. */
470 *argcp = nargv - *argvp - 1;
471}