Avoid leaving a file open on error return.
[rsync.git] / exclude.c
index 16b2a562fbbbc3733473fb18bec6beb9cb9f0f01..a0090b29a7656a1c82c7c5baee9dd4d7e40bd2df 100644 (file)
--- a/exclude.c
+++ b/exclude.c
@@ -4,7 +4,7 @@
  * Copyright (C) 1996-2001 Andrew Tridgell <tridge@samba.org>
  * Copyright (C) 1996 Paul Mackerras
  * Copyright (C) 2002 Martin Pool
- * Copyright (C) 2003-2009 Wayne Davison
+ * Copyright (C) 2003-2018 Wayne Davison
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -44,6 +44,8 @@ filter_rule_list filter_list = { .debug_type = "" };
 filter_rule_list cvs_filter_list = { .debug_type = " [global CVS]" };
 filter_rule_list daemon_filter_list = { .debug_type = " [daemon]" };
 
+int saw_xattr_filter = 0;
+
 /* Need room enough for ":MODS " prefix plus some room to grow. */
 #define MAX_RULE_PREFIX (16)
 
@@ -100,52 +102,44 @@ static int mergelist_size = 0;
 
 static void teardown_mergelist(filter_rule *ex)
 {
+       int j;
+
+       if (!ex->u.mergelist)
+               return;
+
        if (DEBUG_GTE(FILTER, 2)) {
                rprintf(FINFO, "[%s] deactivating mergelist #%d%s\n",
                        who_am_i(), mergelist_cnt - 1,
                        ex->u.mergelist->debug_type);
        }
 
-       /* We should deactivate mergelists in LIFO order. */
-       assert(mergelist_cnt > 0);
-       assert(ex == mergelist_parents[mergelist_cnt - 1]);
-
-       /* The parent_dirscan filters should have been freed. */
-       assert(ex->u.mergelist->parent_dirscan_head == NULL);
-
        free(ex->u.mergelist->debug_type);
        free(ex->u.mergelist);
-       mergelist_cnt--;
+
+       for (j = 0; j < mergelist_cnt; j++) {
+               if (mergelist_parents[j] == ex) {
+                       mergelist_parents[j] = NULL;
+                       break;
+               }
+       }
+       while (mergelist_cnt && mergelist_parents[mergelist_cnt-1] == NULL)
+               mergelist_cnt--;
 }
 
 static void free_filter(filter_rule *ex)
 {
+       if (ex->rflags & FILTRULE_PERDIR_MERGE)
+               teardown_mergelist(ex);
        free(ex->pattern);
        free(ex);
 }
 
