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