xref: /illumos-gate/usr/src/cmd/filesync/database.h (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1996 Sun Microsystems, Inc.  All Rights Reserved
24  *
25  * module:
26  *	database.h
27  *
28  * purpose:
29  *	definition of the baseline and rules data structures
30  */
31 
32 #ifndef	_DATABASE_H
33 #define	_DATABASE_H
34 
35 #ifdef	__cplusplus
36 extern "C" {
37 #endif
38 
39 #include <sys/stat.h>
40 #include <sys/acl.h>
41 
42 #define	ACL_UID_BUG	1	/* acl:SETACL sets owner to be caller	*/
43 
44 /*
45  * flag bits describing what we know about an individual file, or in
46  * some cases an entire base pair.  These flags are found in the
47  * base and file stuctures.
48  */
49 typedef int fflags_t;			/* type for file flags		*/
50 
51 #define	F_NEW		0x01		/* newly allocated		*/
52 #define	F_IN_BASELINE	0x02		/* file found in baseline	*/
53 #define	F_IN_SOURCE	0x04		/* file found in source tree	*/
54 #define	F_IN_DEST	0x08		/* file found in dest tree	*/
55 #define	F_EVALUATE	0x10		/* include in analysis		*/
56 #define	F_SPARSE	0x20		/* don't walk this directory	*/
57 #define	F_REMOVE	0x40		/* remove from baseline		*/
58 #define	F_CONFLICT	0x80		/* unresolvable conflict	*/
59 #define	F_LISTED	0x100		/* file came from LIST		*/
60 #define	F_STAT_ERROR	0x200		/* unable to stat file		*/
61 
62 #define	F_WHEREFOUND	(F_IN_BASELINE|F_IN_SOURCE|F_IN_DEST)
63 
64 /*
65  * a base is a pair of directories to be kept in sync
66  * 	all rules and baseline data is stored beneath some base
67  */
68 struct base {
69 	struct base *b_next;		/* pointer to next base		*/
70 	fflags_t   b_flags;		/* what I know about this base	*/
71 	int   b_ident;			/* base sequence # (DBG)	*/
72 	char *b_src_spec;		/* spec name of source dir	*/
73 	char *b_dst_spec;		/* spec name of dest dir	*/
74 	char *b_src_name;		/* expanded name of source dir	*/
75 	char *b_dst_name;		/* expanded name of dest dir	*/
76 
77 	struct rule *b_includes;	/* chain of include rules	*/
78 	struct rule *b_excludes;	/* chain of exclude rules	*/
79 	struct rule *b_restrictions;	/* chain of restrictions	*/
80 
81 	struct file *b_files;		/* chain of files		*/
82 
83 	/* statistics for wrap-up summary				*/
84 	int b_totfiles;			/* total files found in tree	*/
85 	int b_src_copies;		/* files copied to source	*/
86 	int b_src_deletes;		/* files deleted from source	*/
87 	int b_src_misc;			/* ownership changes on source	*/
88 	int b_dst_copies;		/* files copied to dest		*/
89 	int b_dst_deletes;		/* files deleted from dest	*/
90 	int b_dst_misc;			/* ownership changes on source	*/
91 	int b_unresolved;		/* unresolved conflicts		*/
92 };
93 
94 /*
95  * flag bits describing what we know about a particular rule.
96  * These flags are found in the rule structure
97  */
98 typedef	int rflags_t;			/* type for rule flags		*/
99 
100 #define	R_NEW		0x01		/* newly added rule (=OPT_NEW)	*/
101 #define	R_PROGRAM	0x02		/* program (vs literal names)	*/
102 #define	R_IGNORE	0x04		/* IGNORE (vs INCLUDE)		*/
103 #define	R_RESTRICT	0x08		/* restriction (-r argument)	*/
104 #define	R_WILD		0x10		/* name involves wild cards	*/
105 #define	R_BOGUS		0x20		/* fabricated rule		*/
106 
107 /*
108  * a rule describes files to be included or excluded
109  *	they are stored under bases
110  */
111 struct rule {
112 	struct rule *r_next;		/* pointer to next rule in base	*/
113 	rflags_t r_flags;		/* flags associated with rule	*/
114 	char   *r_file;			/* file for this rule		*/
115 };
116 
117 
118 /*
119  * this is the information we keep track of for a file
120  */
121 struct fileinfo {
122 	ino_t	f_ino;			/* inode number of this file	*/
123 	long	f_d_maj;		/* maj dev on which it lives	*/
124 	long	f_d_min;		/* minj dev on which it lives	*/
125 
126 	int 	f_type;			/* file/dir/special ...		*/
127 	int	f_mode;			/* protection			*/
128 	int	f_nlink;		/* number of links to file	*/
129 
130 	uid_t	f_uid;			/* owning UID			*/
131 	gid_t	f_gid;			/* owning GID			*/
132 
133 	off_t	f_size;			/* length in bytes		*/
134 	long	f_modtime;		/* last modification time	*/
135 	long	f_modns;		/* low order bits of modtime	*/
136 
137 	long	f_rd_maj;		/* major dev for specials	*/
138 	long	f_rd_min;		/* minor dev for specials	*/
139 
140 	int	f_numacls;		/* number of entries in acls	*/
141 	aclent_t *f_acls;		/* acl list (if any)		*/
142 };
143 
144 /*
145  * flag bits describing the differences we have detected between a file
146  * and the last time it was in sync (based on the baseline).
147  * These flags are used in the srcdiffs and dstdiffs fields of the
148  * file structure
149  */
150 typedef int diffmask_t;			/* type for difference masks	*/
151 
152 #define	D_CREATE	0x01		/* file has been created	*/
153 #define	D_DELETE	0x02		/* file has been deleted	*/
154 #define	D_MTIME		0x04		/* file has been modified	*/
155 #define	D_SIZE		0x08		/* file has changed size	*/
156 #define	D_UID		0x10		/* file has changed user id	*/
157 #define	D_GID		0x20		/* file has changed group id	*/
158 #define	D_PROT		0x40		/* file has changed protection	*/
159 #define	D_LINKS		0x80		/* file has changed link count	*/
160 #define	D_TYPE		0x100		/* file has changed type	*/
161 #define	D_FACLS		0x200		/* file has changed facls	*/
162 #define	D_RENAME_TO	0x400		/* file came from a rename	*/
163 #define	D_RENAME_FROM	0x800		/* file has been renamed	*/
164 
165 /*
166  * these masks are used to determine how important potential changes are.
167  *
168  *	D_CONTENTS	there may be changes to the file's contents
169  *	D_ADMIN		there may be changes to the ownership and protection
170  *	D_IMPORTANT	there may be changes that should block a deletion
171  *
172  * Note:
173  *	I am torn on whether or not to include modtime in D_IMPORTANT.
174  *	Experience suggests that deleting one of many links affects the
175  *	file modification time.
176  */
177 #define	D_ADMIN		(D_UID|D_GID|D_PROT|D_FACLS)
178 #define	D_CONTENTS	(D_SIZE|D_TYPE|D_CREATE|D_MTIME)
179 #define	D_IMPORTANT	(D_SIZE|D_TYPE|D_CREATE|D_MTIME|D_ADMIN)
180 
181 /*
182  * a file is an instance that follows (under a base) from a rule
183  * (for that base).  A file structure may exist because of any
184  * combination of a file under the source, destination, in a
185  * baseline for historical reasons, or merely because a rule
186  * calls it out (whether it exists or not).
187  */
188 struct file {
189 	struct file *f_next;		/* pointer to next file in base	*/
190 	struct file *f_files;		/* pointer to files in subdir	*/
191 	struct base *f_base;		/* pointer to owning base	*/
192 	fflags_t f_flags;		/* flags associated with file	*/
193 	int	f_depth;		/* directory depth for file	*/
194 	char   *f_name;			/* name of this file		*/
195 
196 	/*
197 	 * these fields capture information, gleaned from the baseline
198 	 * that is side-specific, and should not be expected to be in
199 	 * agreement between the two sides.  As a result, this info can
200 	 * not be properly captured in f_info[OPT_BASE] and needs to
201 	 * be kept somewhere else.
202 	 */
203 	long	f_s_modtime;		/* baseline source mod time	*/
204 	ino_t	f_s_inum;		/* baseline source inode #	*/
205 	long	f_s_nlink;		/* baseline source link count	*/
206 	long	f_s_maj;		/* baseline source dev maj	*/
207 	long	f_s_min;		/* baseline source dev min	*/
208 	long	f_d_modtime;		/* baseline target mod time	*/
209 	ino_t	f_d_inum;		/* baseline target inode #	*/
210 	long	f_d_nlink;		/* baseline target link count	*/
211 	long	f_d_maj;		/* baseline target dev maj	*/
212 	long	f_d_min;		/* baseline target dev min	*/
213 
214 	/* stat information from baseline file and evaluation		*/
215 	struct fileinfo f_info[3];	/* baseline, source, dest	*/
216 
217 	/* summary of changes discovered in analysis			*/
218 	diffmask_t f_srcdiffs;		/* changes on source side	*/
219 	diffmask_t f_dstdiffs;		/* changes on dest side		*/
220 
221 	/* this field is only valid for a renamed file			*/
222 	struct file * f_previous;	/* node for previous filename	*/
223 
224 	/*
225 	 * these fields are only valid for a file that has been added
226 	 * to the reconciliation list
227 	 */
228 	struct file *f_rnext;		/* reconciliation chain ptr	*/
229 	char	*f_fullname;		/* full name for reconciling	*/
230 	long	f_modtime;		/* modtime for ordering purpose	*/
231 	long	f_modns;		/* low order modtime 		*/
232 
233 	/* this field is only valid for a file with a hard conflict	*/
234 	char 	*f_problem;		/* description of conflict	*/
235 };
236 
237 /*
238  * globals
239  */
240 extern struct base omnibase;		/* base for global rules	*/
241 extern struct base *bases;		/* base for the main list	*/
242 extern int inum_changes;		/* LISTed dirs with i# changes	*/
243 
244 /* routines to manage base nodes, file nodes, and file infor	*/
245 errmask_t read_baseline(char *);
246 errmask_t write_baseline(char *);
247 struct file *add_file_to_base(struct base *, const char *);
248 struct file *add_file_to_dir(struct file *, const char *);
249 struct base *add_base(const char *src, const char *dst);
250 void note_info(struct file *, const struct stat *, side_t);
251 void update_info(struct file *, side_t);
252 
253 /* routines to manage rules					*/
254 errmask_t read_rules(char *);
255 errmask_t write_rules(char *);
256 errmask_t add_include(struct base *, char *);
257 errmask_t add_ignore(struct base *, char *);
258 
259 /* routines to manage and querry restriction lists		*/
260 errmask_t add_restr(char *);
261 bool_t check_restr(struct base *, const char *);
262 
263 /* routines for dealing with ignore lists			*/
264 void ignore_reset();
265 void ignore_pgm(const char *);
266 void ignore_expr(const char *);
267 void ignore_file(const char *);
268 bool_t ignore_check(const char *);
269 
270 /* database processing routines for the primary passes		*/
271 errmask_t evaluate(struct base *, side_t, bool_t);
272 errmask_t analyze(void);
273 errmask_t find_renames(struct file *);
274 errmask_t reconcile(struct file *);
275 int prune(void);
276 void summary(void);
277 char *full_name(struct file *, side_t, side_t);
278 
279 /* routines in action.c to carry out reconciliation		*/
280 errmask_t do_copy(struct file *, side_t);
281 errmask_t do_remove(struct file *, side_t);
282 errmask_t do_rename(struct file *, side_t);
283 errmask_t do_like(struct file *, side_t, bool_t);
284 
285 /* routines to deal with links in the reconciliation list	*/
286 struct file *find_link(struct file *, side_t);
287 void link_update(struct file *, side_t);
288 bool_t has_other_links(struct file *, side_t);
289 
290 /* maintain a name stack during directory tree traversal	*/
291 void push_name(const char *);
292 void pop_name();
293 char *get_name(struct file *);
294 
295 /* acl manipulation functions					*/
296 int get_acls(const char *, struct fileinfo *);
297 int set_acls(const char *, struct fileinfo *);
298 int cmp_acls(struct fileinfo *, struct fileinfo *);
299 char *show_acls(int, aclent_t *);
300 
301 #ifdef	__cplusplus
302 }
303 #endif
304 
305 #endif	/* _DATABASE_H */
306