- A slight improvement to the option-parsing code.
[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-25 16:32:40
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-17 19:41:31
84 +++ options.c   2004-11-25 16:38:16
85 @@ -116,12 +116,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 @@ -142,6 +143,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 @@ -314,7 +316,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 @@ -374,8 +376,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 @@ -712,7 +714,7 @@ int parse_arguments(int *argc, const cha
129                 case OPT_LINK_DEST:
130  #if HAVE_LINK
131                         link_dest = 1;
132 -                       break;
133 +                       goto compare_dest;
134  #else
135                         snprintf(err_buf, sizeof err_buf,
136                                  "hard links are not supported on this %s\n",
137 @@ -720,6 +722,20 @@ int parse_arguments(int *argc, const cha
138                         return 0;
139  #endif
140  
141 +               case OPT_COMPARE_DEST:
142 +                       saw_compare_dest = 1;
143 +               compare_dest:
144 +                       if (num_comp_dest >= MAX_COMP_DEST-1) {
145 +                               rprintf(FERROR, "ERROR: too many %s args given\n",
146 +                                       link_dest ? "--link-dest" : "--compare-dest");
147 +                               return 0;
148 +                       }
149 +                       arg = poptGetOptArg(pc);
150 +                       if (sanitize_paths)
151 +                               arg = sanitize_path(NULL, arg, NULL, 0);
152 +                       compare_dest[num_comp_dest++] = (char *)arg;
153 +                       break;
154 +
155                 default:
156                         /* A large opt value means that set_refuse_options()
157                          * turned this option off (opt-BASE is its index). */
158 @@ -802,6 +818,11 @@ int parse_arguments(int *argc, const cha
159                 return 0;
160         }
161  
162 +       if (saw_compare_dest && link_dest) {
163 +               rprintf(FINFO,
164 +                       "WARNING: promoting --compare-dest options to --link-dest.\n");
165 +       }
166 +
167         if (archive_mode) {
168                 if (!files_from)
169                         recurse = 1;
170 @@ -829,8 +850,6 @@ int parse_arguments(int *argc, const cha
171                         tmpdir = sanitize_path(NULL, tmpdir, NULL, 0);
172                 if (partial_dir)
173                         partial_dir = sanitize_path(NULL, partial_dir, NULL, 0);
174 -               if (compare_dest)
175 -                       compare_dest = sanitize_path(NULL, compare_dest, NULL, 0);
176                 if (backup_dir)
177                         backup_dir = sanitize_path(NULL, backup_dir, NULL, 0);
178                 if (files_from)
179 @@ -838,6 +857,7 @@ int parse_arguments(int *argc, const cha
180         }
181         if (server_exclude_list.head && !am_sender) {
182                 struct exclude_list_struct *elp = &server_exclude_list;
183 +               int i;
184                 if (tmpdir) {
185                         clean_fname(tmpdir, 1);
186                         if (check_exclude(elp, tmpdir, 1) < 0)
187 @@ -848,9 +868,9 @@ int parse_arguments(int *argc, const cha
188                         if (check_exclude(elp, partial_dir, 1) < 0)
189                                 goto options_rejected;
190                 }
191 -               if (compare_dest) {
192 -                       clean_fname(compare_dest, 1);
193 -                       if (check_exclude(elp, compare_dest, 1) < 0)
194 +               for (i = 0; i < num_comp_dest; i++) {
195 +                       clean_fname(compare_dest[i], 1);
196 +                       if (check_exclude(elp, compare_dest[i], 1) < 0)
197                                 goto options_rejected;
198                 }
199                 if (backup_dir) {
200 @@ -923,7 +943,7 @@ int parse_arguments(int *argc, const cha
201                          am_server ? "server" : "client");
202                 return 0;
203  #endif
204 -               if (compare_dest) {
205 +               if (saw_compare_dest || link_dest) {
206                         snprintf(err_buf, sizeof err_buf,
207                                  "--inplace does not yet work with %s\n",
208                                  link_dest ? "--link-dest" : "--compare-dest");
209 @@ -990,8 +1010,8 @@ int parse_arguments(int *argc, const cha
210   **/
211  void server_options(char **args,int *argc)
212  {
213 +       static char argstr[50+MAX_COMP_DEST*2];
214         int ac = *argc;
215 -       static char argstr[50];
216         char *arg;
217  
218         int i, x;
219 @@ -1179,13 +1199,16 @@ void server_options(char **args,int *arg
220                 args[ac++] = tmpdir;
221         }
222  
223 -       if (compare_dest && am_sender) {
224 +       if (compare_dest[0] && am_sender) {
225                 /* the server only needs this option if it is not the sender,
226                  *   and it may be an older version that doesn't know this
227                  *   option, so don't send it if client is the sender.
228                  */
229 -               args[ac++] = link_dest ? "--link-dest" : "--compare-dest";
230 -               args[ac++] = compare_dest;
231 +               int i;
232 +               for (i = 0; i < num_comp_dest; i++) {
233 +                       args[ac++] = link_dest ? "--link-dest" : "--compare-dest";
234 +                       args[ac++] = compare_dest[i];
235 +               }
236         }
237  
238         if (files_from && (!am_sender || remote_filesfrom_file)) {
239 --- orig/receiver.c     2004-11-03 20:30:45
240 +++ receiver.c  2004-11-03 22:50:43
241 @@ -39,7 +39,7 @@ extern int cvs_exclude;
242  extern int io_error;
243  extern char *tmpdir;
244  extern char *partial_dir;
245 -extern char *compare_dest;
246 +extern char *compare_dest[];
247  extern int make_backups;
248  extern int do_progress;
249  extern char *backup_dir;
250 @@ -439,7 +439,8 @@ int recv_files(int f_in, struct file_lis
251                 partialptr = partial_dir ? partial_dir_fname(fname) : fname;
252  
253                 if (f_in_name >= 0) {
254 -                       switch (read_byte(f_in_name)) {
255 +                       uchar j;
256 +                       switch (j = read_byte(f_in_name)) {
257                         case FNAMECMP_FNAME:
258                                 fnamecmp = fname;
259                                 break;
260 @@ -452,7 +453,7 @@ int recv_files(int f_in, struct file_lis
261                         case FNAMECMP_CMPDEST:
262                         default:
263                                 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
264 -                                        compare_dest, fname);
265 +                                        compare_dest[j], fname);
266                                 fnamecmp = fnamecmpbuf;
267                                 break;
268                         }
269 --- orig/rsync.h        2004-11-03 20:30:45
270 +++ rsync.h     2004-07-30 07:23:54
271 @@ -98,6 +98,8 @@
272  
273  #define MAX_ARGS 1000
274  
275 +#define MAX_COMP_DEST 20
276 +
277  #define MPLEX_BASE 7
278  
279  #define NO_EXCLUDES    0
280 --- orig/testsuite/compare-dest.test    2004-07-23 17:16:13
281 +++ testsuite/compare-dest.test 2004-11-25 16:56:33
282 @@ -11,22 +11,25 @@
283  
284  set -x
285  
286 -altdir="$tmpdir/alt"
287 +alt1dir="$tmpdir/alt1"
288 +alt2dir="$tmpdir/alt2"
289  
290  # Build some files/dirs/links to copy
291  
292  hands_setup
293  
294  # Setup the alt and chk dirs
295 -$RSYNC -av --include=text --include='*/' --exclude='*' "$fromdir/" "$altdir/"
296 +$RSYNC -av --include=text --include='*/' --exclude='*' "$fromdir/" "$alt1dir/"
297 +$RSYNC -av --include=etc-ltr-list --include='*/' --exclude='*' "$fromdir/" "$alt2dir/"
298  
299  sleep 1
300  touch "$fromdir/dir/text"
301  
302 -$RSYNC -av --exclude=/text "$fromdir/" "$chkdir/"
303 +$RSYNC -av --exclude=/text --exclude=etc-ltr-list "$fromdir/" "$chkdir/"
304  
305  # Let's do it!
306 -checkit "$RSYNC -avv --no-whole-file --compare-dest=\"$altdir\" \
307 +checkit "$RSYNC -avv --no-whole-file \
308 +    --compare-dest=\"$alt1dir\" --compare-dest=\"$alt2dir\" \
309      \"$fromdir/\" \"$todir/\"" "$chkdir" "$todir"
310  
311  # The script would have aborted on error, so getting here means we've won.