Document the new "filter" parameter.
[rsync/rsync.git] / exclude.c
CommitLineData
f0f5767f 1/* -*- c-file-style: "linux" -*-
0f2ac855 2 *
07a874fd
MP
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
0f2ac855 6 *
07a874fd
MP
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.
0f2ac855 11 *
07a874fd
MP
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.
0f2ac855 16 *
07a874fd
MP
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 */
c627d613 21
2b6b4d53
AT
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 :) */
c627d613 24
d567322f
MP
25/* include/exclude cluestick added by Martin Pool <mbp@samba.org> */
26
c627d613
AT
27#include "rsync.h"
28
29extern int verbose;
40d38dc0
WD
30extern int eol_nulls;
31extern int list_only;
32extern int recurse;
6dfd07d0
WD
33extern int io_error;
34extern int sanitize_paths;
35extern int protocol_version;
40d38dc0
WD
36
37extern char curr_dir[];
6dfd07d0
WD
38extern unsigned int curr_dir_len;
39extern unsigned int module_dirlen;
c627d613 40
67340e95 41struct exclude_list_struct exclude_list = { 0, 0, "" };
67340e95 42struct exclude_list_struct server_exclude_list = { 0, 0, "server " };
c627d613 43
6dfd07d0
WD
44/* Need room enough for ":MODS " prefix plus some room to grow. */
45#define MAX_EXCLUDE_PREFIX (16)
46
47/* The dirbuf is set by push_local_excludes() to the current subdirectory
48 * relative to curr_dir that is being processed. The path always has a
49 * trailing slash appended, and the variable dirbuf_len contains the length
50 * of this path prefix. The path is always absolute. */
51static char dirbuf[MAXPATHLEN+1];
52static unsigned int dirbuf_len = 0;
53static int dirbuf_depth;
54
55/* This is True when we're scanning parent dirs for per-dir merge-files. */
56static BOOL parent_dirscan = False;
57
58/* This array contains a list of all the currently active per-dir merge
59 * files. This makes it easier to save the appropriate values when we
60 * "push" down into each subdirectory. */
61static struct exclude_struct **mergelist_parents;
62static int mergelist_cnt = 0;
63static int mergelist_size = 0;
64
65/* Each exclude_list_struct describes a singly-linked list by keeping track
66 * of both the head and tail pointers. The list is slightly unusual in that
67 * a parent-dir's content can be appended to the end of the local list in a
68 * special way: the last item in the local list has its "next" pointer set
69 * to point to the inherited list, but the local list's tail pointer points
70 * at the end of the local list. Thus, if the local list is empty, the head
71 * will be pointing at the inherited content but the tail will be NULL. To
72 * help you visualize this, here are the possible list arrangements:
73 *
74 * Completely Empty Local Content Only
75 * ================================== ====================================
76 * head -> NULL head -> Local1 -> Local2 -> NULL
77 * tail -> NULL tail -------------^
78 *
79 * Inherited Content Only Both Local and Inherited Content
80 * ================================== ====================================
81 * head -> Parent1 -> Parent2 -> NULL head -> L1 -> L2 -> P1 -> P2 -> NULL
82 * tail -> NULL tail ---------^
83 *
84 * This means that anyone wanting to traverse the whole list to use it just
85 * needs to start at the head and use the "next" pointers until it goes
86 * NULL. To add new local content, we insert the item after the tail item
87 * and update the tail (obviously, if "tail" was NULL, we insert it at the
88 * head). To clear the local list, WE MUST NOT FREE THE INHERITED CONTENT
89 * because it is shared between the current list and our parent list(s).
90 * The easiest way to handle this is to simply truncate the list after the
91 * tail item and then free the local list from the head. When inheriting
92 * the list for a new local dir, we just save off the exclude_list_struct
93 * values (so we can pop back to them later) and set the tail to NULL.
94 */
95
96static void free_exclude(struct exclude_struct *ex)
97{
98 if (ex->match_flags & MATCHFLG_PERDIR_MERGE) {
99 free(ex->u.mergelist->debug_type);
100 free(ex->u.mergelist);
101 mergelist_cnt--;
102 }
103 free(ex->pattern);
104 free(ex);
105}
106
107/* Build an exclude structure given an exclude pattern. The value in "pat"
108 * is not null-terminated. */
5e972dcf
WD
109static void make_exclude(struct exclude_list_struct *listp, const char *pat,
110 unsigned int pat_len, unsigned int mflags)
c627d613 111{
2b6b4d53 112 struct exclude_struct *ret;
f8f72644 113 const char *cp;
5e972dcf 114 unsigned int ex_len;
c627d613 115
6dfd07d0
WD
116 if (verbose > 2) {
117 rprintf(FINFO, "[%s] make_exclude(%.*s, %s%s)\n",
118 who_am_i(), (int)pat_len, pat,
119 mflags & MATCHFLG_PERDIR_MERGE ? "per-dir-merge"
120 : mflags & MATCHFLG_INCLUDE ? "include" : "exclude",
121 listp->debug_type);
122 }
123
58cadc86 124 ret = new(struct exclude_struct);
f8f72644
WD
125 if (!ret)
126 out_of_memory("make_exclude");
c627d613 127
5f5be796 128 memset(ret, 0, sizeof ret[0]);
2b6b4d53 129
6dfd07d0
WD
130 if (mflags & MATCHFLG_ABS_PATH) {
131 if (*pat != '/') {
132 mflags &= ~MATCHFLG_ABS_PATH;
133 ex_len = 0;
134 } else
135 ex_len = dirbuf_len - module_dirlen - 1;
136 } else
f8f72644
WD
137 ex_len = 0;
138 ret->pattern = new_array(char, ex_len + pat_len + 1);
139 if (!ret->pattern)
140 out_of_memory("make_exclude");
141 if (ex_len)
6dfd07d0 142 memcpy(ret->pattern, dirbuf + module_dirlen, ex_len);
5e972dcf 143 strlcpy(ret->pattern + ex_len, pat, pat_len + 1);
f8f72644
WD
144 pat_len += ex_len;
145
146 if (strpbrk(ret->pattern, "*[?")) {
5e972dcf 147 mflags |= MATCHFLG_WILD;
96d3590a 148 if ((cp = strstr(ret->pattern, "**")) != NULL) {
5e972dcf 149 mflags |= MATCHFLG_WILD2;
170381c0 150 /* If the pattern starts with **, note that. */
96d3590a 151 if (cp == ret->pattern)
5e972dcf 152 mflags |= MATCHFLG_WILD2_PREFIX;
0f2ac855 153 }
2bca43f6 154 }
c627d613 155
5be7fa93
WD
156 if (pat_len > 1 && ret->pattern[pat_len-1] == '/') {
157 ret->pattern[pat_len-1] = 0;
5e972dcf 158 mflags |= MATCHFLG_DIRECTORY;
2b6b4d53 159 }
c627d613 160
6dfd07d0
WD
161 if (mflags & MATCHFLG_PERDIR_MERGE) {
162 struct exclude_list_struct *lp;
163 unsigned int len;
164 int i;
165
166 if ((cp = strrchr(ret->pattern, '/')) != NULL)
167 cp++;
168 else
169 cp = ret->pattern;
170
171 /* If the local merge file was already mentioned, don't
172 * add it again. */
173 for (i = 0; i < mergelist_cnt; i++) {
174 struct exclude_struct *ex = mergelist_parents[i];
175 const char *s = strrchr(ex->pattern, '/');
176 if (s)
177 s++;
178 else
179 s = ex->pattern;
180 len = strlen(s);
181 if (len == pat_len - (cp - ret->pattern)
182 && memcmp(s, cp, len) == 0) {
183 free_exclude(ret);
184 return;
185 }
186 }
187
188 if (!(lp = new_array(struct exclude_list_struct, 1)))
189 out_of_memory("make_exclude");
190 lp->head = lp->tail = NULL;
191 if (asprintf(&lp->debug_type, " (per-dir %s)", cp) < 0)
192 out_of_memory("make_exclude");
193 ret->u.mergelist = lp;
194
195 if (mergelist_cnt == mergelist_size) {
196 mergelist_size += 5;
197 mergelist_parents = realloc_array(mergelist_parents,
198 struct exclude_struct *,
199 mergelist_size);
200 if (!mergelist_parents)
201 out_of_memory("make_exclude");
202 }
203 mergelist_parents[mergelist_cnt++] = ret;
204 } else {
205 for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
206 ret->u.slash_cnt++;
207 }
0944563e 208
c1b29492
WD
209 ret->match_flags = mflags;
210
6dfd07d0
WD
211 if (!listp->tail) {
212 ret->next = listp->head;
b2aa573b 213 listp->head = listp->tail = ret;
6dfd07d0
WD
214 } else {
215 ret->next = listp->tail->next;
b2aa573b
WD
216 listp->tail->next = ret;
217 listp->tail = ret;
218 }
2b6b4d53
AT
219}
220
6dfd07d0 221static void clear_exclude_list(struct exclude_list_struct *listp)
2b6b4d53 222{
6dfd07d0
WD
223 if (listp->tail) {
224 struct exclude_struct *ent, *next;
225 /* Truncate any inherited items from the local list. */
226 listp->tail->next = NULL;
227 /* Now free everything that is left. */
228 for (ent = listp->head; ent; ent = next) {
229 next = ent->next;
230 free_exclude(ent);
231 }
232 }
233
234 listp->head = listp->tail = NULL;
2b6b4d53 235}
c627d613 236
6dfd07d0
WD
237/* This returns an expanded (absolute) filename for the merge-file name if
238 * the name has any slashes in it OR if the parent_dirscan var is True;
239 * otherwise it returns the original merge_file name. If the len_ptr value
240 * is non-NULL the merge_file name is limited by the referenced length
241 * value and will be updated with the length of the resulting name. We
242 * always return a name that is null terminated, even if the merge_file
243 * name was not. */
244static char *parse_merge_name(const char *merge_file, unsigned int *len_ptr,
245 unsigned int prefix_skip)
5be7fa93 246{
6dfd07d0
WD
247 static char buf[MAXPATHLEN];
248 char *fn, tmpbuf[MAXPATHLEN];
249 unsigned int fn_len;
250
251 if (!parent_dirscan && *merge_file != '/') {
252 /* Return the name unchanged it doesn't have any slashes. */
253 if (len_ptr) {
254 const char *p = merge_file + *len_ptr;
255 while (--p > merge_file && *p != '/') {}
256 if (p == merge_file) {
257 strlcpy(buf, merge_file, *len_ptr + 1);
258 return buf;
259 }
260 } else if (strchr(merge_file, '/') == NULL)
261 return (char *)merge_file;
262 }
5be7fa93 263
6dfd07d0
WD
264 fn = *merge_file == '/' ? buf : tmpbuf;
265 if (sanitize_paths) {
266 const char *r = prefix_skip ? "/" : NULL;
267 /* null-terminate the name if it isn't already */
268 if (len_ptr && merge_file[*len_ptr]) {
269 char *to = fn == buf ? tmpbuf : buf;
270 strlcpy(to, merge_file, *len_ptr + 1);
271 merge_file = to;
272 }
273 if (!sanitize_path(fn, merge_file, r, dirbuf_depth)) {
274 rprintf(FERROR, "merge-file name overflows: %s\n",
275 merge_file);
276 return NULL;
277 }
278 } else {
279 strlcpy(fn, merge_file, len_ptr ? *len_ptr + 1 : MAXPATHLEN);
280 clean_fname(fn, 1);
b2aa573b 281 }
6dfd07d0
WD
282
283 fn_len = strlen(fn);
284 if (fn == buf)
285 goto done;
286
287 if (dirbuf_len + fn_len >= MAXPATHLEN) {
288 rprintf(FERROR, "merge-file name overflows: %s\n", fn);
289 return NULL;
290 }
291 memcpy(buf, dirbuf + prefix_skip, dirbuf_len - prefix_skip);
292 memcpy(buf + dirbuf_len - prefix_skip, fn, fn_len + 1);
293 fn_len = clean_fname(buf, 1);
294
295 done:
296 if (len_ptr)
297 *len_ptr = fn_len;
298 return buf;
299}
5be7fa93 300
6dfd07d0
WD
301/* Sets the dirbuf and dirbuf_len values. */
302void set_excludes_dir(const char *dir, unsigned int dirlen)
303{
304 unsigned int len;
305 if (*dir != '/') {
306 memcpy(dirbuf, curr_dir, curr_dir_len);
307 dirbuf[curr_dir_len] = '/';
308 len = curr_dir_len + 1;
309 if (len + dirlen >= MAXPATHLEN)
310 dirlen = 0;
311 } else
312 len = 0;
313 memcpy(dirbuf + len, dir, dirlen);
314 dirbuf[dirlen + len] = '\0';
315 dirbuf_len = clean_fname(dirbuf, 1);
316 if (dirbuf_len > 1 && dirbuf[dirbuf_len-1] == '.'
317 && dirbuf[dirbuf_len-2] == '/')
318 dirbuf_len -= 2;
319 if (dirbuf_len != 1)
320 dirbuf[dirbuf_len++] = '/';
321 dirbuf[dirbuf_len] = '\0';
322 if (sanitize_paths)
323 dirbuf_depth = count_dir_elements(dirbuf + module_dirlen);
324}
325
326/* This routine takes a per-dir merge-file entry and finishes its setup.
327 * If the name has a path portion then we check to see if it refers to a
328 * parent directory of the first transfer dir. If it does, we scan all the
329 * dirs from that point through the parent dir of the transfer dir looking
330 * for the per-dir merge-file in each one. */
331static BOOL setup_merge_file(struct exclude_struct *ex,
332 struct exclude_list_struct *lp, int flags)
333{
334 char buf[MAXPATHLEN];
335 char *x, *y, *pat = ex->pattern;
336 unsigned int len;
337
338 if (!(x = parse_merge_name(pat, NULL, 0)) || *x != '/')
339 return 0;
340
341 y = strrchr(x, '/');
342 *y = '\0';
343 ex->pattern = strdup(y+1);
344 if (!*x)
345 x = "/";
346 if (*x == '/')
347 strlcpy(buf, x, MAXPATHLEN);
348 else
349 pathjoin(buf, MAXPATHLEN, dirbuf, x);
350
351 len = clean_fname(buf, 1);
352 if (len != 1 && len < MAXPATHLEN-1) {
353 buf[len++] = '/';
354 buf[len] = '\0';
355 }
356 /* This ensures that the specified dir is a parent of the transfer. */
357 for (x = buf, y = dirbuf; *x && *x == *y; x++, y++) {}
358 if (*x)
359 y += strlen(y); /* nope -- skip the scan */
360
361 parent_dirscan = True;
362 while (*y) {
363 char save[MAXPATHLEN];
364 strlcpy(save, y, MAXPATHLEN);
365 *y = '\0';
366 dirbuf_len = y - dirbuf;
367 strlcpy(x, ex->pattern, MAXPATHLEN - (x - buf));
368 add_exclude_file(lp, buf, flags | XFLG_ABS_PATH);
369 if (ex->match_flags & MATCHFLG_NO_INHERIT)
370 lp->head = NULL;
371 lp->tail = NULL;
372 strlcpy(y, save, MAXPATHLEN);
373 while ((*x++ = *y++) != '/') {}
374 }
375 parent_dirscan = False;
376 free(pat);
377 return 1;
378}
379
380/* Each time rsync changes to a new directory it call this function to
381 * handle all the per-dir merge-files. The "dir" value is the current path
382 * relative to curr_dir (which might not be null-terminated). We copy it
383 * into dirbuf so that we can easily append a file name on the end. */
384void *push_local_excludes(const char *dir, unsigned int dirlen)
385{
386 struct exclude_list_struct *ap, *push;
387 int i;
388
389 set_excludes_dir(dir, dirlen);
390
391 push = new_array(struct exclude_list_struct, mergelist_cnt);
392 if (!push)
393 out_of_memory("push_local_excludes");
394
395 for (i = 0, ap = push; i < mergelist_cnt; i++) {
396 memcpy(ap++, mergelist_parents[i]->u.mergelist,
397 sizeof (struct exclude_list_struct));
398 }
399
400 /* Note: add_exclude_file() might increase mergelist_cnt, so keep
401 * this loop separate from the above loop. */
402 for (i = 0; i < mergelist_cnt; i++) {
403 struct exclude_struct *ex = mergelist_parents[i];
404 struct exclude_list_struct *lp = ex->u.mergelist;
405 int flags = 0;
406
407 if (verbose > 2) {
408 rprintf(FINFO, "[%s] pushing exclude list%s\n",
409 who_am_i(), lp->debug_type);
410 }
411
412 lp->tail = NULL; /* Switch any local rules to inherited. */
413 if (ex->match_flags & MATCHFLG_NO_INHERIT)
414 lp->head = NULL;
415 if (ex->match_flags & MATCHFLG_WORD_SPLIT)
416 flags |= XFLG_WORD_SPLIT;
417 if (ex->match_flags & MATCHFLG_NO_PREFIXES)
418 flags |= XFLG_NO_PREFIXES;
419 if (ex->match_flags & MATCHFLG_INCLUDE)
420 flags |= XFLG_DEF_INCLUDE;
421 else if (ex->match_flags & MATCHFLG_NO_PREFIXES)
422 flags |= XFLG_DEF_EXCLUDE;
423
424 if (ex->match_flags & MATCHFLG_FINISH_SETUP) {
425 ex->match_flags &= ~MATCHFLG_FINISH_SETUP;
426 if (setup_merge_file(ex, lp, flags))
427 set_excludes_dir(dir, dirlen);
428 }
429
430 if (strlcpy(dirbuf + dirbuf_len, ex->pattern,
431 MAXPATHLEN - dirbuf_len) < MAXPATHLEN - dirbuf_len)
432 add_exclude_file(lp, dirbuf, flags | XFLG_ABS_PATH);
433 else {
434 io_error |= IOERR_GENERAL;
435 rprintf(FINFO,
436 "cannot add local excludes in long-named directory %s\n",
437 full_fname(dirbuf));
438 }
439 dirbuf[dirbuf_len] = '\0';
440 }
441
442 return (void*)push;
443}
444
445void pop_local_excludes(void *mem)
446{
447 struct exclude_list_struct *ap, *pop = (struct exclude_list_struct*)mem;
448 int i;
449
450 for (i = mergelist_cnt; i-- > 0; ) {
451 struct exclude_struct *ex = mergelist_parents[i];
452 struct exclude_list_struct *lp = ex->u.mergelist;
453
454 if (verbose > 2) {
455 rprintf(FINFO, "[%s] popping exclude list%s\n",
456 who_am_i(), lp->debug_type);
457 }
458
459 clear_exclude_list(lp);
460 }
461
462 for (i = 0, ap = pop; i < mergelist_cnt; i++) {
463 memcpy(mergelist_parents[i]->u.mergelist, ap++,
464 sizeof (struct exclude_list_struct));
465 }
466
467 free(pop);
5be7fa93
WD
468}
469
d567322f 470static int check_one_exclude(char *name, struct exclude_struct *ex,
5be7fa93 471 int name_is_dir)
2b6b4d53 472{
9f186578 473 char *p, full_name[MAXPATHLEN];
0f2ac855 474 int match_start = 0;
2b6b4d53
AT
475 char *pattern = ex->pattern;
476
9f186578
WD
477 if (!*name)
478 return 0;
479
170381c0
WD
480 /* If the pattern does not have any slashes AND it does not have
481 * a "**" (which could match a slash), then we just match the
482 * name portion of the path. */
6dfd07d0 483 if (!ex->u.slash_cnt && !(ex->match_flags & MATCHFLG_WILD2)) {
5be7fa93
WD
484 if ((p = strrchr(name,'/')) != NULL)
485 name = p+1;
486 }
9f186578 487 else if (ex->match_flags & MATCHFLG_ABS_PATH && *name != '/'
6dfd07d0
WD
488 && curr_dir_len > module_dirlen + 1) {
489 pathjoin(full_name, sizeof full_name,
490 curr_dir + module_dirlen + 1, name);
5be7fa93
WD
491 name = full_name;
492 }
2b6b4d53 493
5e972dcf
WD
494 if (ex->match_flags & MATCHFLG_DIRECTORY && !name_is_dir)
495 return 0;
2b6b4d53 496
170381c0 497 if (*pattern == '/') {
2b6b4d53
AT
498 match_start = 1;
499 pattern++;
170381c0
WD
500 if (*name == '/')
501 name++;
2b6b4d53
AT
502 }
503
170381c0 504 if (ex->match_flags & MATCHFLG_WILD) {
170381c0
WD
505 /* A non-anchored match with an infix slash and no "**"
506 * needs to match the last slash_cnt+1 name elements. */
6dfd07d0 507 if (!match_start && ex->u.slash_cnt
5e972dcf 508 && !(ex->match_flags & MATCHFLG_WILD2)) {
6dfd07d0 509 int cnt = ex->u.slash_cnt + 1;
170381c0
WD
510 for (p = name + strlen(name) - 1; p >= name; p--) {
511 if (*p == '/' && !--cnt)
512 break;
513 }
514 name = p+1;
515 }
fe332038 516 if (wildmatch(pattern, name))
2b6b4d53 517 return 1;
170381c0
WD
518 if (ex->match_flags & MATCHFLG_WILD2_PREFIX) {
519 /* If the **-prefixed pattern has a '/' as the next
520 * character, then try to match the rest of the
521 * pattern at the root. */
fe332038 522 if (pattern[2] == '/' && wildmatch(pattern+3, name))
170381c0 523 return 1;
c36cd317 524 }
170381c0
WD
525 else if (!match_start && ex->match_flags & MATCHFLG_WILD2) {
526 /* A non-anchored match with an infix or trailing "**"
527 * (but not a prefixed "**") needs to try matching
528 * after every slash. */
529 while ((name = strchr(name, '/')) != NULL) {
530 name++;
fe332038 531 if (wildmatch(pattern, name))
170381c0
WD
532 return 1;
533 }
534 }
535 } else if (match_start) {
536 if (strcmp(name,pattern) == 0)
537 return 1;
2b6b4d53
AT
538 } else {
539 int l1 = strlen(name);
ea2111d1 540 int l2 = strlen(pattern);
0f2ac855 541 if (l2 <= l1 &&
ea2111d1 542 strcmp(name+(l1-l2),pattern) == 0 &&
170381c0 543 (l1==l2 || name[l1-(l2+1)] == '/')) {
2b6b4d53 544 return 1;
c36cd317 545 }
2b6b4d53
AT
546 }
547
548 return 0;
c627d613
AT
549}
550
551
d567322f
MP
552static void report_exclude_result(char const *name,
553 struct exclude_struct const *ent,
61414c83 554 int name_is_dir, const char *type)
d567322f 555{
0f2ac855
WD
556 /* If a trailing slash is present to match only directories,
557 * then it is stripped out by make_exclude. So as a special
558 * case we add it back in here. */
559
ea847c62 560 if (verbose >= 2) {
6dfd07d0 561 rprintf(FINFO, "[%s] %scluding %s %s because of pattern %s%s%s\n",
5e972dcf
WD
562 who_am_i(),
563 ent->match_flags & MATCHFLG_INCLUDE ? "in" : "ex",
6dfd07d0
WD
564 name_is_dir ? "directory" : "file", name, ent->pattern,
565 ent->match_flags & MATCHFLG_DIRECTORY ? "/" : "", type);
ea847c62 566 }
d567322f
MP
567}
568
569
570/*
a6536635
WD
571 * Return -1 if file "name" is defined to be excluded by the specified
572 * exclude list, 1 if it is included, and 0 if it was not matched.
d567322f 573 */
67340e95 574int check_exclude(struct exclude_list_struct *listp, char *name, int name_is_dir)
c627d613 575{
0f2ac855 576 struct exclude_struct *ent;
c627d613 577
b2aa573b 578 for (ent = listp->head; ent; ent = ent->next) {
6dfd07d0
WD
579 if (ent->match_flags & MATCHFLG_PERDIR_MERGE) {
580 int rc = check_exclude(ent->u.mergelist, name,
581 name_is_dir);
582 if (rc)
583 return rc;
584 continue;
585 }
5be7fa93 586 if (check_one_exclude(name, ent, name_is_dir)) {
67340e95
WD
587 report_exclude_result(name, ent, name_is_dir,
588 listp->debug_type);
5e972dcf 589 return ent->match_flags & MATCHFLG_INCLUDE ? 1 : -1;
0f2ac855 590 }
2b6b4d53 591 }
c627d613 592
2b6b4d53 593 return 0;
c627d613
AT
594}
595
596
f8f72644
WD
597/* Get the next include/exclude arg from the string. The token will not
598 * be '\0' terminated, so use the returned length to limit the string.
599 * Also, be sure to add this length to the returned pointer before passing
e425fbe8 600 * it back to ask for the next token. This routine parses the "!" (list-
6dfd07d0 601 * clearing) token and (if xflags does NOT contain XFLG_NO_PREFIXES) the
e425fbe8 602 * +/- prefixes for overriding the include/exclude mode. The *flag_ptr
65e24870 603 * value will also be set to the MATCHFLG_* bits for the current token.
f8f72644 604 */
6dfd07d0
WD
605static const char *get_exclude_tok(const char *p, int xflags,
606 unsigned int *len_ptr, unsigned int *flag_ptr)
f8f72644 607{
abca4eba 608 const unsigned char *s = (const unsigned char *)p;
5e972dcf 609 unsigned int len, mflags = 0;
6dfd07d0 610 int empty_pat_is_OK = 0;
f8f72644 611
96d3590a
WD
612 if (xflags & XFLG_WORD_SPLIT) {
613 /* Skip over any initial whitespace. */
614 while (isspace(*s))
f8f72644 615 s++;
6dfd07d0 616 /* Update to point to real start of rule. */
abca4eba 617 p = (const char *)s;
f8f72644 618 }
6dfd07d0
WD
619 if (!*s)
620 return NULL;
621
622 /* Figure out what kind of a filter rule "s" is pointing at. */
623 if (!(xflags & (XFLG_DEF_INCLUDE | XFLG_DEF_EXCLUDE))) {
624 char *mods = "";
625 switch (*s) {
626 case ':':
627 mflags |= MATCHFLG_PERDIR_MERGE
628 | MATCHFLG_FINISH_SETUP;
629 /* FALL THROUGH */
630 case '.':
631 mflags |= MATCHFLG_MERGE_FILE;
632 mods = "-+Cens";
633 break;
634 case '+':
635 mflags |= MATCHFLG_INCLUDE;
636 break;
637 case '-':
638 break;
639 case '!':
640 mflags |= MATCHFLG_CLEAR_LIST;
641 mods = NULL;
642 break;
643 default:
644 rprintf(FERROR, "Unknown filter rule: %s\n", p);
645 exit_cleanup(RERR_SYNTAX);
646 }
647 while (mods && *++s && *s != ' ' && *s != '=' && *s != '_') {
648 if (strchr(mods, *s) == NULL) {
649 if (xflags & XFLG_WORD_SPLIT && isspace(*s)) {
650 s--;
651 break;
652 }
653 rprintf(FERROR,
654 "unknown option '%c' in filter rule: %s\n",
655 *s, p);
656 exit_cleanup(RERR_SYNTAX);
657 }
658 switch (*s) {
659 case '-':
660 mflags |= MATCHFLG_NO_PREFIXES;
661 break;
662 case '+':
663 mflags |= MATCHFLG_NO_PREFIXES
664 | MATCHFLG_INCLUDE;
665 break;
666 case 'C':
667 empty_pat_is_OK = 1;
668 mflags |= MATCHFLG_NO_PREFIXES
669 | MATCHFLG_WORD_SPLIT
670 | MATCHFLG_NO_INHERIT;
671 break;
672 case 'e':
673 mflags |= MATCHFLG_EXCLUDE_SELF;
674 break;
675 case 'n':
676 mflags |= MATCHFLG_NO_INHERIT;
677 break;
678 case 's':
679 mflags |= MATCHFLG_WORD_SPLIT;
680 break;
681 }
682 }
683 if (*s)
684 s++;
685 } else if (!(xflags & XFLG_NO_PREFIXES)
96d3590a 686 && (*s == '-' || *s == '+') && s[1] == ' ') {
5e972dcf
WD
687 if (*s == '+')
688 mflags |= MATCHFLG_INCLUDE;
96d3590a 689 s += 2;
6dfd07d0
WD
690 } else {
691 if (xflags & XFLG_DEF_INCLUDE)
692 mflags |= MATCHFLG_INCLUDE;
693 if (*s == '!')
694 mflags |= MATCHFLG_CLEAR_LIST; /* Tentative! */
695 }
696
9a5e37fc
WD
697 if (xflags & XFLG_DIRECTORY)
698 mflags |= MATCHFLG_DIRECTORY;
96d3590a
WD
699
700 if (xflags & XFLG_WORD_SPLIT) {
701 const unsigned char *cp = s;
702 /* Token ends at whitespace or the end of the string. */
703 while (!isspace(*cp) && *cp != '\0')
704 cp++;
705 len = cp - s;
706 } else
707 len = strlen(s);
708
6dfd07d0
WD
709 if (mflags & MATCHFLG_CLEAR_LIST) {
710 if (!(xflags & (XFLG_DEF_INCLUDE | XFLG_DEF_EXCLUDE)) && len) {
711 rprintf(FERROR,
712 "'!' rule has trailing characters: %s\n", p);
713 exit_cleanup(RERR_SYNTAX);
714 }
715 if (len > 1)
716 mflags &= ~MATCHFLG_CLEAR_LIST;
717 } else if (!len && !empty_pat_is_OK) {
718 rprintf(FERROR, "unexpected end of filter rule: %s\n", p);
719 exit_cleanup(RERR_SYNTAX);
720 }
721
722 if (xflags & XFLG_ABS_PATH)
723 mflags |= MATCHFLG_ABS_PATH;
96d3590a
WD
724
725 *len_ptr = len;
5e972dcf 726 *flag_ptr = mflags;
96d3590a 727 return (const char *)s;
f8f72644
WD
728}
729
730
b2aa573b
WD
731void add_exclude(struct exclude_list_struct *listp, const char *pattern,
732 int xflags)
c627d613 733{
5e972dcf 734 unsigned int pat_len, mflags;
6dfd07d0 735 const char *cp, *p;
5be7fa93 736
f8f72644 737 if (!pattern)
5e7dbaca 738 return;
f8f72644 739
b2aa573b 740 while (1) {
6dfd07d0
WD
741 /* Remember that the returned string is NOT '\0' terminated! */
742 cp = get_exclude_tok(pattern, xflags, &pat_len, &mflags);
743 if (!cp)
b2aa573b 744 break;
6dfd07d0
WD
745 if (pat_len >= MAXPATHLEN) {
746 rprintf(FERROR, "discarding over-long exclude: %s\n",
747 cp);
748 continue;
749 }
750 pattern = cp + pat_len;
5e972dcf
WD
751
752 if (mflags & MATCHFLG_CLEAR_LIST) {
de91e757
WD
753 if (verbose > 2) {
754 rprintf(FINFO,
6dfd07d0 755 "[%s] clearing exclude list%s\n",
de91e757
WD
756 who_am_i(), listp->debug_type);
757 }
8cbf495a 758 clear_exclude_list(listp);
5e972dcf
WD
759 continue;
760 }
b2aa573b 761
6dfd07d0
WD
762 if (!pat_len) {
763 cp = ".cvsignore";
764 pat_len = 10;
765 }
5e972dcf 766
6dfd07d0
WD
767 if (mflags & MATCHFLG_MERGE_FILE) {
768 unsigned int len = pat_len;
769 if (mflags & MATCHFLG_EXCLUDE_SELF) {
770 const char *name = strrchr(cp, '/');
771 if (name)
772 len -= ++name - cp;
773 else
774 name = cp;
775 make_exclude(listp, name, len, 0);
776 mflags &= ~MATCHFLG_EXCLUDE_SELF;
777 len = pat_len;
778 }
779 if (mflags & MATCHFLG_PERDIR_MERGE) {
780 if (parent_dirscan) {
781 if (!(p = parse_merge_name(cp, &len, module_dirlen)))
782 continue;
783 make_exclude(listp, p, len, mflags);
784 continue;
785 }
786 } else {
787 int flgs = XFLG_FATAL_ERRORS;
788 if (!(p = parse_merge_name(cp, &len, 0)))
789 continue;
790 if (mflags & MATCHFLG_INCLUDE)
791 flgs |= XFLG_DEF_INCLUDE;
792 else if (mflags & MATCHFLG_NO_PREFIXES)
793 flgs |= XFLG_DEF_EXCLUDE;
794 add_exclude_file(listp, p, flgs);
795 continue;
796 }
f8f72644 797 }
6dfd07d0
WD
798
799 make_exclude(listp, cp, pat_len, mflags);
8c35542d 800 }
c627d613
AT
801}
802
c627d613 803
b2aa573b 804void add_exclude_file(struct exclude_list_struct *listp, const char *fname,
f8f72644 805 int xflags)
c627d613 806{
5e7dbaca 807 FILE *fp;
6dfd07d0 808 char line[MAXPATHLEN+MAX_EXCLUDE_PREFIX+1]; /* +1 for trailing slash. */
7cd72c79 809 char *eob = line + sizeof line - 1;
40d38dc0 810 int word_split = xflags & XFLG_WORD_SPLIT;
ccdff3eb 811
5be7fa93
WD
812 if (!fname || !*fname)
813 return;
814
815 if (*fname != '-' || fname[1])
5e7dbaca 816 fp = fopen(fname, "rb");
ccdff3eb 817 else
5e7dbaca
WD
818 fp = stdin;
819 if (!fp) {
f8f72644 820 if (xflags & XFLG_FATAL_ERRORS) {
a039749b 821 rsyserr(FERROR, errno,
6dfd07d0
WD
822 "failed to open %sclude file %s",
823 xflags & XFLG_DEF_INCLUDE ? "in" : "ex",
824 safe_fname(fname));
65417579 825 exit_cleanup(RERR_FILEIO);
2b6b4d53 826 }
5be7fa93 827 return;
2b6b4d53 828 }
6dfd07d0
WD
829 dirbuf[dirbuf_len] = '\0';
830
831 if (verbose > 2) {
832 rprintf(FINFO, "[%s] add_exclude_file(%s,%d)\n",
833 who_am_i(), safe_fname(fname), xflags);
834 }
2b6b4d53 835
ccdff3eb 836 while (1) {
5e7dbaca 837 char *s = line;
619d21ff 838 int ch, overflow = 0;
ccdff3eb 839 while (1) {
5e7dbaca
WD
840 if ((ch = getc(fp)) == EOF) {
841 if (ferror(fp) && errno == EINTR)
ccdff3eb
WD
842 continue;
843 break;
844 }
40d38dc0
WD
845 if (word_split && isspace(ch))
846 break;
ccdff3eb
WD
847 if (eol_nulls? !ch : (ch == '\n' || ch == '\r'))
848 break;
849 if (s < eob)
850 *s++ = ch;
619d21ff
WD
851 else
852 overflow = 1;
853 }
854 if (overflow) {
855 rprintf(FERROR, "discarding over-long exclude: %s...\n", line);
856 s = line;
ccdff3eb
WD
857 }
858 *s = '\0';
7f0feb4d
WD
859 /* Skip an empty token and (when line parsing) comments. */
860 if (*line && (word_split || (*line != ';' && *line != '#')))
f8f72644 861 add_exclude(listp, line, xflags);
5e7dbaca 862 if (ch == EOF)
ccdff3eb 863 break;
2b6b4d53 864 }
5e7dbaca 865 fclose(fp);
c627d613
AT
866}
867
868
869void send_exclude_list(int f)
870{
b2aa573b 871 struct exclude_struct *ent;
25cf8893 872
353f2724
WD
873 /* This is a complete hack - blame Rusty. FIXME!
874 * Remove this hack when older rsyncs (below 2.6.4) are gone. */
875 if (list_only == 1 && !recurse)
6dfd07d0 876 add_exclude(&exclude_list, "/*/*", XFLG_DEF_EXCLUDE);
2b6b4d53 877
b2aa573b 878 for (ent = exclude_list.head; ent; ent = ent->next) {
5f5be796 879 unsigned int l;
6dfd07d0 880 char p[MAXPATHLEN+MAX_EXCLUDE_PREFIX+1];
2fb139c1 881
b2aa573b 882 l = strlcpy(p, ent->pattern, sizeof p);
5f5be796
WD
883 if (l == 0 || l >= MAXPATHLEN)
884 continue;
5e972dcf 885 if (ent->match_flags & MATCHFLG_DIRECTORY) {
a3dbb20a
WD
886 p[l++] = '/';
887 p[l] = '\0';
5f5be796 888 }
2b6b4d53 889
6dfd07d0
WD
890 if (ent->match_flags & MATCHFLG_PERDIR_MERGE) {
891 char buf[MAX_EXCLUDE_PREFIX], *op = buf;
892 if (protocol_version < 29) {
893 rprintf(FERROR,
894 "remote rsync is too old to understand per-directory merge files.\n");
895 exit_cleanup(RERR_SYNTAX);
896 }
897 *op++ = ':';
898 if (ent->match_flags & MATCHFLG_WORD_SPLIT)
899 *op++ = 's';
900 if (ent->match_flags & MATCHFLG_NO_INHERIT)
901 *op++ = 'n';
902 if (ent->match_flags & MATCHFLG_EXCLUDE_SELF)
903 *op++ = 'e';
904 if (ent->match_flags & MATCHFLG_NO_PREFIXES) {
905 if (ent->match_flags & MATCHFLG_INCLUDE)
906 *op++ = '+';
907 else
908 *op++ = '-';
909 }
910 *op++ = ' ';
911 if (op - buf > MAX_EXCLUDE_PREFIX)
912 overflow("send_exclude_list");
913 write_int(f, l + (op - buf));
914 write_buf(f, buf, op - buf);
915 } else if (ent->match_flags & MATCHFLG_INCLUDE) {
a3dbb20a
WD
916 write_int(f, l + 2);
917 write_buf(f, "+ ", 2);
6dfd07d0
WD
918 } else if (protocol_version >= 29
919 || ((*p == '-' || *p == '+') && p[1] == ' ')) {
a3dbb20a
WD
920 write_int(f, l + 2);
921 write_buf(f, "- ", 2);
922 } else
923 write_int(f, l);
924 write_buf(f, p, l);
0f2ac855 925 }
2b6b4d53 926
a3dbb20a 927 write_int(f, 0);
c627d613
AT
928}
929
930
931void recv_exclude_list(int f)
932{
6dfd07d0
WD
933 char line[MAXPATHLEN+MAX_EXCLUDE_PREFIX+1]; /* +1 for trailing slash. */
934 unsigned int xflags = protocol_version >= 29 ? 0 : XFLG_DEF_EXCLUDE;
9dd891bb
MP
935 unsigned int l;
936
5f5be796
WD
937 while ((l = read_int(f)) != 0) {
938 if (l >= sizeof line)
939 overflow("recv_exclude_list");
940 read_sbuf(f, line, l);
6dfd07d0 941 add_exclude(&exclude_list, line, xflags);
651443a7 942 }
651443a7
DD
943}
944
0f2ac855 945
f8f72644
WD
946static char default_cvsignore[] =
947 /* These default ignored items come from the CVS manual. */
948 "RCS SCCS CVS CVS.adm RCSLOG cvslog.* tags TAGS"
949 " .make.state .nse_depinfo *~ #* .#* ,* _$* *$"
950 " *.old *.bak *.BAK *.orig *.rej .del-*"
951 " *.a *.olb *.o *.obj *.so *.exe"
952 " *.Z *.elc *.ln core"
953 /* The rest we added to suit ourself. */
954 " .svn/";
c627d613
AT
955
956void add_cvs_excludes(void)
957{
6dfd07d0
WD
958 static unsigned int cvs_flags = XFLG_WORD_SPLIT | XFLG_NO_PREFIXES
959 | XFLG_DEF_EXCLUDE;
2b6b4d53
AT
960 char fname[MAXPATHLEN];
961 char *p;
0f2ac855 962
6dfd07d0
WD
963 add_exclude(&exclude_list, ":C", 0);
964 add_exclude(&exclude_list, default_cvsignore, cvs_flags);
c627d613 965
a7725e6d 966 if ((p = getenv("HOME"))
f8f72644 967 && pathjoin(fname, sizeof fname, p, ".cvsignore") < sizeof fname) {
6dfd07d0 968 add_exclude_file(&exclude_list, fname, cvs_flags);
f8f72644 969 }
c627d613 970
6dfd07d0 971 add_exclude(&exclude_list, getenv("CVSIGNORE"), cvs_flags);
c627d613 972}