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