Convert a couple files to UTF-8; more Copyright years.
[rsync.git] / exclude.c
bloba15a4bb3ab2decf5a0b14467145776734307511f
1 /*
2 * The filter include/exclude routines.
4 * Copyright (C) 1996-2001 Andrew Tridgell <tridge@samba.org>
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2002 Martin Pool
7 * Copyright (C) 2003-2019 Wayne Davison
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, visit the http://fsf.org website.
23 #include "rsync.h"
25 extern int am_server;
26 extern int am_sender;
27 extern int eol_nulls;
28 extern int io_error;
29 extern int local_server;
30 extern int prune_empty_dirs;
31 extern int ignore_perishable;
32 extern int delete_mode;
33 extern int delete_excluded;
34 extern int cvs_exclude;
35 extern int sanitize_paths;
36 extern int protocol_version;
37 extern int module_id;
39 extern char curr_dir[MAXPATHLEN];
40 extern unsigned int curr_dir_len;
41 extern unsigned int module_dirlen;
43 filter_rule_list filter_list = { .debug_type = "" };
44 filter_rule_list cvs_filter_list = { .debug_type = " [global CVS]" };
45 filter_rule_list daemon_filter_list = { .debug_type = " [daemon]" };
47 int saw_xattr_filter = 0;
49 /* Need room enough for ":MODS " prefix plus some room to grow. */
50 #define MAX_RULE_PREFIX (16)
52 #define SLASH_WILD3_SUFFIX "/***"
54 /* The dirbuf is set by push_local_filters() to the current subdirectory
55 * relative to curr_dir that is being processed. The path always has a
56 * trailing slash appended, and the variable dirbuf_len contains the length
57 * of this path prefix. The path is always absolute. */
58 static char dirbuf[MAXPATHLEN+1];
59 static unsigned int dirbuf_len = 0;
60 static int dirbuf_depth;
62 /* This is True when we're scanning parent dirs for per-dir merge-files. */
63 static BOOL parent_dirscan = False;
65 /* This array contains a list of all the currently active per-dir merge
66 * files. This makes it easier to save the appropriate values when we
67 * "push" down into each subdirectory. */
68 static filter_rule **mergelist_parents;
69 static int mergelist_cnt = 0;
70 static int mergelist_size = 0;
72 /* Each filter_list_struct describes a singly-linked list by keeping track
73 * of both the head and tail pointers. The list is slightly unusual in that
74 * a parent-dir's content can be appended to the end of the local list in a
75 * special way: the last item in the local list has its "next" pointer set
76 * to point to the inherited list, but the local list's tail pointer points
77 * at the end of the local list. Thus, if the local list is empty, the head
78 * will be pointing at the inherited content but the tail will be NULL. To
79 * help you visualize this, here are the possible list arrangements:
81 * Completely Empty Local Content Only
82 * ================================== ====================================
83 * head -> NULL head -> Local1 -> Local2 -> NULL
84 * tail -> NULL tail -------------^
86 * Inherited Content Only Both Local and Inherited Content
87 * ================================== ====================================
88 * head -> Parent1 -> Parent2 -> NULL head -> L1 -> L2 -> P1 -> P2 -> NULL
89 * tail -> NULL tail ---------^
91 * This means that anyone wanting to traverse the whole list to use it just
92 * needs to start at the head and use the "next" pointers until it goes
93 * NULL. To add new local content, we insert the item after the tail item
94 * and update the tail (obviously, if "tail" was NULL, we insert it at the
95 * head). To clear the local list, WE MUST NOT FREE THE INHERITED CONTENT
96 * because it is shared between the current list and our parent list(s).
97 * The easiest way to handle this is to simply truncate the list after the
98 * tail item and then free the local list from the head. When inheriting
99 * the list for a new local dir, we just save off the filter_list_struct
100 * values (so we can pop back to them later) and set the tail to NULL.
103 static void teardown_mergelist(filter_rule *ex)
105 int j;
107 if (!ex->u.mergelist)
108 return;
110 if (DEBUG_GTE(FILTER, 2)) {
111 rprintf(FINFO, "[%s] deactivating mergelist #%d%s\n",
112 who_am_i(), mergelist_cnt - 1,
113 ex->u.mergelist->debug_type);
116 free(ex->u.mergelist->debug_type);
117 free(ex->u.mergelist);
119 for (j = 0; j < mergelist_cnt; j++) {
120 if (mergelist_parents[j] == ex) {
121 mergelist_parents[j] = NULL;
122 break;
125 while (mergelist_cnt && mergelist_parents[mergelist_cnt-1] == NULL)
126 mergelist_cnt--;
129 static void free_filter(filter_rule *ex)
131 if (ex->rflags & FILTRULE_PERDIR_MERGE)
132 teardown_mergelist(ex);
133 free(ex->pattern);
134 free(ex);
137 static void free_filters(filter_rule *ent)
139 while (ent) {
140 filter_rule *next = ent->next;
141 free_filter(ent);
142 ent = next;
146 /* Build a filter structure given a filter pattern. The value in "pat"
147 * is not null-terminated. "rule" is either held or freed, so the
148 * caller should not free it. */
149 static void add_rule(filter_rule_list *listp, const char *pat, unsigned int pat_len,
150 filter_rule *rule, int xflags)
152 const char *cp;
153 unsigned int pre_len, suf_len, slash_cnt = 0;
155 if (DEBUG_GTE(FILTER, 2)) {
156 rprintf(FINFO, "[%s] add_rule(%s%.*s%s)%s\n",
157 who_am_i(), get_rule_prefix(rule, pat, 0, NULL),
158 (int)pat_len, pat,
159 (rule->rflags & FILTRULE_DIRECTORY) ? "/" : "",
160 listp->debug_type);
163 /* These flags also indicate that we're reading a list that
164 * needs to be filtered now, not post-filtered later. */
165 if (xflags & (XFLG_ANCHORED2ABS|XFLG_ABS_IF_SLASH)
166 && (rule->rflags & FILTRULES_SIDES)
167 == (am_sender ? FILTRULE_RECEIVER_SIDE : FILTRULE_SENDER_SIDE)) {
168 /* This filter applies only to the other side. Drop it. */
169 free_filter(rule);
170 return;
173 if (pat_len > 1 && pat[pat_len-1] == '/') {
174 pat_len--;
175 rule->rflags |= FILTRULE_DIRECTORY;
178 for (cp = pat; cp < pat + pat_len; cp++) {
179 if (*cp == '/')
180 slash_cnt++;
183 if (!(rule->rflags & (FILTRULE_ABS_PATH | FILTRULE_MERGE_FILE))
184 && ((xflags & (XFLG_ANCHORED2ABS|XFLG_ABS_IF_SLASH) && *pat == '/')
185 || (xflags & XFLG_ABS_IF_SLASH && slash_cnt))) {
186 rule->rflags |= FILTRULE_ABS_PATH;
187 if (*pat == '/')
188 pre_len = dirbuf_len - module_dirlen - 1;
189 else
190 pre_len = 0;
191 } else
192 pre_len = 0;
194 /* The daemon wants dir-exclude rules to get an appended "/" + "***". */
195 if (xflags & XFLG_DIR2WILD3
196 && BITS_SETnUNSET(rule->rflags, FILTRULE_DIRECTORY, FILTRULE_INCLUDE)) {
197 rule->rflags &= ~FILTRULE_DIRECTORY;
198 suf_len = sizeof SLASH_WILD3_SUFFIX - 1;
199 } else
200 suf_len = 0;
202 if (!(rule->pattern = new_array(char, pre_len + pat_len + suf_len + 1)))
203 out_of_memory("add_rule");
204 if (pre_len) {
205 memcpy(rule->pattern, dirbuf + module_dirlen, pre_len);
206 for (cp = rule->pattern; cp < rule->pattern + pre_len; cp++) {
207 if (*cp == '/')
208 slash_cnt++;
211 strlcpy(rule->pattern + pre_len, pat, pat_len + 1);
212 pat_len += pre_len;
213 if (suf_len) {
214 memcpy(rule->pattern + pat_len, SLASH_WILD3_SUFFIX, suf_len+1);
215 pat_len += suf_len;
216 slash_cnt++;
219 if (strpbrk(rule->pattern, "*[?")) {
220 rule->rflags |= FILTRULE_WILD;
221 if ((cp = strstr(rule->pattern, "**")) != NULL) {
222 rule->rflags |= FILTRULE_WILD2;
223 /* If the pattern starts with **, note that. */
224 if (cp == rule->pattern)
225 rule->rflags |= FILTRULE_WILD2_PREFIX;
226 /* If the pattern ends with ***, note that. */
227 if (pat_len >= 3
228 && rule->pattern[pat_len-3] == '*'
229 && rule->pattern[pat_len-2] == '*'
230 && rule->pattern[pat_len-1] == '*')
231 rule->rflags |= FILTRULE_WILD3_SUFFIX;
235 if (rule->rflags & FILTRULE_PERDIR_MERGE) {
236 filter_rule_list *lp;
237 unsigned int len;
238 int i;
240 if ((cp = strrchr(rule->pattern, '/')) != NULL)
241 cp++;
242 else
243 cp = rule->pattern;
245 /* If the local merge file was already mentioned, don't
246 * add it again. */
247 for (i = 0; i < mergelist_cnt; i++) {
248 filter_rule *ex = mergelist_parents[i];
249 const char *s;
250 if (!ex)
251 continue;
252 s = strrchr(ex->pattern, '/');
253 if (s)
254 s++;
255 else
256 s = ex->pattern;
257 len = strlen(s);
258 if (len == pat_len - (cp - rule->pattern) && memcmp(s, cp, len) == 0) {
259 free_filter(rule);
260 return;
264 if (!(lp = new_array0(filter_rule_list, 1)))
265 out_of_memory("add_rule");
266 if (asprintf(&lp->debug_type, " [per-dir %s]", cp) < 0)
267 out_of_memory("add_rule");
268 rule->u.mergelist = lp;
270 if (mergelist_cnt == mergelist_size) {
271 mergelist_size += 5;
272 mergelist_parents = realloc_array(mergelist_parents,
273 filter_rule *,
274 mergelist_size);
275 if (!mergelist_parents)
276 out_of_memory("add_rule");
278 if (DEBUG_GTE(FILTER, 2)) {
279 rprintf(FINFO, "[%s] activating mergelist #%d%s\n",
280 who_am_i(), mergelist_cnt, lp->debug_type);
282 mergelist_parents[mergelist_cnt++] = rule;
283 } else
284 rule->u.slash_cnt = slash_cnt;
286 if (!listp->tail) {
287 rule->next = listp->head;
288 listp->head = listp->tail = rule;
289 } else {
290 rule->next = listp->tail->next;
291 listp->tail->next = rule;
292 listp->tail = rule;
296 /* This frees any non-inherited items, leaving just inherited items on the list. */
297 static void pop_filter_list(filter_rule_list *listp)
299 filter_rule *inherited;
301 if (!listp->tail)
302 return;
304 inherited = listp->tail->next;
306 /* Truncate any inherited items from the local list. */
307 listp->tail->next = NULL;
308 /* Now free everything that is left. */
309 free_filters(listp->head);
311 listp->head = inherited;
312 listp->tail = NULL;
315 /* This returns an expanded (absolute) filename for the merge-file name if
316 * the name has any slashes in it OR if the parent_dirscan var is True;
317 * otherwise it returns the original merge_file name. If the len_ptr value
318 * is non-NULL the merge_file name is limited by the referenced length
319 * value and will be updated with the length of the resulting name. We
320 * always return a name that is null terminated, even if the merge_file
321 * name was not. */
322 static char *parse_merge_name(const char *merge_file, unsigned int *len_ptr,
323 unsigned int prefix_skip)
325 static char buf[MAXPATHLEN];
326 char *fn, tmpbuf[MAXPATHLEN];
327 unsigned int fn_len;
329 if (!parent_dirscan && *merge_file != '/') {
330 /* Return the name unchanged it doesn't have any slashes. */
331 if (len_ptr) {
332 const char *p = merge_file + *len_ptr;
333 while (--p > merge_file && *p != '/') {}
334 if (p == merge_file) {
335 strlcpy(buf, merge_file, *len_ptr + 1);
336 return buf;
338 } else if (strchr(merge_file, '/') == NULL)
339 return (char *)merge_file;
342 fn = *merge_file == '/' ? buf : tmpbuf;
343 if (sanitize_paths) {
344 const char *r = prefix_skip ? "/" : NULL;
345 /* null-terminate the name if it isn't already */
346 if (len_ptr && merge_file[*len_ptr]) {
347 char *to = fn == buf ? tmpbuf : buf;
348 strlcpy(to, merge_file, *len_ptr + 1);
349 merge_file = to;
351 if (!sanitize_path(fn, merge_file, r, dirbuf_depth, SP_DEFAULT)) {
352 rprintf(FERROR, "merge-file name overflows: %s\n",
353 merge_file);
354 return NULL;
356 fn_len = strlen(fn);
357 } else {
358 strlcpy(fn, merge_file, len_ptr ? *len_ptr + 1 : MAXPATHLEN);
359 fn_len = clean_fname(fn, CFN_COLLAPSE_DOT_DOT_DIRS);
362 /* If the name isn't in buf yet, it wasn't absolute. */
363 if (fn != buf) {
364 int d_len = dirbuf_len - prefix_skip;
365 if (d_len + fn_len >= MAXPATHLEN) {
366 rprintf(FERROR, "merge-file name overflows: %s\n", fn);
367 return NULL;
369 memcpy(buf, dirbuf + prefix_skip, d_len);
370 memcpy(buf + d_len, fn, fn_len + 1);
371 fn_len = clean_fname(buf, CFN_COLLAPSE_DOT_DOT_DIRS);
374 if (len_ptr)
375 *len_ptr = fn_len;
376 return buf;
379 /* Sets the dirbuf and dirbuf_len values. */
380 void set_filter_dir(const char *dir, unsigned int dirlen)
382 unsigned int len;
383 if (*dir != '/') {
384 memcpy(dirbuf, curr_dir, curr_dir_len);
385 dirbuf[curr_dir_len] = '/';
386 len = curr_dir_len + 1;
387 if (len + dirlen >= MAXPATHLEN)
388 dirlen = 0;
389 } else
390 len = 0;
391 memcpy(dirbuf + len, dir, dirlen);
392 dirbuf[dirlen + len] = '\0';
393 dirbuf_len = clean_fname(dirbuf, CFN_COLLAPSE_DOT_DOT_DIRS);
394 if (dirbuf_len > 1 && dirbuf[dirbuf_len-1] == '.'
395 && dirbuf[dirbuf_len-2] == '/')
396 dirbuf_len -= 2;
397 if (dirbuf_len != 1)
398 dirbuf[dirbuf_len++] = '/';
399 dirbuf[dirbuf_len] = '\0';
400 if (sanitize_paths)
401 dirbuf_depth = count_dir_elements(dirbuf + module_dirlen);
404 /* This routine takes a per-dir merge-file entry and finishes its setup.
405 * If the name has a path portion then we check to see if it refers to a
406 * parent directory of the first transfer dir. If it does, we scan all the
407 * dirs from that point through the parent dir of the transfer dir looking
408 * for the per-dir merge-file in each one. */
409 static BOOL setup_merge_file(int mergelist_num, filter_rule *ex,
410 filter_rule_list *lp)
412 char buf[MAXPATHLEN];
413 char *x, *y, *pat = ex->pattern;
414 unsigned int len;
416 if (!(x = parse_merge_name(pat, NULL, 0)) || *x != '/')
417 return 0;
419 if (DEBUG_GTE(FILTER, 2)) {
420 rprintf(FINFO, "[%s] performing parent_dirscan for mergelist #%d%s\n",
421 who_am_i(), mergelist_num, lp->debug_type);
423 y = strrchr(x, '/');
424 *y = '\0';
425 ex->pattern = strdup(y+1);
426 if (!*x)
427 x = "/";
428 if (*x == '/')
429 strlcpy(buf, x, MAXPATHLEN);
430 else
431 pathjoin(buf, MAXPATHLEN, dirbuf, x);
433 len = clean_fname(buf, CFN_COLLAPSE_DOT_DOT_DIRS);
434 if (len != 1 && len < MAXPATHLEN-1) {
435 buf[len++] = '/';
436 buf[len] = '\0';
438 /* This ensures that the specified dir is a parent of the transfer. */
439 for (x = buf, y = dirbuf; *x && *x == *y; x++, y++) {}
440 if (*x)
441 y += strlen(y); /* nope -- skip the scan */
443 parent_dirscan = True;
444 while (*y) {
445 char save[MAXPATHLEN];
446 strlcpy(save, y, MAXPATHLEN);
447 *y = '\0';
448 dirbuf_len = y - dirbuf;
449 strlcpy(x, ex->pattern, MAXPATHLEN - (x - buf));
450 parse_filter_file(lp, buf, ex, XFLG_ANCHORED2ABS);
451 if (ex->rflags & FILTRULE_NO_INHERIT) {
452 /* Free the undesired rules to clean up any per-dir
453 * mergelists they defined. Otherwise pop_local_filters
454 * may crash trying to restore nonexistent state for
455 * those mergelists. */
456 free_filters(lp->head);
457 lp->head = NULL;
459 lp->tail = NULL;
460 strlcpy(y, save, MAXPATHLEN);
461 while ((*x++ = *y++) != '/') {}
463 parent_dirscan = False;
464 if (DEBUG_GTE(FILTER, 2)) {
465 rprintf(FINFO, "[%s] completed parent_dirscan for mergelist #%d%s\n",
466 who_am_i(), mergelist_num, lp->debug_type);
468 free(pat);
469 return 1;
472 struct local_filter_state {
473 int mergelist_cnt;
474 filter_rule_list mergelists[1];
477 /* Each time rsync changes to a new directory it call this function to
478 * handle all the per-dir merge-files. The "dir" value is the current path
479 * relative to curr_dir (which might not be null-terminated). We copy it
480 * into dirbuf so that we can easily append a file name on the end. */
481 void *push_local_filters(const char *dir, unsigned int dirlen)
483 struct local_filter_state *push;
484 int i;
486 set_filter_dir(dir, dirlen);
487 if (DEBUG_GTE(FILTER, 2)) {
488 rprintf(FINFO, "[%s] pushing local filters for %s\n",
489 who_am_i(), dirbuf);
492 if (!mergelist_cnt) {
493 /* No old state to save and no new merge files to push. */
494 return NULL;
497 push = (struct local_filter_state *)new_array(char,
498 sizeof (struct local_filter_state)
499 + (mergelist_cnt-1) * sizeof (filter_rule_list));
500 if (!push)
501 out_of_memory("push_local_filters");
503 push->mergelist_cnt = mergelist_cnt;
504 for (i = 0; i < mergelist_cnt; i++) {
505 filter_rule *ex = mergelist_parents[i];
506 if (!ex)
507 continue;
508 memcpy(&push->mergelists[i], ex->u.mergelist, sizeof (filter_rule_list));
511 /* Note: parse_filter_file() might increase mergelist_cnt, so keep
512 * this loop separate from the above loop. */
513 for (i = 0; i < mergelist_cnt; i++) {
514 filter_rule *ex = mergelist_parents[i];
515 filter_rule_list *lp;
516 if (!ex)
517 continue;
518 lp = ex->u.mergelist;
520 if (DEBUG_GTE(FILTER, 2)) {
521 rprintf(FINFO, "[%s] pushing mergelist #%d%s\n",
522 who_am_i(), i, lp->debug_type);
525 lp->tail = NULL; /* Switch any local rules to inherited. */
526 if (ex->rflags & FILTRULE_NO_INHERIT)
527 lp->head = NULL;
529 if (ex->rflags & FILTRULE_FINISH_SETUP) {
530 ex->rflags &= ~FILTRULE_FINISH_SETUP;
531 if (setup_merge_file(i, ex, lp))
532 set_filter_dir(dir, dirlen);
535 if (strlcpy(dirbuf + dirbuf_len, ex->pattern,
536 MAXPATHLEN - dirbuf_len) < MAXPATHLEN - dirbuf_len) {
537 parse_filter_file(lp, dirbuf, ex,
538 XFLG_ANCHORED2ABS);
539 } else {
540 io_error |= IOERR_GENERAL;
541 rprintf(FERROR,
542 "cannot add local filter rules in long-named directory: %s\n",
543 full_fname(dirbuf));
545 dirbuf[dirbuf_len] = '\0';
548 return (void*)push;
551 void pop_local_filters(void *mem)
553 struct local_filter_state *pop = (struct local_filter_state *)mem;
554 int i;
555 int old_mergelist_cnt = pop ? pop->mergelist_cnt : 0;
557 if (DEBUG_GTE(FILTER, 2))
558 rprintf(FINFO, "[%s] popping local filters\n", who_am_i());
560 for (i = mergelist_cnt; i-- > 0; ) {
561 filter_rule *ex = mergelist_parents[i];
562 filter_rule_list *lp;
563 if (!ex)
564 continue;
565 lp = ex->u.mergelist;
567 if (DEBUG_GTE(FILTER, 2)) {
568 rprintf(FINFO, "[%s] popping mergelist #%d%s\n",
569 who_am_i(), i, lp->debug_type);
572 pop_filter_list(lp);
573 if (i >= old_mergelist_cnt && lp->head) {
574 /* This mergelist does not exist in the state to be restored, but it
575 * still has inherited rules. This can sometimes happen if a per-dir
576 * merge file calls setup_merge_file() in push_local_filters() and that
577 * leaves some inherited rules that aren't in the pushed list state. */
578 if (DEBUG_GTE(FILTER, 2)) {
579 rprintf(FINFO, "[%s] freeing parent_dirscan filters of mergelist #%d%s\n",
580 who_am_i(), i, ex->u.mergelist->debug_type);
582 pop_filter_list(lp);
586 if (!pop)
587 return; /* No state to restore. */
589 for (i = 0; i < old_mergelist_cnt; i++) {
590 filter_rule *ex = mergelist_parents[i];
591 if (!ex)
592 continue;
593 memcpy(ex->u.mergelist, &pop->mergelists[i], sizeof (filter_rule_list));
596 free(pop);
599 void change_local_filter_dir(const char *dname, int dlen, int dir_depth)
601 static int cur_depth = -1;
602 static void *filt_array[MAXPATHLEN/2+1];
604 if (!dname) {
605 for ( ; cur_depth >= 0; cur_depth--) {
606 if (filt_array[cur_depth]) {
607 pop_local_filters(filt_array[cur_depth]);
608 filt_array[cur_depth] = NULL;
611 return;
614 assert(dir_depth < MAXPATHLEN/2+1);
616 for ( ; cur_depth >= dir_depth; cur_depth--) {
617 if (filt_array[cur_depth]) {
618 pop_local_filters(filt_array[cur_depth]);
619 filt_array[cur_depth] = NULL;
623 cur_depth = dir_depth;
624 filt_array[cur_depth] = push_local_filters(dname, dlen);
627 static int rule_matches(const char *fname, filter_rule *ex, int name_flags)
629 int slash_handling, str_cnt = 0, anchored_match = 0;
630 int ret_match = ex->rflags & FILTRULE_NEGATE ? 0 : 1;
631 char *p, *pattern = ex->pattern;
632 const char *strings[16]; /* more than enough */
633 const char *name = fname + (*fname == '/');
635 if (!*name)
636 return 0;
638 if (!(name_flags & NAME_IS_XATTR) ^ !(ex->rflags & FILTRULE_XATTR))
639 return 0;
641 if (!ex->u.slash_cnt && !(ex->rflags & FILTRULE_WILD2)) {
642 /* If the pattern does not have any slashes AND it does
643 * not have a "**" (which could match a slash), then we
644 * just match the name portion of the path. */
645 if ((p = strrchr(name,'/')) != NULL)
646 name = p+1;
647 } else if (ex->rflags & FILTRULE_ABS_PATH && *fname != '/'
648 && curr_dir_len > module_dirlen + 1) {
649 /* If we're matching against an absolute-path pattern,
650 * we need to prepend our full path info. */
651 strings[str_cnt++] = curr_dir + module_dirlen + 1;
652 strings[str_cnt++] = "/";
653 } else if (ex->rflags & FILTRULE_WILD2_PREFIX && *fname != '/') {
654 /* Allow "**"+"/" to match at the start of the string. */
655 strings[str_cnt++] = "/";
657 strings[str_cnt++] = name;
658 if (name_flags & NAME_IS_DIR) {
659 /* Allow a trailing "/"+"***" to match the directory. */
660 if (ex->rflags & FILTRULE_WILD3_SUFFIX)
661 strings[str_cnt++] = "/";
662 } else if (ex->rflags & FILTRULE_DIRECTORY)
663 return !ret_match;
664 strings[str_cnt] = NULL;
666 if (*pattern == '/') {
667 anchored_match = 1;
668 pattern++;
671 if (!anchored_match && ex->u.slash_cnt
672 && !(ex->rflags & FILTRULE_WILD2)) {
673 /* A non-anchored match with an infix slash and no "**"
674 * needs to match the last slash_cnt+1 name elements. */
675 slash_handling = ex->u.slash_cnt + 1;
676 } else if (!anchored_match && !(ex->rflags & FILTRULE_WILD2_PREFIX)
677 && ex->rflags & FILTRULE_WILD2) {
678 /* A non-anchored match with an infix or trailing "**" (but not
679 * a prefixed "**") needs to try matching after every slash. */
680 slash_handling = -1;
681 } else {
682 /* The pattern matches only at the start of the path or name. */
683 slash_handling = 0;
686 if (ex->rflags & FILTRULE_WILD) {
687 if (wildmatch_array(pattern, strings, slash_handling))
688 return ret_match;
689 } else if (str_cnt > 1) {
690 if (litmatch_array(pattern, strings, slash_handling))
691 return ret_match;
692 } else if (anchored_match) {
693 if (strcmp(name, pattern) == 0)
694 return ret_match;
695 } else {
696 int l1 = strlen(name);
697 int l2 = strlen(pattern);
698 if (l2 <= l1 &&
699 strcmp(name+(l1-l2),pattern) == 0 &&
700 (l1==l2 || name[l1-(l2+1)] == '/')) {
701 return ret_match;
705 return !ret_match;
708 static void report_filter_result(enum logcode code, char const *name,
709 filter_rule const *ent,
710 int name_flags, const char *type)
712 /* If a trailing slash is present to match only directories,
713 * then it is stripped out by add_rule(). So as a special
714 * case we add it back in here. */
716 if (DEBUG_GTE(FILTER, 1)) {
717 static char *actions[2][2]
718 = { {"show", "hid"}, {"risk", "protect"} };
719 const char *w = who_am_i();
720 const char *t = name_flags & NAME_IS_XATTR ? "xattr"
721 : name_flags & NAME_IS_DIR ? "directory"
722 : "file";
723 rprintf(code, "[%s] %sing %s %s because of pattern %s%s%s\n",
724 w, actions[*w!='s'][!(ent->rflags & FILTRULE_INCLUDE)],
725 t, name, ent->pattern,
726 ent->rflags & FILTRULE_DIRECTORY ? "/" : "", type);
730 /* This function is used to check if a file should be included/excluded
731 * from the list of files based on its name and type etc. The value of
732 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
733 int name_is_excluded(const char *fname, int name_flags, int filter_level)
735 if (daemon_filter_list.head && check_filter(&daemon_filter_list, FLOG, fname, name_flags) < 0) {
736 if (!(name_flags & NAME_IS_XATTR))
737 errno = ENOENT;
738 return 1;
741 if (filter_level != ALL_FILTERS)
742 return 0;
744 if (filter_list.head && check_filter(&filter_list, FINFO, fname, name_flags) < 0)
745 return 1;
747 return 0;
750 /* Return -1 if file "name" is defined to be excluded by the specified
751 * exclude list, 1 if it is included, and 0 if it was not matched. */
752 int check_filter(filter_rule_list *listp, enum logcode code,
753 const char *name, int name_flags)
755 filter_rule *ent;
757 for (ent = listp->head; ent; ent = ent->next) {
758 if (ignore_perishable && ent->rflags & FILTRULE_PERISHABLE)
759 continue;
760 if (ent->rflags & FILTRULE_PERDIR_MERGE) {
761 int rc = check_filter(ent->u.mergelist, code, name, name_flags);
762 if (rc)
763 return rc;
764 continue;
766 if (ent->rflags & FILTRULE_CVS_IGNORE) {
767 int rc = check_filter(&cvs_filter_list, code, name, name_flags);
768 if (rc)
769 return rc;
770 continue;
772 if (rule_matches(name, ent, name_flags)) {
773 report_filter_result(code, name, ent, name_flags, listp->debug_type);
774 return ent->rflags & FILTRULE_INCLUDE ? 1 : -1;
778 return 0;
781 #define RULE_STRCMP(s,r) rule_strcmp((s), (r), sizeof (r) - 1)
783 static const uchar *rule_strcmp(const uchar *str, const char *rule, int rule_len)
785 if (strncmp((char*)str, rule, rule_len) != 0)
786 return NULL;
787 if (isspace(str[rule_len]) || str[rule_len] == '_' || !str[rule_len])
788 return str + rule_len - 1;
789 if (str[rule_len] == ',')
790 return str + rule_len;
791 return NULL;
794 #define FILTRULES_FROM_CONTAINER (FILTRULE_ABS_PATH | FILTRULE_INCLUDE \
795 | FILTRULE_DIRECTORY | FILTRULE_NEGATE \
796 | FILTRULE_PERISHABLE)
798 /* Gets the next include/exclude rule from *rulestr_ptr and advances
799 * *rulestr_ptr to point beyond it. Stores the pattern's start (within
800 * *rulestr_ptr) and length in *pat_ptr and *pat_len_ptr, and returns a newly
801 * allocated filter_rule containing the rest of the information. Returns
802 * NULL if there are no more rules in the input.
804 * The template provides defaults for the new rule to inherit, and the
805 * template rflags and the xflags additionally affect parsing. */
806 static filter_rule *parse_rule_tok(const char **rulestr_ptr,
807 const filter_rule *template, int xflags,
808 const char **pat_ptr, unsigned int *pat_len_ptr)
810 const uchar *s = (const uchar *)*rulestr_ptr;
811 filter_rule *rule;
812 unsigned int len;
814 if (template->rflags & FILTRULE_WORD_SPLIT) {
815 /* Skip over any initial whitespace. */
816 while (isspace(*s))
817 s++;
818 /* Update to point to real start of rule. */
819 *rulestr_ptr = (const char *)s;
821 if (!*s)
822 return NULL;
824 if (!(rule = new0(filter_rule)))
825 out_of_memory("parse_rule_tok");
827 /* Inherit from the template. Don't inherit FILTRULES_SIDES; we check
828 * that later. */
829 rule->rflags = template->rflags & FILTRULES_FROM_CONTAINER;
831 /* Figure out what kind of a filter rule "s" is pointing at. Note
832 * that if FILTRULE_NO_PREFIXES is set, the rule is either an include
833 * or an exclude based on the inheritance of the FILTRULE_INCLUDE
834 * flag (above). XFLG_OLD_PREFIXES indicates a compatibility mode
835 * for old include/exclude patterns where just "+ " and "- " are
836 * allowed as optional prefixes. */
837 if (template->rflags & FILTRULE_NO_PREFIXES) {
838 if (*s == '!' && template->rflags & FILTRULE_CVS_IGNORE)
839 rule->rflags |= FILTRULE_CLEAR_LIST; /* Tentative! */
840 } else if (xflags & XFLG_OLD_PREFIXES) {
841 if (*s == '-' && s[1] == ' ') {
842 rule->rflags &= ~FILTRULE_INCLUDE;
843 s += 2;
844 } else if (*s == '+' && s[1] == ' ') {
845 rule->rflags |= FILTRULE_INCLUDE;
846 s += 2;
847 } else if (*s == '!')
848 rule->rflags |= FILTRULE_CLEAR_LIST; /* Tentative! */
849 } else {
850 char ch = 0;
851 BOOL prefix_specifies_side = False;
852 switch (*s) {
853 case 'c':
854 if ((s = RULE_STRCMP(s, "clear")) != NULL)
855 ch = '!';
856 break;
857 case 'd':
858 if ((s = RULE_STRCMP(s, "dir-merge")) != NULL)
859 ch = ':';
860 break;
861 case 'e':
862 if ((s = RULE_STRCMP(s, "exclude")) != NULL)
863 ch = '-';
864 break;
865 case 'h':
866 if ((s = RULE_STRCMP(s, "hide")) != NULL)
867 ch = 'H';
868 break;
869 case 'i':
870 if ((s = RULE_STRCMP(s, "include")) != NULL)
871 ch = '+';
872 break;
873 case 'm':
874 if ((s = RULE_STRCMP(s, "merge")) != NULL)
875 ch = '.';
876 break;
877 case 'p':
878 if ((s = RULE_STRCMP(s, "protect")) != NULL)
879 ch = 'P';
880 break;
881 case 'r':
882 if ((s = RULE_STRCMP(s, "risk")) != NULL)
883 ch = 'R';
884 break;
885 case 's':
886 if ((s = RULE_STRCMP(s, "show")) != NULL)
887 ch = 'S';
888 break;
889 default:
890 ch = *s;
891 if (s[1] == ',')
892 s++;
893 break;
895 switch (ch) {
896 case ':':
897 rule->rflags |= FILTRULE_PERDIR_MERGE
898 | FILTRULE_FINISH_SETUP;
899 /* FALL THROUGH */
900 case '.':
901 rule->rflags |= FILTRULE_MERGE_FILE;
902 break;
903 case '+':
904 rule->rflags |= FILTRULE_INCLUDE;
905 break;
906 case '-':
907 break;
908 case 'S':
909 rule->rflags |= FILTRULE_INCLUDE;
910 /* FALL THROUGH */
911 case 'H':
912 rule->rflags |= FILTRULE_SENDER_SIDE;
913 prefix_specifies_side = True;
914 break;
915 case 'R':
916 rule->rflags |= FILTRULE_INCLUDE;
917 /* FALL THROUGH */
918 case 'P':
919 rule->rflags |= FILTRULE_RECEIVER_SIDE;
920 prefix_specifies_side = True;
921 break;
922 case '!':
923 rule->rflags |= FILTRULE_CLEAR_LIST;
924 break;
925 default:
926 rprintf(FERROR, "Unknown filter rule: `%s'\n", *rulestr_ptr);
927 exit_cleanup(RERR_SYNTAX);
929 while (ch != '!' && *++s && *s != ' ' && *s != '_') {
930 if (template->rflags & FILTRULE_WORD_SPLIT && isspace(*s)) {
931 s--;
932 break;
934 switch (*s) {
935 default:
936 invalid:
937 rprintf(FERROR,
938 "invalid modifier '%c' at position %d in filter rule: %s\n",
939 *s, (int)(s - (const uchar *)*rulestr_ptr), *rulestr_ptr);
940 exit_cleanup(RERR_SYNTAX);
941 case '-':
942 if (!BITS_SETnUNSET(rule->rflags, FILTRULE_MERGE_FILE, FILTRULE_NO_PREFIXES))
943 goto invalid;
944 rule->rflags |= FILTRULE_NO_PREFIXES;
945 break;
946 case '+':
947 if (!BITS_SETnUNSET(rule->rflags, FILTRULE_MERGE_FILE, FILTRULE_NO_PREFIXES))
948 goto invalid;
949 rule->rflags |= FILTRULE_NO_PREFIXES
950 | FILTRULE_INCLUDE;
951 break;
952 case '/':
953 rule->rflags |= FILTRULE_ABS_PATH;
954 break;
955 case '!':
956 /* Negation really goes with the pattern, so it
957 * isn't useful as a merge-file default. */
958 if (rule->rflags & FILTRULE_MERGE_FILE)
959 goto invalid;
960 rule->rflags |= FILTRULE_NEGATE;
961 break;
962 case 'C':
963 if (rule->rflags & FILTRULE_NO_PREFIXES || prefix_specifies_side)
964 goto invalid;
965 rule->rflags |= FILTRULE_NO_PREFIXES
966 | FILTRULE_WORD_SPLIT
967 | FILTRULE_NO_INHERIT
968 | FILTRULE_CVS_IGNORE;
969 break;
970 case 'e':
971 if (!(rule->rflags & FILTRULE_MERGE_FILE))
972 goto invalid;
973 rule->rflags |= FILTRULE_EXCLUDE_SELF;
974 break;
975 case 'n':
976 if (!(rule->rflags & FILTRULE_MERGE_FILE))
977 goto invalid;
978 rule->rflags |= FILTRULE_NO_INHERIT;
979 break;
980 case 'p':
981 rule->rflags |= FILTRULE_PERISHABLE;
982 break;
983 case 'r':
984 if (prefix_specifies_side)
985 goto invalid;
986 rule->rflags |= FILTRULE_RECEIVER_SIDE;
987 break;
988 case 's':
989 if (prefix_specifies_side)
990 goto invalid;
991 rule->rflags |= FILTRULE_SENDER_SIDE;
992 break;
993 case 'w':
994 if (!(rule->rflags & FILTRULE_MERGE_FILE))
995 goto invalid;
996 rule->rflags |= FILTRULE_WORD_SPLIT;
997 break;
998 case 'x':
999 rule->rflags |= FILTRULE_XATTR;
1000 saw_xattr_filter = 1;
1001 break;
1004 if (*s)
1005 s++;
1007 if (template->rflags & FILTRULES_SIDES) {
1008 if (rule->rflags & FILTRULES_SIDES) {
1009 /* The filter and template both specify side(s). This
1010 * is dodgy (and won't work correctly if the template is
1011 * a one-sided per-dir merge rule), so reject it. */
1012 rprintf(FERROR,
1013 "specified-side merge file contains specified-side filter: %s\n",
1014 *rulestr_ptr);
1015 exit_cleanup(RERR_SYNTAX);
1017 rule->rflags |= template->rflags & FILTRULES_SIDES;
1020 if (template->rflags & FILTRULE_WORD_SPLIT) {
1021 const uchar *cp = s;
1022 /* Token ends at whitespace or the end of the string. */
1023 while (!isspace(*cp) && *cp != '\0')
1024 cp++;
1025 len = cp - s;
1026 } else
1027 len = strlen((char*)s);
1029 if (rule->rflags & FILTRULE_CLEAR_LIST) {
1030 if (!(rule->rflags & FILTRULE_NO_PREFIXES)
1031 && !(xflags & XFLG_OLD_PREFIXES) && len) {
1032 rprintf(FERROR,
1033 "'!' rule has trailing characters: %s\n", *rulestr_ptr);
1034 exit_cleanup(RERR_SYNTAX);
1036 if (len > 1)
1037 rule->rflags &= ~FILTRULE_CLEAR_LIST;
1038 } else if (!len && !(rule->rflags & FILTRULE_CVS_IGNORE)) {
1039 rprintf(FERROR, "unexpected end of filter rule: %s\n", *rulestr_ptr);
1040 exit_cleanup(RERR_SYNTAX);
1043 /* --delete-excluded turns an un-modified include/exclude into a sender-side rule. */
1044 if (delete_excluded
1045 && !(rule->rflags & (FILTRULES_SIDES|FILTRULE_MERGE_FILE|FILTRULE_PERDIR_MERGE)))
1046 rule->rflags |= FILTRULE_SENDER_SIDE;
1048 *pat_ptr = (const char *)s;
1049 *pat_len_ptr = len;
1050 *rulestr_ptr = *pat_ptr + len;
1051 return rule;
1054 static char default_cvsignore[] =
1055 /* These default ignored items come from the CVS manual. */
1056 "RCS SCCS CVS CVS.adm RCSLOG cvslog.* tags TAGS"
1057 " .make.state .nse_depinfo *~ #* .#* ,* _$* *$"
1058 " *.old *.bak *.BAK *.orig *.rej .del-*"
1059 " *.a *.olb *.o *.obj *.so *.exe"
1060 " *.Z *.elc *.ln core"
1061 /* The rest we added to suit ourself. */
1062 " .svn/ .git/ .hg/ .bzr/";
1064 static void get_cvs_excludes(uint32 rflags)
1066 static int initialized = 0;
1067 char *p, fname[MAXPATHLEN];
1069 if (initialized)
1070 return;
1071 initialized = 1;
1073 parse_filter_str(&cvs_filter_list, default_cvsignore,
1074 rule_template(rflags | (protocol_version >= 30 ? FILTRULE_PERISHABLE : 0)),
1077 p = module_id >= 0 && lp_use_chroot(module_id) ? "/" : getenv("HOME");
1078 if (p && pathjoin(fname, MAXPATHLEN, p, ".cvsignore") < MAXPATHLEN)
1079 parse_filter_file(&cvs_filter_list, fname, rule_template(rflags), 0);
1081 parse_filter_str(&cvs_filter_list, getenv("CVSIGNORE"), rule_template(rflags), 0);
1084 const filter_rule *rule_template(uint32 rflags)
1086 static filter_rule template; /* zero-initialized */
1087 template.rflags = rflags;
1088 return &template;
1091 void parse_filter_str(filter_rule_list *listp, const char *rulestr,
1092 const filter_rule *template, int xflags)
1094 filter_rule *rule;
1095 const char *pat;
1096 unsigned int pat_len;
1098 if (!rulestr)
1099 return;
1101 while (1) {
1102 uint32 new_rflags;
1104 /* Remember that the returned string is NOT '\0' terminated! */
1105 if (!(rule = parse_rule_tok(&rulestr, template, xflags, &pat, &pat_len)))
1106 break;
1108 if (pat_len >= MAXPATHLEN) {
1109 rprintf(FERROR, "discarding over-long filter: %.*s\n",
1110 (int)pat_len, pat);
1111 free_continue:
1112 free_filter(rule);
1113 continue;
1116 new_rflags = rule->rflags;
1117 if (new_rflags & FILTRULE_CLEAR_LIST) {
1118 if (DEBUG_GTE(FILTER, 2)) {
1119 rprintf(FINFO,
1120 "[%s] clearing filter list%s\n",
1121 who_am_i(), listp->debug_type);
1123 pop_filter_list(listp);
1124 listp->head = NULL;
1125 goto free_continue;
1128 if (new_rflags & FILTRULE_MERGE_FILE) {
1129 if (!pat_len) {
1130 pat = ".cvsignore";
1131 pat_len = 10;
1133 if (new_rflags & FILTRULE_EXCLUDE_SELF) {
1134 const char *name;
1135 filter_rule *excl_self;
1137 if (!(excl_self = new0(filter_rule)))
1138 out_of_memory("parse_filter_str");
1139 /* Find the beginning of the basename and add an exclude for it. */
1140 for (name = pat + pat_len; name > pat && name[-1] != '/'; name--) {}
1141 add_rule(listp, name, (pat + pat_len) - name, excl_self, 0);
1142 rule->rflags &= ~FILTRULE_EXCLUDE_SELF;
1144 if (new_rflags & FILTRULE_PERDIR_MERGE) {
1145 if (parent_dirscan) {
1146 const char *p;
1147 unsigned int len = pat_len;
1148 if ((p = parse_merge_name(pat, &len, module_dirlen)))
1149 add_rule(listp, p, len, rule, 0);
1150 else
1151 free_filter(rule);
1152 continue;
1154 } else {
1155 const char *p;
1156 unsigned int len = pat_len;
1157 if ((p = parse_merge_name(pat, &len, 0)))
1158 parse_filter_file(listp, p, rule, XFLG_FATAL_ERRORS);
1159 free_filter(rule);
1160 continue;
1164 add_rule(listp, pat, pat_len, rule, xflags);
1166 if (new_rflags & FILTRULE_CVS_IGNORE
1167 && !(new_rflags & FILTRULE_MERGE_FILE))
1168 get_cvs_excludes(new_rflags);
1172 void parse_filter_file(filter_rule_list *listp, const char *fname, const filter_rule *template, int xflags)
1174 FILE *fp;
1175 char line[BIGPATHBUFLEN];
1176 char *eob = line + sizeof line - 1;
1177 BOOL word_split = (template->rflags & FILTRULE_WORD_SPLIT) != 0;
1179 if (!fname || !*fname)
1180 return;
1182 if (*fname != '-' || fname[1] || am_server) {
1183 if (daemon_filter_list.head) {
1184 strlcpy(line, fname, sizeof line);
1185 clean_fname(line, CFN_COLLAPSE_DOT_DOT_DIRS);
1186 if (check_filter(&daemon_filter_list, FLOG, line, 0) < 0)
1187 fp = NULL;
1188 else
1189 fp = fopen(line, "rb");
1190 } else
1191 fp = fopen(fname, "rb");
1192 } else
1193 fp = stdin;
1195 if (DEBUG_GTE(FILTER, 2)) {
1196 rprintf(FINFO, "[%s] parse_filter_file(%s,%x,%x)%s\n",
1197 who_am_i(), fname, template->rflags, xflags,
1198 fp ? "" : " [not found]");
1201 if (!fp) {
1202 if (xflags & XFLG_FATAL_ERRORS) {
1203 rsyserr(FERROR, errno,
1204 "failed to open %sclude file %s",
1205 template->rflags & FILTRULE_INCLUDE ? "in" : "ex",
1206 fname);
1207 exit_cleanup(RERR_FILEIO);
1209 return;
1211 dirbuf[dirbuf_len] = '\0';
1213 while (1) {
1214 char *s = line;
1215 int ch, overflow = 0;
1216 while (1) {
1217 if ((ch = getc(fp)) == EOF) {
1218 if (ferror(fp) && errno == EINTR) {
1219 clearerr(fp);
1220 continue;
1222 break;
1224 if (word_split && isspace(ch))
1225 break;
1226 if (eol_nulls? !ch : (ch == '\n' || ch == '\r'))
1227 break;
1228 if (s < eob)
1229 *s++ = ch;
1230 else
1231 overflow = 1;
1233 if (overflow) {
1234 rprintf(FERROR, "discarding over-long filter: %s...\n", line);
1235 s = line;
1237 *s = '\0';
1238 /* Skip an empty token and (when line parsing) comments. */
1239 if (*line && (word_split || (*line != ';' && *line != '#')))
1240 parse_filter_str(listp, line, template, xflags);
1241 if (ch == EOF)
1242 break;
1244 fclose(fp);
1247 /* If the "for_xfer" flag is set, the prefix is made compatible with the
1248 * current protocol_version (if possible) or a NULL is returned (if not
1249 * possible). */
1250 char *get_rule_prefix(filter_rule *rule, const char *pat, int for_xfer,
1251 unsigned int *plen_ptr)
1253 static char buf[MAX_RULE_PREFIX+1];
1254 char *op = buf;
1255 int legal_len = for_xfer && protocol_version < 29 ? 1 : MAX_RULE_PREFIX-1;
1257 if (rule->rflags & FILTRULE_PERDIR_MERGE) {
1258 if (legal_len == 1)
1259 return NULL;
1260 *op++ = ':';
1261 } else if (rule->rflags & FILTRULE_INCLUDE)
1262 *op++ = '+';
1263 else if (legal_len != 1
1264 || ((*pat == '-' || *pat == '+') && pat[1] == ' '))
1265 *op++ = '-';
1266 else
1267 legal_len = 0;
1269 if (rule->rflags & FILTRULE_ABS_PATH)
1270 *op++ = '/';
1271 if (rule->rflags & FILTRULE_NEGATE)
1272 *op++ = '!';
1273 if (rule->rflags & FILTRULE_CVS_IGNORE)
1274 *op++ = 'C';
1275 else {
1276 if (rule->rflags & FILTRULE_NO_INHERIT)
1277 *op++ = 'n';
1278 if (rule->rflags & FILTRULE_WORD_SPLIT)
1279 *op++ = 'w';
1280 if (rule->rflags & FILTRULE_NO_PREFIXES) {
1281 if (rule->rflags & FILTRULE_INCLUDE)
1282 *op++ = '+';
1283 else
1284 *op++ = '-';
1287 if (rule->rflags & FILTRULE_EXCLUDE_SELF)
1288 *op++ = 'e';
1289 if (rule->rflags & FILTRULE_XATTR)
1290 *op++ = 'x';
1291 if (rule->rflags & FILTRULE_SENDER_SIDE
1292 && (!for_xfer || protocol_version >= 29))
1293 *op++ = 's';
1294 if (rule->rflags & FILTRULE_RECEIVER_SIDE
1295 && (!for_xfer || protocol_version >= 29
1296 || (delete_excluded && am_sender)))
1297 *op++ = 'r';
1298 if (rule->rflags & FILTRULE_PERISHABLE) {
1299 if (!for_xfer || protocol_version >= 30)
1300 *op++ = 'p';
1301 else if (am_sender)
1302 return NULL;
1304 if (op - buf > legal_len)
1305 return NULL;
1306 if (legal_len)
1307 *op++ = ' ';
1308 *op = '\0';
1309 if (plen_ptr)
1310 *plen_ptr = op - buf;
1311 return buf;
1314 static void send_rules(int f_out, filter_rule_list *flp)
1316 filter_rule *ent, *prev = NULL;
1318 for (ent = flp->head; ent; ent = ent->next) {
1319 unsigned int len, plen, dlen;
1320 int elide = 0;
1321 char *p;
1323 /* Note we need to check delete_excluded here in addition to
1324 * the code in parse_rule_tok() because some rules may have
1325 * been added before we found the --delete-excluded option.
1326 * We must also elide any CVS merge-file rules to avoid a
1327 * backward compatibility problem, and we elide any no-prefix
1328 * merge files as an optimization (since they can only have
1329 * include/exclude rules). */
1330 if (ent->rflags & FILTRULE_SENDER_SIDE)
1331 elide = am_sender ? 1 : -1;
1332 if (ent->rflags & FILTRULE_RECEIVER_SIDE)
1333 elide = elide ? 0 : am_sender ? -1 : 1;
1334 else if (delete_excluded && !elide
1335 && (!(ent->rflags & FILTRULE_PERDIR_MERGE)
1336 || ent->rflags & FILTRULE_NO_PREFIXES))
1337 elide = am_sender ? 1 : -1;
1338 if (elide < 0) {
1339 if (prev)
1340 prev->next = ent->next;
1341 else
1342 flp->head = ent->next;
1343 } else
1344 prev = ent;
1345 if (elide > 0)
1346 continue;
1347 if (ent->rflags & FILTRULE_CVS_IGNORE
1348 && !(ent->rflags & FILTRULE_MERGE_FILE)) {
1349 int f = am_sender || protocol_version < 29 ? f_out : -2;
1350 send_rules(f, &cvs_filter_list);
1351 if (f == f_out)
1352 continue;
1354 p = get_rule_prefix(ent, ent->pattern, 1, &plen);
1355 if (!p) {
1356 rprintf(FERROR,
1357 "filter rules are too modern for remote rsync.\n");
1358 exit_cleanup(RERR_PROTOCOL);
1360 if (f_out < 0)
1361 continue;
1362 len = strlen(ent->pattern);
1363 dlen = ent->rflags & FILTRULE_DIRECTORY ? 1 : 0;
1364 if (!(plen + len + dlen))
1365 continue;
1366 write_int(f_out, plen + len + dlen);
1367 if (plen)
1368 write_buf(f_out, p, plen);
1369 write_buf(f_out, ent->pattern, len);
1370 if (dlen)
1371 write_byte(f_out, '/');
1373 flp->tail = prev;
1376 /* This is only called by the client. */
1377 void send_filter_list(int f_out)
1379 int receiver_wants_list = prune_empty_dirs
1380 || (delete_mode && (!delete_excluded || protocol_version >= 29));
1382 if (local_server || (am_sender && !receiver_wants_list))
1383 f_out = -1;
1384 if (cvs_exclude && am_sender) {
1385 if (protocol_version >= 29)
1386 parse_filter_str(&filter_list, ":C", rule_template(0), 0);
1387 parse_filter_str(&filter_list, "-C", rule_template(0), 0);
1390 send_rules(f_out, &filter_list);
1392 if (f_out >= 0)
1393 write_int(f_out, 0);
1395 if (cvs_exclude) {
1396 if (!am_sender || protocol_version < 29)
1397 parse_filter_str(&filter_list, ":C", rule_template(0), 0);
1398 if (!am_sender)
1399 parse_filter_str(&filter_list, "-C", rule_template(0), 0);
1403 /* This is only called by the server. */
1404 void recv_filter_list(int f_in)
1406 char line[BIGPATHBUFLEN];
1407 int xflags = protocol_version >= 29 ? 0 : XFLG_OLD_PREFIXES;
1408 int receiver_wants_list = prune_empty_dirs
1409 || (delete_mode
1410 && (!delete_excluded || protocol_version >= 29));
1411 unsigned int len;
1413 if (!local_server && (am_sender || receiver_wants_list)) {
1414 while ((len = read_int(f_in)) != 0) {
1415 if (len >= sizeof line)
1416 overflow_exit("recv_rules");
1417 read_sbuf(f_in, line, len);
1418 parse_filter_str(&filter_list, line, rule_template(0), xflags);
1422 if (cvs_exclude) {
1423 if (local_server || am_sender || protocol_version < 29)
1424 parse_filter_str(&filter_list, ":C", rule_template(0), 0);
1425 if (local_server || am_sender)
1426 parse_filter_str(&filter_list, "-C", rule_template(0), 0);
1429 if (local_server) /* filter out any rules that aren't for us. */
1430 send_rules(-1, &filter_list);