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