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