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