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