2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
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.
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.
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.
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 :) */
27 static struct exclude_struct **exclude_list;
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>
35 static int only_included_files = 1;
36 static struct exclude_struct *exclude_the_rest;
38 int send_included_file_names(int f,struct file_list *flist)
40 struct exclude_struct *ex, **ex_list;
44 if (!only_included_files || (exclude_the_rest == NULL))
48 rprintf(FINFO,"(using include-only optimization) ");
51 /* set exclude_list to NULL temporarily so check_exclude */
52 /* will always return true */
53 ex_list = exclude_list;
55 for (n=0; (ex = ex_list[n]) != NULL; n++) {
56 if (ex == exclude_the_rest)
60 /* skip the allowed beginning slashes */
63 /* silently skip files that don't exist to
64 be more like non-optimized case */
66 send_file_name(f,flist,p,0,0);
68 exclude_list = ex_list;
73 /* build an exclude structure given a exclude pattern */
74 static struct exclude_struct *make_exclude(char *pattern, int include)
76 struct exclude_struct *ret;
78 ret = (struct exclude_struct *)malloc(sizeof(*ret));
79 if (!ret) out_of_memory("make_exclude");
81 memset(ret, 0, sizeof(*ret));
83 if (strncmp(pattern,"- ",2) == 0) {
85 } else if (strncmp(pattern,"+ ",2) == 0) {
89 ret->include = include;
92 ret->pattern = strdup(pattern);
94 if (!ret->pattern) out_of_memory("make_exclude");
96 if (strpbrk(pattern, "*[?")) {
97 if (!ret->include && (*pattern == '*') && (*(pattern+1) == '\0')) {
98 exclude_the_rest = ret;
100 only_included_files = 0;
102 ret->regular_exp = 1;
103 } else if (!ret->include) {
104 only_included_files = 0;
107 if (strlen(pattern) > 1 && pattern[strlen(pattern)-1] == '/') {
108 ret->pattern[strlen(pattern)-1] = 0;
112 if (!strchr(ret->pattern,'/')) {
119 static void free_exclude(struct exclude_struct *ex)
122 memset(ex,0,sizeof(*ex));
126 static int check_one_exclude(char *name,struct exclude_struct *ex,
131 char *pattern = ex->pattern;
133 if (ex->local && (p=strrchr(name,'/')))
136 if (!name[0]) return 0;
138 if (ex->directory && !S_ISDIR(st->st_mode)) return 0;
140 if (*pattern == '/' && *name != '/') {
145 if (ex->regular_exp) {
146 if (fnmatch(pattern, name, 0) == 0)
149 int l1 = strlen(name);
150 int l2 = strlen(pattern);
152 strcmp(name+(l1-l2),pattern) == 0 &&
153 (l1==l2 || (!match_start && name[l1-(l2+1)] == '/')))
161 int check_exclude(char *name,struct exclude_struct **local_exclude_list,
166 if (name && (name[0] == '.') && !name[1])
167 /* never exclude '.', even if somebody does --exclude '*' */
171 for (n=0; exclude_list[n]; n++)
172 if (check_one_exclude(name,exclude_list[n],st))
173 return !exclude_list[n]->include;
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;
186 void add_exclude_list(char *pattern,struct exclude_struct ***list, int include)
190 for (; (*list)[len]; len++) ;
192 if (strcmp(pattern,"!") == 0) {
194 rprintf(FINFO,"clearing exclude list\n");
196 free_exclude((*list)[len]);
200 only_included_files = 1;
201 exclude_the_rest = NULL;
205 *list = (struct exclude_struct **)Realloc(*list,sizeof(struct exclude_struct *)*(len+2));
207 if (!*list || !((*list)[len] = make_exclude(pattern, include)))
208 out_of_memory("add_exclude");
211 rprintf(FINFO,"add_exclude(%s)\n",pattern);
213 (*list)[len+1] = NULL;
216 void add_exclude(char *pattern, int include)
218 add_exclude_list(pattern,&exclude_list, include);
221 struct exclude_struct **make_exclude_list(char *fname,
222 struct exclude_struct **list1,
223 int fatal, int include)
225 struct exclude_struct **list=list1;
226 FILE *f = fopen(fname,"r");
227 char line[MAXPATHLEN];
230 rprintf(FERROR,"%s : %s\n",fname,strerror(errno));
231 exit_cleanup(RERR_FILEIO);
236 while (fgets(line,MAXPATHLEN,f)) {
237 int l = strlen(line);
238 if (l && line[l-1] == '\n') l--;
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);
252 void add_exclude_file(char *fname,int fatal,int include)
254 if (!fname || !*fname) return;
256 exclude_list = make_exclude_list(fname,exclude_list,fatal,include);
260 void send_exclude_list(int f)
263 extern int remote_version;
270 for (i=0;exclude_list[i];i++) {
271 char *pattern = exclude_list[i]->pattern;
275 if (l == 0) continue;
276 if (exclude_list[i]->include) {
277 if (remote_version < 19) {
278 rprintf(FERROR,"remote rsync does not support include syntax - aborting\n");
279 exit_cleanup(RERR_UNSUPPORTED);
286 write_buf(f,pattern,l);
293 void recv_exclude_list(int f)
295 char line[MAXPATHLEN];
297 while ((l=read_int(f))) {
298 if (l >= MAXPATHLEN) overflow("recv_exclude_list");
305 void add_exclude_line(char *p)
308 if (!p || !*p) return;
310 if (!p) out_of_memory("add_exclude_line");
311 for (tok=strtok(p," "); tok; tok=strtok(NULL," "))
316 void add_include_line(char *p)
319 if (!p || !*p) return;
321 if (!p) out_of_memory("add_include_line");
322 for (tok=strtok(p," "); tok; tok=strtok(NULL," "))
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",
337 void add_cvs_excludes(void)
339 char fname[MAXPATHLEN];
343 for (i=0; cvs_ignore_list[i]; i++)
344 add_exclude(cvs_ignore_list[i], 0);
346 if ((p=getenv("HOME")) && strlen(p) < (MAXPATHLEN-12)) {
347 slprintf(fname,sizeof(fname), "%s/.cvsignore",p);
348 add_exclude_file(fname,0,0);
351 add_exclude_line(getenv("CVSIGNORE"));