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