File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / rsync / exclude.c
Revision 1.1.1.2 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Mon Oct 14 07:51:14 2013 UTC (10 years, 8 months ago) by misho
Branches: rsync, MAIN
CVS tags: RSYNC3_1_0, HEAD
v 3.1.0

    1: /*
    2:  * The filter include/exclude routines.
    3:  *
    4:  * Copyright (C) 1996-2001 Andrew Tridgell <tridge@samba.org>
    5:  * Copyright (C) 1996 Paul Mackerras
    6:  * Copyright (C) 2002 Martin Pool
    7:  * Copyright (C) 2003-2013 Wayne Davison
    8:  *
    9:  * This program is free software; you can redistribute it and/or modify
   10:  * it under the terms of the GNU General Public License as published by
   11:  * the Free Software Foundation; either version 3 of the License, or
   12:  * (at your option) any later version.
   13:  *
   14:  * This program is distributed in the hope that it will be useful,
   15:  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   16:  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   17:  * GNU General Public License for more details.
   18:  *
   19:  * You should have received a copy of the GNU General Public License along
   20:  * with this program; if not, visit the http://fsf.org website.
   21:  */
   22: 
   23: #include "rsync.h"
   24: 
   25: extern int am_server;
   26: extern int am_sender;
   27: extern int eol_nulls;
   28: extern int io_error;
   29: extern int local_server;
   30: extern int prune_empty_dirs;
   31: extern int ignore_perishable;
   32: extern int delete_mode;
   33: extern int delete_excluded;
   34: extern int cvs_exclude;
   35: extern int sanitize_paths;
   36: extern int protocol_version;
   37: extern int module_id;
   38: 
   39: extern char curr_dir[MAXPATHLEN];
   40: extern unsigned int curr_dir_len;
   41: extern unsigned int module_dirlen;
   42: 
   43: filter_rule_list filter_list = { .debug_type = "" };
   44: filter_rule_list cvs_filter_list = { .debug_type = " [global CVS]" };
   45: filter_rule_list daemon_filter_list = { .debug_type = " [daemon]" };
   46: 
   47: /* Need room enough for ":MODS " prefix plus some room to grow. */
   48: #define MAX_RULE_PREFIX (16)
   49: 
   50: #define SLASH_WILD3_SUFFIX "/***"
   51: 
   52: /* The dirbuf is set by push_local_filters() to the current subdirectory
   53:  * relative to curr_dir that is being processed.  The path always has a
   54:  * trailing slash appended, and the variable dirbuf_len contains the length
   55:  * of this path prefix.  The path is always absolute. */
   56: static char dirbuf[MAXPATHLEN+1];
   57: static unsigned int dirbuf_len = 0;
   58: static int dirbuf_depth;
   59: 
   60: /* This is True when we're scanning parent dirs for per-dir merge-files. */
   61: static BOOL parent_dirscan = False;
   62: 
   63: /* This array contains a list of all the currently active per-dir merge
   64:  * files.  This makes it easier to save the appropriate values when we
   65:  * "push" down into each subdirectory. */
   66: static filter_rule **mergelist_parents;
   67: static int mergelist_cnt = 0;
   68: static int mergelist_size = 0;
   69: 
   70: /* Each filter_list_struct describes a singly-linked list by keeping track
   71:  * of both the head and tail pointers.  The list is slightly unusual in that
   72:  * a parent-dir's content can be appended to the end of the local list in a
   73:  * special way:  the last item in the local list has its "next" pointer set
   74:  * to point to the inherited list, but the local list's tail pointer points
   75:  * at the end of the local list.  Thus, if the local list is empty, the head
   76:  * will be pointing at the inherited content but the tail will be NULL.  To
   77:  * help you visualize this, here are the possible list arrangements:
   78:  *
   79:  * Completely Empty                     Local Content Only
   80:  * ==================================   ====================================
   81:  * head -> NULL                         head -> Local1 -> Local2 -> NULL
   82:  * tail -> NULL                         tail -------------^
   83:  *
   84:  * Inherited Content Only               Both Local and Inherited Content
   85:  * ==================================   ====================================
   86:  * head -> Parent1 -> Parent2 -> NULL   head -> L1 -> L2 -> P1 -> P2 -> NULL
   87:  * tail -> NULL                         tail ---------^
   88:  *
   89:  * This means that anyone wanting to traverse the whole list to use it just
   90:  * needs to start at the head and use the "next" pointers until it goes
   91:  * NULL.  To add new local content, we insert the item after the tail item
   92:  * and update the tail (obviously, if "tail" was NULL, we insert it at the
   93:  * head).  To clear the local list, WE MUST NOT FREE THE INHERITED CONTENT
   94:  * because it is shared between the current list and our parent list(s).
   95:  * The easiest way to handle this is to simply truncate the list after the
   96:  * tail item and then free the local list from the head.  When inheriting
   97:  * the list for a new local dir, we just save off the filter_list_struct
   98:  * values (so we can pop back to them later) and set the tail to NULL.
   99:  */
  100: 
  101: static void teardown_mergelist(filter_rule *ex)
  102: {
  103: 	if (DEBUG_GTE(FILTER, 2)) {
  104: 		rprintf(FINFO, "[%s] deactivating mergelist #%d%s\n",
  105: 			who_am_i(), mergelist_cnt - 1,
  106: 			ex->u.mergelist->debug_type);
  107: 	}
  108: 
  109: 	/* We should deactivate mergelists in LIFO order. */
  110: 	assert(mergelist_cnt > 0);
  111: 	assert(ex == mergelist_parents[mergelist_cnt - 1]);
  112: 
  113: 	/* The parent_dirscan filters should have been freed. */
  114: 	assert(ex->u.mergelist->parent_dirscan_head == NULL);
  115: 
  116: 	free(ex->u.mergelist->debug_type);
  117: 	free(ex->u.mergelist);
  118: 	mergelist_cnt--;
  119: }
  120: 
  121: static void free_filter(filter_rule *ex)
  122: {
  123: 	free(ex->pattern);
  124: 	free(ex);
  125: }
  126: 
  127: static void free_filters(filter_rule *head)
  128: {
  129: 	filter_rule *rev_head = NULL;
  130: 
  131: 	/* Reverse the list so we deactivate mergelists in the proper LIFO
  132: 	 * order. */
  133: 	while (head) {
  134: 		filter_rule *next = head->next;
  135: 		head->next = rev_head;
  136: 		rev_head = head;
  137: 		head = next;
  138: 	}
  139: 
  140: 	while (rev_head) {
  141: 		filter_rule *prev = rev_head->next;
  142: 		/* Tear down mergelists here, not in free_filter, so that we
  143: 		 * affect only real filter lists and not temporarily allocated
  144: 		 * filters. */
  145: 		if (rev_head->rflags & FILTRULE_PERDIR_MERGE)
  146: 			teardown_mergelist(rev_head);
  147: 		free_filter(rev_head);
  148: 		rev_head = prev;
  149: 	}
  150: }
  151: 
  152: /* Build a filter structure given a filter pattern.  The value in "pat"
  153:  * is not null-terminated.  "rule" is either held or freed, so the
  154:  * caller should not free it. */
  155: static void add_rule(filter_rule_list *listp, const char *pat, unsigned int pat_len,
  156: 		     filter_rule *rule, int xflags)
  157: {
  158: 	const char *cp;
  159: 	unsigned int pre_len, suf_len, slash_cnt = 0;
  160: 
  161: 	if (DEBUG_GTE(FILTER, 2)) {
  162: 		rprintf(FINFO, "[%s] add_rule(%s%.*s%s)%s\n",
  163: 			who_am_i(), get_rule_prefix(rule, pat, 0, NULL),
  164: 			(int)pat_len, pat,
  165: 			(rule->rflags & FILTRULE_DIRECTORY) ? "/" : "",
  166: 			listp->debug_type);
  167: 	}
  168: 
  169: 	/* These flags also indicate that we're reading a list that
  170: 	 * needs to be filtered now, not post-filtered later. */
  171: 	if (xflags & (XFLG_ANCHORED2ABS|XFLG_ABS_IF_SLASH)
  172: 		&& (rule->rflags & FILTRULES_SIDES)
  173: 			== (am_sender ? FILTRULE_RECEIVER_SIDE : FILTRULE_SENDER_SIDE)) {
  174: 		/* This filter applies only to the other side.  Drop it. */
  175: 		free_filter(rule);
  176: 		return;
  177: 	}
  178: 
  179: 	if (pat_len > 1 && pat[pat_len-1] == '/') {
  180: 		pat_len--;
  181: 		rule->rflags |= FILTRULE_DIRECTORY;
  182: 	}
  183: 
  184: 	for (cp = pat; cp < pat + pat_len; cp++) {
  185: 		if (*cp == '/')
  186: 			slash_cnt++;
  187: 	}
  188: 
  189: 	if (!(rule->rflags & (FILTRULE_ABS_PATH | FILTRULE_MERGE_FILE))
  190: 	 && ((xflags & (XFLG_ANCHORED2ABS|XFLG_ABS_IF_SLASH) && *pat == '/')
  191: 	  || (xflags & XFLG_ABS_IF_SLASH && slash_cnt))) {
  192: 		rule->rflags |= FILTRULE_ABS_PATH;
  193: 		if (*pat == '/')
  194: 			pre_len = dirbuf_len - module_dirlen - 1;
  195: 		else
  196: 			pre_len = 0;
  197: 	} else
  198: 		pre_len = 0;
  199: 
  200: 	/* The daemon wants dir-exclude rules to get an appended "/" + "***". */
  201: 	if (xflags & XFLG_DIR2WILD3
  202: 	 && BITS_SETnUNSET(rule->rflags, FILTRULE_DIRECTORY, FILTRULE_INCLUDE)) {
  203: 		rule->rflags &= ~FILTRULE_DIRECTORY;
  204: 		suf_len = sizeof SLASH_WILD3_SUFFIX - 1;
  205: 	} else
  206: 		suf_len = 0;
  207: 
  208: 	if (!(rule->pattern = new_array(char, pre_len + pat_len + suf_len + 1)))
  209: 		out_of_memory("add_rule");
  210: 	if (pre_len) {
  211: 		memcpy(rule->pattern, dirbuf + module_dirlen, pre_len);
  212: 		for (cp = rule->pattern; cp < rule->pattern + pre_len; cp++) {
  213: 			if (*cp == '/')
  214: 				slash_cnt++;
  215: 		}
  216: 	}
  217: 	strlcpy(rule->pattern + pre_len, pat, pat_len + 1);
  218: 	pat_len += pre_len;
  219: 	if (suf_len) {
  220: 		memcpy(rule->pattern + pat_len, SLASH_WILD3_SUFFIX, suf_len+1);
  221: 		pat_len += suf_len;
  222: 		slash_cnt++;
  223: 	}
  224: 
  225: 	if (strpbrk(rule->pattern, "*[?")) {
  226: 		rule->rflags |= FILTRULE_WILD;
  227: 		if ((cp = strstr(rule->pattern, "**")) != NULL) {
  228: 			rule->rflags |= FILTRULE_WILD2;
  229: 			/* If the pattern starts with **, note that. */
  230: 			if (cp == rule->pattern)
  231: 				rule->rflags |= FILTRULE_WILD2_PREFIX;
  232: 			/* If the pattern ends with ***, note that. */
  233: 			if (pat_len >= 3
  234: 			 && rule->pattern[pat_len-3] == '*'
  235: 			 && rule->pattern[pat_len-2] == '*'
  236: 			 && rule->pattern[pat_len-1] == '*')
  237: 				rule->rflags |= FILTRULE_WILD3_SUFFIX;
  238: 		}
  239: 	}
  240: 
  241: 	if (rule->rflags & FILTRULE_PERDIR_MERGE) {
  242: 		filter_rule_list *lp;
  243: 		unsigned int len;
  244: 		int i;
  245: 
  246: 		if ((cp = strrchr(rule->pattern, '/')) != NULL)
  247: 			cp++;
  248: 		else
  249: 			cp = rule->pattern;
  250: 
  251: 		/* If the local merge file was already mentioned, don't
  252: 		 * add it again. */
  253: 		for (i = 0; i < mergelist_cnt; i++) {
  254: 			filter_rule *ex = mergelist_parents[i];
  255: 			const char *s = strrchr(ex->pattern, '/');
  256: 			if (s)
  257: 				s++;
  258: 			else
  259: 				s = ex->pattern;
  260: 			len = strlen(s);
  261: 			if (len == pat_len - (cp - rule->pattern) && memcmp(s, cp, len) == 0) {
  262: 				free_filter(rule);
  263: 				return;
  264: 			}
  265: 		}
  266: 
  267: 		if (!(lp = new_array(filter_rule_list, 1)))
  268: 			out_of_memory("add_rule");
  269: 		lp->head = lp->tail = lp->parent_dirscan_head = NULL;
  270: 		if (asprintf(&lp->debug_type, " [per-dir %s]", cp) < 0)
  271: 			out_of_memory("add_rule");
  272: 		rule->u.mergelist = lp;
  273: 
  274: 		if (mergelist_cnt == mergelist_size) {
  275: 			mergelist_size += 5;
  276: 			mergelist_parents = realloc_array(mergelist_parents,
  277: 						filter_rule *,
  278: 						mergelist_size);
  279: 			if (!mergelist_parents)
  280: 				out_of_memory("add_rule");
  281: 		}
  282: 		if (DEBUG_GTE(FILTER, 2)) {
  283: 			rprintf(FINFO, "[%s] activating mergelist #%d%s\n",
  284: 				who_am_i(), mergelist_cnt, lp->debug_type);
  285: 		}
  286: 		mergelist_parents[mergelist_cnt++] = rule;
  287: 	} else
  288: 		rule->u.slash_cnt = slash_cnt;
  289: 
  290: 	if (!listp->tail) {
  291: 		rule->next = listp->head;
  292: 		listp->head = listp->tail = rule;
  293: 	} else {
  294: 		rule->next = listp->tail->next;
  295: 		listp->tail->next = rule;
  296: 		listp->tail = rule;
  297: 	}
  298: }
  299: 
  300: static void clear_filter_list(filter_rule_list *listp)
  301: {
  302: 	if (listp->tail) {
  303: 		/* Truncate any inherited items from the local list. */
  304: 		listp->tail->next = NULL;
  305: 		/* Now free everything that is left. */
  306: 		free_filters(listp->head);
  307: 	}
  308: 
  309: 	listp->head = listp->tail = NULL;
  310: }
  311: 
  312: /* This returns an expanded (absolute) filename for the merge-file name if
  313:  * the name has any slashes in it OR if the parent_dirscan var is True;
  314:  * otherwise it returns the original merge_file name.  If the len_ptr value
  315:  * is non-NULL the merge_file name is limited by the referenced length
  316:  * value and will be updated with the length of the resulting name.  We
  317:  * always return a name that is null terminated, even if the merge_file
  318:  * name was not. */
  319: static char *parse_merge_name(const char *merge_file, unsigned int *len_ptr,
  320: 			      unsigned int prefix_skip)
  321: {
  322: 	static char buf[MAXPATHLEN];
  323: 	char *fn, tmpbuf[MAXPATHLEN];
  324: 	unsigned int fn_len;
  325: 
  326: 	if (!parent_dirscan && *merge_file != '/') {
  327: 		/* Return the name unchanged it doesn't have any slashes. */
  328: 		if (len_ptr) {
  329: 			const char *p = merge_file + *len_ptr;
  330: 			while (--p > merge_file && *p != '/') {}
  331: 			if (p == merge_file) {
  332: 				strlcpy(buf, merge_file, *len_ptr + 1);
  333: 				return buf;
  334: 			}
  335: 		} else if (strchr(merge_file, '/') == NULL)
  336: 			return (char *)merge_file;
  337: 	}
  338: 
  339: 	fn = *merge_file == '/' ? buf : tmpbuf;
  340: 	if (sanitize_paths) {
  341: 		const char *r = prefix_skip ? "/" : NULL;
  342: 		/* null-terminate the name if it isn't already */
  343: 		if (len_ptr && merge_file[*len_ptr]) {
  344: 			char *to = fn == buf ? tmpbuf : buf;
  345: 			strlcpy(to, merge_file, *len_ptr + 1);
  346: 			merge_file = to;
  347: 		}
  348: 		if (!sanitize_path(fn, merge_file, r, dirbuf_depth, SP_DEFAULT)) {
  349: 			rprintf(FERROR, "merge-file name overflows: %s\n",
  350: 				merge_file);
  351: 			return NULL;
  352: 		}
  353: 		fn_len = strlen(fn);
  354: 	} else {
  355: 		strlcpy(fn, merge_file, len_ptr ? *len_ptr + 1 : MAXPATHLEN);
  356: 		fn_len = clean_fname(fn, CFN_COLLAPSE_DOT_DOT_DIRS);
  357: 	}
  358: 
  359: 	/* If the name isn't in buf yet, it's wasn't absolute. */
  360: 	if (fn != buf) {
  361: 		int d_len = dirbuf_len - prefix_skip;
  362: 		if (d_len + fn_len >= MAXPATHLEN) {
  363: 			rprintf(FERROR, "merge-file name overflows: %s\n", fn);
  364: 			return NULL;
  365: 		}
  366: 		memcpy(buf, dirbuf + prefix_skip, d_len);
  367: 		memcpy(buf + d_len, fn, fn_len + 1);
  368: 		fn_len = clean_fname(buf, CFN_COLLAPSE_DOT_DOT_DIRS);
  369: 	}
  370: 
  371: 	if (len_ptr)
  372: 		*len_ptr = fn_len;
  373: 	return buf;
  374: }
  375: 
  376: /* Sets the dirbuf and dirbuf_len values. */
  377: void set_filter_dir(const char *dir, unsigned int dirlen)
  378: {
  379: 	unsigned int len;
  380: 	if (*dir != '/') {
  381: 		memcpy(dirbuf, curr_dir, curr_dir_len);
  382: 		dirbuf[curr_dir_len] = '/';
  383: 		len = curr_dir_len + 1;
  384: 		if (len + dirlen >= MAXPATHLEN)
  385: 			dirlen = 0;
  386: 	} else
  387: 		len = 0;
  388: 	memcpy(dirbuf + len, dir, dirlen);
  389: 	dirbuf[dirlen + len] = '\0';
  390: 	dirbuf_len = clean_fname(dirbuf, CFN_COLLAPSE_DOT_DOT_DIRS);
  391: 	if (dirbuf_len > 1 && dirbuf[dirbuf_len-1] == '.'
  392: 	    && dirbuf[dirbuf_len-2] == '/')
  393: 		dirbuf_len -= 2;
  394: 	if (dirbuf_len != 1)
  395: 		dirbuf[dirbuf_len++] = '/';
  396: 	dirbuf[dirbuf_len] = '\0';
  397: 	if (sanitize_paths)
  398: 		dirbuf_depth = count_dir_elements(dirbuf + module_dirlen);
  399: }
  400: 
  401: /* This routine takes a per-dir merge-file entry and finishes its setup.
  402:  * If the name has a path portion then we check to see if it refers to a
  403:  * parent directory of the first transfer dir.  If it does, we scan all the
  404:  * dirs from that point through the parent dir of the transfer dir looking
  405:  * for the per-dir merge-file in each one. */
  406: static BOOL setup_merge_file(int mergelist_num, filter_rule *ex,
  407: 			     filter_rule_list *lp)
  408: {
  409: 	char buf[MAXPATHLEN];
  410: 	char *x, *y, *pat = ex->pattern;
  411: 	unsigned int len;
  412: 
  413: 	if (!(x = parse_merge_name(pat, NULL, 0)) || *x != '/')
  414: 		return 0;
  415: 
  416: 	if (DEBUG_GTE(FILTER, 2)) {
  417: 		rprintf(FINFO, "[%s] performing parent_dirscan for mergelist #%d%s\n",
  418: 			who_am_i(), mergelist_num, lp->debug_type);
  419: 	}
  420: 	y = strrchr(x, '/');
  421: 	*y = '\0';
  422: 	ex->pattern = strdup(y+1);
  423: 	if (!*x)
  424: 		x = "/";
  425: 	if (*x == '/')
  426: 		strlcpy(buf, x, MAXPATHLEN);
  427: 	else
  428: 		pathjoin(buf, MAXPATHLEN, dirbuf, x);
  429: 
  430: 	len = clean_fname(buf, CFN_COLLAPSE_DOT_DOT_DIRS);
  431: 	if (len != 1 && len < MAXPATHLEN-1) {
  432: 		buf[len++] = '/';
  433: 		buf[len] = '\0';
  434: 	}
  435: 	/* This ensures that the specified dir is a parent of the transfer. */
  436: 	for (x = buf, y = dirbuf; *x && *x == *y; x++, y++) {}
  437: 	if (*x)
  438: 		y += strlen(y); /* nope -- skip the scan */
  439: 
  440: 	parent_dirscan = True;
  441: 	while (*y) {
  442: 		char save[MAXPATHLEN];
  443: 		strlcpy(save, y, MAXPATHLEN);
  444: 		*y = '\0';
  445: 		dirbuf_len = y - dirbuf;
  446: 		strlcpy(x, ex->pattern, MAXPATHLEN - (x - buf));
  447: 		parse_filter_file(lp, buf, ex, XFLG_ANCHORED2ABS);
  448: 		if (ex->rflags & FILTRULE_NO_INHERIT) {
  449: 			/* Free the undesired rules to clean up any per-dir
  450: 			 * mergelists they defined.  Otherwise pop_local_filters
  451: 			 * may crash trying to restore nonexistent state for
  452: 			 * those mergelists. */
  453: 			free_filters(lp->head);
  454: 			lp->head = NULL;
  455: 		}
  456: 		lp->tail = NULL;
  457: 		strlcpy(y, save, MAXPATHLEN);
  458: 		while ((*x++ = *y++) != '/') {}
  459: 	}
  460: 	/* Save current head for freeing when the mergelist becomes inactive. */
  461: 	lp->parent_dirscan_head = lp->head;
  462: 	parent_dirscan = False;
  463: 	if (DEBUG_GTE(FILTER, 2)) {
  464: 		rprintf(FINFO, "[%s] completed parent_dirscan for mergelist #%d%s\n",
  465: 			who_am_i(), mergelist_num, lp->debug_type);
  466: 	}
  467: 	free(pat);
  468: 	return 1;
  469: }
  470: 
  471: struct local_filter_state {
  472: 	int mergelist_cnt;
  473: 	filter_rule_list mergelists[1];
  474: };
  475: 
  476: /* Each time rsync changes to a new directory it call this function to
  477:  * handle all the per-dir merge-files.  The "dir" value is the current path
  478:  * relative to curr_dir (which might not be null-terminated).  We copy it
  479:  * into dirbuf so that we can easily append a file name on the end. */
  480: void *push_local_filters(const char *dir, unsigned int dirlen)
  481: {
  482: 	struct local_filter_state *push;
  483: 	int i;
  484: 
  485: 	set_filter_dir(dir, dirlen);
  486: 	if (DEBUG_GTE(FILTER, 2)) {
  487: 		rprintf(FINFO, "[%s] pushing local filters for %s\n",
  488: 			who_am_i(), dirbuf);
  489: 	}
  490: 
  491: 	if (!mergelist_cnt) {
  492: 		/* No old state to save and no new merge files to push. */
  493: 		return NULL;
  494: 	}
  495: 
  496: 	push = (struct local_filter_state *)new_array(char,
  497: 			  sizeof (struct local_filter_state)
  498: 			+ (mergelist_cnt-1) * sizeof (filter_rule_list));
  499: 	if (!push)
  500: 		out_of_memory("push_local_filters");
  501: 
  502: 	push->mergelist_cnt = mergelist_cnt;
  503: 	for (i = 0; i < mergelist_cnt; i++) {
  504: 		memcpy(&push->mergelists[i], mergelist_parents[i]->u.mergelist,
  505: 		       sizeof (filter_rule_list));
  506: 	}
  507: 
  508: 	/* Note: parse_filter_file() might increase mergelist_cnt, so keep
  509: 	 * this loop separate from the above loop. */
  510: 	for (i = 0; i < mergelist_cnt; i++) {
  511: 		filter_rule *ex = mergelist_parents[i];
  512: 		filter_rule_list *lp = ex->u.mergelist;
  513: 
  514: 		if (DEBUG_GTE(FILTER, 2)) {
  515: 			rprintf(FINFO, "[%s] pushing mergelist #%d%s\n",
  516: 				who_am_i(), i, lp->debug_type);
  517: 		}
  518: 
  519: 		lp->tail = NULL; /* Switch any local rules to inherited. */
  520: 		if (ex->rflags & FILTRULE_NO_INHERIT)
  521: 			lp->head = NULL;
  522: 
  523: 		if (ex->rflags & FILTRULE_FINISH_SETUP) {
  524: 			ex->rflags &= ~FILTRULE_FINISH_SETUP;
  525: 			if (setup_merge_file(i, ex, lp))
  526: 				set_filter_dir(dir, dirlen);
  527: 		}
  528: 
  529: 		if (strlcpy(dirbuf + dirbuf_len, ex->pattern,
  530: 		    MAXPATHLEN - dirbuf_len) < MAXPATHLEN - dirbuf_len) {
  531: 			parse_filter_file(lp, dirbuf, ex,
  532: 					  XFLG_ANCHORED2ABS);
  533: 		} else {
  534: 			io_error |= IOERR_GENERAL;
  535: 			rprintf(FERROR,
  536: 			    "cannot add local filter rules in long-named directory: %s\n",
  537: 			    full_fname(dirbuf));
  538: 		}
  539: 		dirbuf[dirbuf_len] = '\0';
  540: 	}
  541: 
  542: 	return (void*)push;
  543: }
  544: 
  545: void pop_local_filters(void *mem)
  546: {
  547: 	struct local_filter_state *pop = (struct local_filter_state *)mem;
  548: 	int i;
  549: 	int old_mergelist_cnt = pop ? pop->mergelist_cnt : 0;
  550: 
  551: 	if (DEBUG_GTE(FILTER, 2))
  552: 		rprintf(FINFO, "[%s] popping local filters\n", who_am_i());
  553: 
  554: 	for (i = mergelist_cnt; i-- > 0; ) {
  555: 		filter_rule *ex = mergelist_parents[i];
  556: 		filter_rule_list *lp = ex->u.mergelist;
  557: 
  558: 		if (DEBUG_GTE(FILTER, 2)) {
  559: 			rprintf(FINFO, "[%s] popping mergelist #%d%s\n",
  560: 				who_am_i(), i, lp->debug_type);
  561: 		}
  562: 
  563: 		clear_filter_list(lp);
  564: 
  565: 		if (i >= old_mergelist_cnt) {
  566: 			/* This mergelist does not exist in the state to be
  567: 			 * restored.  Free its parent_dirscan list to clean up
  568: 			 * any per-dir mergelists defined there so we don't
  569: 			 * crash trying to restore nonexistent state for them
  570: 			 * below.  (Counterpart to setup_merge_file call in
  571: 			 * push_local_filters.  Must be done here, not in
  572: 			 * free_filter, for LIFO order.) */
  573: 			if (DEBUG_GTE(FILTER, 2)) {
  574: 				rprintf(FINFO, "[%s] freeing parent_dirscan filters of mergelist #%d%s\n",
  575: 					who_am_i(), i, ex->u.mergelist->debug_type);
  576: 			}
  577: 			free_filters(lp->parent_dirscan_head);
  578: 			lp->parent_dirscan_head = NULL;
  579: 		}
  580: 	}
  581: 
  582: 	/* If we cleaned things up properly, the only still-active mergelists
  583: 	 * should be those with a state to be restored. */
  584: 	assert(mergelist_cnt == old_mergelist_cnt);
  585: 
  586: 	if (!pop) {
  587: 		/* No state to restore. */
  588: 		return;
  589: 	}
  590: 
  591: 	for (i = 0; i < mergelist_cnt; i++) {
  592: 		memcpy(mergelist_parents[i]->u.mergelist, &pop->mergelists[i],
  593: 		       sizeof (filter_rule_list));
  594: 	}
  595: 
  596: 	free(pop);
  597: }
  598: 
  599: void change_local_filter_dir(const char *dname, int dlen, int dir_depth)
  600: {
  601: 	static int cur_depth = -1;
  602: 	static void *filt_array[MAXPATHLEN/2+1];
  603: 
  604: 	if (!dname) {
  605: 		for ( ; cur_depth >= 0; cur_depth--) {
  606: 			if (filt_array[cur_depth]) {
  607: 				pop_local_filters(filt_array[cur_depth]);
  608: 				filt_array[cur_depth] = NULL;
  609: 			}
  610: 		}
  611: 		return;
  612: 	}
  613: 
  614: 	assert(dir_depth < MAXPATHLEN/2+1);
  615: 
  616: 	for ( ; cur_depth >= dir_depth; cur_depth--) {
  617: 		if (filt_array[cur_depth]) {
  618: 			pop_local_filters(filt_array[cur_depth]);
  619: 			filt_array[cur_depth] = NULL;
  620: 		}
  621: 	}
  622: 
  623: 	cur_depth = dir_depth;
  624: 	filt_array[cur_depth] = push_local_filters(dname, dlen);
  625: }
  626: 
  627: static int rule_matches(const char *fname, filter_rule *ex, int name_is_dir)
  628: {
  629: 	int slash_handling, str_cnt = 0, anchored_match = 0;
  630: 	int ret_match = ex->rflags & FILTRULE_NEGATE ? 0 : 1;
  631: 	char *p, *pattern = ex->pattern;
  632: 	const char *strings[16]; /* more than enough */
  633: 	const char *name = fname + (*fname == '/');
  634: 
  635: 	if (!*name)
  636: 		return 0;
  637: 
  638: 	if (!ex->u.slash_cnt && !(ex->rflags & FILTRULE_WILD2)) {
  639: 		/* If the pattern does not have any slashes AND it does
  640: 		 * not have a "**" (which could match a slash), then we
  641: 		 * just match the name portion of the path. */
  642: 		if ((p = strrchr(name,'/')) != NULL)
  643: 			name = p+1;
  644: 	} else if (ex->rflags & FILTRULE_ABS_PATH && *fname != '/'
  645: 	    && curr_dir_len > module_dirlen + 1) {
  646: 		/* If we're matching against an absolute-path pattern,
  647: 		 * we need to prepend our full path info. */
  648: 		strings[str_cnt++] = curr_dir + module_dirlen + 1;
  649: 		strings[str_cnt++] = "/";
  650: 	} else if (ex->rflags & FILTRULE_WILD2_PREFIX && *fname != '/') {
  651: 		/* Allow "**"+"/" to match at the start of the string. */
  652: 		strings[str_cnt++] = "/";
  653: 	}
  654: 	strings[str_cnt++] = name;
  655: 	if (name_is_dir) {
  656: 		/* Allow a trailing "/"+"***" to match the directory. */
  657: 		if (ex->rflags & FILTRULE_WILD3_SUFFIX)
  658: 			strings[str_cnt++] = "/";
  659: 	} else if (ex->rflags & FILTRULE_DIRECTORY)
  660: 		return !ret_match;
  661: 	strings[str_cnt] = NULL;
  662: 
  663: 	if (*pattern == '/') {
  664: 		anchored_match = 1;
  665: 		pattern++;
  666: 	}
  667: 
  668: 	if (!anchored_match && ex->u.slash_cnt
  669: 	    && !(ex->rflags & FILTRULE_WILD2)) {
  670: 		/* A non-anchored match with an infix slash and no "**"
  671: 		 * needs to match the last slash_cnt+1 name elements. */
  672: 		slash_handling = ex->u.slash_cnt + 1;
  673: 	} else if (!anchored_match && !(ex->rflags & FILTRULE_WILD2_PREFIX)
  674: 				   && ex->rflags & FILTRULE_WILD2) {
  675: 		/* A non-anchored match with an infix or trailing "**" (but not
  676: 		 * a prefixed "**") needs to try matching after every slash. */
  677: 		slash_handling = -1;
  678: 	} else {
  679: 		/* The pattern matches only at the start of the path or name. */
  680: 		slash_handling = 0;
  681: 	}
  682: 
  683: 	if (ex->rflags & FILTRULE_WILD) {
  684: 		if (wildmatch_array(pattern, strings, slash_handling))
  685: 			return ret_match;
  686: 	} else if (str_cnt > 1) {
  687: 		if (litmatch_array(pattern, strings, slash_handling))
  688: 			return ret_match;
  689: 	} else if (anchored_match) {
  690: 		if (strcmp(name, pattern) == 0)
  691: 			return ret_match;
  692: 	} else {
  693: 		int l1 = strlen(name);
  694: 		int l2 = strlen(pattern);
  695: 		if (l2 <= l1 &&
  696: 		    strcmp(name+(l1-l2),pattern) == 0 &&
  697: 		    (l1==l2 || name[l1-(l2+1)] == '/')) {
  698: 			return ret_match;
  699: 		}
  700: 	}
  701: 
  702: 	return !ret_match;
  703: }
  704: 
  705: static void report_filter_result(enum logcode code, char const *name,
  706: 				 filter_rule const *ent,
  707: 				 int name_is_dir, const char *type)
  708: {
  709: 	/* If a trailing slash is present to match only directories,
  710: 	 * then it is stripped out by add_rule().  So as a special
  711: 	 * case we add it back in here. */
  712: 
  713: 	if (DEBUG_GTE(FILTER, 1)) {
  714: 		static char *actions[2][2]
  715: 		    = { {"show", "hid"}, {"risk", "protect"} };
  716: 		const char *w = who_am_i();
  717: 		rprintf(code, "[%s] %sing %s %s because of pattern %s%s%s\n",
  718: 		    w, actions[*w!='s'][!(ent->rflags & FILTRULE_INCLUDE)],
  719: 		    name_is_dir ? "directory" : "file", name, ent->pattern,
  720: 		    ent->rflags & FILTRULE_DIRECTORY ? "/" : "", type);
  721: 	}
  722: }
  723: 
  724: /* Return -1 if file "name" is defined to be excluded by the specified
  725:  * exclude list, 1 if it is included, and 0 if it was not matched. */
  726: int check_filter(filter_rule_list *listp, enum logcode code,
  727: 		 const char *name, int name_is_dir)
  728: {
  729: 	filter_rule *ent;
  730: 
  731: 	for (ent = listp->head; ent; ent = ent->next) {
  732: 		if (ignore_perishable && ent->rflags & FILTRULE_PERISHABLE)
  733: 			continue;
  734: 		if (ent->rflags & FILTRULE_PERDIR_MERGE) {
  735: 			int rc = check_filter(ent->u.mergelist, code, name,
  736: 					      name_is_dir);
  737: 			if (rc)
  738: 				return rc;
  739: 			continue;
  740: 		}
  741: 		if (ent->rflags & FILTRULE_CVS_IGNORE) {
  742: 			int rc = check_filter(&cvs_filter_list, code, name,
  743: 					      name_is_dir);
  744: 			if (rc)
  745: 				return rc;
  746: 			continue;
  747: 		}
  748: 		if (rule_matches(name, ent, name_is_dir)) {
  749: 			report_filter_result(code, name, ent, name_is_dir,
  750: 					     listp->debug_type);
  751: 			return ent->rflags & FILTRULE_INCLUDE ? 1 : -1;
  752: 		}
  753: 	}
  754: 
  755: 	return 0;
  756: }
  757: 
  758: #define RULE_STRCMP(s,r) rule_strcmp((s), (r), sizeof (r) - 1)
  759: 
  760: static const uchar *rule_strcmp(const uchar *str, const char *rule, int rule_len)
  761: {
  762: 	if (strncmp((char*)str, rule, rule_len) != 0)
  763: 		return NULL;
  764: 	if (isspace(str[rule_len]) || str[rule_len] == '_' || !str[rule_len])
  765: 		return str + rule_len - 1;
  766: 	if (str[rule_len] == ',')
  767: 		return str + rule_len;
  768: 	return NULL;
  769: }
  770: 
  771: #define FILTRULES_FROM_CONTAINER (FILTRULE_ABS_PATH | FILTRULE_INCLUDE \
  772: 				| FILTRULE_DIRECTORY | FILTRULE_NEGATE \
  773: 				| FILTRULE_PERISHABLE)
  774: 
  775: /* Gets the next include/exclude rule from *rulestr_ptr and advances
  776:  * *rulestr_ptr to point beyond it.  Stores the pattern's start (within
  777:  * *rulestr_ptr) and length in *pat_ptr and *pat_len_ptr, and returns a newly
  778:  * allocated filter_rule containing the rest of the information.  Returns
  779:  * NULL if there are no more rules in the input.
  780:  *
  781:  * The template provides defaults for the new rule to inherit, and the
  782:  * template rflags and the xflags additionally affect parsing. */
  783: static filter_rule *parse_rule_tok(const char **rulestr_ptr,
  784: 				   const filter_rule *template, int xflags,
  785: 				   const char **pat_ptr, unsigned int *pat_len_ptr)
  786: {
  787: 	const uchar *s = (const uchar *)*rulestr_ptr;
  788: 	filter_rule *rule;
  789: 	unsigned int len;
  790: 
  791: 	if (template->rflags & FILTRULE_WORD_SPLIT) {
  792: 		/* Skip over any initial whitespace. */
  793: 		while (isspace(*s))
  794: 			s++;
  795: 		/* Update to point to real start of rule. */
  796: 		*rulestr_ptr = (const char *)s;
  797: 	}
  798: 	if (!*s)
  799: 		return NULL;
  800: 
  801: 	if (!(rule = new0(filter_rule)))
  802: 		out_of_memory("parse_rule_tok");
  803: 
  804: 	/* Inherit from the template.  Don't inherit FILTRULES_SIDES; we check
  805: 	 * that later. */
  806: 	rule->rflags = template->rflags & FILTRULES_FROM_CONTAINER;
  807: 
  808: 	/* Figure out what kind of a filter rule "s" is pointing at.  Note
  809: 	 * that if FILTRULE_NO_PREFIXES is set, the rule is either an include
  810: 	 * or an exclude based on the inheritance of the FILTRULE_INCLUDE
  811: 	 * flag (above).  XFLG_OLD_PREFIXES indicates a compatibility mode
  812: 	 * for old include/exclude patterns where just "+ " and "- " are
  813: 	 * allowed as optional prefixes.  */
  814: 	if (template->rflags & FILTRULE_NO_PREFIXES) {
  815: 		if (*s == '!' && template->rflags & FILTRULE_CVS_IGNORE)
  816: 			rule->rflags |= FILTRULE_CLEAR_LIST; /* Tentative! */
  817: 	} else if (xflags & XFLG_OLD_PREFIXES) {
  818: 		if (*s == '-' && s[1] == ' ') {
  819: 			rule->rflags &= ~FILTRULE_INCLUDE;
  820: 			s += 2;
  821: 		} else if (*s == '+' && s[1] == ' ') {
  822: 			rule->rflags |= FILTRULE_INCLUDE;
  823: 			s += 2;
  824: 		} else if (*s == '!')
  825: 			rule->rflags |= FILTRULE_CLEAR_LIST; /* Tentative! */
  826: 	} else {
  827: 		char ch = 0;
  828: 		BOOL prefix_specifies_side = False;
  829: 		switch (*s) {
  830: 		case 'c':
  831: 			if ((s = RULE_STRCMP(s, "clear")) != NULL)
  832: 				ch = '!';
  833: 			break;
  834: 		case 'd':
  835: 			if ((s = RULE_STRCMP(s, "dir-merge")) != NULL)
  836: 				ch = ':';
  837: 			break;
  838: 		case 'e':
  839: 			if ((s = RULE_STRCMP(s, "exclude")) != NULL)
  840: 				ch = '-';
  841: 			break;
  842: 		case 'h':
  843: 			if ((s = RULE_STRCMP(s, "hide")) != NULL)
  844: 				ch = 'H';
  845: 			break;
  846: 		case 'i':
  847: 			if ((s = RULE_STRCMP(s, "include")) != NULL)
  848: 				ch = '+';
  849: 			break;
  850: 		case 'm':
  851: 			if ((s = RULE_STRCMP(s, "merge")) != NULL)
  852: 				ch = '.';
  853: 			break;
  854: 		case 'p':
  855: 			if ((s = RULE_STRCMP(s, "protect")) != NULL)
  856: 				ch = 'P';
  857: 			break;
  858: 		case 'r':
  859: 			if ((s = RULE_STRCMP(s, "risk")) != NULL)
  860: 				ch = 'R';
  861: 			break;
  862: 		case 's':
  863: 			if ((s = RULE_STRCMP(s, "show")) != NULL)
  864: 				ch = 'S';
  865: 			break;
  866: 		default:
  867: 			ch = *s;
  868: 			if (s[1] == ',')
  869: 				s++;
  870: 			break;
  871: 		}
  872: 		switch (ch) {
  873: 		case ':':
  874: 			rule->rflags |= FILTRULE_PERDIR_MERGE
  875: 				       | FILTRULE_FINISH_SETUP;
  876: 			/* FALL THROUGH */
  877: 		case '.':
  878: 			rule->rflags |= FILTRULE_MERGE_FILE;
  879: 			break;
  880: 		case '+':
  881: 			rule->rflags |= FILTRULE_INCLUDE;
  882: 			break;
  883: 		case '-':
  884: 			break;
  885: 		case 'S':
  886: 			rule->rflags |= FILTRULE_INCLUDE;
  887: 			/* FALL THROUGH */
  888: 		case 'H':
  889: 			rule->rflags |= FILTRULE_SENDER_SIDE;
  890: 			prefix_specifies_side = True;
  891: 			break;
  892: 		case 'R':
  893: 			rule->rflags |= FILTRULE_INCLUDE;
  894: 			/* FALL THROUGH */
  895: 		case 'P':
  896: 			rule->rflags |= FILTRULE_RECEIVER_SIDE;
  897: 			prefix_specifies_side = True;
  898: 			break;
  899: 		case '!':
  900: 			rule->rflags |= FILTRULE_CLEAR_LIST;
  901: 			break;
  902: 		default:
  903: 			rprintf(FERROR, "Unknown filter rule: `%s'\n", *rulestr_ptr);
  904: 			exit_cleanup(RERR_SYNTAX);
  905: 		}
  906: 		while (ch != '!' && *++s && *s != ' ' && *s != '_') {
  907: 			if (template->rflags & FILTRULE_WORD_SPLIT && isspace(*s)) {
  908: 				s--;
  909: 				break;
  910: 			}
  911: 			switch (*s) {
  912: 			default:
  913: 			    invalid:
  914: 				rprintf(FERROR,
  915: 					"invalid modifier '%c' at position %d in filter rule: %s\n",
  916: 					*s, (int)(s - (const uchar *)*rulestr_ptr), *rulestr_ptr);
  917: 				exit_cleanup(RERR_SYNTAX);
  918: 			case '-':
  919: 				if (!BITS_SETnUNSET(rule->rflags, FILTRULE_MERGE_FILE, FILTRULE_NO_PREFIXES))
  920: 					goto invalid;
  921: 				rule->rflags |= FILTRULE_NO_PREFIXES;
  922: 				break;
  923: 			case '+':
  924: 				if (!BITS_SETnUNSET(rule->rflags, FILTRULE_MERGE_FILE, FILTRULE_NO_PREFIXES))
  925: 					goto invalid;
  926: 				rule->rflags |= FILTRULE_NO_PREFIXES
  927: 					      | FILTRULE_INCLUDE;
  928: 				break;
  929: 			case '/':
  930: 				rule->rflags |= FILTRULE_ABS_PATH;
  931: 				break;
  932: 			case '!':
  933: 				/* Negation really goes with the pattern, so it
  934: 				 * isn't useful as a merge-file default. */
  935: 				if (rule->rflags & FILTRULE_MERGE_FILE)
  936: 					goto invalid;
  937: 				rule->rflags |= FILTRULE_NEGATE;
  938: 				break;
  939: 			case 'C':
  940: 				if (rule->rflags & FILTRULE_NO_PREFIXES || prefix_specifies_side)
  941: 					goto invalid;
  942: 				rule->rflags |= FILTRULE_NO_PREFIXES
  943: 					      | FILTRULE_WORD_SPLIT
  944: 					      | FILTRULE_NO_INHERIT
  945: 					      | FILTRULE_CVS_IGNORE;
  946: 				break;
  947: 			case 'e':
  948: 				if (!(rule->rflags & FILTRULE_MERGE_FILE))
  949: 					goto invalid;
  950: 				rule->rflags |= FILTRULE_EXCLUDE_SELF;
  951: 				break;
  952: 			case 'n':
  953: 				if (!(rule->rflags & FILTRULE_MERGE_FILE))
  954: 					goto invalid;
  955: 				rule->rflags |= FILTRULE_NO_INHERIT;
  956: 				break;
  957: 			case 'p':
  958: 				rule->rflags |= FILTRULE_PERISHABLE;
  959: 				break;
  960: 			case 'r':
  961: 				if (prefix_specifies_side)
  962: 					goto invalid;
  963: 				rule->rflags |= FILTRULE_RECEIVER_SIDE;
  964: 				break;
  965: 			case 's':
  966: 				if (prefix_specifies_side)
  967: 					goto invalid;
  968: 				rule->rflags |= FILTRULE_SENDER_SIDE;
  969: 				break;
  970: 			case 'w':
  971: 				if (!(rule->rflags & FILTRULE_MERGE_FILE))
  972: 					goto invalid;
  973: 				rule->rflags |= FILTRULE_WORD_SPLIT;
  974: 				break;
  975: 			}
  976: 		}
  977: 		if (*s)
  978: 			s++;
  979: 	}
  980: 	if (template->rflags & FILTRULES_SIDES) {
  981: 		if (rule->rflags & FILTRULES_SIDES) {
  982: 			/* The filter and template both specify side(s).  This
  983: 			 * is dodgy (and won't work correctly if the template is
  984: 			 * a one-sided per-dir merge rule), so reject it. */
  985: 			rprintf(FERROR,
  986: 				"specified-side merge file contains specified-side filter: %s\n",
  987: 				*rulestr_ptr);
  988: 			exit_cleanup(RERR_SYNTAX);
  989: 		}
  990: 		rule->rflags |= template->rflags & FILTRULES_SIDES;
  991: 	}
  992: 
  993: 	if (template->rflags & FILTRULE_WORD_SPLIT) {
  994: 		const uchar *cp = s;
  995: 		/* Token ends at whitespace or the end of the string. */
  996: 		while (!isspace(*cp) && *cp != '\0')
  997: 			cp++;
  998: 		len = cp - s;
  999: 	} else
 1000: 		len = strlen((char*)s);
 1001: 
 1002: 	if (rule->rflags & FILTRULE_CLEAR_LIST) {
 1003: 		if (!(rule->rflags & FILTRULE_NO_PREFIXES)
 1004: 		 && !(xflags & XFLG_OLD_PREFIXES) && len) {
 1005: 			rprintf(FERROR,
 1006: 				"'!' rule has trailing characters: %s\n", *rulestr_ptr);
 1007: 			exit_cleanup(RERR_SYNTAX);
 1008: 		}
 1009: 		if (len > 1)
 1010: 			rule->rflags &= ~FILTRULE_CLEAR_LIST;
 1011: 	} else if (!len && !(rule->rflags & FILTRULE_CVS_IGNORE)) {
 1012: 		rprintf(FERROR, "unexpected end of filter rule: %s\n", *rulestr_ptr);
 1013: 		exit_cleanup(RERR_SYNTAX);
 1014: 	}
 1015: 
 1016: 	/* --delete-excluded turns an un-modified include/exclude into a sender-side rule.  */
 1017: 	if (delete_excluded
 1018: 	 && !(rule->rflags & (FILTRULES_SIDES|FILTRULE_MERGE_FILE|FILTRULE_PERDIR_MERGE)))
 1019: 		rule->rflags |= FILTRULE_SENDER_SIDE;
 1020: 
 1021: 	*pat_ptr = (const char *)s;
 1022: 	*pat_len_ptr = len;
 1023: 	*rulestr_ptr = *pat_ptr + len;
 1024: 	return rule;
 1025: }
 1026: 
 1027: static char default_cvsignore[] =
 1028: 	/* These default ignored items come from the CVS manual. */
 1029: 	"RCS SCCS CVS CVS.adm RCSLOG cvslog.* tags TAGS"
 1030: 	" .make.state .nse_depinfo *~ #* .#* ,* _$* *$"
 1031: 	" *.old *.bak *.BAK *.orig *.rej .del-*"
 1032: 	" *.a *.olb *.o *.obj *.so *.exe"
 1033: 	" *.Z *.elc *.ln core"
 1034: 	/* The rest we added to suit ourself. */
 1035: 	" .svn/ .git/ .hg/ .bzr/";
 1036: 
 1037: static void get_cvs_excludes(uint32 rflags)
 1038: {
 1039: 	static int initialized = 0;
 1040: 	char *p, fname[MAXPATHLEN];
 1041: 
 1042: 	if (initialized)
 1043: 		return;
 1044: 	initialized = 1;
 1045: 
 1046: 	parse_filter_str(&cvs_filter_list, default_cvsignore,
 1047: 			 rule_template(rflags | (protocol_version >= 30 ? FILTRULE_PERISHABLE : 0)),
 1048: 			 0);
 1049: 
 1050: 	p = module_id >= 0 && lp_use_chroot(module_id) ? "/" : getenv("HOME");
 1051: 	if (p && pathjoin(fname, MAXPATHLEN, p, ".cvsignore") < MAXPATHLEN)
 1052: 		parse_filter_file(&cvs_filter_list, fname, rule_template(rflags), 0);
 1053: 
 1054: 	parse_filter_str(&cvs_filter_list, getenv("CVSIGNORE"), rule_template(rflags), 0);
 1055: }
 1056: 
 1057: const filter_rule *rule_template(uint32 rflags)
 1058: {
 1059: 	static filter_rule template; /* zero-initialized */
 1060: 	template.rflags = rflags;
 1061: 	return &template;
 1062: }
 1063: 
 1064: void parse_filter_str(filter_rule_list *listp, const char *rulestr,
 1065: 		     const filter_rule *template, int xflags)
 1066: {
 1067: 	filter_rule *rule;
 1068: 	const char *pat;
 1069: 	unsigned int pat_len;
 1070: 
 1071: 	if (!rulestr)
 1072: 		return;
 1073: 
 1074: 	while (1) {
 1075: 		uint32 new_rflags;
 1076: 
 1077: 		/* Remember that the returned string is NOT '\0' terminated! */
 1078: 		if (!(rule = parse_rule_tok(&rulestr, template, xflags, &pat, &pat_len)))
 1079: 			break;
 1080: 
 1081: 		if (pat_len >= MAXPATHLEN) {
 1082: 			rprintf(FERROR, "discarding over-long filter: %.*s\n",
 1083: 				(int)pat_len, pat);
 1084: 		    free_continue:
 1085: 			free_filter(rule);
 1086: 			continue;
 1087: 		}
 1088: 
 1089: 		new_rflags = rule->rflags;
 1090: 		if (new_rflags & FILTRULE_CLEAR_LIST) {
 1091: 			if (DEBUG_GTE(FILTER, 2)) {
 1092: 				rprintf(FINFO,
 1093: 					"[%s] clearing filter list%s\n",
 1094: 					who_am_i(), listp->debug_type);
 1095: 			}
 1096: 			clear_filter_list(listp);
 1097: 			goto free_continue;
 1098: 		}
 1099: 
 1100: 		if (new_rflags & FILTRULE_MERGE_FILE) {
 1101: 			if (!pat_len) {
 1102: 				pat = ".cvsignore";
 1103: 				pat_len = 10;
 1104: 			}
 1105: 			if (new_rflags & FILTRULE_EXCLUDE_SELF) {
 1106: 				const char *name;
 1107: 				filter_rule *excl_self;
 1108: 
 1109: 				if (!(excl_self = new0(filter_rule)))
 1110: 					out_of_memory("parse_filter_str");
 1111: 				/* Find the beginning of the basename and add an exclude for it. */
 1112: 				for (name = pat + pat_len; name > pat && name[-1] != '/'; name--) {}
 1113: 				add_rule(listp, name, (pat + pat_len) - name, excl_self, 0);
 1114: 				rule->rflags &= ~FILTRULE_EXCLUDE_SELF;
 1115: 			}
 1116: 			if (new_rflags & FILTRULE_PERDIR_MERGE) {
 1117: 				if (parent_dirscan) {
 1118: 					const char *p;
 1119: 					unsigned int len = pat_len;
 1120: 					if ((p = parse_merge_name(pat, &len, module_dirlen)))
 1121: 						add_rule(listp, p, len, rule, 0);
 1122: 					else
 1123: 						free_filter(rule);
 1124: 					continue;
 1125: 				}
 1126: 			} else {
 1127: 				const char *p;
 1128: 				unsigned int len = pat_len;
 1129: 				if ((p = parse_merge_name(pat, &len, 0)))
 1130: 					parse_filter_file(listp, p, rule, XFLG_FATAL_ERRORS);
 1131: 				free_filter(rule);
 1132: 				continue;
 1133: 			}
 1134: 		}
 1135: 
 1136: 		add_rule(listp, pat, pat_len, rule, xflags);
 1137: 
 1138: 		if (new_rflags & FILTRULE_CVS_IGNORE
 1139: 		    && !(new_rflags & FILTRULE_MERGE_FILE))
 1140: 			get_cvs_excludes(new_rflags);
 1141: 	}
 1142: }
 1143: 
 1144: void parse_filter_file(filter_rule_list *listp, const char *fname, const filter_rule *template, int xflags)
 1145: {
 1146: 	FILE *fp;
 1147: 	char line[BIGPATHBUFLEN];
 1148: 	char *eob = line + sizeof line - 1;
 1149: 	BOOL word_split = (template->rflags & FILTRULE_WORD_SPLIT) != 0;
 1150: 
 1151: 	if (!fname || !*fname)
 1152: 		return;
 1153: 
 1154: 	if (*fname != '-' || fname[1] || am_server) {
 1155: 		if (daemon_filter_list.head) {
 1156: 			strlcpy(line, fname, sizeof line);
 1157: 			clean_fname(line, CFN_COLLAPSE_DOT_DOT_DIRS);
 1158: 			if (check_filter(&daemon_filter_list, FLOG, line, 0) < 0)
 1159: 				fp = NULL;
 1160: 			else
 1161: 				fp = fopen(line, "rb");
 1162: 		} else
 1163: 			fp = fopen(fname, "rb");
 1164: 	} else
 1165: 		fp = stdin;
 1166: 
 1167: 	if (DEBUG_GTE(FILTER, 2)) {
 1168: 		rprintf(FINFO, "[%s] parse_filter_file(%s,%x,%x)%s\n",
 1169: 			who_am_i(), fname, template->rflags, xflags,
 1170: 			fp ? "" : " [not found]");
 1171: 	}
 1172: 
 1173: 	if (!fp) {
 1174: 		if (xflags & XFLG_FATAL_ERRORS) {
 1175: 			rsyserr(FERROR, errno,
 1176: 				"failed to open %sclude file %s",
 1177: 				template->rflags & FILTRULE_INCLUDE ? "in" : "ex",
 1178: 				fname);
 1179: 			exit_cleanup(RERR_FILEIO);
 1180: 		}
 1181: 		return;
 1182: 	}
 1183: 	dirbuf[dirbuf_len] = '\0';
 1184: 
 1185: 	while (1) {
 1186: 		char *s = line;
 1187: 		int ch, overflow = 0;
 1188: 		while (1) {
 1189: 			if ((ch = getc(fp)) == EOF) {
 1190: 				if (ferror(fp) && errno == EINTR) {
 1191: 					clearerr(fp);
 1192: 					continue;
 1193: 				}
 1194: 				break;
 1195: 			}
 1196: 			if (word_split && isspace(ch))
 1197: 				break;
 1198: 			if (eol_nulls? !ch : (ch == '\n' || ch == '\r'))
 1199: 				break;
 1200: 			if (s < eob)
 1201: 				*s++ = ch;
 1202: 			else
 1203: 				overflow = 1;
 1204: 		}
 1205: 		if (overflow) {
 1206: 			rprintf(FERROR, "discarding over-long filter: %s...\n", line);
 1207: 			s = line;
 1208: 		}
 1209: 		*s = '\0';
 1210: 		/* Skip an empty token and (when line parsing) comments. */
 1211: 		if (*line && (word_split || (*line != ';' && *line != '#')))
 1212: 			parse_filter_str(listp, line, template, xflags);
 1213: 		if (ch == EOF)
 1214: 			break;
 1215: 	}
 1216: 	fclose(fp);
 1217: }
 1218: 
 1219: /* If the "for_xfer" flag is set, the prefix is made compatible with the
 1220:  * current protocol_version (if possible) or a NULL is returned (if not
 1221:  * possible). */
 1222: char *get_rule_prefix(filter_rule *rule, const char *pat, int for_xfer,
 1223: 		      unsigned int *plen_ptr)
 1224: {
 1225: 	static char buf[MAX_RULE_PREFIX+1];
 1226: 	char *op = buf;
 1227: 	int legal_len = for_xfer && protocol_version < 29 ? 1 : MAX_RULE_PREFIX-1;
 1228: 
 1229: 	if (rule->rflags & FILTRULE_PERDIR_MERGE) {
 1230: 		if (legal_len == 1)
 1231: 			return NULL;
 1232: 		*op++ = ':';
 1233: 	} else if (rule->rflags & FILTRULE_INCLUDE)
 1234: 		*op++ = '+';
 1235: 	else if (legal_len != 1
 1236: 	    || ((*pat == '-' || *pat == '+') && pat[1] == ' '))
 1237: 		*op++ = '-';
 1238: 	else
 1239: 		legal_len = 0;
 1240: 
 1241: 	if (rule->rflags & FILTRULE_ABS_PATH)
 1242: 		*op++ = '/';
 1243: 	if (rule->rflags & FILTRULE_NEGATE)
 1244: 		*op++ = '!';
 1245: 	if (rule->rflags & FILTRULE_CVS_IGNORE)
 1246: 		*op++ = 'C';
 1247: 	else {
 1248: 		if (rule->rflags & FILTRULE_NO_INHERIT)
 1249: 			*op++ = 'n';
 1250: 		if (rule->rflags & FILTRULE_WORD_SPLIT)
 1251: 			*op++ = 'w';
 1252: 		if (rule->rflags & FILTRULE_NO_PREFIXES) {
 1253: 			if (rule->rflags & FILTRULE_INCLUDE)
 1254: 				*op++ = '+';
 1255: 			else
 1256: 				*op++ = '-';
 1257: 		}
 1258: 	}
 1259: 	if (rule->rflags & FILTRULE_EXCLUDE_SELF)
 1260: 		*op++ = 'e';
 1261: 	if (rule->rflags & FILTRULE_SENDER_SIDE
 1262: 	    && (!for_xfer || protocol_version >= 29))
 1263: 		*op++ = 's';
 1264: 	if (rule->rflags & FILTRULE_RECEIVER_SIDE
 1265: 	    && (!for_xfer || protocol_version >= 29
 1266: 	     || (delete_excluded && am_sender)))
 1267: 		*op++ = 'r';
 1268: 	if (rule->rflags & FILTRULE_PERISHABLE) {
 1269: 		if (!for_xfer || protocol_version >= 30)
 1270: 			*op++ = 'p';
 1271: 		else if (am_sender)
 1272: 			return NULL;
 1273: 	}
 1274: 	if (op - buf > legal_len)
 1275: 		return NULL;
 1276: 	if (legal_len)
 1277: 		*op++ = ' ';
 1278: 	*op = '\0';
 1279: 	if (plen_ptr)
 1280: 		*plen_ptr = op - buf;
 1281: 	return buf;
 1282: }
 1283: 
 1284: static void send_rules(int f_out, filter_rule_list *flp)
 1285: {
 1286: 	filter_rule *ent, *prev = NULL;
 1287: 
 1288: 	for (ent = flp->head; ent; ent = ent->next) {
 1289: 		unsigned int len, plen, dlen;
 1290: 		int elide = 0;
 1291: 		char *p;
 1292: 
 1293: 		/* Note we need to check delete_excluded here in addition to
 1294: 		 * the code in parse_rule_tok() because some rules may have
 1295: 		 * been added before we found the --delete-excluded option.
 1296: 		 * We must also elide any CVS merge-file rules to avoid a
 1297: 		 * backward compatibility problem, and we elide any no-prefix
 1298: 		 * merge files as an optimization (since they can only have
 1299: 		 * include/exclude rules). */
 1300: 		if (ent->rflags & FILTRULE_SENDER_SIDE)
 1301: 			elide = am_sender ? 1 : -1;
 1302: 		if (ent->rflags & FILTRULE_RECEIVER_SIDE)
 1303: 			elide = elide ? 0 : am_sender ? -1 : 1;
 1304: 		else if (delete_excluded && !elide
 1305: 		 && (!(ent->rflags & FILTRULE_PERDIR_MERGE)
 1306: 		  || ent->rflags & FILTRULE_NO_PREFIXES))
 1307: 			elide = am_sender ? 1 : -1;
 1308: 		if (elide < 0) {
 1309: 			if (prev)
 1310: 				prev->next = ent->next;
 1311: 			else
 1312: 				flp->head = ent->next;
 1313: 		} else
 1314: 			prev = ent;
 1315: 		if (elide > 0)
 1316: 			continue;
 1317: 		if (ent->rflags & FILTRULE_CVS_IGNORE
 1318: 		    && !(ent->rflags & FILTRULE_MERGE_FILE)) {
 1319: 			int f = am_sender || protocol_version < 29 ? f_out : -2;
 1320: 			send_rules(f, &cvs_filter_list);
 1321: 			if (f == f_out)
 1322: 				continue;
 1323: 		}
 1324: 		p = get_rule_prefix(ent, ent->pattern, 1, &plen);
 1325: 		if (!p) {
 1326: 			rprintf(FERROR,
 1327: 				"filter rules are too modern for remote rsync.\n");
 1328: 			exit_cleanup(RERR_PROTOCOL);
 1329: 		}
 1330: 		if (f_out < 0)
 1331: 			continue;
 1332: 		len = strlen(ent->pattern);
 1333: 		dlen = ent->rflags & FILTRULE_DIRECTORY ? 1 : 0;
 1334: 		if (!(plen + len + dlen))
 1335: 			continue;
 1336: 		write_int(f_out, plen + len + dlen);
 1337: 		if (plen)
 1338: 			write_buf(f_out, p, plen);
 1339: 		write_buf(f_out, ent->pattern, len);
 1340: 		if (dlen)
 1341: 			write_byte(f_out, '/');
 1342: 	}
 1343: 	flp->tail = prev;
 1344: }
 1345: 
 1346: /* This is only called by the client. */
 1347: void send_filter_list(int f_out)
 1348: {
 1349: 	int receiver_wants_list = prune_empty_dirs
 1350: 	    || (delete_mode && (!delete_excluded || protocol_version >= 29));
 1351: 
 1352: 	if (local_server || (am_sender && !receiver_wants_list))
 1353: 		f_out = -1;
 1354: 	if (cvs_exclude && am_sender) {
 1355: 		if (protocol_version >= 29)
 1356: 			parse_filter_str(&filter_list, ":C", rule_template(0), 0);
 1357: 		parse_filter_str(&filter_list, "-C", rule_template(0), 0);
 1358: 	}
 1359: 
 1360: 	send_rules(f_out, &filter_list);
 1361: 
 1362: 	if (f_out >= 0)
 1363: 		write_int(f_out, 0);
 1364: 
 1365: 	if (cvs_exclude) {
 1366: 		if (!am_sender || protocol_version < 29)
 1367: 			parse_filter_str(&filter_list, ":C", rule_template(0), 0);
 1368: 		if (!am_sender)
 1369: 			parse_filter_str(&filter_list, "-C", rule_template(0), 0);
 1370: 	}
 1371: }
 1372: 
 1373: /* This is only called by the server. */
 1374: void recv_filter_list(int f_in)
 1375: {
 1376: 	char line[BIGPATHBUFLEN];
 1377: 	int xflags = protocol_version >= 29 ? 0 : XFLG_OLD_PREFIXES;
 1378: 	int receiver_wants_list = prune_empty_dirs
 1379: 	    || (delete_mode
 1380: 	     && (!delete_excluded || protocol_version >= 29));
 1381: 	unsigned int len;
 1382: 
 1383: 	if (!local_server && (am_sender || receiver_wants_list)) {
 1384: 		while ((len = read_int(f_in)) != 0) {
 1385: 			if (len >= sizeof line)
 1386: 				overflow_exit("recv_rules");
 1387: 			read_sbuf(f_in, line, len);
 1388: 			parse_filter_str(&filter_list, line, rule_template(0), xflags);
 1389: 		}
 1390: 	}
 1391: 
 1392: 	if (cvs_exclude) {
 1393: 		if (local_server || am_sender || protocol_version < 29)
 1394: 			parse_filter_str(&filter_list, ":C", rule_template(0), 0);
 1395: 		if (local_server || am_sender)
 1396: 			parse_filter_str(&filter_list, "-C", rule_template(0), 0);
 1397: 	}
 1398: 
 1399: 	if (local_server) /* filter out any rules that aren't for us. */
 1400: 		send_rules(-1, &filter_list);
 1401: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>