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 sum->remainder = read_int(f);
53 * Receive the checksums for a buffer
55 static struct sum_struct *receive_sums(int f)
61 s = new(struct sum_struct);
62 if (!s) out_of_memory("receive_sums");
69 rprintf(FINFO, "count=%ld n=%ld rem=%ld\n",
70 (long) s->count, (long) s->blength,
76 s->sums = new_array(struct sum_buf, s->count);
77 if (!s->sums) out_of_memory("receive_sums");
79 for (i = 0; i < (int) s->count; i++) {
80 s->sums[i].sum1 = read_int(f);
81 read_buf(f, s->sums[i].sum2, s->s2length);
83 s->sums[i].offset = offset;
86 if (i == (int) s->count-1 && s->remainder != 0) {
87 s->sums[i].len = s->remainder;
89 s->sums[i].len = s->blength;
91 offset += s->sums[i].len;
94 rprintf(FINFO, "chunk[%d] len=%d offset=%.0f sum1=%08x\n",
95 i, s->sums[i].len, (double)s->sums[i].offset, s->sums[i].sum1);
105 void send_files(struct file_list *flist, int f_out, int f_in)
108 struct sum_struct *s;
109 struct map_struct *buf = NULL;
111 char fname[MAXPATHLEN];
113 struct file_struct *file;
115 extern struct stats stats;
116 struct stats initial_stats;
117 extern int write_batch; /* dw */
118 extern int read_batch; /* dw */
119 int checksums_match; /* dw */
120 int buff_len; /* dw */
121 char buff[CHUNK_SIZE]; /* dw */
126 rprintf(FINFO, "send_files starting\n");
135 csum_length = SUM_LENGTH;
136 write_int(f_out, -1);
138 rprintf(FINFO, "send_files phase=%d\n", phase);
144 if (i < 0 || i >= flist->count) {
145 rprintf(FERROR, "Invalid file index %d (count=%d)\n",
147 exit_cleanup(RERR_PROTOCOL);
150 file = flist->files[i];
152 stats.num_transferred_files++;
153 stats.total_transferred_size += file->length;
157 strlcpy(fname, file->basedir, MAXPATHLEN);
158 if (strlen(fname) == MAXPATHLEN-1) {
160 rprintf(FERROR, "send_files failed on long-named directory %s\n",
164 strlcat(fname, "/", MAXPATHLEN);
165 offset = strlen(file->basedir)+1;
167 strlcat(fname, f_name(file), MAXPATHLEN);
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);
185 rprintf(FERROR, "receive_sums failed\n");
190 write_batch_csum_info(&i, flist->count, s);
193 fd = do_open(fname, O_RDONLY, 0);
196 rprintf(FERROR, "send_files failed to open %s: %s\n",
197 full_fname(fname), strerror(errno));
202 /* map the local file */
203 if (do_fstat(fd, &st) != 0) {
205 rprintf(FERROR, "fstat failed: %s\n", strerror(errno));
211 if (st.st_size > 0) {
212 buf = map_file(fd, st.st_size);
218 rprintf(FINFO, "send_files mapped %s of size %.0f\n",
219 fname, (double)st.st_size);
224 write_batch_delta_file((char *)&i, sizeof(i));
226 write_sum_head(f_out, s);
229 if (verbose > 2 && !read_batch)
230 rprintf(FINFO, "calling match_sums %s\n", fname);
232 if (!am_server && verbose) { /* log transfer */
233 rprintf(FINFO, "%s\n", fname+offset);
236 set_compression(fname);
238 if (read_batch) { /* dw */
239 /* read checksums originally computed on sender side */
240 read_batch_csum_info(i, s, &checksums_match);
241 if (checksums_match) {
242 read_batch_delta_file( (char *) &j, sizeof(int) );
243 if (j != i) { /* if flist index entries don't match*/
244 rprintf(FINFO, "index mismatch in send_files\n");
245 rprintf(FINFO, "read index = %d flist ndx = %d\n", j, i);
246 close_batch_delta_file();
247 close_batch_csums_file();
251 write_sum_head(f_out, s);
254 read_batch_delta_file( (char *) &buff_len, sizeof(int) );
255 write_int(f_out, buff_len);
260 read_batch_delta_file(buff, buff_len);
261 write_buf(f_out, buff, buff_len);
265 read_batch_delta_file( buff, MD4_SUM_LENGTH);
266 write_buf(f_out, buff, MD4_SUM_LENGTH);
269 } else { /* not checksum match */
270 rprintf (FINFO, "readbatch & checksums don't match\n");
271 rprintf (FINFO, "filename=%s is being skipped\n", fname);
275 match_sums(f_out, s, buf, st.st_size);
276 log_send(file, &initial_stats);
279 if (!read_batch) { /* dw */
285 "read errors mapping %s: (%d) %s\n",
297 rprintf(FINFO, "sender finished %s\n", fname);
301 rprintf(FINFO, "send files finished\n");
305 write_int(f_out, -1);
306 if (write_batch || read_batch) { /* dw */
307 close_batch_csums_file();
308 close_batch_delta_file();