Changed the optimized include mode (which kicks in when there are a series
[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         } else if (!ret->include) {
104                 only_included_files = 0;
105         }
106
107         if (strlen(pattern) > 1 && pattern[strlen(pattern)-1] == '/') {
108                 ret->pattern[strlen(pattern)-1] = 0;
109                 ret->directory = 1;
110         }
111
112         if (!strchr(ret->pattern,'/')) {
113                 ret->local = 1;
114         }
115
116         return ret;
117 }
118
119 static void free_exclude(struct exclude_struct *ex)
120 {
121         free(ex->pattern);
122         memset(ex,0,sizeof(*ex));
123         free(ex);
124 }
125
126 static int check_one_exclude(char *name,struct exclude_struct *ex,
127                              STRUCT_STAT *st)
128 {
129         char *p;
130         int match_start=0;
131         char *pattern = ex->pattern;
132
133         if (ex->local && (p=strrchr(name,'/')))
134                 name = p+1;
135
136         if (!name[0]) return 0;
137
138         if (ex->directory && !S_ISDIR(st->st_mode)) return 0;
139
140         if (*pattern == '/' && *name != '/') {
141                 match_start = 1;
142                 pattern++;
143         }
144
145         if (ex->regular_exp) {
146                 if (fnmatch(pattern, name, 0) == 0)
147                         return 1;
148         } else {
149                 int l1 = strlen(name);
150                 int l2 = strlen(pattern);
151                 if (l2 <= l1 && 
152                     strcmp(name+(l1-l2),pattern) == 0 &&
153                     (l1==l2 || (!match_start && name[l1-(l2+1)] == '/')))
154                         return 1;
155         }
156
157         return 0;
158 }
159
160
161 int check_exclude(char *name,struct exclude_struct **local_exclude_list,
162                   STRUCT_STAT *st)
163 {
164         int n;
165
166         if (name && (name[0] == '.') && !name[1])
167                 /* never exclude '.', even if somebody does --exclude '*' */
168                 return 0;
169
170         if (exclude_list) {
171                 for (n=0; exclude_list[n]; n++)
172                         if (check_one_exclude(name,exclude_list[n],st))
173                                 return !exclude_list[n]->include;
174         }
175
176         if (local_exclude_list) {
177                 for (n=0; local_exclude_list[n]; n++)
178                         if (check_one_exclude(name,local_exclude_list[n],st))
179                                 return !local_exclude_list[n]->include;
180         }
181
182         return 0;
183 }
184
185
186 void add_exclude_list(char *pattern,struct exclude_struct ***list, int include)
187 {
188         int len=0;
189         if (list && *list)
190                 for (; (*list)[len]; len++) ;
191
192         if (strcmp(pattern,"!") == 0) {
193                 if (verbose > 2)
194                         rprintf(FINFO,"clearing exclude list\n");
195                 while ((len)--) {
196                         free_exclude((*list)[len]);
197                 }
198                 free((*list));
199                 *list = NULL;
200                 only_included_files = 1;
201                 exclude_the_rest = NULL;
202                 return;
203         }
204
205         *list = (struct exclude_struct **)Realloc(*list,sizeof(struct exclude_struct *)*(len+2));
206         
207         if (!*list || !((*list)[len] = make_exclude(pattern, include)))
208                 out_of_memory("add_exclude");
209         
210         if (verbose > 2)
211                 rprintf(FINFO,"add_exclude(%s)\n",pattern);
212         
213         (*list)[len+1] = NULL;
214 }
215
216 void add_exclude(char *pattern, int include)
217 {
218         add_exclude_list(pattern,&exclude_list, include);
219 }
220
221 struct exclude_struct **make_exclude_list(char *fname,
222                                           struct exclude_struct **list1,
223                                           int fatal, int include)
224 {
225         struct exclude_struct **list=list1;
226         FILE *f = fopen(fname,"r");
227         char line[MAXPATHLEN];
228         if (!f) {
229                 if (fatal) {
230                         rprintf(FERROR,"%s : %s\n",fname,strerror(errno));
231                         exit_cleanup(RERR_FILEIO);
232                 }
233                 return list;
234         }
235
236         while (fgets(line,MAXPATHLEN,f)) {
237                 int l = strlen(line);
238                 if (l && line[l-1] == '\n') l--;
239                 line[l] = 0;
240                 if (line[0] && (line[0] != ';') && (line[0] != '#')) {
241                         /* Skip lines starting with semicolon or pound.
242                            It probably wouldn't cause any harm to not skip
243                              them but there's no need to save them. */
244                         add_exclude_list(line,&list,include);
245                 }
246         }
247         fclose(f);
248         return list;
249 }
250
251
252 void add_exclude_file(char *fname,int fatal,int include)
253 {
254         if (!fname || !*fname) return;
255
256         exclude_list = make_exclude_list(fname,exclude_list,fatal,include);
257 }
258
259
260 void send_exclude_list(int f)
261 {
262         int i;
263         extern int remote_version;
264
265         if (!exclude_list) {
266                 write_int(f,0);
267                 return;
268         }
269
270         for (i=0;exclude_list[i];i++) {
271                 char *pattern = exclude_list[i]->pattern; 
272                 int l;
273
274                 l = strlen(pattern);
275                 if (l == 0) continue;
276                 if (exclude_list[i]->include) {
277                         if (remote_version < 19) {
278                                 rprintf(FERROR,"remote rsync does not support include syntax - aborting\n");
279                                 exit_cleanup(RERR_UNSUPPORTED);
280                         }
281                         write_int(f,l+2);
282                         write_buf(f,"+ ",2);
283                 } else {
284                         write_int(f,l);
285                 }
286                 write_buf(f,pattern,l);
287         }    
288
289         write_int(f,0);
290 }
291
292
293 void recv_exclude_list(int f)
294 {
295         char line[MAXPATHLEN];
296         int l;
297         while ((l=read_int(f))) {
298                 if (l >= MAXPATHLEN) overflow("recv_exclude_list");
299                 read_sbuf(f,line,l);
300                 add_exclude(line,0);
301         }
302 }
303
304
305 void add_exclude_line(char *p)
306 {
307         char *tok;
308         if (!p || !*p) return;
309         p = strdup(p);
310         if (!p) out_of_memory("add_exclude_line");
311         for (tok=strtok(p," "); tok; tok=strtok(NULL," "))
312                 add_exclude(tok, 0);
313         free(p);
314 }
315
316 void add_include_line(char *p)
317 {
318         char *tok;
319         if (!p || !*p) return;
320         p = strdup(p);
321         if (!p) out_of_memory("add_include_line");
322         for (tok=strtok(p," "); tok; tok=strtok(NULL," "))
323                 add_exclude(tok, 1);
324         free(p);
325 }
326
327
328 static char *cvs_ignore_list[] = {
329   "RCS","SCCS","CVS","CVS.adm","RCSLOG","cvslog.*",
330   "tags","TAGS",".make.state",".nse_depinfo",
331   "*~", "#*", ".#*", ",*", "*.old", "*.bak", "*.BAK", "*.orig",
332   "*.rej", ".del-*", "*.a", "*.o", "*.obj", "*.so", "*.Z", "*.elc", "*.ln",
333   "core",NULL};
334
335
336
337 void add_cvs_excludes(void)
338 {
339         char fname[MAXPATHLEN];
340         char *p;
341         int i;
342   
343         for (i=0; cvs_ignore_list[i]; i++)
344                 add_exclude(cvs_ignore_list[i], 0);
345
346         if ((p=getenv("HOME")) && strlen(p) < (MAXPATHLEN-12)) {
347                 slprintf(fname,sizeof(fname), "%s/.cvsignore",p);
348                 add_exclude_file(fname,0,0);
349         }
350
351         add_exclude_line(getenv("CVSIGNORE"));
352 }