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-2022 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.
28 extern int am_generator;
33 extern int local_server;
34 extern int prune_empty_dirs;
35 extern int ignore_perishable;
36 extern int relative_paths;
37 extern int delete_mode;
38 extern int delete_excluded;
39 extern int cvs_exclude;
40 extern int sanitize_paths;
41 extern int protocol_version;
42 extern int trust_sender_args;
45 extern char curr_dir[MAXPATHLEN];
46 extern unsigned int curr_dir_len;
47 extern unsigned int module_dirlen;
49 filter_rule_list filter_list = { .debug_type = "" };
50 filter_rule_list cvs_filter_list = { .debug_type = " [global CVS]" };
51 filter_rule_list daemon_filter_list = { .debug_type = " [daemon]" };
52 filter_rule_list implied_filter_list = { .debug_type = " [implied]" };
54 int saw_xattr_filter = 0;
55 int trust_sender_args = 0;
56 int trust_sender_filter = 0;
58 /* Need room enough for ":MODS " prefix plus some room to grow. */
59 #define MAX_RULE_PREFIX (16)
61 #define SLASH_WILD3_SUFFIX "/***"
63 /* The dirbuf is set by push_local_filters() to the current subdirectory
64 * relative to curr_dir that is being processed. The path always has a
65 * trailing slash appended, and the variable dirbuf_len contains the length
66 * of this path prefix. The path is always absolute. */
67 static char dirbuf[MAXPATHLEN+1];
68 static unsigned int dirbuf_len = 0;
69 static int dirbuf_depth;
71 /* This is True when we're scanning parent dirs for per-dir merge-files. */
72 static BOOL parent_dirscan = False;
74 /* This array contains a list of all the currently active per-dir merge
75 * files. This makes it easier to save the appropriate values when we
76 * "push" down into each subdirectory. */
77 static filter_rule **mergelist_parents;
78 static int mergelist_cnt = 0;
79 static int mergelist_size = 0;
83 static uchar cur_elide_value = REMOTE_RULE;
85 /* Each filter_list_struct describes a singly-linked list by keeping track
86 * of both the head and tail pointers. The list is slightly unusual in that
87 * a parent-dir's content can be appended to the end of the local list in a
88 * special way: the last item in the local list has its "next" pointer set
89 * to point to the inherited list, but the local list's tail pointer points
90 * at the end of the local list. Thus, if the local list is empty, the head
91 * will be pointing at the inherited content but the tail will be NULL. To
92 * help you visualize this, here are the possible list arrangements:
94 * Completely Empty Local Content Only
95 * ================================== ====================================
96 * head -> NULL head -> Local1 -> Local2 -> NULL
97 * tail -> NULL tail -------------^
99 * Inherited Content Only Both Local and Inherited Content
100 * ================================== ====================================
101 * head -> Parent1 -> Parent2 -> NULL head -> L1 -> L2 -> P1 -> P2 -> NULL
102 * tail -> NULL tail ---------^
104 * This means that anyone wanting to traverse the whole list to use it just
105 * needs to start at the head and use the "next" pointers until it goes
106 * NULL. To add new local content, we insert the item after the tail item
107 * and update the tail (obviously, if "tail" was NULL, we insert it at the
108 * head). To clear the local list, WE MUST NOT FREE THE INHERITED CONTENT
109 * because it is shared between the current list and our parent list(s).
110 * The easiest way to handle this is to simply truncate the list after the
111 * tail item and then free the local list from the head. When inheriting
112 * the list for a new local dir, we just save off the filter_list_struct
113 * values (so we can pop back to them later) and set the tail to NULL.
116 static void teardown_mergelist(filter_rule *ex)
120 if (!ex->u.mergelist)
123 if (DEBUG_GTE(FILTER, 2)) {
124 rprintf(FINFO, "[%s] deactivating mergelist #%d%s\n",
125 who_am_i(), mergelist_cnt - 1,
126 ex->u.mergelist->debug_type);
129 free(ex->u.mergelist->debug_type);
130 free(ex->u.mergelist);
132 for (j = 0; j < mergelist_cnt; j++) {
133 if (mergelist_parents[j] == ex) {
134 mergelist_parents[j] = NULL;
138 while (mergelist_cnt && mergelist_parents[mergelist_cnt-1] == NULL)
142 static void free_filter(filter_rule *ex)
144 if (ex->rflags & FILTRULE_PERDIR_MERGE)
145 teardown_mergelist(ex);
150 static void free_filters(filter_rule *ent)
153 filter_rule *next = ent->next;
159 /* Build a filter structure given a filter pattern. The value in "pat"
160 * is not null-terminated. "rule" is either held or freed, so the
161 * caller should not free it. */
162 static void add_rule(filter_rule_list *listp, const char *pat, unsigned int pat_len,
163 filter_rule *rule, int xflags)
166 unsigned int pre_len, suf_len, slash_cnt = 0;
167 char *mention_rule_suffix;
169 if (DEBUG_GTE(FILTER, 1) && pat_len && (pat[pat_len-1] == ' ' || pat[pat_len-1] == '\t'))
170 mention_rule_suffix = " -- CAUTION: trailing whitespace!";
172 mention_rule_suffix = DEBUG_GTE(FILTER, 2) ? "" : NULL;
173 if (mention_rule_suffix) {
174 rprintf(FINFO, "[%s] add_rule(%s%.*s%s)%s%s\n",
175 who_am_i(), get_rule_prefix(rule, pat, 0, NULL),
176 (int)pat_len, pat, (rule->rflags & FILTRULE_DIRECTORY) ? "/" : "",
177 listp->debug_type, mention_rule_suffix);
180 /* These flags also indicate that we're reading a list that
181 * needs to be filtered now, not post-filtered later. */
182 if (xflags & (XFLG_ANCHORED2ABS|XFLG_ABS_IF_SLASH)
183 && (rule->rflags & FILTRULES_SIDES)
184 == (am_sender ? FILTRULE_RECEIVER_SIDE : FILTRULE_SENDER_SIDE)) {
185 /* This filter applies only to the other side. Drop it. */
190 if (pat_len > 1 && pat[pat_len-1] == '/') {
192 rule->rflags |= FILTRULE_DIRECTORY;
195 for (cp = pat; cp < pat + pat_len; cp++) {
200 if (!(rule->rflags & (FILTRULE_ABS_PATH | FILTRULE_MERGE_FILE))
201 && ((xflags & (XFLG_ANCHORED2ABS|XFLG_ABS_IF_SLASH) && *pat == '/')
202 || (xflags & XFLG_ABS_IF_SLASH && slash_cnt))) {
203 rule->rflags |= FILTRULE_ABS_PATH;
205 pre_len = dirbuf_len - module_dirlen - 1;
211 /* The daemon wants dir-exclude rules to get an appended "/" + "***". */
212 if (xflags & XFLG_DIR2WILD3
213 && BITS_SETnUNSET(rule->rflags, FILTRULE_DIRECTORY, FILTRULE_INCLUDE)) {
214 rule->rflags &= ~FILTRULE_DIRECTORY;
215 suf_len = sizeof SLASH_WILD3_SUFFIX - 1;
219 rule->pattern = new_array(char, pre_len + pat_len + suf_len + 1);
221 memcpy(rule->pattern, dirbuf + module_dirlen, pre_len);
222 for (cp = rule->pattern; cp < rule->pattern + pre_len; cp++) {
228 strlcpy(rule->pattern + pre_len, pat, pat_len + 1);
231 memcpy(rule->pattern + pat_len, SLASH_WILD3_SUFFIX, suf_len+1);
236 if (strpbrk(rule->pattern, "*[?")) {
237 rule->rflags |= FILTRULE_WILD;
238 if ((cp = strstr(rule->pattern, "**")) != NULL) {
239 rule->rflags |= FILTRULE_WILD2;
240 /* If the pattern starts with **, note that. */
241 if (cp == rule->pattern)
242 rule->rflags |= FILTRULE_WILD2_PREFIX;
243 /* If the pattern ends with ***, note that. */
245 && rule->pattern[pat_len-3] == '*'
246 && rule->pattern[pat_len-2] == '*'
247 && rule->pattern[pat_len-1] == '*')
248 rule->rflags |= FILTRULE_WILD3_SUFFIX;
252 if (rule->rflags & FILTRULE_PERDIR_MERGE) {
253 filter_rule_list *lp;
257 if ((cp = strrchr(rule->pattern, '/')) != NULL)
262 /* If the local merge file was already mentioned, don't
264 for (i = 0; i < mergelist_cnt; i++) {
265 filter_rule *ex = mergelist_parents[i];
269 s = strrchr(ex->pattern, '/');
275 if (len == pat_len - (cp - rule->pattern) && memcmp(s, cp, len) == 0) {
281 lp = new_array0(filter_rule_list, 1);
282 if (asprintf(&lp->debug_type, " [per-dir %s]", cp) < 0)
283 out_of_memory("add_rule");
284 rule->u.mergelist = lp;
286 if (mergelist_cnt == mergelist_size) {
288 mergelist_parents = realloc_array(mergelist_parents, filter_rule *, mergelist_size);
290 if (DEBUG_GTE(FILTER, 2)) {
291 rprintf(FINFO, "[%s] activating mergelist #%d%s\n",
292 who_am_i(), mergelist_cnt, lp->debug_type);
294 mergelist_parents[mergelist_cnt++] = rule;
296 rule->u.slash_cnt = slash_cnt;
299 rule->next = listp->head;
300 listp->head = listp->tail = rule;
302 rule->next = listp->tail->next;
303 listp->tail->next = rule;
308 /* If the wildcards failed, the remote shell might give us a file matching the literal
309 * wildcards. Since "*" & "?" already match themselves, this just needs to deal with
310 * failed "[foo]" idioms.
312 static void maybe_add_literal_brackets_rule(filter_rule const *based_on, int arg_len)
315 const char *arg = based_on->pattern, *cp;
320 arg_len = strlen(arg);
322 for (cp = arg; *cp; cp++) {
323 if (*cp == '\\' && cp[1]) {
325 } else if (*cp == '[')
331 rule = new0(filter_rule);
332 rule->rflags = based_on->rflags;
333 rule->u.slash_cnt = based_on->u.slash_cnt;
334 p = rule->pattern = new_array(char, arg_len + cnt + 1);
335 for (cp = arg; *cp; ) {
336 if (*cp == '\\' && cp[1]) {
338 } else if (*cp == '[')
344 rule->next = implied_filter_list.head;
345 implied_filter_list.head = rule;
346 if (DEBUG_GTE(FILTER, 3)) {
347 rprintf(FINFO, "[%s] add_implied_include(%s%s)\n", who_am_i(), rule->pattern,
348 rule->rflags & FILTRULE_DIRECTORY ? "/" : "");
352 static char *partial_string_buf = NULL;
353 static int partial_string_len = 0;
354 void implied_include_partial_string(const char *s_start, const char *s_end)
356 partial_string_len = s_end - s_start;
357 if (partial_string_len <= 0 || partial_string_len >= MAXPATHLEN) { /* too-large should be impossible... */
358 partial_string_len = 0;
361 if (!partial_string_buf)
362 partial_string_buf = new_array(char, MAXPATHLEN);
363 memcpy(partial_string_buf, s_start, partial_string_len);
366 void free_implied_include_partial_string()
368 if (partial_string_buf) {
369 if (partial_string_len)
370 add_implied_include("", 0);
371 free(partial_string_buf);
372 partial_string_buf = NULL;
374 partial_string_len = 0; /* paranoia */
377 /* Each arg the client sends to the remote sender turns into an implied include
378 * that the receiver uses to validate the file list from the sender. */
379 void add_implied_include(const char *arg, int skip_daemon_module)
381 int arg_len, saw_wild = 0, saw_live_open_brkt = 0, backslash_cnt = 0;
385 if (trust_sender_args)
387 if (partial_string_len) {
388 arg_len = strlen(arg);
389 if (partial_string_len + arg_len >= MAXPATHLEN) {
390 partial_string_len = 0;
391 return; /* Should be impossible... */
393 memcpy(partial_string_buf + partial_string_len, arg, arg_len + 1);
394 partial_string_len = 0;
395 arg = partial_string_buf;
397 if (skip_daemon_module) {
398 if ((cp = strchr(arg, '/')) != NULL)
403 if (relative_paths) {
404 if ((cp = strstr(arg, "/./")) != NULL)
406 } else if ((cp = strrchr(arg, '/')) != NULL) {
409 if (*arg == '.' && arg[1] == '\0')
411 arg_len = strlen(arg);
414 if (strpbrk(arg, "*[?")) {
415 /* We need to add room to escape backslashes if wildcard chars are present. */
416 for (cp = arg; (cp = strchr(cp, '\\')) != NULL; cp++)
420 arg_len++; /* Leave room for the prefixed slash */
421 p = new_pat = new_array(char, arg_len + 1);
424 for (cp = arg; *cp; ) {
429 cp++; /* A \] in a non-wild filter causes a problem, so drop the \ . */
430 } else if (!strchr("*[?", cp[1])) {
438 if (p[-1] == '/') { /* This is safe because of the initial slash. */
443 } else if (cp[1] == '\0') {
458 } else if (cp[1] == '\0') {
468 saw_live_open_brkt = 1;
477 arg_len = p - new_pat;
481 filter_rule *rule = new0(filter_rule);
482 rule->rflags = FILTRULE_INCLUDE + (saw_wild ? FILTRULE_WILD : 0);
483 rule->u.slash_cnt = slash_cnt;
484 arg = rule->pattern = new_pat;
485 if (!implied_filter_list.head)
486 implied_filter_list.head = implied_filter_list.tail = rule;
488 rule->next = implied_filter_list.head;
489 implied_filter_list.head = rule;
491 if (DEBUG_GTE(FILTER, 3))
492 rprintf(FINFO, "[%s] add_implied_include(%s)\n", who_am_i(), arg);
493 if (saw_live_open_brkt)
494 maybe_add_literal_brackets_rule(rule, arg_len);
495 if (relative_paths && slash_cnt) {
496 int sub_slash_cnt = slash_cnt;
497 while ((p = strrchr(new_pat, '/')) != NULL && p != new_pat) {
498 filter_rule const *ent;
502 for (ent = implied_filter_list.head; ent; ent = ent->next) {
503 if (ent != rule && strcmp(ent->pattern, new_pat) == 0) {
510 break; /* We added all parent dirs already */
512 R_rule = new0(filter_rule);
513 R_rule->rflags = FILTRULE_INCLUDE | FILTRULE_DIRECTORY;
514 /* Check if our sub-path has wildcards or escaped backslashes */
515 if (saw_wild && strpbrk(new_pat, "*[?\\"))
516 R_rule->rflags |= FILTRULE_WILD;
517 R_rule->pattern = strdup(new_pat);
518 R_rule->u.slash_cnt = --sub_slash_cnt;
519 R_rule->next = implied_filter_list.head;
520 implied_filter_list.head = R_rule;
521 if (DEBUG_GTE(FILTER, 3)) {
522 rprintf(FINFO, "[%s] add_implied_include(%s/)\n",
523 who_am_i(), R_rule->pattern);
525 if (saw_live_open_brkt)
526 maybe_add_literal_brackets_rule(R_rule, -1);
528 for (p = new_pat; sub_slash_cnt < slash_cnt; sub_slash_cnt++) {
536 if (recurse || xfer_dirs) {
537 /* Now create a rule with an added "/" & "**" or "*" at the end */
538 filter_rule *rule = new0(filter_rule);
539 rule->rflags = FILTRULE_INCLUDE | FILTRULE_WILD;
541 rule->rflags |= FILTRULE_WILD2;
542 /* We must leave enough room for / * * \0. */
543 if (!saw_wild && backslash_cnt) {
544 /* We are appending a wildcard, so now the backslashes need to be escaped. */
545 p = rule->pattern = new_array(char, arg_len + backslash_cnt + 3 + 1);
546 for (cp = arg; *cp; ) { /* Note that arg_len != 0 because backslash_cnt > 0 */
552 p = rule->pattern = new_array(char, arg_len + 3 + 1);
554 memcpy(p, arg, arg_len);
566 rule->u.slash_cnt = slash_cnt;
567 rule->next = implied_filter_list.head;
568 implied_filter_list.head = rule;
569 if (DEBUG_GTE(FILTER, 3))
570 rprintf(FINFO, "[%s] add_implied_include(%s)\n", who_am_i(), rule->pattern);
571 if (saw_live_open_brkt)
572 maybe_add_literal_brackets_rule(rule, p - rule->pattern);
576 /* This frees any non-inherited items, leaving just inherited items on the list. */
577 static void pop_filter_list(filter_rule_list *listp)
579 filter_rule *inherited;
584 inherited = listp->tail->next;
586 /* Truncate any inherited items from the local list. */
587 listp->tail->next = NULL;
588 /* Now free everything that is left. */
589 free_filters(listp->head);
591 listp->head = inherited;
595 /* This returns an expanded (absolute) filename for the merge-file name if
596 * the name has any slashes in it OR if the parent_dirscan var is True;
597 * otherwise it returns the original merge_file name. If the len_ptr value
598 * is non-NULL the merge_file name is limited by the referenced length
599 * value and will be updated with the length of the resulting name. We
600 * always return a name that is null terminated, even if the merge_file
602 static char *parse_merge_name(const char *merge_file, unsigned int *len_ptr,
603 unsigned int prefix_skip)
605 static char buf[MAXPATHLEN];
606 char *fn, tmpbuf[MAXPATHLEN];
609 if (!parent_dirscan && *merge_file != '/') {
610 /* Return the name unchanged it doesn't have any slashes. */
612 const char *p = merge_file + *len_ptr;
613 while (--p > merge_file && *p != '/') {}
614 if (p == merge_file) {
615 strlcpy(buf, merge_file, *len_ptr + 1);
618 } else if (strchr(merge_file, '/') == NULL)
619 return (char *)merge_file;
622 fn = *merge_file == '/' ? buf : tmpbuf;
623 if (sanitize_paths) {
624 const char *r = prefix_skip ? "/" : NULL;
625 /* null-terminate the name if it isn't already */
626 if (len_ptr && merge_file[*len_ptr]) {
627 char *to = fn == buf ? tmpbuf : buf;
628 strlcpy(to, merge_file, *len_ptr + 1);
631 if (!sanitize_path(fn, merge_file, r, dirbuf_depth, SP_DEFAULT)) {
632 rprintf(FERROR, "merge-file name overflows: %s\n",
638 strlcpy(fn, merge_file, len_ptr ? *len_ptr + 1 : MAXPATHLEN);
639 fn_len = clean_fname(fn, CFN_COLLAPSE_DOT_DOT_DIRS);
642 /* If the name isn't in buf yet, it wasn't absolute. */
644 int d_len = dirbuf_len - prefix_skip;
645 if (d_len + fn_len >= MAXPATHLEN) {
646 rprintf(FERROR, "merge-file name overflows: %s\n", fn);
649 memcpy(buf, dirbuf + prefix_skip, d_len);
650 memcpy(buf + d_len, fn, fn_len + 1);
651 fn_len = clean_fname(buf, CFN_COLLAPSE_DOT_DOT_DIRS);
659 /* Sets the dirbuf and dirbuf_len values. */
660 void set_filter_dir(const char *dir, unsigned int dirlen)
664 memcpy(dirbuf, curr_dir, curr_dir_len);
665 dirbuf[curr_dir_len] = '/';
666 len = curr_dir_len + 1;
667 if (len + dirlen >= MAXPATHLEN)
671 memcpy(dirbuf + len, dir, dirlen);
672 dirbuf[dirlen + len] = '\0';
673 dirbuf_len = clean_fname(dirbuf, CFN_COLLAPSE_DOT_DOT_DIRS);
674 if (dirbuf_len > 1 && dirbuf[dirbuf_len-1] == '.'
675 && dirbuf[dirbuf_len-2] == '/')
678 dirbuf[dirbuf_len++] = '/';
679 dirbuf[dirbuf_len] = '\0';
681 dirbuf_depth = count_dir_elements(dirbuf + module_dirlen);
684 /* This routine takes a per-dir merge-file entry and finishes its setup.
685 * If the name has a path portion then we check to see if it refers to a
686 * parent directory of the first transfer dir. If it does, we scan all the
687 * dirs from that point through the parent dir of the transfer dir looking
688 * for the per-dir merge-file in each one. */
689 static BOOL setup_merge_file(int mergelist_num, filter_rule *ex,
690 filter_rule_list *lp)
692 char buf[MAXPATHLEN];
693 char *x, *y, *pat = ex->pattern;
696 if (!(x = parse_merge_name(pat, NULL, 0)) || *x != '/')
699 if (DEBUG_GTE(FILTER, 2)) {
700 rprintf(FINFO, "[%s] performing parent_dirscan for mergelist #%d%s\n",
701 who_am_i(), mergelist_num, lp->debug_type);
705 ex->pattern = strdup(y+1);
709 strlcpy(buf, x, MAXPATHLEN);
711 pathjoin(buf, MAXPATHLEN, dirbuf, x);
713 len = clean_fname(buf, CFN_COLLAPSE_DOT_DOT_DIRS);
714 if (len != 1 && len < MAXPATHLEN-1) {
718 /* This ensures that the specified dir is a parent of the transfer. */
719 for (x = buf, y = dirbuf; *x && *x == *y; x++, y++) {}
721 y += strlen(y); /* nope -- skip the scan */
723 parent_dirscan = True;
725 char save[MAXPATHLEN];
726 strlcpy(save, y, MAXPATHLEN);
728 dirbuf_len = y - dirbuf;
729 strlcpy(x, ex->pattern, MAXPATHLEN - (x - buf));
730 parse_filter_file(lp, buf, ex, XFLG_ANCHORED2ABS);
731 if (ex->rflags & FILTRULE_NO_INHERIT) {
732 /* Free the undesired rules to clean up any per-dir
733 * mergelists they defined. Otherwise pop_local_filters
734 * may crash trying to restore nonexistent state for
735 * those mergelists. */
736 free_filters(lp->head);
740 strlcpy(y, save, MAXPATHLEN);
741 while ((*x++ = *y++) != '/') {}
743 parent_dirscan = False;
744 if (DEBUG_GTE(FILTER, 2)) {
745 rprintf(FINFO, "[%s] completed parent_dirscan for mergelist #%d%s\n",
746 who_am_i(), mergelist_num, lp->debug_type);
752 struct local_filter_state {
754 filter_rule_list mergelists[1];
757 /* Each time rsync changes to a new directory it call this function to
758 * handle all the per-dir merge-files. The "dir" value is the current path
759 * relative to curr_dir (which might not be null-terminated). We copy it
760 * into dirbuf so that we can easily append a file name on the end. */
761 void *push_local_filters(const char *dir, unsigned int dirlen)
763 struct local_filter_state *push;
766 set_filter_dir(dir, dirlen);
767 if (DEBUG_GTE(FILTER, 2)) {
768 rprintf(FINFO, "[%s] pushing local filters for %s\n",
772 if (!mergelist_cnt) {
773 /* No old state to save and no new merge files to push. */
777 push = (struct local_filter_state *)new_array(char,
778 sizeof (struct local_filter_state)
779 + (mergelist_cnt-1) * sizeof (filter_rule_list));
781 push->mergelist_cnt = mergelist_cnt;
782 for (i = 0; i < mergelist_cnt; i++) {
783 filter_rule *ex = mergelist_parents[i];
786 memcpy(&push->mergelists[i], ex->u.mergelist, sizeof (filter_rule_list));
789 /* Note: parse_filter_file() might increase mergelist_cnt, so keep
790 * this loop separate from the above loop. */
791 for (i = 0; i < mergelist_cnt; i++) {
792 filter_rule *ex = mergelist_parents[i];
793 filter_rule_list *lp;
796 lp = ex->u.mergelist;
798 if (DEBUG_GTE(FILTER, 2)) {
799 rprintf(FINFO, "[%s] pushing mergelist #%d%s\n",
800 who_am_i(), i, lp->debug_type);
803 lp->tail = NULL; /* Switch any local rules to inherited. */
804 if (ex->rflags & FILTRULE_NO_INHERIT)
807 if (ex->rflags & FILTRULE_FINISH_SETUP) {
808 ex->rflags &= ~FILTRULE_FINISH_SETUP;
809 if (setup_merge_file(i, ex, lp))
810 set_filter_dir(dir, dirlen);
813 if (strlcpy(dirbuf + dirbuf_len, ex->pattern,
814 MAXPATHLEN - dirbuf_len) < MAXPATHLEN - dirbuf_len) {
815 parse_filter_file(lp, dirbuf, ex,
818 io_error |= IOERR_GENERAL;
820 "cannot add local filter rules in long-named directory: %s\n",
823 dirbuf[dirbuf_len] = '\0';
829 void pop_local_filters(void *mem)
831 struct local_filter_state *pop = (struct local_filter_state *)mem;
833 int old_mergelist_cnt = pop ? pop->mergelist_cnt : 0;
835 if (DEBUG_GTE(FILTER, 2))
836 rprintf(FINFO, "[%s] popping local filters\n", who_am_i());
838 for (i = mergelist_cnt; i-- > 0; ) {
839 filter_rule *ex = mergelist_parents[i];
840 filter_rule_list *lp;
843 lp = ex->u.mergelist;
845 if (DEBUG_GTE(FILTER, 2)) {
846 rprintf(FINFO, "[%s] popping mergelist #%d%s\n",
847 who_am_i(), i, lp->debug_type);
851 if (i >= old_mergelist_cnt && lp->head) {
852 /* This mergelist does not exist in the state to be restored, but it
853 * still has inherited rules. This can sometimes happen if a per-dir
854 * merge file calls setup_merge_file() in push_local_filters() and that
855 * leaves some inherited rules that aren't in the pushed list state. */
856 if (DEBUG_GTE(FILTER, 2)) {
857 rprintf(FINFO, "[%s] freeing parent_dirscan filters of mergelist #%d%s\n",
858 who_am_i(), i, ex->u.mergelist->debug_type);
865 return; /* No state to restore. */
867 for (i = 0; i < old_mergelist_cnt; i++) {
868 filter_rule *ex = mergelist_parents[i];
871 memcpy(ex->u.mergelist, &pop->mergelists[i], sizeof (filter_rule_list));
877 void change_local_filter_dir(const char *dname, int dlen, int dir_depth)
879 static int cur_depth = -1;
880 static void *filt_array[MAXPATHLEN/2+1];
883 for ( ; cur_depth >= 0; cur_depth--) {
884 if (filt_array[cur_depth]) {
885 pop_local_filters(filt_array[cur_depth]);
886 filt_array[cur_depth] = NULL;
892 assert(dir_depth < MAXPATHLEN/2+1);
894 for ( ; cur_depth >= dir_depth; cur_depth--) {
895 if (filt_array[cur_depth]) {
896 pop_local_filters(filt_array[cur_depth]);
897 filt_array[cur_depth] = NULL;
901 cur_depth = dir_depth;
902 filt_array[cur_depth] = push_local_filters(dname, dlen);
905 static int rule_matches(const char *fname, filter_rule *ex, int name_flags)
907 int slash_handling, str_cnt = 0, anchored_match = 0;
908 int ret_match = ex->rflags & FILTRULE_NEGATE ? 0 : 1;
909 char *p, *pattern = ex->pattern;
910 const char *strings[16]; /* more than enough */
911 const char *name = fname + (*fname == '/');
913 if (!*name || ex->elide == cur_elide_value)
916 if (!(name_flags & NAME_IS_XATTR) ^ !(ex->rflags & FILTRULE_XATTR))
919 if (!ex->u.slash_cnt && !(ex->rflags & FILTRULE_WILD2)) {
920 /* If the pattern does not have any slashes AND it does
921 * not have a "**" (which could match a slash), then we
922 * just match the name portion of the path. */
923 if ((p = strrchr(name,'/')) != NULL)
925 } else if (ex->rflags & FILTRULE_ABS_PATH && *fname != '/'
926 && curr_dir_len > module_dirlen + 1) {
927 /* If we're matching against an absolute-path pattern,
928 * we need to prepend our full path info. */
929 strings[str_cnt++] = curr_dir + module_dirlen + 1;
930 strings[str_cnt++] = "/";
931 } else if (ex->rflags & FILTRULE_WILD2_PREFIX && *fname != '/') {
932 /* Allow "**"+"/" to match at the start of the string. */
933 strings[str_cnt++] = "/";
935 strings[str_cnt++] = name;
936 if (name_flags & NAME_IS_DIR) {
937 /* Allow a trailing "/"+"***" to match the directory. */
938 if (ex->rflags & FILTRULE_WILD3_SUFFIX)
939 strings[str_cnt++] = "/";
940 } else if (ex->rflags & FILTRULE_DIRECTORY)
942 strings[str_cnt] = NULL;
944 if (*pattern == '/') {
949 if (!anchored_match && ex->u.slash_cnt
950 && !(ex->rflags & FILTRULE_WILD2)) {
951 /* A non-anchored match with an infix slash and no "**"
952 * needs to match the last slash_cnt+1 name elements. */
953 slash_handling = ex->u.slash_cnt + 1;
954 } else if (!anchored_match && !(ex->rflags & FILTRULE_WILD2_PREFIX)
955 && ex->rflags & FILTRULE_WILD2) {
956 /* A non-anchored match with an infix or trailing "**" (but not
957 * a prefixed "**") needs to try matching after every slash. */
960 /* The pattern matches only at the start of the path or name. */
964 if (ex->rflags & FILTRULE_WILD) {
965 if (wildmatch_array(pattern, strings, slash_handling))
967 } else if (str_cnt > 1) {
968 if (litmatch_array(pattern, strings, slash_handling))
970 } else if (anchored_match) {
971 if (strcmp(name, pattern) == 0)
974 int l1 = strlen(name);
975 int l2 = strlen(pattern);
977 strcmp(name+(l1-l2),pattern) == 0 &&
978 (l1==l2 || name[l1-(l2+1)] == '/')) {
986 static void report_filter_result(enum logcode code, char const *name,
987 filter_rule const *ent,
988 int name_flags, const char *type)
990 int log_level = am_sender || am_generator ? 1 : 3;
992 /* If a trailing slash is present to match only directories,
993 * then it is stripped out by add_rule(). So as a special
994 * case we add it back in the log output. */
995 if (DEBUG_GTE(FILTER, log_level)) {
996 static char *actions[2][2]
997 = { {"show", "hid"}, {"risk", "protect"} };
998 const char *w = who_am_i();
999 const char *t = name_flags & NAME_IS_XATTR ? "xattr"
1000 : name_flags & NAME_IS_DIR ? "directory"
1002 rprintf(code, "[%s] %sing %s %s because of pattern %s%s%s\n",
1003 w, actions[*w=='g'][!(ent->rflags & FILTRULE_INCLUDE)],
1004 t, name, ent->pattern,
1005 ent->rflags & FILTRULE_DIRECTORY ? "/" : "", type);
1009 /* This function is used to check if a file should be included/excluded
1010 * from the list of files based on its name and type etc. The value of
1011 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
1012 int name_is_excluded(const char *fname, int name_flags, int filter_level)
1014 if (daemon_filter_list.head && check_filter(&daemon_filter_list, FLOG, fname, name_flags) < 0) {
1015 if (!(name_flags & NAME_IS_XATTR))
1020 if (filter_level != ALL_FILTERS)
1023 if (filter_list.head && check_filter(&filter_list, FINFO, fname, name_flags) < 0)
1029 int check_server_filter(filter_rule_list *listp, enum logcode code, const char *name, int name_flags)
1032 cur_elide_value = LOCAL_RULE;
1033 ret = check_filter(listp, code, name, name_flags);
1034 cur_elide_value = REMOTE_RULE;
1038 /* Return -1 if file "name" is defined to be excluded by the specified
1039 * exclude list, 1 if it is included, and 0 if it was not matched. */
1040 int check_filter(filter_rule_list *listp, enum logcode code,
1041 const char *name, int name_flags)
1045 for (ent = listp->head; ent; ent = ent->next) {
1046 if (ignore_perishable && ent->rflags & FILTRULE_PERISHABLE)
1048 if (ent->rflags & FILTRULE_PERDIR_MERGE) {
1049 int rc = check_filter(ent->u.mergelist, code, name, name_flags);
1054 if (ent->rflags & FILTRULE_CVS_IGNORE) {
1055 int rc = check_filter(&cvs_filter_list, code, name, name_flags);
1060 if (rule_matches(name, ent, name_flags)) {
1061 report_filter_result(code, name, ent, name_flags, listp->debug_type);
1062 return ent->rflags & FILTRULE_INCLUDE ? 1 : -1;
1069 #define RULE_STRCMP(s,r) rule_strcmp((s), (r), sizeof (r) - 1)
1071 static const uchar *rule_strcmp(const uchar *str, const char *rule, int rule_len)
1073 if (strncmp((char*)str, rule, rule_len) != 0)
1075 if (isspace(str[rule_len]) || str[rule_len] == '_' || !str[rule_len])
1076 return str + rule_len - 1;
1077 if (str[rule_len] == ',')
1078 return str + rule_len;
1082 #define FILTRULES_FROM_CONTAINER (FILTRULE_ABS_PATH | FILTRULE_INCLUDE \
1083 | FILTRULE_DIRECTORY | FILTRULE_NEGATE \
1084 | FILTRULE_PERISHABLE)
1086 /* Gets the next include/exclude rule from *rulestr_ptr and advances
1087 * *rulestr_ptr to point beyond it. Stores the pattern's start (within
1088 * *rulestr_ptr) and length in *pat_ptr and *pat_len_ptr, and returns a newly
1089 * allocated filter_rule containing the rest of the information. Returns
1090 * NULL if there are no more rules in the input.
1092 * The template provides defaults for the new rule to inherit, and the
1093 * template rflags and the xflags additionally affect parsing. */
1094 static filter_rule *parse_rule_tok(const char **rulestr_ptr,
1095 const filter_rule *template, int xflags,
1096 const char **pat_ptr, unsigned int *pat_len_ptr)
1098 const uchar *s = (const uchar *)*rulestr_ptr;
1102 if (template->rflags & FILTRULE_WORD_SPLIT) {
1103 /* Skip over any initial whitespace. */
1106 /* Update to point to real start of rule. */
1107 *rulestr_ptr = (const char *)s;
1112 rule = new0(filter_rule);
1114 /* Inherit from the template. Don't inherit FILTRULES_SIDES; we check
1116 rule->rflags = template->rflags & FILTRULES_FROM_CONTAINER;
1118 /* Figure out what kind of a filter rule "s" is pointing at. Note
1119 * that if FILTRULE_NO_PREFIXES is set, the rule is either an include
1120 * or an exclude based on the inheritance of the FILTRULE_INCLUDE
1121 * flag (above). XFLG_OLD_PREFIXES indicates a compatibility mode
1122 * for old include/exclude patterns where just "+ " and "- " are
1123 * allowed as optional prefixes. */
1124 if (template->rflags & FILTRULE_NO_PREFIXES) {
1125 if (*s == '!' && template->rflags & FILTRULE_CVS_IGNORE)
1126 rule->rflags |= FILTRULE_CLEAR_LIST; /* Tentative! */
1127 } else if (xflags & XFLG_OLD_PREFIXES) {
1128 if (*s == '-' && s[1] == ' ') {
1129 rule->rflags &= ~FILTRULE_INCLUDE;
1131 } else if (*s == '+' && s[1] == ' ') {
1132 rule->rflags |= FILTRULE_INCLUDE;
1134 } else if (*s == '!')
1135 rule->rflags |= FILTRULE_CLEAR_LIST; /* Tentative! */
1138 BOOL prefix_specifies_side = False;
1141 if ((s = RULE_STRCMP(s, "clear")) != NULL)
1145 if ((s = RULE_STRCMP(s, "dir-merge")) != NULL)
1149 if ((s = RULE_STRCMP(s, "exclude")) != NULL)
1153 if ((s = RULE_STRCMP(s, "hide")) != NULL)
1157 if ((s = RULE_STRCMP(s, "include")) != NULL)
1161 if ((s = RULE_STRCMP(s, "merge")) != NULL)
1165 if ((s = RULE_STRCMP(s, "protect")) != NULL)
1169 if ((s = RULE_STRCMP(s, "risk")) != NULL)
1173 if ((s = RULE_STRCMP(s, "show")) != NULL)
1184 trust_sender_filter = 1;
1185 rule->rflags |= FILTRULE_PERDIR_MERGE
1186 | FILTRULE_FINISH_SETUP;
1189 rule->rflags |= FILTRULE_MERGE_FILE;
1192 rule->rflags |= FILTRULE_INCLUDE;
1197 rule->rflags |= FILTRULE_INCLUDE;
1200 rule->rflags |= FILTRULE_SENDER_SIDE;
1201 prefix_specifies_side = True;
1204 rule->rflags |= FILTRULE_INCLUDE;
1207 rule->rflags |= FILTRULE_RECEIVER_SIDE;
1208 prefix_specifies_side = True;
1211 rule->rflags |= FILTRULE_CLEAR_LIST;
1214 rprintf(FERROR, "Unknown filter rule: `%s'\n", *rulestr_ptr);
1215 exit_cleanup(RERR_SYNTAX);
1217 while (ch != '!' && *++s && *s != ' ' && *s != '_') {
1218 if (template->rflags & FILTRULE_WORD_SPLIT && isspace(*s)) {
1226 "invalid modifier '%c' at position %d in filter rule: %s\n",
1227 *s, (int)(s - (const uchar *)*rulestr_ptr), *rulestr_ptr);
1228 exit_cleanup(RERR_SYNTAX);
1230 if (!BITS_SETnUNSET(rule->rflags, FILTRULE_MERGE_FILE, FILTRULE_NO_PREFIXES))
1232 rule->rflags |= FILTRULE_NO_PREFIXES;
1235 if (!BITS_SETnUNSET(rule->rflags, FILTRULE_MERGE_FILE, FILTRULE_NO_PREFIXES))
1237 rule->rflags |= FILTRULE_NO_PREFIXES
1241 rule->rflags |= FILTRULE_ABS_PATH;
1244 /* Negation really goes with the pattern, so it
1245 * isn't useful as a merge-file default. */
1246 if (rule->rflags & FILTRULE_MERGE_FILE)
1248 rule->rflags |= FILTRULE_NEGATE;
1251 if (rule->rflags & FILTRULE_NO_PREFIXES || prefix_specifies_side)
1253 rule->rflags |= FILTRULE_NO_PREFIXES
1254 | FILTRULE_WORD_SPLIT
1255 | FILTRULE_NO_INHERIT
1256 | FILTRULE_CVS_IGNORE;
1259 if (!(rule->rflags & FILTRULE_MERGE_FILE))
1261 rule->rflags |= FILTRULE_EXCLUDE_SELF;
1264 if (!(rule->rflags & FILTRULE_MERGE_FILE))
1266 rule->rflags |= FILTRULE_NO_INHERIT;
1269 rule->rflags |= FILTRULE_PERISHABLE;
1272 if (prefix_specifies_side)
1274 rule->rflags |= FILTRULE_RECEIVER_SIDE;
1277 if (prefix_specifies_side)
1279 rule->rflags |= FILTRULE_SENDER_SIDE;
1282 if (!(rule->rflags & FILTRULE_MERGE_FILE))
1284 rule->rflags |= FILTRULE_WORD_SPLIT;
1287 rule->rflags |= FILTRULE_XATTR;
1288 saw_xattr_filter = 1;
1295 if (template->rflags & FILTRULES_SIDES) {
1296 if (rule->rflags & FILTRULES_SIDES) {
1297 /* The filter and template both specify side(s). This
1298 * is dodgy (and won't work correctly if the template is
1299 * a one-sided per-dir merge rule), so reject it. */
1301 "specified-side merge file contains specified-side filter: %s\n",
1303 exit_cleanup(RERR_SYNTAX);
1305 rule->rflags |= template->rflags & FILTRULES_SIDES;
1308 if (template->rflags & FILTRULE_WORD_SPLIT) {
1309 const uchar *cp = s;
1310 /* Token ends at whitespace or the end of the string. */
1311 while (!isspace(*cp) && *cp != '\0')
1315 len = strlen((char*)s);
1317 if (rule->rflags & FILTRULE_CLEAR_LIST) {
1318 if (!(rule->rflags & FILTRULE_NO_PREFIXES)
1319 && !(xflags & XFLG_OLD_PREFIXES) && len) {
1321 "'!' rule has trailing characters: %s\n", *rulestr_ptr);
1322 exit_cleanup(RERR_SYNTAX);
1325 rule->rflags &= ~FILTRULE_CLEAR_LIST;
1326 } else if (!len && !(rule->rflags & FILTRULE_CVS_IGNORE)) {
1327 rprintf(FERROR, "unexpected end of filter rule: %s\n", *rulestr_ptr);
1328 exit_cleanup(RERR_SYNTAX);
1331 /* --delete-excluded turns an un-modified include/exclude into a sender-side rule. */
1333 && !(rule->rflags & (FILTRULES_SIDES|FILTRULE_MERGE_FILE|FILTRULE_PERDIR_MERGE)))
1334 rule->rflags |= FILTRULE_SENDER_SIDE;
1336 *pat_ptr = (const char *)s;
1338 *rulestr_ptr = *pat_ptr + len;
1342 static void get_cvs_excludes(uint32 rflags)
1344 static int initialized = 0;
1345 char *p, fname[MAXPATHLEN];
1351 parse_filter_str(&cvs_filter_list, default_cvsignore(),
1352 rule_template(rflags | (protocol_version >= 30 ? FILTRULE_PERISHABLE : 0)),
1355 p = module_id >= 0 && lp_use_chroot(module_id) ? "/" : getenv("HOME");
1356 if (p && pathjoin(fname, MAXPATHLEN, p, ".cvsignore") < MAXPATHLEN)
1357 parse_filter_file(&cvs_filter_list, fname, rule_template(rflags), 0);
1359 parse_filter_str(&cvs_filter_list, getenv("CVSIGNORE"), rule_template(rflags), 0);
1362 const filter_rule *rule_template(uint32 rflags)
1364 static filter_rule template; /* zero-initialized */
1365 template.rflags = rflags;
1369 void parse_filter_str(filter_rule_list *listp, const char *rulestr,
1370 const filter_rule *template, int xflags)
1374 unsigned int pat_len;
1382 /* Remember that the returned string is NOT '\0' terminated! */
1383 if (!(rule = parse_rule_tok(&rulestr, template, xflags, &pat, &pat_len)))
1386 if (pat_len >= MAXPATHLEN) {
1387 rprintf(FERROR, "discarding over-long filter: %.*s\n",
1394 new_rflags = rule->rflags;
1395 if (new_rflags & FILTRULE_CLEAR_LIST) {
1396 if (DEBUG_GTE(FILTER, 2)) {
1398 "[%s] clearing filter list%s\n",
1399 who_am_i(), listp->debug_type);
1401 pop_filter_list(listp);
1406 if (new_rflags & FILTRULE_MERGE_FILE) {
1411 if (new_rflags & FILTRULE_EXCLUDE_SELF) {
1413 filter_rule *excl_self;
1415 excl_self = new0(filter_rule);
1416 /* Find the beginning of the basename and add an exclude for it. */
1417 for (name = pat + pat_len; name > pat && name[-1] != '/'; name--) {}
1418 add_rule(listp, name, (pat + pat_len) - name, excl_self, 0);
1419 rule->rflags &= ~FILTRULE_EXCLUDE_SELF;
1421 if (new_rflags & FILTRULE_PERDIR_MERGE) {
1422 if (parent_dirscan) {
1424 unsigned int len = pat_len;
1425 if ((p = parse_merge_name(pat, &len, module_dirlen)))
1426 add_rule(listp, p, len, rule, 0);
1433 unsigned int len = pat_len;
1434 if ((p = parse_merge_name(pat, &len, 0)))
1435 parse_filter_file(listp, p, rule, XFLG_FATAL_ERRORS);
1441 add_rule(listp, pat, pat_len, rule, xflags);
1443 if (new_rflags & FILTRULE_CVS_IGNORE
1444 && !(new_rflags & FILTRULE_MERGE_FILE))
1445 get_cvs_excludes(new_rflags);
1449 void parse_filter_file(filter_rule_list *listp, const char *fname, const filter_rule *template, int xflags)
1452 char line[BIGPATHBUFLEN];
1453 char *eob = line + sizeof line - 1;
1454 BOOL word_split = (template->rflags & FILTRULE_WORD_SPLIT) != 0;
1456 if (!fname || !*fname)
1459 if (*fname != '-' || fname[1] || am_server) {
1460 if (daemon_filter_list.head) {
1461 strlcpy(line, fname, sizeof line);
1462 clean_fname(line, CFN_COLLAPSE_DOT_DOT_DIRS);
1463 if (check_filter(&daemon_filter_list, FLOG, line, 0) < 0)
1466 fp = fopen(line, "rb");
1468 fp = fopen(fname, "rb");
1472 if (DEBUG_GTE(FILTER, 2)) {
1473 rprintf(FINFO, "[%s] parse_filter_file(%s,%x,%x)%s\n",
1474 who_am_i(), fname, template->rflags, xflags,
1475 fp ? "" : " [not found]");
1479 if (xflags & XFLG_FATAL_ERRORS) {
1480 rsyserr(FERROR, errno,
1481 "failed to open %sclude file %s",
1482 template->rflags & FILTRULE_INCLUDE ? "in" : "ex",
1484 exit_cleanup(RERR_FILEIO);
1488 dirbuf[dirbuf_len] = '\0';
1492 int ch, overflow = 0;
1494 if ((ch = getc(fp)) == EOF) {
1495 if (ferror(fp) && errno == EINTR) {
1501 if (word_split && isspace(ch))
1503 if (eol_nulls? !ch : (ch == '\n' || ch == '\r'))
1511 rprintf(FERROR, "discarding over-long filter: %s...\n", line);
1515 /* Skip an empty token and (when line parsing) comments. */
1516 if (*line && (word_split || (*line != ';' && *line != '#')))
1517 parse_filter_str(listp, line, template, xflags);
1524 /* If the "for_xfer" flag is set, the prefix is made compatible with the
1525 * current protocol_version (if possible) or a NULL is returned (if not
1527 char *get_rule_prefix(filter_rule *rule, const char *pat, int for_xfer,
1528 unsigned int *plen_ptr)
1530 static char buf[MAX_RULE_PREFIX+1];
1532 int legal_len = for_xfer && protocol_version < 29 ? 1 : MAX_RULE_PREFIX-1;
1534 if (rule->rflags & FILTRULE_PERDIR_MERGE) {
1538 } else if (rule->rflags & FILTRULE_INCLUDE)
1540 else if (legal_len != 1
1541 || ((*pat == '-' || *pat == '+') && pat[1] == ' '))
1546 if (rule->rflags & FILTRULE_ABS_PATH)
1548 if (rule->rflags & FILTRULE_NEGATE)
1550 if (rule->rflags & FILTRULE_CVS_IGNORE)
1553 if (rule->rflags & FILTRULE_NO_INHERIT)
1555 if (rule->rflags & FILTRULE_WORD_SPLIT)
1557 if (rule->rflags & FILTRULE_NO_PREFIXES) {
1558 if (rule->rflags & FILTRULE_INCLUDE)
1564 if (rule->rflags & FILTRULE_EXCLUDE_SELF)
1566 if (rule->rflags & FILTRULE_XATTR)
1568 if (rule->rflags & FILTRULE_SENDER_SIDE
1569 && (!for_xfer || protocol_version >= 29))
1571 if (rule->rflags & FILTRULE_RECEIVER_SIDE
1572 && (!for_xfer || protocol_version >= 29
1573 || (delete_excluded && am_sender)))
1575 if (rule->rflags & FILTRULE_PERISHABLE) {
1576 if (!for_xfer || protocol_version >= 30)
1581 if (op - buf > legal_len)
1587 *plen_ptr = op - buf;
1591 static void send_rules(int f_out, filter_rule_list *flp)
1595 for (ent = flp->head; ent; ent = ent->next) {
1596 unsigned int len, plen, dlen;
1600 /* Note we need to check delete_excluded here in addition to
1601 * the code in parse_rule_tok() because some rules may have
1602 * been added before we found the --delete-excluded option.
1603 * We must also elide any CVS merge-file rules to avoid a
1604 * backward compatibility problem, and we elide any no-prefix
1605 * merge files as an optimization (since they can only have
1606 * include/exclude rules). */
1607 if (ent->rflags & FILTRULE_SENDER_SIDE)
1608 elide = am_sender ? LOCAL_RULE : REMOTE_RULE;
1609 if (ent->rflags & FILTRULE_RECEIVER_SIDE)
1610 elide = elide ? 0 : am_sender ? REMOTE_RULE : LOCAL_RULE;
1611 else if (delete_excluded && !elide
1612 && (!(ent->rflags & FILTRULE_PERDIR_MERGE)
1613 || ent->rflags & FILTRULE_NO_PREFIXES))
1614 elide = am_sender ? LOCAL_RULE : REMOTE_RULE;
1616 if (elide == LOCAL_RULE)
1618 if (ent->rflags & FILTRULE_CVS_IGNORE
1619 && !(ent->rflags & FILTRULE_MERGE_FILE)) {
1620 int f = am_sender || protocol_version < 29 ? f_out : -2;
1621 send_rules(f, &cvs_filter_list);
1625 p = get_rule_prefix(ent, ent->pattern, 1, &plen);
1628 "filter rules are too modern for remote rsync.\n");
1629 exit_cleanup(RERR_PROTOCOL);
1633 len = strlen(ent->pattern);
1634 dlen = ent->rflags & FILTRULE_DIRECTORY ? 1 : 0;
1635 if (!(plen + len + dlen))
1637 write_int(f_out, plen + len + dlen);
1639 write_buf(f_out, p, plen);
1640 write_buf(f_out, ent->pattern, len);
1642 write_byte(f_out, '/');
1646 /* This is only called by the client. */
1647 void send_filter_list(int f_out)
1649 int receiver_wants_list = prune_empty_dirs
1650 || (delete_mode && (!delete_excluded || protocol_version >= 29));
1652 if (local_server || (am_sender && !receiver_wants_list))
1654 if (cvs_exclude && am_sender) {
1655 if (protocol_version >= 29)
1656 parse_filter_str(&filter_list, ":C", rule_template(0), 0);
1657 parse_filter_str(&filter_list, "-C", rule_template(0), 0);
1660 send_rules(f_out, &filter_list);
1663 write_int(f_out, 0);
1666 if (!am_sender || protocol_version < 29)
1667 parse_filter_str(&filter_list, ":C", rule_template(0), 0);
1669 parse_filter_str(&filter_list, "-C", rule_template(0), 0);
1673 /* This is only called by the server. */
1674 void recv_filter_list(int f_in)
1676 char line[BIGPATHBUFLEN];
1677 int xflags = protocol_version >= 29 ? 0 : XFLG_OLD_PREFIXES;
1678 int receiver_wants_list = prune_empty_dirs
1679 || (delete_mode && (!delete_excluded || protocol_version >= 29));
1682 if (!local_server && (am_sender || receiver_wants_list)) {
1683 while ((len = read_int(f_in)) != 0) {
1684 if (len >= sizeof line)
1685 overflow_exit("recv_rules");
1686 read_sbuf(f_in, line, len);
1687 parse_filter_str(&filter_list, line, rule_template(0), xflags);
1692 if (local_server || am_sender || protocol_version < 29)
1693 parse_filter_str(&filter_list, ":C", rule_template(0), 0);
1694 if (local_server || am_sender)
1695 parse_filter_str(&filter_list, "-C", rule_template(0), 0);
1698 if (local_server) /* filter out any rules that aren't for us. */
1699 send_rules(-1, &filter_list);