1 /* -*- c-file-style: "linux" -*-
3 * Copyright (C) 1996-2001 by Andrew Tridgell <tridge@samba.org>
4 * Copyright (C) 1996 by Paul Mackerras
5 * Copyright (C) 2002 by Martin Pool
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 /* a lot of this stuff was originally derived from GNU tar, although
23 it has now changed so much that it is hard to tell :) */
25 /* include/exclude cluestick added by Martin Pool <mbp@samba.org> */
30 extern int delete_mode;
32 static struct exclude_struct **exclude_list;
34 /** Build an exclude structure given a exclude pattern */
35 static struct exclude_struct *make_exclude(const char *pattern, int include)
37 struct exclude_struct *ret;
39 ret = (struct exclude_struct *)malloc(sizeof(*ret));
40 if (!ret) out_of_memory("make_exclude");
42 memset(ret, 0, sizeof(*ret));
44 if (strncmp(pattern,"- ",2) == 0) {
46 } else if (strncmp(pattern,"+ ",2) == 0) {
50 ret->include = include;
53 ret->pattern = strdup(pattern);
55 if (!ret->pattern) out_of_memory("make_exclude");
57 if (strpbrk(pattern, "*[?")) {
59 ret->fnmatch_flags = FNM_PATHNAME;
60 if (strstr(pattern, "**")) {
64 if (fnmatch("a/b/*", "a/b/c/d", FNM_PATHNAME)==0) {
65 rprintf(FERROR,"WARNING: fnmatch FNM_PATHNAME is broken on your system\n");
68 ret->fnmatch_flags = 0;
72 if (strlen(pattern) > 1 && pattern[strlen(pattern)-1] == '/') {
73 ret->pattern[strlen(pattern)-1] = 0;
77 if (!strchr(ret->pattern,'/')) {
84 static void free_exclude(struct exclude_struct *ex)
87 memset(ex,0,sizeof(*ex));
91 static int check_one_exclude(char *name, struct exclude_struct *ex,
96 char *pattern = ex->pattern;
98 if (ex->local && (p=strrchr(name,'/')))
101 if (!name[0]) return 0;
103 if (ex->directory && !S_ISDIR(st->st_mode)) return 0;
105 if (*pattern == '/' && *name != '/') {
110 if (ex->regular_exp) {
111 if (fnmatch(pattern, name, ex->fnmatch_flags) == 0) {
115 int l1 = strlen(name);
116 int l2 = strlen(pattern);
118 strcmp(name+(l1-l2),pattern) == 0 &&
119 (l1==l2 || (!match_start && name[l1-(l2+1)] == '/'))) {
128 static void report_exclude_result(char const *name,
129 struct exclude_struct const *ent,
130 STRUCT_STAT const *st)
132 /* If a trailing slash is present to match only directories,
133 * then it is stripped out by make_exclude. So as a special
134 * case we add it back in here. */
137 rprintf(FINFO, "%s %s %s because of pattern %s%s\n",
138 ent->include ? "including" : "excluding",
139 S_ISDIR(st->st_mode) ? "directory" : "file",
141 ent->directory ? "/" : "");
146 * Return true if file NAME is defined to be excluded by either
147 * LOCAL_EXCLUDE_LIST or the globals EXCLUDE_LIST.
149 int check_exclude(char *name, struct exclude_struct **local_exclude_list,
153 struct exclude_struct *ent;
155 if (name && (name[0] == '.') && !name[1])
156 /* never exclude '.', even if somebody does --exclude '*' */
160 for (n=0; exclude_list[n]; n++) {
161 ent = exclude_list[n];
162 if (check_one_exclude(name, ent, st)) {
163 report_exclude_result(name, ent, st);
164 return !ent->include;
169 if (local_exclude_list) {
170 for (n=0; local_exclude_list[n]; n++) {
171 ent = local_exclude_list[n];
172 if (check_one_exclude(name, ent, st)) {
173 report_exclude_result(name, ent, st);
174 return !ent->include;
183 void add_exclude_list(const char *pattern, struct exclude_struct ***list, int include)
187 for (; (*list)[len]; len++) ;
189 if (strcmp(pattern,"!") == 0) {
191 rprintf(FINFO,"clearing exclude list\n");
193 free_exclude((*list)[len]);
200 *list = (struct exclude_struct **)Realloc(*list,sizeof(struct exclude_struct *)*(len+2));
202 if (!*list || !((*list)[len] = make_exclude(pattern, include)))
203 out_of_memory("add_exclude");
206 rprintf(FINFO,"add_exclude(%s,%s)\n",pattern,
207 include ? "include" : "exclude");
210 (*list)[len+1] = NULL;
213 void add_exclude(const char *pattern, int include)
215 add_exclude_list(pattern,&exclude_list, include);
218 struct exclude_struct **make_exclude_list(const char *fname,
219 struct exclude_struct **list1,
220 int fatal, int include)
222 struct exclude_struct **list=list1;
223 FILE *f = fopen(fname,"r");
224 char line[MAXPATHLEN];
227 rsyserr(FERROR, errno,
228 "failed to open %s file %s",
229 include ? "include" : "exclude",
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(const 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;
264 extern int list_only, recurse;
266 /* This is a complete hack - blame Rusty.
268 * FIXME: This pattern shows up in the output of
269 * report_exclude_result(), which is not ideal. */
270 if (list_only && !recurse) {
271 add_exclude("/*/*", 0);
279 for (i=0;exclude_list[i];i++) {
281 char pattern[MAXPATHLEN];
283 strlcpy(pattern,exclude_list[i]->pattern,sizeof(pattern));
284 if (exclude_list[i]->directory) strlcat(pattern,"/", sizeof(pattern));
287 if (l == 0) continue;
288 if (exclude_list[i]->include) {
289 if (remote_version < 19) {
290 rprintf(FERROR,"remote rsync does not support include syntax - aborting\n");
291 exit_cleanup(RERR_UNSUPPORTED);
298 write_buf(f,pattern,l);
305 void recv_exclude_list(int f)
307 char line[MAXPATHLEN];
310 while ((l=read_int(f))) {
311 if (l >= MAXPATHLEN) overflow("recv_exclude_list");
317 /* Get the next include/exclude arg from the string. It works in a similar way
318 ** to strtok - initially an arg is sent over, from then on NULL. This
319 ** routine takes into account any +/- in the strings and does not
320 ** consider the space following it as a delimeter.
322 char *get_exclude_tok(char *p)
337 /* Skip over any initial spaces */
338 while(isspace((int) *s))
341 /* Are we at the end of the string? */
343 /* remember the beginning of the token */
346 /* Is this a '+' or '-' followed by a space (not whitespace)? */
347 if ((*s=='+' || *s=='-') && *(s+1)==' ')
350 /* Skip to the next space or the end of the string */
351 while(!isspace((int) *s) && *s != '\0')
357 /* Have we reached the end of the string? */
366 void add_exclude_line(char *p)
369 if (!p || !*p) return;
371 if (!p) out_of_memory("add_exclude_line");
372 for (tok=get_exclude_tok(p); tok; tok=get_exclude_tok(NULL))
377 void add_include_line(char *p)
380 if (!p || !*p) return;
382 if (!p) out_of_memory("add_include_line");
383 for (tok=get_exclude_tok(p); tok; tok=get_exclude_tok(NULL))
389 static char *cvs_ignore_list[] = {
390 "RCS","SCCS","CVS","CVS.adm","RCSLOG","cvslog.*",
391 "tags","TAGS",".make.state",".nse_depinfo",
392 "*~", "#*", ".#*", ",*", "*.old", "*.bak", "*.BAK", "*.orig",
393 "*.rej", ".del-*", "*.a", "*.o", "*.obj", "*.so", "*.Z", "*.elc", "*.ln",
398 void add_cvs_excludes(void)
400 char fname[MAXPATHLEN];
404 for (i=0; cvs_ignore_list[i]; i++)
405 add_exclude(cvs_ignore_list[i], 0);
407 if ((p=getenv("HOME")) && strlen(p) < (MAXPATHLEN-12)) {
408 snprintf(fname,sizeof(fname), "%s/.cvsignore",p);
409 add_exclude_file(fname,0,0);
412 add_exclude_line(getenv("CVSIGNORE"));