-static void free_filters(filter_rule *head)
+static void free_filters(filter_rule *ent)
 {
-       filter_rule *rev_head = NULL;
-
-       /* Reverse the list so we deactivate mergelists in the proper LIFO
-        * order. */
-       while (head) {
-               filter_rule *next = head->next;
-               head->next = rev_head;
-               rev_head = head;
-               head = next;
-       }
-
-       while (rev_head) {
-               filter_rule *prev = rev_head->next;
-               /* Tear down mergelists here, not in free_filter, so that we
-                * affect only real filter lists and not temporarily allocated
-                * filters. */
-               if (rev_head->rflags & FILTRULE_PERDIR_MERGE)
-                       teardown_mergelist(rev_head);
-               free_filter(rev_head);
-               rev_head = prev;
+       while (ent) {
+               filter_rule *next = ent->next;
+               free_filter(ent);
+               ent = next;
        }
 }
 
@@ -252,7 +246,10 @@ static void add_rule(filter_rule_list *listp, const char *pat, unsigned int pat_
                 * add it again. */
                for (i = 0; i < mergelist_cnt; i++) {
                        filter_rule *ex = mergelist_parents[i];
-                       const char *s = strrchr(ex->pattern, '/');
+                       const char *s;
+                       if (!ex)
+                               continue;
+                       s = strrchr(ex->pattern, '/');
                        if (s)
                                s++;
                        else
@@ -264,9 +261,8 @@ static void add_rule(filter_rule_list *listp, const char *pat, unsigned int pat_
                        }
                }
 
-               if (!(lp = new_array(filter_rule_list, 1)))
+               if (!(lp = new_array0(filter_rule_list, 1)))
                        out_of_memory("add_rule");
-               lp->head = lp->tail = lp->parent_dirscan_head = NULL;
                if (asprintf(&lp->debug_type, " [per-dir %s]", cp) < 0)
                        out_of_memory("add_rule");
                rule->u.mergelist = lp;
@@ -297,16 +293,23 @@ static void add_rule(filter_rule_list *listp, const char *pat, unsigned int pat_
        }
 }
 
-static void clear_filter_list(filter_rule_list *listp)
+/* This frees any non-inherited items, leaving just inherited items on the list. */
+static void pop_filter_list(filter_rule_list *listp)
 {
-       if (listp->tail) {
-               /* Truncate any inherited items from the local list. */
-               listp->tail->next = NULL;
-               /* Now free everything that is left. */
-               free_filters(listp->head);
-       }
+       filter_rule *inherited;
+
+       if (!listp->tail)
+               return;
+
+       inherited = listp->tail->next;
 
-       listp->head = listp->tail = NULL;
+       /* Truncate any inherited items from the local list. */
+       listp->tail->next = NULL;
+       /* Now free everything that is left. */
+       free_filters(listp->head);
+
+       listp->head = inherited;
+       listp->tail = NULL;
 }
 
 /* This returns an expanded (absolute) filename for the merge-file name if
@@ -356,7 +359,7 @@ static char *parse_merge_name(const char *merge_file, unsigned int *len_ptr,
                fn_len = clean_fname(fn, CFN_COLLAPSE_DOT_DOT_DIRS);
        }
 
-       /* If the name isn't in buf yet, it's wasn't absolute. */
+       /* If the name isn't in buf yet, it wasn't absolute. */
        if (fn != buf) {
                int d_len = dirbuf_len - prefix_skip;
                if (d_len + fn_len >= MAXPATHLEN) {
@@ -457,8 +460,6 @@ static BOOL setup_merge_file(int mergelist_num, filter_rule *ex,
                strlcpy(y, save, MAXPATHLEN);
                while ((*x++ = *y++) != '/') {}
        }
-       /* Save current head for freeing when the mergelist becomes inactive. */
-       lp->parent_dirscan_head = lp->head;
        parent_dirscan = False;
        if (DEBUG_GTE(FILTER, 2)) {
                rprintf(FINFO, "[%s] completed parent_dirscan for mergelist #%d%s\n",
@@ -501,15 +502,20 @@ void *push_local_filters(const char *dir, unsigned int dirlen)
 
        push->mergelist_cnt = mergelist_cnt;
        for (i = 0; i < mergelist_cnt; i++) {
-               memcpy(&push->mergelists[i], mergelist_parents[i]->u.mergelist,
-                      sizeof (filter_rule_list));
+               filter_rule *ex = mergelist_parents[i];
+               if (!ex)
+                       continue;
+               memcpy(&push->mergelists[i], ex->u.mergelist, sizeof (filter_rule_list));
        }
 
        /* Note: parse_filter_file() might increase mergelist_cnt, so keep
         * this loop separate from the above loop. */
        for (i = 0; i < mergelist_cnt; i++) {
                filter_rule *ex = mergelist_parents[i];
-               filter_rule_list *lp = ex->u.mergelist;
+               filter_rule_list *lp;
+               if (!ex)
+                       continue;
+               lp = ex->u.mergelist;
 
                if (DEBUG_GTE(FILTER, 2)) {
                        rprintf(FINFO, "[%s] pushing mergelist #%d%s\n",
@@ -553,44 +559,38 @@ void pop_local_filters(void *mem)
 
        for (i = mergelist_cnt; i-- > 0; ) {
                filter_rule *ex = mergelist_parents[i];
-               filter_rule_list *lp = ex->u.mergelist;
+               filter_rule_list *lp;
+               if (!ex)
+                       continue;
+               lp = ex->u.mergelist;
 
                if (DEBUG_GTE(FILTER, 2)) {
                        rprintf(FINFO, "[%s] popping mergelist #%d%s\n",
                                who_am_i(), i, lp->debug_type);
                }
 
-               clear_filter_list(lp);
-
-               if (i >= old_mergelist_cnt) {
-                       /* This mergelist does not exist in the state to be
-                        * restored.  Free its parent_dirscan list to clean up
-                        * any per-dir mergelists defined there so we don't
-                        * crash trying to restore nonexistent state for them
-                        * below.  (Counterpart to setup_merge_file call in
-                        * push_local_filters.  Must be done here, not in
-                        * free_filter, for LIFO order.) */
+               pop_filter_list(lp);
+               if (i >= old_mergelist_cnt && lp->head) {
+                       /* This mergelist does not exist in the state to be restored, but it
+                        * still has inherited rules.  This can sometimes happen if a per-dir
+                        * merge file calls setup_merge_file() in push_local_filters() and that
+                        * leaves some inherited rules that aren't in the pushed list state. */
                        if (DEBUG_GTE(FILTER, 2)) {
                                rprintf(FINFO, "[%s] freeing parent_dirscan filters of mergelist #%d%s\n",
                                        who_am_i(), i, ex->u.mergelist->debug_type);
                        }
-                       free_filters(lp->parent_dirscan_head);
-                       lp->parent_dirscan_head = NULL;
+                       pop_filter_list(lp);
                }
        }
 
-       /* If we cleaned things up properly, the only still-active mergelists
-        * should be those with a state to be restored. */
-       assert(mergelist_cnt == old_mergelist_cnt);
-
-       if (!pop) {
-               /* No state to restore. */
-               return;
-       }
+       if (!pop)
+               return; /* No state to restore. */
 
-       for (i = 0; i < mergelist_cnt; i++) {
-               memcpy(mergelist_parents[i]->u.mergelist, &pop->mergelists[i],
-                      sizeof (filter_rule_list));
+       for (i = 0; i < old_mergelist_cnt; i++) {
+               filter_rule *ex = mergelist_parents[i];
+               if (!ex)
+                       continue;
+               memcpy(ex->u.mergelist, &pop->mergelists[i], sizeof (filter_rule_list));
        }
 
        free(pop);
@@ -624,7 +624,7 @@ void change_local_filter_dir(const char *dname, int dlen, int dir_depth)
        filt_array[cur_depth] = push_local_filters(dname, dlen);
 }
 
-static int rule_matches(const char *fname, filter_rule *ex, int name_is_dir)
+static int rule_matches(const char *fname, filter_rule *ex, int name_flags)
 {
        int slash_handling, str_cnt = 0, anchored_match = 0;
        int ret_match = ex->rflags & FILTRULE_NEGATE ? 0 : 1;
@@ -635,6 +635,9 @@ static int rule_matches(const char *fname, filter_rule *ex, int name_is_dir)
        if (!*name)
                return 0;
 
+       if (!(name_flags & NAME_IS_XATTR) ^ !(ex->rflags & FILTRULE_XATTR))
+               return 0;
+
        if (!ex->u.slash_cnt && !(ex->rflags & FILTRULE_WILD2)) {
                /* If the pattern does not have any slashes AND it does
                 * not have a "**" (which could match a slash), then we
@@ -652,7 +655,7 @@ static int rule_matches(const char *fname, filter_rule *ex, int name_is_dir)
                strings[str_cnt++] = "/";
        }
        strings[str_cnt++] = name;
-       if (name_is_dir) {
+       if (name_flags & NAME_IS_DIR) {
                /* Allow a trailing "/"+"***" to match the directory. */
                if (ex->rflags & FILTRULE_WILD3_SUFFIX)
                        strings[str_cnt++] = "/";
@@ -704,7 +707,7 @@ static int rule_matches(const char *fname, filter_rule *ex, int name_is_dir)
 
 static void report_filter_result(enum logcode code, char const *name,
                                 filter_rule const *ent,
-                                int name_is_dir, const char *type)
+                                int name_flags, const char *type)
 {
        /* If a trailing slash is present to match only directories,
         * then it is stripped out by add_rule().  So as a special
@@ -714,17 +717,40 @@ static void report_filter_result(enum logcode code, char const *name,
                static char *actions[2][2]
                    = { {"show", "hid"}, {"risk", "protect"} };
                const char *w = who_am_i();
+               const char *t = name_flags & NAME_IS_XATTR ? "xattr"
+                             : name_flags & NAME_IS_DIR ? "directory"
+                             : "file";
                rprintf(code, "[%s] %sing %s %s because of pattern %s%s%s\n",
                    w, actions[*w!='s'][!(ent->rflags & FILTRULE_INCLUDE)],
-                   name_is_dir ? "directory" : "file", name, ent->pattern,
+                   t, name, ent->pattern,
                    ent->rflags & FILTRULE_DIRECTORY ? "/" : "", type);
        }
 }
 
+/* This function is used to check if a file should be included/excluded
+ * from the list of files based on its name and type etc.  The value of
+ * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
+int name_is_excluded(const char *fname, int name_flags, int filter_level)
+{
+       if (daemon_filter_list.head && check_filter(&daemon_filter_list, FLOG, fname, name_flags) < 0) {
+               if (!(name_flags & NAME_IS_XATTR))
+                       errno = ENOENT;
+               return 1;
+       }
+
+       if (filter_level != ALL_FILTERS)
+               return 0;
+
+       if (filter_list.head && check_filter(&filter_list, FINFO, fname, name_flags) < 0)
+               return 1;
+
+       return 0;
+}
+
 /* Return -1 if file "name" is defined to be excluded by the specified
  * exclude list, 1 if it is included, and 0 if it was not matched. */
 int check_filter(filter_rule_list *listp, enum logcode code,
-                const char *name, int name_is_dir)
+                const char *name, int name_flags)
 {
        filter_rule *ent;
 
@@ -732,22 +758,19 @@ int check_filter(filter_rule_list *listp, enum logcode code,
                if (ignore_perishable && ent->rflags & FILTRULE_PERISHABLE)
                        continue;
                if (ent->rflags & FILTRULE_PERDIR_MERGE) {
-                       int rc = check_filter(ent->u.mergelist, code, name,
-                                             name_is_dir);
+                       int rc = check_filter(ent->u.mergelist, code, name, name_flags);
                        if (rc)
                                return rc;
                        continue;
                }
                if (ent->rflags & FILTRULE_CVS_IGNORE) {
-                       int rc = check_filter(&cvs_filter_list, code, name,
-                                             name_is_dir);
+                       int rc = check_filter(&cvs_filter_list, code, name, name_flags);
                        if (rc)
                                return rc;
                        continue;
                }
-               if (rule_matches(name, ent, name_is_dir)) {
-                       report_filter_result(code, name, ent, name_is_dir,
-                                            listp->debug_type);
+               if (rule_matches(name, ent, name_flags)) {
+                       report_filter_result(code, name, ent, name_flags, listp->debug_type);
                        return ent->rflags & FILTRULE_INCLUDE ? 1 : -1;
                }
        }
@@ -872,7 +895,7 @@ static filter_rule *parse_rule_tok(const char **rulestr_ptr,
                switch (ch) {
                case ':':
                        rule->rflags |= FILTRULE_PERDIR_MERGE
-                                      | FILTRULE_FINISH_SETUP;
+                                     | FILTRULE_FINISH_SETUP;
                        /* FALL THROUGH */
                case '.':
                        rule->rflags |= FILTRULE_MERGE_FILE;
@@ -972,6 +995,10 @@ static filter_rule *parse_rule_tok(const char **rulestr_ptr,
                                        goto invalid;
                                rule->rflags |= FILTRULE_WORD_SPLIT;
                                break;
+                       case 'x':
+                               rule->rflags |= FILTRULE_XATTR;
+                               saw_xattr_filter = 1;
+                               break;
                        }
                }
                if (*s)
@@ -1013,13 +1040,9 @@ static filter_rule *parse_rule_tok(const char **rulestr_ptr,
                exit_cleanup(RERR_SYNTAX);
        }
 
-       /* --delete-excluded turns an un-modified include/exclude into a
-        * sender-side rule.  We also affect per-dir merge files that take
-        * no prefixes as a simple optimization. */
+       /* --delete-excluded turns an un-modified include/exclude into a sender-side rule.  */
        if (delete_excluded
-        && !(rule->rflags & FILTRULES_SIDES)
-        && (!(rule->rflags & FILTRULE_PERDIR_MERGE)
-         || rule->rflags & FILTRULE_NO_PREFIXES))
+        && !(rule->rflags & (FILTRULES_SIDES|FILTRULE_MERGE_FILE|FILTRULE_PERDIR_MERGE)))
                rule->rflags |= FILTRULE_SENDER_SIDE;
 
        *pat_ptr = (const char *)s;
@@ -1097,7 +1120,8 @@ void parse_filter_str(filter_rule_list *listp, const char *rulestr,
                                        "[%s] clearing filter list%s\n",
                                        who_am_i(), listp->debug_type);
                        }
-                       clear_filter_list(listp);
+                       pop_filter_list(listp);
+                       listp->head = NULL;
                        goto free_continue;
                }
 
@@ -1262,6 +1286,8 @@ char *get_rule_prefix(filter_rule *rule, const char *pat, int for_xfer,
        }
        if (rule->rflags & FILTRULE_EXCLUDE_SELF)
                *op++ = 'e';
+       if (rule->rflags & FILTRULE_XATTR)
+               *op++ = 'x';
        if (rule->rflags & FILTRULE_SENDER_SIDE
            && (!for_xfer || protocol_version >= 29))
                *op++ = 's';