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.
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 xfer_sum_len
;
35 extern int csum_length
;
36 extern int append_mode
;
37 extern int copy_links
;
40 extern int whole_file
;
41 extern int allowed_lull
;
42 extern int copy_devices
;
43 extern int preserve_xattrs
;
44 extern int protocol_version
;
45 extern int remove_source_files
;
46 extern int updating_basis_file
;
47 extern int make_backups
;
49 extern int inplace_partial
;
51 extern int write_batch
;
52 extern int file_old_total
;
53 extern BOOL want_progress_now
;
54 extern struct stats stats
;
55 extern struct file_list
*cur_flist
, *first_flist
, *dir_flist
;
56 extern char num_dev_ino_buf
[4 + 8 + 8];
58 BOOL extra_flist_sending_enabled
;
63 * The sender gets checksums from the generator, calculates deltas,
64 * and transmits them to the receiver. The sender process runs on the
65 * machine holding the source files.
69 * Receive the checksums for a buffer
71 static struct sum_struct
*receive_sums(int f
)
73 struct sum_struct
*s
= new(struct sum_struct
);
74 int lull_mod
= protocol_version
>= 31 ? 0 : allowed_lull
* 5;
82 if (DEBUG_GTE(DELTASUM
, 3)) {
83 rprintf(FINFO
, "count=%s n=%ld rem=%ld\n",
84 big_num(s
->count
), (long)s
->blength
, (long)s
->remainder
);
87 if (append_mode
> 0) {
88 s
->flength
= (OFF_T
)s
->count
* s
->blength
;
90 s
->flength
-= s
->blength
- s
->remainder
;
97 s
->sums
= new_array(struct sum_buf
, s
->count
);
98 s
->sum2_array
= new_array(char, (size_t)s
->count
* xfer_sum_len
);
100 for (i
= 0; i
< s
->count
; i
++) {
101 s
->sums
[i
].sum1
= read_int(f
);
102 read_buf(f
, sum2_at(s
, i
), s
->s2length
);
104 s
->sums
[i
].offset
= offset
;
105 s
->sums
[i
].flags
= 0;
107 if (i
== s
->count
-1 && s
->remainder
!= 0)
108 s
->sums
[i
].len
= s
->remainder
;
110 s
->sums
[i
].len
= s
->blength
;
111 offset
+= s
->sums
[i
].len
;
113 if (lull_mod
&& !(i
% lull_mod
))
114 maybe_send_keepalive(time(NULL
), True
);
116 if (DEBUG_GTE(DELTASUM
, 3)) {
118 "chunk[%d] len=%d offset=%s sum1=%08x\n",
119 i
, s
->sums
[i
].len
, big_num(s
->sums
[i
].offset
),
129 void successful_send(int ndx
)
131 char fname
[MAXPATHLEN
];
133 struct file_struct
*file
;
134 struct file_list
*flist
;
137 if (!remove_source_files
)
140 flist
= flist_for_ndx(ndx
, "successful_send");
141 file
= flist
->files
[ndx
- flist
->ndx_start
];
142 if (!change_pathname(file
, NULL
, 0))
146 if ((copy_links
? do_stat(fname
, &st
) : do_lstat(fname
, &st
)) < 0) {
147 failed_op
= "re-lstat";
152 && (int64
)st
.st_dev
== IVAL64(num_dev_ino_buf
, 4)
153 && (int64
)st
.st_ino
== IVAL64(num_dev_ino_buf
, 4 + 8)) {
154 rprintf(FERROR_XFER
, "ERROR: Skipping sender remove of destination file: %s\n", fname
);
158 if (st
.st_size
!= F_LENGTH(file
) || st
.st_mtime
!= file
->modtime
160 || (NSEC_BUMP(file
) && (uint32
)st
.ST_MTIME_NSEC
!= F_MOD_NSEC(file
))
163 rprintf(FERROR_XFER
, "ERROR: Skipping sender remove for changed file: %s\n", fname
);
167 if (do_unlink(fname
) < 0) {
168 failed_op
= "remove";
171 rprintf(FINFO
, "sender file already removed: %s\n", fname
);
173 rsyserr(FERROR_XFER
, errno
, "sender failed to %s %s", failed_op
, fname
);
175 if (INFO_GTE(REMOVE
, 1))
176 rprintf(FINFO
, "sender removed %s\n", fname
);
180 static void write_ndx_and_attrs(int f_out
, int ndx
, int iflags
,
181 const char *fname
, struct file_struct
*file
,
182 uchar fnamecmp_type
, char *buf
, int len
)
184 write_ndx(f_out
, ndx
);
185 if (protocol_version
< 29)
187 write_shortint(f_out
, iflags
);
188 if (iflags
& ITEM_BASIS_TYPE_FOLLOWS
)
189 write_byte(f_out
, fnamecmp_type
);
190 if (iflags
& ITEM_XNAME_FOLLOWS
)
191 write_vstring(f_out
, buf
, len
);
192 #ifdef SUPPORT_XATTRS
193 if (preserve_xattrs
&& iflags
& ITEM_REPORT_XATTR
&& do_xfers
194 && !(want_xattr_optim
&& BITS_SET(iflags
, ITEM_XNAME_FOLLOWS
|ITEM_LOCAL_CHANGE
)))
195 send_xattr_request(fname
, file
, f_out
);
199 void send_files(int f_in
, int f_out
)
202 struct sum_struct
*s
;
203 struct map_struct
*mbuf
= NULL
;
205 char fname
[MAXPATHLEN
], xname
[MAXPATHLEN
];
206 const char *path
, *slash
;
209 struct file_struct
*file
;
210 int phase
= 0, max_phase
= protocol_version
>= 29 ? 2 : 1;
211 int itemizing
= am_server
? logfile_format_has_i
: stdout_format_has_i
;
212 enum logcode log_code
= log_before_transfer
? FLOG
: FINFO
;
213 int f_xfer
= write_batch
< 0 ? batch_fd
: f_out
;
214 int save_io_error
= io_error
;
217 if (DEBUG_GTE(SEND
, 1))
218 rprintf(FINFO
, "send_files starting\n");
227 send_extra_file_list(f_out
, MIN_FILECNT_LOOKAHEAD
);
228 extra_flist_sending_enabled
= !flist_eof
;
231 /* This call also sets cur_flist. */
232 ndx
= read_ndx_and_attrs(f_in
, f_out
, &iflags
, &fnamecmp_type
,
234 extra_flist_sending_enabled
= False
;
236 if (ndx
== NDX_DONE
) {
237 if (!am_server
&& cur_flist
) {
238 set_current_file_index(NULL
, 0);
239 if (INFO_GTE(PROGRESS
, 2))
242 if (inc_recurse
&& first_flist
) {
243 file_old_total
-= first_flist
->used
;
244 flist_free(first_flist
);
246 if (first_flist
== cur_flist
)
247 file_old_total
= cur_flist
->used
;
248 write_ndx(f_out
, NDX_DONE
);
252 if (++phase
> max_phase
)
254 if (DEBUG_GTE(SEND
, 1))
255 rprintf(FINFO
, "send_files phase=%d\n", phase
);
256 write_ndx(f_out
, NDX_DONE
);
261 send_extra_file_list(f_out
, MIN_FILECNT_LOOKAHEAD
);
263 if (ndx
- cur_flist
->ndx_start
>= 0)
264 file
= cur_flist
->files
[ndx
- cur_flist
->ndx_start
];
266 file
= dir_flist
->files
[cur_flist
->parent_ndx
];
267 if (F_PATHNAME(file
)) {
268 path
= F_PATHNAME(file
);
273 if (!change_pathname(file
, NULL
, 0))
277 if (DEBUG_GTE(SEND
, 1))
278 rprintf(FINFO
, "send_files(%d, %s%s%s)\n", ndx
, path
,slash
,fname
);
280 #ifdef SUPPORT_XATTRS
281 if (preserve_xattrs
&& iflags
& ITEM_REPORT_XATTR
&& do_xfers
282 && !(want_xattr_optim
&& BITS_SET(iflags
, ITEM_XNAME_FOLLOWS
|ITEM_LOCAL_CHANGE
)))
283 recv_xattr_request(file
, f_in
);
286 if (!(iflags
& ITEM_TRANSFER
)) {
287 maybe_log_item(file
, iflags
, itemizing
, xname
);
288 write_ndx_and_attrs(f_out
, ndx
, iflags
, fname
, file
, fnamecmp_type
, xname
, xlen
);
289 if (iflags
& ITEM_IS_NEW
) {
290 stats
.created_files
++;
291 if (S_ISREG(file
->mode
)) {
292 /* Nothing further to count. */
293 } else if (S_ISDIR(file
->mode
))
294 stats
.created_dirs
++;
296 else if (S_ISLNK(file
->mode
))
297 stats
.created_symlinks
++;
299 else if (IS_DEVICE(file
->mode
))
300 stats
.created_devices
++;
302 stats
.created_specials
++;
308 "got transfer request in phase 2 [%s]\n",
310 exit_cleanup(RERR_PROTOCOL
);
313 if (file
->flags
& FLAG_FILE_SENT
) {
314 if (csum_length
== SHORT_SUM_LENGTH
) {
315 /* For inplace: redo phase turns off the backup
316 * flag so that we do a regular inplace send. */
317 make_backups
= -make_backups
;
318 append_mode
= -append_mode
;
319 csum_length
= SUM_LENGTH
;
322 if (csum_length
!= SHORT_SUM_LENGTH
) {
323 make_backups
= -make_backups
;
324 append_mode
= -append_mode
;
325 csum_length
= SHORT_SUM_LENGTH
;
327 if (iflags
& ITEM_IS_NEW
)
328 stats
.created_files
++;
331 updating_basis_file
= (inplace_partial
&& fnamecmp_type
== FNAMECMP_PARTIAL_DIR
)
332 || (inplace
&& (protocol_version
>= 29 ? fnamecmp_type
== FNAMECMP_FNAME
: make_backups
<= 0));
335 set_current_file_index(file
, ndx
);
336 stats
.xferred_files
++;
337 stats
.total_transferred_size
+= F_LENGTH(file
);
339 remember_initial_stats();
341 if (!do_xfers
) { /* log the transfer */
342 log_item(FCLIENT
, file
, iflags
, NULL
);
343 write_ndx_and_attrs(f_out
, ndx
, iflags
, fname
, file
, fnamecmp_type
, xname
, xlen
);
347 if (!(s
= receive_sums(f_in
))) {
348 io_error
|= IOERR_GENERAL
;
349 rprintf(FERROR_XFER
, "receive_sums failed\n");
350 exit_cleanup(RERR_PROTOCOL
);
353 fd
= do_open(fname
, O_RDONLY
, 0);
355 if (errno
== ENOENT
) {
356 enum logcode c
= am_daemon
&& protocol_version
< 28 ? FERROR
: FWARNING
;
357 io_error
|= IOERR_VANISHED
;
358 rprintf(c
, "file has vanished: %s\n",
361 io_error
|= IOERR_GENERAL
;
362 rsyserr(FERROR_XFER
, errno
,
363 "send_files failed to open %s",
367 if (protocol_version
>= 30)
368 send_msg_int(MSG_NO_SEND
, ndx
);
372 /* map the local file */
373 if (do_fstat(fd
, &st
) != 0) {
374 io_error
|= IOERR_GENERAL
;
375 rsyserr(FERROR_XFER
, errno
, "fstat failed");
378 exit_cleanup(RERR_FILEIO
);
381 if (IS_DEVICE(st
.st_mode
)) {
383 rprintf(FERROR
, "attempt to copy device contents without --copy-devices\n");
384 exit_cleanup(RERR_PROTOCOL
);
387 st
.st_size
= get_device_size(fd
, fname
);
390 if (append_mode
> 0 && st
.st_size
< F_LENGTH(file
)) {
391 rprintf(FWARNING
, "skipped diminished file: %s\n",
395 if (protocol_version
>= 30)
396 send_msg_int(MSG_NO_SEND
, ndx
);
401 int32 read_size
= MAX(s
->blength
* 3, MAX_MAP_SIZE
);
402 mbuf
= map_file(fd
, st
.st_size
, read_size
, s
->blength
);
406 if (DEBUG_GTE(DELTASUM
, 2)) {
407 rprintf(FINFO
, "send_files mapped %s%s%s of size %s\n",
408 path
,slash
,fname
, big_num(st
.st_size
));
411 write_ndx_and_attrs(f_out
, ndx
, iflags
, fname
, file
, fnamecmp_type
, xname
, xlen
);
412 write_sum_head(f_xfer
, s
);
414 if (DEBUG_GTE(DELTASUM
, 2))
415 rprintf(FINFO
, "calling match_sums %s%s%s\n", path
,slash
,fname
);
417 if (log_before_transfer
)
418 log_item(FCLIENT
, file
, iflags
, NULL
);
419 else if (!am_server
&& INFO_GTE(NAME
, 1) && INFO_EQ(PROGRESS
, 1))
420 rprintf(FCLIENT
, "%s\n", fname
);
422 set_compression(fname
);
424 match_sums(f_xfer
, s
, mbuf
, st
.st_size
);
425 if (INFO_GTE(PROGRESS
, 1))
426 end_progress(st
.st_size
);
427 else if (want_progress_now
)
428 instant_progress(fname
);
430 log_item(log_code
, file
, iflags
, NULL
);
433 j
= unmap_file(mbuf
);
435 io_error
|= IOERR_GENERAL
;
436 rsyserr(FERROR_XFER
, j
,
437 "read errors mapping %s",
445 if (DEBUG_GTE(SEND
, 1))
446 rprintf(FINFO
, "sender finished %s%s%s\n", path
,slash
,fname
);
448 /* Flag that we actually sent this entry. */
449 file
->flags
|= FLAG_FILE_SENT
;
451 if (make_backups
< 0)
452 make_backups
= -make_backups
;
454 if (io_error
!= save_io_error
&& protocol_version
>= 30)
455 send_msg_int(MSG_IO_ERROR
, io_error
);
457 if (DEBUG_GTE(SEND
, 1))
458 rprintf(FINFO
, "send files finished\n");
462 write_ndx(f_out
, NDX_DONE
);