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