]> git.ipfire.org Git - thirdparty/rsync.git/blame - sender.c
More tweaks for Actions.
[thirdparty/rsync.git] / sender.c
CommitLineData
e6e3f12f 1/*
0f78b815
WD
2 * Routines only used by the sending process.
3 *
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
c3b553a9 6 * Copyright (C) 2003-2022 Wayne Davison
0f78b815
WD
7 *
8 * This program is free software; you can redistribute it and/or modify
8e41b68e
WD
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.
0f78b815
WD
12 *
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.
17 *
e7c67065 18 * You should have received a copy of the GNU General Public License along
4fd842f9 19 * with this program; if not, visit the http://fsf.org website.
0f78b815 20 */
2f03f956
AT
21
22#include "rsync.h"
5dd14f0c 23#include "inums.h"
2f03f956 24
a0545709 25extern int do_xfers;
8715db2c
WD
26extern int am_server;
27extern int am_daemon;
5183c0d6 28extern int local_server;
3ea6e0e7 29extern int inc_recurse;
8a9cfb24 30extern int log_before_transfer;
2fedf3d5 31extern int stdout_format_has_i;
ecc7623e 32extern int logfile_format_has_i;
ff08acd4 33extern int want_xattr_optim;
2f03f956 34extern int csum_length;
6cc11982 35extern int append_mode;
79332c0d 36extern int copy_links;
2f03f956 37extern int io_error;
7a7810aa 38extern int flist_eof;
dbb1c2d1 39extern int whole_file;
605fed4b 40extern int allowed_lull;
8aa46511 41extern int copy_devices;
16edf865 42extern int preserve_xattrs;
d6631cf3 43extern int protocol_version;
47c11975 44extern int remove_source_files;
53f8519a 45extern int updating_basis_file;
8b115ac8 46extern int make_backups;
53f8519a 47extern int inplace;
3a7bf54a 48extern int inplace_partial;
a0545709
WD
49extern int batch_fd;
50extern int write_batch;
fe16d9a6 51extern int file_old_total;
15fa9ab0 52extern BOOL want_progress_now;
a3221d2a 53extern struct stats stats;
6755a7d7 54extern struct file_list *cur_flist, *first_flist, *dir_flist;
5183c0d6 55extern char num_dev_ino_buf[4 + 8 + 8];
2f03f956 56
7a7810aa
WD
57BOOL extra_flist_sending_enabled;
58
0f9c48b1
MP
59/**
60 * @file
61 *
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.
65 **/
fc0257c9 66
ce8149b6
MP
67/**
68 * Receive the checksums for a buffer
69 **/
2f03f956
AT
70static struct sum_struct *receive_sums(int f)
71{
11eb67ee 72 struct sum_struct *s = new(struct sum_struct);
05c36015 73 int lull_mod = protocol_version >= 31 ? 0 : allowed_lull * 5;
2f03f956 74 OFF_T offset = 0;
11eb67ee 75 int32 i;
2f03f956 76
fc0257c9
S
77 read_sum_head(f, s);
78
2f03f956
AT
79 s->sums = NULL;
80
5a18b34d 81 if (DEBUG_GTE(DELTASUM, 3)) {
6d56efa6 82 rprintf(FINFO, "count=%s n=%ld rem=%ld\n",
adc2476f 83 big_num(s->count), (long)s->blength, (long)s->remainder);
da9d12f5 84 }
2f03f956 85
f3d6d480 86 if (append_mode > 0) {
6cc11982
WD
87 s->flength = (OFF_T)s->count * s->blength;
88 if (s->remainder)
89 s->flength -= s->blength - s->remainder;
90 return s;
91 }
92
e6e3f12f 93 if (s->count == 0)
2f03f956
AT
94 return(s);
95
11eb67ee 96 s->sums = new_array(struct sum_buf, s->count);
2f03f956 97
eed47b6b 98 for (i = 0; i < s->count; i++) {
2f03f956 99 s->sums[i].sum1 = read_int(f);
e6e3f12f 100 read_buf(f, s->sums[i].sum2, s->s2length);
2f03f956
AT
101
102 s->sums[i].offset = offset;
a3221d2a 103 s->sums[i].flags = 0;
2f03f956 104
eed47b6b 105 if (i == s->count-1 && s->remainder != 0)
2f03f956 106 s->sums[i].len = s->remainder;
a3221d2a 107 else
fc0257c9 108 s->sums[i].len = s->blength;
2f03f956
AT
109 offset += s->sums[i].len;
110
05c36015 111 if (lull_mod && !(i % lull_mod))
92d02148 112 maybe_send_keepalive(time(NULL), True);
605fed4b 113
5a18b34d 114 if (DEBUG_GTE(DELTASUM, 3)) {
a3221d2a 115 rprintf(FINFO,
6d56efa6 116 "chunk[%d] len=%d offset=%s sum1=%08x\n",
adc2476f 117 i, s->sums[i].len, big_num(s->sums[i].offset),
a3221d2a
WD
118 s->sums[i].sum1);
119 }
2f03f956
AT
120 }
121
122 s->flength = offset;
123
124 return s;
125}
126
5f40615c 127void successful_send(int ndx)
95306d9d
WD
128{
129 char fname[MAXPATHLEN];
852585b1 130 char *failed_op;
95306d9d 131 struct file_struct *file;
f3d6d480 132 struct file_list *flist;
852585b1 133 STRUCT_STAT st;
95306d9d 134
f3d6d480 135 if (!remove_source_files)
95306d9d
WD
136 return;
137
e982d591 138 flist = flist_for_ndx(ndx, "successful_send");
f3d6d480 139 file = flist->files[ndx - flist->ndx_start];
29a89172 140 if (!change_pathname(file, NULL, 0))
f3d6d480
WD
141 return;
142 f_name(file, fname);
143
79332c0d 144 if ((copy_links ? do_stat(fname, &st) : do_lstat(fname, &st)) < 0) {
852585b1
WD
145 failed_op = "re-lstat";
146 goto failed;
147 }
148
5183c0d6
WD
149 if (local_server
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);
153 return;
154 }
155
79332c0d 156 if (st.st_size != F_LENGTH(file) || st.st_mtime != file->modtime
852585b1
WD
157#ifdef ST_MTIME_NSEC
158 || (NSEC_BUMP(file) && (uint32)st.ST_MTIME_NSEC != F_MOD_NSEC(file))
159#endif
79332c0d 160 ) {
de8ec0b3 161 rprintf(FERROR_XFER, "ERROR: Skipping sender remove for changed file: %s\n", fname);
852585b1
WD
162 return;
163 }
164
165 if (do_unlink(fname) < 0) {
166 failed_op = "remove";
167 failed:
168 if (errno == ENOENT)
169 rprintf(FINFO, "sender file already removed: %s\n", fname);
170 else
de8ec0b3 171 rsyserr(FERROR_XFER, errno, "sender failed to %s %s", failed_op, fname);
852585b1 172 } else {
951e826b 173 if (INFO_GTE(REMOVE, 1))
f3d6d480 174 rprintf(FINFO, "sender removed %s\n", fname);
852585b1 175 }
95306d9d 176}
2f03f956 177
16edf865
WD
178static 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)
d144e43b 181{
9ae7a2cd 182 write_ndx(f_out, ndx);
d144e43b
WD
183 if (protocol_version < 29)
184 return;
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);
16edf865 190#ifdef SUPPORT_XATTRS
ff08acd4
WD
191 if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers
192 && !(want_xattr_optim && BITS_SET(iflags, ITEM_XNAME_FOLLOWS|ITEM_LOCAL_CHANGE)))
16edf865
WD
193 send_xattr_request(fname, file, f_out);
194#endif
d144e43b
WD
195}
196
f3d6d480 197void send_files(int f_in, int f_out)
e6e3f12f 198{
c1659c79 199 int fd = -1;
2f03f956 200 struct sum_struct *s;
41cc97ae 201 struct map_struct *mbuf = NULL;
2f03f956 202 STRUCT_STAT st;
f3d6d480
WD
203 char fname[MAXPATHLEN], xname[MAXPATHLEN];
204 const char *path, *slash;
4d53c4dd 205 uchar fnamecmp_type;
6087ef2a 206 int iflags, xlen;
2f03f956 207 struct file_struct *file;
11290705 208 int phase = 0, max_phase = protocol_version >= 29 ? 2 : 1;
2fedf3d5
WD
209 int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i;
210 enum logcode log_code = log_before_transfer ? FLOG : FINFO;
a0545709 211 int f_xfer = write_batch < 0 ? batch_fd : f_out;
59924aa8 212 int save_io_error = io_error;
f3d6d480 213 int ndx, j;
2f03f956 214
951e826b 215 if (DEBUG_GTE(SEND, 1))
e6e3f12f 216 rprintf(FINFO, "send_files starting\n");
2f03f956 217
dbb1c2d1
WD
218 if (whole_file < 0)
219 whole_file = 0;
220
15fa9ab0
WD
221 progress_init();
222
2f03f956 223 while (1) {
7a7810aa 224 if (inc_recurse) {
ce827c3e 225 send_extra_file_list(f_out, MIN_FILECNT_LOOKAHEAD);
7a7810aa
WD
226 extra_flist_sending_enabled = !flist_eof;
227 }
f3d6d480
WD
228
229 /* This call also sets cur_flist. */
20caffd2 230 ndx = read_ndx_and_attrs(f_in, f_out, &iflags, &fnamecmp_type,
16edf865 231 xname, &xlen);
7a7810aa
WD
232 extra_flist_sending_enabled = False;
233
f3d6d480 234 if (ndx == NDX_DONE) {
15fa9ab0 235 if (!am_server && cur_flist) {
951e826b 236 set_current_file_index(NULL, 0);
15fa9ab0
WD
237 if (INFO_GTE(PROGRESS, 2))
238 end_progress(0);
951e826b 239 }
3ea6e0e7 240 if (inc_recurse && first_flist) {
fe16d9a6 241 file_old_total -= first_flist->used;
f3d6d480
WD
242 flist_free(first_flist);
243 if (first_flist) {
fe16d9a6
WD
244 if (first_flist == cur_flist)
245 file_old_total = cur_flist->used;
9ae7a2cd 246 write_ndx(f_out, NDX_DONE);
f3d6d480
WD
247 continue;
248 }
249 }
11290705 250 if (++phase > max_phase)
8b48bf11 251 break;
951e826b 252 if (DEBUG_GTE(SEND, 1))
8b48bf11 253 rprintf(FINFO, "send_files phase=%d\n", phase);
9ae7a2cd 254 write_ndx(f_out, NDX_DONE);
8b48bf11 255 continue;
2f03f956
AT
256 }
257
16edf865 258 if (inc_recurse)
ce827c3e 259 send_extra_file_list(f_out, MIN_FILECNT_LOOKAHEAD);
16edf865 260
6755a7d7
WD
261 if (ndx - cur_flist->ndx_start >= 0)
262 file = cur_flist->files[ndx - cur_flist->ndx_start];
263 else
264 file = dir_flist->files[cur_flist->parent_ndx];
8fc4033e
WD
265 if (F_PATHNAME(file)) {
266 path = F_PATHNAME(file);
f3d6d480
WD
267 slash = "/";
268 } else {
269 path = slash = "";
270 }
29a89172 271 if (!change_pathname(file, NULL, 0))
f3d6d480
WD
272 continue;
273 f_name(file, fname);
7f37167d 274
951e826b 275 if (DEBUG_GTE(SEND, 1))
f3d6d480 276 rprintf(FINFO, "send_files(%d, %s%s%s)\n", ndx, path,slash,fname);
8a9cfb24 277
16edf865 278#ifdef SUPPORT_XATTRS
ff08acd4
WD
279 if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers
280 && !(want_xattr_optim && BITS_SET(iflags, ITEM_XNAME_FOLLOWS|ITEM_LOCAL_CHANGE)))
16edf865
WD
281 recv_xattr_request(file, f_in);
282#endif
283
fad3dc42 284 if (!(iflags & ITEM_TRANSFER)) {
6087ef2a 285 maybe_log_item(file, iflags, itemizing, xname);
e63ff70e 286 write_ndx_and_attrs(f_out, ndx, iflags, fname, file, fnamecmp_type, xname, xlen);
e366e530
WD
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++;
293#ifdef SUPPORT_LINKS
294 else if (S_ISLNK(file->mode))
295 stats.created_symlinks++;
296#endif
297 else if (IS_DEVICE(file->mode))
298 stats.created_devices++;
299 else
300 stats.created_specials++;
301 }
165e6d44
WD
302 continue;
303 }
11290705
WD
304 if (phase == 2) {
305 rprintf(FERROR,
306 "got transfer request in phase 2 [%s]\n",
307 who_am_i());
308 exit_cleanup(RERR_PROTOCOL);
309 }
8a9cfb24 310
f3d6d480
WD
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;
318 }
319 } else {
320 if (csum_length != SHORT_SUM_LENGTH) {
321 make_backups = -make_backups;
322 append_mode = -append_mode;
323 csum_length = SHORT_SUM_LENGTH;
324 }
e366e530
WD
325 if (iflags & ITEM_IS_NEW)
326 stats.created_files++;
f3d6d480
WD
327 }
328
3a7bf54a
WD
329 updating_basis_file = (inplace_partial && fnamecmp_type == FNAMECMP_PARTIAL_DIR)
330 || (inplace && (protocol_version >= 29 ? fnamecmp_type == FNAMECMP_FNAME : make_backups <= 0));
b951e023 331
15fa9ab0 332 if (!am_server)
65b4e4b2 333 set_current_file_index(file, ndx);
e366e530 334 stats.xferred_files++;
112d728f 335 stats.total_transferred_size += F_LENGTH(file);
2f03f956 336
cce44865 337 remember_initial_stats();
4dde3347 338
a0545709 339 if (!do_xfers) { /* log the transfer */
4dde3347 340 log_item(FCLIENT, file, iflags, NULL);
e63ff70e 341 write_ndx_and_attrs(f_out, ndx, iflags, fname, file, fnamecmp_type, xname, xlen);
2f03f956
AT
342 continue;
343 }
344
efa95a18 345 if (!(s = receive_sums(f_in))) {
06c28400 346 io_error |= IOERR_GENERAL;
342bfb5e 347 rprintf(FERROR_XFER, "receive_sums failed\n");
f3d6d480 348 exit_cleanup(RERR_PROTOCOL);
2f03f956 349 }
76f79ba7 350
b9f592fb
WD
351 fd = do_open(fname, O_RDONLY, 0);
352 if (fd == -1) {
353 if (errno == ENOENT) {
e63ff70e 354 enum logcode c = am_daemon && protocol_version < 28 ? FERROR : FWARNING;
b9f592fb
WD
355 io_error |= IOERR_VANISHED;
356 rprintf(c, "file has vanished: %s\n",
357 full_fname(fname));
358 } else {
06c28400 359 io_error |= IOERR_GENERAL;
3f0211b6 360 rsyserr(FERROR_XFER, errno,
b9f592fb
WD
361 "send_files failed to open %s",
362 full_fname(fname));
41cc97ae 363 }
b9f592fb 364 free_sums(s);
f3d6d480
WD
365 if (protocol_version >= 30)
366 send_msg_int(MSG_NO_SEND, ndx);
b9f592fb
WD
367 continue;
368 }
e6e3f12f 369
b9f592fb
WD
370 /* map the local file */
371 if (do_fstat(fd, &st) != 0) {
372 io_error |= IOERR_GENERAL;
342bfb5e 373 rsyserr(FERROR_XFER, errno, "fstat failed");
b9f592fb
WD
374 free_sums(s);
375 close(fd);
f397616e 376 exit_cleanup(RERR_FILEIO);
b9f592fb 377 }
11a5a3c7 378
8aa46511
WD
379 if (IS_DEVICE(st.st_mode)) {
380 if (!copy_devices) {
381 rprintf(FERROR, "attempt to copy device contents without --copy-devices\n");
382 exit_cleanup(RERR_PROTOCOL);
383 }
384 if (st.st_size == 0)
385 st.st_size = get_device_size(fd, fname);
386 }
387
9cd85b84
WD
388 if (append_mode > 0 && st.st_size < F_LENGTH(file)) {
389 rprintf(FWARNING, "skipped diminished file: %s\n",
390 full_fname(fname));
391 free_sums(s);
392 close(fd);
393 if (protocol_version >= 30)
394 send_msg_int(MSG_NO_SEND, ndx);
395 continue;
396 }
397
96d910c7 398 if (st.st_size) {
9b919d59
WD
399 int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
400 mbuf = map_file(fd, st.st_size, read_size, s->blength);
96d910c7
WD
401 } else
402 mbuf = NULL;
6902ed17 403
5a18b34d 404 if (DEBUG_GTE(DELTASUM, 2)) {
6d56efa6 405 rprintf(FINFO, "send_files mapped %s%s%s of size %s\n",
adc2476f 406 path,slash,fname, big_num(st.st_size));
6902ed17 407 }
e6e3f12f 408
e63ff70e 409 write_ndx_and_attrs(f_out, ndx, iflags, fname, file, fnamecmp_type, xname, xlen);
a0545709 410 write_sum_head(f_xfer, s);
b9f592fb 411
5a18b34d 412 if (DEBUG_GTE(DELTASUM, 2))
f3d6d480 413 rprintf(FINFO, "calling match_sums %s%s%s\n", path,slash,fname);
83fff1aa 414
8a9cfb24 415 if (log_before_transfer)
4dde3347 416 log_item(FCLIENT, file, iflags, NULL);
951e826b 417 else if (!am_server && INFO_GTE(NAME, 1) && INFO_EQ(PROGRESS, 1))
f3d6d480 418 rprintf(FCLIENT, "%s\n", fname);
ecc81fce 419
83fff1aa 420 set_compression(fname);
1b7c47cb 421
a0545709 422 match_sums(f_xfer, s, mbuf, st.st_size);
951e826b 423 if (INFO_GTE(PROGRESS, 1))
0394e34a 424 end_progress(st.st_size);
15fa9ab0
WD
425 else if (want_progress_now)
426 instant_progress(fname);
0394e34a 427
4dde3347 428 log_item(log_code, file, iflags, NULL);
6902ed17 429
b9f592fb
WD
430 if (mbuf) {
431 j = unmap_file(mbuf);
432 if (j) {
433 io_error |= IOERR_GENERAL;
3f0211b6 434 rsyserr(FERROR_XFER, j,
b9f592fb
WD
435 "read errors mapping %s",
436 full_fname(fname));
6a7cc46c 437 }
6902ed17 438 }
b9f592fb 439 close(fd);
e6e3f12f 440
2f03f956 441 free_sums(s);
e6e3f12f 442
951e826b 443 if (DEBUG_GTE(SEND, 1))
f3d6d480 444 rprintf(FINFO, "sender finished %s%s%s\n", path,slash,fname);
95306d9d
WD
445
446 /* Flag that we actually sent this entry. */
f3d6d480 447 file->flags |= FLAG_FILE_SENT;
2f03f956 448 }
f3d6d480
WD
449 if (make_backups < 0)
450 make_backups = -make_backups;
2f03f956 451
59924aa8
WD
452 if (io_error != save_io_error && protocol_version >= 30)
453 send_msg_int(MSG_IO_ERROR, io_error);
454
951e826b 455 if (DEBUG_GTE(SEND, 1))
e6e3f12f 456 rprintf(FINFO, "send files finished\n");
2f03f956
AT
457
458 match_report();
459
9ae7a2cd 460 write_ndx(f_out, NDX_DONE);
2f03f956 461}