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 local_server;
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 want_xattr_optim;
34 extern int csum_length;
35 extern int append_mode;
36 extern int copy_links;
39 extern int whole_file;
40 extern int allowed_lull;
41 extern int copy_devices;
42 extern int preserve_xattrs;
43 extern int protocol_version;
44 extern int remove_source_files;
45 extern int updating_basis_file;
46 extern int make_backups;
48 extern int inplace_partial;
50 extern int write_batch;
51 extern int file_old_total;
52 extern BOOL want_progress_now;
53 extern struct stats stats;
54 extern struct file_list *cur_flist, *first_flist, *dir_flist;
55 extern char num_dev_ino_buf[4 + 8 + 8];
57 BOOL extra_flist_sending_enabled;
62 * The sender gets checksums from the generator, calculates deltas,
63 * and transmits them to the receiver. The sender process runs on the
64 * machine holding the source files.
68 * Receive the checksums for a buffer
70 static struct sum_struct *receive_sums(int f)
72 struct sum_struct *s = new(struct sum_struct);
73 int lull_mod = protocol_version >= 31 ? 0 : allowed_lull * 5;
81 if (DEBUG_GTE(DELTASUM, 3)) {
82 rprintf(FINFO, "count=%s n=%ld rem=%ld\n",
83 big_num(s->count), (long)s->blength, (long)s->remainder);
86 if (append_mode > 0) {
87 s->flength = (OFF_T)s->count * s->blength;
89 s->flength -= s->blength - s->remainder;
96 s->sums = new_array(struct sum_buf, s->count);
98 for (i = 0; i < s->count; i++) {
99 s->sums[i].sum1 = read_int(f);
100 read_buf(f, s->sums[i].sum2, s->s2length);
102 s->sums[i].offset = offset;
103 s->sums[i].flags = 0;
105 if (i == s->count-1 && s->remainder != 0)
106 s->sums[i].len = s->remainder;
108 s->sums[i].len = s->blength;
109 offset += s->sums[i].len;
111 if (lull_mod && !(i % lull_mod))
112 maybe_send_keepalive(time(NULL), True);
114 if (DEBUG_GTE(DELTASUM, 3)) {
116 "chunk[%d] len=%d offset=%s sum1=%08x\n",
117 i, s->sums[i].len, big_num(s->sums[i].offset),
127 void successful_send(int ndx)
129 char fname[MAXPATHLEN];
131 struct file_struct *file;
132 struct file_list *flist;
135 if (!remove_source_files)
138 flist = flist_for_ndx(ndx, "successful_send");
139 file = flist->files[ndx - flist->ndx_start];
140 if (!change_pathname(file, NULL, 0))
144 if ((copy_links ? do_stat(fname, &st) : do_lstat(fname, &st)) < 0) {
145 failed_op = "re-lstat";
150 && (int64)st.st_dev == IVAL64(num_dev_ino_buf, 4)
151 && (int64)st.st_ino == IVAL64(num_dev_ino_buf, 4 + 8)) {
152 rprintf(FERROR_XFER, "ERROR: Skipping sender remove of destination file: %s\n", fname);
156 if (st.st_size != F_LENGTH(file) || st.st_mtime != file->modtime
158 || (NSEC_BUMP(file) && (uint32)st.ST_MTIME_NSEC != F_MOD_NSEC(file))
161 rprintf(FERROR_XFER, "ERROR: Skipping sender remove for changed file: %s\n", fname);
165 if (do_unlink(fname) < 0) {
166 failed_op = "remove";
169 rprintf(FINFO, "sender file already removed: %s\n", fname);
171 rsyserr(FERROR_XFER, errno, "sender failed to %s %s", failed_op, fname);
173 if (INFO_GTE(REMOVE, 1))
174 rprintf(FINFO, "sender removed %s\n", fname);
178 static void write_ndx_and_attrs(int f_out, int ndx, int iflags,
179 const char *fname, struct file_struct *file,
180 uchar fnamecmp_type, char *buf, int len)
182 write_ndx(f_out, ndx);
183 if (protocol_version < 29)
185 write_shortint(f_out, iflags);
186 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
187 write_byte(f_out, fnamecmp_type);
188 if (iflags & ITEM_XNAME_FOLLOWS)
189 write_vstring(f_out, buf, len);
190 #ifdef SUPPORT_XATTRS
191 if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers
192 && !(want_xattr_optim && BITS_SET(iflags, ITEM_XNAME_FOLLOWS|ITEM_LOCAL_CHANGE)))
193 send_xattr_request(fname, file, f_out);
197 void send_files(int f_in, int f_out)
200 struct sum_struct *s;
201 struct map_struct *mbuf = NULL;
203 char fname[MAXPATHLEN], xname[MAXPATHLEN];
204 const char *path, *slash;
207 struct file_struct *file;
208 int phase = 0, max_phase = protocol_version >= 29 ? 2 : 1;
209 int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i;
210 enum logcode log_code = log_before_transfer ? FLOG : FINFO;
211 int f_xfer = write_batch < 0 ? batch_fd : f_out;
212 int save_io_error = io_error;
215 if (DEBUG_GTE(SEND, 1))
216 rprintf(FINFO, "send_files starting\n");
225 send_extra_file_list(f_out, MIN_FILECNT_LOOKAHEAD);
226 extra_flist_sending_enabled = !flist_eof;
229 /* This call also sets cur_flist. */
230 ndx = read_ndx_and_attrs(f_in, f_out, &iflags, &fnamecmp_type,
232 extra_flist_sending_enabled = False;
234 if (ndx == NDX_DONE) {
235 if (!am_server && cur_flist) {
236 set_current_file_index(NULL, 0);
237 if (INFO_GTE(PROGRESS, 2))
240 if (inc_recurse && first_flist) {
241 file_old_total -= first_flist->used;
242 flist_free(first_flist);
244 if (first_flist == cur_flist)
245 file_old_total = cur_flist->used;
246 write_ndx(f_out, NDX_DONE);
250 if (++phase > max_phase)
252 if (DEBUG_GTE(SEND, 1))
253 rprintf(FINFO, "send_files phase=%d\n", phase);
254 write_ndx(f_out, NDX_DONE);
259 send_extra_file_list(f_out, MIN_FILECNT_LOOKAHEAD);
261 if (ndx - cur_flist->ndx_start >= 0)
262 file = cur_flist->files[ndx - cur_flist->ndx_start];
264 file = dir_flist->files[cur_flist->parent_ndx];
265 if (F_PATHNAME(file)) {
266 path = F_PATHNAME(file);
271 if (!change_pathname(file, NULL, 0))
275 if (DEBUG_GTE(SEND, 1))
276 rprintf(FINFO, "send_files(%d, %s%s%s)\n", ndx, path,slash,fname);
278 #ifdef SUPPORT_XATTRS
279 if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers
280 && !(want_xattr_optim && BITS_SET(iflags, ITEM_XNAME_FOLLOWS|ITEM_LOCAL_CHANGE)))
281 recv_xattr_request(file, f_in);
284 if (!(iflags & ITEM_TRANSFER)) {
285 maybe_log_item(file, iflags, itemizing, xname);
286 write_ndx_and_attrs(f_out, ndx, iflags, fname, file, fnamecmp_type, xname, xlen);
287 if (iflags & ITEM_IS_NEW) {
288 stats.created_files++;
289 if (S_ISREG(file->mode)) {
290 /* Nothing further to count. */
291 } else if (S_ISDIR(file->mode))
292 stats.created_dirs++;
294 else if (S_ISLNK(file->mode))
295 stats.created_symlinks++;
297 else if (IS_DEVICE(file->mode))
298 stats.created_devices++;
300 stats.created_specials++;
306 "got transfer request in phase 2 [%s]\n",
308 exit_cleanup(RERR_PROTOCOL);
311 if (file->flags & FLAG_FILE_SENT) {
312 if (csum_length == SHORT_SUM_LENGTH) {
313 /* For inplace: redo phase turns off the backup
314 * flag so that we do a regular inplace send. */
315 make_backups = -make_backups;
316 append_mode = -append_mode;
317 csum_length = SUM_LENGTH;
320 if (csum_length != SHORT_SUM_LENGTH) {
321 make_backups = -make_backups;
322 append_mode = -append_mode;
323 csum_length = SHORT_SUM_LENGTH;
325 if (iflags & ITEM_IS_NEW)
326 stats.created_files++;
329 updating_basis_file = (inplace_partial && fnamecmp_type == FNAMECMP_PARTIAL_DIR)
330 || (inplace && (protocol_version >= 29 ? fnamecmp_type == FNAMECMP_FNAME : make_backups <= 0));
333 set_current_file_index(file, ndx);
334 stats.xferred_files++;
335 stats.total_transferred_size += F_LENGTH(file);
337 remember_initial_stats();
339 if (!do_xfers) { /* log the transfer */
340 log_item(FCLIENT, file, iflags, NULL);
341 write_ndx_and_attrs(f_out, ndx, iflags, fname, file, fnamecmp_type, xname, xlen);
345 if (!(s = receive_sums(f_in))) {
346 io_error |= IOERR_GENERAL;
347 rprintf(FERROR_XFER, "receive_sums failed\n");
348 exit_cleanup(RERR_PROTOCOL);
351 fd = do_open(fname, O_RDONLY, 0);
353 if (errno == ENOENT) {
354 enum logcode c = am_daemon && protocol_version < 28 ? FERROR : FWARNING;
355 io_error |= IOERR_VANISHED;
356 rprintf(c, "file has vanished: %s\n",
359 io_error |= IOERR_GENERAL;
360 rsyserr(FERROR_XFER, errno,
361 "send_files failed to open %s",
365 if (protocol_version >= 30)
366 send_msg_int(MSG_NO_SEND, ndx);
370 /* map the local file */
371 if (do_fstat(fd, &st) != 0) {
372 io_error |= IOERR_GENERAL;
373 rsyserr(FERROR_XFER, errno, "fstat failed");
376 exit_cleanup(RERR_FILEIO);
379 if (IS_DEVICE(st.st_mode)) {
381 rprintf(FERROR, "attempt to copy device contents without --copy-devices\n");
382 exit_cleanup(RERR_PROTOCOL);
385 st.st_size = get_device_size(fd, fname);
388 if (append_mode > 0 && st.st_size < F_LENGTH(file)) {
389 rprintf(FWARNING, "skipped diminished file: %s\n",
393 if (protocol_version >= 30)
394 send_msg_int(MSG_NO_SEND, ndx);
399 int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
400 mbuf = map_file(fd, st.st_size, read_size, s->blength);
404 if (DEBUG_GTE(DELTASUM, 2)) {
405 rprintf(FINFO, "send_files mapped %s%s%s of size %s\n",
406 path,slash,fname, big_num(st.st_size));
409 write_ndx_and_attrs(f_out, ndx, iflags, fname, file, fnamecmp_type, xname, xlen);
410 write_sum_head(f_xfer, s);
412 if (DEBUG_GTE(DELTASUM, 2))
413 rprintf(FINFO, "calling match_sums %s%s%s\n", path,slash,fname);
415 if (log_before_transfer)
416 log_item(FCLIENT, file, iflags, NULL);
417 else if (!am_server && INFO_GTE(NAME, 1) && INFO_EQ(PROGRESS, 1))
418 rprintf(FCLIENT, "%s\n", fname);
420 set_compression(fname);
422 match_sums(f_xfer, s, mbuf, st.st_size);
423 if (INFO_GTE(PROGRESS, 1))
424 end_progress(st.st_size);
425 else if (want_progress_now)
426 instant_progress(fname);
428 log_item(log_code, file, iflags, NULL);
431 j = unmap_file(mbuf);
433 io_error |= IOERR_GENERAL;
434 rsyserr(FERROR_XFER, j,
435 "read errors mapping %s",
443 if (DEBUG_GTE(SEND, 1))
444 rprintf(FINFO, "sender finished %s%s%s\n", path,slash,fname);
446 /* Flag that we actually sent this entry. */
447 file->flags |= FLAG_FILE_SENT;
449 if (make_backups < 0)
450 make_backups = -make_backups;
452 if (io_error != save_io_error && protocol_version >= 30)
453 send_msg_int(MSG_IO_ERROR, io_error);
455 if (DEBUG_GTE(SEND, 1))
456 rprintf(FINFO, "send files finished\n");
460 write_ndx(f_out, NDX_DONE);