Optimized the calling of access() in one loop.
[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.
6
7 --- generator.c 23 Jun 2004 16:51:21 -0000      1.89
8 +++ generator.c 23 Jun 2004 17:29:39 -0000
9 @@ -42,7 +42,7 @@ extern int size_only;
10  extern int io_timeout;
11  extern int protocol_version;
12  extern int always_checksum;
13 -extern char *compare_dest;
14 +extern char *compare_dest[];
15  extern int link_dest;
16  extern int whole_file;
17  extern int local_server;
18 @@ -80,13 +80,12 @@ static int skip_file(char *fname, struct
19         if (always_checksum && S_ISREG(st->st_mode)) {
20                 char sum[MD4_SUM_LENGTH];
21                 char fnamecmpdest[MAXPATHLEN];
22 +               int i;
23  
24 -               if (compare_dest != NULL) {
25 -                       if (access(fname, 0) != 0) {
26 -                               pathjoin(fnamecmpdest, sizeof fnamecmpdest,
27 -                                        compare_dest, fname);
28 -                               fname = fnamecmpdest;
29 -                       }
30 +               for (i = 0; compare_dest[i] != NULL && access(fname, 0) < 0; i++) {
31 +                       pathjoin(fnamecmpdest, sizeof fnamecmpdest,
32 +                                       compare_dest[i], fname);
33 +                       fname = fnamecmpdest;
34                 }
35                 file_checksum(fname,sum,st->st_size);
36                 return memcmp(sum, file->u.sum, protocol_version < 21 ? 2
37 @@ -423,13 +422,18 @@ void recv_generator(char *fname, struct 
38  
39         fnamecmp = fname;
40  
41 -       if (statret == -1 && compare_dest != NULL) {
42 +       if (statret == -1 && compare_dest[0] != NULL) {
43                 /* try the file at compare_dest instead */
44                 int saveerrno = errno;
45 -               pathjoin(fnamecmpbuf, sizeof fnamecmpbuf, compare_dest, fname);
46 -               statret = link_stat(fnamecmpbuf, &st, 0);
47 -               if (!S_ISREG(st.st_mode))
48 -                       statret = -1;
49 +               int i;
50 +               for (i = 0; compare_dest[i] != NULL; i++) {
51 +                       pathjoin(fnamecmpbuf, sizeof fnamecmpbuf, compare_dest[i], fname);
52 +                       if ((statret = link_stat(fnamecmpbuf, &st, 0)) == 0) {
53 +                               if (S_ISREG(st.st_mode))
54 +                                       break;
55 +                               statret = -1;
56 +                       }
57 +               }
58                 if (statret == -1)
59                         errno = saveerrno;
60  #if HAVE_LINK
61 --- options.c   20 Jun 2004 19:47:05 -0000      1.157
62 +++ options.c   23 Jun 2004 17:29:39 -0000
63 @@ -117,7 +117,8 @@ unsigned int backup_dir_remainder;
64  
65  char *backup_suffix = NULL;
66  char *tmpdir = NULL;
67 -char *compare_dest = NULL;
68 +char *compare_dest[MAX_COMP_DEST+1];
69 +int num_comp_dest = 0;
70  char *config_file = NULL;
71  char *shell_cmd = NULL;
72  char *log_format = NULL;
73 @@ -139,6 +140,7 @@ char *batch_prefix = NULL;
74  
75  static int daemon_opt;   /* sets am_daemon after option error-reporting */
76  static int modify_window_set;
77 +static int saw_compare_dest = 0;
78  
79  /** Local address to bind.  As a character string because it's
80   * interpreted by the IPv6 layer: should be a numeric IP4 or IP6
81 @@ -308,7 +310,7 @@ void usage(enum logcode F)
82  }
83  
84  enum {OPT_VERSION = 1000, OPT_SENDER, OPT_EXCLUDE, OPT_EXCLUDE_FROM,
85 -      OPT_DELETE_AFTER, OPT_DELETE_EXCLUDED, OPT_LINK_DEST,
86 +      OPT_DELETE_AFTER, OPT_DELETE_EXCLUDED, OPT_COMPARE_DEST, OPT_LINK_DEST,
87        OPT_INCLUDE, OPT_INCLUDE_FROM, OPT_MODIFY_WINDOW,
88        OPT_READ_BATCH, OPT_WRITE_BATCH, OPT_TIMEOUT,
89        OPT_REFUSED_BASE = 9000};
90 @@ -366,8 +368,8 @@ static struct poptOption long_options[] 
91    {"max-delete",       0,  POPT_ARG_INT,    &max_delete, 0, 0, 0 },
92    {"timeout",          0,  POPT_ARG_INT,    &io_timeout, OPT_TIMEOUT, 0, 0 },
93    {"temp-dir",        'T', POPT_ARG_STRING, &tmpdir, 0, 0, 0 },
94 -  {"compare-dest",     0,  POPT_ARG_STRING, &compare_dest, 0, 0, 0 },
95 -  {"link-dest",        0,  POPT_ARG_STRING, &compare_dest,  OPT_LINK_DEST, 0, 0 },
96 +  {"compare-dest",     0,  POPT_ARG_STRING, 0,              OPT_COMPARE_DEST, 0, 0 },
97 +  {"link-dest",        0,  POPT_ARG_STRING, 0,              OPT_LINK_DEST, 0, 0 },
98    /* TODO: Should this take an optional int giving the compression level? */
99    {"compress",        'z', POPT_ARG_NONE,   &do_compression, 0, 0, 0 },
100    {"daemon",           0,  POPT_ARG_NONE,   &daemon_opt, 0, 0, 0 },
101 @@ -585,8 +587,36 @@ int parse_arguments(int *argc, const cha
102                                 select_timeout = io_timeout;
103                         break;
104  
105 +               case OPT_COMPARE_DEST:
106 +#if HAVE_LINK
107 +                       if (num_comp_dest >= MAX_COMP_DEST-1) {
108 +                               rprintf(FERROR, "ERROR: %s\n", "too many --compare-dest args given");
109 +                               return 0;
110 +                       }
111 +                       arg = poptGetOptArg(pc);
112 +                       if (sanitize_paths)
113 +                               arg = alloc_sanitize_path(arg, curr_dir);
114 +                       compare_dest[num_comp_dest++] = (char *)arg;
115 +                       saw_compare_dest = 1;
116 +                       break;
117 +#else
118 +                       snprintf(err_buf, sizeof err_buf,
119 +                                "hard links are not supported on this %s\n",
120 +                                am_server ? "server" : "client");
121 +                       rprintf(FERROR, "ERROR: %s", err_buf);
122 +                       return 0;
123 +#endif
124 +
125                 case OPT_LINK_DEST:
126  #if HAVE_LINK
127 +                       if (num_comp_dest >= MAX_COMP_DEST-1) {
128 +                               rprintf(FERROR, "ERROR: %s\n", "too many --compare-dest args given");
129 +                               return 0;
130 +                       }
131 +                       arg = poptGetOptArg(pc);
132 +                       if (sanitize_paths)
133 +                               arg = alloc_sanitize_path(arg, curr_dir);
134 +                       compare_dest[num_comp_dest++] = (char *)arg;
135                         link_dest = 1;
136                         break;
137  #else
138 @@ -661,6 +691,11 @@ int parse_arguments(int *argc, const cha
139                 exit_cleanup(RERR_SYNTAX);
140         }
141  
142 +       if (saw_compare_dest && link_dest) {
143 +               rprintf(FINFO,
144 +                       "WARNING: promoting --compare-dest options to --link-dest.\n");
145 +       }
146 +
147         if (archive_mode) {
148                 if (!files_from)
149                         recurse = 1;
150 @@ -689,8 +724,6 @@ int parse_arguments(int *argc, const cha
151                         (*argv)[i] = alloc_sanitize_path((*argv)[i], NULL);
152                 if (tmpdir)
153                         tmpdir = alloc_sanitize_path(tmpdir, curr_dir);
154 -               if (compare_dest)
155 -                       compare_dest = alloc_sanitize_path(compare_dest, curr_dir);
156                 if (backup_dir)
157                         backup_dir = alloc_sanitize_path(backup_dir, curr_dir);
158                 if (files_from)
159 @@ -785,8 +818,8 @@ int parse_arguments(int *argc, const cha
160   **/
161  void server_options(char **args,int *argc)
162  {
163 +       static char argstr[50+MAX_COMP_DEST*2];
164         int ac = *argc;
165 -       static char argstr[50];
166         char *arg;
167  
168         int i, x;
169 @@ -968,13 +1001,16 @@ void server_options(char **args,int *arg
170                 args[ac++] = tmpdir;
171         }
172  
173 -       if (compare_dest && am_sender) {
174 +       if (compare_dest[0] && am_sender) {
175                 /* the server only needs this option if it is not the sender,
176                  *   and it may be an older version that doesn't know this
177                  *   option, so don't send it if client is the sender.
178                  */
179 -               args[ac++] = link_dest ? "--link-dest" : "--compare-dest";
180 -               args[ac++] = compare_dest;
181 +               int i;
182 +               for (i = 0; i < num_comp_dest; i++) {
183 +                       args[ac++] = link_dest ? "--link-dest" : "--compare-dest";
184 +                       args[ac++] = compare_dest[i];
185 +               }
186         }
187  
188         if (files_from && (!am_sender || remote_filesfrom_file)) {
189 --- receiver.c  14 Jun 2004 15:09:36 -0000      1.82
190 +++ receiver.c  23 Jun 2004 17:29:39 -0000
191 @@ -36,7 +36,7 @@ extern int preserve_perms;
192  extern int cvs_exclude;
193  extern int io_error;
194  extern char *tmpdir;
195 -extern char *compare_dest;
196 +extern char *compare_dest[];
197  extern int make_backups;
198  extern int do_progress;
199  extern char *backup_dir;
200 @@ -306,7 +306,7 @@ int recv_files(int f_in,struct file_list
201         struct file_struct *file;
202         struct stats initial_stats;
203         int save_make_backups = make_backups;
204 -       int i, recv_ok, phase = 0;
205 +       int i, j, recv_ok, phase = 0;
206  
207         if (verbose > 2) {
208                 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
209 @@ -369,10 +369,10 @@ int recv_files(int f_in,struct file_list
210                 /* open the file */
211                 fd1 = do_open(fnamecmp, O_RDONLY, 0);
212  
213 -               if (fd1 == -1 && compare_dest != NULL) {
214 +               for (j = 0; fd1 == -1 && compare_dest[j] != NULL; j++) {
215                         /* try the file at compare_dest instead */
216                         pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
217 -                                compare_dest, fname);
218 +                                compare_dest[j], fname);
219                         fnamecmp = fnamecmpbuf;
220                         fd1 = do_open(fnamecmp, O_RDONLY, 0);
221                 }
222 --- rsync.h     16 May 2004 07:28:24 -0000      1.204
223 +++ rsync.h     23 Jun 2004 17:29:40 -0000
224 @@ -98,6 +98,8 @@
225  
226  #define MAX_ARGS 1000
227  
228 +#define MAX_COMP_DEST 20
229 +
230  #define MPLEX_BASE 7
231  
232  #define NO_EXCLUDES    0