- Save some memory in each exclude_struct item by dumping the "include"
[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 = { 0, 0, "" };
37struct exclude_list_struct local_exclude_list = { 0, 0, "per-dir .cvsignore " };
38struct exclude_list_struct server_exclude_list = { 0, 0, "server " };
39char *exclude_path_prefix = NULL;
40
41/** Build an exclude structure given an exclude pattern. */
42static void make_exclude(struct exclude_list_struct *listp, const char *pat,
43 unsigned int pat_len, unsigned int mflags)
44{
45 struct exclude_struct *ret;
46 const char *cp;
47 unsigned 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
55 if (exclude_path_prefix)
56 mflags |= MATCHFLG_ABS_PATH;
57 if (exclude_path_prefix && *pat == '/')
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);
66 strlcpy(ret->pattern + ex_len, pat, pat_len + 1);
67 pat_len += ex_len;
68
69 if (strpbrk(ret->pattern, "*[?")) {
70 mflags |= MATCHFLG_WILD;
71 if ((cp = strstr(ret->pattern, "**")) != NULL) {
72 mflags |= MATCHFLG_WILD2;
73 /* If the pattern starts with **, note that. */
74 if (cp == ret->pattern)
75 mflags |= MATCHFLG_WILD2_PREFIX;
76 }
77 }
78
79 if (pat_len > 1 && ret->pattern[pat_len-1] == '/') {
80 ret->pattern[pat_len-1] = 0;
81 mflags |= MATCHFLG_DIRECTORY;
82 }
83
84 for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
85 ret->slash_cnt++;
86
87 if (!listp->tail)
88 listp->head = listp->tail = ret;
89 else {
90 listp->tail->next = ret;
91 listp->tail = ret;
92 }
93
94 ret->match_flags = mflags;
95}
96
97static void free_exclude(struct exclude_struct *ex)
98{
99 free(ex->pattern);
100 free(ex);
101}
102
103void free_exclude_list(struct exclude_list_struct *listp)
104{
105 struct exclude_struct *ent, *next;
106
107 for (ent = listp->head; ent; ent = next) {
108 next = ent->next;
109 free_exclude(ent);
110 }
111
112 listp->head = listp->tail = NULL;
113}
114
115static int check_one_exclude(char *name, struct exclude_struct *ex,
116 int name_is_dir)
117{
118 char *p;
119 int match_start = 0;
120 char *pattern = ex->pattern;
121
122 /* If the pattern does not have any slashes AND it does not have
123 * a "**" (which could match a slash), then we just match the
124 * name portion of the path. */
125 if (!ex->slash_cnt && !(ex->match_flags & MATCHFLG_WILD2)) {
126 if ((p = strrchr(name,'/')) != NULL)
127 name = p+1;
128 }
129 else if (ex->match_flags & MATCHFLG_ABS_PATH && *name != '/') {
130 static char full_name[MAXPATHLEN];
131 int plus = curr_dir[1] == '\0'? 1 : 0;
132 pathjoin(full_name, sizeof full_name, curr_dir+plus, name);
133 name = full_name;
134 }
135
136 if (!name[0]) return 0;
137
138 if (ex->match_flags & MATCHFLG_DIRECTORY && !name_is_dir)
139 return 0;
140
141 if (*pattern == '/') {
142 match_start = 1;
143 pattern++;
144 if (*name == '/')
145 name++;
146 }
147
148 if (ex->match_flags & MATCHFLG_WILD) {
149 /* A non-anchored match with an infix slash and no "**"
150 * needs to match the last slash_cnt+1 name elements. */
151 if (!match_start && ex->slash_cnt
152 && !(ex->match_flags & MATCHFLG_WILD2)) {
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 }
160 if (wildmatch(pattern, name))
161 return 1;
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. */
166 if (pattern[2] == '/' && wildmatch(pattern+3, name))
167 return 1;
168 }
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++;
175 if (wildmatch(pattern, name))
176 return 1;
177 }
178 }
179 } else if (match_start) {
180 if (strcmp(name,pattern) == 0)
181 return 1;
182 } else {
183 int l1 = strlen(name);
184 int l2 = strlen(pattern);
185 if (l2 <= l1 &&
186 strcmp(name+(l1-l2),pattern) == 0 &&
187 (l1==l2 || name[l1-(l2+1)] == '/')) {
188 return 1;
189 }
190 }
191
192 return 0;
193}
194
195
196static void report_exclude_result(char const *name,
197 struct exclude_struct const *ent,
198 int name_is_dir, const char *type)
199{
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
204 if (verbose >= 2) {
205 rprintf(FINFO, "[%s] %scluding %s %s because of %spattern %s%s\n",
206 who_am_i(),
207 ent->match_flags & MATCHFLG_INCLUDE ? "in" : "ex",
208 name_is_dir ? "directory" : "file", name, type,
209 ent->pattern,
210 ent->match_flags & MATCHFLG_DIRECTORY ? "/" : "");
211 }
212}
213
214
215/*
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.
218 */
219int check_exclude(struct exclude_list_struct *listp, char *name, int name_is_dir)
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,
226 listp->debug_type);
227 return ent->match_flags & MATCHFLG_INCLUDE ? 1 : -1;
228 }
229 }
230
231 return 0;
232}
233
234
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
238 * it back to ask for the next token. This routine will not parse the +/-
239 * prefixes or the "!" token when xflags contains XFLG_WORDS_ONLY. The
240 * *incl_ptr value will be 1 for an include, 0 for an exclude, and -1 for
241 * the list-clearing "!" token.
242 */
243static const char *get_exclude_tok(const char *p, unsigned int *len_ptr,
244 unsigned int *flag_ptr, int xflags)
245{
246 const unsigned char *s = (const unsigned char *)p;
247 unsigned int len, mflags = 0;
248
249 if (xflags & XFLG_WORD_SPLIT) {
250 /* Skip over any initial whitespace. */
251 while (isspace(*s))
252 s++;
253 /* Update for "!" check. */
254 p = (const char *)s;
255 }
256
257 /* Is this a '+' or '-' followed by a space (not whitespace)? */
258 if (!(xflags & XFLG_WORDS_ONLY)
259 && (*s == '-' || *s == '+') && s[1] == ' ') {
260 if (*s == '+')
261 mflags |= MATCHFLG_INCLUDE;
262 s += 2;
263 } else if (xflags & XFLG_DEF_INCLUDE)
264 mflags |= MATCHFLG_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
275 if (*p == '!' && len == 1 && !(xflags & XFLG_WORDS_ONLY))
276 mflags |= MATCHFLG_CLEAR_LIST;
277
278 *len_ptr = len;
279 *flag_ptr = mflags;
280 return (const char *)s;
281}
282
283
284void add_exclude(struct exclude_list_struct *listp, const char *pattern,
285 int xflags)
286{
287 unsigned int pat_len, mflags;
288 const char *cp;
289
290 if (!pattern)
291 return;
292
293 cp = pattern;
294 pat_len = 0;
295 while (1) {
296 cp = get_exclude_tok(cp + pat_len, &pat_len, &mflags, xflags);
297 if (!pat_len)
298 break;
299
300 if (mflags & MATCHFLG_CLEAR_LIST) {
301 if (verbose > 2) {
302 rprintf(FINFO,
303 "[%s] clearing %sexclude list\n",
304 who_am_i(), listp->debug_type);
305 }
306 free_exclude_list(listp);
307 continue;
308 }
309
310 make_exclude(listp, cp, pat_len, mflags);
311
312 if (verbose > 2) {
313 rprintf(FINFO, "[%s] add_exclude(%.*s, %s%sclude)\n",
314 who_am_i(), pat_len, cp, listp->debug_type,
315 mflags & MATCHFLG_INCLUDE ? "in" : "ex");
316 }
317 }
318}
319
320
321void add_exclude_file(struct exclude_list_struct *listp, const char *fname,
322 int xflags)
323{
324 FILE *fp;
325 char line[MAXPATHLEN+3]; /* Room for "x " prefix and trailing slash. */
326 char *eob = line + MAXPATHLEN - 1;
327 int word_split = xflags & XFLG_WORD_SPLIT;
328
329 if (!fname || !*fname)
330 return;
331
332 if (*fname != '-' || fname[1])
333 fp = fopen(fname, "rb");
334 else
335 fp = stdin;
336 if (!fp) {
337 if (xflags & XFLG_FATAL_ERRORS) {
338 rsyserr(FERROR, errno,
339 "failed to open %s file %s",
340 xflags & XFLG_DEF_INCLUDE ? "include" : "exclude",
341 fname);
342 exit_cleanup(RERR_FILEIO);
343 }
344 return;
345 }
346
347 while (1) {
348 char *s = line;
349 int ch, overflow = 0;
350 while (1) {
351 if ((ch = getc(fp)) == EOF) {
352 if (ferror(fp) && errno == EINTR)
353 continue;
354 break;
355 }
356 if (word_split && isspace(ch))
357 break;
358 if (eol_nulls? !ch : (ch == '\n' || ch == '\r'))
359 break;
360 if (s < eob)
361 *s++ = ch;
362 else
363 overflow = 1;
364 }
365 if (overflow) {
366 rprintf(FERROR, "discarding over-long exclude: %s...\n", line);
367 s = line;
368 }
369 *s = '\0';
370 /* Skip an empty token and (when line parsing) comments. */
371 if (*line && (word_split || (*line != ';' && *line != '#')))
372 add_exclude(listp, line, xflags);
373 if (ch == EOF)
374 break;
375 }
376 fclose(fp);
377}
378
379
380void send_exclude_list(int f)
381{
382 struct exclude_struct *ent;
383
384 /* This is a complete hack - blame Rusty.
385 *
386 * FIXME: This pattern shows up in the output of
387 * report_exclude_result(), which is not ideal. */
388 if (list_only && !recurse)
389 add_exclude(&exclude_list, "/*/*", 0);
390
391 for (ent = exclude_list.head; ent; ent = ent->next) {
392 unsigned int l;
393 char p[MAXPATHLEN+1];
394
395 l = strlcpy(p, ent->pattern, sizeof p);
396 if (l == 0 || l >= MAXPATHLEN)
397 continue;
398 if (ent->match_flags & MATCHFLG_DIRECTORY) {
399 p[l++] = '/';
400 p[l] = '\0';
401 }
402
403 if (ent->match_flags & MATCHFLG_INCLUDE) {
404 write_int(f, l + 2);
405 write_buf(f, "+ ", 2);
406 } else if ((*p == '-' || *p == '+') && p[1] == ' ') {
407 write_int(f, l + 2);
408 write_buf(f, "- ", 2);
409 } else
410 write_int(f, l);
411 write_buf(f, p, l);
412 }
413
414 write_int(f, 0);
415}
416
417
418void recv_exclude_list(int f)
419{
420 char line[MAXPATHLEN+3]; /* Room for "x " prefix and trailing slash. */
421 unsigned int l;
422
423 while ((l = read_int(f)) != 0) {
424 if (l >= sizeof line)
425 overflow("recv_exclude_list");
426 read_sbuf(f, line, l);
427 add_exclude(&exclude_list, line, 0);
428 }
429}
430
431
432static char default_cvsignore[] =
433 /* These default ignored items come from the CVS manual. */
434 "RCS SCCS CVS CVS.adm RCSLOG cvslog.* tags TAGS"
435 " .make.state .nse_depinfo *~ #* .#* ,* _$* *$"
436 " *.old *.bak *.BAK *.orig *.rej .del-*"
437 " *.a *.olb *.o *.obj *.so *.exe"
438 " *.Z *.elc *.ln core"
439 /* The rest we added to suit ourself. */
440 " .svn/";
441
442void add_cvs_excludes(void)
443{
444 char fname[MAXPATHLEN];
445 char *p;
446
447 add_exclude(&exclude_list, default_cvsignore,
448 XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);
449
450 if ((p = getenv("HOME"))
451 && pathjoin(fname, sizeof fname, p, ".cvsignore") < sizeof fname) {
452 add_exclude_file(&exclude_list, fname,
453 XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);
454 }
455
456 add_exclude(&exclude_list, getenv("CVSIGNORE"),
457 XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);
458}