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 | |
2b6b4d53 AT |
29 | /* build an exclude structure given a exclude pattern */ |
30 | static 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 | ||
68 | static 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 |
76 | static 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) { | |
ea2111d1 | 96 | if (fnmatch(pattern, name, 0) == 0) |
2b6b4d53 AT |
97 | return 1; |
98 | } else { | |
99 | int l1 = strlen(name); | |
ea2111d1 | 100 | int l2 = strlen(pattern); |
2b6b4d53 | 101 | if (l2 <= l1 && |
ea2111d1 | 102 | strcmp(name+(l1-l2),pattern) == 0 && |
2b6b4d53 AT |
103 | (l1==l2 || (!match_start && name[l1-(l2+1)] == '/'))) |
104 | return 1; | |
105 | } | |
106 | ||
107 | return 0; | |
c627d613 AT |
108 | } |
109 | ||
110 | ||
2b6b4d53 AT |
111 | int 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 | 132 | void 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 | ||
fe8c0a98 | 149 | *list = (struct exclude_struct **)Realloc(*list,sizeof(struct exclude_struct *)*(len+2)); |
2b6b4d53 AT |
150 | |
151 | if (!*list || !((*list)[len] = make_exclude(pattern, include))) | |
152 | out_of_memory("add_exclude"); | |
153 | ||
154 | if (verbose > 2) | |
155 | rprintf(FINFO,"add_exclude(%s)\n",pattern); | |
156 | ||
157 | (*list)[len+1] = NULL; | |
c627d613 AT |
158 | } |
159 | ||
2b6b4d53 | 160 | void add_exclude(char *pattern, int include) |
c627d613 | 161 | { |
2b6b4d53 | 162 | add_exclude_list(pattern,&exclude_list, include); |
c627d613 AT |
163 | } |
164 | ||
2b6b4d53 AT |
165 | struct exclude_struct **make_exclude_list(char *fname, |
166 | struct exclude_struct **list1, | |
167 | int fatal, int include) | |
c627d613 | 168 | { |
2b6b4d53 AT |
169 | struct exclude_struct **list=list1; |
170 | FILE *f = fopen(fname,"r"); | |
171 | char line[MAXPATHLEN]; | |
172 | if (!f) { | |
173 | if (fatal) { | |
174 | rprintf(FERROR,"%s : %s\n",fname,strerror(errno)); | |
175 | exit_cleanup(1); | |
176 | } | |
177 | return list; | |
178 | } | |
179 | ||
180 | while (fgets(line,MAXPATHLEN,f)) { | |
181 | int l = strlen(line); | |
182 | if (l && line[l-1] == '\n') l--; | |
183 | line[l] = 0; | |
184 | if (line[0]) add_exclude_list(line,&list,include); | |
185 | } | |
186 | fclose(f); | |
187 | return list; | |
c627d613 AT |
188 | } |
189 | ||
190 | ||
2b6b4d53 | 191 | void add_exclude_file(char *fname,int fatal,int include) |
c627d613 | 192 | { |
8f3a2d54 AT |
193 | if (!fname || !*fname) return; |
194 | ||
2b6b4d53 | 195 | exclude_list = make_exclude_list(fname,exclude_list,fatal,include); |
c627d613 AT |
196 | } |
197 | ||
198 | ||
199 | void send_exclude_list(int f) | |
200 | { | |
2b6b4d53 AT |
201 | int i; |
202 | extern int remote_version; | |
203 | ||
204 | if (!exclude_list) { | |
205 | write_int(f,0); | |
206 | return; | |
207 | } | |
208 | ||
209 | for (i=0;exclude_list[i];i++) { | |
210 | char *pattern = exclude_list[i]->orig; | |
211 | int l; | |
212 | ||
213 | if (remote_version < 19) { | |
214 | if (strncmp(pattern,"+ ", 2)==0) { | |
215 | rprintf(FERROR,"remote rsync does not support include syntax - aborting\n"); | |
216 | exit_cleanup(1); | |
217 | } | |
218 | ||
219 | if (strncmp(pattern,"- ", 2) == 0) { | |
220 | pattern += 2; | |
221 | } | |
222 | } | |
223 | ||
224 | l = strlen(pattern); | |
225 | if (l == 0) continue; | |
226 | write_int(f,l); | |
227 | write_buf(f,pattern,l); | |
228 | } | |
229 | ||
230 | write_int(f,0); | |
c627d613 AT |
231 | } |
232 | ||
233 | ||
234 | void recv_exclude_list(int f) | |
235 | { | |
2b6b4d53 AT |
236 | char line[MAXPATHLEN]; |
237 | int l; | |
238 | while ((l=read_int(f))) { | |
239 | if (l >= MAXPATHLEN) overflow("recv_exclude_list"); | |
240 | read_sbuf(f,line,l); | |
241 | add_exclude(line,0); | |
242 | } | |
c627d613 AT |
243 | } |
244 | ||
245 | ||
8f3a2d54 AT |
246 | void add_exclude_line(char *p) |
247 | { | |
248 | char *tok; | |
249 | if (!p || !*p) return; | |
250 | p = strdup(p); | |
251 | if (!p) out_of_memory("add_exclude_line"); | |
252 | for (tok=strtok(p," "); tok; tok=strtok(NULL," ")) | |
2b6b4d53 | 253 | add_exclude(tok, 0); |
8f3a2d54 AT |
254 | free(p); |
255 | } | |
256 | ||
257 | ||
c627d613 AT |
258 | static char *cvs_ignore_list[] = { |
259 | "RCS","SCCS","CVS","CVS.adm","RCSLOG","cvslog.*", | |
260 | "tags","TAGS",".make.state",".nse_depinfo", | |
261 | "*~", "#*", ".#*", ",*", "*.old", "*.bak", "*.BAK", "*.orig", | |
262 | "*.rej", ".del-*", "*.a", "*.o", "*.obj", "*.so", "*.Z", "*.elc", "*.ln", | |
263 | "core",NULL}; | |
264 | ||
265 | ||
266 | ||
267 | void add_cvs_excludes(void) | |
268 | { | |
2b6b4d53 AT |
269 | char fname[MAXPATHLEN]; |
270 | char *p; | |
271 | int i; | |
c627d613 | 272 | |
2b6b4d53 AT |
273 | for (i=0; cvs_ignore_list[i]; i++) |
274 | add_exclude(cvs_ignore_list[i], 0); | |
c627d613 | 275 | |
2b6b4d53 AT |
276 | if ((p=getenv("HOME")) && strlen(p) < (MAXPATHLEN-12)) { |
277 | slprintf(fname,sizeof(fname)-1, "%s/.cvsignore",p); | |
278 | add_exclude_file(fname,0,0); | |
279 | } | |
c627d613 | 280 | |
2b6b4d53 | 281 | add_exclude_line(getenv("CVSIGNORE")); |
c627d613 | 282 | } |