]>
Commit | Line | Data |
---|---|---|
e6e3f12f | 1 | /* |
0f78b815 WD |
2 | * Routines only used by the sending process. |
3 | * | |
4 | * Copyright (C) 1996 Andrew Tridgell | |
5 | * Copyright (C) 1996 Paul Mackerras | |
3e44bbd3 | 6 | * Copyright (C) 2003-2021 Wayne Davison |
0f78b815 WD |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify | |
8e41b68e WD |
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. | |
0f78b815 WD |
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 | * | |
e7c67065 | 18 | * You should have received a copy of the GNU General Public License along |
4fd842f9 | 19 | * with this program; if not, visit the http://fsf.org website. |
0f78b815 | 20 | */ |
2f03f956 AT |
21 | |
22 | #include "rsync.h" | |
5dd14f0c | 23 | #include "inums.h" |
2f03f956 | 24 | |
a0545709 | 25 | extern int do_xfers; |
8715db2c WD |
26 | extern int am_server; |
27 | extern int am_daemon; | |
3ea6e0e7 | 28 | extern int inc_recurse; |
8a9cfb24 | 29 | extern int log_before_transfer; |
2fedf3d5 | 30 | extern int stdout_format_has_i; |
ecc7623e | 31 | extern int logfile_format_has_i; |
ff08acd4 | 32 | extern int want_xattr_optim; |
2f03f956 | 33 | extern int csum_length; |
6cc11982 | 34 | extern int append_mode; |
79332c0d | 35 | extern int copy_links; |
2f03f956 | 36 | extern int io_error; |
7a7810aa | 37 | extern int flist_eof; |
dbb1c2d1 | 38 | extern int whole_file; |
605fed4b | 39 | extern int allowed_lull; |
16edf865 | 40 | extern int preserve_xattrs; |
d6631cf3 | 41 | extern int protocol_version; |
47c11975 | 42 | extern int remove_source_files; |
53f8519a | 43 | extern int updating_basis_file; |
8b115ac8 | 44 | extern int make_backups; |
53f8519a | 45 | extern int inplace; |
3a7bf54a | 46 | extern int inplace_partial; |
a0545709 WD |
47 | extern int batch_fd; |
48 | extern int write_batch; | |
fe16d9a6 | 49 | extern int file_old_total; |
15fa9ab0 | 50 | extern BOOL want_progress_now; |
a3221d2a | 51 | extern struct stats stats; |
6755a7d7 | 52 | extern struct file_list *cur_flist, *first_flist, *dir_flist; |
2f03f956 | 53 | |
7a7810aa WD |
54 | BOOL extra_flist_sending_enabled; |
55 | ||
0f9c48b1 MP |
56 | /** |
57 | * @file | |
58 | * | |
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 | **/ | |
fc0257c9 | 63 | |
ce8149b6 MP |
64 | /** |
65 | * Receive the checksums for a buffer | |
66 | **/ | |
2f03f956 AT |
67 | static struct sum_struct *receive_sums(int f) |
68 | { | |
11eb67ee | 69 | struct sum_struct *s = new(struct sum_struct); |
05c36015 | 70 | int lull_mod = protocol_version >= 31 ? 0 : allowed_lull * 5; |
2f03f956 | 71 | OFF_T offset = 0; |
11eb67ee | 72 | int32 i; |
2f03f956 | 73 | |
fc0257c9 S |
74 | read_sum_head(f, s); |
75 | ||
2f03f956 AT |
76 | s->sums = NULL; |
77 | ||
5a18b34d | 78 | if (DEBUG_GTE(DELTASUM, 3)) { |
6d56efa6 | 79 | rprintf(FINFO, "count=%s n=%ld rem=%ld\n", |
adc2476f | 80 | big_num(s->count), (long)s->blength, (long)s->remainder); |
da9d12f5 | 81 | } |
2f03f956 | 82 | |
f3d6d480 | 83 | if (append_mode > 0) { |
6cc11982 WD |
84 | s->flength = (OFF_T)s->count * s->blength; |
85 | if (s->remainder) | |
86 | s->flength -= s->blength - s->remainder; | |
87 | return s; | |
88 | } | |
89 | ||
e6e3f12f | 90 | if (s->count == 0) |
2f03f956 AT |
91 | return(s); |
92 | ||
11eb67ee | 93 | s->sums = new_array(struct sum_buf, s->count); |
2f03f956 | 94 | |
eed47b6b | 95 | for (i = 0; i < s->count; i++) { |
2f03f956 | 96 | s->sums[i].sum1 = read_int(f); |
e6e3f12f | 97 | read_buf(f, s->sums[i].sum2, s->s2length); |
2f03f956 AT |
98 | |
99 | s->sums[i].offset = offset; | |
a3221d2a | 100 | s->sums[i].flags = 0; |
2f03f956 | 101 | |
eed47b6b | 102 | if (i == s->count-1 && s->remainder != 0) |
2f03f956 | 103 | s->sums[i].len = s->remainder; |
a3221d2a | 104 | else |
fc0257c9 | 105 | s->sums[i].len = s->blength; |
2f03f956 AT |
106 | offset += s->sums[i].len; |
107 | ||
05c36015 | 108 | if (lull_mod && !(i % lull_mod)) |
92d02148 | 109 | maybe_send_keepalive(time(NULL), True); |
605fed4b | 110 | |
5a18b34d | 111 | if (DEBUG_GTE(DELTASUM, 3)) { |
a3221d2a | 112 | rprintf(FINFO, |
6d56efa6 | 113 | "chunk[%d] len=%d offset=%s sum1=%08x\n", |
adc2476f | 114 | i, s->sums[i].len, big_num(s->sums[i].offset), |
a3221d2a WD |
115 | s->sums[i].sum1); |
116 | } | |
2f03f956 AT |
117 | } |
118 | ||
119 | s->flength = offset; | |
120 | ||
121 | return s; | |
122 | } | |
123 | ||
5f40615c | 124 | void successful_send(int ndx) |
95306d9d WD |
125 | { |
126 | char fname[MAXPATHLEN]; | |
852585b1 | 127 | char *failed_op; |
95306d9d | 128 | struct file_struct *file; |
f3d6d480 | 129 | struct file_list *flist; |
852585b1 | 130 | STRUCT_STAT st; |
95306d9d | 131 | |
f3d6d480 | 132 | if (!remove_source_files) |
95306d9d WD |
133 | return; |
134 | ||
e982d591 | 135 | flist = flist_for_ndx(ndx, "successful_send"); |
f3d6d480 | 136 | file = flist->files[ndx - flist->ndx_start]; |
29a89172 | 137 | if (!change_pathname(file, NULL, 0)) |
f3d6d480 WD |
138 | return; |
139 | f_name(file, fname); | |
140 | ||
79332c0d | 141 | if ((copy_links ? do_stat(fname, &st) : do_lstat(fname, &st)) < 0) { |
852585b1 WD |
142 | failed_op = "re-lstat"; |
143 | goto failed; | |
144 | } | |
145 | ||
79332c0d | 146 | if (st.st_size != F_LENGTH(file) || st.st_mtime != file->modtime |
852585b1 WD |
147 | #ifdef ST_MTIME_NSEC |
148 | || (NSEC_BUMP(file) && (uint32)st.ST_MTIME_NSEC != F_MOD_NSEC(file)) | |
149 | #endif | |
79332c0d | 150 | ) { |
de8ec0b3 | 151 | rprintf(FERROR_XFER, "ERROR: Skipping sender remove for changed file: %s\n", fname); |
852585b1 WD |
152 | return; |
153 | } | |
154 | ||
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 | |
de8ec0b3 | 161 | rsyserr(FERROR_XFER, errno, "sender failed to %s %s", failed_op, fname); |
852585b1 | 162 | } else { |
951e826b | 163 | if (INFO_GTE(REMOVE, 1)) |
f3d6d480 | 164 | rprintf(FINFO, "sender removed %s\n", fname); |
852585b1 | 165 | } |
95306d9d | 166 | } |
2f03f956 | 167 | |
16edf865 WD |
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) | |
d144e43b | 171 | { |
9ae7a2cd | 172 | write_ndx(f_out, ndx); |
d144e43b WD |
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); | |
16edf865 | 180 | #ifdef SUPPORT_XATTRS |
ff08acd4 WD |
181 | if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers |
182 | && !(want_xattr_optim && BITS_SET(iflags, ITEM_XNAME_FOLLOWS|ITEM_LOCAL_CHANGE))) | |
16edf865 WD |
183 | send_xattr_request(fname, file, f_out); |
184 | #endif | |
d144e43b WD |
185 | } |
186 | ||
f3d6d480 | 187 | void send_files(int f_in, int f_out) |
e6e3f12f | 188 | { |
c1659c79 | 189 | int fd = -1; |
2f03f956 | 190 | struct sum_struct *s; |
41cc97ae | 191 | struct map_struct *mbuf = NULL; |
2f03f956 | 192 | STRUCT_STAT st; |
f3d6d480 WD |
193 | char fname[MAXPATHLEN], xname[MAXPATHLEN]; |
194 | const char *path, *slash; | |
4d53c4dd | 195 | uchar fnamecmp_type; |
6087ef2a | 196 | int iflags, xlen; |
2f03f956 | 197 | struct file_struct *file; |
11290705 | 198 | int phase = 0, max_phase = protocol_version >= 29 ? 2 : 1; |
2fedf3d5 WD |
199 | int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i; |
200 | enum logcode log_code = log_before_transfer ? FLOG : FINFO; | |
a0545709 | 201 | int f_xfer = write_batch < 0 ? batch_fd : f_out; |
59924aa8 | 202 | int save_io_error = io_error; |
f3d6d480 | 203 | int ndx, j; |
2f03f956 | 204 | |
951e826b | 205 | if (DEBUG_GTE(SEND, 1)) |
e6e3f12f | 206 | rprintf(FINFO, "send_files starting\n"); |
2f03f956 | 207 | |
dbb1c2d1 WD |
208 | if (whole_file < 0) |
209 | whole_file = 0; | |
210 | ||
15fa9ab0 WD |
211 | progress_init(); |
212 | ||
2f03f956 | 213 | while (1) { |
7a7810aa | 214 | if (inc_recurse) { |
ce827c3e | 215 | send_extra_file_list(f_out, MIN_FILECNT_LOOKAHEAD); |
7a7810aa WD |
216 | extra_flist_sending_enabled = !flist_eof; |
217 | } | |
f3d6d480 WD |
218 | |
219 | /* This call also sets cur_flist. */ | |
20caffd2 | 220 | ndx = read_ndx_and_attrs(f_in, f_out, &iflags, &fnamecmp_type, |
16edf865 | 221 | xname, &xlen); |
7a7810aa WD |
222 | extra_flist_sending_enabled = False; |
223 | ||
f3d6d480 | 224 | if (ndx == NDX_DONE) { |
15fa9ab0 | 225 | if (!am_server && cur_flist) { |
951e826b | 226 | set_current_file_index(NULL, 0); |
15fa9ab0 WD |
227 | if (INFO_GTE(PROGRESS, 2)) |
228 | end_progress(0); | |
951e826b | 229 | } |
3ea6e0e7 | 230 | if (inc_recurse && first_flist) { |
fe16d9a6 | 231 | file_old_total -= first_flist->used; |
f3d6d480 WD |
232 | flist_free(first_flist); |
233 | if (first_flist) { | |
fe16d9a6 WD |
234 | if (first_flist == cur_flist) |
235 | file_old_total = cur_flist->used; | |
9ae7a2cd | 236 | write_ndx(f_out, NDX_DONE); |
f3d6d480 WD |
237 | continue; |
238 | } | |
239 | } | |
11290705 | 240 | if (++phase > max_phase) |
8b48bf11 | 241 | break; |
951e826b | 242 | if (DEBUG_GTE(SEND, 1)) |
8b48bf11 | 243 | rprintf(FINFO, "send_files phase=%d\n", phase); |
9ae7a2cd | 244 | write_ndx(f_out, NDX_DONE); |
8b48bf11 | 245 | continue; |
2f03f956 AT |
246 | } |
247 | ||
16edf865 | 248 | if (inc_recurse) |
ce827c3e | 249 | send_extra_file_list(f_out, MIN_FILECNT_LOOKAHEAD); |
16edf865 | 250 | |
6755a7d7 WD |
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]; | |
8fc4033e WD |
255 | if (F_PATHNAME(file)) { |
256 | path = F_PATHNAME(file); | |
f3d6d480 WD |
257 | slash = "/"; |
258 | } else { | |
259 | path = slash = ""; | |
260 | } | |
29a89172 | 261 | if (!change_pathname(file, NULL, 0)) |
f3d6d480 WD |
262 | continue; |
263 | f_name(file, fname); | |
7f37167d | 264 | |
951e826b | 265 | if (DEBUG_GTE(SEND, 1)) |
f3d6d480 | 266 | rprintf(FINFO, "send_files(%d, %s%s%s)\n", ndx, path,slash,fname); |
8a9cfb24 | 267 | |
16edf865 | 268 | #ifdef SUPPORT_XATTRS |
ff08acd4 WD |
269 | if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers |
270 | && !(want_xattr_optim && BITS_SET(iflags, ITEM_XNAME_FOLLOWS|ITEM_LOCAL_CHANGE))) | |
16edf865 WD |
271 | recv_xattr_request(file, f_in); |
272 | #endif | |
273 | ||
fad3dc42 | 274 | if (!(iflags & ITEM_TRANSFER)) { |
6087ef2a | 275 | maybe_log_item(file, iflags, itemizing, xname); |
e63ff70e | 276 | write_ndx_and_attrs(f_out, ndx, iflags, fname, file, fnamecmp_type, xname, xlen); |
e366e530 WD |
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 | } | |
165e6d44 WD |
292 | continue; |
293 | } | |
11290705 WD |
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 | } | |
8a9cfb24 | 300 | |
f3d6d480 WD |
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 | } | |
e366e530 WD |
315 | if (iflags & ITEM_IS_NEW) |
316 | stats.created_files++; | |
f3d6d480 WD |
317 | } |
318 | ||
3a7bf54a WD |
319 | updating_basis_file = (inplace_partial && fnamecmp_type == FNAMECMP_PARTIAL_DIR) |
320 | || (inplace && (protocol_version >= 29 ? fnamecmp_type == FNAMECMP_FNAME : make_backups <= 0)); | |
b951e023 | 321 | |
15fa9ab0 | 322 | if (!am_server) |
65b4e4b2 | 323 | set_current_file_index(file, ndx); |
e366e530 | 324 | stats.xferred_files++; |
112d728f | 325 | stats.total_transferred_size += F_LENGTH(file); |
2f03f956 | 326 | |
cce44865 | 327 | remember_initial_stats(); |
4dde3347 | 328 | |
a0545709 | 329 | if (!do_xfers) { /* log the transfer */ |
4dde3347 | 330 | log_item(FCLIENT, file, iflags, NULL); |
e63ff70e | 331 | write_ndx_and_attrs(f_out, ndx, iflags, fname, file, fnamecmp_type, xname, xlen); |
2f03f956 AT |
332 | continue; |
333 | } | |
334 | ||
efa95a18 | 335 | if (!(s = receive_sums(f_in))) { |
06c28400 | 336 | io_error |= IOERR_GENERAL; |
342bfb5e | 337 | rprintf(FERROR_XFER, "receive_sums failed\n"); |
f3d6d480 | 338 | exit_cleanup(RERR_PROTOCOL); |
2f03f956 | 339 | } |
76f79ba7 | 340 | |
b9f592fb WD |
341 | fd = do_open(fname, O_RDONLY, 0); |
342 | if (fd == -1) { | |
343 | if (errno == ENOENT) { | |
e63ff70e | 344 | enum logcode c = am_daemon && protocol_version < 28 ? FERROR : FWARNING; |
b9f592fb WD |
345 | io_error |= IOERR_VANISHED; |
346 | rprintf(c, "file has vanished: %s\n", | |
347 | full_fname(fname)); | |
348 | } else { | |
06c28400 | 349 | io_error |= IOERR_GENERAL; |
3f0211b6 | 350 | rsyserr(FERROR_XFER, errno, |
b9f592fb WD |
351 | "send_files failed to open %s", |
352 | full_fname(fname)); | |
41cc97ae | 353 | } |
b9f592fb | 354 | free_sums(s); |
f3d6d480 WD |
355 | if (protocol_version >= 30) |
356 | send_msg_int(MSG_NO_SEND, ndx); | |
b9f592fb WD |
357 | continue; |
358 | } | |
e6e3f12f | 359 | |
b9f592fb WD |
360 | /* map the local file */ |
361 | if (do_fstat(fd, &st) != 0) { | |
362 | io_error |= IOERR_GENERAL; | |
342bfb5e | 363 | rsyserr(FERROR_XFER, errno, "fstat failed"); |
b9f592fb WD |
364 | free_sums(s); |
365 | close(fd); | |
f397616e | 366 | exit_cleanup(RERR_FILEIO); |
b9f592fb | 367 | } |
11a5a3c7 | 368 | |
9cd85b84 WD |
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; | |
377 | } | |
378 | ||
96d910c7 | 379 | if (st.st_size) { |
9b919d59 WD |
380 | int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE); |
381 | mbuf = map_file(fd, st.st_size, read_size, s->blength); | |
96d910c7 WD |
382 | } else |
383 | mbuf = NULL; | |
6902ed17 | 384 | |
5a18b34d | 385 | if (DEBUG_GTE(DELTASUM, 2)) { |
6d56efa6 | 386 | rprintf(FINFO, "send_files mapped %s%s%s of size %s\n", |
adc2476f | 387 | path,slash,fname, big_num(st.st_size)); |
6902ed17 | 388 | } |
e6e3f12f | 389 | |
e63ff70e | 390 | write_ndx_and_attrs(f_out, ndx, iflags, fname, file, fnamecmp_type, xname, xlen); |
a0545709 | 391 | write_sum_head(f_xfer, s); |
b9f592fb | 392 | |
5a18b34d | 393 | if (DEBUG_GTE(DELTASUM, 2)) |
f3d6d480 | 394 | rprintf(FINFO, "calling match_sums %s%s%s\n", path,slash,fname); |
83fff1aa | 395 | |
8a9cfb24 | 396 | if (log_before_transfer) |
4dde3347 | 397 | log_item(FCLIENT, file, iflags, NULL); |
951e826b | 398 | else if (!am_server && INFO_GTE(NAME, 1) && INFO_EQ(PROGRESS, 1)) |
f3d6d480 | 399 | rprintf(FCLIENT, "%s\n", fname); |
ecc81fce | 400 | |
83fff1aa | 401 | set_compression(fname); |
1b7c47cb | 402 | |
a0545709 | 403 | match_sums(f_xfer, s, mbuf, st.st_size); |
951e826b | 404 | if (INFO_GTE(PROGRESS, 1)) |
0394e34a | 405 | end_progress(st.st_size); |
15fa9ab0 WD |
406 | else if (want_progress_now) |
407 | instant_progress(fname); | |
0394e34a | 408 | |
4dde3347 | 409 | log_item(log_code, file, iflags, NULL); |
6902ed17 | 410 | |
b9f592fb WD |
411 | if (mbuf) { |
412 | j = unmap_file(mbuf); | |
413 | if (j) { | |
414 | io_error |= IOERR_GENERAL; | |
3f0211b6 | 415 | rsyserr(FERROR_XFER, j, |
b9f592fb WD |
416 | "read errors mapping %s", |
417 | full_fname(fname)); | |
6a7cc46c | 418 | } |
6902ed17 | 419 | } |
b9f592fb | 420 | close(fd); |
e6e3f12f | 421 | |
2f03f956 | 422 | free_sums(s); |
e6e3f12f | 423 | |
951e826b | 424 | if (DEBUG_GTE(SEND, 1)) |
f3d6d480 | 425 | rprintf(FINFO, "sender finished %s%s%s\n", path,slash,fname); |
95306d9d WD |
426 | |
427 | /* Flag that we actually sent this entry. */ | |
f3d6d480 | 428 | file->flags |= FLAG_FILE_SENT; |
2f03f956 | 429 | } |
f3d6d480 WD |
430 | if (make_backups < 0) |
431 | make_backups = -make_backups; | |
2f03f956 | 432 | |
59924aa8 WD |
433 | if (io_error != save_io_error && protocol_version >= 30) |
434 | send_msg_int(MSG_IO_ERROR, io_error); | |
435 | ||
951e826b | 436 | if (DEBUG_GTE(SEND, 1)) |
e6e3f12f | 437 | rprintf(FINFO, "send files finished\n"); |
2f03f956 AT |
438 | |
439 | match_report(); | |
440 | ||
9ae7a2cd | 441 | write_ndx(f_out, NDX_DONE); |
2f03f956 | 442 | } |