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, "per-dir .cvsignore " };
38 struct exclude_list_struct server_exclude_list = { 0, 0, "server " };
39 char *exclude_path_prefix = NULL;
41 /** Build an exclude structure given an exclude pattern. */
42 static void make_exclude(struct exclude_list_struct *listp, const char *pat,
43 unsigned int pat_len, unsigned int mflags)
45 struct exclude_struct *ret;
49 ret = new(struct exclude_struct);
51 out_of_memory("make_exclude");
53 memset(ret, 0, sizeof ret[0]);
55 if (exclude_path_prefix)
56 mflags |= MATCHFLG_ABS_PATH;
57 if (exclude_path_prefix && *pat == '/')
58 ex_len = strlen(exclude_path_prefix);
61 ret->pattern = new_array(char, ex_len + pat_len + 1);
63 out_of_memory("make_exclude");
65 memcpy(ret->pattern, exclude_path_prefix, ex_len);
66 strlcpy(ret->pattern + ex_len, pat, pat_len + 1);
69 if (strpbrk(ret->pattern, "*[?")) {
70 mflags |= MATCHFLG_WILD;
71 if ((cp = strstr(ret->pattern, "**")) != NULL) {
72 mflags |= MATCHFLG_WILD2;
73 /* If the pattern starts with **, note that. */
74 if (cp == ret->pattern)
75 mflags |= MATCHFLG_WILD2_PREFIX;
79 if (pat_len > 1 && ret->pattern[pat_len-1] == '/') {
80 ret->pattern[pat_len-1] = 0;
81 mflags |= MATCHFLG_DIRECTORY;
84 for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
87 ret->match_flags = mflags;
90 listp->head = listp->tail = ret;
92 listp->tail->next = ret;
97 static void free_exclude(struct exclude_struct *ex)
103 void clear_exclude_list(struct exclude_list_struct *listp)
105 struct exclude_struct *ent, *next;
107 for (ent = listp->head; ent; ent = next) {
112 listp->head = listp->tail = NULL;
115 static int check_one_exclude(char *name, struct exclude_struct *ex,
118 char *p, full_name[MAXPATHLEN];
120 char *pattern = ex->pattern;
125 /* If the pattern does not have any slashes AND it does not have
126 * a "**" (which could match a slash), then we just match the
127 * name portion of the path. */
128 if (!ex->slash_cnt && !(ex->match_flags & MATCHFLG_WILD2)) {
129 if ((p = strrchr(name,'/')) != NULL)
132 else if (ex->match_flags & MATCHFLG_ABS_PATH && *name != '/'
134 pathjoin(full_name, sizeof full_name, curr_dir + 1, name);
138 if (ex->match_flags & MATCHFLG_DIRECTORY && !name_is_dir)
141 if (*pattern == '/') {
148 if (ex->match_flags & MATCHFLG_WILD) {
149 /* A non-anchored match with an infix slash and no "**"
150 * needs to match the last slash_cnt+1 name elements. */
151 if (!match_start && ex->slash_cnt
152 && !(ex->match_flags & MATCHFLG_WILD2)) {
153 int cnt = ex->slash_cnt + 1;
154 for (p = name + strlen(name) - 1; p >= name; p--) {
155 if (*p == '/' && !--cnt)
160 if (wildmatch(pattern, name))
162 if (ex->match_flags & MATCHFLG_WILD2_PREFIX) {
163 /* If the **-prefixed pattern has a '/' as the next
164 * character, then try to match the rest of the
165 * pattern at the root. */
166 if (pattern[2] == '/' && wildmatch(pattern+3, name))
169 else if (!match_start && ex->match_flags & MATCHFLG_WILD2) {
170 /* A non-anchored match with an infix or trailing "**"
171 * (but not a prefixed "**") needs to try matching
172 * after every slash. */
173 while ((name = strchr(name, '/')) != NULL) {
175 if (wildmatch(pattern, name))
179 } else if (match_start) {
180 if (strcmp(name,pattern) == 0)
183 int l1 = strlen(name);
184 int l2 = strlen(pattern);
186 strcmp(name+(l1-l2),pattern) == 0 &&
187 (l1==l2 || name[l1-(l2+1)] == '/')) {
196 static void report_exclude_result(char const *name,
197 struct exclude_struct const *ent,
198 int name_is_dir, const char *type)
200 /* If a trailing slash is present to match only directories,
201 * then it is stripped out by make_exclude. So as a special
202 * case we add it back in here. */
205 rprintf(FINFO, "[%s] %scluding %s %s because of %spattern %s%s\n",
207 ent->match_flags & MATCHFLG_INCLUDE ? "in" : "ex",
208 name_is_dir ? "directory" : "file", name, type,
210 ent->match_flags & MATCHFLG_DIRECTORY ? "/" : "");
216 * Return -1 if file "name" is defined to be excluded by the specified
217 * exclude list, 1 if it is included, and 0 if it was not matched.
219 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,
227 return ent->match_flags & MATCHFLG_INCLUDE ? 1 : -1;
235 /* Get the next include/exclude arg from the string. The token will not
236 * be '\0' terminated, so use the returned length to limit the string.
237 * Also, be sure to add this length to the returned pointer before passing
238 * it back to ask for the next token. This routine parses the +/- prefixes
239 * and the "!" token unless xflags contains XFLG_WORDS_ONLY. The *flag_ptr
240 * value will also be set to the MATCHFLG_* bits for the current token.
242 static const char *get_exclude_tok(const char *p, unsigned int *len_ptr,
243 unsigned int *flag_ptr, int xflags)
245 const unsigned char *s = (const unsigned char *)p;
246 unsigned int len, mflags = 0;
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_WORDS_ONLY)
258 && (*s == '-' || *s == '+') && s[1] == ' ') {
260 mflags |= MATCHFLG_INCLUDE;
262 } else if (xflags & XFLG_DEF_INCLUDE)
263 mflags |= MATCHFLG_INCLUDE;
265 if (xflags & XFLG_WORD_SPLIT) {
266 const unsigned char *cp = s;
267 /* Token ends at whitespace or the end of the string. */
268 while (!isspace(*cp) && *cp != '\0')
274 if (*p == '!' && len == 1 && !(xflags & XFLG_WORDS_ONLY))
275 mflags |= MATCHFLG_CLEAR_LIST;
279 return (const char *)s;
283 void add_exclude(struct exclude_list_struct *listp, const char *pattern,
286 unsigned int pat_len, mflags;
295 cp = get_exclude_tok(cp + pat_len, &pat_len, &mflags, xflags);
299 if (mflags & MATCHFLG_CLEAR_LIST) {
302 "[%s] clearing %sexclude list\n",
303 who_am_i(), listp->debug_type);
305 clear_exclude_list(listp);
309 make_exclude(listp, cp, pat_len, mflags);
312 rprintf(FINFO, "[%s] add_exclude(%.*s, %s%sclude)\n",
313 who_am_i(), (int)pat_len, cp, listp->debug_type,
314 mflags & MATCHFLG_INCLUDE ? "in" : "ex");
320 void add_exclude_file(struct exclude_list_struct *listp, const char *fname,
324 char line[MAXPATHLEN+3]; /* Room for "x " prefix and trailing slash. */
325 char *eob = line + sizeof line - 1;
326 int word_split = xflags & XFLG_WORD_SPLIT;
328 if (!fname || !*fname)
331 if (*fname != '-' || fname[1])
332 fp = fopen(fname, "rb");
336 if (xflags & XFLG_FATAL_ERRORS) {
337 rsyserr(FERROR, errno,
338 "failed to open %s file %s",
339 xflags & XFLG_DEF_INCLUDE ? "include" : "exclude",
341 exit_cleanup(RERR_FILEIO);
348 int ch, overflow = 0;
350 if ((ch = getc(fp)) == EOF) {
351 if (ferror(fp) && errno == EINTR)
355 if (word_split && isspace(ch))
357 if (eol_nulls? !ch : (ch == '\n' || ch == '\r'))
365 rprintf(FERROR, "discarding over-long exclude: %s...\n", line);
369 /* Skip an empty token and (when line parsing) comments. */
370 if (*line && (word_split || (*line != ';' && *line != '#')))
371 add_exclude(listp, line, xflags);
379 void send_exclude_list(int f)
381 struct exclude_struct *ent;
383 /* This is a complete hack - blame Rusty.
385 * FIXME: This pattern shows up in the output of
386 * report_exclude_result(), which is not ideal. */
387 if (list_only && !recurse)
388 add_exclude(&exclude_list, "/*/*", 0);
390 for (ent = exclude_list.head; ent; ent = ent->next) {
392 char p[MAXPATHLEN+1];
394 l = strlcpy(p, ent->pattern, sizeof p);
395 if (l == 0 || l >= MAXPATHLEN)
397 if (ent->match_flags & MATCHFLG_DIRECTORY) {
402 if (ent->match_flags & MATCHFLG_INCLUDE) {
404 write_buf(f, "+ ", 2);
405 } else if ((*p == '-' || *p == '+') && p[1] == ' ') {
407 write_buf(f, "- ", 2);
417 void recv_exclude_list(int f)
419 char line[MAXPATHLEN+3]; /* Room for "x " prefix and trailing slash. */
422 while ((l = read_int(f)) != 0) {
423 if (l >= sizeof line)
424 overflow("recv_exclude_list");
425 read_sbuf(f, line, l);
426 add_exclude(&exclude_list, line, 0);
431 static char default_cvsignore[] =
432 /* These default ignored items come from the CVS manual. */
433 "RCS SCCS CVS CVS.adm RCSLOG cvslog.* tags TAGS"
434 " .make.state .nse_depinfo *~ #* .#* ,* _$* *$"
435 " *.old *.bak *.BAK *.orig *.rej .del-*"
436 " *.a *.olb *.o *.obj *.so *.exe"
437 " *.Z *.elc *.ln core"
438 /* The rest we added to suit ourself. */
441 void add_cvs_excludes(void)
443 char fname[MAXPATHLEN];
446 add_exclude(&exclude_list, default_cvsignore,
447 XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);
449 if ((p = getenv("HOME"))
450 && pathjoin(fname, sizeof fname, p, ".cvsignore") < sizeof fname) {
451 add_exclude_file(&exclude_list, fname,
452 XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);
455 add_exclude(&exclude_list, getenv("CVSIGNORE"),
456 XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);