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 | |
b3bf9b9d | 6 | * Copyright (C) 2003-2009 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; |
2f03f956 | 32 | extern int csum_length; |
6cc11982 | 33 | extern int append_mode; |
2f03f956 | 34 | extern int io_error; |
7a7810aa | 35 | extern int flist_eof; |
605fed4b | 36 | extern int allowed_lull; |
16edf865 | 37 | extern int preserve_xattrs; |
d6631cf3 | 38 | extern int protocol_version; |
47c11975 | 39 | extern int remove_source_files; |
53f8519a | 40 | extern int updating_basis_file; |
8b115ac8 | 41 | extern int make_backups; |
53f8519a | 42 | extern int inplace; |
a0545709 WD |
43 | extern int batch_fd; |
44 | extern int write_batch; | |
fe16d9a6 | 45 | extern int file_old_total; |
92d02148 | 46 | extern BOOL we_send_keepalive_messages; |
a3221d2a | 47 | extern struct stats stats; |
6755a7d7 | 48 | extern struct file_list *cur_flist, *first_flist, *dir_flist; |
2f03f956 | 49 | |
7a7810aa WD |
50 | BOOL extra_flist_sending_enabled; |
51 | ||
0f9c48b1 MP |
52 | /** |
53 | * @file | |
54 | * | |
55 | * The sender gets checksums from the generator, calculates deltas, | |
56 | * and transmits them to the receiver. The sender process runs on the | |
57 | * machine holding the source files. | |
58 | **/ | |
fc0257c9 | 59 | |
ce8149b6 MP |
60 | /** |
61 | * Receive the checksums for a buffer | |
62 | **/ | |
2f03f956 AT |
63 | static struct sum_struct *receive_sums(int f) |
64 | { | |
65 | struct sum_struct *s; | |
a1cbe76e | 66 | int32 i; |
605fed4b | 67 | int lull_mod = allowed_lull * 5; |
2f03f956 AT |
68 | OFF_T offset = 0; |
69 | ||
a3221d2a WD |
70 | if (!(s = new(struct sum_struct))) |
71 | out_of_memory("receive_sums"); | |
2f03f956 | 72 | |
fc0257c9 S |
73 | read_sum_head(f, s); |
74 | ||
2f03f956 AT |
75 | s->sums = NULL; |
76 | ||
5a18b34d | 77 | if (DEBUG_GTE(DELTASUM, 3)) { |
6d56efa6 | 78 | rprintf(FINFO, "count=%s n=%ld rem=%ld\n", |
adc2476f | 79 | big_num(s->count), (long)s->blength, (long)s->remainder); |
da9d12f5 | 80 | } |
2f03f956 | 81 | |
f3d6d480 | 82 | if (append_mode > 0) { |
6cc11982 WD |
83 | s->flength = (OFF_T)s->count * s->blength; |
84 | if (s->remainder) | |
85 | s->flength -= s->blength - s->remainder; | |
86 | return s; | |
87 | } | |
88 | ||
e6e3f12f | 89 | if (s->count == 0) |
2f03f956 AT |
90 | return(s); |
91 | ||
a3221d2a WD |
92 | if (!(s->sums = new_array(struct sum_buf, s->count))) |
93 | out_of_memory("receive_sums"); | |
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 | ||
92d02148 WD |
108 | if (we_send_keepalive_messages && !(i % lull_mod)) |
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]; | |
127 | struct file_struct *file; | |
f3d6d480 | 128 | struct file_list *flist; |
95306d9d | 129 | |
f3d6d480 | 130 | if (!remove_source_files) |
95306d9d WD |
131 | return; |
132 | ||
e982d591 | 133 | flist = flist_for_ndx(ndx, "successful_send"); |
f3d6d480 | 134 | file = flist->files[ndx - flist->ndx_start]; |
29a89172 | 135 | if (!change_pathname(file, NULL, 0)) |
f3d6d480 WD |
136 | return; |
137 | f_name(file, fname); | |
138 | ||
139 | if (do_unlink(fname) == 0) { | |
951e826b | 140 | if (INFO_GTE(REMOVE, 1)) |
f3d6d480 WD |
141 | rprintf(FINFO, "sender removed %s\n", fname); |
142 | } else | |
143 | rsyserr(FERROR, errno, "sender failed to remove %s", fname); | |
95306d9d | 144 | } |
2f03f956 | 145 | |
16edf865 WD |
146 | static void write_ndx_and_attrs(int f_out, int ndx, int iflags, |
147 | const char *fname, struct file_struct *file, | |
148 | uchar fnamecmp_type, char *buf, int len) | |
d144e43b | 149 | { |
9ae7a2cd | 150 | write_ndx(f_out, ndx); |
d144e43b WD |
151 | if (protocol_version < 29) |
152 | return; | |
153 | write_shortint(f_out, iflags); | |
154 | if (iflags & ITEM_BASIS_TYPE_FOLLOWS) | |
155 | write_byte(f_out, fnamecmp_type); | |
156 | if (iflags & ITEM_XNAME_FOLLOWS) | |
157 | write_vstring(f_out, buf, len); | |
16edf865 | 158 | #ifdef SUPPORT_XATTRS |
d510e82f | 159 | if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers) |
16edf865 WD |
160 | send_xattr_request(fname, file, f_out); |
161 | #endif | |
d144e43b WD |
162 | } |
163 | ||
f3d6d480 | 164 | void send_files(int f_in, int f_out) |
e6e3f12f | 165 | { |
c1659c79 | 166 | int fd = -1; |
2f03f956 | 167 | struct sum_struct *s; |
41cc97ae | 168 | struct map_struct *mbuf = NULL; |
2f03f956 | 169 | STRUCT_STAT st; |
f3d6d480 WD |
170 | char fname[MAXPATHLEN], xname[MAXPATHLEN]; |
171 | const char *path, *slash; | |
4d53c4dd | 172 | uchar fnamecmp_type; |
6087ef2a | 173 | int iflags, xlen; |
2f03f956 | 174 | struct file_struct *file; |
11290705 | 175 | int phase = 0, max_phase = protocol_version >= 29 ? 2 : 1; |
2fedf3d5 WD |
176 | int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i; |
177 | enum logcode log_code = log_before_transfer ? FLOG : FINFO; | |
a0545709 | 178 | int f_xfer = write_batch < 0 ? batch_fd : f_out; |
59924aa8 | 179 | int save_io_error = io_error; |
f3d6d480 | 180 | int ndx, j; |
2f03f956 | 181 | |
951e826b | 182 | if (DEBUG_GTE(SEND, 1)) |
e6e3f12f | 183 | rprintf(FINFO, "send_files starting\n"); |
2f03f956 | 184 | |
2f03f956 | 185 | while (1) { |
7a7810aa | 186 | if (inc_recurse) { |
ce827c3e | 187 | send_extra_file_list(f_out, MIN_FILECNT_LOOKAHEAD); |
7a7810aa WD |
188 | extra_flist_sending_enabled = !flist_eof; |
189 | } | |
f3d6d480 WD |
190 | |
191 | /* This call also sets cur_flist. */ | |
20caffd2 | 192 | ndx = read_ndx_and_attrs(f_in, f_out, &iflags, &fnamecmp_type, |
16edf865 | 193 | xname, &xlen); |
7a7810aa WD |
194 | extra_flist_sending_enabled = False; |
195 | ||
f3d6d480 | 196 | if (ndx == NDX_DONE) { |
951e826b WD |
197 | if (!am_server && INFO_GTE(PROGRESS, 2) && cur_flist) { |
198 | set_current_file_index(NULL, 0); | |
199 | end_progress(0); | |
200 | } | |
3ea6e0e7 | 201 | if (inc_recurse && first_flist) { |
fe16d9a6 | 202 | file_old_total -= first_flist->used; |
f3d6d480 WD |
203 | flist_free(first_flist); |
204 | if (first_flist) { | |
fe16d9a6 WD |
205 | if (first_flist == cur_flist) |
206 | file_old_total = cur_flist->used; | |
9ae7a2cd | 207 | write_ndx(f_out, NDX_DONE); |
f3d6d480 WD |
208 | continue; |
209 | } | |
210 | } | |
11290705 | 211 | if (++phase > max_phase) |
8b48bf11 | 212 | break; |
951e826b | 213 | if (DEBUG_GTE(SEND, 1)) |
8b48bf11 | 214 | rprintf(FINFO, "send_files phase=%d\n", phase); |
9ae7a2cd | 215 | write_ndx(f_out, NDX_DONE); |
8b48bf11 | 216 | continue; |
2f03f956 AT |
217 | } |
218 | ||
16edf865 | 219 | if (inc_recurse) |
ce827c3e | 220 | send_extra_file_list(f_out, MIN_FILECNT_LOOKAHEAD); |
16edf865 | 221 | |
6755a7d7 WD |
222 | if (ndx - cur_flist->ndx_start >= 0) |
223 | file = cur_flist->files[ndx - cur_flist->ndx_start]; | |
224 | else | |
225 | file = dir_flist->files[cur_flist->parent_ndx]; | |
8fc4033e WD |
226 | if (F_PATHNAME(file)) { |
227 | path = F_PATHNAME(file); | |
f3d6d480 WD |
228 | slash = "/"; |
229 | } else { | |
230 | path = slash = ""; | |
231 | } | |
29a89172 | 232 | if (!change_pathname(file, NULL, 0)) |
f3d6d480 WD |
233 | continue; |
234 | f_name(file, fname); | |
7f37167d | 235 | |
951e826b | 236 | if (DEBUG_GTE(SEND, 1)) |
f3d6d480 | 237 | rprintf(FINFO, "send_files(%d, %s%s%s)\n", ndx, path,slash,fname); |
8a9cfb24 | 238 | |
16edf865 | 239 | #ifdef SUPPORT_XATTRS |
d510e82f | 240 | if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers) |
16edf865 WD |
241 | recv_xattr_request(file, f_in); |
242 | #endif | |
243 | ||
fad3dc42 | 244 | if (!(iflags & ITEM_TRANSFER)) { |
6087ef2a | 245 | maybe_log_item(file, iflags, itemizing, xname); |
16edf865 WD |
246 | write_ndx_and_attrs(f_out, ndx, iflags, fname, file, |
247 | fnamecmp_type, xname, xlen); | |
e366e530 WD |
248 | if (iflags & ITEM_IS_NEW) { |
249 | stats.created_files++; | |
250 | if (S_ISREG(file->mode)) { | |
251 | /* Nothing further to count. */ | |
252 | } else if (S_ISDIR(file->mode)) | |
253 | stats.created_dirs++; | |
254 | #ifdef SUPPORT_LINKS | |
255 | else if (S_ISLNK(file->mode)) | |
256 | stats.created_symlinks++; | |
257 | #endif | |
258 | else if (IS_DEVICE(file->mode)) | |
259 | stats.created_devices++; | |
260 | else | |
261 | stats.created_specials++; | |
262 | } | |
165e6d44 WD |
263 | continue; |
264 | } | |
11290705 WD |
265 | if (phase == 2) { |
266 | rprintf(FERROR, | |
267 | "got transfer request in phase 2 [%s]\n", | |
268 | who_am_i()); | |
269 | exit_cleanup(RERR_PROTOCOL); | |
270 | } | |
8a9cfb24 | 271 | |
f3d6d480 WD |
272 | if (file->flags & FLAG_FILE_SENT) { |
273 | if (csum_length == SHORT_SUM_LENGTH) { | |
274 | /* For inplace: redo phase turns off the backup | |
275 | * flag so that we do a regular inplace send. */ | |
276 | make_backups = -make_backups; | |
277 | append_mode = -append_mode; | |
278 | csum_length = SUM_LENGTH; | |
279 | } | |
280 | } else { | |
281 | if (csum_length != SHORT_SUM_LENGTH) { | |
282 | make_backups = -make_backups; | |
283 | append_mode = -append_mode; | |
284 | csum_length = SHORT_SUM_LENGTH; | |
285 | } | |
e366e530 WD |
286 | if (iflags & ITEM_IS_NEW) |
287 | stats.created_files++; | |
f3d6d480 WD |
288 | } |
289 | ||
6087ef2a | 290 | updating_basis_file = inplace && (protocol_version >= 29 |
f3d6d480 | 291 | ? fnamecmp_type == FNAMECMP_FNAME : make_backups <= 0); |
b951e023 | 292 | |
951e826b | 293 | if (!am_server && INFO_GTE(PROGRESS, 1)) |
65b4e4b2 | 294 | set_current_file_index(file, ndx); |
e366e530 | 295 | stats.xferred_files++; |
112d728f | 296 | stats.total_transferred_size += F_LENGTH(file); |
2f03f956 | 297 | |
4dde3347 WD |
298 | if (!log_before_transfer) |
299 | remember_initial_stats(); | |
300 | ||
a0545709 | 301 | if (!do_xfers) { /* log the transfer */ |
4dde3347 | 302 | log_item(FCLIENT, file, iflags, NULL); |
16edf865 WD |
303 | write_ndx_and_attrs(f_out, ndx, iflags, fname, file, |
304 | fnamecmp_type, xname, xlen); | |
2f03f956 AT |
305 | continue; |
306 | } | |
307 | ||
efa95a18 | 308 | if (!(s = receive_sums(f_in))) { |
06c28400 | 309 | io_error |= IOERR_GENERAL; |
342bfb5e | 310 | rprintf(FERROR_XFER, "receive_sums failed\n"); |
f3d6d480 | 311 | exit_cleanup(RERR_PROTOCOL); |
2f03f956 | 312 | } |
76f79ba7 | 313 | |
b9f592fb WD |
314 | fd = do_open(fname, O_RDONLY, 0); |
315 | if (fd == -1) { | |
316 | if (errno == ENOENT) { | |
317 | enum logcode c = am_daemon | |
318 | && protocol_version < 28 ? FERROR | |
3f0211b6 | 319 | : FWARNING; |
b9f592fb WD |
320 | io_error |= IOERR_VANISHED; |
321 | rprintf(c, "file has vanished: %s\n", | |
322 | full_fname(fname)); | |
323 | } else { | |
06c28400 | 324 | io_error |= IOERR_GENERAL; |
3f0211b6 | 325 | rsyserr(FERROR_XFER, errno, |
b9f592fb WD |
326 | "send_files failed to open %s", |
327 | full_fname(fname)); | |
41cc97ae | 328 | } |
b9f592fb | 329 | free_sums(s); |
f3d6d480 WD |
330 | if (protocol_version >= 30) |
331 | send_msg_int(MSG_NO_SEND, ndx); | |
b9f592fb WD |
332 | continue; |
333 | } | |
e6e3f12f | 334 | |
b9f592fb WD |
335 | /* map the local file */ |
336 | if (do_fstat(fd, &st) != 0) { | |
337 | io_error |= IOERR_GENERAL; | |
342bfb5e | 338 | rsyserr(FERROR_XFER, errno, "fstat failed"); |
b9f592fb WD |
339 | free_sums(s); |
340 | close(fd); | |
f397616e | 341 | exit_cleanup(RERR_FILEIO); |
b9f592fb | 342 | } |
11a5a3c7 | 343 | |
96d910c7 | 344 | if (st.st_size) { |
9b919d59 WD |
345 | int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE); |
346 | mbuf = map_file(fd, st.st_size, read_size, s->blength); | |
96d910c7 WD |
347 | } else |
348 | mbuf = NULL; | |
6902ed17 | 349 | |
5a18b34d | 350 | if (DEBUG_GTE(DELTASUM, 2)) { |
6d56efa6 | 351 | rprintf(FINFO, "send_files mapped %s%s%s of size %s\n", |
adc2476f | 352 | path,slash,fname, big_num(st.st_size)); |
6902ed17 | 353 | } |
e6e3f12f | 354 | |
16edf865 WD |
355 | write_ndx_and_attrs(f_out, ndx, iflags, fname, file, |
356 | fnamecmp_type, xname, xlen); | |
a0545709 | 357 | write_sum_head(f_xfer, s); |
b9f592fb | 358 | |
5a18b34d | 359 | if (DEBUG_GTE(DELTASUM, 2)) |
f3d6d480 | 360 | rprintf(FINFO, "calling match_sums %s%s%s\n", path,slash,fname); |
83fff1aa | 361 | |
8a9cfb24 | 362 | if (log_before_transfer) |
4dde3347 | 363 | log_item(FCLIENT, file, iflags, NULL); |
951e826b | 364 | else if (!am_server && INFO_GTE(NAME, 1) && INFO_EQ(PROGRESS, 1)) |
f3d6d480 | 365 | rprintf(FCLIENT, "%s\n", fname); |
ecc81fce | 366 | |
83fff1aa | 367 | set_compression(fname); |
1b7c47cb | 368 | |
a0545709 | 369 | match_sums(f_xfer, s, mbuf, st.st_size); |
951e826b | 370 | if (INFO_GTE(PROGRESS, 1)) |
0394e34a WD |
371 | end_progress(st.st_size); |
372 | ||
4dde3347 | 373 | log_item(log_code, file, iflags, NULL); |
6902ed17 | 374 | |
b9f592fb WD |
375 | if (mbuf) { |
376 | j = unmap_file(mbuf); | |
377 | if (j) { | |
378 | io_error |= IOERR_GENERAL; | |
3f0211b6 | 379 | rsyserr(FERROR_XFER, j, |
b9f592fb WD |
380 | "read errors mapping %s", |
381 | full_fname(fname)); | |
6a7cc46c | 382 | } |
6902ed17 | 383 | } |
b9f592fb | 384 | close(fd); |
e6e3f12f | 385 | |
2f03f956 | 386 | free_sums(s); |
e6e3f12f | 387 | |
951e826b | 388 | if (DEBUG_GTE(SEND, 1)) |
f3d6d480 | 389 | rprintf(FINFO, "sender finished %s%s%s\n", path,slash,fname); |
95306d9d WD |
390 | |
391 | /* Flag that we actually sent this entry. */ | |
f3d6d480 | 392 | file->flags |= FLAG_FILE_SENT; |
2f03f956 | 393 | } |
f3d6d480 WD |
394 | if (make_backups < 0) |
395 | make_backups = -make_backups; | |
2f03f956 | 396 | |
59924aa8 WD |
397 | if (io_error != save_io_error && protocol_version >= 30) |
398 | send_msg_int(MSG_IO_ERROR, io_error); | |
399 | ||
951e826b | 400 | if (DEBUG_GTE(SEND, 1)) |
e6e3f12f | 401 | rprintf(FINFO, "send files finished\n"); |
2f03f956 AT |
402 | |
403 | match_report(); | |
404 | ||
9ae7a2cd | 405 | write_ndx(f_out, NDX_DONE); |
2f03f956 | 406 | } |