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