Fixed bug introduced by calling do_open() for O_RDONLY files. Changed it
[rsync/rsync.git] / exclude.c
CommitLineData
c627d613
AT
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
2b6b4d53
AT
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 :) */
c627d613
AT
22
23#include "rsync.h"
24
25extern int verbose;
8458724d 26extern int delete_mode;
c627d613 27
2b6b4d53 28static struct exclude_struct **exclude_list;
c627d613 29
2bca43f6
DD
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
8458724d 45 if (!only_included_files || (exclude_the_rest == NULL) || delete_mode)
2bca43f6
DD
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 }
19c14f98
DD
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);
2bca43f6
DD
68 }
69 exclude_list = ex_list;
70
71 return 1;
72}
73
2b6b4d53
AT
74/* build an exclude structure given a exclude pattern */
75static struct exclude_struct *make_exclude(char *pattern, int include)
c627d613 76{
2b6b4d53 77 struct exclude_struct *ret;
c627d613 78
2b6b4d53
AT
79 ret = (struct exclude_struct *)malloc(sizeof(*ret));
80 if (!ret) out_of_memory("make_exclude");
c627d613 81
2b6b4d53
AT
82 memset(ret, 0, sizeof(*ret));
83
2b6b4d53
AT
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
587cb08d 95 if (!ret->pattern) out_of_memory("make_exclude");
c627d613 96
2bca43f6
DD
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;
a8b9d4ed 104 ret->fnmatch_flags = strstr(pattern, "**") ? 0 : FNM_PATHNAME;
2bca43f6
DD
105 } else if (!ret->include) {
106 only_included_files = 0;
107 }
c627d613 108
2b6b4d53
AT
109 if (strlen(pattern) > 1 && pattern[strlen(pattern)-1] == '/') {
110 ret->pattern[strlen(pattern)-1] = 0;
111 ret->directory = 1;
112 }
c627d613 113
2b6b4d53
AT
114 if (!strchr(ret->pattern,'/')) {
115 ret->local = 1;
116 }
0944563e 117
2b6b4d53
AT
118 return ret;
119}
120
121static void free_exclude(struct exclude_struct *ex)
122{
2b6b4d53
AT
123 free(ex->pattern);
124 memset(ex,0,sizeof(*ex));
125 free(ex);
126}
c627d613 127
2b6b4d53
AT
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) {
a8b9d4ed 148 if (fnmatch(pattern, name, ex->fnmatch_flags) == 0)
2b6b4d53
AT
149 return 1;
150 } else {
151 int l1 = strlen(name);
ea2111d1 152 int l2 = strlen(pattern);
2b6b4d53 153 if (l2 <= l1 &&
ea2111d1 154 strcmp(name+(l1-l2),pattern) == 0 &&
2b6b4d53
AT
155 (l1==l2 || (!match_start && name[l1-(l2+1)] == '/')))
156 return 1;
157 }
158
159 return 0;
c627d613
AT
160}
161
162
2b6b4d53
AT
163int check_exclude(char *name,struct exclude_struct **local_exclude_list,
164 STRUCT_STAT *st)
c627d613 165{
2b6b4d53 166 int n;
c627d613 167
5d5811f7
DD
168 if (name && (name[0] == '.') && !name[1])
169 /* never exclude '.', even if somebody does --exclude '*' */
170 return 0;
171
2b6b4d53
AT
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 }
c627d613 177
2b6b4d53
AT
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 }
c627d613 183
2b6b4d53 184 return 0;
c627d613
AT
185}
186
187
2b6b4d53 188void add_exclude_list(char *pattern,struct exclude_struct ***list, int include)
c627d613 189{
2b6b4d53
AT
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;
2bca43f6
DD
202 only_included_files = 1;
203 exclude_the_rest = NULL;
2b6b4d53
AT
204 return;
205 }
206
fe8c0a98 207 *list = (struct exclude_struct **)Realloc(*list,sizeof(struct exclude_struct *)*(len+2));
2b6b4d53
AT
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;
c627d613
AT
216}
217
2b6b4d53 218void add_exclude(char *pattern, int include)
c627d613 219{
2b6b4d53 220 add_exclude_list(pattern,&exclude_list, include);
c627d613
AT
221}
222
2b6b4d53
AT
223struct exclude_struct **make_exclude_list(char *fname,
224 struct exclude_struct **list1,
225 int fatal, int include)
c627d613 226{
2b6b4d53
AT
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));
65417579 233 exit_cleanup(RERR_FILEIO);
2b6b4d53
AT
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;
122f19a6
DD
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 }
2b6b4d53
AT
248 }
249 fclose(f);
250 return list;
c627d613
AT
251}
252
253
2b6b4d53 254void add_exclude_file(char *fname,int fatal,int include)
c627d613 255{
8f3a2d54
AT
256 if (!fname || !*fname) return;
257
2b6b4d53 258 exclude_list = make_exclude_list(fname,exclude_list,fatal,include);
c627d613
AT
259}
260
261
262void send_exclude_list(int f)
263{
2b6b4d53
AT
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++) {
587cb08d 273 char *pattern = exclude_list[i]->pattern;
2b6b4d53
AT
274 int l;
275
587cb08d
DD
276 l = strlen(pattern);
277 if (l == 0) continue;
278 if (exclude_list[i]->include) {
279 if (remote_version < 19) {
2b6b4d53 280 rprintf(FERROR,"remote rsync does not support include syntax - aborting\n");
ec9df380 281 exit_cleanup(RERR_UNSUPPORTED);
2b6b4d53 282 }
587cb08d
DD
283 write_int(f,l+2);
284 write_buf(f,"+ ",2);
285 } else {
286 write_int(f,l);
2b6b4d53 287 }
2b6b4d53
AT
288 write_buf(f,pattern,l);
289 }
290
291 write_int(f,0);
c627d613
AT
292}
293
294
295void recv_exclude_list(int f)
296{
2b6b4d53
AT
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 }
c627d613
AT
304}
305
651443a7
DD
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);
c627d613 325
651443a7
DD
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
8f3a2d54
AT
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");
651443a7 361 for (tok=get_exclude_tok(p); tok; tok=get_exclude_tok(NULL))
2b6b4d53 362 add_exclude(tok, 0);
8f3a2d54
AT
363 free(p);
364}
365
cd64343a
DD
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");
651443a7 372 for (tok=get_exclude_tok(p); tok; tok=get_exclude_tok(NULL))
cd64343a
DD
373 add_exclude(tok, 1);
374 free(p);
375}
376
8f3a2d54 377
c627d613
AT
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{
2b6b4d53
AT
389 char fname[MAXPATHLEN];
390 char *p;
391 int i;
c627d613 392
2b6b4d53
AT
393 for (i=0; cvs_ignore_list[i]; i++)
394 add_exclude(cvs_ignore_list[i], 0);
c627d613 395
2b6b4d53 396 if ((p=getenv("HOME")) && strlen(p) < (MAXPATHLEN-12)) {
37f9805d 397 slprintf(fname,sizeof(fname), "%s/.cvsignore",p);
2b6b4d53
AT
398 add_exclude_file(fname,0,0);
399 }
c627d613 400
2b6b4d53 401 add_exclude_line(getenv("CVSIGNORE"));
c627d613 402}