Preparing for release of 3.2.4pre3
[rsync.git] / sender.c
blob92724c816dd5ec09428343bc37f47a8dfd0ecf11
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 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 preserve_xattrs;
41 extern int protocol_version;
42 extern int remove_source_files;
43 extern int updating_basis_file;
44 extern int make_backups;
45 extern int inplace;
46 extern int inplace_partial;
47 extern int batch_fd;
48 extern int write_batch;
49 extern int file_old_total;
50 extern BOOL want_progress_now;
51 extern struct stats stats;
52 extern struct file_list *cur_flist, *first_flist, *dir_flist;
54 BOOL extra_flist_sending_enabled;
56 /**
57 * @file
59 * The sender gets checksums from the generator, calculates deltas,
60 * and transmits them to the receiver. The sender process runs on the
61 * machine holding the source files.
62 **/
64 /**
65 * Receive the checksums for a buffer
66 **/
67 static struct sum_struct *receive_sums(int f)
69 struct sum_struct *s = new(struct sum_struct);
70 int lull_mod = protocol_version >= 31 ? 0 : allowed_lull * 5;
71 OFF_T offset = 0;
72 int32 i;
74 read_sum_head(f, s);
76 s->sums = NULL;
78 if (DEBUG_GTE(DELTASUM, 3)) {
79 rprintf(FINFO, "count=%s n=%ld rem=%ld\n",
80 big_num(s->count), (long)s->blength, (long)s->remainder);
83 if (append_mode > 0) {
84 s->flength = (OFF_T)s->count * s->blength;
85 if (s->remainder)
86 s->flength -= s->blength - s->remainder;
87 return s;
90 if (s->count == 0)
91 return(s);
93 s->sums = new_array(struct sum_buf, s->count);
95 for (i = 0; i < s->count; i++) {
96 s->sums[i].sum1 = read_int(f);
97 read_buf(f, s->sums[i].sum2, s->s2length);
99 s->sums[i].offset = offset;
100 s->sums[i].flags = 0;
102 if (i == s->count-1 && s->remainder != 0)
103 s->sums[i].len = s->remainder;
104 else
105 s->sums[i].len = s->blength;
106 offset += s->sums[i].len;
108 if (lull_mod && !(i % lull_mod))
109 maybe_send_keepalive(time(NULL), True);
111 if (DEBUG_GTE(DELTASUM, 3)) {
112 rprintf(FINFO,
113 "chunk[%d] len=%d offset=%s sum1=%08x\n",
114 i, s->sums[i].len, big_num(s->sums[i].offset),
115 s->sums[i].sum1);
119 s->flength = offset;
121 return s;
124 void successful_send(int ndx)
126 char fname[MAXPATHLEN];
127 char *failed_op;
128 struct file_struct *file;
129 struct file_list *flist;
130 STRUCT_STAT st;
132 if (!remove_source_files)
133 return;
135 flist = flist_for_ndx(ndx, "successful_send");
136 file = flist->files[ndx - flist->ndx_start];
137 if (!change_pathname(file, NULL, 0))
138 return;
139 f_name(file, fname);
141 if ((copy_links ? do_stat(fname, &st) : do_lstat(fname, &st)) < 0) {
142 failed_op = "re-lstat";
143 goto failed;
146 if (st.st_size != F_LENGTH(file) || st.st_mtime != file->modtime
147 #ifdef ST_MTIME_NSEC
148 || (NSEC_BUMP(file) && (uint32)st.ST_MTIME_NSEC != F_MOD_NSEC(file))
149 #endif
151 rprintf(FERROR_XFER, "ERROR: Skipping sender remove for changed file: %s\n", fname);
152 return;
155 if (do_unlink(fname) < 0) {
156 failed_op = "remove";
157 failed:
158 if (errno == ENOENT)
159 rprintf(FINFO, "sender file already removed: %s\n", fname);
160 else
161 rsyserr(FERROR_XFER, errno, "sender failed to %s %s", failed_op, fname);
162 } else {
163 if (INFO_GTE(REMOVE, 1))
164 rprintf(FINFO, "sender removed %s\n", fname);
168 static void write_ndx_and_attrs(int f_out, int ndx, int iflags,
169 const char *fname, struct file_struct *file,
170 uchar fnamecmp_type, char *buf, int len)
172 write_ndx(f_out, ndx);
173 if (protocol_version < 29)
174 return;
175 write_shortint(f_out, iflags);
176 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
177 write_byte(f_out, fnamecmp_type);
178 if (iflags & ITEM_XNAME_FOLLOWS)
179 write_vstring(f_out, buf, len);
180 #ifdef SUPPORT_XATTRS
181 if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers
182 && !(want_xattr_optim && BITS_SET(iflags, ITEM_XNAME_FOLLOWS|ITEM_LOCAL_CHANGE)))
183 send_xattr_request(fname, file, f_out);
184 #endif
187 void send_files(int f_in, int f_out)
189 int fd = -1;
190 struct sum_struct *s;
191 struct map_struct *mbuf = NULL;
192 STRUCT_STAT st;
193 char fname[MAXPATHLEN], xname[MAXPATHLEN];
194 const char *path, *slash;
195 uchar fnamecmp_type;
196 int iflags, xlen;
197 struct file_struct *file;
198 int phase = 0, max_phase = protocol_version >= 29 ? 2 : 1;
199 int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i;
200 enum logcode log_code = log_before_transfer ? FLOG : FINFO;
201 int f_xfer = write_batch < 0 ? batch_fd : f_out;
202 int save_io_error = io_error;
203 int ndx, j;
205 if (DEBUG_GTE(SEND, 1))
206 rprintf(FINFO, "send_files starting\n");
208 if (whole_file < 0)
209 whole_file = 0;
211 progress_init();
213 while (1) {
214 if (inc_recurse) {
215 send_extra_file_list(f_out, MIN_FILECNT_LOOKAHEAD);
216 extra_flist_sending_enabled = !flist_eof;
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;
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);
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;
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;
248 if (inc_recurse)
249 send_extra_file_list(f_out, MIN_FILECNT_LOOKAHEAD);
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 = "";
261 if (!change_pathname(file, NULL, 0))
262 continue;
263 f_name(file, fname);
265 if (DEBUG_GTE(SEND, 1))
266 rprintf(FINFO, "send_files(%d, %s%s%s)\n", ndx, path,slash,fname);
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
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++;
292 continue;
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);
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;
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;
315 if (iflags & ITEM_IS_NEW)
316 stats.created_files++;
319 updating_basis_file = (inplace_partial && fnamecmp_type == FNAMECMP_PARTIAL_DIR)
320 || (inplace && (protocol_version >= 29 ? fnamecmp_type == FNAMECMP_FNAME : make_backups <= 0));
322 if (!am_server)
323 set_current_file_index(file, ndx);
324 stats.xferred_files++;
325 stats.total_transferred_size += F_LENGTH(file);
327 remember_initial_stats();
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;
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);
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));
354 free_sums(s);
355 if (protocol_version >= 30)
356 send_msg_int(MSG_NO_SEND, ndx);
357 continue;
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);
369 if (append_mode > 0 && st.st_size < F_LENGTH(file)) {
370 rprintf(FWARNING, "skipped diminished file: %s\n",
371 full_fname(fname));
372 free_sums(s);
373 close(fd);
374 if (protocol_version >= 30)
375 send_msg_int(MSG_NO_SEND, ndx);
376 continue;
379 if (st.st_size) {
380 int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
381 mbuf = map_file(fd, st.st_size, read_size, s->blength);
382 } else
383 mbuf = NULL;
385 if (DEBUG_GTE(DELTASUM, 2)) {
386 rprintf(FINFO, "send_files mapped %s%s%s of size %s\n",
387 path,slash,fname, big_num(st.st_size));
390 write_ndx_and_attrs(f_out, ndx, iflags, fname, file, fnamecmp_type, xname, xlen);
391 write_sum_head(f_xfer, s);
393 if (DEBUG_GTE(DELTASUM, 2))
394 rprintf(FINFO, "calling match_sums %s%s%s\n", path,slash,fname);
396 if (log_before_transfer)
397 log_item(FCLIENT, file, iflags, NULL);
398 else if (!am_server && INFO_GTE(NAME, 1) && INFO_EQ(PROGRESS, 1))
399 rprintf(FCLIENT, "%s\n", fname);
401 set_compression(fname);
403 match_sums(f_xfer, s, mbuf, st.st_size);
404 if (INFO_GTE(PROGRESS, 1))
405 end_progress(st.st_size);
406 else if (want_progress_now)
407 instant_progress(fname);
409 log_item(log_code, file, iflags, NULL);
411 if (mbuf) {
412 j = unmap_file(mbuf);
413 if (j) {
414 io_error |= IOERR_GENERAL;
415 rsyserr(FERROR_XFER, j,
416 "read errors mapping %s",
417 full_fname(fname));
420 close(fd);
422 free_sums(s);
424 if (DEBUG_GTE(SEND, 1))
425 rprintf(FINFO, "sender finished %s%s%s\n", path,slash,fname);
427 /* Flag that we actually sent this entry. */
428 file->flags |= FLAG_FILE_SENT;
430 if (make_backups < 0)
431 make_backups = -make_backups;
433 if (io_error != save_io_error && protocol_version >= 30)
434 send_msg_int(MSG_IO_ERROR, io_error);
436 if (DEBUG_GTE(SEND, 1))
437 rprintf(FINFO, "send files finished\n");
439 match_report();
441 write_ndx(f_out, NDX_DONE);