Merged in the security fixes from 2.5.7.
[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;
135 snprintf(full_name, sizeof full_name,
136 "%s/%s", curr_dir+plus, name);
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,
5be7fa93 201 int name_is_dir)
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
207 if (verbose >= 2)
208 rprintf(FINFO, "%s %s %s because of pattern %s%s\n",
209 ent->include ? "including" : "excluding",
5be7fa93 210 name_is_dir ? "directory" : "file",
0f2ac855
WD
211 name, ent->pattern,
212 ent->directory ? "/" : "");
d567322f
MP
213}
214
215
216/*
217 * Return true if file NAME is defined to be excluded by either
218 * LOCAL_EXCLUDE_LIST or the globals EXCLUDE_LIST.
219 */
5be7fa93 220int check_exclude(struct exclude_struct **list, char *name, int name_is_dir)
c627d613 221{
0f2ac855 222 struct exclude_struct *ent;
c627d613 223
5be7fa93
WD
224 while ((ent = *list++) != NULL) {
225 if (check_one_exclude(name, ent, name_is_dir)) {
226 report_exclude_result(name, ent, name_is_dir);
227 return !ent->include;
0f2ac855 228 }
2b6b4d53 229 }
c627d613 230
2b6b4d53 231 return 0;
c627d613
AT
232}
233
234
5be7fa93 235void add_exclude(struct exclude_struct ***listp, const char *pattern, int include)
c627d613 236{
5be7fa93
WD
237 struct exclude_struct **list = *listp;
238 int len = 0;
239
240 if (*pattern == '!' && !pattern[1]) {
241 free_exclude_list(listp);
242 return;
2b6b4d53
AT
243 }
244
5be7fa93
WD
245 if (list)
246 for (; list[len]; len++) {}
0f2ac855 247
58cadc86 248 list = *listp = realloc_array(list, struct exclude_struct *, len+2);
5be7fa93
WD
249
250 if (!list || !(list[len] = make_exclude(pattern, include)))
2b6b4d53 251 out_of_memory("add_exclude");
0f2ac855 252
8c35542d
MP
253 if (verbose > 2) {
254 rprintf(FINFO,"add_exclude(%s,%s)\n",pattern,
0f2ac855 255 include ? "include" : "exclude");
8c35542d
MP
256 }
257
5be7fa93 258 list[len+1] = NULL;
c627d613
AT
259}
260
c627d613 261
5be7fa93
WD
262void add_exclude_file(struct exclude_struct ***listp, const char *fname,
263 int fatal, int include)
c627d613 264{
ccdff3eb 265 int fd;
2b6b4d53 266 char line[MAXPATHLEN];
ccdff3eb
WD
267 char *eob = line + MAXPATHLEN - 1;
268 extern int eol_nulls;
269
5be7fa93
WD
270 if (!fname || !*fname)
271 return;
272
273 if (*fname != '-' || fname[1])
ccdff3eb
WD
274 fd = open(fname, O_RDONLY|O_BINARY);
275 else
276 fd = 0;
277 if (fd < 0) {
2b6b4d53 278 if (fatal) {
a039749b 279 rsyserr(FERROR, errno,
0f2ac855
WD
280 "failed to open %s file %s",
281 include ? "include" : "exclude",
282 fname);
65417579 283 exit_cleanup(RERR_FILEIO);
2b6b4d53 284 }
5be7fa93 285 return;
2b6b4d53
AT
286 }
287
ccdff3eb
WD
288 while (1) {
289 char ch, *s = line;
290 int cnt;
291 while (1) {
292 if ((cnt = read(fd, &ch, 1)) <= 0) {
293 if (cnt < 0 && errno == EINTR)
294 continue;
295 break;
296 }
297 if (eol_nulls? !ch : (ch == '\n' || ch == '\r'))
298 break;
299 if (s < eob)
300 *s++ = ch;
301 }
302 *s = '\0';
303 if (*line && *line != ';' && *line != '#') {
122f19a6 304 /* Skip lines starting with semicolon or pound.
ccdff3eb
WD
305 * It probably wouldn't cause any harm to not skip
306 * them but there's no need to save them. */
5be7fa93 307 add_exclude(listp, line, include);
122f19a6 308 }
ccdff3eb
WD
309 if (cnt <= 0)
310 break;
2b6b4d53 311 }
ccdff3eb 312 close(fd);
c627d613
AT
313}
314
315
316void send_exclude_list(int f)
317{
2b6b4d53 318 int i;
d04e9c51 319 extern int protocol_version;
25cf8893
AT
320 extern int list_only, recurse;
321
bb7c4fa3
MP
322 /* This is a complete hack - blame Rusty.
323 *
324 * FIXME: This pattern shows up in the output of
325 * report_exclude_result(), which is not ideal. */
5be7fa93
WD
326 if (list_only && !recurse)
327 add_exclude(&exclude_list, "/*/*", ADD_EXCLUDE);
2b6b4d53
AT
328
329 if (!exclude_list) {
330 write_int(f,0);
331 return;
332 }
333
334 for (i=0;exclude_list[i];i++) {
2b6b4d53 335 int l;
2fb139c1
AT
336 char pattern[MAXPATHLEN];
337
0f2ac855 338 strlcpy(pattern,exclude_list[i]->pattern,sizeof(pattern));
2fb139c1 339 if (exclude_list[i]->directory) strlcat(pattern,"/", sizeof(pattern));
2b6b4d53 340
587cb08d
DD
341 l = strlen(pattern);
342 if (l == 0) continue;
343 if (exclude_list[i]->include) {
d04e9c51 344 if (protocol_version < 19) {
2b6b4d53 345 rprintf(FERROR,"remote rsync does not support include syntax - aborting\n");
ec9df380 346 exit_cleanup(RERR_UNSUPPORTED);
2b6b4d53 347 }
587cb08d
DD
348 write_int(f,l+2);
349 write_buf(f,"+ ",2);
350 } else {
351 write_int(f,l);
2b6b4d53 352 }
2b6b4d53 353 write_buf(f,pattern,l);
0f2ac855 354 }
2b6b4d53
AT
355
356 write_int(f,0);
c627d613
AT
357}
358
359
360void recv_exclude_list(int f)
361{
2b6b4d53 362 char line[MAXPATHLEN];
9dd891bb
MP
363 unsigned int l;
364
2b6b4d53
AT
365 while ((l=read_int(f))) {
366 if (l >= MAXPATHLEN) overflow("recv_exclude_list");
367 read_sbuf(f,line,l);
5be7fa93 368 add_exclude(&exclude_list, line, ADD_EXCLUDE);
2b6b4d53 369 }
c627d613
AT
370}
371
651443a7
DD
372/* Get the next include/exclude arg from the string. It works in a similar way
373** to strtok - initially an arg is sent over, from then on NULL. This
374** routine takes into account any +/- in the strings and does not
375** consider the space following it as a delimeter.
376*/
377char *get_exclude_tok(char *p)
378{
379 static char *s;
380 static int more;
381 char *t;
382
383 if (p) {
384 s=p;
385 if (*p)
386 more=1;
387 }
388
389 if (!more)
390 return(NULL);
c627d613 391
651443a7 392 /* Skip over any initial spaces */
32f76175 393 while (isspace(* (unsigned char *) s))
651443a7
DD
394 s++;
395
396 /* Are we at the end of the string? */
397 if (*s) {
398 /* remember the beginning of the token */
399 t=s;
400
401 /* Is this a '+' or '-' followed by a space (not whitespace)? */
402 if ((*s=='+' || *s=='-') && *(s+1)==' ')
403 s+=2;
0f2ac855 404
651443a7 405 /* Skip to the next space or the end of the string */
32f76175 406 while (!isspace(* (unsigned char *) s) && *s != '\0')
651443a7
DD
407 s++;
408 } else {
409 t=NULL;
410 }
411
412 /* Have we reached the end of the string? */
413 if (*s)
414 *s++='\0';
415 else
416 more=0;
417 return(t);
418}
419
0f2ac855 420
5be7fa93
WD
421void add_exclude_line(struct exclude_struct ***listp,
422 const char *line, int include)
8f3a2d54 423{
5be7fa93
WD
424 char *tok, *p;
425 if (!line || !*line) return;
426 p = strdup(line);
8f3a2d54 427 if (!p) out_of_memory("add_exclude_line");
0f2ac855 428 for (tok=get_exclude_tok(p); tok; tok=get_exclude_tok(NULL))
5be7fa93 429 add_exclude(listp, tok, include);
cd64343a
DD
430 free(p);
431}
432
8f3a2d54 433
c627d613 434static char *cvs_ignore_list[] = {
95dd949c
WD
435 "RCS/", "SCCS/", "CVS/", ".svn/", "CVS.adm", "RCSLOG", "cvslog.*",
436 "tags", "TAGS", ".make.state", ".nse_depinfo",
437 "*~", "#*", ".#*", ", *", "*.old", "*.bak", "*.BAK", "*.orig",
c627d613 438 "*.rej", ".del-*", "*.a", "*.o", "*.obj", "*.so", "*.Z", "*.elc", "*.ln",
95dd949c 439 "core", NULL};
c627d613
AT
440
441
442void add_cvs_excludes(void)
443{
2b6b4d53
AT
444 char fname[MAXPATHLEN];
445 char *p;
446 int i;
0f2ac855 447
2b6b4d53 448 for (i=0; cvs_ignore_list[i]; i++)
5be7fa93 449 add_exclude(&exclude_list, cvs_ignore_list[i], ADD_EXCLUDE);
c627d613 450
2b6b4d53 451 if ((p=getenv("HOME")) && strlen(p) < (MAXPATHLEN-12)) {
8950ac03 452 snprintf(fname,sizeof(fname), "%s/.cvsignore",p);
5be7fa93 453 add_exclude_file(&exclude_list,fname,MISSING_OK,ADD_EXCLUDE);
2b6b4d53 454 }
c627d613 455
5be7fa93 456 add_exclude_line(&exclude_list, getenv("CVSIGNORE"), ADD_EXCLUDE);
c627d613 457}