Add "use chroot" and "pid file" rsyncd.conf options. The former allows one
[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
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 */
35static int only_included_files = 1;
36static struct exclude_struct *exclude_the_rest;
37
38int 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 }
63 send_file_name(f,flist,p,0,0);
64 }
65 exclude_list = ex_list;
66
67 return 1;
68}
69
2b6b4d53
AT
70/* build an exclude structure given a exclude pattern */
71static struct exclude_struct *make_exclude(char *pattern, int include)
c627d613 72{
2b6b4d53 73 struct exclude_struct *ret;
c627d613 74
2b6b4d53
AT
75 ret = (struct exclude_struct *)malloc(sizeof(*ret));
76 if (!ret) out_of_memory("make_exclude");
c627d613 77
2b6b4d53
AT
78 memset(ret, 0, sizeof(*ret));
79
80 ret->orig = strdup(pattern);
81
82 if (strncmp(pattern,"- ",2) == 0) {
83 pattern += 2;
84 } else if (strncmp(pattern,"+ ",2) == 0) {
85 ret->include = 1;
86 pattern += 2;
87 } else {
88 ret->include = include;
89 }
90
91 ret->pattern = strdup(pattern);
92
93 if (!ret->orig || !ret->pattern) out_of_memory("make_exclude");
c627d613 94
2bca43f6
DD
95 if (strpbrk(pattern, "*[?")) {
96 if (!ret->include && (*pattern == '*') && (*(pattern+1) == '\0')) {
97 exclude_the_rest = ret;
98 } else {
99 only_included_files = 0;
100 }
101 ret->regular_exp = 1;
102 } else if (!ret->include) {
103 only_included_files = 0;
104 }
c627d613 105
2b6b4d53
AT
106 if (strlen(pattern) > 1 && pattern[strlen(pattern)-1] == '/') {
107 ret->pattern[strlen(pattern)-1] = 0;
108 ret->directory = 1;
109 }
c627d613 110
2b6b4d53
AT
111 if (!strchr(ret->pattern,'/')) {
112 ret->local = 1;
113 }
0944563e 114
2b6b4d53
AT
115 return ret;
116}
117
118static void free_exclude(struct exclude_struct *ex)
119{
120 free(ex->orig);
121 free(ex->pattern);
122 memset(ex,0,sizeof(*ex));
123 free(ex);
124}
c627d613 125
2b6b4d53
AT
126static 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
161int check_exclude(char *name,struct exclude_struct **local_exclude_list,
162 STRUCT_STAT *st)
c627d613 163{
2b6b4d53 164 int n;
c627d613 165
2b6b4d53
AT
166 if (exclude_list) {
167 for (n=0; exclude_list[n]; n++)
168 if (check_one_exclude(name,exclude_list[n],st))
169 return !exclude_list[n]->include;
170 }
c627d613 171
2b6b4d53
AT
172 if (local_exclude_list) {
173 for (n=0; local_exclude_list[n]; n++)
174 if (check_one_exclude(name,local_exclude_list[n],st))
175 return !local_exclude_list[n]->include;
176 }
c627d613 177
2b6b4d53 178 return 0;
c627d613
AT
179}
180
181
2b6b4d53 182void add_exclude_list(char *pattern,struct exclude_struct ***list, int include)
c627d613 183{
2b6b4d53
AT
184 int len=0;
185 if (list && *list)
186 for (; (*list)[len]; len++) ;
187
188 if (strcmp(pattern,"!") == 0) {
189 if (verbose > 2)
190 rprintf(FINFO,"clearing exclude list\n");
191 while ((len)--) {
192 free_exclude((*list)[len]);
193 }
194 free((*list));
195 *list = NULL;
2bca43f6
DD
196 only_included_files = 1;
197 exclude_the_rest = NULL;
2b6b4d53
AT
198 return;
199 }
200
fe8c0a98 201 *list = (struct exclude_struct **)Realloc(*list,sizeof(struct exclude_struct *)*(len+2));
2b6b4d53
AT
202
203 if (!*list || !((*list)[len] = make_exclude(pattern, include)))
204 out_of_memory("add_exclude");
205
206 if (verbose > 2)
207 rprintf(FINFO,"add_exclude(%s)\n",pattern);
208
209 (*list)[len+1] = NULL;
c627d613
AT
210}
211
2b6b4d53 212void add_exclude(char *pattern, int include)
c627d613 213{
2b6b4d53 214 add_exclude_list(pattern,&exclude_list, include);
c627d613
AT
215}
216
2b6b4d53
AT
217struct exclude_struct **make_exclude_list(char *fname,
218 struct exclude_struct **list1,
219 int fatal, int include)
c627d613 220{
2b6b4d53
AT
221 struct exclude_struct **list=list1;
222 FILE *f = fopen(fname,"r");
223 char line[MAXPATHLEN];
224 if (!f) {
225 if (fatal) {
226 rprintf(FERROR,"%s : %s\n",fname,strerror(errno));
227 exit_cleanup(1);
228 }
229 return list;
230 }
231
232 while (fgets(line,MAXPATHLEN,f)) {
233 int l = strlen(line);
234 if (l && line[l-1] == '\n') l--;
235 line[l] = 0;
236 if (line[0]) add_exclude_list(line,&list,include);
237 }
238 fclose(f);
239 return list;
c627d613
AT
240}
241
242
2b6b4d53 243void add_exclude_file(char *fname,int fatal,int include)
c627d613 244{
8f3a2d54
AT
245 if (!fname || !*fname) return;
246
2b6b4d53 247 exclude_list = make_exclude_list(fname,exclude_list,fatal,include);
c627d613
AT
248}
249
250
251void send_exclude_list(int f)
252{
2b6b4d53
AT
253 int i;
254 extern int remote_version;
255
256 if (!exclude_list) {
257 write_int(f,0);
258 return;
259 }
260
261 for (i=0;exclude_list[i];i++) {
262 char *pattern = exclude_list[i]->orig;
263 int l;
264
265 if (remote_version < 19) {
266 if (strncmp(pattern,"+ ", 2)==0) {
267 rprintf(FERROR,"remote rsync does not support include syntax - aborting\n");
268 exit_cleanup(1);
269 }
270
271 if (strncmp(pattern,"- ", 2) == 0) {
272 pattern += 2;
273 }
274 }
275
276 l = strlen(pattern);
277 if (l == 0) continue;
278 write_int(f,l);
279 write_buf(f,pattern,l);
280 }
281
282 write_int(f,0);
c627d613
AT
283}
284
285
286void recv_exclude_list(int f)
287{
2b6b4d53
AT
288 char line[MAXPATHLEN];
289 int l;
290 while ((l=read_int(f))) {
291 if (l >= MAXPATHLEN) overflow("recv_exclude_list");
292 read_sbuf(f,line,l);
293 add_exclude(line,0);
294 }
c627d613
AT
295}
296
297
8f3a2d54
AT
298void add_exclude_line(char *p)
299{
300 char *tok;
301 if (!p || !*p) return;
302 p = strdup(p);
303 if (!p) out_of_memory("add_exclude_line");
304 for (tok=strtok(p," "); tok; tok=strtok(NULL," "))
2b6b4d53 305 add_exclude(tok, 0);
8f3a2d54
AT
306 free(p);
307}
308
309
c627d613
AT
310static char *cvs_ignore_list[] = {
311 "RCS","SCCS","CVS","CVS.adm","RCSLOG","cvslog.*",
312 "tags","TAGS",".make.state",".nse_depinfo",
313 "*~", "#*", ".#*", ",*", "*.old", "*.bak", "*.BAK", "*.orig",
314 "*.rej", ".del-*", "*.a", "*.o", "*.obj", "*.so", "*.Z", "*.elc", "*.ln",
315 "core",NULL};
316
317
318
319void add_cvs_excludes(void)
320{
2b6b4d53
AT
321 char fname[MAXPATHLEN];
322 char *p;
323 int i;
c627d613 324
2b6b4d53
AT
325 for (i=0; cvs_ignore_list[i]; i++)
326 add_exclude(cvs_ignore_list[i], 0);
c627d613 327
2b6b4d53
AT
328 if ((p=getenv("HOME")) && strlen(p) < (MAXPATHLEN-12)) {
329 slprintf(fname,sizeof(fname)-1, "%s/.cvsignore",p);
330 add_exclude_file(fname,0,0);
331 }
c627d613 332
2b6b4d53 333 add_exclude_line(getenv("CVSIGNORE"));
c627d613 334}