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