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.
40 * Receive the checksums for a buffer
42 static struct sum_struct *receive_sums(int f)
48 s = (struct sum_struct *)malloc(sizeof(*s));
49 if (!s) out_of_memory("receive_sums");
51 s->count = read_int(f);
53 s->remainder = read_int(f);
57 rprintf(FINFO,"count=%ld n=%ld rem=%ld\n",
58 (long) s->count, (long) s->n, (long) s->remainder);
63 s->sums = (struct sum_buf *)malloc(sizeof(s->sums[0])*s->count);
64 if (!s->sums) out_of_memory("receive_sums");
66 for (i=0; i < (int) s->count;i++) {
67 s->sums[i].sum1 = read_int(f);
68 read_buf(f,s->sums[i].sum2,csum_length);
70 s->sums[i].offset = offset;
73 if (i == (int) s->count-1 && s->remainder != 0) {
74 s->sums[i].len = s->remainder;
76 s->sums[i].len = s->n;
78 offset += s->sums[i].len;
81 rprintf(FINFO,"chunk[%d] len=%d offset=%.0f sum1=%08x\n",
82 i,s->sums[i].len,(double)s->sums[i].offset,s->sums[i].sum1);
92 void send_files(struct file_list *flist,int f_out,int f_in)
96 struct map_struct *buf = NULL;
98 char fname[MAXPATHLEN];
100 struct file_struct *file;
102 extern struct stats stats;
103 struct stats initial_stats;
104 extern int write_batch; /* dw */
105 extern int read_batch; /* dw */
106 int checksums_match; /* dw */
107 int buff_len; /* dw */
108 char buff[CHUNK_SIZE]; /* dw */
113 rprintf(FINFO,"send_files starting\n");
122 csum_length = SUM_LENGTH;
125 rprintf(FINFO,"send_files phase=%d\n",phase);
131 if (i < 0 || i >= flist->count) {
132 rprintf(FERROR,"Invalid file index %d (count=%d)\n",
134 exit_cleanup(RERR_PROTOCOL);
137 file = flist->files[i];
139 stats.num_transferred_files++;
140 stats.total_transferred_size += file->length;
144 strlcpy(fname,file->basedir,MAXPATHLEN);
145 if (strlen(fname) == MAXPATHLEN-1) {
147 rprintf(FERROR, "send_files failed on long-named directory %s\n",
151 strlcat(fname,"/",MAXPATHLEN);
152 offset = strlen(file->basedir)+1;
154 strlcat(fname,f_name(file),MAXPATHLEN);
157 rprintf(FINFO,"send_files(%d,%s)\n",i,fname);
160 if (!am_server && verbose) { /* log transfer */
161 rprintf(FINFO, "%s\n", fname+offset);
167 initial_stats = stats;
169 s = receive_sums(f_in);
172 rprintf(FERROR,"receive_sums failed\n");
177 write_batch_csum_info(&i,flist->count,s);
180 fd = do_open(fname, O_RDONLY, 0);
183 rprintf(FERROR,"send_files failed to open %s: %s\n",
184 fname,strerror(errno));
189 /* map the local file */
190 if (do_fstat(fd,&st) != 0) {
192 rprintf(FERROR,"fstat failed : %s\n",strerror(errno));
198 if (st.st_size > 0) {
199 buf = map_file(fd,st.st_size);
205 rprintf(FINFO,"send_files mapped %s of size %.0f\n",
206 fname,(double)st.st_size);
211 write_batch_delta_file((char *)&i,sizeof(i));
213 write_int(f_out,s->count);
214 write_int(f_out,s->n);
215 write_int(f_out,s->remainder);
220 rprintf(FINFO,"calling match_sums %s\n",fname);
222 if (!am_server && verbose) { /* log transfer */
223 rprintf(FINFO, "%s\n", fname+offset);
226 set_compression(fname);
228 if (read_batch) { /* dw */
229 /* read checksums originally computed on sender side */
230 read_batch_csum_info(i, s, &checksums_match);
231 if (checksums_match) {
232 read_batch_delta_file( (char *) &j, sizeof(int) );
233 if (j != i) { /* if flist index entries don't match*/
234 rprintf(FINFO,"index mismatch in send_files\n");
235 rprintf(FINFO,"read index = %d flist ndx = %d\n",j,i);
236 close_batch_delta_file();
237 close_batch_csums_file();
242 write_int(f_out,s->count);
243 write_int(f_out,s->n);
244 write_int(f_out,s->remainder);
247 read_batch_delta_file( (char *) &buff_len, sizeof(int) );
248 write_int(f_out,buff_len);
254 read_batch_delta_file(buff, buff_len);
255 write_buf(f_out,buff,buff_len);
259 read_batch_delta_file( buff, MD4_SUM_LENGTH);
260 write_buf(f_out, buff, MD4_SUM_LENGTH);
263 } else { /* not checksum match */
264 rprintf (FINFO,"readbatch & checksums don't match\n");
265 rprintf (FINFO,"filename=%s is being skipped\n",
270 match_sums(f_out,s,buf,st.st_size);
271 log_send(file, &initial_stats);
274 if (!read_batch) { /* dw */
275 if (buf) unmap_file(buf);
282 rprintf(FINFO,"sender finished %s\n",fname);
286 rprintf(FINFO,"send files finished\n");
291 if (write_batch || read_batch) { /* dw */
292 close_batch_csums_file();
293 close_batch_delta_file();