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