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