Call add_filter() and add_filter_file() with their new flag args.
[rsync/rsync.git] / exclude.c
CommitLineData
f0f5767f 1/* -*- c-file-style: "linux" -*-
0f2ac855 2 *
07a874fd
MP
3 * Copyright (C) 1996-2001 by Andrew Tridgell <tridge@samba.org>
4 * Copyright (C) 1996 by Paul Mackerras
5 * Copyright (C) 2002 by Martin Pool
0f2ac855 6 *
07a874fd
MP
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
0f2ac855 11 *
07a874fd
MP
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
0f2ac855 16 *
07a874fd
MP
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
c627d613 21
2b6b4d53
AT
22/* a lot of this stuff was originally derived from GNU tar, although
23 it has now changed so much that it is hard to tell :) */
c627d613 24
d567322f
MP
25/* include/exclude cluestick added by Martin Pool <mbp@samba.org> */
26
c627d613
AT
27#include "rsync.h"
28
29extern int verbose;
5a016db9 30extern int am_server;
40d38dc0
WD
31extern int eol_nulls;
32extern int list_only;
33extern int recurse;
6dfd07d0
WD
34extern int io_error;
35extern int sanitize_paths;
36extern int protocol_version;
bf39270e 37extern int module_id;
40d38dc0
WD
38
39extern char curr_dir[];
6dfd07d0
WD
40extern unsigned int curr_dir_len;
41extern unsigned int module_dirlen;
c627d613 42
7842418b
WD
43struct filter_list_struct filter_list = { 0, 0, "" };
44struct filter_list_struct server_filter_list = { 0, 0, "server " };
c627d613 45
6dfd07d0 46/* Need room enough for ":MODS " prefix plus some room to grow. */
7842418b 47#define MAX_RULE_PREFIX (16)
6dfd07d0 48
7842418b 49/* The dirbuf is set by push_local_filters() to the current subdirectory
6dfd07d0
WD
50 * relative to curr_dir that is being processed. The path always has a
51 * trailing slash appended, and the variable dirbuf_len contains the length
52 * of this path prefix. The path is always absolute. */
53static char dirbuf[MAXPATHLEN+1];
54static unsigned int dirbuf_len = 0;
55static int dirbuf_depth;
56
57/* This is True when we're scanning parent dirs for per-dir merge-files. */
58static BOOL parent_dirscan = False;
59
60/* This array contains a list of all the currently active per-dir merge
61 * files. This makes it easier to save the appropriate values when we
62 * "push" down into each subdirectory. */
7842418b 63static struct filter_struct **mergelist_parents;
6dfd07d0
WD
64static int mergelist_cnt = 0;
65static int mergelist_size = 0;
66
7842418b 67/* Each filter_list_struct describes a singly-linked list by keeping track
6dfd07d0
WD
68 * of both the head and tail pointers. The list is slightly unusual in that
69 * a parent-dir's content can be appended to the end of the local list in a
70 * special way: the last item in the local list has its "next" pointer set
71 * to point to the inherited list, but the local list's tail pointer points
72 * at the end of the local list. Thus, if the local list is empty, the head
73 * will be pointing at the inherited content but the tail will be NULL. To
74 * help you visualize this, here are the possible list arrangements:
75 *
76 * Completely Empty Local Content Only
77 * ================================== ====================================
78 * head -> NULL head -> Local1 -> Local2 -> NULL
79 * tail -> NULL tail -------------^
80 *
81 * Inherited Content Only Both Local and Inherited Content
82 * ================================== ====================================
83 * head -> Parent1 -> Parent2 -> NULL head -> L1 -> L2 -> P1 -> P2 -> NULL
84 * tail -> NULL tail ---------^
85 *
86 * This means that anyone wanting to traverse the whole list to use it just
87 * needs to start at the head and use the "next" pointers until it goes
88 * NULL. To add new local content, we insert the item after the tail item
89 * and update the tail (obviously, if "tail" was NULL, we insert it at the
90 * head). To clear the local list, WE MUST NOT FREE THE INHERITED CONTENT
91 * because it is shared between the current list and our parent list(s).
92 * The easiest way to handle this is to simply truncate the list after the
93 * tail item and then free the local list from the head. When inheriting
7842418b 94 * the list for a new local dir, we just save off the filter_list_struct
6dfd07d0
WD
95 * values (so we can pop back to them later) and set the tail to NULL.
96 */
97
7842418b 98static void free_filter(struct filter_struct *ex)
6dfd07d0
WD
99{
100 if (ex->match_flags & MATCHFLG_PERDIR_MERGE) {
101 free(ex->u.mergelist->debug_type);
102 free(ex->u.mergelist);
103 mergelist_cnt--;
104 }
105 free(ex->pattern);
106 free(ex);
107}
108
7842418b 109/* Build a filter structure given a filter pattern. The value in "pat"
6dfd07d0 110 * is not null-terminated. */
bf39270e
WD
111static void filter_rule(struct filter_list_struct *listp, const char *pat,
112 unsigned int pat_len, unsigned int mflags, int xflags)
c627d613 113{
7842418b 114 struct filter_struct *ret;
f8f72644 115 const char *cp;
5e972dcf 116 unsigned int ex_len;
c627d613 117
6dfd07d0 118 if (verbose > 2) {
bf39270e 119 rprintf(FINFO, "[%s] filter_rule(%.*s, %s%s)\n",
6dfd07d0
WD
120 who_am_i(), (int)pat_len, pat,
121 mflags & MATCHFLG_PERDIR_MERGE ? "per-dir-merge"
122 : mflags & MATCHFLG_INCLUDE ? "include" : "exclude",
123 listp->debug_type);
124 }
125
7842418b 126 ret = new(struct filter_struct);
f8f72644 127 if (!ret)
bf39270e 128 out_of_memory("filter_rule");
5f5be796 129 memset(ret, 0, sizeof ret[0]);
2b6b4d53 130
bf39270e
WD
131 if (xflags & XFLG_ANCHORED2ABS && *pat == '/'
132 && !(mflags & (MATCHFLG_ABS_PATH | MATCHFLG_MERGE_FILE))) {
133 mflags |= MATCHFLG_ABS_PATH;
134 ex_len = dirbuf_len - module_dirlen - 1;
6dfd07d0 135 } else
f8f72644
WD
136 ex_len = 0;
137 ret->pattern = new_array(char, ex_len + pat_len + 1);
138 if (!ret->pattern)
bf39270e 139 out_of_memory("filter_rule");
f8f72644 140 if (ex_len)
6dfd07d0 141 memcpy(ret->pattern, dirbuf + module_dirlen, ex_len);
5e972dcf 142 strlcpy(ret->pattern + ex_len, pat, pat_len + 1);
f8f72644
WD
143 pat_len += ex_len;
144
145 if (strpbrk(ret->pattern, "*[?")) {
5e972dcf 146 mflags |= MATCHFLG_WILD;
96d3590a 147 if ((cp = strstr(ret->pattern, "**")) != NULL) {
5e972dcf 148 mflags |= MATCHFLG_WILD2;
170381c0 149 /* If the pattern starts with **, note that. */
96d3590a 150 if (cp == ret->pattern)
5e972dcf 151 mflags |= MATCHFLG_WILD2_PREFIX;
0f2ac855 152 }
2bca43f6 153 }
c627d613 154
5be7fa93
WD
155 if (pat_len > 1 && ret->pattern[pat_len-1] == '/') {
156 ret->pattern[pat_len-1] = 0;
5e972dcf 157 mflags |= MATCHFLG_DIRECTORY;
2b6b4d53 158 }
c627d613 159
6dfd07d0 160 if (mflags & MATCHFLG_PERDIR_MERGE) {
7842418b 161 struct filter_list_struct *lp;
6dfd07d0
WD
162 unsigned int len;
163 int i;
164
165 if ((cp = strrchr(ret->pattern, '/')) != NULL)
166 cp++;
167 else
168 cp = ret->pattern;
169
170 /* If the local merge file was already mentioned, don't
171 * add it again. */
172 for (i = 0; i < mergelist_cnt; i++) {
7842418b 173 struct filter_struct *ex = mergelist_parents[i];
6dfd07d0
WD
174 const char *s = strrchr(ex->pattern, '/');
175 if (s)
bf39270e 176 s++;
6dfd07d0 177 else
bf39270e 178 s = ex->pattern;
6dfd07d0
WD
179 len = strlen(s);
180 if (len == pat_len - (cp - ret->pattern)
181 && memcmp(s, cp, len) == 0) {
7842418b 182 free_filter(ret);
6dfd07d0
WD
183 return;
184 }
185 }
186
7842418b 187 if (!(lp = new_array(struct filter_list_struct, 1)))
bf39270e 188 out_of_memory("filter_rule");
6dfd07d0
WD
189 lp->head = lp->tail = NULL;
190 if (asprintf(&lp->debug_type, " (per-dir %s)", cp) < 0)
bf39270e 191 out_of_memory("filter_rule");
6dfd07d0
WD
192 ret->u.mergelist = lp;
193
194 if (mergelist_cnt == mergelist_size) {
195 mergelist_size += 5;
196 mergelist_parents = realloc_array(mergelist_parents,
7842418b 197 struct filter_struct *,
6dfd07d0
WD
198 mergelist_size);
199 if (!mergelist_parents)
bf39270e 200 out_of_memory("filter_rule");
6dfd07d0
WD
201 }
202 mergelist_parents[mergelist_cnt++] = ret;
203 } else {
204 for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
205 ret->u.slash_cnt++;
206 }
0944563e 207
c1b29492
WD
208 ret->match_flags = mflags;
209
6dfd07d0
WD
210 if (!listp->tail) {
211 ret->next = listp->head;
b2aa573b 212 listp->head = listp->tail = ret;
6dfd07d0
WD
213 } else {
214 ret->next = listp->tail->next;
b2aa573b
WD
215 listp->tail->next = ret;
216 listp->tail = ret;
217 }
2b6b4d53
AT
218}
219
7842418b 220static void clear_filter_list(struct filter_list_struct *listp)
2b6b4d53 221{
6dfd07d0 222 if (listp->tail) {
7842418b 223 struct filter_struct *ent, *next;
6dfd07d0
WD
224 /* Truncate any inherited items from the local list. */
225 listp->tail->next = NULL;
226 /* Now free everything that is left. */
227 for (ent = listp->head; ent; ent = next) {
228 next = ent->next;
7842418b 229 free_filter(ent);
6dfd07d0
WD
230 }
231 }
232
233 listp->head = listp->tail = NULL;
2b6b4d53 234}
c627d613 235
6dfd07d0
WD
236/* This returns an expanded (absolute) filename for the merge-file name if
237 * the name has any slashes in it OR if the parent_dirscan var is True;
238 * otherwise it returns the original merge_file name. If the len_ptr value
239 * is non-NULL the merge_file name is limited by the referenced length
240 * value and will be updated with the length of the resulting name. We
241 * always return a name that is null terminated, even if the merge_file
242 * name was not. */
243static char *parse_merge_name(const char *merge_file, unsigned int *len_ptr,
244 unsigned int prefix_skip)
5be7fa93 245{
6dfd07d0
WD
246 static char buf[MAXPATHLEN];
247 char *fn, tmpbuf[MAXPATHLEN];
248 unsigned int fn_len;
249
250 if (!parent_dirscan && *merge_file != '/') {
251 /* Return the name unchanged it doesn't have any slashes. */
252 if (len_ptr) {
253 const char *p = merge_file + *len_ptr;
254 while (--p > merge_file && *p != '/') {}
255 if (p == merge_file) {
256 strlcpy(buf, merge_file, *len_ptr + 1);
257 return buf;
258 }
259 } else if (strchr(merge_file, '/') == NULL)
260 return (char *)merge_file;
261 }
5be7fa93 262
6dfd07d0
WD
263 fn = *merge_file == '/' ? buf : tmpbuf;
264 if (sanitize_paths) {
265 const char *r = prefix_skip ? "/" : NULL;
266 /* null-terminate the name if it isn't already */
267 if (len_ptr && merge_file[*len_ptr]) {
268 char *to = fn == buf ? tmpbuf : buf;
269 strlcpy(to, merge_file, *len_ptr + 1);
270 merge_file = to;
271 }
272 if (!sanitize_path(fn, merge_file, r, dirbuf_depth)) {
273 rprintf(FERROR, "merge-file name overflows: %s\n",
274 merge_file);
275 return NULL;
276 }
277 } else {
278 strlcpy(fn, merge_file, len_ptr ? *len_ptr + 1 : MAXPATHLEN);
279 clean_fname(fn, 1);
b2aa573b 280 }
6dfd07d0
WD
281
282 fn_len = strlen(fn);
283 if (fn == buf)
284 goto done;
285
286 if (dirbuf_len + fn_len >= MAXPATHLEN) {
287 rprintf(FERROR, "merge-file name overflows: %s\n", fn);
288 return NULL;
289 }
290 memcpy(buf, dirbuf + prefix_skip, dirbuf_len - prefix_skip);
291 memcpy(buf + dirbuf_len - prefix_skip, fn, fn_len + 1);
292 fn_len = clean_fname(buf, 1);
293
294 done:
295 if (len_ptr)
296 *len_ptr = fn_len;
297 return buf;
298}
5be7fa93 299
6dfd07d0 300/* Sets the dirbuf and dirbuf_len values. */
7842418b 301void set_filter_dir(const char *dir, unsigned int dirlen)
6dfd07d0
WD
302{
303 unsigned int len;
304 if (*dir != '/') {
305 memcpy(dirbuf, curr_dir, curr_dir_len);
306 dirbuf[curr_dir_len] = '/';
307 len = curr_dir_len + 1;
308 if (len + dirlen >= MAXPATHLEN)
309 dirlen = 0;
310 } else
311 len = 0;
312 memcpy(dirbuf + len, dir, dirlen);
313 dirbuf[dirlen + len] = '\0';
314 dirbuf_len = clean_fname(dirbuf, 1);
315 if (dirbuf_len > 1 && dirbuf[dirbuf_len-1] == '.'
316 && dirbuf[dirbuf_len-2] == '/')
317 dirbuf_len -= 2;
318 if (dirbuf_len != 1)
319 dirbuf[dirbuf_len++] = '/';
320 dirbuf[dirbuf_len] = '\0';
321 if (sanitize_paths)
322 dirbuf_depth = count_dir_elements(dirbuf + module_dirlen);
323}
324
325/* This routine takes a per-dir merge-file entry and finishes its setup.
326 * If the name has a path portion then we check to see if it refers to a
327 * parent directory of the first transfer dir. If it does, we scan all the
328 * dirs from that point through the parent dir of the transfer dir looking
329 * for the per-dir merge-file in each one. */
7842418b
WD
330static BOOL setup_merge_file(struct filter_struct *ex,
331 struct filter_list_struct *lp, int flags)
6dfd07d0
WD
332{
333 char buf[MAXPATHLEN];
334 char *x, *y, *pat = ex->pattern;
335 unsigned int len;
336
337 if (!(x = parse_merge_name(pat, NULL, 0)) || *x != '/')
338 return 0;
339
340 y = strrchr(x, '/');
341 *y = '\0';
342 ex->pattern = strdup(y+1);
343 if (!*x)
344 x = "/";
345 if (*x == '/')
346 strlcpy(buf, x, MAXPATHLEN);
347 else
348 pathjoin(buf, MAXPATHLEN, dirbuf, x);
349
350 len = clean_fname(buf, 1);
351 if (len != 1 && len < MAXPATHLEN-1) {
352 buf[len++] = '/';
353 buf[len] = '\0';
354 }
355 /* This ensures that the specified dir is a parent of the transfer. */
356 for (x = buf, y = dirbuf; *x && *x == *y; x++, y++) {}
357 if (*x)
358 y += strlen(y); /* nope -- skip the scan */
359
360 parent_dirscan = True;
361 while (*y) {
362 char save[MAXPATHLEN];
363 strlcpy(save, y, MAXPATHLEN);
364 *y = '\0';
365 dirbuf_len = y - dirbuf;
366 strlcpy(x, ex->pattern, MAXPATHLEN - (x - buf));
bf39270e 367 add_filter_file(lp, buf, flags | XFLG_ANCHORED2ABS);
6dfd07d0
WD
368 if (ex->match_flags & MATCHFLG_NO_INHERIT)
369 lp->head = NULL;
370 lp->tail = NULL;
371 strlcpy(y, save, MAXPATHLEN);
372 while ((*x++ = *y++) != '/') {}
373 }
374 parent_dirscan = False;
375 free(pat);
376 return 1;
377}
378
379/* Each time rsync changes to a new directory it call this function to
380 * handle all the per-dir merge-files. The "dir" value is the current path
381 * relative to curr_dir (which might not be null-terminated). We copy it
382 * into dirbuf so that we can easily append a file name on the end. */
7842418b 383void *push_local_filters(const char *dir, unsigned int dirlen)
6dfd07d0 384{
7842418b 385 struct filter_list_struct *ap, *push;
6dfd07d0
WD
386 int i;
387
7842418b 388 set_filter_dir(dir, dirlen);
6dfd07d0 389
a2b371cd
WD
390 if (!mergelist_cnt)
391 return NULL;
392
7842418b 393 push = new_array(struct filter_list_struct, mergelist_cnt);
6dfd07d0 394 if (!push)
7842418b 395 out_of_memory("push_local_filters");
6dfd07d0
WD
396
397 for (i = 0, ap = push; i < mergelist_cnt; i++) {
398 memcpy(ap++, mergelist_parents[i]->u.mergelist,
7842418b 399 sizeof (struct filter_list_struct));
6dfd07d0
WD
400 }
401
7842418b 402 /* Note: add_filter_file() might increase mergelist_cnt, so keep
6dfd07d0
WD
403 * this loop separate from the above loop. */
404 for (i = 0; i < mergelist_cnt; i++) {
7842418b
WD
405 struct filter_struct *ex = mergelist_parents[i];
406 struct filter_list_struct *lp = ex->u.mergelist;
6dfd07d0
WD
407 int flags = 0;
408
409 if (verbose > 2) {
7842418b 410 rprintf(FINFO, "[%s] pushing filter list%s\n",
6dfd07d0
WD
411 who_am_i(), lp->debug_type);
412 }
413
414 lp->tail = NULL; /* Switch any local rules to inherited. */
415 if (ex->match_flags & MATCHFLG_NO_INHERIT)
416 lp->head = NULL;
417 if (ex->match_flags & MATCHFLG_WORD_SPLIT)
418 flags |= XFLG_WORD_SPLIT;
419 if (ex->match_flags & MATCHFLG_NO_PREFIXES)
420 flags |= XFLG_NO_PREFIXES;
421 if (ex->match_flags & MATCHFLG_INCLUDE)
422 flags |= XFLG_DEF_INCLUDE;
423 else if (ex->match_flags & MATCHFLG_NO_PREFIXES)
424 flags |= XFLG_DEF_EXCLUDE;
425
426 if (ex->match_flags & MATCHFLG_FINISH_SETUP) {
427 ex->match_flags &= ~MATCHFLG_FINISH_SETUP;
428 if (setup_merge_file(ex, lp, flags))
7842418b 429 set_filter_dir(dir, dirlen);
6dfd07d0
WD
430 }
431
432 if (strlcpy(dirbuf + dirbuf_len, ex->pattern,
433 MAXPATHLEN - dirbuf_len) < MAXPATHLEN - dirbuf_len)
bf39270e 434 add_filter_file(lp, dirbuf, flags | XFLG_ANCHORED2ABS);
6dfd07d0
WD
435 else {
436 io_error |= IOERR_GENERAL;
437 rprintf(FINFO,
7842418b 438 "cannot add local filter rules in long-named directory: %s\n",
6dfd07d0
WD
439 full_fname(dirbuf));
440 }
441 dirbuf[dirbuf_len] = '\0';
442 }
443
444 return (void*)push;
445}
446
7842418b 447void pop_local_filters(void *mem)
6dfd07d0 448{
7842418b 449 struct filter_list_struct *ap, *pop = (struct filter_list_struct*)mem;
6dfd07d0
WD
450 int i;
451
452 for (i = mergelist_cnt; i-- > 0; ) {
7842418b
WD
453 struct filter_struct *ex = mergelist_parents[i];
454 struct filter_list_struct *lp = ex->u.mergelist;
6dfd07d0
WD
455
456 if (verbose > 2) {
7842418b 457 rprintf(FINFO, "[%s] popping filter list%s\n",
6dfd07d0
WD
458 who_am_i(), lp->debug_type);
459 }
460
7842418b 461 clear_filter_list(lp);
6dfd07d0
WD
462 }
463
a2b371cd
WD
464 if (!pop)
465 return;
466
6dfd07d0
WD
467 for (i = 0, ap = pop; i < mergelist_cnt; i++) {
468 memcpy(mergelist_parents[i]->u.mergelist, ap++,
7842418b 469 sizeof (struct filter_list_struct));
6dfd07d0
WD
470 }
471
472 free(pop);
5be7fa93
WD
473}
474
7842418b 475static int rule_matches(char *name, struct filter_struct *ex, int name_is_dir)
2b6b4d53 476{
9f186578 477 char *p, full_name[MAXPATHLEN];
0f2ac855 478 int match_start = 0;
f2ae9e85 479 int ret_match = ex->match_flags & MATCHFLG_NEGATE ? 0 : 1;
2b6b4d53
AT
480 char *pattern = ex->pattern;
481
9f186578
WD
482 if (!*name)
483 return 0;
484
170381c0
WD
485 /* If the pattern does not have any slashes AND it does not have
486 * a "**" (which could match a slash), then we just match the
487 * name portion of the path. */
6dfd07d0 488 if (!ex->u.slash_cnt && !(ex->match_flags & MATCHFLG_WILD2)) {
5be7fa93
WD
489 if ((p = strrchr(name,'/')) != NULL)
490 name = p+1;
491 }
9f186578 492 else if (ex->match_flags & MATCHFLG_ABS_PATH && *name != '/'
6dfd07d0
WD
493 && curr_dir_len > module_dirlen + 1) {
494 pathjoin(full_name, sizeof full_name,
495 curr_dir + module_dirlen + 1, name);
5be7fa93
WD
496 name = full_name;
497 }
2b6b4d53 498
5e972dcf 499 if (ex->match_flags & MATCHFLG_DIRECTORY && !name_is_dir)
f2ae9e85 500 return !ret_match;
2b6b4d53 501
170381c0 502 if (*pattern == '/') {
2b6b4d53
AT
503 match_start = 1;
504 pattern++;
170381c0
WD
505 if (*name == '/')
506 name++;
2b6b4d53
AT
507 }
508
170381c0 509 if (ex->match_flags & MATCHFLG_WILD) {
170381c0
WD
510 /* A non-anchored match with an infix slash and no "**"
511 * needs to match the last slash_cnt+1 name elements. */
6dfd07d0 512 if (!match_start && ex->u.slash_cnt
5e972dcf 513 && !(ex->match_flags & MATCHFLG_WILD2)) {
6dfd07d0 514 int cnt = ex->u.slash_cnt + 1;
170381c0
WD
515 for (p = name + strlen(name) - 1; p >= name; p--) {
516 if (*p == '/' && !--cnt)
517 break;
518 }
519 name = p+1;
520 }
fe332038 521 if (wildmatch(pattern, name))
f2ae9e85 522 return ret_match;
170381c0
WD
523 if (ex->match_flags & MATCHFLG_WILD2_PREFIX) {
524 /* If the **-prefixed pattern has a '/' as the next
525 * character, then try to match the rest of the
526 * pattern at the root. */
fe332038 527 if (pattern[2] == '/' && wildmatch(pattern+3, name))
f2ae9e85 528 return ret_match;
c36cd317 529 }
170381c0
WD
530 else if (!match_start && ex->match_flags & MATCHFLG_WILD2) {
531 /* A non-anchored match with an infix or trailing "**"
532 * (but not a prefixed "**") needs to try matching
533 * after every slash. */
534 while ((name = strchr(name, '/')) != NULL) {
535 name++;
fe332038 536 if (wildmatch(pattern, name))
f2ae9e85 537 return ret_match;
170381c0
WD
538 }
539 }
540 } else if (match_start) {
541 if (strcmp(name,pattern) == 0)
f2ae9e85 542 return ret_match;
2b6b4d53
AT
543 } else {
544 int l1 = strlen(name);
ea2111d1 545 int l2 = strlen(pattern);
0f2ac855 546 if (l2 <= l1 &&
ea2111d1 547 strcmp(name+(l1-l2),pattern) == 0 &&
170381c0 548 (l1==l2 || name[l1-(l2+1)] == '/')) {
f2ae9e85 549 return ret_match;
c36cd317 550 }
2b6b4d53
AT
551 }
552
f2ae9e85 553 return !ret_match;
c627d613
AT
554}
555
556
7842418b
WD
557static void report_filter_result(char const *name,
558 struct filter_struct const *ent,
559 int name_is_dir, const char *type)
d567322f 560{
0f2ac855 561 /* If a trailing slash is present to match only directories,
bf39270e 562 * then it is stripped out by filter_rule. So as a special
0f2ac855
WD
563 * case we add it back in here. */
564
ea847c62 565 if (verbose >= 2) {
6dfd07d0 566 rprintf(FINFO, "[%s] %scluding %s %s because of pattern %s%s%s\n",
5e972dcf
WD
567 who_am_i(),
568 ent->match_flags & MATCHFLG_INCLUDE ? "in" : "ex",
6dfd07d0
WD
569 name_is_dir ? "directory" : "file", name, ent->pattern,
570 ent->match_flags & MATCHFLG_DIRECTORY ? "/" : "", type);
ea847c62 571 }
d567322f
MP
572}
573
574
575/*
a6536635
WD
576 * Return -1 if file "name" is defined to be excluded by the specified
577 * exclude list, 1 if it is included, and 0 if it was not matched.
d567322f 578 */
7842418b 579int check_filter(struct filter_list_struct *listp, char *name, int name_is_dir)
c627d613 580{
7842418b 581 struct filter_struct *ent;
c627d613 582
b2aa573b 583 for (ent = listp->head; ent; ent = ent->next) {
6dfd07d0 584 if (ent->match_flags & MATCHFLG_PERDIR_MERGE) {
7842418b
WD
585 int rc = check_filter(ent->u.mergelist, name,
586 name_is_dir);
6dfd07d0
WD
587 if (rc)
588 return rc;
589 continue;
590 }
7842418b
WD
591 if (rule_matches(name, ent, name_is_dir)) {
592 report_filter_result(name, ent, name_is_dir,
67340e95 593 listp->debug_type);
5e972dcf 594 return ent->match_flags & MATCHFLG_INCLUDE ? 1 : -1;
0f2ac855 595 }
2b6b4d53 596 }
c627d613 597
2b6b4d53 598 return 0;
c627d613
AT
599}
600
601
f8f72644
WD
602/* Get the next include/exclude arg from the string. The token will not
603 * be '\0' terminated, so use the returned length to limit the string.
604 * Also, be sure to add this length to the returned pointer before passing
e425fbe8 605 * it back to ask for the next token. This routine parses the "!" (list-
6dfd07d0 606 * clearing) token and (if xflags does NOT contain XFLG_NO_PREFIXES) the
e425fbe8 607 * +/- prefixes for overriding the include/exclude mode. The *flag_ptr
65e24870 608 * value will also be set to the MATCHFLG_* bits for the current token.
f8f72644 609 */
7842418b
WD
610static const char *get_filter_tok(const char *p, int xflags,
611 unsigned int *len_ptr, unsigned int *flag_ptr)
f8f72644 612{
abca4eba 613 const unsigned char *s = (const unsigned char *)p;
5e972dcf 614 unsigned int len, mflags = 0;
6dfd07d0 615 int empty_pat_is_OK = 0;
f8f72644 616
96d3590a
WD
617 if (xflags & XFLG_WORD_SPLIT) {
618 /* Skip over any initial whitespace. */
619 while (isspace(*s))
f8f72644 620 s++;
6dfd07d0 621 /* Update to point to real start of rule. */
abca4eba 622 p = (const char *)s;
f8f72644 623 }
6dfd07d0
WD
624 if (!*s)
625 return NULL;
626
627 /* Figure out what kind of a filter rule "s" is pointing at. */
628 if (!(xflags & (XFLG_DEF_INCLUDE | XFLG_DEF_EXCLUDE))) {
629 char *mods = "";
630 switch (*s) {
631 case ':':
632 mflags |= MATCHFLG_PERDIR_MERGE
633 | MATCHFLG_FINISH_SETUP;
634 /* FALL THROUGH */
635 case '.':
636 mflags |= MATCHFLG_MERGE_FILE;
637 mods = "-+Cens";
638 break;
639 case '+':
640 mflags |= MATCHFLG_INCLUDE;
bf39270e 641 /* FALL THROUGH */
6dfd07d0 642 case '-':
f2ae9e85 643 mods = "!/";
6dfd07d0
WD
644 break;
645 case '!':
646 mflags |= MATCHFLG_CLEAR_LIST;
647 mods = NULL;
648 break;
649 default:
650 rprintf(FERROR, "Unknown filter rule: %s\n", p);
651 exit_cleanup(RERR_SYNTAX);
652 }
bf39270e 653 while (mods && *++s && *s != ' ' && *s != '_') {
6dfd07d0
WD
654 if (strchr(mods, *s) == NULL) {
655 if (xflags & XFLG_WORD_SPLIT && isspace(*s)) {
656 s--;
657 break;
658 }
659 rprintf(FERROR,
bf39270e 660 "unknown modifier '%c' in filter rule: %s\n",
6dfd07d0
WD
661 *s, p);
662 exit_cleanup(RERR_SYNTAX);
663 }
664 switch (*s) {
665 case '-':
666 mflags |= MATCHFLG_NO_PREFIXES;
667 break;
668 case '+':
669 mflags |= MATCHFLG_NO_PREFIXES
670 | MATCHFLG_INCLUDE;
671 break;
bf39270e
WD
672 case '/':
673 mflags |= MATCHFLG_ABS_PATH;
674 break;
f2ae9e85
WD
675 case '!':
676 mflags |= MATCHFLG_NEGATE;
677 break;
6dfd07d0
WD
678 case 'C':
679 empty_pat_is_OK = 1;
680 mflags |= MATCHFLG_NO_PREFIXES
681 | MATCHFLG_WORD_SPLIT
682 | MATCHFLG_NO_INHERIT;
683 break;
684 case 'e':
685 mflags |= MATCHFLG_EXCLUDE_SELF;
686 break;
687 case 'n':
688 mflags |= MATCHFLG_NO_INHERIT;
689 break;
0b2901b7 690 case 'w':
6dfd07d0
WD
691 mflags |= MATCHFLG_WORD_SPLIT;
692 break;
693 }
694 }
695 if (*s)
696 s++;
697 } else if (!(xflags & XFLG_NO_PREFIXES)
96d3590a 698 && (*s == '-' || *s == '+') && s[1] == ' ') {
5e972dcf
WD
699 if (*s == '+')
700 mflags |= MATCHFLG_INCLUDE;
96d3590a 701 s += 2;
6dfd07d0
WD
702 } else {
703 if (xflags & XFLG_DEF_INCLUDE)
704 mflags |= MATCHFLG_INCLUDE;
705 if (*s == '!')
706 mflags |= MATCHFLG_CLEAR_LIST; /* Tentative! */
707 }
708
9a5e37fc
WD
709 if (xflags & XFLG_DIRECTORY)
710 mflags |= MATCHFLG_DIRECTORY;
96d3590a
WD
711
712 if (xflags & XFLG_WORD_SPLIT) {
713 const unsigned char *cp = s;
714 /* Token ends at whitespace or the end of the string. */
715 while (!isspace(*cp) && *cp != '\0')
716 cp++;
717 len = cp - s;
718 } else
0eeb1cf8 719 len = strlen((char*)s);
96d3590a 720
6dfd07d0
WD
721 if (mflags & MATCHFLG_CLEAR_LIST) {
722 if (!(xflags & (XFLG_DEF_INCLUDE | XFLG_DEF_EXCLUDE)) && len) {
723 rprintf(FERROR,
724 "'!' rule has trailing characters: %s\n", p);
725 exit_cleanup(RERR_SYNTAX);
726 }
727 if (len > 1)
728 mflags &= ~MATCHFLG_CLEAR_LIST;
729 } else if (!len && !empty_pat_is_OK) {
730 rprintf(FERROR, "unexpected end of filter rule: %s\n", p);
731 exit_cleanup(RERR_SYNTAX);
732 }
733
96d3590a 734 *len_ptr = len;
5e972dcf 735 *flag_ptr = mflags;
96d3590a 736 return (const char *)s;
f8f72644
WD
737}
738
739
7842418b
WD
740void add_filter(struct filter_list_struct *listp, const char *pattern,
741 int xflags)
c627d613 742{
5e972dcf 743 unsigned int pat_len, mflags;
6dfd07d0 744 const char *cp, *p;
5be7fa93 745
f8f72644 746 if (!pattern)
5e7dbaca 747 return;
f8f72644 748
b2aa573b 749 while (1) {
6dfd07d0 750 /* Remember that the returned string is NOT '\0' terminated! */
7842418b 751 cp = get_filter_tok(pattern, xflags, &pat_len, &mflags);
6dfd07d0 752 if (!cp)
b2aa573b 753 break;
6dfd07d0 754 if (pat_len >= MAXPATHLEN) {
7842418b 755 rprintf(FERROR, "discarding over-long filter: %s\n",
6dfd07d0
WD
756 cp);
757 continue;
758 }
759 pattern = cp + pat_len;
5e972dcf
WD
760
761 if (mflags & MATCHFLG_CLEAR_LIST) {
de91e757
WD
762 if (verbose > 2) {
763 rprintf(FINFO,
7842418b 764 "[%s] clearing filter list%s\n",
de91e757
WD
765 who_am_i(), listp->debug_type);
766 }
7842418b 767 clear_filter_list(listp);
5e972dcf
WD
768 continue;
769 }
b2aa573b 770
6dfd07d0
WD
771 if (!pat_len) {
772 cp = ".cvsignore";
773 pat_len = 10;
774 }
5e972dcf 775
6dfd07d0
WD
776 if (mflags & MATCHFLG_MERGE_FILE) {
777 unsigned int len = pat_len;
778 if (mflags & MATCHFLG_EXCLUDE_SELF) {
779 const char *name = strrchr(cp, '/');
780 if (name)
781 len -= ++name - cp;
782 else
783 name = cp;
bf39270e 784 filter_rule(listp, name, len, 0, 0);
6dfd07d0
WD
785 mflags &= ~MATCHFLG_EXCLUDE_SELF;
786 len = pat_len;
787 }
788 if (mflags & MATCHFLG_PERDIR_MERGE) {
789 if (parent_dirscan) {
790 if (!(p = parse_merge_name(cp, &len, module_dirlen)))
791 continue;
bf39270e 792 filter_rule(listp, p, len, mflags, 0);
6dfd07d0
WD
793 continue;
794 }
795 } else {
796 int flgs = XFLG_FATAL_ERRORS;
797 if (!(p = parse_merge_name(cp, &len, 0)))
798 continue;
799 if (mflags & MATCHFLG_INCLUDE)
800 flgs |= XFLG_DEF_INCLUDE;
801 else if (mflags & MATCHFLG_NO_PREFIXES)
802 flgs |= XFLG_DEF_EXCLUDE;
7842418b 803 add_filter_file(listp, p, flgs);
6dfd07d0
WD
804 continue;
805 }
f8f72644 806 }
6dfd07d0 807
bf39270e 808 filter_rule(listp, cp, pat_len, mflags, xflags);
8c35542d 809 }
c627d613
AT
810}
811
c627d613 812
7842418b
WD
813void add_filter_file(struct filter_list_struct *listp, const char *fname,
814 int xflags)
c627d613 815{
5e7dbaca 816 FILE *fp;
7842418b 817 char line[MAXPATHLEN+MAX_RULE_PREFIX+1]; /* +1 for trailing slash. */
7cd72c79 818 char *eob = line + sizeof line - 1;
40d38dc0 819 int word_split = xflags & XFLG_WORD_SPLIT;
ccdff3eb 820
5be7fa93
WD
821 if (!fname || !*fname)
822 return;
823
5a016db9
WD
824 if (*fname != '-' || fname[1] || am_server) {
825 if (server_filter_list.head) {
826 strlcpy(line, fname, sizeof line);
827 clean_fname(line, 1);
828 if (check_filter(&server_filter_list, line, 0) < 0)
829 fp = NULL;
830 else
831 fp = fopen(line, "rb");
832 } else
833 fp = fopen(fname, "rb");
834 } else
5e7dbaca 835 fp = stdin;
bf39270e
WD
836
837 if (verbose > 2) {
838 rprintf(FINFO, "[%s] add_filter_file(%s,%d)%s\n",
839 who_am_i(), safe_fname(fname), xflags,
840 fp ? "" : " [not found]");
841 }
842
5e7dbaca 843 if (!fp) {
f8f72644 844 if (xflags & XFLG_FATAL_ERRORS) {
a039749b 845 rsyserr(FERROR, errno,
6dfd07d0
WD
846 "failed to open %sclude file %s",
847 xflags & XFLG_DEF_INCLUDE ? "in" : "ex",
848 safe_fname(fname));
65417579 849 exit_cleanup(RERR_FILEIO);
2b6b4d53 850 }
5be7fa93 851 return;
2b6b4d53 852 }
6dfd07d0
WD
853 dirbuf[dirbuf_len] = '\0';
854
ccdff3eb 855 while (1) {
5e7dbaca 856 char *s = line;
619d21ff 857 int ch, overflow = 0;
ccdff3eb 858 while (1) {
5e7dbaca
WD
859 if ((ch = getc(fp)) == EOF) {
860 if (ferror(fp) && errno == EINTR)
ccdff3eb
WD
861 continue;
862 break;
863 }
40d38dc0
WD
864 if (word_split && isspace(ch))
865 break;
ccdff3eb
WD
866 if (eol_nulls? !ch : (ch == '\n' || ch == '\r'))
867 break;
868 if (s < eob)
869 *s++ = ch;
619d21ff
WD
870 else
871 overflow = 1;
872 }
873 if (overflow) {
7842418b 874 rprintf(FERROR, "discarding over-long filter: %s...\n", line);
619d21ff 875 s = line;
ccdff3eb
WD
876 }
877 *s = '\0';
7f0feb4d
WD
878 /* Skip an empty token and (when line parsing) comments. */
879 if (*line && (word_split || (*line != ';' && *line != '#')))
7842418b 880 add_filter(listp, line, xflags);
5e7dbaca 881 if (ch == EOF)
ccdff3eb 882 break;
2b6b4d53 883 }
5e7dbaca 884 fclose(fp);
c627d613
AT
885}
886
417b5999
WD
887char *get_rule_prefix(int match_flags, const char *pat, unsigned int *plen_ptr)
888{
889 static char buf[MAX_RULE_PREFIX+1];
890 char *op = buf;
891
892 if (match_flags & MATCHFLG_PERDIR_MERGE) {
893 *op++ = ':';
894 if (match_flags & MATCHFLG_WORD_SPLIT)
895 *op++ = 's';
896 if (match_flags & MATCHFLG_NO_INHERIT)
897 *op++ = 'n';
898 if (match_flags & MATCHFLG_EXCLUDE_SELF)
899 *op++ = 'e';
900 if (match_flags & MATCHFLG_NO_PREFIXES) {
901 if (match_flags & MATCHFLG_INCLUDE)
902 *op++ = '+';
903 else
904 *op++ = '-';
905 }
906 *op++ = ' ';
907 } else if (match_flags & MATCHFLG_INCLUDE) {
908 *op++ = '+';
909 *op++ = ' ';
910 } else if (protocol_version >= 29
911 || ((*pat == '-' || *pat == '+') && pat[1] == ' ')) {
912 *op++ = '-';
913 *op++ = ' ';
914 }
915 *op = '\0';
916 if (plen_ptr)
917 *plen_ptr = op - buf;
918 if (op - buf > MAX_RULE_PREFIX)
919 overflow("get_rule_prefix");
920 return buf;
921}
c627d613 922
7842418b 923void send_filter_list(int f)
c627d613 924{
7842418b 925 struct filter_struct *ent;
25cf8893 926
353f2724
WD
927 /* This is a complete hack - blame Rusty. FIXME!
928 * Remove this hack when older rsyncs (below 2.6.4) are gone. */
929 if (list_only == 1 && !recurse)
7842418b 930 add_filter(&filter_list, "/*/*", XFLG_DEF_EXCLUDE);
2b6b4d53 931
7842418b 932 for (ent = filter_list.head; ent; ent = ent->next) {
417b5999
WD
933 unsigned int len, plen, dlen;
934 char *p;
2fb139c1 935
417b5999
WD
936 len = strlen(ent->pattern);
937 if (len == 0 || len >= MAXPATHLEN)
5f5be796 938 continue;
417b5999
WD
939 p = get_rule_prefix(ent->match_flags, ent->pattern, &plen);
940 if (protocol_version < 29 && *p == ':') {
941 if (strcmp(p, ":sn- ") == 0
942 && strcmp(ent->pattern, ".cvsignore") == 0)
943 continue;
944 rprintf(FERROR,
945 "remote rsync is too old to understand per-directory merge files.\n");
946 exit_cleanup(RERR_SYNTAX);
5f5be796 947 }
417b5999
WD
948 dlen = ent->match_flags & MATCHFLG_DIRECTORY ? 1 : 0;
949 write_int(f, plen + len + dlen);
950 if (plen)
951 write_buf(f, p, plen);
952 write_buf(f, ent->pattern, len);
953 if (dlen)
954 write_byte(f, '/');
0f2ac855 955 }
2b6b4d53 956
a3dbb20a 957 write_int(f, 0);
c627d613
AT
958}
959
960
7842418b 961void recv_filter_list(int f)
c627d613 962{
7842418b 963 char line[MAXPATHLEN+MAX_RULE_PREFIX+1]; /* +1 for trailing slash. */
6dfd07d0 964 unsigned int xflags = protocol_version >= 29 ? 0 : XFLG_DEF_EXCLUDE;
9dd891bb
MP
965 unsigned int l;
966
5f5be796
WD
967 while ((l = read_int(f)) != 0) {
968 if (l >= sizeof line)
7842418b 969 overflow("recv_filter_list");
5f5be796 970 read_sbuf(f, line, l);
7842418b 971 add_filter(&filter_list, line, xflags);
651443a7 972 }
651443a7
DD
973}
974
0f2ac855 975
f8f72644
WD
976static char default_cvsignore[] =
977 /* These default ignored items come from the CVS manual. */
978 "RCS SCCS CVS CVS.adm RCSLOG cvslog.* tags TAGS"
979 " .make.state .nse_depinfo *~ #* .#* ,* _$* *$"
980 " *.old *.bak *.BAK *.orig *.rej .del-*"
981 " *.a *.olb *.o *.obj *.so *.exe"
982 " *.Z *.elc *.ln core"
983 /* The rest we added to suit ourself. */
984 " .svn/";
c627d613
AT
985
986void add_cvs_excludes(void)
987{
6dfd07d0
WD
988 static unsigned int cvs_flags = XFLG_WORD_SPLIT | XFLG_NO_PREFIXES
989 | XFLG_DEF_EXCLUDE;
2b6b4d53 990 char fname[MAXPATHLEN];
bf39270e
WD
991 char *p = module_id >= 0 && lp_use_chroot(module_id)
992 ? "/" : getenv("HOME");
0f2ac855 993
7842418b
WD
994 add_filter(&filter_list, ":C", 0);
995 add_filter(&filter_list, default_cvsignore, cvs_flags);
c627d613 996
bf39270e 997 if (p && pathjoin(fname, MAXPATHLEN, p, ".cvsignore") < MAXPATHLEN) {
7842418b 998 add_filter_file(&filter_list, fname, cvs_flags);
f8f72644 999 }
c627d613 1000
7842418b 1001 add_filter(&filter_list, getenv("CVSIGNORE"), cvs_flags);
c627d613 1002}