Added provision for packaging for Linux Standards Base compliant Linux systems.
[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;
8458724d 26extern int delete_mode;
c627d613 27
2b6b4d53 28static struct exclude_struct **exclude_list;
c627d613 29
2b6b4d53
AT
30/* build an exclude structure given a exclude pattern */
31static struct exclude_struct *make_exclude(char *pattern, int include)
c627d613 32{
2b6b4d53 33 struct exclude_struct *ret;
c627d613 34
2b6b4d53
AT
35 ret = (struct exclude_struct *)malloc(sizeof(*ret));
36 if (!ret) out_of_memory("make_exclude");
c627d613 37
2b6b4d53
AT
38 memset(ret, 0, sizeof(*ret));
39
2b6b4d53
AT
40 if (strncmp(pattern,"- ",2) == 0) {
41 pattern += 2;
42 } else if (strncmp(pattern,"+ ",2) == 0) {
43 ret->include = 1;
44 pattern += 2;
45 } else {
46 ret->include = include;
47 }
48
49 ret->pattern = strdup(pattern);
50
587cb08d 51 if (!ret->pattern) out_of_memory("make_exclude");
c627d613 52
2bca43f6 53 if (strpbrk(pattern, "*[?")) {
2bca43f6 54 ret->regular_exp = 1;
9bec5286
AT
55 ret->fnmatch_flags = FNM_PATHNAME;
56 if (strstr(pattern, "**")) {
57 static int tested;
58 if (!tested) {
59 tested = 1;
60 if (fnmatch("a/b/*", "a/b/c/d", FNM_PATHNAME)==0) {
61 rprintf(FERROR,"WARNING: fnmatch FNM_PATHNAME is broken on your system\n");
62 }
63 }
64 ret->fnmatch_flags = 0;
65 }
2bca43f6 66 }
c627d613 67
2b6b4d53
AT
68 if (strlen(pattern) > 1 && pattern[strlen(pattern)-1] == '/') {
69 ret->pattern[strlen(pattern)-1] = 0;
70 ret->directory = 1;
71 }
c627d613 72
2b6b4d53
AT
73 if (!strchr(ret->pattern,'/')) {
74 ret->local = 1;
75 }
0944563e 76
2b6b4d53
AT
77 return ret;
78}
79
80static void free_exclude(struct exclude_struct *ex)
81{
2b6b4d53
AT
82 free(ex->pattern);
83 memset(ex,0,sizeof(*ex));
84 free(ex);
85}
c627d613 86
2b6b4d53
AT
87static int check_one_exclude(char *name,struct exclude_struct *ex,
88 STRUCT_STAT *st)
89{
90 char *p;
91 int match_start=0;
92 char *pattern = ex->pattern;
93
94 if (ex->local && (p=strrchr(name,'/')))
95 name = p+1;
96
97 if (!name[0]) return 0;
98
99 if (ex->directory && !S_ISDIR(st->st_mode)) return 0;
100
101 if (*pattern == '/' && *name != '/') {
102 match_start = 1;
103 pattern++;
104 }
105
106 if (ex->regular_exp) {
c36cd317 107 if (fnmatch(pattern, name, ex->fnmatch_flags) == 0) {
2b6b4d53 108 return 1;
c36cd317 109 }
2b6b4d53
AT
110 } else {
111 int l1 = strlen(name);
ea2111d1 112 int l2 = strlen(pattern);
2b6b4d53 113 if (l2 <= l1 &&
ea2111d1 114 strcmp(name+(l1-l2),pattern) == 0 &&
c36cd317 115 (l1==l2 || (!match_start && name[l1-(l2+1)] == '/'))) {
2b6b4d53 116 return 1;
c36cd317 117 }
2b6b4d53
AT
118 }
119
120 return 0;
c627d613
AT
121}
122
123
2b6b4d53
AT
124int check_exclude(char *name,struct exclude_struct **local_exclude_list,
125 STRUCT_STAT *st)
c627d613 126{
2b6b4d53 127 int n;
c627d613 128
5d5811f7
DD
129 if (name && (name[0] == '.') && !name[1])
130 /* never exclude '.', even if somebody does --exclude '*' */
131 return 0;
132
2b6b4d53
AT
133 if (exclude_list) {
134 for (n=0; exclude_list[n]; n++)
135 if (check_one_exclude(name,exclude_list[n],st))
136 return !exclude_list[n]->include;
137 }
c627d613 138
2b6b4d53
AT
139 if (local_exclude_list) {
140 for (n=0; local_exclude_list[n]; n++)
141 if (check_one_exclude(name,local_exclude_list[n],st))
142 return !local_exclude_list[n]->include;
143 }
c627d613 144
2b6b4d53 145 return 0;
c627d613
AT
146}
147
148
2b6b4d53 149void add_exclude_list(char *pattern,struct exclude_struct ***list, int include)
c627d613 150{
2b6b4d53
AT
151 int len=0;
152 if (list && *list)
153 for (; (*list)[len]; len++) ;
154
155 if (strcmp(pattern,"!") == 0) {
156 if (verbose > 2)
157 rprintf(FINFO,"clearing exclude list\n");
158 while ((len)--) {
159 free_exclude((*list)[len]);
160 }
161 free((*list));
162 *list = NULL;
163 return;
164 }
165
fe8c0a98 166 *list = (struct exclude_struct **)Realloc(*list,sizeof(struct exclude_struct *)*(len+2));
2b6b4d53
AT
167
168 if (!*list || !((*list)[len] = make_exclude(pattern, include)))
169 out_of_memory("add_exclude");
170
171 if (verbose > 2)
172 rprintf(FINFO,"add_exclude(%s)\n",pattern);
173
174 (*list)[len+1] = NULL;
c627d613
AT
175}
176
2b6b4d53 177void add_exclude(char *pattern, int include)
c627d613 178{
2b6b4d53 179 add_exclude_list(pattern,&exclude_list, include);
c627d613
AT
180}
181
2b6b4d53
AT
182struct exclude_struct **make_exclude_list(char *fname,
183 struct exclude_struct **list1,
184 int fatal, int include)
c627d613 185{
2b6b4d53
AT
186 struct exclude_struct **list=list1;
187 FILE *f = fopen(fname,"r");
188 char line[MAXPATHLEN];
189 if (!f) {
190 if (fatal) {
191 rprintf(FERROR,"%s : %s\n",fname,strerror(errno));
65417579 192 exit_cleanup(RERR_FILEIO);
2b6b4d53
AT
193 }
194 return list;
195 }
196
197 while (fgets(line,MAXPATHLEN,f)) {
198 int l = strlen(line);
199 if (l && line[l-1] == '\n') l--;
200 line[l] = 0;
122f19a6
DD
201 if (line[0] && (line[0] != ';') && (line[0] != '#')) {
202 /* Skip lines starting with semicolon or pound.
203 It probably wouldn't cause any harm to not skip
204 them but there's no need to save them. */
205 add_exclude_list(line,&list,include);
206 }
2b6b4d53
AT
207 }
208 fclose(f);
209 return list;
c627d613
AT
210}
211
212
2b6b4d53 213void add_exclude_file(char *fname,int fatal,int include)
c627d613 214{
8f3a2d54
AT
215 if (!fname || !*fname) return;
216
2b6b4d53 217 exclude_list = make_exclude_list(fname,exclude_list,fatal,include);
c627d613
AT
218}
219
220
221void send_exclude_list(int f)
222{
2b6b4d53
AT
223 int i;
224 extern int remote_version;
25cf8893
AT
225 extern int list_only, recurse;
226
227 /* this is a complete hack - blame Rusty */
228 if (list_only && !recurse) {
229 add_exclude("/*/*", 0);
230 }
2b6b4d53
AT
231
232 if (!exclude_list) {
233 write_int(f,0);
234 return;
235 }
236
237 for (i=0;exclude_list[i];i++) {
2b6b4d53 238 int l;
2fb139c1
AT
239 char pattern[MAXPATHLEN];
240
241 strlcpy(pattern,exclude_list[i]->pattern,sizeof(pattern));
242 if (exclude_list[i]->directory) strlcat(pattern,"/", sizeof(pattern));
2b6b4d53 243
587cb08d
DD
244 l = strlen(pattern);
245 if (l == 0) continue;
246 if (exclude_list[i]->include) {
247 if (remote_version < 19) {
2b6b4d53 248 rprintf(FERROR,"remote rsync does not support include syntax - aborting\n");
ec9df380 249 exit_cleanup(RERR_UNSUPPORTED);
2b6b4d53 250 }
587cb08d
DD
251 write_int(f,l+2);
252 write_buf(f,"+ ",2);
253 } else {
254 write_int(f,l);
2b6b4d53 255 }
2b6b4d53
AT
256 write_buf(f,pattern,l);
257 }
258
259 write_int(f,0);
c627d613
AT
260}
261
262
263void recv_exclude_list(int f)
264{
2b6b4d53
AT
265 char line[MAXPATHLEN];
266 int l;
267 while ((l=read_int(f))) {
268 if (l >= MAXPATHLEN) overflow("recv_exclude_list");
269 read_sbuf(f,line,l);
270 add_exclude(line,0);
271 }
c627d613
AT
272}
273
651443a7
DD
274/* Get the next include/exclude arg from the string. It works in a similar way
275** to strtok - initially an arg is sent over, from then on NULL. This
276** routine takes into account any +/- in the strings and does not
277** consider the space following it as a delimeter.
278*/
279char *get_exclude_tok(char *p)
280{
281 static char *s;
282 static int more;
283 char *t;
284
285 if (p) {
286 s=p;
287 if (*p)
288 more=1;
289 }
290
291 if (!more)
292 return(NULL);
c627d613 293
651443a7
DD
294 /* Skip over any initial spaces */
295 while(isspace(*s))
296 s++;
297
298 /* Are we at the end of the string? */
299 if (*s) {
300 /* remember the beginning of the token */
301 t=s;
302
303 /* Is this a '+' or '-' followed by a space (not whitespace)? */
304 if ((*s=='+' || *s=='-') && *(s+1)==' ')
305 s+=2;
306
307 /* Skip to the next space or the end of the string */
308 while(!isspace(*s) && *s!='\0')
309 s++;
310 } else {
311 t=NULL;
312 }
313
314 /* Have we reached the end of the string? */
315 if (*s)
316 *s++='\0';
317 else
318 more=0;
319 return(t);
320}
321
322
8f3a2d54
AT
323void add_exclude_line(char *p)
324{
325 char *tok;
326 if (!p || !*p) return;
327 p = strdup(p);
328 if (!p) out_of_memory("add_exclude_line");
651443a7 329 for (tok=get_exclude_tok(p); tok; tok=get_exclude_tok(NULL))
2b6b4d53 330 add_exclude(tok, 0);
8f3a2d54
AT
331 free(p);
332}
333
cd64343a
DD
334void add_include_line(char *p)
335{
336 char *tok;
337 if (!p || !*p) return;
338 p = strdup(p);
339 if (!p) out_of_memory("add_include_line");
651443a7 340 for (tok=get_exclude_tok(p); tok; tok=get_exclude_tok(NULL))
cd64343a
DD
341 add_exclude(tok, 1);
342 free(p);
343}
344
8f3a2d54 345
c627d613
AT
346static char *cvs_ignore_list[] = {
347 "RCS","SCCS","CVS","CVS.adm","RCSLOG","cvslog.*",
348 "tags","TAGS",".make.state",".nse_depinfo",
349 "*~", "#*", ".#*", ",*", "*.old", "*.bak", "*.BAK", "*.orig",
350 "*.rej", ".del-*", "*.a", "*.o", "*.obj", "*.so", "*.Z", "*.elc", "*.ln",
351 "core",NULL};
352
353
354
355void add_cvs_excludes(void)
356{
2b6b4d53
AT
357 char fname[MAXPATHLEN];
358 char *p;
359 int i;
c627d613 360
2b6b4d53
AT
361 for (i=0; cvs_ignore_list[i]; i++)
362 add_exclude(cvs_ignore_list[i], 0);
c627d613 363
2b6b4d53 364 if ((p=getenv("HOME")) && strlen(p) < (MAXPATHLEN-12)) {
37f9805d 365 slprintf(fname,sizeof(fname), "%s/.cvsignore",p);
2b6b4d53
AT
366 add_exclude_file(fname,0,0);
367 }
c627d613 368
2b6b4d53 369 add_exclude_line(getenv("CVSIGNORE"));
c627d613 370}