1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  */
6 
7 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
8 /*	  All Rights Reserved	*/
9 
10 /*
11  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
12  * Use is subject to license terms.
13  */
14 
15 #ifndef _RESTORE_H
16 #define	_RESTORE_H
17 
18 #include <stdio.h>
19 #include <string.h>
20 #include <malloc.h>
21 #include <netdb.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <sys/stat.h>
26 #include <sys/param.h>
27 #include <sys/time.h>
28 #include <sys/vnode.h>
29 #include <locale.h>
30 #include <stdlib.h>
31 #include <sys/fs/ufs_inode.h>
32 #include <sys/fs/ufs_fs.h>
33 #include <sys/fs/ufs_fsdir.h>
34 #include <note.h>
35 
36 #ifdef	__cplusplus
37 extern "C" {
38 #endif
39 
40 #define	ROOTINO	UFSROOTINO
41 #define	SUPPORTS_MTB_TAPE_FORMAT
42 #include <protocols/dumprestore.h>
43 #include <memutils.h>
44 #include <assert.h>
45 
46 /*
47  * Flags
48  */
49 extern int	cvtflag;	/* convert from old to new tape format */
50 extern int	bflag;		/* set input block size */
51 extern int	dflag;		/* print out debugging info */
52 extern int	hflag;		/* restore heirarchies */
53 extern int	mflag;		/* restore by name instead of inode number */
54 extern int	vflag;		/* print out actions taken */
55 extern int	yflag;		/* always try to recover from tape errors */
56 extern int	paginating;	/* paginate bulk interactive output */
57 extern int	offline;	/* take tape offline when closing */
58 extern int	autoload;	/* wait for tape to autoload; implies offline */
59 /*
60  * Global variables
61  */
62 extern int	autoload_tries;	/* number of times to check on autoload */
63 extern int	autoload_period; /* seconds, tries*period = total wait time */
64 extern struct byteorder_ctx *byteorder;
65 extern char	*progname;	/* our name */
66 extern char	*dumpmap;	/* map of inodes on this dump tape */
67 extern char	*clrimap;	/* map of inodes to be deleted */
68 extern char	*c_label;	/* label we expect to see on the tape */
69 extern ino_t	maxino;		/* highest numbered inode in this file system */
70 extern long	dumpnum;	/* location of the dump on this tape */
71 extern int	volno;		/* current volume being read */
72 extern uint_t	ntrec;		/* number of tp_bsize records per tape block */
73 extern uint_t	saved_ntrec;	/* number of tp_bsize records per tape block */
74 extern ssize_t	tape_rec_size;	/* tape record size (tp_bsize * ntrec) */
75 extern time_t	dumptime;	/* time that this dump begins */
76 extern time_t	dumpdate;	/* time that this dump was made */
77 extern char	command;	/* opration being performed */
78 extern FILE	*terminal;	/* file descriptor for the terminal input */
79 extern char	*tmpdir;	/* where to put the rst{dir,mode}... files */
80 extern char	*pager_catenated; /* pager command and args */
81 extern char	**pager_vector;	/* pager_catenated split up for execve() */
82 extern int	pager_len;	/* # elements in pager_vector; includes NULL */
83 extern int	inattrspace;	/* true if currently scanning attribute space */
84 extern int	savepwd;	/* this is where restore is running from */
85 
86 /*
87  * Each file in the file system is described by one of these entries
88  * Note that the e_next field is used by the symbol table hash lists
89  * and then reused by the remove code after the entry is removed from
90  * the symbol table.
91  */
92 struct entry {
93 	char	*e_name;		/* the current name of this entry */
94 	ushort_t e_namlen;		/* length of this name */
95 	char	e_type;			/* type of this entry, see below */
96 	short	e_flags;		/* status flags, see below */
97 	ino_t	e_ino;			/* inode number in previous file sys */
98 	long	e_index;		/* unique index (for dumpped table) */
99 	struct	entry *e_parent;	/* pointer to parent directory (..) */
100 	struct	entry *e_sibling;	/* next element in this directory (.) */
101 	struct	entry *e_links;		/* hard links to this inode */
102 	struct	entry *e_entries;	/* for directories, their entries */
103 	struct	entry *e_xattrs;	/* pointer to extended attribute root */
104 	struct	entry *e_next;		/* hash chain list and removelist */
105 };
106 /* types */
107 #define	LEAF 1			/* non-directory entry */
108 #define	NODE 2			/* directory entry */
109 #define	LINK 4			/* synthesized type, stripped by addentry */
110 #define	ROOT 8			/* synthesized type, stripped by addentry */
111 /* flags */
112 #define	EXTRACT		0x0001	/* entry is to be replaced from the tape */
113 #define	NEW		0x0002	/* a new entry to be extracted */
114 #define	KEEP		0x0004	/* entry is not to change */
115 #define	REMOVED		0x0010	/* entry has been removed */
116 #define	TMPNAME		0x0020	/* entry has been given a temporary name */
117 #define	EXISTED		0x0040	/* directory already existed during extract */
118 #define	XATTR		0x0080	/* file belongs in an attribute tree */
119 #define	XATTRROOT	0x0100	/* directory is root of an attribute tree */
120 /*
121  * functions defined on entry structs
122  */
123 extern struct entry *lookupino(ino_t);
124 extern struct entry *lookupname(char *);
125 extern struct entry *addentry(char *, ino_t, int);
126 extern void deleteino(ino_t);
127 extern char *myname(struct entry *);
128 extern void freeentry(struct entry *);
129 extern void moveentry(struct entry *, char *);
130 extern char *savename(char *);
131 extern void freename(char *);
132 extern void dumpsymtable(char *, int);
133 extern void initsymtable(char *);
134 extern void mktempname(struct entry *);
135 extern char *gentempname(struct entry *);
136 extern void newnode(struct entry *);
137 extern void removenode(struct entry *);
138 extern void removeleaf(struct entry *);
139 extern ino_t lowerbnd(ino_t);
140 extern ino_t upperbnd(ino_t);
141 extern void badentry(struct entry *, char *);
142 extern char *flagvalues(struct entry *);
143 extern ino_t dirlookup(char *);
144 #define	NIL ((struct entry *)(0))
145 
146 /*
147  * Definitions for library routines operating on directories.
148  * These definitions are used only for reading fake directory
149  * entries from restore's temporary file "restoresymtable"
150  * These have little to do with real directory entries.
151  */
152 #if !defined(DEV_BSIZE)
153 #define	DEV_BSIZE	512
154 #endif
155 #define	DIRBLKSIZ	DEV_BSIZE
156 typedef struct _rstdirdesc {
157 	int	dd_fd;
158 	int	dd_refcnt;  /* so rst_{open,close}dir() avoid leaking memory */
159 	off64_t	dd_loc;
160 	off64_t	dd_size;
161 	char	dd_buf[DIRBLKSIZ];
162 } RST_DIR;
163 
164 /*
165  * Constants associated with entry structs
166  */
167 #define	HARDLINK	1
168 #define	SYMLINK		2
169 #define	TMPHDR		"RSTTMP"
170 
171 /*
172  * The entry describes the next file available on the tape
173  */
174 struct context {
175 	char	*name;		/* name of file */
176 	ino_t	ino;		/* inumber of file */
177 	struct	dinode *dip;	/* pointer to inode */
178 	int	action;		/* action being taken on this file */
179 	int	ts;		/* TS_* type of tape record */
180 } curfile;
181 /* actions */
182 #define	USING	1	/* extracting from the tape */
183 #define	SKIP	2	/* skipping */
184 #define	UNKNOWN 3	/* disposition or starting point is unknown */
185 
186 /*
187  * Structure and routines associated with listing directories
188  * and expanding meta-characters in pathnames.
189  */
190 struct afile {
191 	ino_t	fnum;		/* inode number of file */
192 	char	*fname;		/* file name */
193 	short	fflags;		/* extraction flags, if any */
194 	char	ftype;		/* file type, e.g. LEAF or NODE */
195 };
196 struct arglist {
197 	struct afile	*head;	/* start of argument list */
198 	struct afile	*last;	/* end of argument list */
199 	struct afile	*base;	/* current list arena */
200 	int		nent;	/* maximum size of list */
201 	char		*cmd;	/* the current command */
202 };
203 
204 /*
205  * Other exported routines
206  */
207 #ifdef __STDC__
208 extern int mkentry(char *, ino_t, struct arglist *);
209 extern int expand(char *, int, struct arglist *);
210 extern ino_t psearch(char *);
211 extern void metaget(char **data, size_t *size);
212 extern void metaproc(char *, char *, size_t);
213 extern long listfile(char *, ino_t, int);
214 extern long addfile(char *, ino_t, int);
215 extern long deletefile(char *, ino_t, int);
216 extern long nodeupdates(char *, ino_t, int);
217 extern long verifyfile(char *, ino_t, int);
218 extern void extractdirs(int genmode);
219 extern void skipdirs(void);
220 extern void treescan(char *, ino_t, long (*)(char *, ino_t, int));
221 extern RST_DIR *rst_opendir(char *);
222 extern void rst_closedir(RST_DIR *);
223 extern struct direct *rst_readdir(RST_DIR *);
224 extern void setdirmodes(void);
225 extern int genliteraldir(char *, ino_t);
226 extern int inodetype(ino_t);
227 extern void done(int) __NORETURN;
228 extern void runcmdshell(void);
229 extern void canon(char *, char *, size_t);
230 extern void onintr(int);
231 extern void removeoldleaves(void);
232 extern void findunreflinks(void);
233 extern void removeoldnodes(void);
234 extern void createleaves(char *);
235 extern void createfiles(void);
236 extern void createlinks(void);
237 extern void checkrestore(void);
238 extern void setinput(char *, char *);
239 extern void newtapebuf(size_t);
240 extern void setup(void);
241 extern void setupR(void);
242 extern void getvol(int);
243 extern void printdumpinfo(void);
244 extern int extractfile(char *);
245 extern void skipmaps(void);
246 extern void skipfile(void);
247 extern void getfile(void (*)(char *, size_t), void (*)(char *, size_t));
248 extern void null(char *, size_t);
249 extern void findtapeblksize(int);
250 extern void flsht(void);
251 extern void closemt(int);
252 extern int readhdr(struct s_spcl *);
253 extern int gethead(struct s_spcl *);
254 extern int volnumber(ino_t);
255 extern void findinode(struct s_spcl *);
256 extern void pathcheck(char *);
257 extern void renameit(char *, char *);
258 extern int linkit(char *, char *, int);
259 extern int lf_linkit(char *, char *, int);
260 extern int reply(char *);
261 /*PRINTFLIKE1*/
262 extern void panic(const char *, ...);
263 extern char *lctime(time_t *);
264 extern int safe_open(int, const char *file, int mode, int perms);
265 extern FILE *safe_fopen(const char *filename, const char *smode, int perms);
266 extern void reset_dump(void);
267 extern void get_next_device(void);
268 extern void initpagercmd(void);
269 extern void resolve(char *, int *, char **);
270 extern int complexcopy(char *, char *, int);
271 #else	/* !STDC */
272 extern int mkentry();
273 extern int expand();
274 extern ino_t psearch();
275 extern void metaget();
276 extern void metaproc();
277 extern long listfile();
278 extern long addfile();
279 extern long deletefile();
280 extern long nodeupdates();
281 extern long verifyfile();
282 extern void extractdirs();
283 extern void skipdirs();
284 extern void treescan();
285 extern RST_DIR *rst_opendir();
286 extern void rst_closedir();
287 extern struct direct *rst_readdir();
288 extern void setdirmodes();
289 extern int genliteraldir();
290 extern int inodetype();
291 extern void done();
292 extern void runcmdshell();
293 extern void canon();
294 extern void onintr();
295 extern void removeoldleaves();
296 extern void findunreflinks();
297 extern void removeoldnodes();
298 extern void createleaves();
299 extern void createfiles();
300 extern void createlinks();
301 extern void checkrestore();
302 extern void setinput();
303 extern void newtapebuf();
304 extern void setup();
305 extern void setupR();
306 extern void getvol();
307 extern void printdumpinfo();
308 extern int extractfile();
309 extern void skipmaps();
310 extern void skipfile();
311 extern void getfile();
312 extern void null();
313 extern void findtapeblksize();
314 extern void flsht();
315 extern void closemt();
316 extern int readhdr();
317 extern int gethead();
318 extern int volnumber();
319 extern void findinode();
320 extern void pathcheck();
321 extern void renameit();
322 extern int linkit();
323 extern int lf_linkit();
324 extern int reply();
325 extern void panic();
326 extern char *lctime();
327 extern int safe_open();
328 extern FILE *safe_fopen();
329 extern void reset_dump();
330 extern void get_next_device();
331 extern void initpagercmd();
332 extern void resolve();
333 extern int complexcopy();
334 #endif	/* STDC */
335 
336 /*
337  * Useful macros
338  */
339 #define	MWORD(m, i)	((m)[(ino_t)((i)-1)/NBBY])
340 #define	MBIT(i)		(1<<((ino_t)((i)-1)%NBBY))
341 #define	BIS(i, w)	(MWORD((w), (i)) |=  MBIT(i))
342 #define	BIC(i, w)	(MWORD((w), (i)) &= ~MBIT(i))
343 #define	BIT(i, w)	(MWORD((w), (i)) & MBIT(i))
344 
345 /*
346  * Macro used to get to the last segment of a complex string
347  */
348 #define	LASTPART(s)	{int len = strlen(s)+1;\
349 				while (s[len] != '\0')\
350 					{s += len; len = strlen(s)+1; }\
351 			}
352 
353 /*
354  * Define maximum length of complex string.  For now we use
355  * MAXPATHLEN * 2 since recursion is not (yet) supported.
356  * (add 3 for the 3 NULL characters in a two-part path)
357  * Note that each component of a complex string is still
358  * limited to MAXPATHLEN length.
359  */
360 #define	MAXCOMPLEXLEN	(MAXPATHLEN*2 + 3)
361 
362 /*
363  * Define an overflow-free version of howmany so that we don't
364  * run into trouble with large files.
365  */
366 #define	d_howmany(x, y)	((x) / (y) + ((x) % (y) != 0))
367 
368 /*
369  * Defines used by findtapeblksize()
370  */
371 #define	TAPE_FILE	0
372 #define	ARCHIVE_FILE	1
373 
374 #undef	setjmp
375 #define	setjmp(b)		sigsetjmp((b), 1)
376 #define	longjmp			siglongjmp
377 #define	jmp_buf			sigjmp_buf
378 #define	chown			lchown
379 
380 /*
381  * Defaults
382  */
383 #define	TAPE	"/dev/rmt/0b"		/* default tape device */
384 #define	RESTORESYMTABLE	"./restoresymtable"
385 
386 #define	dprintf		if (dflag) (void) fprintf
387 #define	vprintf		if (vflag) (void) fprintf
388 
389 #define	GOOD 1
390 #define	FAIL 0
391 
392 #define	ALLOW_OFFLINE	0
393 #define	FORCE_OFFLINE	1		/* offline drive for autoload */
394 
395 #define	DEF_PAGER	"/usr/bin/more"
396 
397 #ifdef	__cplusplus
398 }
399 #endif
400 
401 #endif /* _RESTORE_H */
402