The inode and dev values in the idev struct are now uint64.
[rsync/rsync.git] / exclude.c
... / ...
CommitLineData
1/* -*- c-file-style: "linux" -*-
2 *
3 * Copyright (C) 1996-2001 by Andrew Tridgell <tridge@samba.org>
4 * Copyright (C) 1996 by Paul Mackerras
5 * Copyright (C) 2002 by Martin Pool
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22/* a lot of this stuff was originally derived from GNU tar, although
23 it has now changed so much that it is hard to tell :) */
24
25/* include/exclude cluestick added by Martin Pool <mbp@samba.org> */
26
27#include "rsync.h"
28
29extern int verbose;
30
31struct exclude_list_struct exclude_list;
32struct exclude_list_struct local_exclude_list;
33struct exclude_list_struct server_exclude_list;
34char *exclude_path_prefix = NULL;
35
36/** Build an exclude structure given a exclude pattern */
37static void make_exclude(struct exclude_list_struct *listp, const char *pattern,
38 int pat_len, int include)
39{
40 struct exclude_struct *ret;
41 const char *cp;
42 int ex_len;
43
44 ret = new(struct exclude_struct);
45 if (!ret)
46 out_of_memory("make_exclude");
47
48 memset(ret, 0, sizeof ret[0]);
49 ret->include = include;
50
51 if (exclude_path_prefix)
52 ret->match_flags |= MATCHFLG_ABS_PATH;
53 if (exclude_path_prefix && *pattern == '/')
54 ex_len = strlen(exclude_path_prefix);
55 else
56 ex_len = 0;
57 ret->pattern = new_array(char, ex_len + pat_len + 1);
58 if (!ret->pattern)
59 out_of_memory("make_exclude");
60 if (ex_len)
61 memcpy(ret->pattern, exclude_path_prefix, ex_len);
62 strlcpy(ret->pattern + ex_len, pattern, pat_len + 1);
63 pat_len += ex_len;
64
65 if (strpbrk(ret->pattern, "*[?")) {
66 ret->match_flags |= MATCHFLG_WILD;
67 if ((cp = strstr(ret->pattern, "**")) != NULL) {
68 ret->match_flags |= MATCHFLG_WILD2;
69 /* If the pattern starts with **, note that. */
70 if (cp == ret->pattern)
71 ret->match_flags |= MATCHFLG_WILD2_PREFIX;
72 }
73 }
74
75 if (pat_len > 1 && ret->pattern[pat_len-1] == '/') {
76 ret->pattern[pat_len-1] = 0;
77 ret->directory = 1;
78 }
79
80 for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
81 ret->slash_cnt++;
82
83 if (!listp->tail)
84 listp->head = listp->tail = ret;
85 else {
86 listp->tail->next = ret;
87 listp->tail = ret;
88 }
89}
90
91static void free_exclude(struct exclude_struct *ex)
92{
93 free(ex->pattern);
94 free(ex);
95}
96
97void free_exclude_list(struct exclude_list_struct *listp)
98{
99 struct exclude_struct *ent, *next;
100
101 if (verbose > 2)
102 rprintf(FINFO, "[%s] clearing exclude list\n", who_am_i());
103
104 for (ent = listp->head; ent; ent = next) {
105 next = ent->next;
106 free_exclude(ent);
107 }
108
109 memset(listp, 0, sizeof listp[0]);
110}
111
112static int check_one_exclude(char *name, struct exclude_struct *ex,
113 int name_is_dir)
114{
115 char *p;
116 int match_start = 0;
117 char *pattern = ex->pattern;
118
119 /* If the pattern does not have any slashes AND it does not have
120 * a "**" (which could match a slash), then we just match the
121 * name portion of the path. */
122 if (!ex->slash_cnt && !(ex->match_flags & MATCHFLG_WILD2)) {
123 if ((p = strrchr(name,'/')) != NULL)
124 name = p+1;
125 }
126 else if ((ex->match_flags & MATCHFLG_ABS_PATH) && *name != '/') {
127 static char full_name[MAXPATHLEN];
128 extern char curr_dir[];
129 int plus = curr_dir[1] == '\0'? 1 : 0;
130 pathjoin(full_name, sizeof full_name, curr_dir+plus, name);
131 name = full_name;
132 }
133
134 if (!name[0]) return 0;
135
136 if (ex->directory && !name_is_dir) return 0;
137
138 if (*pattern == '/') {
139 match_start = 1;
140 pattern++;
141 if (*name == '/')
142 name++;
143 }
144
145 if (ex->match_flags & MATCHFLG_WILD) {
146 /* A non-anchored match with an infix slash and no "**"
147 * needs to match the last slash_cnt+1 name elements. */
148 if (!match_start && ex->slash_cnt &&
149 !(ex->match_flags & MATCHFLG_WILD2)) {
150 int cnt = ex->slash_cnt + 1;
151 for (p = name + strlen(name) - 1; p >= name; p--) {
152 if (*p == '/' && !--cnt)
153 break;
154 }
155 name = p+1;
156 }
157 if (wildmatch(pattern, name))
158 return 1;
159 if (ex->match_flags & MATCHFLG_WILD2_PREFIX) {
160 /* If the **-prefixed pattern has a '/' as the next
161 * character, then try to match the rest of the
162 * pattern at the root. */
163 if (pattern[2] == '/' && wildmatch(pattern+3, name))
164 return 1;
165 }
166 else if (!match_start && ex->match_flags & MATCHFLG_WILD2) {
167 /* A non-anchored match with an infix or trailing "**"
168 * (but not a prefixed "**") needs to try matching
169 * after every slash. */
170 while ((name = strchr(name, '/')) != NULL) {
171 name++;
172 if (wildmatch(pattern, name))
173 return 1;
174 }
175 }
176 } else if (match_start) {
177 if (strcmp(name,pattern) == 0)
178 return 1;
179 } else {
180 int l1 = strlen(name);
181 int l2 = strlen(pattern);
182 if (l2 <= l1 &&
183 strcmp(name+(l1-l2),pattern) == 0 &&
184 (l1==l2 || name[l1-(l2+1)] == '/')) {
185 return 1;
186 }
187 }
188
189 return 0;
190}
191
192
193static void report_exclude_result(char const *name,
194 struct exclude_struct const *ent,
195 int name_is_dir, const char *type)
196{
197 /* If a trailing slash is present to match only directories,
198 * then it is stripped out by make_exclude. So as a special
199 * case we add it back in here. */
200
201 if (verbose >= 2) {
202 rprintf(FINFO, "[%s] %scluding %s %s because of %s %s%s\n",
203 who_am_i(), ent->include ? "in" : "ex",
204 name_is_dir ? "directory" : "file", name, type,
205 ent->pattern, ent->directory ? "/" : "");
206 }
207}
208
209
210/*
211 * Return true if file NAME is defined to be excluded by the specified
212 * exclude list.
213 */
214int check_exclude(struct exclude_list_struct *listp, char *name, int name_is_dir,
215 const char *type)
216{
217 struct exclude_struct *ent;
218
219 for (ent = listp->head; ent; ent = ent->next) {
220 if (check_one_exclude(name, ent, name_is_dir)) {
221 report_exclude_result(name, ent, name_is_dir, type);
222 return !ent->include;
223 }
224 }
225
226 return 0;
227}
228
229
230/* Get the next include/exclude arg from the string. The token will not
231 * be '\0' terminated, so use the returned length to limit the string.
232 * Also, be sure to add this length to the returned pointer before passing
233 * it back to ask for the next token. This routine will not split off a
234 * prefix of "+ " or "- " unless xflags contains XFLG_NO_PREFIXES. The
235 * *incl_ptr value will be 1 for an include, 0 for an exclude, and -1 for
236 * the list-clearing "!" token.
237 */
238static const char *get_exclude_tok(const char *p, int *len_ptr, int *incl_ptr,
239 int xflags)
240{
241 const unsigned char *s = (const unsigned char *)p;
242 int len;
243
244 if (xflags & XFLG_WORD_SPLIT) {
245 /* Skip over any initial whitespace. */
246 while (isspace(*s))
247 s++;
248 /* Update for "!" check. */
249 p = (const char *)s;
250 }
251
252 /* Is this a '+' or '-' followed by a space (not whitespace)? */
253 if (!(xflags & XFLG_NO_PREFIXES)
254 && (*s == '-' || *s == '+') && s[1] == ' ') {
255 *incl_ptr = *s == '+';
256 s += 2;
257 } else
258 *incl_ptr = xflags & XFLG_DEF_INCLUDE;
259
260 if (xflags & XFLG_WORD_SPLIT) {
261 const unsigned char *cp = s;
262 /* Token ends at whitespace or the end of the string. */
263 while (!isspace(*cp) && *cp != '\0')
264 cp++;
265 len = cp - s;
266 } else
267 len = strlen(s);
268
269 if (*p == '!' && len == 1 && !(xflags & XFLG_NO_PREFIXES))
270 *incl_ptr = -1;
271
272 *len_ptr = len;
273 return (const char *)s;
274}
275
276
277void add_exclude(struct exclude_list_struct *listp, const char *pattern,
278 int xflags)
279{
280 int pat_len, incl;
281 const char *cp;
282
283 if (!pattern)
284 return;
285
286 cp = pattern;
287 pat_len = 0;
288 while (1) {
289 cp = get_exclude_tok(cp + pat_len, &pat_len, &incl, xflags);
290 if (!pat_len)
291 break;
292 /* If we got the special "!" token, clear the list. */
293 if (incl < 0)
294 free_exclude_list(listp);
295 else {
296 make_exclude(listp, cp, pat_len, incl);
297
298 if (verbose > 2) {
299 rprintf(FINFO, "[%s] add_exclude(%s,%s)\n",
300 who_am_i(), cp,
301 incl ? "include" : "exclude");
302 }
303 }
304 }
305}
306
307
308void add_exclude_file(struct exclude_list_struct *listp, const char *fname,
309 int xflags)
310{
311 FILE *fp;
312 char line[MAXPATHLEN];
313 char *eob = line + MAXPATHLEN - 1;
314 extern int eol_nulls;
315
316 if (!fname || !*fname)
317 return;
318
319 if (*fname != '-' || fname[1])
320 fp = fopen(fname, "rb");
321 else
322 fp = stdin;
323 if (!fp) {
324 if (xflags & XFLG_FATAL_ERRORS) {
325 rsyserr(FERROR, errno,
326 "failed to open %s file %s",
327 xflags & XFLG_DEF_INCLUDE ? "include" : "exclude",
328 fname);
329 exit_cleanup(RERR_FILEIO);
330 }
331 return;
332 }
333
334 while (1) {
335 char *s = line;
336 int ch;
337 while (1) {
338 if ((ch = getc(fp)) == EOF) {
339 if (ferror(fp) && errno == EINTR)
340 continue;
341 break;
342 }
343 if (eol_nulls? !ch : (ch == '\n' || ch == '\r'))
344 break;
345 if (s < eob)
346 *s++ = ch;
347 }
348 *s = '\0';
349 /* Skip lines starting with semicolon or pound. */
350 if (*line && *line != ';' && *line != '#')
351 add_exclude(listp, line, xflags);
352 if (ch == EOF)
353 break;
354 }
355 fclose(fp);
356}
357
358
359void send_exclude_list(int f)
360{
361 struct exclude_struct *ent;
362 extern int list_only, recurse;
363
364 /* This is a complete hack - blame Rusty.
365 *
366 * FIXME: This pattern shows up in the output of
367 * report_exclude_result(), which is not ideal. */
368 if (list_only && !recurse)
369 add_exclude(&exclude_list, "/*/*", 0);
370
371 for (ent = exclude_list.head; ent; ent = ent->next) {
372 unsigned int l;
373 char p[MAXPATHLEN+1];
374
375 l = strlcpy(p, ent->pattern, sizeof p);
376 if (l == 0 || l >= MAXPATHLEN)
377 continue;
378 if (ent->directory) {
379 p[l++] = '/';
380 p[l] = '\0';
381 }
382
383 if (ent->include) {
384 write_int(f, l + 2);
385 write_buf(f, "+ ", 2);
386 } else if ((*p == '-' || *p == '+') && p[1] == ' ') {
387 write_int(f, l + 2);
388 write_buf(f, "- ", 2);
389 } else
390 write_int(f, l);
391 write_buf(f, p, l);
392 }
393
394 write_int(f, 0);
395}
396
397
398void recv_exclude_list(int f)
399{
400 char line[MAXPATHLEN+1]; /* Allows a trailing slash on a max-len dir */
401 unsigned int l;
402
403 while ((l = read_int(f)) != 0) {
404 if (l >= sizeof line)
405 overflow("recv_exclude_list");
406 read_sbuf(f, line, l);
407 add_exclude(&exclude_list, line, 0);
408 }
409}
410
411
412static char default_cvsignore[] =
413 /* These default ignored items come from the CVS manual. */
414 "RCS SCCS CVS CVS.adm RCSLOG cvslog.* tags TAGS"
415 " .make.state .nse_depinfo *~ #* .#* ,* _$* *$"
416 " *.old *.bak *.BAK *.orig *.rej .del-*"
417 " *.a *.olb *.o *.obj *.so *.exe"
418 " *.Z *.elc *.ln core"
419 /* The rest we added to suit ourself. */
420 " .svn/";
421
422void add_cvs_excludes(void)
423{
424 char fname[MAXPATHLEN];
425 char *p;
426
427 add_exclude(&exclude_list, default_cvsignore,
428 XFLG_WORD_SPLIT | XFLG_NO_PREFIXES);
429
430 if ((p = getenv("HOME"))
431 && pathjoin(fname, sizeof fname, p, ".cvsignore") < sizeof fname) {
432 add_exclude_file(&exclude_list, fname,
433 XFLG_WORD_SPLIT | XFLG_NO_PREFIXES);
434 }
435
436 add_exclude(&exclude_list, getenv("CVSIGNORE"),
437 XFLG_WORD_SPLIT | XFLG_NO_PREFIXES);
438}