2 * Routines only used by the sending process.
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2003-2007 Wayne Davison
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, visit the http://fsf.org website.
29 extern int inc_recurse;
30 extern int log_before_transfer;
31 extern int stdout_format_has_i;
32 extern int logfile_format_has_i;
33 extern int csum_length;
34 extern int append_mode;
36 extern int allowed_lull;
37 extern int preserve_xattrs;
38 extern int protocol_version;
39 extern int remove_source_files;
40 extern int updating_basis_file;
41 extern int make_backups;
42 extern int do_progress;
45 extern int write_batch;
46 extern struct stats stats;
47 extern struct file_list *cur_flist, *first_flist, *dir_flist;
52 * The sender gets checksums from the generator, calculates deltas,
53 * and transmits them to the receiver. The sender process runs on the
54 * machine holding the source files.
58 * Receive the checksums for a buffer
60 static struct sum_struct *receive_sums(int f)
64 int lull_mod = allowed_lull * 5;
67 if (!(s = new(struct sum_struct)))
68 out_of_memory("receive_sums");
75 rprintf(FINFO, "count=%.0f n=%ld rem=%ld\n",
76 (double)s->count, (long)s->blength, (long)s->remainder);
79 if (append_mode > 0) {
80 s->flength = (OFF_T)s->count * s->blength;
82 s->flength -= s->blength - s->remainder;
89 if (!(s->sums = new_array(struct sum_buf, s->count)))
90 out_of_memory("receive_sums");
92 for (i = 0; i < s->count; i++) {
93 s->sums[i].sum1 = read_int(f);
94 read_buf(f, s->sums[i].sum2, s->s2length);
96 s->sums[i].offset = offset;
99 if (i == s->count-1 && s->remainder != 0)
100 s->sums[i].len = s->remainder;
102 s->sums[i].len = s->blength;
103 offset += s->sums[i].len;
105 if (allowed_lull && !(i % lull_mod))
106 maybe_send_keepalive();
110 "chunk[%d] len=%d offset=%.0f sum1=%08x\n",
111 i, s->sums[i].len, (double)s->sums[i].offset,
121 void successful_send(int ndx)
123 char fname[MAXPATHLEN];
124 struct file_struct *file;
125 struct file_list *flist;
127 if (!remove_source_files)
130 if (!(flist = flist_for_ndx(ndx))) {
132 "INTERNAL ERROR: unable to find flist for item %d\n",
137 file = flist->files[ndx - flist->ndx_start];
138 if (!push_pathname(F_PATHNAME(file), -1))
142 if (do_unlink(fname) == 0) {
144 rprintf(FINFO, "sender removed %s\n", fname);
146 rsyserr(FERROR, errno, "sender failed to remove %s", fname);
149 static void write_ndx_and_attrs(int f_out, int ndx, int iflags,
150 const char *fname, struct file_struct *file,
151 uchar fnamecmp_type, char *buf, int len)
153 write_ndx(f_out, ndx);
154 if (protocol_version < 29)
156 write_shortint(f_out, iflags);
157 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
158 write_byte(f_out, fnamecmp_type);
159 if (iflags & ITEM_XNAME_FOLLOWS)
160 write_vstring(f_out, buf, len);
161 #ifdef SUPPORT_XATTRS
162 if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && !dry_run)
163 send_xattr_request(fname, file, f_out);
167 void send_files(int f_in, int f_out)
170 struct sum_struct *s;
171 struct map_struct *mbuf = NULL;
173 char fname[MAXPATHLEN], xname[MAXPATHLEN];
174 const char *path, *slash;
177 struct file_struct *file;
178 int phase = 0, max_phase = protocol_version >= 29 ? 2 : 1;
179 struct stats initial_stats;
180 int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i;
181 enum logcode log_code = log_before_transfer ? FLOG : FINFO;
182 int f_xfer = write_batch < 0 ? batch_fd : f_out;
186 rprintf(FINFO, "send_files starting\n");
190 send_extra_file_list(f_out, FILECNT_LOOKAHEAD);
192 /* This call also sets cur_flist. */
193 ndx = read_ndx_and_attrs(f_in, &iflags, &fnamecmp_type,
195 if (ndx == NDX_DONE) {
196 if (inc_recurse && first_flist) {
197 flist_free(first_flist);
199 write_ndx(f_out, NDX_DONE);
203 if (++phase > max_phase)
206 rprintf(FINFO, "send_files phase=%d\n", phase);
207 write_ndx(f_out, NDX_DONE);
212 send_extra_file_list(f_out, FILECNT_LOOKAHEAD);
214 if (ndx - cur_flist->ndx_start >= 0)
215 file = cur_flist->files[ndx - cur_flist->ndx_start];
217 file = dir_flist->files[cur_flist->parent_ndx];
218 if (F_PATHNAME(file)) {
219 path = F_PATHNAME(file);
224 if (!push_pathname(F_PATHNAME(file), -1))
229 rprintf(FINFO, "send_files(%d, %s%s%s)\n", ndx, path,slash,fname);
231 #ifdef SUPPORT_XATTRS
232 if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && !dry_run)
233 recv_xattr_request(file, f_in);
236 if (!(iflags & ITEM_TRANSFER)) {
237 maybe_log_item(file, iflags, itemizing, xname);
238 write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
239 fnamecmp_type, xname, xlen);
244 "got transfer request in phase 2 [%s]\n",
246 exit_cleanup(RERR_PROTOCOL);
249 if (file->flags & FLAG_FILE_SENT) {
250 if (csum_length == SHORT_SUM_LENGTH) {
251 /* For inplace: redo phase turns off the backup
252 * flag so that we do a regular inplace send. */
253 make_backups = -make_backups;
254 append_mode = -append_mode;
255 csum_length = SUM_LENGTH;
258 if (csum_length != SHORT_SUM_LENGTH) {
259 make_backups = -make_backups;
260 append_mode = -append_mode;
261 csum_length = SHORT_SUM_LENGTH;
265 updating_basis_file = inplace && (protocol_version >= 29
266 ? fnamecmp_type == FNAMECMP_FNAME : make_backups <= 0);
268 if (!am_server && do_progress)
269 set_current_file_index(file, ndx);
270 stats.num_transferred_files++;
271 stats.total_transferred_size += F_LENGTH(file);
273 if (!do_xfers) { /* log the transfer */
274 log_item(FCLIENT, file, &stats, iflags, NULL);
275 write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
276 fnamecmp_type, xname, xlen);
280 initial_stats = stats;
282 if (!(s = receive_sums(f_in))) {
283 io_error |= IOERR_GENERAL;
284 rprintf(FERROR, "receive_sums failed\n");
285 exit_cleanup(RERR_PROTOCOL);
288 fd = do_open(fname, O_RDONLY, 0);
290 if (errno == ENOENT) {
291 enum logcode c = am_daemon
292 && protocol_version < 28 ? FERROR
294 io_error |= IOERR_VANISHED;
295 rprintf(c, "file has vanished: %s\n",
298 io_error |= IOERR_GENERAL;
299 rsyserr(FERROR_XFER, errno,
300 "send_files failed to open %s",
304 if (protocol_version >= 30)
305 send_msg_int(MSG_NO_SEND, ndx);
309 /* map the local file */
310 if (do_fstat(fd, &st) != 0) {
311 io_error |= IOERR_GENERAL;
312 rsyserr(FERROR, errno, "fstat failed");
315 exit_cleanup(RERR_PROTOCOL);
319 int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
320 mbuf = map_file(fd, st.st_size, read_size, s->blength);
325 rprintf(FINFO, "send_files mapped %s%s%s of size %.0f\n",
326 path,slash,fname, (double)st.st_size);
329 write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
330 fnamecmp_type, xname, xlen);
331 write_sum_head(f_xfer, s);
334 rprintf(FINFO, "calling match_sums %s%s%s\n", path,slash,fname);
336 if (log_before_transfer)
337 log_item(FCLIENT, file, &initial_stats, iflags, NULL);
338 else if (!am_server && verbose && do_progress)
339 rprintf(FCLIENT, "%s\n", fname);
341 set_compression(fname);
343 match_sums(f_xfer, s, mbuf, st.st_size);
345 end_progress(st.st_size);
347 log_item(log_code, file, &initial_stats, iflags, NULL);
350 j = unmap_file(mbuf);
352 io_error |= IOERR_GENERAL;
353 rsyserr(FERROR_XFER, j,
354 "read errors mapping %s",
363 rprintf(FINFO, "sender finished %s%s%s\n", path,slash,fname);
365 /* Flag that we actually sent this entry. */
366 file->flags |= FLAG_FILE_SENT;
368 if (make_backups < 0)
369 make_backups = -make_backups;
372 rprintf(FINFO, "send files finished\n");
376 write_ndx(f_out, NDX_DONE);