Changed exclude/include matching so that normally wildcards will stop at
[rsync/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 "rsync.h"
24
25 extern int verbose;
26
27 static struct exclude_struct **exclude_list;
28
29 /*
30  * Optimization for special case when all included files are explicitly
31  *   listed without wildcards in the "exclude" list followed by a "- *"
32  *   to exclude the rest.
33  * Contributed by Dave Dykstra <dwd@bell-labs.com>
34  */
35 static int only_included_files = 1;
36 static struct exclude_struct *exclude_the_rest;
37
38 int send_included_file_names(int f,struct file_list *flist)
39 {
40         struct exclude_struct *ex, **ex_list;
41         int n;
42         char *p;
43
44         if (!only_included_files || (exclude_the_rest == NULL))
45                 return 0;
46
47         if (verbose > 1) {
48                 rprintf(FINFO,"(using include-only optimization) ");
49         }
50
51         /* set exclude_list to NULL temporarily so check_exclude */
52         /*   will always return true */
53         ex_list = exclude_list;
54         exclude_list = NULL;
55         for (n=0; (ex = ex_list[n]) != NULL; n++) {
56                 if (ex == exclude_the_rest)
57                         break;
58                 p = ex->pattern;
59                 while (*p == '/') {
60                         /* skip the allowed beginning slashes */
61                         p++;
62                 }
63                 /* silently skip files that don't exist to
64                    be more like non-optimized case */
65                 if (access(p,0) == 0)
66                         send_file_name(f,flist,p,0,0);
67         }
68         exclude_list = ex_list;
69         
70         return 1;
71 }
72
73 /* build an exclude structure given a exclude pattern */
74 static struct exclude_struct *make_exclude(char *pattern, int include)
75 {
76         struct exclude_struct *ret;
77
78         ret = (struct exclude_struct *)malloc(sizeof(*ret));
79         if (!ret) out_of_memory("make_exclude");
80
81         memset(ret, 0, sizeof(*ret));
82
83         if (strncmp(pattern,"- ",2) == 0) {
84                 pattern += 2;
85         } else if (strncmp(pattern,"+ ",2) == 0) {
86                 ret->include = 1;
87                 pattern += 2;
88         } else {
89                 ret->include = include;
90         }
91
92         ret->pattern = strdup(pattern);
93
94         if (!ret->pattern) out_of_memory("make_exclude");
95
96         if (strpbrk(pattern, "*[?")) {
97             if (!ret->include && (*pattern == '*') && (*(pattern+1) == '\0')) {
98                     exclude_the_rest = ret;
99             } else {
100                     only_included_files = 0;
101             }
102             ret->regular_exp = 1;
103             ret->fnmatch_flags = strstr(pattern, "**") ? 0 : FNM_PATHNAME;
104         } else if (!ret->include) {
105                 only_included_files = 0;
106         }
107
108         if (strlen(pattern) > 1 && pattern[strlen(pattern)-1] == '/') {
109                 ret->pattern[strlen(pattern)-1] = 0;
110                 ret->directory = 1;
111         }
112
113         if (!strchr(ret->pattern,'/')) {
114                 ret->local = 1;
115         }
116
117         return ret;
118 }
119
120 static void free_exclude(struct exclude_struct *ex)
121 {
122         free(ex->pattern);
123         memset(ex,0,sizeof(*ex));
124         free(ex);
125 }
126
127 static int check_one_exclude(char *name,struct exclude_struct *ex,
128                              STRUCT_STAT *st)
129 {
130         char *p;
131         int match_start=0;
132         char *pattern = ex->pattern;
133
134         if (ex->local && (p=strrchr(name,'/')))
135                 name = p+1;
136
137         if (!name[0]) return 0;
138
139         if (ex->directory && !S_ISDIR(st->st_mode)) return 0;
140
141         if (*pattern == '/' && *name != '/') {
142                 match_start = 1;
143                 pattern++;
144         }
145
146         if (ex->regular_exp) {
147                 if (fnmatch(pattern, name, ex->fnmatch_flags) == 0)
148                         return 1;
149         } else {
150                 int l1 = strlen(name);
151                 int l2 = strlen(pattern);
152                 if (l2 <= l1 && 
153                     strcmp(name+(l1-l2),pattern) == 0 &&
154                     (l1==l2 || (!match_start && name[l1-(l2+1)] == '/')))
155                         return 1;
156         }
157
158         return 0;
159 }
160
161
162 int check_exclude(char *name,struct exclude_struct **local_exclude_list,
163                   STRUCT_STAT *st)
164 {
165         int n;
166
167         if (name && (name[0] == '.') && !name[1])
168                 /* never exclude '.', even if somebody does --exclude '*' */
169                 return 0;
170
171         if (exclude_list) {
172                 for (n=0; exclude_list[n]; n++)
173                         if (check_one_exclude(name,exclude_list[n],st))
174                                 return !exclude_list[n]->include;
175         }
176
177         if (local_exclude_list) {
178                 for (n=0; local_exclude_list[n]; n++)
179                         if (check_one_exclude(name,local_exclude_list[n],st))
180                                 return !local_exclude_list[n]->include;
181         }
182
183         return 0;
184 }
185
186
187 void add_exclude_list(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                 only_included_files = 1;
202                 exclude_the_rest = NULL;
203                 return;
204         }
205
206         *list = (struct exclude_struct **)Realloc(*list,sizeof(struct exclude_struct *)*(len+2));
207         
208         if (!*list || !((*list)[len] = make_exclude(pattern, include)))
209                 out_of_memory("add_exclude");
210         
211         if (verbose > 2)
212                 rprintf(FINFO,"add_exclude(%s)\n",pattern);
213         
214         (*list)[len+1] = NULL;
215 }
216
217 void add_exclude(char *pattern, int include)
218 {
219         add_exclude_list(pattern,&exclude_list, include);
220 }
221
222 struct exclude_struct **make_exclude_list(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                         rprintf(FERROR,"%s : %s\n",fname,strerror(errno));
232                         exit_cleanup(RERR_FILEIO);
233                 }
234                 return list;
235         }
236
237         while (fgets(line,MAXPATHLEN,f)) {
238                 int l = strlen(line);
239                 if (l && line[l-1] == '\n') l--;
240                 line[l] = 0;
241                 if (line[0] && (line[0] != ';') && (line[0] != '#')) {
242                         /* Skip lines starting with semicolon or pound.
243                            It probably wouldn't cause any harm to not skip
244                              them but there's no need to save them. */
245                         add_exclude_list(line,&list,include);
246                 }
247         }
248         fclose(f);
249         return list;
250 }
251
252
253 void add_exclude_file(char *fname,int fatal,int include)
254 {
255         if (!fname || !*fname) return;
256
257         exclude_list = make_exclude_list(fname,exclude_list,fatal,include);
258 }
259
260
261 void send_exclude_list(int f)
262 {
263         int i;
264         extern int remote_version;
265
266         if (!exclude_list) {
267                 write_int(f,0);
268                 return;
269         }
270
271         for (i=0;exclude_list[i];i++) {
272                 char *pattern = exclude_list[i]->pattern; 
273                 int l;
274
275                 l = strlen(pattern);
276                 if (l == 0) continue;
277                 if (exclude_list[i]->include) {
278                         if (remote_version < 19) {
279                                 rprintf(FERROR,"remote rsync does not support include syntax - aborting\n");
280                                 exit_cleanup(RERR_UNSUPPORTED);
281                         }
282                         write_int(f,l+2);
283                         write_buf(f,"+ ",2);
284                 } else {
285                         write_int(f,l);
286                 }
287                 write_buf(f,pattern,l);
288         }    
289
290         write_int(f,0);
291 }
292
293
294 void recv_exclude_list(int f)
295 {
296         char line[MAXPATHLEN];
297         int l;
298         while ((l=read_int(f))) {
299                 if (l >= MAXPATHLEN) overflow("recv_exclude_list");
300                 read_sbuf(f,line,l);
301                 add_exclude(line,0);
302         }
303 }
304
305
306 void add_exclude_line(char *p)
307 {
308         char *tok;
309         if (!p || !*p) return;
310         p = strdup(p);
311         if (!p) out_of_memory("add_exclude_line");
312         for (tok=strtok(p," "); tok; tok=strtok(NULL," "))
313                 add_exclude(tok, 0);
314         free(p);
315 }
316
317 void add_include_line(char *p)
318 {
319         char *tok;
320         if (!p || !*p) return;
321         p = strdup(p);
322         if (!p) out_of_memory("add_include_line");
323         for (tok=strtok(p," "); tok; tok=strtok(NULL," "))
324                 add_exclude(tok, 1);
325         free(p);
326 }
327
328
329 static char *cvs_ignore_list[] = {
330   "RCS","SCCS","CVS","CVS.adm","RCSLOG","cvslog.*",
331   "tags","TAGS",".make.state",".nse_depinfo",
332   "*~", "#*", ".#*", ",*", "*.old", "*.bak", "*.BAK", "*.orig",
333   "*.rej", ".del-*", "*.a", "*.o", "*.obj", "*.so", "*.Z", "*.elc", "*.ln",
334   "core",NULL};
335
336
337
338 void add_cvs_excludes(void)
339 {
340         char fname[MAXPATHLEN];
341         char *p;
342         int i;
343   
344         for (i=0; cvs_ignore_list[i]; i++)
345                 add_exclude(cvs_ignore_list[i], 0);
346
347         if ((p=getenv("HOME")) && strlen(p) < (MAXPATHLEN-12)) {
348                 slprintf(fname,sizeof(fname), "%s/.cvsignore",p);
349                 add_exclude_file(fname,0,0);
350         }
351
352         add_exclude_line(getenv("CVSIGNORE"));
353 }