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