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

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