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