Commit | Line | Data |
---|---|---|
f0f5767f | 1 | /* -*- c-file-style: "linux" -*- |
0f2ac855 | 2 | * |
07a874fd MP |
3 | * Copyright (C) 1996-2001 by Andrew Tridgell <tridge@samba.org> |
4 | * Copyright (C) 1996 by Paul Mackerras | |
5 | * Copyright (C) 2002 by Martin Pool | |
0f2ac855 | 6 | * |
07a874fd MP |
7 | * This program is free software; you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
0f2ac855 | 11 | * |
07a874fd MP |
12 | * This program is distributed in the hope that it will be useful, |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
0f2ac855 | 16 | * |
07a874fd MP |
17 | * You should have received a copy of the GNU General Public License |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
20 | */ | |
c627d613 | 21 | |
2b6b4d53 AT |
22 | /* a lot of this stuff was originally derived from GNU tar, although |
23 | it has now changed so much that it is hard to tell :) */ | |
c627d613 | 24 | |
d567322f MP |
25 | /* include/exclude cluestick added by Martin Pool <mbp@samba.org> */ |
26 | ||
c627d613 AT |
27 | #include "rsync.h" |
28 | ||
29 | extern int verbose; | |
30 | ||
5be7fa93 WD |
31 | struct exclude_struct **exclude_list; |
32 | struct exclude_struct **local_exclude_list; | |
33 | struct exclude_struct **server_exclude_list; | |
34 | char *exclude_path_prefix = NULL; | |
c627d613 | 35 | |
07a874fd | 36 | /** Build an exclude structure given a exclude pattern */ |
f8f72644 WD |
37 | static struct exclude_struct *make_exclude(const char *pattern, int pat_len, |
38 | int include) | |
c627d613 | 39 | { |
2b6b4d53 | 40 | struct exclude_struct *ret; |
f8f72644 WD |
41 | const char *cp; |
42 | int ex_len; | |
c627d613 | 43 | |
58cadc86 | 44 | ret = new(struct exclude_struct); |
f8f72644 WD |
45 | if (!ret) |
46 | out_of_memory("make_exclude"); | |
c627d613 | 47 | |
5f5be796 | 48 | memset(ret, 0, sizeof ret[0]); |
f8f72644 | 49 | ret->include = include; |
2b6b4d53 | 50 | |
5be7fa93 WD |
51 | if (exclude_path_prefix) |
52 | ret->match_flags |= MATCHFLG_ABS_PATH; | |
f8f72644 WD |
53 | if (exclude_path_prefix && *pattern == '/') |
54 | ex_len = strlen(exclude_path_prefix); | |
55 | else | |
56 | ex_len = 0; | |
57 | ret->pattern = new_array(char, ex_len + pat_len + 1); | |
58 | if (!ret->pattern) | |
59 | out_of_memory("make_exclude"); | |
60 | if (ex_len) | |
61 | memcpy(ret->pattern, exclude_path_prefix, ex_len); | |
62 | strlcpy(ret->pattern + ex_len, pattern, pat_len + 1); | |
63 | pat_len += ex_len; | |
64 | ||
65 | if (strpbrk(ret->pattern, "*[?")) { | |
170381c0 | 66 | ret->match_flags |= MATCHFLG_WILD; |
96d3590a | 67 | if ((cp = strstr(ret->pattern, "**")) != NULL) { |
170381c0 WD |
68 | ret->match_flags |= MATCHFLG_WILD2; |
69 | /* If the pattern starts with **, note that. */ | |
96d3590a | 70 | if (cp == ret->pattern) |
170381c0 | 71 | ret->match_flags |= MATCHFLG_WILD2_PREFIX; |
0f2ac855 | 72 | } |
2bca43f6 | 73 | } |
c627d613 | 74 | |
5be7fa93 WD |
75 | if (pat_len > 1 && ret->pattern[pat_len-1] == '/') { |
76 | ret->pattern[pat_len-1] = 0; | |
2b6b4d53 AT |
77 | ret->directory = 1; |
78 | } | |
c627d613 | 79 | |
170381c0 WD |
80 | for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++) |
81 | ret->slash_cnt++; | |
0944563e | 82 | |
2b6b4d53 AT |
83 | return ret; |
84 | } | |
85 | ||
86 | static void free_exclude(struct exclude_struct *ex) | |
87 | { | |
2b6b4d53 | 88 | free(ex->pattern); |
5f5be796 | 89 | memset(ex, 0, sizeof ex[0]); |
2b6b4d53 AT |
90 | free(ex); |
91 | } | |
c627d613 | 92 | |
5be7fa93 WD |
93 | |
94 | void free_exclude_list(struct exclude_struct ***listp) | |
95 | { | |
96 | struct exclude_struct **list = *listp; | |
97 | ||
98 | if (verbose > 2) | |
ea847c62 | 99 | rprintf(FINFO, "[%s] clearing exclude list\n", who_am_i()); |
5be7fa93 WD |
100 | |
101 | if (!list) | |
102 | return; | |
103 | ||
104 | while (*list) | |
105 | free_exclude(*list++); | |
106 | ||
107 | free(*listp); | |
108 | *listp = NULL; | |
109 | } | |
110 | ||
d567322f | 111 | static int check_one_exclude(char *name, struct exclude_struct *ex, |
5be7fa93 | 112 | int name_is_dir) |
2b6b4d53 AT |
113 | { |
114 | char *p; | |
0f2ac855 | 115 | int match_start = 0; |
2b6b4d53 AT |
116 | char *pattern = ex->pattern; |
117 | ||
170381c0 WD |
118 | /* If the pattern does not have any slashes AND it does not have |
119 | * a "**" (which could match a slash), then we just match the | |
120 | * name portion of the path. */ | |
5be7fa93 WD |
121 | if (!ex->slash_cnt && !(ex->match_flags & MATCHFLG_WILD2)) { |
122 | if ((p = strrchr(name,'/')) != NULL) | |
123 | name = p+1; | |
124 | } | |
125 | else if ((ex->match_flags & MATCHFLG_ABS_PATH) && *name != '/') { | |
126 | static char full_name[MAXPATHLEN]; | |
127 | extern char curr_dir[]; | |
128 | int plus = curr_dir[1] == '\0'? 1 : 0; | |
a7725e6d | 129 | pathjoin(full_name, sizeof full_name, curr_dir+plus, name); |
5be7fa93 WD |
130 | name = full_name; |
131 | } | |
2b6b4d53 AT |
132 | |
133 | if (!name[0]) return 0; | |
134 | ||
5be7fa93 | 135 | if (ex->directory && !name_is_dir) return 0; |
2b6b4d53 | 136 | |
170381c0 | 137 | if (*pattern == '/') { |
2b6b4d53 AT |
138 | match_start = 1; |
139 | pattern++; | |
170381c0 WD |
140 | if (*name == '/') |
141 | name++; | |
2b6b4d53 AT |
142 | } |
143 | ||
170381c0 | 144 | if (ex->match_flags & MATCHFLG_WILD) { |
170381c0 WD |
145 | /* A non-anchored match with an infix slash and no "**" |
146 | * needs to match the last slash_cnt+1 name elements. */ | |
147 | if (!match_start && ex->slash_cnt && | |
148 | !(ex->match_flags & MATCHFLG_WILD2)) { | |
149 | int cnt = ex->slash_cnt + 1; | |
150 | for (p = name + strlen(name) - 1; p >= name; p--) { | |
151 | if (*p == '/' && !--cnt) | |
152 | break; | |
153 | } | |
154 | name = p+1; | |
155 | } | |
fe332038 | 156 | if (wildmatch(pattern, name)) |
2b6b4d53 | 157 | return 1; |
170381c0 WD |
158 | if (ex->match_flags & MATCHFLG_WILD2_PREFIX) { |
159 | /* If the **-prefixed pattern has a '/' as the next | |
160 | * character, then try to match the rest of the | |
161 | * pattern at the root. */ | |
fe332038 | 162 | if (pattern[2] == '/' && wildmatch(pattern+3, name)) |
170381c0 | 163 | return 1; |
c36cd317 | 164 | } |
170381c0 WD |
165 | else if (!match_start && ex->match_flags & MATCHFLG_WILD2) { |
166 | /* A non-anchored match with an infix or trailing "**" | |
167 | * (but not a prefixed "**") needs to try matching | |
168 | * after every slash. */ | |
169 | while ((name = strchr(name, '/')) != NULL) { | |
170 | name++; | |
fe332038 | 171 | if (wildmatch(pattern, name)) |
170381c0 WD |
172 | return 1; |
173 | } | |
174 | } | |
175 | } else if (match_start) { | |
176 | if (strcmp(name,pattern) == 0) | |
177 | return 1; | |
2b6b4d53 AT |
178 | } else { |
179 | int l1 = strlen(name); | |
ea2111d1 | 180 | int l2 = strlen(pattern); |
0f2ac855 | 181 | if (l2 <= l1 && |
ea2111d1 | 182 | strcmp(name+(l1-l2),pattern) == 0 && |
170381c0 | 183 | (l1==l2 || name[l1-(l2+1)] == '/')) { |
2b6b4d53 | 184 | return 1; |
c36cd317 | 185 | } |
2b6b4d53 AT |
186 | } |
187 | ||
188 | return 0; | |
c627d613 AT |
189 | } |
190 | ||
191 | ||
d567322f MP |
192 | static void report_exclude_result(char const *name, |
193 | struct exclude_struct const *ent, | |
5be7fa93 | 194 | int name_is_dir) |
d567322f | 195 | { |
0f2ac855 WD |
196 | /* If a trailing slash is present to match only directories, |
197 | * then it is stripped out by make_exclude. So as a special | |
198 | * case we add it back in here. */ | |
199 | ||
ea847c62 | 200 | if (verbose >= 2) { |
f8f72644 WD |
201 | rprintf(FINFO, "[%s] %scluding %s %s because of pattern %s%s\n", |
202 | who_am_i(), ent->include ? "in" : "ex", | |
203 | name_is_dir ? "directory" : "file", name, ent->pattern, | |
0f2ac855 | 204 | ent->directory ? "/" : ""); |
ea847c62 | 205 | } |
d567322f MP |
206 | } |
207 | ||
208 | ||
209 | /* | |
210 | * Return true if file NAME is defined to be excluded by either | |
211 | * LOCAL_EXCLUDE_LIST or the globals EXCLUDE_LIST. | |
212 | */ | |
5be7fa93 | 213 | int check_exclude(struct exclude_struct **list, char *name, int name_is_dir) |
c627d613 | 214 | { |
0f2ac855 | 215 | struct exclude_struct *ent; |
c627d613 | 216 | |
5be7fa93 WD |
217 | while ((ent = *list++) != NULL) { |
218 | if (check_one_exclude(name, ent, name_is_dir)) { | |
219 | report_exclude_result(name, ent, name_is_dir); | |
220 | return !ent->include; | |
0f2ac855 | 221 | } |
2b6b4d53 | 222 | } |
c627d613 | 223 | |
2b6b4d53 | 224 | return 0; |
c627d613 AT |
225 | } |
226 | ||
227 | ||
f8f72644 WD |
228 | /* Get the next include/exclude arg from the string. The token will not |
229 | * be '\0' terminated, so use the returned length to limit the string. | |
230 | * Also, be sure to add this length to the returned pointer before passing | |
231 | * it back to ask for the next token. This routine will not split off a | |
96d3590a WD |
232 | * prefix of "+ " or "- " unless xflags contains XFLG_NO_PREFIXES. The |
233 | * *incl_ptr value will be 1 for an include, 0 for an exclude, and -1 for | |
234 | * the list-clearing "!" token. | |
f8f72644 | 235 | */ |
96d3590a WD |
236 | static const char *get_exclude_tok(const char *p, int *len_ptr, int *incl_ptr, |
237 | int xflags) | |
f8f72644 | 238 | { |
abca4eba | 239 | const unsigned char *s = (const unsigned char *)p; |
96d3590a | 240 | int len; |
f8f72644 | 241 | |
96d3590a WD |
242 | if (xflags & XFLG_WORD_SPLIT) { |
243 | /* Skip over any initial whitespace. */ | |
244 | while (isspace(*s)) | |
f8f72644 | 245 | s++; |
abca4eba WD |
246 | /* Update for "!" check. */ |
247 | p = (const char *)s; | |
f8f72644 WD |
248 | } |
249 | ||
96d3590a WD |
250 | /* Is this a '+' or '-' followed by a space (not whitespace)? */ |
251 | if (!(xflags & XFLG_NO_PREFIXES) | |
252 | && (*s == '-' || *s == '+') && s[1] == ' ') { | |
253 | *incl_ptr = *s == '+'; | |
254 | s += 2; | |
255 | } else | |
256 | *incl_ptr = xflags & XFLG_DEF_INCLUDE; | |
257 | ||
258 | if (xflags & XFLG_WORD_SPLIT) { | |
259 | const unsigned char *cp = s; | |
260 | /* Token ends at whitespace or the end of the string. */ | |
261 | while (!isspace(*cp) && *cp != '\0') | |
262 | cp++; | |
263 | len = cp - s; | |
264 | } else | |
265 | len = strlen(s); | |
266 | ||
abca4eba | 267 | if (*p == '!' && len == 1 && !(xflags & XFLG_NO_PREFIXES)) |
96d3590a WD |
268 | *incl_ptr = -1; |
269 | ||
270 | *len_ptr = len; | |
271 | return (const char *)s; | |
f8f72644 WD |
272 | } |
273 | ||
274 | ||
275 | void add_exclude(struct exclude_struct ***listp, const char *pattern, int xflags) | |
c627d613 | 276 | { |
5be7fa93 | 277 | struct exclude_struct **list = *listp; |
96d3590a WD |
278 | int pat_len, list_len = 0; |
279 | int incl, add_cnt = 1; | |
f8f72644 | 280 | const char *cp; |
5be7fa93 | 281 | |
f8f72644 | 282 | if (!pattern) |
5e7dbaca | 283 | return; |
f8f72644 WD |
284 | |
285 | if (xflags & XFLG_WORD_SPLIT) { | |
96d3590a WD |
286 | int add = 0; |
287 | /* Count maximum extra tokens we might encounter. */ | |
288 | for (cp = pattern; *cp; cp++) { | |
289 | if (isspace(*(unsigned char *)cp)) { | |
290 | add_cnt += add; | |
291 | add = 0; | |
292 | } else | |
293 | add = 1; | |
f8f72644 | 294 | } |
96d3590a WD |
295 | } |
296 | ||
297 | cp = get_exclude_tok(pattern, &pat_len, &incl, xflags); | |
298 | if (!pat_len) | |
299 | return; | |
f8f72644 | 300 | |
96d3590a WD |
301 | /* Check for the special "!" token that clears the list. Yes, we |
302 | * only honor it at the start of a XFLG_WORD_SPLIT string. */ | |
303 | if (incl < 0) { | |
304 | free_exclude_list(listp); | |
305 | if (!--add_cnt) | |
306 | return; | |
307 | cp = get_exclude_tok(cp + pat_len, &pat_len, &incl, xflags); | |
308 | if (!pat_len) | |
f8f72644 | 309 | return; |
2b6b4d53 AT |
310 | } |
311 | ||
5be7fa93 | 312 | if (list) |
f8f72644 | 313 | for ( ; list[list_len]; list_len++) {} |
0f2ac855 | 314 | |
f8f72644 WD |
315 | list = *listp = realloc_array(list, struct exclude_struct *, |
316 | list_len + add_cnt + 1); | |
317 | if (!list) | |
2b6b4d53 | 318 | out_of_memory("add_exclude"); |
0f2ac855 | 319 | |
f8f72644 | 320 | while (pat_len) { |
f8f72644 WD |
321 | list[list_len++] = make_exclude(cp, pat_len, incl); |
322 | ||
323 | if (verbose > 2) { | |
324 | rprintf(FINFO, "[%s] add_exclude(%s,%s)\n", | |
325 | who_am_i(), cp, | |
326 | incl ? "include" : "exclude"); | |
327 | } | |
96d3590a | 328 | cp = get_exclude_tok(cp + pat_len, &pat_len, &incl, xflags); |
8c35542d MP |
329 | } |
330 | ||
f8f72644 | 331 | list[list_len] = NULL; |
c627d613 AT |
332 | } |
333 | ||
c627d613 | 334 | |
5be7fa93 | 335 | void add_exclude_file(struct exclude_struct ***listp, const char *fname, |
f8f72644 | 336 | int xflags) |
c627d613 | 337 | { |
5e7dbaca | 338 | FILE *fp; |
2b6b4d53 | 339 | char line[MAXPATHLEN]; |
ccdff3eb WD |
340 | char *eob = line + MAXPATHLEN - 1; |
341 | extern int eol_nulls; | |
342 | ||
5be7fa93 WD |
343 | if (!fname || !*fname) |
344 | return; | |
345 | ||
346 | if (*fname != '-' || fname[1]) | |
5e7dbaca | 347 | fp = fopen(fname, "rb"); |
ccdff3eb | 348 | else |
5e7dbaca WD |
349 | fp = stdin; |
350 | if (!fp) { | |
f8f72644 | 351 | if (xflags & XFLG_FATAL_ERRORS) { |
a039749b | 352 | rsyserr(FERROR, errno, |
0f2ac855 | 353 | "failed to open %s file %s", |
f8f72644 | 354 | xflags & XFLG_DEF_INCLUDE ? "include" : "exclude", |
0f2ac855 | 355 | fname); |
65417579 | 356 | exit_cleanup(RERR_FILEIO); |
2b6b4d53 | 357 | } |
5be7fa93 | 358 | return; |
2b6b4d53 AT |
359 | } |
360 | ||
ccdff3eb | 361 | while (1) { |
5e7dbaca WD |
362 | char *s = line; |
363 | int ch; | |
ccdff3eb | 364 | while (1) { |
5e7dbaca WD |
365 | if ((ch = getc(fp)) == EOF) { |
366 | if (ferror(fp) && errno == EINTR) | |
ccdff3eb WD |
367 | continue; |
368 | break; | |
369 | } | |
370 | if (eol_nulls? !ch : (ch == '\n' || ch == '\r')) | |
371 | break; | |
372 | if (s < eob) | |
373 | *s++ = ch; | |
374 | } | |
375 | *s = '\0'; | |
376 | if (*line && *line != ';' && *line != '#') { | |
122f19a6 | 377 | /* Skip lines starting with semicolon or pound. |
ccdff3eb WD |
378 | * It probably wouldn't cause any harm to not skip |
379 | * them but there's no need to save them. */ | |
f8f72644 | 380 | add_exclude(listp, line, xflags); |
122f19a6 | 381 | } |
5e7dbaca | 382 | if (ch == EOF) |
ccdff3eb | 383 | break; |
2b6b4d53 | 384 | } |
5e7dbaca | 385 | fclose(fp); |
c627d613 AT |
386 | } |
387 | ||
388 | ||
389 | void send_exclude_list(int f) | |
390 | { | |
2b6b4d53 | 391 | int i; |
25cf8893 AT |
392 | extern int list_only, recurse; |
393 | ||
bb7c4fa3 MP |
394 | /* This is a complete hack - blame Rusty. |
395 | * | |
396 | * FIXME: This pattern shows up in the output of | |
397 | * report_exclude_result(), which is not ideal. */ | |
5be7fa93 | 398 | if (list_only && !recurse) |
f8f72644 | 399 | add_exclude(&exclude_list, "/*/*", 0); |
2b6b4d53 AT |
400 | |
401 | if (!exclude_list) { | |
a3dbb20a | 402 | write_int(f, 0); |
2b6b4d53 AT |
403 | return; |
404 | } | |
405 | ||
5f5be796 WD |
406 | for (i = 0; exclude_list[i]; i++) { |
407 | unsigned int l; | |
a3dbb20a | 408 | char p[MAXPATHLEN+1]; |
2fb139c1 | 409 | |
a3dbb20a | 410 | l = strlcpy(p, exclude_list[i]->pattern, sizeof p); |
5f5be796 WD |
411 | if (l == 0 || l >= MAXPATHLEN) |
412 | continue; | |
413 | if (exclude_list[i]->directory) { | |
a3dbb20a WD |
414 | p[l++] = '/'; |
415 | p[l] = '\0'; | |
5f5be796 | 416 | } |
2b6b4d53 | 417 | |
587cb08d | 418 | if (exclude_list[i]->include) { |
a3dbb20a WD |
419 | write_int(f, l + 2); |
420 | write_buf(f, "+ ", 2); | |
421 | } else if ((*p == '-' || *p == '+') && p[1] == ' ') { | |
422 | write_int(f, l + 2); | |
423 | write_buf(f, "- ", 2); | |
424 | } else | |
425 | write_int(f, l); | |
426 | write_buf(f, p, l); | |
0f2ac855 | 427 | } |
2b6b4d53 | 428 | |
a3dbb20a | 429 | write_int(f, 0); |
c627d613 AT |
430 | } |
431 | ||
432 | ||
433 | void recv_exclude_list(int f) | |
434 | { | |
5f5be796 | 435 | char line[MAXPATHLEN+1]; /* Allows a trailing slash on a max-len dir */ |
9dd891bb MP |
436 | unsigned int l; |
437 | ||
5f5be796 WD |
438 | while ((l = read_int(f)) != 0) { |
439 | if (l >= sizeof line) | |
440 | overflow("recv_exclude_list"); | |
441 | read_sbuf(f, line, l); | |
f8f72644 | 442 | add_exclude(&exclude_list, line, 0); |
651443a7 | 443 | } |
651443a7 DD |
444 | } |
445 | ||
0f2ac855 | 446 | |
f8f72644 WD |
447 | static char default_cvsignore[] = |
448 | /* These default ignored items come from the CVS manual. */ | |
449 | "RCS SCCS CVS CVS.adm RCSLOG cvslog.* tags TAGS" | |
450 | " .make.state .nse_depinfo *~ #* .#* ,* _$* *$" | |
451 | " *.old *.bak *.BAK *.orig *.rej .del-*" | |
452 | " *.a *.olb *.o *.obj *.so *.exe" | |
453 | " *.Z *.elc *.ln core" | |
454 | /* The rest we added to suit ourself. */ | |
455 | " .svn/"; | |
c627d613 AT |
456 | |
457 | void add_cvs_excludes(void) | |
458 | { | |
2b6b4d53 AT |
459 | char fname[MAXPATHLEN]; |
460 | char *p; | |
0f2ac855 | 461 | |
f8f72644 WD |
462 | add_exclude(&exclude_list, default_cvsignore, |
463 | XFLG_WORD_SPLIT | XFLG_NO_PREFIXES); | |
c627d613 | 464 | |
a7725e6d | 465 | if ((p = getenv("HOME")) |
f8f72644 WD |
466 | && pathjoin(fname, sizeof fname, p, ".cvsignore") < sizeof fname) { |
467 | add_exclude_file(&exclude_list, fname, | |
468 | XFLG_WORD_SPLIT | XFLG_NO_PREFIXES); | |
469 | } | |
c627d613 | 470 | |
f8f72644 WD |
471 | add_exclude(&exclude_list, getenv("CVSIGNORE"), |
472 | XFLG_WORD_SPLIT | XFLG_NO_PREFIXES); | |
c627d613 | 473 | } |