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 remote_version;
43 sum->count = read_int(f);
44 sum->blength = read_int(f);
45 if (remote_version < 27)
47 sum->s2length = csum_length;
49 sum->s2length = read_int(f);
51 sum->remainder = read_int(f);
55 * Receive the checksums for a buffer
57 static struct sum_struct *receive_sums(int f)
63 s = (struct sum_struct *)malloc(sizeof(*s));
64 if (!s) out_of_memory("receive_sums");
71 rprintf(FINFO, "count=%ld n=%ld rem=%ld\n",
72 (long) s->count, (long) s->blength,
78 s->sums = (struct sum_buf *)malloc(sizeof(s->sums[0])*s->count);
79 if (!s->sums) out_of_memory("receive_sums");
81 for (i = 0; i < (int) s->count; i++) {
82 s->sums[i].sum1 = read_int(f);
83 read_buf(f, s->sums[i].sum2, s->s2length);
85 s->sums[i].offset = offset;
88 if (i == (int) s->count-1 && s->remainder != 0) {
89 s->sums[i].len = s->remainder;
91 s->sums[i].len = s->blength;
93 offset += s->sums[i].len;
96 rprintf(FINFO, "chunk[%d] len=%d offset=%.0f sum1=%08x\n",
97 i, s->sums[i].len, (double)s->sums[i].offset, s->sums[i].sum1);
107 void send_files(struct file_list *flist, int f_out, int f_in)
110 struct sum_struct *s;
111 struct map_struct *buf = NULL;
113 char fname[MAXPATHLEN];
115 struct file_struct *file;
117 extern struct stats stats;
118 struct stats initial_stats;
119 extern int write_batch; /* dw */
120 extern int read_batch; /* dw */
121 int checksums_match; /* dw */
122 int buff_len; /* dw */
123 char buff[CHUNK_SIZE]; /* dw */
128 rprintf(FINFO, "send_files starting\n");
137 csum_length = SUM_LENGTH;
138 write_int(f_out, -1);
140 rprintf(FINFO, "send_files phase=%d\n", phase);
146 if (i < 0 || i >= flist->count) {
147 rprintf(FERROR, "Invalid file index %d (count=%d)\n",
149 exit_cleanup(RERR_PROTOCOL);
152 file = flist->files[i];
154 stats.num_transferred_files++;
155 stats.total_transferred_size += file->length;
159 strlcpy(fname, file->basedir, MAXPATHLEN);
160 if (strlen(fname) == MAXPATHLEN-1) {
162 rprintf(FERROR, "send_files failed on long-named directory %s\n",
166 strlcat(fname, "/", MAXPATHLEN);
167 offset = strlen(file->basedir)+1;
169 strlcat(fname, f_name(file), MAXPATHLEN);
172 rprintf(FINFO, "send_files(%d, %s)\n", i, fname);
175 if (!am_server && verbose) { /* log transfer */
176 rprintf(FINFO, "%s\n", fname+offset);
182 initial_stats = stats;
184 s = receive_sums(f_in);
187 rprintf(FERROR, "receive_sums failed\n");
192 write_batch_csum_info(&i, flist->count, s);
195 fd = do_open(fname, O_RDONLY, 0);
198 rprintf(FERROR, "send_files failed to open %s: %s\n",
199 fname, strerror(errno));
204 /* map the local file */
205 if (do_fstat(fd, &st) != 0) {
207 rprintf(FERROR, "fstat failed : %s\n", strerror(errno));
213 if (st.st_size > 0) {
214 buf = map_file(fd, st.st_size);
220 rprintf(FINFO, "send_files mapped %s of size %.0f\n",
221 fname, (double)st.st_size);
226 write_batch_delta_file((char *)&i, sizeof(i));
228 write_sum_head(f_out, s);
231 if (verbose > 2 && !read_batch)
232 rprintf(FINFO, "calling match_sums %s\n", fname);
234 if (!am_server && verbose) { /* log transfer */
235 rprintf(FINFO, "%s\n", fname+offset);
238 set_compression(fname);
240 if (read_batch) { /* dw */
241 /* read checksums originally computed on sender side */
242 read_batch_csum_info(i, s, &checksums_match);
243 if (checksums_match) {
244 read_batch_delta_file( (char *) &j, sizeof(int) );
245 if (j != i) { /* if flist index entries don't match*/
246 rprintf(FINFO, "index mismatch in send_files\n");
247 rprintf(FINFO, "read index = %d flist ndx = %d\n", j, i);
248 close_batch_delta_file();
249 close_batch_csums_file();
253 write_sum_head(f_out, s);
256 read_batch_delta_file( (char *) &buff_len, sizeof(int) );
257 write_int(f_out, buff_len);
262 read_batch_delta_file(buff, buff_len);
263 write_buf(f_out, buff, buff_len);
267 read_batch_delta_file( buff, MD4_SUM_LENGTH);
268 write_buf(f_out, buff, MD4_SUM_LENGTH);
271 } else { /* not checksum match */
272 rprintf (FINFO, "readbatch & checksums don't match\n");
273 rprintf (FINFO, "filename=%s is being skipped\n", fname);
277 match_sums(f_out, s, buf, st.st_size);
278 log_send(file, &initial_stats);
281 if (!read_batch) { /* dw */
282 if (buf) unmap_file(buf);
289 rprintf(FINFO, "sender finished %s\n", fname);
293 rprintf(FINFO, "send files finished\n");
297 write_int(f_out, -1);
298 if (write_batch || read_batch) { /* dw */
299 close_batch_csums_file();
300 close_batch_delta_file();