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-2008 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 struct filter_list_struct filter_list
= { .debug_type
= "" };
44 struct filter_list_struct cvs_filter_list
= { .debug_type
= " [global CVS]" };
45 struct filter_list_struct 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 MODIFIERS_MERGE_FILE "-+Cenw"
51 #define MODIFIERS_INCL_EXCL "/!Crsp"
52 #define MODIFIERS_HIDE_PROTECT "/!p"
54 #define SLASH_WILD3_SUFFIX "/***"
56 /* The dirbuf is set by push_local_filters() to the current subdirectory
57 * relative to curr_dir that is being processed. The path always has a
58 * trailing slash appended, and the variable dirbuf_len contains the length
59 * of this path prefix. The path is always absolute. */
60 static char dirbuf
[MAXPATHLEN
+1];
61 static unsigned int dirbuf_len
= 0;
62 static int dirbuf_depth
;
64 /* This is True when we're scanning parent dirs for per-dir merge-files. */
65 static BOOL parent_dirscan
= False
;
67 /* This array contains a list of all the currently active per-dir merge
68 * files. This makes it easier to save the appropriate values when we
69 * "push" down into each subdirectory. */
70 static struct filter_struct
**mergelist_parents
;
71 static int mergelist_cnt
= 0;
72 static int mergelist_size
= 0;
74 /* Each filter_list_struct describes a singly-linked list by keeping track
75 * of both the head and tail pointers. The list is slightly unusual in that
76 * a parent-dir's content can be appended to the end of the local list in a
77 * special way: the last item in the local list has its "next" pointer set
78 * to point to the inherited list, but the local list's tail pointer points
79 * at the end of the local list. Thus, if the local list is empty, the head
80 * will be pointing at the inherited content but the tail will be NULL. To
81 * help you visualize this, here are the possible list arrangements:
83 * Completely Empty Local Content Only
84 * ================================== ====================================
85 * head -> NULL head -> Local1 -> Local2 -> NULL
86 * tail -> NULL tail -------------^
88 * Inherited Content Only Both Local and Inherited Content
89 * ================================== ====================================
90 * head -> Parent1 -> Parent2 -> NULL head -> L1 -> L2 -> P1 -> P2 -> NULL
91 * tail -> NULL tail ---------^
93 * This means that anyone wanting to traverse the whole list to use it just
94 * needs to start at the head and use the "next" pointers until it goes
95 * NULL. To add new local content, we insert the item after the tail item
96 * and update the tail (obviously, if "tail" was NULL, we insert it at the
97 * head). To clear the local list, WE MUST NOT FREE THE INHERITED CONTENT
98 * because it is shared between the current list and our parent list(s).
99 * The easiest way to handle this is to simply truncate the list after the
100 * tail item and then free the local list from the head. When inheriting
101 * the list for a new local dir, we just save off the filter_list_struct
102 * values (so we can pop back to them later) and set the tail to NULL.
105 static void teardown_mergelist(struct filter_struct
*ex
)
107 if (DEBUG_GTE(FILTER
, 2)) {
108 rprintf(FINFO
, "[%s] deactivating mergelist #%d%s\n",
109 who_am_i(), mergelist_cnt
- 1,
110 ex
->u
.mergelist
->debug_type
);
113 /* We should deactivate mergelists in LIFO order. */
114 assert(mergelist_cnt
> 0);
115 assert(ex
== mergelist_parents
[mergelist_cnt
- 1]);
117 /* The parent_dirscan filters should have been freed. */
118 assert(ex
->u
.mergelist
->parent_dirscan_head
== NULL
);
120 free(ex
->u
.mergelist
->debug_type
);
121 free(ex
->u
.mergelist
);
125 static void free_filter(struct filter_struct
*ex
)
127 if (ex
->match_flags
& MATCHFLG_PERDIR_MERGE
)
128 teardown_mergelist(ex
);
133 static void free_filters(struct filter_struct
*head
)
135 struct filter_struct
*rev_head
= NULL
;
137 /* Reverse the list so we deactivate mergelists in the proper LIFO
140 struct filter_struct
*next
= head
->next
;
141 head
->next
= rev_head
;
147 struct filter_struct
*prev
= rev_head
->next
;
148 free_filter(rev_head
);
153 /* Build a filter structure given a filter pattern. The value in "pat"
154 * is not null-terminated. */
155 static void add_rule(struct filter_list_struct
*listp
, const char *pat
,
156 unsigned int pat_len
, uint32 mflags
, int xflags
)
158 struct filter_struct
*ret
;
160 unsigned int pre_len
, suf_len
, slash_cnt
= 0;
162 if (DEBUG_GTE(FILTER
, 2)) {
163 rprintf(FINFO
, "[%s] add_rule(%s%.*s%s)%s\n",
164 who_am_i(), get_rule_prefix(mflags
, pat
, 0, NULL
),
166 (mflags
& MATCHFLG_DIRECTORY
) ? "/" : "",
170 /* These flags also indicate that we're reading a list that
171 * needs to be filtered now, not post-filtered later. */
172 if (xflags
& (XFLG_ANCHORED2ABS
|XFLG_ABS_IF_SLASH
)) {
173 uint32 mf
= mflags
& (MATCHFLG_RECEIVER_SIDE
|MATCHFLG_SENDER_SIDE
);
175 if (mf
== MATCHFLG_RECEIVER_SIDE
)
178 if (mf
== MATCHFLG_SENDER_SIDE
)
183 if (!(ret
= new0(struct filter_struct
)))
184 out_of_memory("add_rule");
186 if (pat_len
> 1 && pat
[pat_len
-1] == '/') {
188 mflags
|= MATCHFLG_DIRECTORY
;
191 for (cp
= pat
; cp
< pat
+ pat_len
; cp
++) {
196 if (!(mflags
& (MATCHFLG_ABS_PATH
| MATCHFLG_MERGE_FILE
))
197 && ((xflags
& (XFLG_ANCHORED2ABS
|XFLG_ABS_IF_SLASH
) && *pat
== '/')
198 || (xflags
& XFLG_ABS_IF_SLASH
&& slash_cnt
))) {
199 mflags
|= MATCHFLG_ABS_PATH
;
201 pre_len
= dirbuf_len
- module_dirlen
- 1;
207 /* The daemon wants dir-exclude rules to get an appended "/" + "***". */
208 if (xflags
& XFLG_DIR2WILD3
209 && BITS_SETnUNSET(mflags
, MATCHFLG_DIRECTORY
, MATCHFLG_INCLUDE
)) {
210 mflags
&= ~MATCHFLG_DIRECTORY
;
211 suf_len
= sizeof SLASH_WILD3_SUFFIX
- 1;
215 if (!(ret
->pattern
= new_array(char, pre_len
+ pat_len
+ suf_len
+ 1)))
216 out_of_memory("add_rule");
218 memcpy(ret
->pattern
, dirbuf
+ module_dirlen
, pre_len
);
219 for (cp
= ret
->pattern
; cp
< ret
->pattern
+ pre_len
; cp
++) {
224 strlcpy(ret
->pattern
+ pre_len
, pat
, pat_len
+ 1);
227 memcpy(ret
->pattern
+ pat_len
, SLASH_WILD3_SUFFIX
, suf_len
+1);
232 if (strpbrk(ret
->pattern
, "*[?")) {
233 mflags
|= MATCHFLG_WILD
;
234 if ((cp
= strstr(ret
->pattern
, "**")) != NULL
) {
235 mflags
|= MATCHFLG_WILD2
;
236 /* If the pattern starts with **, note that. */
237 if (cp
== ret
->pattern
)
238 mflags
|= MATCHFLG_WILD2_PREFIX
;
239 /* If the pattern ends with ***, note that. */
241 && ret
->pattern
[pat_len
-3] == '*'
242 && ret
->pattern
[pat_len
-2] == '*'
243 && ret
->pattern
[pat_len
-1] == '*')
244 mflags
|= MATCHFLG_WILD3_SUFFIX
;
248 if (mflags
& MATCHFLG_PERDIR_MERGE
) {
249 struct filter_list_struct
*lp
;
253 if ((cp
= strrchr(ret
->pattern
, '/')) != NULL
)
258 /* If the local merge file was already mentioned, don't
260 for (i
= 0; i
< mergelist_cnt
; i
++) {
261 struct filter_struct
*ex
= mergelist_parents
[i
];
262 const char *s
= strrchr(ex
->pattern
, '/');
268 if (len
== pat_len
- (cp
- ret
->pattern
)
269 && memcmp(s
, cp
, len
) == 0) {
275 if (!(lp
= new_array(struct filter_list_struct
, 1)))
276 out_of_memory("add_rule");
277 lp
->head
= lp
->tail
= lp
->parent_dirscan_head
= NULL
;
278 if (asprintf(&lp
->debug_type
, " [per-dir %s]", cp
) < 0)
279 out_of_memory("add_rule");
280 ret
->u
.mergelist
= lp
;
282 if (mergelist_cnt
== mergelist_size
) {
284 mergelist_parents
= realloc_array(mergelist_parents
,
285 struct filter_struct
*,
287 if (!mergelist_parents
)
288 out_of_memory("add_rule");
290 if (DEBUG_GTE(FILTER
, 2)) {
291 rprintf(FINFO
, "[%s] activating mergelist #%d%s\n",
292 who_am_i(), mergelist_cnt
, lp
->debug_type
);
294 mergelist_parents
[mergelist_cnt
++] = ret
;
296 ret
->u
.slash_cnt
= slash_cnt
;
298 ret
->match_flags
= mflags
;
301 ret
->next
= listp
->head
;
302 listp
->head
= listp
->tail
= ret
;
304 ret
->next
= listp
->tail
->next
;
305 listp
->tail
->next
= ret
;
310 static void clear_filter_list(struct filter_list_struct
*listp
)
313 /* Truncate any inherited items from the local list. */
314 listp
->tail
->next
= NULL
;
315 /* Now free everything that is left. */
316 free_filters(listp
->head
);
319 listp
->head
= listp
->tail
= NULL
;
322 /* This returns an expanded (absolute) filename for the merge-file name if
323 * the name has any slashes in it OR if the parent_dirscan var is True;
324 * otherwise it returns the original merge_file name. If the len_ptr value
325 * is non-NULL the merge_file name is limited by the referenced length
326 * value and will be updated with the length of the resulting name. We
327 * always return a name that is null terminated, even if the merge_file
329 static char *parse_merge_name(const char *merge_file
, unsigned int *len_ptr
,
330 unsigned int prefix_skip
)
332 static char buf
[MAXPATHLEN
];
333 char *fn
, tmpbuf
[MAXPATHLEN
];
336 if (!parent_dirscan
&& *merge_file
!= '/') {
337 /* Return the name unchanged it doesn't have any slashes. */
339 const char *p
= merge_file
+ *len_ptr
;
340 while (--p
> merge_file
&& *p
!= '/') {}
341 if (p
== merge_file
) {
342 strlcpy(buf
, merge_file
, *len_ptr
+ 1);
345 } else if (strchr(merge_file
, '/') == NULL
)
346 return (char *)merge_file
;
349 fn
= *merge_file
== '/' ? buf
: tmpbuf
;
350 if (sanitize_paths
) {
351 const char *r
= prefix_skip
? "/" : NULL
;
352 /* null-terminate the name if it isn't already */
353 if (len_ptr
&& merge_file
[*len_ptr
]) {
354 char *to
= fn
== buf
? tmpbuf
: buf
;
355 strlcpy(to
, merge_file
, *len_ptr
+ 1);
358 if (!sanitize_path(fn
, merge_file
, r
, dirbuf_depth
, SP_DEFAULT
)) {
359 rprintf(FERROR
, "merge-file name overflows: %s\n",
365 strlcpy(fn
, merge_file
, len_ptr
? *len_ptr
+ 1 : MAXPATHLEN
);
366 fn_len
= clean_fname(fn
, CFN_COLLAPSE_DOT_DOT_DIRS
);
369 /* If the name isn't in buf yet, it's wasn't absolute. */
371 int d_len
= dirbuf_len
- prefix_skip
;
372 if (d_len
+ fn_len
>= MAXPATHLEN
) {
373 rprintf(FERROR
, "merge-file name overflows: %s\n", fn
);
376 memcpy(buf
, dirbuf
+ prefix_skip
, d_len
);
377 memcpy(buf
+ d_len
, fn
, fn_len
+ 1);
378 fn_len
= clean_fname(buf
, CFN_COLLAPSE_DOT_DOT_DIRS
);
386 /* Sets the dirbuf and dirbuf_len values. */
387 void set_filter_dir(const char *dir
, unsigned int dirlen
)
391 memcpy(dirbuf
, curr_dir
, curr_dir_len
);
392 dirbuf
[curr_dir_len
] = '/';
393 len
= curr_dir_len
+ 1;
394 if (len
+ dirlen
>= MAXPATHLEN
)
398 memcpy(dirbuf
+ len
, dir
, dirlen
);
399 dirbuf
[dirlen
+ len
] = '\0';
400 dirbuf_len
= clean_fname(dirbuf
, CFN_COLLAPSE_DOT_DOT_DIRS
);
401 if (dirbuf_len
> 1 && dirbuf
[dirbuf_len
-1] == '.'
402 && dirbuf
[dirbuf_len
-2] == '/')
405 dirbuf
[dirbuf_len
++] = '/';
406 dirbuf
[dirbuf_len
] = '\0';
408 dirbuf_depth
= count_dir_elements(dirbuf
+ module_dirlen
);
411 /* This routine takes a per-dir merge-file entry and finishes its setup.
412 * If the name has a path portion then we check to see if it refers to a
413 * parent directory of the first transfer dir. If it does, we scan all the
414 * dirs from that point through the parent dir of the transfer dir looking
415 * for the per-dir merge-file in each one. */
416 static BOOL
setup_merge_file(int mergelist_num
, struct filter_struct
*ex
,
417 struct filter_list_struct
*lp
)
419 char buf
[MAXPATHLEN
];
420 char *x
, *y
, *pat
= ex
->pattern
;
423 if (!(x
= parse_merge_name(pat
, NULL
, 0)) || *x
!= '/')
426 if (DEBUG_GTE(FILTER
, 2)) {
427 rprintf(FINFO
, "[%s] performing parent_dirscan for mergelist #%d%s\n",
428 who_am_i(), mergelist_num
, lp
->debug_type
);
432 ex
->pattern
= strdup(y
+1);
436 strlcpy(buf
, x
, MAXPATHLEN
);
438 pathjoin(buf
, MAXPATHLEN
, dirbuf
, x
);
440 len
= clean_fname(buf
, CFN_COLLAPSE_DOT_DOT_DIRS
);
441 if (len
!= 1 && len
< MAXPATHLEN
-1) {
445 /* This ensures that the specified dir is a parent of the transfer. */
446 for (x
= buf
, y
= dirbuf
; *x
&& *x
== *y
; x
++, y
++) {}
448 y
+= strlen(y
); /* nope -- skip the scan */
450 parent_dirscan
= True
;
452 char save
[MAXPATHLEN
];
453 strlcpy(save
, y
, MAXPATHLEN
);
455 dirbuf_len
= y
- dirbuf
;
456 strlcpy(x
, ex
->pattern
, MAXPATHLEN
- (x
- buf
));
457 parse_filter_file(lp
, buf
, ex
->match_flags
, XFLG_ANCHORED2ABS
);
458 if (ex
->match_flags
& MATCHFLG_NO_INHERIT
) {
459 /* Free the undesired rules to clean up any per-dir
460 * mergelists they defined. Otherwise pop_local_filters
461 * may crash trying to restore nonexistent state for
462 * those mergelists. */
463 free_filters(lp
->head
);
467 strlcpy(y
, save
, MAXPATHLEN
);
468 while ((*x
++ = *y
++) != '/') {}
470 /* Save current head for freeing when the mergelist becomes inactive. */
471 lp
->parent_dirscan_head
= lp
->head
;
472 parent_dirscan
= False
;
473 if (DEBUG_GTE(FILTER
, 2)) {
474 rprintf(FINFO
, "[%s] completed parent_dirscan for mergelist #%d%s\n",
475 who_am_i(), mergelist_num
, lp
->debug_type
);
481 struct local_filter_state
{
483 struct filter_list_struct mergelists
[0];
486 /* Each time rsync changes to a new directory it call this function to
487 * handle all the per-dir merge-files. The "dir" value is the current path
488 * relative to curr_dir (which might not be null-terminated). We copy it
489 * into dirbuf so that we can easily append a file name on the end. */
490 void *push_local_filters(const char *dir
, unsigned int dirlen
)
492 struct local_filter_state
*push
;
495 set_filter_dir(dir
, dirlen
);
496 if (DEBUG_GTE(FILTER
, 2)) {
497 rprintf(FINFO
, "[%s] pushing local filters for %s\n",
501 if (!mergelist_cnt
) {
502 /* No old state to save and no new merge files to push. */
506 push
= (struct local_filter_state
*)new_array(char,
507 sizeof (struct local_filter_state
)
508 + mergelist_cnt
* sizeof (struct filter_list_struct
));
510 out_of_memory("push_local_filters");
512 push
->mergelist_cnt
= mergelist_cnt
;
513 for (i
= 0; i
< mergelist_cnt
; i
++) {
514 memcpy(&push
->mergelists
[i
], mergelist_parents
[i
]->u
.mergelist
,
515 sizeof (struct filter_list_struct
));
518 /* Note: parse_filter_file() might increase mergelist_cnt, so keep
519 * this loop separate from the above loop. */
520 for (i
= 0; i
< mergelist_cnt
; i
++) {
521 struct filter_struct
*ex
= mergelist_parents
[i
];
522 struct filter_list_struct
*lp
= ex
->u
.mergelist
;
524 if (DEBUG_GTE(FILTER
, 2)) {
525 rprintf(FINFO
, "[%s] pushing mergelist #%d%s\n",
526 who_am_i(), i
, lp
->debug_type
);
529 lp
->tail
= NULL
; /* Switch any local rules to inherited. */
530 if (ex
->match_flags
& MATCHFLG_NO_INHERIT
)
533 if (ex
->match_flags
& MATCHFLG_FINISH_SETUP
) {
534 ex
->match_flags
&= ~MATCHFLG_FINISH_SETUP
;
535 if (setup_merge_file(i
, ex
, lp
))
536 set_filter_dir(dir
, dirlen
);
539 if (strlcpy(dirbuf
+ dirbuf_len
, ex
->pattern
,
540 MAXPATHLEN
- dirbuf_len
) < MAXPATHLEN
- dirbuf_len
) {
541 parse_filter_file(lp
, dirbuf
, ex
->match_flags
,
544 io_error
|= IOERR_GENERAL
;
546 "cannot add local filter rules in long-named directory: %s\n",
549 dirbuf
[dirbuf_len
] = '\0';
555 void pop_local_filters(void *mem
)
557 struct local_filter_state
*pop
= (struct local_filter_state
*)mem
;
559 int old_mergelist_cnt
= pop
? pop
->mergelist_cnt
: 0;
561 if (DEBUG_GTE(FILTER
, 2))
562 rprintf(FINFO
, "[%s] popping local filters\n", who_am_i());
564 for (i
= mergelist_cnt
; i
-- > 0; ) {
565 struct filter_struct
*ex
= mergelist_parents
[i
];
566 struct filter_list_struct
*lp
= ex
->u
.mergelist
;
568 if (DEBUG_GTE(FILTER
, 2)) {
569 rprintf(FINFO
, "[%s] popping mergelist #%d%s\n",
570 who_am_i(), i
, lp
->debug_type
);
573 clear_filter_list(lp
);
575 if (i
>= old_mergelist_cnt
) {
576 /* This mergelist does not exist in the state to be
577 * restored. Free its parent_dirscan list to clean up
578 * any per-dir mergelists defined there so we don't
579 * crash trying to restore nonexistent state for them
580 * below. (Counterpart to setup_merge_file call in
581 * push_local_filters. Must be done here, not in
582 * free_filter, for LIFO order.) */
583 if (DEBUG_GTE(FILTER
, 2)) {
584 rprintf(FINFO
, "[%s] freeing parent_dirscan filters of mergelist #%d%s\n",
585 who_am_i(), i
, ex
->u
.mergelist
->debug_type
);
587 free_filters(lp
->parent_dirscan_head
);
588 lp
->parent_dirscan_head
= NULL
;
592 /* If we cleaned things up properly, the only still-active mergelists
593 * should be those with a state to be restored. */
594 assert(mergelist_cnt
== old_mergelist_cnt
);
597 /* No state to restore. */
601 for (i
= 0; i
< mergelist_cnt
; i
++) {
602 memcpy(mergelist_parents
[i
]->u
.mergelist
, &pop
->mergelists
[i
],
603 sizeof (struct filter_list_struct
));
609 void change_local_filter_dir(const char *dname
, int dlen
, int dir_depth
)
611 static int cur_depth
= -1;
612 static void *filt_array
[MAXPATHLEN
/2+1];
615 for ( ; cur_depth
>= 0; cur_depth
--) {
616 if (filt_array
[cur_depth
]) {
617 pop_local_filters(filt_array
[cur_depth
]);
618 filt_array
[cur_depth
] = NULL
;
624 assert(dir_depth
< MAXPATHLEN
/2+1);
626 for ( ; cur_depth
>= dir_depth
; cur_depth
--) {
627 if (filt_array
[cur_depth
]) {
628 pop_local_filters(filt_array
[cur_depth
]);
629 filt_array
[cur_depth
] = NULL
;
633 cur_depth
= dir_depth
;
634 filt_array
[cur_depth
] = push_local_filters(dname
, dlen
);
637 static int rule_matches(const char *fname
, struct filter_struct
*ex
, int name_is_dir
)
639 int slash_handling
, str_cnt
= 0, anchored_match
= 0;
640 int ret_match
= ex
->match_flags
& MATCHFLG_NEGATE
? 0 : 1;
641 char *p
, *pattern
= ex
->pattern
;
642 const char *strings
[16]; /* more than enough */
643 const char *name
= fname
+ (*fname
== '/');
648 if (!ex
->u
.slash_cnt
&& !(ex
->match_flags
& MATCHFLG_WILD2
)) {
649 /* If the pattern does not have any slashes AND it does
650 * not have a "**" (which could match a slash), then we
651 * just match the name portion of the path. */
652 if ((p
= strrchr(name
,'/')) != NULL
)
654 } else if (ex
->match_flags
& MATCHFLG_ABS_PATH
&& *fname
!= '/'
655 && curr_dir_len
> module_dirlen
+ 1) {
656 /* If we're matching against an absolute-path pattern,
657 * we need to prepend our full path info. */
658 strings
[str_cnt
++] = curr_dir
+ module_dirlen
+ 1;
659 strings
[str_cnt
++] = "/";
660 } else if (ex
->match_flags
& MATCHFLG_WILD2_PREFIX
&& *fname
!= '/') {
661 /* Allow "**"+"/" to match at the start of the string. */
662 strings
[str_cnt
++] = "/";
664 strings
[str_cnt
++] = name
;
666 /* Allow a trailing "/"+"***" to match the directory. */
667 if (ex
->match_flags
& MATCHFLG_WILD3_SUFFIX
)
668 strings
[str_cnt
++] = "/";
669 } else if (ex
->match_flags
& MATCHFLG_DIRECTORY
)
671 strings
[str_cnt
] = NULL
;
673 if (*pattern
== '/') {
678 if (!anchored_match
&& ex
->u
.slash_cnt
679 && !(ex
->match_flags
& MATCHFLG_WILD2
)) {
680 /* A non-anchored match with an infix slash and no "**"
681 * needs to match the last slash_cnt+1 name elements. */
682 slash_handling
= ex
->u
.slash_cnt
+ 1;
683 } else if (!anchored_match
&& !(ex
->match_flags
& MATCHFLG_WILD2_PREFIX
)
684 && ex
->match_flags
& MATCHFLG_WILD2
) {
685 /* A non-anchored match with an infix or trailing "**" (but not
686 * a prefixed "**") needs to try matching after every slash. */
689 /* The pattern matches only at the start of the path or name. */
693 if (ex
->match_flags
& MATCHFLG_WILD
) {
694 if (wildmatch_array(pattern
, strings
, slash_handling
))
696 } else if (str_cnt
> 1) {
697 if (litmatch_array(pattern
, strings
, slash_handling
))
699 } else if (anchored_match
) {
700 if (strcmp(name
, pattern
) == 0)
703 int l1
= strlen(name
);
704 int l2
= strlen(pattern
);
706 strcmp(name
+(l1
-l2
),pattern
) == 0 &&
707 (l1
==l2
|| name
[l1
-(l2
+1)] == '/')) {
716 static void report_filter_result(enum logcode code
, char const *name
,
717 struct filter_struct
const *ent
,
718 int name_is_dir
, const char *type
)
720 /* If a trailing slash is present to match only directories,
721 * then it is stripped out by add_rule(). So as a special
722 * case we add it back in here. */
724 if (DEBUG_GTE(FILTER
, 1)) {
725 static char *actions
[2][2]
726 = { {"show", "hid"}, {"risk", "protect"} };
727 const char *w
= who_am_i();
728 rprintf(code
, "[%s] %sing %s %s because of pattern %s%s%s\n",
729 w
, actions
[*w
!='s'][!(ent
->match_flags
&MATCHFLG_INCLUDE
)],
730 name_is_dir
? "directory" : "file", name
, ent
->pattern
,
731 ent
->match_flags
& MATCHFLG_DIRECTORY
? "/" : "", type
);
737 * Return -1 if file "name" is defined to be excluded by the specified
738 * exclude list, 1 if it is included, and 0 if it was not matched.
740 int check_filter(struct filter_list_struct
*listp
, enum logcode code
,
741 const char *name
, int name_is_dir
)
743 struct filter_struct
*ent
;
745 for (ent
= listp
->head
; ent
; ent
= ent
->next
) {
746 if (ignore_perishable
&& ent
->match_flags
& MATCHFLG_PERISHABLE
)
748 if (ent
->match_flags
& MATCHFLG_PERDIR_MERGE
) {
749 int rc
= check_filter(ent
->u
.mergelist
, code
, name
,
755 if (ent
->match_flags
& MATCHFLG_CVS_IGNORE
) {
756 int rc
= check_filter(&cvs_filter_list
, code
, name
,
762 if (rule_matches(name
, ent
, name_is_dir
)) {
763 report_filter_result(code
, name
, ent
, name_is_dir
,
765 return ent
->match_flags
& MATCHFLG_INCLUDE
? 1 : -1;
772 #define RULE_STRCMP(s,r) rule_strcmp((s), (r), sizeof (r) - 1)
774 static const uchar
*rule_strcmp(const uchar
*str
, const char *rule
, int rule_len
)
776 if (strncmp((char*)str
, rule
, rule_len
) != 0)
778 if (isspace(str
[rule_len
]) || str
[rule_len
] == '_' || !str
[rule_len
])
779 return str
+ rule_len
- 1;
780 if (str
[rule_len
] == ',')
781 return str
+ rule_len
;
785 /* Get the next include/exclude arg from the string. The token will not
786 * be '\0' terminated, so use the returned length to limit the string.
787 * Also, be sure to add this length to the returned pointer before passing
788 * it back to ask for the next token. This routine parses the "!" (list-
789 * clearing) token and (depending on the mflags) the various prefixes.
790 * The *mflags_ptr value will be set on exit to the new MATCHFLG_* bits
791 * for the current token. */
792 static const char *parse_rule_tok(const char *p
, uint32 mflags
, int xflags
,
793 unsigned int *len_ptr
, uint32
*mflags_ptr
)
795 const uchar
*s
= (const uchar
*)p
;
799 if (mflags
& MATCHFLG_WORD_SPLIT
) {
800 /* Skip over any initial whitespace. */
803 /* Update to point to real start of rule. */
809 new_mflags
= mflags
& MATCHFLGS_FROM_CONTAINER
;
811 /* Figure out what kind of a filter rule "s" is pointing at. Note
812 * that if MATCHFLG_NO_PREFIXES is set, the rule is either an include
813 * or an exclude based on the inheritance of the MATCHFLG_INCLUDE
814 * flag (above). XFLG_OLD_PREFIXES indicates a compatibility mode
815 * for old include/exclude patterns where just "+ " and "- " are
816 * allowed as optional prefixes. */
817 if (mflags
& MATCHFLG_NO_PREFIXES
) {
818 if (*s
== '!' && mflags
& MATCHFLG_CVS_IGNORE
)
819 new_mflags
|= MATCHFLG_CLEAR_LIST
; /* Tentative! */
820 } else if (xflags
& XFLG_OLD_PREFIXES
) {
821 if (*s
== '-' && s
[1] == ' ') {
822 new_mflags
&= ~MATCHFLG_INCLUDE
;
824 } else if (*s
== '+' && s
[1] == ' ') {
825 new_mflags
|= MATCHFLG_INCLUDE
;
827 } else if (*s
== '!')
828 new_mflags
|= MATCHFLG_CLEAR_LIST
; /* Tentative! */
830 char ch
= 0, *mods
= "";
833 if ((s
= RULE_STRCMP(s
, "clear")) != NULL
)
837 if ((s
= RULE_STRCMP(s
, "dir-merge")) != NULL
)
841 if ((s
= RULE_STRCMP(s
, "exclude")) != NULL
)
845 if ((s
= RULE_STRCMP(s
, "hide")) != NULL
)
849 if ((s
= RULE_STRCMP(s
, "include")) != NULL
)
853 if ((s
= RULE_STRCMP(s
, "merge")) != NULL
)
857 if ((s
= RULE_STRCMP(s
, "protect")) != NULL
)
861 if ((s
= RULE_STRCMP(s
, "risk")) != NULL
)
865 if ((s
= RULE_STRCMP(s
, "show")) != NULL
)
876 new_mflags
|= MATCHFLG_PERDIR_MERGE
877 | MATCHFLG_FINISH_SETUP
;
880 new_mflags
|= MATCHFLG_MERGE_FILE
;
881 mods
= MODIFIERS_INCL_EXCL MODIFIERS_MERGE_FILE
;
884 new_mflags
|= MATCHFLG_INCLUDE
;
887 mods
= MODIFIERS_INCL_EXCL
;
890 new_mflags
|= MATCHFLG_INCLUDE
;
893 new_mflags
|= MATCHFLG_SENDER_SIDE
;
894 mods
= MODIFIERS_HIDE_PROTECT
;
897 new_mflags
|= MATCHFLG_INCLUDE
;
900 new_mflags
|= MATCHFLG_RECEIVER_SIDE
;
901 mods
= MODIFIERS_HIDE_PROTECT
;
904 new_mflags
|= MATCHFLG_CLEAR_LIST
;
908 rprintf(FERROR
, "Unknown filter rule: `%s'\n", p
);
909 exit_cleanup(RERR_SYNTAX
);
911 while (mods
&& *++s
&& *s
!= ' ' && *s
!= '_') {
912 if (strchr(mods
, *s
) == NULL
) {
913 if (mflags
& MATCHFLG_WORD_SPLIT
&& isspace(*s
)) {
919 "invalid modifier sequence at '%c' in filter rule: %s\n",
921 exit_cleanup(RERR_SYNTAX
);
925 if (new_mflags
& MATCHFLG_NO_PREFIXES
)
927 new_mflags
|= MATCHFLG_NO_PREFIXES
;
930 if (new_mflags
& MATCHFLG_NO_PREFIXES
)
932 new_mflags
|= MATCHFLG_NO_PREFIXES
936 new_mflags
|= MATCHFLG_ABS_PATH
;
939 new_mflags
|= MATCHFLG_NEGATE
;
942 if (new_mflags
& MATCHFLG_NO_PREFIXES
)
944 new_mflags
|= MATCHFLG_NO_PREFIXES
945 | MATCHFLG_WORD_SPLIT
946 | MATCHFLG_NO_INHERIT
947 | MATCHFLG_CVS_IGNORE
;
950 new_mflags
|= MATCHFLG_EXCLUDE_SELF
;
953 new_mflags
|= MATCHFLG_NO_INHERIT
;
956 new_mflags
|= MATCHFLG_PERISHABLE
;
959 new_mflags
|= MATCHFLG_RECEIVER_SIDE
;
962 new_mflags
|= MATCHFLG_SENDER_SIDE
;
965 new_mflags
|= MATCHFLG_WORD_SPLIT
;
973 if (mflags
& MATCHFLG_WORD_SPLIT
) {
975 /* Token ends at whitespace or the end of the string. */
976 while (!isspace(*cp
) && *cp
!= '\0')
980 len
= strlen((char*)s
);
982 if (new_mflags
& MATCHFLG_CLEAR_LIST
) {
983 if (!(mflags
& MATCHFLG_NO_PREFIXES
)
984 && !(xflags
& XFLG_OLD_PREFIXES
) && len
) {
986 "'!' rule has trailing characters: %s\n", p
);
987 exit_cleanup(RERR_SYNTAX
);
990 new_mflags
&= ~MATCHFLG_CLEAR_LIST
;
991 } else if (!len
&& !(new_mflags
& MATCHFLG_CVS_IGNORE
)) {
992 rprintf(FERROR
, "unexpected end of filter rule: %s\n", p
);
993 exit_cleanup(RERR_SYNTAX
);
996 /* --delete-excluded turns an un-modified include/exclude into a
997 * sender-side rule. We also affect per-dir merge files that take
998 * no prefixes as a simple optimization. */
1000 && !(new_mflags
& (MATCHFLG_RECEIVER_SIDE
|MATCHFLG_SENDER_SIDE
))
1001 && (!(new_mflags
& MATCHFLG_PERDIR_MERGE
)
1002 || new_mflags
& MATCHFLG_NO_PREFIXES
))
1003 new_mflags
|= MATCHFLG_SENDER_SIDE
;
1006 *mflags_ptr
= new_mflags
;
1007 return (const char *)s
;
1011 static char default_cvsignore
[] =
1012 /* These default ignored items come from the CVS manual. */
1013 "RCS SCCS CVS CVS.adm RCSLOG cvslog.* tags TAGS"
1014 " .make.state .nse_depinfo *~ #* .#* ,* _$* *$"
1015 " *.old *.bak *.BAK *.orig *.rej .del-*"
1016 " *.a *.olb *.o *.obj *.so *.exe"
1017 " *.Z *.elc *.ln core"
1018 /* The rest we added to suit ourself. */
1019 " .svn/ .git/ .bzr/";
1021 static void get_cvs_excludes(uint32 mflags
)
1023 static int initialized
= 0;
1024 char *p
, fname
[MAXPATHLEN
];
1030 parse_rule(&cvs_filter_list
, default_cvsignore
,
1031 mflags
| (protocol_version
>= 30 ? MATCHFLG_PERISHABLE
: 0),
1034 p
= module_id
>= 0 && lp_use_chroot(module_id
) ? "/" : getenv("HOME");
1035 if (p
&& pathjoin(fname
, MAXPATHLEN
, p
, ".cvsignore") < MAXPATHLEN
)
1036 parse_filter_file(&cvs_filter_list
, fname
, mflags
, 0);
1038 parse_rule(&cvs_filter_list
, getenv("CVSIGNORE"), mflags
, 0);
1042 void parse_rule(struct filter_list_struct
*listp
, const char *pattern
,
1043 uint32 mflags
, int xflags
)
1045 unsigned int pat_len
;
1053 /* Remember that the returned string is NOT '\0' terminated! */
1054 cp
= parse_rule_tok(pattern
, mflags
, xflags
,
1055 &pat_len
, &new_mflags
);
1059 pattern
= cp
+ pat_len
;
1061 if (pat_len
>= MAXPATHLEN
) {
1062 rprintf(FERROR
, "discarding over-long filter: %.*s\n",
1067 if (new_mflags
& MATCHFLG_CLEAR_LIST
) {
1068 if (DEBUG_GTE(FILTER
, 2)) {
1070 "[%s] clearing filter list%s\n",
1071 who_am_i(), listp
->debug_type
);
1073 clear_filter_list(listp
);
1077 if (new_mflags
& MATCHFLG_MERGE_FILE
) {
1084 if (new_mflags
& MATCHFLG_EXCLUDE_SELF
) {
1085 const char *name
= cp
+ len
;
1086 while (name
> cp
&& name
[-1] != '/') name
--;
1088 add_rule(listp
, name
, len
, 0, 0);
1089 new_mflags
&= ~MATCHFLG_EXCLUDE_SELF
;
1092 if (new_mflags
& MATCHFLG_PERDIR_MERGE
) {
1093 if (parent_dirscan
) {
1094 if (!(p
= parse_merge_name(cp
, &len
,
1097 add_rule(listp
, p
, len
, new_mflags
, 0);
1101 if (!(p
= parse_merge_name(cp
, &len
, 0)))
1103 parse_filter_file(listp
, p
, new_mflags
,
1109 add_rule(listp
, cp
, pat_len
, new_mflags
, xflags
);
1111 if (new_mflags
& MATCHFLG_CVS_IGNORE
1112 && !(new_mflags
& MATCHFLG_MERGE_FILE
))
1113 get_cvs_excludes(new_mflags
);
1118 void parse_filter_file(struct filter_list_struct
*listp
, const char *fname
,
1119 uint32 mflags
, int xflags
)
1122 char line
[BIGPATHBUFLEN
];
1123 char *eob
= line
+ sizeof line
- 1;
1124 int word_split
= mflags
& MATCHFLG_WORD_SPLIT
;
1126 if (!fname
|| !*fname
)
1129 if (*fname
!= '-' || fname
[1] || am_server
) {
1130 if (daemon_filter_list
.head
) {
1131 strlcpy(line
, fname
, sizeof line
);
1132 clean_fname(line
, CFN_COLLAPSE_DOT_DOT_DIRS
);
1133 if (check_filter(&daemon_filter_list
, FLOG
, line
, 0) < 0)
1136 fp
= fopen(line
, "rb");
1138 fp
= fopen(fname
, "rb");
1142 if (DEBUG_GTE(FILTER
, 2)) {
1143 rprintf(FINFO
, "[%s] parse_filter_file(%s,%x,%x)%s\n",
1144 who_am_i(), fname
, mflags
, xflags
,
1145 fp
? "" : " [not found]");
1149 if (xflags
& XFLG_FATAL_ERRORS
) {
1150 rsyserr(FERROR
, errno
,
1151 "failed to open %sclude file %s",
1152 mflags
& MATCHFLG_INCLUDE
? "in" : "ex",
1154 exit_cleanup(RERR_FILEIO
);
1158 dirbuf
[dirbuf_len
] = '\0';
1162 int ch
, overflow
= 0;
1164 if ((ch
= getc(fp
)) == EOF
) {
1165 if (ferror(fp
) && errno
== EINTR
) {
1171 if (word_split
&& isspace(ch
))
1173 if (eol_nulls
? !ch
: (ch
== '\n' || ch
== '\r'))
1181 rprintf(FERROR
, "discarding over-long filter: %s...\n", line
);
1185 /* Skip an empty token and (when line parsing) comments. */
1186 if (*line
&& (word_split
|| (*line
!= ';' && *line
!= '#')))
1187 parse_rule(listp
, line
, mflags
, xflags
);
1194 /* If the "for_xfer" flag is set, the prefix is made compatible with the
1195 * current protocol_version (if possible) or a NULL is returned (if not
1197 char *get_rule_prefix(int match_flags
, const char *pat
, int for_xfer
,
1198 unsigned int *plen_ptr
)
1200 static char buf
[MAX_RULE_PREFIX
+1];
1202 int legal_len
= for_xfer
&& protocol_version
< 29 ? 1 : MAX_RULE_PREFIX
-1;
1204 if (match_flags
& MATCHFLG_PERDIR_MERGE
) {
1208 } else if (match_flags
& MATCHFLG_INCLUDE
)
1210 else if (legal_len
!= 1
1211 || ((*pat
== '-' || *pat
== '+') && pat
[1] == ' '))
1216 if (match_flags
& MATCHFLG_NEGATE
)
1218 if (match_flags
& MATCHFLG_CVS_IGNORE
)
1221 if (match_flags
& MATCHFLG_NO_INHERIT
)
1223 if (match_flags
& MATCHFLG_WORD_SPLIT
)
1225 if (match_flags
& MATCHFLG_NO_PREFIXES
) {
1226 if (match_flags
& MATCHFLG_INCLUDE
)
1232 if (match_flags
& MATCHFLG_EXCLUDE_SELF
)
1234 if (match_flags
& MATCHFLG_SENDER_SIDE
1235 && (!for_xfer
|| protocol_version
>= 29))
1237 if (match_flags
& MATCHFLG_RECEIVER_SIDE
1238 && (!for_xfer
|| protocol_version
>= 29
1239 || (delete_excluded
&& am_sender
)))
1241 if (match_flags
& MATCHFLG_PERISHABLE
) {
1242 if (!for_xfer
|| protocol_version
>= 30)
1247 if (op
- buf
> legal_len
)
1253 *plen_ptr
= op
- buf
;
1257 static void send_rules(int f_out
, struct filter_list_struct
*flp
)
1259 struct filter_struct
*ent
, *prev
= NULL
;
1261 for (ent
= flp
->head
; ent
; ent
= ent
->next
) {
1262 unsigned int len
, plen
, dlen
;
1266 /* Note we need to check delete_excluded here in addition to
1267 * the code in parse_rule_tok() because some rules may have
1268 * been added before we found the --delete-excluded option.
1269 * We must also elide any CVS merge-file rules to avoid a
1270 * backward compatibility problem, and we elide any no-prefix
1271 * merge files as an optimization (since they can only have
1272 * include/exclude rules). */
1273 if (ent
->match_flags
& MATCHFLG_SENDER_SIDE
)
1274 elide
= am_sender
? 1 : -1;
1275 if (ent
->match_flags
& MATCHFLG_RECEIVER_SIDE
)
1276 elide
= elide
? 0 : am_sender
? -1 : 1;
1277 else if (delete_excluded
&& !elide
1278 && (!(ent
->match_flags
& MATCHFLG_PERDIR_MERGE
)
1279 || ent
->match_flags
& MATCHFLG_NO_PREFIXES
))
1280 elide
= am_sender
? 1 : -1;
1283 prev
->next
= ent
->next
;
1285 flp
->head
= ent
->next
;
1290 if (ent
->match_flags
& MATCHFLG_CVS_IGNORE
1291 && !(ent
->match_flags
& MATCHFLG_MERGE_FILE
)) {
1292 int f
= am_sender
|| protocol_version
< 29 ? f_out
: -2;
1293 send_rules(f
, &cvs_filter_list
);
1297 p
= get_rule_prefix(ent
->match_flags
, ent
->pattern
, 1, &plen
);
1300 "filter rules are too modern for remote rsync.\n");
1301 exit_cleanup(RERR_PROTOCOL
);
1305 len
= strlen(ent
->pattern
);
1306 dlen
= ent
->match_flags
& MATCHFLG_DIRECTORY
? 1 : 0;
1307 if (!(plen
+ len
+ dlen
))
1309 write_int(f_out
, plen
+ len
+ dlen
);
1311 write_buf(f_out
, p
, plen
);
1312 write_buf(f_out
, ent
->pattern
, len
);
1314 write_byte(f_out
, '/');
1319 /* This is only called by the client. */
1320 void send_filter_list(int f_out
)
1322 int receiver_wants_list
= prune_empty_dirs
1323 || (delete_mode
&& (!delete_excluded
|| protocol_version
>= 29));
1325 if (local_server
|| (am_sender
&& !receiver_wants_list
))
1327 if (cvs_exclude
&& am_sender
) {
1328 if (protocol_version
>= 29)
1329 parse_rule(&filter_list
, ":C", 0, 0);
1330 parse_rule(&filter_list
, "-C", 0, 0);
1333 send_rules(f_out
, &filter_list
);
1336 write_int(f_out
, 0);
1339 if (!am_sender
|| protocol_version
< 29)
1340 parse_rule(&filter_list
, ":C", 0, 0);
1342 parse_rule(&filter_list
, "-C", 0, 0);
1346 /* This is only called by the server. */
1347 void recv_filter_list(int f_in
)
1349 char line
[BIGPATHBUFLEN
];
1350 int xflags
= protocol_version
>= 29 ? 0 : XFLG_OLD_PREFIXES
;
1351 int receiver_wants_list
= prune_empty_dirs
1353 && (!delete_excluded
|| protocol_version
>= 29));
1356 if (!local_server
&& (am_sender
|| receiver_wants_list
)) {
1357 while ((len
= read_int(f_in
)) != 0) {
1358 if (len
>= sizeof line
)
1359 overflow_exit("recv_rules");
1360 read_sbuf(f_in
, line
, len
);
1361 parse_rule(&filter_list
, line
, 0, xflags
);
1366 if (local_server
|| am_sender
|| protocol_version
< 29)
1367 parse_rule(&filter_list
, ":C", 0, 0);
1368 if (local_server
|| am_sender
)
1369 parse_rule(&filter_list
, "-C", 0, 0);
1372 if (local_server
) /* filter out any rules that aren't for us. */
1373 send_rules(-1, &filter_list
);