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