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