Restore the old include behavior where a command-line include could
[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, "local-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         if (verbose > 2) {
107                 rprintf(FINFO, "[%s] clearing %sexclude list\n",
108                         who_am_i(), listp->debug_type);
109         }
110
111         for (ent = listp->head; ent; ent = next) {
112                 next = ent->next;
113                 free_exclude(ent);
114         }
115
116         listp->head = listp->tail = NULL;
117 }
118
119 static int check_one_exclude(char *name, struct exclude_struct *ex,
120                              int name_is_dir)
121 {
122         char *p;
123         int match_start = 0;
124         char *pattern = ex->pattern;
125
126         /* If the pattern does not have any slashes AND it does not have
127          * a "**" (which could match a slash), then we just match the
128          * name portion of the path. */
129         if (!ex->slash_cnt && !(ex->match_flags & MATCHFLG_WILD2)) {
130                 if ((p = strrchr(name,'/')) != NULL)
131                         name = p+1;
132         }
133         else if ((ex->match_flags & MATCHFLG_ABS_PATH) && *name != '/') {
134                 static char full_name[MAXPATHLEN];
135                 int plus = curr_dir[1] == '\0'? 1 : 0;
136                 pathjoin(full_name, sizeof full_name, curr_dir+plus, name);
137                 name = full_name;
138         }
139
140         if (!name[0]) return 0;
141
142         if (ex->directory && !name_is_dir) return 0;
143
144         if (*pattern == '/') {
145                 match_start = 1;
146                 pattern++;
147                 if (*name == '/')
148                         name++;
149         }
150
151         if (ex->match_flags & MATCHFLG_WILD) {
152                 /* A non-anchored match with an infix slash and no "**"
153                  * needs to match the last slash_cnt+1 name elements. */
154                 if (!match_start && ex->slash_cnt &&
155                     !(ex->match_flags & MATCHFLG_WILD2)) {
156                         int cnt = ex->slash_cnt + 1;
157                         for (p = name + strlen(name) - 1; p >= name; p--) {
158                                 if (*p == '/' && !--cnt)
159                                         break;
160                         }
161                         name = p+1;
162                 }
163                 if (wildmatch(pattern, name))
164                         return 1;
165                 if (ex->match_flags & MATCHFLG_WILD2_PREFIX) {
166                         /* If the **-prefixed pattern has a '/' as the next
167                          * character, then try to match the rest of the
168                          * pattern at the root. */
169                         if (pattern[2] == '/' && wildmatch(pattern+3, name))
170                                 return 1;
171                 }
172                 else if (!match_start && ex->match_flags & MATCHFLG_WILD2) {
173                         /* A non-anchored match with an infix or trailing "**"
174                          * (but not a prefixed "**") needs to try matching
175                          * after every slash. */
176                         while ((name = strchr(name, '/')) != NULL) {
177                                 name++;
178                                 if (wildmatch(pattern, name))
179                                         return 1;
180                         }
181                 }
182         } else if (match_start) {
183                 if (strcmp(name,pattern) == 0)
184                         return 1;
185         } else {
186                 int l1 = strlen(name);
187                 int l2 = strlen(pattern);
188                 if (l2 <= l1 &&
189                     strcmp(name+(l1-l2),pattern) == 0 &&
190                     (l1==l2 || name[l1-(l2+1)] == '/')) {
191                         return 1;
192                 }
193         }
194
195         return 0;
196 }
197
198
199 static void report_exclude_result(char const *name,
200                                   struct exclude_struct const *ent,
201                                   int name_is_dir, const char *type)
202 {
203         /* If a trailing slash is present to match only directories,
204          * then it is stripped out by make_exclude.  So as a special
205          * case we add it back in here. */
206
207         if (verbose >= 2) {
208                 rprintf(FINFO, "[%s] %scluding %s %s because of %spattern %s%s\n",
209                         who_am_i(), ent->include ? "in" : "ex",
210                         name_is_dir ? "directory" : "file", name, type,
211                         ent->pattern, ent->directory ? "/" : "");
212         }
213 }
214
215
216 /*
217  * Return true if file NAME is defined to be excluded by the specified
218  * exclude list.  Returns -1 for an exclude, 1 for an include, and 0 if
219  * no match.
220  */
221 int check_exclude(struct exclude_list_struct *listp, char *name, int name_is_dir)
222 {
223         struct exclude_struct *ent;
224
225         for (ent = listp->head; ent; ent = ent->next) {
226                 if (check_one_exclude(name, ent, name_is_dir)) {
227                         report_exclude_result(name, ent, name_is_dir,
228                                               listp->debug_type);
229                         return ent->include ? 1 : -1;
230                 }
231         }
232
233         return 0;
234 }
235
236
237 /* Get the next include/exclude arg from the string.  The token will not
238  * be '\0' terminated, so use the returned length to limit the string.
239  * Also, be sure to add this length to the returned pointer before passing
240  * it back to ask for the next token.  This routine will not split off a
241  * prefix of "+ " or "- " unless xflags contains XFLG_NO_PREFIXES.  The
242  * *incl_ptr value will be 1 for an include, 0 for an exclude, and -1 for
243  * the list-clearing "!" token.
244  */
245 static const char *get_exclude_tok(const char *p, int *len_ptr, int *incl_ptr,
246                                    int xflags)
247 {
248         const unsigned char *s = (const unsigned char *)p;
249         int len;
250
251         if (xflags & XFLG_WORD_SPLIT) {
252                 /* Skip over any initial whitespace. */
253                 while (isspace(*s))
254                         s++;
255                 /* Update for "!" check. */
256                 p = (const char *)s;
257         }
258
259         /* Is this a '+' or '-' followed by a space (not whitespace)? */
260         if (!(xflags & XFLG_NO_PREFIXES)
261             && (*s == '-' || *s == '+') && s[1] == ' ') {
262                 *incl_ptr = *s == '+';
263                 s += 2;
264         } else
265                 *incl_ptr = xflags & XFLG_DEF_INCLUDE;
266
267         if (xflags & XFLG_WORD_SPLIT) {
268                 const unsigned char *cp = s;
269                 /* Token ends at whitespace or the end of the string. */
270                 while (!isspace(*cp) && *cp != '\0')
271                         cp++;
272                 len = cp - s;
273         } else
274                 len = strlen(s);
275
276         if (*p == '!' && len == 1 && !(xflags & XFLG_NO_PREFIXES))
277                 *incl_ptr = -1;
278
279         *len_ptr = len;
280         return (const char *)s;
281 }
282
283
284 void add_exclude(struct exclude_list_struct *listp, const char *pattern,
285                  int xflags)
286 {
287         int pat_len, incl;
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, &incl, xflags);
297                 if (!pat_len)
298                         break;
299                 /* If we got the special "!" token, clear the list. */
300                 if (incl < 0)
301                         free_exclude_list(listp);
302                 else {
303                         make_exclude(listp, cp, pat_len, incl);
304
305                         if (verbose > 2) {
306                                 rprintf(FINFO, "[%s] add_exclude(%.*s, %s%s)\n",
307                                         who_am_i(), pat_len, cp,
308                                         listp->debug_type,
309                                         incl ? "include" : "exclude");
310                         }
311                 }
312         }
313 }
314
315
316 void add_exclude_file(struct exclude_list_struct *listp, const char *fname,
317                       int xflags)
318 {
319         FILE *fp;
320         char line[MAXPATHLEN];
321         char *eob = line + MAXPATHLEN - 1;
322         int word_split = xflags & XFLG_WORD_SPLIT;
323
324         if (!fname || !*fname)
325                 return;
326
327         if (*fname != '-' || fname[1])
328                 fp = fopen(fname, "rb");
329         else
330                 fp = stdin;
331         if (!fp) {
332                 if (xflags & XFLG_FATAL_ERRORS) {
333                         rsyserr(FERROR, errno,
334                                 "failed to open %s file %s",
335                                 xflags & XFLG_DEF_INCLUDE ? "include" : "exclude",
336                                 fname);
337                         exit_cleanup(RERR_FILEIO);
338                 }
339                 return;
340         }
341
342         while (1) {
343                 char *s = line;
344                 int ch;
345                 while (1) {
346                         if ((ch = getc(fp)) == EOF) {
347                                 if (ferror(fp) && errno == EINTR)
348                                         continue;
349                                 break;
350                         }
351                         if (word_split && isspace(ch))
352                                 break;
353                         if (eol_nulls? !ch : (ch == '\n' || ch == '\r'))
354                                 break;
355                         if (s < eob)
356                                 *s++ = ch;
357                 }
358                 *s = '\0';
359                 /* Skip lines starting with semicolon or pound. */
360                 if (*line && *line != ';' && *line != '#')
361                         add_exclude(listp, line, xflags);
362                 if (ch == EOF)
363                         break;
364         }
365         fclose(fp);
366 }
367
368
369 void send_exclude_list(int f)
370 {
371         struct exclude_struct *ent;
372
373         /* This is a complete hack - blame Rusty.
374          *
375          * FIXME: This pattern shows up in the output of
376          * report_exclude_result(), which is not ideal. */
377         if (list_only && !recurse)
378                 add_exclude(&exclude_list, "/*/*", 0);
379
380         for (ent = exclude_list.head; ent; ent = ent->next) {
381                 unsigned int l;
382                 char p[MAXPATHLEN+1];
383
384                 l = strlcpy(p, ent->pattern, sizeof p);
385                 if (l == 0 || l >= MAXPATHLEN)
386                         continue;
387                 if (ent->directory) {
388                         p[l++] = '/';
389                         p[l] = '\0';
390                 }
391
392                 if (ent->include) {
393                         write_int(f, l + 2);
394                         write_buf(f, "+ ", 2);
395                 } else if ((*p == '-' || *p == '+') && p[1] == ' ') {
396                         write_int(f, l + 2);
397                         write_buf(f, "- ", 2);
398                 } else
399                         write_int(f, l);
400                 write_buf(f, p, l);
401         }
402
403         write_int(f, 0);
404 }
405
406
407 void recv_exclude_list(int f)
408 {
409         char line[MAXPATHLEN+1]; /* Allows a trailing slash on a max-len dir */
410         unsigned int l;
411
412         while ((l = read_int(f)) != 0) {
413                 if (l >= sizeof line)
414                         overflow("recv_exclude_list");
415                 read_sbuf(f, line, l);
416                 add_exclude(&exclude_list, line, 0);
417         }
418 }
419
420
421 static char default_cvsignore[] = 
422         /* These default ignored items come from the CVS manual. */
423         "RCS SCCS CVS CVS.adm RCSLOG cvslog.* tags TAGS"
424         " .make.state .nse_depinfo *~ #* .#* ,* _$* *$"
425         " *.old *.bak *.BAK *.orig *.rej .del-*"
426         " *.a *.olb *.o *.obj *.so *.exe"
427         " *.Z *.elc *.ln core"
428         /* The rest we added to suit ourself. */
429         " .svn/";
430
431 void add_cvs_excludes(void)
432 {
433         char fname[MAXPATHLEN];
434         char *p;
435
436         add_exclude(&exclude_list, default_cvsignore,
437                     XFLG_WORD_SPLIT | XFLG_NO_PREFIXES);
438
439         if ((p = getenv("HOME"))
440             && pathjoin(fname, sizeof fname, p, ".cvsignore") < sizeof fname) {
441                 add_exclude_file(&exclude_list, fname,
442                                  XFLG_WORD_SPLIT | XFLG_NO_PREFIXES);
443         }
444
445         add_exclude(&exclude_list, getenv("CVSIGNORE"),
446                     XFLG_WORD_SPLIT | XFLG_NO_PREFIXES);
447 }