Commit | Line | Data |
---|---|---|
0f78b815 WD |
1 | /* |
2 | * Routines only used by the receiving process. | |
3 | * | |
4 | * Copyright (C) 1996-2000 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; | |
a0009fc3 | 26 | extern int do_xfers; |
8715db2c WD |
27 | extern int am_server; |
28 | extern int do_progress; | |
910e89b9 | 29 | extern int log_before_transfer; |
227a9c41 | 30 | extern int log_format_has_i; |
ecc7623e | 31 | extern int logfile_format_has_i; |
2f03f956 | 32 | extern int csum_length; |
16cc9ca2 | 33 | extern int read_batch; |
a0009fc3 | 34 | extern int write_batch; |
16cc9ca2 | 35 | extern int batch_gen_fd; |
41cfde6b | 36 | extern int protocol_version; |
2f03f956 AT |
37 | extern int relative_paths; |
38 | extern int preserve_hard_links; | |
92b9eb97 | 39 | extern int preserve_perms; |
c56595d7 | 40 | extern int basis_dir_cnt; |
53dd3135 | 41 | extern int make_backups; |
925c517f | 42 | extern int cleanup_got_literal; |
3e870a64 | 43 | extern int remove_sent_files; |
6cc11982 | 44 | extern int append_mode; |
e7ee91de | 45 | extern int sparse_files; |
55e50d89 | 46 | extern int keep_partial; |
ba582f75 | 47 | extern int checksum_seed; |
a3221d2a | 48 | extern int inplace; |
48e1c8c6 | 49 | extern int delay_updates; |
8715db2c | 50 | extern struct stats stats; |
910e89b9 | 51 | extern char *log_format; |
8715db2c WD |
52 | extern char *tmpdir; |
53 | extern char *partial_dir; | |
54 | extern char *basis_dir[]; | |
154cdaaa | 55 | extern struct file_list *the_file_list; |
7842418b | 56 | extern struct filter_list_struct server_filter_list; |
5ebab6c1 | 57 | |
10f994a5 | 58 | static struct bitbag *delayed_bits = NULL; |
1ed91a04 | 59 | static int phase = 0; |
3e7934a5 | 60 | |
2f03f956 | 61 | |
52d3e106 S |
62 | /* |
63 | * get_tmpname() - create a tmp filename for a given filename | |
64 | * | |
65 | * If a tmpdir is defined, use that as the directory to | |
66 | * put it in. Otherwise, the tmp filename is in the same | |
67 | * directory as the given name. Note that there may be no | |
68 | * directory at all in the given name! | |
17fadf7d | 69 | * |
52d3e106 S |
70 | * The tmp filename is basically the given filename with a |
71 | * dot prepended, and .XXXXXX appended (for mkstemp() to | |
72 | * put its unique gunk in). Take care to not exceed | |
73 | * either the MAXPATHLEN or NAME_MAX, esp. the last, as | |
74 | * the basename basically becomes 8 chars longer. In that | |
75 | * case, the original name is shortened sufficiently to | |
76 | * make it all fit. | |
17fadf7d | 77 | * |
52d3e106 S |
78 | * Of course, there's no real reason for the tmp name to |
79 | * look like the original, except to satisfy us humans. | |
80 | * As long as it's unique, rsync will work. | |
81 | */ | |
82 | ||
2f03f956 AT |
83 | static int get_tmpname(char *fnametmp, char *fname) |
84 | { | |
85 | char *f; | |
52d3e106 S |
86 | int length = 0; |
87 | int maxname; | |
2f03f956 | 88 | |
2f03f956 | 89 | if (tmpdir) { |
a24639bb | 90 | /* Note: this can't overflow, so the return value is safe */ |
b7cee949 | 91 | length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2); |
52d3e106 S |
92 | fnametmp[length++] = '/'; |
93 | fnametmp[length] = '\0'; /* always NULL terminated */ | |
0c2ef5f4 | 94 | } |
52d3e106 | 95 | |
0c2ef5f4 | 96 | if ((f = strrchr(fname, '/')) != NULL) { |
52d3e106 S |
97 | ++f; |
98 | if (!tmpdir) { | |
99 | length = f - fname; | |
0c2ef5f4 | 100 | /* copy up to and including the slash */ |
52d3e106 | 101 | strlcpy(fnametmp, fname, length + 1); |
0c2ef5f4 | 102 | } |
446e239e | 103 | } else |
52d3e106 | 104 | f = fname; |
52d3e106 S |
105 | fnametmp[length++] = '.'; |
106 | fnametmp[length] = '\0'; /* always NULL terminated */ | |
2f03f956 | 107 | |
52d3e106 | 108 | maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8); |
2f03f956 | 109 | |
0c2ef5f4 | 110 | if (maxname < 1) { |
45c49b52 | 111 | rprintf(FERROR, "temporary filename too long: %s\n", fname); |
52d3e106 | 112 | fnametmp[0] = '\0'; |
2f03f956 AT |
113 | return 0; |
114 | } | |
115 | ||
17fadf7d | 116 | strlcpy(fnametmp + length, f, maxname); |
52d3e106 | 117 | strcat(fnametmp + length, ".XXXXXX"); |
2f03f956 AT |
118 | |
119 | return 1; | |
120 | } | |
121 | ||
122 | ||
7e5fa372 WD |
123 | static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r, |
124 | char *fname, int fd, OFF_T total_size) | |
2f03f956 | 125 | { |
7e5fa372 WD |
126 | static char file_sum1[MD4_SUM_LENGTH]; |
127 | static char file_sum2[MD4_SUM_LENGTH]; | |
128 | struct map_struct *mapbuf; | |
fc0257c9 | 129 | struct sum_struct sum; |
6c495e0d | 130 | int32 len; |
2f03f956 | 131 | OFF_T offset = 0; |
89e540e6 | 132 | OFF_T offset2; |
2f03f956 | 133 | char *data; |
6c495e0d | 134 | int32 i; |
e1f67417 | 135 | char *map = NULL; |
17fadf7d | 136 | |
fc0257c9 | 137 | read_sum_head(f_in, &sum); |
17fadf7d | 138 | |
7e5fa372 | 139 | if (fd_r >= 0 && size_r > 0) { |
80264051 WD |
140 | int32 read_size = MAX(sum.blength * 2, 16*1024); |
141 | mapbuf = map_file(fd_r, size_r, read_size, sum.blength); | |
7e5fa372 WD |
142 | if (verbose > 2) { |
143 | rprintf(FINFO, "recv mapped %s of size %.0f\n", | |
45c49b52 | 144 | fname_r, (double)size_r); |
7e5fa372 WD |
145 | } |
146 | } else | |
147 | mapbuf = NULL; | |
148 | ||
ba582f75 | 149 | sum_init(checksum_seed); |
17fadf7d | 150 | |
6cc11982 WD |
151 | if (append_mode) { |
152 | OFF_T j; | |
153 | sum.flength = (OFF_T)sum.count * sum.blength; | |
154 | if (sum.remainder) | |
155 | sum.flength -= sum.blength - sum.remainder; | |
156 | for (j = CHUNK_SIZE; j < sum.flength; j += CHUNK_SIZE) { | |
97894c64 WD |
157 | if (do_progress) |
158 | show_progress(offset, total_size); | |
6cc11982 WD |
159 | sum_update(map_ptr(mapbuf, offset, CHUNK_SIZE), |
160 | CHUNK_SIZE); | |
161 | offset = j; | |
162 | } | |
163 | if (offset < sum.flength) { | |
164 | int32 len = sum.flength - offset; | |
97894c64 WD |
165 | if (do_progress) |
166 | show_progress(offset, total_size); | |
6cc11982 WD |
167 | sum_update(map_ptr(mapbuf, offset, len), len); |
168 | offset = sum.flength; | |
169 | } | |
170 | if (fd != -1 && do_lseek(fd, offset, SEEK_SET) != offset) { | |
171 | rsyserr(FERROR, errno, "lseek failed on %s", | |
172 | full_fname(fname)); | |
173 | exit_cleanup(RERR_FILEIO); | |
174 | } | |
175 | } | |
176 | ||
3f55bd5d | 177 | while ((i = recv_token(f_in, &data)) != 0) { |
16417f8b WD |
178 | if (do_progress) |
179 | show_progress(offset, total_size); | |
2f03f956 AT |
180 | |
181 | if (i > 0) { | |
2f03f956 | 182 | if (verbose > 3) { |
5f808dfb AT |
183 | rprintf(FINFO,"data recv %d at %.0f\n", |
184 | i,(double)offset); | |
2f03f956 AT |
185 | } |
186 | ||
187 | stats.literal_data += i; | |
188 | cleanup_got_literal = 1; | |
17fadf7d | 189 | |
6c495e0d | 190 | sum_update(data, i); |
2f03f956 | 191 | |
c52461f9 WD |
192 | if (fd != -1 && write_file(fd,data,i) != i) |
193 | goto report_write_error; | |
2f03f956 AT |
194 | offset += i; |
195 | continue; | |
17fadf7d | 196 | } |
2f03f956 AT |
197 | |
198 | i = -(i+1); | |
6c495e0d | 199 | offset2 = i * (OFF_T)sum.blength; |
fc0257c9 | 200 | len = sum.blength; |
d2a918b4 | 201 | if (i == (int)sum.count-1 && sum.remainder != 0) |
fc0257c9 | 202 | len = sum.remainder; |
17fadf7d | 203 | |
2f03f956 | 204 | stats.matched_data += len; |
17fadf7d | 205 | |
6c495e0d WD |
206 | if (verbose > 3) { |
207 | rprintf(FINFO, | |
208 | "chunk[%d] of size %ld at %.0f offset=%.0f\n", | |
209 | i, (long)len, (double)offset2, (double)offset); | |
210 | } | |
17fadf7d | 211 | |
446e239e WD |
212 | if (mapbuf) { |
213 | map = map_ptr(mapbuf,offset2,len); | |
17fadf7d | 214 | |
c55f7021 | 215 | see_token(map, len); |
6c495e0d | 216 | sum_update(map, len); |
c55f7021 | 217 | } |
17fadf7d | 218 | |
fab65a5b | 219 | if (inplace) { |
89e540e6 | 220 | if (offset == offset2 && fd != -1) { |
c52461f9 WD |
221 | if (flush_write_file(fd) < 0) |
222 | goto report_write_error; | |
89e540e6 WD |
223 | offset += len; |
224 | if (do_lseek(fd, len, SEEK_CUR) != offset) { | |
fab65a5b WD |
225 | rsyserr(FERROR, errno, |
226 | "lseek failed on %s", | |
227 | full_fname(fname)); | |
228 | exit_cleanup(RERR_FILEIO); | |
229 | } | |
89e540e6 | 230 | continue; |
a3221d2a | 231 | } |
2f03f956 | 232 | } |
1ed91a04 | 233 | if (fd != -1 && map && write_file(fd, map, len) != (int)len) |
c52461f9 | 234 | goto report_write_error; |
2f03f956 AT |
235 | offset += len; |
236 | } | |
237 | ||
85f14172 WD |
238 | if (flush_write_file(fd) < 0) |
239 | goto report_write_error; | |
76c21947 | 240 | |
4f5b0756 | 241 | #ifdef HAVE_FTRUNCATE |
fab65a5b | 242 | if (inplace && fd != -1) |
a3221d2a WD |
243 | ftruncate(fd, offset); |
244 | #endif | |
245 | ||
16417f8b WD |
246 | if (do_progress) |
247 | end_progress(total_size); | |
2f03f956 AT |
248 | |
249 | if (fd != -1 && offset > 0 && sparse_end(fd) != 0) { | |
c52461f9 | 250 | report_write_error: |
d62bcc17 WD |
251 | rsyserr(FERROR, errno, "write failed on %s", |
252 | full_fname(fname)); | |
65417579 | 253 | exit_cleanup(RERR_FILEIO); |
2f03f956 AT |
254 | } |
255 | ||
256 | sum_end(file_sum1); | |
257 | ||
7e5fa372 WD |
258 | if (mapbuf) |
259 | unmap_file(mapbuf); | |
260 | ||
bc63ae3f | 261 | read_buf(f_in,file_sum2,MD4_SUM_LENGTH); |
d2a918b4 | 262 | if (verbose > 2) |
bc63ae3f | 263 | rprintf(FINFO,"got file_sum\n"); |
d2a918b4 | 264 | if (fd != -1 && memcmp(file_sum1, file_sum2, MD4_SUM_LENGTH) != 0) |
bc63ae3f | 265 | return 0; |
2f03f956 AT |
266 | return 1; |
267 | } | |
268 | ||
269 | ||
8ed9d849 WD |
270 | static void discard_receive_data(int f_in, OFF_T length) |
271 | { | |
7e5fa372 | 272 | receive_data(f_in, NULL, -1, 0, NULL, -1, length); |
8ed9d849 WD |
273 | } |
274 | ||
42be5320 WD |
275 | static void handle_delayed_updates(struct file_list *flist, char *local_name) |
276 | { | |
277 | char *fname, *partialptr, numbuf[4]; | |
278 | int i; | |
279 | ||
10f994a5 | 280 | for (i = -1; (i = bitbag_next_bit(delayed_bits, i)) >= 0; ) { |
42be5320 | 281 | struct file_struct *file = flist->files[i]; |
6cbde57d | 282 | fname = local_name ? local_name : f_name(file, NULL); |
42be5320 WD |
283 | if ((partialptr = partial_dir_fname(fname)) != NULL) { |
284 | if (make_backups && !make_backup(fname)) | |
285 | continue; | |
286 | if (verbose > 2) { | |
287 | rprintf(FINFO, "renaming %s to %s\n", | |
45c49b52 | 288 | partialptr, fname); |
42be5320 | 289 | } |
6a94c58b WD |
290 | /* We don't use robust_rename() here because the |
291 | * partial-dir must be on the same drive. */ | |
42be5320 WD |
292 | if (do_rename(partialptr, fname) < 0) { |
293 | rsyserr(FERROR, errno, | |
294 | "rename failed for %s (from %s)", | |
45c49b52 | 295 | full_fname(fname), partialptr); |
42be5320 WD |
296 | } else { |
297 | if (remove_sent_files | |
298 | || (preserve_hard_links | |
299 | && file->link_u.links)) { | |
300 | SIVAL(numbuf, 0, i); | |
301 | send_msg(MSG_SUCCESS,numbuf,4); | |
302 | } | |
45c49b52 | 303 | handle_partial_dir(partialptr, PDIR_DELETE); |
42be5320 WD |
304 | } |
305 | } | |
306 | } | |
307 | } | |
308 | ||
154cdaaa WD |
309 | static int get_next_gen_i(int batch_gen_fd, int next_gen_i, int desired_i) |
310 | { | |
311 | while (next_gen_i < desired_i) { | |
312 | if (next_gen_i >= 0) { | |
313 | rprintf(FINFO, | |
1ed91a04 WD |
314 | "(No batched update for%s \"%s\")\n", |
315 | phase ? " resend of" : "", | |
6cbde57d | 316 | f_name(the_file_list->files[next_gen_i], NULL)); |
154cdaaa WD |
317 | } |
318 | next_gen_i = read_int(batch_gen_fd); | |
319 | if (next_gen_i == -1) | |
320 | next_gen_i = the_file_list->count; | |
321 | } | |
322 | return next_gen_i; | |
323 | } | |
324 | ||
8ed9d849 | 325 | |
b35d0d8e MP |
326 | /** |
327 | * main routine for receiver process. | |
328 | * | |
329 | * Receiver process runs on the same host as the generator process. */ | |
9e4a8d29 | 330 | int recv_files(int f_in, struct file_list *flist, char *local_name) |
17fadf7d | 331 | { |
16cc9ca2 | 332 | int next_gen_i = -1; |
2f03f956 AT |
333 | int fd1,fd2; |
334 | STRUCT_STAT st; | |
b9232d45 | 335 | int iflags, xlen; |
446e239e | 336 | char *fname, fbuf[MAXPATHLEN]; |
b9232d45 | 337 | char xname[MAXPATHLEN]; |
2f03f956 | 338 | char fnametmp[MAXPATHLEN]; |
3e870a64 | 339 | char *fnamecmp, *partialptr, numbuf[4]; |
375a4556 | 340 | char fnamecmpbuf[MAXPATHLEN]; |
9e4a8d29 | 341 | uchar fnamecmp_type; |
2f03f956 | 342 | struct file_struct *file; |
1b7c47cb | 343 | struct stats initial_stats; |
4e834af1 | 344 | int save_make_backups = make_backups; |
ecc7623e | 345 | int itemizing = am_server ? logfile_format_has_i : log_format_has_i; |
1ed91a04 | 346 | int max_phase = protocol_version >= 29 ? 2 : 1; |
ac3f7b81 | 347 | int i, recv_ok; |
11a5a3c7 | 348 | |
d2a918b4 | 349 | if (verbose > 2) |
2f03f956 | 350 | rprintf(FINFO,"recv_files(%d) starting\n",flist->count); |
2f03f956 | 351 | |
cb869c26 | 352 | if (flist->hlink_pool) { |
9935066b S |
353 | pool_destroy(flist->hlink_pool); |
354 | flist->hlink_pool = NULL; | |
355 | } | |
356 | ||
3e7934a5 | 357 | if (delay_updates) |
10f994a5 | 358 | delayed_bits = bitbag_create(flist->count); |
48e1c8c6 | 359 | |
17fadf7d | 360 | while (1) { |
2f03f956 AT |
361 | cleanup_disable(); |
362 | ||
363 | i = read_int(f_in); | |
364 | if (i == -1) { | |
16cc9ca2 | 365 | if (read_batch) { |
154cdaaa WD |
366 | get_next_gen_i(batch_gen_fd, next_gen_i, |
367 | flist->count); | |
16cc9ca2 WD |
368 | next_gen_i = -1; |
369 | } | |
ac3f7b81 | 370 | if (++phase > max_phase) |
4e834af1 | 371 | break; |
4e834af1 WD |
372 | csum_length = SUM_LENGTH; |
373 | if (verbose > 2) | |
374 | rprintf(FINFO, "recv_files phase=%d\n", phase); | |
ac3f7b81 | 375 | if (phase == 2 && delay_updates) |
42be5320 | 376 | handle_delayed_updates(flist, local_name); |
4e834af1 | 377 | send_msg(MSG_DONE, "", 0); |
aec6b9f8 | 378 | if (keep_partial && !partial_dir) |
4e834af1 | 379 | make_backups = 0; /* prevents double backup */ |
e7ee91de WD |
380 | if (append_mode) { |
381 | append_mode = 0; | |
382 | sparse_files = 0; | |
383 | } | |
4e834af1 | 384 | continue; |
2f03f956 AT |
385 | } |
386 | ||
b9232d45 WD |
387 | iflags = read_item_attrs(f_in, -1, i, &fnamecmp_type, |
388 | xname, &xlen); | |
58a14ed9 WD |
389 | if (iflags == ITEM_IS_NEW) /* no-op packet */ |
390 | continue; | |
2f03f956 AT |
391 | |
392 | file = flist->files[i]; | |
6cbde57d | 393 | fname = local_name ? local_name : f_name(file, fbuf); |
227a9c41 WD |
394 | |
395 | if (verbose > 2) | |
45c49b52 | 396 | rprintf(FINFO, "recv_files(%s)\n", fname); |
910e89b9 | 397 | |
22907b6b | 398 | if (!(iflags & ITEM_TRANSFER)) { |
b9232d45 | 399 | maybe_log_item(file, iflags, itemizing, xname); |
58a14ed9 WD |
400 | continue; |
401 | } | |
ac3f7b81 WD |
402 | if (phase == 2) { |
403 | rprintf(FERROR, | |
404 | "got transfer request in phase 2 [%s]\n", | |
405 | who_am_i()); | |
406 | exit_cleanup(RERR_PROTOCOL); | |
407 | } | |
910e89b9 | 408 | |
97feb557 | 409 | stats.current_file_index = i; |
2f03f956 AT |
410 | stats.num_transferred_files++; |
411 | stats.total_transferred_size += file->length; | |
925c517f | 412 | cleanup_got_literal = 0; |
2f03f956 | 413 | |
8c2ffaf0 WD |
414 | if (server_filter_list.head |
415 | && check_filter(&server_filter_list, fname, 0) < 0) { | |
416 | rprintf(FERROR, "attempt to hack rsync failed.\n"); | |
417 | exit_cleanup(RERR_PROTOCOL); | |
418 | } | |
419 | ||
a0009fc3 | 420 | if (!do_xfers) { /* log the transfer */ |
4bb0af79 | 421 | if (!am_server && log_format) |
9497b0d4 | 422 | log_item(file, &stats, iflags, NULL); |
f957e8fd WD |
423 | if (read_batch) |
424 | discard_receive_data(f_in, file->length); | |
2f03f956 AT |
425 | continue; |
426 | } | |
a0009fc3 WD |
427 | if (write_batch < 0) { |
428 | log_item(file, &stats, iflags, NULL); | |
b10917a4 WD |
429 | if (!am_server) |
430 | discard_receive_data(f_in, file->length); | |
a0009fc3 WD |
431 | continue; |
432 | } | |
2f03f956 | 433 | |
16cc9ca2 | 434 | if (read_batch) { |
154cdaaa | 435 | next_gen_i = get_next_gen_i(batch_gen_fd, next_gen_i, i); |
16cc9ca2 | 436 | if (i < next_gen_i) { |
45c49b52 WD |
437 | rprintf(FINFO, |
438 | "(Skipping batched update for \"%s\")\n", | |
439 | fname); | |
16cc9ca2 WD |
440 | discard_receive_data(f_in, file->length); |
441 | continue; | |
442 | } | |
41cfde6b | 443 | next_gen_i = -1; |
16cc9ca2 WD |
444 | } |
445 | ||
41cfde6b WD |
446 | partialptr = partial_dir ? partial_dir_fname(fname) : fname; |
447 | ||
9e4a8d29 WD |
448 | if (protocol_version >= 29) { |
449 | switch (fnamecmp_type) { | |
41cfde6b | 450 | case FNAMECMP_FNAME: |
a7260c40 | 451 | fnamecmp = fname; |
41cfde6b WD |
452 | break; |
453 | case FNAMECMP_PARTIAL_DIR: | |
9e4a8d29 | 454 | fnamecmp = partialptr; |
41cfde6b WD |
455 | break; |
456 | case FNAMECMP_BACKUP: | |
457 | fnamecmp = get_backup_name(fname); | |
458 | break; | |
73273075 | 459 | case FNAMECMP_FUZZY: |
b9232d45 WD |
460 | if (file->dirname) { |
461 | pathjoin(fnamecmpbuf, MAXPATHLEN, | |
462 | file->dirname, xname); | |
463 | fnamecmp = fnamecmpbuf; | |
464 | } else | |
465 | fnamecmp = xname; | |
73273075 | 466 | break; |
41cfde6b | 467 | default: |
9e4a8d29 | 468 | if (fnamecmp_type >= basis_dir_cnt) { |
c56595d7 WD |
469 | rprintf(FERROR, |
470 | "invalid basis_dir index: %d.\n", | |
9e4a8d29 | 471 | fnamecmp_type); |
c56595d7 WD |
472 | exit_cleanup(RERR_PROTOCOL); |
473 | } | |
41cfde6b | 474 | pathjoin(fnamecmpbuf, sizeof fnamecmpbuf, |
9e4a8d29 | 475 | basis_dir[fnamecmp_type], fname); |
41cfde6b WD |
476 | fnamecmp = fnamecmpbuf; |
477 | break; | |
478 | } | |
9e4a8d29 WD |
479 | if (!fnamecmp || (server_filter_list.head |
480 | && check_filter(&server_filter_list, fname, 0) < 0)) | |
481 | fnamecmp = fname; | |
482 | } else { | |
483 | /* Reminder: --inplace && --partial-dir are never | |
484 | * enabled at the same time. */ | |
485 | if (inplace && make_backups) { | |
486 | if (!(fnamecmp = get_backup_name(fname))) | |
487 | fnamecmp = fname; | |
488 | } else if (partial_dir && partialptr) | |
489 | fnamecmp = partialptr; | |
490 | else | |
491 | fnamecmp = fname; | |
492 | } | |
dc55d7bd | 493 | |
910e89b9 WD |
494 | initial_stats = stats; |
495 | ||
17fadf7d | 496 | /* open the file */ |
8c9fd200 | 497 | fd1 = do_open(fnamecmp, O_RDONLY, 0); |
375a4556 | 498 | |
9e4a8d29 WD |
499 | if (fd1 == -1 && protocol_version < 29) { |
500 | if (fnamecmp != fname) { | |
501 | fnamecmp = fname; | |
502 | fd1 = do_open(fnamecmp, O_RDONLY, 0); | |
503 | } | |
504 | ||
505 | if (fd1 == -1 && basis_dir[0]) { | |
506 | /* pre-29 allowed only one alternate basis */ | |
507 | pathjoin(fnamecmpbuf, sizeof fnamecmpbuf, | |
508 | basis_dir[0], fname); | |
509 | fnamecmp = fnamecmpbuf; | |
510 | fd1 = do_open(fnamecmp, O_RDONLY, 0); | |
511 | } | |
512 | } | |
513 | ||
2f03f956 | 514 | if (fd1 != -1 && do_fstat(fd1,&st) != 0) { |
d62bcc17 WD |
515 | rsyserr(FERROR, errno, "fstat %s failed", |
516 | full_fname(fnamecmp)); | |
8ed9d849 | 517 | discard_receive_data(f_in, file->length); |
2f03f956 AT |
518 | close(fd1); |
519 | continue; | |
520 | } | |
521 | ||
350e4e4d S |
522 | if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) { |
523 | /* this special handling for directories | |
524 | * wouldn't be necessary if robust_rename() | |
525 | * and the underlying robust_unlink could cope | |
526 | * with directories | |
527 | */ | |
ea42541f WD |
528 | rprintf(FERROR,"recv_files: %s is a directory\n", |
529 | full_fname(fnamecmp)); | |
8ed9d849 | 530 | discard_receive_data(f_in, file->length); |
2f03f956 AT |
531 | close(fd1); |
532 | continue; | |
533 | } | |
534 | ||
350e4e4d S |
535 | if (fd1 != -1 && !S_ISREG(st.st_mode)) { |
536 | close(fd1); | |
537 | fd1 = -1; | |
350e4e4d S |
538 | } |
539 | ||
a9d6e6fc WD |
540 | /* If we're not preserving permissions, change the file-list's |
541 | * mode based on the local permissions and some heuristics. */ | |
542 | if (!preserve_perms) { | |
543 | int exists = fd1 != -1; | |
544 | file->mode = dest_mode(file->mode, st.st_mode, exists); | |
4df9f368 AT |
545 | } |
546 | ||
a3221d2a WD |
547 | /* We now check to see if we are writing file "inplace" */ |
548 | if (inplace) { | |
97894c64 | 549 | fd2 = do_open(fname, O_WRONLY|O_CREAT, 0600); |
a3221d2a WD |
550 | if (fd2 == -1) { |
551 | rsyserr(FERROR, errno, "open %s failed", | |
dc55d7bd | 552 | full_fname(fname)); |
8ed9d849 | 553 | discard_receive_data(f_in, file->length); |
a3221d2a WD |
554 | if (fd1 != -1) |
555 | close(fd1); | |
556 | continue; | |
557 | } | |
558 | } else { | |
559 | if (!get_tmpname(fnametmp,fname)) { | |
8ed9d849 | 560 | discard_receive_data(f_in, file->length); |
a3221d2a WD |
561 | if (fd1 != -1) |
562 | close(fd1); | |
563 | continue; | |
564 | } | |
565 | ||
a3221d2a WD |
566 | /* we initially set the perms without the |
567 | * setuid/setgid bits to ensure that there is no race | |
568 | * condition. They are then correctly updated after | |
569 | * the lchown. Thanks to snabb@epipe.fi for pointing | |
570 | * this out. We also set it initially without group | |
571 | * access because of a similar race condition. */ | |
f62c17e3 | 572 | fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS); |
17fadf7d | 573 | |
a3221d2a WD |
574 | /* in most cases parent directories will already exist |
575 | * because their information should have been previously | |
576 | * transferred, but that may not be the case with -R */ | |
577 | if (fd2 == -1 && relative_paths && errno == ENOENT | |
904e5af1 | 578 | && create_directory_path(fnametmp) == 0) { |
b9232d45 WD |
579 | /* Get back to name with XXXXXX in it. */ |
580 | get_tmpname(fnametmp, fname); | |
a3221d2a WD |
581 | fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS); |
582 | } | |
583 | if (fd2 == -1) { | |
584 | rsyserr(FERROR, errno, "mkstemp %s failed", | |
585 | full_fname(fnametmp)); | |
8ed9d849 | 586 | discard_receive_data(f_in, file->length); |
a3221d2a WD |
587 | if (fd1 != -1) |
588 | close(fd1); | |
589 | continue; | |
590 | } | |
591 | ||
a7e60f0c | 592 | if (keep_partial) |
a7260c40 | 593 | cleanup_set(fnametmp, partialptr, file, fd1, fd2); |
a3221d2a | 594 | } |
2f03f956 | 595 | |
a8ed4958 | 596 | /* log the transfer */ |
910e89b9 | 597 | if (log_before_transfer) |
9497b0d4 | 598 | log_item(file, &initial_stats, iflags, NULL); |
4bb0af79 | 599 | else if (!am_server && verbose && do_progress) |
45c49b52 | 600 | rprintf(FINFO, "%s\n", fname); |
11a5a3c7 | 601 | |
2f03f956 | 602 | /* recv file data */ |
7e5fa372 WD |
603 | recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size, |
604 | fname, fd2, file->length); | |
1b7c47cb | 605 | |
910e89b9 | 606 | if (!log_before_transfer) |
9497b0d4 | 607 | log_item(file, &initial_stats, iflags, NULL); |
17fadf7d | 608 | |
d2a918b4 | 609 | if (fd1 != -1) |
2f03f956 | 610 | close(fd1); |
9f27cd8c | 611 | if (close(fd2) < 0) { |
d62bcc17 WD |
612 | rsyserr(FERROR, errno, "close failed on %s", |
613 | full_fname(fnametmp)); | |
9f27cd8c WD |
614 | exit_cleanup(RERR_FILEIO); |
615 | } | |
17fadf7d | 616 | |
68795c64 | 617 | if ((recv_ok && (!delay_updates || !partialptr)) || inplace) { |
01d124d9 WD |
618 | char *temp_copy_name; |
619 | if (partialptr == fname) | |
620 | partialptr = temp_copy_name = NULL; | |
621 | else if (*partial_dir == '/') | |
622 | temp_copy_name = NULL; | |
623 | else | |
624 | temp_copy_name = partialptr; | |
625 | finish_transfer(fname, fnametmp, temp_copy_name, | |
4a4d2b1b WD |
626 | file, recv_ok, 1); |
627 | if (fnamecmp == partialptr) { | |
aec6b9f8 WD |
628 | do_unlink(partialptr); |
629 | handle_partial_dir(partialptr, PDIR_DELETE); | |
630 | } | |
631 | } else if (keep_partial && partialptr | |
632 | && handle_partial_dir(partialptr, PDIR_CREATE)) { | |
4a4d2b1b WD |
633 | finish_transfer(partialptr, fnametmp, NULL, |
634 | file, recv_ok, !partial_dir); | |
3e7934a5 | 635 | if (delay_updates && recv_ok) { |
10f994a5 | 636 | bitbag_set_bit(delayed_bits, i); |
3e7934a5 WD |
637 | recv_ok = -1; |
638 | } | |
aec6b9f8 | 639 | } else { |
a7260c40 | 640 | partialptr = NULL; |
55e50d89 | 641 | do_unlink(fnametmp); |
a7260c40 WD |
642 | } |
643 | ||
2f03f956 | 644 | cleanup_disable(); |
554e0a8d | 645 | |
22907b6b WD |
646 | if (recv_ok > 0) { |
647 | if (remove_sent_files | |
648 | || (preserve_hard_links && file->link_u.links)) { | |
3e870a64 WD |
649 | SIVAL(numbuf, 0, i); |
650 | send_msg(MSG_SUCCESS, numbuf, 4); | |
651 | } | |
22907b6b | 652 | } else if (!recv_ok) { |
ed7e7955 | 653 | int msgtype = phase || read_batch ? FERROR : FINFO; |
007e3c0e | 654 | if (msgtype == FERROR || verbose) { |
a7260c40 WD |
655 | char *errstr, *redostr, *keptstr; |
656 | if (!(keep_partial && partialptr) && !inplace) | |
657 | keptstr = "discarded"; | |
658 | else if (partial_dir) | |
659 | keptstr = "put into partial-dir"; | |
660 | else | |
661 | keptstr = "retained"; | |
007e3c0e WD |
662 | if (msgtype == FERROR) { |
663 | errstr = "ERROR"; | |
664 | redostr = ""; | |
665 | } else { | |
666 | errstr = "WARNING"; | |
667 | redostr = " (will try again)"; | |
668 | } | |
669 | rprintf(msgtype, | |
a7260c40 | 670 | "%s: %s failed verification -- update %s%s.\n", |
45c49b52 | 671 | errstr, fname, keptstr, redostr); |
007e3c0e | 672 | } |
ed7e7955 | 673 | if (!phase) { |
3e870a64 WD |
674 | SIVAL(numbuf, 0, i); |
675 | send_msg(MSG_REDO, numbuf, 4); | |
2f03f956 AT |
676 | } |
677 | } | |
678 | } | |
4e834af1 | 679 | make_backups = save_make_backups; |
2f03f956 | 680 | |
ac3f7b81 WD |
681 | if (phase == 2 && delay_updates) /* for protocol_version < 29 */ |
682 | handle_delayed_updates(flist, local_name); | |
48e1c8c6 | 683 | |
2f03f956 AT |
684 | if (verbose > 2) |
685 | rprintf(FINFO,"recv_files finished\n"); | |
17fadf7d | 686 | |
2f03f956 AT |
687 | return 0; |
688 | } |