Commit | Line | Data |
---|---|---|
e6e3f12f | 1 | /* |
2f03f956 AT |
2 | Copyright (C) Andrew Tridgell 1996 |
3 | Copyright (C) Paul Mackerras 1996 | |
e6e3f12f | 4 | |
2f03f956 AT |
5 | This program is free software; you can redistribute it and/or modify |
6 | it under the terms of the GNU General Public License as published by | |
7 | the Free Software Foundation; either version 2 of the License, or | |
8 | (at your option) any later version. | |
e6e3f12f | 9 | |
2f03f956 AT |
10 | This program is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
e6e3f12f | 14 | |
2f03f956 AT |
15 | You should have received a copy of the GNU General Public License |
16 | along with this program; if not, write to the Free Software | |
17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
18 | */ | |
19 | ||
20 | #include "rsync.h" | |
21 | ||
22 | extern int verbose; | |
a0545709 | 23 | extern int do_xfers; |
8715db2c WD |
24 | extern int am_server; |
25 | extern int am_daemon; | |
8a9cfb24 | 26 | extern int log_before_transfer; |
7f37167d WD |
27 | extern int log_format_has_i; |
28 | extern int daemon_log_format_has_i; | |
2f03f956 | 29 | extern int csum_length; |
2f03f956 | 30 | extern int io_error; |
605fed4b | 31 | extern int allowed_lull; |
d6631cf3 | 32 | extern int protocol_version; |
95306d9d | 33 | extern int remove_sent_files; |
53f8519a | 34 | extern int updating_basis_file; |
8b115ac8 | 35 | extern int make_backups; |
8a9cfb24 | 36 | extern int do_progress; |
53f8519a | 37 | extern int inplace; |
a0545709 WD |
38 | extern int batch_fd; |
39 | extern int write_batch; | |
a3221d2a | 40 | extern struct stats stats; |
5f40615c | 41 | extern struct file_list *the_file_list; |
8715db2c | 42 | extern char *log_format; |
2f03f956 AT |
43 | |
44 | ||
0f9c48b1 MP |
45 | /** |
46 | * @file | |
47 | * | |
48 | * The sender gets checksums from the generator, calculates deltas, | |
49 | * and transmits them to the receiver. The sender process runs on the | |
50 | * machine holding the source files. | |
51 | **/ | |
fc0257c9 | 52 | |
ce8149b6 MP |
53 | /** |
54 | * Receive the checksums for a buffer | |
55 | **/ | |
2f03f956 AT |
56 | static struct sum_struct *receive_sums(int f) |
57 | { | |
58 | struct sum_struct *s; | |
a1cbe76e | 59 | int32 i; |
605fed4b | 60 | int lull_mod = allowed_lull * 5; |
2f03f956 AT |
61 | OFF_T offset = 0; |
62 | ||
a3221d2a WD |
63 | if (!(s = new(struct sum_struct))) |
64 | out_of_memory("receive_sums"); | |
2f03f956 | 65 | |
fc0257c9 S |
66 | read_sum_head(f, s); |
67 | ||
2f03f956 AT |
68 | s->sums = NULL; |
69 | ||
da9d12f5 | 70 | if (verbose > 3) { |
6c495e0d WD |
71 | rprintf(FINFO, "count=%.0f n=%ld rem=%ld\n", |
72 | (double)s->count, (long)s->blength, (long)s->remainder); | |
da9d12f5 | 73 | } |
2f03f956 | 74 | |
e6e3f12f | 75 | if (s->count == 0) |
2f03f956 AT |
76 | return(s); |
77 | ||
a3221d2a WD |
78 | if (!(s->sums = new_array(struct sum_buf, s->count))) |
79 | out_of_memory("receive_sums"); | |
2f03f956 | 80 | |
eed47b6b | 81 | for (i = 0; i < s->count; i++) { |
2f03f956 | 82 | s->sums[i].sum1 = read_int(f); |
e6e3f12f | 83 | read_buf(f, s->sums[i].sum2, s->s2length); |
2f03f956 AT |
84 | |
85 | s->sums[i].offset = offset; | |
a3221d2a | 86 | s->sums[i].flags = 0; |
2f03f956 | 87 | |
eed47b6b | 88 | if (i == s->count-1 && s->remainder != 0) |
2f03f956 | 89 | s->sums[i].len = s->remainder; |
a3221d2a | 90 | else |
fc0257c9 | 91 | s->sums[i].len = s->blength; |
2f03f956 AT |
92 | offset += s->sums[i].len; |
93 | ||
605fed4b WD |
94 | if (allowed_lull && !(i % lull_mod)) |
95 | maybe_send_keepalive(); | |
96 | ||
a3221d2a WD |
97 | if (verbose > 3) { |
98 | rprintf(FINFO, | |
99 | "chunk[%d] len=%d offset=%.0f sum1=%08x\n", | |
100 | i, s->sums[i].len, (double)s->sums[i].offset, | |
101 | s->sums[i].sum1); | |
102 | } | |
2f03f956 AT |
103 | } |
104 | ||
105 | s->flength = offset; | |
106 | ||
107 | return s; | |
108 | } | |
109 | ||
5f40615c | 110 | void successful_send(int ndx) |
95306d9d WD |
111 | { |
112 | char fname[MAXPATHLEN]; | |
113 | struct file_struct *file; | |
114 | unsigned int offset; | |
115 | ||
5f40615c | 116 | if (ndx < 0 || ndx >= the_file_list->count) |
95306d9d WD |
117 | return; |
118 | ||
5f40615c | 119 | file = the_file_list->files[ndx]; |
95306d9d WD |
120 | /* The generator might tell us about symlinks we didn't send. */ |
121 | if (!(file->flags & FLAG_SENT) && !S_ISLNK(file->mode)) | |
122 | return; | |
123 | if (file->dir.root) { | |
124 | offset = stringjoin(fname, sizeof fname, | |
125 | file->dir.root, "/", NULL); | |
126 | } else | |
127 | offset = 0; | |
128 | f_name_to(file, fname + offset); | |
129 | if (remove_sent_files && do_unlink(fname) == 0 && verbose > 1) { | |
130 | rprintf(FINFO, "sender removed %s\n", | |
131 | safe_fname(fname + offset)); | |
132 | } | |
133 | } | |
2f03f956 | 134 | |
927c8068 WD |
135 | static void write_ndx_and_attrs(int f_out, int ndx, int iflags, |
136 | uchar fnamecmp_type, char *buf, int len) | |
4d53c4dd WD |
137 | { |
138 | write_int(f_out, ndx); | |
139 | if (protocol_version < 29) | |
140 | return; | |
141 | write_shortint(f_out, iflags); | |
142 | if (iflags & ITEM_BASIS_TYPE_FOLLOWS) | |
143 | write_byte(f_out, fnamecmp_type); | |
927c8068 | 144 | if (iflags & ITEM_XNAME_FOLLOWS) |
4d53c4dd | 145 | write_vstring(f_out, buf, len); |
4d53c4dd WD |
146 | } |
147 | ||
165e6d44 | 148 | /* This is also used by receive.c with f_out = -1. */ |
6087ef2a WD |
149 | int read_item_attrs(int f_in, int f_out, int ndx, uchar *type_ptr, |
150 | char *buf, int *len_ptr) | |
165e6d44 | 151 | { |
fad3dc42 | 152 | int len; |
4d53c4dd | 153 | uchar fnamecmp_type = FNAMECMP_FNAME; |
165e6d44 | 154 | int iflags = protocol_version >= 29 ? read_shortint(f_in) |
fad3dc42 | 155 | : ITEM_TRANSFER | ITEM_MISSING_DATA; |
165e6d44 WD |
156 | |
157 | /* Handle the new keep-alive (no-op) packet. */ | |
158 | if (ndx == the_file_list->count && iflags == ITEM_IS_NEW) | |
159 | ; | |
160 | else if (ndx < 0 || ndx >= the_file_list->count) { | |
927c8068 | 161 | rprintf(FERROR, "Invalid file index: %d (count=%d) [%s]\n", |
165e6d44 WD |
162 | ndx, the_file_list->count, who_am_i()); |
163 | exit_cleanup(RERR_PROTOCOL); | |
164 | } else if (iflags == ITEM_IS_NEW) { | |
927c8068 WD |
165 | rprintf(FERROR, "Invalid itemized flag word: %x [%s]\n", |
166 | iflags, who_am_i()); | |
165e6d44 WD |
167 | exit_cleanup(RERR_PROTOCOL); |
168 | } | |
169 | ||
4d53c4dd WD |
170 | if (iflags & ITEM_BASIS_TYPE_FOLLOWS) |
171 | fnamecmp_type = read_byte(f_in); | |
172 | *type_ptr = fnamecmp_type; | |
173 | ||
174 | if (iflags & ITEM_XNAME_FOLLOWS) { | |
175 | if ((len = read_vstring(f_in, buf, MAXPATHLEN)) < 0) | |
176 | exit_cleanup(RERR_PROTOCOL); | |
177 | } else { | |
fad3dc42 WD |
178 | *buf = '\0'; |
179 | len = -1; | |
180 | } | |
6087ef2a | 181 | *len_ptr = len; |
fad3dc42 | 182 | |
fad3dc42 WD |
183 | if (iflags & ITEM_TRANSFER) { |
184 | if (!S_ISREG(the_file_list->files[ndx]->mode)) { | |
185 | rprintf(FERROR, | |
927c8068 | 186 | "received request to transfer non-regular file: %d [%s]\n", |
fad3dc42 WD |
187 | ndx, who_am_i()); |
188 | exit_cleanup(RERR_PROTOCOL); | |
189 | } | |
190 | } else if (f_out >= 0) { | |
11290705 | 191 | write_ndx_and_attrs(f_out, ndx, iflags, |
927c8068 | 192 | fnamecmp_type, buf, len); |
165e6d44 WD |
193 | } |
194 | ||
195 | return iflags; | |
196 | } | |
197 | ||
e6e3f12f S |
198 | void send_files(struct file_list *flist, int f_out, int f_in) |
199 | { | |
c1659c79 | 200 | int fd = -1; |
2f03f956 | 201 | struct sum_struct *s; |
41cc97ae | 202 | struct map_struct *mbuf = NULL; |
2f03f956 | 203 | STRUCT_STAT st; |
ecc81fce | 204 | char *fname2, fname[MAXPATHLEN]; |
6087ef2a | 205 | char xname[MAXPATHLEN]; |
4d53c4dd | 206 | uchar fnamecmp_type; |
6087ef2a | 207 | int iflags, xlen; |
2f03f956 | 208 | struct file_struct *file; |
11290705 | 209 | int phase = 0, max_phase = protocol_version >= 29 ? 2 : 1; |
1b7c47cb | 210 | struct stats initial_stats; |
8b115ac8 | 211 | int save_make_backups = make_backups; |
7f37167d WD |
212 | int itemizing = am_daemon ? daemon_log_format_has_i |
213 | : !am_server && log_format_has_i; | |
a0545709 | 214 | int f_xfer = write_batch < 0 ? batch_fd : f_out; |
8a9cfb24 | 215 | int i, j; |
2f03f956 AT |
216 | |
217 | if (verbose > 2) | |
e6e3f12f | 218 | rprintf(FINFO, "send_files starting\n"); |
2f03f956 | 219 | |
2f03f956 | 220 | while (1) { |
3fef5364 | 221 | unsigned int offset; |
2f03f956 AT |
222 | |
223 | i = read_int(f_in); | |
224 | if (i == -1) { | |
11290705 | 225 | if (++phase > max_phase) |
8b48bf11 | 226 | break; |
8b48bf11 WD |
227 | csum_length = SUM_LENGTH; |
228 | if (verbose > 2) | |
229 | rprintf(FINFO, "send_files phase=%d\n", phase); | |
230 | write_int(f_out, -1); | |
231 | /* For inplace: redo phase turns off the backup | |
232 | * flag so that we do a regular inplace send. */ | |
233 | make_backups = 0; | |
234 | continue; | |
2f03f956 AT |
235 | } |
236 | ||
e732fb0c | 237 | iflags = read_item_attrs(f_in, f_out, i, &fnamecmp_type, |
6087ef2a | 238 | xname, &xlen); |
165e6d44 WD |
239 | if (iflags == ITEM_IS_NEW) /* no-op packet */ |
240 | continue; | |
2f03f956 | 241 | |
8a9cfb24 | 242 | file = flist->files[i]; |
7f37167d WD |
243 | if (file->dir.root) { |
244 | /* N.B. We're sure that this fits, so offset is OK. */ | |
245 | offset = strlcpy(fname, file->dir.root, sizeof fname); | |
246 | if (!offset || fname[offset-1] != '/') | |
247 | fname[offset++] = '/'; | |
248 | } else | |
249 | offset = 0; | |
250 | fname2 = f_name_to(file, fname + offset); | |
251 | ||
252 | if (verbose > 2) | |
253 | rprintf(FINFO, "send_files(%d, %s)\n", i, fname); | |
8a9cfb24 | 254 | |
fad3dc42 | 255 | if (!(iflags & ITEM_TRANSFER)) { |
6087ef2a | 256 | maybe_log_item(file, iflags, itemizing, xname); |
165e6d44 WD |
257 | continue; |
258 | } | |
11290705 WD |
259 | if (phase == 2) { |
260 | rprintf(FERROR, | |
261 | "got transfer request in phase 2 [%s]\n", | |
262 | who_am_i()); | |
263 | exit_cleanup(RERR_PROTOCOL); | |
264 | } | |
8a9cfb24 | 265 | |
6087ef2a WD |
266 | updating_basis_file = inplace && (protocol_version >= 29 |
267 | ? fnamecmp_type == FNAMECMP_FNAME : !make_backups); | |
b951e023 | 268 | |
97feb557 | 269 | stats.current_file_index = i; |
2f03f956 AT |
270 | stats.num_transferred_files++; |
271 | stats.total_transferred_size += file->length; | |
272 | ||
a0545709 | 273 | if (!do_xfers) { /* log the transfer */ |
8a832465 | 274 | if (!am_server && log_format) |
b694f8a2 | 275 | log_item(file, &stats, iflags, NULL); |
e732fb0c | 276 | write_ndx_and_attrs(f_out, i, iflags, fnamecmp_type, |
927c8068 | 277 | xname, xlen); |
2f03f956 AT |
278 | continue; |
279 | } | |
280 | ||
1b7c47cb AT |
281 | initial_stats = stats; |
282 | ||
efa95a18 | 283 | if (!(s = receive_sums(f_in))) { |
06c28400 | 284 | io_error |= IOERR_GENERAL; |
e6e3f12f | 285 | rprintf(FERROR, "receive_sums failed\n"); |
2f03f956 AT |
286 | return; |
287 | } | |
76f79ba7 | 288 | |
b9f592fb WD |
289 | fd = do_open(fname, O_RDONLY, 0); |
290 | if (fd == -1) { | |
291 | if (errno == ENOENT) { | |
292 | enum logcode c = am_daemon | |
293 | && protocol_version < 28 ? FERROR | |
294 | : FINFO; | |
295 | io_error |= IOERR_VANISHED; | |
296 | rprintf(c, "file has vanished: %s\n", | |
297 | full_fname(fname)); | |
298 | } else { | |
06c28400 | 299 | io_error |= IOERR_GENERAL; |
b9f592fb WD |
300 | rsyserr(FERROR, errno, |
301 | "send_files failed to open %s", | |
302 | full_fname(fname)); | |
41cc97ae | 303 | } |
b9f592fb WD |
304 | free_sums(s); |
305 | continue; | |
306 | } | |
e6e3f12f | 307 | |
b9f592fb WD |
308 | /* map the local file */ |
309 | if (do_fstat(fd, &st) != 0) { | |
310 | io_error |= IOERR_GENERAL; | |
311 | rsyserr(FERROR, errno, "fstat failed"); | |
312 | free_sums(s); | |
313 | close(fd); | |
314 | return; | |
315 | } | |
11a5a3c7 | 316 | |
96d910c7 | 317 | if (st.st_size) { |
9b919d59 WD |
318 | int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE); |
319 | mbuf = map_file(fd, st.st_size, read_size, s->blength); | |
96d910c7 WD |
320 | } else |
321 | mbuf = NULL; | |
6902ed17 | 322 | |
b9f592fb WD |
323 | if (verbose > 2) { |
324 | rprintf(FINFO, "send_files mapped %s of size %.0f\n", | |
ecc81fce | 325 | safe_fname(fname), (double)st.st_size); |
6902ed17 | 326 | } |
e6e3f12f | 327 | |
e732fb0c | 328 | write_ndx_and_attrs(f_out, i, iflags, fnamecmp_type, |
927c8068 | 329 | xname, xlen); |
a0545709 | 330 | write_sum_head(f_xfer, s); |
b9f592fb | 331 | |
ecc81fce WD |
332 | if (verbose > 2) { |
333 | rprintf(FINFO, "calling match_sums %s\n", | |
334 | safe_fname(fname)); | |
11a5a3c7 | 335 | } |
83fff1aa | 336 | |
8a9cfb24 | 337 | if (log_before_transfer) |
b694f8a2 | 338 | log_item(file, &initial_stats, iflags, NULL); |
8a832465 | 339 | else if (!am_server && verbose && do_progress) |
ecc81fce WD |
340 | rprintf(FINFO, "%s\n", safe_fname(fname2)); |
341 | ||
83fff1aa | 342 | set_compression(fname); |
1b7c47cb | 343 | |
a0545709 | 344 | match_sums(f_xfer, s, mbuf, st.st_size); |
0394e34a WD |
345 | if (do_progress) |
346 | end_progress(st.st_size); | |
347 | ||
8a9cfb24 | 348 | if (!log_before_transfer) |
b694f8a2 | 349 | log_item(file, &initial_stats, iflags, NULL); |
6902ed17 | 350 | |
b9f592fb WD |
351 | if (mbuf) { |
352 | j = unmap_file(mbuf); | |
353 | if (j) { | |
354 | io_error |= IOERR_GENERAL; | |
355 | rsyserr(FERROR, j, | |
356 | "read errors mapping %s", | |
357 | full_fname(fname)); | |
6a7cc46c | 358 | } |
6902ed17 | 359 | } |
b9f592fb | 360 | close(fd); |
e6e3f12f | 361 | |
2f03f956 | 362 | free_sums(s); |
e6e3f12f | 363 | |
ecc81fce WD |
364 | if (verbose > 2) { |
365 | rprintf(FINFO, "sender finished %s\n", | |
366 | safe_fname(fname)); | |
367 | } | |
95306d9d WD |
368 | |
369 | /* Flag that we actually sent this entry. */ | |
370 | file->flags |= FLAG_SENT; | |
2f03f956 | 371 | } |
8b115ac8 | 372 | make_backups = save_make_backups; |
2f03f956 AT |
373 | |
374 | if (verbose > 2) | |
e6e3f12f | 375 | rprintf(FINFO, "send files finished\n"); |
2f03f956 AT |
376 | |
377 | match_report(); | |
378 | ||
e6e3f12f | 379 | write_int(f_out, -1); |
2f03f956 | 380 | } |