+ if (listp->tail) {
+ struct filter_struct *ent, *next;
+ /* Truncate any inherited items from the local list. */
+ listp->tail->next = NULL;
+ /* Now free everything that is left. */
+ for (ent = listp->head; ent; ent = next) {
+ next = ent->next;
+ free_filter(ent);
+ }
+ }
+
+ listp->head = listp->tail = NULL;
+}
+
+/* This returns an expanded (absolute) filename for the merge-file name if
+ * the name has any slashes in it OR if the parent_dirscan var is True;
+ * otherwise it returns the original merge_file name. If the len_ptr value
+ * is non-NULL the merge_file name is limited by the referenced length
+ * value and will be updated with the length of the resulting name. We
+ * always return a name that is null terminated, even if the merge_file
+ * name was not. */
+static char *parse_merge_name(const char *merge_file, unsigned int *len_ptr,
+ unsigned int prefix_skip)
+{
+ static char buf[MAXPATHLEN];
+ char *fn, tmpbuf[MAXPATHLEN];
+ unsigned int fn_len;
+
+ if (!parent_dirscan && *merge_file != '/') {
+ /* Return the name unchanged it doesn't have any slashes. */
+ if (len_ptr) {
+ const char *p = merge_file + *len_ptr;
+ while (--p > merge_file && *p != '/') {}
+ if (p == merge_file) {
+ strlcpy(buf, merge_file, *len_ptr + 1);
+ return buf;
+ }
+ } else if (strchr(merge_file, '/') == NULL)
+ return (char *)merge_file;
+ }
+
+ fn = *merge_file == '/' ? buf : tmpbuf;
+ if (sanitize_paths) {
+ const char *r = prefix_skip ? "/" : NULL;
+ /* null-terminate the name if it isn't already */
+ if (len_ptr && merge_file[*len_ptr]) {
+ char *to = fn == buf ? tmpbuf : buf;
+ strlcpy(to, merge_file, *len_ptr + 1);
+ merge_file = to;
+ }
+ if (!sanitize_path(fn, merge_file, r, dirbuf_depth)) {
+ rprintf(FERROR, "merge-file name overflows: %s\n",
+ safe_fname(merge_file));
+ return NULL;
+ }
+ } else {
+ strlcpy(fn, merge_file, len_ptr ? *len_ptr + 1 : MAXPATHLEN);
+ clean_fname(fn, 1);
+ }
+
+ fn_len = strlen(fn);
+ if (fn == buf)
+ goto done;
+
+ if (dirbuf_len + fn_len >= MAXPATHLEN) {
+ rprintf(FERROR, "merge-file name overflows: %s\n",
+ safe_fname(fn));
+ return NULL;
+ }
+ memcpy(buf, dirbuf + prefix_skip, dirbuf_len - prefix_skip);
+ memcpy(buf + dirbuf_len - prefix_skip, fn, fn_len + 1);
+ fn_len = clean_fname(buf, 1);
+
+ done:
+ if (len_ptr)
+ *len_ptr = fn_len;
+ return buf;
+}
+
+/* Sets the dirbuf and dirbuf_len values. */
+void set_filter_dir(const char *dir, unsigned int dirlen)
+{
+ unsigned int len;
+ if (*dir != '/') {
+ memcpy(dirbuf, curr_dir, curr_dir_len);
+ dirbuf[curr_dir_len] = '/';
+ len = curr_dir_len + 1;
+ if (len + dirlen >= MAXPATHLEN)
+ dirlen = 0;
+ } else
+ len = 0;
+ memcpy(dirbuf + len, dir, dirlen);
+ dirbuf[dirlen + len] = '\0';
+ dirbuf_len = clean_fname(dirbuf, 1);
+ if (dirbuf_len > 1 && dirbuf[dirbuf_len-1] == '.'
+ && dirbuf[dirbuf_len-2] == '/')
+ dirbuf_len -= 2;
+ if (dirbuf_len != 1)
+ dirbuf[dirbuf_len++] = '/';
+ dirbuf[dirbuf_len] = '\0';
+ if (sanitize_paths)
+ dirbuf_depth = count_dir_elements(dirbuf + module_dirlen);