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;
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.
39 void read_sum_head(int f, struct sum_struct *sum)
41 extern int protocol_version;
43 sum->count = read_int(f);
44 sum->blength = read_int(f);
45 if (protocol_version < 27) {
46 sum->s2length = csum_length;
48 sum->s2length = read_int(f);
49 if (sum->s2length > MD4_SUM_LENGTH) {
50 rprintf(FERROR, "Invalid checksum length %ld\n",
52 exit_cleanup(RERR_PROTOCOL);
55 sum->remainder = read_int(f);
59 * Receive the checksums for a buffer
61 static struct sum_struct *receive_sums(int f)
67 s = new(struct sum_struct);
68 if (!s) out_of_memory("receive_sums");
75 rprintf(FINFO, "count=%ld n=%u rem=%u\n",
76 (long)s->count, s->blength, s->remainder);
82 s->sums = new_array(struct sum_buf, s->count);
83 if (!s->sums) out_of_memory("receive_sums");
85 for (i = 0; i < (int) s->count; i++) {
86 s->sums[i].sum1 = read_int(f);
87 read_buf(f, s->sums[i].sum2, s->s2length);
89 s->sums[i].offset = offset;
92 if (i == (int) s->count-1 && s->remainder != 0) {
93 s->sums[i].len = s->remainder;
95 s->sums[i].len = s->blength;
97 offset += s->sums[i].len;
100 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);
111 void send_files(struct file_list *flist, int f_out, int f_in)
114 struct sum_struct *s;
115 struct map_struct *buf = NULL;
117 char fname[MAXPATHLEN];
119 struct file_struct *file;
121 extern struct stats stats;
122 struct stats initial_stats;
123 extern int write_batch;
124 extern int read_batch;
127 char buff[CHUNK_SIZE];
132 rprintf(FINFO, "send_files starting\n");
141 csum_length = SUM_LENGTH;
142 write_int(f_out, -1);
144 rprintf(FINFO, "send_files phase=%d\n", phase);
150 if (i < 0 || i >= flist->count) {
151 rprintf(FERROR, "Invalid file index %d (count=%d)\n",
153 exit_cleanup(RERR_PROTOCOL);
156 file = flist->files[i];
158 stats.num_transferred_files++;
159 stats.total_transferred_size += file->length;
163 strlcpy(fname, file->basedir, MAXPATHLEN);
164 if (strlen(fname) == MAXPATHLEN-1) {
165 io_error |= IOERR_GENERAL;
166 rprintf(FERROR, "send_files failed on long-named directory %s\n",
170 strlcat(fname, "/", MAXPATHLEN);
171 offset = strlen(file->basedir)+1;
173 f_name_to(file, fname + offset, MAXPATHLEN - offset);
176 rprintf(FINFO, "send_files(%d, %s)\n", i, fname);
179 if (!am_server && verbose) { /* log transfer */
180 rprintf(FINFO, "%s\n", fname+offset);
186 initial_stats = stats;
188 s = receive_sums(f_in);
190 io_error |= IOERR_GENERAL;
191 rprintf(FERROR, "receive_sums failed\n");
196 write_batch_csum_info(&i, flist->count, s);
199 fd = do_open(fname, O_RDONLY, 0);
201 if (errno == ENOENT) {
202 io_error |= IOERR_VANISHED;
203 rprintf(FINFO, "file has vanished: %s\n",
206 io_error |= IOERR_GENERAL;
207 rprintf(FERROR, "send_files failed to open %s: %s\n",
208 full_fname(fname), strerror(errno));
214 /* map the local file */
215 if (do_fstat(fd, &st) != 0) {
216 io_error |= IOERR_GENERAL;
217 rprintf(FERROR, "fstat failed: %s\n", strerror(errno));
223 if (st.st_size > 0) {
224 buf = map_file(fd, st.st_size);
230 rprintf(FINFO, "send_files mapped %s of size %.0f\n",
231 fname, (double)st.st_size);
236 write_batch_delta_file((char *)&i, sizeof(i));
238 write_sum_head(f_out, s);
241 if (verbose > 2 && !read_batch)
242 rprintf(FINFO, "calling match_sums %s\n", fname);
244 if (!am_server && verbose) { /* log transfer */
245 rprintf(FINFO, "%s\n", fname+offset);
248 set_compression(fname);
251 /* read checksums originally computed on sender side */
252 read_batch_csum_info(i, s, &checksums_match);
253 if (checksums_match) {
254 read_batch_delta_file((char*)&j, sizeof (int));
255 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();
263 write_sum_head(f_out, s);
266 read_batch_delta_file((char*)&buff_len, sizeof (int));
267 write_int(f_out, buff_len);
272 read_batch_delta_file(buff, buff_len);
273 write_buf(f_out, buff, buff_len);
277 read_batch_delta_file( buff, MD4_SUM_LENGTH);
278 write_buf(f_out, buff, MD4_SUM_LENGTH);
281 } else { /* not checksum match */
282 rprintf (FINFO, "readbatch & checksums don't match\n");
283 rprintf (FINFO, "filename=%s is being skipped\n", fname);
287 match_sums(f_out, s, buf, st.st_size);
288 log_send(file, &initial_stats);
295 io_error |= IOERR_GENERAL;
297 "read errors mapping %s: (%d) %s\n",
298 full_fname(fname), j, strerror(j));
307 rprintf(FINFO, "sender finished %s\n", fname);
311 rprintf(FINFO, "send files finished\n");
315 write_int(f_out, -1);
316 if (write_batch || read_batch) {
317 close_batch_csums_file();
318 close_batch_delta_file();