+
+ lp->tail = NULL; /* Switch any local rules to inherited. */
+ if (ex->match_flags & MATCHFLG_NO_INHERIT)
+ lp->head = NULL;
+
+ if (ex->match_flags & MATCHFLG_FINISH_SETUP) {
+ ex->match_flags &= ~MATCHFLG_FINISH_SETUP;
+ if (setup_merge_file(ex, lp))
+ set_filter_dir(dir, dirlen);
+ }
+
+ if (strlcpy(dirbuf + dirbuf_len, ex->pattern,
+ MAXPATHLEN - dirbuf_len) < MAXPATHLEN - dirbuf_len) {
+ parse_filter_file(lp, dirbuf, ex->match_flags,
+ XFLG_ANCHORED2ABS);
+ } else {
+ io_error |= IOERR_GENERAL;
+ rprintf(FINFO,
+ "cannot add local filter rules in long-named directory: %s\n",
+ full_fname(dirbuf));
+ }
+ dirbuf[dirbuf_len] = '\0';
+ }
+
+ return (void*)push;
+}
+
+void pop_local_filters(void *mem)
+{
+ struct filter_list_struct *ap, *pop = (struct filter_list_struct*)mem;
+ int i;
+
+ for (i = mergelist_cnt; i-- > 0; ) {
+ struct filter_struct *ex = mergelist_parents[i];
+ struct filter_list_struct *lp = ex->u.mergelist;
+
+ if (verbose > 2) {
+ rprintf(FINFO, "[%s] popping filter list%s\n",
+ who_am_i(), lp->debug_type);
+ }
+
+ clear_filter_list(lp);
+ }
+
+ if (!pop)
+ return;
+
+ for (i = 0, ap = pop; i < mergelist_cnt; i++) {
+ memcpy(mergelist_parents[i]->u.mergelist, ap++,
+ sizeof (struct filter_list_struct));
+ }
+
+ free(pop);
+}
+
+static int rule_matches(char *name, struct filter_struct *ex, int name_is_dir)
+{
+ int slash_handling, str_cnt = 0, anchored_match = 0;
+ int ret_match = ex->match_flags & MATCHFLG_NEGATE ? 0 : 1;
+ char *p, *pattern = ex->pattern;
+ const char *strings[16]; /* more than enough */
+
+ if (!*name)
+ return 0;
+
+ if (!ex->u.slash_cnt && !(ex->match_flags & MATCHFLG_WILD2)) {
+ /* If the pattern does not have any slashes AND it does
+ * not have a "**" (which could match a slash), then we
+ * just match the name portion of the path. */
+ if ((p = strrchr(name,'/')) != NULL)
+ name = p+1;
+ } else if (ex->match_flags & MATCHFLG_ABS_PATH && *name != '/'
+ && curr_dir_len > module_dirlen + 1) {
+ /* If we're matching against an absolute-path pattern,
+ * we need to prepend our full path info. */
+ strings[str_cnt++] = curr_dir + module_dirlen + 1;
+ strings[str_cnt++] = "/";
+ } else if (ex->match_flags & MATCHFLG_WILD2_PREFIX && *name != '/') {
+ /* Allow "**"+"/" to match at the start of the string. */
+ strings[str_cnt++] = "/";
+ }
+ strings[str_cnt++] = name;
+ if (name_is_dir) {
+ /* Allow a trailing "/"+"***" to match the directory. */
+ if (ex->match_flags & MATCHFLG_WILD3_SUFFIX)
+ strings[str_cnt++] = "/";
+ } else if (ex->match_flags & MATCHFLG_DIRECTORY)
+ return !ret_match;
+ strings[str_cnt] = NULL;
+
+ if (*pattern == '/') {
+ anchored_match = 1;
+ pattern++;
+ if (strings[0][0] == '/')
+ strings[0]++;
+ }
+
+ if (!anchored_match && ex->u.slash_cnt
+ && !(ex->match_flags & MATCHFLG_WILD2)) {
+ /* A non-anchored match with an infix slash and no "**"
+ * needs to match the last slash_cnt+1 name elements. */
+ slash_handling = ex->u.slash_cnt + 1;
+ } else if (!anchored_match && !(ex->match_flags & MATCHFLG_WILD2_PREFIX)
+ && ex->match_flags & MATCHFLG_WILD2) {
+ /* A non-anchored match with an infix or trailing "**" (but not
+ * a prefixed "**") needs to try matching after every slash. */
+ slash_handling = -1;
+ } else {
+ /* The pattern matches only at the start of the path or name. */
+ slash_handling = 0;
+ }
+
+ if (ex->match_flags & MATCHFLG_WILD) {
+ if (wildmatch_array(pattern, strings, slash_handling))
+ return ret_match;
+ } else if (str_cnt > 1) {
+ if (litmatch_array(pattern, strings, slash_handling))
+ return ret_match;
+ } else if (anchored_match) {
+ if (strcmp(strings[0], pattern) == 0)
+ return ret_match;