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