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