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