Fixed a failing hunk.
[rsync/rsync-patches.git] / compare-dest.diff
1 This patch allows multiple --compare-dest or --link-dest options to be
2 used, making the transfer of some files more optimal.  Note that the
3 algorithm does NOT search for the best match -- it stops at the first
4 match and uses that as the basis file for the transfer, so be sure to
5 order your arguments appropriately (the args are searched in the order
6 they are suppled).
7
8 Before compiling, be sure to run "make proto".
9
10 --- orig/generator.c    2004-11-11 22:13:09
11 +++ generator.c 2004-11-03 22:47:23
12 @@ -46,7 +46,7 @@ extern int io_timeout;
13  extern int protocol_version;
14  extern int always_checksum;
15  extern char *partial_dir;
16 -extern char *compare_dest;
17 +extern char *compare_dest[];
18  extern int link_dest;
19  extern int whole_file;
20  extern int local_server;
21 @@ -429,11 +429,19 @@ static void recv_generator(char *fname, 
22         fnamecmp = fname;
23         fnamecmp_type = FNAMECMP_FNAME;
24  
25 -       if (statret == -1 && compare_dest != NULL) {
26 +       if (statret == -1 && compare_dest[0] != NULL) {
27                 /* try the file at compare_dest instead */
28 -               pathjoin(fnamecmpbuf, sizeof fnamecmpbuf, compare_dest, fname);
29 -               if (link_stat(fnamecmpbuf, &st, 0) == 0
30 -                   && S_ISREG(st.st_mode)) {
31 +               int i = 0;
32 +               do {
33 +                       pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
34 +                                compare_dest[i], fname);
35 +                       if (link_stat(fnamecmpbuf, &st, 0) == 0
36 +                           && S_ISREG(st.st_mode)) {
37 +                               statret = 0;
38 +                               break;
39 +                       }
40 +               } while (compare_dest[++i] != NULL);
41 +               if (statret == 0) {
42  #if HAVE_LINK
43                         if (link_dest && !dry_run) {
44                                 if (do_link(fnamecmpbuf, fname) < 0) {
45 @@ -444,15 +452,14 @@ static void recv_generator(char *fname, 
46                                                         safe_fname(fname));
47                                         }
48                                         fnamecmp = fnamecmpbuf;
49 -                                       fnamecmp_type = FNAMECMP_CMPDEST;
50 +                                       fnamecmp_type = FNAMECMP_CMPDEST + i;
51                                 }
52                         } else
53  #endif
54                         {
55                                 fnamecmp = fnamecmpbuf;
56 -                               fnamecmp_type = FNAMECMP_CMPDEST;
57 +                               fnamecmp_type = FNAMECMP_CMPDEST + i;
58                         }
59 -                       statret = 0;
60                 }
61         }
62  
63 --- orig/main.c 2004-11-03 20:30:45
64 +++ main.c      2004-07-30 07:23:54
65 @@ -59,7 +59,7 @@ extern int filesfrom_fd;
66  extern pid_t cleanup_child_pid;
67  extern char *files_from;
68  extern char *remote_filesfrom_file;
69 -extern char *compare_dest;
70 +extern char *compare_dest[];
71  extern char *rsync_path;
72  extern char *shell_cmd;
73  extern char *batch_name;
74 @@ -463,7 +463,7 @@ static int do_recv(int f_in,int f_out,st
75         int pid;
76         int status = 0;
77         int error_pipe[2], name_pipe[2];
78 -       BOOL need_name_pipe = compare_dest && !dry_run;
79 +       BOOL need_name_pipe = compare_dest[0] && !dry_run;
80  
81         /* The receiving side mustn't obey this, or an existing symlink that
82          * points to an identical file won't be replaced by the referent. */
83 --- orig/options.c      2004-11-11 22:13:09
84 +++ options.c   2004-11-11 22:14:34
85 @@ -115,12 +115,13 @@ int write_batch = 0;
86  int read_batch = 0;
87  int backup_dir_len = 0;
88  int backup_suffix_len;
89 +int num_comp_dest = 0;
90  unsigned int backup_dir_remainder;
91  
92  char *backup_suffix = NULL;
93  char *tmpdir = NULL;
94  char *partial_dir = NULL;
95 -char *compare_dest = NULL;
96 +char *compare_dest[MAX_COMP_DEST+1];
97  char *config_file = NULL;
98  char *shell_cmd = NULL;
99  char *log_format = NULL;
100 @@ -141,6 +142,7 @@ char *batch_name = NULL;
101  
102  static int daemon_opt;   /* sets am_daemon after option error-reporting */
103  static int modify_window_set;
104 +static int saw_compare_dest = 0;
105  static char *max_size_arg;
106  
107  /** Local address to bind.  As a character string because it's
108 @@ -313,7 +315,7 @@ void usage(enum logcode F)
109  }
110  
111  enum {OPT_VERSION = 1000, OPT_DAEMON, OPT_SENDER, OPT_EXCLUDE, OPT_EXCLUDE_FROM,
112 -      OPT_DELETE_AFTER, OPT_DELETE_EXCLUDED, OPT_LINK_DEST,
113 +      OPT_DELETE_AFTER, OPT_DELETE_EXCLUDED, OPT_COMPARE_DEST, OPT_LINK_DEST,
114        OPT_INCLUDE, OPT_INCLUDE_FROM, OPT_MODIFY_WINDOW,
115        OPT_READ_BATCH, OPT_WRITE_BATCH, OPT_TIMEOUT, OPT_MAX_SIZE,
116        OPT_REFUSED_BASE = 9000};
117 @@ -373,8 +375,8 @@ static struct poptOption long_options[] 
118    {"max-size",         0,  POPT_ARG_STRING, &max_size_arg,  OPT_MAX_SIZE, 0, 0 },
119    {"timeout",          0,  POPT_ARG_INT,    &io_timeout, OPT_TIMEOUT, 0, 0 },
120    {"temp-dir",        'T', POPT_ARG_STRING, &tmpdir, 0, 0, 0 },
121 -  {"compare-dest",     0,  POPT_ARG_STRING, &compare_dest, 0, 0, 0 },
122 -  {"link-dest",        0,  POPT_ARG_STRING, &compare_dest, OPT_LINK_DEST, 0, 0 },
123 +  {"compare-dest",     0,  POPT_ARG_STRING, 0, OPT_COMPARE_DEST, 0, 0 },
124 +  {"link-dest",        0,  POPT_ARG_STRING, 0, OPT_LINK_DEST, 0, 0 },
125    /* TODO: Should this take an optional int giving the compression level? */
126    {"compress",        'z', POPT_ARG_NONE,   &do_compression, 0, 0, 0 },
127    {"stats",            0,  POPT_ARG_NONE,   &do_stats, 0, 0, 0 },
128 @@ -705,8 +707,28 @@ int parse_arguments(int *argc, const cha
129                                 select_timeout = io_timeout;
130                         break;
131  
132 +               case OPT_COMPARE_DEST:
133 +                       if (num_comp_dest >= MAX_COMP_DEST-1) {
134 +                               rprintf(FERROR, "ERROR: %s\n", "too many --compare-dest args given");
135 +                               return 0;
136 +                       }
137 +                       arg = poptGetOptArg(pc);
138 +                       if (sanitize_paths)
139 +                               arg = sanitize_path(NULL, arg, NULL, 0);
140 +                       compare_dest[num_comp_dest++] = (char *)arg;
141 +                       saw_compare_dest = 1;
142 +                       break;
143 +
144                 case OPT_LINK_DEST:
145  #if HAVE_LINK
146 +                       if (num_comp_dest >= MAX_COMP_DEST-1) {
147 +                               rprintf(FERROR, "ERROR: %s\n", "too many --link-dest args given");
148 +                               return 0;
149 +                       }
150 +                       arg = poptGetOptArg(pc);
151 +                       if (sanitize_paths)
152 +                               arg = sanitize_path(NULL, arg, NULL, 0);
153 +                       compare_dest[num_comp_dest++] = (char *)arg;
154                         link_dest = 1;
155                         break;
156  #else
157 @@ -798,6 +820,11 @@ int parse_arguments(int *argc, const cha
158                 return 0;
159         }
160  
161 +       if (saw_compare_dest && link_dest) {
162 +               rprintf(FINFO,
163 +                       "WARNING: promoting --compare-dest options to --link-dest.\n");
164 +       }
165 +
166         if (archive_mode) {
167                 if (!files_from)
168                         recurse = 1;
169 @@ -825,8 +852,6 @@ int parse_arguments(int *argc, const cha
170                         tmpdir = sanitize_path(NULL, tmpdir, NULL, 0);
171                 if (partial_dir)
172                         partial_dir = sanitize_path(NULL, partial_dir, NULL, 0);
173 -               if (compare_dest)
174 -                       compare_dest = sanitize_path(NULL, compare_dest, NULL, 0);
175                 if (backup_dir)
176                         backup_dir = sanitize_path(NULL, backup_dir, NULL, 0);
177                 if (files_from)
178 @@ -834,6 +859,7 @@ int parse_arguments(int *argc, const cha
179         }
180         if (server_exclude_list.head && !am_sender) {
181                 struct exclude_list_struct *elp = &server_exclude_list;
182 +               int i;
183                 if (tmpdir) {
184                         clean_fname(tmpdir, 1);
185                         if (check_exclude(elp, tmpdir, 1) < 0)
186 @@ -844,9 +870,9 @@ int parse_arguments(int *argc, const cha
187                         if (check_exclude(elp, partial_dir, 1) < 0)
188                                 goto options_rejected;
189                 }
190 -               if (compare_dest) {
191 -                       clean_fname(compare_dest, 1);
192 -                       if (check_exclude(elp, compare_dest, 1) < 0)
193 +               for (i = 0; i < num_comp_dest; i++) {
194 +                       clean_fname(compare_dest[i], 1);
195 +                       if (check_exclude(elp, compare_dest[i], 1) < 0)
196                                 goto options_rejected;
197                 }
198                 if (backup_dir) {
199 @@ -917,7 +943,7 @@ int parse_arguments(int *argc, const cha
200                          am_server ? "server" : "client");
201                 return 0;
202  #endif
203 -               if (compare_dest) {
204 +               if (saw_compare_dest || link_dest) {
205                         snprintf(err_buf, sizeof err_buf,
206                                  "--inplace does not yet work with %s\n",
207                                  link_dest ? "--link-dest" : "--compare-dest");
208 @@ -984,8 +1010,8 @@ int parse_arguments(int *argc, const cha
209   **/
210  void server_options(char **args,int *argc)
211  {
212 +       static char argstr[50+MAX_COMP_DEST*2];
213         int ac = *argc;
214 -       static char argstr[50];
215         char *arg;
216  
217         int i, x;
218 @@ -1173,13 +1199,16 @@ void server_options(char **args,int *arg
219                 args[ac++] = tmpdir;
220         }
221  
222 -       if (compare_dest && am_sender) {
223 +       if (compare_dest[0] && am_sender) {
224                 /* the server only needs this option if it is not the sender,
225                  *   and it may be an older version that doesn't know this
226                  *   option, so don't send it if client is the sender.
227                  */
228 -               args[ac++] = link_dest ? "--link-dest" : "--compare-dest";
229 -               args[ac++] = compare_dest;
230 +               int i;
231 +               for (i = 0; i < num_comp_dest; i++) {
232 +                       args[ac++] = link_dest ? "--link-dest" : "--compare-dest";
233 +                       args[ac++] = compare_dest[i];
234 +               }
235         }
236  
237         if (files_from && (!am_sender || remote_filesfrom_file)) {
238 --- orig/receiver.c     2004-11-03 20:30:45
239 +++ receiver.c  2004-11-03 22:50:43
240 @@ -39,7 +39,7 @@ extern int cvs_exclude;
241  extern int io_error;
242  extern char *tmpdir;
243  extern char *partial_dir;
244 -extern char *compare_dest;
245 +extern char *compare_dest[];
246  extern int make_backups;
247  extern int do_progress;
248  extern char *backup_dir;
249 @@ -439,7 +439,8 @@ int recv_files(int f_in, struct file_lis
250                 partialptr = partial_dir ? partial_dir_fname(fname) : fname;
251  
252                 if (f_in_name >= 0) {
253 -                       switch (read_byte(f_in_name)) {
254 +                       uchar j;
255 +                       switch (j = read_byte(f_in_name)) {
256                         case FNAMECMP_FNAME:
257                                 fnamecmp = fname;
258                                 break;
259 @@ -452,7 +453,7 @@ int recv_files(int f_in, struct file_lis
260                         case FNAMECMP_CMPDEST:
261                         default:
262                                 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
263 -                                        compare_dest, fname);
264 +                                        compare_dest[j], fname);
265                                 fnamecmp = fnamecmpbuf;
266                                 break;
267                         }
268 --- orig/rsync.h        2004-11-03 20:30:45
269 +++ rsync.h     2004-07-30 07:23:54
270 @@ -98,6 +98,8 @@
271  
272  #define MAX_ARGS 1000
273  
274 +#define MAX_COMP_DEST 20
275 +
276  #define MPLEX_BASE 7
277  
278  #define NO_EXCLUDES    0