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 itemize_changes;
24 extern int log_before_transfer;
25 extern int csum_length;
26 extern struct stats stats;
31 extern int protocol_version;
32 extern int updating_basis_file;
33 extern int make_backups;
34 extern int do_progress;
36 extern char *log_format;
37 extern struct stats stats;
43 * The sender gets checksums from the generator, calculates deltas,
44 * and transmits them to the receiver. The sender process runs on the
45 * machine holding the source files.
49 * Receive the checksums for a buffer
51 static struct sum_struct *receive_sums(int f)
57 if (!(s = new(struct sum_struct)))
58 out_of_memory("receive_sums");
65 rprintf(FINFO, "count=%.0f n=%ld rem=%ld\n",
66 (double)s->count, (long)s->blength, (long)s->remainder);
72 if (!(s->sums = new_array(struct sum_buf, s->count)))
73 out_of_memory("receive_sums");
75 for (i = 0; i < s->count; i++) {
76 s->sums[i].sum1 = read_int(f);
77 read_buf(f, s->sums[i].sum2, s->s2length);
79 s->sums[i].offset = offset;
82 if (i == s->count-1 && s->remainder != 0)
83 s->sums[i].len = s->remainder;
85 s->sums[i].len = s->blength;
86 offset += s->sums[i].len;
90 "chunk[%d] len=%d offset=%.0f sum1=%08x\n",
91 i, s->sums[i].len, (double)s->sums[i].offset,
103 void send_files(struct file_list *flist, int f_out, int f_in)
106 struct sum_struct *s;
107 struct map_struct *mbuf = NULL;
109 char *fname2, fname[MAXPATHLEN];
111 struct file_struct *file;
113 struct stats initial_stats;
114 int save_make_backups = make_backups;
118 rprintf(FINFO, "send_files starting\n");
127 csum_length = SUM_LENGTH;
128 write_int(f_out, -1);
130 rprintf(FINFO, "send_files phase=%d\n", phase);
131 /* For inplace: redo phase turns off the backup
132 * flag so that we do a regular inplace send. */
139 if (i < 0 || i >= flist->count) {
140 rprintf(FERROR, "Invalid file index %d (count=%d)\n",
142 exit_cleanup(RERR_PROTOCOL);
145 file = flist->files[i];
147 if (itemize_changes) {
148 iflags = read_byte(f_in);
149 if (!(iflags & ITEM_UPDATING) || !S_ISREG(file->mode)) {
152 write_byte(f_out, iflags);
154 log_send(file, &stats, iflags);
158 iflags = ITEM_UPDATING | ITEM_MISSING_DATA;
160 if (inplace && protocol_version >= 29) {
161 uchar fnamecmp_type = read_byte(f_in);
162 updating_basis_file = fnamecmp_type == FNAMECMP_FNAME;
164 updating_basis_file = inplace && !make_backups;
166 if (!S_ISREG(file->mode)) {
167 rprintf(FERROR, "[%s] got index of non-regular file: %d\n",
169 exit_cleanup(RERR_PROTOCOL);
172 stats.current_file_index = i;
173 stats.num_transferred_files++;
174 stats.total_transferred_size += file->length;
176 if (file->dir.root) {
177 /* N.B. We're sure that this fits, so offset is OK. */
178 offset = strlcpy(fname, file->dir.root, sizeof fname);
179 if (!offset || fname[offset-1] != '/')
180 fname[offset++] = '/';
183 fname2 = f_name_to(file, fname + offset);
186 rprintf(FINFO, "send_files(%d, %s)\n", i, fname);
188 if (dry_run) { /* log the transfer */
189 if (!am_server && verbose && !log_format)
190 rprintf(FINFO, "%s\n", safe_fname(fname2));
192 log_send(file, &stats, iflags);
195 write_byte(f_out, iflags);
199 initial_stats = stats;
201 if (!(s = receive_sums(f_in))) {
202 io_error |= IOERR_GENERAL;
203 rprintf(FERROR, "receive_sums failed\n");
207 fd = do_open(fname, O_RDONLY, 0);
209 if (errno == ENOENT) {
210 enum logcode c = am_daemon
211 && protocol_version < 28 ? FERROR
213 io_error |= IOERR_VANISHED;
214 rprintf(c, "file has vanished: %s\n",
217 io_error |= IOERR_GENERAL;
218 rsyserr(FERROR, errno,
219 "send_files failed to open %s",
226 /* map the local file */
227 if (do_fstat(fd, &st) != 0) {
228 io_error |= IOERR_GENERAL;
229 rsyserr(FERROR, errno, "fstat failed");
236 int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
237 mbuf = map_file(fd, st.st_size, read_size, s->blength);
242 rprintf(FINFO, "send_files mapped %s of size %.0f\n",
243 safe_fname(fname), (double)st.st_size);
248 write_byte(f_out, iflags);
249 write_sum_head(f_out, s);
252 rprintf(FINFO, "calling match_sums %s\n",
256 if (log_before_transfer)
257 log_send(file, &initial_stats, iflags);
258 else if (!am_server && verbose && (!log_format || do_progress))
259 rprintf(FINFO, "%s\n", safe_fname(fname2));
261 set_compression(fname);
263 match_sums(f_out, s, mbuf, st.st_size);
264 if (!log_before_transfer)
265 log_send(file, &initial_stats, iflags);
268 j = unmap_file(mbuf);
270 io_error |= IOERR_GENERAL;
272 "read errors mapping %s",
281 rprintf(FINFO, "sender finished %s\n",
285 make_backups = save_make_backups;
288 rprintf(FINFO, "send files finished\n");
292 write_int(f_out, -1);