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; | |
8458724d | 30 | extern int delete_mode; |
c627d613 | 31 | |
2b6b4d53 | 32 | static struct exclude_struct **exclude_list; |
c627d613 | 33 | |
07a874fd | 34 | /** Build an exclude structure given a exclude pattern */ |
f0f5767f | 35 | static struct exclude_struct *make_exclude(const char *pattern, int include) |
c627d613 | 36 | { |
2b6b4d53 | 37 | struct exclude_struct *ret; |
170381c0 | 38 | char *cp; |
c627d613 | 39 | |
2b6b4d53 AT |
40 | ret = (struct exclude_struct *)malloc(sizeof(*ret)); |
41 | if (!ret) out_of_memory("make_exclude"); | |
c627d613 | 42 | |
2b6b4d53 AT |
43 | memset(ret, 0, sizeof(*ret)); |
44 | ||
2b6b4d53 AT |
45 | if (strncmp(pattern,"- ",2) == 0) { |
46 | pattern += 2; | |
47 | } else if (strncmp(pattern,"+ ",2) == 0) { | |
48 | ret->include = 1; | |
49 | pattern += 2; | |
50 | } else { | |
51 | ret->include = include; | |
52 | } | |
53 | ||
54 | ret->pattern = strdup(pattern); | |
55 | ||
587cb08d | 56 | if (!ret->pattern) out_of_memory("make_exclude"); |
c627d613 | 57 | |
2bca43f6 | 58 | if (strpbrk(pattern, "*[?")) { |
170381c0 | 59 | ret->match_flags |= MATCHFLG_WILD; |
0f2ac855 WD |
60 | if (strstr(pattern, "**")) { |
61 | static int tested; | |
62 | if (!tested) { | |
63 | tested = 1; | |
64 | if (fnmatch("a/b/*","a/b/c/d",FNM_PATHNAME)==0) | |
65 | rprintf(FERROR,"WARNING: fnmatch FNM_PATHNAME is broken on your system\n"); | |
66 | } | |
170381c0 WD |
67 | ret->match_flags |= MATCHFLG_WILD2; |
68 | /* If the pattern starts with **, note that. */ | |
69 | if (*pattern == '*' && pattern[1] == '*') | |
70 | ret->match_flags |= MATCHFLG_WILD2_PREFIX; | |
0f2ac855 | 71 | } |
2bca43f6 | 72 | } |
c627d613 | 73 | |
2b6b4d53 AT |
74 | if (strlen(pattern) > 1 && pattern[strlen(pattern)-1] == '/') { |
75 | ret->pattern[strlen(pattern)-1] = 0; | |
76 | ret->directory = 1; | |
77 | } | |
c627d613 | 78 | |
170381c0 WD |
79 | for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++) |
80 | ret->slash_cnt++; | |
0944563e | 81 | |
2b6b4d53 AT |
82 | return ret; |
83 | } | |
84 | ||
85 | static void free_exclude(struct exclude_struct *ex) | |
86 | { | |
2b6b4d53 AT |
87 | free(ex->pattern); |
88 | memset(ex,0,sizeof(*ex)); | |
89 | free(ex); | |
90 | } | |
c627d613 | 91 | |
d567322f MP |
92 | static int check_one_exclude(char *name, struct exclude_struct *ex, |
93 | STRUCT_STAT *st) | |
2b6b4d53 AT |
94 | { |
95 | char *p; | |
0f2ac855 | 96 | int match_start = 0; |
2b6b4d53 AT |
97 | char *pattern = ex->pattern; |
98 | ||
170381c0 WD |
99 | /* If the pattern does not have any slashes AND it does not have |
100 | * a "**" (which could match a slash), then we just match the | |
101 | * name portion of the path. */ | |
102 | if (!ex->slash_cnt && !(ex->match_flags & MATCHFLG_WILD2) && | |
103 | (p = strrchr(name,'/')) != NULL) | |
2b6b4d53 AT |
104 | name = p+1; |
105 | ||
106 | if (!name[0]) return 0; | |
107 | ||
108 | if (ex->directory && !S_ISDIR(st->st_mode)) return 0; | |
109 | ||
170381c0 | 110 | if (*pattern == '/') { |
2b6b4d53 AT |
111 | match_start = 1; |
112 | pattern++; | |
170381c0 WD |
113 | if (*name == '/') |
114 | name++; | |
2b6b4d53 AT |
115 | } |
116 | ||
170381c0 WD |
117 | if (ex->match_flags & MATCHFLG_WILD) { |
118 | int fnmatch_flags = (ex->match_flags & MATCHFLG_WILD2)? | |
119 | 0 : FNM_PATHNAME; | |
120 | /* A non-anchored match with an infix slash and no "**" | |
121 | * needs to match the last slash_cnt+1 name elements. */ | |
122 | if (!match_start && ex->slash_cnt && | |
123 | !(ex->match_flags & MATCHFLG_WILD2)) { | |
124 | int cnt = ex->slash_cnt + 1; | |
125 | for (p = name + strlen(name) - 1; p >= name; p--) { | |
126 | if (*p == '/' && !--cnt) | |
127 | break; | |
128 | } | |
129 | name = p+1; | |
130 | } | |
131 | if (fnmatch(pattern, name, fnmatch_flags) == 0) | |
2b6b4d53 | 132 | return 1; |
170381c0 WD |
133 | if (ex->match_flags & MATCHFLG_WILD2_PREFIX) { |
134 | /* If the **-prefixed pattern has a '/' as the next | |
135 | * character, then try to match the rest of the | |
136 | * pattern at the root. */ | |
137 | if (pattern[2] == '/' && | |
138 | fnmatch(pattern+3, name, fnmatch_flags) == 0) | |
139 | return 1; | |
c36cd317 | 140 | } |
170381c0 WD |
141 | else if (!match_start && ex->match_flags & MATCHFLG_WILD2) { |
142 | /* A non-anchored match with an infix or trailing "**" | |
143 | * (but not a prefixed "**") needs to try matching | |
144 | * after every slash. */ | |
145 | while ((name = strchr(name, '/')) != NULL) { | |
146 | name++; | |
147 | if (fnmatch(pattern, name, fnmatch_flags) == 0) | |
148 | return 1; | |
149 | } | |
150 | } | |
151 | } else if (match_start) { | |
152 | if (strcmp(name,pattern) == 0) | |
153 | return 1; | |
2b6b4d53 AT |
154 | } else { |
155 | int l1 = strlen(name); | |
ea2111d1 | 156 | int l2 = strlen(pattern); |
0f2ac855 | 157 | if (l2 <= l1 && |
ea2111d1 | 158 | strcmp(name+(l1-l2),pattern) == 0 && |
170381c0 | 159 | (l1==l2 || name[l1-(l2+1)] == '/')) { |
2b6b4d53 | 160 | return 1; |
c36cd317 | 161 | } |
2b6b4d53 AT |
162 | } |
163 | ||
164 | return 0; | |
c627d613 AT |
165 | } |
166 | ||
167 | ||
d567322f MP |
168 | static void report_exclude_result(char const *name, |
169 | struct exclude_struct const *ent, | |
170 | STRUCT_STAT const *st) | |
171 | { | |
0f2ac855 WD |
172 | /* If a trailing slash is present to match only directories, |
173 | * then it is stripped out by make_exclude. So as a special | |
174 | * case we add it back in here. */ | |
175 | ||
176 | if (verbose >= 2) | |
177 | rprintf(FINFO, "%s %s %s because of pattern %s%s\n", | |
178 | ent->include ? "including" : "excluding", | |
179 | S_ISDIR(st->st_mode) ? "directory" : "file", | |
180 | name, ent->pattern, | |
181 | ent->directory ? "/" : ""); | |
d567322f MP |
182 | } |
183 | ||
184 | ||
185 | /* | |
186 | * Return true if file NAME is defined to be excluded by either | |
187 | * LOCAL_EXCLUDE_LIST or the globals EXCLUDE_LIST. | |
188 | */ | |
189 | int check_exclude(char *name, struct exclude_struct **local_exclude_list, | |
2b6b4d53 | 190 | STRUCT_STAT *st) |
c627d613 | 191 | { |
2b6b4d53 | 192 | int n; |
0f2ac855 | 193 | struct exclude_struct *ent; |
c627d613 | 194 | |
5d5811f7 DD |
195 | if (name && (name[0] == '.') && !name[1]) |
196 | /* never exclude '.', even if somebody does --exclude '*' */ | |
197 | return 0; | |
198 | ||
2b6b4d53 | 199 | if (exclude_list) { |
d567322f | 200 | for (n=0; exclude_list[n]; n++) { |
0f2ac855 | 201 | ent = exclude_list[n]; |
d567322f | 202 | if (check_one_exclude(name, ent, st)) { |
0f2ac855 | 203 | report_exclude_result(name, ent, st); |
d567322f | 204 | return !ent->include; |
0f2ac855 WD |
205 | } |
206 | } | |
2b6b4d53 | 207 | } |
c627d613 | 208 | |
2b6b4d53 | 209 | if (local_exclude_list) { |
d567322f | 210 | for (n=0; local_exclude_list[n]; n++) { |
0f2ac855 | 211 | ent = local_exclude_list[n]; |
d567322f | 212 | if (check_one_exclude(name, ent, st)) { |
0f2ac855 | 213 | report_exclude_result(name, ent, st); |
d567322f | 214 | return !ent->include; |
0f2ac855 WD |
215 | } |
216 | } | |
2b6b4d53 | 217 | } |
c627d613 | 218 | |
2b6b4d53 | 219 | return 0; |
c627d613 AT |
220 | } |
221 | ||
222 | ||
f0f5767f | 223 | void add_exclude_list(const char *pattern, struct exclude_struct ***list, int include) |
c627d613 | 224 | { |
2b6b4d53 AT |
225 | int len=0; |
226 | if (list && *list) | |
227 | for (; (*list)[len]; len++) ; | |
228 | ||
229 | if (strcmp(pattern,"!") == 0) { | |
230 | if (verbose > 2) | |
231 | rprintf(FINFO,"clearing exclude list\n"); | |
232 | while ((len)--) { | |
233 | free_exclude((*list)[len]); | |
234 | } | |
235 | free((*list)); | |
236 | *list = NULL; | |
237 | return; | |
238 | } | |
239 | ||
fe8c0a98 | 240 | *list = (struct exclude_struct **)Realloc(*list,sizeof(struct exclude_struct *)*(len+2)); |
0f2ac855 | 241 | |
2b6b4d53 AT |
242 | if (!*list || !((*list)[len] = make_exclude(pattern, include))) |
243 | out_of_memory("add_exclude"); | |
0f2ac855 | 244 | |
8c35542d MP |
245 | if (verbose > 2) { |
246 | rprintf(FINFO,"add_exclude(%s,%s)\n",pattern, | |
0f2ac855 | 247 | include ? "include" : "exclude"); |
8c35542d MP |
248 | } |
249 | ||
2b6b4d53 | 250 | (*list)[len+1] = NULL; |
c627d613 AT |
251 | } |
252 | ||
f0f5767f | 253 | void add_exclude(const char *pattern, int include) |
c627d613 | 254 | { |
2b6b4d53 | 255 | add_exclude_list(pattern,&exclude_list, include); |
c627d613 AT |
256 | } |
257 | ||
f0f5767f | 258 | struct exclude_struct **make_exclude_list(const char *fname, |
2b6b4d53 AT |
259 | struct exclude_struct **list1, |
260 | int fatal, int include) | |
c627d613 | 261 | { |
2b6b4d53 | 262 | struct exclude_struct **list=list1; |
ccdff3eb | 263 | int fd; |
2b6b4d53 | 264 | char line[MAXPATHLEN]; |
ccdff3eb WD |
265 | char *eob = line + MAXPATHLEN - 1; |
266 | extern int eol_nulls; | |
267 | ||
268 | if (strcmp(fname, "-") != 0) | |
269 | fd = open(fname, O_RDONLY|O_BINARY); | |
270 | else | |
271 | fd = 0; | |
272 | if (fd < 0) { | |
2b6b4d53 | 273 | if (fatal) { |
a039749b | 274 | rsyserr(FERROR, errno, |
0f2ac855 WD |
275 | "failed to open %s file %s", |
276 | include ? "include" : "exclude", | |
277 | fname); | |
65417579 | 278 | exit_cleanup(RERR_FILEIO); |
2b6b4d53 AT |
279 | } |
280 | return list; | |
281 | } | |
282 | ||
ccdff3eb WD |
283 | while (1) { |
284 | char ch, *s = line; | |
285 | int cnt; | |
286 | while (1) { | |
287 | if ((cnt = read(fd, &ch, 1)) <= 0) { | |
288 | if (cnt < 0 && errno == EINTR) | |
289 | continue; | |
290 | break; | |
291 | } | |
292 | if (eol_nulls? !ch : (ch == '\n' || ch == '\r')) | |
293 | break; | |
294 | if (s < eob) | |
295 | *s++ = ch; | |
296 | } | |
297 | *s = '\0'; | |
298 | if (*line && *line != ';' && *line != '#') { | |
122f19a6 | 299 | /* Skip lines starting with semicolon or pound. |
ccdff3eb WD |
300 | * It probably wouldn't cause any harm to not skip |
301 | * them but there's no need to save them. */ | |
122f19a6 DD |
302 | add_exclude_list(line,&list,include); |
303 | } | |
ccdff3eb WD |
304 | if (cnt <= 0) |
305 | break; | |
2b6b4d53 | 306 | } |
ccdff3eb | 307 | close(fd); |
2b6b4d53 | 308 | return list; |
c627d613 AT |
309 | } |
310 | ||
311 | ||
f0f5767f | 312 | void add_exclude_file(const char *fname, int fatal, int include) |
c627d613 | 313 | { |
8f3a2d54 AT |
314 | if (!fname || !*fname) return; |
315 | ||
2b6b4d53 | 316 | exclude_list = make_exclude_list(fname,exclude_list,fatal,include); |
c627d613 AT |
317 | } |
318 | ||
319 | ||
320 | void send_exclude_list(int f) | |
321 | { | |
2b6b4d53 AT |
322 | int i; |
323 | extern int remote_version; | |
25cf8893 AT |
324 | extern int list_only, recurse; |
325 | ||
bb7c4fa3 MP |
326 | /* This is a complete hack - blame Rusty. |
327 | * | |
328 | * FIXME: This pattern shows up in the output of | |
329 | * report_exclude_result(), which is not ideal. */ | |
25cf8893 AT |
330 | if (list_only && !recurse) { |
331 | add_exclude("/*/*", 0); | |
332 | } | |
2b6b4d53 AT |
333 | |
334 | if (!exclude_list) { | |
335 | write_int(f,0); | |
336 | return; | |
337 | } | |
338 | ||
339 | for (i=0;exclude_list[i];i++) { | |
2b6b4d53 | 340 | int l; |
2fb139c1 AT |
341 | char pattern[MAXPATHLEN]; |
342 | ||
0f2ac855 | 343 | strlcpy(pattern,exclude_list[i]->pattern,sizeof(pattern)); |
2fb139c1 | 344 | if (exclude_list[i]->directory) strlcat(pattern,"/", sizeof(pattern)); |
2b6b4d53 | 345 | |
587cb08d DD |
346 | l = strlen(pattern); |
347 | if (l == 0) continue; | |
348 | if (exclude_list[i]->include) { | |
349 | if (remote_version < 19) { | |
2b6b4d53 | 350 | rprintf(FERROR,"remote rsync does not support include syntax - aborting\n"); |
ec9df380 | 351 | exit_cleanup(RERR_UNSUPPORTED); |
2b6b4d53 | 352 | } |
587cb08d DD |
353 | write_int(f,l+2); |
354 | write_buf(f,"+ ",2); | |
355 | } else { | |
356 | write_int(f,l); | |
2b6b4d53 | 357 | } |
2b6b4d53 | 358 | write_buf(f,pattern,l); |
0f2ac855 | 359 | } |
2b6b4d53 AT |
360 | |
361 | write_int(f,0); | |
c627d613 AT |
362 | } |
363 | ||
364 | ||
365 | void recv_exclude_list(int f) | |
366 | { | |
2b6b4d53 | 367 | char line[MAXPATHLEN]; |
9dd891bb MP |
368 | unsigned int l; |
369 | ||
2b6b4d53 AT |
370 | while ((l=read_int(f))) { |
371 | if (l >= MAXPATHLEN) overflow("recv_exclude_list"); | |
372 | read_sbuf(f,line,l); | |
373 | add_exclude(line,0); | |
374 | } | |
c627d613 AT |
375 | } |
376 | ||
651443a7 DD |
377 | /* Get the next include/exclude arg from the string. It works in a similar way |
378 | ** to strtok - initially an arg is sent over, from then on NULL. This | |
379 | ** routine takes into account any +/- in the strings and does not | |
380 | ** consider the space following it as a delimeter. | |
381 | */ | |
382 | char *get_exclude_tok(char *p) | |
383 | { | |
384 | static char *s; | |
385 | static int more; | |
386 | char *t; | |
387 | ||
388 | if (p) { | |
389 | s=p; | |
390 | if (*p) | |
391 | more=1; | |
392 | } | |
393 | ||
394 | if (!more) | |
395 | return(NULL); | |
c627d613 | 396 | |
651443a7 | 397 | /* Skip over any initial spaces */ |
32f76175 | 398 | while (isspace(* (unsigned char *) s)) |
651443a7 DD |
399 | s++; |
400 | ||
401 | /* Are we at the end of the string? */ | |
402 | if (*s) { | |
403 | /* remember the beginning of the token */ | |
404 | t=s; | |
405 | ||
406 | /* Is this a '+' or '-' followed by a space (not whitespace)? */ | |
407 | if ((*s=='+' || *s=='-') && *(s+1)==' ') | |
408 | s+=2; | |
0f2ac855 | 409 | |
651443a7 | 410 | /* Skip to the next space or the end of the string */ |
32f76175 | 411 | while (!isspace(* (unsigned char *) s) && *s != '\0') |
651443a7 DD |
412 | s++; |
413 | } else { | |
414 | t=NULL; | |
415 | } | |
416 | ||
417 | /* Have we reached the end of the string? */ | |
418 | if (*s) | |
419 | *s++='\0'; | |
420 | else | |
421 | more=0; | |
422 | return(t); | |
423 | } | |
424 | ||
0f2ac855 | 425 | |
8f3a2d54 AT |
426 | void add_exclude_line(char *p) |
427 | { | |
428 | char *tok; | |
429 | if (!p || !*p) return; | |
430 | p = strdup(p); | |
431 | if (!p) out_of_memory("add_exclude_line"); | |
0f2ac855 | 432 | for (tok=get_exclude_tok(p); tok; tok=get_exclude_tok(NULL)) |
2b6b4d53 | 433 | add_exclude(tok, 0); |
8f3a2d54 AT |
434 | free(p); |
435 | } | |
436 | ||
cd64343a DD |
437 | void add_include_line(char *p) |
438 | { | |
439 | char *tok; | |
440 | if (!p || !*p) return; | |
441 | p = strdup(p); | |
442 | if (!p) out_of_memory("add_include_line"); | |
651443a7 | 443 | for (tok=get_exclude_tok(p); tok; tok=get_exclude_tok(NULL)) |
cd64343a DD |
444 | add_exclude(tok, 1); |
445 | free(p); | |
446 | } | |
447 | ||
8f3a2d54 | 448 | |
c627d613 | 449 | static char *cvs_ignore_list[] = { |
95dd949c WD |
450 | "RCS/", "SCCS/", "CVS/", ".svn/", "CVS.adm", "RCSLOG", "cvslog.*", |
451 | "tags", "TAGS", ".make.state", ".nse_depinfo", | |
452 | "*~", "#*", ".#*", ", *", "*.old", "*.bak", "*.BAK", "*.orig", | |
c627d613 | 453 | "*.rej", ".del-*", "*.a", "*.o", "*.obj", "*.so", "*.Z", "*.elc", "*.ln", |
95dd949c | 454 | "core", NULL}; |
c627d613 AT |
455 | |
456 | ||
457 | void add_cvs_excludes(void) | |
458 | { | |
2b6b4d53 AT |
459 | char fname[MAXPATHLEN]; |
460 | char *p; | |
461 | int i; | |
0f2ac855 | 462 | |
2b6b4d53 AT |
463 | for (i=0; cvs_ignore_list[i]; i++) |
464 | add_exclude(cvs_ignore_list[i], 0); | |
c627d613 | 465 | |
2b6b4d53 | 466 | if ((p=getenv("HOME")) && strlen(p) < (MAXPATHLEN-12)) { |
8950ac03 | 467 | snprintf(fname,sizeof(fname), "%s/.cvsignore",p); |
2b6b4d53 AT |
468 | add_exclude_file(fname,0,0); |
469 | } | |
c627d613 | 470 | |
2b6b4d53 | 471 | add_exclude_line(getenv("CVSIGNORE")); |
c627d613 | 472 | } |