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