cbf6105f1da4c07cf993efca71fa12bdf21927f5
[rsync.git] / exclude.c
1 /* 
2    Copyright (C) Andrew Tridgell 1996
3    Copyright (C) Paul Mackerras 1996
4    
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9    
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14    
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 /* a lot of this stuff was originally derived from GNU tar, although
21    it has now changed so much that it is hard to tell :) */
22
23 /* include/exclude cluestick added by Martin Pool <mbp@samba.org> */
24
25 #include "rsync.h"
26
27 extern int verbose;
28 extern int delete_mode;
29
30 static struct exclude_struct **exclude_list;
31
32 /* build an exclude structure given a exclude pattern */
33 static struct exclude_struct *make_exclude(char *pattern, int include)
34 {
35         struct exclude_struct *ret;
36
37         ret = (struct exclude_struct *)malloc(sizeof(*ret));
38         if (!ret) out_of_memory("make_exclude");
39
40         memset(ret, 0, sizeof(*ret));
41
42         if (strncmp(pattern,"- ",2) == 0) {
43                 pattern += 2;
44         } else if (strncmp(pattern,"+ ",2) == 0) {
45                 ret->include = 1;
46                 pattern += 2;
47         } else {
48                 ret->include = include;
49         }
50
51         ret->pattern = strdup(pattern);
52
53         if (!ret->pattern) out_of_memory("make_exclude");
54
55         if (strpbrk(pattern, "*[?")) {
56             ret->regular_exp = 1;
57             ret->fnmatch_flags = FNM_PATHNAME;
58             if (strstr(pattern, "**")) {
59                     static int tested;
60                     if (!tested) {
61                             tested = 1;
62                             if (fnmatch("a/b/*", "a/b/c/d", FNM_PATHNAME)==0) {
63                                     rprintf(FERROR,"WARNING: fnmatch FNM_PATHNAME is broken on your system\n");
64                             }
65                     }
66                     ret->fnmatch_flags = 0;
67             }
68         }
69
70         if (strlen(pattern) > 1 && pattern[strlen(pattern)-1] == '/') {
71                 ret->pattern[strlen(pattern)-1] = 0;
72                 ret->directory = 1;
73         }
74
75         if (!strchr(ret->pattern,'/')) {
76                 ret->local = 1;
77         }
78
79         return ret;
80 }
81
82 static void free_exclude(struct exclude_struct *ex)
83 {
84         free(ex->pattern);
85         memset(ex,0,sizeof(*ex));
86         free(ex);
87 }
88
89 static int check_one_exclude(char *name, struct exclude_struct *ex,
90                              STRUCT_STAT *st)
91 {
92         char *p;
93         int match_start=0;
94         char *pattern = ex->pattern;
95
96         if (ex->local && (p=strrchr(name,'/')))
97                 name = p+1;
98
99         if (!name[0]) return 0;
100
101         if (ex->directory && !S_ISDIR(st->st_mode)) return 0;
102
103         if (*pattern == '/' && *name != '/') {
104                 match_start = 1;
105                 pattern++;
106         }
107
108         if (ex->regular_exp) {
109                 if (fnmatch(pattern, name, ex->fnmatch_flags) == 0) {
110                         return 1;
111                 }
112         } else {
113                 int l1 = strlen(name);
114                 int l2 = strlen(pattern);
115                 if (l2 <= l1 && 
116                     strcmp(name+(l1-l2),pattern) == 0 &&
117                     (l1==l2 || (!match_start && name[l1-(l2+1)] == '/'))) {
118                         return 1;
119                 }
120         }
121
122         return 0;
123 }
124
125
126 static void report_exclude_result(char const *name,
127                                   struct exclude_struct const *ent,
128                                   STRUCT_STAT const *st)
129 {
130         /* If a trailing slash is present to match only directories,
131          * then it is stripped out by make_exclude.  So as a special
132          * case we add it back in here. */
133         
134         if (verbose >= 2)
135                 rprintf(FINFO, "%s %s %s because of pattern %s%s\n",
136                         ent->include ? "including" : "excluding",
137                         S_ISDIR(st->st_mode) ? "directory" : "file",
138                         name, ent->pattern,
139                         ent->directory ? "/" : "");
140 }
141
142
143 /*
144  * Return true if file NAME is defined to be excluded by either
145  * LOCAL_EXCLUDE_LIST or the globals EXCLUDE_LIST.
146  */
147 int check_exclude(char *name, struct exclude_struct **local_exclude_list,
148                   STRUCT_STAT *st)
149 {
150         int n;
151         struct exclude_struct *ent;
152
153         if (name && (name[0] == '.') && !name[1])
154                 /* never exclude '.', even if somebody does --exclude '*' */
155                 return 0;
156
157         if (exclude_list) {
158                 for (n=0; exclude_list[n]; n++) {
159                         ent = exclude_list[n];
160                         if (check_one_exclude(name, ent, st)) {
161                                 report_exclude_result(name, ent, st);
162                                 return !ent->include;
163                         }
164                 }
165         }
166
167         if (local_exclude_list) {
168                 for (n=0; local_exclude_list[n]; n++) {
169                         ent = exclude_list[n];
170                         if (check_one_exclude(name, ent, st)) {
171                                 report_exclude_result(name, ent, st);
172                                 return !ent->include;
173                         }
174                 }
175         }
176
177         return 0;
178 }
179
180
181 void add_exclude_list(char *pattern,struct exclude_struct ***list, int include)
182 {
183         int len=0;
184         if (list && *list)
185                 for (; (*list)[len]; len++) ;
186
187         if (strcmp(pattern,"!") == 0) {
188                 if (verbose > 2)
189                         rprintf(FINFO,"clearing exclude list\n");
190                 while ((len)--) {
191                         free_exclude((*list)[len]);
192                 }
193                 free((*list));
194                 *list = NULL;
195                 return;
196         }
197
198         *list = (struct exclude_struct **)Realloc(*list,sizeof(struct exclude_struct *)*(len+2));
199         
200         if (!*list || !((*list)[len] = make_exclude(pattern, include)))
201                 out_of_memory("add_exclude");
202         
203         if (verbose > 2)
204                 rprintf(FINFO,"add_exclude(%s)\n",pattern);
205         
206         (*list)[len+1] = NULL;
207 }
208
209 void add_exclude(char *pattern, int include)
210 {
211         add_exclude_list(pattern,&exclude_list, include);
212 }
213
214 struct exclude_struct **make_exclude_list(char *fname,
215                                           struct exclude_struct **list1,
216                                           int fatal, int include)
217 {
218         struct exclude_struct **list=list1;
219         FILE *f = fopen(fname,"r");
220         char line[MAXPATHLEN];
221         if (!f) {
222                 if (fatal) {
223                         rsyserr(FERROR, errno,
224                                 "failed to open %s file %s",
225                                 include ? "include" : "exclude",
226                                 fname);
227                         exit_cleanup(RERR_FILEIO);
228                 }
229                 return list;
230         }
231
232         while (fgets(line,MAXPATHLEN,f)) {
233                 int l = strlen(line);
234                 if (l && line[l-1] == '\n') l--;
235                 line[l] = 0;
236                 if (line[0] && (line[0] != ';') && (line[0] != '#')) {
237                         /* Skip lines starting with semicolon or pound.
238                            It probably wouldn't cause any harm to not skip
239                              them but there's no need to save them. */
240                         add_exclude_list(line,&list,include);
241                 }
242         }
243         fclose(f);
244         return list;
245 }
246
247
248 void add_exclude_file(char *fname,int fatal,int include)
249 {
250         if (!fname || !*fname) return;
251
252         exclude_list = make_exclude_list(fname,exclude_list,fatal,include);
253 }
254
255
256 void send_exclude_list(int f)
257 {
258         int i;
259         extern int remote_version;
260         extern int list_only, recurse;
261
262         /* this is a complete hack - blame Rusty */
263         if (list_only && !recurse) {
264                 add_exclude("/*/*", 0);
265         }
266
267         if (!exclude_list) {
268                 write_int(f,0);
269                 return;
270         }
271
272         for (i=0;exclude_list[i];i++) {
273                 int l;
274                 char pattern[MAXPATHLEN];
275
276                 strlcpy(pattern,exclude_list[i]->pattern,sizeof(pattern)); 
277                 if (exclude_list[i]->directory) strlcat(pattern,"/", sizeof(pattern));
278
279                 l = strlen(pattern);
280                 if (l == 0) continue;
281                 if (exclude_list[i]->include) {
282                         if (remote_version < 19) {
283                                 rprintf(FERROR,"remote rsync does not support include syntax - aborting\n");
284                                 exit_cleanup(RERR_UNSUPPORTED);
285                         }
286                         write_int(f,l+2);
287                         write_buf(f,"+ ",2);
288                 } else {
289                         write_int(f,l);
290                 }
291                 write_buf(f,pattern,l);
292         }    
293
294         write_int(f,0);
295 }
296
297
298 void recv_exclude_list(int f)
299 {
300         char line[MAXPATHLEN];
301         int l;
302         while ((l=read_int(f))) {
303                 if (l >= MAXPATHLEN) overflow("recv_exclude_list");
304                 read_sbuf(f,line,l);
305                 add_exclude(line,0);
306         }
307 }
308
309 /* Get the next include/exclude arg from the string. It works in a similar way
310 ** to strtok - initially an arg is sent over, from then on NULL. This
311 ** routine takes into account any +/- in the strings and does not
312 ** consider the space following it as a delimeter.
313 */
314 char *get_exclude_tok(char *p)
315 {
316         static char *s;
317         static int more;
318         char *t;
319
320         if (p) {
321                 s=p;
322                 if (*p)
323                         more=1;
324         }
325
326         if (!more)
327                 return(NULL);
328
329         /* Skip over any initial spaces */
330         while(isspace(*s))
331                 s++;
332
333         /* Are we at the end of the string? */
334         if (*s) {
335                 /* remember the beginning of the token */
336                 t=s;
337
338                 /* Is this a '+' or '-' followed by a space (not whitespace)? */
339                 if ((*s=='+' || *s=='-') && *(s+1)==' ')
340                         s+=2;
341         
342                 /* Skip to the next space or the end of the string */
343                 while(!isspace(*s) && *s!='\0')
344                         s++;
345         } else {
346                 t=NULL;
347         }
348
349         /* Have we reached the end of the string? */
350         if (*s)
351                 *s++='\0';
352         else
353                 more=0;
354         return(t);
355 }
356
357         
358 void add_exclude_line(char *p)
359 {
360         char *tok;
361         if (!p || !*p) return;
362         p = strdup(p);
363         if (!p) out_of_memory("add_exclude_line");
364         for (tok=get_exclude_tok(p); tok; tok=get_exclude_tok(NULL))
365                 add_exclude(tok, 0);
366         free(p);
367 }
368
369 void add_include_line(char *p)
370 {
371         char *tok;
372         if (!p || !*p) return;
373         p = strdup(p);
374         if (!p) out_of_memory("add_include_line");
375         for (tok=get_exclude_tok(p); tok; tok=get_exclude_tok(NULL))
376                 add_exclude(tok, 1);
377         free(p);
378 }
379
380
381 static char *cvs_ignore_list[] = {
382   "RCS","SCCS","CVS","CVS.adm","RCSLOG","cvslog.*",
383   "tags","TAGS",".make.state",".nse_depinfo",
384   "*~", "#*", ".#*", ",*", "*.old", "*.bak", "*.BAK", "*.orig",
385   "*.rej", ".del-*", "*.a", "*.o", "*.obj", "*.so", "*.Z", "*.elc", "*.ln",
386   "core",NULL};
387
388
389
390 void add_cvs_excludes(void)
391 {
392         char fname[MAXPATHLEN];
393         char *p;
394         int i;
395   
396         for (i=0; cvs_ignore_list[i]; i++)
397                 add_exclude(cvs_ignore_list[i], 0);
398
399         if ((p=getenv("HOME")) && strlen(p) < (MAXPATHLEN-12)) {
400                 slprintf(fname,sizeof(fname), "%s/.cvsignore",p);
401                 add_exclude_file(fname,0,0);
402         }
403
404         add_exclude_line(getenv("CVSIGNORE"));
405 }