Mention how --update behaves when the type of the file differs on
[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;
40d38dc0
WD
30extern int eol_nulls;
31extern int list_only;
32extern int recurse;
33
34extern char curr_dir[];
c627d613 35
67340e95
WD
36struct exclude_list_struct exclude_list = { 0, 0, "" };
37struct exclude_list_struct local_exclude_list = { 0, 0, "local-cvsignore " };
38struct exclude_list_struct server_exclude_list = { 0, 0, "server " };
5be7fa93 39char *exclude_path_prefix = NULL;
c627d613 40
07a874fd 41/** Build an exclude structure given a exclude pattern */
b2aa573b
WD
42static void make_exclude(struct exclude_list_struct *listp, const char *pattern,
43 int pat_len, int include)
c627d613 44{
2b6b4d53 45 struct exclude_struct *ret;
f8f72644
WD
46 const char *cp;
47 int ex_len;
c627d613 48
58cadc86 49 ret = new(struct exclude_struct);
f8f72644
WD
50 if (!ret)
51 out_of_memory("make_exclude");
c627d613 52
5f5be796 53 memset(ret, 0, sizeof ret[0]);
f8f72644 54 ret->include = include;
2b6b4d53 55
5be7fa93
WD
56 if (exclude_path_prefix)
57 ret->match_flags |= MATCHFLG_ABS_PATH;
f8f72644
WD
58 if (exclude_path_prefix && *pattern == '/')
59 ex_len = strlen(exclude_path_prefix);
60 else
61 ex_len = 0;
62 ret->pattern = new_array(char, ex_len + pat_len + 1);
63 if (!ret->pattern)
64 out_of_memory("make_exclude");
65 if (ex_len)
66 memcpy(ret->pattern, exclude_path_prefix, ex_len);
67 strlcpy(ret->pattern + ex_len, pattern, pat_len + 1);
68 pat_len += ex_len;
69
70 if (strpbrk(ret->pattern, "*[?")) {
170381c0 71 ret->match_flags |= MATCHFLG_WILD;
96d3590a 72 if ((cp = strstr(ret->pattern, "**")) != NULL) {
170381c0
WD
73 ret->match_flags |= MATCHFLG_WILD2;
74 /* If the pattern starts with **, note that. */
96d3590a 75 if (cp == ret->pattern)
170381c0 76 ret->match_flags |= MATCHFLG_WILD2_PREFIX;
0f2ac855 77 }
2bca43f6 78 }
c627d613 79
5be7fa93
WD
80 if (pat_len > 1 && ret->pattern[pat_len-1] == '/') {
81 ret->pattern[pat_len-1] = 0;
2b6b4d53
AT
82 ret->directory = 1;
83 }
c627d613 84
170381c0
WD
85 for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
86 ret->slash_cnt++;
0944563e 87
b2aa573b
WD
88 if (!listp->tail)
89 listp->head = listp->tail = ret;
90 else {
91 listp->tail->next = ret;
92 listp->tail = ret;
93 }
2b6b4d53
AT
94}
95
96static void free_exclude(struct exclude_struct *ex)
97{
2b6b4d53 98 free(ex->pattern);
2b6b4d53
AT
99 free(ex);
100}
c627d613 101
b2aa573b 102void free_exclude_list(struct exclude_list_struct *listp)
5be7fa93 103{
b2aa573b 104 struct exclude_struct *ent, *next;
5be7fa93 105
67340e95
WD
106 if (verbose > 2) {
107 rprintf(FINFO, "[%s] clearing %sexclude list\n",
108 who_am_i(), listp->debug_type);
109 }
5be7fa93 110
b2aa573b
WD
111 for (ent = listp->head; ent; ent = next) {
112 next = ent->next;
113 free_exclude(ent);
114 }
5be7fa93 115
67340e95 116 listp->head = listp->tail = NULL;
5be7fa93
WD
117}
118
d567322f 119static int check_one_exclude(char *name, struct exclude_struct *ex,
5be7fa93 120 int name_is_dir)
2b6b4d53
AT
121{
122 char *p;
0f2ac855 123 int match_start = 0;
2b6b4d53
AT
124 char *pattern = ex->pattern;
125
170381c0
WD
126 /* If the pattern does not have any slashes AND it does not have
127 * a "**" (which could match a slash), then we just match the
128 * name portion of the path. */
5be7fa93
WD
129 if (!ex->slash_cnt && !(ex->match_flags & MATCHFLG_WILD2)) {
130 if ((p = strrchr(name,'/')) != NULL)
131 name = p+1;
132 }
133 else if ((ex->match_flags & MATCHFLG_ABS_PATH) && *name != '/') {
134 static char full_name[MAXPATHLEN];
5be7fa93 135 int plus = curr_dir[1] == '\0'? 1 : 0;
a7725e6d 136 pathjoin(full_name, sizeof full_name, curr_dir+plus, name);
5be7fa93
WD
137 name = full_name;
138 }
2b6b4d53
AT
139
140 if (!name[0]) return 0;
141
5be7fa93 142 if (ex->directory && !name_is_dir) return 0;
2b6b4d53 143
170381c0 144 if (*pattern == '/') {
2b6b4d53
AT
145 match_start = 1;
146 pattern++;
170381c0
WD
147 if (*name == '/')
148 name++;
2b6b4d53
AT
149 }
150
170381c0 151 if (ex->match_flags & MATCHFLG_WILD) {
170381c0
WD
152 /* A non-anchored match with an infix slash and no "**"
153 * needs to match the last slash_cnt+1 name elements. */
154 if (!match_start && ex->slash_cnt &&
155 !(ex->match_flags & MATCHFLG_WILD2)) {
156 int cnt = ex->slash_cnt + 1;
157 for (p = name + strlen(name) - 1; p >= name; p--) {
158 if (*p == '/' && !--cnt)
159 break;
160 }
161 name = p+1;
162 }
fe332038 163 if (wildmatch(pattern, name))
2b6b4d53 164 return 1;
170381c0
WD
165 if (ex->match_flags & MATCHFLG_WILD2_PREFIX) {
166 /* If the **-prefixed pattern has a '/' as the next
167 * character, then try to match the rest of the
168 * pattern at the root. */
fe332038 169 if (pattern[2] == '/' && wildmatch(pattern+3, name))
170381c0 170 return 1;
c36cd317 171 }
170381c0
WD
172 else if (!match_start && ex->match_flags & MATCHFLG_WILD2) {
173 /* A non-anchored match with an infix or trailing "**"
174 * (but not a prefixed "**") needs to try matching
175 * after every slash. */
176 while ((name = strchr(name, '/')) != NULL) {
177 name++;
fe332038 178 if (wildmatch(pattern, name))
170381c0
WD
179 return 1;
180 }
181 }
182 } else if (match_start) {
183 if (strcmp(name,pattern) == 0)
184 return 1;
2b6b4d53
AT
185 } else {
186 int l1 = strlen(name);
ea2111d1 187 int l2 = strlen(pattern);
0f2ac855 188 if (l2 <= l1 &&
ea2111d1 189 strcmp(name+(l1-l2),pattern) == 0 &&
170381c0 190 (l1==l2 || name[l1-(l2+1)] == '/')) {
2b6b4d53 191 return 1;
c36cd317 192 }
2b6b4d53
AT
193 }
194
195 return 0;
c627d613
AT
196}
197
198
d567322f
MP
199static void report_exclude_result(char const *name,
200 struct exclude_struct const *ent,
61414c83 201 int name_is_dir, const char *type)
d567322f 202{
0f2ac855
WD
203 /* If a trailing slash is present to match only directories,
204 * then it is stripped out by make_exclude. So as a special
205 * case we add it back in here. */
206
ea847c62 207 if (verbose >= 2) {
67340e95 208 rprintf(FINFO, "[%s] %scluding %s %s because of %spattern %s%s\n",
f8f72644 209 who_am_i(), ent->include ? "in" : "ex",
61414c83
WD
210 name_is_dir ? "directory" : "file", name, type,
211 ent->pattern, ent->directory ? "/" : "");
ea847c62 212 }
d567322f
MP
213}
214
215
216/*
b2aa573b 217 * Return true if file NAME is defined to be excluded by the specified
9fdb334e
WD
218 * exclude list. Returns -1 for an exclude, 1 for an include, and 0 if
219 * no match.
d567322f 220 */
67340e95 221int check_exclude(struct exclude_list_struct *listp, char *name, int name_is_dir)
c627d613 222{
0f2ac855 223 struct exclude_struct *ent;
c627d613 224
b2aa573b 225 for (ent = listp->head; ent; ent = ent->next) {
5be7fa93 226 if (check_one_exclude(name, ent, name_is_dir)) {
67340e95
WD
227 report_exclude_result(name, ent, name_is_dir,
228 listp->debug_type);
9fdb334e 229 return ent->include ? 1 : -1;
0f2ac855 230 }
2b6b4d53 231 }
c627d613 232
2b6b4d53 233 return 0;
c627d613
AT
234}
235
236
f8f72644
WD
237/* Get the next include/exclude arg from the string. The token will not
238 * be '\0' terminated, so use the returned length to limit the string.
239 * Also, be sure to add this length to the returned pointer before passing
240 * it back to ask for the next token. This routine will not split off a
96d3590a
WD
241 * prefix of "+ " or "- " unless xflags contains XFLG_NO_PREFIXES. The
242 * *incl_ptr value will be 1 for an include, 0 for an exclude, and -1 for
243 * the list-clearing "!" token.
f8f72644 244 */
96d3590a
WD
245static const char *get_exclude_tok(const char *p, int *len_ptr, int *incl_ptr,
246 int xflags)
f8f72644 247{
abca4eba 248 const unsigned char *s = (const unsigned char *)p;
96d3590a 249 int len;
f8f72644 250
96d3590a
WD
251 if (xflags & XFLG_WORD_SPLIT) {
252 /* Skip over any initial whitespace. */
253 while (isspace(*s))
f8f72644 254 s++;
abca4eba
WD
255 /* Update for "!" check. */
256 p = (const char *)s;
f8f72644
WD
257 }
258
96d3590a
WD
259 /* Is this a '+' or '-' followed by a space (not whitespace)? */
260 if (!(xflags & XFLG_NO_PREFIXES)
261 && (*s == '-' || *s == '+') && s[1] == ' ') {
262 *incl_ptr = *s == '+';
263 s += 2;
264 } else
265 *incl_ptr = xflags & XFLG_DEF_INCLUDE;
266
267 if (xflags & XFLG_WORD_SPLIT) {
268 const unsigned char *cp = s;
269 /* Token ends at whitespace or the end of the string. */
270 while (!isspace(*cp) && *cp != '\0')
271 cp++;
272 len = cp - s;
273 } else
274 len = strlen(s);
275
abca4eba 276 if (*p == '!' && len == 1 && !(xflags & XFLG_NO_PREFIXES))
96d3590a
WD
277 *incl_ptr = -1;
278
279 *len_ptr = len;
280 return (const char *)s;
f8f72644
WD
281}
282
283
b2aa573b
WD
284void add_exclude(struct exclude_list_struct *listp, const char *pattern,
285 int xflags)
c627d613 286{
b2aa573b 287 int pat_len, incl;
f8f72644 288 const char *cp;
5be7fa93 289
f8f72644 290 if (!pattern)
5e7dbaca 291 return;
f8f72644 292
b2aa573b
WD
293 cp = pattern;
294 pat_len = 0;
295 while (1) {
96d3590a
WD
296 cp = get_exclude_tok(cp + pat_len, &pat_len, &incl, xflags);
297 if (!pat_len)
b2aa573b
WD
298 break;
299 /* If we got the special "!" token, clear the list. */
300 if (incl < 0)
301 free_exclude_list(listp);
302 else {
303 make_exclude(listp, cp, pat_len, incl);
304
305 if (verbose > 2) {
67340e95 306 rprintf(FINFO, "[%s] add_exclude(%.*s, %s%s)\n",
24e1569f 307 who_am_i(), pat_len, cp,
67340e95 308 listp->debug_type,
b2aa573b
WD
309 incl ? "include" : "exclude");
310 }
f8f72644 311 }
8c35542d 312 }
c627d613
AT
313}
314
c627d613 315
b2aa573b 316void add_exclude_file(struct exclude_list_struct *listp, const char *fname,
f8f72644 317 int xflags)
c627d613 318{
5e7dbaca 319 FILE *fp;
2b6b4d53 320 char line[MAXPATHLEN];
ccdff3eb 321 char *eob = line + MAXPATHLEN - 1;
40d38dc0 322 int word_split = xflags & XFLG_WORD_SPLIT;
ccdff3eb 323
5be7fa93
WD
324 if (!fname || !*fname)
325 return;
326
327 if (*fname != '-' || fname[1])
5e7dbaca 328 fp = fopen(fname, "rb");
ccdff3eb 329 else
5e7dbaca
WD
330 fp = stdin;
331 if (!fp) {
f8f72644 332 if (xflags & XFLG_FATAL_ERRORS) {
a039749b 333 rsyserr(FERROR, errno,
0f2ac855 334 "failed to open %s file %s",
f8f72644 335 xflags & XFLG_DEF_INCLUDE ? "include" : "exclude",
0f2ac855 336 fname);
65417579 337 exit_cleanup(RERR_FILEIO);
2b6b4d53 338 }
5be7fa93 339 return;
2b6b4d53
AT
340 }
341
ccdff3eb 342 while (1) {
5e7dbaca
WD
343 char *s = line;
344 int ch;
ccdff3eb 345 while (1) {
5e7dbaca
WD
346 if ((ch = getc(fp)) == EOF) {
347 if (ferror(fp) && errno == EINTR)
ccdff3eb
WD
348 continue;
349 break;
350 }
40d38dc0
WD
351 if (word_split && isspace(ch))
352 break;
ccdff3eb
WD
353 if (eol_nulls? !ch : (ch == '\n' || ch == '\r'))
354 break;
355 if (s < eob)
356 *s++ = ch;
357 }
358 *s = '\0';
b2aa573b
WD
359 /* Skip lines starting with semicolon or pound. */
360 if (*line && *line != ';' && *line != '#')
f8f72644 361 add_exclude(listp, line, xflags);
5e7dbaca 362 if (ch == EOF)
ccdff3eb 363 break;
2b6b4d53 364 }
5e7dbaca 365 fclose(fp);
c627d613
AT
366}
367
368
369void send_exclude_list(int f)
370{
b2aa573b 371 struct exclude_struct *ent;
25cf8893 372
bb7c4fa3
MP
373 /* This is a complete hack - blame Rusty.
374 *
375 * FIXME: This pattern shows up in the output of
376 * report_exclude_result(), which is not ideal. */
5be7fa93 377 if (list_only && !recurse)
f8f72644 378 add_exclude(&exclude_list, "/*/*", 0);
2b6b4d53 379
b2aa573b 380 for (ent = exclude_list.head; ent; ent = ent->next) {
5f5be796 381 unsigned int l;
a3dbb20a 382 char p[MAXPATHLEN+1];
2fb139c1 383
b2aa573b 384 l = strlcpy(p, ent->pattern, sizeof p);
5f5be796
WD
385 if (l == 0 || l >= MAXPATHLEN)
386 continue;
b2aa573b 387 if (ent->directory) {
a3dbb20a
WD
388 p[l++] = '/';
389 p[l] = '\0';
5f5be796 390 }
2b6b4d53 391
b2aa573b 392 if (ent->include) {
a3dbb20a
WD
393 write_int(f, l + 2);
394 write_buf(f, "+ ", 2);
395 } else if ((*p == '-' || *p == '+') && p[1] == ' ') {
396 write_int(f, l + 2);
397 write_buf(f, "- ", 2);
398 } else
399 write_int(f, l);
400 write_buf(f, p, l);
0f2ac855 401 }
2b6b4d53 402
a3dbb20a 403 write_int(f, 0);
c627d613
AT
404}
405
406
407void recv_exclude_list(int f)
408{
5f5be796 409 char line[MAXPATHLEN+1]; /* Allows a trailing slash on a max-len dir */
9dd891bb
MP
410 unsigned int l;
411
5f5be796
WD
412 while ((l = read_int(f)) != 0) {
413 if (l >= sizeof line)
414 overflow("recv_exclude_list");
415 read_sbuf(f, line, l);
f8f72644 416 add_exclude(&exclude_list, line, 0);
651443a7 417 }
651443a7
DD
418}
419
0f2ac855 420
f8f72644
WD
421static char default_cvsignore[] =
422 /* These default ignored items come from the CVS manual. */
423 "RCS SCCS CVS CVS.adm RCSLOG cvslog.* tags TAGS"
424 " .make.state .nse_depinfo *~ #* .#* ,* _$* *$"
425 " *.old *.bak *.BAK *.orig *.rej .del-*"
426 " *.a *.olb *.o *.obj *.so *.exe"
427 " *.Z *.elc *.ln core"
428 /* The rest we added to suit ourself. */
429 " .svn/";
c627d613
AT
430
431void add_cvs_excludes(void)
432{
2b6b4d53
AT
433 char fname[MAXPATHLEN];
434 char *p;
0f2ac855 435
f8f72644
WD
436 add_exclude(&exclude_list, default_cvsignore,
437 XFLG_WORD_SPLIT | XFLG_NO_PREFIXES);
c627d613 438
a7725e6d 439 if ((p = getenv("HOME"))
f8f72644
WD
440 && pathjoin(fname, sizeof fname, p, ".cvsignore") < sizeof fname) {
441 add_exclude_file(&exclude_list, fname,
442 XFLG_WORD_SPLIT | XFLG_NO_PREFIXES);
443 }
c627d613 444
f8f72644
WD
445 add_exclude(&exclude_list, getenv("CVSIGNORE"),
446 XFLG_WORD_SPLIT | XFLG_NO_PREFIXES);
c627d613 447}