Added an mflags arg to get_filter_tok(), add_filter_file(), and
[rsync/rsync.git] / exclude.c
1 /* -*- c-file-style: "linux" -*-
2  *
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
6  *
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.
11  *
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.
16  *
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.
20  */
21
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 :) */
24
25 /* include/exclude cluestick added by Martin Pool <mbp@samba.org> */
26
27 #include "rsync.h"
28
29 extern int verbose;
30 extern int am_server;
31 extern int eol_nulls;
32 extern int list_only;
33 extern int recurse;
34 extern int io_error;
35 extern int sanitize_paths;
36 extern int protocol_version;
37 extern int module_id;
38
39 extern char curr_dir[];
40 extern unsigned int curr_dir_len;
41 extern unsigned int module_dirlen;
42
43 struct filter_list_struct filter_list = { 0, 0, "" };
44 struct filter_list_struct server_filter_list = { 0, 0, "server " };
45
46 /* Need room enough for ":MODS " prefix plus some room to grow. */
47 #define MAX_RULE_PREFIX (16)
48
49 #define MODIFIERS_MERGE_FILE "-+Cens"
50 #define MODIFIERS_INCL_EXCL "/!"
51
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;
59
60 /* This is True when we're scanning parent dirs for per-dir merge-files. */
61 static BOOL parent_dirscan = False;
62
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;
69
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:
78  *
79  * Completely Empty                     Local Content Only
80  * ==================================   ====================================
81  * head -> NULL                         head -> Local1 -> Local2 -> NULL
82  * tail -> NULL                         tail -------------^
83  *
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 ---------^
88  *
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.
99  */
100
101 static void free_filter(struct filter_struct *ex)
102 {
103         if (ex->match_flags & MATCHFLG_PERDIR_MERGE) {
104                 free(ex->u.mergelist->debug_type);
105                 free(ex->u.mergelist);
106                 mergelist_cnt--;
107         }
108         free(ex->pattern);
109         free(ex);
110 }
111
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)
116 {
117         struct filter_struct *ret;
118         const char *cp;
119         unsigned int ex_len;
120
121         if (verbose > 2) {
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",
126                         listp->debug_type);
127         }
128
129         ret = new(struct filter_struct);
130         if (!ret)
131                 out_of_memory("filter_rule");
132         memset(ret, 0, sizeof ret[0]);
133
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;
138         } else
139                 ex_len = 0;
140         ret->pattern = new_array(char, ex_len + pat_len + 1);
141         if (!ret->pattern)
142                 out_of_memory("filter_rule");
143         if (ex_len)
144                 memcpy(ret->pattern, dirbuf + module_dirlen, ex_len);
145         strlcpy(ret->pattern + ex_len, pat, pat_len + 1);
146         pat_len += ex_len;
147
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;
155                 }
156         }
157
158         if (pat_len > 1 && ret->pattern[pat_len-1] == '/') {
159                 ret->pattern[pat_len-1] = 0;
160                 mflags |= MATCHFLG_DIRECTORY;
161         }
162
163         if (mflags & MATCHFLG_PERDIR_MERGE) {
164                 struct filter_list_struct *lp;
165                 unsigned int len;
166                 int i;
167
168                 if ((cp = strrchr(ret->pattern, '/')) != NULL)
169                         cp++;
170                 else
171                         cp = ret->pattern;
172
173                 /* If the local merge file was already mentioned, don't
174                  * add it again. */
175                 for (i = 0; i < mergelist_cnt; i++) {
176                         struct filter_struct *ex = mergelist_parents[i];
177                         const char *s = strrchr(ex->pattern, '/');
178                         if (s)
179                                 s++;
180                         else
181                                 s = ex->pattern;
182                         len = strlen(s);
183                         if (len == pat_len - (cp - ret->pattern)
184                             && memcmp(s, cp, len) == 0) {
185                                 free_filter(ret);
186                                 return;
187                         }
188                 }
189
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;
196
197                 if (mergelist_cnt == mergelist_size) {
198                         mergelist_size += 5;
199                         mergelist_parents = realloc_array(mergelist_parents,
200                                                 struct filter_struct *,
201                                                 mergelist_size);
202                         if (!mergelist_parents)
203                                 out_of_memory("filter_rule");
204                 }
205                 mergelist_parents[mergelist_cnt++] = ret;
206         } else {
207                 for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
208                         ret->u.slash_cnt++;
209         }
210
211         ret->match_flags = mflags;
212
213         if (!listp->tail) {
214                 ret->next = listp->head;
215                 listp->head = listp->tail = ret;
216         } else {
217                 ret->next = listp->tail->next;
218                 listp->tail->next = ret;
219                 listp->tail = ret;
220         }
221 }
222
223 static void clear_filter_list(struct filter_list_struct *listp)
224 {
225         if (listp->tail) {
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) {
231                         next = ent->next;
232                         free_filter(ent);
233                 }
234         }
235
236         listp->head = listp->tail = NULL;
237 }
238
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
245  * name was not. */
246 static char *parse_merge_name(const char *merge_file, unsigned int *len_ptr,
247                               unsigned int prefix_skip)
248 {
249         static char buf[MAXPATHLEN];
250         char *fn, tmpbuf[MAXPATHLEN];
251         unsigned int fn_len;
252
253         if (!parent_dirscan && *merge_file != '/') {
254                 /* Return the name unchanged it doesn't have any slashes. */
255                 if (len_ptr) {
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);
260                                 return buf;
261                         }
262                 } else if (strchr(merge_file, '/') == NULL)
263                         return (char *)merge_file;
264         }
265
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);
273                         merge_file = to;
274                 }
275                 if (!sanitize_path(fn, merge_file, r, dirbuf_depth)) {
276                         rprintf(FERROR, "merge-file name overflows: %s\n",
277                                 merge_file);
278                         return NULL;
279                 }
280         } else {
281                 strlcpy(fn, merge_file, len_ptr ? *len_ptr + 1 : MAXPATHLEN);
282                 clean_fname(fn, 1);
283         }
284         
285         fn_len = strlen(fn);
286         if (fn == buf)
287                 goto done;
288
289         if (dirbuf_len + fn_len >= MAXPATHLEN) {
290                 rprintf(FERROR, "merge-file name overflows: %s\n", fn);
291                 return NULL;
292         }
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);
296
297     done:
298         if (len_ptr)
299                 *len_ptr = fn_len;
300         return buf;
301 }
302
303 /* Sets the dirbuf and dirbuf_len values. */
304 void set_filter_dir(const char *dir, unsigned int dirlen)
305 {
306         unsigned int len;
307         if (*dir != '/') {
308                 memcpy(dirbuf, curr_dir, curr_dir_len);
309                 dirbuf[curr_dir_len] = '/';
310                 len = curr_dir_len + 1;
311                 if (len + dirlen >= MAXPATHLEN)
312                         dirlen = 0;
313         } else
314                 len = 0;
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] == '/')
320                 dirbuf_len -= 2;
321         if (dirbuf_len != 1)
322                 dirbuf[dirbuf_len++] = '/';
323         dirbuf[dirbuf_len] = '\0';
324         if (sanitize_paths)
325                 dirbuf_depth = count_dir_elements(dirbuf + module_dirlen);
326 }
327
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)
335 {
336         char buf[MAXPATHLEN];
337         char *x, *y, *pat = ex->pattern;
338         unsigned int len;
339
340         if (!(x = parse_merge_name(pat, NULL, 0)) || *x != '/')
341                 return 0;
342
343         y = strrchr(x, '/');
344         *y = '\0';
345         ex->pattern = strdup(y+1);
346         if (!*x)
347                 x = "/";
348         if (*x == '/')
349                 strlcpy(buf, x, MAXPATHLEN);
350         else
351                 pathjoin(buf, MAXPATHLEN, dirbuf, x);
352
353         len = clean_fname(buf, 1);
354         if (len != 1 && len < MAXPATHLEN-1) {
355                 buf[len++] = '/';
356                 buf[len] = '\0';
357         }
358         /* This ensures that the specified dir is a parent of the transfer. */
359         for (x = buf, y = dirbuf; *x && *x == *y; x++, y++) {}
360         if (*x)
361                 y += strlen(y); /* nope -- skip the scan */
362
363         parent_dirscan = True;
364         while (*y) {
365                 char save[MAXPATHLEN];
366                 strlcpy(save, y, MAXPATHLEN);
367                 *y = '\0';
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)
372                         lp->head = NULL;
373                 lp->tail = NULL;
374                 strlcpy(y, save, MAXPATHLEN);
375                 while ((*x++ = *y++) != '/') {}
376         }
377         parent_dirscan = False;
378         free(pat);
379         return 1;
380 }
381
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)
387 {
388         struct filter_list_struct *ap, *push;
389         int i;
390
391         set_filter_dir(dir, dirlen);
392
393         if (!mergelist_cnt)
394                 return NULL;
395
396         push = new_array(struct filter_list_struct, mergelist_cnt);
397         if (!push)
398                 out_of_memory("push_local_filters");
399
400         for (i = 0, ap = push; i < mergelist_cnt; i++) {
401                 memcpy(ap++, mergelist_parents[i]->u.mergelist,
402                        sizeof (struct filter_list_struct));
403         }
404
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;
410
411                 if (verbose > 2) {
412                         rprintf(FINFO, "[%s] pushing filter list%s\n",
413                                 who_am_i(), lp->debug_type);
414                 }
415
416                 lp->tail = NULL; /* Switch any local rules to inherited. */
417                 if (ex->match_flags & MATCHFLG_NO_INHERIT)
418                         lp->head = NULL;
419
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);
424                 }
425
426                 if (strlcpy(dirbuf + dirbuf_len, ex->pattern,
427                     MAXPATHLEN - dirbuf_len) < MAXPATHLEN - dirbuf_len) {
428                         add_filter_file(lp, dirbuf, ex->match_flags,
429                                         XFLG_ANCHORED2ABS);
430                 } else {
431                         io_error |= IOERR_GENERAL;
432                         rprintf(FINFO,
433                             "cannot add local filter rules in long-named directory: %s\n",
434                             full_fname(dirbuf));
435                 }
436                 dirbuf[dirbuf_len] = '\0';
437         }
438
439         return (void*)push;
440 }
441
442 void pop_local_filters(void *mem)
443 {
444         struct filter_list_struct *ap, *pop = (struct filter_list_struct*)mem;
445         int i;
446
447         for (i = mergelist_cnt; i-- > 0; ) {
448                 struct filter_struct *ex = mergelist_parents[i];
449                 struct filter_list_struct *lp = ex->u.mergelist;
450
451                 if (verbose > 2) {
452                         rprintf(FINFO, "[%s] popping filter list%s\n",
453                                 who_am_i(), lp->debug_type);
454                 }
455
456                 clear_filter_list(lp);
457         }
458
459         if (!pop)
460                 return;
461
462         for (i = 0, ap = pop; i < mergelist_cnt; i++) {
463                 memcpy(mergelist_parents[i]->u.mergelist, ap++,
464                        sizeof (struct filter_list_struct));
465         }
466
467         free(pop);
468 }
469
470 static int rule_matches(char *name, struct filter_struct *ex, int name_is_dir)
471 {
472         char *p, full_name[MAXPATHLEN];
473         int match_start = 0;
474         int ret_match = ex->match_flags & MATCHFLG_NEGATE ? 0 : 1;
475         char *pattern = ex->pattern;
476
477         if (!*name)
478                 return 0;
479
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)
485                         name = p+1;
486         }
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);
491                 name = full_name;
492         }
493
494         if (ex->match_flags & MATCHFLG_DIRECTORY && !name_is_dir)
495                 return !ret_match;
496
497         if (*pattern == '/') {
498                 match_start = 1;
499                 pattern++;
500                 if (*name == '/')
501                         name++;
502         }
503
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)
512                                         break;
513                         }
514                         name = p+1;
515                 }
516                 if (wildmatch(pattern, name))
517                         return ret_match;
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))
523                                 return ret_match;
524                 }
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) {
530                                 name++;
531                                 if (wildmatch(pattern, name))
532                                         return ret_match;
533                         }
534                 }
535         } else if (match_start) {
536                 if (strcmp(name,pattern) == 0)
537                         return ret_match;
538         } else {
539                 int l1 = strlen(name);
540                 int l2 = strlen(pattern);
541                 if (l2 <= l1 &&
542                     strcmp(name+(l1-l2),pattern) == 0 &&
543                     (l1==l2 || name[l1-(l2+1)] == '/')) {
544                         return ret_match;
545                 }
546         }
547
548         return !ret_match;
549 }
550
551
552 static void report_filter_result(char const *name,
553                                  struct filter_struct const *ent,
554                                  int name_is_dir, const char *type)
555 {
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. */
559
560         if (verbose >= 2) {
561                 rprintf(FINFO, "[%s] %scluding %s %s because of pattern %s%s%s\n",
562                         who_am_i(),
563                         ent->match_flags & MATCHFLG_INCLUDE ? "in" : "ex",
564                         name_is_dir ? "directory" : "file", name, ent->pattern,
565                         ent->match_flags & MATCHFLG_DIRECTORY ? "/" : "", type);
566         }
567 }
568
569
570 /*
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.
573  */
574 int check_filter(struct filter_list_struct *listp, char *name, int name_is_dir)
575 {
576         struct filter_struct *ent;
577
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,
581                                               name_is_dir);
582                         if (rc)
583                                 return rc;
584                         continue;
585                 }
586                 if (rule_matches(name, ent, name_is_dir)) {
587                         report_filter_result(name, ent, name_is_dir,
588                                               listp->debug_type);
589                         return ent->match_flags & MATCHFLG_INCLUDE ? 1 : -1;
590                 }
591         }
592
593         return 0;
594 }
595
596
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)
606 {
607         const unsigned char *s = (const unsigned char *)p;
608         unsigned int len, new_mflags;
609         int empty_pat_is_OK = 0;
610
611         if (mflags & MATCHFLG_WORD_SPLIT) {
612                 /* Skip over any initial whitespace. */
613                 while (isspace(*s))
614                         s++;
615                 /* Update to point to real start of rule. */
616                 p = (const char *)s;
617         }
618         if (!*s)
619                 return NULL;
620
621         new_mflags = mflags & MATCHFLGS_FROM_CONTAINER;
622
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) {
630                 if (*s == '!')
631                         new_mflags |= MATCHFLG_CLEAR_LIST; /* Tentative! */
632         } else if (xflags & XFLG_OLD_PREFIXES) {
633                 if (*s == '-' && s[1] == ' ') {
634                         new_mflags &= ~MATCHFLG_INCLUDE;
635                         s += 2;
636                 } else if (*s == '+' && s[1] == ' ') {
637                         new_mflags |= MATCHFLG_INCLUDE;
638                         s += 2;
639                 }
640                 if (*s == '!')
641                         new_mflags |= MATCHFLG_CLEAR_LIST; /* Tentative! */
642         } else {
643                 char *mods = "";
644                 switch (*s) {
645                 case ':':
646                         new_mflags |= MATCHFLG_PERDIR_MERGE
647                                     | MATCHFLG_FINISH_SETUP;
648                         /* FALL THROUGH */
649                 case '.':
650                         new_mflags |= MATCHFLG_MERGE_FILE;
651                         mods = MODIFIERS_INCL_EXCL MODIFIERS_MERGE_FILE;
652                         break;
653                 case '+':
654                         new_mflags |= MATCHFLG_INCLUDE;
655                         /* FALL THROUGH */
656                 case '-':
657                         mods = MODIFIERS_INCL_EXCL;
658                         break;
659                 case '!':
660                         new_mflags |= MATCHFLG_CLEAR_LIST;
661                         mods = NULL;
662                         break;
663                 default:
664                         rprintf(FERROR, "Unknown filter rule: %s\n", p);
665                         exit_cleanup(RERR_SYNTAX);
666                 }
667                 while (mods && *++s && *s != ' ' && *s != '_') {
668                         if (strchr(mods, *s) == NULL) {
669                                 if (mflags & MATCHFLG_WORD_SPLIT && isspace(*s)) {
670                                         s--;
671                                         break;
672                                 }
673                             invalid:
674                                 rprintf(FERROR,
675                                         "invalid modifier sequence at '%c' in filter rule: %s\n",
676                                         *s, p);
677                                 exit_cleanup(RERR_SYNTAX);
678                         }
679                         switch (*s) {
680                         case '-':
681                                 if (new_mflags & MATCHFLG_NO_PREFIXES)
682                                     goto invalid;
683                                 new_mflags |= MATCHFLG_NO_PREFIXES;
684                                 break;
685                         case '+':
686                                 if (new_mflags & MATCHFLG_NO_PREFIXES)
687                                     goto invalid;
688                                 new_mflags |= MATCHFLG_NO_PREFIXES
689                                             | MATCHFLG_INCLUDE;
690                                 break;
691                         case '/':
692                                 new_mflags |= MATCHFLG_ABS_PATH;
693                                 break;
694                         case '!':
695                                 new_mflags |= MATCHFLG_NEGATE;
696                                 break;
697                         case 'C':
698                                 if (new_mflags & MATCHFLG_NO_PREFIXES)
699                                     goto invalid;
700                                 empty_pat_is_OK = 1;
701                                 new_mflags |= MATCHFLG_NO_PREFIXES
702                                             | MATCHFLG_WORD_SPLIT
703                                             | MATCHFLG_NO_INHERIT;
704                                 break;
705                         case 'e':
706                                 new_mflags |= MATCHFLG_EXCLUDE_SELF;
707                                 break;
708                         case 'n':
709                                 new_mflags |= MATCHFLG_NO_INHERIT;
710                                 break;
711                         case 'w':
712                                 new_mflags |= MATCHFLG_WORD_SPLIT;
713                                 break;
714                         }
715                 }
716                 if (*s)
717                         s++;
718         }
719
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')
724                         cp++;
725                 len = cp - s;
726         } else
727                 len = strlen((char*)s);
728
729         if (new_mflags & MATCHFLG_CLEAR_LIST) {
730                 if (!(xflags & XFLG_OLD_PREFIXES) && len) {
731                         rprintf(FERROR,
732                                 "'!' rule has trailing characters: %s\n", p);
733                         exit_cleanup(RERR_SYNTAX);
734                 }
735                 if (len > 1)
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);
740         }
741
742         *len_ptr = len;
743         *mflags_ptr = new_mflags;
744         return (const char *)s;
745 }
746
747
748 void add_filter(struct filter_list_struct *listp, const char *pattern,
749                 unsigned mflags, int xflags)
750 {
751         unsigned int pat_len, new_mflags;
752         const char *cp, *p;
753
754         if (!pattern)
755                 return;
756
757         while (1) {
758                 /* Remember that the returned string is NOT '\0' terminated! */
759                 cp = get_filter_tok(pattern, mflags, xflags,
760                                     &pat_len, &new_mflags);
761                 if (!cp)
762                         break;
763                 if (pat_len >= MAXPATHLEN) {
764                         rprintf(FERROR, "discarding over-long filter: %s\n",
765                                 cp);
766                         continue;
767                 }
768                 pattern = cp + pat_len;
769
770                 if (new_mflags & MATCHFLG_CLEAR_LIST) {
771                         if (verbose > 2) {
772                                 rprintf(FINFO,
773                                         "[%s] clearing filter list%s\n",
774                                         who_am_i(), listp->debug_type);
775                         }
776                         clear_filter_list(listp);
777                         continue;
778                 }
779
780                 if (!pat_len) {
781                         cp = ".cvsignore";
782                         pat_len = 10;
783                 }
784
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, '/');
789                                 if (name)
790                                         len -= ++name - cp;
791                                 else
792                                         name = cp;
793                                 filter_rule(listp, name, len, 0, 0);
794                                 new_mflags &= ~MATCHFLG_EXCLUDE_SELF;
795                                 len = pat_len;
796                         }
797                         if (new_mflags & MATCHFLG_PERDIR_MERGE) {
798                                 if (parent_dirscan) {
799                                         if (!(p = parse_merge_name(cp, &len,
800                                                                 module_dirlen)))
801                                                 continue;
802                                         filter_rule(listp, p, len,
803                                                     new_mflags, 0);
804                                         continue;
805                                 }
806                         } else {
807                                 if (!(p = parse_merge_name(cp, &len, 0)))
808                                         continue;
809                                 add_filter_file(listp, p, new_mflags,
810                                                 XFLG_FATAL_ERRORS);
811                                 continue;
812                         }
813                 }
814
815                 filter_rule(listp, cp, pat_len, new_mflags, xflags);
816         }
817 }
818
819
820 void add_filter_file(struct filter_list_struct *listp, const char *fname,
821                      unsigned mflags, int xflags)
822 {
823         FILE *fp;
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;
827
828         if (!fname || !*fname)
829                 return;
830
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)
836                                 fp = NULL;
837                         else
838                                 fp = fopen(line, "rb");
839                 } else
840                         fp = fopen(fname, "rb");
841         } else
842                 fp = stdin;
843
844         if (verbose > 2) {
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]");
848         }
849
850         if (!fp) {
851                 if (xflags & XFLG_FATAL_ERRORS) {
852                         rsyserr(FERROR, errno,
853                                 "failed to open %sclude file %s",
854                                 mflags & MATCHFLG_INCLUDE ? "in" : "ex",
855                                 safe_fname(fname));
856                         exit_cleanup(RERR_FILEIO);
857                 }
858                 return;
859         }
860         dirbuf[dirbuf_len] = '\0';
861
862         while (1) {
863                 char *s = line;
864                 int ch, overflow = 0;
865                 while (1) {
866                         if ((ch = getc(fp)) == EOF) {
867                                 if (ferror(fp) && errno == EINTR)
868                                         continue;
869                                 break;
870                         }
871                         if (word_split && isspace(ch))
872                                 break;
873                         if (eol_nulls? !ch : (ch == '\n' || ch == '\r'))
874                                 break;
875                         if (s < eob)
876                                 *s++ = ch;
877                         else
878                                 overflow = 1;
879                 }
880                 if (overflow) {
881                         rprintf(FERROR, "discarding over-long filter: %s...\n", line);
882                         s = line;
883                 }
884                 *s = '\0';
885                 /* Skip an empty token and (when line parsing) comments. */
886                 if (*line && (word_split || (*line != ';' && *line != '#')))
887                         add_filter(listp, line, mflags, xflags);
888                 if (ch == EOF)
889                         break;
890         }
891         fclose(fp);
892 }
893
894 char *get_rule_prefix(int match_flags, const char *pat, unsigned int *plen_ptr)
895 {
896         static char buf[MAX_RULE_PREFIX+1];
897         char *op = buf;
898
899         if (match_flags & MATCHFLG_PERDIR_MERGE) {
900                 *op++ = ':';
901                 if (match_flags & MATCHFLG_WORD_SPLIT)
902                         *op++ = 's';
903                 if (match_flags & MATCHFLG_NO_INHERIT)
904                         *op++ = 'n';
905                 if (match_flags & MATCHFLG_EXCLUDE_SELF)
906                         *op++ = 'e';
907                 if (match_flags & MATCHFLG_NO_PREFIXES) {
908                         if (match_flags & MATCHFLG_INCLUDE)
909                                 *op++ = '+';
910                         else
911                                 *op++ = '-';
912                 }
913                 *op++ = ' ';
914         } else if (match_flags & MATCHFLG_INCLUDE) {
915                 *op++ = '+';
916                 *op++ = ' ';
917         } else if (protocol_version >= 29
918             || ((*pat == '-' || *pat == '+') && pat[1] == ' ')) {
919                 *op++ = '-';
920                 *op++ = ' ';
921         }
922         *op = '\0';
923         if (plen_ptr)
924                 *plen_ptr = op - buf;
925         if (op - buf > MAX_RULE_PREFIX)
926                 overflow("get_rule_prefix");
927         return buf;
928 }
929
930 void send_filter_list(int f)
931 {
932         struct filter_struct *ent;
933
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);
938
939         for (ent = filter_list.head; ent; ent = ent->next) {
940                 unsigned int len, plen, dlen;
941                 char *p;
942
943                 len = strlen(ent->pattern);
944                 if (len == 0 || len >= MAXPATHLEN)
945                         continue;
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)
950                                 continue;
951                         rprintf(FERROR,
952                                 "remote rsync is too old to understand per-directory merge files.\n");
953                         exit_cleanup(RERR_SYNTAX);
954                 }
955                 dlen = ent->match_flags & MATCHFLG_DIRECTORY ? 1 : 0;
956                 write_int(f, plen + len + dlen);
957                 if (plen)
958                         write_buf(f, p, plen);
959                 write_buf(f, ent->pattern, len);
960                 if (dlen)
961                         write_byte(f, '/');
962         }
963
964         write_int(f, 0);
965 }
966
967
968 void recv_filter_list(int f)
969 {
970         char line[MAXPATHLEN+MAX_RULE_PREFIX+1]; /* +1 for trailing slash. */
971         unsigned int xflags = protocol_version >= 29 ? 0 : XFLG_OLD_PREFIXES;
972         unsigned int l;
973
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);
979         }
980 }
981
982
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. */
991         " .svn/";
992
993 void add_cvs_excludes(void)
994 {
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");
999
1000         add_filter(&filter_list, ":C", 0, 0);
1001         add_filter(&filter_list, default_cvsignore, cvs_mflags, 0);
1002
1003         if (p && pathjoin(fname, MAXPATHLEN, p, ".cvsignore") < MAXPATHLEN)
1004                 add_filter_file(&filter_list, fname, cvs_mflags, 0);
1005
1006         add_filter(&filter_list, getenv("CVSIGNORE"), cvs_mflags, 0);
1007 }