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 old_style_args;
37 extern int relative_paths;
38 extern int delete_mode;
39 extern int delete_excluded;
40 extern int cvs_exclude;
41 extern int sanitize_paths;
42 extern int protocol_version;
43 extern int read_batch;
47 extern char *filesfrom_host;
48 extern char curr_dir[MAXPATHLEN];
49 extern unsigned int curr_dir_len;
50 extern unsigned int module_dirlen;
52 filter_rule_list filter_list = { .debug_type = "" };
53 filter_rule_list cvs_filter_list = { .debug_type = " [global CVS]" };
54 filter_rule_list daemon_filter_list = { .debug_type = " [daemon]" };
55 filter_rule_list implied_filter_list = { .debug_type = " [implied]" };
57 int saw_xattr_filter = 0;
58 int trust_sender_filter = 0;
60 /* Need room enough for ":MODS " prefix plus some room to grow. */
61 #define MAX_RULE_PREFIX (16)
63 #define SLASH_WILD3_SUFFIX "/***"
65 /* The dirbuf is set by push_local_filters() to the current subdirectory
66 * relative to curr_dir that is being processed. The path always has a
67 * trailing slash appended, and the variable dirbuf_len contains the length
68 * of this path prefix. The path is always absolute. */
69 static char dirbuf[MAXPATHLEN+1];
70 static unsigned int dirbuf_len = 0;
71 static int dirbuf_depth;
73 /* This is True when we're scanning parent dirs for per-dir merge-files. */
74 static BOOL parent_dirscan = False;
76 /* This array contains a list of all the currently active per-dir merge
77 * files. This makes it easier to save the appropriate values when we
78 * "push" down into each subdirectory. */
79 static filter_rule **mergelist_parents;
80 static int mergelist_cnt = 0;
81 static int mergelist_size = 0;
83 /* Each filter_list_struct describes a singly-linked list by keeping track
84 * of both the head and tail pointers. The list is slightly unusual in that
85 * a parent-dir's content can be appended to the end of the local list in a
86 * special way: the last item in the local list has its "next" pointer set
87 * to point to the inherited list, but the local list's tail pointer points
88 * at the end of the local list. Thus, if the local list is empty, the head
89 * will be pointing at the inherited content but the tail will be NULL. To
90 * help you visualize this, here are the possible list arrangements:
92 * Completely Empty Local Content Only
93 * ================================== ====================================
94 * head -> NULL head -> Local1 -> Local2 -> NULL
95 * tail -> NULL tail -------------^
97 * Inherited Content Only Both Local and Inherited Content
98 * ================================== ====================================
99 * head -> Parent1 -> Parent2 -> NULL head -> L1 -> L2 -> P1 -> P2 -> NULL
100 * tail -> NULL tail ---------^
102 * This means that anyone wanting to traverse the whole list to use it just
103 * needs to start at the head and use the "next" pointers until it goes
104 * NULL. To add new local content, we insert the item after the tail item
105 * and update the tail (obviously, if "tail" was NULL, we insert it at the
106 * head). To clear the local list, WE MUST NOT FREE THE INHERITED CONTENT
107 * because it is shared between the current list and our parent list(s).
108 * The easiest way to handle this is to simply truncate the list after the
109 * tail item and then free the local list from the head. When inheriting
110 * the list for a new local dir, we just save off the filter_list_struct
111 * values (so we can pop back to them later) and set the tail to NULL.
114 static void teardown_mergelist(filter_rule *ex)
118 if (!ex->u.mergelist)
121 if (DEBUG_GTE(FILTER, 2)) {
122 rprintf(FINFO, "[%s] deactivating mergelist #%d%s\n",
123 who_am_i(), mergelist_cnt - 1,
124 ex->u.mergelist->debug_type);
127 free(ex->u.mergelist->debug_type);
128 free(ex->u.mergelist);
130 for (j = 0; j < mergelist_cnt; j++) {
131 if (mergelist_parents[j] == ex) {
132 mergelist_parents[j] = NULL;
136 while (mergelist_cnt && mergelist_parents[mergelist_cnt-1] == NULL)
140 static void free_filter(filter_rule *ex)
142 if (ex->rflags & FILTRULE_PERDIR_MERGE)
143 teardown_mergelist(ex);
148 static void free_filters(filter_rule *ent)
151 filter_rule *next = ent->next;
157 /* Build a filter structure given a filter pattern. The value in "pat"
158 * is not null-terminated. "rule" is either held or freed, so the
159 * caller should not free it. */
160 static void add_rule(filter_rule_list *listp, const char *pat, unsigned int pat_len,
161 filter_rule *rule, int xflags)
164 unsigned int pre_len, suf_len, slash_cnt = 0;
165 char *mention_rule_suffix;
167 if (DEBUG_GTE(FILTER, 1) && pat_len && (pat[pat_len-1] == ' ' || pat[pat_len-1] == '\t'))
168 mention_rule_suffix = " -- CAUTION: trailing whitespace!";
170 mention_rule_suffix = DEBUG_GTE(FILTER, 2) ? "" : NULL;
171 if (mention_rule_suffix) {
172 rprintf(FINFO, "[%s] add_rule(%s%.*s%s)%s%s\n",
173 who_am_i(), get_rule_prefix(rule, pat, 0, NULL),
174 (int)pat_len, pat, (rule->rflags & FILTRULE_DIRECTORY) ? "/" : "",
175 listp->debug_type, mention_rule_suffix);
178 /* These flags also indicate that we're reading a list that
179 * needs to be filtered now, not post-filtered later. */
180 if (xflags & (XFLG_ANCHORED2ABS|XFLG_ABS_IF_SLASH)
181 && (rule->rflags & FILTRULES_SIDES)
182 == (am_sender ? FILTRULE_RECEIVER_SIDE : FILTRULE_SENDER_SIDE)) {
183 /* This filter applies only to the other side. Drop it. */
188 if (pat_len > 1 && pat[pat_len-1] == '/') {
190 rule->rflags |= FILTRULE_DIRECTORY;
193 for (cp = pat; cp < pat + pat_len; cp++) {
198 if (!(rule->rflags & (FILTRULE_ABS_PATH | FILTRULE_MERGE_FILE))
199 && ((xflags & (XFLG_ANCHORED2ABS|XFLG_ABS_IF_SLASH) && *pat == '/')
200 || (xflags & XFLG_ABS_IF_SLASH && slash_cnt))) {
201 rule->rflags |= FILTRULE_ABS_PATH;
203 pre_len = dirbuf_len - module_dirlen - 1;
209 /* The daemon wants dir-exclude rules to get an appended "/" + "***". */
210 if (xflags & XFLG_DIR2WILD3
211 && BITS_SETnUNSET(rule->rflags, FILTRULE_DIRECTORY, FILTRULE_INCLUDE)) {
212 rule->rflags &= ~FILTRULE_DIRECTORY;
213 suf_len = sizeof SLASH_WILD3_SUFFIX - 1;
217 rule->pattern = new_array(char, pre_len + pat_len + suf_len + 1);
219 memcpy(rule->pattern, dirbuf + module_dirlen, pre_len);
220 for (cp = rule->pattern; cp < rule->pattern + pre_len; cp++) {
225 strlcpy(rule->pattern + pre_len, pat, pat_len + 1);
228 memcpy(rule->pattern + pat_len, SLASH_WILD3_SUFFIX, suf_len+1);
233 if (strpbrk(rule->pattern, "*[?")) {
234 rule->rflags |= FILTRULE_WILD;
235 if ((cp = strstr(rule->pattern, "**")) != NULL) {
236 rule->rflags |= FILTRULE_WILD2;
237 /* If the pattern starts with **, note that. */
238 if (cp == rule->pattern)
239 rule->rflags |= FILTRULE_WILD2_PREFIX;
240 /* If the pattern ends with ***, note that. */
242 && rule->pattern[pat_len-3] == '*'
243 && rule->pattern[pat_len-2] == '*'
244 && rule->pattern[pat_len-1] == '*')
245 rule->rflags |= FILTRULE_WILD3_SUFFIX;
249 if (rule->rflags & FILTRULE_PERDIR_MERGE) {
250 filter_rule_list *lp;
254 if ((cp = strrchr(rule->pattern, '/')) != NULL)
259 /* If the local merge file was already mentioned, don't
261 for (i = 0; i < mergelist_cnt; i++) {
262 filter_rule *ex = mergelist_parents[i];
266 s = strrchr(ex->pattern, '/');
272 if (len == pat_len - (cp - rule->pattern) && memcmp(s, cp, len) == 0) {
278 lp = new_array0(filter_rule_list, 1);
279 if (asprintf(&lp->debug_type, " [per-dir %s]", cp) < 0)
280 out_of_memory("add_rule");
281 rule->u.mergelist = lp;
283 if (mergelist_cnt == mergelist_size) {
285 mergelist_parents = realloc_array(mergelist_parents, filter_rule *, mergelist_size);
287 if (DEBUG_GTE(FILTER, 2)) {
288 rprintf(FINFO, "[%s] activating mergelist #%d%s\n",
289 who_am_i(), mergelist_cnt, lp->debug_type);
291 mergelist_parents[mergelist_cnt++] = rule;
293 rule->u.slash_cnt = slash_cnt;
296 rule->next = listp->head;
297 listp->head = listp->tail = rule;
299 rule->next = listp->tail->next;
300 listp->tail->next = rule;
305 /* If the wildcards failed, the remote shell might give us a file matching the literal
306 * wildcards. Since "*" & "?" already match themselves, this just needs to deal with
307 * failed "[foo]" idioms.
309 static void maybe_add_literal_brackets_rule(filter_rule const *based_on, int arg_len)
312 const char *arg = based_on->pattern, *cp;
317 arg_len = strlen(arg);
319 for (cp = arg; *cp; cp++) {
320 if (*cp == '\\' && cp[1]) {
322 } else if (*cp == '[')
328 rule = new0(filter_rule);
329 rule->rflags = based_on->rflags;
330 rule->u.slash_cnt = based_on->u.slash_cnt;
331 p = rule->pattern = new_array(char, arg_len + cnt + 1);
332 for (cp = arg; *cp; ) {
333 if (*cp == '\\' && cp[1]) {
335 } else if (*cp == '[')
341 rule->next = implied_filter_list.head;
342 implied_filter_list.head = rule;
343 if (DEBUG_GTE(FILTER, 3)) {
344 rprintf(FINFO, "[%s] add_implied_include(%s%s)\n", who_am_i(), rule->pattern,
345 rule->rflags & FILTRULE_DIRECTORY ? "/" : "");
349 static char *partial_string_buf = NULL;
350 static int partial_string_len = 0;
351 void implied_include_partial_string(const char *s_start, const char *s_end)
353 partial_string_len = s_end - s_start;
354 if (partial_string_len <= 0 || partial_string_len >= MAXPATHLEN) { /* too-large should be impossible... */
355 partial_string_len = 0;
358 if (!partial_string_buf)
359 partial_string_buf = new_array(char, MAXPATHLEN);
360 memcpy(partial_string_buf, s_start, partial_string_len);
363 void free_implied_include_partial_string()
365 if (partial_string_buf) {
366 free(partial_string_buf);
367 partial_string_buf = NULL;
371 /* Each arg the client sends to the remote sender turns into an implied include
372 * that the receiver uses to validate the file list from the sender. */
373 void add_implied_include(const char *arg, int skip_daemon_module)
376 int arg_len, saw_wild = 0, saw_live_open_brkt = 0, backslash_cnt = 0;
377 int slash_cnt = 1; /* We know we're adding a leading slash. */
380 if (am_server || old_style_args || list_only || read_batch || filesfrom_host != NULL)
382 if (partial_string_len) {
383 arg_len = strlen(arg);
384 if (partial_string_len + arg_len >= MAXPATHLEN)
385 return; /* Should be impossible... */
386 memcpy(partial_string_buf + partial_string_len, arg, arg_len + 1);
387 partial_string_len = 0;
388 arg = partial_string_buf;
390 if (skip_daemon_module) {
391 if ((cp = strchr(arg, '/')) != NULL)
396 if (relative_paths) {
397 if ((cp = strstr(arg, "/./")) != NULL)
399 } else if ((cp = strrchr(arg, '/')) != NULL) {
401 if (*arg == '.' && arg[1] == '\0')
404 arg_len = strlen(arg);
406 if (strpbrk(arg, "*[?")) {
407 /* We need to add room to escape backslashes if wildcard chars are present. */
408 for (cp = arg; (cp = strchr(cp, '\\')) != NULL; cp++)
412 arg_len++; /* Leave room for the prefixed slash */
413 rule = new0(filter_rule);
414 if (!implied_filter_list.head)
415 implied_filter_list.head = implied_filter_list.tail = rule;
417 rule->next = implied_filter_list.head;
418 implied_filter_list.head = rule;
420 rule->rflags = FILTRULE_INCLUDE + (saw_wild ? FILTRULE_WILD : 0);
421 p = rule->pattern = new_array(char, arg_len + 1);
423 for (cp = arg; *cp; ) {
427 cp++; /* A \] in a filter might cause a problem w/o wildcards. */
428 else if (!strchr("*[?", cp[1])) {
436 if (p[-1] == '/') { /* This is safe because of the initial slash. */
440 if (relative_paths) {
441 filter_rule const *ent;
444 for (ent = implied_filter_list.head; ent; ent = ent->next) {
445 if (ent != rule && strcmp(ent->pattern, rule->pattern) == 0) {
451 filter_rule *R_rule = new0(filter_rule);
452 R_rule->rflags = FILTRULE_INCLUDE | FILTRULE_DIRECTORY;
453 /* Check if our sub-path has wildcards or escaped backslashes */
454 if (saw_wild && strpbrk(rule->pattern, "*[?\\"))
455 R_rule->rflags |= FILTRULE_WILD;
456 R_rule->pattern = strdup(rule->pattern);
457 R_rule->u.slash_cnt = slash_cnt;
458 R_rule->next = implied_filter_list.head;
459 implied_filter_list.head = R_rule;
460 if (DEBUG_GTE(FILTER, 3)) {
461 rprintf(FINFO, "[%s] add_implied_include(%s/)\n",
462 who_am_i(), R_rule->pattern);
464 if (saw_live_open_brkt)
465 maybe_add_literal_brackets_rule(R_rule, -1);
472 saw_live_open_brkt = 1;
481 rule->u.slash_cnt = slash_cnt;
483 arg_len = p - arg; /* We recompute it due to backslash weirdness. */
484 if (DEBUG_GTE(FILTER, 3))
485 rprintf(FINFO, "[%s] add_implied_include(%s)\n", who_am_i(), rule->pattern);
486 if (saw_live_open_brkt)
487 maybe_add_literal_brackets_rule(rule, arg_len);
490 if (recurse || xfer_dirs) {
491 /* Now create a rule with an added "/" & "**" or "*" at the end */
492 rule = new0(filter_rule);
493 rule->rflags = FILTRULE_INCLUDE | FILTRULE_WILD;
495 rule->rflags |= FILTRULE_WILD2;
496 /* We must leave enough room for / * * \0. */
497 if (!saw_wild && backslash_cnt) {
498 /* We are appending a wildcard, so now the backslashes need to be escaped. */
499 p = rule->pattern = new_array(char, arg_len + backslash_cnt + 3 + 1);
500 for (cp = arg; *cp; ) {
506 p = rule->pattern = new_array(char, arg_len + 3 + 1);
508 memcpy(p, arg, arg_len);
518 rule->u.slash_cnt = slash_cnt + 1;
519 rule->next = implied_filter_list.head;
520 implied_filter_list.head = rule;
521 if (DEBUG_GTE(FILTER, 3))
522 rprintf(FINFO, "[%s] add_implied_include(%s)\n", who_am_i(), rule->pattern);
523 if (saw_live_open_brkt)
524 maybe_add_literal_brackets_rule(rule, p - rule->pattern);
528 /* This frees any non-inherited items, leaving just inherited items on the list. */
529 static void pop_filter_list(filter_rule_list *listp)
531 filter_rule *inherited;
536 inherited = listp->tail->next;
538 /* Truncate any inherited items from the local list. */
539 listp->tail->next = NULL;
540 /* Now free everything that is left. */
541 free_filters(listp->head);
543 listp->head = inherited;
547 /* This returns an expanded (absolute) filename for the merge-file name if
548 * the name has any slashes in it OR if the parent_dirscan var is True;
549 * otherwise it returns the original merge_file name. If the len_ptr value
550 * is non-NULL the merge_file name is limited by the referenced length
551 * value and will be updated with the length of the resulting name. We
552 * always return a name that is null terminated, even if the merge_file
554 static char *parse_merge_name(const char *merge_file, unsigned int *len_ptr,
555 unsigned int prefix_skip)
557 static char buf[MAXPATHLEN];
558 char *fn, tmpbuf[MAXPATHLEN];
561 if (!parent_dirscan && *merge_file != '/') {
562 /* Return the name unchanged it doesn't have any slashes. */
564 const char *p = merge_file + *len_ptr;
565 while (--p > merge_file && *p != '/') {}
566 if (p == merge_file) {
567 strlcpy(buf, merge_file, *len_ptr + 1);
570 } else if (strchr(merge_file, '/') == NULL)
571 return (char *)merge_file;
574 fn = *merge_file == '/' ? buf : tmpbuf;
575 if (sanitize_paths) {
576 const char *r = prefix_skip ? "/" : NULL;
577 /* null-terminate the name if it isn't already */
578 if (len_ptr && merge_file[*len_ptr]) {
579 char *to = fn == buf ? tmpbuf : buf;
580 strlcpy(to, merge_file, *len_ptr + 1);
583 if (!sanitize_path(fn, merge_file, r, dirbuf_depth, SP_DEFAULT)) {
584 rprintf(FERROR, "merge-file name overflows: %s\n",
590 strlcpy(fn, merge_file, len_ptr ? *len_ptr + 1 : MAXPATHLEN);
591 fn_len = clean_fname(fn, CFN_COLLAPSE_DOT_DOT_DIRS);
594 /* If the name isn't in buf yet, it wasn't absolute. */
596 int d_len = dirbuf_len - prefix_skip;
597 if (d_len + fn_len >= MAXPATHLEN) {
598 rprintf(FERROR, "merge-file name overflows: %s\n", fn);
601 memcpy(buf, dirbuf + prefix_skip, d_len);
602 memcpy(buf + d_len, fn, fn_len + 1);
603 fn_len = clean_fname(buf, CFN_COLLAPSE_DOT_DOT_DIRS);
611 /* Sets the dirbuf and dirbuf_len values. */
612 void set_filter_dir(const char *dir, unsigned int dirlen)
616 memcpy(dirbuf, curr_dir, curr_dir_len);
617 dirbuf[curr_dir_len] = '/';
618 len = curr_dir_len + 1;
619 if (len + dirlen >= MAXPATHLEN)
623 memcpy(dirbuf + len, dir, dirlen);
624 dirbuf[dirlen + len] = '\0';
625 dirbuf_len = clean_fname(dirbuf, CFN_COLLAPSE_DOT_DOT_DIRS);
626 if (dirbuf_len > 1 && dirbuf[dirbuf_len-1] == '.'
627 && dirbuf[dirbuf_len-2] == '/')
630 dirbuf[dirbuf_len++] = '/';
631 dirbuf[dirbuf_len] = '\0';
633 dirbuf_depth = count_dir_elements(dirbuf + module_dirlen);
636 /* This routine takes a per-dir merge-file entry and finishes its setup.
637 * If the name has a path portion then we check to see if it refers to a
638 * parent directory of the first transfer dir. If it does, we scan all the
639 * dirs from that point through the parent dir of the transfer dir looking
640 * for the per-dir merge-file in each one. */
641 static BOOL setup_merge_file(int mergelist_num, filter_rule *ex,
642 filter_rule_list *lp)
644 char buf[MAXPATHLEN];
645 char *x, *y, *pat = ex->pattern;
648 if (!(x = parse_merge_name(pat, NULL, 0)) || *x != '/')
651 if (DEBUG_GTE(FILTER, 2)) {
652 rprintf(FINFO, "[%s] performing parent_dirscan for mergelist #%d%s\n",
653 who_am_i(), mergelist_num, lp->debug_type);
657 ex->pattern = strdup(y+1);
661 strlcpy(buf, x, MAXPATHLEN);
663 pathjoin(buf, MAXPATHLEN, dirbuf, x);
665 len = clean_fname(buf, CFN_COLLAPSE_DOT_DOT_DIRS);
666 if (len != 1 && len < MAXPATHLEN-1) {
670 /* This ensures that the specified dir is a parent of the transfer. */
671 for (x = buf, y = dirbuf; *x && *x == *y; x++, y++) {}
673 y += strlen(y); /* nope -- skip the scan */
675 parent_dirscan = True;
677 char save[MAXPATHLEN];
678 strlcpy(save, y, MAXPATHLEN);
680 dirbuf_len = y - dirbuf;
681 strlcpy(x, ex->pattern, MAXPATHLEN - (x - buf));
682 parse_filter_file(lp, buf, ex, XFLG_ANCHORED2ABS);
683 if (ex->rflags & FILTRULE_NO_INHERIT) {
684 /* Free the undesired rules to clean up any per-dir
685 * mergelists they defined. Otherwise pop_local_filters
686 * may crash trying to restore nonexistent state for
687 * those mergelists. */
688 free_filters(lp->head);
692 strlcpy(y, save, MAXPATHLEN);
693 while ((*x++ = *y++) != '/') {}
695 parent_dirscan = False;
696 if (DEBUG_GTE(FILTER, 2)) {
697 rprintf(FINFO, "[%s] completed parent_dirscan for mergelist #%d%s\n",
698 who_am_i(), mergelist_num, lp->debug_type);
704 struct local_filter_state {
706 filter_rule_list mergelists[1];
709 /* Each time rsync changes to a new directory it call this function to
710 * handle all the per-dir merge-files. The "dir" value is the current path
711 * relative to curr_dir (which might not be null-terminated). We copy it
712 * into dirbuf so that we can easily append a file name on the end. */
713 void *push_local_filters(const char *dir, unsigned int dirlen)
715 struct local_filter_state *push;
718 set_filter_dir(dir, dirlen);
719 if (DEBUG_GTE(FILTER, 2)) {
720 rprintf(FINFO, "[%s] pushing local filters for %s\n",
724 if (!mergelist_cnt) {
725 /* No old state to save and no new merge files to push. */
729 push = (struct local_filter_state *)new_array(char,
730 sizeof (struct local_filter_state)
731 + (mergelist_cnt-1) * sizeof (filter_rule_list));
733 push->mergelist_cnt = mergelist_cnt;
734 for (i = 0; i < mergelist_cnt; i++) {
735 filter_rule *ex = mergelist_parents[i];
738 memcpy(&push->mergelists[i], ex->u.mergelist, sizeof (filter_rule_list));
741 /* Note: parse_filter_file() might increase mergelist_cnt, so keep
742 * this loop separate from the above loop. */
743 for (i = 0; i < mergelist_cnt; i++) {
744 filter_rule *ex = mergelist_parents[i];
745 filter_rule_list *lp;
748 lp = ex->u.mergelist;
750 if (DEBUG_GTE(FILTER, 2)) {
751 rprintf(FINFO, "[%s] pushing mergelist #%d%s\n",
752 who_am_i(), i, lp->debug_type);
755 lp->tail = NULL; /* Switch any local rules to inherited. */
756 if (ex->rflags & FILTRULE_NO_INHERIT)
759 if (ex->rflags & FILTRULE_FINISH_SETUP) {
760 ex->rflags &= ~FILTRULE_FINISH_SETUP;
761 if (setup_merge_file(i, ex, lp))
762 set_filter_dir(dir, dirlen);
765 if (strlcpy(dirbuf + dirbuf_len, ex->pattern,
766 MAXPATHLEN - dirbuf_len) < MAXPATHLEN - dirbuf_len) {
767 parse_filter_file(lp, dirbuf, ex,
770 io_error |= IOERR_GENERAL;
772 "cannot add local filter rules in long-named directory: %s\n",
775 dirbuf[dirbuf_len] = '\0';
781 void pop_local_filters(void *mem)
783 struct local_filter_state *pop = (struct local_filter_state *)mem;
785 int old_mergelist_cnt = pop ? pop->mergelist_cnt : 0;
787 if (DEBUG_GTE(FILTER, 2))
788 rprintf(FINFO, "[%s] popping local filters\n", who_am_i());
790 for (i = mergelist_cnt; i-- > 0; ) {
791 filter_rule *ex = mergelist_parents[i];
792 filter_rule_list *lp;
795 lp = ex->u.mergelist;
797 if (DEBUG_GTE(FILTER, 2)) {
798 rprintf(FINFO, "[%s] popping mergelist #%d%s\n",
799 who_am_i(), i, lp->debug_type);
803 if (i >= old_mergelist_cnt && lp->head) {
804 /* This mergelist does not exist in the state to be restored, but it
805 * still has inherited rules. This can sometimes happen if a per-dir
806 * merge file calls setup_merge_file() in push_local_filters() and that
807 * leaves some inherited rules that aren't in the pushed list state. */
808 if (DEBUG_GTE(FILTER, 2)) {
809 rprintf(FINFO, "[%s] freeing parent_dirscan filters of mergelist #%d%s\n",
810 who_am_i(), i, ex->u.mergelist->debug_type);
817 return; /* No state to restore. */
819 for (i = 0; i < old_mergelist_cnt; i++) {
820 filter_rule *ex = mergelist_parents[i];
823 memcpy(ex->u.mergelist, &pop->mergelists[i], sizeof (filter_rule_list));
829 void change_local_filter_dir(const char *dname, int dlen, int dir_depth)
831 static int cur_depth = -1;
832 static void *filt_array[MAXPATHLEN/2+1];
835 for ( ; cur_depth >= 0; cur_depth--) {
836 if (filt_array[cur_depth]) {
837 pop_local_filters(filt_array[cur_depth]);
838 filt_array[cur_depth] = NULL;
844 assert(dir_depth < MAXPATHLEN/2+1);
846 for ( ; cur_depth >= dir_depth; cur_depth--) {
847 if (filt_array[cur_depth]) {
848 pop_local_filters(filt_array[cur_depth]);
849 filt_array[cur_depth] = NULL;
853 cur_depth = dir_depth;
854 filt_array[cur_depth] = push_local_filters(dname, dlen);
857 static int rule_matches(const char *fname, filter_rule *ex, int name_flags)
859 int slash_handling, str_cnt = 0, anchored_match = 0;
860 int ret_match = ex->rflags & FILTRULE_NEGATE ? 0 : 1;
861 char *p, *pattern = ex->pattern;
862 const char *strings[16]; /* more than enough */
863 const char *name = fname + (*fname == '/');
868 if (!(name_flags & NAME_IS_XATTR) ^ !(ex->rflags & FILTRULE_XATTR))
871 if (!ex->u.slash_cnt && !(ex->rflags & FILTRULE_WILD2)) {
872 /* If the pattern does not have any slashes AND it does
873 * not have a "**" (which could match a slash), then we
874 * just match the name portion of the path. */
875 if ((p = strrchr(name,'/')) != NULL)
877 } else if (ex->rflags & FILTRULE_ABS_PATH && *fname != '/'
878 && curr_dir_len > module_dirlen + 1) {
879 /* If we're matching against an absolute-path pattern,
880 * we need to prepend our full path info. */
881 strings[str_cnt++] = curr_dir + module_dirlen + 1;
882 strings[str_cnt++] = "/";
883 } else if (ex->rflags & FILTRULE_WILD2_PREFIX && *fname != '/') {
884 /* Allow "**"+"/" to match at the start of the string. */
885 strings[str_cnt++] = "/";
887 strings[str_cnt++] = name;
888 if (name_flags & NAME_IS_DIR) {
889 /* Allow a trailing "/"+"***" to match the directory. */
890 if (ex->rflags & FILTRULE_WILD3_SUFFIX)
891 strings[str_cnt++] = "/";
892 } else if (ex->rflags & FILTRULE_DIRECTORY)
894 strings[str_cnt] = NULL;
896 if (*pattern == '/') {
901 if (!anchored_match && ex->u.slash_cnt
902 && !(ex->rflags & FILTRULE_WILD2)) {
903 /* A non-anchored match with an infix slash and no "**"
904 * needs to match the last slash_cnt+1 name elements. */
905 slash_handling = ex->u.slash_cnt + 1;
906 } else if (!anchored_match && !(ex->rflags & FILTRULE_WILD2_PREFIX)
907 && ex->rflags & FILTRULE_WILD2) {
908 /* A non-anchored match with an infix or trailing "**" (but not
909 * a prefixed "**") needs to try matching after every slash. */
912 /* The pattern matches only at the start of the path or name. */
916 if (ex->rflags & FILTRULE_WILD) {
917 if (wildmatch_array(pattern, strings, slash_handling))
919 } else if (str_cnt > 1) {
920 if (litmatch_array(pattern, strings, slash_handling))
922 } else if (anchored_match) {
923 if (strcmp(name, pattern) == 0)
926 int l1 = strlen(name);
927 int l2 = strlen(pattern);
929 strcmp(name+(l1-l2),pattern) == 0 &&
930 (l1==l2 || name[l1-(l2+1)] == '/')) {
938 static void report_filter_result(enum logcode code, char const *name,
939 filter_rule const *ent,
940 int name_flags, const char *type)
942 int log_level = am_sender || am_generator ? 1 : 3;
944 /* If a trailing slash is present to match only directories,
945 * then it is stripped out by add_rule(). So as a special
946 * case we add it back in the log output. */
947 if (DEBUG_GTE(FILTER, log_level)) {
948 static char *actions[2][2]
949 = { {"show", "hid"}, {"risk", "protect"} };
950 const char *w = who_am_i();
951 const char *t = name_flags & NAME_IS_XATTR ? "xattr"
952 : name_flags & NAME_IS_DIR ? "directory"
954 rprintf(code, "[%s] %sing %s %s because of pattern %s%s%s\n",
955 w, actions[*w=='g'][!(ent->rflags & FILTRULE_INCLUDE)],
956 t, name, ent->pattern,
957 ent->rflags & FILTRULE_DIRECTORY ? "/" : "", type);
961 /* This function is used to check if a file should be included/excluded
962 * from the list of files based on its name and type etc. The value of
963 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
964 int name_is_excluded(const char *fname, int name_flags, int filter_level)
966 if (daemon_filter_list.head && check_filter(&daemon_filter_list, FLOG, fname, name_flags) < 0) {
967 if (!(name_flags & NAME_IS_XATTR))
972 if (filter_level != ALL_FILTERS)
975 if (filter_list.head && check_filter(&filter_list, FINFO, fname, name_flags) < 0)
981 /* Return -1 if file "name" is defined to be excluded by the specified
982 * exclude list, 1 if it is included, and 0 if it was not matched. */
983 int check_filter(filter_rule_list *listp, enum logcode code,
984 const char *name, int name_flags)
988 for (ent = listp->head; ent; ent = ent->next) {
989 if (ignore_perishable && ent->rflags & FILTRULE_PERISHABLE)
991 if (ent->rflags & FILTRULE_PERDIR_MERGE) {
992 int rc = check_filter(ent->u.mergelist, code, name, name_flags);
997 if (ent->rflags & FILTRULE_CVS_IGNORE) {
998 int rc = check_filter(&cvs_filter_list, code, name, name_flags);
1003 if (rule_matches(name, ent, name_flags)) {
1004 report_filter_result(code, name, ent, name_flags, listp->debug_type);
1005 return ent->rflags & FILTRULE_INCLUDE ? 1 : -1;
1012 #define RULE_STRCMP(s,r) rule_strcmp((s), (r), sizeof (r) - 1)
1014 static const uchar *rule_strcmp(const uchar *str, const char *rule, int rule_len)
1016 if (strncmp((char*)str, rule, rule_len) != 0)
1018 if (isspace(str[rule_len]) || str[rule_len] == '_' || !str[rule_len])
1019 return str + rule_len - 1;
1020 if (str[rule_len] == ',')
1021 return str + rule_len;
1025 #define FILTRULES_FROM_CONTAINER (FILTRULE_ABS_PATH | FILTRULE_INCLUDE \
1026 | FILTRULE_DIRECTORY | FILTRULE_NEGATE \
1027 | FILTRULE_PERISHABLE)
1029 /* Gets the next include/exclude rule from *rulestr_ptr and advances
1030 * *rulestr_ptr to point beyond it. Stores the pattern's start (within
1031 * *rulestr_ptr) and length in *pat_ptr and *pat_len_ptr, and returns a newly
1032 * allocated filter_rule containing the rest of the information. Returns
1033 * NULL if there are no more rules in the input.
1035 * The template provides defaults for the new rule to inherit, and the
1036 * template rflags and the xflags additionally affect parsing. */
1037 static filter_rule *parse_rule_tok(const char **rulestr_ptr,
1038 const filter_rule *template, int xflags,
1039 const char **pat_ptr, unsigned int *pat_len_ptr)
1041 const uchar *s = (const uchar *)*rulestr_ptr;
1045 if (template->rflags & FILTRULE_WORD_SPLIT) {
1046 /* Skip over any initial whitespace. */
1049 /* Update to point to real start of rule. */
1050 *rulestr_ptr = (const char *)s;
1055 rule = new0(filter_rule);
1057 /* Inherit from the template. Don't inherit FILTRULES_SIDES; we check
1059 rule->rflags = template->rflags & FILTRULES_FROM_CONTAINER;
1061 /* Figure out what kind of a filter rule "s" is pointing at. Note
1062 * that if FILTRULE_NO_PREFIXES is set, the rule is either an include
1063 * or an exclude based on the inheritance of the FILTRULE_INCLUDE
1064 * flag (above). XFLG_OLD_PREFIXES indicates a compatibility mode
1065 * for old include/exclude patterns where just "+ " and "- " are
1066 * allowed as optional prefixes. */
1067 if (template->rflags & FILTRULE_NO_PREFIXES) {
1068 if (*s == '!' && template->rflags & FILTRULE_CVS_IGNORE)
1069 rule->rflags |= FILTRULE_CLEAR_LIST; /* Tentative! */
1070 } else if (xflags & XFLG_OLD_PREFIXES) {
1071 if (*s == '-' && s[1] == ' ') {
1072 rule->rflags &= ~FILTRULE_INCLUDE;
1074 } else if (*s == '+' && s[1] == ' ') {
1075 rule->rflags |= FILTRULE_INCLUDE;
1077 } else if (*s == '!')
1078 rule->rflags |= FILTRULE_CLEAR_LIST; /* Tentative! */
1081 BOOL prefix_specifies_side = False;
1084 if ((s = RULE_STRCMP(s, "clear")) != NULL)
1088 if ((s = RULE_STRCMP(s, "dir-merge")) != NULL)
1092 if ((s = RULE_STRCMP(s, "exclude")) != NULL)
1096 if ((s = RULE_STRCMP(s, "hide")) != NULL)
1100 if ((s = RULE_STRCMP(s, "include")) != NULL)
1104 if ((s = RULE_STRCMP(s, "merge")) != NULL)
1108 if ((s = RULE_STRCMP(s, "protect")) != NULL)
1112 if ((s = RULE_STRCMP(s, "risk")) != NULL)
1116 if ((s = RULE_STRCMP(s, "show")) != NULL)
1127 trust_sender_filter = 1;
1128 rule->rflags |= FILTRULE_PERDIR_MERGE
1129 | FILTRULE_FINISH_SETUP;
1132 rule->rflags |= FILTRULE_MERGE_FILE;
1135 rule->rflags |= FILTRULE_INCLUDE;
1140 rule->rflags |= FILTRULE_INCLUDE;
1143 rule->rflags |= FILTRULE_SENDER_SIDE;
1144 prefix_specifies_side = True;
1147 rule->rflags |= FILTRULE_INCLUDE;
1150 rule->rflags |= FILTRULE_RECEIVER_SIDE;
1151 prefix_specifies_side = True;
1154 rule->rflags |= FILTRULE_CLEAR_LIST;
1157 rprintf(FERROR, "Unknown filter rule: `%s'\n", *rulestr_ptr);
1158 exit_cleanup(RERR_SYNTAX);
1160 while (ch != '!' && *++s && *s != ' ' && *s != '_') {
1161 if (template->rflags & FILTRULE_WORD_SPLIT && isspace(*s)) {
1169 "invalid modifier '%c' at position %d in filter rule: %s\n",
1170 *s, (int)(s - (const uchar *)*rulestr_ptr), *rulestr_ptr);
1171 exit_cleanup(RERR_SYNTAX);
1173 if (!BITS_SETnUNSET(rule->rflags, FILTRULE_MERGE_FILE, FILTRULE_NO_PREFIXES))
1175 rule->rflags |= FILTRULE_NO_PREFIXES;
1178 if (!BITS_SETnUNSET(rule->rflags, FILTRULE_MERGE_FILE, FILTRULE_NO_PREFIXES))
1180 rule->rflags |= FILTRULE_NO_PREFIXES
1184 rule->rflags |= FILTRULE_ABS_PATH;
1187 /* Negation really goes with the pattern, so it
1188 * isn't useful as a merge-file default. */
1189 if (rule->rflags & FILTRULE_MERGE_FILE)
1191 rule->rflags |= FILTRULE_NEGATE;
1194 if (rule->rflags & FILTRULE_NO_PREFIXES || prefix_specifies_side)
1196 rule->rflags |= FILTRULE_NO_PREFIXES
1197 | FILTRULE_WORD_SPLIT
1198 | FILTRULE_NO_INHERIT
1199 | FILTRULE_CVS_IGNORE;
1202 if (!(rule->rflags & FILTRULE_MERGE_FILE))
1204 rule->rflags |= FILTRULE_EXCLUDE_SELF;
1207 if (!(rule->rflags & FILTRULE_MERGE_FILE))
1209 rule->rflags |= FILTRULE_NO_INHERIT;
1212 rule->rflags |= FILTRULE_PERISHABLE;
1215 if (prefix_specifies_side)
1217 rule->rflags |= FILTRULE_RECEIVER_SIDE;
1220 if (prefix_specifies_side)
1222 rule->rflags |= FILTRULE_SENDER_SIDE;
1225 if (!(rule->rflags & FILTRULE_MERGE_FILE))
1227 rule->rflags |= FILTRULE_WORD_SPLIT;
1230 rule->rflags |= FILTRULE_XATTR;
1231 saw_xattr_filter = 1;
1238 if (template->rflags & FILTRULES_SIDES) {
1239 if (rule->rflags & FILTRULES_SIDES) {
1240 /* The filter and template both specify side(s). This
1241 * is dodgy (and won't work correctly if the template is
1242 * a one-sided per-dir merge rule), so reject it. */
1244 "specified-side merge file contains specified-side filter: %s\n",
1246 exit_cleanup(RERR_SYNTAX);
1248 rule->rflags |= template->rflags & FILTRULES_SIDES;
1251 if (template->rflags & FILTRULE_WORD_SPLIT) {
1252 const uchar *cp = s;
1253 /* Token ends at whitespace or the end of the string. */
1254 while (!isspace(*cp) && *cp != '\0')
1258 len = strlen((char*)s);
1260 if (rule->rflags & FILTRULE_CLEAR_LIST) {
1261 if (!(rule->rflags & FILTRULE_NO_PREFIXES)
1262 && !(xflags & XFLG_OLD_PREFIXES) && len) {
1264 "'!' rule has trailing characters: %s\n", *rulestr_ptr);
1265 exit_cleanup(RERR_SYNTAX);
1268 rule->rflags &= ~FILTRULE_CLEAR_LIST;
1269 } else if (!len && !(rule->rflags & FILTRULE_CVS_IGNORE)) {
1270 rprintf(FERROR, "unexpected end of filter rule: %s\n", *rulestr_ptr);
1271 exit_cleanup(RERR_SYNTAX);
1274 /* --delete-excluded turns an un-modified include/exclude into a sender-side rule. */
1276 && !(rule->rflags & (FILTRULES_SIDES|FILTRULE_MERGE_FILE|FILTRULE_PERDIR_MERGE)))
1277 rule->rflags |= FILTRULE_SENDER_SIDE;
1279 *pat_ptr = (const char *)s;
1281 *rulestr_ptr = *pat_ptr + len;
1285 static void get_cvs_excludes(uint32 rflags)
1287 static int initialized = 0;
1288 char *p, fname[MAXPATHLEN];
1294 parse_filter_str(&cvs_filter_list, default_cvsignore(),
1295 rule_template(rflags | (protocol_version >= 30 ? FILTRULE_PERISHABLE : 0)),
1298 p = module_id >= 0 && lp_use_chroot(module_id) ? "/" : getenv("HOME");
1299 if (p && pathjoin(fname, MAXPATHLEN, p, ".cvsignore") < MAXPATHLEN)
1300 parse_filter_file(&cvs_filter_list, fname, rule_template(rflags), 0);
1302 parse_filter_str(&cvs_filter_list, getenv("CVSIGNORE"), rule_template(rflags), 0);
1305 const filter_rule *rule_template(uint32 rflags)
1307 static filter_rule template; /* zero-initialized */
1308 template.rflags = rflags;
1312 void parse_filter_str(filter_rule_list *listp, const char *rulestr,
1313 const filter_rule *template, int xflags)
1317 unsigned int pat_len;
1325 /* Remember that the returned string is NOT '\0' terminated! */
1326 if (!(rule = parse_rule_tok(&rulestr, template, xflags, &pat, &pat_len)))
1329 if (pat_len >= MAXPATHLEN) {
1330 rprintf(FERROR, "discarding over-long filter: %.*s\n",
1337 new_rflags = rule->rflags;
1338 if (new_rflags & FILTRULE_CLEAR_LIST) {
1339 if (DEBUG_GTE(FILTER, 2)) {
1341 "[%s] clearing filter list%s\n",
1342 who_am_i(), listp->debug_type);
1344 pop_filter_list(listp);
1349 if (new_rflags & FILTRULE_MERGE_FILE) {
1354 if (new_rflags & FILTRULE_EXCLUDE_SELF) {
1356 filter_rule *excl_self;
1358 excl_self = new0(filter_rule);
1359 /* Find the beginning of the basename and add an exclude for it. */
1360 for (name = pat + pat_len; name > pat && name[-1] != '/'; name--) {}
1361 add_rule(listp, name, (pat + pat_len) - name, excl_self, 0);
1362 rule->rflags &= ~FILTRULE_EXCLUDE_SELF;
1364 if (new_rflags & FILTRULE_PERDIR_MERGE) {
1365 if (parent_dirscan) {
1367 unsigned int len = pat_len;
1368 if ((p = parse_merge_name(pat, &len, module_dirlen)))
1369 add_rule(listp, p, len, rule, 0);
1376 unsigned int len = pat_len;
1377 if ((p = parse_merge_name(pat, &len, 0)))
1378 parse_filter_file(listp, p, rule, XFLG_FATAL_ERRORS);
1384 add_rule(listp, pat, pat_len, rule, xflags);
1386 if (new_rflags & FILTRULE_CVS_IGNORE
1387 && !(new_rflags & FILTRULE_MERGE_FILE))
1388 get_cvs_excludes(new_rflags);
1392 void parse_filter_file(filter_rule_list *listp, const char *fname, const filter_rule *template, int xflags)
1395 char line[BIGPATHBUFLEN];
1396 char *eob = line + sizeof line - 1;
1397 BOOL word_split = (template->rflags & FILTRULE_WORD_SPLIT) != 0;
1399 if (!fname || !*fname)
1402 if (*fname != '-' || fname[1] || am_server) {
1403 if (daemon_filter_list.head) {
1404 strlcpy(line, fname, sizeof line);
1405 clean_fname(line, CFN_COLLAPSE_DOT_DOT_DIRS);
1406 if (check_filter(&daemon_filter_list, FLOG, line, 0) < 0)
1409 fp = fopen(line, "rb");
1411 fp = fopen(fname, "rb");
1415 if (DEBUG_GTE(FILTER, 2)) {
1416 rprintf(FINFO, "[%s] parse_filter_file(%s,%x,%x)%s\n",
1417 who_am_i(), fname, template->rflags, xflags,
1418 fp ? "" : " [not found]");
1422 if (xflags & XFLG_FATAL_ERRORS) {
1423 rsyserr(FERROR, errno,
1424 "failed to open %sclude file %s",
1425 template->rflags & FILTRULE_INCLUDE ? "in" : "ex",
1427 exit_cleanup(RERR_FILEIO);
1431 dirbuf[dirbuf_len] = '\0';
1435 int ch, overflow = 0;
1437 if ((ch = getc(fp)) == EOF) {
1438 if (ferror(fp) && errno == EINTR) {
1444 if (word_split && isspace(ch))
1446 if (eol_nulls? !ch : (ch == '\n' || ch == '\r'))
1454 rprintf(FERROR, "discarding over-long filter: %s...\n", line);
1458 /* Skip an empty token and (when line parsing) comments. */
1459 if (*line && (word_split || (*line != ';' && *line != '#')))
1460 parse_filter_str(listp, line, template, xflags);
1467 /* If the "for_xfer" flag is set, the prefix is made compatible with the
1468 * current protocol_version (if possible) or a NULL is returned (if not
1470 char *get_rule_prefix(filter_rule *rule, const char *pat, int for_xfer,
1471 unsigned int *plen_ptr)
1473 static char buf[MAX_RULE_PREFIX+1];
1475 int legal_len = for_xfer && protocol_version < 29 ? 1 : MAX_RULE_PREFIX-1;
1477 if (rule->rflags & FILTRULE_PERDIR_MERGE) {
1481 } else if (rule->rflags & FILTRULE_INCLUDE)
1483 else if (legal_len != 1
1484 || ((*pat == '-' || *pat == '+') && pat[1] == ' '))
1489 if (rule->rflags & FILTRULE_ABS_PATH)
1491 if (rule->rflags & FILTRULE_NEGATE)
1493 if (rule->rflags & FILTRULE_CVS_IGNORE)
1496 if (rule->rflags & FILTRULE_NO_INHERIT)
1498 if (rule->rflags & FILTRULE_WORD_SPLIT)
1500 if (rule->rflags & FILTRULE_NO_PREFIXES) {
1501 if (rule->rflags & FILTRULE_INCLUDE)
1507 if (rule->rflags & FILTRULE_EXCLUDE_SELF)
1509 if (rule->rflags & FILTRULE_XATTR)
1511 if (rule->rflags & FILTRULE_SENDER_SIDE
1512 && (!for_xfer || protocol_version >= 29))
1514 if (rule->rflags & FILTRULE_RECEIVER_SIDE
1515 && (!for_xfer || protocol_version >= 29
1516 || (delete_excluded && am_sender)))
1518 if (rule->rflags & FILTRULE_PERISHABLE) {
1519 if (!for_xfer || protocol_version >= 30)
1524 if (op - buf > legal_len)
1530 *plen_ptr = op - buf;
1534 static void send_rules(int f_out, filter_rule_list *flp)
1536 filter_rule *ent, *prev = NULL;
1538 for (ent = flp->head; ent; ent = ent->next) {
1539 unsigned int len, plen, dlen;
1543 /* Note we need to check delete_excluded here in addition to
1544 * the code in parse_rule_tok() because some rules may have
1545 * been added before we found the --delete-excluded option.
1546 * We must also elide any CVS merge-file rules to avoid a
1547 * backward compatibility problem, and we elide any no-prefix
1548 * merge files as an optimization (since they can only have
1549 * include/exclude rules). */
1550 if (ent->rflags & FILTRULE_SENDER_SIDE)
1551 elide = am_sender ? 1 : -1;
1552 if (ent->rflags & FILTRULE_RECEIVER_SIDE)
1553 elide = elide ? 0 : am_sender ? -1 : 1;
1554 else if (delete_excluded && !elide
1555 && (!(ent->rflags & FILTRULE_PERDIR_MERGE)
1556 || ent->rflags & FILTRULE_NO_PREFIXES))
1557 elide = am_sender ? 1 : -1;
1560 prev->next = ent->next;
1562 flp->head = ent->next;
1567 if (ent->rflags & FILTRULE_CVS_IGNORE
1568 && !(ent->rflags & FILTRULE_MERGE_FILE)) {
1569 int f = am_sender || protocol_version < 29 ? f_out : -2;
1570 send_rules(f, &cvs_filter_list);
1574 p = get_rule_prefix(ent, ent->pattern, 1, &plen);
1577 "filter rules are too modern for remote rsync.\n");
1578 exit_cleanup(RERR_PROTOCOL);
1582 len = strlen(ent->pattern);
1583 dlen = ent->rflags & FILTRULE_DIRECTORY ? 1 : 0;
1584 if (!(plen + len + dlen))
1586 write_int(f_out, plen + len + dlen);
1588 write_buf(f_out, p, plen);
1589 write_buf(f_out, ent->pattern, len);
1591 write_byte(f_out, '/');
1596 /* This is only called by the client. */
1597 void send_filter_list(int f_out)
1599 int receiver_wants_list = prune_empty_dirs
1600 || (delete_mode && (!delete_excluded || protocol_version >= 29));
1602 if (local_server || (am_sender && !receiver_wants_list))
1604 if (cvs_exclude && am_sender) {
1605 if (protocol_version >= 29)
1606 parse_filter_str(&filter_list, ":C", rule_template(0), 0);
1607 parse_filter_str(&filter_list, "-C", rule_template(0), 0);
1610 send_rules(f_out, &filter_list);
1613 write_int(f_out, 0);
1616 if (!am_sender || protocol_version < 29)
1617 parse_filter_str(&filter_list, ":C", rule_template(0), 0);
1619 parse_filter_str(&filter_list, "-C", rule_template(0), 0);
1623 /* This is only called by the server. */
1624 void recv_filter_list(int f_in)
1626 char line[BIGPATHBUFLEN];
1627 int xflags = protocol_version >= 29 ? 0 : XFLG_OLD_PREFIXES;
1628 int receiver_wants_list = prune_empty_dirs
1629 || (delete_mode && (!delete_excluded || protocol_version >= 29));
1632 if (!local_server && (am_sender || receiver_wants_list)) {
1633 while ((len = read_int(f_in)) != 0) {
1634 if (len >= sizeof line)
1635 overflow_exit("recv_rules");
1636 read_sbuf(f_in, line, len);
1637 parse_filter_str(&filter_list, line, rule_template(0), xflags);
1642 if (local_server || am_sender || protocol_version < 29)
1643 parse_filter_str(&filter_list, ":C", rule_template(0), 0);
1644 if (local_server || am_sender)
1645 parse_filter_str(&filter_list, "-C", rule_template(0), 0);
1648 if (local_server) /* filter out any rules that aren't for us. */
1649 send_rules(-1, &filter_list);