Depends-On-Patch: g2r-basis-filename.diff

This patch allows multiple --compare-dest or --link-dest options to be
used, making the transfer of some files more optimal.  Note that the
algorithm does NOT search for the best match -- it stops at the first
match and uses that as the basis file for the transfer, so be sure to
order your arguments appropriately (the args are searched in the order
they are suppled).

Before compiling, be sure to run "make proto".

--- orig/generator.c	2004-07-22 00:05:38
+++ generator.c	2004-07-19 08:32:53
@@ -42,7 +42,7 @@ extern int size_only;
 extern int io_timeout;
 extern int protocol_version;
 extern int always_checksum;
-extern char *compare_dest;
+extern char *compare_dest[];
 extern int link_dest;
 extern int whole_file;
 extern int local_server;
@@ -79,13 +79,12 @@ static int skip_file(char *fname, struct
 	if (always_checksum && S_ISREG(st->st_mode)) {
 		char sum[MD4_SUM_LENGTH];
 		char fnamecmpdest[MAXPATHLEN];
+		int i;
 
-		if (compare_dest != NULL) {
-			if (access(fname, 0) != 0) {
-				pathjoin(fnamecmpdest, sizeof fnamecmpdest,
-					 compare_dest, fname);
-				fname = fnamecmpdest;
-			}
+		for (i = 0; compare_dest[i] != NULL && access(fname, 0) < 0; i++) {
+			pathjoin(fnamecmpdest, sizeof fnamecmpdest,
+					compare_dest[i], fname);
+			fname = fnamecmpdest;
 		}
 		file_checksum(fname,sum,st->st_size);
 		return memcmp(sum, file->u.sum, protocol_version < 21 ? 2
@@ -413,13 +412,18 @@ static void recv_generator(char *fname, 
 
 	fnamecmp = fname;
 
-	if (statret == -1 && compare_dest != NULL) {
+	if (statret == -1 && compare_dest[0] != NULL) {
 		/* try the file at compare_dest instead */
 		int saveerrno = errno;
-		pathjoin(fnamecmpbuf, sizeof fnamecmpbuf, compare_dest, fname);
-		statret = link_stat(fnamecmpbuf, &st, 0);
-		if (!S_ISREG(st.st_mode))
-			statret = -1;
+		int i;
+		for (i = 0; compare_dest[i] != NULL; i++) {
+			pathjoin(fnamecmpbuf, sizeof fnamecmpbuf, compare_dest[i], fname);
+			if ((statret = link_stat(fnamecmpbuf, &st, 0)) == 0) {
+				if (S_ISREG(st.st_mode))
+					break;
+				statret = -1;
+			}
+		}
 		if (statret == -1) {
 			errno = saveerrno;
 			*fnamecmpbuf = '\0';
--- orig/main.c	2004-07-22 00:10:43
+++ main.c	2004-07-22 00:30:38
@@ -58,7 +58,7 @@ extern int filesfrom_fd;
 extern pid_t cleanup_child_pid;
 extern char *files_from;
 extern char *remote_filesfrom_file;
-extern char *compare_dest;
+extern char *compare_dest[];
 extern char *rsync_path;
 extern char *shell_cmd;
 extern char *batch_name;
@@ -458,7 +458,7 @@ static int do_recv(int f_in,int f_out,st
 	int pid;
 	int status = 0;
 	int error_pipe[2], name_pipe[2];
-	BOOL need_name_pipe = compare_dest && !dry_run;
+	BOOL need_name_pipe = compare_dest[0] && !dry_run;
 
 	if (preserve_hard_links)
 		init_hard_links(flist);
--- orig/options.c	2004-07-23 17:16:13
+++ options.c	2004-07-03 17:33:30
@@ -118,7 +118,8 @@ unsigned int backup_dir_remainder;
 
 char *backup_suffix = NULL;
 char *tmpdir = NULL;
-char *compare_dest = NULL;
+char *compare_dest[MAX_COMP_DEST+1];
+int num_comp_dest = 0;
 char *config_file = NULL;
 char *shell_cmd = NULL;
 char *log_format = NULL;
@@ -139,6 +140,7 @@ char *batch_name = NULL;
 
 static int daemon_opt;   /* sets am_daemon after option error-reporting */
 static int modify_window_set;
+static int saw_compare_dest = 0;
 
 /** Local address to bind.  As a character string because it's
  * interpreted by the IPv6 layer: should be a numeric IP4 or IP6
@@ -315,7 +317,7 @@ void usage(enum logcode F)
 }
 
 enum {OPT_VERSION = 1000, OPT_SENDER, OPT_EXCLUDE, OPT_EXCLUDE_FROM,
-      OPT_DELETE_AFTER, OPT_DELETE_EXCLUDED, OPT_LINK_DEST,
+      OPT_DELETE_AFTER, OPT_DELETE_EXCLUDED, OPT_COMPARE_DEST, OPT_LINK_DEST,
       OPT_INCLUDE, OPT_INCLUDE_FROM, OPT_MODIFY_WINDOW,
       OPT_READ_BATCH, OPT_WRITE_BATCH, OPT_TIMEOUT,
       OPT_REFUSED_BASE = 9000};
@@ -374,8 +376,8 @@ static struct poptOption long_options[] 
   {"max-delete",       0,  POPT_ARG_INT,    &max_delete, 0, 0, 0 },
   {"timeout",          0,  POPT_ARG_INT,    &io_timeout, OPT_TIMEOUT, 0, 0 },
   {"temp-dir",        'T', POPT_ARG_STRING, &tmpdir, 0, 0, 0 },
-  {"compare-dest",     0,  POPT_ARG_STRING, &compare_dest, 0, 0, 0 },
-  {"link-dest",        0,  POPT_ARG_STRING, &compare_dest,  OPT_LINK_DEST, 0, 0 },
+  {"compare-dest",     0,  POPT_ARG_STRING, 0,              OPT_COMPARE_DEST, 0, 0 },
+  {"link-dest",        0,  POPT_ARG_STRING, 0,              OPT_LINK_DEST, 0, 0 },
   /* TODO: Should this take an optional int giving the compression level? */
   {"compress",        'z', POPT_ARG_NONE,   &do_compression, 0, 0, 0 },
   {"daemon",           0,  POPT_ARG_NONE,   &daemon_opt, 0, 0, 0 },
@@ -591,8 +593,36 @@ int parse_arguments(int *argc, const cha
 				select_timeout = io_timeout;
 			break;
 
+		case OPT_COMPARE_DEST:
+#if HAVE_LINK
+			if (num_comp_dest >= MAX_COMP_DEST-1) {
+				rprintf(FERROR, "ERROR: %s\n", "too many --compare-dest args given");
+				return 0;
+			}
+			arg = poptGetOptArg(pc);
+			if (sanitize_paths)
+				arg = alloc_sanitize_path(arg, curr_dir);
+			compare_dest[num_comp_dest++] = (char *)arg;
+			saw_compare_dest = 1;
+			break;
+#else
+			snprintf(err_buf, sizeof err_buf,
+				 "hard links are not supported on this %s\n",
+				 am_server ? "server" : "client");
+			rprintf(FERROR, "ERROR: %s", err_buf);
+			return 0;
+#endif
+
 		case OPT_LINK_DEST:
 #if HAVE_LINK
+			if (num_comp_dest >= MAX_COMP_DEST-1) {
+				rprintf(FERROR, "ERROR: %s\n", "too many --link-dest args given");
+				return 0;
+			}
+			arg = poptGetOptArg(pc);
+			if (sanitize_paths)
+				arg = alloc_sanitize_path(arg, curr_dir);
+			compare_dest[num_comp_dest++] = (char *)arg;
 			link_dest = 1;
 			break;
 #else
@@ -690,6 +720,11 @@ int parse_arguments(int *argc, const cha
 		exit_cleanup(RERR_SYNTAX);
 	}
 
+	if (saw_compare_dest && link_dest) {
+		rprintf(FINFO,
+			"WARNING: promoting --compare-dest options to --link-dest.\n");
+	}
+
 	if (archive_mode) {
 		if (!files_from)
 			recurse = 1;
@@ -718,8 +753,6 @@ int parse_arguments(int *argc, const cha
 			(*argv)[i] = alloc_sanitize_path((*argv)[i], NULL);
 		if (tmpdir)
 			tmpdir = alloc_sanitize_path(tmpdir, curr_dir);
-		if (compare_dest)
-			compare_dest = alloc_sanitize_path(compare_dest, curr_dir);
 		if (backup_dir)
 			backup_dir = alloc_sanitize_path(backup_dir, curr_dir);
 		if (files_from)
@@ -825,8 +858,8 @@ int parse_arguments(int *argc, const cha
  **/
 void server_options(char **args,int *argc)
 {
+	static char argstr[50+MAX_COMP_DEST*2];
 	int ac = *argc;
-	static char argstr[50];
 	char *arg;
 
 	int i, x;
@@ -1004,13 +1037,16 @@ void server_options(char **args,int *arg
 		args[ac++] = tmpdir;
 	}
 
-	if (compare_dest && am_sender) {
+	if (compare_dest[0] && am_sender) {
 		/* the server only needs this option if it is not the sender,
 		 *   and it may be an older version that doesn't know this
 		 *   option, so don't send it if client is the sender.
 		 */
-		args[ac++] = link_dest ? "--link-dest" : "--compare-dest";
-		args[ac++] = compare_dest;
+		int i;
+		for (i = 0; i < num_comp_dest; i++) {
+			args[ac++] = link_dest ? "--link-dest" : "--compare-dest";
+			args[ac++] = compare_dest[i];
+		}
 	}
 
 	if (files_from && (!am_sender || remote_filesfrom_file)) {
--- orig/receiver.c	2004-07-22 00:20:50
+++ receiver.c	2004-07-03 20:14:37
@@ -38,7 +38,6 @@ extern int preserve_perms;
 extern int cvs_exclude;
 extern int io_error;
 extern char *tmpdir;
-extern char *compare_dest;
 extern int make_backups;
 extern int do_progress;
 extern char *backup_dir;
--- orig/rsync.h	2004-07-23 17:16:13
+++ rsync.h	2004-07-03 17:33:30
@@ -98,6 +98,8 @@
 
 #define MAX_ARGS 1000
 
+#define MAX_COMP_DEST 20
+
 #define MPLEX_BASE 7
 
 #define NO_EXCLUDES	0
