1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright 1998, 2001-2003 Sun Microsystems, Inc.  All rights reserved.
3*0Sstevel@tonic-gate  * Use is subject to license terms.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate 
6*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
7*0Sstevel@tonic-gate /*	  All Rights Reserved	*/
8*0Sstevel@tonic-gate 
9*0Sstevel@tonic-gate /*
10*0Sstevel@tonic-gate  * Copyright (c) 1983 Regents of the University of California.
11*0Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
12*0Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
13*0Sstevel@tonic-gate  */
14*0Sstevel@tonic-gate 
15*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
16*0Sstevel@tonic-gate 
17*0Sstevel@tonic-gate /*
18*0Sstevel@tonic-gate  *	Modified to recursively extract all files within a subtree
19*0Sstevel@tonic-gate  *	(supressed by the h option) and recreate the heirarchical
20*0Sstevel@tonic-gate  *	structure of that subtree and move extracted files to their
21*0Sstevel@tonic-gate  *	proper homes (supressed by the m option).
22*0Sstevel@tonic-gate  *	Includes the s (skip files) option for use with multiple
23*0Sstevel@tonic-gate  *	dumps on a single tape.
24*0Sstevel@tonic-gate  *	8/29/80		by Mike Litzkow
25*0Sstevel@tonic-gate  *
26*0Sstevel@tonic-gate  *	Modified to work on the new file system and to recover from
27*0Sstevel@tonic-gate  *	tape read errors.
28*0Sstevel@tonic-gate  *	1/19/82		by Kirk McKusick
29*0Sstevel@tonic-gate  *
30*0Sstevel@tonic-gate  *	Full incremental restore running entirely in user code and
31*0Sstevel@tonic-gate  *	interactive tape browser.
32*0Sstevel@tonic-gate  *	1/19/83		by Kirk McKusick
33*0Sstevel@tonic-gate  */
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #include "restore.h"
36*0Sstevel@tonic-gate #include <signal.h>
37*0Sstevel@tonic-gate #include <byteorder.h>
38*0Sstevel@tonic-gate #include <priv_utils.h>
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #include <euc.h>
41*0Sstevel@tonic-gate #include <getwidth.h>
42*0Sstevel@tonic-gate #include <sys/mtio.h>
43*0Sstevel@tonic-gate eucwidth_t wp;
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate int	bflag = 0, dflag = 0, vflag = 0, yflag = 0;
46*0Sstevel@tonic-gate int	hflag = 1, mflag = 1, paginating = 0, offline = 0, autoload = 0;
47*0Sstevel@tonic-gate int	autoload_tries;
48*0Sstevel@tonic-gate int	autoload_period;
49*0Sstevel@tonic-gate int	cvtflag = 0;		/* Converting from old dump format */
50*0Sstevel@tonic-gate char	command = '\0';
51*0Sstevel@tonic-gate long	dumpnum = 1;
52*0Sstevel@tonic-gate int	volno = 0;
53*0Sstevel@tonic-gate uint_t	ntrec;			/* blocking factor, in KB */
54*0Sstevel@tonic-gate uint_t	saved_ntrec;		/* saved blocking factor, in KB */
55*0Sstevel@tonic-gate ssize_t	tape_rec_size = 0;	/* tape record size (ntrec * tp_bsize) */
56*0Sstevel@tonic-gate size_t	newtapebuf_size = 0;	/* save size of last call to newtapebuf */
57*0Sstevel@tonic-gate char	*progname;
58*0Sstevel@tonic-gate char	*dumpmap;
59*0Sstevel@tonic-gate char	*clrimap;
60*0Sstevel@tonic-gate char	*c_label;		/* if non-NULL, we must see this tape label */
61*0Sstevel@tonic-gate ino_t	maxino;
62*0Sstevel@tonic-gate time_t	dumptime;
63*0Sstevel@tonic-gate time_t	dumpdate;
64*0Sstevel@tonic-gate FILE 	*terminal;
65*0Sstevel@tonic-gate char	*tmpdir;
66*0Sstevel@tonic-gate char	*pager_catenated;
67*0Sstevel@tonic-gate char	**pager_vector;
68*0Sstevel@tonic-gate int	pager_len;
69*0Sstevel@tonic-gate int	inattrspace = 0;
70*0Sstevel@tonic-gate int	savepwd;
71*0Sstevel@tonic-gate int32_t	tp_bsize = TP_BSIZE_MIN;
72*0Sstevel@tonic-gate struct byteorder_ctx *byteorder;
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate static void set_tmpdir(void);
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate main(argc, argv)
77*0Sstevel@tonic-gate 	int argc;
78*0Sstevel@tonic-gate 	char *argv[];
79*0Sstevel@tonic-gate {
80*0Sstevel@tonic-gate 	static struct arglist alist = { 0, 0, 0, 0, 0 };
81*0Sstevel@tonic-gate 	int  count;
82*0Sstevel@tonic-gate 	char *cp;
83*0Sstevel@tonic-gate 	char *fname;
84*0Sstevel@tonic-gate 	ino_t ino;
85*0Sstevel@tonic-gate 	char *inputdev;
86*0Sstevel@tonic-gate 	char *archivefile = 0;
87*0Sstevel@tonic-gate 	char *symtbl = RESTORESYMTABLE;
88*0Sstevel@tonic-gate 	char name[MAXPATHLEN];
89*0Sstevel@tonic-gate 	int  fflag = 0;
90*0Sstevel@tonic-gate 	struct sigaction sa, osa;
91*0Sstevel@tonic-gate 	int multiplier;
92*0Sstevel@tonic-gate 	char units;
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate 	if ((progname = strrchr(argv[0], '/')) != NULL)
95*0Sstevel@tonic-gate 		progname++;
96*0Sstevel@tonic-gate 	else
97*0Sstevel@tonic-gate 		progname = argv[0];
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 	if (strcmp("hsmrestore", progname) == 0) {
100*0Sstevel@tonic-gate 		(void) fprintf(stderr,
101*0Sstevel@tonic-gate 		    gettext("hsmrestore emulation is no longer supported.\n"));
102*0Sstevel@tonic-gate 		done(1);
103*0Sstevel@tonic-gate 	}
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate 	/*
106*0Sstevel@tonic-gate 	 * Convert the effective uid of 0 to the single privilege
107*0Sstevel@tonic-gate 	 * we really want.  When running with all privileges, this
108*0Sstevel@tonic-gate 	 * is a no-op.  When the set-uid bit is stripped restore
109*0Sstevel@tonic-gate 	 * still works for local tapes.  Fail when trying to access
110*0Sstevel@tonic-gate 	 * a remote tape in that case and not immediately.
111*0Sstevel@tonic-gate 	 */
112*0Sstevel@tonic-gate 	(void) __init_suid_priv(0, PRIV_NET_PRIVADDR, (char *)NULL);
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 	inputdev = DEFTAPE;
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 	/*
117*0Sstevel@tonic-gate 	 * This doesn't work because ufsrestore is statically linked:
118*0Sstevel@tonic-gate 	 * (void) setlocale(LC_ALL, "");
119*0Sstevel@tonic-gate 	 * The problem seems to be with LC_COLLATE, so set all the
120*0Sstevel@tonic-gate 	 * others explicitly.  Bug 1157128 was created against the I18N
121*0Sstevel@tonic-gate 	 * library.  When that bug is fixed this should go back to the way
122*0Sstevel@tonic-gate 	 * it was.
123*0Sstevel@tonic-gate 	 * XXX 1157128 was closed as a dup of 1099747.  That bug was fixed by
124*0Sstevel@tonic-gate 	 * disallowing setlocale() to anything other than "C".  "" is
125*0Sstevel@tonic-gate 	 * allowed, but only if none of the envars LC_ALL, LC_COLLATE, or LANG
126*0Sstevel@tonic-gate 	 * select anything other than "C".
127*0Sstevel@tonic-gate 	 */
128*0Sstevel@tonic-gate 	(void) setlocale(LC_CTYPE, "");
129*0Sstevel@tonic-gate 	(void) setlocale(LC_NUMERIC, "");
130*0Sstevel@tonic-gate 	(void) setlocale(LC_TIME, "");
131*0Sstevel@tonic-gate 	(void) setlocale(LC_MONETARY, "");
132*0Sstevel@tonic-gate 	(void) setlocale(LC_MESSAGES, "");
133*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
134*0Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
135*0Sstevel@tonic-gate #endif
136*0Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
137*0Sstevel@tonic-gate 	getwidth(&wp);
138*0Sstevel@tonic-gate 	if ((byteorder = byteorder_create()) == NULL) {
139*0Sstevel@tonic-gate 		(void) fprintf(stderr,
140*0Sstevel@tonic-gate 		    gettext("Cannot create byteorder context\n"));
141*0Sstevel@tonic-gate 		done(1);
142*0Sstevel@tonic-gate 	}
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 	if ((savepwd = open(".", O_RDONLY)) < 0) {
145*0Sstevel@tonic-gate 		(void) fprintf(stderr,
146*0Sstevel@tonic-gate 		    gettext("Cannot save current directory context\n"));
147*0Sstevel@tonic-gate 		done(1);
148*0Sstevel@tonic-gate 	}
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 	set_tmpdir();
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate 	autoload_period = 12;
153*0Sstevel@tonic-gate 	autoload_tries = 12;	/* traditional default of ~2.5 minutes */
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate 	sa.sa_handler = onintr;
156*0Sstevel@tonic-gate 	sa.sa_flags = SA_RESTART;
157*0Sstevel@tonic-gate 	(void) sigemptyset(&sa.sa_mask);
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate 	(void) sigaction(SIGINT, &sa, &osa);
160*0Sstevel@tonic-gate 	if (osa.sa_handler == SIG_IGN)
161*0Sstevel@tonic-gate 		(void) sigaction(SIGINT, &osa, (struct sigaction *)0);
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate 	(void) sigaction(SIGTERM, &sa, &osa);
164*0Sstevel@tonic-gate 	if (osa.sa_handler == SIG_IGN)
165*0Sstevel@tonic-gate 		(void) sigaction(SIGTERM, &osa, (struct sigaction *)0);
166*0Sstevel@tonic-gate 	if (argc < 2) {
167*0Sstevel@tonic-gate usage:
168*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Usage:\n\
169*0Sstevel@tonic-gate \t%s tabcdfhsvyLloT [file file ...]\n\
170*0Sstevel@tonic-gate \t%s xabcdfhmsvyLloT [file file ...]\n\
171*0Sstevel@tonic-gate \t%s iabcdfhmsvyLloT\n\
172*0Sstevel@tonic-gate \t%s rabcdfsvyLloT\n\
173*0Sstevel@tonic-gate \t%s RabcdfsvyLloT\n\n\
174*0Sstevel@tonic-gate a requires an archive file name\n\
175*0Sstevel@tonic-gate b requires a blocking factor\n\
176*0Sstevel@tonic-gate f requires a dump file\n\
177*0Sstevel@tonic-gate s requires a file number\n\
178*0Sstevel@tonic-gate L requires a tape label\n\
179*0Sstevel@tonic-gate If set, the envar TMPDIR selects where temporary files are kept\n"),
180*0Sstevel@tonic-gate 		    progname, progname, progname, progname, progname);
181*0Sstevel@tonic-gate 		done(1);
182*0Sstevel@tonic-gate 	}
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate 	argv++;			/* the bag-of-options */
185*0Sstevel@tonic-gate 	argc -= 2;		/* count of parameters to the options  */
186*0Sstevel@tonic-gate 	command = '\0';
187*0Sstevel@tonic-gate 	c_label = (char *)NULL;	/* any tape's acceptable */
188*0Sstevel@tonic-gate 	for (cp = *argv++; *cp; cp++) {
189*0Sstevel@tonic-gate 		switch (*cp) {		/* BE CAUTIOUS OF FALLTHROUGHS */
190*0Sstevel@tonic-gate 		case 'T':
191*0Sstevel@tonic-gate 			if (argc < 1) {
192*0Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
193*0Sstevel@tonic-gate 				    "Missing autoload timeout period\n"));
194*0Sstevel@tonic-gate 				done(1);
195*0Sstevel@tonic-gate 			}
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate 			count = atoi(*argv);
198*0Sstevel@tonic-gate 			if (count < 1) {
199*0Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
200*0Sstevel@tonic-gate 			    "Unreasonable autoload timeout period `%s'\n"),
201*0Sstevel@tonic-gate 					*argv);
202*0Sstevel@tonic-gate 				done(1);
203*0Sstevel@tonic-gate 			}
204*0Sstevel@tonic-gate 			units = *(*argv + strlen(*argv) - 1);
205*0Sstevel@tonic-gate 			switch (units) {
206*0Sstevel@tonic-gate 			case 's':
207*0Sstevel@tonic-gate 				multiplier = 1;
208*0Sstevel@tonic-gate 				break;
209*0Sstevel@tonic-gate 			case 'h':
210*0Sstevel@tonic-gate 				multiplier = 3600;
211*0Sstevel@tonic-gate 				break;
212*0Sstevel@tonic-gate 			case '0': case '1': case '2': case '3': case '4':
213*0Sstevel@tonic-gate 			case '5': case '6': case '7': case '8': case '9':
214*0Sstevel@tonic-gate 			case 'm':
215*0Sstevel@tonic-gate 				multiplier = 60;
216*0Sstevel@tonic-gate 				break;
217*0Sstevel@tonic-gate 			default:
218*0Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
219*0Sstevel@tonic-gate 				    "Unknown timeout units indicator `%c'\n"),
220*0Sstevel@tonic-gate 				    units);
221*0Sstevel@tonic-gate 				done(1);
222*0Sstevel@tonic-gate 			}
223*0Sstevel@tonic-gate 			autoload_tries = 1 +
224*0Sstevel@tonic-gate 			    ((count * multiplier) / autoload_period);
225*0Sstevel@tonic-gate 			argv++;
226*0Sstevel@tonic-gate 			argc--;
227*0Sstevel@tonic-gate 			break;
228*0Sstevel@tonic-gate 		case 'l':
229*0Sstevel@tonic-gate 			autoload++;
230*0Sstevel@tonic-gate 			/*FALLTHROUGH*/
231*0Sstevel@tonic-gate 		case 'o':
232*0Sstevel@tonic-gate 			offline++;
233*0Sstevel@tonic-gate 			break;
234*0Sstevel@tonic-gate 		case '-':
235*0Sstevel@tonic-gate 			break;
236*0Sstevel@tonic-gate 		case 'a':
237*0Sstevel@tonic-gate 			if (argc < 1) {
238*0Sstevel@tonic-gate 				(void) fprintf(stderr,
239*0Sstevel@tonic-gate 					gettext("missing archive file name\n"));
240*0Sstevel@tonic-gate 				done(1);
241*0Sstevel@tonic-gate 			}
242*0Sstevel@tonic-gate 			archivefile = *argv++;
243*0Sstevel@tonic-gate 			if (*archivefile == '\0') {
244*0Sstevel@tonic-gate 				(void) fprintf(stderr,
245*0Sstevel@tonic-gate 				    gettext("empty archive file name\n"));
246*0Sstevel@tonic-gate 				done(1);
247*0Sstevel@tonic-gate 			}
248*0Sstevel@tonic-gate 			argc--;
249*0Sstevel@tonic-gate 			break;
250*0Sstevel@tonic-gate 		case 'c':
251*0Sstevel@tonic-gate 			cvtflag++;
252*0Sstevel@tonic-gate 			break;
253*0Sstevel@tonic-gate 		case 'd':
254*0Sstevel@tonic-gate 			dflag++;
255*0Sstevel@tonic-gate 			break;
256*0Sstevel@tonic-gate 		case 'D':
257*0Sstevel@tonic-gate 			/*
258*0Sstevel@tonic-gate 			 * This used to be the Dflag, but it doesn't
259*0Sstevel@tonic-gate 			 * hurt to always check, so was removed.  This
260*0Sstevel@tonic-gate 			 * case is here for backward compatability.
261*0Sstevel@tonic-gate 			 */
262*0Sstevel@tonic-gate 			break;
263*0Sstevel@tonic-gate 		case 'h':
264*0Sstevel@tonic-gate 			hflag = 0;
265*0Sstevel@tonic-gate 			break;
266*0Sstevel@tonic-gate 		case 'm':
267*0Sstevel@tonic-gate 			mflag = 0;
268*0Sstevel@tonic-gate 			break;
269*0Sstevel@tonic-gate 		case 'v':
270*0Sstevel@tonic-gate 			vflag++;
271*0Sstevel@tonic-gate 			break;
272*0Sstevel@tonic-gate 		case 'y':
273*0Sstevel@tonic-gate 			yflag++;
274*0Sstevel@tonic-gate 			break;
275*0Sstevel@tonic-gate 		case 'f':
276*0Sstevel@tonic-gate 			if (argc < 1) {
277*0Sstevel@tonic-gate 				(void) fprintf(stderr,
278*0Sstevel@tonic-gate 				    gettext("missing device specifier\n"));
279*0Sstevel@tonic-gate 				done(1);
280*0Sstevel@tonic-gate 			}
281*0Sstevel@tonic-gate 			inputdev = *argv++;
282*0Sstevel@tonic-gate 			if (*inputdev == '\0') {
283*0Sstevel@tonic-gate 				(void) fprintf(stderr,
284*0Sstevel@tonic-gate 				    gettext("empty device specifier\n"));
285*0Sstevel@tonic-gate 				done(1);
286*0Sstevel@tonic-gate 			}
287*0Sstevel@tonic-gate 			fflag++;
288*0Sstevel@tonic-gate 			argc--;
289*0Sstevel@tonic-gate 			break;
290*0Sstevel@tonic-gate 		case 'b':
291*0Sstevel@tonic-gate 			/*
292*0Sstevel@tonic-gate 			 * change default tape blocksize
293*0Sstevel@tonic-gate 			 */
294*0Sstevel@tonic-gate 			bflag++;
295*0Sstevel@tonic-gate 			if (argc < 1) {
296*0Sstevel@tonic-gate 				(void) fprintf(stderr,
297*0Sstevel@tonic-gate 					gettext("missing block size\n"));
298*0Sstevel@tonic-gate 				done(1);
299*0Sstevel@tonic-gate 			}
300*0Sstevel@tonic-gate 			saved_ntrec = ntrec = atoi(*argv++);
301*0Sstevel@tonic-gate 			if (ntrec == 0 || (ntrec&1)) {
302*0Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
303*0Sstevel@tonic-gate 			    "Block size must be a positive, even integer\n"));
304*0Sstevel@tonic-gate 				done(1);
305*0Sstevel@tonic-gate 			}
306*0Sstevel@tonic-gate 			ntrec /= (tp_bsize/DEV_BSIZE);
307*0Sstevel@tonic-gate 			argc--;
308*0Sstevel@tonic-gate 			break;
309*0Sstevel@tonic-gate 		case 's':
310*0Sstevel@tonic-gate 			/*
311*0Sstevel@tonic-gate 			 * dumpnum (skip to) for multifile dump tapes
312*0Sstevel@tonic-gate 			 */
313*0Sstevel@tonic-gate 			if (argc < 1) {
314*0Sstevel@tonic-gate 				(void) fprintf(stderr,
315*0Sstevel@tonic-gate 					gettext("missing dump number\n"));
316*0Sstevel@tonic-gate 				done(1);
317*0Sstevel@tonic-gate 			}
318*0Sstevel@tonic-gate 			dumpnum = atoi(*argv++);
319*0Sstevel@tonic-gate 			if (dumpnum <= 0) {
320*0Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
321*0Sstevel@tonic-gate 			    "Dump number must be a positive integer\n"));
322*0Sstevel@tonic-gate 				done(1);
323*0Sstevel@tonic-gate 			}
324*0Sstevel@tonic-gate 			argc--;
325*0Sstevel@tonic-gate 			break;
326*0Sstevel@tonic-gate 		case 't':
327*0Sstevel@tonic-gate 		case 'R':
328*0Sstevel@tonic-gate 		case 'r':
329*0Sstevel@tonic-gate 		case 'x':
330*0Sstevel@tonic-gate 		case 'i':
331*0Sstevel@tonic-gate 			if (command != '\0') {
332*0Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
333*0Sstevel@tonic-gate 				    "%c and %c are mutually exclusive\n"),
334*0Sstevel@tonic-gate 				    (uchar_t)*cp, (uchar_t)command);
335*0Sstevel@tonic-gate 				goto usage;
336*0Sstevel@tonic-gate 			}
337*0Sstevel@tonic-gate 			command = *cp;
338*0Sstevel@tonic-gate 			break;
339*0Sstevel@tonic-gate 		case 'L':
340*0Sstevel@tonic-gate 			if (argc < 1 || **argv == '\0') {
341*0Sstevel@tonic-gate 				(void) fprintf(stderr,
342*0Sstevel@tonic-gate 				    gettext("Missing tape label name\n"));
343*0Sstevel@tonic-gate 				done(1);
344*0Sstevel@tonic-gate 			}
345*0Sstevel@tonic-gate 			c_label = *argv++; /* must get tape with this label */
346*0Sstevel@tonic-gate 			if (strlen(c_label) > (sizeof (spcl.c_label) - 1)) {
347*0Sstevel@tonic-gate 				c_label[sizeof (spcl.c_label) - 1] = '\0';
348*0Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
349*0Sstevel@tonic-gate 		    "Truncating label to maximum supported length: `%s'\n"),
350*0Sstevel@tonic-gate 				    c_label);
351*0Sstevel@tonic-gate 			}
352*0Sstevel@tonic-gate 			argc--;
353*0Sstevel@tonic-gate 			break;
354*0Sstevel@tonic-gate 
355*0Sstevel@tonic-gate 		default:
356*0Sstevel@tonic-gate 			(void) fprintf(stderr,
357*0Sstevel@tonic-gate 			    gettext("Bad key character %c\n"), (uchar_t)*cp);
358*0Sstevel@tonic-gate 			goto usage;
359*0Sstevel@tonic-gate 		}
360*0Sstevel@tonic-gate 	}
361*0Sstevel@tonic-gate 	if (command == '\0') {
362*0Sstevel@tonic-gate 		(void) fprintf(stderr,
363*0Sstevel@tonic-gate 		    gettext("must specify i, t, r, R, or x\n"));
364*0Sstevel@tonic-gate 		goto usage;
365*0Sstevel@tonic-gate 	}
366*0Sstevel@tonic-gate 	setinput(inputdev, archivefile);
367*0Sstevel@tonic-gate 	if (argc == 0) {	/* re-use last argv slot for default */
368*0Sstevel@tonic-gate 		argc = 1;
369*0Sstevel@tonic-gate 		*--argv = mflag ? "." : "2";
370*0Sstevel@tonic-gate 	}
371*0Sstevel@tonic-gate 	switch (command) {
372*0Sstevel@tonic-gate 
373*0Sstevel@tonic-gate 	/*
374*0Sstevel@tonic-gate 	 * Interactive mode.
375*0Sstevel@tonic-gate 	 */
376*0Sstevel@tonic-gate 	case 'i':
377*0Sstevel@tonic-gate 		setup();
378*0Sstevel@tonic-gate 		extractdirs(1);
379*0Sstevel@tonic-gate 		initsymtable((char *)0);
380*0Sstevel@tonic-gate 		initpagercmd();
381*0Sstevel@tonic-gate 		runcmdshell();
382*0Sstevel@tonic-gate 		done(0);
383*0Sstevel@tonic-gate 		/* NOTREACHED */
384*0Sstevel@tonic-gate 	/*
385*0Sstevel@tonic-gate 	 * Incremental restoration of a file system.
386*0Sstevel@tonic-gate 	 */
387*0Sstevel@tonic-gate 	case 'r':
388*0Sstevel@tonic-gate 		setup();
389*0Sstevel@tonic-gate 		if (dumptime > 0) {
390*0Sstevel@tonic-gate 			/*
391*0Sstevel@tonic-gate 			 * This is an incremental dump tape.
392*0Sstevel@tonic-gate 			 */
393*0Sstevel@tonic-gate 			vprintf(stdout, gettext("Begin incremental restore\n"));
394*0Sstevel@tonic-gate 			initsymtable(symtbl);
395*0Sstevel@tonic-gate 			extractdirs(1);
396*0Sstevel@tonic-gate 			removeoldleaves();
397*0Sstevel@tonic-gate 			vprintf(stdout, gettext("Calculate node updates.\n"));
398*0Sstevel@tonic-gate 			strcpy(name, ".");
399*0Sstevel@tonic-gate 			name[2] = '\0';
400*0Sstevel@tonic-gate 			treescan(name, ROOTINO, nodeupdates);
401*0Sstevel@tonic-gate 			attrscan(1, nodeupdates);
402*0Sstevel@tonic-gate 			findunreflinks();
403*0Sstevel@tonic-gate 			removeoldnodes();
404*0Sstevel@tonic-gate 		} else {
405*0Sstevel@tonic-gate 			/*
406*0Sstevel@tonic-gate 			 * This is a level zero dump tape.
407*0Sstevel@tonic-gate 			 */
408*0Sstevel@tonic-gate 			vprintf(stdout, gettext("Begin level 0 restore\n"));
409*0Sstevel@tonic-gate 			initsymtable((char *)0);
410*0Sstevel@tonic-gate 			extractdirs(1);
411*0Sstevel@tonic-gate 			vprintf(stdout,
412*0Sstevel@tonic-gate 			    gettext("Calculate extraction list.\n"));
413*0Sstevel@tonic-gate 			strcpy(name, ".");
414*0Sstevel@tonic-gate 			name[2] = '\0';
415*0Sstevel@tonic-gate 			treescan(name, ROOTINO, nodeupdates);
416*0Sstevel@tonic-gate 			attrscan(1, nodeupdates);
417*0Sstevel@tonic-gate 		}
418*0Sstevel@tonic-gate 		createleaves(symtbl);
419*0Sstevel@tonic-gate 		createlinks();
420*0Sstevel@tonic-gate 		setdirmodes();
421*0Sstevel@tonic-gate 		checkrestore();
422*0Sstevel@tonic-gate 		if (dflag) {
423*0Sstevel@tonic-gate 			vprintf(stdout,
424*0Sstevel@tonic-gate 			    gettext("Verify the directory structure\n"));
425*0Sstevel@tonic-gate 			strcpy(name, ".");
426*0Sstevel@tonic-gate 			name[2] = '\0';
427*0Sstevel@tonic-gate 			treescan(name, ROOTINO, verifyfile);
428*0Sstevel@tonic-gate 		}
429*0Sstevel@tonic-gate 		dumpsymtable(symtbl, (long)1);
430*0Sstevel@tonic-gate 		done(0);
431*0Sstevel@tonic-gate 		/* NOTREACHED */
432*0Sstevel@tonic-gate 	/*
433*0Sstevel@tonic-gate 	 * Resume an incremental file system restoration.
434*0Sstevel@tonic-gate 	 */
435*0Sstevel@tonic-gate 	case 'R':
436*0Sstevel@tonic-gate 		setupR();
437*0Sstevel@tonic-gate 		initsymtable(symtbl);
438*0Sstevel@tonic-gate 		skipmaps();
439*0Sstevel@tonic-gate 		skipdirs();
440*0Sstevel@tonic-gate 		createleaves(symtbl);
441*0Sstevel@tonic-gate 		createlinks();
442*0Sstevel@tonic-gate 		setdirmodes();
443*0Sstevel@tonic-gate 		checkrestore();
444*0Sstevel@tonic-gate 		dumpsymtable(symtbl, (long)1);
445*0Sstevel@tonic-gate 		done(0);
446*0Sstevel@tonic-gate 		/* NOTREACHED */
447*0Sstevel@tonic-gate 	/*
448*0Sstevel@tonic-gate 	 * List contents of tape.
449*0Sstevel@tonic-gate 	 */
450*0Sstevel@tonic-gate 	case 't':
451*0Sstevel@tonic-gate 		setup();
452*0Sstevel@tonic-gate 		extractdirs(0);
453*0Sstevel@tonic-gate 		initsymtable((char *)0);
454*0Sstevel@tonic-gate 		if (vflag)
455*0Sstevel@tonic-gate 			printdumpinfo();
456*0Sstevel@tonic-gate 		while (argc--) {
457*0Sstevel@tonic-gate 			canon(*argv++, name, sizeof (name));
458*0Sstevel@tonic-gate 			name[strlen(name)+1] = '\0';
459*0Sstevel@tonic-gate 			ino = dirlookup(name);
460*0Sstevel@tonic-gate 			if (ino == 0)
461*0Sstevel@tonic-gate 				continue;
462*0Sstevel@tonic-gate 			treescan(name, ino, listfile);
463*0Sstevel@tonic-gate 		}
464*0Sstevel@tonic-gate 		done(0);
465*0Sstevel@tonic-gate 		/* NOTREACHED */
466*0Sstevel@tonic-gate 	/*
467*0Sstevel@tonic-gate 	 * Batch extraction of tape contents.
468*0Sstevel@tonic-gate 	 */
469*0Sstevel@tonic-gate 	case 'x':
470*0Sstevel@tonic-gate 		setup();
471*0Sstevel@tonic-gate 		extractdirs(1);
472*0Sstevel@tonic-gate 		initsymtable((char *)0);
473*0Sstevel@tonic-gate 		while (argc--) {
474*0Sstevel@tonic-gate 			if (mflag) {
475*0Sstevel@tonic-gate 				canon(*argv++, name, sizeof (name));
476*0Sstevel@tonic-gate 				if (expand(name, 0, &alist) == 0) {
477*0Sstevel@tonic-gate 					/* no meta-characters to expand */
478*0Sstevel@tonic-gate 					ino = dirlookup(name);
479*0Sstevel@tonic-gate 					if (ino == 0)
480*0Sstevel@tonic-gate 						continue;
481*0Sstevel@tonic-gate 					pathcheck(name);
482*0Sstevel@tonic-gate 				} else {
483*0Sstevel@tonic-gate 					/* add each of the expansions */
484*0Sstevel@tonic-gate 					while ((alist.last - alist.head) > 0) {
485*0Sstevel@tonic-gate 						fname = alist.head->fname;
486*0Sstevel@tonic-gate 						ino = dirlookup(fname);
487*0Sstevel@tonic-gate 						if (ino != 0) {
488*0Sstevel@tonic-gate 							pathcheck(fname);
489*0Sstevel@tonic-gate 							treescan(fname, ino,
490*0Sstevel@tonic-gate 							    addfile);
491*0Sstevel@tonic-gate 						}
492*0Sstevel@tonic-gate 						freename(fname);
493*0Sstevel@tonic-gate 						alist.head++;
494*0Sstevel@tonic-gate 					}
495*0Sstevel@tonic-gate 					alist.head = (struct afile *)NULL;
496*0Sstevel@tonic-gate 					continue; /* argc loop */
497*0Sstevel@tonic-gate 				}
498*0Sstevel@tonic-gate 			} else {
499*0Sstevel@tonic-gate 				ino = (ino_t)atol(*argv);
500*0Sstevel@tonic-gate 				if ((*(*argv++) == '-') || ino < ROOTINO) {
501*0Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
502*0Sstevel@tonic-gate 					    "bad inode number: %ld\n"),
503*0Sstevel@tonic-gate 					    ino);
504*0Sstevel@tonic-gate 					done(1);
505*0Sstevel@tonic-gate 				}
506*0Sstevel@tonic-gate 				name[0] = '\0';
507*0Sstevel@tonic-gate 			}
508*0Sstevel@tonic-gate 			treescan(name, ino, addfile);
509*0Sstevel@tonic-gate 			attrscan(0, addfile);
510*0Sstevel@tonic-gate 		}
511*0Sstevel@tonic-gate 		createfiles();
512*0Sstevel@tonic-gate 		createlinks();
513*0Sstevel@tonic-gate 		setdirmodes();
514*0Sstevel@tonic-gate 		if (dflag)
515*0Sstevel@tonic-gate 			checkrestore();
516*0Sstevel@tonic-gate 		done(0);
517*0Sstevel@tonic-gate 		/* NOTREACHED */
518*0Sstevel@tonic-gate 	}
519*0Sstevel@tonic-gate #ifdef lint
520*0Sstevel@tonic-gate 	return (0);
521*0Sstevel@tonic-gate #endif
522*0Sstevel@tonic-gate }
523*0Sstevel@tonic-gate 
524*0Sstevel@tonic-gate /*
525*0Sstevel@tonic-gate  * Determine where the user wants us to put our temporary files,
526*0Sstevel@tonic-gate  * and make sure we can actually do so.  Bail out if there's a problem.
527*0Sstevel@tonic-gate  */
528*0Sstevel@tonic-gate void
529*0Sstevel@tonic-gate set_tmpdir(void)
530*0Sstevel@tonic-gate {
531*0Sstevel@tonic-gate 	int fd;
532*0Sstevel@tonic-gate 	char name[MAXPATHLEN];
533*0Sstevel@tonic-gate 
534*0Sstevel@tonic-gate 	tmpdir = getenv("TMPDIR");
535*0Sstevel@tonic-gate 	if ((tmpdir == (char *)NULL) || (*tmpdir == '\0'))
536*0Sstevel@tonic-gate 		tmpdir = "/tmp";
537*0Sstevel@tonic-gate 
538*0Sstevel@tonic-gate 	if (*tmpdir != '/') {
539*0Sstevel@tonic-gate 		(void) fprintf(stderr,
540*0Sstevel@tonic-gate 		    gettext("TMPDIR is not an absolute path (`%s').\n"),
541*0Sstevel@tonic-gate 		    tmpdir);
542*0Sstevel@tonic-gate 		done(1);
543*0Sstevel@tonic-gate 	}
544*0Sstevel@tonic-gate 
545*0Sstevel@tonic-gate 	/*
546*0Sstevel@tonic-gate 	 * The actual use of tmpdir is in dirs.c, and is of the form
547*0Sstevel@tonic-gate 	 * tmpdir + "/rst" + type (three characters) + "%ld.XXXXXX" +
548*0Sstevel@tonic-gate 	 * a trailing NUL, where %ld is an arbitrary time_t.
549*0Sstevel@tonic-gate 	 *
550*0Sstevel@tonic-gate 	 * Thus, the magic 31 is strlen(itoa(MAX_TIME_T)) + "/rst" +
551*0Sstevel@tonic-gate 	 * ".XXXXXX" + '\0'.  A time_t is 64 bits, so MAX_TIME_T is
552*0Sstevel@tonic-gate 	 * LONG_MAX - nineteen digits.  In theory, so many things in
553*0Sstevel@tonic-gate 	 * ufsrestore will break once time_t's value goes beyond 32
554*0Sstevel@tonic-gate 	 * bits that it's not worth worrying about this particular
555*0Sstevel@tonic-gate 	 * instance at this time, but we've got to start somewhere.
556*0Sstevel@tonic-gate 	 *
557*0Sstevel@tonic-gate 	 * Note that the use of a pid below is just for testing the
558*0Sstevel@tonic-gate 	 * validity of the named directory.
559*0Sstevel@tonic-gate 	 */
560*0Sstevel@tonic-gate 	if (strlen(tmpdir) > (MAXPATHLEN - 31)) {
561*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("TMPDIR too long\n"));
562*0Sstevel@tonic-gate 		done(1);
563*0Sstevel@tonic-gate 	}
564*0Sstevel@tonic-gate 
565*0Sstevel@tonic-gate 	/* Guaranteed to fit by above test (sizeof(time_t) >= sizeof(pid_t)) */
566*0Sstevel@tonic-gate 	(void) snprintf(name, sizeof (name), "%s/rstdir.%ld", tmpdir, getpid());
567*0Sstevel@tonic-gate 
568*0Sstevel@tonic-gate 	/*
569*0Sstevel@tonic-gate 	 * This is effectively a stripped-down version of safe_open(),
570*0Sstevel@tonic-gate 	 * because if the file exists, we want to fail.
571*0Sstevel@tonic-gate 	 */
572*0Sstevel@tonic-gate 	fd = open(name, O_CREAT|O_EXCL|O_RDWR, 0600);
573*0Sstevel@tonic-gate 	if (fd < 0) {
574*0Sstevel@tonic-gate 		perror(gettext("Can not create temporary file"));
575*0Sstevel@tonic-gate 		done(1);
576*0Sstevel@tonic-gate 	}
577*0Sstevel@tonic-gate 
578*0Sstevel@tonic-gate 	(void) close(fd);
579*0Sstevel@tonic-gate 	if (unlink(name) < 0) {
580*0Sstevel@tonic-gate 		perror(gettext("Can not delete temporary file"));
581*0Sstevel@tonic-gate 		done(1);
582*0Sstevel@tonic-gate 	}
583*0Sstevel@tonic-gate }
584