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; | |
26 | ||
2b6b4d53 | 27 | static 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 | */ | |
35 | static int only_included_files = 1; | |
36 | static struct exclude_struct *exclude_the_rest; | |
37 | ||
38 | int 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 | } | |
19c14f98 DD |
63 | /* silently skip files that don't exist to |
64 | be more like non-optimized case */ | |
65 | if (access(p,0) == 0) | |
66 | send_file_name(f,flist,p,0,0); | |
2bca43f6 DD |
67 | } |
68 | exclude_list = ex_list; | |
69 | ||
70 | return 1; | |
71 | } | |
72 | ||
2b6b4d53 AT |
73 | /* build an exclude structure given a exclude pattern */ |
74 | static struct exclude_struct *make_exclude(char *pattern, int include) | |
c627d613 | 75 | { |
2b6b4d53 | 76 | struct exclude_struct *ret; |
c627d613 | 77 | |
2b6b4d53 AT |
78 | ret = (struct exclude_struct *)malloc(sizeof(*ret)); |
79 | if (!ret) out_of_memory("make_exclude"); | |
c627d613 | 80 | |
2b6b4d53 AT |
81 | memset(ret, 0, sizeof(*ret)); |
82 | ||
2b6b4d53 AT |
83 | if (strncmp(pattern,"- ",2) == 0) { |
84 | pattern += 2; | |
85 | } else if (strncmp(pattern,"+ ",2) == 0) { | |
86 | ret->include = 1; | |
87 | pattern += 2; | |
88 | } else { | |
89 | ret->include = include; | |
90 | } | |
91 | ||
92 | ret->pattern = strdup(pattern); | |
93 | ||
587cb08d | 94 | if (!ret->pattern) out_of_memory("make_exclude"); |
c627d613 | 95 | |
2bca43f6 DD |
96 | if (strpbrk(pattern, "*[?")) { |
97 | if (!ret->include && (*pattern == '*') && (*(pattern+1) == '\0')) { | |
98 | exclude_the_rest = ret; | |
99 | } else { | |
100 | only_included_files = 0; | |
101 | } | |
102 | ret->regular_exp = 1; | |
103 | } else if (!ret->include) { | |
104 | only_included_files = 0; | |
105 | } | |
c627d613 | 106 | |
2b6b4d53 AT |
107 | if (strlen(pattern) > 1 && pattern[strlen(pattern)-1] == '/') { |
108 | ret->pattern[strlen(pattern)-1] = 0; | |
109 | ret->directory = 1; | |
110 | } | |
c627d613 | 111 | |
2b6b4d53 AT |
112 | if (!strchr(ret->pattern,'/')) { |
113 | ret->local = 1; | |
114 | } | |
0944563e | 115 | |
2b6b4d53 AT |
116 | return ret; |
117 | } | |
118 | ||
119 | static void free_exclude(struct exclude_struct *ex) | |
120 | { | |
2b6b4d53 AT |
121 | free(ex->pattern); |
122 | memset(ex,0,sizeof(*ex)); | |
123 | free(ex); | |
124 | } | |
c627d613 | 125 | |
2b6b4d53 AT |
126 | static int check_one_exclude(char *name,struct exclude_struct *ex, |
127 | STRUCT_STAT *st) | |
128 | { | |
129 | char *p; | |
130 | int match_start=0; | |
131 | char *pattern = ex->pattern; | |
132 | ||
133 | if (ex->local && (p=strrchr(name,'/'))) | |
134 | name = p+1; | |
135 | ||
136 | if (!name[0]) return 0; | |
137 | ||
138 | if (ex->directory && !S_ISDIR(st->st_mode)) return 0; | |
139 | ||
140 | if (*pattern == '/' && *name != '/') { | |
141 | match_start = 1; | |
142 | pattern++; | |
143 | } | |
144 | ||
145 | if (ex->regular_exp) { | |
ea2111d1 | 146 | if (fnmatch(pattern, name, 0) == 0) |
2b6b4d53 AT |
147 | return 1; |
148 | } else { | |
149 | int l1 = strlen(name); | |
ea2111d1 | 150 | int l2 = strlen(pattern); |
2b6b4d53 | 151 | if (l2 <= l1 && |
ea2111d1 | 152 | strcmp(name+(l1-l2),pattern) == 0 && |
2b6b4d53 AT |
153 | (l1==l2 || (!match_start && name[l1-(l2+1)] == '/'))) |
154 | return 1; | |
155 | } | |
156 | ||
157 | return 0; | |
c627d613 AT |
158 | } |
159 | ||
160 | ||
2b6b4d53 AT |
161 | int check_exclude(char *name,struct exclude_struct **local_exclude_list, |
162 | STRUCT_STAT *st) | |
c627d613 | 163 | { |
2b6b4d53 | 164 | int n; |
c627d613 | 165 | |
5d5811f7 DD |
166 | if (name && (name[0] == '.') && !name[1]) |
167 | /* never exclude '.', even if somebody does --exclude '*' */ | |
168 | return 0; | |
169 | ||
2b6b4d53 AT |
170 | if (exclude_list) { |
171 | for (n=0; exclude_list[n]; n++) | |
172 | if (check_one_exclude(name,exclude_list[n],st)) | |
173 | return !exclude_list[n]->include; | |
174 | } | |
c627d613 | 175 | |
2b6b4d53 AT |
176 | if (local_exclude_list) { |
177 | for (n=0; local_exclude_list[n]; n++) | |
178 | if (check_one_exclude(name,local_exclude_list[n],st)) | |
179 | return !local_exclude_list[n]->include; | |
180 | } | |
c627d613 | 181 | |
2b6b4d53 | 182 | return 0; |
c627d613 AT |
183 | } |
184 | ||
185 | ||
2b6b4d53 | 186 | void add_exclude_list(char *pattern,struct exclude_struct ***list, int include) |
c627d613 | 187 | { |
2b6b4d53 AT |
188 | int len=0; |
189 | if (list && *list) | |
190 | for (; (*list)[len]; len++) ; | |
191 | ||
192 | if (strcmp(pattern,"!") == 0) { | |
193 | if (verbose > 2) | |
194 | rprintf(FINFO,"clearing exclude list\n"); | |
195 | while ((len)--) { | |
196 | free_exclude((*list)[len]); | |
197 | } | |
198 | free((*list)); | |
199 | *list = NULL; | |
2bca43f6 DD |
200 | only_included_files = 1; |
201 | exclude_the_rest = NULL; | |
2b6b4d53 AT |
202 | return; |
203 | } | |
204 | ||
fe8c0a98 | 205 | *list = (struct exclude_struct **)Realloc(*list,sizeof(struct exclude_struct *)*(len+2)); |
2b6b4d53 AT |
206 | |
207 | if (!*list || !((*list)[len] = make_exclude(pattern, include))) | |
208 | out_of_memory("add_exclude"); | |
209 | ||
210 | if (verbose > 2) | |
211 | rprintf(FINFO,"add_exclude(%s)\n",pattern); | |
212 | ||
213 | (*list)[len+1] = NULL; | |
c627d613 AT |
214 | } |
215 | ||
2b6b4d53 | 216 | void add_exclude(char *pattern, int include) |
c627d613 | 217 | { |
2b6b4d53 | 218 | add_exclude_list(pattern,&exclude_list, include); |
c627d613 AT |
219 | } |
220 | ||
2b6b4d53 AT |
221 | struct exclude_struct **make_exclude_list(char *fname, |
222 | struct exclude_struct **list1, | |
223 | int fatal, int include) | |
c627d613 | 224 | { |
2b6b4d53 AT |
225 | struct exclude_struct **list=list1; |
226 | FILE *f = fopen(fname,"r"); | |
227 | char line[MAXPATHLEN]; | |
228 | if (!f) { | |
229 | if (fatal) { | |
230 | rprintf(FERROR,"%s : %s\n",fname,strerror(errno)); | |
65417579 | 231 | exit_cleanup(RERR_FILEIO); |
2b6b4d53 AT |
232 | } |
233 | return list; | |
234 | } | |
235 | ||
236 | while (fgets(line,MAXPATHLEN,f)) { | |
237 | int l = strlen(line); | |
238 | if (l && line[l-1] == '\n') l--; | |
239 | line[l] = 0; | |
122f19a6 DD |
240 | if (line[0] && (line[0] != ';') && (line[0] != '#')) { |
241 | /* Skip lines starting with semicolon or pound. | |
242 | It probably wouldn't cause any harm to not skip | |
243 | them but there's no need to save them. */ | |
244 | add_exclude_list(line,&list,include); | |
245 | } | |
2b6b4d53 AT |
246 | } |
247 | fclose(f); | |
248 | return list; | |
c627d613 AT |
249 | } |
250 | ||
251 | ||
2b6b4d53 | 252 | void add_exclude_file(char *fname,int fatal,int include) |
c627d613 | 253 | { |
8f3a2d54 AT |
254 | if (!fname || !*fname) return; |
255 | ||
2b6b4d53 | 256 | exclude_list = make_exclude_list(fname,exclude_list,fatal,include); |
c627d613 AT |
257 | } |
258 | ||
259 | ||
260 | void send_exclude_list(int f) | |
261 | { | |
2b6b4d53 AT |
262 | int i; |
263 | extern int remote_version; | |
264 | ||
265 | if (!exclude_list) { | |
266 | write_int(f,0); | |
267 | return; | |
268 | } | |
269 | ||
270 | for (i=0;exclude_list[i];i++) { | |
587cb08d | 271 | char *pattern = exclude_list[i]->pattern; |
2b6b4d53 AT |
272 | int l; |
273 | ||
587cb08d DD |
274 | l = strlen(pattern); |
275 | if (l == 0) continue; | |
276 | if (exclude_list[i]->include) { | |
277 | if (remote_version < 19) { | |
2b6b4d53 | 278 | rprintf(FERROR,"remote rsync does not support include syntax - aborting\n"); |
ec9df380 | 279 | exit_cleanup(RERR_UNSUPPORTED); |
2b6b4d53 | 280 | } |
587cb08d DD |
281 | write_int(f,l+2); |
282 | write_buf(f,"+ ",2); | |
283 | } else { | |
284 | write_int(f,l); | |
2b6b4d53 | 285 | } |
2b6b4d53 AT |
286 | write_buf(f,pattern,l); |
287 | } | |
288 | ||
289 | write_int(f,0); | |
c627d613 AT |
290 | } |
291 | ||
292 | ||
293 | void recv_exclude_list(int f) | |
294 | { | |
2b6b4d53 AT |
295 | char line[MAXPATHLEN]; |
296 | int l; | |
297 | while ((l=read_int(f))) { | |
298 | if (l >= MAXPATHLEN) overflow("recv_exclude_list"); | |
299 | read_sbuf(f,line,l); | |
300 | add_exclude(line,0); | |
301 | } | |
c627d613 AT |
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"); | |
311 | for (tok=strtok(p," "); tok; tok=strtok(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"); | |
322 | for (tok=strtok(p," "); tok; tok=strtok(NULL," ")) | |
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 | } |