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