1 /* -*- c-file-style: "linux" -*-
3 * Copyright (C) 1996-2001 by Andrew Tridgell <tridge@samba.org>
4 * Copyright (C) 1996 by Paul Mackerras
5 * Copyright (C) 2002 by Martin Pool
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 /* a lot of this stuff was originally derived from GNU tar, although
23 it has now changed so much that it is hard to tell :) */
25 /* include/exclude cluestick added by Martin Pool <mbp@samba.org> */
36 extern int local_server;
37 extern int prune_empty_dirs;
38 extern int delete_mode;
39 extern int delete_excluded;
40 extern int cvs_exclude;
41 extern int sanitize_paths;
42 extern int protocol_version;
45 extern char curr_dir[];
46 extern unsigned int curr_dir_len;
47 extern unsigned int module_dirlen;
49 struct filter_list_struct filter_list = { 0, 0, "" };
50 struct filter_list_struct cvs_filter_list = { 0, 0, " [global CVS]" };
51 struct filter_list_struct server_filter_list = { 0, 0, " [daemon]" };
53 /* Need room enough for ":MODS " prefix plus some room to grow. */
54 #define MAX_RULE_PREFIX (16)
56 #define MODIFIERS_MERGE_FILE "-+Cenw"
57 #define MODIFIERS_INCL_EXCL "/!Crs"
58 #define MODIFIERS_HIDE_PROTECT "/!"
60 /* The dirbuf is set by push_local_filters() to the current subdirectory
61 * relative to curr_dir that is being processed. The path always has a
62 * trailing slash appended, and the variable dirbuf_len contains the length
63 * of this path prefix. The path is always absolute. */
64 static char dirbuf[MAXPATHLEN+1];
65 static unsigned int dirbuf_len = 0;
66 static int dirbuf_depth;
68 /* This is True when we're scanning parent dirs for per-dir merge-files. */
69 static BOOL parent_dirscan = False;
71 /* This array contains a list of all the currently active per-dir merge
72 * files. This makes it easier to save the appropriate values when we
73 * "push" down into each subdirectory. */
74 static struct filter_struct **mergelist_parents;
75 static int mergelist_cnt = 0;
76 static int mergelist_size = 0;
78 /* Each filter_list_struct describes a singly-linked list by keeping track
79 * of both the head and tail pointers. The list is slightly unusual in that
80 * a parent-dir's content can be appended to the end of the local list in a
81 * special way: the last item in the local list has its "next" pointer set
82 * to point to the inherited list, but the local list's tail pointer points
83 * at the end of the local list. Thus, if the local list is empty, the head
84 * will be pointing at the inherited content but the tail will be NULL. To
85 * help you visualize this, here are the possible list arrangements:
87 * Completely Empty Local Content Only
88 * ================================== ====================================
89 * head -> NULL head -> Local1 -> Local2 -> NULL
90 * tail -> NULL tail -------------^
92 * Inherited Content Only Both Local and Inherited Content
93 * ================================== ====================================
94 * head -> Parent1 -> Parent2 -> NULL head -> L1 -> L2 -> P1 -> P2 -> NULL
95 * tail -> NULL tail ---------^
97 * This means that anyone wanting to traverse the whole list to use it just
98 * needs to start at the head and use the "next" pointers until it goes
99 * NULL. To add new local content, we insert the item after the tail item
100 * and update the tail (obviously, if "tail" was NULL, we insert it at the
101 * head). To clear the local list, WE MUST NOT FREE THE INHERITED CONTENT
102 * because it is shared between the current list and our parent list(s).
103 * The easiest way to handle this is to simply truncate the list after the
104 * tail item and then free the local list from the head. When inheriting
105 * the list for a new local dir, we just save off the filter_list_struct
106 * values (so we can pop back to them later) and set the tail to NULL.
109 static void free_filter(struct filter_struct *ex)
111 if (ex->match_flags & MATCHFLG_PERDIR_MERGE) {
112 free(ex->u.mergelist->debug_type);
113 free(ex->u.mergelist);
120 /* Build a filter structure given a filter pattern. The value in "pat"
121 * is not null-terminated. */
122 static void add_rule(struct filter_list_struct *listp, const char *pat,
123 unsigned int pat_len, uint32 mflags, int xflags)
125 struct filter_struct *ret;
130 rprintf(FINFO, "[%s] add_rule(%s%.*s%s)%s\n",
131 who_am_i(), get_rule_prefix(mflags, pat, 0, NULL),
133 (mflags & MATCHFLG_DIRECTORY) ? "/" : "",
137 /* These flags also indicate that we're reading a list that
138 * needs to be filtered now, not post-filtered later. */
139 if (xflags & (XFLG_ANCHORED2ABS|XFLG_ABS_IF_SLASH)) {
140 uint32 mf = mflags & (MATCHFLG_RECEIVER_SIDE|MATCHFLG_SENDER_SIDE);
142 if (mf == MATCHFLG_RECEIVER_SIDE)
145 if (mf == MATCHFLG_SENDER_SIDE)
150 if (!(ret = new(struct filter_struct)))
151 out_of_memory("add_rule");
152 memset(ret, 0, sizeof ret[0]);
154 if (!(mflags & (MATCHFLG_ABS_PATH | MATCHFLG_MERGE_FILE))
155 && ((xflags & (XFLG_ANCHORED2ABS|XFLG_ABS_IF_SLASH) && *pat == '/')
156 || (xflags & XFLG_ABS_IF_SLASH && strchr(pat, '/') != NULL))) {
157 mflags |= MATCHFLG_ABS_PATH;
159 ex_len = dirbuf_len - module_dirlen - 1;
164 if (!(ret->pattern = new_array(char, ex_len + pat_len + 1)))
165 out_of_memory("add_rule");
167 memcpy(ret->pattern, dirbuf + module_dirlen, ex_len);
168 strlcpy(ret->pattern + ex_len, pat, pat_len + 1);
171 if (strpbrk(ret->pattern, "*[?")) {
172 mflags |= MATCHFLG_WILD;
173 if ((cp = strstr(ret->pattern, "**")) != NULL) {
174 mflags |= MATCHFLG_WILD2;
175 /* If the pattern starts with **, note that. */
176 if (cp == ret->pattern)
177 mflags |= MATCHFLG_WILD2_PREFIX;
178 /* If the pattern ends with ***, note that. */
180 && ret->pattern[pat_len-3] == '*'
181 && ret->pattern[pat_len-2] == '*'
182 && ret->pattern[pat_len-1] == '*')
183 mflags |= MATCHFLG_WILD3_SUFFIX;
187 if (pat_len > 1 && ret->pattern[pat_len-1] == '/') {
188 ret->pattern[pat_len-1] = 0;
189 mflags |= MATCHFLG_DIRECTORY;
192 if (mflags & MATCHFLG_PERDIR_MERGE) {
193 struct filter_list_struct *lp;
197 if ((cp = strrchr(ret->pattern, '/')) != NULL)
202 /* If the local merge file was already mentioned, don't
204 for (i = 0; i < mergelist_cnt; i++) {
205 struct filter_struct *ex = mergelist_parents[i];
206 const char *s = strrchr(ex->pattern, '/');
212 if (len == pat_len - (cp - ret->pattern)
213 && memcmp(s, cp, len) == 0) {
219 if (!(lp = new_array(struct filter_list_struct, 1)))
220 out_of_memory("add_rule");
221 lp->head = lp->tail = NULL;
222 if (asprintf(&lp->debug_type, " [per-dir %s]", cp) < 0)
223 out_of_memory("add_rule");
224 ret->u.mergelist = lp;
226 if (mergelist_cnt == mergelist_size) {
228 mergelist_parents = realloc_array(mergelist_parents,
229 struct filter_struct *,
231 if (!mergelist_parents)
232 out_of_memory("add_rule");
234 mergelist_parents[mergelist_cnt++] = ret;
236 for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
240 ret->match_flags = mflags;
243 ret->next = listp->head;
244 listp->head = listp->tail = ret;
246 ret->next = listp->tail->next;
247 listp->tail->next = ret;
252 static void clear_filter_list(struct filter_list_struct *listp)
255 struct filter_struct *ent, *next;
256 /* Truncate any inherited items from the local list. */
257 listp->tail->next = NULL;
258 /* Now free everything that is left. */
259 for (ent = listp->head; ent; ent = next) {
265 listp->head = listp->tail = NULL;
268 /* This returns an expanded (absolute) filename for the merge-file name if
269 * the name has any slashes in it OR if the parent_dirscan var is True;
270 * otherwise it returns the original merge_file name. If the len_ptr value
271 * is non-NULL the merge_file name is limited by the referenced length
272 * value and will be updated with the length of the resulting name. We
273 * always return a name that is null terminated, even if the merge_file
275 static char *parse_merge_name(const char *merge_file, unsigned int *len_ptr,
276 unsigned int prefix_skip)
278 static char buf[MAXPATHLEN];
279 char *fn, tmpbuf[MAXPATHLEN];
282 if (!parent_dirscan && *merge_file != '/') {
283 /* Return the name unchanged it doesn't have any slashes. */
285 const char *p = merge_file + *len_ptr;
286 while (--p > merge_file && *p != '/') {}
287 if (p == merge_file) {
288 strlcpy(buf, merge_file, *len_ptr + 1);
291 } else if (strchr(merge_file, '/') == NULL)
292 return (char *)merge_file;
295 fn = *merge_file == '/' ? buf : tmpbuf;
296 if (sanitize_paths) {
297 const char *r = prefix_skip ? "/" : NULL;
298 /* null-terminate the name if it isn't already */
299 if (len_ptr && merge_file[*len_ptr]) {
300 char *to = fn == buf ? tmpbuf : buf;
301 strlcpy(to, merge_file, *len_ptr + 1);
304 if (!sanitize_path(fn, merge_file, r, dirbuf_depth)) {
305 rprintf(FERROR, "merge-file name overflows: %s\n",
310 strlcpy(fn, merge_file, len_ptr ? *len_ptr + 1 : MAXPATHLEN);
318 if (dirbuf_len + fn_len >= MAXPATHLEN) {
319 rprintf(FERROR, "merge-file name overflows: %s\n", fn);
322 memcpy(buf, dirbuf + prefix_skip, dirbuf_len - prefix_skip);
323 memcpy(buf + dirbuf_len - prefix_skip, fn, fn_len + 1);
324 fn_len = clean_fname(buf, 1);
332 /* Sets the dirbuf and dirbuf_len values. */
333 void set_filter_dir(const char *dir, unsigned int dirlen)
337 memcpy(dirbuf, curr_dir, curr_dir_len);
338 dirbuf[curr_dir_len] = '/';
339 len = curr_dir_len + 1;
340 if (len + dirlen >= MAXPATHLEN)
344 memcpy(dirbuf + len, dir, dirlen);
345 dirbuf[dirlen + len] = '\0';
346 dirbuf_len = clean_fname(dirbuf, 1);
347 if (dirbuf_len > 1 && dirbuf[dirbuf_len-1] == '.'
348 && dirbuf[dirbuf_len-2] == '/')
351 dirbuf[dirbuf_len++] = '/';
352 dirbuf[dirbuf_len] = '\0';
354 dirbuf_depth = count_dir_elements(dirbuf + module_dirlen);
357 /* This routine takes a per-dir merge-file entry and finishes its setup.
358 * If the name has a path portion then we check to see if it refers to a
359 * parent directory of the first transfer dir. If it does, we scan all the
360 * dirs from that point through the parent dir of the transfer dir looking
361 * for the per-dir merge-file in each one. */
362 static BOOL setup_merge_file(struct filter_struct *ex,
363 struct filter_list_struct *lp)
365 char buf[MAXPATHLEN];
366 char *x, *y, *pat = ex->pattern;
369 if (!(x = parse_merge_name(pat, NULL, 0)) || *x != '/')
374 ex->pattern = strdup(y+1);
378 strlcpy(buf, x, MAXPATHLEN);
380 pathjoin(buf, MAXPATHLEN, dirbuf, x);
382 len = clean_fname(buf, 1);
383 if (len != 1 && len < MAXPATHLEN-1) {
387 /* This ensures that the specified dir is a parent of the transfer. */
388 for (x = buf, y = dirbuf; *x && *x == *y; x++, y++) {}
390 y += strlen(y); /* nope -- skip the scan */
392 parent_dirscan = True;
394 char save[MAXPATHLEN];
395 strlcpy(save, y, MAXPATHLEN);
397 dirbuf_len = y - dirbuf;
398 strlcpy(x, ex->pattern, MAXPATHLEN - (x - buf));
399 parse_filter_file(lp, buf, ex->match_flags, XFLG_ANCHORED2ABS);
400 if (ex->match_flags & MATCHFLG_NO_INHERIT)
403 strlcpy(y, save, MAXPATHLEN);
404 while ((*x++ = *y++) != '/') {}
406 parent_dirscan = False;
411 /* Each time rsync changes to a new directory it call this function to
412 * handle all the per-dir merge-files. The "dir" value is the current path
413 * relative to curr_dir (which might not be null-terminated). We copy it
414 * into dirbuf so that we can easily append a file name on the end. */
415 void *push_local_filters(const char *dir, unsigned int dirlen)
417 struct filter_list_struct *ap, *push;
420 set_filter_dir(dir, dirlen);
425 push = new_array(struct filter_list_struct, mergelist_cnt);
427 out_of_memory("push_local_filters");
429 for (i = 0, ap = push; i < mergelist_cnt; i++) {
430 memcpy(ap++, mergelist_parents[i]->u.mergelist,
431 sizeof (struct filter_list_struct));
434 /* Note: parse_filter_file() might increase mergelist_cnt, so keep
435 * this loop separate from the above loop. */
436 for (i = 0; i < mergelist_cnt; i++) {
437 struct filter_struct *ex = mergelist_parents[i];
438 struct filter_list_struct *lp = ex->u.mergelist;
441 rprintf(FINFO, "[%s] pushing filter list%s\n",
442 who_am_i(), lp->debug_type);
445 lp->tail = NULL; /* Switch any local rules to inherited. */
446 if (ex->match_flags & MATCHFLG_NO_INHERIT)
449 if (ex->match_flags & MATCHFLG_FINISH_SETUP) {
450 ex->match_flags &= ~MATCHFLG_FINISH_SETUP;
451 if (setup_merge_file(ex, lp))
452 set_filter_dir(dir, dirlen);
455 if (strlcpy(dirbuf + dirbuf_len, ex->pattern,
456 MAXPATHLEN - dirbuf_len) < MAXPATHLEN - dirbuf_len) {
457 parse_filter_file(lp, dirbuf, ex->match_flags,
460 io_error |= IOERR_GENERAL;
462 "cannot add local filter rules in long-named directory: %s\n",
465 dirbuf[dirbuf_len] = '\0';
471 void pop_local_filters(void *mem)
473 struct filter_list_struct *ap, *pop = (struct filter_list_struct*)mem;
476 for (i = mergelist_cnt; i-- > 0; ) {
477 struct filter_struct *ex = mergelist_parents[i];
478 struct filter_list_struct *lp = ex->u.mergelist;
481 rprintf(FINFO, "[%s] popping filter list%s\n",
482 who_am_i(), lp->debug_type);
485 clear_filter_list(lp);
491 for (i = 0, ap = pop; i < mergelist_cnt; i++) {
492 memcpy(mergelist_parents[i]->u.mergelist, ap++,
493 sizeof (struct filter_list_struct));
499 static int rule_matches(char *name, struct filter_struct *ex, int name_is_dir)
501 int slash_handling, str_cnt = 0, anchored_match = 0;
502 int ret_match = ex->match_flags & MATCHFLG_NEGATE ? 0 : 1;
503 char *p, *pattern = ex->pattern;
504 const char *strings[16]; /* more than enough */
509 if (!ex->u.slash_cnt && !(ex->match_flags & MATCHFLG_WILD2)) {
510 /* If the pattern does not have any slashes AND it does
511 * not have a "**" (which could match a slash), then we
512 * just match the name portion of the path. */
513 if ((p = strrchr(name,'/')) != NULL)
515 } else if (ex->match_flags & MATCHFLG_ABS_PATH && *name != '/'
516 && curr_dir_len > module_dirlen + 1) {
517 /* If we're matching against an absolute-path pattern,
518 * we need to prepend our full path info. */
519 strings[str_cnt++] = curr_dir + module_dirlen + 1;
520 strings[str_cnt++] = "/";
521 } else if (ex->match_flags & MATCHFLG_WILD2_PREFIX && *name != '/') {
522 /* Allow "**"+"/" to match at the start of the string. */
523 strings[str_cnt++] = "/";
525 strings[str_cnt++] = name;
527 /* Allow a trailing "/"+"***" to match the directory. */
528 if (ex->match_flags & MATCHFLG_WILD3_SUFFIX)
529 strings[str_cnt++] = "/";
530 } else if (ex->match_flags & MATCHFLG_DIRECTORY)
532 strings[str_cnt] = NULL;
534 if (*pattern == '/') {
537 if (strings[0][0] == '/')
541 if (!anchored_match && ex->u.slash_cnt
542 && !(ex->match_flags & MATCHFLG_WILD2)) {
543 /* A non-anchored match with an infix slash and no "**"
544 * needs to match the last slash_cnt+1 name elements. */
545 slash_handling = ex->u.slash_cnt + 1;
546 } else if (!anchored_match && !(ex->match_flags & MATCHFLG_WILD2_PREFIX)
547 && ex->match_flags & MATCHFLG_WILD2) {
548 /* A non-anchored match with an infix or trailing "**" (but not
549 * a prefixed "**") needs to try matching after every slash. */
552 /* The pattern matches only at the start of the path or name. */
556 if (ex->match_flags & MATCHFLG_WILD) {
557 if (wildmatch_array(pattern, strings, slash_handling))
559 } else if (str_cnt > 1) {
560 if (litmatch_array(pattern, strings, slash_handling))
562 } else if (anchored_match) {
563 if (strcmp(strings[0], pattern) == 0)
566 int l1 = strlen(name);
567 int l2 = strlen(pattern);
569 strcmp(name+(l1-l2),pattern) == 0 &&
570 (l1==l2 || name[l1-(l2+1)] == '/')) {
579 static void report_filter_result(char const *name,
580 struct filter_struct const *ent,
581 int name_is_dir, const char *type)
583 /* If a trailing slash is present to match only directories,
584 * then it is stripped out by add_rule(). So as a special
585 * case we add it back in here. */
588 static char *actions[2][2]
589 = { {"show", "hid"}, {"risk", "protect"} };
590 const char *w = who_am_i();
591 rprintf(FINFO, "[%s] %sing %s %s because of pattern %s%s%s\n",
592 w, actions[*w!='s'][!(ent->match_flags&MATCHFLG_INCLUDE)],
593 name_is_dir ? "directory" : "file", name, ent->pattern,
594 ent->match_flags & MATCHFLG_DIRECTORY ? "/" : "", type);
600 * Return -1 if file "name" is defined to be excluded by the specified
601 * exclude list, 1 if it is included, and 0 if it was not matched.
603 int check_filter(struct filter_list_struct *listp, char *name, int name_is_dir)
605 struct filter_struct *ent;
607 for (ent = listp->head; ent; ent = ent->next) {
608 if (ent->match_flags & MATCHFLG_PERDIR_MERGE) {
609 int rc = check_filter(ent->u.mergelist, name,
615 if (ent->match_flags & MATCHFLG_CVS_IGNORE) {
616 int rc = check_filter(&cvs_filter_list, name,
622 if (rule_matches(name, ent, name_is_dir)) {
623 report_filter_result(name, ent, name_is_dir,
625 return ent->match_flags & MATCHFLG_INCLUDE ? 1 : -1;
632 #define RULE_STRCMP(s,r) rule_strcmp((s), (r), sizeof (r) - 1)
634 static const uchar *rule_strcmp(const uchar *str, const char *rule, int rule_len)
636 if (strncmp((char*)str, rule, rule_len) != 0)
638 if (isspace(str[rule_len]) || str[rule_len] == '_' || !str[rule_len])
639 return str + rule_len - 1;
640 if (str[rule_len] == ',')
641 return str + rule_len;
645 /* Get the next include/exclude arg from the string. The token will not
646 * be '\0' terminated, so use the returned length to limit the string.
647 * Also, be sure to add this length to the returned pointer before passing
648 * it back to ask for the next token. This routine parses the "!" (list-
649 * clearing) token and (depending on the mflags) the various prefixes.
650 * The *mflags_ptr value will be set on exit to the new MATCHFLG_* bits
651 * for the current token. */
652 static const char *parse_rule_tok(const char *p, uint32 mflags, int xflags,
653 unsigned int *len_ptr, uint32 *mflags_ptr)
655 const uchar *s = (const uchar *)p;
659 if (mflags & MATCHFLG_WORD_SPLIT) {
660 /* Skip over any initial whitespace. */
663 /* Update to point to real start of rule. */
669 new_mflags = mflags & MATCHFLGS_FROM_CONTAINER;
671 /* Figure out what kind of a filter rule "s" is pointing at. Note
672 * that if MATCHFLG_NO_PREFIXES is set, the rule is either an include
673 * or an exclude based on the inheritance of the MATCHFLG_INCLUDE
674 * flag (above). XFLG_OLD_PREFIXES indicates a compatibility mode
675 * for old include/exclude patterns where just "+ " and "- " are
676 * allowed as optional prefixes. */
677 if (mflags & MATCHFLG_NO_PREFIXES) {
678 if (*s == '!' && mflags & MATCHFLG_CVS_IGNORE)
679 new_mflags |= MATCHFLG_CLEAR_LIST; /* Tentative! */
680 } else if (xflags & XFLG_OLD_PREFIXES) {
681 if (*s == '-' && s[1] == ' ') {
682 new_mflags &= ~MATCHFLG_INCLUDE;
684 } else if (*s == '+' && s[1] == ' ') {
685 new_mflags |= MATCHFLG_INCLUDE;
687 } else if (*s == '!')
688 new_mflags |= MATCHFLG_CLEAR_LIST; /* Tentative! */
690 char ch = 0, *mods = "";
693 if ((s = RULE_STRCMP(s, "clear")) != NULL)
697 if ((s = RULE_STRCMP(s, "dir-merge")) != NULL)
701 if ((s = RULE_STRCMP(s, "exclude")) != NULL)
705 if ((s = RULE_STRCMP(s, "hide")) != NULL)
709 if ((s = RULE_STRCMP(s, "include")) != NULL)
713 if ((s = RULE_STRCMP(s, "merge")) != NULL)
717 if ((s = RULE_STRCMP(s, "protect")) != NULL)
721 if ((s = RULE_STRCMP(s, "risk")) != NULL)
725 if ((s = RULE_STRCMP(s, "show")) != NULL)
736 new_mflags |= MATCHFLG_PERDIR_MERGE
737 | MATCHFLG_FINISH_SETUP;
740 new_mflags |= MATCHFLG_MERGE_FILE;
741 mods = MODIFIERS_INCL_EXCL MODIFIERS_MERGE_FILE;
744 new_mflags |= MATCHFLG_INCLUDE;
747 mods = MODIFIERS_INCL_EXCL;
750 new_mflags |= MATCHFLG_INCLUDE;
753 new_mflags |= MATCHFLG_SENDER_SIDE;
754 mods = MODIFIERS_HIDE_PROTECT;
757 new_mflags |= MATCHFLG_INCLUDE;
760 new_mflags |= MATCHFLG_RECEIVER_SIDE;
761 mods = MODIFIERS_HIDE_PROTECT;
764 new_mflags |= MATCHFLG_CLEAR_LIST;
768 rprintf(FERROR, "Unknown filter rule: `%s'\n", p);
769 exit_cleanup(RERR_SYNTAX);
771 while (mods && *++s && *s != ' ' && *s != '_') {
772 if (strchr(mods, *s) == NULL) {
773 if (mflags & MATCHFLG_WORD_SPLIT && isspace(*s)) {
779 "invalid modifier sequence at '%c' in filter rule: %s\n",
781 exit_cleanup(RERR_SYNTAX);
785 if (new_mflags & MATCHFLG_NO_PREFIXES)
787 new_mflags |= MATCHFLG_NO_PREFIXES;
790 if (new_mflags & MATCHFLG_NO_PREFIXES)
792 new_mflags |= MATCHFLG_NO_PREFIXES
796 new_mflags |= MATCHFLG_ABS_PATH;
799 new_mflags |= MATCHFLG_NEGATE;
802 if (new_mflags & MATCHFLG_NO_PREFIXES)
804 new_mflags |= MATCHFLG_NO_PREFIXES
805 | MATCHFLG_WORD_SPLIT
806 | MATCHFLG_NO_INHERIT
807 | MATCHFLG_CVS_IGNORE;
810 new_mflags |= MATCHFLG_EXCLUDE_SELF;
813 new_mflags |= MATCHFLG_NO_INHERIT;
816 new_mflags |= MATCHFLG_RECEIVER_SIDE;
819 new_mflags |= MATCHFLG_SENDER_SIDE;
822 new_mflags |= MATCHFLG_WORD_SPLIT;
830 if (mflags & MATCHFLG_WORD_SPLIT) {
832 /* Token ends at whitespace or the end of the string. */
833 while (!isspace(*cp) && *cp != '\0')
837 len = strlen((char*)s);
839 if (new_mflags & MATCHFLG_CLEAR_LIST) {
840 if (!(mflags & MATCHFLG_NO_PREFIXES)
841 && !(xflags & XFLG_OLD_PREFIXES) && len) {
843 "'!' rule has trailing characters: %s\n", p);
844 exit_cleanup(RERR_SYNTAX);
847 new_mflags &= ~MATCHFLG_CLEAR_LIST;
848 } else if (!len && !(new_mflags & MATCHFLG_CVS_IGNORE)) {
849 rprintf(FERROR, "unexpected end of filter rule: %s\n", p);
850 exit_cleanup(RERR_SYNTAX);
854 *mflags_ptr = new_mflags;
855 return (const char *)s;
859 static char default_cvsignore[] =
860 /* These default ignored items come from the CVS manual. */
861 "RCS SCCS CVS CVS.adm RCSLOG cvslog.* tags TAGS"
862 " .make.state .nse_depinfo *~ #* .#* ,* _$* *$"
863 " *.old *.bak *.BAK *.orig *.rej .del-*"
864 " *.a *.olb *.o *.obj *.so *.exe"
865 " *.Z *.elc *.ln core"
866 /* The rest we added to suit ourself. */
869 static void get_cvs_excludes(uint32 mflags)
871 char *p, fname[MAXPATHLEN];
872 static int initialized = 0;
878 parse_rule(&cvs_filter_list, default_cvsignore, mflags, 0);
880 p = module_id >= 0 && lp_use_chroot(module_id) ? "/" : getenv("HOME");
881 if (p && pathjoin(fname, MAXPATHLEN, p, ".cvsignore") < MAXPATHLEN)
882 parse_filter_file(&cvs_filter_list, fname, mflags, 0);
884 parse_rule(&cvs_filter_list, getenv("CVSIGNORE"), mflags, 0);
888 void parse_rule(struct filter_list_struct *listp, const char *pattern,
889 uint32 mflags, int xflags)
891 unsigned int pat_len;
899 /* Remember that the returned string is NOT '\0' terminated! */
900 cp = parse_rule_tok(pattern, mflags, xflags,
901 &pat_len, &new_mflags);
904 if (pat_len >= MAXPATHLEN) {
905 rprintf(FERROR, "discarding over-long filter: %s\n",
909 pattern = cp + pat_len;
911 if (new_mflags & MATCHFLG_CLEAR_LIST) {
914 "[%s] clearing filter list%s\n",
915 who_am_i(), listp->debug_type);
917 clear_filter_list(listp);
921 if (new_mflags & MATCHFLG_MERGE_FILE) {
928 if (new_mflags & MATCHFLG_EXCLUDE_SELF) {
929 const char *name = strrchr(cp, '/');
934 add_rule(listp, name, len, 0, 0);
935 new_mflags &= ~MATCHFLG_EXCLUDE_SELF;
938 if (new_mflags & MATCHFLG_PERDIR_MERGE) {
939 if (parent_dirscan) {
940 if (!(p = parse_merge_name(cp, &len,
943 add_rule(listp, p, len, new_mflags, 0);
947 if (!(p = parse_merge_name(cp, &len, 0)))
949 parse_filter_file(listp, p, new_mflags,
955 add_rule(listp, cp, pat_len, new_mflags, xflags);
957 if (new_mflags & MATCHFLG_CVS_IGNORE
958 && !(new_mflags & MATCHFLG_MERGE_FILE))
959 get_cvs_excludes(new_mflags);
964 void parse_filter_file(struct filter_list_struct *listp, const char *fname,
965 uint32 mflags, int xflags)
968 char line[BIGPATHBUFLEN];
969 char *eob = line + sizeof line - 1;
970 int word_split = mflags & MATCHFLG_WORD_SPLIT;
972 if (!fname || !*fname)
975 if (*fname != '-' || fname[1] || am_server) {
976 if (server_filter_list.head) {
977 strlcpy(line, fname, sizeof line);
978 clean_fname(line, 1);
979 if (check_filter(&server_filter_list, line, 0) < 0)
982 fp = fopen(line, "rb");
984 fp = fopen(fname, "rb");
989 rprintf(FINFO, "[%s] parse_filter_file(%s,%x,%x)%s\n",
990 who_am_i(), fname, mflags, xflags,
991 fp ? "" : " [not found]");
995 if (xflags & XFLG_FATAL_ERRORS) {
996 rsyserr(FERROR, errno,
997 "failed to open %sclude file %s",
998 mflags & MATCHFLG_INCLUDE ? "in" : "ex",
1000 exit_cleanup(RERR_FILEIO);
1004 dirbuf[dirbuf_len] = '\0';
1008 int ch, overflow = 0;
1010 if ((ch = getc(fp)) == EOF) {
1011 if (ferror(fp) && errno == EINTR) {
1017 if (word_split && isspace(ch))
1019 if (eol_nulls? !ch : (ch == '\n' || ch == '\r'))
1027 rprintf(FERROR, "discarding over-long filter: %s...\n", line);
1031 /* Skip an empty token and (when line parsing) comments. */
1032 if (*line && (word_split || (*line != ';' && *line != '#')))
1033 parse_rule(listp, line, mflags, xflags);
1040 /* If the "for_xfer" flag is set, the prefix is made compatible with the
1041 * current protocol_version (if possible) or a NULL is returned (if not
1043 char *get_rule_prefix(int match_flags, const char *pat, int for_xfer,
1044 unsigned int *plen_ptr)
1046 static char buf[MAX_RULE_PREFIX+1];
1048 int legal_len = for_xfer && protocol_version < 29 ? 1 : MAX_RULE_PREFIX-1;
1050 if (match_flags & MATCHFLG_PERDIR_MERGE) {
1054 } else if (match_flags & MATCHFLG_INCLUDE)
1056 else if (legal_len != 1
1057 || ((*pat == '-' || *pat == '+') && pat[1] == ' '))
1062 if (match_flags & MATCHFLG_CVS_IGNORE)
1065 if (match_flags & MATCHFLG_NO_INHERIT)
1067 if (match_flags & MATCHFLG_WORD_SPLIT)
1069 if (match_flags & MATCHFLG_NO_PREFIXES) {
1070 if (match_flags & MATCHFLG_INCLUDE)
1076 if (match_flags & MATCHFLG_EXCLUDE_SELF)
1078 if (match_flags & MATCHFLG_SENDER_SIDE
1079 && (!for_xfer || protocol_version >= 29))
1081 if (match_flags & MATCHFLG_RECEIVER_SIDE
1082 && (!for_xfer || protocol_version >= 29
1083 || (delete_excluded && am_sender)))
1085 if (op - buf > legal_len)
1091 *plen_ptr = op - buf;
1095 static void send_rules(int f_out, struct filter_list_struct *flp)
1097 struct filter_struct *ent, *prev = NULL;
1099 for (ent = flp->head; ent; ent = ent->next) {
1100 unsigned int len, plen, dlen;
1104 if (ent->match_flags & MATCHFLG_SENDER_SIDE)
1105 elide = am_sender ? 1 : -1;
1106 if (ent->match_flags & MATCHFLG_RECEIVER_SIDE)
1107 elide = elide ? 0 : am_sender ? -1 : 1;
1108 else if (delete_excluded && !elide)
1109 elide = am_sender ? 1 : -1;
1112 prev->next = ent->next;
1114 flp->head = ent->next;
1119 if (ent->match_flags & MATCHFLG_CVS_IGNORE
1120 && !(ent->match_flags & MATCHFLG_MERGE_FILE)) {
1121 int f = am_sender || protocol_version < 29 ? f_out : -2;
1122 send_rules(f, &cvs_filter_list);
1126 p = get_rule_prefix(ent->match_flags, ent->pattern, 1, &plen);
1129 "filter rules are too modern for remote rsync.\n");
1130 exit_cleanup(RERR_SYNTAX);
1134 len = strlen(ent->pattern);
1135 dlen = ent->match_flags & MATCHFLG_DIRECTORY ? 1 : 0;
1136 if (!(plen + len + dlen))
1138 write_int(f_out, plen + len + dlen);
1140 write_buf(f_out, p, plen);
1141 write_buf(f_out, ent->pattern, len);
1143 write_byte(f_out, '/');
1148 /* This is only called by the client. */
1149 void send_filter_list(int f_out)
1151 int receiver_wants_list = prune_empty_dirs
1152 || (delete_mode && (!delete_excluded || protocol_version >= 29));
1154 if (local_server || (am_sender && !receiver_wants_list))
1156 if (cvs_exclude && am_sender) {
1157 if (protocol_version >= 29)
1158 parse_rule(&filter_list, ":C", 0, 0);
1159 parse_rule(&filter_list, "-C", 0, 0);
1162 /* This is a complete hack - blame Rusty. FIXME!
1163 * Remove this hack when older rsyncs (below 2.6.4) are gone. */
1164 if (list_only == 1 && !recurse)
1165 parse_rule(&filter_list, "/*/*", MATCHFLG_NO_PREFIXES, 0);
1167 send_rules(f_out, &filter_list);
1170 write_int(f_out, 0);
1173 if (!am_sender || protocol_version < 29)
1174 parse_rule(&filter_list, ":C", 0, 0);
1176 parse_rule(&filter_list, "-C", 0, 0);
1180 /* This is only called by the server. */
1181 void recv_filter_list(int f_in)
1183 char line[BIGPATHBUFLEN];
1184 int xflags = protocol_version >= 29 ? 0 : XFLG_OLD_PREFIXES;
1185 int receiver_wants_list = prune_empty_dirs
1187 && (!delete_excluded || protocol_version >= 29));
1190 if (!local_server && (am_sender || receiver_wants_list)) {
1191 while ((len = read_int(f_in)) != 0) {
1192 if (len >= sizeof line)
1193 overflow_exit("recv_rules");
1194 read_sbuf(f_in, line, len);
1195 parse_rule(&filter_list, line, 0, xflags);
1200 if (local_server || am_sender || protocol_version < 29)
1201 parse_rule(&filter_list, ":C", 0, 0);
1202 if (local_server || am_sender)
1203 parse_rule(&filter_list, "-C", 0, 0);
1206 if (local_server) /* filter out any rules that aren't for us. */
1207 send_rules(-1, &filter_list);