Added some defines for the various exclude-function arg literals.
[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;
30
5be7fa93
WD
31struct exclude_struct **exclude_list;
32struct exclude_struct **local_exclude_list;
33struct exclude_struct **server_exclude_list;
34char *exclude_path_prefix = NULL;
c627d613 35
07a874fd 36/** Build an exclude structure given a exclude pattern */
f0f5767f 37static struct exclude_struct *make_exclude(const char *pattern, int include)
c627d613 38{
2b6b4d53 39 struct exclude_struct *ret;
170381c0 40 char *cp;
5be7fa93 41 int pat_len;
c627d613 42
2b6b4d53
AT
43 ret = (struct exclude_struct *)malloc(sizeof(*ret));
44 if (!ret) out_of_memory("make_exclude");
c627d613 45
2b6b4d53
AT
46 memset(ret, 0, sizeof(*ret));
47
2b6b4d53
AT
48 if (strncmp(pattern,"- ",2) == 0) {
49 pattern += 2;
50 } else if (strncmp(pattern,"+ ",2) == 0) {
51 ret->include = 1;
52 pattern += 2;
53 } else {
54 ret->include = include;
55 }
56
5be7fa93
WD
57 if (exclude_path_prefix)
58 ret->match_flags |= MATCHFLG_ABS_PATH;
59 if (exclude_path_prefix && *pattern == '/') {
60 ret->pattern = malloc(strlen(exclude_path_prefix)
61 + strlen(pattern) + 1);
62 if (!ret->pattern) out_of_memory("make_exclude");
63 sprintf(ret->pattern, "%s%s", exclude_path_prefix, pattern);
64 }
65 else {
66 ret->pattern = strdup(pattern);
67 if (!ret->pattern) out_of_memory("make_exclude");
68 }
c627d613 69
2bca43f6 70 if (strpbrk(pattern, "*[?")) {
170381c0 71 ret->match_flags |= MATCHFLG_WILD;
0f2ac855
WD
72 if (strstr(pattern, "**")) {
73 static int tested;
74 if (!tested) {
75 tested = 1;
76 if (fnmatch("a/b/*","a/b/c/d",FNM_PATHNAME)==0)
77 rprintf(FERROR,"WARNING: fnmatch FNM_PATHNAME is broken on your system\n");
78 }
170381c0
WD
79 ret->match_flags |= MATCHFLG_WILD2;
80 /* If the pattern starts with **, note that. */
81 if (*pattern == '*' && pattern[1] == '*')
82 ret->match_flags |= MATCHFLG_WILD2_PREFIX;
0f2ac855 83 }
2bca43f6 84 }
c627d613 85
5be7fa93
WD
86 pat_len = strlen(ret->pattern);
87 if (pat_len > 1 && ret->pattern[pat_len-1] == '/') {
88 ret->pattern[pat_len-1] = 0;
2b6b4d53
AT
89 ret->directory = 1;
90 }
c627d613 91
170381c0
WD
92 for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
93 ret->slash_cnt++;
0944563e 94
2b6b4d53
AT
95 return ret;
96}
97
98static void free_exclude(struct exclude_struct *ex)
99{
2b6b4d53
AT
100 free(ex->pattern);
101 memset(ex,0,sizeof(*ex));
102 free(ex);
103}
c627d613 104
5be7fa93
WD
105
106void free_exclude_list(struct exclude_struct ***listp)
107{
108 struct exclude_struct **list = *listp;
109
110 if (verbose > 2)
111 rprintf(FINFO,"clearing exclude list\n");
112
113 if (!list)
114 return;
115
116 while (*list)
117 free_exclude(*list++);
118
119 free(*listp);
120 *listp = NULL;
121}
122
d567322f 123static int check_one_exclude(char *name, struct exclude_struct *ex,
5be7fa93 124 int name_is_dir)
2b6b4d53
AT
125{
126 char *p;
0f2ac855 127 int match_start = 0;
2b6b4d53
AT
128 char *pattern = ex->pattern;
129
170381c0
WD
130 /* If the pattern does not have any slashes AND it does not have
131 * a "**" (which could match a slash), then we just match the
132 * name portion of the path. */
5be7fa93
WD
133 if (!ex->slash_cnt && !(ex->match_flags & MATCHFLG_WILD2)) {
134 if ((p = strrchr(name,'/')) != NULL)
135 name = p+1;
136 }
137 else if ((ex->match_flags & MATCHFLG_ABS_PATH) && *name != '/') {
138 static char full_name[MAXPATHLEN];
139 extern char curr_dir[];
140 int plus = curr_dir[1] == '\0'? 1 : 0;
141 snprintf(full_name, sizeof full_name,
142 "%s/%s", curr_dir+plus, name);
143 name = full_name;
144 }
2b6b4d53
AT
145
146 if (!name[0]) return 0;
147
5be7fa93 148 if (ex->directory && !name_is_dir) return 0;
2b6b4d53 149
170381c0 150 if (*pattern == '/') {
2b6b4d53
AT
151 match_start = 1;
152 pattern++;
170381c0
WD
153 if (*name == '/')
154 name++;
2b6b4d53
AT
155 }
156
170381c0
WD
157 if (ex->match_flags & MATCHFLG_WILD) {
158 int fnmatch_flags = (ex->match_flags & MATCHFLG_WILD2)?
159 0 : FNM_PATHNAME;
160 /* A non-anchored match with an infix slash and no "**"
161 * needs to match the last slash_cnt+1 name elements. */
162 if (!match_start && ex->slash_cnt &&
163 !(ex->match_flags & MATCHFLG_WILD2)) {
164 int cnt = ex->slash_cnt + 1;
165 for (p = name + strlen(name) - 1; p >= name; p--) {
166 if (*p == '/' && !--cnt)
167 break;
168 }
169 name = p+1;
170 }
171 if (fnmatch(pattern, name, fnmatch_flags) == 0)
2b6b4d53 172 return 1;
170381c0
WD
173 if (ex->match_flags & MATCHFLG_WILD2_PREFIX) {
174 /* If the **-prefixed pattern has a '/' as the next
175 * character, then try to match the rest of the
176 * pattern at the root. */
177 if (pattern[2] == '/' &&
178 fnmatch(pattern+3, name, fnmatch_flags) == 0)
179 return 1;
c36cd317 180 }
170381c0
WD
181 else if (!match_start && ex->match_flags & MATCHFLG_WILD2) {
182 /* A non-anchored match with an infix or trailing "**"
183 * (but not a prefixed "**") needs to try matching
184 * after every slash. */
185 while ((name = strchr(name, '/')) != NULL) {
186 name++;
187 if (fnmatch(pattern, name, fnmatch_flags) == 0)
188 return 1;
189 }
190 }
191 } else if (match_start) {
192 if (strcmp(name,pattern) == 0)
193 return 1;
2b6b4d53
AT
194 } else {
195 int l1 = strlen(name);
ea2111d1 196 int l2 = strlen(pattern);
0f2ac855 197 if (l2 <= l1 &&
ea2111d1 198 strcmp(name+(l1-l2),pattern) == 0 &&
170381c0 199 (l1==l2 || name[l1-(l2+1)] == '/')) {
2b6b4d53 200 return 1;
c36cd317 201 }
2b6b4d53
AT
202 }
203
204 return 0;
c627d613
AT
205}
206
207
d567322f
MP
208static void report_exclude_result(char const *name,
209 struct exclude_struct const *ent,
5be7fa93 210 int name_is_dir)
d567322f 211{
0f2ac855
WD
212 /* If a trailing slash is present to match only directories,
213 * then it is stripped out by make_exclude. So as a special
214 * case we add it back in here. */
215
216 if (verbose >= 2)
217 rprintf(FINFO, "%s %s %s because of pattern %s%s\n",
218 ent->include ? "including" : "excluding",
5be7fa93 219 name_is_dir ? "directory" : "file",
0f2ac855
WD
220 name, ent->pattern,
221 ent->directory ? "/" : "");
d567322f
MP
222}
223
224
225/*
226 * Return true if file NAME is defined to be excluded by either
227 * LOCAL_EXCLUDE_LIST or the globals EXCLUDE_LIST.
228 */
5be7fa93 229int check_exclude(struct exclude_struct **list, char *name, int name_is_dir)
c627d613 230{
0f2ac855 231 struct exclude_struct *ent;
c627d613 232
5be7fa93
WD
233 while ((ent = *list++) != NULL) {
234 if (check_one_exclude(name, ent, name_is_dir)) {
235 report_exclude_result(name, ent, name_is_dir);
236 return !ent->include;
0f2ac855 237 }
2b6b4d53 238 }
c627d613 239
2b6b4d53 240 return 0;
c627d613
AT
241}
242
243
5be7fa93 244void add_exclude(struct exclude_struct ***listp, const char *pattern, int include)
c627d613 245{
5be7fa93
WD
246 struct exclude_struct **list = *listp;
247 int len = 0;
248
249 if (*pattern == '!' && !pattern[1]) {
250 free_exclude_list(listp);
251 return;
2b6b4d53
AT
252 }
253
5be7fa93
WD
254 if (list)
255 for (; list[len]; len++) {}
0f2ac855 256
5be7fa93
WD
257 list = *listp = (struct exclude_struct **)Realloc(list,sizeof(struct exclude_struct *)*(len+2));
258
259 if (!list || !(list[len] = make_exclude(pattern, include)))
2b6b4d53 260 out_of_memory("add_exclude");
0f2ac855 261
8c35542d
MP
262 if (verbose > 2) {
263 rprintf(FINFO,"add_exclude(%s,%s)\n",pattern,
0f2ac855 264 include ? "include" : "exclude");
8c35542d
MP
265 }
266
5be7fa93 267 list[len+1] = NULL;
c627d613
AT
268}
269
c627d613 270
5be7fa93
WD
271void add_exclude_file(struct exclude_struct ***listp, const char *fname,
272 int fatal, int include)
c627d613 273{
ccdff3eb 274 int fd;
2b6b4d53 275 char line[MAXPATHLEN];
ccdff3eb
WD
276 char *eob = line + MAXPATHLEN - 1;
277 extern int eol_nulls;
278
5be7fa93
WD
279 if (!fname || !*fname)
280 return;
281
282 if (*fname != '-' || fname[1])
ccdff3eb
WD
283 fd = open(fname, O_RDONLY|O_BINARY);
284 else
285 fd = 0;
286 if (fd < 0) {
2b6b4d53 287 if (fatal) {
a039749b 288 rsyserr(FERROR, errno,
0f2ac855
WD
289 "failed to open %s file %s",
290 include ? "include" : "exclude",
291 fname);
65417579 292 exit_cleanup(RERR_FILEIO);
2b6b4d53 293 }
5be7fa93 294 return;
2b6b4d53
AT
295 }
296
ccdff3eb
WD
297 while (1) {
298 char ch, *s = line;
299 int cnt;
300 while (1) {
301 if ((cnt = read(fd, &ch, 1)) <= 0) {
302 if (cnt < 0 && errno == EINTR)
303 continue;
304 break;
305 }
306 if (eol_nulls? !ch : (ch == '\n' || ch == '\r'))
307 break;
308 if (s < eob)
309 *s++ = ch;
310 }
311 *s = '\0';
312 if (*line && *line != ';' && *line != '#') {
122f19a6 313 /* Skip lines starting with semicolon or pound.
ccdff3eb
WD
314 * It probably wouldn't cause any harm to not skip
315 * them but there's no need to save them. */
5be7fa93 316 add_exclude(listp, line, include);
122f19a6 317 }
ccdff3eb
WD
318 if (cnt <= 0)
319 break;
2b6b4d53 320 }
ccdff3eb 321 close(fd);
c627d613
AT
322}
323
324
325void send_exclude_list(int f)
326{
2b6b4d53
AT
327 int i;
328 extern int remote_version;
25cf8893
AT
329 extern int list_only, recurse;
330
bb7c4fa3
MP
331 /* This is a complete hack - blame Rusty.
332 *
333 * FIXME: This pattern shows up in the output of
334 * report_exclude_result(), which is not ideal. */
5be7fa93
WD
335 if (list_only && !recurse)
336 add_exclude(&exclude_list, "/*/*", ADD_EXCLUDE);
2b6b4d53
AT
337
338 if (!exclude_list) {
339 write_int(f,0);
340 return;
341 }
342
343 for (i=0;exclude_list[i];i++) {
2b6b4d53 344 int l;
2fb139c1
AT
345 char pattern[MAXPATHLEN];
346
0f2ac855 347 strlcpy(pattern,exclude_list[i]->pattern,sizeof(pattern));
2fb139c1 348 if (exclude_list[i]->directory) strlcat(pattern,"/", sizeof(pattern));
2b6b4d53 349
587cb08d
DD
350 l = strlen(pattern);
351 if (l == 0) continue;
352 if (exclude_list[i]->include) {
353 if (remote_version < 19) {
2b6b4d53 354 rprintf(FERROR,"remote rsync does not support include syntax - aborting\n");
ec9df380 355 exit_cleanup(RERR_UNSUPPORTED);
2b6b4d53 356 }
587cb08d
DD
357 write_int(f,l+2);
358 write_buf(f,"+ ",2);
359 } else {
360 write_int(f,l);
2b6b4d53 361 }
2b6b4d53 362 write_buf(f,pattern,l);
0f2ac855 363 }
2b6b4d53
AT
364
365 write_int(f,0);
c627d613
AT
366}
367
368
369void recv_exclude_list(int f)
370{
2b6b4d53 371 char line[MAXPATHLEN];
9dd891bb
MP
372 unsigned int l;
373
2b6b4d53
AT
374 while ((l=read_int(f))) {
375 if (l >= MAXPATHLEN) overflow("recv_exclude_list");
376 read_sbuf(f,line,l);
5be7fa93 377 add_exclude(&exclude_list, line, ADD_EXCLUDE);
2b6b4d53 378 }
c627d613
AT
379}
380
651443a7
DD
381/* Get the next include/exclude arg from the string. It works in a similar way
382** to strtok - initially an arg is sent over, from then on NULL. This
383** routine takes into account any +/- in the strings and does not
384** consider the space following it as a delimeter.
385*/
386char *get_exclude_tok(char *p)
387{
388 static char *s;
389 static int more;
390 char *t;
391
392 if (p) {
393 s=p;
394 if (*p)
395 more=1;
396 }
397
398 if (!more)
399 return(NULL);
c627d613 400
651443a7 401 /* Skip over any initial spaces */
32f76175 402 while (isspace(* (unsigned char *) s))
651443a7
DD
403 s++;
404
405 /* Are we at the end of the string? */
406 if (*s) {
407 /* remember the beginning of the token */
408 t=s;
409
410 /* Is this a '+' or '-' followed by a space (not whitespace)? */
411 if ((*s=='+' || *s=='-') && *(s+1)==' ')
412 s+=2;
0f2ac855 413
651443a7 414 /* Skip to the next space or the end of the string */
32f76175 415 while (!isspace(* (unsigned char *) s) && *s != '\0')
651443a7
DD
416 s++;
417 } else {
418 t=NULL;
419 }
420
421 /* Have we reached the end of the string? */
422 if (*s)
423 *s++='\0';
424 else
425 more=0;
426 return(t);
427}
428
0f2ac855 429
5be7fa93
WD
430void add_exclude_line(struct exclude_struct ***listp,
431 const char *line, int include)
8f3a2d54 432{
5be7fa93
WD
433 char *tok, *p;
434 if (!line || !*line) return;
435 p = strdup(line);
8f3a2d54 436 if (!p) out_of_memory("add_exclude_line");
0f2ac855 437 for (tok=get_exclude_tok(p); tok; tok=get_exclude_tok(NULL))
5be7fa93 438 add_exclude(listp, tok, include);
cd64343a
DD
439 free(p);
440}
441
8f3a2d54 442
c627d613 443static char *cvs_ignore_list[] = {
95dd949c
WD
444 "RCS/", "SCCS/", "CVS/", ".svn/", "CVS.adm", "RCSLOG", "cvslog.*",
445 "tags", "TAGS", ".make.state", ".nse_depinfo",
446 "*~", "#*", ".#*", ", *", "*.old", "*.bak", "*.BAK", "*.orig",
c627d613 447 "*.rej", ".del-*", "*.a", "*.o", "*.obj", "*.so", "*.Z", "*.elc", "*.ln",
95dd949c 448 "core", NULL};
c627d613
AT
449
450
451void add_cvs_excludes(void)
452{
2b6b4d53
AT
453 char fname[MAXPATHLEN];
454 char *p;
455 int i;
0f2ac855 456
2b6b4d53 457 for (i=0; cvs_ignore_list[i]; i++)
5be7fa93 458 add_exclude(&exclude_list, cvs_ignore_list[i], ADD_EXCLUDE);
c627d613 459
2b6b4d53 460 if ((p=getenv("HOME")) && strlen(p) < (MAXPATHLEN-12)) {
8950ac03 461 snprintf(fname,sizeof(fname), "%s/.cvsignore",p);
5be7fa93 462 add_exclude_file(&exclude_list,fname,MISSING_OK,ADD_EXCLUDE);
2b6b4d53 463 }
c627d613 464
5be7fa93 465 add_exclude_line(&exclude_list, getenv("CVSIGNORE"), ADD_EXCLUDE);
c627d613 466}