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.
30 extern int local_server;
31 extern int prune_empty_dirs;
32 extern int ignore_perishable;
33 extern int delete_mode;
34 extern int delete_excluded;
35 extern int cvs_exclude;
36 extern int sanitize_paths;
37 extern int protocol_version;
40 extern char curr_dir[];
41 extern unsigned int curr_dir_len;
42 extern unsigned int module_dirlen;
44 struct filter_list_struct filter_list = { 0, 0, "" };
45 struct filter_list_struct cvs_filter_list = { 0, 0, " [global CVS]" };
46 struct filter_list_struct daemon_filter_list = { 0, 0, " [daemon]" };
48 /* Need room enough for ":MODS " prefix plus some room to grow. */
49 #define MAX_RULE_PREFIX (16)
51 #define MODIFIERS_MERGE_FILE "-+Cenw"
52 #define MODIFIERS_INCL_EXCL "/!Crsp"
53 #define MODIFIERS_HIDE_PROTECT "/!p"
55 /* The dirbuf is set by push_local_filters() to the current subdirectory
56 * relative to curr_dir that is being processed. The path always has a
57 * trailing slash appended, and the variable dirbuf_len contains the length
58 * of this path prefix. The path is always absolute. */
59 static char dirbuf[MAXPATHLEN+1];
60 static unsigned int dirbuf_len = 0;
61 static int dirbuf_depth;
63 /* This is True when we're scanning parent dirs for per-dir merge-files. */
64 static BOOL parent_dirscan = False;
66 /* This array contains a list of all the currently active per-dir merge
67 * files. This makes it easier to save the appropriate values when we
68 * "push" down into each subdirectory. */
69 static struct filter_struct **mergelist_parents;
70 static int mergelist_cnt = 0;
71 static int mergelist_size = 0;
73 /* Each filter_list_struct describes a singly-linked list by keeping track
74 * of both the head and tail pointers. The list is slightly unusual in that
75 * a parent-dir's content can be appended to the end of the local list in a
76 * special way: the last item in the local list has its "next" pointer set
77 * to point to the inherited list, but the local list's tail pointer points
78 * at the end of the local list. Thus, if the local list is empty, the head
79 * will be pointing at the inherited content but the tail will be NULL. To
80 * help you visualize this, here are the possible list arrangements:
82 * Completely Empty Local Content Only
83 * ================================== ====================================
84 * head -> NULL head -> Local1 -> Local2 -> NULL
85 * tail -> NULL tail -------------^
87 * Inherited Content Only Both Local and Inherited Content
88 * ================================== ====================================
89 * head -> Parent1 -> Parent2 -> NULL head -> L1 -> L2 -> P1 -> P2 -> NULL
90 * tail -> NULL tail ---------^
92 * This means that anyone wanting to traverse the whole list to use it just
93 * needs to start at the head and use the "next" pointers until it goes
94 * NULL. To add new local content, we insert the item after the tail item
95 * and update the tail (obviously, if "tail" was NULL, we insert it at the
96 * head). To clear the local list, WE MUST NOT FREE THE INHERITED CONTENT
97 * because it is shared between the current list and our parent list(s).
98 * The easiest way to handle this is to simply truncate the list after the
99 * tail item and then free the local list from the head. When inheriting
100 * the list for a new local dir, we just save off the filter_list_struct
101 * values (so we can pop back to them later) and set the tail to NULL.
104 static void free_filter(struct filter_struct *ex)
106 if (ex->match_flags & MATCHFLG_PERDIR_MERGE) {
107 free(ex->u.mergelist->debug_type);
108 free(ex->u.mergelist);
115 /* Build a filter structure given a filter pattern. The value in "pat"
116 * is not null-terminated. */
117 static void add_rule(struct filter_list_struct *listp, const char *pat,
118 unsigned int pat_len, uint32 mflags, int xflags)
120 struct filter_struct *ret;
122 unsigned int pre_len, slash_cnt = 0;
125 rprintf(FINFO, "[%s] add_rule(%s%.*s%s)%s\n",
126 who_am_i(), get_rule_prefix(mflags, pat, 0, NULL),
128 (mflags & MATCHFLG_DIRECTORY) ? "/" : "",
132 /* These flags also indicate that we're reading a list that
133 * needs to be filtered now, not post-filtered later. */
134 if (xflags & (XFLG_ANCHORED2ABS|XFLG_ABS_IF_SLASH)) {
135 uint32 mf = mflags & (MATCHFLG_RECEIVER_SIDE|MATCHFLG_SENDER_SIDE);
137 if (mf == MATCHFLG_RECEIVER_SIDE)
140 if (mf == MATCHFLG_SENDER_SIDE)
145 if (!(ret = new0(struct filter_struct)))
146 out_of_memory("add_rule");
148 if (pat_len > 1 && pat[pat_len-1] == '/') {
150 mflags |= MATCHFLG_DIRECTORY;
153 for (cp = pat; cp < pat + pat_len; cp++) {
158 if (!(mflags & (MATCHFLG_ABS_PATH | MATCHFLG_MERGE_FILE))
159 && ((xflags & (XFLG_ANCHORED2ABS|XFLG_ABS_IF_SLASH) && *pat == '/')
160 || (xflags & XFLG_ABS_IF_SLASH && slash_cnt))) {
161 mflags |= MATCHFLG_ABS_PATH;
163 pre_len = dirbuf_len - module_dirlen - 1;
169 if (!(ret->pattern = new_array(char, pre_len + pat_len + 1)))
170 out_of_memory("add_rule");
172 memcpy(ret->pattern, dirbuf + module_dirlen, pre_len);
173 for (cp = ret->pattern; cp < ret->pattern + pre_len; cp++) {
178 strlcpy(ret->pattern + pre_len, pat, pat_len + 1);
181 if (strpbrk(ret->pattern, "*[?")) {
182 mflags |= MATCHFLG_WILD;
183 if ((cp = strstr(ret->pattern, "**")) != NULL) {
184 mflags |= MATCHFLG_WILD2;
185 /* If the pattern starts with **, note that. */
186 if (cp == ret->pattern)
187 mflags |= MATCHFLG_WILD2_PREFIX;
188 /* If the pattern ends with ***, note that. */
190 && ret->pattern[pat_len-3] == '*'
191 && ret->pattern[pat_len-2] == '*'
192 && ret->pattern[pat_len-1] == '*')
193 mflags |= MATCHFLG_WILD3_SUFFIX;
197 if (mflags & MATCHFLG_PERDIR_MERGE) {
198 struct filter_list_struct *lp;
202 if ((cp = strrchr(ret->pattern, '/')) != NULL)
207 /* If the local merge file was already mentioned, don't
209 for (i = 0; i < mergelist_cnt; i++) {
210 struct filter_struct *ex = mergelist_parents[i];
211 const char *s = strrchr(ex->pattern, '/');
217 if (len == pat_len - (cp - ret->pattern)
218 && memcmp(s, cp, len) == 0) {
224 if (!(lp = new_array(struct filter_list_struct, 1)))
225 out_of_memory("add_rule");
226 lp->head = lp->tail = NULL;
227 if (asprintf(&lp->debug_type, " [per-dir %s]", cp) < 0)
228 out_of_memory("add_rule");
229 ret->u.mergelist = lp;
231 if (mergelist_cnt == mergelist_size) {
233 mergelist_parents = realloc_array(mergelist_parents,
234 struct filter_struct *,
236 if (!mergelist_parents)
237 out_of_memory("add_rule");
239 mergelist_parents[mergelist_cnt++] = ret;
241 ret->u.slash_cnt = slash_cnt;
243 ret->match_flags = mflags;
246 ret->next = listp->head;
247 listp->head = listp->tail = ret;
249 ret->next = listp->tail->next;
250 listp->tail->next = ret;
255 static void clear_filter_list(struct filter_list_struct *listp)
258 struct filter_struct *ent, *next;
259 /* Truncate any inherited items from the local list. */
260 listp->tail->next = NULL;
261 /* Now free everything that is left. */
262 for (ent = listp->head; ent; ent = next) {
268 listp->head = listp->tail = NULL;
271 /* This returns an expanded (absolute) filename for the merge-file name if
272 * the name has any slashes in it OR if the parent_dirscan var is True;
273 * otherwise it returns the original merge_file name. If the len_ptr value
274 * is non-NULL the merge_file name is limited by the referenced length
275 * value and will be updated with the length of the resulting name. We
276 * always return a name that is null terminated, even if the merge_file
278 static char *parse_merge_name(const char *merge_file, unsigned int *len_ptr,
279 unsigned int prefix_skip)
281 static char buf[MAXPATHLEN];
282 char *fn, tmpbuf[MAXPATHLEN];
285 if (!parent_dirscan && *merge_file != '/') {
286 /* Return the name unchanged it doesn't have any slashes. */
288 const char *p = merge_file + *len_ptr;
289 while (--p > merge_file && *p != '/') {}
290 if (p == merge_file) {
291 strlcpy(buf, merge_file, *len_ptr + 1);
294 } else if (strchr(merge_file, '/') == NULL)
295 return (char *)merge_file;
298 fn = *merge_file == '/' ? buf : tmpbuf;
299 if (sanitize_paths) {
300 const char *r = prefix_skip ? "/" : NULL;
301 /* null-terminate the name if it isn't already */
302 if (len_ptr && merge_file[*len_ptr]) {
303 char *to = fn == buf ? tmpbuf : buf;
304 strlcpy(to, merge_file, *len_ptr + 1);
307 if (!sanitize_path(fn, merge_file, r, dirbuf_depth, SP_DEFAULT)) {
308 rprintf(FERROR, "merge-file name overflows: %s\n",
314 strlcpy(fn, merge_file, len_ptr ? *len_ptr + 1 : MAXPATHLEN);
315 fn_len = clean_fname(fn, CFN_COLLAPSE_DOT_DOT_DIRS);
318 /* If the name isn't in buf yet, it's wasn't absolute. */
320 if (dirbuf_len + fn_len >= MAXPATHLEN) {
321 rprintf(FERROR, "merge-file name overflows: %s\n", fn);
324 memcpy(buf, dirbuf + prefix_skip, dirbuf_len - prefix_skip);
325 memcpy(buf + dirbuf_len - prefix_skip, fn, fn_len + 1);
326 fn_len = clean_fname(buf, CFN_COLLAPSE_DOT_DOT_DIRS);
334 /* Sets the dirbuf and dirbuf_len values. */
335 void set_filter_dir(const char *dir, unsigned int dirlen)
339 memcpy(dirbuf, curr_dir, curr_dir_len);
340 dirbuf[curr_dir_len] = '/';
341 len = curr_dir_len + 1;
342 if (len + dirlen >= MAXPATHLEN)
346 memcpy(dirbuf + len, dir, dirlen);
347 dirbuf[dirlen + len] = '\0';
348 dirbuf_len = clean_fname(dirbuf, CFN_COLLAPSE_DOT_DOT_DIRS);
349 if (dirbuf_len > 1 && dirbuf[dirbuf_len-1] == '.'
350 && dirbuf[dirbuf_len-2] == '/')
353 dirbuf[dirbuf_len++] = '/';
354 dirbuf[dirbuf_len] = '\0';
356 dirbuf_depth = count_dir_elements(dirbuf + module_dirlen);
359 /* This routine takes a per-dir merge-file entry and finishes its setup.
360 * If the name has a path portion then we check to see if it refers to a
361 * parent directory of the first transfer dir. If it does, we scan all the
362 * dirs from that point through the parent dir of the transfer dir looking
363 * for the per-dir merge-file in each one. */
364 static BOOL setup_merge_file(struct filter_struct *ex,
365 struct filter_list_struct *lp)
367 char buf[MAXPATHLEN];
368 char *x, *y, *pat = ex->pattern;
371 if (!(x = parse_merge_name(pat, NULL, 0)) || *x != '/')
376 ex->pattern = strdup(y+1);
380 strlcpy(buf, x, MAXPATHLEN);
382 pathjoin(buf, MAXPATHLEN, dirbuf, x);
384 len = clean_fname(buf, CFN_COLLAPSE_DOT_DOT_DIRS);
385 if (len != 1 && len < MAXPATHLEN-1) {
389 /* This ensures that the specified dir is a parent of the transfer. */
390 for (x = buf, y = dirbuf; *x && *x == *y; x++, y++) {}
392 y += strlen(y); /* nope -- skip the scan */
394 parent_dirscan = True;
396 char save[MAXPATHLEN];
397 strlcpy(save, y, MAXPATHLEN);
399 dirbuf_len = y - dirbuf;
400 strlcpy(x, ex->pattern, MAXPATHLEN - (x - buf));
401 parse_filter_file(lp, buf, ex->match_flags, XFLG_ANCHORED2ABS);
402 if (ex->match_flags & MATCHFLG_NO_INHERIT)
405 strlcpy(y, save, MAXPATHLEN);
406 while ((*x++ = *y++) != '/') {}
408 parent_dirscan = False;
413 /* Each time rsync changes to a new directory it call this function to
414 * handle all the per-dir merge-files. The "dir" value is the current path
415 * relative to curr_dir (which might not be null-terminated). We copy it
416 * into dirbuf so that we can easily append a file name on the end. */
417 void *push_local_filters(const char *dir, unsigned int dirlen)
419 struct filter_list_struct *ap, *push;
422 set_filter_dir(dir, dirlen);
427 push = new_array(struct filter_list_struct, mergelist_cnt);
429 out_of_memory("push_local_filters");
431 for (i = 0, ap = push; i < mergelist_cnt; i++) {
432 memcpy(ap++, mergelist_parents[i]->u.mergelist,
433 sizeof (struct filter_list_struct));
436 /* Note: parse_filter_file() might increase mergelist_cnt, so keep
437 * this loop separate from the above loop. */
438 for (i = 0; i < mergelist_cnt; i++) {
439 struct filter_struct *ex = mergelist_parents[i];
440 struct filter_list_struct *lp = ex->u.mergelist;
443 rprintf(FINFO, "[%s] pushing filter list%s\n",
444 who_am_i(), lp->debug_type);
447 lp->tail = NULL; /* Switch any local rules to inherited. */
448 if (ex->match_flags & MATCHFLG_NO_INHERIT)
451 if (ex->match_flags & MATCHFLG_FINISH_SETUP) {
452 ex->match_flags &= ~MATCHFLG_FINISH_SETUP;
453 if (setup_merge_file(ex, lp))
454 set_filter_dir(dir, dirlen);
457 if (strlcpy(dirbuf + dirbuf_len, ex->pattern,
458 MAXPATHLEN - dirbuf_len) < MAXPATHLEN - dirbuf_len) {
459 parse_filter_file(lp, dirbuf, ex->match_flags,
462 io_error |= IOERR_GENERAL;
464 "cannot add local filter rules in long-named directory: %s\n",
467 dirbuf[dirbuf_len] = '\0';
473 void pop_local_filters(void *mem)
475 struct filter_list_struct *ap, *pop = (struct filter_list_struct*)mem;
478 for (i = mergelist_cnt; i-- > 0; ) {
479 struct filter_struct *ex = mergelist_parents[i];
480 struct filter_list_struct *lp = ex->u.mergelist;
483 rprintf(FINFO, "[%s] popping filter list%s\n",
484 who_am_i(), lp->debug_type);
487 clear_filter_list(lp);
493 for (i = 0, ap = pop; i < mergelist_cnt; i++) {
494 memcpy(mergelist_parents[i]->u.mergelist, ap++,
495 sizeof (struct filter_list_struct));
501 void change_local_filter_dir(const char *dname, int dlen, int dir_depth)
503 static int cur_depth = -1;
504 static void *filt_array[MAXPATHLEN/2+1];
507 for ( ; cur_depth >= 0; cur_depth--) {
508 if (filt_array[cur_depth]) {
509 pop_local_filters(filt_array[cur_depth]);
510 filt_array[cur_depth] = NULL;
516 assert(dir_depth < MAXPATHLEN/2+1);
518 for ( ; cur_depth >= dir_depth; cur_depth--) {
519 if (filt_array[cur_depth]) {
520 pop_local_filters(filt_array[cur_depth]);
521 filt_array[cur_depth] = NULL;
525 cur_depth = dir_depth;
526 filt_array[cur_depth] = push_local_filters(dname, dlen);
529 static int rule_matches(const char *fname, struct filter_struct *ex, int name_is_dir)
531 int slash_handling, str_cnt = 0, anchored_match = 0;
532 int ret_match = ex->match_flags & MATCHFLG_NEGATE ? 0 : 1;
533 char *p, *pattern = ex->pattern;
534 const char *strings[16]; /* more than enough */
535 const char *name = fname + (*fname == '/');
540 if (!ex->u.slash_cnt && !(ex->match_flags & MATCHFLG_WILD2)) {
541 /* If the pattern does not have any slashes AND it does
542 * not have a "**" (which could match a slash), then we
543 * just match the name portion of the path. */
544 if ((p = strrchr(name,'/')) != NULL)
546 } else if (ex->match_flags & MATCHFLG_ABS_PATH && *fname != '/'
547 && curr_dir_len > module_dirlen + 1) {
548 /* If we're matching against an absolute-path pattern,
549 * we need to prepend our full path info. */
550 strings[str_cnt++] = curr_dir + module_dirlen + 1;
551 strings[str_cnt++] = "/";
552 } else if (ex->match_flags & MATCHFLG_WILD2_PREFIX && *fname != '/') {
553 /* Allow "**"+"/" to match at the start of the string. */
554 strings[str_cnt++] = "/";
556 strings[str_cnt++] = name;
558 /* Allow a trailing "/"+"***" to match the directory. */
559 if (ex->match_flags & MATCHFLG_WILD3_SUFFIX)
560 strings[str_cnt++] = "/";
561 } else if (ex->match_flags & MATCHFLG_DIRECTORY)
563 strings[str_cnt] = NULL;
565 if (*pattern == '/') {
570 if (!anchored_match && ex->u.slash_cnt
571 && !(ex->match_flags & MATCHFLG_WILD2)) {
572 /* A non-anchored match with an infix slash and no "**"
573 * needs to match the last slash_cnt+1 name elements. */
574 slash_handling = ex->u.slash_cnt + 1;
575 } else if (!anchored_match && !(ex->match_flags & MATCHFLG_WILD2_PREFIX)
576 && ex->match_flags & MATCHFLG_WILD2) {
577 /* A non-anchored match with an infix or trailing "**" (but not
578 * a prefixed "**") needs to try matching after every slash. */
581 /* The pattern matches only at the start of the path or name. */
585 if (ex->match_flags & MATCHFLG_WILD) {
586 if (wildmatch_array(pattern, strings, slash_handling))
588 } else if (str_cnt > 1) {
589 if (litmatch_array(pattern, strings, slash_handling))
591 } else if (anchored_match) {
592 if (strcmp(name, pattern) == 0)
595 int l1 = strlen(name);
596 int l2 = strlen(pattern);
598 strcmp(name+(l1-l2),pattern) == 0 &&
599 (l1==l2 || name[l1-(l2+1)] == '/')) {
608 static void report_filter_result(char const *name,
609 struct filter_struct const *ent,
610 int name_is_dir, const char *type)
612 /* If a trailing slash is present to match only directories,
613 * then it is stripped out by add_rule(). So as a special
614 * case we add it back in here. */
617 static char *actions[2][2]
618 = { {"show", "hid"}, {"risk", "protect"} };
619 const char *w = who_am_i();
620 rprintf(FINFO, "[%s] %sing %s %s because of pattern %s%s%s\n",
621 w, actions[*w!='s'][!(ent->match_flags&MATCHFLG_INCLUDE)],
622 name_is_dir ? "directory" : "file", name, ent->pattern,
623 ent->match_flags & MATCHFLG_DIRECTORY ? "/" : "", type);
629 * Return -1 if file "name" is defined to be excluded by the specified
630 * exclude list, 1 if it is included, and 0 if it was not matched.
632 int check_filter(struct filter_list_struct *listp, const char *name, int name_is_dir)
634 struct filter_struct *ent;
636 for (ent = listp->head; ent; ent = ent->next) {
637 if (ignore_perishable && ent->match_flags & MATCHFLG_PERISHABLE)
639 if (ent->match_flags & MATCHFLG_PERDIR_MERGE) {
640 int rc = check_filter(ent->u.mergelist, name,
646 if (ent->match_flags & MATCHFLG_CVS_IGNORE) {
647 int rc = check_filter(&cvs_filter_list, name,
653 if (rule_matches(name, ent, name_is_dir)) {
654 report_filter_result(name, ent, name_is_dir,
656 return ent->match_flags & MATCHFLG_INCLUDE ? 1 : -1;
663 #define RULE_STRCMP(s,r) rule_strcmp((s), (r), sizeof (r) - 1)
665 static const uchar *rule_strcmp(const uchar *str, const char *rule, int rule_len)
667 if (strncmp((char*)str, rule, rule_len) != 0)
669 if (isspace(str[rule_len]) || str[rule_len] == '_' || !str[rule_len])
670 return str + rule_len - 1;
671 if (str[rule_len] == ',')
672 return str + rule_len;
676 /* Get the next include/exclude arg from the string. The token will not
677 * be '\0' terminated, so use the returned length to limit the string.
678 * Also, be sure to add this length to the returned pointer before passing
679 * it back to ask for the next token. This routine parses the "!" (list-
680 * clearing) token and (depending on the mflags) the various prefixes.
681 * The *mflags_ptr value will be set on exit to the new MATCHFLG_* bits
682 * for the current token. */
683 static const char *parse_rule_tok(const char *p, uint32 mflags, int xflags,
684 unsigned int *len_ptr, uint32 *mflags_ptr)
686 const uchar *s = (const uchar *)p;
690 if (mflags & MATCHFLG_WORD_SPLIT) {
691 /* Skip over any initial whitespace. */
694 /* Update to point to real start of rule. */
700 new_mflags = mflags & MATCHFLGS_FROM_CONTAINER;
702 /* Figure out what kind of a filter rule "s" is pointing at. Note
703 * that if MATCHFLG_NO_PREFIXES is set, the rule is either an include
704 * or an exclude based on the inheritance of the MATCHFLG_INCLUDE
705 * flag (above). XFLG_OLD_PREFIXES indicates a compatibility mode
706 * for old include/exclude patterns where just "+ " and "- " are
707 * allowed as optional prefixes. */
708 if (mflags & MATCHFLG_NO_PREFIXES) {
709 if (*s == '!' && mflags & MATCHFLG_CVS_IGNORE)
710 new_mflags |= MATCHFLG_CLEAR_LIST; /* Tentative! */
711 } else if (xflags & XFLG_OLD_PREFIXES) {
712 if (*s == '-' && s[1] == ' ') {
713 new_mflags &= ~MATCHFLG_INCLUDE;
715 } else if (*s == '+' && s[1] == ' ') {
716 new_mflags |= MATCHFLG_INCLUDE;
718 } else if (*s == '!')
719 new_mflags |= MATCHFLG_CLEAR_LIST; /* Tentative! */
721 char ch = 0, *mods = "";
724 if ((s = RULE_STRCMP(s, "clear")) != NULL)
728 if ((s = RULE_STRCMP(s, "dir-merge")) != NULL)
732 if ((s = RULE_STRCMP(s, "exclude")) != NULL)
736 if ((s = RULE_STRCMP(s, "hide")) != NULL)
740 if ((s = RULE_STRCMP(s, "include")) != NULL)
744 if ((s = RULE_STRCMP(s, "merge")) != NULL)
748 if ((s = RULE_STRCMP(s, "protect")) != NULL)
752 if ((s = RULE_STRCMP(s, "risk")) != NULL)
756 if ((s = RULE_STRCMP(s, "show")) != NULL)
767 new_mflags |= MATCHFLG_PERDIR_MERGE
768 | MATCHFLG_FINISH_SETUP;
771 new_mflags |= MATCHFLG_MERGE_FILE;
772 mods = MODIFIERS_INCL_EXCL MODIFIERS_MERGE_FILE;
775 new_mflags |= MATCHFLG_INCLUDE;
778 mods = MODIFIERS_INCL_EXCL;
781 new_mflags |= MATCHFLG_INCLUDE;
784 new_mflags |= MATCHFLG_SENDER_SIDE;
785 mods = MODIFIERS_HIDE_PROTECT;
788 new_mflags |= MATCHFLG_INCLUDE;
791 new_mflags |= MATCHFLG_RECEIVER_SIDE;
792 mods = MODIFIERS_HIDE_PROTECT;
795 new_mflags |= MATCHFLG_CLEAR_LIST;
799 rprintf(FERROR, "Unknown filter rule: `%s'\n", p);
800 exit_cleanup(RERR_SYNTAX);
802 while (mods && *++s && *s != ' ' && *s != '_') {
803 if (strchr(mods, *s) == NULL) {
804 if (mflags & MATCHFLG_WORD_SPLIT && isspace(*s)) {
810 "invalid modifier sequence at '%c' in filter rule: %s\n",
812 exit_cleanup(RERR_SYNTAX);
816 if (new_mflags & MATCHFLG_NO_PREFIXES)
818 new_mflags |= MATCHFLG_NO_PREFIXES;
821 if (new_mflags & MATCHFLG_NO_PREFIXES)
823 new_mflags |= MATCHFLG_NO_PREFIXES
827 new_mflags |= MATCHFLG_ABS_PATH;
830 new_mflags |= MATCHFLG_NEGATE;
833 if (new_mflags & MATCHFLG_NO_PREFIXES)
835 new_mflags |= MATCHFLG_NO_PREFIXES
836 | MATCHFLG_WORD_SPLIT
837 | MATCHFLG_NO_INHERIT
838 | MATCHFLG_CVS_IGNORE;
841 new_mflags |= MATCHFLG_EXCLUDE_SELF;
844 new_mflags |= MATCHFLG_NO_INHERIT;
847 new_mflags |= MATCHFLG_PERISHABLE;
850 new_mflags |= MATCHFLG_RECEIVER_SIDE;
853 new_mflags |= MATCHFLG_SENDER_SIDE;
856 new_mflags |= MATCHFLG_WORD_SPLIT;
864 if (mflags & MATCHFLG_WORD_SPLIT) {
866 /* Token ends at whitespace or the end of the string. */
867 while (!isspace(*cp) && *cp != '\0')
871 len = strlen((char*)s);
873 if (new_mflags & MATCHFLG_CLEAR_LIST) {
874 if (!(mflags & MATCHFLG_NO_PREFIXES)
875 && !(xflags & XFLG_OLD_PREFIXES) && len) {
877 "'!' rule has trailing characters: %s\n", p);
878 exit_cleanup(RERR_SYNTAX);
881 new_mflags &= ~MATCHFLG_CLEAR_LIST;
882 } else if (!len && !(new_mflags & MATCHFLG_CVS_IGNORE)) {
883 rprintf(FERROR, "unexpected end of filter rule: %s\n", p);
884 exit_cleanup(RERR_SYNTAX);
887 /* --delete-excluded turns an un-modified include/exclude into a
888 * sender-side rule. We also affect per-dir merge files that take
889 * no prefixes as a simple optimization. */
891 && !(new_mflags & (MATCHFLG_RECEIVER_SIDE|MATCHFLG_SENDER_SIDE))
892 && (!(new_mflags & MATCHFLG_PERDIR_MERGE)
893 || new_mflags & MATCHFLG_NO_PREFIXES))
894 new_mflags |= MATCHFLG_SENDER_SIDE;
897 *mflags_ptr = new_mflags;
898 return (const char *)s;
902 static char default_cvsignore[] =
903 /* These default ignored items come from the CVS manual. */
904 "RCS SCCS CVS CVS.adm RCSLOG cvslog.* tags TAGS"
905 " .make.state .nse_depinfo *~ #* .#* ,* _$* *$"
906 " *.old *.bak *.BAK *.orig *.rej .del-*"
907 " *.a *.olb *.o *.obj *.so *.exe"
908 " *.Z *.elc *.ln core"
909 /* The rest we added to suit ourself. */
910 " .svn/ .git/ .bzr/";
912 static void get_cvs_excludes(uint32 mflags)
914 static int initialized = 0;
915 char *p, fname[MAXPATHLEN];
921 parse_rule(&cvs_filter_list, default_cvsignore,
922 mflags | (protocol_version >= 30 ? MATCHFLG_PERISHABLE : 0),
925 p = module_id >= 0 && lp_use_chroot(module_id) ? "/" : getenv("HOME");
926 if (p && pathjoin(fname, MAXPATHLEN, p, ".cvsignore") < MAXPATHLEN)
927 parse_filter_file(&cvs_filter_list, fname, mflags, 0);
929 parse_rule(&cvs_filter_list, getenv("CVSIGNORE"), mflags, 0);
933 void parse_rule(struct filter_list_struct *listp, const char *pattern,
934 uint32 mflags, int xflags)
936 unsigned int pat_len;
944 /* Remember that the returned string is NOT '\0' terminated! */
945 cp = parse_rule_tok(pattern, mflags, xflags,
946 &pat_len, &new_mflags);
950 pattern = cp + pat_len;
952 if (pat_len >= MAXPATHLEN) {
953 rprintf(FERROR, "discarding over-long filter: %.*s\n",
958 if (new_mflags & MATCHFLG_CLEAR_LIST) {
961 "[%s] clearing filter list%s\n",
962 who_am_i(), listp->debug_type);
964 clear_filter_list(listp);
968 if (new_mflags & MATCHFLG_MERGE_FILE) {
975 if (new_mflags & MATCHFLG_EXCLUDE_SELF) {
976 const char *name = cp + len;
977 while (name > cp && name[-1] != '/') name--;
979 add_rule(listp, name, len, 0, 0);
980 new_mflags &= ~MATCHFLG_EXCLUDE_SELF;
983 if (new_mflags & MATCHFLG_PERDIR_MERGE) {
984 if (parent_dirscan) {
985 if (!(p = parse_merge_name(cp, &len,
988 add_rule(listp, p, len, new_mflags, 0);
992 if (!(p = parse_merge_name(cp, &len, 0)))
994 parse_filter_file(listp, p, new_mflags,
1000 add_rule(listp, cp, pat_len, new_mflags, xflags);
1002 if (new_mflags & MATCHFLG_CVS_IGNORE
1003 && !(new_mflags & MATCHFLG_MERGE_FILE))
1004 get_cvs_excludes(new_mflags);
1009 void parse_filter_file(struct filter_list_struct *listp, const char *fname,
1010 uint32 mflags, int xflags)
1013 char line[BIGPATHBUFLEN];
1014 char *eob = line + sizeof line - 1;
1015 int word_split = mflags & MATCHFLG_WORD_SPLIT;
1017 if (!fname || !*fname)
1020 if (*fname != '-' || fname[1] || am_server) {
1021 if (daemon_filter_list.head) {
1022 strlcpy(line, fname, sizeof line);
1023 clean_fname(line, CFN_COLLAPSE_DOT_DOT_DIRS);
1024 if (check_filter(&daemon_filter_list, line, 0) < 0)
1027 fp = fopen(line, "rb");
1029 fp = fopen(fname, "rb");
1034 rprintf(FINFO, "[%s] parse_filter_file(%s,%x,%x)%s\n",
1035 who_am_i(), fname, mflags, xflags,
1036 fp ? "" : " [not found]");
1040 if (xflags & XFLG_FATAL_ERRORS) {
1041 rsyserr(FERROR, errno,
1042 "failed to open %sclude file %s",
1043 mflags & MATCHFLG_INCLUDE ? "in" : "ex",
1045 exit_cleanup(RERR_FILEIO);
1049 dirbuf[dirbuf_len] = '\0';
1053 int ch, overflow = 0;
1055 if ((ch = getc(fp)) == EOF) {
1056 if (ferror(fp) && errno == EINTR) {
1062 if (word_split && isspace(ch))
1064 if (eol_nulls? !ch : (ch == '\n' || ch == '\r'))
1072 rprintf(FERROR, "discarding over-long filter: %s...\n", line);
1076 /* Skip an empty token and (when line parsing) comments. */
1077 if (*line && (word_split || (*line != ';' && *line != '#')))
1078 parse_rule(listp, line, mflags, xflags);
1085 /* If the "for_xfer" flag is set, the prefix is made compatible with the
1086 * current protocol_version (if possible) or a NULL is returned (if not
1088 char *get_rule_prefix(int match_flags, const char *pat, int for_xfer,
1089 unsigned int *plen_ptr)
1091 static char buf[MAX_RULE_PREFIX+1];
1093 int legal_len = for_xfer && protocol_version < 29 ? 1 : MAX_RULE_PREFIX-1;
1095 if (match_flags & MATCHFLG_PERDIR_MERGE) {
1099 } else if (match_flags & MATCHFLG_INCLUDE)
1101 else if (legal_len != 1
1102 || ((*pat == '-' || *pat == '+') && pat[1] == ' '))
1107 if (match_flags & MATCHFLG_NEGATE)
1109 if (match_flags & MATCHFLG_CVS_IGNORE)
1112 if (match_flags & MATCHFLG_NO_INHERIT)
1114 if (match_flags & MATCHFLG_WORD_SPLIT)
1116 if (match_flags & MATCHFLG_NO_PREFIXES) {
1117 if (match_flags & MATCHFLG_INCLUDE)
1123 if (match_flags & MATCHFLG_EXCLUDE_SELF)
1125 if (match_flags & MATCHFLG_SENDER_SIDE
1126 && (!for_xfer || protocol_version >= 29))
1128 if (match_flags & MATCHFLG_RECEIVER_SIDE
1129 && (!for_xfer || protocol_version >= 29
1130 || (delete_excluded && am_sender)))
1132 if (match_flags & MATCHFLG_PERISHABLE) {
1133 if (!for_xfer || protocol_version >= 30)
1138 if (op - buf > legal_len)
1144 *plen_ptr = op - buf;
1148 static void send_rules(int f_out, struct filter_list_struct *flp)
1150 struct filter_struct *ent, *prev = NULL;
1152 for (ent = flp->head; ent; ent = ent->next) {
1153 unsigned int len, plen, dlen;
1157 /* Note we need to check delete_excluded here in addition to
1158 * the code in parse_rule_tok() because some rules may have
1159 * been added before we found the --delete-excluded option.
1160 * We must also elide any CVS merge-file rules to avoid a
1161 * backward compatibility problem, and we elide any no-prefix
1162 * merge files as an optimization (since they can only have
1163 * include/exclude rules). */
1164 if (ent->match_flags & MATCHFLG_SENDER_SIDE)
1165 elide = am_sender ? 1 : -1;
1166 if (ent->match_flags & MATCHFLG_RECEIVER_SIDE)
1167 elide = elide ? 0 : am_sender ? -1 : 1;
1168 else if (delete_excluded && !elide
1169 && (!(ent->match_flags & MATCHFLG_PERDIR_MERGE)
1170 || ent->match_flags & MATCHFLG_NO_PREFIXES))
1171 elide = am_sender ? 1 : -1;
1174 prev->next = ent->next;
1176 flp->head = ent->next;
1181 if (ent->match_flags & MATCHFLG_CVS_IGNORE
1182 && !(ent->match_flags & MATCHFLG_MERGE_FILE)) {
1183 int f = am_sender || protocol_version < 29 ? f_out : -2;
1184 send_rules(f, &cvs_filter_list);
1188 p = get_rule_prefix(ent->match_flags, ent->pattern, 1, &plen);
1191 "filter rules are too modern for remote rsync.\n");
1192 exit_cleanup(RERR_PROTOCOL);
1196 len = strlen(ent->pattern);
1197 dlen = ent->match_flags & MATCHFLG_DIRECTORY ? 1 : 0;
1198 if (!(plen + len + dlen))
1200 write_int(f_out, plen + len + dlen);
1202 write_buf(f_out, p, plen);
1203 write_buf(f_out, ent->pattern, len);
1205 write_byte(f_out, '/');
1210 /* This is only called by the client. */
1211 void send_filter_list(int f_out)
1213 int receiver_wants_list = prune_empty_dirs
1214 || (delete_mode && (!delete_excluded || protocol_version >= 29));
1216 if (local_server || (am_sender && !receiver_wants_list))
1218 if (cvs_exclude && am_sender) {
1219 if (protocol_version >= 29)
1220 parse_rule(&filter_list, ":C", 0, 0);
1221 parse_rule(&filter_list, "-C", 0, 0);
1224 send_rules(f_out, &filter_list);
1227 write_int(f_out, 0);
1230 if (!am_sender || protocol_version < 29)
1231 parse_rule(&filter_list, ":C", 0, 0);
1233 parse_rule(&filter_list, "-C", 0, 0);
1237 /* This is only called by the server. */
1238 void recv_filter_list(int f_in)
1240 char line[BIGPATHBUFLEN];
1241 int xflags = protocol_version >= 29 ? 0 : XFLG_OLD_PREFIXES;
1242 int receiver_wants_list = prune_empty_dirs
1244 && (!delete_excluded || protocol_version >= 29));
1247 if (!local_server && (am_sender || receiver_wants_list)) {
1248 while ((len = read_int(f_in)) != 0) {
1249 if (len >= sizeof line)
1250 overflow_exit("recv_rules");
1251 read_sbuf(f_in, line, len);
1252 parse_rule(&filter_list, line, 0, xflags);
1257 if (local_server || am_sender || protocol_version < 29)
1258 parse_rule(&filter_list, ":C", 0, 0);
1259 if (local_server || am_sender)
1260 parse_rule(&filter_list, "-C", 0, 0);
1263 if (local_server) /* filter out any rules that aren't for us. */
1264 send_rules(-1, &filter_list);