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