Indentation tweak.
[rsync.git] / sender.c
blob2bbff2faae83ada289bd6f305e2d3cbea681bfaf
1 /*
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.
22 #include "rsync.h"
23 #include "inums.h"
25 extern int do_xfers;
26 extern int am_server;
27 extern int am_daemon;
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 xfer_sum_len;
35 extern int csum_length;
36 extern int append_mode;
37 extern int copy_links;
38 extern int io_error;
39 extern int flist_eof;
40 extern int whole_file;
41 extern int allowed_lull;
42 extern int copy_devices;
43 extern int preserve_xattrs;
44 extern int protocol_version;
45 extern int remove_source_files;
46 extern int updating_basis_file;
47 extern int make_backups;
48 extern int inplace;
49 extern int inplace_partial;
50 extern int batch_fd;
51 extern int write_batch;
52 extern int file_old_total;
53 extern BOOL want_progress_now;
54 extern struct stats stats;
55 extern struct file_list *cur_flist, *first_flist, *dir_flist;
56 extern char num_dev_ino_buf[4 + 8 + 8];
58 BOOL extra_flist_sending_enabled;
60 /**
61 * @file
63 * The sender gets checksums from the generator, calculates deltas,
64 * and transmits them to the receiver. The sender process runs on the
65 * machine holding the source files.
66 **/
68 /**
69 * Receive the checksums for a buffer
70 **/
71 static struct sum_struct *receive_sums(int f)
73 struct sum_struct *s = new(struct sum_struct);
74 int lull_mod = protocol_version >= 31 ? 0 : allowed_lull * 5;
75 OFF_T offset = 0;
76 int32 i;
78 read_sum_head(f, s);
80 s->sums = NULL;
82 if (DEBUG_GTE(DELTASUM, 3)) {
83 rprintf(FINFO, "count=%s n=%ld rem=%ld\n",
84 big_num(s->count), (long)s->blength, (long)s->remainder);
87 if (append_mode > 0) {
88 s->flength = (OFF_T)s->count * s->blength;
89 if (s->remainder)
90 s->flength -= s->blength - s->remainder;
91 return s;
94 if (s->count == 0)
95 return(s);
97 s->sums = new_array(struct sum_buf, s->count);
98 s->sum2_array = new_array(char, (size_t)s->count * xfer_sum_len);
100 for (i = 0; i < s->count; i++) {
101 s->sums[i].sum1 = read_int(f);
102 read_buf(f, sum2_at(s, i), s->s2length);
104 s->sums[i].offset = offset;
105 s->sums[i].flags = 0;
107 if (i == s->count-1 && s->remainder != 0)
108 s->sums[i].len = s->remainder;
109 else
110 s->sums[i].len = s->blength;
111 offset += s->sums[i].len;
113 if (lull_mod && !(i % lull_mod))
114 maybe_send_keepalive(time(NULL), True);
116 if (DEBUG_GTE(DELTASUM, 3)) {
117 rprintf(FINFO,
118 "chunk[%d] len=%d offset=%s sum1=%08x\n",
119 i, s->sums[i].len, big_num(s->sums[i].offset),
120 s->sums[i].sum1);
124 s->flength = offset;
126 return s;
129 void successful_send(int ndx)
131 char fname[MAXPATHLEN];
132 char *failed_op;
133 struct file_struct *file;
134 struct file_list *flist;
135 STRUCT_STAT st;
137 if (!remove_source_files)
138 return;
140 flist = flist_for_ndx(ndx, "successful_send");
141 file = flist->files[ndx - flist->ndx_start];
142 if (!change_pathname(file, NULL, 0))
143 return;
144 f_name(file, fname);
146 if ((copy_links ? do_stat(fname, &st) : do_lstat(fname, &st)) < 0) {
147 failed_op = "re-lstat";
148 goto failed;
151 if (local_server
152 && (int64)st.st_dev == IVAL64(num_dev_ino_buf, 4)
153 && (int64)st.st_ino == IVAL64(num_dev_ino_buf, 4 + 8)) {
154 rprintf(FERROR_XFER, "ERROR: Skipping sender remove of destination file: %s\n", fname);
155 return;
158 if (st.st_size != F_LENGTH(file) || st.st_mtime != file->modtime
159 #ifdef ST_MTIME_NSEC
160 || (NSEC_BUMP(file) && (uint32)st.ST_MTIME_NSEC != F_MOD_NSEC(file))
161 #endif
163 rprintf(FERROR_XFER, "ERROR: Skipping sender remove for changed file: %s\n", fname);
164 return;
167 if (do_unlink(fname) < 0) {
168 failed_op = "remove";
169 failed:
170 if (errno == ENOENT)
171 rprintf(FINFO, "sender file already removed: %s\n", fname);
172 else
173 rsyserr(FERROR_XFER, errno, "sender failed to %s %s", failed_op, fname);
174 } else {
175 if (INFO_GTE(REMOVE, 1))
176 rprintf(FINFO, "sender removed %s\n", fname);
180 static void write_ndx_and_attrs(int f_out, int ndx, int iflags,
181 const char *fname, struct file_struct *file,
182 uchar fnamecmp_type, char *buf, int len)
184 write_ndx(f_out, ndx);
185 if (protocol_version < 29)
186 return;
187 write_shortint(f_out, iflags);
188 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
189 write_byte(f_out, fnamecmp_type);
190 if (iflags & ITEM_XNAME_FOLLOWS)
191 write_vstring(f_out, buf, len);
192 #ifdef SUPPORT_XATTRS
193 if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers
194 && !(want_xattr_optim && BITS_SET(iflags, ITEM_XNAME_FOLLOWS|ITEM_LOCAL_CHANGE)))
195 send_xattr_request(fname, file, f_out);
196 #endif
199 void send_files(int f_in, int f_out)
201 int fd = -1;
202 struct sum_struct *s;
203 struct map_struct *mbuf = NULL;
204 STRUCT_STAT st;
205 char fname[MAXPATHLEN], xname[MAXPATHLEN];
206 const char *path, *slash;
207 uchar fnamecmp_type;
208 int iflags, xlen;
209 struct file_struct *file;
210 int phase = 0, max_phase = protocol_version >= 29 ? 2 : 1;
211 int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i;
212 enum logcode log_code = log_before_transfer ? FLOG : FINFO;
213 int f_xfer = write_batch < 0 ? batch_fd : f_out;
214 int save_io_error = io_error;
215 int ndx, j;
217 if (DEBUG_GTE(SEND, 1))
218 rprintf(FINFO, "send_files starting\n");
220 if (whole_file < 0)
221 whole_file = 0;
223 progress_init();
225 while (1) {
226 if (inc_recurse) {
227 send_extra_file_list(f_out, MIN_FILECNT_LOOKAHEAD);
228 extra_flist_sending_enabled = !flist_eof;
231 /* This call also sets cur_flist. */
232 ndx = read_ndx_and_attrs(f_in, f_out, &iflags, &fnamecmp_type,
233 xname, &xlen);
234 extra_flist_sending_enabled = False;
236 if (ndx == NDX_DONE) {
237 if (!am_server && cur_flist) {
238 set_current_file_index(NULL, 0);
239 if (INFO_GTE(PROGRESS, 2))
240 end_progress(0);
242 if (inc_recurse && first_flist) {
243 file_old_total -= first_flist->used;
244 flist_free(first_flist);
245 if (first_flist) {
246 if (first_flist == cur_flist)
247 file_old_total = cur_flist->used;
248 write_ndx(f_out, NDX_DONE);
249 continue;
252 if (++phase > max_phase)
253 break;
254 if (DEBUG_GTE(SEND, 1))
255 rprintf(FINFO, "send_files phase=%d\n", phase);
256 write_ndx(f_out, NDX_DONE);
257 continue;
260 if (inc_recurse)
261 send_extra_file_list(f_out, MIN_FILECNT_LOOKAHEAD);
263 if (ndx - cur_flist->ndx_start >= 0)
264 file = cur_flist->files[ndx - cur_flist->ndx_start];
265 else
266 file = dir_flist->files[cur_flist->parent_ndx];
267 if (F_PATHNAME(file)) {
268 path = F_PATHNAME(file);
269 slash = "/";
270 } else {
271 path = slash = "";
273 if (!change_pathname(file, NULL, 0))
274 continue;
275 f_name(file, fname);
277 if (DEBUG_GTE(SEND, 1))
278 rprintf(FINFO, "send_files(%d, %s%s%s)\n", ndx, path,slash,fname);
280 #ifdef SUPPORT_XATTRS
281 if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers
282 && !(want_xattr_optim && BITS_SET(iflags, ITEM_XNAME_FOLLOWS|ITEM_LOCAL_CHANGE)))
283 recv_xattr_request(file, f_in);
284 #endif
286 if (!(iflags & ITEM_TRANSFER)) {
287 maybe_log_item(file, iflags, itemizing, xname);
288 write_ndx_and_attrs(f_out, ndx, iflags, fname, file, fnamecmp_type, xname, xlen);
289 if (iflags & ITEM_IS_NEW) {
290 stats.created_files++;
291 if (S_ISREG(file->mode)) {
292 /* Nothing further to count. */
293 } else if (S_ISDIR(file->mode))
294 stats.created_dirs++;
295 #ifdef SUPPORT_LINKS
296 else if (S_ISLNK(file->mode))
297 stats.created_symlinks++;
298 #endif
299 else if (IS_DEVICE(file->mode))
300 stats.created_devices++;
301 else
302 stats.created_specials++;
304 continue;
306 if (phase == 2) {
307 rprintf(FERROR,
308 "got transfer request in phase 2 [%s]\n",
309 who_am_i());
310 exit_cleanup(RERR_PROTOCOL);
313 if (file->flags & FLAG_FILE_SENT) {
314 if (csum_length == SHORT_SUM_LENGTH) {
315 /* For inplace: redo phase turns off the backup
316 * flag so that we do a regular inplace send. */
317 make_backups = -make_backups;
318 append_mode = -append_mode;
319 csum_length = SUM_LENGTH;
321 } else {
322 if (csum_length != SHORT_SUM_LENGTH) {
323 make_backups = -make_backups;
324 append_mode = -append_mode;
325 csum_length = SHORT_SUM_LENGTH;
327 if (iflags & ITEM_IS_NEW)
328 stats.created_files++;
331 updating_basis_file = (inplace_partial && fnamecmp_type == FNAMECMP_PARTIAL_DIR)
332 || (inplace && (protocol_version >= 29 ? fnamecmp_type == FNAMECMP_FNAME : make_backups <= 0));
334 if (!am_server)
335 set_current_file_index(file, ndx);
336 stats.xferred_files++;
337 stats.total_transferred_size += F_LENGTH(file);
339 remember_initial_stats();
341 if (!do_xfers) { /* log the transfer */
342 log_item(FCLIENT, file, iflags, NULL);
343 write_ndx_and_attrs(f_out, ndx, iflags, fname, file, fnamecmp_type, xname, xlen);
344 continue;
347 if (!(s = receive_sums(f_in))) {
348 io_error |= IOERR_GENERAL;
349 rprintf(FERROR_XFER, "receive_sums failed\n");
350 exit_cleanup(RERR_PROTOCOL);
353 fd = do_open(fname, O_RDONLY, 0);
354 if (fd == -1) {
355 if (errno == ENOENT) {
356 enum logcode c = am_daemon && protocol_version < 28 ? FERROR : FWARNING;
357 io_error |= IOERR_VANISHED;
358 rprintf(c, "file has vanished: %s\n",
359 full_fname(fname));
360 } else {
361 io_error |= IOERR_GENERAL;
362 rsyserr(FERROR_XFER, errno,
363 "send_files failed to open %s",
364 full_fname(fname));
366 free_sums(s);
367 if (protocol_version >= 30)
368 send_msg_int(MSG_NO_SEND, ndx);
369 continue;
372 /* map the local file */
373 if (do_fstat(fd, &st) != 0) {
374 io_error |= IOERR_GENERAL;
375 rsyserr(FERROR_XFER, errno, "fstat failed");
376 free_sums(s);
377 close(fd);
378 exit_cleanup(RERR_FILEIO);
381 if (IS_DEVICE(st.st_mode)) {
382 if (!copy_devices) {
383 rprintf(FERROR, "attempt to copy device contents without --copy-devices\n");
384 exit_cleanup(RERR_PROTOCOL);
386 if (st.st_size == 0)
387 st.st_size = get_device_size(fd, fname);
390 if (append_mode > 0 && st.st_size < F_LENGTH(file)) {
391 rprintf(FWARNING, "skipped diminished file: %s\n",
392 full_fname(fname));
393 free_sums(s);
394 close(fd);
395 if (protocol_version >= 30)
396 send_msg_int(MSG_NO_SEND, ndx);
397 continue;
400 if (st.st_size) {
401 int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
402 mbuf = map_file(fd, st.st_size, read_size, s->blength);
403 } else
404 mbuf = NULL;
406 if (DEBUG_GTE(DELTASUM, 2)) {
407 rprintf(FINFO, "send_files mapped %s%s%s of size %s\n",
408 path,slash,fname, big_num(st.st_size));
411 write_ndx_and_attrs(f_out, ndx, iflags, fname, file, fnamecmp_type, xname, xlen);
412 write_sum_head(f_xfer, s);
414 if (DEBUG_GTE(DELTASUM, 2))
415 rprintf(FINFO, "calling match_sums %s%s%s\n", path,slash,fname);
417 if (log_before_transfer)
418 log_item(FCLIENT, file, iflags, NULL);
419 else if (!am_server && INFO_GTE(NAME, 1) && INFO_EQ(PROGRESS, 1))
420 rprintf(FCLIENT, "%s\n", fname);
422 set_compression(fname);
424 match_sums(f_xfer, s, mbuf, st.st_size);
425 if (INFO_GTE(PROGRESS, 1))
426 end_progress(st.st_size);
427 else if (want_progress_now)
428 instant_progress(fname);
430 log_item(log_code, file, iflags, NULL);
432 if (mbuf) {
433 j = unmap_file(mbuf);
434 if (j) {
435 io_error |= IOERR_GENERAL;
436 rsyserr(FERROR_XFER, j,
437 "read errors mapping %s",
438 full_fname(fname));
441 close(fd);
443 free_sums(s);
445 if (DEBUG_GTE(SEND, 1))
446 rprintf(FINFO, "sender finished %s%s%s\n", path,slash,fname);
448 /* Flag that we actually sent this entry. */
449 file->flags |= FLAG_FILE_SENT;
451 if (make_backups < 0)
452 make_backups = -make_backups;
454 if (io_error != save_io_error && protocol_version >= 30)
455 send_msg_int(MSG_IO_ERROR, io_error);
457 if (DEBUG_GTE(SEND, 1))
458 rprintf(FINFO, "send files finished\n");
460 match_report();
462 write_ndx(f_out, NDX_DONE);