More tweaks for Actions.
[rsync.git] / sender.c
1 /*
2  * Routines only used by the sending process.
3  *
4  * Copyright (C) 1996 Andrew Tridgell
5  * Copyright (C) 1996 Paul Mackerras
6  * Copyright (C) 2003-2022 Wayne Davison
7  *
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.
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  *
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.
20  */
21
22 #include "rsync.h"
23 #include "inums.h"
24
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 csum_length;
35 extern int append_mode;
36 extern int copy_links;
37 extern int io_error;
38 extern int flist_eof;
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;
47 extern int inplace;
48 extern int inplace_partial;
49 extern int batch_fd;
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];
56
57 BOOL extra_flist_sending_enabled;
58
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  **/
66
67 /**
68  * Receive the checksums for a buffer
69  **/
70 static struct sum_struct *receive_sums(int f)
71 {
72         struct sum_struct *s = new(struct sum_struct);
73         int lull_mod = protocol_version >= 31 ? 0 : allowed_lull * 5;
74         OFF_T offset = 0;
75         int32 i;
76
77         read_sum_head(f, s);
78
79         s->sums = NULL;
80
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);
84         }
85
86         if (append_mode > 0) {
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
93         if (s->count == 0)
94                 return(s);
95
96         s->sums = new_array(struct sum_buf, s->count);
97
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);
101
102                 s->sums[i].offset = offset;
103                 s->sums[i].flags = 0;
104
105                 if (i == s->count-1 && s->remainder != 0)
106                         s->sums[i].len = s->remainder;
107                 else
108                         s->sums[i].len = s->blength;
109                 offset += s->sums[i].len;
110
111                 if (lull_mod && !(i % lull_mod))
112                         maybe_send_keepalive(time(NULL), True);
113
114                 if (DEBUG_GTE(DELTASUM, 3)) {
115                         rprintf(FINFO,
116                                 "chunk[%d] len=%d offset=%s sum1=%08x\n",
117                                 i, s->sums[i].len, big_num(s->sums[i].offset),
118                                 s->sums[i].sum1);
119                 }
120         }
121
122         s->flength = offset;
123
124         return s;
125 }
126
127 void successful_send(int ndx)
128 {
129         char fname[MAXPATHLEN];
130         char *failed_op;
131         struct file_struct *file;
132         struct file_list *flist;
133         STRUCT_STAT st;
134
135         if (!remove_source_files)
136                 return;
137
138         flist = flist_for_ndx(ndx, "successful_send");
139         file = flist->files[ndx - flist->ndx_start];
140         if (!change_pathname(file, NULL, 0))
141                 return;
142         f_name(file, fname);
143
144         if ((copy_links ? do_stat(fname, &st) : do_lstat(fname, &st)) < 0) {
145                 failed_op = "re-lstat";
146                 goto failed;
147         }
148
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
156         if (st.st_size != F_LENGTH(file) || st.st_mtime != file->modtime
157 #ifdef ST_MTIME_NSEC
158          || (NSEC_BUMP(file) && (uint32)st.ST_MTIME_NSEC != F_MOD_NSEC(file))
159 #endif
160         ) {
161                 rprintf(FERROR_XFER, "ERROR: Skipping sender remove for changed file: %s\n", fname);
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
171                         rsyserr(FERROR_XFER, errno, "sender failed to %s %s", failed_op, fname);
172         } else {
173                 if (INFO_GTE(REMOVE, 1))
174                         rprintf(FINFO, "sender removed %s\n", fname);
175         }
176 }
177
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)
181 {
182         write_ndx(f_out, ndx);
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);
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);
194 #endif
195 }
196
197 void send_files(int f_in, int f_out)
198 {
199         int fd = -1;
200         struct sum_struct *s;
201         struct map_struct *mbuf = NULL;
202         STRUCT_STAT st;
203         char fname[MAXPATHLEN], xname[MAXPATHLEN];
204         const char *path, *slash;
205         uchar fnamecmp_type;
206         int iflags, xlen;
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;
213         int ndx, j;
214
215         if (DEBUG_GTE(SEND, 1))
216                 rprintf(FINFO, "send_files starting\n");
217
218         if (whole_file < 0)
219                 whole_file = 0;
220
221         progress_init();
222
223         while (1) {
224                 if (inc_recurse) {
225                         send_extra_file_list(f_out, MIN_FILECNT_LOOKAHEAD);
226                         extra_flist_sending_enabled = !flist_eof;
227                 }
228
229                 /* This call also sets cur_flist. */
230                 ndx = read_ndx_and_attrs(f_in, f_out, &iflags, &fnamecmp_type,
231                                          xname, &xlen);
232                 extra_flist_sending_enabled = False;
233
234                 if (ndx == NDX_DONE) {
235                         if (!am_server && cur_flist) {
236                                 set_current_file_index(NULL, 0);
237                                 if (INFO_GTE(PROGRESS, 2))
238                                         end_progress(0);
239                         }
240                         if (inc_recurse && first_flist) {
241                                 file_old_total -= first_flist->used;
242                                 flist_free(first_flist);
243                                 if (first_flist) {
244                                         if (first_flist == cur_flist)
245                                                 file_old_total = cur_flist->used;
246                                         write_ndx(f_out, NDX_DONE);
247                                         continue;
248                                 }
249                         }
250                         if (++phase > max_phase)
251                                 break;
252                         if (DEBUG_GTE(SEND, 1))
253                                 rprintf(FINFO, "send_files phase=%d\n", phase);
254                         write_ndx(f_out, NDX_DONE);
255                         continue;
256                 }
257
258                 if (inc_recurse)
259                         send_extra_file_list(f_out, MIN_FILECNT_LOOKAHEAD);
260
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];
265                 if (F_PATHNAME(file)) {
266                         path = F_PATHNAME(file);
267                         slash = "/";
268                 } else {
269                         path = slash = "";
270                 }
271                 if (!change_pathname(file, NULL, 0))
272                         continue;
273                 f_name(file, fname);
274
275                 if (DEBUG_GTE(SEND, 1))
276                         rprintf(FINFO, "send_files(%d, %s%s%s)\n", ndx, path,slash,fname);
277
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);
282 #endif
283
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++;
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                         }
302                         continue;
303                 }
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                 }
310
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                         }
325                         if (iflags & ITEM_IS_NEW)
326                                 stats.created_files++;
327                 }
328
329                 updating_basis_file = (inplace_partial && fnamecmp_type == FNAMECMP_PARTIAL_DIR)
330                     || (inplace && (protocol_version >= 29 ? fnamecmp_type == FNAMECMP_FNAME : make_backups <= 0));
331
332                 if (!am_server)
333                         set_current_file_index(file, ndx);
334                 stats.xferred_files++;
335                 stats.total_transferred_size += F_LENGTH(file);
336
337                 remember_initial_stats();
338
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);
342                         continue;
343                 }
344
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);
349                 }
350
351                 fd = do_open(fname, O_RDONLY, 0);
352                 if (fd == -1) {
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",
357                                         full_fname(fname));
358                         } else {
359                                 io_error |= IOERR_GENERAL;
360                                 rsyserr(FERROR_XFER, errno,
361                                         "send_files failed to open %s",
362                                         full_fname(fname));
363                         }
364                         free_sums(s);
365                         if (protocol_version >= 30)
366                                 send_msg_int(MSG_NO_SEND, ndx);
367                         continue;
368                 }
369
370                 /* map the local file */
371                 if (do_fstat(fd, &st) != 0) {
372                         io_error |= IOERR_GENERAL;
373                         rsyserr(FERROR_XFER, errno, "fstat failed");
374                         free_sums(s);
375                         close(fd);
376                         exit_cleanup(RERR_FILEIO);
377                 }
378
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
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
398                 if (st.st_size) {
399                         int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
400                         mbuf = map_file(fd, st.st_size, read_size, s->blength);
401                 } else
402                         mbuf = NULL;
403
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));
407                 }
408
409                 write_ndx_and_attrs(f_out, ndx, iflags, fname, file, fnamecmp_type, xname, xlen);
410                 write_sum_head(f_xfer, s);
411
412                 if (DEBUG_GTE(DELTASUM, 2))
413                         rprintf(FINFO, "calling match_sums %s%s%s\n", path,slash,fname);
414
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);
419
420                 set_compression(fname);
421
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);
427
428                 log_item(log_code, file, iflags, NULL);
429
430                 if (mbuf) {
431                         j = unmap_file(mbuf);
432                         if (j) {
433                                 io_error |= IOERR_GENERAL;
434                                 rsyserr(FERROR_XFER, j,
435                                         "read errors mapping %s",
436                                         full_fname(fname));
437                         }
438                 }
439                 close(fd);
440
441                 free_sums(s);
442
443                 if (DEBUG_GTE(SEND, 1))
444                         rprintf(FINFO, "sender finished %s%s%s\n", path,slash,fname);
445
446                 /* Flag that we actually sent this entry. */
447                 file->flags |= FLAG_FILE_SENT;
448         }
449         if (make_backups < 0)
450                 make_backups = -make_backups;
451
452         if (io_error != save_io_error && protocol_version >= 30)
453                 send_msg_int(MSG_IO_ERROR, io_error);
454
455         if (DEBUG_GTE(SEND, 1))
456                 rprintf(FINFO, "send files finished\n");
457
458         match_report();
459
460         write_ndx(f_out, NDX_DONE);
461 }