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 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
;
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
;
46 extern int inplace_partial
;
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
;
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.
65 * Receive the checksums for a buffer
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;
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
;
86 s
->flength
-= s
->blength
- s
->remainder
;
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
;
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)) {
113 "chunk[%d] len=%d offset=%s sum1=%08x\n",
114 i
, s
->sums
[i
].len
, big_num(s
->sums
[i
].offset
),
124 void successful_send(int ndx
)
126 char fname
[MAXPATHLEN
];
128 struct file_struct
*file
;
129 struct file_list
*flist
;
132 if (!remove_source_files
)
135 flist
= flist_for_ndx(ndx
, "successful_send");
136 file
= flist
->files
[ndx
- flist
->ndx_start
];
137 if (!change_pathname(file
, NULL
, 0))
141 if ((copy_links
? do_stat(fname
, &st
) : do_lstat(fname
, &st
)) < 0) {
142 failed_op
= "re-lstat";
146 if (st
.st_size
!= F_LENGTH(file
) || st
.st_mtime
!= file
->modtime
148 || (NSEC_BUMP(file
) && (uint32
)st
.ST_MTIME_NSEC
!= F_MOD_NSEC(file
))
151 rprintf(FERROR_XFER
, "ERROR: Skipping sender remove for changed file: %s\n", fname
);
155 if (do_unlink(fname
) < 0) {
156 failed_op
= "remove";
159 rprintf(FINFO
, "sender file already removed: %s\n", fname
);
161 rsyserr(FERROR_XFER
, errno
, "sender failed to %s %s", failed_op
, fname
);
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)
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
);
187 void send_files(int f_in
, int f_out
)
190 struct sum_struct
*s
;
191 struct map_struct
*mbuf
= NULL
;
193 char fname
[MAXPATHLEN
], xname
[MAXPATHLEN
];
194 const char *path
, *slash
;
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
;
205 if (DEBUG_GTE(SEND
, 1))
206 rprintf(FINFO
, "send_files starting\n");
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
,
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))
230 if (inc_recurse
&& first_flist
) {
231 file_old_total
-= first_flist
->used
;
232 flist_free(first_flist
);
234 if (first_flist
== cur_flist
)
235 file_old_total
= cur_flist
->used
;
236 write_ndx(f_out
, NDX_DONE
);
240 if (++phase
> max_phase
)
242 if (DEBUG_GTE(SEND
, 1))
243 rprintf(FINFO
, "send_files phase=%d\n", phase
);
244 write_ndx(f_out
, NDX_DONE
);
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
];
254 file
= dir_flist
->files
[cur_flist
->parent_ndx
];
255 if (F_PATHNAME(file
)) {
256 path
= F_PATHNAME(file
);
261 if (!change_pathname(file
, NULL
, 0))
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
);
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
++;
284 else if (S_ISLNK(file
->mode
))
285 stats
.created_symlinks
++;
287 else if (IS_DEVICE(file
->mode
))
288 stats
.created_devices
++;
290 stats
.created_specials
++;
296 "got transfer request in phase 2 [%s]\n",
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
;
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));
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
);
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);
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",
349 io_error
|= IOERR_GENERAL
;
350 rsyserr(FERROR_XFER
, errno
,
351 "send_files failed to open %s",
355 if (protocol_version
>= 30)
356 send_msg_int(MSG_NO_SEND
, ndx
);
360 /* map the local file */
361 if (do_fstat(fd
, &st
) != 0) {
362 io_error
|= IOERR_GENERAL
;
363 rsyserr(FERROR_XFER
, errno
, "fstat failed");
366 exit_cleanup(RERR_FILEIO
);
369 if (append_mode
> 0 && st
.st_size
< F_LENGTH(file
)) {
370 rprintf(FWARNING
, "skipped diminished file: %s\n",
374 if (protocol_version
>= 30)
375 send_msg_int(MSG_NO_SEND
, ndx
);
380 int32 read_size
= MAX(s
->blength
* 3, MAX_MAP_SIZE
);
381 mbuf
= map_file(fd
, st
.st_size
, read_size
, s
->blength
);
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
);
412 j
= unmap_file(mbuf
);
414 io_error
|= IOERR_GENERAL
;
415 rsyserr(FERROR_XFER
, j
,
416 "read errors mapping %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");
441 write_ndx(f_out
, NDX_DONE
);