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> */
31 struct exclude_struct **exclude_list;
32 struct exclude_struct **local_exclude_list;
33 struct exclude_struct **server_exclude_list;
34 char *exclude_path_prefix = NULL;
36 /** Build an exclude structure given a exclude pattern */
37 static struct exclude_struct *make_exclude(const char *pattern, int pat_len,
40 struct exclude_struct *ret;
44 ret = new(struct exclude_struct);
46 out_of_memory("make_exclude");
48 memset(ret, 0, sizeof ret[0]);
49 ret->include = include;
51 if (exclude_path_prefix)
52 ret->match_flags |= MATCHFLG_ABS_PATH;
53 if (exclude_path_prefix && *pattern == '/')
54 ex_len = strlen(exclude_path_prefix);
57 ret->pattern = new_array(char, ex_len + pat_len + 1);
59 out_of_memory("make_exclude");
61 memcpy(ret->pattern, exclude_path_prefix, ex_len);
62 strlcpy(ret->pattern + ex_len, pattern, pat_len + 1);
65 if (strpbrk(ret->pattern, "*[?")) {
66 ret->match_flags |= MATCHFLG_WILD;
67 if ((cp = strstr(ret->pattern, "**")) != NULL) {
68 ret->match_flags |= MATCHFLG_WILD2;
69 /* If the pattern starts with **, note that. */
70 if (cp == ret->pattern)
71 ret->match_flags |= MATCHFLG_WILD2_PREFIX;
75 if (pat_len > 1 && ret->pattern[pat_len-1] == '/') {
76 ret->pattern[pat_len-1] = 0;
80 for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
86 static void free_exclude(struct exclude_struct *ex)
89 memset(ex, 0, sizeof ex[0]);
94 void free_exclude_list(struct exclude_struct ***listp)
96 struct exclude_struct **list = *listp;
99 rprintf(FINFO, "[%s] clearing exclude list\n", who_am_i());
105 free_exclude(*list++);
111 static int check_one_exclude(char *name, struct exclude_struct *ex,
116 char *pattern = ex->pattern;
118 /* If the pattern does not have any slashes AND it does not have
119 * a "**" (which could match a slash), then we just match the
120 * name portion of the path. */
121 if (!ex->slash_cnt && !(ex->match_flags & MATCHFLG_WILD2)) {
122 if ((p = strrchr(name,'/')) != NULL)
125 else if ((ex->match_flags & MATCHFLG_ABS_PATH) && *name != '/') {
126 static char full_name[MAXPATHLEN];
127 extern char curr_dir[];
128 int plus = curr_dir[1] == '\0'? 1 : 0;
129 pathjoin(full_name, sizeof full_name, curr_dir+plus, name);
133 if (!name[0]) return 0;
135 if (ex->directory && !name_is_dir) return 0;
137 if (*pattern == '/') {
144 if (ex->match_flags & MATCHFLG_WILD) {
145 /* A non-anchored match with an infix slash and no "**"
146 * needs to match the last slash_cnt+1 name elements. */
147 if (!match_start && ex->slash_cnt &&
148 !(ex->match_flags & MATCHFLG_WILD2)) {
149 int cnt = ex->slash_cnt + 1;
150 for (p = name + strlen(name) - 1; p >= name; p--) {
151 if (*p == '/' && !--cnt)
156 if (wildmatch(pattern, name))
158 if (ex->match_flags & MATCHFLG_WILD2_PREFIX) {
159 /* If the **-prefixed pattern has a '/' as the next
160 * character, then try to match the rest of the
161 * pattern at the root. */
162 if (pattern[2] == '/' && wildmatch(pattern+3, name))
165 else if (!match_start && ex->match_flags & MATCHFLG_WILD2) {
166 /* A non-anchored match with an infix or trailing "**"
167 * (but not a prefixed "**") needs to try matching
168 * after every slash. */
169 while ((name = strchr(name, '/')) != NULL) {
171 if (wildmatch(pattern, name))
175 } else if (match_start) {
176 if (strcmp(name,pattern) == 0)
179 int l1 = strlen(name);
180 int l2 = strlen(pattern);
182 strcmp(name+(l1-l2),pattern) == 0 &&
183 (l1==l2 || name[l1-(l2+1)] == '/')) {
192 static void report_exclude_result(char const *name,
193 struct exclude_struct const *ent,
194 int name_is_dir, const char *type)
196 /* If a trailing slash is present to match only directories,
197 * then it is stripped out by make_exclude. So as a special
198 * case we add it back in here. */
201 rprintf(FINFO, "[%s] %scluding %s %s because of %s %s%s\n",
202 who_am_i(), ent->include ? "in" : "ex",
203 name_is_dir ? "directory" : "file", name, type,
204 ent->pattern, ent->directory ? "/" : "");
210 * Return true if file NAME is defined to be excluded by either
211 * LOCAL_EXCLUDE_LIST or the globals EXCLUDE_LIST.
213 int check_exclude(struct exclude_struct **list, char *name, int name_is_dir,
216 struct exclude_struct *ent;
218 while ((ent = *list++) != NULL) {
219 if (check_one_exclude(name, ent, name_is_dir)) {
220 report_exclude_result(name, ent, name_is_dir, type);
221 return !ent->include;
229 /* Get the next include/exclude arg from the string. The token will not
230 * be '\0' terminated, so use the returned length to limit the string.
231 * Also, be sure to add this length to the returned pointer before passing
232 * it back to ask for the next token. This routine will not split off a
233 * prefix of "+ " or "- " unless xflags contains XFLG_NO_PREFIXES. The
234 * *incl_ptr value will be 1 for an include, 0 for an exclude, and -1 for
235 * the list-clearing "!" token.
237 static const char *get_exclude_tok(const char *p, int *len_ptr, int *incl_ptr,
240 const unsigned char *s = (const unsigned char *)p;
243 if (xflags & XFLG_WORD_SPLIT) {
244 /* Skip over any initial whitespace. */
247 /* Update for "!" check. */
251 /* Is this a '+' or '-' followed by a space (not whitespace)? */
252 if (!(xflags & XFLG_NO_PREFIXES)
253 && (*s == '-' || *s == '+') && s[1] == ' ') {
254 *incl_ptr = *s == '+';
257 *incl_ptr = xflags & XFLG_DEF_INCLUDE;
259 if (xflags & XFLG_WORD_SPLIT) {
260 const unsigned char *cp = s;
261 /* Token ends at whitespace or the end of the string. */
262 while (!isspace(*cp) && *cp != '\0')
268 if (*p == '!' && len == 1 && !(xflags & XFLG_NO_PREFIXES))
272 return (const char *)s;
276 void add_exclude(struct exclude_struct ***listp, const char *pattern, int xflags)
278 struct exclude_struct **list = *listp;
279 int pat_len, list_len = 0;
280 int incl, add_cnt = 1;
286 if (xflags & XFLG_WORD_SPLIT) {
288 /* Count maximum extra tokens we might encounter. */
289 for (cp = pattern; *cp; cp++) {
290 if (isspace(*(unsigned char *)cp)) {
298 cp = get_exclude_tok(pattern, &pat_len, &incl, xflags);
302 /* Check for the special "!" token that clears the list. Yes, we
303 * only honor it at the start of a XFLG_WORD_SPLIT string. */
305 free_exclude_list(listp);
308 cp = get_exclude_tok(cp + pat_len, &pat_len, &incl, xflags);
314 for ( ; list[list_len]; list_len++) {}
316 list = *listp = realloc_array(list, struct exclude_struct *,
317 list_len + add_cnt + 1);
319 out_of_memory("add_exclude");
322 list[list_len++] = make_exclude(cp, pat_len, incl);
325 rprintf(FINFO, "[%s] add_exclude(%s,%s)\n",
327 incl ? "include" : "exclude");
329 cp = get_exclude_tok(cp + pat_len, &pat_len, &incl, xflags);
332 list[list_len] = NULL;
336 void add_exclude_file(struct exclude_struct ***listp, const char *fname,
340 char line[MAXPATHLEN];
341 char *eob = line + MAXPATHLEN - 1;
342 extern int eol_nulls;
344 if (!fname || !*fname)
347 if (*fname != '-' || fname[1])
348 fp = fopen(fname, "rb");
352 if (xflags & XFLG_FATAL_ERRORS) {
353 rsyserr(FERROR, errno,
354 "failed to open %s file %s",
355 xflags & XFLG_DEF_INCLUDE ? "include" : "exclude",
357 exit_cleanup(RERR_FILEIO);
366 if ((ch = getc(fp)) == EOF) {
367 if (ferror(fp) && errno == EINTR)
371 if (eol_nulls? !ch : (ch == '\n' || ch == '\r'))
377 if (*line && *line != ';' && *line != '#') {
378 /* Skip lines starting with semicolon or pound.
379 * It probably wouldn't cause any harm to not skip
380 * them but there's no need to save them. */
381 add_exclude(listp, line, xflags);
390 void send_exclude_list(int f)
393 extern int list_only, recurse;
395 /* This is a complete hack - blame Rusty.
397 * FIXME: This pattern shows up in the output of
398 * report_exclude_result(), which is not ideal. */
399 if (list_only && !recurse)
400 add_exclude(&exclude_list, "/*/*", 0);
407 for (i = 0; exclude_list[i]; i++) {
409 char p[MAXPATHLEN+1];
411 l = strlcpy(p, exclude_list[i]->pattern, sizeof p);
412 if (l == 0 || l >= MAXPATHLEN)
414 if (exclude_list[i]->directory) {
419 if (exclude_list[i]->include) {
421 write_buf(f, "+ ", 2);
422 } else if ((*p == '-' || *p == '+') && p[1] == ' ') {
424 write_buf(f, "- ", 2);
434 void recv_exclude_list(int f)
436 char line[MAXPATHLEN+1]; /* Allows a trailing slash on a max-len dir */
439 while ((l = read_int(f)) != 0) {
440 if (l >= sizeof line)
441 overflow("recv_exclude_list");
442 read_sbuf(f, line, l);
443 add_exclude(&exclude_list, line, 0);
448 static char default_cvsignore[] =
449 /* These default ignored items come from the CVS manual. */
450 "RCS SCCS CVS CVS.adm RCSLOG cvslog.* tags TAGS"
451 " .make.state .nse_depinfo *~ #* .#* ,* _$* *$"
452 " *.old *.bak *.BAK *.orig *.rej .del-*"
453 " *.a *.olb *.o *.obj *.so *.exe"
454 " *.Z *.elc *.ln core"
455 /* The rest we added to suit ourself. */
458 void add_cvs_excludes(void)
460 char fname[MAXPATHLEN];
463 add_exclude(&exclude_list, default_cvsignore,
464 XFLG_WORD_SPLIT | XFLG_NO_PREFIXES);
466 if ((p = getenv("HOME"))
467 && pathjoin(fname, sizeof fname, p, ".cvsignore") < sizeof fname) {
468 add_exclude_file(&exclude_list, fname,
469 XFLG_WORD_SPLIT | XFLG_NO_PREFIXES);
472 add_exclude(&exclude_list, getenv("CVSIGNORE"),
473 XFLG_WORD_SPLIT | XFLG_NO_PREFIXES);