1 This patch to rsync adds a %C log escape that expands to the sender's
2 post-transfer checksum of a file for protocol 30 or above. This way, if
3 you need the MD5 checksums of transferred files, you can have rsync log
4 them instead of spending extra processor time on a separate command to
7 -- Matt McCutchen <hashproduct@gmail.com>
11 @@ -55,6 +55,9 @@ extern char curr_dir[];
12 extern char *module_dir;
13 extern unsigned int module_dirlen;
15 +extern char sender_file_sum[MAX_DIGEST_LEN];
16 +extern int file_sum_len;
18 static int log_initialised;
19 static int logfile_was_closed;
20 static FILE *logfile_fp;
21 @@ -610,6 +613,19 @@ static void log_formatted(enum logcode c
22 snprintf(buf2, sizeof buf2, fmt, (double)b);
26 + if (iflags & ITEM_TRANSFER && protocol_version >= 30) {
28 + for (i = 0; i < file_sum_len; i++)
29 + snprintf(buf2 + i*2, 3, "%02x", (int)CVAL(sender_file_sum,i));
32 + for (i = 0; i < file_sum_len*2; i++)
39 if (iflags & ITEM_DELETED) {
43 @@ -286,6 +286,10 @@ static void hash_search(int f,struct sum
44 map_ptr(buf, len-1, 1);
47 +/* Global variables to make the sender's checksum of a transferred file
48 + * available to the code for log escape %C. */
49 +char sender_file_sum[MAX_DIGEST_LEN];
50 +int file_sum_len = MD5_DIGEST_LEN;
53 * Scan through a origin file, looking for sections that match
54 @@ -303,9 +307,6 @@ static void hash_search(int f,struct sum
56 void match_sums(int f, struct sum_struct *s, struct map_struct *buf, OFF_T len)
58 - char file_sum[MAX_DIGEST_LEN];
64 @@ -353,14 +354,26 @@ void match_sums(int f, struct sum_struct
65 matched(f, s, buf, len, -1);
68 - sum_len = sum_end(file_sum);
69 - /* If we had a read error, send a bad checksum. */
70 - if (buf && buf->status != 0)
72 + file_sum_len = sum_end(sender_file_sum);
74 + /* If we had a read error, send a bad checksum. We use all bits
75 + * off or all bits on so that a user logging checksums with %C
76 + * can recognize a bad checksum. */
77 + if (buf && buf->status != 0) {
79 + for (i = 0; i < file_sum_len; i++) {
80 + if (sender_file_sum[i])
83 + if (i < file_sum_len)
84 + memset(sender_file_sum, 0, file_sum_len);
86 + memset(sender_file_sum, 0xFF, file_sum_len);
90 rprintf(FINFO,"sending file_sum\n");
91 - write_buf(f, file_sum, sum_len);
92 + write_buf(f, sender_file_sum, file_sum_len);
95 rprintf(FINFO, "false_alarms=%d hash_hits=%d matches=%d\n",
98 @@ -1439,7 +1439,8 @@ int parse_arguments(int *argc, const cha
99 else if (log_format_has(stdout_format, 'i'))
100 stdout_format_has_i = itemize_changes | 1;
101 if (!log_format_has(stdout_format, 'b')
102 - && !log_format_has(stdout_format, 'c'))
103 + && !log_format_has(stdout_format, 'c')
104 + && !log_format_has(stdout_format, 'C'))
105 log_before_transfer = !am_server;
106 } else if (itemize_changes) {
107 stdout_format = "%i %n%L";
110 @@ -62,6 +62,9 @@ static int phase = 0, redoing = 0;
111 /* We're either updating the basis file or an identical copy: */
112 static int updating_basis;
114 +extern char sender_file_sum[MAX_DIGEST_LEN];
115 +extern int file_sum_len;
118 * get_tmpname() - create a tmp filename for a given filename
120 @@ -128,10 +131,9 @@ static int receive_data(int f_in, char *
121 const char *fname, int fd, OFF_T total_size)
123 static char file_sum1[MAX_DIGEST_LEN];
124 - static char file_sum2[MAX_DIGEST_LEN];
125 struct map_struct *mapbuf;
126 struct sum_struct sum;
127 - int32 len, sum_len;
132 @@ -261,15 +263,15 @@ static int receive_data(int f_in, char *
133 exit_cleanup(RERR_FILEIO);
136 - sum_len = sum_end(file_sum1);
137 + file_sum_len = sum_end(file_sum1);
142 - read_buf(f_in, file_sum2, sum_len);
143 + read_buf(f_in, sender_file_sum, file_sum_len);
145 rprintf(FINFO,"got file_sum\n");
146 - if (fd != -1 && memcmp(file_sum1, file_sum2, sum_len) != 0)
147 + if (fd != -1 && memcmp(file_sum1, sender_file_sum, file_sum_len) != 0)
153 @@ -1970,7 +1970,7 @@ by the server and defaults to the curren
154 is used to set a specific checksum seed, which is useful for
155 applications that want repeatable block and file checksums, or
156 in the case where the user wants a more random checksum seed.
157 -Note that setting NUM to 0 causes rsync to use the default of code(time())
158 +Setting NUM to 0 causes rsync to use the default of code(time())
162 --- old/rsyncd.conf.yo
163 +++ new/rsyncd.conf.yo
164 @@ -421,7 +421,8 @@ quote(itemization(
165 it() %a the remote IP address
166 it() %b the number of bytes actually transferred
167 it() %B the permission bits of the file (e.g. rwxrwxrwt)
168 - it() %c the checksum bytes received for this file (only when sending)
169 + it() %c the total size of the block checksums received for the basis file (only when sending)
170 + it() %C the full-file MD5 checksum of a transferred file (only for protocol 30 or above).
171 it() %f the filename (long form on sender; no trailing "/")
172 it() %G the gid of the file (decimal) or "DEFAULT"
173 it() %h the remote host name