Always include "." when processing exclude lists. This avoids confusion
[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
AT
22
23#include "rsync.h"
24
25extern int verbose;
26
2b6b4d53 27static struct exclude_struct **exclude_list;
c627d613 28
2bca43f6
DD
29/*
30 * Optimization for special case when all included files are explicitly
31 * listed without wildcards in the "exclude" list followed by a "- *"
32 * to exclude the rest.
33 * Contributed by Dave Dykstra <dwd@bell-labs.com>
34 */
35static int only_included_files = 1;
36static struct exclude_struct *exclude_the_rest;
37
38int send_included_file_names(int f,struct file_list *flist)
39{
40 struct exclude_struct *ex, **ex_list;
41 int n;
42 char *p;
43
44 if (!only_included_files || (exclude_the_rest == NULL))
45 return 0;
46
47 if (verbose > 1) {
48 rprintf(FINFO,"(using include-only optimization) ");
49 }
50
51 /* set exclude_list to NULL temporarily so check_exclude */
52 /* will always return true */
53 ex_list = exclude_list;
54 exclude_list = NULL;
55 for (n=0; (ex = ex_list[n]) != NULL; n++) {
56 if (ex == exclude_the_rest)
57 break;
58 p = ex->pattern;
59 while (*p == '/') {
60 /* skip the allowed beginning slashes */
61 p++;
62 }
63 send_file_name(f,flist,p,0,0);
64 }
65 exclude_list = ex_list;
66
67 return 1;
68}
69
2b6b4d53
AT
70/* build an exclude structure given a exclude pattern */
71static struct exclude_struct *make_exclude(char *pattern, int include)
c627d613 72{
2b6b4d53 73 struct exclude_struct *ret;
c627d613 74
2b6b4d53
AT
75 ret = (struct exclude_struct *)malloc(sizeof(*ret));
76 if (!ret) out_of_memory("make_exclude");
c627d613 77
2b6b4d53
AT
78 memset(ret, 0, sizeof(*ret));
79
2b6b4d53
AT
80 if (strncmp(pattern,"- ",2) == 0) {
81 pattern += 2;
82 } else if (strncmp(pattern,"+ ",2) == 0) {
83 ret->include = 1;
84 pattern += 2;
85 } else {
86 ret->include = include;
87 }
88
89 ret->pattern = strdup(pattern);
90
587cb08d 91 if (!ret->pattern) out_of_memory("make_exclude");
c627d613 92
2bca43f6
DD
93 if (strpbrk(pattern, "*[?")) {
94 if (!ret->include && (*pattern == '*') && (*(pattern+1) == '\0')) {
95 exclude_the_rest = ret;
96 } else {
97 only_included_files = 0;
98 }
99 ret->regular_exp = 1;
100 } else if (!ret->include) {
101 only_included_files = 0;
102 }
c627d613 103
2b6b4d53
AT
104 if (strlen(pattern) > 1 && pattern[strlen(pattern)-1] == '/') {
105 ret->pattern[strlen(pattern)-1] = 0;
106 ret->directory = 1;
107 }
c627d613 108
2b6b4d53
AT
109 if (!strchr(ret->pattern,'/')) {
110 ret->local = 1;
111 }
0944563e 112
2b6b4d53
AT
113 return ret;
114}
115
116static void free_exclude(struct exclude_struct *ex)
117{
2b6b4d53
AT
118 free(ex->pattern);
119 memset(ex,0,sizeof(*ex));
120 free(ex);
121}
c627d613 122
2b6b4d53
AT
123static int check_one_exclude(char *name,struct exclude_struct *ex,
124 STRUCT_STAT *st)
125{
126 char *p;
127 int match_start=0;
128 char *pattern = ex->pattern;
129
130 if (ex->local && (p=strrchr(name,'/')))
131 name = p+1;
132
133 if (!name[0]) return 0;
134
135 if (ex->directory && !S_ISDIR(st->st_mode)) return 0;
136
137 if (*pattern == '/' && *name != '/') {
138 match_start = 1;
139 pattern++;
140 }
141
142 if (ex->regular_exp) {
ea2111d1 143 if (fnmatch(pattern, name, 0) == 0)
2b6b4d53
AT
144 return 1;
145 } else {
146 int l1 = strlen(name);
ea2111d1 147 int l2 = strlen(pattern);
2b6b4d53 148 if (l2 <= l1 &&
ea2111d1 149 strcmp(name+(l1-l2),pattern) == 0 &&
2b6b4d53
AT
150 (l1==l2 || (!match_start && name[l1-(l2+1)] == '/')))
151 return 1;
152 }
153
154 return 0;
c627d613
AT
155}
156
157
2b6b4d53
AT
158int check_exclude(char *name,struct exclude_struct **local_exclude_list,
159 STRUCT_STAT *st)
c627d613 160{
2b6b4d53 161 int n;
c627d613 162
5d5811f7
DD
163 if (name && (name[0] == '.') && !name[1])
164 /* never exclude '.', even if somebody does --exclude '*' */
165 return 0;
166
2b6b4d53
AT
167 if (exclude_list) {
168 for (n=0; exclude_list[n]; n++)
169 if (check_one_exclude(name,exclude_list[n],st))
170 return !exclude_list[n]->include;
171 }
c627d613 172
2b6b4d53
AT
173 if (local_exclude_list) {
174 for (n=0; local_exclude_list[n]; n++)
175 if (check_one_exclude(name,local_exclude_list[n],st))
176 return !local_exclude_list[n]->include;
177 }
c627d613 178
2b6b4d53 179 return 0;
c627d613
AT
180}
181
182
2b6b4d53 183void add_exclude_list(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;
2bca43f6
DD
197 only_included_files = 1;
198 exclude_the_rest = NULL;
2b6b4d53
AT
199 return;
200 }
201
fe8c0a98 202 *list = (struct exclude_struct **)Realloc(*list,sizeof(struct exclude_struct *)*(len+2));
2b6b4d53
AT
203
204 if (!*list || !((*list)[len] = make_exclude(pattern, include)))
205 out_of_memory("add_exclude");
206
207 if (verbose > 2)
208 rprintf(FINFO,"add_exclude(%s)\n",pattern);
209
210 (*list)[len+1] = NULL;
c627d613
AT
211}
212
2b6b4d53 213void add_exclude(char *pattern, int include)
c627d613 214{
2b6b4d53 215 add_exclude_list(pattern,&exclude_list, include);
c627d613
AT
216}
217
2b6b4d53
AT
218struct exclude_struct **make_exclude_list(char *fname,
219 struct exclude_struct **list1,
220 int fatal, int include)
c627d613 221{
2b6b4d53
AT
222 struct exclude_struct **list=list1;
223 FILE *f = fopen(fname,"r");
224 char line[MAXPATHLEN];
225 if (!f) {
226 if (fatal) {
227 rprintf(FERROR,"%s : %s\n",fname,strerror(errno));
65417579 228 exit_cleanup(RERR_FILEIO);
2b6b4d53
AT
229 }
230 return list;
231 }
232
233 while (fgets(line,MAXPATHLEN,f)) {
234 int l = strlen(line);
235 if (l && line[l-1] == '\n') l--;
236 line[l] = 0;
237 if (line[0]) add_exclude_list(line,&list,include);
238 }
239 fclose(f);
240 return list;
c627d613
AT
241}
242
243
2b6b4d53 244void add_exclude_file(char *fname,int fatal,int include)
c627d613 245{
8f3a2d54
AT
246 if (!fname || !*fname) return;
247
2b6b4d53 248 exclude_list = make_exclude_list(fname,exclude_list,fatal,include);
c627d613
AT
249}
250
251
252void send_exclude_list(int f)
253{
2b6b4d53
AT
254 int i;
255 extern int remote_version;
256
257 if (!exclude_list) {
258 write_int(f,0);
259 return;
260 }
261
262 for (i=0;exclude_list[i];i++) {
587cb08d 263 char *pattern = exclude_list[i]->pattern;
2b6b4d53
AT
264 int l;
265
587cb08d
DD
266 l = strlen(pattern);
267 if (l == 0) continue;
268 if (exclude_list[i]->include) {
269 if (remote_version < 19) {
2b6b4d53 270 rprintf(FERROR,"remote rsync does not support include syntax - aborting\n");
ec9df380 271 exit_cleanup(RERR_UNSUPPORTED);
2b6b4d53 272 }
587cb08d
DD
273 write_int(f,l+2);
274 write_buf(f,"+ ",2);
275 } else {
276 write_int(f,l);
2b6b4d53 277 }
2b6b4d53
AT
278 write_buf(f,pattern,l);
279 }
280
281 write_int(f,0);
c627d613
AT
282}
283
284
285void recv_exclude_list(int f)
286{
2b6b4d53
AT
287 char line[MAXPATHLEN];
288 int l;
289 while ((l=read_int(f))) {
290 if (l >= MAXPATHLEN) overflow("recv_exclude_list");
291 read_sbuf(f,line,l);
292 add_exclude(line,0);
293 }
c627d613
AT
294}
295
296
8f3a2d54
AT
297void add_exclude_line(char *p)
298{
299 char *tok;
300 if (!p || !*p) return;
301 p = strdup(p);
302 if (!p) out_of_memory("add_exclude_line");
303 for (tok=strtok(p," "); tok; tok=strtok(NULL," "))
2b6b4d53 304 add_exclude(tok, 0);
8f3a2d54
AT
305 free(p);
306}
307
cd64343a
DD
308void add_include_line(char *p)
309{
310 char *tok;
311 if (!p || !*p) return;
312 p = strdup(p);
313 if (!p) out_of_memory("add_include_line");
314 for (tok=strtok(p," "); tok; tok=strtok(NULL," "))
315 add_exclude(tok, 1);
316 free(p);
317}
318
8f3a2d54 319
c627d613
AT
320static char *cvs_ignore_list[] = {
321 "RCS","SCCS","CVS","CVS.adm","RCSLOG","cvslog.*",
322 "tags","TAGS",".make.state",".nse_depinfo",
323 "*~", "#*", ".#*", ",*", "*.old", "*.bak", "*.BAK", "*.orig",
324 "*.rej", ".del-*", "*.a", "*.o", "*.obj", "*.so", "*.Z", "*.elc", "*.ln",
325 "core",NULL};
326
327
328
329void add_cvs_excludes(void)
330{
2b6b4d53
AT
331 char fname[MAXPATHLEN];
332 char *p;
333 int i;
c627d613 334
2b6b4d53
AT
335 for (i=0; cvs_ignore_list[i]; i++)
336 add_exclude(cvs_ignore_list[i], 0);
c627d613 337
2b6b4d53 338 if ((p=getenv("HOME")) && strlen(p) < (MAXPATHLEN-12)) {
37f9805d 339 slprintf(fname,sizeof(fname), "%s/.cvsignore",p);
2b6b4d53
AT
340 add_exclude_file(fname,0,0);
341 }
c627d613 342
2b6b4d53 343 add_exclude_line(getenv("CVSIGNORE"));
c627d613 344}