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