xref: /illumos-gate/usr/src/cmd/backup/restore/restore.h (revision 2c5ec7a875dcd76853e6618614e990f1e8cdd56d)
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 #ifdef __STDC__
124 extern struct entry *lookupino(ino_t);
125 extern struct entry *lookupname(char *);
126 extern struct entry *addentry(char *, ino_t, int);
127 extern void deleteino(ino_t);
128 extern char *myname(struct entry *);
129 extern void freeentry(struct entry *);
130 extern void moveentry(struct entry *, char *);
131 extern char *savename(char *);
132 extern void freename(char *);
133 extern void dumpsymtable(char *, int);
134 extern void initsymtable(char *);
135 extern void mktempname(struct entry *);
136 extern char *gentempname(struct entry *);
137 extern void newnode(struct entry *);
138 extern void removenode(struct entry *);
139 extern void removeleaf(struct entry *);
140 extern ino_t lowerbnd(ino_t);
141 extern ino_t upperbnd(ino_t);
142 extern void badentry(struct entry *, char *);
143 extern char *flagvalues(struct entry *);
144 extern ino_t dirlookup(char *);
145 #else
146 extern struct entry *lookupino();
147 extern struct entry *lookupname();
148 extern struct entry *addentry();
149 extern void deleteino();
150 extern char *myname();
151 extern void freeentry();
152 extern void moveentry();
153 extern char *savename();
154 extern void freename();
155 extern void dumpsymtable();
156 extern void initsymtable();
157 extern void mktempname();
158 extern char *gentempname();
159 extern void newnode();
160 extern void removenode();
161 extern void removeleaf();
162 extern ino_t lowerbnd();
163 extern ino_t upperbnd();
164 extern void badentry();
165 extern char *flagvalues();
166 extern ino_t dirlookup();
167 #endif
168 #define	NIL ((struct entry *)(0))
169 
170 /*
171  * Definitions for library routines operating on directories.
172  * These definitions are used only for reading fake directory
173  * entries from restore's temporary file "restoresymtable"
174  * These have little to do with real directory entries.
175  */
176 #if !defined(DEV_BSIZE)
177 #define	DEV_BSIZE	512
178 #endif
179 #define	DIRBLKSIZ	DEV_BSIZE
180 typedef struct _rstdirdesc {
181 	int	dd_fd;
182 	int	dd_refcnt;  /* so rst_{open,close}dir() avoid leaking memory */
183 	off64_t	dd_loc;
184 	off64_t	dd_size;
185 	char	dd_buf[DIRBLKSIZ];
186 } RST_DIR;
187 
188 /*
189  * Constants associated with entry structs
190  */
191 #define	HARDLINK	1
192 #define	SYMLINK		2
193 #define	TMPHDR		"RSTTMP"
194 
195 /*
196  * The entry describes the next file available on the tape
197  */
198 struct context {
199 	char	*name;		/* name of file */
200 	ino_t	ino;		/* inumber of file */
201 	struct	dinode *dip;	/* pointer to inode */
202 	int	action;		/* action being taken on this file */
203 	int	ts;		/* TS_* type of tape record */
204 } curfile;
205 /* actions */
206 #define	USING	1	/* extracting from the tape */
207 #define	SKIP	2	/* skipping */
208 #define	UNKNOWN 3	/* disposition or starting point is unknown */
209 
210 /*
211  * Structure and routines associated with listing directories
212  * and expanding meta-characters in pathnames.
213  */
214 struct afile {
215 	ino_t	fnum;		/* inode number of file */
216 	char	*fname;		/* file name */
217 	short	fflags;		/* extraction flags, if any */
218 	char	ftype;		/* file type, e.g. LEAF or NODE */
219 };
220 struct arglist {
221 	struct afile	*head;	/* start of argument list */
222 	struct afile	*last;	/* end of argument list */
223 	struct afile	*base;	/* current list arena */
224 	int		nent;	/* maximum size of list */
225 	char		*cmd;	/* the current command */
226 };
227 
228 /*
229  * Other exported routines
230  */
231 #ifdef __STDC__
232 extern int mkentry(char *, ino_t, struct arglist *);
233 extern int expand(char *, int, struct arglist *);
234 extern ino_t psearch(char *);
235 extern void metaget(char **data, size_t *size);
236 extern void metaproc(char *, char *, size_t);
237 extern long listfile(char *, ino_t, int);
238 extern long addfile(char *, ino_t, int);
239 extern long deletefile(char *, ino_t, int);
240 extern long nodeupdates(char *, ino_t, int);
241 extern long verifyfile(char *, ino_t, int);
242 extern void extractdirs(int genmode);
243 extern void skipdirs(void);
244 extern void treescan(char *, ino_t, long (*)(char *, ino_t, int));
245 extern RST_DIR *rst_opendir(char *);
246 extern void rst_closedir(RST_DIR *);
247 extern struct direct *rst_readdir(RST_DIR *);
248 extern void setdirmodes(void);
249 extern int genliteraldir(char *, ino_t);
250 extern int inodetype(ino_t);
251 extern void done(int) __NORETURN;
252 extern void runcmdshell(void);
253 extern void canon(char *, char *, size_t);
254 extern void onintr(int);
255 extern void removeoldleaves(void);
256 extern void findunreflinks(void);
257 extern void removeoldnodes(void);
258 extern void createleaves(char *);
259 extern void createfiles(void);
260 extern void createlinks(void);
261 extern void checkrestore(void);
262 extern void setinput(char *, char *);
263 extern void newtapebuf(size_t);
264 extern void setup(void);
265 extern void setupR(void);
266 extern void getvol(int);
267 extern void printdumpinfo(void);
268 extern int extractfile(char *);
269 extern void skipmaps(void);
270 extern void skipfile(void);
271 extern void getfile(void (*)(char *, size_t), void (*)(char *, size_t));
272 extern void null(char *, size_t);
273 extern void findtapeblksize(int);
274 extern void flsht(void);
275 extern void closemt(int);
276 extern int readhdr(struct s_spcl *);
277 extern int gethead(struct s_spcl *);
278 extern int volnumber(ino_t);
279 extern void findinode(struct s_spcl *);
280 extern void pathcheck(char *);
281 extern void renameit(char *, char *);
282 extern int linkit(char *, char *, int);
283 extern int lf_linkit(char *, char *, int);
284 extern int reply(char *);
285 /*PRINTFLIKE1*/
286 extern void panic(const char *, ...);
287 extern char *lctime(time_t *);
288 extern int safe_open(int, const char *file, int mode, int perms);
289 extern FILE *safe_fopen(const char *filename, const char *smode, int perms);
290 extern void reset_dump(void);
291 extern void get_next_device(void);
292 extern void initpagercmd(void);
293 extern void resolve(char *, int *, char **);
294 extern int complexcopy(char *, char *, int);
295 #else	/* !STDC */
296 extern int mkentry();
297 extern int expand();
298 extern ino_t psearch();
299 extern void metaget();
300 extern void metaproc();
301 extern long listfile();
302 extern long addfile();
303 extern long deletefile();
304 extern long nodeupdates();
305 extern long verifyfile();
306 extern void extractdirs();
307 extern void skipdirs();
308 extern void treescan();
309 extern RST_DIR *rst_opendir();
310 extern void rst_closedir();
311 extern struct direct *rst_readdir();
312 extern void setdirmodes();
313 extern int genliteraldir();
314 extern int inodetype();
315 extern void done();
316 extern void runcmdshell();
317 extern void canon();
318 extern void onintr();
319 extern void removeoldleaves();
320 extern void findunreflinks();
321 extern void removeoldnodes();
322 extern void createleaves();
323 extern void createfiles();
324 extern void createlinks();
325 extern void checkrestore();
326 extern void setinput();
327 extern void newtapebuf();
328 extern void setup();
329 extern void setupR();
330 extern void getvol();
331 extern void printdumpinfo();
332 extern int extractfile();
333 extern void skipmaps();
334 extern void skipfile();
335 extern void getfile();
336 extern void null();
337 extern void findtapeblksize();
338 extern void flsht();
339 extern void closemt();
340 extern int readhdr();
341 extern int gethead();
342 extern int volnumber();
343 extern void findinode();
344 extern void pathcheck();
345 extern void renameit();
346 extern int linkit();
347 extern int lf_linkit();
348 extern int reply();
349 extern void panic();
350 extern char *lctime();
351 extern int safe_open();
352 extern FILE *safe_fopen();
353 extern void reset_dump();
354 extern void get_next_device();
355 extern void initpagercmd();
356 extern void resolve();
357 extern int complexcopy();
358 #endif	/* STDC */
359 
360 /*
361  * Useful macros
362  */
363 #define	MWORD(m, i)	((m)[(ino_t)((i)-1)/NBBY])
364 #define	MBIT(i)		(1<<((ino_t)((i)-1)%NBBY))
365 #define	BIS(i, w)	(MWORD((w), (i)) |=  MBIT(i))
366 #define	BIC(i, w)	(MWORD((w), (i)) &= ~MBIT(i))
367 #define	BIT(i, w)	(MWORD((w), (i)) & MBIT(i))
368 
369 /*
370  * Macro used to get to the last segment of a complex string
371  */
372 #define	LASTPART(s)	{int len = strlen(s)+1;\
373 				while (s[len] != '\0')\
374 					{s += len; len = strlen(s)+1; }\
375 			}
376 
377 /*
378  * Define maximum length of complex string.  For now we use
379  * MAXPATHLEN * 2 since recursion is not (yet) supported.
380  * (add 3 for the 3 NULL characters in a two-part path)
381  * Note that each component of a complex string is still
382  * limited to MAXPATHLEN length.
383  */
384 #define	MAXCOMPLEXLEN	(MAXPATHLEN*2 + 3)
385 
386 /*
387  * Define an overflow-free version of howmany so that we don't
388  * run into trouble with large files.
389  */
390 #define	d_howmany(x, y)	((x) / (y) + ((x) % (y) != 0))
391 
392 /*
393  * Defines used by findtapeblksize()
394  */
395 #define	TAPE_FILE	0
396 #define	ARCHIVE_FILE	1
397 
398 #undef	setjmp
399 #define	setjmp(b)		sigsetjmp((b), 1)
400 #define	longjmp			siglongjmp
401 #define	jmp_buf			sigjmp_buf
402 #define	chown			lchown
403 
404 /*
405  * Defaults
406  */
407 #define	TAPE	"/dev/rmt/0b"		/* default tape device */
408 #define	RESTORESYMTABLE	"./restoresymtable"
409 
410 #define	dprintf		if (dflag) (void) fprintf
411 #define	vprintf		if (vflag) (void) fprintf
412 
413 #define	GOOD 1
414 #define	FAIL 0
415 
416 #define	ALLOW_OFFLINE	0
417 #define	FORCE_OFFLINE	1		/* offline drive for autoload */
418 
419 #define	DEF_PAGER	"/usr/bin/more"
420 
421 #ifdef	__cplusplus
422 }
423 #endif
424 
425 #endif /* _RESTORE_H */
426