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