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