2 * Routines only used by the sending process.
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2003-2022 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.
28 extern int inc_recurse;
29 extern int log_before_transfer;
30 extern int stdout_format_has_i;
31 extern int logfile_format_has_i;
32 extern int want_xattr_optim;
33 extern int csum_length;
34 extern int append_mode;
35 extern int copy_links;
38 extern int whole_file;
39 extern int allowed_lull;
40 extern int copy_devices;
41 extern int preserve_xattrs;
42 extern int protocol_version;
43 extern int remove_source_files;
44 extern int updating_basis_file;
45 extern int make_backups;
47 extern int inplace_partial;
49 extern int write_batch;
50 extern int file_old_total;
51 extern BOOL want_progress_now;
52 extern struct stats stats;
53 extern struct file_list *cur_flist, *first_flist, *dir_flist;
55 BOOL extra_flist_sending_enabled;
60 * The sender gets checksums from the generator, calculates deltas,
61 * and transmits them to the receiver. The sender process runs on the
62 * machine holding the source files.
66 * Receive the checksums for a buffer
68 static struct sum_struct *receive_sums(int f)
70 struct sum_struct *s = new(struct sum_struct);
71 int lull_mod = protocol_version >= 31 ? 0 : allowed_lull * 5;
79 if (DEBUG_GTE(DELTASUM, 3)) {
80 rprintf(FINFO, "count=%s n=%ld rem=%ld\n",
81 big_num(s->count), (long)s->blength, (long)s->remainder);
84 if (append_mode > 0) {
85 s->flength = (OFF_T)s->count * s->blength;
87 s->flength -= s->blength - s->remainder;
94 s->sums = new_array(struct sum_buf, s->count);
96 for (i = 0; i < s->count; i++) {
97 s->sums[i].sum1 = read_int(f);
98 read_buf(f, s->sums[i].sum2, s->s2length);
100 s->sums[i].offset = offset;
101 s->sums[i].flags = 0;
103 if (i == s->count-1 && s->remainder != 0)
104 s->sums[i].len = s->remainder;
106 s->sums[i].len = s->blength;
107 offset += s->sums[i].len;
109 if (lull_mod && !(i % lull_mod))
110 maybe_send_keepalive(time(NULL), True);
112 if (DEBUG_GTE(DELTASUM, 3)) {
114 "chunk[%d] len=%d offset=%s sum1=%08x\n",
115 i, s->sums[i].len, big_num(s->sums[i].offset),
125 void successful_send(int ndx)
127 char fname[MAXPATHLEN];
129 struct file_struct *file;
130 struct file_list *flist;
133 if (!remove_source_files)
136 flist = flist_for_ndx(ndx, "successful_send");
137 file = flist->files[ndx - flist->ndx_start];
138 if (!change_pathname(file, NULL, 0))
142 if ((copy_links ? do_stat(fname, &st) : do_lstat(fname, &st)) < 0) {
143 failed_op = "re-lstat";
147 if (st.st_size != F_LENGTH(file) || st.st_mtime != file->modtime
149 || (NSEC_BUMP(file) && (uint32)st.ST_MTIME_NSEC != F_MOD_NSEC(file))
152 rprintf(FERROR_XFER, "ERROR: Skipping sender remove for changed file: %s\n", fname);
156 if (do_unlink(fname) < 0) {
157 failed_op = "remove";
160 rprintf(FINFO, "sender file already removed: %s\n", fname);
162 rsyserr(FERROR_XFER, errno, "sender failed to %s %s", failed_op, fname);
164 if (INFO_GTE(REMOVE, 1))
165 rprintf(FINFO, "sender removed %s\n", fname);
169 static void write_ndx_and_attrs(int f_out, int ndx, int iflags,
170 const char *fname, struct file_struct *file,
171 uchar fnamecmp_type, char *buf, int len)
173 write_ndx(f_out, ndx);
174 if (protocol_version < 29)
176 write_shortint(f_out, iflags);
177 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
178 write_byte(f_out, fnamecmp_type);
179 if (iflags & ITEM_XNAME_FOLLOWS)
180 write_vstring(f_out, buf, len);
181 #ifdef SUPPORT_XATTRS
182 if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers
183 && !(want_xattr_optim && BITS_SET(iflags, ITEM_XNAME_FOLLOWS|ITEM_LOCAL_CHANGE)))
184 send_xattr_request(fname, file, f_out);
188 void send_files(int f_in, int f_out)
191 struct sum_struct *s;
192 struct map_struct *mbuf = NULL;
194 char fname[MAXPATHLEN], xname[MAXPATHLEN];
195 const char *path, *slash;
198 struct file_struct *file;
199 int phase = 0, max_phase = protocol_version >= 29 ? 2 : 1;
200 int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i;
201 enum logcode log_code = log_before_transfer ? FLOG : FINFO;
202 int f_xfer = write_batch < 0 ? batch_fd : f_out;
203 int save_io_error = io_error;
206 if (DEBUG_GTE(SEND, 1))
207 rprintf(FINFO, "send_files starting\n");
216 send_extra_file_list(f_out, MIN_FILECNT_LOOKAHEAD);
217 extra_flist_sending_enabled = !flist_eof;
220 /* This call also sets cur_flist. */
221 ndx = read_ndx_and_attrs(f_in, f_out, &iflags, &fnamecmp_type,
223 extra_flist_sending_enabled = False;
225 if (ndx == NDX_DONE) {
226 if (!am_server && cur_flist) {
227 set_current_file_index(NULL, 0);
228 if (INFO_GTE(PROGRESS, 2))
231 if (inc_recurse && first_flist) {
232 file_old_total -= first_flist->used;
233 flist_free(first_flist);
235 if (first_flist == cur_flist)
236 file_old_total = cur_flist->used;
237 write_ndx(f_out, NDX_DONE);
241 if (++phase > max_phase)
243 if (DEBUG_GTE(SEND, 1))
244 rprintf(FINFO, "send_files phase=%d\n", phase);
245 write_ndx(f_out, NDX_DONE);
250 send_extra_file_list(f_out, MIN_FILECNT_LOOKAHEAD);
252 if (ndx - cur_flist->ndx_start >= 0)
253 file = cur_flist->files[ndx - cur_flist->ndx_start];
255 file = dir_flist->files[cur_flist->parent_ndx];
256 if (F_PATHNAME(file)) {
257 path = F_PATHNAME(file);
262 if (!change_pathname(file, NULL, 0))
266 if (DEBUG_GTE(SEND, 1))
267 rprintf(FINFO, "send_files(%d, %s%s%s)\n", ndx, path,slash,fname);
269 #ifdef SUPPORT_XATTRS
270 if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers
271 && !(want_xattr_optim && BITS_SET(iflags, ITEM_XNAME_FOLLOWS|ITEM_LOCAL_CHANGE)))
272 recv_xattr_request(file, f_in);
275 if (!(iflags & ITEM_TRANSFER)) {
276 maybe_log_item(file, iflags, itemizing, xname);
277 write_ndx_and_attrs(f_out, ndx, iflags, fname, file, fnamecmp_type, xname, xlen);
278 if (iflags & ITEM_IS_NEW) {
279 stats.created_files++;
280 if (S_ISREG(file->mode)) {
281 /* Nothing further to count. */
282 } else if (S_ISDIR(file->mode))
283 stats.created_dirs++;
285 else if (S_ISLNK(file->mode))
286 stats.created_symlinks++;
288 else if (IS_DEVICE(file->mode))
289 stats.created_devices++;
291 stats.created_specials++;
297 "got transfer request in phase 2 [%s]\n",
299 exit_cleanup(RERR_PROTOCOL);
302 if (file->flags & FLAG_FILE_SENT) {
303 if (csum_length == SHORT_SUM_LENGTH) {
304 /* For inplace: redo phase turns off the backup
305 * flag so that we do a regular inplace send. */
306 make_backups = -make_backups;
307 append_mode = -append_mode;
308 csum_length = SUM_LENGTH;
311 if (csum_length != SHORT_SUM_LENGTH) {
312 make_backups = -make_backups;
313 append_mode = -append_mode;
314 csum_length = SHORT_SUM_LENGTH;
316 if (iflags & ITEM_IS_NEW)
317 stats.created_files++;
320 updating_basis_file = (inplace_partial && fnamecmp_type == FNAMECMP_PARTIAL_DIR)
321 || (inplace && (protocol_version >= 29 ? fnamecmp_type == FNAMECMP_FNAME : make_backups <= 0));
324 set_current_file_index(file, ndx);
325 stats.xferred_files++;
326 stats.total_transferred_size += F_LENGTH(file);
328 remember_initial_stats();
330 if (!do_xfers) { /* log the transfer */
331 log_item(FCLIENT, file, iflags, NULL);
332 write_ndx_and_attrs(f_out, ndx, iflags, fname, file, fnamecmp_type, xname, xlen);
336 if (!(s = receive_sums(f_in))) {
337 io_error |= IOERR_GENERAL;
338 rprintf(FERROR_XFER, "receive_sums failed\n");
339 exit_cleanup(RERR_PROTOCOL);
342 fd = do_open(fname, O_RDONLY, 0);
344 if (errno == ENOENT) {
345 enum logcode c = am_daemon && protocol_version < 28 ? FERROR : FWARNING;
346 io_error |= IOERR_VANISHED;
347 rprintf(c, "file has vanished: %s\n",
350 io_error |= IOERR_GENERAL;
351 rsyserr(FERROR_XFER, errno,
352 "send_files failed to open %s",
356 if (protocol_version >= 30)
357 send_msg_int(MSG_NO_SEND, ndx);
361 /* map the local file */
362 if (do_fstat(fd, &st) != 0) {
363 io_error |= IOERR_GENERAL;
364 rsyserr(FERROR_XFER, errno, "fstat failed");
367 exit_cleanup(RERR_FILEIO);
370 if (IS_DEVICE(st.st_mode)) {
372 rprintf(FERROR, "attempt to copy device contents without --copy-devices\n");
373 exit_cleanup(RERR_PROTOCOL);
376 st.st_size = get_device_size(fd, fname);
379 if (append_mode > 0 && st.st_size < F_LENGTH(file)) {
380 rprintf(FWARNING, "skipped diminished file: %s\n",
384 if (protocol_version >= 30)
385 send_msg_int(MSG_NO_SEND, ndx);
390 int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
391 mbuf = map_file(fd, st.st_size, read_size, s->blength);
395 if (DEBUG_GTE(DELTASUM, 2)) {
396 rprintf(FINFO, "send_files mapped %s%s%s of size %s\n",
397 path,slash,fname, big_num(st.st_size));
400 write_ndx_and_attrs(f_out, ndx, iflags, fname, file, fnamecmp_type, xname, xlen);
401 write_sum_head(f_xfer, s);
403 if (DEBUG_GTE(DELTASUM, 2))
404 rprintf(FINFO, "calling match_sums %s%s%s\n", path,slash,fname);
406 if (log_before_transfer)
407 log_item(FCLIENT, file, iflags, NULL);
408 else if (!am_server && INFO_GTE(NAME, 1) && INFO_EQ(PROGRESS, 1))
409 rprintf(FCLIENT, "%s\n", fname);
411 set_compression(fname);
413 match_sums(f_xfer, s, mbuf, st.st_size);
414 if (INFO_GTE(PROGRESS, 1))
415 end_progress(st.st_size);
416 else if (want_progress_now)
417 instant_progress(fname);
419 log_item(log_code, file, iflags, NULL);
422 j = unmap_file(mbuf);
424 io_error |= IOERR_GENERAL;
425 rsyserr(FERROR_XFER, j,
426 "read errors mapping %s",
434 if (DEBUG_GTE(SEND, 1))
435 rprintf(FINFO, "sender finished %s%s%s\n", path,slash,fname);
437 /* Flag that we actually sent this entry. */
438 file->flags |= FLAG_FILE_SENT;
440 if (make_backups < 0)
441 make_backups = -make_backups;
443 if (io_error != save_io_error && protocol_version >= 30)
444 send_msg_int(MSG_IO_ERROR, io_error);
446 if (DEBUG_GTE(SEND, 1))
447 rprintf(FINFO, "send files finished\n");
451 write_ndx(f_out, NDX_DONE);