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 delete_mode;
38 extern int delete_excluded;
39 extern int cvs_exclude;
40 extern int sanitize_paths;
41 extern int protocol_version;
44 extern char curr_dir[];
45 extern unsigned int curr_dir_len;
46 extern unsigned int module_dirlen;
48 struct filter_list_struct filter_list = { 0, 0, "" };
49 struct filter_list_struct cvs_filter_list = { 0, 0, " [cvsignore]" };
50 struct filter_list_struct server_filter_list = { 0, 0, " [server]" };
52 /* Need room enough for ":MODS " prefix plus some room to grow. */
53 #define MAX_RULE_PREFIX (16)
55 #define MODIFIERS_MERGE_FILE "-+Cenw"
56 #define MODIFIERS_INCL_EXCL "/!Crs"
57 #define MODIFIERS_HIDE_PROTECT "/!"
59 /* The dirbuf is set by push_local_filters() to the current subdirectory
60 * relative to curr_dir that is being processed. The path always has a
61 * trailing slash appended, and the variable dirbuf_len contains the length
62 * of this path prefix. The path is always absolute. */
63 static char dirbuf[MAXPATHLEN+1];
64 static unsigned int dirbuf_len = 0;
65 static int dirbuf_depth;
67 /* This is True when we're scanning parent dirs for per-dir merge-files. */
68 static BOOL parent_dirscan = False;
70 /* This array contains a list of all the currently active per-dir merge
71 * files. This makes it easier to save the appropriate values when we
72 * "push" down into each subdirectory. */
73 static struct filter_struct **mergelist_parents;
74 static int mergelist_cnt = 0;
75 static int mergelist_size = 0;
77 /* Each filter_list_struct describes a singly-linked list by keeping track
78 * of both the head and tail pointers. The list is slightly unusual in that
79 * a parent-dir's content can be appended to the end of the local list in a
80 * special way: the last item in the local list has its "next" pointer set
81 * to point to the inherited list, but the local list's tail pointer points
82 * at the end of the local list. Thus, if the local list is empty, the head
83 * will be pointing at the inherited content but the tail will be NULL. To
84 * help you visualize this, here are the possible list arrangements:
86 * Completely Empty Local Content Only
87 * ================================== ====================================
88 * head -> NULL head -> Local1 -> Local2 -> NULL
89 * tail -> NULL tail -------------^
91 * Inherited Content Only Both Local and Inherited Content
92 * ================================== ====================================
93 * head -> Parent1 -> Parent2 -> NULL head -> L1 -> L2 -> P1 -> P2 -> NULL
94 * tail -> NULL tail ---------^
96 * This means that anyone wanting to traverse the whole list to use it just
97 * needs to start at the head and use the "next" pointers until it goes
98 * NULL. To add new local content, we insert the item after the tail item
99 * and update the tail (obviously, if "tail" was NULL, we insert it at the
100 * head). To clear the local list, WE MUST NOT FREE THE INHERITED CONTENT
101 * because it is shared between the current list and our parent list(s).
102 * The easiest way to handle this is to simply truncate the list after the
103 * tail item and then free the local list from the head. When inheriting
104 * the list for a new local dir, we just save off the filter_list_struct
105 * values (so we can pop back to them later) and set the tail to NULL.
108 static void free_filter(struct filter_struct *ex)
110 if (ex->match_flags & MATCHFLG_PERDIR_MERGE) {
111 free(ex->u.mergelist->debug_type);
112 free(ex->u.mergelist);
119 /* Build a filter structure given a filter pattern. The value in "pat"
120 * is not null-terminated. */
121 static void add_rule(struct filter_list_struct *listp, const char *pat,
122 unsigned int pat_len, uint32 mflags, int xflags)
124 struct filter_struct *ret;
129 rprintf(FINFO, "[%s] add_rule(%s%.*s%s)%s\n",
130 who_am_i(), get_rule_prefix(mflags, pat, 0, NULL),
132 (mflags & MATCHFLG_DIRECTORY) ? "/" : "",
136 if (!(ret = new(struct filter_struct)))
137 out_of_memory("add_rule");
138 memset(ret, 0, sizeof ret[0]);
140 if (xflags & XFLG_ANCHORED2ABS && *pat == '/'
141 && !(mflags & (MATCHFLG_ABS_PATH | MATCHFLG_MERGE_FILE))) {
142 mflags |= MATCHFLG_ABS_PATH;
143 ex_len = dirbuf_len - module_dirlen - 1;
146 if (!(ret->pattern = new_array(char, ex_len + pat_len + 1)))
147 out_of_memory("add_rule");
149 memcpy(ret->pattern, dirbuf + module_dirlen, ex_len);
150 strlcpy(ret->pattern + ex_len, pat, pat_len + 1);
153 if (strpbrk(ret->pattern, "*[?")) {
154 mflags |= MATCHFLG_WILD;
155 if ((cp = strstr(ret->pattern, "**")) != NULL) {
156 mflags |= MATCHFLG_WILD2;
157 /* If the pattern starts with **, note that. */
158 if (cp == ret->pattern)
159 mflags |= MATCHFLG_WILD2_PREFIX;
163 if (pat_len > 1 && ret->pattern[pat_len-1] == '/') {
164 ret->pattern[pat_len-1] = 0;
165 mflags |= MATCHFLG_DIRECTORY;
168 if (mflags & MATCHFLG_PERDIR_MERGE) {
169 struct filter_list_struct *lp;
173 if ((cp = strrchr(ret->pattern, '/')) != NULL)
178 /* If the local merge file was already mentioned, don't
180 for (i = 0; i < mergelist_cnt; i++) {
181 struct filter_struct *ex = mergelist_parents[i];
182 const char *s = strrchr(ex->pattern, '/');
188 if (len == pat_len - (cp - ret->pattern)
189 && memcmp(s, cp, len) == 0) {
195 if (!(lp = new_array(struct filter_list_struct, 1)))
196 out_of_memory("add_rule");
197 lp->head = lp->tail = NULL;
198 if (asprintf(&lp->debug_type, " [per-dir %s]", cp) < 0)
199 out_of_memory("add_rule");
200 ret->u.mergelist = lp;
202 if (mergelist_cnt == mergelist_size) {
204 mergelist_parents = realloc_array(mergelist_parents,
205 struct filter_struct *,
207 if (!mergelist_parents)
208 out_of_memory("add_rule");
210 mergelist_parents[mergelist_cnt++] = ret;
212 for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
216 ret->match_flags = mflags;
219 ret->next = listp->head;
220 listp->head = listp->tail = ret;
222 ret->next = listp->tail->next;
223 listp->tail->next = ret;
228 static void clear_filter_list(struct filter_list_struct *listp)
231 struct filter_struct *ent, *next;
232 /* Truncate any inherited items from the local list. */
233 listp->tail->next = NULL;
234 /* Now free everything that is left. */
235 for (ent = listp->head; ent; ent = next) {
241 listp->head = listp->tail = NULL;
244 /* This returns an expanded (absolute) filename for the merge-file name if
245 * the name has any slashes in it OR if the parent_dirscan var is True;
246 * otherwise it returns the original merge_file name. If the len_ptr value
247 * is non-NULL the merge_file name is limited by the referenced length
248 * value and will be updated with the length of the resulting name. We
249 * always return a name that is null terminated, even if the merge_file
251 static char *parse_merge_name(const char *merge_file, unsigned int *len_ptr,
252 unsigned int prefix_skip)
254 static char buf[MAXPATHLEN];
255 char *fn, tmpbuf[MAXPATHLEN];
258 if (!parent_dirscan && *merge_file != '/') {
259 /* Return the name unchanged it doesn't have any slashes. */
261 const char *p = merge_file + *len_ptr;
262 while (--p > merge_file && *p != '/') {}
263 if (p == merge_file) {
264 strlcpy(buf, merge_file, *len_ptr + 1);
267 } else if (strchr(merge_file, '/') == NULL)
268 return (char *)merge_file;
271 fn = *merge_file == '/' ? buf : tmpbuf;
272 if (sanitize_paths) {
273 const char *r = prefix_skip ? "/" : NULL;
274 /* null-terminate the name if it isn't already */
275 if (len_ptr && merge_file[*len_ptr]) {
276 char *to = fn == buf ? tmpbuf : buf;
277 strlcpy(to, merge_file, *len_ptr + 1);
280 if (!sanitize_path(fn, merge_file, r, dirbuf_depth)) {
281 rprintf(FERROR, "merge-file name overflows: %s\n",
282 safe_fname(merge_file));
286 strlcpy(fn, merge_file, len_ptr ? *len_ptr + 1 : MAXPATHLEN);
294 if (dirbuf_len + fn_len >= MAXPATHLEN) {
295 rprintf(FERROR, "merge-file name overflows: %s\n",
299 memcpy(buf, dirbuf + prefix_skip, dirbuf_len - prefix_skip);
300 memcpy(buf + dirbuf_len - prefix_skip, fn, fn_len + 1);
301 fn_len = clean_fname(buf, 1);
309 /* Sets the dirbuf and dirbuf_len values. */
310 void set_filter_dir(const char *dir, unsigned int dirlen)
314 memcpy(dirbuf, curr_dir, curr_dir_len);
315 dirbuf[curr_dir_len] = '/';
316 len = curr_dir_len + 1;
317 if (len + dirlen >= MAXPATHLEN)
321 memcpy(dirbuf + len, dir, dirlen);
322 dirbuf[dirlen + len] = '\0';
323 dirbuf_len = clean_fname(dirbuf, 1);
324 if (dirbuf_len > 1 && dirbuf[dirbuf_len-1] == '.'
325 && dirbuf[dirbuf_len-2] == '/')
328 dirbuf[dirbuf_len++] = '/';
329 dirbuf[dirbuf_len] = '\0';
331 dirbuf_depth = count_dir_elements(dirbuf + module_dirlen);
334 /* This routine takes a per-dir merge-file entry and finishes its setup.
335 * If the name has a path portion then we check to see if it refers to a
336 * parent directory of the first transfer dir. If it does, we scan all the
337 * dirs from that point through the parent dir of the transfer dir looking
338 * for the per-dir merge-file in each one. */
339 static BOOL setup_merge_file(struct filter_struct *ex,
340 struct filter_list_struct *lp)
342 char buf[MAXPATHLEN];
343 char *x, *y, *pat = ex->pattern;
346 if (!(x = parse_merge_name(pat, NULL, 0)) || *x != '/')
351 ex->pattern = strdup(y+1);
355 strlcpy(buf, x, MAXPATHLEN);
357 pathjoin(buf, MAXPATHLEN, dirbuf, x);
359 len = clean_fname(buf, 1);
360 if (len != 1 && len < MAXPATHLEN-1) {
364 /* This ensures that the specified dir is a parent of the transfer. */
365 for (x = buf, y = dirbuf; *x && *x == *y; x++, y++) {}
367 y += strlen(y); /* nope -- skip the scan */
369 parent_dirscan = True;
371 char save[MAXPATHLEN];
372 strlcpy(save, y, MAXPATHLEN);
374 dirbuf_len = y - dirbuf;
375 strlcpy(x, ex->pattern, MAXPATHLEN - (x - buf));
376 parse_filter_file(lp, buf, ex->match_flags, XFLG_ANCHORED2ABS);
377 if (ex->match_flags & MATCHFLG_NO_INHERIT)
380 strlcpy(y, save, MAXPATHLEN);
381 while ((*x++ = *y++) != '/') {}
383 parent_dirscan = False;
388 /* Each time rsync changes to a new directory it call this function to
389 * handle all the per-dir merge-files. The "dir" value is the current path
390 * relative to curr_dir (which might not be null-terminated). We copy it
391 * into dirbuf so that we can easily append a file name on the end. */
392 void *push_local_filters(const char *dir, unsigned int dirlen)
394 struct filter_list_struct *ap, *push;
397 set_filter_dir(dir, dirlen);
402 push = new_array(struct filter_list_struct, mergelist_cnt);
404 out_of_memory("push_local_filters");
406 for (i = 0, ap = push; i < mergelist_cnt; i++) {
407 memcpy(ap++, mergelist_parents[i]->u.mergelist,
408 sizeof (struct filter_list_struct));
411 /* Note: parse_filter_file() might increase mergelist_cnt, so keep
412 * this loop separate from the above loop. */
413 for (i = 0; i < mergelist_cnt; i++) {
414 struct filter_struct *ex = mergelist_parents[i];
415 struct filter_list_struct *lp = ex->u.mergelist;
418 rprintf(FINFO, "[%s] pushing filter list%s\n",
419 who_am_i(), lp->debug_type);
422 lp->tail = NULL; /* Switch any local rules to inherited. */
423 if (ex->match_flags & MATCHFLG_NO_INHERIT)
426 if (ex->match_flags & MATCHFLG_FINISH_SETUP) {
427 ex->match_flags &= ~MATCHFLG_FINISH_SETUP;
428 if (setup_merge_file(ex, lp))
429 set_filter_dir(dir, dirlen);
432 if (strlcpy(dirbuf + dirbuf_len, ex->pattern,
433 MAXPATHLEN - dirbuf_len) < MAXPATHLEN - dirbuf_len) {
434 parse_filter_file(lp, dirbuf, ex->match_flags,
437 io_error |= IOERR_GENERAL;
439 "cannot add local filter rules in long-named directory: %s\n",
442 dirbuf[dirbuf_len] = '\0';
448 void pop_local_filters(void *mem)
450 struct filter_list_struct *ap, *pop = (struct filter_list_struct*)mem;
453 for (i = mergelist_cnt; i-- > 0; ) {
454 struct filter_struct *ex = mergelist_parents[i];
455 struct filter_list_struct *lp = ex->u.mergelist;
458 rprintf(FINFO, "[%s] popping filter list%s\n",
459 who_am_i(), lp->debug_type);
462 clear_filter_list(lp);
468 for (i = 0, ap = pop; i < mergelist_cnt; i++) {
469 memcpy(mergelist_parents[i]->u.mergelist, ap++,
470 sizeof (struct filter_list_struct));
476 static int rule_matches(char *name, struct filter_struct *ex, int name_is_dir)
478 char *p, full_name[MAXPATHLEN];
480 int ret_match = ex->match_flags & MATCHFLG_NEGATE ? 0 : 1;
481 char *pattern = ex->pattern;
486 /* If the pattern does not have any slashes AND it does not have
487 * a "**" (which could match a slash), then we just match the
488 * name portion of the path. */
489 if (!ex->u.slash_cnt && !(ex->match_flags & MATCHFLG_WILD2)) {
490 if ((p = strrchr(name,'/')) != NULL)
493 else if (ex->match_flags & MATCHFLG_ABS_PATH && *name != '/'
494 && curr_dir_len > module_dirlen + 1) {
495 pathjoin(full_name, sizeof full_name,
496 curr_dir + module_dirlen + 1, name);
500 if (ex->match_flags & MATCHFLG_DIRECTORY && !name_is_dir)
503 if (*pattern == '/') {
510 if (ex->match_flags & MATCHFLG_WILD) {
511 /* A non-anchored match with an infix slash and no "**"
512 * needs to match the last slash_cnt+1 name elements. */
513 if (!match_start && ex->u.slash_cnt
514 && !(ex->match_flags & MATCHFLG_WILD2)) {
515 int cnt = ex->u.slash_cnt + 1;
516 for (p = name + strlen(name) - 1; p >= name; p--) {
517 if (*p == '/' && !--cnt)
522 if (wildmatch(pattern, name))
524 if (ex->match_flags & MATCHFLG_WILD2_PREFIX) {
525 /* If the **-prefixed pattern has a '/' as the next
526 * character, then try to match the rest of the
527 * pattern at the root. */
528 if (pattern[2] == '/' && wildmatch(pattern+3, name))
531 else if (!match_start && ex->match_flags & MATCHFLG_WILD2) {
532 /* A non-anchored match with an infix or trailing "**"
533 * (but not a prefixed "**") needs to try matching
534 * after every slash. */
535 while ((name = strchr(name, '/')) != NULL) {
537 if (wildmatch(pattern, name))
541 } else if (match_start) {
542 if (strcmp(name,pattern) == 0)
545 int l1 = strlen(name);
546 int l2 = strlen(pattern);
548 strcmp(name+(l1-l2),pattern) == 0 &&
549 (l1==l2 || name[l1-(l2+1)] == '/')) {
558 static void report_filter_result(char const *name,
559 struct filter_struct const *ent,
560 int name_is_dir, const char *type)
562 /* If a trailing slash is present to match only directories,
563 * then it is stripped out by add_rule(). So as a special
564 * case we add it back in here. */
567 rprintf(FINFO, "[%s] %scluding %s %s because of pattern %s%s%s\n",
569 ent->match_flags & MATCHFLG_INCLUDE ? "in" : "ex",
570 name_is_dir ? "directory" : "file", name, ent->pattern,
571 ent->match_flags & MATCHFLG_DIRECTORY ? "/" : "", type);
577 * Return -1 if file "name" is defined to be excluded by the specified
578 * exclude list, 1 if it is included, and 0 if it was not matched.
580 int check_filter(struct filter_list_struct *listp, char *name, int name_is_dir)
582 struct filter_struct *ent;
584 for (ent = listp->head; ent; ent = ent->next) {
585 if (ent->match_flags & MATCHFLG_PERDIR_MERGE) {
586 int rc = check_filter(ent->u.mergelist, name,
592 if (ent->match_flags & MATCHFLG_CVS_IGNORE) {
593 int rc = check_filter(&cvs_filter_list, name,
599 if (rule_matches(name, ent, name_is_dir)) {
600 report_filter_result(name, ent, name_is_dir,
602 return ent->match_flags & MATCHFLG_INCLUDE ? 1 : -1;
609 #define RULE_STRCMP(s,r) rule_strcmp((s), (r), sizeof (r) - 1)
611 static const char *rule_strcmp(const char *str, const char *rule, int rule_len)
613 if (strncmp(str, rule, rule_len) != 0)
615 if (isspace(str[rule_len]) || str[rule_len] == '_' || !str[rule_len])
616 return str + rule_len - 1;
617 if (str[rule_len] == ',')
618 return str + rule_len;
622 /* Get the next include/exclude arg from the string. The token will not
623 * be '\0' terminated, so use the returned length to limit the string.
624 * Also, be sure to add this length to the returned pointer before passing
625 * it back to ask for the next token. This routine parses the "!" (list-
626 * clearing) token and (depending on the mflags) the various prefixes.
627 * The *mflags_ptr value will be set on exit to the new MATCHFLG_* bits
628 * for the current token. */
629 static const char *parse_rule_tok(const char *p, uint32 mflags, int xflags,
630 unsigned int *len_ptr, uint32 *mflags_ptr)
632 const uchar *s = (const uchar *)p;
636 if (mflags & MATCHFLG_WORD_SPLIT) {
637 /* Skip over any initial whitespace. */
640 /* Update to point to real start of rule. */
646 new_mflags = mflags & MATCHFLGS_FROM_CONTAINER;
648 /* Figure out what kind of a filter rule "s" is pointing at. Note
649 * that if MATCHFLG_NO_PREFIXES is set, the rule is either an include
650 * or an exclude based on the inheritance of the MATCHFLG_INCLUDE
651 * flag (above). XFLG_OLD_PREFIXES indicates a compatibility mode
652 * for old include/exclude patterns where just "+ " and "- " are
653 * allowed as optional prefixes. */
654 if (mflags & MATCHFLG_NO_PREFIXES) {
655 if (*s == '!' && mflags & MATCHFLG_CVS_IGNORE)
656 new_mflags |= MATCHFLG_CLEAR_LIST; /* Tentative! */
657 } else if (xflags & XFLG_OLD_PREFIXES) {
658 if (*s == '-' && s[1] == ' ') {
659 new_mflags &= ~MATCHFLG_INCLUDE;
661 } else if (*s == '+' && s[1] == ' ') {
662 new_mflags |= MATCHFLG_INCLUDE;
666 new_mflags |= MATCHFLG_CLEAR_LIST; /* Tentative! */
668 char ch = 0, *mods = "";
671 if ((s = RULE_STRCMP(s, "clear")) != NULL)
675 if ((s = RULE_STRCMP(s, "dir-merge")) != NULL)
679 if ((s = RULE_STRCMP(s, "exclude")) != NULL)
683 if ((s = RULE_STRCMP(s, "hide")) != NULL)
687 if ((s = RULE_STRCMP(s, "include")) != NULL)
691 if ((s = RULE_STRCMP(s, "merge")) != NULL)
695 if ((s = RULE_STRCMP(s, "protect")) != NULL)
699 if ((s = RULE_STRCMP(s, "risk")) != NULL)
703 if ((s = RULE_STRCMP(s, "show")) != NULL)
715 new_mflags |= MATCHFLG_PERDIR_MERGE
716 | MATCHFLG_FINISH_SETUP;
719 new_mflags |= MATCHFLG_MERGE_FILE;
720 mods = MODIFIERS_INCL_EXCL MODIFIERS_MERGE_FILE;
723 new_mflags |= MATCHFLG_INCLUDE;
726 mods = MODIFIERS_INCL_EXCL;
729 new_mflags |= MATCHFLG_INCLUDE;
732 new_mflags |= MATCHFLG_SENDER_SIDE;
733 mods = MODIFIERS_HIDE_PROTECT;
736 new_mflags |= MATCHFLG_INCLUDE;
739 new_mflags |= MATCHFLG_RECEIVER_SIDE;
740 mods = MODIFIERS_HIDE_PROTECT;
743 new_mflags |= MATCHFLG_CLEAR_LIST;
747 rprintf(FERROR, "Unknown filter rule: %s\n", p);
748 exit_cleanup(RERR_SYNTAX);
750 while (mods && *++s && *s != ' ' && *s != '_') {
751 if (strchr(mods, *s) == NULL) {
752 if (mflags & MATCHFLG_WORD_SPLIT && isspace(*s)) {
758 "invalid modifier sequence at '%c' in filter rule: %s\n",
760 exit_cleanup(RERR_SYNTAX);
764 if (new_mflags & MATCHFLG_NO_PREFIXES)
766 new_mflags |= MATCHFLG_NO_PREFIXES;
769 if (new_mflags & MATCHFLG_NO_PREFIXES)
771 new_mflags |= MATCHFLG_NO_PREFIXES
775 new_mflags |= MATCHFLG_ABS_PATH;
778 new_mflags |= MATCHFLG_NEGATE;
781 if (new_mflags & MATCHFLG_NO_PREFIXES)
783 new_mflags |= MATCHFLG_NO_PREFIXES
784 | MATCHFLG_WORD_SPLIT
785 | MATCHFLG_NO_INHERIT
786 | MATCHFLG_CVS_IGNORE;
789 new_mflags |= MATCHFLG_EXCLUDE_SELF;
792 new_mflags |= MATCHFLG_NO_INHERIT;
795 new_mflags |= MATCHFLG_RECEIVER_SIDE;
798 new_mflags |= MATCHFLG_SENDER_SIDE;
801 new_mflags |= MATCHFLG_WORD_SPLIT;
809 if (mflags & MATCHFLG_WORD_SPLIT) {
811 /* Token ends at whitespace or the end of the string. */
812 while (!isspace(*cp) && *cp != '\0')
816 len = strlen((char*)s);
818 if (new_mflags & MATCHFLG_CLEAR_LIST) {
819 if (!(xflags & XFLG_OLD_PREFIXES) && len) {
821 "'!' rule has trailing characters: %s\n", p);
822 exit_cleanup(RERR_SYNTAX);
825 new_mflags &= ~MATCHFLG_CLEAR_LIST;
826 } else if (!len && !(new_mflags & MATCHFLG_CVS_IGNORE)) {
827 rprintf(FERROR, "unexpected end of filter rule: %s\n", p);
828 exit_cleanup(RERR_SYNTAX);
832 *mflags_ptr = new_mflags;
833 return (const char *)s;
837 static char default_cvsignore[] =
838 /* These default ignored items come from the CVS manual. */
839 "RCS SCCS CVS CVS.adm RCSLOG cvslog.* tags TAGS"
840 " .make.state .nse_depinfo *~ #* .#* ,* _$* *$"
841 " *.old *.bak *.BAK *.orig *.rej .del-*"
842 " *.a *.olb *.o *.obj *.so *.exe"
843 " *.Z *.elc *.ln core"
844 /* The rest we added to suit ourself. */
847 static void get_cvs_excludes(uint32 mflags)
849 char *p, fname[MAXPATHLEN];
850 static int initialized = 0;
856 parse_rule(&cvs_filter_list, default_cvsignore, mflags, 0);
858 p = module_id >= 0 && lp_use_chroot(module_id) ? "/" : getenv("HOME");
859 if (p && pathjoin(fname, MAXPATHLEN, p, ".cvsignore") < MAXPATHLEN)
860 parse_filter_file(&cvs_filter_list, fname, mflags, 0);
862 parse_rule(&cvs_filter_list, getenv("CVSIGNORE"), mflags, 0);
866 void parse_rule(struct filter_list_struct *listp, const char *pattern,
867 uint32 mflags, int xflags)
869 unsigned int pat_len;
877 /* Remember that the returned string is NOT '\0' terminated! */
878 cp = parse_rule_tok(pattern, mflags, xflags,
879 &pat_len, &new_mflags);
882 if (pat_len >= MAXPATHLEN) {
883 rprintf(FERROR, "discarding over-long filter: %s\n",
887 pattern = cp + pat_len;
889 if (new_mflags & MATCHFLG_CLEAR_LIST) {
892 "[%s] clearing filter list%s\n",
893 who_am_i(), listp->debug_type);
895 clear_filter_list(listp);
899 if (new_mflags & MATCHFLG_MERGE_FILE) {
906 if (new_mflags & MATCHFLG_EXCLUDE_SELF) {
907 const char *name = strrchr(cp, '/');
912 add_rule(listp, name, len, 0, 0);
913 new_mflags &= ~MATCHFLG_EXCLUDE_SELF;
916 if (new_mflags & MATCHFLG_PERDIR_MERGE) {
917 if (parent_dirscan) {
918 if (!(p = parse_merge_name(cp, &len,
921 add_rule(listp, p, len, new_mflags, 0);
925 if (!(p = parse_merge_name(cp, &len, 0)))
927 parse_filter_file(listp, p, new_mflags,
933 add_rule(listp, cp, pat_len, new_mflags, xflags);
935 if (new_mflags & MATCHFLG_CVS_IGNORE
936 && !(new_mflags & MATCHFLG_MERGE_FILE))
937 get_cvs_excludes(new_mflags);
942 void parse_filter_file(struct filter_list_struct *listp, const char *fname,
943 uint32 mflags, int xflags)
946 char line[MAXPATHLEN+MAX_RULE_PREFIX+1]; /* +1 for trailing slash. */
947 char *eob = line + sizeof line - 1;
948 int word_split = mflags & MATCHFLG_WORD_SPLIT;
950 if (!fname || !*fname)
953 if (*fname != '-' || fname[1] || am_server) {
954 if (server_filter_list.head) {
955 strlcpy(line, fname, sizeof line);
956 clean_fname(line, 1);
957 if (check_filter(&server_filter_list, line, 0) < 0)
960 fp = fopen(line, "rb");
962 fp = fopen(fname, "rb");
967 rprintf(FINFO, "[%s] parse_filter_file(%s,%x,%x)%s\n",
968 who_am_i(), safe_fname(fname), mflags, xflags,
969 fp ? "" : " [not found]");
973 if (xflags & XFLG_FATAL_ERRORS) {
974 rsyserr(FERROR, errno,
975 "failed to open %sclude file %s",
976 mflags & MATCHFLG_INCLUDE ? "in" : "ex",
978 exit_cleanup(RERR_FILEIO);
982 dirbuf[dirbuf_len] = '\0';
986 int ch, overflow = 0;
988 if ((ch = getc(fp)) == EOF) {
989 if (ferror(fp) && errno == EINTR)
993 if (word_split && isspace(ch))
995 if (eol_nulls? !ch : (ch == '\n' || ch == '\r'))
1003 rprintf(FERROR, "discarding over-long filter: %s...\n", line);
1007 /* Skip an empty token and (when line parsing) comments. */
1008 if (*line && (word_split || (*line != ';' && *line != '#')))
1009 parse_rule(listp, line, mflags, xflags);
1016 /* If the "for_xfer" flag is set, the prefix is made compatible with the
1017 * current protocol_version (if possible) or a NULL is returned (if not
1019 char *get_rule_prefix(int match_flags, const char *pat, int for_xfer,
1020 unsigned int *plen_ptr)
1022 static char buf[MAX_RULE_PREFIX+1];
1024 int legal_len = for_xfer && protocol_version < 29 ? 1 : MAX_RULE_PREFIX;
1026 if (match_flags & MATCHFLG_PERDIR_MERGE) {
1030 } else if (match_flags & MATCHFLG_INCLUDE)
1032 else if (legal_len != 1
1033 || ((*pat == '-' || *pat == '+') && pat[1] == ' '))
1038 if (match_flags & MATCHFLG_CVS_IGNORE)
1041 if (match_flags & MATCHFLG_NO_INHERIT)
1043 if (match_flags & MATCHFLG_WORD_SPLIT)
1045 if (match_flags & MATCHFLG_NO_PREFIXES) {
1046 if (match_flags & MATCHFLG_INCLUDE)
1052 if (match_flags & MATCHFLG_EXCLUDE_SELF)
1054 if (match_flags & MATCHFLG_SENDER_SIDE
1055 && (!for_xfer || protocol_version >= 29))
1057 if (match_flags & MATCHFLG_RECEIVER_SIDE
1058 && (!for_xfer || protocol_version >= 29
1059 || (delete_excluded && am_sender)))
1063 if (op - buf > legal_len)
1067 *plen_ptr = op - buf;
1071 static void send_rules(int f_out, struct filter_list_struct *flp)
1073 struct filter_struct *ent, *prev = NULL;
1075 for (ent = flp->head; ent; ent = ent->next) {
1076 unsigned int len, plen, dlen;
1080 if (ent->match_flags & MATCHFLG_SENDER_SIDE)
1081 elide = am_sender ? 1 : -1;
1082 if (ent->match_flags & MATCHFLG_RECEIVER_SIDE)
1083 elide = elide ? 0 : am_sender ? -1 : 1;
1084 else if (delete_excluded && !elide)
1085 elide = am_sender ? 1 : -1;
1088 prev->next = ent->next;
1090 flp->head = ent->next;
1095 if (ent->match_flags & MATCHFLG_CVS_IGNORE
1096 && !(ent->match_flags & MATCHFLG_MERGE_FILE)) {
1097 int f = am_sender || protocol_version < 29 ? f_out : -1;
1098 send_rules(f, &cvs_filter_list);
1102 p = get_rule_prefix(ent->match_flags, ent->pattern, 1, &plen);
1105 "filter rules are too modern for remote rsync.\n");
1106 exit_cleanup(RERR_SYNTAX);
1110 len = strlen(ent->pattern);
1111 dlen = ent->match_flags & MATCHFLG_DIRECTORY ? 1 : 0;
1112 if (!(plen + len + dlen))
1114 write_int(f_out, plen + len + dlen);
1116 write_buf(f_out, p, plen);
1117 write_buf(f_out, ent->pattern, len);
1119 write_byte(f_out, '/');
1124 /* This is only called by the client. */
1125 void send_filter_list(int f_out)
1127 int receiver_wants_list = delete_mode
1128 && (!delete_excluded || protocol_version >= 29);
1130 if (local_server || (am_sender && !receiver_wants_list))
1132 if (cvs_exclude && am_sender) {
1133 if (protocol_version >= 29)
1134 parse_rule(&filter_list, ":C", 0, 0);
1135 parse_rule(&filter_list, "-C", 0, 0);
1138 /* This is a complete hack - blame Rusty. FIXME!
1139 * Remove this hack when older rsyncs (below 2.6.4) are gone. */
1140 if (list_only == 1 && !recurse)
1141 parse_rule(&filter_list, "/*/*", MATCHFLG_NO_PREFIXES, 0);
1143 send_rules(f_out, &filter_list);
1146 write_int(f_out, 0);
1149 if (!am_sender || protocol_version < 29)
1150 parse_rule(&filter_list, ":C", 0, 0);
1152 parse_rule(&filter_list, "-C", 0, 0);
1156 /* This is only called by the server. */
1157 void recv_filter_list(int f_in)
1159 char line[MAXPATHLEN+MAX_RULE_PREFIX+1]; /* +1 for trailing slash. */
1160 int xflags = protocol_version >= 29 ? 0 : XFLG_OLD_PREFIXES;
1161 int receiver_wants_list = delete_mode
1162 && (!delete_excluded || protocol_version >= 29);
1165 if (!local_server && (am_sender || receiver_wants_list)) {
1166 while ((len = read_int(f_in)) != 0) {
1167 if (len >= sizeof line)
1168 overflow("recv_rules");
1169 read_sbuf(f_in, line, len);
1170 parse_rule(&filter_list, line, 0, xflags);
1175 if (local_server || am_sender || protocol_version < 29)
1176 parse_rule(&filter_list, ":C", 0, 0);
1177 if (local_server || am_sender)
1178 parse_rule(&filter_list, "-C", 0, 0);
1181 if (local_server) /* filter out any rules that aren't for us. */
1182 send_rules(-1, &filter_list);