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> */
34 extern char curr_dir[];
36 struct exclude_list_struct exclude_list;
37 struct exclude_list_struct local_exclude_list;
38 struct exclude_list_struct server_exclude_list;
39 char *exclude_path_prefix = NULL;
41 /** Build an exclude structure given a exclude pattern */
42 static void make_exclude(struct exclude_list_struct *listp, const char *pattern,
43 int pat_len, int include)
45 struct exclude_struct *ret;
49 ret = new(struct exclude_struct);
51 out_of_memory("make_exclude");
53 memset(ret, 0, sizeof ret[0]);
54 ret->include = include;
56 if (exclude_path_prefix)
57 ret->match_flags |= MATCHFLG_ABS_PATH;
58 if (exclude_path_prefix && *pattern == '/')
59 ex_len = strlen(exclude_path_prefix);
62 ret->pattern = new_array(char, ex_len + pat_len + 1);
64 out_of_memory("make_exclude");
66 memcpy(ret->pattern, exclude_path_prefix, ex_len);
67 strlcpy(ret->pattern + ex_len, pattern, pat_len + 1);
70 if (strpbrk(ret->pattern, "*[?")) {
71 ret->match_flags |= MATCHFLG_WILD;
72 if ((cp = strstr(ret->pattern, "**")) != NULL) {
73 ret->match_flags |= MATCHFLG_WILD2;
74 /* If the pattern starts with **, note that. */
75 if (cp == ret->pattern)
76 ret->match_flags |= MATCHFLG_WILD2_PREFIX;
80 if (pat_len > 1 && ret->pattern[pat_len-1] == '/') {
81 ret->pattern[pat_len-1] = 0;
85 for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
89 listp->head = listp->tail = ret;
91 listp->tail->next = ret;
96 static void free_exclude(struct exclude_struct *ex)
102 void free_exclude_list(struct exclude_list_struct *listp)
104 struct exclude_struct *ent, *next;
107 rprintf(FINFO, "[%s] clearing exclude list\n", who_am_i());
109 for (ent = listp->head; ent; ent = next) {
114 memset(listp, 0, sizeof listp[0]);
117 static int check_one_exclude(char *name, struct exclude_struct *ex,
122 char *pattern = ex->pattern;
124 /* If the pattern does not have any slashes AND it does not have
125 * a "**" (which could match a slash), then we just match the
126 * name portion of the path. */
127 if (!ex->slash_cnt && !(ex->match_flags & MATCHFLG_WILD2)) {
128 if ((p = strrchr(name,'/')) != NULL)
131 else if ((ex->match_flags & MATCHFLG_ABS_PATH) && *name != '/') {
132 static char full_name[MAXPATHLEN];
133 int plus = curr_dir[1] == '\0'? 1 : 0;
134 pathjoin(full_name, sizeof full_name, curr_dir+plus, name);
138 if (!name[0]) return 0;
140 if (ex->directory && !name_is_dir) return 0;
142 if (*pattern == '/') {
149 if (ex->match_flags & MATCHFLG_WILD) {
150 /* A non-anchored match with an infix slash and no "**"
151 * needs to match the last slash_cnt+1 name elements. */
152 if (!match_start && ex->slash_cnt &&
153 !(ex->match_flags & MATCHFLG_WILD2)) {
154 int cnt = ex->slash_cnt + 1;
155 for (p = name + strlen(name) - 1; p >= name; p--) {
156 if (*p == '/' && !--cnt)
161 if (wildmatch(pattern, name))
163 if (ex->match_flags & MATCHFLG_WILD2_PREFIX) {
164 /* If the **-prefixed pattern has a '/' as the next
165 * character, then try to match the rest of the
166 * pattern at the root. */
167 if (pattern[2] == '/' && wildmatch(pattern+3, name))
170 else if (!match_start && ex->match_flags & MATCHFLG_WILD2) {
171 /* A non-anchored match with an infix or trailing "**"
172 * (but not a prefixed "**") needs to try matching
173 * after every slash. */
174 while ((name = strchr(name, '/')) != NULL) {
176 if (wildmatch(pattern, name))
180 } else if (match_start) {
181 if (strcmp(name,pattern) == 0)
184 int l1 = strlen(name);
185 int l2 = strlen(pattern);
187 strcmp(name+(l1-l2),pattern) == 0 &&
188 (l1==l2 || name[l1-(l2+1)] == '/')) {
197 static void report_exclude_result(char const *name,
198 struct exclude_struct const *ent,
199 int name_is_dir, const char *type)
201 /* If a trailing slash is present to match only directories,
202 * then it is stripped out by make_exclude. So as a special
203 * case we add it back in here. */
206 rprintf(FINFO, "[%s] %scluding %s %s because of %s %s%s\n",
207 who_am_i(), ent->include ? "in" : "ex",
208 name_is_dir ? "directory" : "file", name, type,
209 ent->pattern, ent->directory ? "/" : "");
215 * Return true if file NAME is defined to be excluded by the specified
218 int check_exclude(struct exclude_list_struct *listp, char *name, int name_is_dir,
221 struct exclude_struct *ent;
223 for (ent = listp->head; ent; ent = ent->next) {
224 if (check_one_exclude(name, ent, name_is_dir)) {
225 report_exclude_result(name, ent, name_is_dir, type);
226 return !ent->include;
234 /* Get the next include/exclude arg from the string. The token will not
235 * be '\0' terminated, so use the returned length to limit the string.
236 * Also, be sure to add this length to the returned pointer before passing
237 * it back to ask for the next token. This routine will not split off a
238 * prefix of "+ " or "- " unless xflags contains XFLG_NO_PREFIXES. The
239 * *incl_ptr value will be 1 for an include, 0 for an exclude, and -1 for
240 * the list-clearing "!" token.
242 static const char *get_exclude_tok(const char *p, int *len_ptr, int *incl_ptr,
245 const unsigned char *s = (const unsigned char *)p;
248 if (xflags & XFLG_WORD_SPLIT) {
249 /* Skip over any initial whitespace. */
252 /* Update for "!" check. */
256 /* Is this a '+' or '-' followed by a space (not whitespace)? */
257 if (!(xflags & XFLG_NO_PREFIXES)
258 && (*s == '-' || *s == '+') && s[1] == ' ') {
259 *incl_ptr = *s == '+';
262 *incl_ptr = xflags & XFLG_DEF_INCLUDE;
264 if (xflags & XFLG_WORD_SPLIT) {
265 const unsigned char *cp = s;
266 /* Token ends at whitespace or the end of the string. */
267 while (!isspace(*cp) && *cp != '\0')
273 if (*p == '!' && len == 1 && !(xflags & XFLG_NO_PREFIXES))
277 return (const char *)s;
281 void add_exclude(struct exclude_list_struct *listp, const char *pattern,
293 cp = get_exclude_tok(cp + pat_len, &pat_len, &incl, xflags);
296 /* If we got the special "!" token, clear the list. */
298 free_exclude_list(listp);
300 make_exclude(listp, cp, pat_len, incl);
303 rprintf(FINFO, "[%s] add_exclude(%s,%s)\n",
305 incl ? "include" : "exclude");
312 void add_exclude_file(struct exclude_list_struct *listp, const char *fname,
316 char line[MAXPATHLEN];
317 char *eob = line + MAXPATHLEN - 1;
318 int word_split = xflags & XFLG_WORD_SPLIT;
320 if (!fname || !*fname)
323 if (*fname != '-' || fname[1])
324 fp = fopen(fname, "rb");
328 if (xflags & XFLG_FATAL_ERRORS) {
329 rsyserr(FERROR, errno,
330 "failed to open %s file %s",
331 xflags & XFLG_DEF_INCLUDE ? "include" : "exclude",
333 exit_cleanup(RERR_FILEIO);
342 if ((ch = getc(fp)) == EOF) {
343 if (ferror(fp) && errno == EINTR)
347 if (word_split && isspace(ch))
349 if (eol_nulls? !ch : (ch == '\n' || ch == '\r'))
355 /* Skip lines starting with semicolon or pound. */
356 if (*line && *line != ';' && *line != '#')
357 add_exclude(listp, line, xflags);
365 void send_exclude_list(int f)
367 struct exclude_struct *ent;
369 /* This is a complete hack - blame Rusty.
371 * FIXME: This pattern shows up in the output of
372 * report_exclude_result(), which is not ideal. */
373 if (list_only && !recurse)
374 add_exclude(&exclude_list, "/*/*", 0);
376 for (ent = exclude_list.head; ent; ent = ent->next) {
378 char p[MAXPATHLEN+1];
380 l = strlcpy(p, ent->pattern, sizeof p);
381 if (l == 0 || l >= MAXPATHLEN)
383 if (ent->directory) {
390 write_buf(f, "+ ", 2);
391 } else if ((*p == '-' || *p == '+') && p[1] == ' ') {
393 write_buf(f, "- ", 2);
403 void recv_exclude_list(int f)
405 char line[MAXPATHLEN+1]; /* Allows a trailing slash on a max-len dir */
408 while ((l = read_int(f)) != 0) {
409 if (l >= sizeof line)
410 overflow("recv_exclude_list");
411 read_sbuf(f, line, l);
412 add_exclude(&exclude_list, line, 0);
417 static char default_cvsignore[] =
418 /* These default ignored items come from the CVS manual. */
419 "RCS SCCS CVS CVS.adm RCSLOG cvslog.* tags TAGS"
420 " .make.state .nse_depinfo *~ #* .#* ,* _$* *$"
421 " *.old *.bak *.BAK *.orig *.rej .del-*"
422 " *.a *.olb *.o *.obj *.so *.exe"
423 " *.Z *.elc *.ln core"
424 /* The rest we added to suit ourself. */
427 void add_cvs_excludes(void)
429 char fname[MAXPATHLEN];
432 add_exclude(&exclude_list, default_cvsignore,
433 XFLG_WORD_SPLIT | XFLG_NO_PREFIXES);
435 if ((p = getenv("HOME"))
436 && pathjoin(fname, sizeof fname, p, ".cvsignore") < sizeof fname) {
437 add_exclude_file(&exclude_list, fname,
438 XFLG_WORD_SPLIT | XFLG_NO_PREFIXES);
441 add_exclude(&exclude_list, getenv("CVSIGNORE"),
442 XFLG_WORD_SPLIT | XFLG_NO_PREFIXES);