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