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