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 int make_backups;
31 extern struct stats stats;
37 * The sender gets checksums from the generator, calculates deltas,
38 * and transmits them to the receiver. The sender process runs on the
39 * machine holding the source files.
41 void read_sum_head(int f, struct sum_struct *sum)
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 if (sum->s2length > MD4_SUM_LENGTH) {
50 rprintf(FERROR, "Invalid checksum length %ld\n",
52 exit_cleanup(RERR_PROTOCOL);
55 sum->remainder = read_int(f);
59 * Receive the checksums for a buffer
61 static struct sum_struct *receive_sums(int f)
67 if (!(s = new(struct sum_struct)))
68 out_of_memory("receive_sums");
75 rprintf(FINFO, "count=%ld n=%u rem=%u\n",
76 (long)s->count, s->blength, s->remainder);
82 if (!(s->sums = new_array(struct sum_buf, s->count)))
83 out_of_memory("receive_sums");
85 for (i = 0; i < (int)s->count; i++) {
86 s->sums[i].sum1 = read_int(f);
87 read_buf(f, s->sums[i].sum2, s->s2length);
89 s->sums[i].offset = offset;
92 if (i == (int)s->count-1 && s->remainder != 0)
93 s->sums[i].len = s->remainder;
95 s->sums[i].len = s->blength;
96 offset += s->sums[i].len;
100 "chunk[%d] len=%d offset=%.0f sum1=%08x\n",
101 i, s->sums[i].len, (double)s->sums[i].offset,
113 void send_files(struct file_list *flist, int f_out, int f_in)
116 struct sum_struct *s;
117 struct map_struct *mbuf = NULL;
119 char *fname2, fname[MAXPATHLEN];
121 struct file_struct *file;
123 struct stats initial_stats;
124 int save_make_backups = make_backups;
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);
141 /* For inplace: redo phase turns off the backup
142 * flag so that we do a regular inplace send. */
149 if (i < 0 || i >= flist->count) {
150 rprintf(FERROR, "Invalid file index %d (count=%d)\n",
152 exit_cleanup(RERR_PROTOCOL);
155 file = flist->files[i];
157 stats.current_file_index = i;
158 stats.num_transferred_files++;
159 stats.total_transferred_size += file->length;
162 /* N.B. We're sure that this fits, so offset is OK. */
163 offset = strlcpy(fname, file->basedir, sizeof fname);
164 if (!offset || fname[offset-1] != '/')
165 fname[offset++] = '/';
168 fname2 = f_name_to(file, fname + offset);
171 rprintf(FINFO, "send_files(%d, %s)\n", i, fname);
174 if (!am_server && verbose) /* log the transfer */
175 rprintf(FINFO, "%s\n", safe_fname(fname2));
180 initial_stats = stats;
182 if (!(s = receive_sums(f_in))) {
183 io_error |= IOERR_GENERAL;
184 rprintf(FERROR, "receive_sums failed\n");
188 fd = do_open(fname, O_RDONLY, 0);
190 if (errno == ENOENT) {
191 enum logcode c = am_daemon
192 && protocol_version < 28 ? FERROR
194 io_error |= IOERR_VANISHED;
195 rprintf(c, "file has vanished: %s\n",
198 io_error |= IOERR_GENERAL;
199 rsyserr(FERROR, errno,
200 "send_files failed to open %s",
207 /* map the local file */
208 if (do_fstat(fd, &st) != 0) {
209 io_error |= IOERR_GENERAL;
210 rsyserr(FERROR, errno, "fstat failed");
217 OFF_T map_size = MAX(s->blength * 3, MAX_MAP_SIZE);
218 mbuf = map_file(fd, st.st_size, map_size, s->blength);
223 rprintf(FINFO, "send_files mapped %s of size %.0f\n",
224 safe_fname(fname), (double)st.st_size);
228 write_sum_head(f_out, s);
231 rprintf(FINFO, "calling match_sums %s\n",
235 if (!am_server && verbose) /* log the transfer */
236 rprintf(FINFO, "%s\n", safe_fname(fname2));
238 set_compression(fname);
240 match_sums(f_out, s, mbuf, st.st_size);
241 log_send(file, &initial_stats);
244 j = unmap_file(mbuf);
246 io_error |= IOERR_GENERAL;
248 "read errors mapping %s",
257 rprintf(FINFO, "sender finished %s\n",
261 make_backups = save_make_backups;
264 rprintf(FINFO, "send files finished\n");
268 write_int(f_out, -1);