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