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-2014 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 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 sender-side rule. */
1018 && !(rule
->rflags
& (FILTRULES_SIDES
|FILTRULE_MERGE_FILE
|FILTRULE_PERDIR_MERGE
)))
1019 rule
->rflags
|= FILTRULE_SENDER_SIDE
;
1021 *pat_ptr
= (const char *)s
;
1023 *rulestr_ptr
= *pat_ptr
+ len
;
1027 static char default_cvsignore
[] =
1028 /* These default ignored items come from the CVS manual. */
1029 "RCS SCCS CVS CVS.adm RCSLOG cvslog.* tags TAGS"
1030 " .make.state .nse_depinfo *~ #* .#* ,* _$* *$"
1031 " *.old *.bak *.BAK *.orig *.rej .del-*"
1032 " *.a *.olb *.o *.obj *.so *.exe"
1033 " *.Z *.elc *.ln core"
1034 /* The rest we added to suit ourself. */
1035 " .svn/ .git/ .hg/ .bzr/";
1037 static void get_cvs_excludes(uint32 rflags
)
1039 static int initialized
= 0;
1040 char *p
, fname
[MAXPATHLEN
];
1046 parse_filter_str(&cvs_filter_list
, default_cvsignore
,
1047 rule_template(rflags
| (protocol_version
>= 30 ? FILTRULE_PERISHABLE
: 0)),
1050 p
= module_id
>= 0 && lp_use_chroot(module_id
) ? "/" : getenv("HOME");
1051 if (p
&& pathjoin(fname
, MAXPATHLEN
, p
, ".cvsignore") < MAXPATHLEN
)
1052 parse_filter_file(&cvs_filter_list
, fname
, rule_template(rflags
), 0);
1054 parse_filter_str(&cvs_filter_list
, getenv("CVSIGNORE"), rule_template(rflags
), 0);
1057 const filter_rule
*rule_template(uint32 rflags
)
1059 static filter_rule
template; /* zero-initialized */
1060 template.rflags
= rflags
;
1064 void parse_filter_str(filter_rule_list
*listp
, const char *rulestr
,
1065 const filter_rule
*template, int xflags
)
1069 unsigned int pat_len
;
1077 /* Remember that the returned string is NOT '\0' terminated! */
1078 if (!(rule
= parse_rule_tok(&rulestr
, template, xflags
, &pat
, &pat_len
)))
1081 if (pat_len
>= MAXPATHLEN
) {
1082 rprintf(FERROR
, "discarding over-long filter: %.*s\n",
1089 new_rflags
= rule
->rflags
;
1090 if (new_rflags
& FILTRULE_CLEAR_LIST
) {
1091 if (DEBUG_GTE(FILTER
, 2)) {
1093 "[%s] clearing filter list%s\n",
1094 who_am_i(), listp
->debug_type
);
1096 clear_filter_list(listp
);
1100 if (new_rflags
& FILTRULE_MERGE_FILE
) {
1105 if (new_rflags
& FILTRULE_EXCLUDE_SELF
) {
1107 filter_rule
*excl_self
;
1109 if (!(excl_self
= new0(filter_rule
)))
1110 out_of_memory("parse_filter_str");
1111 /* Find the beginning of the basename and add an exclude for it. */
1112 for (name
= pat
+ pat_len
; name
> pat
&& name
[-1] != '/'; name
--) {}
1113 add_rule(listp
, name
, (pat
+ pat_len
) - name
, excl_self
, 0);
1114 rule
->rflags
&= ~FILTRULE_EXCLUDE_SELF
;
1116 if (new_rflags
& FILTRULE_PERDIR_MERGE
) {
1117 if (parent_dirscan
) {
1119 unsigned int len
= pat_len
;
1120 if ((p
= parse_merge_name(pat
, &len
, module_dirlen
)))
1121 add_rule(listp
, p
, len
, rule
, 0);
1128 unsigned int len
= pat_len
;
1129 if ((p
= parse_merge_name(pat
, &len
, 0)))
1130 parse_filter_file(listp
, p
, rule
, XFLG_FATAL_ERRORS
);
1136 add_rule(listp
, pat
, pat_len
, rule
, xflags
);
1138 if (new_rflags
& FILTRULE_CVS_IGNORE
1139 && !(new_rflags
& FILTRULE_MERGE_FILE
))
1140 get_cvs_excludes(new_rflags
);
1144 void parse_filter_file(filter_rule_list
*listp
, const char *fname
, const filter_rule
*template, int xflags
)
1147 char line
[BIGPATHBUFLEN
];
1148 char *eob
= line
+ sizeof line
- 1;
1149 BOOL word_split
= (template->rflags
& FILTRULE_WORD_SPLIT
) != 0;
1151 if (!fname
|| !*fname
)
1154 if (*fname
!= '-' || fname
[1] || am_server
) {
1155 if (daemon_filter_list
.head
) {
1156 strlcpy(line
, fname
, sizeof line
);
1157 clean_fname(line
, CFN_COLLAPSE_DOT_DOT_DIRS
);
1158 if (check_filter(&daemon_filter_list
, FLOG
, line
, 0) < 0)
1161 fp
= fopen(line
, "rb");
1163 fp
= fopen(fname
, "rb");
1167 if (DEBUG_GTE(FILTER
, 2)) {
1168 rprintf(FINFO
, "[%s] parse_filter_file(%s,%x,%x)%s\n",
1169 who_am_i(), fname
, template->rflags
, xflags
,
1170 fp
? "" : " [not found]");
1174 if (xflags
& XFLG_FATAL_ERRORS
) {
1175 rsyserr(FERROR
, errno
,
1176 "failed to open %sclude file %s",
1177 template->rflags
& FILTRULE_INCLUDE
? "in" : "ex",
1179 exit_cleanup(RERR_FILEIO
);
1183 dirbuf
[dirbuf_len
] = '\0';
1187 int ch
, overflow
= 0;
1189 if ((ch
= getc(fp
)) == EOF
) {
1190 if (ferror(fp
) && errno
== EINTR
) {
1196 if (word_split
&& isspace(ch
))
1198 if (eol_nulls
? !ch
: (ch
== '\n' || ch
== '\r'))
1206 rprintf(FERROR
, "discarding over-long filter: %s...\n", line
);
1210 /* Skip an empty token and (when line parsing) comments. */
1211 if (*line
&& (word_split
|| (*line
!= ';' && *line
!= '#')))
1212 parse_filter_str(listp
, line
, template, xflags
);
1219 /* If the "for_xfer" flag is set, the prefix is made compatible with the
1220 * current protocol_version (if possible) or a NULL is returned (if not
1222 char *get_rule_prefix(filter_rule
*rule
, const char *pat
, int for_xfer
,
1223 unsigned int *plen_ptr
)
1225 static char buf
[MAX_RULE_PREFIX
+1];
1227 int legal_len
= for_xfer
&& protocol_version
< 29 ? 1 : MAX_RULE_PREFIX
-1;
1229 if (rule
->rflags
& FILTRULE_PERDIR_MERGE
) {
1233 } else if (rule
->rflags
& FILTRULE_INCLUDE
)
1235 else if (legal_len
!= 1
1236 || ((*pat
== '-' || *pat
== '+') && pat
[1] == ' '))
1241 if (rule
->rflags
& FILTRULE_ABS_PATH
)
1243 if (rule
->rflags
& FILTRULE_NEGATE
)
1245 if (rule
->rflags
& FILTRULE_CVS_IGNORE
)
1248 if (rule
->rflags
& FILTRULE_NO_INHERIT
)
1250 if (rule
->rflags
& FILTRULE_WORD_SPLIT
)
1252 if (rule
->rflags
& FILTRULE_NO_PREFIXES
) {
1253 if (rule
->rflags
& FILTRULE_INCLUDE
)
1259 if (rule
->rflags
& FILTRULE_EXCLUDE_SELF
)
1261 if (rule
->rflags
& FILTRULE_SENDER_SIDE
1262 && (!for_xfer
|| protocol_version
>= 29))
1264 if (rule
->rflags
& FILTRULE_RECEIVER_SIDE
1265 && (!for_xfer
|| protocol_version
>= 29
1266 || (delete_excluded
&& am_sender
)))
1268 if (rule
->rflags
& FILTRULE_PERISHABLE
) {
1269 if (!for_xfer
|| protocol_version
>= 30)
1274 if (op
- buf
> legal_len
)
1280 *plen_ptr
= op
- buf
;
1284 static void send_rules(int f_out
, filter_rule_list
*flp
)
1286 filter_rule
*ent
, *prev
= NULL
;
1288 for (ent
= flp
->head
; ent
; ent
= ent
->next
) {
1289 unsigned int len
, plen
, dlen
;
1293 /* Note we need to check delete_excluded here in addition to
1294 * the code in parse_rule_tok() because some rules may have
1295 * been added before we found the --delete-excluded option.
1296 * We must also elide any CVS merge-file rules to avoid a
1297 * backward compatibility problem, and we elide any no-prefix
1298 * merge files as an optimization (since they can only have
1299 * include/exclude rules). */
1300 if (ent
->rflags
& FILTRULE_SENDER_SIDE
)
1301 elide
= am_sender
? 1 : -1;
1302 if (ent
->rflags
& FILTRULE_RECEIVER_SIDE
)
1303 elide
= elide
? 0 : am_sender
? -1 : 1;
1304 else if (delete_excluded
&& !elide
1305 && (!(ent
->rflags
& FILTRULE_PERDIR_MERGE
)
1306 || ent
->rflags
& FILTRULE_NO_PREFIXES
))
1307 elide
= am_sender
? 1 : -1;
1310 prev
->next
= ent
->next
;
1312 flp
->head
= ent
->next
;
1317 if (ent
->rflags
& FILTRULE_CVS_IGNORE
1318 && !(ent
->rflags
& FILTRULE_MERGE_FILE
)) {
1319 int f
= am_sender
|| protocol_version
< 29 ? f_out
: -2;
1320 send_rules(f
, &cvs_filter_list
);
1324 p
= get_rule_prefix(ent
, ent
->pattern
, 1, &plen
);
1327 "filter rules are too modern for remote rsync.\n");
1328 exit_cleanup(RERR_PROTOCOL
);
1332 len
= strlen(ent
->pattern
);
1333 dlen
= ent
->rflags
& FILTRULE_DIRECTORY
? 1 : 0;
1334 if (!(plen
+ len
+ dlen
))
1336 write_int(f_out
, plen
+ len
+ dlen
);
1338 write_buf(f_out
, p
, plen
);
1339 write_buf(f_out
, ent
->pattern
, len
);
1341 write_byte(f_out
, '/');
1346 /* This is only called by the client. */
1347 void send_filter_list(int f_out
)
1349 int receiver_wants_list
= prune_empty_dirs
1350 || (delete_mode
&& (!delete_excluded
|| protocol_version
>= 29));
1352 if (local_server
|| (am_sender
&& !receiver_wants_list
))
1354 if (cvs_exclude
&& am_sender
) {
1355 if (protocol_version
>= 29)
1356 parse_filter_str(&filter_list
, ":C", rule_template(0), 0);
1357 parse_filter_str(&filter_list
, "-C", rule_template(0), 0);
1360 send_rules(f_out
, &filter_list
);
1363 write_int(f_out
, 0);
1366 if (!am_sender
|| protocol_version
< 29)
1367 parse_filter_str(&filter_list
, ":C", rule_template(0), 0);
1369 parse_filter_str(&filter_list
, "-C", rule_template(0), 0);
1373 /* This is only called by the server. */
1374 void recv_filter_list(int f_in
)
1376 char line
[BIGPATHBUFLEN
];
1377 int xflags
= protocol_version
>= 29 ? 0 : XFLG_OLD_PREFIXES
;
1378 int receiver_wants_list
= prune_empty_dirs
1380 && (!delete_excluded
|| protocol_version
>= 29));
1383 if (!local_server
&& (am_sender
|| receiver_wants_list
)) {
1384 while ((len
= read_int(f_in
)) != 0) {
1385 if (len
>= sizeof line
)
1386 overflow_exit("recv_rules");
1387 read_sbuf(f_in
, line
, len
);
1388 parse_filter_str(&filter_list
, line
, rule_template(0), xflags
);
1393 if (local_server
|| am_sender
|| protocol_version
< 29)
1394 parse_filter_str(&filter_list
, ":C", rule_template(0), 0);
1395 if (local_server
|| am_sender
)
1396 parse_filter_str(&filter_list
, "-C", rule_template(0), 0);
1399 if (local_server
) /* filter out any rules that aren't for us. */
1400 send_rules(-1, &filter_list
);