Cleaned up whitespace/indentation.
[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 delete_mode;
31
32 static struct exclude_struct **exclude_list;
33
34 /** Build an exclude structure given a exclude pattern */
35 static struct exclude_struct *make_exclude(const char *pattern, int include)
36 {
37         struct exclude_struct *ret;
38         char *cp;
39
40         ret = (struct exclude_struct *)malloc(sizeof(*ret));
41         if (!ret) out_of_memory("make_exclude");
42
43         memset(ret, 0, sizeof(*ret));
44
45         if (strncmp(pattern,"- ",2) == 0) {
46                 pattern += 2;
47         } else if (strncmp(pattern,"+ ",2) == 0) {
48                 ret->include = 1;
49                 pattern += 2;
50         } else {
51                 ret->include = include;
52         }
53
54         ret->pattern = strdup(pattern);
55
56         if (!ret->pattern) out_of_memory("make_exclude");
57
58         if (strpbrk(pattern, "*[?")) {
59                 ret->match_flags |= MATCHFLG_WILD;
60                 if (strstr(pattern, "**")) {
61                         static int tested;
62                         if (!tested) {
63                                 tested = 1;
64                                 if (fnmatch("a/b/*","a/b/c/d",FNM_PATHNAME)==0)
65                                         rprintf(FERROR,"WARNING: fnmatch FNM_PATHNAME is broken on your system\n");
66                         }
67                         ret->match_flags |= MATCHFLG_WILD2;
68                         /* If the pattern starts with **, note that. */
69                         if (*pattern == '*' && pattern[1] == '*')
70                                 ret->match_flags |= MATCHFLG_WILD2_PREFIX;
71                 }
72         }
73
74         if (strlen(pattern) > 1 && pattern[strlen(pattern)-1] == '/') {
75                 ret->pattern[strlen(pattern)-1] = 0;
76                 ret->directory = 1;
77         }
78
79         for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
80                 ret->slash_cnt++;
81
82         return ret;
83 }
84
85 static void free_exclude(struct exclude_struct *ex)
86 {
87         free(ex->pattern);
88         memset(ex,0,sizeof(*ex));
89         free(ex);
90 }
91
92 static int check_one_exclude(char *name, struct exclude_struct *ex,
93                              STRUCT_STAT *st)
94 {
95         char *p;
96         int match_start = 0;
97         char *pattern = ex->pattern;
98
99         /* If the pattern does not have any slashes AND it does not have
100          * a "**" (which could match a slash), then we just match the
101          * name portion of the path. */
102         if (!ex->slash_cnt && !(ex->match_flags & MATCHFLG_WILD2) &&
103             (p = strrchr(name,'/')) != NULL)
104                 name = p+1;
105
106         if (!name[0]) return 0;
107
108         if (ex->directory && !S_ISDIR(st->st_mode)) return 0;
109
110         if (*pattern == '/') {
111                 match_start = 1;
112                 pattern++;
113                 if (*name == '/')
114                         name++;
115         }
116
117         if (ex->match_flags & MATCHFLG_WILD) {
118                 int fnmatch_flags = (ex->match_flags & MATCHFLG_WILD2)?
119                                         0 : FNM_PATHNAME;
120                 /* A non-anchored match with an infix slash and no "**"
121                  * needs to match the last slash_cnt+1 name elements. */
122                 if (!match_start && ex->slash_cnt &&
123                     !(ex->match_flags & MATCHFLG_WILD2)) {
124                         int cnt = ex->slash_cnt + 1;
125                         for (p = name + strlen(name) - 1; p >= name; p--) {
126                                 if (*p == '/' && !--cnt)
127                                         break;
128                         }
129                         name = p+1;
130                 }
131                 if (fnmatch(pattern, name, fnmatch_flags) == 0)
132                         return 1;
133                 if (ex->match_flags & MATCHFLG_WILD2_PREFIX) {
134                         /* If the **-prefixed pattern has a '/' as the next
135                          * character, then try to match the rest of the
136                          * pattern at the root. */
137                         if (pattern[2] == '/' &&
138                             fnmatch(pattern+3, name, fnmatch_flags) == 0)
139                                 return 1;
140                 }
141                 else if (!match_start && ex->match_flags & MATCHFLG_WILD2) {
142                         /* A non-anchored match with an infix or trailing "**"
143                          * (but not a prefixed "**") needs to try matching
144                          * after every slash. */
145                         while ((name = strchr(name, '/')) != NULL) {
146                                 name++;
147                                 if (fnmatch(pattern, name, fnmatch_flags) == 0)
148                                         return 1;
149                         }
150                 }
151         } else if (match_start) {
152                 if (strcmp(name,pattern) == 0)
153                         return 1;
154         } else {
155                 int l1 = strlen(name);
156                 int l2 = strlen(pattern);
157                 if (l2 <= l1 &&
158                     strcmp(name+(l1-l2),pattern) == 0 &&
159                     (l1==l2 || name[l1-(l2+1)] == '/')) {
160                         return 1;
161                 }
162         }
163
164         return 0;
165 }
166
167
168 static void report_exclude_result(char const *name,
169                                   struct exclude_struct const *ent,
170                                   STRUCT_STAT const *st)
171 {
172         /* If a trailing slash is present to match only directories,
173          * then it is stripped out by make_exclude.  So as a special
174          * case we add it back in here. */
175
176         if (verbose >= 2)
177                 rprintf(FINFO, "%s %s %s because of pattern %s%s\n",
178                         ent->include ? "including" : "excluding",
179                         S_ISDIR(st->st_mode) ? "directory" : "file",
180                         name, ent->pattern,
181                         ent->directory ? "/" : "");
182 }
183
184
185 /*
186  * Return true if file NAME is defined to be excluded by either
187  * LOCAL_EXCLUDE_LIST or the globals EXCLUDE_LIST.
188  */
189 int check_exclude(char *name, struct exclude_struct **local_exclude_list,
190                   STRUCT_STAT *st)
191 {
192         int n;
193         struct exclude_struct *ent;
194
195         if (name && (name[0] == '.') && !name[1])
196                 /* never exclude '.', even if somebody does --exclude '*' */
197                 return 0;
198
199         if (exclude_list) {
200                 for (n=0; exclude_list[n]; n++) {
201                         ent = exclude_list[n];
202                         if (check_one_exclude(name, ent, st)) {
203                                 report_exclude_result(name, ent, st);
204                                 return !ent->include;
205                         }
206                 }
207         }
208
209         if (local_exclude_list) {
210                 for (n=0; local_exclude_list[n]; n++) {
211                         ent = local_exclude_list[n];
212                         if (check_one_exclude(name, ent, st)) {
213                                 report_exclude_result(name, ent, st);
214                                 return !ent->include;
215                         }
216                 }
217         }
218
219         return 0;
220 }
221
222
223 void add_exclude_list(const char *pattern, struct exclude_struct ***list, int include)
224 {
225         int len=0;
226         if (list && *list)
227                 for (; (*list)[len]; len++) ;
228
229         if (strcmp(pattern,"!") == 0) {
230                 if (verbose > 2)
231                         rprintf(FINFO,"clearing exclude list\n");
232                 while ((len)--) {
233                         free_exclude((*list)[len]);
234                 }
235                 free((*list));
236                 *list = NULL;
237                 return;
238         }
239
240         *list = (struct exclude_struct **)Realloc(*list,sizeof(struct exclude_struct *)*(len+2));
241
242         if (!*list || !((*list)[len] = make_exclude(pattern, include)))
243                 out_of_memory("add_exclude");
244
245         if (verbose > 2) {
246                 rprintf(FINFO,"add_exclude(%s,%s)\n",pattern,
247                         include ? "include" : "exclude");
248         }
249
250         (*list)[len+1] = NULL;
251 }
252
253 void add_exclude(const char *pattern, int include)
254 {
255         add_exclude_list(pattern,&exclude_list, include);
256 }
257
258 struct exclude_struct **make_exclude_list(const char *fname,
259                                           struct exclude_struct **list1,
260                                           int fatal, int include)
261 {
262         struct exclude_struct **list=list1;
263         int fd;
264         char line[MAXPATHLEN];
265         char *eob = line + MAXPATHLEN - 1;
266         extern int eol_nulls;
267
268         if (strcmp(fname, "-") != 0)
269                 fd = open(fname, O_RDONLY|O_BINARY);
270         else
271                 fd = 0;
272         if (fd < 0) {
273                 if (fatal) {
274                         rsyserr(FERROR, errno,
275                                 "failed to open %s file %s",
276                                 include ? "include" : "exclude",
277                                 fname);
278                         exit_cleanup(RERR_FILEIO);
279                 }
280                 return list;
281         }
282
283         while (1) {
284                 char ch, *s = line;
285                 int cnt;
286                 while (1) {
287                         if ((cnt = read(fd, &ch, 1)) <= 0) {
288                                 if (cnt < 0 && errno == EINTR)
289                                         continue;
290                                 break;
291                         }
292                         if (eol_nulls? !ch : (ch == '\n' || ch == '\r'))
293                                 break;
294                         if (s < eob)
295                                 *s++ = ch;
296                 }
297                 *s = '\0';
298                 if (*line && *line != ';' && *line != '#') {
299                         /* Skip lines starting with semicolon or pound.
300                          * It probably wouldn't cause any harm to not skip
301                          * them but there's no need to save them. */
302                         add_exclude_list(line,&list,include);
303                 }
304                 if (cnt <= 0)
305                         break;
306         }
307         close(fd);
308         return list;
309 }
310
311
312 void add_exclude_file(const char *fname, int fatal, int include)
313 {
314         if (!fname || !*fname) return;
315
316         exclude_list = make_exclude_list(fname,exclude_list,fatal,include);
317 }
318
319
320 void send_exclude_list(int f)
321 {
322         int i;
323         extern int remote_version;
324         extern int list_only, recurse;
325
326         /* This is a complete hack - blame Rusty.
327          *
328          * FIXME: This pattern shows up in the output of
329          * report_exclude_result(), which is not ideal. */
330         if (list_only && !recurse) {
331                 add_exclude("/*/*", 0);
332         }
333
334         if (!exclude_list) {
335                 write_int(f,0);
336                 return;
337         }
338
339         for (i=0;exclude_list[i];i++) {
340                 int l;
341                 char pattern[MAXPATHLEN];
342
343                 strlcpy(pattern,exclude_list[i]->pattern,sizeof(pattern));
344                 if (exclude_list[i]->directory) strlcat(pattern,"/", sizeof(pattern));
345
346                 l = strlen(pattern);
347                 if (l == 0) continue;
348                 if (exclude_list[i]->include) {
349                         if (remote_version < 19) {
350                                 rprintf(FERROR,"remote rsync does not support include syntax - aborting\n");
351                                 exit_cleanup(RERR_UNSUPPORTED);
352                         }
353                         write_int(f,l+2);
354                         write_buf(f,"+ ",2);
355                 } else {
356                         write_int(f,l);
357                 }
358                 write_buf(f,pattern,l);
359         }
360
361         write_int(f,0);
362 }
363
364
365 void recv_exclude_list(int f)
366 {
367         char line[MAXPATHLEN];
368         unsigned int l;
369
370         while ((l=read_int(f))) {
371                 if (l >= MAXPATHLEN) overflow("recv_exclude_list");
372                 read_sbuf(f,line,l);
373                 add_exclude(line,0);
374         }
375 }
376
377 /* Get the next include/exclude arg from the string. It works in a similar way
378 ** to strtok - initially an arg is sent over, from then on NULL. This
379 ** routine takes into account any +/- in the strings and does not
380 ** consider the space following it as a delimeter.
381 */
382 char *get_exclude_tok(char *p)
383 {
384         static char *s;
385         static int more;
386         char *t;
387
388         if (p) {
389                 s=p;
390                 if (*p)
391                         more=1;
392         }
393
394         if (!more)
395                 return(NULL);
396
397         /* Skip over any initial spaces */
398         while (isspace(* (unsigned char *) s))
399                 s++;
400
401         /* Are we at the end of the string? */
402         if (*s) {
403                 /* remember the beginning of the token */
404                 t=s;
405
406                 /* Is this a '+' or '-' followed by a space (not whitespace)? */
407                 if ((*s=='+' || *s=='-') && *(s+1)==' ')
408                         s+=2;
409
410                 /* Skip to the next space or the end of the string */
411                 while (!isspace(* (unsigned char *) s) && *s != '\0')
412                         s++;
413         } else {
414                 t=NULL;
415         }
416
417         /* Have we reached the end of the string? */
418         if (*s)
419                 *s++='\0';
420         else
421                 more=0;
422         return(t);
423 }
424
425
426 void add_exclude_line(char *p)
427 {
428         char *tok;
429         if (!p || !*p) return;
430         p = strdup(p);
431         if (!p) out_of_memory("add_exclude_line");
432         for (tok=get_exclude_tok(p); tok; tok=get_exclude_tok(NULL))
433                 add_exclude(tok, 0);
434         free(p);
435 }
436
437 void add_include_line(char *p)
438 {
439         char *tok;
440         if (!p || !*p) return;
441         p = strdup(p);
442         if (!p) out_of_memory("add_include_line");
443         for (tok=get_exclude_tok(p); tok; tok=get_exclude_tok(NULL))
444                 add_exclude(tok, 1);
445         free(p);
446 }
447
448
449 static char *cvs_ignore_list[] = {
450   "RCS/", "SCCS/", "CVS/", ".svn/", "CVS.adm", "RCSLOG", "cvslog.*",
451   "tags", "TAGS", ".make.state", ".nse_depinfo",
452   "*~", "#*", ".#*", ", *", "*.old", "*.bak", "*.BAK", "*.orig",
453   "*.rej", ".del-*", "*.a", "*.o", "*.obj", "*.so", "*.Z", "*.elc", "*.ln",
454   "core", NULL};
455
456
457 void add_cvs_excludes(void)
458 {
459         char fname[MAXPATHLEN];
460         char *p;
461         int i;
462
463         for (i=0; cvs_ignore_list[i]; i++)
464                 add_exclude(cvs_ignore_list[i], 0);
465
466         if ((p=getenv("HOME")) && strlen(p) < (MAXPATHLEN-12)) {
467                 snprintf(fname,sizeof(fname), "%s/.cvsignore",p);
468                 add_exclude_file(fname,0,0);
469         }
470
471         add_exclude_line(getenv("CVSIGNORE"));
472 }