patch from Alberto Accomazzi <aaccomazzi@cfa.harvard.edu> to add
[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                 send_file_name(f,flist,p,0,0);
64         }
65         exclude_list = ex_list;
66         
67         return 1;
68 }
69
70 /* build an exclude structure given a exclude pattern */
71 static struct exclude_struct *make_exclude(char *pattern, int include)
72 {
73         struct exclude_struct *ret;
74
75         ret = (struct exclude_struct *)malloc(sizeof(*ret));
76         if (!ret) out_of_memory("make_exclude");
77
78         memset(ret, 0, sizeof(*ret));
79
80         if (strncmp(pattern,"- ",2) == 0) {
81                 pattern += 2;
82         } else if (strncmp(pattern,"+ ",2) == 0) {
83                 ret->include = 1;
84                 pattern += 2;
85         } else {
86                 ret->include = include;
87         }
88
89         ret->pattern = strdup(pattern);
90
91         if (!ret->pattern) out_of_memory("make_exclude");
92
93         if (strpbrk(pattern, "*[?")) {
94             if (!ret->include && (*pattern == '*') && (*(pattern+1) == '\0')) {
95                     exclude_the_rest = ret;
96             } else {
97                     only_included_files = 0;
98             }
99             ret->regular_exp = 1;
100         } else if (!ret->include) {
101                 only_included_files = 0;
102         }
103
104         if (strlen(pattern) > 1 && pattern[strlen(pattern)-1] == '/') {
105                 ret->pattern[strlen(pattern)-1] = 0;
106                 ret->directory = 1;
107         }
108
109         if (!strchr(ret->pattern,'/')) {
110                 ret->local = 1;
111         }
112
113         return ret;
114 }
115
116 static void free_exclude(struct exclude_struct *ex)
117 {
118         free(ex->pattern);
119         memset(ex,0,sizeof(*ex));
120         free(ex);
121 }
122
123 static int check_one_exclude(char *name,struct exclude_struct *ex,
124                              STRUCT_STAT *st)
125 {
126         char *p;
127         int match_start=0;
128         char *pattern = ex->pattern;
129
130         if (ex->local && (p=strrchr(name,'/')))
131                 name = p+1;
132
133         if (!name[0]) return 0;
134
135         if (ex->directory && !S_ISDIR(st->st_mode)) return 0;
136
137         if (*pattern == '/' && *name != '/') {
138                 match_start = 1;
139                 pattern++;
140         }
141
142         if (ex->regular_exp) {
143                 if (fnmatch(pattern, name, 0) == 0)
144                         return 1;
145         } else {
146                 int l1 = strlen(name);
147                 int l2 = strlen(pattern);
148                 if (l2 <= l1 && 
149                     strcmp(name+(l1-l2),pattern) == 0 &&
150                     (l1==l2 || (!match_start && name[l1-(l2+1)] == '/')))
151                         return 1;
152         }
153
154         return 0;
155 }
156
157
158 int check_exclude(char *name,struct exclude_struct **local_exclude_list,
159                   STRUCT_STAT *st)
160 {
161         int n;
162
163         if (exclude_list) {
164                 for (n=0; exclude_list[n]; n++)
165                         if (check_one_exclude(name,exclude_list[n],st))
166                                 return !exclude_list[n]->include;
167         }
168
169         if (local_exclude_list) {
170                 for (n=0; local_exclude_list[n]; n++)
171                         if (check_one_exclude(name,local_exclude_list[n],st))
172                                 return !local_exclude_list[n]->include;
173         }
174
175         return 0;
176 }
177
178
179 void add_exclude_list(char *pattern,struct exclude_struct ***list, int include)
180 {
181         int len=0;
182         if (list && *list)
183                 for (; (*list)[len]; len++) ;
184
185         if (strcmp(pattern,"!") == 0) {
186                 if (verbose > 2)
187                         rprintf(FINFO,"clearing exclude list\n");
188                 while ((len)--) {
189                         free_exclude((*list)[len]);
190                 }
191                 free((*list));
192                 *list = NULL;
193                 only_included_files = 1;
194                 exclude_the_rest = 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                         rprintf(FERROR,"%s : %s\n",fname,strerror(errno));
224                         exit_cleanup(RERR_FILEIO);
225                 }
226                 return list;
227         }
228
229         while (fgets(line,MAXPATHLEN,f)) {
230                 int l = strlen(line);
231                 if (l && line[l-1] == '\n') l--;
232                 line[l] = 0;
233                 if (line[0]) add_exclude_list(line,&list,include);
234         }
235         fclose(f);
236         return list;
237 }
238
239
240 void add_exclude_file(char *fname,int fatal,int include)
241 {
242         if (!fname || !*fname) return;
243
244         exclude_list = make_exclude_list(fname,exclude_list,fatal,include);
245 }
246
247
248 void send_exclude_list(int f)
249 {
250         int i;
251         extern int remote_version;
252
253         if (!exclude_list) {
254                 write_int(f,0);
255                 return;
256         }
257
258         for (i=0;exclude_list[i];i++) {
259                 char *pattern = exclude_list[i]->pattern; 
260                 int l;
261
262                 l = strlen(pattern);
263                 if (l == 0) continue;
264                 if (exclude_list[i]->include) {
265                         if (remote_version < 19) {
266                                 rprintf(FERROR,"remote rsync does not support include syntax - aborting\n");
267                                 exit_cleanup(RERR_NOSUPPORT);
268                         }
269                         write_int(f,l+2);
270                         write_buf(f,"+ ",2);
271                 } else {
272                         write_int(f,l);
273                 }
274                 write_buf(f,pattern,l);
275         }    
276
277         write_int(f,0);
278 }
279
280
281 void recv_exclude_list(int f)
282 {
283         char line[MAXPATHLEN];
284         int l;
285         while ((l=read_int(f))) {
286                 if (l >= MAXPATHLEN) overflow("recv_exclude_list");
287                 read_sbuf(f,line,l);
288                 add_exclude(line,0);
289         }
290 }
291
292
293 void add_exclude_line(char *p)
294 {
295         char *tok;
296         if (!p || !*p) return;
297         p = strdup(p);
298         if (!p) out_of_memory("add_exclude_line");
299         for (tok=strtok(p," "); tok; tok=strtok(NULL," "))
300                 add_exclude(tok, 0);
301         free(p);
302 }
303
304
305 static char *cvs_ignore_list[] = {
306   "RCS","SCCS","CVS","CVS.adm","RCSLOG","cvslog.*",
307   "tags","TAGS",".make.state",".nse_depinfo",
308   "*~", "#*", ".#*", ",*", "*.old", "*.bak", "*.BAK", "*.orig",
309   "*.rej", ".del-*", "*.a", "*.o", "*.obj", "*.so", "*.Z", "*.elc", "*.ln",
310   "core",NULL};
311
312
313
314 void add_cvs_excludes(void)
315 {
316         char fname[MAXPATHLEN];
317         char *p;
318         int i;
319   
320         for (i=0; cvs_ignore_list[i]; i++)
321                 add_exclude(cvs_ignore_list[i], 0);
322
323         if ((p=getenv("HOME")) && strlen(p) < (MAXPATHLEN-12)) {
324                 slprintf(fname,sizeof(fname)-1, "%s/.cvsignore",p);
325                 add_exclude_file(fname,0,0);
326         }
327
328         add_exclude_line(getenv("CVSIGNORE"));
329 }