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