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