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;
29 extern int protocol_version;
30 extern struct stats stats;
36 * The sender gets checksums from the generator, calculates deltas,
37 * and transmits them to the receiver. The sender process runs on the
38 * machine holding the source files.
40 void read_sum_head(int f, struct sum_struct *sum)
42 sum->count = read_int(f);
43 sum->blength = read_int(f);
44 if (protocol_version < 27) {
45 sum->s2length = csum_length;
47 sum->s2length = read_int(f);
48 if (sum->s2length > MD4_SUM_LENGTH) {
49 rprintf(FERROR, "Invalid checksum length %ld\n",
51 exit_cleanup(RERR_PROTOCOL);
54 sum->remainder = read_int(f);
58 * Receive the checksums for a buffer
60 static struct sum_struct *receive_sums(int f)
66 if (!(s = new(struct sum_struct)))
67 out_of_memory("receive_sums");
74 rprintf(FINFO, "count=%ld n=%u rem=%u\n",
75 (long)s->count, s->blength, s->remainder);
81 if (!(s->sums = new_array(struct sum_buf, s->count)))
82 out_of_memory("receive_sums");
84 for (i = 0; i < (int)s->count; i++) {
85 s->sums[i].sum1 = read_int(f);
86 read_buf(f, s->sums[i].sum2, s->s2length);
88 s->sums[i].offset = offset;
91 if (i == (int)s->count-1 && s->remainder != 0)
92 s->sums[i].len = s->remainder;
94 s->sums[i].len = s->blength;
95 offset += s->sums[i].len;
99 "chunk[%d] len=%d offset=%.0f sum1=%08x\n",
100 i, s->sums[i].len, (double)s->sums[i].offset,
112 void send_files(struct file_list *flist, int f_out, int f_in)
115 struct sum_struct *s;
116 struct map_struct *mbuf = NULL;
118 char fname[MAXPATHLEN];
120 struct file_struct *file;
122 struct stats initial_stats;
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.current_file_index = i;
153 stats.num_transferred_files++;
154 stats.total_transferred_size += file->length;
157 /* N.B. We're sure that this fits, so offset is OK. */
158 offset = strlcpy(fname, file->basedir, sizeof fname);
159 if (!offset || fname[offset-1] != '/')
160 fname[offset++] = '/';
163 f_name_to(file, fname + offset);
166 rprintf(FINFO, "send_files(%d, %s)\n", i, fname);
169 if (!am_server && verbose) {
170 rprintf(FINFO, "%s\n", fname+offset);
176 initial_stats = stats;
178 if (!(s = receive_sums(f_in))) {
179 io_error |= IOERR_GENERAL;
180 rprintf(FERROR, "receive_sums failed\n");
184 fd = do_open(fname, O_RDONLY, 0);
186 if (errno == ENOENT) {
187 enum logcode c = am_daemon
188 && protocol_version < 28 ? FERROR
190 io_error |= IOERR_VANISHED;
191 rprintf(c, "file has vanished: %s\n",
194 io_error |= IOERR_GENERAL;
195 rsyserr(FERROR, errno,
196 "send_files failed to open %s",
203 /* map the local file */
204 if (do_fstat(fd, &st) != 0) {
205 io_error |= IOERR_GENERAL;
206 rsyserr(FERROR, errno, "fstat failed");
212 mbuf = st.st_size ? map_file(fd, st.st_size, s->blength) : NULL;
215 rprintf(FINFO, "send_files mapped %s of size %.0f\n",
216 fname, (double)st.st_size);
220 write_sum_head(f_out, s);
223 rprintf(FINFO, "calling match_sums %s\n", fname);
225 if (!am_server && verbose) {
226 rprintf(FINFO, "%s\n", fname+offset);
229 set_compression(fname);
231 match_sums(f_out, s, mbuf, st.st_size);
232 log_send(file, &initial_stats);
235 j = unmap_file(mbuf);
237 io_error |= IOERR_GENERAL;
239 "read errors mapping %s",
248 rprintf(FINFO, "sender finished %s\n", fname);
252 rprintf(FINFO, "send files finished\n");
256 write_int(f_out, -1);