2 * Routines only used by the sending process.
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2003-2020 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 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
;
45 extern int inplace_partial
;
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
;
53 BOOL extra_flist_sending_enabled
;
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.
64 * Receive the checksums for a buffer
66 static struct sum_struct
*receive_sums(int f
)
70 int lull_mod
= protocol_version
>= 31 ? 0 : allowed_lull
* 5;
73 if (!(s
= new(struct sum_struct
)))
74 out_of_memory("receive_sums");
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
);
85 if (append_mode
> 0) {
86 s
->flength
= (OFF_T
)s
->count
* s
->blength
;
88 s
->flength
-= s
->blength
- s
->remainder
;
95 if (!(s
->sums
= new_array(struct sum_buf
, s
->count
)))
96 out_of_memory("receive_sums");
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
);
102 s
->sums
[i
].offset
= offset
;
103 s
->sums
[i
].flags
= 0;
105 if (i
== s
->count
-1 && s
->remainder
!= 0)
106 s
->sums
[i
].len
= s
->remainder
;
108 s
->sums
[i
].len
= s
->blength
;
109 offset
+= s
->sums
[i
].len
;
111 if (lull_mod
&& !(i
% lull_mod
))
112 maybe_send_keepalive(time(NULL
), True
);
114 if (DEBUG_GTE(DELTASUM
, 3)) {
116 "chunk[%d] len=%d offset=%s sum1=%08x\n",
117 i
, s
->sums
[i
].len
, big_num(s
->sums
[i
].offset
),
127 void successful_send(int ndx
)
129 char fname
[MAXPATHLEN
];
131 struct file_struct
*file
;
132 struct file_list
*flist
;
135 if (!remove_source_files
)
138 flist
= flist_for_ndx(ndx
, "successful_send");
139 file
= flist
->files
[ndx
- flist
->ndx_start
];
140 if (!change_pathname(file
, NULL
, 0))
144 if ((copy_links
? do_stat(fname
, &st
) : do_lstat(fname
, &st
)) < 0) {
145 failed_op
= "re-lstat";
149 if (st
.st_size
!= F_LENGTH(file
) || st
.st_mtime
!= file
->modtime
151 || (NSEC_BUMP(file
) && (uint32
)st
.ST_MTIME_NSEC
!= F_MOD_NSEC(file
))
154 rprintf(FERROR_XFER
, "ERROR: Skipping sender remove for changed file: %s\n", fname
);
158 if (do_unlink(fname
) < 0) {
159 failed_op
= "remove";
162 rprintf(FINFO
, "sender file already removed: %s\n", fname
);
164 rsyserr(FERROR_XFER
, errno
, "sender failed to %s %s", failed_op
, fname
);
166 if (INFO_GTE(REMOVE
, 1))
167 rprintf(FINFO
, "sender removed %s\n", fname
);
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
)
175 write_ndx(f_out
, ndx
);
176 if (protocol_version
< 29)
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
);
190 void send_files(int f_in
, int f_out
)
193 struct sum_struct
*s
;
194 struct map_struct
*mbuf
= NULL
;
196 char fname
[MAXPATHLEN
], xname
[MAXPATHLEN
];
197 const char *path
, *slash
;
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
;
208 if (DEBUG_GTE(SEND
, 1))
209 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
,
277 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
++;
285 else if (S_ISLNK(file
->mode
))
286 stats
.created_symlinks
++;
288 else if (IS_DEVICE(file
->mode
))
289 stats
.created_devices
++;
291 stats
.created_specials
++;
297 "got transfer request in phase 2 [%s]\n",
299 exit_cleanup(RERR_PROTOCOL
);
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
;
311 if (csum_length
!= SHORT_SUM_LENGTH
) {
312 make_backups
= -make_backups
;
313 append_mode
= -append_mode
;
314 csum_length
= SHORT_SUM_LENGTH
;
316 if (iflags
& ITEM_IS_NEW
)
317 stats
.created_files
++;
320 updating_basis_file
= (inplace_partial
&& fnamecmp_type
== FNAMECMP_PARTIAL_DIR
)
321 || (inplace
&& (protocol_version
>= 29 ? fnamecmp_type
== FNAMECMP_FNAME
: make_backups
<= 0));
324 set_current_file_index(file
, ndx
);
325 stats
.xferred_files
++;
326 stats
.total_transferred_size
+= F_LENGTH(file
);
328 remember_initial_stats();
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
,
333 fnamecmp_type
, xname
, xlen
);
337 if (!(s
= receive_sums(f_in
))) {
338 io_error
|= IOERR_GENERAL
;
339 rprintf(FERROR_XFER
, "receive_sums failed\n");
340 exit_cleanup(RERR_PROTOCOL
);
343 fd
= do_open(fname
, O_RDONLY
, 0);
345 if (errno
== ENOENT
) {
346 enum logcode c
= am_daemon
347 && protocol_version
< 28 ? FERROR
349 io_error
|= IOERR_VANISHED
;
350 rprintf(c
, "file has vanished: %s\n",
353 io_error
|= IOERR_GENERAL
;
354 rsyserr(FERROR_XFER
, errno
,
355 "send_files failed to open %s",
359 if (protocol_version
>= 30)
360 send_msg_int(MSG_NO_SEND
, ndx
);
364 /* map the local file */
365 if (do_fstat(fd
, &st
) != 0) {
366 io_error
|= IOERR_GENERAL
;
367 rsyserr(FERROR_XFER
, errno
, "fstat failed");
370 exit_cleanup(RERR_FILEIO
);
374 int32 read_size
= MAX(s
->blength
* 3, MAX_MAP_SIZE
);
375 mbuf
= map_file(fd
, st
.st_size
, read_size
, s
->blength
);
379 if (DEBUG_GTE(DELTASUM
, 2)) {
380 rprintf(FINFO
, "send_files mapped %s%s%s of size %s\n",
381 path
,slash
,fname
, big_num(st
.st_size
));
384 write_ndx_and_attrs(f_out
, ndx
, iflags
, fname
, file
,
385 fnamecmp_type
, xname
, xlen
);
386 write_sum_head(f_xfer
, s
);
388 if (DEBUG_GTE(DELTASUM
, 2))
389 rprintf(FINFO
, "calling match_sums %s%s%s\n", path
,slash
,fname
);
391 if (log_before_transfer
)
392 log_item(FCLIENT
, file
, iflags
, NULL
);
393 else if (!am_server
&& INFO_GTE(NAME
, 1) && INFO_EQ(PROGRESS
, 1))
394 rprintf(FCLIENT
, "%s\n", fname
);
396 set_compression(fname
);
398 match_sums(f_xfer
, s
, mbuf
, st
.st_size
);
399 if (INFO_GTE(PROGRESS
, 1))
400 end_progress(st
.st_size
);
401 else if (want_progress_now
)
402 instant_progress(fname
);
404 log_item(log_code
, file
, iflags
, NULL
);
407 j
= unmap_file(mbuf
);
409 io_error
|= IOERR_GENERAL
;
410 rsyserr(FERROR_XFER
, j
,
411 "read errors mapping %s",
419 if (DEBUG_GTE(SEND
, 1))
420 rprintf(FINFO
, "sender finished %s%s%s\n", path
,slash
,fname
);
422 /* Flag that we actually sent this entry. */
423 file
->flags
|= FLAG_FILE_SENT
;
425 if (make_backups
< 0)
426 make_backups
= -make_backups
;
428 if (io_error
!= save_io_error
&& protocol_version
>= 30)
429 send_msg_int(MSG_IO_ERROR
, io_error
);
431 if (DEBUG_GTE(SEND
, 1))
432 rprintf(FINFO
, "send files finished\n");
436 write_ndx(f_out
, NDX_DONE
);