2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
5 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.
10 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.
15 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.
23 extern int csum_length;
24 extern struct stats stats;
29 extern int protocol_version;
35 * The sender gets checksums from the generator, calculates deltas,
36 * and transmits them to the receiver. The sender process runs on the
37 * machine holding the source files.
39 void read_sum_head(int f, struct sum_struct *sum)
41 sum->count = read_int(f);
42 sum->blength = read_int(f);
43 if (protocol_version < 27) {
44 sum->s2length = csum_length;
46 sum->s2length = read_int(f);
47 if (sum->s2length > MD4_SUM_LENGTH) {
48 rprintf(FERROR, "Invalid checksum length %ld\n",
50 exit_cleanup(RERR_PROTOCOL);
53 sum->remainder = read_int(f);
57 * Receive the checksums for a buffer
59 static struct sum_struct *receive_sums(int f)
65 s = new(struct sum_struct);
66 if (!s) out_of_memory("receive_sums");
73 rprintf(FINFO, "count=%ld n=%u rem=%u\n",
74 (long)s->count, s->blength, s->remainder);
80 s->sums = new_array(struct sum_buf, s->count);
81 if (!s->sums) out_of_memory("receive_sums");
83 for (i = 0; i < (int) s->count; i++) {
84 s->sums[i].sum1 = read_int(f);
85 read_buf(f, s->sums[i].sum2, s->s2length);
87 s->sums[i].offset = offset;
90 if (i == (int) s->count-1 && s->remainder != 0) {
91 s->sums[i].len = s->remainder;
93 s->sums[i].len = s->blength;
95 offset += s->sums[i].len;
98 rprintf(FINFO, "chunk[%d] len=%d offset=%.0f sum1=%08x\n",
99 i, s->sums[i].len, (double)s->sums[i].offset, s->sums[i].sum1);
109 void send_files(struct file_list *flist, int f_out, int f_in)
112 struct sum_struct *s;
113 struct map_struct *mbuf = NULL;
115 char fname[MAXPATHLEN];
117 struct file_struct *file;
119 extern struct stats stats;
120 struct stats initial_stats;
121 extern int write_batch;
122 extern int read_batch;
125 char buff[CHUNK_SIZE];
130 rprintf(FINFO, "send_files starting\n");
139 csum_length = SUM_LENGTH;
140 write_int(f_out, -1);
142 rprintf(FINFO, "send_files phase=%d\n", phase);
148 if (i < 0 || i >= flist->count) {
149 rprintf(FERROR, "Invalid file index %d (count=%d)\n",
151 exit_cleanup(RERR_PROTOCOL);
154 file = flist->files[i];
156 stats.current_file_index = i;
157 stats.num_transferred_files++;
158 stats.total_transferred_size += file->length;
161 /* N.B. We're sure that this fits, so offset is OK. */
162 offset = strlcpy(fname, file->basedir, sizeof fname);
163 if (!offset || fname[offset-1] != '/')
164 fname[offset++] = '/';
167 f_name_to(file, fname + offset);
170 rprintf(FINFO, "send_files(%d, %s)\n", i, fname);
173 if (!am_server && verbose) { /* log transfer */
174 rprintf(FINFO, "%s\n", fname+offset);
180 initial_stats = stats;
182 s = receive_sums(f_in);
184 io_error |= IOERR_GENERAL;
185 rprintf(FERROR, "receive_sums failed\n");
190 write_batch_csum_info(&i, s);
193 fd = do_open(fname, O_RDONLY, 0);
195 if (errno == ENOENT) {
196 enum logcode c = am_daemon
197 && protocol_version < 28 ? FERROR
199 io_error |= IOERR_VANISHED;
200 rprintf(c, "file has vanished: %s\n",
203 io_error |= IOERR_GENERAL;
204 rsyserr(FERROR, errno,
205 "send_files failed to open %s",
212 /* map the local file */
213 if (do_fstat(fd, &st) != 0) {
214 io_error |= IOERR_GENERAL;
215 rsyserr(FERROR, errno, "fstat failed");
221 mbuf = st.st_size ? map_file(fd, st.st_size) : NULL;
224 rprintf(FINFO, "send_files mapped %s of size %.0f\n",
225 fname, (double)st.st_size);
231 write_batch_delta_file((char *)&i, sizeof i);
233 write_sum_head(f_out, s);
236 if (verbose > 2 && !read_batch)
237 rprintf(FINFO, "calling match_sums %s\n", fname);
239 if (!am_server && verbose) { /* log transfer */
240 rprintf(FINFO, "%s\n", fname+offset);
243 set_compression(fname);
246 /* read checksums originally computed on sender side */
247 read_batch_csum_info(i, s, &checksums_match);
248 if (checksums_match) {
249 read_batch_delta_file((char*)&j, sizeof (int));
250 if (j != i) { /* if flist index entries don't match*/
251 rprintf(FINFO, "index mismatch in send_files\n");
252 rprintf(FINFO, "read index = %d flist ndx = %d\n", j, i);
253 close_batch_delta_file();
254 close_batch_csums_file();
258 write_sum_head(f_out, s);
261 read_batch_delta_file((char*)&buff_len, sizeof (int));
262 write_int(f_out, buff_len);
267 read_batch_delta_file(buff, buff_len);
268 write_buf(f_out, buff, buff_len);
272 read_batch_delta_file(buff, MD4_SUM_LENGTH);
273 write_buf(f_out, buff, MD4_SUM_LENGTH);
276 } else { /* not checksum match */
277 rprintf (FINFO, "readbatch & checksums don't match\n");
278 rprintf (FINFO, "filename=%s is being skipped\n", fname);
282 match_sums(f_out, s, mbuf, st.st_size);
283 log_send(file, &initial_stats);
288 j = unmap_file(mbuf);
290 io_error |= IOERR_GENERAL;
292 "read errors mapping %s",
302 rprintf(FINFO, "sender finished %s\n", fname);
306 rprintf(FINFO, "send files finished\n");
310 write_int(f_out, -1);
311 if (write_batch || read_batch) {
312 close_batch_csums_file();
313 close_batch_delta_file();