Make sure the log file is always opened before root privileges (if any)
[rsync/rsync.git] / exclude.c
CommitLineData
c627d613
AT
1/*
2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18*/
19
2b6b4d53
AT
20/* a lot of this stuff was originally derived from GNU tar, although
21 it has now changed so much that it is hard to tell :) */
c627d613 22
d567322f
MP
23/* include/exclude cluestick added by Martin Pool <mbp@samba.org> */
24
c627d613
AT
25#include "rsync.h"
26
27extern int verbose;
8458724d 28extern int delete_mode;
c627d613 29
2b6b4d53 30static struct exclude_struct **exclude_list;
c627d613 31
2b6b4d53
AT
32/* build an exclude structure given a exclude pattern */
33static struct exclude_struct *make_exclude(char *pattern, int include)
c627d613 34{
2b6b4d53 35 struct exclude_struct *ret;
c627d613 36
2b6b4d53
AT
37 ret = (struct exclude_struct *)malloc(sizeof(*ret));
38 if (!ret) out_of_memory("make_exclude");
c627d613 39
2b6b4d53
AT
40 memset(ret, 0, sizeof(*ret));
41
2b6b4d53
AT
42 if (strncmp(pattern,"- ",2) == 0) {
43 pattern += 2;
44 } else if (strncmp(pattern,"+ ",2) == 0) {
45 ret->include = 1;
46 pattern += 2;
47 } else {
48 ret->include = include;
49 }
50
51 ret->pattern = strdup(pattern);
52
587cb08d 53 if (!ret->pattern) out_of_memory("make_exclude");
c627d613 54
2bca43f6 55 if (strpbrk(pattern, "*[?")) {
2bca43f6 56 ret->regular_exp = 1;
9bec5286
AT
57 ret->fnmatch_flags = FNM_PATHNAME;
58 if (strstr(pattern, "**")) {
59 static int tested;
60 if (!tested) {
61 tested = 1;
62 if (fnmatch("a/b/*", "a/b/c/d", FNM_PATHNAME)==0) {
63 rprintf(FERROR,"WARNING: fnmatch FNM_PATHNAME is broken on your system\n");
64 }
65 }
66 ret->fnmatch_flags = 0;
67 }
2bca43f6 68 }
c627d613 69
2b6b4d53
AT
70 if (strlen(pattern) > 1 && pattern[strlen(pattern)-1] == '/') {
71 ret->pattern[strlen(pattern)-1] = 0;
72 ret->directory = 1;
73 }
c627d613 74
2b6b4d53
AT
75 if (!strchr(ret->pattern,'/')) {
76 ret->local = 1;
77 }
0944563e 78
2b6b4d53
AT
79 return ret;
80}
81
82static void free_exclude(struct exclude_struct *ex)
83{
2b6b4d53
AT
84 free(ex->pattern);
85 memset(ex,0,sizeof(*ex));
86 free(ex);
87}
c627d613 88
d567322f
MP
89static int check_one_exclude(char *name, struct exclude_struct *ex,
90 STRUCT_STAT *st)
2b6b4d53
AT
91{
92 char *p;
93 int match_start=0;
94 char *pattern = ex->pattern;
95
96 if (ex->local && (p=strrchr(name,'/')))
97 name = p+1;
98
99 if (!name[0]) return 0;
100
101 if (ex->directory && !S_ISDIR(st->st_mode)) return 0;
102
103 if (*pattern == '/' && *name != '/') {
104 match_start = 1;
105 pattern++;
106 }
107
108 if (ex->regular_exp) {
c36cd317 109 if (fnmatch(pattern, name, ex->fnmatch_flags) == 0) {
2b6b4d53 110 return 1;
c36cd317 111 }
2b6b4d53
AT
112 } else {
113 int l1 = strlen(name);
ea2111d1 114 int l2 = strlen(pattern);
2b6b4d53 115 if (l2 <= l1 &&
ea2111d1 116 strcmp(name+(l1-l2),pattern) == 0 &&
c36cd317 117 (l1==l2 || (!match_start && name[l1-(l2+1)] == '/'))) {
2b6b4d53 118 return 1;
c36cd317 119 }
2b6b4d53
AT
120 }
121
122 return 0;
c627d613
AT
123}
124
125
d567322f
MP
126static void report_exclude_result(char const *name,
127 struct exclude_struct const *ent,
128 STRUCT_STAT const *st)
129{
130 /* If a trailing slash is present to match only directories,
131 * then it is stripped out by make_exclude. So as a special
132 * case we add it back in here. */
133
134 if (verbose >= 2)
135 rprintf(FINFO, "%s %s %s because of pattern %s%s\n",
136 ent->include ? "including" : "excluding",
137 S_ISDIR(st->st_mode) ? "directory" : "file",
138 name, ent->pattern,
139 ent->directory ? "/" : "");
140}
141
142
143/*
144 * Return true if file NAME is defined to be excluded by either
145 * LOCAL_EXCLUDE_LIST or the globals EXCLUDE_LIST.
146 */
147int check_exclude(char *name, struct exclude_struct **local_exclude_list,
2b6b4d53 148 STRUCT_STAT *st)
c627d613 149{
2b6b4d53 150 int n;
1f52f4c4 151 struct exclude_struct *ent;
c627d613 152
5d5811f7
DD
153 if (name && (name[0] == '.') && !name[1])
154 /* never exclude '.', even if somebody does --exclude '*' */
155 return 0;
156
2b6b4d53 157 if (exclude_list) {
d567322f
MP
158 for (n=0; exclude_list[n]; n++) {
159 ent = exclude_list[n];
160 if (check_one_exclude(name, ent, st)) {
161 report_exclude_result(name, ent, st);
162 return !ent->include;
163 }
164 }
2b6b4d53 165 }
c627d613 166
2b6b4d53 167 if (local_exclude_list) {
d567322f
MP
168 for (n=0; local_exclude_list[n]; n++) {
169 ent = exclude_list[n];
170 if (check_one_exclude(name, ent, st)) {
171 report_exclude_result(name, ent, st);
172 return !ent->include;
173 }
174 }
2b6b4d53 175 }
c627d613 176
2b6b4d53 177 return 0;
c627d613
AT
178}
179
180
2b6b4d53 181void add_exclude_list(char *pattern,struct exclude_struct ***list, int include)
c627d613 182{
2b6b4d53
AT
183 int len=0;
184 if (list && *list)
185 for (; (*list)[len]; len++) ;
186
187 if (strcmp(pattern,"!") == 0) {
188 if (verbose > 2)
189 rprintf(FINFO,"clearing exclude list\n");
190 while ((len)--) {
191 free_exclude((*list)[len]);
192 }
193 free((*list));
194 *list = NULL;
195 return;
196 }
197
fe8c0a98 198 *list = (struct exclude_struct **)Realloc(*list,sizeof(struct exclude_struct *)*(len+2));
2b6b4d53
AT
199
200 if (!*list || !((*list)[len] = make_exclude(pattern, include)))
201 out_of_memory("add_exclude");
202
203 if (verbose > 2)
204 rprintf(FINFO,"add_exclude(%s)\n",pattern);
205
206 (*list)[len+1] = NULL;
c627d613
AT
207}
208
2b6b4d53 209void add_exclude(char *pattern, int include)
c627d613 210{
2b6b4d53 211 add_exclude_list(pattern,&exclude_list, include);
c627d613
AT
212}
213
2b6b4d53
AT
214struct exclude_struct **make_exclude_list(char *fname,
215 struct exclude_struct **list1,
216 int fatal, int include)
c627d613 217{
2b6b4d53
AT
218 struct exclude_struct **list=list1;
219 FILE *f = fopen(fname,"r");
220 char line[MAXPATHLEN];
221 if (!f) {
222 if (fatal) {
223 rprintf(FERROR,"%s : %s\n",fname,strerror(errno));
65417579 224 exit_cleanup(RERR_FILEIO);
2b6b4d53
AT
225 }
226 return list;
227 }
228
229 while (fgets(line,MAXPATHLEN,f)) {
230 int l = strlen(line);
231 if (l && line[l-1] == '\n') l--;
232 line[l] = 0;
122f19a6
DD
233 if (line[0] && (line[0] != ';') && (line[0] != '#')) {
234 /* Skip lines starting with semicolon or pound.
235 It probably wouldn't cause any harm to not skip
236 them but there's no need to save them. */
237 add_exclude_list(line,&list,include);
238 }
2b6b4d53
AT
239 }
240 fclose(f);
241 return list;
c627d613
AT
242}
243
244
2b6b4d53 245void add_exclude_file(char *fname,int fatal,int include)
c627d613 246{
8f3a2d54
AT
247 if (!fname || !*fname) return;
248
2b6b4d53 249 exclude_list = make_exclude_list(fname,exclude_list,fatal,include);
c627d613
AT
250}
251
252
253void send_exclude_list(int f)
254{
2b6b4d53
AT
255 int i;
256 extern int remote_version;
25cf8893
AT
257 extern int list_only, recurse;
258
259 /* this is a complete hack - blame Rusty */
260 if (list_only && !recurse) {
261 add_exclude("/*/*", 0);
262 }
2b6b4d53
AT
263
264 if (!exclude_list) {
265 write_int(f,0);
266 return;
267 }
268
269 for (i=0;exclude_list[i];i++) {
2b6b4d53 270 int l;
2fb139c1
AT
271 char pattern[MAXPATHLEN];
272
273 strlcpy(pattern,exclude_list[i]->pattern,sizeof(pattern));
274 if (exclude_list[i]->directory) strlcat(pattern,"/", sizeof(pattern));
2b6b4d53 275
587cb08d
DD
276 l = strlen(pattern);
277 if (l == 0) continue;
278 if (exclude_list[i]->include) {
279 if (remote_version < 19) {
2b6b4d53 280 rprintf(FERROR,"remote rsync does not support include syntax - aborting\n");
ec9df380 281 exit_cleanup(RERR_UNSUPPORTED);
2b6b4d53 282 }
587cb08d
DD
283 write_int(f,l+2);
284 write_buf(f,"+ ",2);
285 } else {
286 write_int(f,l);
2b6b4d53 287 }
2b6b4d53
AT
288 write_buf(f,pattern,l);
289 }
290
291 write_int(f,0);
c627d613
AT
292}
293
294
295void recv_exclude_list(int f)
296{
2b6b4d53
AT
297 char line[MAXPATHLEN];
298 int l;
299 while ((l=read_int(f))) {
300 if (l >= MAXPATHLEN) overflow("recv_exclude_list");
301 read_sbuf(f,line,l);
302 add_exclude(line,0);
303 }
c627d613
AT
304}
305
651443a7
DD
306/* Get the next include/exclude arg from the string. It works in a similar way
307** to strtok - initially an arg is sent over, from then on NULL. This
308** routine takes into account any +/- in the strings and does not
309** consider the space following it as a delimeter.
310*/
311char *get_exclude_tok(char *p)
312{
313 static char *s;
314 static int more;
315 char *t;
316
317 if (p) {
318 s=p;
319 if (*p)
320 more=1;
321 }
322
323 if (!more)
324 return(NULL);
c627d613 325
651443a7
DD
326 /* Skip over any initial spaces */
327 while(isspace(*s))
328 s++;
329
330 /* Are we at the end of the string? */
331 if (*s) {
332 /* remember the beginning of the token */
333 t=s;
334
335 /* Is this a '+' or '-' followed by a space (not whitespace)? */
336 if ((*s=='+' || *s=='-') && *(s+1)==' ')
337 s+=2;
338
339 /* Skip to the next space or the end of the string */
340 while(!isspace(*s) && *s!='\0')
341 s++;
342 } else {
343 t=NULL;
344 }
345
346 /* Have we reached the end of the string? */
347 if (*s)
348 *s++='\0';
349 else
350 more=0;
351 return(t);
352}
353
354
8f3a2d54
AT
355void add_exclude_line(char *p)
356{
357 char *tok;
358 if (!p || !*p) return;
359 p = strdup(p);
360 if (!p) out_of_memory("add_exclude_line");
651443a7 361 for (tok=get_exclude_tok(p); tok; tok=get_exclude_tok(NULL))
2b6b4d53 362 add_exclude(tok, 0);
8f3a2d54
AT
363 free(p);
364}
365
cd64343a
DD
366void add_include_line(char *p)
367{
368 char *tok;
369 if (!p || !*p) return;
370 p = strdup(p);
371 if (!p) out_of_memory("add_include_line");
651443a7 372 for (tok=get_exclude_tok(p); tok; tok=get_exclude_tok(NULL))
cd64343a
DD
373 add_exclude(tok, 1);
374 free(p);
375}
376
8f3a2d54 377
c627d613
AT
378static char *cvs_ignore_list[] = {
379 "RCS","SCCS","CVS","CVS.adm","RCSLOG","cvslog.*",
380 "tags","TAGS",".make.state",".nse_depinfo",
381 "*~", "#*", ".#*", ",*", "*.old", "*.bak", "*.BAK", "*.orig",
382 "*.rej", ".del-*", "*.a", "*.o", "*.obj", "*.so", "*.Z", "*.elc", "*.ln",
383 "core",NULL};
384
385
386
387void add_cvs_excludes(void)
388{
2b6b4d53
AT
389 char fname[MAXPATHLEN];
390 char *p;
391 int i;
c627d613 392
2b6b4d53
AT
393 for (i=0; cvs_ignore_list[i]; i++)
394 add_exclude(cvs_ignore_list[i], 0);
c627d613 395
2b6b4d53 396 if ((p=getenv("HOME")) && strlen(p) < (MAXPATHLEN-12)) {
37f9805d 397 slprintf(fname,sizeof(fname), "%s/.cvsignore",p);
2b6b4d53
AT
398 add_exclude_file(fname,0,0);
399 }
c627d613 400
2b6b4d53 401 add_exclude_line(getenv("CVSIGNORE"));
c627d613 402}