added support for --include, --include-from and the +/- syntax
[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 /* build an exclude structure given a exclude pattern */
30 static struct exclude_struct *make_exclude(char *pattern, int include)
31 {
32         struct exclude_struct *ret;
33
34         ret = (struct exclude_struct *)malloc(sizeof(*ret));
35         if (!ret) out_of_memory("make_exclude");
36
37         memset(ret, 0, sizeof(*ret));
38
39         ret->orig = strdup(pattern);
40
41         if (strncmp(pattern,"- ",2) == 0) {
42                 pattern += 2;
43         } else if (strncmp(pattern,"+ ",2) == 0) {
44                 ret->include = 1;
45                 pattern += 2;
46         } else {
47                 ret->include = include;
48         }
49
50         ret->pattern = strdup(pattern);
51
52         if (!ret->orig || !ret->pattern) out_of_memory("make_exclude");
53
54         if (strpbrk(pattern, "*[?")) ret->regular_exp = 1;
55
56         if (strlen(pattern) > 1 && pattern[strlen(pattern)-1] == '/') {
57                 ret->pattern[strlen(pattern)-1] = 0;
58                 ret->directory = 1;
59         }
60
61         if (!strchr(ret->pattern,'/')) {
62                 ret->local = 1;
63         }
64
65         return ret;
66 }
67
68 static void free_exclude(struct exclude_struct *ex)
69 {
70         free(ex->orig);
71         free(ex->pattern);
72         memset(ex,0,sizeof(*ex));
73         free(ex);
74 }
75
76 static int check_one_exclude(char *name,struct exclude_struct *ex,
77                              STRUCT_STAT *st)
78 {
79         char *p;
80         int match_start=0;
81         char *pattern = ex->pattern;
82
83         if (ex->local && (p=strrchr(name,'/')))
84                 name = p+1;
85
86         if (!name[0]) return 0;
87
88         if (ex->directory && !S_ISDIR(st->st_mode)) return 0;
89
90         if (*pattern == '/' && *name != '/') {
91                 match_start = 1;
92                 pattern++;
93         }
94
95         if (ex->regular_exp) {
96                 if (fnmatch(ex->pattern, name, 0) == 0)
97                         return 1;
98         } else {
99                 int l1 = strlen(name);
100                 int l2 = strlen(ex->pattern);
101                 if (l2 <= l1 && 
102                     strcmp(name+(l1-l2),ex->pattern) == 0 &&
103                     (l1==l2 || (!match_start && name[l1-(l2+1)] == '/')))
104                         return 1;
105         }
106
107         return 0;
108 }
109
110
111 int check_exclude(char *name,struct exclude_struct **local_exclude_list,
112                   STRUCT_STAT *st)
113 {
114         int n;
115
116         if (exclude_list) {
117                 for (n=0; exclude_list[n]; n++)
118                         if (check_one_exclude(name,exclude_list[n],st))
119                                 return !exclude_list[n]->include;
120         }
121
122         if (local_exclude_list) {
123                 for (n=0; local_exclude_list[n]; n++)
124                         if (check_one_exclude(name,local_exclude_list[n],st))
125                                 return !local_exclude_list[n]->include;
126         }
127
128         return 0;
129 }
130
131
132 void add_exclude_list(char *pattern,struct exclude_struct ***list, int include)
133 {
134         int len=0;
135         if (list && *list)
136                 for (; (*list)[len]; len++) ;
137
138         if (strcmp(pattern,"!") == 0) {
139                 if (verbose > 2)
140                         rprintf(FINFO,"clearing exclude list\n");
141                 while ((len)--) {
142                         free_exclude((*list)[len]);
143                 }
144                 free((*list));
145                 *list = NULL;
146                 return;
147         }
148
149         if (!*list) {
150                 *list = (struct exclude_struct **)malloc(sizeof(struct exclude_struct *)*2);
151         } else {
152                 *list = (struct exclude_struct **)realloc(*list,sizeof(struct exclude_struct *)*(len+2));
153         }
154         
155         if (!*list || !((*list)[len] = make_exclude(pattern, include)))
156                 out_of_memory("add_exclude");
157         
158         if (verbose > 2)
159                 rprintf(FINFO,"add_exclude(%s)\n",pattern);
160         
161         (*list)[len+1] = NULL;
162 }
163
164 void add_exclude(char *pattern, int include)
165 {
166         add_exclude_list(pattern,&exclude_list, include);
167 }
168
169 struct exclude_struct **make_exclude_list(char *fname,
170                                           struct exclude_struct **list1,
171                                           int fatal, int include)
172 {
173         struct exclude_struct **list=list1;
174         FILE *f = fopen(fname,"r");
175         char line[MAXPATHLEN];
176         if (!f) {
177                 if (fatal) {
178                         rprintf(FERROR,"%s : %s\n",fname,strerror(errno));
179                         exit_cleanup(1);
180                 }
181                 return list;
182         }
183
184         while (fgets(line,MAXPATHLEN,f)) {
185                 int l = strlen(line);
186                 if (l && line[l-1] == '\n') l--;
187                 line[l] = 0;
188                 if (line[0]) add_exclude_list(line,&list,include);
189         }
190         fclose(f);
191         return list;
192 }
193
194
195 void add_exclude_file(char *fname,int fatal,int include)
196 {
197         if (!fname || !*fname) return;
198
199         exclude_list = make_exclude_list(fname,exclude_list,fatal,include);
200 }
201
202
203 void send_exclude_list(int f)
204 {
205         int i;
206         extern int remote_version;
207
208         if (!exclude_list) {
209                 write_int(f,0);
210                 return;
211         }
212
213         for (i=0;exclude_list[i];i++) {
214                 char *pattern = exclude_list[i]->orig; 
215                 int l;
216
217                 if (remote_version < 19) {
218                         if (strncmp(pattern,"+ ", 2)==0) {
219                                 rprintf(FERROR,"remote rsync does not support include syntax - aborting\n");
220                                 exit_cleanup(1);
221                         }
222                         
223                         if (strncmp(pattern,"- ", 2) == 0) {
224                                 pattern += 2;
225                         }
226                 }
227                 
228                 l = strlen(pattern);
229                 if (l == 0) continue;
230                 write_int(f,l);
231                 write_buf(f,pattern,l);
232         }    
233
234         write_int(f,0);
235 }
236
237
238 void recv_exclude_list(int f)
239 {
240         char line[MAXPATHLEN];
241         int l;
242         while ((l=read_int(f))) {
243                 if (l >= MAXPATHLEN) overflow("recv_exclude_list");
244                 read_sbuf(f,line,l);
245                 add_exclude(line,0);
246         }
247 }
248
249
250 void add_exclude_line(char *p)
251 {
252         char *tok;
253         if (!p || !*p) return;
254         p = strdup(p);
255         if (!p) out_of_memory("add_exclude_line");
256         for (tok=strtok(p," "); tok; tok=strtok(NULL," "))
257                 add_exclude(tok, 0);
258         free(p);
259 }
260
261
262 static char *cvs_ignore_list[] = {
263   "RCS","SCCS","CVS","CVS.adm","RCSLOG","cvslog.*",
264   "tags","TAGS",".make.state",".nse_depinfo",
265   "*~", "#*", ".#*", ",*", "*.old", "*.bak", "*.BAK", "*.orig",
266   "*.rej", ".del-*", "*.a", "*.o", "*.obj", "*.so", "*.Z", "*.elc", "*.ln",
267   "core",NULL};
268
269
270
271 void add_cvs_excludes(void)
272 {
273         char fname[MAXPATHLEN];
274         char *p;
275         int i;
276   
277         for (i=0; cvs_ignore_list[i]; i++)
278                 add_exclude(cvs_ignore_list[i], 0);
279
280         if ((p=getenv("HOME")) && strlen(p) < (MAXPATHLEN-12)) {
281                 slprintf(fname,sizeof(fname)-1, "%s/.cvsignore",p);
282                 add_exclude_file(fname,0,0);
283         }
284
285         add_exclude_line(getenv("CVSIGNORE"));
286 }