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