Blame SOURCES/rsync-3.2.3-filtering-rules.patch

f56e57
diff --git a/exclude.c b/exclude.c
f56e57
index 13c4253..232249f 100644
f56e57
--- a/exclude.c
f56e57
+++ b/exclude.c
f56e57
@@ -79,6 +79,10 @@ static filter_rule **mergelist_parents;
f56e57
 static int mergelist_cnt = 0;
f56e57
 static int mergelist_size = 0;
f56e57
 
f56e57
+#define LOCAL_RULE   1
f56e57
+#define REMOTE_RULE  2
f56e57
+static uchar cur_elide_value = REMOTE_RULE;
f56e57
+
f56e57
 /* Each filter_list_struct describes a singly-linked list by keeping track
f56e57
  * of both the head and tail pointers.  The list is slightly unusual in that
f56e57
  * a parent-dir's content can be appended to the end of the local list in a
f56e57
@@ -218,6 +222,7 @@ static void add_rule(filter_rule_list *listp, const char *pat, unsigned int pat_
f56e57
 				slash_cnt++;
f56e57
 		}
f56e57
 	}
f56e57
+	rule->elide = 0;
f56e57
 	strlcpy(rule->pattern + pre_len, pat, pat_len + 1);
f56e57
 	pat_len += pre_len;
f56e57
 	if (suf_len) {
f56e57
@@ -364,6 +369,8 @@ void implied_include_partial_string(const char *s_start, const char *s_end)
f56e57
 void free_implied_include_partial_string()
f56e57
 {
f56e57
 	if (partial_string_buf) {
f56e57
+		if (partial_string_len)
f56e57
+			add_implied_include("", 0);
f56e57
 		free(partial_string_buf);
f56e57
 		partial_string_buf = NULL;
f56e57
 	}
f56e57
@@ -374,9 +381,8 @@ void free_implied_include_partial_string()
f56e57
  * that the receiver uses to validate the file list from the sender. */
f56e57
 void add_implied_include(const char *arg, int skip_daemon_module)
f56e57
 {
f56e57
-	filter_rule *rule;
f56e57
 	int arg_len, saw_wild = 0, saw_live_open_brkt = 0, backslash_cnt = 0;
f56e57
-	int slash_cnt = 1; /* We know we're adding a leading slash. */
f56e57
+	int slash_cnt = 0;
f56e57
 	const char *cp;
f56e57
 	char *p;
f56e57
 	if (am_server || old_style_args || list_only || read_batch || filesfrom_host != NULL)
f56e57
@@ -407,6 +413,7 @@ void add_implied_include(const char *arg, int skip_daemon_module)
f56e57
 	}
f56e57
 	arg_len = strlen(arg);
