- Complain about an exclude that was too long and then dump it
[rsync/rsync.git] / exclude.c
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
29 extern int verbose;
30 extern int eol_nulls;
31 extern int list_only;
32 extern int recurse;
33
34 extern char curr_dir[];
35
36 struct exclude_list_struct exclude_list = { 0, 0, "" };
37 struct exclude_list_struct local_exclude_list = { 0, 0, "per-dir .cvsignore " };
38 struct exclude_list_struct server_exclude_list = { 0, 0, "server " };
39 char *exclude_path_prefix = NULL;
40
41 /** Build an exclude structure given a exclude pattern */
42 static 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
96 static void free_exclude(struct exclude_struct *ex)
97 {
98         free(ex->pattern);
99         free(ex);
100 }
101
102 void free_exclude_list(struct exclude_list_struct *listp)
103 {
104         struct exclude_struct *ent, *next;
105
106         for (ent = listp->head; ent; ent = next) {
107                 next = ent->next;
108                 free_exclude(ent);
109         }
110
111         listp->head = listp->tail = NULL;
112 }
113
114 static int check_one_exclude(char *name, struct exclude_struct *ex,
115                              int name_is_dir)
116 {
117         char *p;
118         int match_start = 0;
119         char *pattern = ex->pattern;
120
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. */
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];
130                 int plus = curr_dir[1] == '\0'? 1 : 0;
131                 pathjoin(full_name, sizeof full_name, curr_dir+plus, name);
132                 name = full_name;
133         }
134
135         if (!name[0]) return 0;
136
137         if (ex->directory && !name_is_dir) return 0;
138
139         if (*pattern == '/') {
140                 match_start = 1;
141                 pattern++;
142                 if (*name == '/')
143                         name++;
144         }
145
146         if (ex->match_flags & MATCHFLG_WILD) {
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                 }
158                 if (wildmatch(pattern, name))
159                         return 1;
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. */
164                         if (pattern[2] == '/' && wildmatch(pattern+3, name))
165                                 return 1;
166                 }
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++;
173                                 if (wildmatch(pattern, name))
174                                         return 1;
175                         }
176                 }
177         } else if (match_start) {
178                 if (strcmp(name,pattern) == 0)
179                         return 1;
180         } else {
181                 int l1 = strlen(name);
182                 int l2 = strlen(pattern);
183                 if (l2 <= l1 &&
184                     strcmp(name+(l1-l2),pattern) == 0 &&
185                     (l1==l2 || name[l1-(l2+1)] == '/')) {
186                         return 1;
187                 }
188         }
189
190         return 0;
191 }
192
193
194 static void report_exclude_result(char const *name,
195                                   struct exclude_struct const *ent,
196                                   int name_is_dir, const char *type)
197 {
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
202         if (verbose >= 2) {
203                 rprintf(FINFO, "[%s] %scluding %s %s because of %spattern %s%s\n",
204                         who_am_i(), ent->include ? "in" : "ex",
205                         name_is_dir ? "directory" : "file", name, type,
206                         ent->pattern, ent->directory ? "/" : "");
207         }
208 }
209
210
211 /*
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.
214  */
215 int check_exclude(struct exclude_list_struct *listp, char *name, int name_is_dir)
216 {
217         struct exclude_struct *ent;
218
219         for (ent = listp->head; ent; ent = ent->next) {
220                 if (check_one_exclude(name, ent, name_is_dir)) {
221                         report_exclude_result(name, ent, name_is_dir,
222                                               listp->debug_type);
223                         return ent->include ? 1 : -1;
224                 }
225         }
226
227         return 0;
228 }
229
230
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
234  * it back to ask for the next token.  This routine will not parse the +/-
235  * prefixes or the "!" token when xflags contains XFLG_WORDS_ONLY.  The
236  * *incl_ptr value will be 1 for an include, 0 for an exclude, and -1 for
237  * the list-clearing "!" token.
238  */
239 static const char *get_exclude_tok(const char *p, int *len_ptr, int *incl_ptr,
240                                    int xflags)
241 {
242         const unsigned char *s = (const unsigned char *)p;
243         int len;
244
245         if (xflags & XFLG_WORD_SPLIT) {
246                 /* Skip over any initial whitespace. */
247                 while (isspace(*s))
248                         s++;
249                 /* Update for "!" check. */
250                 p = (const char *)s;
251         }
252
253         /* Is this a '+' or '-' followed by a space (not whitespace)? */
254         if (!(xflags & XFLG_WORDS_ONLY)
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
270         if (*p == '!' && len == 1 && !(xflags & XFLG_WORDS_ONLY))
271                 *incl_ptr = -1;
272
273         *len_ptr = len;
274         return (const char *)s;
275 }
276
277
278 void add_exclude(struct exclude_list_struct *listp, const char *pattern,
279                  int xflags)
280 {
281         int pat_len, incl;
282         const char *cp;
283
284         if (!pattern)
285                 return;
286
287         cp = pattern;
288         pat_len = 0;
289         while (1) {
290                 cp = get_exclude_tok(cp + pat_len, &pat_len, &incl, xflags);
291                 if (!pat_len)
292                         break;
293                 /* If we got the special "!" token, clear the list. */
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                         }
300                         free_exclude_list(listp);
301                 } else {
302                         make_exclude(listp, cp, pat_len, incl);
303
304                         if (verbose > 2) {
305                                 rprintf(FINFO, "[%s] add_exclude(%.*s, %s%s)\n",
306                                         who_am_i(), pat_len, cp,
307                                         listp->debug_type,
308                                         incl ? "include" : "exclude");
309                         }
310                 }
311         }
312 }
313
314
315 void add_exclude_file(struct exclude_list_struct *listp, const char *fname,
316                       int xflags)
317 {
318         FILE *fp;
319         char line[MAXPATHLEN+3]; /* Room for "x " prefix and trailing slash. */
320         char *eob = line + MAXPATHLEN - 1;
321         int word_split = xflags & XFLG_WORD_SPLIT;
322
323         if (!fname || !*fname)
324                 return;
325
326         if (*fname != '-' || fname[1])
327                 fp = fopen(fname, "rb");
328         else
329                 fp = stdin;
330         if (!fp) {
331                 if (xflags & XFLG_FATAL_ERRORS) {
332                         rsyserr(FERROR, errno,
333                                 "failed to open %s file %s",
334                                 xflags & XFLG_DEF_INCLUDE ? "include" : "exclude",
335                                 fname);
336                         exit_cleanup(RERR_FILEIO);
337                 }
338                 return;
339         }
340
341         while (1) {
342                 char *s = line;
343                 int ch, overflow = 0;
344                 while (1) {
345                         if ((ch = getc(fp)) == EOF) {
346                                 if (ferror(fp) && errno == EINTR)
347                                         continue;
348                                 break;
349                         }
350                         if (word_split && isspace(ch))
351                                 break;
352                         if (eol_nulls? !ch : (ch == '\n' || ch == '\r'))
353                                 break;
354                         if (s < eob)
355                                 *s++ = ch;
356                         else
357                                 overflow = 1;
358                 }
359                 if (overflow) {
360                         rprintf(FERROR, "discarding over-long exclude: %s...\n", line);
361                         s = line;
362                 }
363                 *s = '\0';
364                 /* Skip an empty token and (when line parsing) comments. */
365                 if (*line && (word_split || (*line != ';' && *line != '#')))
366                         add_exclude(listp, line, xflags);
367                 if (ch == EOF)
368                         break;
369         }
370         fclose(fp);
371 }
372
373
374 void send_exclude_list(int f)
375 {
376         struct exclude_struct *ent;
377
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. */
382         if (list_only && !recurse)
383                 add_exclude(&exclude_list, "/*/*", 0);
384
385         for (ent = exclude_list.head; ent; ent = ent->next) {
386                 unsigned int l;
387                 char p[MAXPATHLEN+1];
388
389                 l = strlcpy(p, ent->pattern, sizeof p);
390                 if (l == 0 || l >= MAXPATHLEN)
391                         continue;
392                 if (ent->directory) {
393                         p[l++] = '/';
394                         p[l] = '\0';
395                 }
396
397                 if (ent->include) {
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);
406         }
407
408         write_int(f, 0);
409 }
410
411
412 void recv_exclude_list(int f)
413 {
414         char line[MAXPATHLEN+3]; /* Room for "x " prefix and trailing slash. */
415         unsigned int l;
416
417         while ((l = read_int(f)) != 0) {
418                 if (l >= sizeof line)
419                         overflow("recv_exclude_list");
420                 read_sbuf(f, line, l);
421                 add_exclude(&exclude_list, line, 0);
422         }
423 }
424
425
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/";
435
436 void add_cvs_excludes(void)
437 {
438         char fname[MAXPATHLEN];
439         char *p;
440
441         add_exclude(&exclude_list, default_cvsignore,
442                     XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);
443
444         if ((p = getenv("HOME"))
445             && pathjoin(fname, sizeof fname, p, ".cvsignore") < sizeof fname) {
446                 add_exclude_file(&exclude_list, fname,
447                                  XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);
448         }
449
450         add_exclude(&exclude_list, getenv("CVSIGNORE"),
451                     XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);
452 }