A couple fixes in add_rule() for XFLG_ABS_IF_SLASH:
[rsync/rsync.git] / exclude.c
... / ...
CommitLineData
1/*
2 * The filter include/exclude routines.
3 *
4 * Copyright (C) 1996-2001 Andrew Tridgell <tridge@samba.org>
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2002 Martin Pool
7 * Copyright (C) 2003-2008 Wayne Davison
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, visit the http://fsf.org website.
21 */
22
23#include "rsync.h"
24
25extern int verbose;
26extern int am_server;
27extern int am_sender;
28extern int eol_nulls;
29extern int io_error;
30extern int local_server;
31extern int prune_empty_dirs;
32extern int ignore_perishable;
33extern int delete_mode;
34extern int delete_excluded;
35extern int cvs_exclude;
36extern int sanitize_paths;
37extern int protocol_version;
38extern int module_id;
39
40extern char curr_dir[];
41extern unsigned int curr_dir_len;
42extern unsigned int module_dirlen;
43
44struct filter_list_struct filter_list = { 0, 0, "" };
45struct filter_list_struct cvs_filter_list = { 0, 0, " [global CVS]" };
46struct filter_list_struct daemon_filter_list = { 0, 0, " [daemon]" };
47
48/* Need room enough for ":MODS " prefix plus some room to grow. */
49#define MAX_RULE_PREFIX (16)
50
51#define MODIFIERS_MERGE_FILE "-+Cenw"
52#define MODIFIERS_INCL_EXCL "/!Crsp"
53#define MODIFIERS_HIDE_PROTECT "/!p"
54
55/* The dirbuf is set by push_local_filters() to the current subdirectory
56 * relative to curr_dir that is being processed. The path always has a
57 * trailing slash appended, and the variable dirbuf_len contains the length
58 * of this path prefix. The path is always absolute. */
59static char dirbuf[MAXPATHLEN+1];
60static unsigned int dirbuf_len = 0;
61static int dirbuf_depth;
62
63/* This is True when we're scanning parent dirs for per-dir merge-files. */
64static BOOL parent_dirscan = False;
65
66/* This array contains a list of all the currently active per-dir merge
67 * files. This makes it easier to save the appropriate values when we
68 * "push" down into each subdirectory. */
69static struct filter_struct **mergelist_parents;
70static int mergelist_cnt = 0;
71static int mergelist_size = 0;
72
73/* Each filter_list_struct describes a singly-linked list by keeping track
74 * of both the head and tail pointers. The list is slightly unusual in that
75 * a parent-dir's content can be appended to the end of the local list in a
76 * special way: the last item in the local list has its "next" pointer set
77 * to point to the inherited list, but the local list's tail pointer points
78 * at the end of the local list. Thus, if the local list is empty, the head
79 * will be pointing at the inherited content but the tail will be NULL. To
80 * help you visualize this, here are the possible list arrangements:
81 *
82 * Completely Empty Local Content Only
83 * ================================== ====================================
84 * head -> NULL head -> Local1 -> Local2 -> NULL
85 * tail -> NULL tail -------------^
86 *
87 * Inherited Content Only Both Local and Inherited Content
88 * ================================== ====================================
89 * head -> Parent1 -> Parent2 -> NULL head -> L1 -> L2 -> P1 -> P2 -> NULL
90 * tail -> NULL tail ---------^
91 *
92 * This means that anyone wanting to traverse the whole list to use it just
93 * needs to start at the head and use the "next" pointers until it goes
94 * NULL. To add new local content, we insert the item after the tail item
95 * and update the tail (obviously, if "tail" was NULL, we insert it at the
96 * head). To clear the local list, WE MUST NOT FREE THE INHERITED CONTENT
97 * because it is shared between the current list and our parent list(s).
98 * The easiest way to handle this is to simply truncate the list after the
99 * tail item and then free the local list from the head. When inheriting
100 * the list for a new local dir, we just save off the filter_list_struct
101 * values (so we can pop back to them later) and set the tail to NULL.
102 */
103
104static void free_filter(struct filter_struct *ex)
105{
106 if (ex->match_flags & MATCHFLG_PERDIR_MERGE) {
107 free(ex->u.mergelist->debug_type);
108 free(ex->u.mergelist);
109 mergelist_cnt--;
110 }
111 free(ex->pattern);
112 free(ex);
113}
114
115/* Build a filter structure given a filter pattern. The value in "pat"
116 * is not null-terminated. */
117static void add_rule(struct filter_list_struct *listp, const char *pat,
118 unsigned int pat_len, uint32 mflags, int xflags)
119{
120 struct filter_struct *ret;
121 const char *cp;
122 unsigned int pre_len, slash_cnt = 0;
123
124 if (verbose > 2) {
125 rprintf(FINFO, "[%s] add_rule(%s%.*s%s)%s\n",
126 who_am_i(), get_rule_prefix(mflags, pat, 0, NULL),
127 (int)pat_len, pat,
128 (mflags & MATCHFLG_DIRECTORY) ? "/" : "",
129 listp->debug_type);
130 }
131
132 /* These flags also indicate that we're reading a list that
133 * needs to be filtered now, not post-filtered later. */
134 if (xflags & (XFLG_ANCHORED2ABS|XFLG_ABS_IF_SLASH)) {
135 uint32 mf = mflags & (MATCHFLG_RECEIVER_SIDE|MATCHFLG_SENDER_SIDE);
136 if (am_sender) {
137 if (mf == MATCHFLG_RECEIVER_SIDE)
138 return;
139 } else {
140 if (mf == MATCHFLG_SENDER_SIDE)
141 return;
142 }
143 }
144
145 if (!(ret = new0(struct filter_struct)))
146 out_of_memory("add_rule");
147
148 if (pat_len > 1 && pat[pat_len-1] == '/') {
149 pat_len--;
150 mflags |= MATCHFLG_DIRECTORY;
151 }
152
153 for (cp = pat; cp < pat + pat_len; cp++) {
154 if (*cp == '/')
155 slash_cnt++;
156 }
157
158 if (!(mflags & (MATCHFLG_ABS_PATH | MATCHFLG_MERGE_FILE))
159 && ((xflags & (XFLG_ANCHORED2ABS|XFLG_ABS_IF_SLASH) && *pat == '/')
160 || (xflags & XFLG_ABS_IF_SLASH && slash_cnt))) {
161 mflags |= MATCHFLG_ABS_PATH;
162 if (*pat == '/')
163 pre_len = dirbuf_len - module_dirlen - 1;
164 else
165 pre_len = 0;
166 } else
167 pre_len = 0;
168
169 if (!(ret->pattern = new_array(char, pre_len + pat_len + 1)))
170 out_of_memory("add_rule");
171 if (pre_len) {
172 memcpy(ret->pattern, dirbuf + module_dirlen, pre_len);
173 for (cp = ret->pattern; cp < ret->pattern + pre_len; cp++) {
174 if (*cp == '/')
175 slash_cnt++;
176 }
177 }
178 strlcpy(ret->pattern + pre_len, pat, pat_len + 1);
179 pat_len += pre_len;
180
181 if (strpbrk(ret->pattern, "*[?")) {
182 mflags |= MATCHFLG_WILD;
183 if ((cp = strstr(ret->pattern, "**")) != NULL) {
184 mflags |= MATCHFLG_WILD2;
185 /* If the pattern starts with **, note that. */
186 if (cp == ret->pattern)
187 mflags |= MATCHFLG_WILD2_PREFIX;
188 /* If the pattern ends with ***, note that. */
189 if (pat_len >= 3
190 && ret->pattern[pat_len-3] == '*'
191 && ret->pattern[pat_len-2] == '*'
192 && ret->pattern[pat_len-1] == '*')
193 mflags |= MATCHFLG_WILD3_SUFFIX;
194 }
195 }
196
197 if (mflags & MATCHFLG_PERDIR_MERGE) {
198 struct filter_list_struct *lp;
199 unsigned int len;
200 int i;
201
202 if ((cp = strrchr(ret->pattern, '/')) != NULL)
203 cp++;
204 else
205 cp = ret->pattern;
206
207 /* If the local merge file was already mentioned, don't
208 * add it again. */
209 for (i = 0; i < mergelist_cnt; i++) {
210 struct filter_struct *ex = mergelist_parents[i];
211 const char *s = strrchr(ex->pattern, '/');
212 if (s)
213 s++;
214 else
215 s = ex->pattern;
216 len = strlen(s);
217 if (len == pat_len - (cp - ret->pattern)
218 && memcmp(s, cp, len) == 0) {
219 free_filter(ret);
220 return;
221 }
222 }
223
224 if (!(lp = new_array(struct filter_list_struct, 1)))
225 out_of_memory("add_rule");
226 lp->head = lp->tail = NULL;
227 if (asprintf(&lp->debug_type, " [per-dir %s]", cp) < 0)
228 out_of_memory("add_rule");
229 ret->u.mergelist = lp;
230
231 if (mergelist_cnt == mergelist_size) {
232 mergelist_size += 5;
233 mergelist_parents = realloc_array(mergelist_parents,
234 struct filter_struct *,
235 mergelist_size);
236 if (!mergelist_parents)
237 out_of_memory("add_rule");
238 }
239 mergelist_parents[mergelist_cnt++] = ret;
240 } else
241 ret->u.slash_cnt = slash_cnt;
242
243 ret->match_flags = mflags;
244
245 if (!listp->tail) {
246 ret->next = listp->head;
247 listp->head = listp->tail = ret;
248 } else {
249 ret->next = listp->tail->next;
250 listp->tail->next = ret;
251 listp->tail = ret;
252 }
253}
254
255static void clear_filter_list(struct filter_list_struct *listp)
256{
257 if (listp->tail) {
258 struct filter_struct *ent, *next;
259 /* Truncate any inherited items from the local list. */
260 listp->tail->next = NULL;
261 /* Now free everything that is left. */
262 for (ent = listp->head; ent; ent = next) {
263 next = ent->next;
264 free_filter(ent);
265 }
266 }
267
268 listp->head = listp->tail = NULL;
269}
270
271/* This returns an expanded (absolute) filename for the merge-file name if
272 * the name has any slashes in it OR if the parent_dirscan var is True;
273 * otherwise it returns the original merge_file name. If the len_ptr value
274 * is non-NULL the merge_file name is limited by the referenced length
275 * value and will be updated with the length of the resulting name. We
276 * always return a name that is null terminated, even if the merge_file
277 * name was not. */
278static char *parse_merge_name(const char *merge_file, unsigned int *len_ptr,
279 unsigned int prefix_skip)
280{
281 static char buf[MAXPATHLEN];
282 char *fn, tmpbuf[MAXPATHLEN];
283 unsigned int fn_len;
284
285 if (!parent_dirscan && *merge_file != '/') {
286 /* Return the name unchanged it doesn't have any slashes. */
287 if (len_ptr) {
288 const char *p = merge_file + *len_ptr;
289 while (--p > merge_file && *p != '/') {}
290 if (p == merge_file) {
291 strlcpy(buf, merge_file, *len_ptr + 1);
292 return buf;
293 }
294 } else if (strchr(merge_file, '/') == NULL)
295 return (char *)merge_file;
296 }
297
298 fn = *merge_file == '/' ? buf : tmpbuf;
299 if (sanitize_paths) {
300 const char *r = prefix_skip ? "/" : NULL;
301 /* null-terminate the name if it isn't already */
302 if (len_ptr && merge_file[*len_ptr]) {
303 char *to = fn == buf ? tmpbuf : buf;
304 strlcpy(to, merge_file, *len_ptr + 1);
305 merge_file = to;
306 }
307 if (!sanitize_path(fn, merge_file, r, dirbuf_depth, SP_DEFAULT)) {
308 rprintf(FERROR, "merge-file name overflows: %s\n",
309 merge_file);
310 return NULL;
311 }
312 fn_len = strlen(fn);
313 } else {
314 strlcpy(fn, merge_file, len_ptr ? *len_ptr + 1 : MAXPATHLEN);
315 fn_len = clean_fname(fn, CFN_COLLAPSE_DOT_DOT_DIRS);
316 }
317
318 /* If the name isn't in buf yet, it's wasn't absolute. */
319 if (fn != buf) {
320 if (dirbuf_len + fn_len >= MAXPATHLEN) {
321 rprintf(FERROR, "merge-file name overflows: %s\n", fn);
322 return NULL;
323 }
324 memcpy(buf, dirbuf + prefix_skip, dirbuf_len - prefix_skip);
325 memcpy(buf + dirbuf_len - prefix_skip, fn, fn_len + 1);
326 fn_len = clean_fname(buf, CFN_COLLAPSE_DOT_DOT_DIRS);
327 }
328
329 if (len_ptr)
330 *len_ptr = fn_len;
331 return buf;
332}
333
334/* Sets the dirbuf and dirbuf_len values. */
335void set_filter_dir(const char *dir, unsigned int dirlen)
336{
337 unsigned int len;
338 if (*dir != '/') {
339 memcpy(dirbuf, curr_dir, curr_dir_len);
340 dirbuf[curr_dir_len] = '/';
341 len = curr_dir_len + 1;
342 if (len + dirlen >= MAXPATHLEN)
343 dirlen = 0;
344 } else
345 len = 0;
346 memcpy(dirbuf + len, dir, dirlen);
347 dirbuf[dirlen + len] = '\0';
348 dirbuf_len = clean_fname(dirbuf, CFN_COLLAPSE_DOT_DOT_DIRS);
349 if (dirbuf_len > 1 && dirbuf[dirbuf_len-1] == '.'
350 && dirbuf[dirbuf_len-2] == '/')
351 dirbuf_len -= 2;
352 if (dirbuf_len != 1)
353 dirbuf[dirbuf_len++] = '/';
354 dirbuf[dirbuf_len] = '\0';
355 if (sanitize_paths)
356 dirbuf_depth = count_dir_elements(dirbuf + module_dirlen);
357}
358
359/* This routine takes a per-dir merge-file entry and finishes its setup.
360 * If the name has a path portion then we check to see if it refers to a
361 * parent directory of the first transfer dir. If it does, we scan all the
362 * dirs from that point through the parent dir of the transfer dir looking
363 * for the per-dir merge-file in each one. */
364static BOOL setup_merge_file(struct filter_struct *ex,
365 struct filter_list_struct *lp)
366{
367 char buf[MAXPATHLEN];
368 char *x, *y, *pat = ex->pattern;
369 unsigned int len;
370
371 if (!(x = parse_merge_name(pat, NULL, 0)) || *x != '/')
372 return 0;
373
374 y = strrchr(x, '/');
375 *y = '\0';
376 ex->pattern = strdup(y+1);
377 if (!*x)
378 x = "/";
379 if (*x == '/')
380 strlcpy(buf, x, MAXPATHLEN);
381 else
382 pathjoin(buf, MAXPATHLEN, dirbuf, x);
383
384 len = clean_fname(buf, CFN_COLLAPSE_DOT_DOT_DIRS);
385 if (len != 1 && len < MAXPATHLEN-1) {
386 buf[len++] = '/';
387 buf[len] = '\0';
388 }
389 /* This ensures that the specified dir is a parent of the transfer. */
390 for (x = buf, y = dirbuf; *x && *x == *y; x++, y++) {}
391 if (*x)
392 y += strlen(y); /* nope -- skip the scan */
393
394 parent_dirscan = True;
395 while (*y) {
396 char save[MAXPATHLEN];
397 strlcpy(save, y, MAXPATHLEN);
398 *y = '\0';
399 dirbuf_len = y - dirbuf;
400 strlcpy(x, ex->pattern, MAXPATHLEN - (x - buf));
401 parse_filter_file(lp, buf, ex->match_flags, XFLG_ANCHORED2ABS);
402 if (ex->match_flags & MATCHFLG_NO_INHERIT)
403 lp->head = NULL;
404 lp->tail = NULL;
405 strlcpy(y, save, MAXPATHLEN);
406 while ((*x++ = *y++) != '/') {}
407 }
408 parent_dirscan = False;
409 free(pat);
410 return 1;
411}
412
413/* Each time rsync changes to a new directory it call this function to
414 * handle all the per-dir merge-files. The "dir" value is the current path
415 * relative to curr_dir (which might not be null-terminated). We copy it
416 * into dirbuf so that we can easily append a file name on the end. */
417void *push_local_filters(const char *dir, unsigned int dirlen)
418{
419 struct filter_list_struct *ap, *push;
420 int i;
421
422 set_filter_dir(dir, dirlen);
423
424 if (!mergelist_cnt)
425 return NULL;
426
427 push = new_array(struct filter_list_struct, mergelist_cnt);
428 if (!push)
429 out_of_memory("push_local_filters");
430
431 for (i = 0, ap = push; i < mergelist_cnt; i++) {
432 memcpy(ap++, mergelist_parents[i]->u.mergelist,
433 sizeof (struct filter_list_struct));
434 }
435
436 /* Note: parse_filter_file() might increase mergelist_cnt, so keep
437 * this loop separate from the above loop. */
438 for (i = 0; i < mergelist_cnt; i++) {
439 struct filter_struct *ex = mergelist_parents[i];
440 struct filter_list_struct *lp = ex->u.mergelist;
441
442 if (verbose > 2) {
443 rprintf(FINFO, "[%s] pushing filter list%s\n",
444 who_am_i(), lp->debug_type);
445 }
446
447 lp->tail = NULL; /* Switch any local rules to inherited. */
448 if (ex->match_flags & MATCHFLG_NO_INHERIT)
449 lp->head = NULL;
450
451 if (ex->match_flags & MATCHFLG_FINISH_SETUP) {
452 ex->match_flags &= ~MATCHFLG_FINISH_SETUP;
453 if (setup_merge_file(ex, lp))
454 set_filter_dir(dir, dirlen);
455 }
456
457 if (strlcpy(dirbuf + dirbuf_len, ex->pattern,
458 MAXPATHLEN - dirbuf_len) < MAXPATHLEN - dirbuf_len) {
459 parse_filter_file(lp, dirbuf, ex->match_flags,
460 XFLG_ANCHORED2ABS);
461 } else {
462 io_error |= IOERR_GENERAL;
463 rprintf(FINFO,
464 "cannot add local filter rules in long-named directory: %s\n",
465 full_fname(dirbuf));
466 }
467 dirbuf[dirbuf_len] = '\0';
468 }
469
470 return (void*)push;
471}
472
473void pop_local_filters(void *mem)
474{
475 struct filter_list_struct *ap, *pop = (struct filter_list_struct*)mem;
476 int i;
477
478 for (i = mergelist_cnt; i-- > 0; ) {
479 struct filter_struct *ex = mergelist_parents[i];
480 struct filter_list_struct *lp = ex->u.mergelist;
481
482 if (verbose > 2) {
483 rprintf(FINFO, "[%s] popping filter list%s\n",
484 who_am_i(), lp->debug_type);
485 }
486
487 clear_filter_list(lp);
488 }
489
490 if (!pop)
491 return;
492
493 for (i = 0, ap = pop; i < mergelist_cnt; i++) {
494 memcpy(mergelist_parents[i]->u.mergelist, ap++,
495 sizeof (struct filter_list_struct));
496 }
497
498 free(pop);
499}
500
501void change_local_filter_dir(const char *dname, int dlen, int dir_depth)
502{
503 static int cur_depth = -1;
504 static void *filt_array[MAXPATHLEN/2+1];
505
506 if (!dname) {
507 for ( ; cur_depth >= 0; cur_depth--) {
508 if (filt_array[cur_depth]) {
509 pop_local_filters(filt_array[cur_depth]);
510 filt_array[cur_depth] = NULL;
511 }
512 }
513 return;
514 }
515
516 assert(dir_depth < MAXPATHLEN/2+1);
517
518 for ( ; cur_depth >= dir_depth; cur_depth--) {
519 if (filt_array[cur_depth]) {
520 pop_local_filters(filt_array[cur_depth]);
521 filt_array[cur_depth] = NULL;
522 }
523 }
524
525 cur_depth = dir_depth;
526 filt_array[cur_depth] = push_local_filters(dname, dlen);
527}
528
529static int rule_matches(const char *fname, struct filter_struct *ex, int name_is_dir)
530{
531 int slash_handling, str_cnt = 0, anchored_match = 0;
532 int ret_match = ex->match_flags & MATCHFLG_NEGATE ? 0 : 1;
533 char *p, *pattern = ex->pattern;
534 const char *strings[16]; /* more than enough */
535 const char *name = fname + (*fname == '/');
536
537 if (!*name)
538 return 0;
539
540 if (!ex->u.slash_cnt && !(ex->match_flags & MATCHFLG_WILD2)) {
541 /* If the pattern does not have any slashes AND it does
542 * not have a "**" (which could match a slash), then we
543 * just match the name portion of the path. */
544 if ((p = strrchr(name,'/')) != NULL)
545 name = p+1;
546 } else if (ex->match_flags & MATCHFLG_ABS_PATH && *fname != '/'
547 && curr_dir_len > module_dirlen + 1) {
548 /* If we're matching against an absolute-path pattern,
549 * we need to prepend our full path info. */
550 strings[str_cnt++] = curr_dir + module_dirlen + 1;
551 strings[str_cnt++] = "/";
552 } else if (ex->match_flags & MATCHFLG_WILD2_PREFIX && *fname != '/') {
553 /* Allow "**"+"/" to match at the start of the string. */
554 strings[str_cnt++] = "/";
555 }
556 strings[str_cnt++] = name;
557 if (name_is_dir) {
558 /* Allow a trailing "/"+"***" to match the directory. */
559 if (ex->match_flags & MATCHFLG_WILD3_SUFFIX)
560 strings[str_cnt++] = "/";
561 } else if (ex->match_flags & MATCHFLG_DIRECTORY)
562 return !ret_match;
563 strings[str_cnt] = NULL;
564
565 if (*pattern == '/') {
566 anchored_match = 1;
567 pattern++;
568 }
569
570 if (!anchored_match && ex->u.slash_cnt
571 && !(ex->match_flags & MATCHFLG_WILD2)) {
572 /* A non-anchored match with an infix slash and no "**"
573 * needs to match the last slash_cnt+1 name elements. */
574 slash_handling = ex->u.slash_cnt + 1;
575 } else if (!anchored_match && !(ex->match_flags & MATCHFLG_WILD2_PREFIX)
576 && ex->match_flags & MATCHFLG_WILD2) {
577 /* A non-anchored match with an infix or trailing "**" (but not
578 * a prefixed "**") needs to try matching after every slash. */
579 slash_handling = -1;
580 } else {
581 /* The pattern matches only at the start of the path or name. */
582 slash_handling = 0;
583 }
584
585 if (ex->match_flags & MATCHFLG_WILD) {
586 if (wildmatch_array(pattern, strings, slash_handling))
587 return ret_match;
588 } else if (str_cnt > 1) {
589 if (litmatch_array(pattern, strings, slash_handling))
590 return ret_match;
591 } else if (anchored_match) {
592 if (strcmp(name, pattern) == 0)
593 return ret_match;
594 } else {
595 int l1 = strlen(name);
596 int l2 = strlen(pattern);
597 if (l2 <= l1 &&
598 strcmp(name+(l1-l2),pattern) == 0 &&
599 (l1==l2 || name[l1-(l2+1)] == '/')) {
600 return ret_match;
601 }
602 }
603
604 return !ret_match;
605}
606
607
608static void report_filter_result(char const *name,
609 struct filter_struct const *ent,
610 int name_is_dir, const char *type)
611{
612 /* If a trailing slash is present to match only directories,
613 * then it is stripped out by add_rule(). So as a special
614 * case we add it back in here. */
615
616 if (verbose >= 2) {
617 static char *actions[2][2]
618 = { {"show", "hid"}, {"risk", "protect"} };
619 const char *w = who_am_i();
620 rprintf(FINFO, "[%s] %sing %s %s because of pattern %s%s%s\n",
621 w, actions[*w!='s'][!(ent->match_flags&MATCHFLG_INCLUDE)],
622 name_is_dir ? "directory" : "file", name, ent->pattern,
623 ent->match_flags & MATCHFLG_DIRECTORY ? "/" : "", type);
624 }
625}
626
627
628/*
629 * Return -1 if file "name" is defined to be excluded by the specified
630 * exclude list, 1 if it is included, and 0 if it was not matched.
631 */
632int check_filter(struct filter_list_struct *listp, const char *name, int name_is_dir)
633{
634 struct filter_struct *ent;
635
636 for (ent = listp->head; ent; ent = ent->next) {
637 if (ignore_perishable && ent->match_flags & MATCHFLG_PERISHABLE)
638 continue;
639 if (ent->match_flags & MATCHFLG_PERDIR_MERGE) {
640 int rc = check_filter(ent->u.mergelist, name,
641 name_is_dir);
642 if (rc)
643 return rc;
644 continue;
645 }
646 if (ent->match_flags & MATCHFLG_CVS_IGNORE) {
647 int rc = check_filter(&cvs_filter_list, name,
648 name_is_dir);
649 if (rc)
650 return rc;
651 continue;
652 }
653 if (rule_matches(name, ent, name_is_dir)) {
654 report_filter_result(name, ent, name_is_dir,
655 listp->debug_type);
656 return ent->match_flags & MATCHFLG_INCLUDE ? 1 : -1;
657 }
658 }
659
660 return 0;
661}
662
663#define RULE_STRCMP(s,r) rule_strcmp((s), (r), sizeof (r) - 1)
664
665static const uchar *rule_strcmp(const uchar *str, const char *rule, int rule_len)
666{
667 if (strncmp((char*)str, rule, rule_len) != 0)
668 return NULL;
669 if (isspace(str[rule_len]) || str[rule_len] == '_' || !str[rule_len])
670 return str + rule_len - 1;
671 if (str[rule_len] == ',')
672 return str + rule_len;
673 return NULL;
674}
675
676/* Get the next include/exclude arg from the string. The token will not
677 * be '\0' terminated, so use the returned length to limit the string.
678 * Also, be sure to add this length to the returned pointer before passing
679 * it back to ask for the next token. This routine parses the "!" (list-
680 * clearing) token and (depending on the mflags) the various prefixes.
681 * The *mflags_ptr value will be set on exit to the new MATCHFLG_* bits
682 * for the current token. */
683static const char *parse_rule_tok(const char *p, uint32 mflags, int xflags,
684 unsigned int *len_ptr, uint32 *mflags_ptr)
685{
686 const uchar *s = (const uchar *)p;
687 uint32 new_mflags;
688 unsigned int len;
689
690 if (mflags & MATCHFLG_WORD_SPLIT) {
691 /* Skip over any initial whitespace. */
692 while (isspace(*s))
693 s++;
694 /* Update to point to real start of rule. */
695 p = (const char *)s;
696 }
697 if (!*s)
698 return NULL;
699
700 new_mflags = mflags & MATCHFLGS_FROM_CONTAINER;
701
702 /* Figure out what kind of a filter rule "s" is pointing at. Note
703 * that if MATCHFLG_NO_PREFIXES is set, the rule is either an include
704 * or an exclude based on the inheritance of the MATCHFLG_INCLUDE
705 * flag (above). XFLG_OLD_PREFIXES indicates a compatibility mode
706 * for old include/exclude patterns where just "+ " and "- " are
707 * allowed as optional prefixes. */
708 if (mflags & MATCHFLG_NO_PREFIXES) {
709 if (*s == '!' && mflags & MATCHFLG_CVS_IGNORE)
710 new_mflags |= MATCHFLG_CLEAR_LIST; /* Tentative! */
711 } else if (xflags & XFLG_OLD_PREFIXES) {
712 if (*s == '-' && s[1] == ' ') {
713 new_mflags &= ~MATCHFLG_INCLUDE;
714 s += 2;
715 } else if (*s == '+' && s[1] == ' ') {
716 new_mflags |= MATCHFLG_INCLUDE;
717 s += 2;
718 } else if (*s == '!')
719 new_mflags |= MATCHFLG_CLEAR_LIST; /* Tentative! */
720 } else {
721 char ch = 0, *mods = "";
722 switch (*s) {
723 case 'c':
724 if ((s = RULE_STRCMP(s, "clear")) != NULL)
725 ch = '!';
726 break;
727 case 'd':
728 if ((s = RULE_STRCMP(s, "dir-merge")) != NULL)
729 ch = ':';
730 break;
731 case 'e':
732 if ((s = RULE_STRCMP(s, "exclude")) != NULL)
733 ch = '-';
734 break;
735 case 'h':
736 if ((s = RULE_STRCMP(s, "hide")) != NULL)
737 ch = 'H';
738 break;
739 case 'i':
740 if ((s = RULE_STRCMP(s, "include")) != NULL)
741 ch = '+';
742 break;
743 case 'm':
744 if ((s = RULE_STRCMP(s, "merge")) != NULL)
745 ch = '.';
746 break;
747 case 'p':
748 if ((s = RULE_STRCMP(s, "protect")) != NULL)
749 ch = 'P';
750 break;
751 case 'r':
752 if ((s = RULE_STRCMP(s, "risk")) != NULL)
753 ch = 'R';
754 break;
755 case 's':
756 if ((s = RULE_STRCMP(s, "show")) != NULL)
757 ch = 'S';
758 break;
759 default:
760 ch = *s;
761 if (s[1] == ',')
762 s++;
763 break;
764 }
765 switch (ch) {
766 case ':':
767 new_mflags |= MATCHFLG_PERDIR_MERGE
768 | MATCHFLG_FINISH_SETUP;
769 /* FALL THROUGH */
770 case '.':
771 new_mflags |= MATCHFLG_MERGE_FILE;
772 mods = MODIFIERS_INCL_EXCL MODIFIERS_MERGE_FILE;
773 break;
774 case '+':
775 new_mflags |= MATCHFLG_INCLUDE;
776 /* FALL THROUGH */
777 case '-':
778 mods = MODIFIERS_INCL_EXCL;
779 break;
780 case 'S':
781 new_mflags |= MATCHFLG_INCLUDE;
782 /* FALL THROUGH */
783 case 'H':
784 new_mflags |= MATCHFLG_SENDER_SIDE;
785 mods = MODIFIERS_HIDE_PROTECT;
786 break;
787 case 'R':
788 new_mflags |= MATCHFLG_INCLUDE;
789 /* FALL THROUGH */
790 case 'P':
791 new_mflags |= MATCHFLG_RECEIVER_SIDE;
792 mods = MODIFIERS_HIDE_PROTECT;
793 break;
794 case '!':
795 new_mflags |= MATCHFLG_CLEAR_LIST;
796 mods = NULL;
797 break;
798 default:
799 rprintf(FERROR, "Unknown filter rule: `%s'\n", p);
800 exit_cleanup(RERR_SYNTAX);
801 }
802 while (mods && *++s && *s != ' ' && *s != '_') {
803 if (strchr(mods, *s) == NULL) {
804 if (mflags & MATCHFLG_WORD_SPLIT && isspace(*s)) {
805 s--;
806 break;
807 }
808 invalid:
809 rprintf(FERROR,
810 "invalid modifier sequence at '%c' in filter rule: %s\n",
811 *s, p);
812 exit_cleanup(RERR_SYNTAX);
813 }
814 switch (*s) {
815 case '-':
816 if (new_mflags & MATCHFLG_NO_PREFIXES)
817 goto invalid;
818 new_mflags |= MATCHFLG_NO_PREFIXES;
819 break;
820 case '+':
821 if (new_mflags & MATCHFLG_NO_PREFIXES)
822 goto invalid;
823 new_mflags |= MATCHFLG_NO_PREFIXES
824 | MATCHFLG_INCLUDE;
825 break;
826 case '/':
827 new_mflags |= MATCHFLG_ABS_PATH;
828 break;
829 case '!':
830 new_mflags |= MATCHFLG_NEGATE;
831 break;
832 case 'C':
833 if (new_mflags & MATCHFLG_NO_PREFIXES)
834 goto invalid;
835 new_mflags |= MATCHFLG_NO_PREFIXES
836 | MATCHFLG_WORD_SPLIT
837 | MATCHFLG_NO_INHERIT
838 | MATCHFLG_CVS_IGNORE;
839 break;
840 case 'e':
841 new_mflags |= MATCHFLG_EXCLUDE_SELF;
842 break;
843 case 'n':
844 new_mflags |= MATCHFLG_NO_INHERIT;
845 break;
846 case 'p':
847 new_mflags |= MATCHFLG_PERISHABLE;
848 break;
849 case 'r':
850 new_mflags |= MATCHFLG_RECEIVER_SIDE;
851 break;
852 case 's':
853 new_mflags |= MATCHFLG_SENDER_SIDE;
854 break;
855 case 'w':
856 new_mflags |= MATCHFLG_WORD_SPLIT;
857 break;
858 }
859 }
860 if (*s)
861 s++;
862 }
863
864 if (mflags & MATCHFLG_WORD_SPLIT) {
865 const uchar *cp = s;
866 /* Token ends at whitespace or the end of the string. */
867 while (!isspace(*cp) && *cp != '\0')
868 cp++;
869 len = cp - s;
870 } else
871 len = strlen((char*)s);
872
873 if (new_mflags & MATCHFLG_CLEAR_LIST) {
874 if (!(mflags & MATCHFLG_NO_PREFIXES)
875 && !(xflags & XFLG_OLD_PREFIXES) && len) {
876 rprintf(FERROR,
877 "'!' rule has trailing characters: %s\n", p);
878 exit_cleanup(RERR_SYNTAX);
879 }
880 if (len > 1)
881 new_mflags &= ~MATCHFLG_CLEAR_LIST;
882 } else if (!len && !(new_mflags & MATCHFLG_CVS_IGNORE)) {
883 rprintf(FERROR, "unexpected end of filter rule: %s\n", p);
884 exit_cleanup(RERR_SYNTAX);
885 }
886
887 /* --delete-excluded turns an un-modified include/exclude into a
888 * sender-side rule. We also affect per-dir merge files that take
889 * no prefixes as a simple optimization. */
890 if (delete_excluded
891 && !(new_mflags & (MATCHFLG_RECEIVER_SIDE|MATCHFLG_SENDER_SIDE))
892 && (!(new_mflags & MATCHFLG_PERDIR_MERGE)
893 || new_mflags & MATCHFLG_NO_PREFIXES))
894 new_mflags |= MATCHFLG_SENDER_SIDE;
895
896 *len_ptr = len;
897 *mflags_ptr = new_mflags;
898 return (const char *)s;
899}
900
901
902static char default_cvsignore[] =
903 /* These default ignored items come from the CVS manual. */
904 "RCS SCCS CVS CVS.adm RCSLOG cvslog.* tags TAGS"
905 " .make.state .nse_depinfo *~ #* .#* ,* _$* *$"
906 " *.old *.bak *.BAK *.orig *.rej .del-*"
907 " *.a *.olb *.o *.obj *.so *.exe"
908 " *.Z *.elc *.ln core"
909 /* The rest we added to suit ourself. */
910 " .svn/ .git/ .bzr/";
911
912static void get_cvs_excludes(uint32 mflags)
913{
914 static int initialized = 0;
915 char *p, fname[MAXPATHLEN];
916
917 if (initialized)
918 return;
919 initialized = 1;
920
921 parse_rule(&cvs_filter_list, default_cvsignore,
922 mflags | (protocol_version >= 30 ? MATCHFLG_PERISHABLE : 0),
923 0);
924
925 p = module_id >= 0 && lp_use_chroot(module_id) ? "/" : getenv("HOME");
926 if (p && pathjoin(fname, MAXPATHLEN, p, ".cvsignore") < MAXPATHLEN)
927 parse_filter_file(&cvs_filter_list, fname, mflags, 0);
928
929 parse_rule(&cvs_filter_list, getenv("CVSIGNORE"), mflags, 0);
930}
931
932
933void parse_rule(struct filter_list_struct *listp, const char *pattern,
934 uint32 mflags, int xflags)
935{
936 unsigned int pat_len;
937 uint32 new_mflags;
938 const char *cp, *p;
939
940 if (!pattern)
941 return;
942
943 while (1) {
944 /* Remember that the returned string is NOT '\0' terminated! */
945 cp = parse_rule_tok(pattern, mflags, xflags,
946 &pat_len, &new_mflags);
947 if (!cp)
948 break;
949
950 pattern = cp + pat_len;
951
952 if (pat_len >= MAXPATHLEN) {
953 rprintf(FERROR, "discarding over-long filter: %.*s\n",
954 (int)pat_len, cp);
955 continue;
956 }
957
958 if (new_mflags & MATCHFLG_CLEAR_LIST) {
959 if (verbose > 2) {
960 rprintf(FINFO,
961 "[%s] clearing filter list%s\n",
962 who_am_i(), listp->debug_type);
963 }
964 clear_filter_list(listp);
965 continue;
966 }
967
968 if (new_mflags & MATCHFLG_MERGE_FILE) {
969 unsigned int len;
970 if (!pat_len) {
971 cp = ".cvsignore";
972 pat_len = 10;
973 }
974 len = pat_len;
975 if (new_mflags & MATCHFLG_EXCLUDE_SELF) {
976 const char *name = cp + len;
977 while (name > cp && name[-1] != '/') name--;
978 len -= name - cp;
979 add_rule(listp, name, len, 0, 0);
980 new_mflags &= ~MATCHFLG_EXCLUDE_SELF;
981 len = pat_len;
982 }
983 if (new_mflags & MATCHFLG_PERDIR_MERGE) {
984 if (parent_dirscan) {
985 if (!(p = parse_merge_name(cp, &len,
986 module_dirlen)))
987 continue;
988 add_rule(listp, p, len, new_mflags, 0);
989 continue;
990 }
991 } else {
992 if (!(p = parse_merge_name(cp, &len, 0)))
993 continue;
994 parse_filter_file(listp, p, new_mflags,
995 XFLG_FATAL_ERRORS);
996 continue;
997 }
998 }
999
1000 add_rule(listp, cp, pat_len, new_mflags, xflags);
1001
1002 if (new_mflags & MATCHFLG_CVS_IGNORE
1003 && !(new_mflags & MATCHFLG_MERGE_FILE))
1004 get_cvs_excludes(new_mflags);
1005 }
1006}
1007
1008
1009void parse_filter_file(struct filter_list_struct *listp, const char *fname,
1010 uint32 mflags, int xflags)
1011{
1012 FILE *fp;
1013 char line[BIGPATHBUFLEN];
1014 char *eob = line + sizeof line - 1;
1015 int word_split = mflags & MATCHFLG_WORD_SPLIT;
1016
1017 if (!fname || !*fname)
1018 return;
1019
1020 if (*fname != '-' || fname[1] || am_server) {
1021 if (daemon_filter_list.head) {
1022 strlcpy(line, fname, sizeof line);
1023 clean_fname(line, CFN_COLLAPSE_DOT_DOT_DIRS);
1024 if (check_filter(&daemon_filter_list, line, 0) < 0)
1025 fp = NULL;
1026 else
1027 fp = fopen(line, "rb");
1028 } else
1029 fp = fopen(fname, "rb");
1030 } else
1031 fp = stdin;
1032
1033 if (verbose > 2) {
1034 rprintf(FINFO, "[%s] parse_filter_file(%s,%x,%x)%s\n",
1035 who_am_i(), fname, mflags, xflags,
1036 fp ? "" : " [not found]");
1037 }
1038
1039 if (!fp) {
1040 if (xflags & XFLG_FATAL_ERRORS) {
1041 rsyserr(FERROR, errno,
1042 "failed to open %sclude file %s",
1043 mflags & MATCHFLG_INCLUDE ? "in" : "ex",
1044 fname);
1045 exit_cleanup(RERR_FILEIO);
1046 }
1047 return;
1048 }
1049 dirbuf[dirbuf_len] = '\0';
1050
1051 while (1) {
1052 char *s = line;
1053 int ch, overflow = 0;
1054 while (1) {
1055 if ((ch = getc(fp)) == EOF) {
1056 if (ferror(fp) && errno == EINTR) {
1057 clearerr(fp);
1058 continue;
1059 }
1060 break;
1061 }
1062 if (word_split && isspace(ch))
1063 break;
1064 if (eol_nulls? !ch : (ch == '\n' || ch == '\r'))
1065 break;
1066 if (s < eob)
1067 *s++ = ch;
1068 else
1069 overflow = 1;
1070 }
1071 if (overflow) {
1072 rprintf(FERROR, "discarding over-long filter: %s...\n", line);
1073 s = line;
1074 }
1075 *s = '\0';
1076 /* Skip an empty token and (when line parsing) comments. */
1077 if (*line && (word_split || (*line != ';' && *line != '#')))
1078 parse_rule(listp, line, mflags, xflags);
1079 if (ch == EOF)
1080 break;
1081 }
1082 fclose(fp);
1083}
1084
1085/* If the "for_xfer" flag is set, the prefix is made compatible with the
1086 * current protocol_version (if possible) or a NULL is returned (if not
1087 * possible). */
1088char *get_rule_prefix(int match_flags, const char *pat, int for_xfer,
1089 unsigned int *plen_ptr)
1090{
1091 static char buf[MAX_RULE_PREFIX+1];
1092 char *op = buf;
1093 int legal_len = for_xfer && protocol_version < 29 ? 1 : MAX_RULE_PREFIX-1;
1094
1095 if (match_flags & MATCHFLG_PERDIR_MERGE) {
1096 if (legal_len == 1)
1097 return NULL;
1098 *op++ = ':';
1099 } else if (match_flags & MATCHFLG_INCLUDE)
1100 *op++ = '+';
1101 else if (legal_len != 1
1102 || ((*pat == '-' || *pat == '+') && pat[1] == ' '))
1103 *op++ = '-';
1104 else
1105 legal_len = 0;
1106
1107 if (match_flags & MATCHFLG_NEGATE)
1108 *op++ = '!';
1109 if (match_flags & MATCHFLG_CVS_IGNORE)
1110 *op++ = 'C';
1111 else {
1112 if (match_flags & MATCHFLG_NO_INHERIT)
1113 *op++ = 'n';
1114 if (match_flags & MATCHFLG_WORD_SPLIT)
1115 *op++ = 'w';
1116 if (match_flags & MATCHFLG_NO_PREFIXES) {
1117 if (match_flags & MATCHFLG_INCLUDE)
1118 *op++ = '+';
1119 else
1120 *op++ = '-';
1121 }
1122 }
1123 if (match_flags & MATCHFLG_EXCLUDE_SELF)
1124 *op++ = 'e';
1125 if (match_flags & MATCHFLG_SENDER_SIDE
1126 && (!for_xfer || protocol_version >= 29))
1127 *op++ = 's';
1128 if (match_flags & MATCHFLG_RECEIVER_SIDE
1129 && (!for_xfer || protocol_version >= 29
1130 || (delete_excluded && am_sender)))
1131 *op++ = 'r';
1132 if (match_flags & MATCHFLG_PERISHABLE) {
1133 if (!for_xfer || protocol_version >= 30)
1134 *op++ = 'p';
1135 else if (am_sender)
1136 return NULL;
1137 }
1138 if (op - buf > legal_len)
1139 return NULL;
1140 if (legal_len)
1141 *op++ = ' ';
1142 *op = '\0';
1143 if (plen_ptr)
1144 *plen_ptr = op - buf;
1145 return buf;
1146}
1147
1148static void send_rules(int f_out, struct filter_list_struct *flp)
1149{
1150 struct filter_struct *ent, *prev = NULL;
1151
1152 for (ent = flp->head; ent; ent = ent->next) {
1153 unsigned int len, plen, dlen;
1154 int elide = 0;
1155 char *p;
1156
1157 /* Note we need to check delete_excluded here in addition to
1158 * the code in parse_rule_tok() because some rules may have
1159 * been added before we found the --delete-excluded option.
1160 * We must also elide any CVS merge-file rules to avoid a
1161 * backward compatibility problem, and we elide any no-prefix
1162 * merge files as an optimization (since they can only have
1163 * include/exclude rules). */
1164 if (ent->match_flags & MATCHFLG_SENDER_SIDE)
1165 elide = am_sender ? 1 : -1;
1166 if (ent->match_flags & MATCHFLG_RECEIVER_SIDE)
1167 elide = elide ? 0 : am_sender ? -1 : 1;
1168 else if (delete_excluded && !elide
1169 && (!(ent->match_flags & MATCHFLG_PERDIR_MERGE)
1170 || ent->match_flags & MATCHFLG_NO_PREFIXES))
1171 elide = am_sender ? 1 : -1;
1172 if (elide < 0) {
1173 if (prev)
1174 prev->next = ent->next;
1175 else
1176 flp->head = ent->next;
1177 } else
1178 prev = ent;
1179 if (elide > 0)
1180 continue;
1181 if (ent->match_flags & MATCHFLG_CVS_IGNORE
1182 && !(ent->match_flags & MATCHFLG_MERGE_FILE)) {
1183 int f = am_sender || protocol_version < 29 ? f_out : -2;
1184 send_rules(f, &cvs_filter_list);
1185 if (f == f_out)
1186 continue;
1187 }
1188 p = get_rule_prefix(ent->match_flags, ent->pattern, 1, &plen);
1189 if (!p) {
1190 rprintf(FERROR,
1191 "filter rules are too modern for remote rsync.\n");
1192 exit_cleanup(RERR_PROTOCOL);
1193 }
1194 if (f_out < 0)
1195 continue;
1196 len = strlen(ent->pattern);
1197 dlen = ent->match_flags & MATCHFLG_DIRECTORY ? 1 : 0;
1198 if (!(plen + len + dlen))
1199 continue;
1200 write_int(f_out, plen + len + dlen);
1201 if (plen)
1202 write_buf(f_out, p, plen);
1203 write_buf(f_out, ent->pattern, len);
1204 if (dlen)
1205 write_byte(f_out, '/');
1206 }
1207 flp->tail = prev;
1208}
1209
1210/* This is only called by the client. */
1211void send_filter_list(int f_out)
1212{
1213 int receiver_wants_list = prune_empty_dirs
1214 || (delete_mode && (!delete_excluded || protocol_version >= 29));
1215
1216 if (local_server || (am_sender && !receiver_wants_list))
1217 f_out = -1;
1218 if (cvs_exclude && am_sender) {
1219 if (protocol_version >= 29)
1220 parse_rule(&filter_list, ":C", 0, 0);
1221 parse_rule(&filter_list, "-C", 0, 0);
1222 }
1223
1224 send_rules(f_out, &filter_list);
1225
1226 if (f_out >= 0)
1227 write_int(f_out, 0);
1228
1229 if (cvs_exclude) {
1230 if (!am_sender || protocol_version < 29)
1231 parse_rule(&filter_list, ":C", 0, 0);
1232 if (!am_sender)
1233 parse_rule(&filter_list, "-C", 0, 0);
1234 }
1235}
1236
1237/* This is only called by the server. */
1238void recv_filter_list(int f_in)
1239{
1240 char line[BIGPATHBUFLEN];
1241 int xflags = protocol_version >= 29 ? 0 : XFLG_OLD_PREFIXES;
1242 int receiver_wants_list = prune_empty_dirs
1243 || (delete_mode
1244 && (!delete_excluded || protocol_version >= 29));
1245 unsigned int len;
1246
1247 if (!local_server && (am_sender || receiver_wants_list)) {
1248 while ((len = read_int(f_in)) != 0) {
1249 if (len >= sizeof line)
1250 overflow_exit("recv_rules");
1251 read_sbuf(f_in, line, len);
1252 parse_rule(&filter_list, line, 0, xflags);
1253 }
1254 }
1255
1256 if (cvs_exclude) {
1257 if (local_server || am_sender || protocol_version < 29)
1258 parse_rule(&filter_list, ":C", 0, 0);
1259 if (local_server || am_sender)
1260 parse_rule(&filter_list, "-C", 0, 0);
1261 }
1262
1263 if (local_server) /* filter out any rules that aren't for us. */
1264 send_rules(-1, &filter_list);
1265}