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> */
35 extern int sanitize_paths;
36 extern int protocol_version;
39 extern char curr_dir[];
40 extern unsigned int curr_dir_len;
41 extern unsigned int module_dirlen;
43 struct filter_list_struct filter_list = { 0, 0, "" };
44 struct filter_list_struct server_filter_list = { 0, 0, "server " };
46 /* Need room enough for ":MODS " prefix plus some room to grow. */
47 #define MAX_RULE_PREFIX (16)
49 #define MODIFIERS_MERGE_FILE "-+Cens"
50 #define MODIFIERS_INCL_EXCL "/!"
52 /* The dirbuf is set by push_local_filters() to the current subdirectory
53 * relative to curr_dir that is being processed. The path always has a
54 * trailing slash appended, and the variable dirbuf_len contains the length
55 * of this path prefix. The path is always absolute. */
56 static char dirbuf[MAXPATHLEN+1];
57 static unsigned int dirbuf_len = 0;
58 static int dirbuf_depth;
60 /* This is True when we're scanning parent dirs for per-dir merge-files. */
61 static BOOL parent_dirscan = False;
63 /* This array contains a list of all the currently active per-dir merge
64 * files. This makes it easier to save the appropriate values when we
65 * "push" down into each subdirectory. */
66 static struct filter_struct **mergelist_parents;
67 static int mergelist_cnt = 0;
68 static int mergelist_size = 0;
70 /* Each filter_list_struct describes a singly-linked list by keeping track
71 * of both the head and tail pointers. The list is slightly unusual in that
72 * a parent-dir's content can be appended to the end of the local list in a
73 * special way: the last item in the local list has its "next" pointer set
74 * to point to the inherited list, but the local list's tail pointer points
75 * at the end of the local list. Thus, if the local list is empty, the head
76 * will be pointing at the inherited content but the tail will be NULL. To
77 * help you visualize this, here are the possible list arrangements:
79 * Completely Empty Local Content Only
80 * ================================== ====================================
81 * head -> NULL head -> Local1 -> Local2 -> NULL
82 * tail -> NULL tail -------------^
84 * Inherited Content Only Both Local and Inherited Content
85 * ================================== ====================================
86 * head -> Parent1 -> Parent2 -> NULL head -> L1 -> L2 -> P1 -> P2 -> NULL
87 * tail -> NULL tail ---------^
89 * This means that anyone wanting to traverse the whole list to use it just
90 * needs to start at the head and use the "next" pointers until it goes
91 * NULL. To add new local content, we insert the item after the tail item
92 * and update the tail (obviously, if "tail" was NULL, we insert it at the
93 * head). To clear the local list, WE MUST NOT FREE THE INHERITED CONTENT
94 * because it is shared between the current list and our parent list(s).
95 * The easiest way to handle this is to simply truncate the list after the
96 * tail item and then free the local list from the head. When inheriting
97 * the list for a new local dir, we just save off the filter_list_struct
98 * values (so we can pop back to them later) and set the tail to NULL.
101 static void free_filter(struct filter_struct *ex)
103 if (ex->match_flags & MATCHFLG_PERDIR_MERGE) {
104 free(ex->u.mergelist->debug_type);
105 free(ex->u.mergelist);
112 /* Build a filter structure given a filter pattern. The value in "pat"
113 * is not null-terminated. */
114 static void filter_rule(struct filter_list_struct *listp, const char *pat,
115 unsigned int pat_len, unsigned mflags, int xflags)
117 struct filter_struct *ret;
122 rprintf(FINFO, "[%s] filter_rule(%.*s, %s%s)\n",
123 who_am_i(), (int)pat_len, pat,
124 mflags & MATCHFLG_PERDIR_MERGE ? "per-dir-merge"
125 : mflags & MATCHFLG_INCLUDE ? "include" : "exclude",
129 ret = new(struct filter_struct);
131 out_of_memory("filter_rule");
132 memset(ret, 0, sizeof ret[0]);
134 if (xflags & XFLG_ANCHORED2ABS && *pat == '/'
135 && !(mflags & (MATCHFLG_ABS_PATH | MATCHFLG_MERGE_FILE))) {
136 mflags |= MATCHFLG_ABS_PATH;
137 ex_len = dirbuf_len - module_dirlen - 1;
140 ret->pattern = new_array(char, ex_len + pat_len + 1);
142 out_of_memory("filter_rule");
144 memcpy(ret->pattern, dirbuf + module_dirlen, ex_len);
145 strlcpy(ret->pattern + ex_len, pat, pat_len + 1);
148 if (strpbrk(ret->pattern, "*[?")) {
149 mflags |= MATCHFLG_WILD;
150 if ((cp = strstr(ret->pattern, "**")) != NULL) {
151 mflags |= MATCHFLG_WILD2;
152 /* If the pattern starts with **, note that. */
153 if (cp == ret->pattern)
154 mflags |= MATCHFLG_WILD2_PREFIX;
158 if (pat_len > 1 && ret->pattern[pat_len-1] == '/') {
159 ret->pattern[pat_len-1] = 0;
160 mflags |= MATCHFLG_DIRECTORY;
163 if (mflags & MATCHFLG_PERDIR_MERGE) {
164 struct filter_list_struct *lp;
168 if ((cp = strrchr(ret->pattern, '/')) != NULL)
173 /* If the local merge file was already mentioned, don't
175 for (i = 0; i < mergelist_cnt; i++) {
176 struct filter_struct *ex = mergelist_parents[i];
177 const char *s = strrchr(ex->pattern, '/');
183 if (len == pat_len - (cp - ret->pattern)
184 && memcmp(s, cp, len) == 0) {
190 if (!(lp = new_array(struct filter_list_struct, 1)))
191 out_of_memory("filter_rule");
192 lp->head = lp->tail = NULL;
193 if (asprintf(&lp->debug_type, " (per-dir %s)", cp) < 0)
194 out_of_memory("filter_rule");
195 ret->u.mergelist = lp;
197 if (mergelist_cnt == mergelist_size) {
199 mergelist_parents = realloc_array(mergelist_parents,
200 struct filter_struct *,
202 if (!mergelist_parents)
203 out_of_memory("filter_rule");
205 mergelist_parents[mergelist_cnt++] = ret;
207 for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
211 ret->match_flags = mflags;
214 ret->next = listp->head;
215 listp->head = listp->tail = ret;
217 ret->next = listp->tail->next;
218 listp->tail->next = ret;
223 static void clear_filter_list(struct filter_list_struct *listp)
226 struct filter_struct *ent, *next;
227 /* Truncate any inherited items from the local list. */
228 listp->tail->next = NULL;
229 /* Now free everything that is left. */
230 for (ent = listp->head; ent; ent = next) {
236 listp->head = listp->tail = NULL;
239 /* This returns an expanded (absolute) filename for the merge-file name if
240 * the name has any slashes in it OR if the parent_dirscan var is True;
241 * otherwise it returns the original merge_file name. If the len_ptr value
242 * is non-NULL the merge_file name is limited by the referenced length
243 * value and will be updated with the length of the resulting name. We
244 * always return a name that is null terminated, even if the merge_file
246 static char *parse_merge_name(const char *merge_file, unsigned int *len_ptr,
247 unsigned int prefix_skip)
249 static char buf[MAXPATHLEN];
250 char *fn, tmpbuf[MAXPATHLEN];
253 if (!parent_dirscan && *merge_file != '/') {
254 /* Return the name unchanged it doesn't have any slashes. */
256 const char *p = merge_file + *len_ptr;
257 while (--p > merge_file && *p != '/') {}
258 if (p == merge_file) {
259 strlcpy(buf, merge_file, *len_ptr + 1);
262 } else if (strchr(merge_file, '/') == NULL)
263 return (char *)merge_file;
266 fn = *merge_file == '/' ? buf : tmpbuf;
267 if (sanitize_paths) {
268 const char *r = prefix_skip ? "/" : NULL;
269 /* null-terminate the name if it isn't already */
270 if (len_ptr && merge_file[*len_ptr]) {
271 char *to = fn == buf ? tmpbuf : buf;
272 strlcpy(to, merge_file, *len_ptr + 1);
275 if (!sanitize_path(fn, merge_file, r, dirbuf_depth)) {
276 rprintf(FERROR, "merge-file name overflows: %s\n",
281 strlcpy(fn, merge_file, len_ptr ? *len_ptr + 1 : MAXPATHLEN);
289 if (dirbuf_len + fn_len >= MAXPATHLEN) {
290 rprintf(FERROR, "merge-file name overflows: %s\n", fn);
293 memcpy(buf, dirbuf + prefix_skip, dirbuf_len - prefix_skip);
294 memcpy(buf + dirbuf_len - prefix_skip, fn, fn_len + 1);
295 fn_len = clean_fname(buf, 1);
303 /* Sets the dirbuf and dirbuf_len values. */
304 void set_filter_dir(const char *dir, unsigned int dirlen)
308 memcpy(dirbuf, curr_dir, curr_dir_len);
309 dirbuf[curr_dir_len] = '/';
310 len = curr_dir_len + 1;
311 if (len + dirlen >= MAXPATHLEN)
315 memcpy(dirbuf + len, dir, dirlen);
316 dirbuf[dirlen + len] = '\0';
317 dirbuf_len = clean_fname(dirbuf, 1);
318 if (dirbuf_len > 1 && dirbuf[dirbuf_len-1] == '.'
319 && dirbuf[dirbuf_len-2] == '/')
322 dirbuf[dirbuf_len++] = '/';
323 dirbuf[dirbuf_len] = '\0';
325 dirbuf_depth = count_dir_elements(dirbuf + module_dirlen);
328 /* This routine takes a per-dir merge-file entry and finishes its setup.
329 * If the name has a path portion then we check to see if it refers to a
330 * parent directory of the first transfer dir. If it does, we scan all the
331 * dirs from that point through the parent dir of the transfer dir looking
332 * for the per-dir merge-file in each one. */
333 static BOOL setup_merge_file(struct filter_struct *ex,
334 struct filter_list_struct *lp)
336 char buf[MAXPATHLEN];
337 char *x, *y, *pat = ex->pattern;
340 if (!(x = parse_merge_name(pat, NULL, 0)) || *x != '/')
345 ex->pattern = strdup(y+1);
349 strlcpy(buf, x, MAXPATHLEN);
351 pathjoin(buf, MAXPATHLEN, dirbuf, x);
353 len = clean_fname(buf, 1);
354 if (len != 1 && len < MAXPATHLEN-1) {
358 /* This ensures that the specified dir is a parent of the transfer. */
359 for (x = buf, y = dirbuf; *x && *x == *y; x++, y++) {}
361 y += strlen(y); /* nope -- skip the scan */
363 parent_dirscan = True;
365 char save[MAXPATHLEN];
366 strlcpy(save, y, MAXPATHLEN);
368 dirbuf_len = y - dirbuf;
369 strlcpy(x, ex->pattern, MAXPATHLEN - (x - buf));
370 add_filter_file(lp, buf, ex->match_flags, XFLG_ANCHORED2ABS);
371 if (ex->match_flags & MATCHFLG_NO_INHERIT)
374 strlcpy(y, save, MAXPATHLEN);
375 while ((*x++ = *y++) != '/') {}
377 parent_dirscan = False;
382 /* Each time rsync changes to a new directory it call this function to
383 * handle all the per-dir merge-files. The "dir" value is the current path
384 * relative to curr_dir (which might not be null-terminated). We copy it
385 * into dirbuf so that we can easily append a file name on the end. */
386 void *push_local_filters(const char *dir, unsigned int dirlen)
388 struct filter_list_struct *ap, *push;
391 set_filter_dir(dir, dirlen);
396 push = new_array(struct filter_list_struct, mergelist_cnt);
398 out_of_memory("push_local_filters");
400 for (i = 0, ap = push; i < mergelist_cnt; i++) {
401 memcpy(ap++, mergelist_parents[i]->u.mergelist,
402 sizeof (struct filter_list_struct));
405 /* Note: add_filter_file() might increase mergelist_cnt, so keep
406 * this loop separate from the above loop. */
407 for (i = 0; i < mergelist_cnt; i++) {
408 struct filter_struct *ex = mergelist_parents[i];
409 struct filter_list_struct *lp = ex->u.mergelist;
412 rprintf(FINFO, "[%s] pushing filter list%s\n",
413 who_am_i(), lp->debug_type);
416 lp->tail = NULL; /* Switch any local rules to inherited. */
417 if (ex->match_flags & MATCHFLG_NO_INHERIT)
420 if (ex->match_flags & MATCHFLG_FINISH_SETUP) {
421 ex->match_flags &= ~MATCHFLG_FINISH_SETUP;
422 if (setup_merge_file(ex, lp))
423 set_filter_dir(dir, dirlen);
426 if (strlcpy(dirbuf + dirbuf_len, ex->pattern,
427 MAXPATHLEN - dirbuf_len) < MAXPATHLEN - dirbuf_len) {
428 add_filter_file(lp, dirbuf, ex->match_flags,
431 io_error |= IOERR_GENERAL;
433 "cannot add local filter rules in long-named directory: %s\n",
436 dirbuf[dirbuf_len] = '\0';
442 void pop_local_filters(void *mem)
444 struct filter_list_struct *ap, *pop = (struct filter_list_struct*)mem;
447 for (i = mergelist_cnt; i-- > 0; ) {
448 struct filter_struct *ex = mergelist_parents[i];
449 struct filter_list_struct *lp = ex->u.mergelist;
452 rprintf(FINFO, "[%s] popping filter list%s\n",
453 who_am_i(), lp->debug_type);
456 clear_filter_list(lp);
462 for (i = 0, ap = pop; i < mergelist_cnt; i++) {
463 memcpy(mergelist_parents[i]->u.mergelist, ap++,
464 sizeof (struct filter_list_struct));
470 static int rule_matches(char *name, struct filter_struct *ex, int name_is_dir)
472 char *p, full_name[MAXPATHLEN];
474 int ret_match = ex->match_flags & MATCHFLG_NEGATE ? 0 : 1;
475 char *pattern = ex->pattern;
480 /* If the pattern does not have any slashes AND it does not have
481 * a "**" (which could match a slash), then we just match the
482 * name portion of the path. */
483 if (!ex->u.slash_cnt && !(ex->match_flags & MATCHFLG_WILD2)) {
484 if ((p = strrchr(name,'/')) != NULL)
487 else if (ex->match_flags & MATCHFLG_ABS_PATH && *name != '/'
488 && curr_dir_len > module_dirlen + 1) {
489 pathjoin(full_name, sizeof full_name,
490 curr_dir + module_dirlen + 1, name);
494 if (ex->match_flags & MATCHFLG_DIRECTORY && !name_is_dir)
497 if (*pattern == '/') {
504 if (ex->match_flags & MATCHFLG_WILD) {
505 /* A non-anchored match with an infix slash and no "**"
506 * needs to match the last slash_cnt+1 name elements. */
507 if (!match_start && ex->u.slash_cnt
508 && !(ex->match_flags & MATCHFLG_WILD2)) {
509 int cnt = ex->u.slash_cnt + 1;
510 for (p = name + strlen(name) - 1; p >= name; p--) {
511 if (*p == '/' && !--cnt)
516 if (wildmatch(pattern, name))
518 if (ex->match_flags & MATCHFLG_WILD2_PREFIX) {
519 /* If the **-prefixed pattern has a '/' as the next
520 * character, then try to match the rest of the
521 * pattern at the root. */
522 if (pattern[2] == '/' && wildmatch(pattern+3, name))
525 else if (!match_start && ex->match_flags & MATCHFLG_WILD2) {
526 /* A non-anchored match with an infix or trailing "**"
527 * (but not a prefixed "**") needs to try matching
528 * after every slash. */
529 while ((name = strchr(name, '/')) != NULL) {
531 if (wildmatch(pattern, name))
535 } else if (match_start) {
536 if (strcmp(name,pattern) == 0)
539 int l1 = strlen(name);
540 int l2 = strlen(pattern);
542 strcmp(name+(l1-l2),pattern) == 0 &&
543 (l1==l2 || name[l1-(l2+1)] == '/')) {
552 static void report_filter_result(char const *name,
553 struct filter_struct const *ent,
554 int name_is_dir, const char *type)
556 /* If a trailing slash is present to match only directories,
557 * then it is stripped out by filter_rule. So as a special
558 * case we add it back in here. */
561 rprintf(FINFO, "[%s] %scluding %s %s because of pattern %s%s%s\n",
563 ent->match_flags & MATCHFLG_INCLUDE ? "in" : "ex",
564 name_is_dir ? "directory" : "file", name, ent->pattern,
565 ent->match_flags & MATCHFLG_DIRECTORY ? "/" : "", type);
571 * Return -1 if file "name" is defined to be excluded by the specified
572 * exclude list, 1 if it is included, and 0 if it was not matched.
574 int check_filter(struct filter_list_struct *listp, char *name, int name_is_dir)
576 struct filter_struct *ent;
578 for (ent = listp->head; ent; ent = ent->next) {
579 if (ent->match_flags & MATCHFLG_PERDIR_MERGE) {
580 int rc = check_filter(ent->u.mergelist, name,
586 if (rule_matches(name, ent, name_is_dir)) {
587 report_filter_result(name, ent, name_is_dir,
589 return ent->match_flags & MATCHFLG_INCLUDE ? 1 : -1;
597 /* Get the next include/exclude arg from the string. The token will not
598 * be '\0' terminated, so use the returned length to limit the string.
599 * Also, be sure to add this length to the returned pointer before passing
600 * it back to ask for the next token. This routine parses the "!" (list-
601 * clearing) token and (depending on the mflags) the various prefixes.
602 * The *mflags_ptr value will be set on exit to the new MATCHFLG_* bits
603 * for the current token. */
604 static const char *get_filter_tok(const char *p, unsigned mflags, int xflags,
605 unsigned int *len_ptr, unsigned int *mflags_ptr)
607 const unsigned char *s = (const unsigned char *)p;
608 unsigned int len, new_mflags;
609 int empty_pat_is_OK = 0;
611 if (mflags & MATCHFLG_WORD_SPLIT) {
612 /* Skip over any initial whitespace. */
615 /* Update to point to real start of rule. */
621 new_mflags = mflags & MATCHFLGS_FROM_CONTAINER;
623 /* Figure out what kind of a filter rule "s" is pointing at. Note
624 * that if MATCHFLG_NO_PREFIXES is set, the rule is either an include
625 * or an exclude based on the inheritance of the MATCHFLG_INCLUDE
626 * flag (above). XFLG_OLD_PREFIXES indicates a compatibility mode
627 * for old include/exclude patterns where just "+ " and "- " are
628 * allowed as optional prefixes. */
629 if (mflags & MATCHFLG_NO_PREFIXES) {
631 new_mflags |= MATCHFLG_CLEAR_LIST; /* Tentative! */
632 } else if (xflags & XFLG_OLD_PREFIXES) {
633 if (*s == '-' && s[1] == ' ') {
634 new_mflags &= ~MATCHFLG_INCLUDE;
636 } else if (*s == '+' && s[1] == ' ') {
637 new_mflags |= MATCHFLG_INCLUDE;
641 new_mflags |= MATCHFLG_CLEAR_LIST; /* Tentative! */
646 new_mflags |= MATCHFLG_PERDIR_MERGE
647 | MATCHFLG_FINISH_SETUP;
650 new_mflags |= MATCHFLG_MERGE_FILE;
651 mods = MODIFIERS_INCL_EXCL MODIFIERS_MERGE_FILE;
654 new_mflags |= MATCHFLG_INCLUDE;
657 mods = MODIFIERS_INCL_EXCL;
660 new_mflags |= MATCHFLG_CLEAR_LIST;
664 rprintf(FERROR, "Unknown filter rule: %s\n", p);
665 exit_cleanup(RERR_SYNTAX);
667 while (mods && *++s && *s != ' ' && *s != '_') {
668 if (strchr(mods, *s) == NULL) {
669 if (mflags & MATCHFLG_WORD_SPLIT && isspace(*s)) {
675 "invalid modifier sequence at '%c' in filter rule: %s\n",
677 exit_cleanup(RERR_SYNTAX);
681 if (new_mflags & MATCHFLG_NO_PREFIXES)
683 new_mflags |= MATCHFLG_NO_PREFIXES;
686 if (new_mflags & MATCHFLG_NO_PREFIXES)
688 new_mflags |= MATCHFLG_NO_PREFIXES
692 new_mflags |= MATCHFLG_ABS_PATH;
695 new_mflags |= MATCHFLG_NEGATE;
698 if (new_mflags & MATCHFLG_NO_PREFIXES)
701 new_mflags |= MATCHFLG_NO_PREFIXES
702 | MATCHFLG_WORD_SPLIT
703 | MATCHFLG_NO_INHERIT;
706 new_mflags |= MATCHFLG_EXCLUDE_SELF;
709 new_mflags |= MATCHFLG_NO_INHERIT;
712 new_mflags |= MATCHFLG_WORD_SPLIT;
720 if (mflags & MATCHFLG_WORD_SPLIT) {
721 const unsigned char *cp = s;
722 /* Token ends at whitespace or the end of the string. */
723 while (!isspace(*cp) && *cp != '\0')
727 len = strlen((char*)s);
729 if (new_mflags & MATCHFLG_CLEAR_LIST) {
730 if (!(xflags & XFLG_OLD_PREFIXES) && len) {
732 "'!' rule has trailing characters: %s\n", p);
733 exit_cleanup(RERR_SYNTAX);
736 new_mflags &= ~MATCHFLG_CLEAR_LIST;
737 } else if (!len && !empty_pat_is_OK) {
738 rprintf(FERROR, "unexpected end of filter rule: %s\n", p);
739 exit_cleanup(RERR_SYNTAX);
743 *mflags_ptr = new_mflags;
744 return (const char *)s;
748 void add_filter(struct filter_list_struct *listp, const char *pattern,
749 unsigned mflags, int xflags)
751 unsigned int pat_len, new_mflags;
758 /* Remember that the returned string is NOT '\0' terminated! */
759 cp = get_filter_tok(pattern, mflags, xflags,
760 &pat_len, &new_mflags);
763 if (pat_len >= MAXPATHLEN) {
764 rprintf(FERROR, "discarding over-long filter: %s\n",
768 pattern = cp + pat_len;
770 if (new_mflags & MATCHFLG_CLEAR_LIST) {
773 "[%s] clearing filter list%s\n",
774 who_am_i(), listp->debug_type);
776 clear_filter_list(listp);
785 if (new_mflags & MATCHFLG_MERGE_FILE) {
786 unsigned int len = pat_len;
787 if (new_mflags & MATCHFLG_EXCLUDE_SELF) {
788 const char *name = strrchr(cp, '/');
793 filter_rule(listp, name, len, 0, 0);
794 new_mflags &= ~MATCHFLG_EXCLUDE_SELF;
797 if (new_mflags & MATCHFLG_PERDIR_MERGE) {
798 if (parent_dirscan) {
799 if (!(p = parse_merge_name(cp, &len,
802 filter_rule(listp, p, len,
807 if (!(p = parse_merge_name(cp, &len, 0)))
809 add_filter_file(listp, p, new_mflags,
815 filter_rule(listp, cp, pat_len, new_mflags, xflags);
820 void add_filter_file(struct filter_list_struct *listp, const char *fname,
821 unsigned mflags, int xflags)
824 char line[MAXPATHLEN+MAX_RULE_PREFIX+1]; /* +1 for trailing slash. */
825 char *eob = line + sizeof line - 1;
826 int word_split = mflags & MATCHFLG_WORD_SPLIT;
828 if (!fname || !*fname)
831 if (*fname != '-' || fname[1] || am_server) {
832 if (server_filter_list.head) {
833 strlcpy(line, fname, sizeof line);
834 clean_fname(line, 1);
835 if (check_filter(&server_filter_list, line, 0) < 0)
838 fp = fopen(line, "rb");
840 fp = fopen(fname, "rb");
845 rprintf(FINFO, "[%s] add_filter_file(%s,%x,%x)%s\n",
846 who_am_i(), safe_fname(fname), mflags, xflags,
847 fp ? "" : " [not found]");
851 if (xflags & XFLG_FATAL_ERRORS) {
852 rsyserr(FERROR, errno,
853 "failed to open %sclude file %s",
854 mflags & MATCHFLG_INCLUDE ? "in" : "ex",
856 exit_cleanup(RERR_FILEIO);
860 dirbuf[dirbuf_len] = '\0';
864 int ch, overflow = 0;
866 if ((ch = getc(fp)) == EOF) {
867 if (ferror(fp) && errno == EINTR)
871 if (word_split && isspace(ch))
873 if (eol_nulls? !ch : (ch == '\n' || ch == '\r'))
881 rprintf(FERROR, "discarding over-long filter: %s...\n", line);
885 /* Skip an empty token and (when line parsing) comments. */
886 if (*line && (word_split || (*line != ';' && *line != '#')))
887 add_filter(listp, line, mflags, xflags);
894 char *get_rule_prefix(int match_flags, const char *pat, unsigned int *plen_ptr)
896 static char buf[MAX_RULE_PREFIX+1];
899 if (match_flags & MATCHFLG_PERDIR_MERGE) {
901 if (match_flags & MATCHFLG_WORD_SPLIT)
903 if (match_flags & MATCHFLG_NO_INHERIT)
905 if (match_flags & MATCHFLG_EXCLUDE_SELF)
907 if (match_flags & MATCHFLG_NO_PREFIXES) {
908 if (match_flags & MATCHFLG_INCLUDE)
914 } else if (match_flags & MATCHFLG_INCLUDE) {
917 } else if (protocol_version >= 29
918 || ((*pat == '-' || *pat == '+') && pat[1] == ' ')) {
924 *plen_ptr = op - buf;
925 if (op - buf > MAX_RULE_PREFIX)
926 overflow("get_rule_prefix");
930 void send_filter_list(int f)
932 struct filter_struct *ent;
934 /* This is a complete hack - blame Rusty. FIXME!
935 * Remove this hack when older rsyncs (below 2.6.4) are gone. */
936 if (list_only == 1 && !recurse)
937 add_filter(&filter_list, "/*/*", MATCHFLG_NO_PREFIXES, 0);
939 for (ent = filter_list.head; ent; ent = ent->next) {
940 unsigned int len, plen, dlen;
943 len = strlen(ent->pattern);
944 if (len == 0 || len >= MAXPATHLEN)
946 p = get_rule_prefix(ent->match_flags, ent->pattern, &plen);
947 if (protocol_version < 29 && *p == ':') {
948 if (strcmp(p, ":sn- ") == 0
949 && strcmp(ent->pattern, ".cvsignore") == 0)
952 "remote rsync is too old to understand per-directory merge files.\n");
953 exit_cleanup(RERR_SYNTAX);
955 dlen = ent->match_flags & MATCHFLG_DIRECTORY ? 1 : 0;
956 write_int(f, plen + len + dlen);
958 write_buf(f, p, plen);
959 write_buf(f, ent->pattern, len);
968 void recv_filter_list(int f)
970 char line[MAXPATHLEN+MAX_RULE_PREFIX+1]; /* +1 for trailing slash. */
971 unsigned int xflags = protocol_version >= 29 ? 0 : XFLG_OLD_PREFIXES;
974 while ((l = read_int(f)) != 0) {
975 if (l >= sizeof line)
976 overflow("recv_filter_list");
977 read_sbuf(f, line, l);
978 add_filter(&filter_list, line, 0, xflags);
983 static char default_cvsignore[] =
984 /* These default ignored items come from the CVS manual. */
985 "RCS SCCS CVS CVS.adm RCSLOG cvslog.* tags TAGS"
986 " .make.state .nse_depinfo *~ #* .#* ,* _$* *$"
987 " *.old *.bak *.BAK *.orig *.rej .del-*"
988 " *.a *.olb *.o *.obj *.so *.exe"
989 " *.Z *.elc *.ln core"
990 /* The rest we added to suit ourself. */
993 void add_cvs_excludes(void)
995 static unsigned cvs_mflags = MATCHFLG_WORD_SPLIT|MATCHFLG_NO_PREFIXES;
996 char fname[MAXPATHLEN];
997 char *p = module_id >= 0 && lp_use_chroot(module_id)
998 ? "/" : getenv("HOME");
1000 add_filter(&filter_list, ":C", 0, 0);
1001 add_filter(&filter_list, default_cvsignore, cvs_mflags, 0);
1003 if (p && pathjoin(fname, MAXPATHLEN, p, ".cvsignore") < MAXPATHLEN)
1004 add_filter_file(&filter_list, fname, cvs_mflags, 0);
1006 add_filter(&filter_list, getenv("CVSIGNORE"), cvs_mflags, 0);