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");
67 ret->fnmatch_flags = 0;
71 if (strlen(pattern) > 1 && pattern[strlen(pattern)-1] == '/') {
72 ret->pattern[strlen(pattern)-1] = 0;
76 if (!strchr(ret->pattern,'/')) {
83 static void free_exclude(struct exclude_struct *ex)
86 memset(ex,0,sizeof(*ex));
90 static int check_one_exclude(char *name, struct exclude_struct *ex,
95 char *pattern = ex->pattern;
97 if (ex->local && (p=strrchr(name,'/')))
100 if (!name[0]) return 0;
102 if (ex->directory && !S_ISDIR(st->st_mode)) return 0;
104 if (*pattern == '/' && *name != '/') {
109 if (ex->regular_exp) {
110 if (fnmatch(pattern, name, ex->fnmatch_flags) == 0) {
114 int l1 = strlen(name);
115 int l2 = strlen(pattern);
117 strcmp(name+(l1-l2),pattern) == 0 &&
118 (l1==l2 || (!match_start && name[l1-(l2+1)] == '/'))) {
127 static void report_exclude_result(char const *name,
128 struct exclude_struct const *ent,
129 STRUCT_STAT const *st)
131 /* If a trailing slash is present to match only directories,
132 * then it is stripped out by make_exclude. So as a special
133 * case we add it back in here. */
136 rprintf(FINFO, "%s %s %s because of pattern %s%s\n",
137 ent->include ? "including" : "excluding",
138 S_ISDIR(st->st_mode) ? "directory" : "file",
140 ent->directory ? "/" : "");
145 * Return true if file NAME is defined to be excluded by either
146 * LOCAL_EXCLUDE_LIST or the globals EXCLUDE_LIST.
148 int check_exclude(char *name, struct exclude_struct **local_exclude_list,
152 struct exclude_struct *ent;
154 if (name && (name[0] == '.') && !name[1])
155 /* never exclude '.', even if somebody does --exclude '*' */
159 for (n=0; exclude_list[n]; n++) {
160 ent = exclude_list[n];
161 if (check_one_exclude(name, ent, st)) {
162 report_exclude_result(name, ent, st);
163 return !ent->include;
168 if (local_exclude_list) {
169 for (n=0; local_exclude_list[n]; n++) {
170 ent = local_exclude_list[n];
171 if (check_one_exclude(name, ent, st)) {
172 report_exclude_result(name, ent, st);
173 return !ent->include;
182 void add_exclude_list(const char *pattern, struct exclude_struct ***list, int include)
186 for (; (*list)[len]; len++) ;
188 if (strcmp(pattern,"!") == 0) {
190 rprintf(FINFO,"clearing exclude list\n");
192 free_exclude((*list)[len]);
199 *list = (struct exclude_struct **)Realloc(*list,sizeof(struct exclude_struct *)*(len+2));
201 if (!*list || !((*list)[len] = make_exclude(pattern, include)))
202 out_of_memory("add_exclude");
205 rprintf(FINFO,"add_exclude(%s,%s)\n",pattern,
206 include ? "include" : "exclude");
209 (*list)[len+1] = NULL;
212 void add_exclude(const char *pattern, int include)
214 add_exclude_list(pattern,&exclude_list, include);
217 struct exclude_struct **make_exclude_list(const char *fname,
218 struct exclude_struct **list1,
219 int fatal, int include)
221 struct exclude_struct **list=list1;
223 char line[MAXPATHLEN];
224 char *eob = line + MAXPATHLEN - 1;
225 extern int eol_nulls;
227 if (strcmp(fname, "-") != 0)
228 fd = open(fname, O_RDONLY|O_BINARY);
233 rsyserr(FERROR, errno,
234 "failed to open %s file %s",
235 include ? "include" : "exclude",
237 exit_cleanup(RERR_FILEIO);
246 if ((cnt = read(fd, &ch, 1)) <= 0) {
247 if (cnt < 0 && errno == EINTR)
251 if (eol_nulls? !ch : (ch == '\n' || ch == '\r'))
257 if (*line && *line != ';' && *line != '#') {
258 /* Skip lines starting with semicolon or pound.
259 * It probably wouldn't cause any harm to not skip
260 * them but there's no need to save them. */
261 add_exclude_list(line,&list,include);
271 void add_exclude_file(const char *fname, int fatal, int include)
273 if (!fname || !*fname) return;
275 exclude_list = make_exclude_list(fname,exclude_list,fatal,include);
279 void send_exclude_list(int f)
282 extern int remote_version;
283 extern int list_only, recurse;
285 /* This is a complete hack - blame Rusty.
287 * FIXME: This pattern shows up in the output of
288 * report_exclude_result(), which is not ideal. */
289 if (list_only && !recurse) {
290 add_exclude("/*/*", 0);
298 for (i=0;exclude_list[i];i++) {
300 char pattern[MAXPATHLEN];
302 strlcpy(pattern,exclude_list[i]->pattern,sizeof(pattern));
303 if (exclude_list[i]->directory) strlcat(pattern,"/", sizeof(pattern));
306 if (l == 0) continue;
307 if (exclude_list[i]->include) {
308 if (remote_version < 19) {
309 rprintf(FERROR,"remote rsync does not support include syntax - aborting\n");
310 exit_cleanup(RERR_UNSUPPORTED);
317 write_buf(f,pattern,l);
324 void recv_exclude_list(int f)
326 char line[MAXPATHLEN];
329 while ((l=read_int(f))) {
330 if (l >= MAXPATHLEN) overflow("recv_exclude_list");
336 /* Get the next include/exclude arg from the string. It works in a similar way
337 ** to strtok - initially an arg is sent over, from then on NULL. This
338 ** routine takes into account any +/- in the strings and does not
339 ** consider the space following it as a delimeter.
341 char *get_exclude_tok(char *p)
356 /* Skip over any initial spaces */
357 while (isspace(* (unsigned char *) s))
360 /* Are we at the end of the string? */
362 /* remember the beginning of the token */
365 /* Is this a '+' or '-' followed by a space (not whitespace)? */
366 if ((*s=='+' || *s=='-') && *(s+1)==' ')
369 /* Skip to the next space or the end of the string */
370 while (!isspace(* (unsigned char *) s) && *s != '\0')
376 /* Have we reached the end of the string? */
385 void add_exclude_line(char *p)
388 if (!p || !*p) return;
390 if (!p) out_of_memory("add_exclude_line");
391 for (tok=get_exclude_tok(p); tok; tok=get_exclude_tok(NULL))
396 void add_include_line(char *p)
399 if (!p || !*p) return;
401 if (!p) out_of_memory("add_include_line");
402 for (tok=get_exclude_tok(p); tok; tok=get_exclude_tok(NULL))
408 static char *cvs_ignore_list[] = {
409 "RCS/", "SCCS/", "CVS/", ".svn/", "CVS.adm", "RCSLOG", "cvslog.*",
410 "tags", "TAGS", ".make.state", ".nse_depinfo",
411 "*~", "#*", ".#*", ", *", "*.old", "*.bak", "*.BAK", "*.orig",
412 "*.rej", ".del-*", "*.a", "*.o", "*.obj", "*.so", "*.Z", "*.elc", "*.ln",
416 void add_cvs_excludes(void)
418 char fname[MAXPATHLEN];
422 for (i=0; cvs_ignore_list[i]; i++)
423 add_exclude(cvs_ignore_list[i], 0);
425 if ((p=getenv("HOME")) && strlen(p) < (MAXPATHLEN-12)) {
426 snprintf(fname,sizeof(fname), "%s/.cvsignore",p);
427 add_exclude_file(fname,0,0);
430 add_exclude_line(getenv("CVSIGNORE"));