f56e57
 	if (arg_len) {
f56e57
+		char *new_pat;
f56e57
 		if (strpbrk(arg, "*[?")) {
f56e57
 			/* We need to add room to escape backslashes if wildcard chars are present. */
f56e57
 			for (cp = arg; (cp = strchr(cp, '\\')) != NULL; cp++)
f56e57
@@ -414,16 +421,9 @@ void add_implied_include(const char *arg, int skip_daemon_module)
f56e57
 			saw_wild = 1;
f56e57
 		}
f56e57
 		arg_len++; /* Leave room for the prefixed slash */
f56e57
-		rule = new0(filter_rule);
f56e57
-		if (!implied_filter_list.head)
f56e57
-			implied_filter_list.head = implied_filter_list.tail = rule;
f56e57
-		else {
f56e57
-			rule->next = implied_filter_list.head;
f56e57
-			implied_filter_list.head = rule;
f56e57
-		}
f56e57
-		rule->rflags = FILTRULE_INCLUDE + (saw_wild ? FILTRULE_WILD : 0);
f56e57
-		p = rule->pattern = new_array(char, arg_len + 1);
f56e57
+		p = new_pat = new_array(char, arg_len + 1);
f56e57
 		*p++ = '/';
f56e57
+		slash_cnt++;
f56e57
 		for (cp = arg; *cp; ) {
f56e57
 			switch (*cp) {
f56e57
 			  case '\\':
f56e57
@@ -439,15 +439,70 @@ void add_implied_include(const char *arg, int skip_daemon_module)
f56e57
 				break;
f56e57
 			  case '/':
f56e57
 				if (p[-1] == '/') { /* This is safe because of the initial slash. */
f56e57
+					if (*++cp == '\0') {
f56e57
+						slash_cnt--;
f56e57
+						p--;
f56e57
+					}
f56e57
+				} else if (cp[1] == '\0') {
f56e57
 					cp++;
f56e57
-					break;
f56e57
+				} else {
f56e57
+					slash_cnt++;
f56e57
+					*p++ = *cp++;
f56e57
 				}
f56e57
-				if (relative_paths) {
f56e57
-					filter_rule const *ent;
f56e57
+				break;
f56e57
+			  case '.':
f56e57
+				if (p[-1] == '/') {
f56e57
+					if (cp[1] == '/') {
f56e57
+						cp += 2;
f56e57
+						if (!*cp) {
f56e57
+							slash_cnt--;
f56e57
+							p--;
f56e57
+						}
f56e57
+					} else if (cp[1] == '\0') {
f56e57
+						cp++;
f56e57
+						slash_cnt--;
f56e57
+						p--;
f56e57
+					} else
f56e57
+						*p++ = *cp++;
f56e57
+				} else
f56e57
+					*p++ = *cp++;
f56e57
+				break;
f56e57
+			  case '[':
f56e57
+				saw_live_open_brkt = 1;
f56e57
+				*p++ = *cp++;
f56e57
+				break;
f56e57
+			  default:
f56e57
+				*p++ = *cp++;
f56e57
+				break;
f56e57
+			}
f56e57
+		}
f56e57
+		*p = '\0';
f56e57
+		arg_len = p - new_pat;
f56e57
+		if (!arg_len)
f56e57
+			free(new_pat);
f56e57
+		else {
f56e57
+			filter_rule *rule = new0(filter_rule);
f56e57
+			rule->rflags = FILTRULE_INCLUDE + (saw_wild ? FILTRULE_WILD : 0);
f56e57
+			rule->u.slash_cnt = slash_cnt;
f56e57
+			arg = rule->pattern = new_pat;
f56e57
+			if (!implied_filter_list.head)
f56e57
+				implied_filter_list.head = implied_filter_list.tail = rule;
f56e57
+			else {
f56e57
+				rule->next = implied_filter_list.head;
f56e57
+				implied_filter_list.head = rule;
f56e57
+			}
f56e57
+			if (DEBUG_GTE(FILTER, 3))
f56e57
+				rprintf(FINFO, "[%s] add_implied_include(%s)\n", who_am_i(), arg);
f56e57
+			if (saw_live_open_brkt)
f56e57
+				maybe_add_literal_brackets_rule(rule, arg_len);
f56e57
+			if (relative_paths && slash_cnt) {
f56e57
+				filter_rule const *ent;
f56e57
+				slash_cnt = 1;
f56e57
+				for (p = new_pat + 1; (p = strchr(p, '/')) != NULL; p++) {
f56e57
 					int found = 0;
f56e57
 					*p = '\0';
f56e57
 					for (ent = implied_filter_list.head; ent; ent = ent->next) {
f56e57
-						if (ent != rule && strcmp(ent->pattern, rule->pattern) == 0) {
f56e57
+						if (ent != rule && strcmp(ent->pattern, new_pat) == 0) {
f56e57
 							found = 1;
f56e57
 							break;
f56e57
 						}
f56e57
@@ -456,9 +511,9 @@ void add_implied_include(const char *arg, int skip_daemon_module)
f56e57
 						filter_rule *R_rule = new0(filter_rule);
f56e57
 						R_rule->rflags = FILTRULE_INCLUDE | FILTRULE_DIRECTORY;
f56e57
 						/* Check if our sub-path has wildcards or escaped backslashes */
f56e57
-						if (saw_wild && strpbrk(rule->pattern, "*[?\\"))
f56e57
+						if (saw_wild && strpbrk(new_pat, "*[?\\"))
f56e57
 							R_rule->rflags |= FILTRULE_WILD;
f56e57
-						R_rule->pattern = strdup(rule->pattern);
f56e57
+						R_rule->pattern = strdup(new_pat);
f56e57
 						R_rule->u.slash_cnt = slash_cnt;
f56e57
 						R_rule->next = implied_filter_list.head;
f56e57
 						implied_filter_list.head = R_rule;
f56e57
@@ -469,32 +524,16 @@ void add_implied_include(const char *arg, int skip_daemon_module)
f56e57
 						if (saw_live_open_brkt)
f56e57
 							maybe_add_literal_brackets_rule(R_rule, -1);
f56e57
 					}
f56e57
+					*p = '/';
f56e57
+					slash_cnt++;
f56e57
 				}
f56e57
-				slash_cnt++;
f56e57
-				*p++ = *cp++;
f56e57
-				break;
f56e57
-			  case '[':
f56e57
-				saw_live_open_brkt = 1;
f56e57
-				*p++ = *cp++;
f56e57
-				break;
f56e57
-			  default:
f56e57
-				*p++ = *cp++;
f56e57
-				break;
f56e57
 			}
f56e57
 		}
f56e57
-		*p = '\0';
f56e57
-		rule->u.slash_cnt = slash_cnt;
f56e57
-		arg = rule->pattern;
f56e57
-		arg_len = p - arg; /* We recompute it due to backslash weirdness. */
f56e57
-		if (DEBUG_GTE(FILTER, 3))
f56e57
-			rprintf(FINFO, "[%s] add_implied_include(%s)\n", who_am_i(), rule->pattern);
f56e57
-		if (saw_live_open_brkt)
f56e57
-			maybe_add_literal_brackets_rule(rule, arg_len);
f56e57
 	}
f56e57
 
f56e57
 	if (recurse || xfer_dirs) {
f56e57
 		/* Now create a rule with an added "/" & "**" or "*" at the end */
f56e57
-		rule = new0(filter_rule);
f56e57
+		filter_rule *rule = new0(filter_rule);
f56e57
 		rule->rflags = FILTRULE_INCLUDE | FILTRULE_WILD;
f56e57
 		if (recurse)
f56e57
 			rule->rflags |= FILTRULE_WILD2;
f56e57
@@ -502,7 +541,7 @@ void add_implied_include(const char *arg, int skip_daemon_module)
f56e57
 		if (!saw_wild && backslash_cnt) {
f56e57
 			/* We are appending a wildcard, so now the backslashes need to be escaped. */
f56e57
 			p = rule->pattern = new_array(char, arg_len + backslash_cnt + 3 + 1);
f56e57
-			for (cp = arg; *cp; ) {
f56e57
+			for (cp = arg; *cp; ) { /* Note that arg_len != 0 because backslash_cnt > 0 */
f56e57
 				if (*cp == '\\')
f56e57
 					*p++ = '\\';
f56e57
 				*p++ = *cp++;
f56e57
@@ -514,13 +553,15 @@ void add_implied_include(const char *arg, int skip_daemon_module)
f56e57
 				p += arg_len;
f56e57
 			}
f56e57
 		}
f56e57
-		if (p[-1] != '/')
f56e57
+		if (p[-1] != '/') {
f56e57
 			*p++ = '/';
f56e57
+			slash_cnt++;
f56e57
+		}
f56e57
 		*p++ = '*';
f56e57
 		if (recurse)
f56e57
 			*p++ = '*';
f56e57
 		*p = '\0';
f56e57
-		rule->u.slash_cnt = slash_cnt + 1;
f56e57
+		rule->u.slash_cnt = slash_cnt;
f56e57
 		rule->next = implied_filter_list.head;
f56e57
 		implied_filter_list.head = rule;
f56e57
 		if (DEBUG_GTE(FILTER, 3))
f56e57
@@ -869,7 +910,7 @@ static int rule_matches(const char *fname, filter_rule *ex, int name_flags)
f56e57
 	const char *strings[16]; /* more than enough */
f56e57
 	const char *name = fname + (*fname == '/');
f56e57
 
f56e57
-	if (!*name)
f56e57
+	if (!*name || ex->elide == cur_elide_value)
f56e57
 		return 0;
f56e57
 
f56e57
 	if (!(name_flags & NAME_IS_XATTR) ^ !(ex->rflags & FILTRULE_XATTR))
f56e57
@@ -985,6 +1026,15 @@ int name_is_excluded(const char *fname, int name_flags, int filter_level)
f56e57
 	return 0;
f56e57
 }
f56e57
 
f56e57
+int check_server_filter(filter_rule_list *listp, enum logcode code, const char *name, int name_flags)
f56e57
+{
f56e57
+	int ret;
f56e57
+	cur_elide_value = LOCAL_RULE;
f56e57
+	ret = check_filter(listp, code, name, name_flags);
f56e57
+	cur_elide_value = REMOTE_RULE;
f56e57
+	return ret;
f56e57
+}
f56e57
+
f56e57
 /* Return -1 if file "name" is defined to be excluded by the specified
f56e57
  * exclude list, 1 if it is included, and 0 if it was not matched. */
f56e57
 int check_filter(filter_rule_list *listp, enum logcode code,
f56e57
@@ -1550,7 +1600,7 @@ char *get_rule_prefix(filter_rule *rule, const char *pat, int for_xfer,
f56e57
 
f56e57
 static void send_rules(int f_out, filter_rule_list *flp)
f56e57
 {
f56e57
-	filter_rule *ent, *prev = NULL;
f56e57
+	filter_rule *ent;
f56e57
 
f56e57
 	for (ent = flp->head; ent; ent = ent->next) {
f56e57
 		unsigned int len, plen, dlen;
f56e57
@@ -1565,21 +1615,15 @@ static void send_rules(int f_out, filter_rule_list *flp)
f56e57
 		 * merge files as an optimization (since they can only have
f56e57
 		 * include/exclude rules). */
f56e57
 		if (ent->rflags & FILTRULE_SENDER_SIDE)
f56e57
-			elide = am_sender ? 1 : -1;
f56e57
+			elide = am_sender ? LOCAL_RULE : REMOTE_RULE;
f56e57
 		if (ent->rflags & FILTRULE_RECEIVER_SIDE)
f56e57
-			elide = elide ? 0 : am_sender ? -1 : 1;
f56e57
+			elide = elide ? 0 : am_sender ? REMOTE_RULE : LOCAL_RULE;
f56e57
 		else if (delete_excluded && !elide
f56e57
 		 && (!(ent->rflags & FILTRULE_PERDIR_MERGE)
f56e57
 		  || ent->rflags & FILTRULE_NO_PREFIXES))
f56e57
-			elide = am_sender ? 1 : -1;
f56e57
-		if (elide < 0) {
f56e57
-			if (prev)
f56e57
-				prev->next = ent->next;
f56e57
-			else
f56e57
-				flp->head = ent->next;
f56e57
-		} else
f56e57
-			prev = ent;
f56e57
-		if (elide > 0)
f56e57
+			elide = am_sender ? LOCAL_RULE : REMOTE_RULE;
f56e57
+		ent->elide = elide;
f56e57
+		if (elide == LOCAL_RULE)
f56e57
 			continue;
f56e57
 		if (ent->rflags & FILTRULE_CVS_IGNORE
f56e57
 		    && !(ent->rflags & FILTRULE_MERGE_FILE)) {
f56e57
@@ -1607,7 +1651,6 @@ static void send_rules(int f_out, filter_rule_list *flp)
f56e57
 		if (dlen)
f56e57
 			write_byte(f_out, '/');
f56e57
 	}
f56e57
-	flp->tail = prev;
f56e57
 }
f56e57
 
f56e57
 /* This is only called by the client. */
f56e57
diff --git a/options.c b/options.c
f56e57
index afc33ce..4d0a1a6 100644
f56e57
--- a/options.c
f56e57
+++ b/options.c
f56e57
@@ -2426,7 +2426,9 @@ char *safe_arg(const char *opt, const char *arg)
f56e57
 	char *ret;
f56e57
 	if (!protect_args && old_style_args < 2 && (!old_style_args || (!is_filename_arg && opt != SPLIT_ARG_WHEN_OLD))) {
f56e57
 		const char *f;
f56e57
-		if (!old_style_args && *arg == '~' && (relative_paths || !strchr(arg, '/'))) {
f56e57
+		if (!old_style_args && *arg == '~' 
f56e57
+				&& ((relative_paths && !strstr(arg, "/./")) 
f56e57
+				|| !strchr(arg, '/'))) {
f56e57
 			extras++;
f56e57
 			escape_leading_tilde = 1;
f56e57
 		}
f56e57
diff --git a/flist.c b/flist.c
f56e57
index 630d685..8c2397b 100644
f56e57
--- a/flist.c
f56e57
+++ b/flist.c
f56e57
@@ -904,10 +904,10 @@ static struct file_struct *recv_file_entry(int f, struct file_list *flist, int x
f56e57
 		exit_cleanup(RERR_UNSUPPORTED);
f56e57
 	}
f56e57
 
f56e57
-	if (*thisname != '.' || thisname[1] != '\0') {
f56e57
+	if (*thisname == '/' ? thisname[1] != '.' || thisname[2] != '\0' : *thisname != '.' || thisname[1] != '\0') {
f56e57
 		int filt_flags = S_ISDIR(mode) ? NAME_IS_DIR : NAME_IS_FILE;
f56e57
 		if (!trust_sender_filter /* a per-dir filter rule means we must trust the sender's filtering */
f56e57
-		 && filter_list.head && check_filter(&filter_list, FINFO, thisname, filt_flags) < 0) {
f56e57
+		 && filter_list.head && check_server_filter(&filter_list, FINFO, thisname, filt_flags) < 0) {
f56e57
 			rprintf(FERROR, "ERROR: rejecting excluded file-list name: %s\n", thisname);
f56e57
 			exit_cleanup(RERR_PROTOCOL);
f56e57
 		}
f56e57
diff --git a/rsync.h b/rsync.h
f56e57
index 53fff2d..b357dad 100644
f56e57
--- a/rsync.h
f56e57
+++ b/rsync.h
f56e57
@@ -899,6 +899,7 @@ typedef struct filter_struct {
f56e57
 		int slash_cnt;
f56e57
 		struct filter_list_struct *mergelist;
f56e57
 	} u;
f56e57
+	uchar elide;
f56e57
 } filter_rule;
f56e57
 
f56e57
 typedef struct filter_list_struct {