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