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