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 remote_version;
24 extern int csum_length;
25 extern struct stats stats;
34 * The sender gets checksums from the generator, calculates deltas,
35 * and transmits them to the receiver. The sender process runs on the
36 * machine holding the source files.
41 * Receive the checksums for a buffer
43 static struct sum_struct *receive_sums(int f)
49 s = (struct sum_struct *)malloc(sizeof(*s));
50 if (!s) out_of_memory("receive_sums");
52 s->count = read_int(f);
54 s->remainder = read_int(f);
58 rprintf(FINFO,"count=%ld n=%ld rem=%ld\n",
59 (long) s->count, (long) s->n, (long) s->remainder);
64 s->sums = (struct sum_buf *)malloc(sizeof(s->sums[0])*s->count);
65 if (!s->sums) out_of_memory("receive_sums");
67 for (i=0; i < (int) s->count;i++) {
68 s->sums[i].sum1 = read_int(f);
69 read_buf(f,s->sums[i].sum2,csum_length);
71 s->sums[i].offset = offset;
74 if (i == (int) s->count-1 && s->remainder != 0) {
75 s->sums[i].len = s->remainder;
77 s->sums[i].len = s->n;
79 offset += s->sums[i].len;
82 rprintf(FINFO,"chunk[%d] len=%d offset=%.0f sum1=%08x\n",
83 i,s->sums[i].len,(double)s->sums[i].offset,s->sums[i].sum1);
93 void send_files(struct file_list *flist,int f_out,int f_in)
97 struct map_struct *buf = NULL;
99 char fname[MAXPATHLEN];
101 struct file_struct *file;
103 extern struct stats stats;
104 struct stats initial_stats;
105 extern int write_batch; /* dw */
106 extern int read_batch; /* dw */
107 int checksums_match; /* dw */
108 int buff_len; /* dw */
109 char buff[CHUNK_SIZE]; /* dw */
114 rprintf(FINFO,"send_files starting\n");
121 if (phase==0 && remote_version >= 13) {
123 csum_length = SUM_LENGTH;
126 rprintf(FINFO,"send_files phase=%d\n",phase);
132 if (i < 0 || i >= flist->count) {
133 rprintf(FERROR,"Invalid file index %d (count=%d)\n",
135 exit_cleanup(RERR_PROTOCOL);
138 file = flist->files[i];
140 stats.num_transferred_files++;
141 stats.total_transferred_size += file->length;
145 strlcpy(fname,file->basedir,MAXPATHLEN);
146 if (strlen(fname) == MAXPATHLEN-1) {
148 rprintf(FERROR, "send_files failed on long-named directory %s\n",
152 strlcat(fname,"/",MAXPATHLEN);
153 offset = strlen(file->basedir)+1;
155 strlcat(fname,f_name(file),MAXPATHLEN);
158 rprintf(FINFO,"send_files(%d,%s)\n",i,fname);
162 log_transfer(file, fname+offset);
168 initial_stats = stats;
170 s = receive_sums(f_in);
173 rprintf(FERROR,"receive_sums failed\n");
178 write_batch_csum_info(&i,flist->count,s);
181 fd = do_open(fname, O_RDONLY, 0);
184 rprintf(FERROR,"send_files failed to open %s: %s\n",
185 fname,strerror(errno));
190 /* map the local file */
191 if (do_fstat(fd,&st) != 0) {
193 rprintf(FERROR,"fstat failed : %s\n",strerror(errno));
199 if (st.st_size > 0) {
200 buf = map_file(fd,st.st_size);
206 rprintf(FINFO,"send_files mapped %s of size %.0f\n",
207 fname,(double)st.st_size);
212 write_batch_delta_file((char *)&i,sizeof(i));
214 write_int(f_out,s->count);
215 write_int(f_out,s->n);
216 write_int(f_out,s->remainder);
221 rprintf(FINFO,"calling match_sums %s\n",fname);
224 log_transfer(file, fname+offset);
227 set_compression(fname);
229 if (read_batch) { /* dw */
230 /* read checksums originally computed on sender side */
231 read_batch_csum_info(i, s, &checksums_match);
232 if (checksums_match) {
233 read_batch_delta_file( (char *) &j, sizeof(int) );
234 if (j != i) { /* if flist index entries don't match*/
235 rprintf(FINFO,"index mismatch in send_files\n");
236 rprintf(FINFO,"read index = %d flist ndx = %d\n",j,i);
237 close_batch_delta_file();
238 close_batch_csums_file();
243 write_int(f_out,s->count);
244 write_int(f_out,s->n);
245 write_int(f_out,s->remainder);
248 read_batch_delta_file( (char *) &buff_len, sizeof(int) );
249 write_int(f_out,buff_len);
255 read_batch_delta_file(buff, buff_len);
256 write_buf(f_out,buff,buff_len);
260 read_batch_delta_file( buff, MD4_SUM_LENGTH);
261 write_buf(f_out, buff, MD4_SUM_LENGTH);
264 } else { /* not checksum match */
265 rprintf (FINFO,"readbatch & checksums don't match\n");
266 rprintf (FINFO,"filename=%s is being skipped\n",
271 match_sums(f_out,s,buf,st.st_size);
272 log_send(file, &initial_stats);
275 if (!read_batch) { /* dw */
276 if (buf) unmap_file(buf);
283 rprintf(FINFO,"sender finished %s\n",fname);
287 rprintf(FINFO,"send files finished\n");
292 if (write_batch || read_batch) { /* dw */
293 close_batch_csums_file();
294 close_batch_delta_file();