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