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