blob: b7ebbe1f133af8790c3be5514f01ebf80931a103 [file] [log] [blame]
J.W. Schultze6e3f122003-08-15 00:57:27 +00001/*
Andrew Tridgell2f03f951998-07-25 02:25:22 +00002 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
J.W. Schultze6e3f122003-08-15 00:57:27 +00004
Andrew Tridgell2f03f951998-07-25 02:25:22 +00005 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
J.W. Schultze6e3f122003-08-15 00:57:27 +00009
Andrew Tridgell2f03f951998-07-25 02:25:22 +000010 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
J.W. Schultze6e3f122003-08-15 00:57:27 +000014
Andrew Tridgell2f03f951998-07-25 02:25:22 +000015 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18*/
19
20#include "rsync.h"
21
22extern int verbose;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000023extern int csum_length;
24extern struct stats stats;
25extern int io_error;
26extern int dry_run;
27extern int am_server;
28
29
Martin Pool0f9c48b2002-04-08 08:34:03 +000030/**
31 * @file
32 *
33 * The sender gets checksums from the generator, calculates deltas,
34 * and transmits them to the receiver. The sender process runs on the
35 * machine holding the source files.
36 **/
37
38
J.W. Schultzfc0257c2003-04-10 01:13:30 +000039void read_sum_head(int f, struct sum_struct *sum)
40{
Wayne Davisond04e9c52003-09-09 15:58:48 +000041 extern int protocol_version;
J.W. Schultzfc0257c2003-04-10 01:13:30 +000042
43 sum->count = read_int(f);
44 sum->blength = read_int(f);
J.W. Schultzdfad66a2003-12-06 21:35:34 +000045 if (protocol_version < 27) {
J.W. Schultzfc0257c2003-04-10 01:13:30 +000046 sum->s2length = csum_length;
J.W. Schultzdfad66a2003-12-06 21:35:34 +000047 } else {
J.W. Schultze6e3f122003-08-15 00:57:27 +000048 sum->s2length = read_int(f);
J.W. Schultzdfad66a2003-12-06 21:35:34 +000049 if (sum->s2length > MD4_SUM_LENGTH) {
Wayne Davisonef5075e2003-12-19 22:19:54 +000050 rprintf(FERROR, "Invalid checksum length %ld\n",
51 (long)sum->s2length);
J.W. Schultzdfad66a2003-12-06 21:35:34 +000052 exit_cleanup(RERR_PROTOCOL);
53 }
54 }
J.W. Schultze6e3f122003-08-15 00:57:27 +000055 sum->remainder = read_int(f);
J.W. Schultzfc0257c2003-04-10 01:13:30 +000056}
57
Martin Poolce8149b2002-04-09 06:03:50 +000058/**
59 * Receive the checksums for a buffer
60 **/
Andrew Tridgell2f03f951998-07-25 02:25:22 +000061static struct sum_struct *receive_sums(int f)
62{
63 struct sum_struct *s;
64 int i;
65 OFF_T offset = 0;
66
Wayne Davison58cadc82003-12-06 21:07:27 +000067 s = new(struct sum_struct);
Andrew Tridgell2f03f951998-07-25 02:25:22 +000068 if (!s) out_of_memory("receive_sums");
69
J.W. Schultzfc0257c2003-04-10 01:13:30 +000070 read_sum_head(f, s);
71
Andrew Tridgell2f03f951998-07-25 02:25:22 +000072 s->sums = NULL;
73
Wayne Davisonda9d12f2004-01-03 18:37:41 +000074 if (verbose > 3) {
75 rprintf(FINFO, "count=%ld n=%u rem=%u\n",
76 (long)s->count, s->blength, s->remainder);
77 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +000078
J.W. Schultze6e3f122003-08-15 00:57:27 +000079 if (s->count == 0)
Andrew Tridgell2f03f951998-07-25 02:25:22 +000080 return(s);
81
Wayne Davison58cadc82003-12-06 21:07:27 +000082 s->sums = new_array(struct sum_buf, s->count);
Andrew Tridgell2f03f951998-07-25 02:25:22 +000083 if (!s->sums) out_of_memory("receive_sums");
84
J.W. Schultze6e3f122003-08-15 00:57:27 +000085 for (i = 0; i < (int) s->count; i++) {
Andrew Tridgell2f03f951998-07-25 02:25:22 +000086 s->sums[i].sum1 = read_int(f);
J.W. Schultze6e3f122003-08-15 00:57:27 +000087 read_buf(f, s->sums[i].sum2, s->s2length);
Andrew Tridgell2f03f951998-07-25 02:25:22 +000088
89 s->sums[i].offset = offset;
90 s->sums[i].i = i;
91
Martin Poola2619892002-01-25 23:07:33 +000092 if (i == (int) s->count-1 && s->remainder != 0) {
Andrew Tridgell2f03f951998-07-25 02:25:22 +000093 s->sums[i].len = s->remainder;
94 } else {
J.W. Schultzfc0257c2003-04-10 01:13:30 +000095 s->sums[i].len = s->blength;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000096 }
97 offset += s->sums[i].len;
98
99 if (verbose > 3)
J.W. Schultze6e3f122003-08-15 00:57:27 +0000100 rprintf(FINFO, "chunk[%d] len=%d offset=%.0f sum1=%08x\n",
101 i, s->sums[i].len, (double)s->sums[i].offset, s->sums[i].sum1);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000102 }
103
104 s->flength = offset;
105
106 return s;
107}
108
109
110
J.W. Schultze6e3f122003-08-15 00:57:27 +0000111void send_files(struct file_list *flist, int f_out, int f_in)
112{
Martin Poolc1659c72001-08-31 09:27:35 +0000113 int fd = -1;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000114 struct sum_struct *s;
Martin Poolc1659c72001-08-31 09:27:35 +0000115 struct map_struct *buf = NULL;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000116 STRUCT_STAT st;
J.W. Schultze6e3f122003-08-15 00:57:27 +0000117 char fname[MAXPATHLEN];
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000118 int i;
119 struct file_struct *file;
120 int phase = 0;
J.W. Schultze6e3f122003-08-15 00:57:27 +0000121 extern struct stats stats;
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000122 struct stats initial_stats;
Wayne Davison64c35232004-01-02 07:42:20 +0000123 extern int write_batch;
124 extern int read_batch;
125 int checksums_match;
126 int buff_len;
127 char buff[CHUNK_SIZE];
128 int j;
129 int done;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000130
131 if (verbose > 2)
J.W. Schultze6e3f122003-08-15 00:57:27 +0000132 rprintf(FINFO, "send_files starting\n");
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000133
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000134 while (1) {
J.W. Schultze6e3f122003-08-15 00:57:27 +0000135 int offset = 0;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000136
137 i = read_int(f_in);
138 if (i == -1) {
J.W. Schultze6e3f122003-08-15 00:57:27 +0000139 if (phase == 0) {
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000140 phase++;
141 csum_length = SUM_LENGTH;
J.W. Schultze6e3f122003-08-15 00:57:27 +0000142 write_int(f_out, -1);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000143 if (verbose > 2)
J.W. Schultze6e3f122003-08-15 00:57:27 +0000144 rprintf(FINFO, "send_files phase=%d\n", phase);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000145 continue;
146 }
147 break;
148 }
149
150 if (i < 0 || i >= flist->count) {
J.W. Schultze6e3f122003-08-15 00:57:27 +0000151 rprintf(FERROR, "Invalid file index %d (count=%d)\n",
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000152 i, flist->count);
Andrew Tridgell65417571998-11-03 07:08:27 +0000153 exit_cleanup(RERR_PROTOCOL);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000154 }
155
156 file = flist->files[i];
157
158 stats.num_transferred_files++;
159 stats.total_transferred_size += file->length;
160
161 fname[0] = 0;
162 if (file->basedir) {
J.W. Schultze6e3f122003-08-15 00:57:27 +0000163 strlcpy(fname, file->basedir, MAXPATHLEN);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000164 if (strlen(fname) == MAXPATHLEN-1) {
Wayne Davison06c28402003-12-15 08:10:31 +0000165 io_error |= IOERR_GENERAL;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000166 rprintf(FERROR, "send_files failed on long-named directory %s\n",
Wayne Davisonea425412003-09-11 04:53:05 +0000167 full_fname(fname));
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000168 return;
169 }
J.W. Schultze6e3f122003-08-15 00:57:27 +0000170 strlcat(fname, "/", MAXPATHLEN);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000171 offset = strlen(file->basedir)+1;
172 }
Wayne Davisoncc964a52004-01-02 08:36:58 +0000173 f_name_to(file, fname + offset, MAXPATHLEN - offset);
J.W. Schultze6e3f122003-08-15 00:57:27 +0000174
175 if (verbose > 2)
176 rprintf(FINFO, "send_files(%d, %s)\n", i, fname);
177
178 if (dry_run) {
J.W. Schultz42d4edc2003-03-25 02:28:54 +0000179 if (!am_server && verbose) { /* log transfer */
180 rprintf(FINFO, "%s\n", fname+offset);
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000181 }
J.W. Schultze6e3f122003-08-15 00:57:27 +0000182 write_int(f_out, i);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000183 continue;
184 }
185
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000186 initial_stats = stats;
187
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000188 s = receive_sums(f_in);
189 if (!s) {
Wayne Davison06c28402003-12-15 08:10:31 +0000190 io_error |= IOERR_GENERAL;
J.W. Schultze6e3f122003-08-15 00:57:27 +0000191 rprintf(FERROR, "receive_sums failed\n");
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000192 return;
193 }
Martin Pool76f79ba2002-01-23 05:51:06 +0000194
195 if (write_batch)
J.W. Schultze6e3f122003-08-15 00:57:27 +0000196 write_batch_csum_info(&i, flist->count, s);
197
Martin Pool6902ed12001-08-14 02:04:47 +0000198 if (!read_batch) {
Martin Poolc1659c72001-08-31 09:27:35 +0000199 fd = do_open(fname, O_RDONLY, 0);
200 if (fd == -1) {
Wayne Davison06c28402003-12-15 08:10:31 +0000201 if (errno == ENOENT) {
202 io_error |= IOERR_VANISHED;
203 rprintf(FINFO, "file has vanished: %s\n",
Wayne Davison64c35232004-01-02 07:42:20 +0000204 full_fname(fname));
Wayne Davison06c28402003-12-15 08:10:31 +0000205 } else {
206 io_error |= IOERR_GENERAL;
207 rprintf(FERROR, "send_files failed to open %s: %s\n",
208 full_fname(fname), strerror(errno));
209 }
Martin Poolc1659c72001-08-31 09:27:35 +0000210 free_sums(s);
211 continue;
212 }
J.W. Schultze6e3f122003-08-15 00:57:27 +0000213
Martin Poolc1659c72001-08-31 09:27:35 +0000214 /* map the local file */
J.W. Schultze6e3f122003-08-15 00:57:27 +0000215 if (do_fstat(fd, &st) != 0) {
Wayne Davison06c28402003-12-15 08:10:31 +0000216 io_error |= IOERR_GENERAL;
Wayne Davisonea425412003-09-11 04:53:05 +0000217 rprintf(FERROR, "fstat failed: %s\n", strerror(errno));
Martin Poolc1659c72001-08-31 09:27:35 +0000218 free_sums(s);
219 close(fd);
220 return;
221 }
J.W. Schultze6e3f122003-08-15 00:57:27 +0000222
Martin Poolc1659c72001-08-31 09:27:35 +0000223 if (st.st_size > 0) {
J.W. Schultze6e3f122003-08-15 00:57:27 +0000224 buf = map_file(fd, st.st_size);
Martin Poolc1659c72001-08-31 09:27:35 +0000225 } else {
226 buf = NULL;
227 }
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000228
J.W. Schultze6e3f122003-08-15 00:57:27 +0000229 if (verbose > 2)
230 rprintf(FINFO, "send_files mapped %s of size %.0f\n",
231 fname, (double)st.st_size);
232
233 write_int(f_out, i);
234
Martin Poolc1659c72001-08-31 09:27:35 +0000235 if (write_batch)
J.W. Schultze6e3f122003-08-15 00:57:27 +0000236 write_batch_delta_file((char *)&i, sizeof(i));
Martin Pool6902ed12001-08-14 02:04:47 +0000237
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000238 write_sum_head(f_out, s);
Martin Pool6902ed12001-08-14 02:04:47 +0000239 }
J.W. Schultze6e3f122003-08-15 00:57:27 +0000240
241 if (verbose > 2 && !read_batch)
242 rprintf(FINFO, "calling match_sums %s\n", fname);
243
J.W. Schultz42d4edc2003-03-25 02:28:54 +0000244 if (!am_server && verbose) { /* log transfer */
245 rprintf(FINFO, "%s\n", fname+offset);
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000246 }
Andrew Tridgell83fff1a1998-11-20 22:26:29 +0000247
248 set_compression(fname);
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000249
Wayne Davison64c35232004-01-02 07:42:20 +0000250 if (read_batch) {
J.W. Schultze6e3f122003-08-15 00:57:27 +0000251 /* read checksums originally computed on sender side */
252 read_batch_csum_info(i, s, &checksums_match);
253 if (checksums_match) {
Wayne Davisoncc964a52004-01-02 08:36:58 +0000254 read_batch_delta_file((char*)&j, sizeof (int));
J.W. Schultze6e3f122003-08-15 00:57:27 +0000255 if (j != i) { /* if flist index entries don't match*/
256 rprintf(FINFO, "index mismatch in send_files\n");
257 rprintf(FINFO, "read index = %d flist ndx = %d\n", j, i);
258 close_batch_delta_file();
259 close_batch_csums_file();
260 exit_cleanup(1);
261 } else {
262 write_int(f_out, j);
263 write_sum_head(f_out, s);
264 done = 0;
265 while (!done) {
Wayne Davisoncc964a52004-01-02 08:36:58 +0000266 read_batch_delta_file((char*)&buff_len, sizeof (int));
J.W. Schultze6e3f122003-08-15 00:57:27 +0000267 write_int(f_out, buff_len);
268 if (buff_len == 0) {
269 done = 1;
270 } else {
271 if (buff_len > 0) {
272 read_batch_delta_file(buff, buff_len);
273 write_buf(f_out, buff, buff_len);
274 }
275 }
276 } /* end while */
277 read_batch_delta_file( buff, MD4_SUM_LENGTH);
278 write_buf(f_out, buff, MD4_SUM_LENGTH);
Martin Pool6902ed12001-08-14 02:04:47 +0000279
J.W. Schultze6e3f122003-08-15 00:57:27 +0000280 } /* j=i */
281 } else { /* not checksum match */
282 rprintf (FINFO, "readbatch & checksums don't match\n");
283 rprintf (FINFO, "filename=%s is being skipped\n", fname);
284 continue;
285 }
286 } else {
287 match_sums(f_out, s, buf, st.st_size);
288 log_send(file, &initial_stats);
289 }
Martin Pool6902ed12001-08-14 02:04:47 +0000290
Wayne Davison64c35232004-01-02 07:42:20 +0000291 if (!read_batch) {
J.W. Schultz6a7cc462003-09-16 02:49:59 +0000292 if (buf) {
293 j = unmap_file(buf);
294 if (j) {
Wayne Davison06c28402003-12-15 08:10:31 +0000295 io_error |= IOERR_GENERAL;
J.W. Schultz6a7cc462003-09-16 02:49:59 +0000296 rprintf(FERROR,
297 "read errors mapping %s: (%d) %s\n",
Wayne Davison06c28402003-12-15 08:10:31 +0000298 full_fname(fname), j, strerror(j));
J.W. Schultz6a7cc462003-09-16 02:49:59 +0000299 }
300 }
J.W. Schultze6e3f122003-08-15 00:57:27 +0000301 close(fd);
Martin Pool6902ed12001-08-14 02:04:47 +0000302 }
J.W. Schultze6e3f122003-08-15 00:57:27 +0000303
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000304 free_sums(s);
J.W. Schultze6e3f122003-08-15 00:57:27 +0000305
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000306 if (verbose > 2)
J.W. Schultze6e3f122003-08-15 00:57:27 +0000307 rprintf(FINFO, "sender finished %s\n", fname);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000308 }
309
310 if (verbose > 2)
J.W. Schultze6e3f122003-08-15 00:57:27 +0000311 rprintf(FINFO, "send files finished\n");
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000312
313 match_report();
314
J.W. Schultze6e3f122003-08-15 00:57:27 +0000315 write_int(f_out, -1);
Wayne Davison64c35232004-01-02 07:42:20 +0000316 if (write_batch || read_batch) {
J.W. Schultze6e3f122003-08-15 00:57:27 +0000317 close_batch_csums_file();
318 close_batch_delta_file();
Martin Pool6902ed12001-08-14 02:04:47 +0000319 }
320
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000321}
322
323
324
325
326