Fix a comment.
[rsync/qnx.git] / exclude.c
blob16b2a562fbbbc3733473fb18bec6beb9cb9f0f01
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-2009 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 /* Need room enough for ":MODS " prefix plus some room to grow. */
48 #define MAX_RULE_PREFIX (16)
50 #define SLASH_WILD3_SUFFIX "/***"
52 /* The dirbuf is set by push_local_filters() to the current subdirectory
53 * relative to curr_dir that is being processed. The path always has a
54 * trailing slash appended, and the variable dirbuf_len contains the length
55 * of this path prefix. The path is always absolute. */
56 static char dirbuf[MAXPATHLEN+1];
57 static unsigned int dirbuf_len = 0;
58 static int dirbuf_depth;
60 /* This is True when we're scanning parent dirs for per-dir merge-files. */
61 static BOOL parent_dirscan = False;
63 /* This array contains a list of all the currently active per-dir merge
64 * files. This makes it easier to save the appropriate values when we
65 * "push" down into each subdirectory. */
66 static filter_rule **mergelist_parents;
67 static int mergelist_cnt = 0;
68 static int mergelist_size = 0;
70 /* Each filter_list_struct describes a singly-linked list by keeping track
71 * of both the head and tail pointers. The list is slightly unusual in that
72 * a parent-dir's content can be appended to the end of the local list in a
73 * special way: the last item in the local list has its "next" pointer set
74 * to point to the inherited list, but the local list's tail pointer points
75 * at the end of the local list. Thus, if the local list is empty, the head
76 * will be pointing at the inherited content but the tail will be NULL. To
77 * help you visualize this, here are the possible list arrangements:
79 * Completely Empty Local Content Only
80 * ================================== ====================================
81 * head -> NULL head -> Local1 -> Local2 -> NULL
82 * tail -> NULL tail -------------^
84 * Inherited Content Only Both Local and Inherited Content
85 * ================================== ====================================
86 * head -> Parent1 -> Parent2 -> NULL head -> L1 -> L2 -> P1 -> P2 -> NULL
87 * tail -> NULL tail ---------^
89 * This means that anyone wanting to traverse the whole list to use it just
90 * needs to start at the head and use the "next" pointers until it goes
91 * NULL. To add new local content, we insert the item after the tail item
92 * and update the tail (obviously, if "tail" was NULL, we insert it at the
93 * head). To clear the local list, WE MUST NOT FREE THE INHERITED CONTENT
94 * because it is shared between the current list and our parent list(s).
95 * The easiest way to handle this is to simply truncate the list after the
96 * tail item and then free the local list from the head. When inheriting
97 * the list for a new local dir, we just save off the filter_list_struct
98 * values (so we can pop back to them later) and set the tail to NULL.
101 static void teardown_mergelist(filter_rule *ex)
103 if (DEBUG_GTE(FILTER, 2)) {
104 rprintf(FINFO, "[%s] deactivating mergelist #%d%s\n",
105 who_am_i(), mergelist_cnt - 1,
106 ex->u.mergelist->debug_type);
109 /* We should deactivate mergelists in LIFO order. */
110 assert(mergelist_cnt > 0);
111 assert(ex == mergelist_parents[mergelist_cnt - 1]);
113 /* The parent_dirscan filters should have been freed. */
114 assert(ex->u.mergelist->parent_dirscan_head == NULL);
116 free(ex->u.mergelist->debug_type);
117 free(ex->u.mergelist);
118 mergelist_cnt--;
121 static void free_filter(filter_rule *ex)
123 free(ex->pattern);
124 free(ex);
127 static void free_filters(filter_rule *head)
129 filter_rule *rev_head = NULL;
131 /* Reverse the list so we deactivate mergelists in the proper LIFO
132 * order. */
133 while (head) {
134 filter_rule *next = head->next;
135 head->next = rev_head;
136 rev_head = head;
137 head = next;
140 while (rev_head) {
141 filter_rule *prev = rev_head->next;
142 /* Tear down mergelists here, not in free_filter, so that we
143 * affect only real filter lists and not temporarily allocated
144 * filters. */
145 if (rev_head->rflags & FILTRULE_PERDIR_MERGE)
146 teardown_mergelist(rev_head);
147 free_filter(rev_head);
148 rev_head = prev;
152 /* Build a filter structure given a filter pattern. The value in "pat"
153 * is not null-terminated. "rule" is either held or freed, so the
154 * caller should not free it. */
155 static void add_rule(filter_rule_list *listp, const char *pat, unsigned int pat_len,
156 filter_rule *rule, int xflags)
158 const char *cp;
159 unsigned int pre_len, suf_len, slash_cnt = 0;
161 if (DEBUG_GTE(FILTER, 2)) {
162 rprintf(FINFO, "[%s] add_rule(%s%.*s%s)%s\n",
163 who_am_i(), get_rule_prefix(rule, pat, 0, NULL),
164 (int)pat_len, pat,
165 (rule->rflags & FILTRULE_DIRECTORY) ? "/" : "",
166 listp->debug_type);
169 /* These flags also indicate that we're reading a list that
170 * needs to be filtered now, not post-filtered later. */
171 if (xflags & (XFLG_ANCHORED2ABS|XFLG_ABS_IF_SLASH)
172 && (rule->rflags & FILTRULES_SIDES)
173 == (am_sender ? FILTRULE_RECEIVER_SIDE : FILTRULE_SENDER_SIDE)) {
174 /* This filter applies only to the other side. Drop it. */
175 free_filter(rule);
176 return;
179 if (pat_len > 1 && pat[pat_len-1] == '/') {
180 pat_len--;
181 rule->rflags |= FILTRULE_DIRECTORY;
184 for (cp = pat; cp < pat + pat_len; cp++) {
185 if (*cp == '/')
186 slash_cnt++;
189 if (!(rule->rflags & (FILTRULE_ABS_PATH | FILTRULE_MERGE_FILE))
190 && ((xflags & (XFLG_ANCHORED2ABS|XFLG_ABS_IF_SLASH) && *pat == '/')
191 || (xflags & XFLG_ABS_IF_SLASH && slash_cnt))) {
192 rule->rflags |= FILTRULE_ABS_PATH;
193 if (*pat == '/')
194 pre_len = dirbuf_len - module_dirlen - 1;
195 else
196 pre_len = 0;
197 } else
198 pre_len = 0;
200 /* The daemon wants dir-exclude rules to get an appended "/" + "***". */
201 if (xflags & XFLG_DIR2WILD3
202 && BITS_SETnUNSET(rule->rflags, FILTRULE_DIRECTORY, FILTRULE_INCLUDE)) {
203 rule->rflags &= ~FILTRULE_DIRECTORY;
204 suf_len = sizeof SLASH_WILD3_SUFFIX - 1;
205 } else
206 suf_len = 0;
208 if (!(rule->pattern = new_array(char, pre_len + pat_len + suf_len + 1)))
209 out_of_memory("add_rule");
210 if (pre_len) {
211 memcpy(rule->pattern, dirbuf + module_dirlen, pre_len);
212 for (cp = rule->pattern; cp < rule->pattern + pre_len; cp++) {
213 if (*cp == '/')
214 slash_cnt++;
217 strlcpy(rule->pattern + pre_len, pat, pat_len + 1);
218 pat_len += pre_len;
219 if (suf_len) {
220 memcpy(rule->pattern + pat_len, SLASH_WILD3_SUFFIX, suf_len+1);
221 pat_len += suf_len;
222 slash_cnt++;
225 if (strpbrk(rule->pattern, "*[?")) {
226 rule->rflags |= FILTRULE_WILD;
227 if ((cp = strstr(rule->pattern, "**")) != NULL) {
228 rule->rflags |= FILTRULE_WILD2;
229 /* If the pattern starts with **, note that. */
230 if (cp == rule->pattern)
231 rule->rflags |= FILTRULE_WILD2_PREFIX;
232 /* If the pattern ends with ***, note that. */
233 if (pat_len >= 3
234 && rule->pattern[pat_len-3] == '*'
235 && rule->pattern[pat_len-2] == '*'
236 && rule->pattern[pat_len-1] == '*')
237 rule->rflags |= FILTRULE_WILD3_SUFFIX;
241 if (rule->rflags & FILTRULE_PERDIR_MERGE) {
242 filter_rule_list *lp;
243 unsigned int len;
244 int i;
246 if ((cp = strrchr(rule->pattern, '/')) != NULL)
247 cp++;
248 else
249 cp = rule->pattern;
251 /* If the local merge file was already mentioned, don't
252 * add it again. */
253 for (i = 0; i < mergelist_cnt; i++) {
254 filter_rule *ex = mergelist_parents[i];
255 const char *s = strrchr(ex->pattern, '/');
256 if (s)
257 s++;
258 else
259 s = ex->pattern;
260 len = strlen(s);
261 if (len == pat_len - (cp - rule->pattern) && memcmp(s, cp, len) == 0) {
262 free_filter(rule);
263 return;
267 if (!(lp = new_array(filter_rule_list, 1)))
268 out_of_memory("add_rule");
269 lp->head = lp->tail = lp->parent_dirscan_head = NULL;
270 if (asprintf(&lp->debug_type, " [per-dir %s]", cp) < 0)
271 out_of_memory("add_rule");
272 rule->u.mergelist = lp;
274 if (mergelist_cnt == mergelist_size) {
275 mergelist_size += 5;
276 mergelist_parents = realloc_array(mergelist_parents,
277 filter_rule *,
278 mergelist_size);
279 if (!mergelist_parents)
280 out_of_memory("add_rule");
282 if (DEBUG_GTE(FILTER, 2)) {
283 rprintf(FINFO, "[%s] activating mergelist #%d%s\n",
284 who_am_i(), mergelist_cnt, lp->debug_type);
286 mergelist_parents[mergelist_cnt++] = rule;
287 } else
288 rule->u.slash_cnt = slash_cnt;
290 if (!listp->tail) {
291 rule->next = listp->head;
292 listp->head = listp->tail = rule;
293 } else {
294 rule->next = listp->tail->next;
295 listp->tail->next = rule;
296 listp->tail = rule;
300 static void clear_filter_list(filter_rule_list *listp)
302 if (listp->tail) {
303 /* Truncate any inherited items from the local list. */
304 listp->tail->next = NULL;
305 /* Now free everything that is left. */
306 free_filters(listp->head);
309 listp->head = listp->tail = NULL;
312 /* This returns an expanded (absolute) filename for the merge-file name if
313 * the name has any slashes in it OR if the parent_dirscan var is True;
314 * otherwise it returns the original merge_file name. If the len_ptr value
315 * is non-NULL the merge_file name is limited by the referenced length
316 * value and will be updated with the length of the resulting name. We
317 * always return a name that is null terminated, even if the merge_file
318 * name was not. */
319 static char *parse_merge_name(const char *merge_file, unsigned int *len_ptr,
320 unsigned int prefix_skip)
322 static char buf[MAXPATHLEN];
323 char *fn, tmpbuf[MAXPATHLEN];
324 unsigned int fn_len;
326 if (!parent_dirscan && *merge_file != '/') {
327 /* Return the name unchanged it doesn't have any slashes. */
328 if (len_ptr) {
329 const char *p = merge_file + *len_ptr;
330 while (--p > merge_file && *p != '/') {}
331 if (p == merge_file) {
332 strlcpy(buf, merge_file, *len_ptr + 1);
333 return buf;
335 } else if (strchr(merge_file, '/') == NULL)
336 return (char *)merge_file;
339 fn = *merge_file == '/' ? buf : tmpbuf;
340 if (sanitize_paths) {
341 const char *r = prefix_skip ? "/" : NULL;
342 /* null-terminate the name if it isn't already */
343 if (len_ptr && merge_file[*len_ptr]) {
344 char *to = fn == buf ? tmpbuf : buf;
345 strlcpy(to, merge_file, *len_ptr + 1);
346 merge_file = to;
348 if (!sanitize_path(fn, merge_file, r, dirbuf_depth, SP_DEFAULT)) {
349 rprintf(FERROR, "merge-file name overflows: %s\n",
350 merge_file);
351 return NULL;
353 fn_len = strlen(fn);
354 } else {
355 strlcpy(fn, merge_file, len_ptr ? *len_ptr + 1 : MAXPATHLEN);
356 fn_len = clean_fname(fn, CFN_COLLAPSE_DOT_DOT_DIRS);
359 /* If the name isn't in buf yet, it's wasn't absolute. */
360 if (fn != buf) {
361 int d_len = dirbuf_len - prefix_skip;
362 if (d_len + fn_len >= MAXPATHLEN) {
363 rprintf(FERROR, "merge-file name overflows: %s\n", fn);
364 return NULL;
366 memcpy(buf, dirbuf + prefix_skip, d_len);
367 memcpy(buf + d_len, fn, fn_len + 1);
368 fn_len = clean_fname(buf, CFN_COLLAPSE_DOT_DOT_DIRS);
371 if (len_ptr)
372 *len_ptr = fn_len;
373 return buf;
376 /* Sets the dirbuf and dirbuf_len values. */
377 void set_filter_dir(const char *dir, unsigned int dirlen)
379 unsigned int len;
380 if (*dir != '/') {
381 memcpy(dirbuf, curr_dir, curr_dir_len);
382 dirbuf[curr_dir_len] = '/';
383 len = curr_dir_len + 1;
384 if (len + dirlen >= MAXPATHLEN)
385 dirlen = 0;
386 } else
387 len = 0;
388 memcpy(dirbuf + len, dir, dirlen);
389 dirbuf[dirlen + len] = '\0';
390 dirbuf_len = clean_fname(dirbuf, CFN_COLLAPSE_DOT_DOT_DIRS);
391 if (dirbuf_len > 1 && dirbuf[dirbuf_len-1] == '.'
392 && dirbuf[dirbuf_len-2] == '/')
393 dirbuf_len -= 2;
394 if (dirbuf_len != 1)
395 dirbuf[dirbuf_len++] = '/';
396 dirbuf[dirbuf_len] = '\0';
397 if (sanitize_paths)
398 dirbuf_depth = count_dir_elements(dirbuf + module_dirlen);
401 /* This routine takes a per-dir merge-file entry and finishes its setup.
402 * If the name has a path portion then we check to see if it refers to a
403 * parent directory of the first transfer dir. If it does, we scan all the
404 * dirs from that point through the parent dir of the transfer dir looking
405 * for the per-dir merge-file in each one. */
406 static BOOL setup_merge_file(int mergelist_num, filter_rule *ex,
407 filter_rule_list *lp)
409 char buf[MAXPATHLEN];
410 char *x, *y, *pat = ex->pattern;
411 unsigned int len;
413 if (!(x = parse_merge_name(pat, NULL, 0)) || *x != '/')
414 return 0;
416 if (DEBUG_GTE(FILTER, 2)) {
417 rprintf(FINFO, "[%s] performing parent_dirscan for mergelist #%d%s\n",
418 who_am_i(), mergelist_num, lp->debug_type);
420 y = strrchr(x, '/');
421 *y = '\0';
422 ex->pattern = strdup(y+1);
423 if (!*x)
424 x = "/";
425 if (*x == '/')
426 strlcpy(buf, x, MAXPATHLEN);
427 else
428 pathjoin(buf, MAXPATHLEN, dirbuf, x);
430 len = clean_fname(buf, CFN_COLLAPSE_DOT_DOT_DIRS);
431 if (len != 1 && len < MAXPATHLEN-1) {
432 buf[len++] = '/';
433 buf[len] = '\0';
435 /* This ensures that the specified dir is a parent of the transfer. */
436 for (x = buf, y = dirbuf; *x && *x == *y; x++, y++) {}
437 if (*x)
438 y += strlen(y); /* nope -- skip the scan */
440 parent_dirscan = True;
441 while (*y) {
442 char save[MAXPATHLEN];
443 strlcpy(save, y, MAXPATHLEN);
444 *y = '\0';
445 dirbuf_len = y - dirbuf;
446 strlcpy(x, ex->pattern, MAXPATHLEN - (x - buf));
447 parse_filter_file(lp, buf, ex, XFLG_ANCHORED2ABS);
448 if (ex->rflags & FILTRULE_NO_INHERIT) {
449 /* Free the undesired rules to clean up any per-dir
450 * mergelists they defined. Otherwise pop_local_filters
451 * may crash trying to restore nonexistent state for
452 * those mergelists. */
453 free_filters(lp->head);
454 lp->head = NULL;
456 lp->tail = NULL;
457 strlcpy(y, save, MAXPATHLEN);
458 while ((*x++ = *y++) != '/') {}
460 /* Save current head for freeing when the mergelist becomes inactive. */
461 lp->parent_dirscan_head = lp->head;
462 parent_dirscan = False;
463 if (DEBUG_GTE(FILTER, 2)) {
464 rprintf(FINFO, "[%s] completed parent_dirscan for mergelist #%d%s\n",
465 who_am_i(), mergelist_num, lp->debug_type);
467 free(pat);
468 return 1;
471 struct local_filter_state {
472 int mergelist_cnt;
473 filter_rule_list mergelists[1];
476 /* Each time rsync changes to a new directory it call this function to
477 * handle all the per-dir merge-files. The "dir" value is the current path
478 * relative to curr_dir (which might not be null-terminated). We copy it
479 * into dirbuf so that we can easily append a file name on the end. */
480 void *push_local_filters(const char *dir, unsigned int dirlen)
482 struct local_filter_state *push;
483 int i;
485 set_filter_dir(dir, dirlen);
486 if (DEBUG_GTE(FILTER, 2)) {
487 rprintf(FINFO, "[%s] pushing local filters for %s\n",
488 who_am_i(), dirbuf);
491 if (!mergelist_cnt) {
492 /* No old state to save and no new merge files to push. */
493 return NULL;
496 push = (struct local_filter_state *)new_array(char,
497 sizeof (struct local_filter_state)
498 + (mergelist_cnt-1) * sizeof (filter_rule_list));
499 if (!push)
500 out_of_memory("push_local_filters");
502 push->mergelist_cnt = mergelist_cnt;
503 for (i = 0; i < mergelist_cnt; i++) {
504 memcpy(&push->mergelists[i], mergelist_parents[i]->u.mergelist,
505 sizeof (filter_rule_list));
508 /* Note: parse_filter_file() might increase mergelist_cnt, so keep
509 * this loop separate from the above loop. */
510 for (i = 0; i < mergelist_cnt; i++) {
511 filter_rule *ex = mergelist_parents[i];
512 filter_rule_list *lp = ex->u.mergelist;
514 if (DEBUG_GTE(FILTER, 2)) {
515 rprintf(FINFO, "[%s] pushing mergelist #%d%s\n",
516 who_am_i(), i, lp->debug_type);
519 lp->tail = NULL; /* Switch any local rules to inherited. */
520 if (ex->rflags & FILTRULE_NO_INHERIT)
521 lp->head = NULL;
523 if (ex->rflags & FILTRULE_FINISH_SETUP) {
524 ex->rflags &= ~FILTRULE_FINISH_SETUP;
525 if (setup_merge_file(i, ex, lp))
526 set_filter_dir(dir, dirlen);
529 if (strlcpy(dirbuf + dirbuf_len, ex->pattern,
530 MAXPATHLEN - dirbuf_len) < MAXPATHLEN - dirbuf_len) {
531 parse_filter_file(lp, dirbuf, ex,
532 XFLG_ANCHORED2ABS);
533 } else {
534 io_error |= IOERR_GENERAL;
535 rprintf(FERROR,
536 "cannot add local filter rules in long-named directory: %s\n",
537 full_fname(dirbuf));
539 dirbuf[dirbuf_len] = '\0';
542 return (void*)push;
545 void pop_local_filters(void *mem)
547 struct local_filter_state *pop = (struct local_filter_state *)mem;
548 int i;
549 int old_mergelist_cnt = pop ? pop->mergelist_cnt : 0;
551 if (DEBUG_GTE(FILTER, 2))
552 rprintf(FINFO, "[%s] popping local filters\n", who_am_i());
554 for (i = mergelist_cnt; i-- > 0; ) {
555 filter_rule *ex = mergelist_parents[i];
556 filter_rule_list *lp = ex->u.mergelist;
558 if (DEBUG_GTE(FILTER, 2)) {
559 rprintf(FINFO, "[%s] popping mergelist #%d%s\n",
560 who_am_i(), i, lp->debug_type);
563 clear_filter_list(lp);
565 if (i >= old_mergelist_cnt) {
566 /* This mergelist does not exist in the state to be
567 * restored. Free its parent_dirscan list to clean up
568 * any per-dir mergelists defined there so we don't
569 * crash trying to restore nonexistent state for them
570 * below. (Counterpart to setup_merge_file call in
571 * push_local_filters. Must be done here, not in
572 * free_filter, for LIFO order.) */
573 if (DEBUG_GTE(FILTER, 2)) {
574 rprintf(FINFO, "[%s] freeing parent_dirscan filters of mergelist #%d%s\n",
575 who_am_i(), i, ex->u.mergelist->debug_type);
577 free_filters(lp->parent_dirscan_head);
578 lp->parent_dirscan_head = NULL;
582 /* If we cleaned things up properly, the only still-active mergelists
583 * should be those with a state to be restored. */
584 assert(mergelist_cnt == old_mergelist_cnt);
586 if (!pop) {
587 /* No state to restore. */
588 return;
591 for (i = 0; i < mergelist_cnt; i++) {
592 memcpy(mergelist_parents[i]->u.mergelist, &pop->mergelists[i],
593 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_is_dir)
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 (!ex->u.slash_cnt && !(ex->rflags & FILTRULE_WILD2)) {
639 /* If the pattern does not have any slashes AND it does
640 * not have a "**" (which could match a slash), then we
641 * just match the name portion of the path. */
642 if ((p = strrchr(name,'/')) != NULL)
643 name = p+1;
644 } else if (ex->rflags & FILTRULE_ABS_PATH && *fname != '/'
645 && curr_dir_len > module_dirlen + 1) {
646 /* If we're matching against an absolute-path pattern,
647 * we need to prepend our full path info. */
648 strings[str_cnt++] = curr_dir + module_dirlen + 1;
649 strings[str_cnt++] = "/";
650 } else if (ex->rflags & FILTRULE_WILD2_PREFIX && *fname != '/') {
651 /* Allow "**"+"/" to match at the start of the string. */
652 strings[str_cnt++] = "/";
654 strings[str_cnt++] = name;
655 if (name_is_dir) {
656 /* Allow a trailing "/"+"***" to match the directory. */
657 if (ex->rflags & FILTRULE_WILD3_SUFFIX)
658 strings[str_cnt++] = "/";
659 } else if (ex->rflags & FILTRULE_DIRECTORY)
660 return !ret_match;
661 strings[str_cnt] = NULL;
663 if (*pattern == '/') {
664 anchored_match = 1;
665 pattern++;
668 if (!anchored_match && ex->u.slash_cnt
669 && !(ex->rflags & FILTRULE_WILD2)) {
670 /* A non-anchored match with an infix slash and no "**"
671 * needs to match the last slash_cnt+1 name elements. */
672 slash_handling = ex->u.slash_cnt + 1;
673 } else if (!anchored_match && !(ex->rflags & FILTRULE_WILD2_PREFIX)
674 && ex->rflags & FILTRULE_WILD2) {
675 /* A non-anchored match with an infix or trailing "**" (but not
676 * a prefixed "**") needs to try matching after every slash. */
677 slash_handling = -1;
678 } else {
679 /* The pattern matches only at the start of the path or name. */
680 slash_handling = 0;
683 if (ex->rflags & FILTRULE_WILD) {
684 if (wildmatch_array(pattern, strings, slash_handling))
685 return ret_match;
686 } else if (str_cnt > 1) {
687 if (litmatch_array(pattern, strings, slash_handling))
688 return ret_match;
689 } else if (anchored_match) {
690 if (strcmp(name, pattern) == 0)
691 return ret_match;
692 } else {
693 int l1 = strlen(name);
694 int l2 = strlen(pattern);
695 if (l2 <= l1 &&
696 strcmp(name+(l1-l2),pattern) == 0 &&
697 (l1==l2 || name[l1-(l2+1)] == '/')) {
698 return ret_match;
702 return !ret_match;
705 static void report_filter_result(enum logcode code, char const *name,
706 filter_rule const *ent,
707 int name_is_dir, const char *type)
709 /* If a trailing slash is present to match only directories,
710 * then it is stripped out by add_rule(). So as a special
711 * case we add it back in here. */
713 if (DEBUG_GTE(FILTER, 1)) {
714 static char *actions[2][2]
715 = { {"show", "hid"}, {"risk", "protect"} };
716 const char *w = who_am_i();
717 rprintf(code, "[%s] %sing %s %s because of pattern %s%s%s\n",
718 w, actions[*w!='s'][!(ent->rflags & FILTRULE_INCLUDE)],
719 name_is_dir ? "directory" : "file", name, ent->pattern,
720 ent->rflags & FILTRULE_DIRECTORY ? "/" : "", type);
724 /* Return -1 if file "name" is defined to be excluded by the specified
725 * exclude list, 1 if it is included, and 0 if it was not matched. */
726 int check_filter(filter_rule_list *listp, enum logcode code,
727 const char *name, int name_is_dir)
729 filter_rule *ent;
731 for (ent = listp->head; ent; ent = ent->next) {
732 if (ignore_perishable && ent->rflags & FILTRULE_PERISHABLE)
733 continue;
734 if (ent->rflags & FILTRULE_PERDIR_MERGE) {
735 int rc = check_filter(ent->u.mergelist, code, name,
736 name_is_dir);
737 if (rc)
738 return rc;
739 continue;
741 if (ent->rflags & FILTRULE_CVS_IGNORE) {
742 int rc = check_filter(&cvs_filter_list, code, name,
743 name_is_dir);
744 if (rc)
745 return rc;
746 continue;
748 if (rule_matches(name, ent, name_is_dir)) {
749 report_filter_result(code, name, ent, name_is_dir,
750 listp->debug_type);
751 return ent->rflags & FILTRULE_INCLUDE ? 1 : -1;
755 return 0;
758 #define RULE_STRCMP(s,r) rule_strcmp((s), (r), sizeof (r) - 1)
760 static const uchar *rule_strcmp(const uchar *str, const char *rule, int rule_len)
762 if (strncmp((char*)str, rule, rule_len) != 0)
763 return NULL;
764 if (isspace(str[rule_len]) || str[rule_len] == '_' || !str[rule_len])
765 return str + rule_len - 1;
766 if (str[rule_len] == ',')
767 return str + rule_len;
768 return NULL;
771 #define FILTRULES_FROM_CONTAINER (FILTRULE_ABS_PATH | FILTRULE_INCLUDE \
772 | FILTRULE_DIRECTORY | FILTRULE_NEGATE \
773 | FILTRULE_PERISHABLE)
775 /* Gets the next include/exclude rule from *rulestr_ptr and advances
776 * *rulestr_ptr to point beyond it. Stores the pattern's start (within
777 * *rulestr_ptr) and length in *pat_ptr and *pat_len_ptr, and returns a newly
778 * allocated filter_rule containing the rest of the information. Returns
779 * NULL if there are no more rules in the input.
781 * The template provides defaults for the new rule to inherit, and the
782 * template rflags and the xflags additionally affect parsing. */
783 static filter_rule *parse_rule_tok(const char **rulestr_ptr,
784 const filter_rule *template, int xflags,
785 const char **pat_ptr, unsigned int *pat_len_ptr)
787 const uchar *s = (const uchar *)*rulestr_ptr;
788 filter_rule *rule;
789 unsigned int len;
791 if (template->rflags & FILTRULE_WORD_SPLIT) {
792 /* Skip over any initial whitespace. */
793 while (isspace(*s))
794 s++;
795 /* Update to point to real start of rule. */
796 *rulestr_ptr = (const char *)s;
798 if (!*s)
799 return NULL;
801 if (!(rule = new0(filter_rule)))
802 out_of_memory("parse_rule_tok");
804 /* Inherit from the template. Don't inherit FILTRULES_SIDES; we check
805 * that later. */
806 rule->rflags = template->rflags & FILTRULES_FROM_CONTAINER;
808 /* Figure out what kind of a filter rule "s" is pointing at. Note
809 * that if FILTRULE_NO_PREFIXES is set, the rule is either an include
810 * or an exclude based on the inheritance of the FILTRULE_INCLUDE
811 * flag (above). XFLG_OLD_PREFIXES indicates a compatibility mode
812 * for old include/exclude patterns where just "+ " and "- " are
813 * allowed as optional prefixes. */
814 if (template->rflags & FILTRULE_NO_PREFIXES) {
815 if (*s == '!' && template->rflags & FILTRULE_CVS_IGNORE)
816 rule->rflags |= FILTRULE_CLEAR_LIST; /* Tentative! */
817 } else if (xflags & XFLG_OLD_PREFIXES) {
818 if (*s == '-' && s[1] == ' ') {
819 rule->rflags &= ~FILTRULE_INCLUDE;
820 s += 2;
821 } else if (*s == '+' && s[1] == ' ') {
822 rule->rflags |= FILTRULE_INCLUDE;
823 s += 2;
824 } else if (*s == '!')
825 rule->rflags |= FILTRULE_CLEAR_LIST; /* Tentative! */
826 } else {
827 char ch = 0;
828 BOOL prefix_specifies_side = False;
829 switch (*s) {
830 case 'c':
831 if ((s = RULE_STRCMP(s, "clear")) != NULL)
832 ch = '!';
833 break;
834 case 'd':
835 if ((s = RULE_STRCMP(s, "dir-merge")) != NULL)
836 ch = ':';
837 break;
838 case 'e':
839 if ((s = RULE_STRCMP(s, "exclude")) != NULL)
840 ch = '-';
841 break;
842 case 'h':
843 if ((s = RULE_STRCMP(s, "hide")) != NULL)
844 ch = 'H';
845 break;
846 case 'i':
847 if ((s = RULE_STRCMP(s, "include")) != NULL)
848 ch = '+';
849 break;
850 case 'm':
851 if ((s = RULE_STRCMP(s, "merge")) != NULL)
852 ch = '.';
853 break;
854 case 'p':
855 if ((s = RULE_STRCMP(s, "protect")) != NULL)
856 ch = 'P';
857 break;
858 case 'r':
859 if ((s = RULE_STRCMP(s, "risk")) != NULL)
860 ch = 'R';
861 break;
862 case 's':
863 if ((s = RULE_STRCMP(s, "show")) != NULL)
864 ch = 'S';
865 break;
866 default:
867 ch = *s;
868 if (s[1] == ',')
869 s++;
870 break;
872 switch (ch) {
873 case ':':
874 rule->rflags |= FILTRULE_PERDIR_MERGE
875 | FILTRULE_FINISH_SETUP;
876 /* FALL THROUGH */
877 case '.':
878 rule->rflags |= FILTRULE_MERGE_FILE;
879 break;
880 case '+':
881 rule->rflags |= FILTRULE_INCLUDE;
882 break;
883 case '-':
884 break;
885 case 'S':
886 rule->rflags |= FILTRULE_INCLUDE;
887 /* FALL THROUGH */
888 case 'H':
889 rule->rflags |= FILTRULE_SENDER_SIDE;
890 prefix_specifies_side = True;
891 break;
892 case 'R':
893 rule->rflags |= FILTRULE_INCLUDE;
894 /* FALL THROUGH */
895 case 'P':
896 rule->rflags |= FILTRULE_RECEIVER_SIDE;
897 prefix_specifies_side = True;
898 break;
899 case '!':
900 rule->rflags |= FILTRULE_CLEAR_LIST;
901 break;
902 default:
903 rprintf(FERROR, "Unknown filter rule: `%s'\n", *rulestr_ptr);
904 exit_cleanup(RERR_SYNTAX);
906 while (ch != '!' && *++s && *s != ' ' && *s != '_') {
907 if (template->rflags & FILTRULE_WORD_SPLIT && isspace(*s)) {
908 s--;
909 break;
911 switch (*s) {
912 default:
913 invalid:
914 rprintf(FERROR,
915 "invalid modifier '%c' at position %d in filter rule: %s\n",
916 *s, (int)(s - (const uchar *)*rulestr_ptr), *rulestr_ptr);
917 exit_cleanup(RERR_SYNTAX);
918 case '-':
919 if (!BITS_SETnUNSET(rule->rflags, FILTRULE_MERGE_FILE, FILTRULE_NO_PREFIXES))
920 goto invalid;
921 rule->rflags |= FILTRULE_NO_PREFIXES;
922 break;
923 case '+':
924 if (!BITS_SETnUNSET(rule->rflags, FILTRULE_MERGE_FILE, FILTRULE_NO_PREFIXES))
925 goto invalid;
926 rule->rflags |= FILTRULE_NO_PREFIXES
927 | FILTRULE_INCLUDE;
928 break;
929 case '/':
930 rule->rflags |= FILTRULE_ABS_PATH;
931 break;
932 case '!':
933 /* Negation really goes with the pattern, so it
934 * isn't useful as a merge-file default. */
935 if (rule->rflags & FILTRULE_MERGE_FILE)
936 goto invalid;
937 rule->rflags |= FILTRULE_NEGATE;
938 break;
939 case 'C':
940 if (rule->rflags & FILTRULE_NO_PREFIXES || prefix_specifies_side)
941 goto invalid;
942 rule->rflags |= FILTRULE_NO_PREFIXES
943 | FILTRULE_WORD_SPLIT
944 | FILTRULE_NO_INHERIT
945 | FILTRULE_CVS_IGNORE;
946 break;
947 case 'e':
948 if (!(rule->rflags & FILTRULE_MERGE_FILE))
949 goto invalid;
950 rule->rflags |= FILTRULE_EXCLUDE_SELF;
951 break;
952 case 'n':
953 if (!(rule->rflags & FILTRULE_MERGE_FILE))
954 goto invalid;
955 rule->rflags |= FILTRULE_NO_INHERIT;
956 break;
957 case 'p':
958 rule->rflags |= FILTRULE_PERISHABLE;
959 break;
960 case 'r':
961 if (prefix_specifies_side)
962 goto invalid;
963 rule->rflags |= FILTRULE_RECEIVER_SIDE;
964 break;
965 case 's':
966 if (prefix_specifies_side)
967 goto invalid;
968 rule->rflags |= FILTRULE_SENDER_SIDE;
969 break;
970 case 'w':
971 if (!(rule->rflags & FILTRULE_MERGE_FILE))
972 goto invalid;
973 rule->rflags |= FILTRULE_WORD_SPLIT;
974 break;
977 if (*s)
978 s++;
980 if (template->rflags & FILTRULES_SIDES) {
981 if (rule->rflags & FILTRULES_SIDES) {
982 /* The filter and template both specify side(s). This
983 * is dodgy (and won't work correctly if the template is
984 * a one-sided per-dir merge rule), so reject it. */
985 rprintf(FERROR,
986 "specified-side merge file contains specified-side filter: %s\n",
987 *rulestr_ptr);
988 exit_cleanup(RERR_SYNTAX);
990 rule->rflags |= template->rflags & FILTRULES_SIDES;
993 if (template->rflags & FILTRULE_WORD_SPLIT) {
994 const uchar *cp = s;
995 /* Token ends at whitespace or the end of the string. */
996 while (!isspace(*cp) && *cp != '\0')
997 cp++;
998 len = cp - s;
999 } else
1000 len = strlen((char*)s);
1002 if (rule->rflags & FILTRULE_CLEAR_LIST) {
1003 if (!(rule->rflags & FILTRULE_NO_PREFIXES)
1004 && !(xflags & XFLG_OLD_PREFIXES) && len) {
1005 rprintf(FERROR,
1006 "'!' rule has trailing characters: %s\n", *rulestr_ptr);
1007 exit_cleanup(RERR_SYNTAX);
1009 if (len > 1)
1010 rule->rflags &= ~FILTRULE_CLEAR_LIST;
1011 } else if (!len && !(rule->rflags & FILTRULE_CVS_IGNORE)) {
1012 rprintf(FERROR, "unexpected end of filter rule: %s\n", *rulestr_ptr);
1013 exit_cleanup(RERR_SYNTAX);
1016 /* --delete-excluded turns an un-modified include/exclude into a
1017 * sender-side rule. We also affect per-dir merge files that take
1018 * no prefixes as a simple optimization. */
1019 if (delete_excluded
1020 && !(rule->rflags & FILTRULES_SIDES)
1021 && (!(rule->rflags & FILTRULE_PERDIR_MERGE)
1022 || rule->rflags & FILTRULE_NO_PREFIXES))
1023 rule->rflags |= FILTRULE_SENDER_SIDE;
1025 *pat_ptr = (const char *)s;
1026 *pat_len_ptr = len;
1027 *rulestr_ptr = *pat_ptr + len;
1028 return rule;
1031 static char default_cvsignore[] =
1032 /* These default ignored items come from the CVS manual. */
1033 "RCS SCCS CVS CVS.adm RCSLOG cvslog.* tags TAGS"
1034 " .make.state .nse_depinfo *~ #* .#* ,* _$* *$"
1035 " *.old *.bak *.BAK *.orig *.rej .del-*"
1036 " *.a *.olb *.o *.obj *.so *.exe"
1037 " *.Z *.elc *.ln core"
1038 /* The rest we added to suit ourself. */
1039 " .svn/ .git/ .hg/ .bzr/";
1041 static void get_cvs_excludes(uint32 rflags)
1043 static int initialized = 0;
1044 char *p, fname[MAXPATHLEN];
1046 if (initialized)
1047 return;
1048 initialized = 1;
1050 parse_filter_str(&cvs_filter_list, default_cvsignore,
1051 rule_template(rflags | (protocol_version >= 30 ? FILTRULE_PERISHABLE : 0)),
1054 p = module_id >= 0 && lp_use_chroot(module_id) ? "/" : getenv("HOME");
1055 if (p && pathjoin(fname, MAXPATHLEN, p, ".cvsignore") < MAXPATHLEN)
1056 parse_filter_file(&cvs_filter_list, fname, rule_template(rflags), 0);
1058 parse_filter_str(&cvs_filter_list, getenv("CVSIGNORE"), rule_template(rflags), 0);
1061 const filter_rule *rule_template(uint32 rflags)
1063 static filter_rule template; /* zero-initialized */
1064 template.rflags = rflags;
1065 return &template;
1068 void parse_filter_str(filter_rule_list *listp, const char *rulestr,
1069 const filter_rule *template, int xflags)
1071 filter_rule *rule;
1072 const char *pat;
1073 unsigned int pat_len;
1075 if (!rulestr)
1076 return;
1078 while (1) {
1079 uint32 new_rflags;
1081 /* Remember that the returned string is NOT '\0' terminated! */
1082 if (!(rule = parse_rule_tok(&rulestr, template, xflags, &pat, &pat_len)))
1083 break;
1085 if (pat_len >= MAXPATHLEN) {
1086 rprintf(FERROR, "discarding over-long filter: %.*s\n",
1087 (int)pat_len, pat);
1088 free_continue:
1089 free_filter(rule);
1090 continue;
1093 new_rflags = rule->rflags;
1094 if (new_rflags & FILTRULE_CLEAR_LIST) {
1095 if (DEBUG_GTE(FILTER, 2)) {
1096 rprintf(FINFO,
1097 "[%s] clearing filter list%s\n",
1098 who_am_i(), listp->debug_type);
1100 clear_filter_list(listp);
1101 goto free_continue;
1104 if (new_rflags & FILTRULE_MERGE_FILE) {
1105 if (!pat_len) {
1106 pat = ".cvsignore";
1107 pat_len = 10;
1109 if (new_rflags & FILTRULE_EXCLUDE_SELF) {
1110 const char *name;
1111 filter_rule *excl_self;
1113 if (!(excl_self = new0(filter_rule)))
1114 out_of_memory("parse_filter_str");
1115 /* Find the beginning of the basename and add an exclude for it. */
1116 for (name = pat + pat_len; name > pat && name[-1] != '/'; name--) {}
1117 add_rule(listp, name, (pat + pat_len) - name, excl_self, 0);
1118 rule->rflags &= ~FILTRULE_EXCLUDE_SELF;
1120 if (new_rflags & FILTRULE_PERDIR_MERGE) {
1121 if (parent_dirscan) {
1122 const char *p;
1123 unsigned int len = pat_len;
1124 if ((p = parse_merge_name(pat, &len, module_dirlen)))
1125 add_rule(listp, p, len, rule, 0);
1126 else
1127 free_filter(rule);
1128 continue;
1130 } else {
1131 const char *p;
1132 unsigned int len = pat_len;
1133 if ((p = parse_merge_name(pat, &len, 0)))
1134 parse_filter_file(listp, p, rule, XFLG_FATAL_ERRORS);
1135 free_filter(rule);
1136 continue;
1140 add_rule(listp, pat, pat_len, rule, xflags);
1142 if (new_rflags & FILTRULE_CVS_IGNORE
1143 && !(new_rflags & FILTRULE_MERGE_FILE))
1144 get_cvs_excludes(new_rflags);
1148 void parse_filter_file(filter_rule_list *listp, const char *fname, const filter_rule *template, int xflags)
1150 FILE *fp;
1151 char line[BIGPATHBUFLEN];
1152 char *eob = line + sizeof line - 1;
1153 BOOL word_split = (template->rflags & FILTRULE_WORD_SPLIT) != 0;
1155 if (!fname || !*fname)
1156 return;
1158 if (*fname != '-' || fname[1] || am_server) {
1159 if (daemon_filter_list.head) {
1160 strlcpy(line, fname, sizeof line);
1161 clean_fname(line, CFN_COLLAPSE_DOT_DOT_DIRS);
1162 if (check_filter(&daemon_filter_list, FLOG, line, 0) < 0)
1163 fp = NULL;
1164 else
1165 fp = fopen(line, "rb");
1166 } else
1167 fp = fopen(fname, "rb");
1168 } else
1169 fp = stdin;
1171 if (DEBUG_GTE(FILTER, 2)) {
1172 rprintf(FINFO, "[%s] parse_filter_file(%s,%x,%x)%s\n",
1173 who_am_i(), fname, template->rflags, xflags,
1174 fp ? "" : " [not found]");
1177 if (!fp) {
1178 if (xflags & XFLG_FATAL_ERRORS) {
1179 rsyserr(FERROR, errno,
1180 "failed to open %sclude file %s",
1181 template->rflags & FILTRULE_INCLUDE ? "in" : "ex",
1182 fname);
1183 exit_cleanup(RERR_FILEIO);
1185 return;
1187 dirbuf[dirbuf_len] = '\0';
1189 while (1) {
1190 char *s = line;
1191 int ch, overflow = 0;
1192 while (1) {
1193 if ((ch = getc(fp)) == EOF) {
1194 if (ferror(fp) && errno == EINTR) {
1195 clearerr(fp);
1196 continue;
1198 break;
1200 if (word_split && isspace(ch))
1201 break;
1202 if (eol_nulls? !ch : (ch == '\n' || ch == '\r'))
1203 break;
1204 if (s < eob)
1205 *s++ = ch;
1206 else
1207 overflow = 1;
1209 if (overflow) {
1210 rprintf(FERROR, "discarding over-long filter: %s...\n", line);
1211 s = line;
1213 *s = '\0';
1214 /* Skip an empty token and (when line parsing) comments. */
1215 if (*line && (word_split || (*line != ';' && *line != '#')))
1216 parse_filter_str(listp, line, template, xflags);
1217 if (ch == EOF)
1218 break;
1220 fclose(fp);
1223 /* If the "for_xfer" flag is set, the prefix is made compatible with the
1224 * current protocol_version (if possible) or a NULL is returned (if not
1225 * possible). */
1226 char *get_rule_prefix(filter_rule *rule, const char *pat, int for_xfer,
1227 unsigned int *plen_ptr)
1229 static char buf[MAX_RULE_PREFIX+1];
1230 char *op = buf;
1231 int legal_len = for_xfer && protocol_version < 29 ? 1 : MAX_RULE_PREFIX-1;
1233 if (rule->rflags & FILTRULE_PERDIR_MERGE) {
1234 if (legal_len == 1)
1235 return NULL;
1236 *op++ = ':';
1237 } else if (rule->rflags & FILTRULE_INCLUDE)
1238 *op++ = '+';
1239 else if (legal_len != 1
1240 || ((*pat == '-' || *pat == '+') && pat[1] == ' '))
1241 *op++ = '-';
1242 else
1243 legal_len = 0;
1245 if (rule->rflags & FILTRULE_ABS_PATH)
1246 *op++ = '/';
1247 if (rule->rflags & FILTRULE_NEGATE)
1248 *op++ = '!';
1249 if (rule->rflags & FILTRULE_CVS_IGNORE)
1250 *op++ = 'C';
1251 else {
1252 if (rule->rflags & FILTRULE_NO_INHERIT)
1253 *op++ = 'n';
1254 if (rule->rflags & FILTRULE_WORD_SPLIT)
1255 *op++ = 'w';
1256 if (rule->rflags & FILTRULE_NO_PREFIXES) {
1257 if (rule->rflags & FILTRULE_INCLUDE)
1258 *op++ = '+';
1259 else
1260 *op++ = '-';
1263 if (rule->rflags & FILTRULE_EXCLUDE_SELF)
1264 *op++ = 'e';
1265 if (rule->rflags & FILTRULE_SENDER_SIDE
1266 && (!for_xfer || protocol_version >= 29))
1267 *op++ = 's';
1268 if (rule->rflags & FILTRULE_RECEIVER_SIDE
1269 && (!for_xfer || protocol_version >= 29
1270 || (delete_excluded && am_sender)))
1271 *op++ = 'r';
1272 if (rule->rflags & FILTRULE_PERISHABLE) {
1273 if (!for_xfer || protocol_version >= 30)
1274 *op++ = 'p';
1275 else if (am_sender)
1276 return NULL;
1278 if (op - buf > legal_len)
1279 return NULL;
1280 if (legal_len)
1281 *op++ = ' ';
1282 *op = '\0';
1283 if (plen_ptr)
1284 *plen_ptr = op - buf;
1285 return buf;
1288 static void send_rules(int f_out, filter_rule_list *flp)
1290 filter_rule *ent, *prev = NULL;
1292 for (ent = flp->head; ent; ent = ent->next) {
1293 unsigned int len, plen, dlen;
1294 int elide = 0;
1295 char *p;
1297 /* Note we need to check delete_excluded here in addition to
1298 * the code in parse_rule_tok() because some rules may have
1299 * been added before we found the --delete-excluded option.
1300 * We must also elide any CVS merge-file rules to avoid a
1301 * backward compatibility problem, and we elide any no-prefix
1302 * merge files as an optimization (since they can only have
1303 * include/exclude rules). */
1304 if (ent->rflags & FILTRULE_SENDER_SIDE)
1305 elide = am_sender ? 1 : -1;
1306 if (ent->rflags & FILTRULE_RECEIVER_SIDE)
1307 elide = elide ? 0 : am_sender ? -1 : 1;
1308 else if (delete_excluded && !elide
1309 && (!(ent->rflags & FILTRULE_PERDIR_MERGE)
1310 || ent->rflags & FILTRULE_NO_PREFIXES))
1311 elide = am_sender ? 1 : -1;
1312 if (elide < 0) {
1313 if (prev)
1314 prev->next = ent->next;
1315 else
1316 flp->head = ent->next;
1317 } else
1318 prev = ent;
1319 if (elide > 0)
1320 continue;
1321 if (ent->rflags & FILTRULE_CVS_IGNORE
1322 && !(ent->rflags & FILTRULE_MERGE_FILE)) {
1323 int f = am_sender || protocol_version < 29 ? f_out : -2;
1324 send_rules(f, &cvs_filter_list);
1325 if (f == f_out)
1326 continue;
1328 p = get_rule_prefix(ent, ent->pattern, 1, &plen);
1329 if (!p) {
1330 rprintf(FERROR,
1331 "filter rules are too modern for remote rsync.\n");
1332 exit_cleanup(RERR_PROTOCOL);
1334 if (f_out < 0)
1335 continue;
1336 len = strlen(ent->pattern);
1337 dlen = ent->rflags & FILTRULE_DIRECTORY ? 1 : 0;
1338 if (!(plen + len + dlen))
1339 continue;
1340 write_int(f_out, plen + len + dlen);
1341 if (plen)
1342 write_buf(f_out, p, plen);
1343 write_buf(f_out, ent->pattern, len);
1344 if (dlen)
1345 write_byte(f_out, '/');
1347 flp->tail = prev;
1350 /* This is only called by the client. */
1351 void send_filter_list(int f_out)
1353 int receiver_wants_list = prune_empty_dirs
1354 || (delete_mode && (!delete_excluded || protocol_version >= 29));
1356 if (local_server || (am_sender && !receiver_wants_list))
1357 f_out = -1;
1358 if (cvs_exclude && am_sender) {
1359 if (protocol_version >= 29)
1360 parse_filter_str(&filter_list, ":C", rule_template(0), 0);
1361 parse_filter_str(&filter_list, "-C", rule_template(0), 0);
1364 send_rules(f_out, &filter_list);
1366 if (f_out >= 0)
1367 write_int(f_out, 0);
1369 if (cvs_exclude) {
1370 if (!am_sender || protocol_version < 29)
1371 parse_filter_str(&filter_list, ":C", rule_template(0), 0);
1372 if (!am_sender)
1373 parse_filter_str(&filter_list, "-C", rule_template(0), 0);
1377 /* This is only called by the server. */
1378 void recv_filter_list(int f_in)
1380 char line[BIGPATHBUFLEN];
1381 int xflags = protocol_version >= 29 ? 0 : XFLG_OLD_PREFIXES;
1382 int receiver_wants_list = prune_empty_dirs
1383 || (delete_mode
1384 && (!delete_excluded || protocol_version >= 29));
1385 unsigned int len;
1387 if (!local_server && (am_sender || receiver_wants_list)) {
1388 while ((len = read_int(f_in)) != 0) {
1389 if (len >= sizeof line)
1390 overflow_exit("recv_rules");
1391 read_sbuf(f_in, line, len);
1392 parse_filter_str(&filter_list, line, rule_template(0), xflags);
1396 if (cvs_exclude) {
1397 if (local_server || am_sender || protocol_version < 29)
1398 parse_filter_str(&filter_list, ":C", rule_template(0), 0);
1399 if (local_server || am_sender)
1400 parse_filter_str(&filter_list, "-C", rule_template(0), 0);
1403 if (local_server) /* filter out any rules that aren't for us. */
1404 send_rules(-1, &filter_list);