Roll over NEWS
[rsync/rsync.git] / exclude.c
... / ...
CommitLineData
1/* -*- c-file-style: "linux" -*-
2
3 Copyright (C) 1996-2001 by Andrew Tridgell <tridge@samba.org>
4 Copyright (C) 1996 by Paul Mackerras
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19*/
20
21/* a lot of this stuff was originally derived from GNU tar, although
22 it has now changed so much that it is hard to tell :) */
23
24/* include/exclude cluestick added by Martin Pool <mbp@samba.org> */
25
26#include "rsync.h"
27
28extern int verbose;
29extern int delete_mode;
30
31static struct exclude_struct **exclude_list;
32
33/* build an exclude structure given a exclude pattern */
34static struct exclude_struct *make_exclude(const char *pattern, int include)
35{
36 struct exclude_struct *ret;
37
38 ret = (struct exclude_struct *)malloc(sizeof(*ret));
39 if (!ret) out_of_memory("make_exclude");
40
41 memset(ret, 0, sizeof(*ret));
42
43 if (strncmp(pattern,"- ",2) == 0) {
44 pattern += 2;
45 } else if (strncmp(pattern,"+ ",2) == 0) {
46 ret->include = 1;
47 pattern += 2;
48 } else {
49 ret->include = include;
50 }
51
52 ret->pattern = strdup(pattern);
53
54 if (!ret->pattern) out_of_memory("make_exclude");
55
56 if (strpbrk(pattern, "*[?")) {
57 ret->regular_exp = 1;
58 ret->fnmatch_flags = FNM_PATHNAME;
59 if (strstr(pattern, "**")) {
60 static int tested;
61 if (!tested) {
62 tested = 1;
63 if (fnmatch("a/b/*", "a/b/c/d", FNM_PATHNAME)==0) {
64 rprintf(FERROR,"WARNING: fnmatch FNM_PATHNAME is broken on your system\n");
65 }
66 }
67 ret->fnmatch_flags = 0;
68 }
69 }
70
71 if (strlen(pattern) > 1 && pattern[strlen(pattern)-1] == '/') {
72 ret->pattern[strlen(pattern)-1] = 0;
73 ret->directory = 1;
74 }
75
76 if (!strchr(ret->pattern,'/')) {
77 ret->local = 1;
78 }
79
80 return ret;
81}
82
83static void free_exclude(struct exclude_struct *ex)
84{
85 free(ex->pattern);
86 memset(ex,0,sizeof(*ex));
87 free(ex);
88}
89
90static int check_one_exclude(char *name, struct exclude_struct *ex,
91 STRUCT_STAT *st)
92{
93 char *p;
94 int match_start=0;
95 char *pattern = ex->pattern;
96
97 if (ex->local && (p=strrchr(name,'/')))
98 name = p+1;
99
100 if (!name[0]) return 0;
101
102 if (ex->directory && !S_ISDIR(st->st_mode)) return 0;
103
104 if (*pattern == '/' && *name != '/') {
105 match_start = 1;
106 pattern++;
107 }
108
109 if (ex->regular_exp) {
110 if (fnmatch(pattern, name, ex->fnmatch_flags) == 0) {
111 return 1;
112 }
113 } else {
114 int l1 = strlen(name);
115 int l2 = strlen(pattern);
116 if (l2 <= l1 &&
117 strcmp(name+(l1-l2),pattern) == 0 &&
118 (l1==l2 || (!match_start && name[l1-(l2+1)] == '/'))) {
119 return 1;
120 }
121 }
122
123 return 0;
124}
125
126
127static void report_exclude_result(char const *name,
128 struct exclude_struct const *ent,
129 STRUCT_STAT const *st)
130{
131 /* If a trailing slash is present to match only directories,
132 * then it is stripped out by make_exclude. So as a special
133 * case we add it back in here. */
134
135 if (verbose >= 2)
136 rprintf(FINFO, "%s %s %s because of pattern %s%s\n",
137 ent->include ? "including" : "excluding",
138 S_ISDIR(st->st_mode) ? "directory" : "file",
139 name, ent->pattern,
140 ent->directory ? "/" : "");
141}
142
143
144/*
145 * Return true if file NAME is defined to be excluded by either
146 * LOCAL_EXCLUDE_LIST or the globals EXCLUDE_LIST.
147 */
148int check_exclude(char *name, struct exclude_struct **local_exclude_list,
149 STRUCT_STAT *st)
150{
151 int n;
152 struct exclude_struct *ent;
153
154 if (name && (name[0] == '.') && !name[1])
155 /* never exclude '.', even if somebody does --exclude '*' */
156 return 0;
157
158 if (exclude_list) {
159 for (n=0; exclude_list[n]; n++) {
160 ent = exclude_list[n];
161 if (check_one_exclude(name, ent, st)) {
162 report_exclude_result(name, ent, st);
163 return !ent->include;
164 }
165 }
166 }
167
168 if (local_exclude_list) {
169 for (n=0; local_exclude_list[n]; n++) {
170 ent = local_exclude_list[n];
171 if (check_one_exclude(name, ent, st)) {
172 report_exclude_result(name, ent, st);
173 return !ent->include;
174 }
175 }
176 }
177
178 return 0;
179}
180
181
182void add_exclude_list(const char *pattern, struct exclude_struct ***list, int include)
183{
184 int len=0;
185 if (list && *list)
186 for (; (*list)[len]; len++) ;
187
188 if (strcmp(pattern,"!") == 0) {
189 if (verbose > 2)
190 rprintf(FINFO,"clearing exclude list\n");
191 while ((len)--) {
192 free_exclude((*list)[len]);
193 }
194 free((*list));
195 *list = NULL;
196 return;
197 }
198
199 *list = (struct exclude_struct **)Realloc(*list,sizeof(struct exclude_struct *)*(len+2));
200
201 if (!*list || !((*list)[len] = make_exclude(pattern, include)))
202 out_of_memory("add_exclude");
203
204 if (verbose > 2) {
205 rprintf(FINFO,"add_exclude(%s,%s)\n",pattern,
206 include ? "include" : "exclude");
207 }
208
209 (*list)[len+1] = NULL;
210}
211
212void add_exclude(const char *pattern, int include)
213{
214 add_exclude_list(pattern,&exclude_list, include);
215}
216
217struct exclude_struct **make_exclude_list(const char *fname,
218 struct exclude_struct **list1,
219 int fatal, int include)
220{
221 struct exclude_struct **list=list1;
222 FILE *f = fopen(fname,"r");
223 char line[MAXPATHLEN];
224 if (!f) {
225 if (fatal) {
226 rsyserr(FERROR, errno,
227 "failed to open %s file %s",
228 include ? "include" : "exclude",
229 fname);
230 exit_cleanup(RERR_FILEIO);
231 }
232 return list;
233 }
234
235 while (fgets(line,MAXPATHLEN,f)) {
236 int l = strlen(line);
237 if (l && line[l-1] == '\n') l--;
238 line[l] = 0;
239 if (line[0] && (line[0] != ';') && (line[0] != '#')) {
240 /* Skip lines starting with semicolon or pound.
241 It probably wouldn't cause any harm to not skip
242 them but there's no need to save them. */
243 add_exclude_list(line,&list,include);
244 }
245 }
246 fclose(f);
247 return list;
248}
249
250
251void add_exclude_file(const char *fname, int fatal, int include)
252{
253 if (!fname || !*fname) return;
254
255 exclude_list = make_exclude_list(fname,exclude_list,fatal,include);
256}
257
258
259void send_exclude_list(int f)
260{
261 int i;
262 extern int remote_version;
263 extern int list_only, recurse;
264
265 /* this is a complete hack - blame Rusty */
266 if (list_only && !recurse) {
267 add_exclude("/*/*", 0);
268 }
269
270 if (!exclude_list) {
271 write_int(f,0);
272 return;
273 }
274
275 for (i=0;exclude_list[i];i++) {
276 int l;
277 char pattern[MAXPATHLEN];
278
279 strlcpy(pattern,exclude_list[i]->pattern,sizeof(pattern));
280 if (exclude_list[i]->directory) strlcat(pattern,"/", sizeof(pattern));
281
282 l = strlen(pattern);
283 if (l == 0) continue;
284 if (exclude_list[i]->include) {
285 if (remote_version < 19) {
286 rprintf(FERROR,"remote rsync does not support include syntax - aborting\n");
287 exit_cleanup(RERR_UNSUPPORTED);
288 }
289 write_int(f,l+2);
290 write_buf(f,"+ ",2);
291 } else {
292 write_int(f,l);
293 }
294 write_buf(f,pattern,l);
295 }
296
297 write_int(f,0);
298}
299
300
301void recv_exclude_list(int f)
302{
303 char line[MAXPATHLEN];
304 unsigned int l;
305
306 while ((l=read_int(f))) {
307 if (l >= MAXPATHLEN) overflow("recv_exclude_list");
308 read_sbuf(f,line,l);
309 add_exclude(line,0);
310 }
311}
312
313/* Get the next include/exclude arg from the string. It works in a similar way
314** to strtok - initially an arg is sent over, from then on NULL. This
315** routine takes into account any +/- in the strings and does not
316** consider the space following it as a delimeter.
317*/
318char *get_exclude_tok(char *p)
319{
320 static char *s;
321 static int more;
322 char *t;
323
324 if (p) {
325 s=p;
326 if (*p)
327 more=1;
328 }
329
330 if (!more)
331 return(NULL);
332
333 /* Skip over any initial spaces */
334 while(isspace(*s))
335 s++;
336
337 /* Are we at the end of the string? */
338 if (*s) {
339 /* remember the beginning of the token */
340 t=s;
341
342 /* Is this a '+' or '-' followed by a space (not whitespace)? */
343 if ((*s=='+' || *s=='-') && *(s+1)==' ')
344 s+=2;
345
346 /* Skip to the next space or the end of the string */
347 while(!isspace(*s) && *s!='\0')
348 s++;
349 } else {
350 t=NULL;
351 }
352
353 /* Have we reached the end of the string? */
354 if (*s)
355 *s++='\0';
356 else
357 more=0;
358 return(t);
359}
360
361
362void add_exclude_line(char *p)
363{
364 char *tok;
365 if (!p || !*p) return;
366 p = strdup(p);
367 if (!p) out_of_memory("add_exclude_line");
368 for (tok=get_exclude_tok(p); tok; tok=get_exclude_tok(NULL))
369 add_exclude(tok, 0);
370 free(p);
371}
372
373void add_include_line(char *p)
374{
375 char *tok;
376 if (!p || !*p) return;
377 p = strdup(p);
378 if (!p) out_of_memory("add_include_line");
379 for (tok=get_exclude_tok(p); tok; tok=get_exclude_tok(NULL))
380 add_exclude(tok, 1);
381 free(p);
382}
383
384
385static char *cvs_ignore_list[] = {
386 "RCS","SCCS","CVS","CVS.adm","RCSLOG","cvslog.*",
387 "tags","TAGS",".make.state",".nse_depinfo",
388 "*~", "#*", ".#*", ",*", "*.old", "*.bak", "*.BAK", "*.orig",
389 "*.rej", ".del-*", "*.a", "*.o", "*.obj", "*.so", "*.Z", "*.elc", "*.ln",
390 "core",NULL};
391
392
393
394void add_cvs_excludes(void)
395{
396 char fname[MAXPATHLEN];
397 char *p;
398 int i;
399
400 for (i=0; cvs_ignore_list[i]; i++)
401 add_exclude(cvs_ignore_list[i], 0);
402
403 if ((p=getenv("HOME")) && strlen(p) < (MAXPATHLEN-12)) {
404 snprintf(fname,sizeof(fname), "%s/.cvsignore",p);
405 add_exclude_file(fname,0,0);
406 }
407
408 add_exclude_line(getenv("CVSIGNORE"));
409}