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.
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
;
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
);
121 static void free_filter(filter_rule
*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
134 filter_rule
*next
= head
->next
;
135 head
->next
= 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
145 if (rev_head
->rflags
& FILTRULE_PERDIR_MERGE
)
146 teardown_mergelist(rev_head
);
147 free_filter(rev_head
);
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
)
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
),
165 (rule
->rflags
& FILTRULE_DIRECTORY
) ? "/" : "",
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. */
179 if (pat_len
> 1 && pat
[pat_len
-1] == '/') {
181 rule
->rflags
|= FILTRULE_DIRECTORY
;
184 for (cp
= pat
; cp
< pat
+ pat_len
; cp
++) {
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
;
194 pre_len
= dirbuf_len
- module_dirlen
- 1;
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;
208 if (!(rule
->pattern
= new_array(char, pre_len
+ pat_len
+ suf_len
+ 1)))
209 out_of_memory("add_rule");
211 memcpy(rule
->pattern
, dirbuf
+ module_dirlen
, pre_len
);
212 for (cp
= rule
->pattern
; cp
< rule
->pattern
+ pre_len
; cp
++) {
217 strlcpy(rule
->pattern
+ pre_len
, pat
, pat_len
+ 1);
220 memcpy(rule
->pattern
+ pat_len
, SLASH_WILD3_SUFFIX
, suf_len
+1);
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. */
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
;
246 if ((cp
= strrchr(rule
->pattern
, '/')) != NULL
)
251 /* If the local merge file was already mentioned, don't
253 for (i
= 0; i
< mergelist_cnt
; i
++) {
254 filter_rule
*ex
= mergelist_parents
[i
];
255 const char *s
= strrchr(ex
->pattern
, '/');
261 if (len
== pat_len
- (cp
- rule
->pattern
) && memcmp(s
, cp
, len
) == 0) {
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
) {
276 mergelist_parents
= realloc_array(mergelist_parents
,
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
;
288 rule
->u
.slash_cnt
= slash_cnt
;
291 rule
->next
= listp
->head
;
292 listp
->head
= listp
->tail
= rule
;
294 rule
->next
= listp
->tail
->next
;
295 listp
->tail
->next
= rule
;
300 static void clear_filter_list(filter_rule_list
*listp
)
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
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
];
326 if (!parent_dirscan
&& *merge_file
!= '/') {
327 /* Return the name unchanged it doesn't have any slashes. */
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);
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);
348 if (!sanitize_path(fn
, merge_file
, r
, dirbuf_depth
, SP_DEFAULT
)) {
349 rprintf(FERROR
, "merge-file name overflows: %s\n",
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. */
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
);
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
);
376 /* Sets the dirbuf and dirbuf_len values. */
377 void set_filter_dir(const char *dir
, unsigned int dirlen
)
381 memcpy(dirbuf
, curr_dir
, curr_dir_len
);
382 dirbuf
[curr_dir_len
] = '/';
383 len
= curr_dir_len
+ 1;
384 if (len
+ dirlen
>= MAXPATHLEN
)
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] == '/')
395 dirbuf
[dirbuf_len
++] = '/';
396 dirbuf
[dirbuf_len
] = '\0';
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
;
413 if (!(x
= parse_merge_name(pat
, NULL
, 0)) || *x
!= '/')
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
);
422 ex
->pattern
= strdup(y
+1);
426 strlcpy(buf
, x
, MAXPATHLEN
);
428 pathjoin(buf
, MAXPATHLEN
, dirbuf
, x
);
430 len
= clean_fname(buf
, CFN_COLLAPSE_DOT_DOT_DIRS
);
431 if (len
!= 1 && len
< MAXPATHLEN
-1) {
435 /* This ensures that the specified dir is a parent of the transfer. */
436 for (x
= buf
, y
= dirbuf
; *x
&& *x
== *y
; x
++, y
++) {}
438 y
+= strlen(y
); /* nope -- skip the scan */
440 parent_dirscan
= True
;
442 char save
[MAXPATHLEN
];
443 strlcpy(save
, y
, MAXPATHLEN
);
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
);
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
);
471 struct local_filter_state
{
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
;
485 set_filter_dir(dir
, dirlen
);
486 if (DEBUG_GTE(FILTER
, 2)) {
487 rprintf(FINFO
, "[%s] pushing local filters for %s\n",
491 if (!mergelist_cnt
) {
492 /* No old state to save and no new merge files to push. */
496 push
= (struct local_filter_state
*)new_array(char,
497 sizeof (struct local_filter_state
)
498 + (mergelist_cnt
-1) * sizeof (filter_rule_list
));
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
)
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
,
534 io_error
|= IOERR_GENERAL
;
536 "cannot add local filter rules in long-named directory: %s\n",
539 dirbuf
[dirbuf_len
] = '\0';
545 void pop_local_filters(void *mem
)
547 struct local_filter_state
*pop
= (struct local_filter_state
*)mem
;
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
);
587 /* No state to restore. */
591 for (i
= 0; i
< mergelist_cnt
; i
++) {
592 memcpy(mergelist_parents
[i
]->u
.mergelist
, &pop
->mergelists
[i
],
593 sizeof (filter_rule_list
));
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];
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
;
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
== '/');
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
)
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
;
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
)
661 strings
[str_cnt
] = NULL
;
663 if (*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. */
679 /* The pattern matches only at the start of the path or name. */
683 if (ex
->rflags
& FILTRULE_WILD
) {
684 if (wildmatch_array(pattern
, strings
, slash_handling
))
686 } else if (str_cnt
> 1) {
687 if (litmatch_array(pattern
, strings
, slash_handling
))
689 } else if (anchored_match
) {
690 if (strcmp(name
, pattern
) == 0)
693 int l1
= strlen(name
);
694 int l2
= strlen(pattern
);
696 strcmp(name
+(l1
-l2
),pattern
) == 0 &&
697 (l1
==l2
|| name
[l1
-(l2
+1)] == '/')) {
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
)
731 for (ent
= listp
->head
; ent
; ent
= ent
->next
) {
732 if (ignore_perishable
&& ent
->rflags
& FILTRULE_PERISHABLE
)
734 if (ent
->rflags
& FILTRULE_PERDIR_MERGE
) {
735 int rc
= check_filter(ent
->u
.mergelist
, code
, name
,
741 if (ent
->rflags
& FILTRULE_CVS_IGNORE
) {
742 int rc
= check_filter(&cvs_filter_list
, code
, name
,
748 if (rule_matches(name
, ent
, name_is_dir
)) {
749 report_filter_result(code
, name
, ent
, name_is_dir
,
751 return ent
->rflags
& FILTRULE_INCLUDE
? 1 : -1;
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)
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
;
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
;
791 if (template->rflags
& FILTRULE_WORD_SPLIT
) {
792 /* Skip over any initial whitespace. */
795 /* Update to point to real start of rule. */
796 *rulestr_ptr
= (const char *)s
;
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
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
;
821 } else if (*s
== '+' && s
[1] == ' ') {
822 rule
->rflags
|= FILTRULE_INCLUDE
;
824 } else if (*s
== '!')
825 rule
->rflags
|= FILTRULE_CLEAR_LIST
; /* Tentative! */
828 BOOL prefix_specifies_side
= False
;
831 if ((s
= RULE_STRCMP(s
, "clear")) != NULL
)
835 if ((s
= RULE_STRCMP(s
, "dir-merge")) != NULL
)
839 if ((s
= RULE_STRCMP(s
, "exclude")) != NULL
)
843 if ((s
= RULE_STRCMP(s
, "hide")) != NULL
)
847 if ((s
= RULE_STRCMP(s
, "include")) != NULL
)
851 if ((s
= RULE_STRCMP(s
, "merge")) != NULL
)
855 if ((s
= RULE_STRCMP(s
, "protect")) != NULL
)
859 if ((s
= RULE_STRCMP(s
, "risk")) != NULL
)
863 if ((s
= RULE_STRCMP(s
, "show")) != NULL
)
874 rule
->rflags
|= FILTRULE_PERDIR_MERGE
875 | FILTRULE_FINISH_SETUP
;
878 rule
->rflags
|= FILTRULE_MERGE_FILE
;
881 rule
->rflags
|= FILTRULE_INCLUDE
;
886 rule
->rflags
|= FILTRULE_INCLUDE
;
889 rule
->rflags
|= FILTRULE_SENDER_SIDE
;
890 prefix_specifies_side
= True
;
893 rule
->rflags
|= FILTRULE_INCLUDE
;
896 rule
->rflags
|= FILTRULE_RECEIVER_SIDE
;
897 prefix_specifies_side
= True
;
900 rule
->rflags
|= FILTRULE_CLEAR_LIST
;
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
)) {
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
);
919 if (!BITS_SETnUNSET(rule
->rflags
, FILTRULE_MERGE_FILE
, FILTRULE_NO_PREFIXES
))
921 rule
->rflags
|= FILTRULE_NO_PREFIXES
;
924 if (!BITS_SETnUNSET(rule
->rflags
, FILTRULE_MERGE_FILE
, FILTRULE_NO_PREFIXES
))
926 rule
->rflags
|= FILTRULE_NO_PREFIXES
930 rule
->rflags
|= FILTRULE_ABS_PATH
;
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
)
937 rule
->rflags
|= FILTRULE_NEGATE
;
940 if (rule
->rflags
& FILTRULE_NO_PREFIXES
|| prefix_specifies_side
)
942 rule
->rflags
|= FILTRULE_NO_PREFIXES
943 | FILTRULE_WORD_SPLIT
944 | FILTRULE_NO_INHERIT
945 | FILTRULE_CVS_IGNORE
;
948 if (!(rule
->rflags
& FILTRULE_MERGE_FILE
))
950 rule
->rflags
|= FILTRULE_EXCLUDE_SELF
;
953 if (!(rule
->rflags
& FILTRULE_MERGE_FILE
))
955 rule
->rflags
|= FILTRULE_NO_INHERIT
;
958 rule
->rflags
|= FILTRULE_PERISHABLE
;
961 if (prefix_specifies_side
)
963 rule
->rflags
|= FILTRULE_RECEIVER_SIDE
;
966 if (prefix_specifies_side
)
968 rule
->rflags
|= FILTRULE_SENDER_SIDE
;
971 if (!(rule
->rflags
& FILTRULE_MERGE_FILE
))
973 rule
->rflags
|= FILTRULE_WORD_SPLIT
;
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. */
986 "specified-side merge file contains specified-side filter: %s\n",
988 exit_cleanup(RERR_SYNTAX
);
990 rule
->rflags
|= template->rflags
& FILTRULES_SIDES
;
993 if (template->rflags
& FILTRULE_WORD_SPLIT
) {
995 /* Token ends at whitespace or the end of the string. */
996 while (!isspace(*cp
) && *cp
!= '\0')
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
) {
1006 "'!' rule has trailing characters: %s\n", *rulestr_ptr
);
1007 exit_cleanup(RERR_SYNTAX
);
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. */
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
;
1027 *rulestr_ptr
= *pat_ptr
+ len
;
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
];
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
;
1068 void parse_filter_str(filter_rule_list
*listp
, const char *rulestr
,
1069 const filter_rule
*template, int xflags
)
1073 unsigned int pat_len
;
1081 /* Remember that the returned string is NOT '\0' terminated! */
1082 if (!(rule
= parse_rule_tok(&rulestr
, template, xflags
, &pat
, &pat_len
)))
1085 if (pat_len
>= MAXPATHLEN
) {
1086 rprintf(FERROR
, "discarding over-long filter: %.*s\n",
1093 new_rflags
= rule
->rflags
;
1094 if (new_rflags
& FILTRULE_CLEAR_LIST
) {
1095 if (DEBUG_GTE(FILTER
, 2)) {
1097 "[%s] clearing filter list%s\n",
1098 who_am_i(), listp
->debug_type
);
1100 clear_filter_list(listp
);
1104 if (new_rflags
& FILTRULE_MERGE_FILE
) {
1109 if (new_rflags
& FILTRULE_EXCLUDE_SELF
) {
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
) {
1123 unsigned int len
= pat_len
;
1124 if ((p
= parse_merge_name(pat
, &len
, module_dirlen
)))
1125 add_rule(listp
, p
, len
, rule
, 0);
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
);
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
)
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
)
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)
1165 fp
= fopen(line
, "rb");
1167 fp
= fopen(fname
, "rb");
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]");
1178 if (xflags
& XFLG_FATAL_ERRORS
) {
1179 rsyserr(FERROR
, errno
,
1180 "failed to open %sclude file %s",
1181 template->rflags
& FILTRULE_INCLUDE
? "in" : "ex",
1183 exit_cleanup(RERR_FILEIO
);
1187 dirbuf
[dirbuf_len
] = '\0';
1191 int ch
, overflow
= 0;
1193 if ((ch
= getc(fp
)) == EOF
) {
1194 if (ferror(fp
) && errno
== EINTR
) {
1200 if (word_split
&& isspace(ch
))
1202 if (eol_nulls
? !ch
: (ch
== '\n' || ch
== '\r'))
1210 rprintf(FERROR
, "discarding over-long filter: %s...\n", line
);
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
);
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
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];
1231 int legal_len
= for_xfer
&& protocol_version
< 29 ? 1 : MAX_RULE_PREFIX
-1;
1233 if (rule
->rflags
& FILTRULE_PERDIR_MERGE
) {
1237 } else if (rule
->rflags
& FILTRULE_INCLUDE
)
1239 else if (legal_len
!= 1
1240 || ((*pat
== '-' || *pat
== '+') && pat
[1] == ' '))
1245 if (rule
->rflags
& FILTRULE_ABS_PATH
)
1247 if (rule
->rflags
& FILTRULE_NEGATE
)
1249 if (rule
->rflags
& FILTRULE_CVS_IGNORE
)
1252 if (rule
->rflags
& FILTRULE_NO_INHERIT
)
1254 if (rule
->rflags
& FILTRULE_WORD_SPLIT
)
1256 if (rule
->rflags
& FILTRULE_NO_PREFIXES
) {
1257 if (rule
->rflags
& FILTRULE_INCLUDE
)
1263 if (rule
->rflags
& FILTRULE_EXCLUDE_SELF
)
1265 if (rule
->rflags
& FILTRULE_SENDER_SIDE
1266 && (!for_xfer
|| protocol_version
>= 29))
1268 if (rule
->rflags
& FILTRULE_RECEIVER_SIDE
1269 && (!for_xfer
|| protocol_version
>= 29
1270 || (delete_excluded
&& am_sender
)))
1272 if (rule
->rflags
& FILTRULE_PERISHABLE
) {
1273 if (!for_xfer
|| protocol_version
>= 30)
1278 if (op
- buf
> legal_len
)
1284 *plen_ptr
= op
- 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
;
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;
1314 prev
->next
= ent
->next
;
1316 flp
->head
= ent
->next
;
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
);
1328 p
= get_rule_prefix(ent
, ent
->pattern
, 1, &plen
);
1331 "filter rules are too modern for remote rsync.\n");
1332 exit_cleanup(RERR_PROTOCOL
);
1336 len
= strlen(ent
->pattern
);
1337 dlen
= ent
->rflags
& FILTRULE_DIRECTORY
? 1 : 0;
1338 if (!(plen
+ len
+ dlen
))
1340 write_int(f_out
, plen
+ len
+ dlen
);
1342 write_buf(f_out
, p
, plen
);
1343 write_buf(f_out
, ent
->pattern
, len
);
1345 write_byte(f_out
, '/');
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
))
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
);
1367 write_int(f_out
, 0);
1370 if (!am_sender
|| protocol_version
< 29)
1371 parse_filter_str(&filter_list
, ":C", rule_template(0), 0);
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
1384 && (!delete_excluded
|| protocol_version
>= 29));
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
);
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
);