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
37extern "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 */
49extern int	cvtflag;	/* convert from old to new tape format */
50extern int	bflag;		/* set input block size */
51extern int	dflag;		/* print out debugging info */
52extern int	hflag;		/* restore heirarchies */
53extern int	mflag;		/* restore by name instead of inode number */
54extern int	vflag;		/* print out actions taken */
55extern int	yflag;		/* always try to recover from tape errors */
56extern int	paginating;	/* paginate bulk interactive output */
57extern int	offline;	/* take tape offline when closing */
58extern int	autoload;	/* wait for tape to autoload; implies offline */
59/*
60 * Global variables
61 */
62extern int	autoload_tries;	/* number of times to check on autoload */
63extern int	autoload_period; /* seconds, tries*period = total wait time */
64extern struct byteorder_ctx *byteorder;
65extern char	*progname;	/* our name */
66extern char	*dumpmap; 	/* map of inodes on this dump tape */
67extern char	*clrimap; 	/* map of inodes to be deleted */
68extern char	*c_label;	/* label we expect to see on the tape */
69extern ino_t	maxino;		/* highest numbered inode in this file system */
70extern long	dumpnum;	/* location of the dump on this tape */
71extern int	volno;		/* current volume being read */
72extern uint_t	ntrec;		/* number of tp_bsize records per tape block */
73extern uint_t	saved_ntrec;	/* number of tp_bsize records per tape block */
74extern ssize_t	tape_rec_size;	/* tape record size (tp_bsize * ntrec) */
75extern time_t	dumptime;	/* time that this dump begins */
76extern time_t	dumpdate;	/* time that this dump was made */
77extern char	command;	/* opration being performed */
78extern FILE	*terminal;	/* file descriptor for the terminal input */
79extern char	*tmpdir;	/* where to put the rst{dir,mode}... files */
80extern char	*pager_catenated; /* pager command and args */
81extern char	**pager_vector;	/* pager_catenated split up for execve() */
82extern int	pager_len;	/* # elements in pager_vector; includes NULL */
83extern int	inattrspace;	/* true if currently scanning attribute space */
84extern 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 */
92struct 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__
124extern struct entry *lookupino(ino_t);
125extern struct entry *lookupname(char *);
126extern struct entry *addentry(char *, ino_t, int);
127extern void deleteino(ino_t);
128extern char *myname(struct entry *);
129extern void freeentry(struct entry *);
130extern void moveentry(struct entry *, char *);
131extern char *savename(char *);
132extern void freename(char *);
133extern void dumpsymtable(char *, int);
134extern void initsymtable(char *);
135extern void mktempname(struct entry *);
136extern char *gentempname(struct entry *);
137extern void newnode(struct entry *);
138extern void removenode(struct entry *);
139extern void removeleaf(struct entry *);
140extern ino_t lowerbnd(ino_t);
141extern ino_t upperbnd(ino_t);
142extern void badentry(struct entry *, char *);
143extern char *flagvalues(struct entry *);
144extern ino_t dirlookup(char *);
145#else
146extern struct entry *lookupino();
147extern struct entry *lookupname();
148extern struct entry *addentry();
149extern void deleteino();
150extern char *myname();
151extern void freeentry();
152extern void moveentry();
153extern char *savename();
154extern void freename();
155extern void dumpsymtable();
156extern void initsymtable();
157extern void mktempname();
158extern char *gentempname();
159extern void newnode();
160extern void removenode();
161extern void removeleaf();
162extern ino_t lowerbnd();
163extern ino_t upperbnd();
164extern void badentry();
165extern char *flagvalues();
166extern 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
180typedef 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 */
198struct 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 */
214struct 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};
220struct 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__
232extern int mkentry(char *, ino_t, struct arglist *);
233extern int expand(char *, int, struct arglist *);
234extern ino_t psearch(char *);
235extern void metaget(char **data, size_t *size);
236extern void metaproc(char *, char *, size_t);
237extern long listfile(char *, ino_t, int);
238extern long addfile(char *, ino_t, int);
239extern long deletefile(char *, ino_t, int);
240extern long nodeupdates(char *, ino_t, int);
241extern long verifyfile(char *, ino_t, int);
242extern void extractdirs(int genmode);
243extern void skipdirs(void);
244extern void treescan(char *, ino_t, long (*)(char *, ino_t, int));
245extern RST_DIR *rst_opendir(char *);
246extern void rst_closedir(RST_DIR *);
247extern struct direct *rst_readdir(RST_DIR *);
248extern void setdirmodes(void);
249extern int genliteraldir(char *, ino_t);
250extern int inodetype(ino_t);
251extern void done(int) __NORETURN;
252extern void runcmdshell(void);
253extern void canon(char *, char *, size_t);
254extern void onintr(int);
255extern void removeoldleaves(void);
256extern void findunreflinks(void);
257extern void removeoldnodes(void);
258extern void createleaves(char *);
259extern void createfiles(void);
260extern void createlinks(void);
261extern void checkrestore(void);
262extern void setinput(char *, char *);
263extern void newtapebuf(size_t);
264extern void setup(void);
265extern void setupR(void);
266extern void getvol(int);
267extern void printdumpinfo(void);
268extern int extractfile(char *);
269extern void skipmaps(void);
270extern void skipfile(void);
271extern void getfile(void (*)(char *, size_t), void (*)(char *, size_t));
272extern void null(char *, size_t);
273extern void findtapeblksize(int);
274extern void flsht(void);
275extern void closemt(int);
276extern int readhdr(struct s_spcl *);
277extern int gethead(struct s_spcl *);
278extern int volnumber(ino_t);
279extern void findinode(struct s_spcl *);
280extern void pathcheck(char *);
281extern void renameit(char *, char *);
282extern int linkit(char *, char *, int);
283extern int lf_linkit(char *, char *, int);
284extern int reply(char *);
285/*PRINTFLIKE1*/
286extern void panic(const char *, ...);
287extern char *lctime(time_t *);
288extern int safe_open(int, const char *file, int mode, int perms);
289extern FILE *safe_fopen(const char *filename, const char *smode, int perms);
290extern void reset_dump(void);
291extern void get_next_device(void);
292extern void initpagercmd(void);
293extern void resolve(char *, int *, char **);
294extern int complexcopy(char *, char *, int);
295#else	/* !STDC */
296extern int mkentry();
297extern int expand();
298extern ino_t psearch();
299extern void metaget();
300extern void metaproc();
301extern long listfile();
302extern long addfile();
303extern long deletefile();
304extern long nodeupdates();
305extern long verifyfile();
306extern void extractdirs();
307extern void skipdirs();
308extern void treescan();
309extern RST_DIR *rst_opendir();
310extern void rst_closedir();
311extern struct direct *rst_readdir();
312extern void setdirmodes();
313extern int genliteraldir();
314extern int inodetype();
315extern void done();
316extern void runcmdshell();
317extern void canon();
318extern void onintr();
319extern void removeoldleaves();
320extern void findunreflinks();
321extern void removeoldnodes();
322extern void createleaves();
323extern void createfiles();
324extern void createlinks();
325extern void checkrestore();
326extern void setinput();
327extern void newtapebuf();
328extern void setup();
329extern void setupR();
330extern void getvol();
331extern void printdumpinfo();
332extern int extractfile();
333extern void skipmaps();
334extern void skipfile();
335extern void getfile();
336extern void null();
337extern void findtapeblksize();
338extern void flsht();
339extern void closemt();
340extern int readhdr();
341extern int gethead();
342extern int volnumber();
343extern void findinode();
344extern void pathcheck();
345extern void renameit();
346extern int linkit();
347extern int lf_linkit();
348extern int reply();
349extern void panic();
350extern char *lctime();
351extern int safe_open();
352extern FILE *safe_fopen();
353extern void reset_dump();
354extern void get_next_device();
355extern void initpagercmd();
356extern void resolve();
357extern 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