9159da4db80ac598b50dda7ea76178624200b832
[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 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;
36 extern int io_error;
37 extern int flist_eof;
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;
46 extern int inplace;
47 extern int inplace_partial;
48 extern int batch_fd;
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;
54
55 BOOL extra_flist_sending_enabled;
56
57 /**
58  * @file
59  *
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.
63  **/
64
65 /**
66  * Receive the checksums for a buffer
67  **/
68 static struct sum_struct *receive_sums(int f)
69 {
70         struct sum_struct *s = new(struct sum_struct);
71         int lull_mod = protocol_version >= 31 ? 0 : allowed_lull * 5;
72         OFF_T offset = 0;
73         int32 i;
74
75         read_sum_head(f, s);
76
77         s->sums = NULL;
78
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);
82         }
83
84         if (append_mode > 0) {
85                 s->flength = (OFF_T)s->count * s->blength;
86                 if (s->remainder)
87                         s->flength -= s->blength - s->remainder;
88                 return s;
89         }
90
91         if (s->count == 0)
92                 return(s);
93
94         s->sums = new_array(struct sum_buf, s->count);
95
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);
99
100                 s->sums[i].offset = offset;
101                 s->sums[i].flags = 0;
102
103                 if (i == s->count-1 && s->remainder != 0)
104                         s->sums[i].len = s->remainder;
105                 else
106                         s->sums[i].len = s->blength;
107                 offset += s->sums[i].len;
108
109                 if (lull_mod && !(i % lull_mod))
110                         maybe_send_keepalive(time(NULL), True);
111
112                 if (DEBUG_GTE(DELTASUM, 3)) {
113                         rprintf(FINFO,
114                                 "chunk[%d] len=%d offset=%s sum1=%08x\n",
115                                 i, s->sums[i].len, big_num(s->sums[i].offset),
116                                 s->sums[i].sum1);
117                 }
118         }
119
120         s->flength = offset;
121
122         return s;
123 }
124
125 void successful_send(int ndx)
126 {
127         char fname[MAXPATHLEN];
128         char *failed_op;
129         struct file_struct *file;
130         struct file_list *flist;
131         STRUCT_STAT st;
132
133         if (!remove_source_files)
134                 return;
135
136         flist = flist_for_ndx(ndx, "successful_send");
137         file = flist->files[ndx - flist->ndx_start];
138         if (!change_pathname(file, NULL, 0))
139                 return;
140         f_name(file, fname);
141
142         if ((copy_links ? do_stat(fname, &st) : do_lstat(fname, &st)) < 0) {
143                 failed_op = "re-lstat";
144                 goto failed;
145         }
146
147         if (st.st_size != F_LENGTH(file) || st.st_mtime != file->modtime
148 #ifdef ST_MTIME_NSEC
149          || (NSEC_BUMP(file) && (uint32)st.ST_MTIME_NSEC != F_MOD_NSEC(file))
150 #endif
151         ) {
152                 rprintf(FERROR_XFER, "ERROR: Skipping sender remove for changed file: %s\n", fname);
153                 return;
154         }
155
156         if (do_unlink(fname) < 0) {
157                 failed_op = "remove";
158           failed:
159                 if (errno == ENOENT)
160                         rprintf(FINFO, "sender file already removed: %s\n", fname);
161                 else
162                         rsyserr(FERROR_XFER, errno, "sender failed to %s %s", failed_op, fname);
163         } else {
164                 if (INFO_GTE(REMOVE, 1))
165                         rprintf(FINFO, "sender removed %s\n", fname);
166         }
167 }
168
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)
172 {
173         write_ndx(f_out, ndx);
174         if (protocol_version < 29)
175                 return;
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);
185 #endif
186 }
187
188 void send_files(int f_in, int f_out)
189 {
190         int fd = -1;
191         struct sum_struct *s;
192         struct map_struct *mbuf = NULL;
193         STRUCT_STAT st;
194         char fname[MAXPATHLEN], xname[MAXPATHLEN];
195         const char *path, *slash;
196         uchar fnamecmp_type;
197         int iflags, xlen;
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;
204         int ndx, j;
205
206         if (DEBUG_GTE(SEND, 1))
207                 rprintf(FINFO, "send_files starting\n");
208
209         if (whole_file < 0)
210                 whole_file = 0;
211
212         progress_init();
213
214         while (1) {
215                 if (inc_recurse) {
216                         send_extra_file_list(f_out, MIN_FILECNT_LOOKAHEAD);
217                         extra_flist_sending_enabled = !flist_eof;
218                 }
219
220                 /* This call also sets cur_flist. */
221                 ndx = read_ndx_and_attrs(f_in, f_out, &iflags, &fnamecmp_type,
222                                          xname, &xlen);
223                 extra_flist_sending_enabled = False;
224
225                 if (ndx == NDX_DONE) {
226                         if (!am_server && cur_flist) {
227                                 set_current_file_index(NULL, 0);
228                                 if (INFO_GTE(PROGRESS, 2))
229                                         end_progress(0);
230                         }
231                         if (inc_recurse && first_flist) {
232                                 file_old_total -= first_flist->used;
233                                 flist_free(first_flist);
234                                 if (first_flist) {
235                                         if (first_flist == cur_flist)
236                                                 file_old_total = cur_flist->used;
237                                         write_ndx(f_out, NDX_DONE);
238                                         continue;
239                                 }
240                         }
241                         if (++phase > max_phase)
242                                 break;
243                         if (DEBUG_GTE(SEND, 1))
244                                 rprintf(FINFO, "send_files phase=%d\n", phase);
245                         write_ndx(f_out, NDX_DONE);
246                         continue;
247                 }
248
249                 if (inc_recurse)
250                         send_extra_file_list(f_out, MIN_FILECNT_LOOKAHEAD);
251
252                 if (ndx - cur_flist->ndx_start >= 0)
253                         file = cur_flist->files[ndx - cur_flist->ndx_start];
254                 else
255                         file = dir_flist->files[cur_flist->parent_ndx];
256                 if (F_PATHNAME(file)) {
257                         path = F_PATHNAME(file);
258                         slash = "/";
259                 } else {
260                         path = slash = "";
261                 }
262                 if (!change_pathname(file, NULL, 0))
263                         continue;
264                 f_name(file, fname);
265
266                 if (DEBUG_GTE(SEND, 1))
267                         rprintf(FINFO, "send_files(%d, %s%s%s)\n", ndx, path,slash,fname);
268
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);
273 #endif
274
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++;
284 #ifdef SUPPORT_LINKS
285                                 else if (S_ISLNK(file->mode))
286                                         stats.created_symlinks++;
287 #endif
288                                 else if (IS_DEVICE(file->mode))
289                                         stats.created_devices++;
290                                 else
291                                         stats.created_specials++;
292                         }
293                         continue;
294                 }
295                 if (phase == 2) {
296                         rprintf(FERROR,
297                                 "got transfer request in phase 2 [%s]\n",
298                                 who_am_i());
299                         exit_cleanup(RERR_PROTOCOL);
300                 }
301
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;
309                         }
310                 } else {
311                         if (csum_length != SHORT_SUM_LENGTH) {
312                                 make_backups = -make_backups;
313                                 append_mode = -append_mode;
314                                 csum_length = SHORT_SUM_LENGTH;
315                         }
316                         if (iflags & ITEM_IS_NEW)
317                                 stats.created_files++;
318                 }
319
320                 updating_basis_file = (inplace_partial && fnamecmp_type == FNAMECMP_PARTIAL_DIR)
321                     || (inplace && (protocol_version >= 29 ? fnamecmp_type == FNAMECMP_FNAME : make_backups <= 0));
322
323                 if (!am_server)
324                         set_current_file_index(file, ndx);
325                 stats.xferred_files++;
326                 stats.total_transferred_size += F_LENGTH(file);
327
328                 remember_initial_stats();
329
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);
333                         continue;
334                 }
335
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);
340                 }
341
342                 fd = do_open(fname, O_RDONLY, 0);
343                 if (fd == -1) {
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",
348                                         full_fname(fname));
349                         } else {
350                                 io_error |= IOERR_GENERAL;
351                                 rsyserr(FERROR_XFER, errno,
352                                         "send_files failed to open %s",
353                                         full_fname(fname));
354                         }
355                         free_sums(s);
356                         if (protocol_version >= 30)
357                                 send_msg_int(MSG_NO_SEND, ndx);
358                         continue;
359                 }
360
361                 /* map the local file */
362                 if (do_fstat(fd, &st) != 0) {
363                         io_error |= IOERR_GENERAL;
364                         rsyserr(FERROR_XFER, errno, "fstat failed");
365                         free_sums(s);
366                         close(fd);
367                         exit_cleanup(RERR_FILEIO);
368                 }
369
370                 if (IS_DEVICE(st.st_mode)) {
371                         if (!copy_devices) {
372                                 rprintf(FERROR, "attempt to copy device contents without --copy-devices\n");
373                                 exit_cleanup(RERR_PROTOCOL);
374                         }
375                         if (st.st_size == 0)
376                                 st.st_size = get_device_size(fd, fname);
377                 }
378
379                 if (append_mode > 0 && st.st_size < F_LENGTH(file)) {
380                         rprintf(FWARNING, "skipped diminished file: %s\n",
381                                 full_fname(fname));
382                         free_sums(s);
383                         close(fd);
384                         if (protocol_version >= 30)
385                                 send_msg_int(MSG_NO_SEND, ndx);
386                         continue;
387                 }
388
389                 if (st.st_size) {
390                         int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
391                         mbuf = map_file(fd, st.st_size, read_size, s->blength);
392                 } else
393                         mbuf = NULL;
394
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));
398                 }
399
400                 write_ndx_and_attrs(f_out, ndx, iflags, fname, file, fnamecmp_type, xname, xlen);
401                 write_sum_head(f_xfer, s);
402
403                 if (DEBUG_GTE(DELTASUM, 2))
404                         rprintf(FINFO, "calling match_sums %s%s%s\n", path,slash,fname);
405
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);
410
411                 set_compression(fname);
412
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);
418
419                 log_item(log_code, file, iflags, NULL);
420
421                 if (mbuf) {
422                         j = unmap_file(mbuf);
423                         if (j) {
424                                 io_error |= IOERR_GENERAL;
425                                 rsyserr(FERROR_XFER, j,
426                                         "read errors mapping %s",
427                                         full_fname(fname));
428                         }
429                 }
430                 close(fd);
431
432                 free_sums(s);
433
434                 if (DEBUG_GTE(SEND, 1))
435                         rprintf(FINFO, "sender finished %s%s%s\n", path,slash,fname);
436
437                 /* Flag that we actually sent this entry. */
438                 file->flags |= FLAG_FILE_SENT;
439         }
440         if (make_backups < 0)
441                 make_backups = -make_backups;
442
443         if (io_error != save_io_error && protocol_version >= 30)
444                 send_msg_int(MSG_IO_ERROR, io_error);
445
446         if (DEBUG_GTE(SEND, 1))
447                 rprintf(FINFO, "send files finished\n");
448
449         match_report();
450
451         write_ndx(f_out, NDX_DONE);
452 }