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