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; | |
a8b9d4ed | 103 | ret->fnmatch_flags = strstr(pattern, "**") ? 0 : FNM_PATHNAME; |
2bca43f6 DD |
104 | } else if (!ret->include) { |
105 | only_included_files = 0; | |
106 | } | |
c627d613 | 107 | |
2b6b4d53 AT |
108 | if (strlen(pattern) > 1 && pattern[strlen(pattern)-1] == '/') { |
109 | ret->pattern[strlen(pattern)-1] = 0; | |
110 | ret->directory = 1; | |
111 | } | |
c627d613 | 112 | |
2b6b4d53 AT |
113 | if (!strchr(ret->pattern,'/')) { |
114 | ret->local = 1; | |
115 | } | |
0944563e | 116 | |
2b6b4d53 AT |
117 | return ret; |
118 | } | |
119 | ||
120 | static void free_exclude(struct exclude_struct *ex) | |
121 | { | |
2b6b4d53 AT |
122 | free(ex->pattern); |
123 | memset(ex,0,sizeof(*ex)); | |
124 | free(ex); | |
125 | } | |
c627d613 | 126 | |
2b6b4d53 AT |
127 | static int check_one_exclude(char *name,struct exclude_struct *ex, |
128 | STRUCT_STAT *st) | |
129 | { | |
130 | char *p; | |
131 | int match_start=0; | |
132 | char *pattern = ex->pattern; | |
133 | ||
134 | if (ex->local && (p=strrchr(name,'/'))) | |
135 | name = p+1; | |
136 | ||
137 | if (!name[0]) return 0; | |
138 | ||
139 | if (ex->directory && !S_ISDIR(st->st_mode)) return 0; | |
140 | ||
141 | if (*pattern == '/' && *name != '/') { | |
142 | match_start = 1; | |
143 | pattern++; | |
144 | } | |
145 | ||
146 | if (ex->regular_exp) { | |
a8b9d4ed | 147 | if (fnmatch(pattern, name, ex->fnmatch_flags) == 0) |
2b6b4d53 AT |
148 | return 1; |
149 | } else { | |
150 | int l1 = strlen(name); | |
ea2111d1 | 151 | int l2 = strlen(pattern); |
2b6b4d53 | 152 | if (l2 <= l1 && |
ea2111d1 | 153 | strcmp(name+(l1-l2),pattern) == 0 && |
2b6b4d53 AT |
154 | (l1==l2 || (!match_start && name[l1-(l2+1)] == '/'))) |
155 | return 1; | |
156 | } | |
157 | ||
158 | return 0; | |
c627d613 AT |
159 | } |
160 | ||
161 | ||
2b6b4d53 AT |
162 | int check_exclude(char *name,struct exclude_struct **local_exclude_list, |
163 | STRUCT_STAT *st) | |
c627d613 | 164 | { |
2b6b4d53 | 165 | int n; |
c627d613 | 166 | |
5d5811f7 DD |
167 | if (name && (name[0] == '.') && !name[1]) |
168 | /* never exclude '.', even if somebody does --exclude '*' */ | |
169 | return 0; | |
170 | ||
2b6b4d53 AT |
171 | if (exclude_list) { |
172 | for (n=0; exclude_list[n]; n++) | |
173 | if (check_one_exclude(name,exclude_list[n],st)) | |
174 | return !exclude_list[n]->include; | |
175 | } | |
c627d613 | 176 | |
2b6b4d53 AT |
177 | if (local_exclude_list) { |
178 | for (n=0; local_exclude_list[n]; n++) | |
179 | if (check_one_exclude(name,local_exclude_list[n],st)) | |
180 | return !local_exclude_list[n]->include; | |
181 | } | |
c627d613 | 182 | |
2b6b4d53 | 183 | return 0; |
c627d613 AT |
184 | } |
185 | ||
186 | ||
2b6b4d53 | 187 | void add_exclude_list(char *pattern,struct exclude_struct ***list, int include) |
c627d613 | 188 | { |
2b6b4d53 AT |
189 | int len=0; |
190 | if (list && *list) | |
191 | for (; (*list)[len]; len++) ; | |
192 | ||
193 | if (strcmp(pattern,"!") == 0) { | |
194 | if (verbose > 2) | |
195 | rprintf(FINFO,"clearing exclude list\n"); | |
196 | while ((len)--) { | |
197 | free_exclude((*list)[len]); | |
198 | } | |
199 | free((*list)); | |
200 | *list = NULL; | |
2bca43f6 DD |
201 | only_included_files = 1; |
202 | exclude_the_rest = NULL; | |
2b6b4d53 AT |
203 | return; |
204 | } | |
205 | ||
fe8c0a98 | 206 | *list = (struct exclude_struct **)Realloc(*list,sizeof(struct exclude_struct *)*(len+2)); |
2b6b4d53 AT |
207 | |
208 | if (!*list || !((*list)[len] = make_exclude(pattern, include))) | |
209 | out_of_memory("add_exclude"); | |
210 | ||
211 | if (verbose > 2) | |
212 | rprintf(FINFO,"add_exclude(%s)\n",pattern); | |
213 | ||
214 | (*list)[len+1] = NULL; | |
c627d613 AT |
215 | } |
216 | ||
2b6b4d53 | 217 | void add_exclude(char *pattern, int include) |
c627d613 | 218 | { |
2b6b4d53 | 219 | add_exclude_list(pattern,&exclude_list, include); |
c627d613 AT |
220 | } |
221 | ||
2b6b4d53 AT |
222 | struct exclude_struct **make_exclude_list(char *fname, |
223 | struct exclude_struct **list1, | |
224 | int fatal, int include) | |
c627d613 | 225 | { |
2b6b4d53 AT |
226 | struct exclude_struct **list=list1; |
227 | FILE *f = fopen(fname,"r"); | |
228 | char line[MAXPATHLEN]; | |
229 | if (!f) { | |
230 | if (fatal) { | |
231 | rprintf(FERROR,"%s : %s\n",fname,strerror(errno)); | |
65417579 | 232 | exit_cleanup(RERR_FILEIO); |
2b6b4d53 AT |
233 | } |
234 | return list; | |
235 | } | |
236 | ||
237 | while (fgets(line,MAXPATHLEN,f)) { | |
238 | int l = strlen(line); | |
239 | if (l && line[l-1] == '\n') l--; | |
240 | line[l] = 0; | |
122f19a6 DD |
241 | if (line[0] && (line[0] != ';') && (line[0] != '#')) { |
242 | /* Skip lines starting with semicolon or pound. | |
243 | It probably wouldn't cause any harm to not skip | |
244 | them but there's no need to save them. */ | |
245 | add_exclude_list(line,&list,include); | |
246 | } | |
2b6b4d53 AT |
247 | } |
248 | fclose(f); | |
249 | return list; | |
c627d613 AT |
250 | } |
251 | ||
252 | ||
2b6b4d53 | 253 | void add_exclude_file(char *fname,int fatal,int include) |
c627d613 | 254 | { |
8f3a2d54 AT |
255 | if (!fname || !*fname) return; |
256 | ||
2b6b4d53 | 257 | exclude_list = make_exclude_list(fname,exclude_list,fatal,include); |
c627d613 AT |
258 | } |
259 | ||
260 | ||
261 | void send_exclude_list(int f) | |
262 | { | |
2b6b4d53 AT |
263 | int i; |
264 | extern int remote_version; | |
265 | ||
266 | if (!exclude_list) { | |
267 | write_int(f,0); | |
268 | return; | |
269 | } | |
270 | ||
271 | for (i=0;exclude_list[i];i++) { | |
587cb08d | 272 | char *pattern = exclude_list[i]->pattern; |
2b6b4d53 AT |
273 | int l; |
274 | ||
587cb08d DD |
275 | l = strlen(pattern); |
276 | if (l == 0) continue; | |
277 | if (exclude_list[i]->include) { | |
278 | if (remote_version < 19) { | |
2b6b4d53 | 279 | rprintf(FERROR,"remote rsync does not support include syntax - aborting\n"); |
ec9df380 | 280 | exit_cleanup(RERR_UNSUPPORTED); |
2b6b4d53 | 281 | } |
587cb08d DD |
282 | write_int(f,l+2); |
283 | write_buf(f,"+ ",2); | |
284 | } else { | |
285 | write_int(f,l); | |
2b6b4d53 | 286 | } |
2b6b4d53 AT |
287 | write_buf(f,pattern,l); |
288 | } | |
289 | ||
290 | write_int(f,0); | |
c627d613 AT |
291 | } |
292 | ||
293 | ||
294 | void recv_exclude_list(int f) | |
295 | { | |
2b6b4d53 AT |
296 | char line[MAXPATHLEN]; |
297 | int l; | |
298 | while ((l=read_int(f))) { | |
299 | if (l >= MAXPATHLEN) overflow("recv_exclude_list"); | |
300 | read_sbuf(f,line,l); | |
301 | add_exclude(line,0); | |
302 | } | |
c627d613 AT |
303 | } |
304 | ||
305 | ||
8f3a2d54 AT |
306 | void add_exclude_line(char *p) |
307 | { | |
308 | char *tok; | |
309 | if (!p || !*p) return; | |
310 | p = strdup(p); | |
311 | if (!p) out_of_memory("add_exclude_line"); | |
312 | for (tok=strtok(p," "); tok; tok=strtok(NULL," ")) | |
2b6b4d53 | 313 | add_exclude(tok, 0); |
8f3a2d54 AT |
314 | free(p); |
315 | } | |
316 | ||
cd64343a DD |
317 | void add_include_line(char *p) |
318 | { | |
319 | char *tok; | |
320 | if (!p || !*p) return; | |
321 | p = strdup(p); | |
322 | if (!p) out_of_memory("add_include_line"); | |
323 | for (tok=strtok(p," "); tok; tok=strtok(NULL," ")) | |
324 | add_exclude(tok, 1); | |
325 | free(p); | |
326 | } | |
327 | ||
8f3a2d54 | 328 | |
c627d613 AT |
329 | static char *cvs_ignore_list[] = { |
330 | "RCS","SCCS","CVS","CVS.adm","RCSLOG","cvslog.*", | |
331 | "tags","TAGS",".make.state",".nse_depinfo", | |
332 | "*~", "#*", ".#*", ",*", "*.old", "*.bak", "*.BAK", "*.orig", | |
333 | "*.rej", ".del-*", "*.a", "*.o", "*.obj", "*.so", "*.Z", "*.elc", "*.ln", | |
334 | "core",NULL}; | |
335 | ||
336 | ||
337 | ||
338 | void add_cvs_excludes(void) | |
339 | { | |
2b6b4d53 AT |
340 | char fname[MAXPATHLEN]; |
341 | char *p; | |
342 | int i; | |
c627d613 | 343 | |
2b6b4d53 AT |
344 | for (i=0; cvs_ignore_list[i]; i++) |
345 | add_exclude(cvs_ignore_list[i], 0); | |
c627d613 | 346 | |
2b6b4d53 | 347 | if ((p=getenv("HOME")) && strlen(p) < (MAXPATHLEN-12)) { |
37f9805d | 348 | slprintf(fname,sizeof(fname), "%s/.cvsignore",p); |
2b6b4d53 AT |
349 | add_exclude_file(fname,0,0); |
350 | } | |
c627d613 | 351 | |
2b6b4d53 | 352 | add_exclude_line(getenv("CVSIGNORE")); |
c627d613 | 353 | } |