]> git.wh0rd.org - dump.git/blame - restore/main.c
updates to the -X option...
[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[] =
1a05d45d 43 "$Id: main.c,v 1.11 2000/03/09 13:12:31 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];
1a05d45d 91static const char *stdin_opt = NULL;
1227625a
SP
92
93#ifdef __linux__
94char *__progname;
95#endif
96
97static void obsolete __P((int *, char **[]));
98static void usage __P((void));
1a05d45d 99static void use_stdin __P((const char *));
1227625a
SP
100
101int
ddd2ef55 102main(int argc, char *argv[])
1227625a
SP
103{
104 int ch;
105 ino_t ino;
ddd2ef55 106 char *inputdev = _PATH_DEFTAPE;
1227625a
SP
107 char *symtbl = "./restoresymtable";
108 char *p, name[MAXPATHLEN];
08db2b86
SP
109 FILE *filelist = NULL;
110 char fname[MAXPATHLEN];
1227625a 111
b45f51d6
SP
112 /* Temp files should *not* be readable. We set permissions later. */
113 (void) umask(077);
0d7af9c5 114 filesys[0] = '\0';
1227625a
SP
115#ifdef __linux__
116 __progname = argv[0];
117#endif
118
b45f51d6
SP
119 if (argc < 2)
120 usage();
121
122 if ((inputdev = getenv("TAPE")) == NULL)
123 inputdev = _PATH_DEFTAPE;
ddd2ef55
SP
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 ;
1227625a 130 obsolete(&argc, &argv);
b45f51d6 131#ifdef KERBEROS
08db2b86 132#define optlist "b:CcdD:f:hikmMNRrs:tT:uvxX:y"
b45f51d6 133#else
08db2b86 134#define optlist "b:CcdD:f:himMNRrs:tT:uvxX:y"
b45f51d6
SP
135#endif
136 while ((ch = getopt(argc, argv, optlist)) != -1)
1227625a
SP
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':
0d7af9c5
SP
151 strncpy(filesys, optarg, NAMELEN);
152 filesys[NAMELEN - 1] = '\0';
1227625a
SP
153 break;
154 case 'T':
155 tmpdir = optarg;
156 break;
157 case 'd':
158 dflag = 1;
159 break;
160 case 'f':
1a05d45d
SP
161 if( !strcmp(optarg,"-") )
162 use_stdin("-f");
1227625a
SP
163 inputdev = optarg;
164 break;
165 case 'h':
166 hflag = 0;
167 break;
b45f51d6
SP
168#ifdef KERBEROS
169 case 'k':
170 dokerberos = 1;
171 break;
172#endif
1227625a
SP
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;
394240ce
SP
188 case 'M':
189 Mflag = 1;
190 break;
1227625a
SP
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;
b45f51d6
SP
202 case 'u':
203 uflag = 1;
204 break;
1227625a
SP
205 case 'v':
206 vflag = 1;
207 break;
08db2b86 208 case 'X':
1a05d45d
SP
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);
08db2b86 216 break;
1227625a
SP
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
ddd2ef55
SP
235 atexit(cleanup);
236
1227625a
SP
237 setinput(inputdev);
238
1a05d45d 239 if (argc == 0 && !filelist) {
1227625a
SP
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
ddd2ef55 251 Vprintf(stdout, "Begin compare restore\n");
1227625a
SP
252 compare_ignore_not_found = 0;
253 setup();
254 printf("filesys = %s\n", filesys);
ddd2ef55
SP
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);
1227625a
SP
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 */
ddd2ef55 286 Vprintf(stdout, "Begin incremental restore\n");
1227625a
SP
287 initsymtable(symtbl);
288 extractdirs(1);
289 removeoldleaves();
ddd2ef55 290 Vprintf(stdout, "Calculate node updates.\n");
1227625a
SP
291 treescan(".", ROOTINO, nodeupdates);
292 findunreflinks();
293 removeoldnodes();
294 } else {
295 /*
296 * This is a level zero dump tape.
297 */
ddd2ef55 298 Vprintf(stdout, "Begin level 0 restore\n");
1227625a
SP
299 initsymtable((char *)0);
300 extractdirs(1);
ddd2ef55 301 Vprintf(stdout, "Calculate extraction list.\n");
1227625a
SP
302 treescan(".", ROOTINO, nodeupdates);
303 }
304 createleaves(symtbl);
305 createlinks();
306 setdirmodes(FORCE);
307 checkrestore();
308 if (dflag) {
ddd2ef55 309 Vprintf(stdout, "Verify the directory structure\n");
1227625a
SP
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;
1a05d45d
SP
327
328/* handle file names from either text file (-X) or the command line */
08db2b86 329#define NEXTFILE(p) \
1a05d45d
SP
330 p = NULL; \
331 if (argc) { \
332 --argc; \
333 p = *argv++; \
08db2b86 334 } \
1a05d45d
SP
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 } \
08db2b86
SP
342 }
343
1227625a
SP
344 /*
345 * List contents of tape.
346 */
347 case 't':
348 setup();
349 extractdirs(0);
350 initsymtable((char *)0);
08db2b86
SP
351 for (;;) {
352 NEXTFILE(p);
353 if (!p)
354 break;
355 canon(p, name, sizeof(name));
1227625a
SP
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);
08db2b86
SP
369 for (;;) {
370 NEXTFILE(p);
371 if (!p)
372 break;
373 canon(p, name, sizeof(name));
1227625a
SP
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 }
ddd2ef55 388 exit(0);
1227625a 389 /* NOTREACHED */
ddd2ef55 390 return 0; /* gcc shut up */
1227625a
SP
391}
392
393static void
ddd2ef55 394usage(void)
1227625a 395{
8d4197bb
SP
396#ifdef KERBEROS
397#define kerbflag "k"
398#else
399#define kerbflag
400#endif
df9ae507
SP
401 (void)fprintf(stderr,
402 "%s %s\n", __progname, _DUMP_VERSION);
403 (void)fprintf(stderr,
1a05d45d 404 "usage:\t%s%s\n\t%s%s\n\t%s%s\n\t%s%s\n\t%s%s\n",
394240ce
SP
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]",
1a05d45d
SP
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 ...]");
ddd2ef55 410 exit(1);
1227625a
SP
411}
412
413/*
414 * obsolete --
415 * Change set of key letters and ordered arguments into something
416 * getopt(3) will like.
417 */
418static void
ddd2ef55 419obsolete(int *argcp, char **argvp[])
1227625a
SP
420{
421 int argc, flags;
ddd2ef55 422 char *ap, **argv, *flagsp = NULL, **nargv, *p = NULL;
1227625a
SP
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)
ddd2ef55 436 err(1, "malloc args");
1227625a
SP
437
438 *nargv++ = *argv;
b45f51d6 439 argv += 2, argc -= 2;
1227625a
SP
440
441 for (flags = 0; *ap; ++ap) {
442 switch (*ap) {
443 case 'b':
0d7af9c5 444 case 'D':
1227625a
SP
445 case 'f':
446 case 's':
0d7af9c5 447 case 'T':
08db2b86 448 case 'X':
1227625a
SP
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)
ddd2ef55 454 err(1, "malloc arg");
1227625a
SP
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. */
b45f51d6 478 while ((*nargv++ = *argv++));
1227625a
SP
479
480 /* Update argument count. */
481 *argcp = nargv - *argvp - 1;
482}
1a05d45d
SP
483
484/*
485 * use_stdin --
486 * reserve stdin for opt (avoid conflicts)
487 */
488void
489use_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}