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 :) */
26 extern int delete_mode;
28 static struct exclude_struct **exclude_list;
30 /* build an exclude structure given a exclude pattern */
31 static struct exclude_struct *make_exclude(char *pattern, int include)
33 struct exclude_struct *ret;
35 ret = (struct exclude_struct *)malloc(sizeof(*ret));
36 if (!ret) out_of_memory("make_exclude");
38 memset(ret, 0, sizeof(*ret));
40 if (strncmp(pattern,"- ",2) == 0) {
42 } else if (strncmp(pattern,"+ ",2) == 0) {
46 ret->include = include;
49 ret->pattern = strdup(pattern);
51 if (!ret->pattern) out_of_memory("make_exclude");
53 if (strpbrk(pattern, "*[?")) {
55 ret->fnmatch_flags = FNM_PATHNAME;
56 if (strstr(pattern, "**")) {
60 if (fnmatch("a/b/*", "a/b/c/d", FNM_PATHNAME)==0) {
61 rprintf(FERROR,"WARNING: fnmatch FNM_PATHNAME is broken on your system\n");
64 ret->fnmatch_flags = 0;
68 if (strlen(pattern) > 1 && pattern[strlen(pattern)-1] == '/') {
69 ret->pattern[strlen(pattern)-1] = 0;
73 if (!strchr(ret->pattern,'/')) {
80 static void free_exclude(struct exclude_struct *ex)
83 memset(ex,0,sizeof(*ex));
87 static int check_one_exclude(char *name,struct exclude_struct *ex,
92 char *pattern = ex->pattern;
94 if (ex->local && (p=strrchr(name,'/')))
97 if (!name[0]) return 0;
99 if (ex->directory && !S_ISDIR(st->st_mode)) return 0;
101 if (*pattern == '/' && *name != '/') {
106 if (ex->regular_exp) {
107 if (fnmatch(pattern, name, ex->fnmatch_flags) == 0) {
111 int l1 = strlen(name);
112 int l2 = strlen(pattern);
114 strcmp(name+(l1-l2),pattern) == 0 &&
115 (l1==l2 || (!match_start && name[l1-(l2+1)] == '/'))) {
124 int check_exclude(char *name,struct exclude_struct **local_exclude_list,
129 if (name && (name[0] == '.') && !name[1])
130 /* never exclude '.', even if somebody does --exclude '*' */
134 for (n=0; exclude_list[n]; n++)
135 if (check_one_exclude(name,exclude_list[n],st))
136 return !exclude_list[n]->include;
139 if (local_exclude_list) {
140 for (n=0; local_exclude_list[n]; n++)
141 if (check_one_exclude(name,local_exclude_list[n],st))
142 return !local_exclude_list[n]->include;
149 void add_exclude_list(char *pattern,struct exclude_struct ***list, int include)
153 for (; (*list)[len]; len++) ;
155 if (strcmp(pattern,"!") == 0) {
157 rprintf(FINFO,"clearing exclude list\n");
159 free_exclude((*list)[len]);
166 *list = (struct exclude_struct **)Realloc(*list,sizeof(struct exclude_struct *)*(len+2));
168 if (!*list || !((*list)[len] = make_exclude(pattern, include)))
169 out_of_memory("add_exclude");
172 rprintf(FINFO,"add_exclude(%s)\n",pattern);
174 (*list)[len+1] = NULL;
177 void add_exclude(char *pattern, int include)
179 add_exclude_list(pattern,&exclude_list, include);
182 struct exclude_struct **make_exclude_list(char *fname,
183 struct exclude_struct **list1,
184 int fatal, int include)
186 struct exclude_struct **list=list1;
187 FILE *f = fopen(fname,"r");
188 char line[MAXPATHLEN];
191 rprintf(FERROR,"%s : %s\n",fname,strerror(errno));
192 exit_cleanup(RERR_FILEIO);
197 while (fgets(line,MAXPATHLEN,f)) {
198 int l = strlen(line);
199 if (l && line[l-1] == '\n') l--;
201 if (line[0] && (line[0] != ';') && (line[0] != '#')) {
202 /* Skip lines starting with semicolon or pound.
203 It probably wouldn't cause any harm to not skip
204 them but there's no need to save them. */
205 add_exclude_list(line,&list,include);
213 void add_exclude_file(char *fname,int fatal,int include)
215 if (!fname || !*fname) return;
217 exclude_list = make_exclude_list(fname,exclude_list,fatal,include);
221 void send_exclude_list(int f)
224 extern int remote_version;
231 for (i=0;exclude_list[i];i++) {
233 char pattern[MAXPATHLEN];
235 strlcpy(pattern,exclude_list[i]->pattern,sizeof(pattern));
236 if (exclude_list[i]->directory) strlcat(pattern,"/", sizeof(pattern));
239 if (l == 0) continue;
240 if (exclude_list[i]->include) {
241 if (remote_version < 19) {
242 rprintf(FERROR,"remote rsync does not support include syntax - aborting\n");
243 exit_cleanup(RERR_UNSUPPORTED);
250 write_buf(f,pattern,l);
257 void recv_exclude_list(int f)
259 char line[MAXPATHLEN];
261 while ((l=read_int(f))) {
262 if (l >= MAXPATHLEN) overflow("recv_exclude_list");
268 /* Get the next include/exclude arg from the string. It works in a similar way
269 ** to strtok - initially an arg is sent over, from then on NULL. This
270 ** routine takes into account any +/- in the strings and does not
271 ** consider the space following it as a delimeter.
273 char *get_exclude_tok(char *p)
288 /* Skip over any initial spaces */
292 /* Are we at the end of the string? */
294 /* remember the beginning of the token */
297 /* Is this a '+' or '-' followed by a space (not whitespace)? */
298 if ((*s=='+' || *s=='-') && *(s+1)==' ')
301 /* Skip to the next space or the end of the string */
302 while(!isspace(*s) && *s!='\0')
308 /* Have we reached the end of the string? */
317 void add_exclude_line(char *p)
320 if (!p || !*p) return;
322 if (!p) out_of_memory("add_exclude_line");
323 for (tok=get_exclude_tok(p); tok; tok=get_exclude_tok(NULL))
328 void add_include_line(char *p)
331 if (!p || !*p) return;
333 if (!p) out_of_memory("add_include_line");
334 for (tok=get_exclude_tok(p); tok; tok=get_exclude_tok(NULL))
340 static char *cvs_ignore_list[] = {
341 "RCS","SCCS","CVS","CVS.adm","RCSLOG","cvslog.*",
342 "tags","TAGS",".make.state",".nse_depinfo",
343 "*~", "#*", ".#*", ",*", "*.old", "*.bak", "*.BAK", "*.orig",
344 "*.rej", ".del-*", "*.a", "*.o", "*.obj", "*.so", "*.Z", "*.elc", "*.ln",
349 void add_cvs_excludes(void)
351 char fname[MAXPATHLEN];
355 for (i=0; cvs_ignore_list[i]; i++)
356 add_exclude(cvs_ignore_list[i], 0);
358 if ((p=getenv("HOME")) && strlen(p) < (MAXPATHLEN-12)) {
359 slprintf(fname,sizeof(fname), "%s/.cvsignore",p);
360 add_exclude_file(fname,0,0);
363 add_exclude_line(getenv("CVSIGNORE"));