Fix a typo.
[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-2020 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 allowed_lull;
39 extern int preserve_xattrs;
40 extern int protocol_version;
41 extern int remove_source_files;
42 extern int updating_basis_file;
43 extern int make_backups;
44 extern int inplace;
45 extern int inplace_partial;
46 extern int batch_fd;
47 extern int write_batch;
48 extern int file_old_total;
49 extern BOOL want_progress_now;
50 extern struct stats stats;
51 extern struct file_list *cur_flist, *first_flist, *dir_flist;
52
53 BOOL extra_flist_sending_enabled;
54
55 /**
56  * @file
57  *
58  * The sender gets checksums from the generator, calculates deltas,
59  * and transmits them to the receiver.  The sender process runs on the
60  * machine holding the source files.
61  **/
62
63 /**
64  * Receive the checksums for a buffer
65  **/
66 static struct sum_struct *receive_sums(int f)
67 {
68         struct sum_struct *s;
69         int32 i;
70         int lull_mod = protocol_version >= 31 ? 0 : allowed_lull * 5;
71         OFF_T offset = 0;
72
73         if (!(s = new(struct sum_struct)))
74                 out_of_memory("receive_sums");
75
76         read_sum_head(f, s);
77
78         s->sums = NULL;
79
80         if (DEBUG_GTE(DELTASUM, 3)) {
81                 rprintf(FINFO, "count=%s n=%ld rem=%ld\n",
82                         big_num(s->count), (long)s->blength, (long)s->remainder);
83         }
84
85         if (append_mode > 0) {
86                 s->flength = (OFF_T)s->count * s->blength;
87                 if (s->remainder)
88                         s->flength -= s->blength - s->remainder;
89                 return s;
90         }
91
92         if (s->count == 0)
93                 return(s);
94
95         if (!(s->sums = new_array(struct sum_buf, s->count)))
96                 out_of_memory("receive_sums");
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 (st.st_size != F_LENGTH(file) || st.st_mtime != file->modtime
150 #ifdef ST_MTIME_NSEC
151          || (NSEC_BUMP(file) && (uint32)st.ST_MTIME_NSEC != F_MOD_NSEC(file))
152 #endif
153         ) {
154                 rprintf(FERROR_XFER, "ERROR: Skipping sender remove for changed file: %s\n", fname);
155                 return;
156         }
157
158         if (do_unlink(fname) < 0) {
159                 failed_op = "remove";
160           failed:
161                 if (errno == ENOENT)
162                         rprintf(FINFO, "sender file already removed: %s\n", fname);
163                 else
164                         rsyserr(FERROR_XFER, errno, "sender failed to %s %s", failed_op, fname);
165         } else {
166                 if (INFO_GTE(REMOVE, 1))
167                         rprintf(FINFO, "sender removed %s\n", fname);
168         }
169 }
170
171 static void write_ndx_and_attrs(int f_out, int ndx, int iflags,
172                                 const char *fname, struct file_struct *file,
173                                 uchar fnamecmp_type, char *buf, int len)
174 {
175         write_ndx(f_out, ndx);
176         if (protocol_version < 29)
177                 return;
178         write_shortint(f_out, iflags);
179         if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
180                 write_byte(f_out, fnamecmp_type);
181         if (iflags & ITEM_XNAME_FOLLOWS)
182                 write_vstring(f_out, buf, len);
183 #ifdef SUPPORT_XATTRS
184         if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers
185          && !(want_xattr_optim && BITS_SET(iflags, ITEM_XNAME_FOLLOWS|ITEM_LOCAL_CHANGE)))
186                 send_xattr_request(fname, file, f_out);
187 #endif
188 }
189
190 void send_files(int f_in, int f_out)
191 {
192         int fd = -1;
193         struct sum_struct *s;
194         struct map_struct *mbuf = NULL;
195         STRUCT_STAT st;
196         char fname[MAXPATHLEN], xname[MAXPATHLEN];
197         const char *path, *slash;
198         uchar fnamecmp_type;
199         int iflags, xlen;
200         struct file_struct *file;
201         int phase = 0, max_phase = protocol_version >= 29 ? 2 : 1;
202         int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i;
203         enum logcode log_code = log_before_transfer ? FLOG : FINFO;
204         int f_xfer = write_batch < 0 ? batch_fd : f_out;
205         int save_io_error = io_error;
206         int ndx, j;
207
208         if (DEBUG_GTE(SEND, 1))
209                 rprintf(FINFO, "send_files starting\n");
210
211         progress_init();
212
213         while (1) {
214                 if (inc_recurse) {
215                         send_extra_file_list(f_out, MIN_FILECNT_LOOKAHEAD);
216                         extra_flist_sending_enabled = !flist_eof;
217                 }
218
219                 /* This call also sets cur_flist. */
220                 ndx = read_ndx_and_attrs(f_in, f_out, &iflags, &fnamecmp_type,
221                                          xname, &xlen);
222                 extra_flist_sending_enabled = False;
223
224                 if (ndx == NDX_DONE) {
225                         if (!am_server && cur_flist) {
226                                 set_current_file_index(NULL, 0);
227                                 if (INFO_GTE(PROGRESS, 2))
228                                         end_progress(0);
229                         }
230                         if (inc_recurse && first_flist) {
231                                 file_old_total -= first_flist->used;
232                                 flist_free(first_flist);
233                                 if (first_flist) {
234                                         if (first_flist == cur_flist)
235                                                 file_old_total = cur_flist->used;
236                                         write_ndx(f_out, NDX_DONE);
237                                         continue;
238                                 }
239                         }
240                         if (++phase > max_phase)
241                                 break;
242                         if (DEBUG_GTE(SEND, 1))
243                                 rprintf(FINFO, "send_files phase=%d\n", phase);
244                         write_ndx(f_out, NDX_DONE);
245                         continue;
246                 }
247
248                 if (inc_recurse)
249                         send_extra_file_list(f_out, MIN_FILECNT_LOOKAHEAD);
250
251                 if (ndx - cur_flist->ndx_start >= 0)
252                         file = cur_flist->files[ndx - cur_flist->ndx_start];
253                 else
254                         file = dir_flist->files[cur_flist->parent_ndx];
255                 if (F_PATHNAME(file)) {
256                         path = F_PATHNAME(file);
257                         slash = "/";
258                 } else {
259                         path = slash = "";
260                 }
261                 if (!change_pathname(file, NULL, 0))
262                         continue;
263                 f_name(file, fname);
264
265                 if (DEBUG_GTE(SEND, 1))
266                         rprintf(FINFO, "send_files(%d, %s%s%s)\n", ndx, path,slash,fname);
267
268 #ifdef SUPPORT_XATTRS
269                 if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers
270                  && !(want_xattr_optim && BITS_SET(iflags, ITEM_XNAME_FOLLOWS|ITEM_LOCAL_CHANGE)))
271                         recv_xattr_request(file, f_in);
272 #endif
273
274                 if (!(iflags & ITEM_TRANSFER)) {
275                         maybe_log_item(file, iflags, itemizing, xname);
276                         write_ndx_and_attrs(f_out, ndx, iflags, fname, file, fnamecmp_type, xname, xlen);
277                         if (iflags & ITEM_IS_NEW) {
278                                 stats.created_files++;
279                                 if (S_ISREG(file->mode)) {
280                                         /* Nothing further to count. */
281                                 } else if (S_ISDIR(file->mode))
282                                         stats.created_dirs++;
283 #ifdef SUPPORT_LINKS
284                                 else if (S_ISLNK(file->mode))
285                                         stats.created_symlinks++;
286 #endif
287                                 else if (IS_DEVICE(file->mode))
288                                         stats.created_devices++;
289                                 else
290                                         stats.created_specials++;
291                         }
292                         continue;
293                 }
294                 if (phase == 2) {
295                         rprintf(FERROR,
296                                 "got transfer request in phase 2 [%s]\n",
297                                 who_am_i());
298                         exit_cleanup(RERR_PROTOCOL);
299                 }
300
301                 if (file->flags & FLAG_FILE_SENT) {
302                         if (csum_length == SHORT_SUM_LENGTH) {
303                                 /* For inplace: redo phase turns off the backup
304                                  * flag so that we do a regular inplace send. */
305                                 make_backups = -make_backups;
306                                 append_mode = -append_mode;
307                                 csum_length = SUM_LENGTH;
308                         }
309                 } else {
310                         if (csum_length != SHORT_SUM_LENGTH) {
311                                 make_backups = -make_backups;
312                                 append_mode = -append_mode;
313                                 csum_length = SHORT_SUM_LENGTH;
314                         }
315                         if (iflags & ITEM_IS_NEW)
316                                 stats.created_files++;
317                 }
318
319                 updating_basis_file = (inplace_partial && fnamecmp_type == FNAMECMP_PARTIAL_DIR)
320                     || (inplace && (protocol_version >= 29 ? fnamecmp_type == FNAMECMP_FNAME : make_backups <= 0));
321
322                 if (!am_server)
323                         set_current_file_index(file, ndx);
324                 stats.xferred_files++;
325                 stats.total_transferred_size += F_LENGTH(file);
326
327                 remember_initial_stats();
328
329                 if (!do_xfers) { /* log the transfer */
330                         log_item(FCLIENT, file, iflags, NULL);
331                         write_ndx_and_attrs(f_out, ndx, iflags, fname, file, fnamecmp_type, xname, xlen);
332                         continue;
333                 }
334
335                 if (!(s = receive_sums(f_in))) {
336                         io_error |= IOERR_GENERAL;
337                         rprintf(FERROR_XFER, "receive_sums failed\n");
338                         exit_cleanup(RERR_PROTOCOL);
339                 }
340
341                 fd = do_open(fname, O_RDONLY, 0);
342                 if (fd == -1) {
343                         if (errno == ENOENT) {
344                                 enum logcode c = am_daemon && protocol_version < 28 ? FERROR : FWARNING;
345                                 io_error |= IOERR_VANISHED;
346                                 rprintf(c, "file has vanished: %s\n",
347                                         full_fname(fname));
348                         } else {
349                                 io_error |= IOERR_GENERAL;
350                                 rsyserr(FERROR_XFER, errno,
351                                         "send_files failed to open %s",
352                                         full_fname(fname));
353                         }
354                         free_sums(s);
355                         if (protocol_version >= 30)
356                                 send_msg_int(MSG_NO_SEND, ndx);
357                         continue;
358                 }
359
360                 /* map the local file */
361                 if (do_fstat(fd, &st) != 0) {
362                         io_error |= IOERR_GENERAL;
363                         rsyserr(FERROR_XFER, errno, "fstat failed");
364                         free_sums(s);
365                         close(fd);
366                         exit_cleanup(RERR_FILEIO);
367                 }
368
369                 if (st.st_size) {
370                         int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
371                         mbuf = map_file(fd, st.st_size, read_size, s->blength);
372                 } else
373                         mbuf = NULL;
374
375                 if (DEBUG_GTE(DELTASUM, 2)) {
376                         rprintf(FINFO, "send_files mapped %s%s%s of size %s\n",
377                                 path,slash,fname, big_num(st.st_size));
378                 }
379
380                 write_ndx_and_attrs(f_out, ndx, iflags, fname, file, fnamecmp_type, xname, xlen);
381                 write_sum_head(f_xfer, s);
382
383                 if (DEBUG_GTE(DELTASUM, 2))
384                         rprintf(FINFO, "calling match_sums %s%s%s\n", path,slash,fname);
385
386                 if (log_before_transfer)
387                         log_item(FCLIENT, file, iflags, NULL);
388                 else if (!am_server && INFO_GTE(NAME, 1) && INFO_EQ(PROGRESS, 1))
389                         rprintf(FCLIENT, "%s\n", fname);
390
391                 set_compression(fname);
392
393                 match_sums(f_xfer, s, mbuf, st.st_size);
394                 if (INFO_GTE(PROGRESS, 1))
395                         end_progress(st.st_size);
396                 else if (want_progress_now)
397                         instant_progress(fname);
398
399                 log_item(log_code, file, iflags, NULL);
400
401                 if (mbuf) {
402                         j = unmap_file(mbuf);
403                         if (j) {
404                                 io_error |= IOERR_GENERAL;
405                                 rsyserr(FERROR_XFER, j,
406                                         "read errors mapping %s",
407                                         full_fname(fname));
408                         }
409                 }
410                 close(fd);
411
412                 free_sums(s);
413
414                 if (DEBUG_GTE(SEND, 1))
415                         rprintf(FINFO, "sender finished %s%s%s\n", path,slash,fname);
416
417                 /* Flag that we actually sent this entry. */
418                 file->flags |= FLAG_FILE_SENT;
419         }
420         if (make_backups < 0)
421                 make_backups = -make_backups;
422
423         if (io_error != save_io_error && protocol_version >= 30)
424                 send_msg_int(MSG_IO_ERROR, io_error);
425
426         if (DEBUG_GTE(SEND, 1))
427                 rprintf(FINFO, "send files finished\n");
428
429         match_report();
430
431         write_ndx(f_out, NDX_DONE);
432 }