File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / rsync / exclude.c
Revision 1.1.1.3 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Tue Nov 1 09:54:32 2016 UTC (7 years, 7 months ago) by misho
Branches: rsync, MAIN
CVS tags: v3_1_2p5, HEAD
rsync 3.1.2

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

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