Changed check_exclude()'s prototype.
[rsync/rsync.git] / exclude.c
... / ...
CommitLineData
1/* -*- c-file-style: "linux" -*-
2 *
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
6 *
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.
11 *
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.
16 *
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 */
21
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 :) */
24
25/* include/exclude cluestick added by Martin Pool <mbp@samba.org> */
26
27#include "rsync.h"
28
29extern int verbose;
30extern int eol_nulls;
31extern int list_only;
32extern int recurse;
33
34extern char curr_dir[];
35
36struct exclude_list_struct exclude_list;
37struct exclude_list_struct local_exclude_list;
38struct exclude_list_struct server_exclude_list;
39char *exclude_path_prefix = NULL;
40
41/** Build an exclude structure given a exclude pattern */
42static void make_exclude(struct exclude_list_struct *listp, const char *pattern,
43 int pat_len, int include)
44{
45 struct exclude_struct *ret;
46 const char *cp;
47 int ex_len;
48
49 ret = new(struct exclude_struct);
50 if (!ret)
51 out_of_memory("make_exclude");
52
53 memset(ret, 0, sizeof ret[0]);
54 ret->include = include;
55
56 if (exclude_path_prefix)
57 ret->match_flags |= MATCHFLG_ABS_PATH;
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, "*[?")) {
71 ret->match_flags |= MATCHFLG_WILD;
72 if ((cp = strstr(ret->pattern, "**")) != NULL) {
73 ret->match_flags |= MATCHFLG_WILD2;
74 /* If the pattern starts with **, note that. */
75 if (cp == ret->pattern)
76 ret->match_flags |= MATCHFLG_WILD2_PREFIX;
77 }
78 }
79
80 if (pat_len > 1 && ret->pattern[pat_len-1] == '/') {
81 ret->pattern[pat_len-1] = 0;
82 ret->directory = 1;
83 }
84
85 for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
86 ret->slash_cnt++;
87
88 if (!listp->tail)
89 listp->head = listp->tail = ret;
90 else {
91 listp->tail->next = ret;
92 listp->tail = ret;
93 }
94}
95
96static void free_exclude(struct exclude_struct *ex)
97{
98 free(ex->pattern);
99 free(ex);
100}
101
102void free_exclude_list(struct exclude_list_struct *listp)
103{
104 struct exclude_struct *ent, *next;
105
106 if (verbose > 2)
107 rprintf(FINFO, "[%s] clearing exclude list\n", who_am_i());
108
109 for (ent = listp->head; ent; ent = next) {
110 next = ent->next;
111 free_exclude(ent);
112 }
113
114 memset(listp, 0, sizeof listp[0]);
115}
116
117static int check_one_exclude(char *name, struct exclude_struct *ex,
118 int name_is_dir)
119{
120 char *p;
121 int match_start = 0;
122 char *pattern = ex->pattern;
123
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. */
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];
133 int plus = curr_dir[1] == '\0'? 1 : 0;
134 pathjoin(full_name, sizeof full_name, curr_dir+plus, name);
135 name = full_name;
136 }
137
138 if (!name[0]) return 0;
139
140 if (ex->directory && !name_is_dir) return 0;
141
142 if (*pattern == '/') {
143 match_start = 1;
144 pattern++;
145 if (*name == '/')
146 name++;
147 }
148
149 if (ex->match_flags & MATCHFLG_WILD) {
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 }
161 if (wildmatch(pattern, name))
162 return 1;
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. */
167 if (pattern[2] == '/' && wildmatch(pattern+3, name))
168 return 1;
169 }
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++;
176 if (wildmatch(pattern, name))
177 return 1;
178 }
179 }
180 } else if (match_start) {
181 if (strcmp(name,pattern) == 0)
182 return 1;
183 } else {
184 int l1 = strlen(name);
185 int l2 = strlen(pattern);
186 if (l2 <= l1 &&
187 strcmp(name+(l1-l2),pattern) == 0 &&
188 (l1==l2 || name[l1-(l2+1)] == '/')) {
189 return 1;
190 }
191 }
192
193 return 0;
194}
195
196
197static void report_exclude_result(char const *name,
198 struct exclude_struct const *ent,
199 int name_is_dir, const char *type)
200{
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
205 if (verbose >= 2) {
206 rprintf(FINFO, "[%s] %scluding %s %s because of %s %s%s\n",
207 who_am_i(), ent->include ? "in" : "ex",
208 name_is_dir ? "directory" : "file", name, type,
209 ent->pattern, ent->directory ? "/" : "");
210 }
211}
212
213
214/*
215 * Return true if file NAME is defined to be excluded by the specified
216 * exclude list.
217 */
218int check_exclude(struct exclude_list_struct *listp, char *name, int name_is_dir,
219 const char *type)
220{
221 struct exclude_struct *ent;
222
223 for (ent = listp->head; ent; ent = ent->next) {
224 if (check_one_exclude(name, ent, name_is_dir)) {
225 report_exclude_result(name, ent, name_is_dir, type);
226 return !ent->include;
227 }
228 }
229
230 return 0;
231}
232
233
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
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.
241 */
242static const char *get_exclude_tok(const char *p, int *len_ptr, int *incl_ptr,
243 int xflags)
244{
245 const unsigned char *s = (const unsigned char *)p;
246 int len;
247
248 if (xflags & XFLG_WORD_SPLIT) {
249 /* Skip over any initial whitespace. */
250 while (isspace(*s))
251 s++;
252 /* Update for "!" check. */
253 p = (const char *)s;
254 }
255
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
273 if (*p == '!' && len == 1 && !(xflags & XFLG_NO_PREFIXES))
274 *incl_ptr = -1;
275
276 *len_ptr = len;
277 return (const char *)s;
278}
279
280
281void add_exclude(struct exclude_list_struct *listp, const char *pattern,
282 int xflags)
283{
284 int pat_len, incl;
285 const char *cp;
286
287 if (!pattern)
288 return;
289
290 cp = pattern;
291 pat_len = 0;
292 while (1) {
293 cp = get_exclude_tok(cp + pat_len, &pat_len, &incl, xflags);
294 if (!pat_len)
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) {
303 rprintf(FINFO, "[%s] add_exclude(%.*s,%s)\n",
304 who_am_i(), pat_len, cp,
305 incl ? "include" : "exclude");
306 }
307 }
308 }
309}
310
311
312void add_exclude_file(struct exclude_list_struct *listp, const char *fname,
313 int xflags)
314{
315 FILE *fp;
316 char line[MAXPATHLEN];
317 char *eob = line + MAXPATHLEN - 1;
318 int word_split = xflags & XFLG_WORD_SPLIT;
319
320 if (!fname || !*fname)
321 return;
322
323 if (*fname != '-' || fname[1])
324 fp = fopen(fname, "rb");
325 else
326 fp = stdin;
327 if (!fp) {
328 if (xflags & XFLG_FATAL_ERRORS) {
329 rsyserr(FERROR, errno,
330 "failed to open %s file %s",
331 xflags & XFLG_DEF_INCLUDE ? "include" : "exclude",
332 fname);
333 exit_cleanup(RERR_FILEIO);
334 }
335 return;
336 }
337
338 while (1) {
339 char *s = line;
340 int ch;
341 while (1) {
342 if ((ch = getc(fp)) == EOF) {
343 if (ferror(fp) && errno == EINTR)
344 continue;
345 break;
346 }
347 if (word_split && isspace(ch))
348 break;
349 if (eol_nulls? !ch : (ch == '\n' || ch == '\r'))
350 break;
351 if (s < eob)
352 *s++ = ch;
353 }
354 *s = '\0';
355 /* Skip lines starting with semicolon or pound. */
356 if (*line && *line != ';' && *line != '#')
357 add_exclude(listp, line, xflags);
358 if (ch == EOF)
359 break;
360 }
361 fclose(fp);
362}
363
364
365void send_exclude_list(int f)
366{
367 struct exclude_struct *ent;
368
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. */
373 if (list_only && !recurse)
374 add_exclude(&exclude_list, "/*/*", 0);
375
376 for (ent = exclude_list.head; ent; ent = ent->next) {
377 unsigned int l;
378 char p[MAXPATHLEN+1];
379
380 l = strlcpy(p, ent->pattern, sizeof p);
381 if (l == 0 || l >= MAXPATHLEN)
382 continue;
383 if (ent->directory) {
384 p[l++] = '/';
385 p[l] = '\0';
386 }
387
388 if (ent->include) {
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);
397 }
398
399 write_int(f, 0);
400}
401
402
403void recv_exclude_list(int f)
404{
405 char line[MAXPATHLEN+1]; /* Allows a trailing slash on a max-len dir */
406 unsigned int l;
407
408 while ((l = read_int(f)) != 0) {
409 if (l >= sizeof line)
410 overflow("recv_exclude_list");
411 read_sbuf(f, line, l);
412 add_exclude(&exclude_list, line, 0);
413 }
414}
415
416
417static 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/";
426
427void add_cvs_excludes(void)
428{
429 char fname[MAXPATHLEN];
430 char *p;
431
432 add_exclude(&exclude_list, default_cvsignore,
433 XFLG_WORD_SPLIT | XFLG_NO_PREFIXES);
434
435 if ((p = getenv("HOME"))
436 && pathjoin(fname, sizeof fname, p, ".cvsignore") < sizeof fname) {
437 add_exclude_file(&exclude_list, fname,
438 XFLG_WORD_SPLIT | XFLG_NO_PREFIXES);
439 }
440
441 add_exclude(&exclude_list, getenv("CVSIGNORE"),
442 XFLG_WORD_SPLIT | XFLG_NO_PREFIXES);
443}