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