heaps of cleanup in the io code.
[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
2b6b4d53
AT
29/* build an exclude structure given a exclude pattern */
30static struct exclude_struct *make_exclude(char *pattern, int include)
c627d613 31{
2b6b4d53 32 struct exclude_struct *ret;
c627d613 33
2b6b4d53
AT
34 ret = (struct exclude_struct *)malloc(sizeof(*ret));
35 if (!ret) out_of_memory("make_exclude");
c627d613 36
2b6b4d53
AT
37 memset(ret, 0, sizeof(*ret));
38
39 ret->orig = strdup(pattern);
40
41 if (strncmp(pattern,"- ",2) == 0) {
42 pattern += 2;
43 } else if (strncmp(pattern,"+ ",2) == 0) {
44 ret->include = 1;
45 pattern += 2;
46 } else {
47 ret->include = include;
48 }
49
50 ret->pattern = strdup(pattern);
51
52 if (!ret->orig || !ret->pattern) out_of_memory("make_exclude");
c627d613 53
2b6b4d53 54 if (strpbrk(pattern, "*[?")) ret->regular_exp = 1;
c627d613 55
2b6b4d53
AT
56 if (strlen(pattern) > 1 && pattern[strlen(pattern)-1] == '/') {
57 ret->pattern[strlen(pattern)-1] = 0;
58 ret->directory = 1;
59 }
c627d613 60
2b6b4d53
AT
61 if (!strchr(ret->pattern,'/')) {
62 ret->local = 1;
63 }
0944563e 64
2b6b4d53
AT
65 return ret;
66}
67
68static void free_exclude(struct exclude_struct *ex)
69{
70 free(ex->orig);
71 free(ex->pattern);
72 memset(ex,0,sizeof(*ex));
73 free(ex);
74}
c627d613 75
2b6b4d53
AT
76static int check_one_exclude(char *name,struct exclude_struct *ex,
77 STRUCT_STAT *st)
78{
79 char *p;
80 int match_start=0;
81 char *pattern = ex->pattern;
82
83 if (ex->local && (p=strrchr(name,'/')))
84 name = p+1;
85
86 if (!name[0]) return 0;
87
88 if (ex->directory && !S_ISDIR(st->st_mode)) return 0;
89
90 if (*pattern == '/' && *name != '/') {
91 match_start = 1;
92 pattern++;
93 }
94
95 if (ex->regular_exp) {
96 if (fnmatch(ex->pattern, name, 0) == 0)
97 return 1;
98 } else {
99 int l1 = strlen(name);
100 int l2 = strlen(ex->pattern);
101 if (l2 <= l1 &&
102 strcmp(name+(l1-l2),ex->pattern) == 0 &&
103 (l1==l2 || (!match_start && name[l1-(l2+1)] == '/')))
104 return 1;
105 }
106
107 return 0;
c627d613
AT
108}
109
110
2b6b4d53
AT
111int check_exclude(char *name,struct exclude_struct **local_exclude_list,
112 STRUCT_STAT *st)
c627d613 113{
2b6b4d53 114 int n;
c627d613 115
2b6b4d53
AT
116 if (exclude_list) {
117 for (n=0; exclude_list[n]; n++)
118 if (check_one_exclude(name,exclude_list[n],st))
119 return !exclude_list[n]->include;
120 }
c627d613 121
2b6b4d53
AT
122 if (local_exclude_list) {
123 for (n=0; local_exclude_list[n]; n++)
124 if (check_one_exclude(name,local_exclude_list[n],st))
125 return !local_exclude_list[n]->include;
126 }
c627d613 127
2b6b4d53 128 return 0;
c627d613
AT
129}
130
131
2b6b4d53 132void add_exclude_list(char *pattern,struct exclude_struct ***list, int include)
c627d613 133{
2b6b4d53
AT
134 int len=0;
135 if (list && *list)
136 for (; (*list)[len]; len++) ;
137
138 if (strcmp(pattern,"!") == 0) {
139 if (verbose > 2)
140 rprintf(FINFO,"clearing exclude list\n");
141 while ((len)--) {
142 free_exclude((*list)[len]);
143 }
144 free((*list));
145 *list = NULL;
146 return;
147 }
148
149 if (!*list) {
150 *list = (struct exclude_struct **)malloc(sizeof(struct exclude_struct *)*2);
151 } else {
152 *list = (struct exclude_struct **)realloc(*list,sizeof(struct exclude_struct *)*(len+2));
153 }
154
155 if (!*list || !((*list)[len] = make_exclude(pattern, include)))
156 out_of_memory("add_exclude");
157
158 if (verbose > 2)
159 rprintf(FINFO,"add_exclude(%s)\n",pattern);
160
161 (*list)[len+1] = NULL;
c627d613
AT
162}
163
2b6b4d53 164void add_exclude(char *pattern, int include)
c627d613 165{
2b6b4d53 166 add_exclude_list(pattern,&exclude_list, include);
c627d613
AT
167}
168
2b6b4d53
AT
169struct exclude_struct **make_exclude_list(char *fname,
170 struct exclude_struct **list1,
171 int fatal, int include)
c627d613 172{
2b6b4d53
AT
173 struct exclude_struct **list=list1;
174 FILE *f = fopen(fname,"r");
175 char line[MAXPATHLEN];
176 if (!f) {
177 if (fatal) {
178 rprintf(FERROR,"%s : %s\n",fname,strerror(errno));
179 exit_cleanup(1);
180 }
181 return list;
182 }
183
184 while (fgets(line,MAXPATHLEN,f)) {
185 int l = strlen(line);
186 if (l && line[l-1] == '\n') l--;
187 line[l] = 0;
188 if (line[0]) add_exclude_list(line,&list,include);
189 }
190 fclose(f);
191 return list;
c627d613
AT
192}
193
194
2b6b4d53 195void add_exclude_file(char *fname,int fatal,int include)
c627d613 196{
8f3a2d54
AT
197 if (!fname || !*fname) return;
198
2b6b4d53 199 exclude_list = make_exclude_list(fname,exclude_list,fatal,include);
c627d613
AT
200}
201
202
203void send_exclude_list(int f)
204{
2b6b4d53
AT
205 int i;
206 extern int remote_version;
207
208 if (!exclude_list) {
209 write_int(f,0);
210 return;
211 }
212
213 for (i=0;exclude_list[i];i++) {
214 char *pattern = exclude_list[i]->orig;
215 int l;
216
217 if (remote_version < 19) {
218 if (strncmp(pattern,"+ ", 2)==0) {
219 rprintf(FERROR,"remote rsync does not support include syntax - aborting\n");
220 exit_cleanup(1);
221 }
222
223 if (strncmp(pattern,"- ", 2) == 0) {
224 pattern += 2;
225 }
226 }
227
228 l = strlen(pattern);
229 if (l == 0) continue;
230 write_int(f,l);
231 write_buf(f,pattern,l);
232 }
233
234 write_int(f,0);
c627d613
AT
235}
236
237
238void recv_exclude_list(int f)
239{
2b6b4d53
AT
240 char line[MAXPATHLEN];
241 int l;
242 while ((l=read_int(f))) {
243 if (l >= MAXPATHLEN) overflow("recv_exclude_list");
244 read_sbuf(f,line,l);
245 add_exclude(line,0);
246 }
c627d613
AT
247}
248
249
8f3a2d54
AT
250void add_exclude_line(char *p)
251{
252 char *tok;
253 if (!p || !*p) return;
254 p = strdup(p);
255 if (!p) out_of_memory("add_exclude_line");
256 for (tok=strtok(p," "); tok; tok=strtok(NULL," "))
2b6b4d53 257 add_exclude(tok, 0);
8f3a2d54
AT
258 free(p);
259}
260
261
c627d613
AT
262static char *cvs_ignore_list[] = {
263 "RCS","SCCS","CVS","CVS.adm","RCSLOG","cvslog.*",
264 "tags","TAGS",".make.state",".nse_depinfo",
265 "*~", "#*", ".#*", ",*", "*.old", "*.bak", "*.BAK", "*.orig",
266 "*.rej", ".del-*", "*.a", "*.o", "*.obj", "*.so", "*.Z", "*.elc", "*.ln",
267 "core",NULL};
268
269
270
271void add_cvs_excludes(void)
272{
2b6b4d53
AT
273 char fname[MAXPATHLEN];
274 char *p;
275 int i;
c627d613 276
2b6b4d53
AT
277 for (i=0; cvs_ignore_list[i]; i++)
278 add_exclude(cvs_ignore_list[i], 0);
c627d613 279
2b6b4d53
AT
280 if ((p=getenv("HOME")) && strlen(p) < (MAXPATHLEN-12)) {
281 slprintf(fname,sizeof(fname)-1, "%s/.cvsignore",p);
282 add_exclude_file(fname,0,0);
283 }
c627d613 284
2b6b4d53 285 add_exclude_line(getenv("CVSIGNORE"));
c627d613 286}