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 = { 0, 0, "" };
37 struct exclude_list_struct local_exclude_list = { 0, 0, "local-cvsignore " };
38 struct exclude_list_struct server_exclude_list = { 0, 0, "server " };
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 %sexclude list\n",
108 who_am_i(), listp->debug_type);
111 for (ent = listp->head; ent; ent = next) {
116 listp->head = listp->tail = NULL;
119 static int check_one_exclude(char *name, struct exclude_struct *ex,
124 char *pattern = ex->pattern;
126 /* If the pattern does not have any slashes AND it does not have
127 * a "**" (which could match a slash), then we just match the
128 * name portion of the path. */
129 if (!ex->slash_cnt && !(ex->match_flags & MATCHFLG_WILD2)) {
130 if ((p = strrchr(name,'/')) != NULL)
133 else if ((ex->match_flags & MATCHFLG_ABS_PATH) && *name != '/') {
134 static char full_name[MAXPATHLEN];
135 int plus = curr_dir[1] == '\0'? 1 : 0;
136 pathjoin(full_name, sizeof full_name, curr_dir+plus, name);
140 if (!name[0]) return 0;
142 if (ex->directory && !name_is_dir) return 0;
144 if (*pattern == '/') {
151 if (ex->match_flags & MATCHFLG_WILD) {
152 /* A non-anchored match with an infix slash and no "**"
153 * needs to match the last slash_cnt+1 name elements. */
154 if (!match_start && ex->slash_cnt &&
155 !(ex->match_flags & MATCHFLG_WILD2)) {
156 int cnt = ex->slash_cnt + 1;
157 for (p = name + strlen(name) - 1; p >= name; p--) {
158 if (*p == '/' && !--cnt)
163 if (wildmatch(pattern, name))
165 if (ex->match_flags & MATCHFLG_WILD2_PREFIX) {
166 /* If the **-prefixed pattern has a '/' as the next
167 * character, then try to match the rest of the
168 * pattern at the root. */
169 if (pattern[2] == '/' && wildmatch(pattern+3, name))
172 else if (!match_start && ex->match_flags & MATCHFLG_WILD2) {
173 /* A non-anchored match with an infix or trailing "**"
174 * (but not a prefixed "**") needs to try matching
175 * after every slash. */
176 while ((name = strchr(name, '/')) != NULL) {
178 if (wildmatch(pattern, name))
182 } else if (match_start) {
183 if (strcmp(name,pattern) == 0)
186 int l1 = strlen(name);
187 int l2 = strlen(pattern);
189 strcmp(name+(l1-l2),pattern) == 0 &&
190 (l1==l2 || name[l1-(l2+1)] == '/')) {
199 static void report_exclude_result(char const *name,
200 struct exclude_struct const *ent,
201 int name_is_dir, const char *type)
203 /* If a trailing slash is present to match only directories,
204 * then it is stripped out by make_exclude. So as a special
205 * case we add it back in here. */
208 rprintf(FINFO, "[%s] %scluding %s %s because of %spattern %s%s\n",
209 who_am_i(), ent->include ? "in" : "ex",
210 name_is_dir ? "directory" : "file", name, type,
211 ent->pattern, ent->directory ? "/" : "");
217 * Return true if file NAME is defined to be excluded by the specified
220 int check_exclude(struct exclude_list_struct *listp, char *name, int name_is_dir)
222 struct exclude_struct *ent;
224 for (ent = listp->head; ent; ent = ent->next) {
225 if (check_one_exclude(name, ent, name_is_dir)) {
226 report_exclude_result(name, ent, name_is_dir,
228 return !ent->include;
236 /* Get the next include/exclude arg from the string. The token will not
237 * be '\0' terminated, so use the returned length to limit the string.
238 * Also, be sure to add this length to the returned pointer before passing
239 * it back to ask for the next token. This routine will not split off a
240 * prefix of "+ " or "- " unless xflags contains XFLG_NO_PREFIXES. The
241 * *incl_ptr value will be 1 for an include, 0 for an exclude, and -1 for
242 * the list-clearing "!" token.
244 static const char *get_exclude_tok(const char *p, int *len_ptr, int *incl_ptr,
247 const unsigned char *s = (const unsigned char *)p;
250 if (xflags & XFLG_WORD_SPLIT) {
251 /* Skip over any initial whitespace. */
254 /* Update for "!" check. */
258 /* Is this a '+' or '-' followed by a space (not whitespace)? */
259 if (!(xflags & XFLG_NO_PREFIXES)
260 && (*s == '-' || *s == '+') && s[1] == ' ') {
261 *incl_ptr = *s == '+';
264 *incl_ptr = xflags & XFLG_DEF_INCLUDE;
266 if (xflags & XFLG_WORD_SPLIT) {
267 const unsigned char *cp = s;
268 /* Token ends at whitespace or the end of the string. */
269 while (!isspace(*cp) && *cp != '\0')
275 if (*p == '!' && len == 1 && !(xflags & XFLG_NO_PREFIXES))
279 return (const char *)s;
283 void add_exclude(struct exclude_list_struct *listp, const char *pattern,
295 cp = get_exclude_tok(cp + pat_len, &pat_len, &incl, xflags);
298 /* If we got the special "!" token, clear the list. */
300 free_exclude_list(listp);
302 make_exclude(listp, cp, pat_len, incl);
305 rprintf(FINFO, "[%s] add_exclude(%.*s, %s%s)\n",
306 who_am_i(), pat_len, cp,
308 incl ? "include" : "exclude");
315 void add_exclude_file(struct exclude_list_struct *listp, const char *fname,
319 char line[MAXPATHLEN];
320 char *eob = line + MAXPATHLEN - 1;
321 int word_split = xflags & XFLG_WORD_SPLIT;
323 if (!fname || !*fname)
326 if (*fname != '-' || fname[1])
327 fp = fopen(fname, "rb");
331 if (xflags & XFLG_FATAL_ERRORS) {
332 rsyserr(FERROR, errno,
333 "failed to open %s file %s",
334 xflags & XFLG_DEF_INCLUDE ? "include" : "exclude",
336 exit_cleanup(RERR_FILEIO);
345 if ((ch = getc(fp)) == EOF) {
346 if (ferror(fp) && errno == EINTR)
350 if (word_split && isspace(ch))
352 if (eol_nulls? !ch : (ch == '\n' || ch == '\r'))
358 /* Skip lines starting with semicolon or pound. */
359 if (*line && *line != ';' && *line != '#')
360 add_exclude(listp, line, xflags);
368 void send_exclude_list(int f)
370 struct exclude_struct *ent;
372 /* This is a complete hack - blame Rusty.
374 * FIXME: This pattern shows up in the output of
375 * report_exclude_result(), which is not ideal. */
376 if (list_only && !recurse)
377 add_exclude(&exclude_list, "/*/*", 0);
379 for (ent = exclude_list.head; ent; ent = ent->next) {
381 char p[MAXPATHLEN+1];
383 l = strlcpy(p, ent->pattern, sizeof p);
384 if (l == 0 || l >= MAXPATHLEN)
386 if (ent->directory) {
393 write_buf(f, "+ ", 2);
394 } else if ((*p == '-' || *p == '+') && p[1] == ' ') {
396 write_buf(f, "- ", 2);
406 void recv_exclude_list(int f)
408 char line[MAXPATHLEN+1]; /* Allows a trailing slash on a max-len dir */
411 while ((l = read_int(f)) != 0) {
412 if (l >= sizeof line)
413 overflow("recv_exclude_list");
414 read_sbuf(f, line, l);
415 add_exclude(&exclude_list, line, 0);
420 static char default_cvsignore[] =
421 /* These default ignored items come from the CVS manual. */
422 "RCS SCCS CVS CVS.adm RCSLOG cvslog.* tags TAGS"
423 " .make.state .nse_depinfo *~ #* .#* ,* _$* *$"
424 " *.old *.bak *.BAK *.orig *.rej .del-*"
425 " *.a *.olb *.o *.obj *.so *.exe"
426 " *.Z *.elc *.ln core"
427 /* The rest we added to suit ourself. */
430 void add_cvs_excludes(void)
432 char fname[MAXPATHLEN];
435 add_exclude(&exclude_list, default_cvsignore,
436 XFLG_WORD_SPLIT | XFLG_NO_PREFIXES);
438 if ((p = getenv("HOME"))
439 && pathjoin(fname, sizeof fname, p, ".cvsignore") < sizeof fname) {
440 add_exclude_file(&exclude_list, fname,
441 XFLG_WORD_SPLIT | XFLG_NO_PREFIXES);
444 add_exclude(&exclude_list, getenv("CVSIGNORE"),
445 XFLG_WORD_SPLIT | XFLG_NO_PREFIXES);