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 | } | |
ffa8ab8e WD |
170 | if (fd != -1 && (j = do_lseek(fd, offset, SEEK_SET)) != offset) { |
171 | rsyserr(FERROR, errno, "lseek of %s returned %.0f, not %.0f", | |
172 | full_fname(fname), (double)j, (double)offset); | |
6cc11982 WD |
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) { |
ffa8ab8e | 221 | OFF_T pos; |
c52461f9 WD |
222 | if (flush_write_file(fd) < 0) |
223 | goto report_write_error; | |
89e540e6 | 224 | offset += len; |
ffa8ab8e | 225 | if ((pos = do_lseek(fd, len, SEEK_CUR)) != offset) { |
fab65a5b | 226 | rsyserr(FERROR, errno, |
ffa8ab8e WD |
227 | "lseek of %s returned %.0f, not %.0f", |
228 | full_fname(fname), | |
229 | (double)pos, (double)offset); | |
fab65a5b WD |
230 | exit_cleanup(RERR_FILEIO); |
231 | } | |
89e540e6 | 232 | continue; |
a3221d2a | 233 | } |
2f03f956 | 234 | } |
1ed91a04 | 235 | if (fd != -1 && map && write_file(fd, map, len) != (int)len) |
c52461f9 | 236 | goto report_write_error; |
2f03f956 AT |
237 | offset += len; |
238 | } | |
239 | ||
85f14172 WD |
240 | if (flush_write_file(fd) < 0) |
241 | goto report_write_error; | |
76c21947 | 242 | |
4f5b0756 | 243 | #ifdef HAVE_FTRUNCATE |
fab65a5b | 244 | if (inplace && fd != -1) |
a3221d2a WD |
245 | ftruncate(fd, offset); |
246 | #endif | |
247 | ||
16417f8b WD |
248 | if (do_progress) |
249 | end_progress(total_size); | |
2f03f956 AT |
250 | |
251 | if (fd != -1 && offset > 0 && sparse_end(fd) != 0) { | |
c52461f9 | 252 | report_write_error: |
d62bcc17 WD |
253 | rsyserr(FERROR, errno, "write failed on %s", |
254 | full_fname(fname)); | |
65417579 | 255 | exit_cleanup(RERR_FILEIO); |
2f03f956 AT |
256 | } |
257 | ||
258 | sum_end(file_sum1); | |
259 | ||
7e5fa372 WD |
260 | if (mapbuf) |
261 | unmap_file(mapbuf); | |
262 | ||
bc63ae3f | 263 | read_buf(f_in,file_sum2,MD4_SUM_LENGTH); |
d2a918b4 | 264 | if (verbose > 2) |
bc63ae3f | 265 | rprintf(FINFO,"got file_sum\n"); |
d2a918b4 | 266 | if (fd != -1 && memcmp(file_sum1, file_sum2, MD4_SUM_LENGTH) != 0) |
bc63ae3f | 267 | return 0; |
2f03f956 AT |
268 | return 1; |
269 | } | |
270 | ||
271 | ||
8ed9d849 WD |
272 | static void discard_receive_data(int f_in, OFF_T length) |
273 | { | |
7e5fa372 | 274 | receive_data(f_in, NULL, -1, 0, NULL, -1, length); |
8ed9d849 WD |
275 | } |
276 | ||
42be5320 WD |
277 | static void handle_delayed_updates(struct file_list *flist, char *local_name) |
278 | { | |
279 | char *fname, *partialptr, numbuf[4]; | |
280 | int i; | |
281 | ||
10f994a5 | 282 | for (i = -1; (i = bitbag_next_bit(delayed_bits, i)) >= 0; ) { |
42be5320 | 283 | struct file_struct *file = flist->files[i]; |
6cbde57d | 284 | fname = local_name ? local_name : f_name(file, NULL); |
42be5320 WD |
285 | if ((partialptr = partial_dir_fname(fname)) != NULL) { |
286 | if (make_backups && !make_backup(fname)) | |
287 | continue; | |
288 | if (verbose > 2) { | |
289 | rprintf(FINFO, "renaming %s to %s\n", | |
45c49b52 | 290 | partialptr, fname); |
42be5320 | 291 | } |
6a94c58b WD |
292 | /* We don't use robust_rename() here because the |
293 | * partial-dir must be on the same drive. */ | |
42be5320 WD |
294 | if (do_rename(partialptr, fname) < 0) { |
295 | rsyserr(FERROR, errno, | |
296 | "rename failed for %s (from %s)", | |
45c49b52 | 297 | full_fname(fname), partialptr); |
42be5320 WD |
298 | } else { |
299 | if (remove_sent_files | |
300 | || (preserve_hard_links | |
301 | && file->link_u.links)) { | |
302 | SIVAL(numbuf, 0, i); | |
303 | send_msg(MSG_SUCCESS,numbuf,4); | |
304 | } | |
45c49b52 | 305 | handle_partial_dir(partialptr, PDIR_DELETE); |
42be5320 WD |
306 | } |
307 | } | |
308 | } | |
309 | } | |
310 | ||
154cdaaa WD |
311 | static int get_next_gen_i(int batch_gen_fd, int next_gen_i, int desired_i) |
312 | { | |
313 | while (next_gen_i < desired_i) { | |
314 | if (next_gen_i >= 0) { | |
315 | rprintf(FINFO, | |
1ed91a04 WD |
316 | "(No batched update for%s \"%s\")\n", |
317 | phase ? " resend of" : "", | |
6cbde57d | 318 | f_name(the_file_list->files[next_gen_i], NULL)); |
154cdaaa WD |
319 | } |
320 | next_gen_i = read_int(batch_gen_fd); | |
321 | if (next_gen_i == -1) | |
322 | next_gen_i = the_file_list->count; | |
323 | } | |
324 | return next_gen_i; | |
325 | } | |
326 | ||
8ed9d849 | 327 | |
b35d0d8e MP |
328 | /** |
329 | * main routine for receiver process. | |
330 | * | |
331 | * Receiver process runs on the same host as the generator process. */ | |
9e4a8d29 | 332 | int recv_files(int f_in, struct file_list *flist, char *local_name) |
17fadf7d | 333 | { |
16cc9ca2 | 334 | int next_gen_i = -1; |
2f03f956 AT |
335 | int fd1,fd2; |
336 | STRUCT_STAT st; | |
b9232d45 | 337 | int iflags, xlen; |
446e239e | 338 | char *fname, fbuf[MAXPATHLEN]; |
b9232d45 | 339 | char xname[MAXPATHLEN]; |
2f03f956 | 340 | char fnametmp[MAXPATHLEN]; |
3e870a64 | 341 | char *fnamecmp, *partialptr, numbuf[4]; |
375a4556 | 342 | char fnamecmpbuf[MAXPATHLEN]; |
9e4a8d29 | 343 | uchar fnamecmp_type; |
2f03f956 | 344 | struct file_struct *file; |
1b7c47cb | 345 | struct stats initial_stats; |
4e834af1 | 346 | int save_make_backups = make_backups; |
ecc7623e | 347 | int itemizing = am_server ? logfile_format_has_i : 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 | ||
95ae5224 | 594 | cleanup_set(fnametmp, partialptr, file, fd1, fd2); |
a3221d2a | 595 | } |
2f03f956 | 596 | |
a8ed4958 | 597 | /* log the transfer */ |
910e89b9 | 598 | if (log_before_transfer) |
9497b0d4 | 599 | log_item(file, &initial_stats, iflags, NULL); |
4bb0af79 | 600 | else if (!am_server && verbose && do_progress) |
45c49b52 | 601 | rprintf(FINFO, "%s\n", fname); |
11a5a3c7 | 602 | |
2f03f956 | 603 | /* recv file data */ |
7e5fa372 WD |
604 | recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size, |
605 | fname, fd2, file->length); | |
1b7c47cb | 606 | |
910e89b9 | 607 | if (!log_before_transfer) |
9497b0d4 | 608 | log_item(file, &initial_stats, iflags, NULL); |
17fadf7d | 609 | |
d2a918b4 | 610 | if (fd1 != -1) |
2f03f956 | 611 | close(fd1); |
9f27cd8c | 612 | if (close(fd2) < 0) { |
d62bcc17 WD |
613 | rsyserr(FERROR, errno, "close failed on %s", |
614 | full_fname(fnametmp)); | |
9f27cd8c WD |
615 | exit_cleanup(RERR_FILEIO); |
616 | } | |
17fadf7d | 617 | |
68795c64 | 618 | if ((recv_ok && (!delay_updates || !partialptr)) || inplace) { |
01d124d9 WD |
619 | char *temp_copy_name; |
620 | if (partialptr == fname) | |
621 | partialptr = temp_copy_name = NULL; | |
622 | else if (*partial_dir == '/') | |
623 | temp_copy_name = NULL; | |
624 | else | |
625 | temp_copy_name = partialptr; | |
626 | finish_transfer(fname, fnametmp, temp_copy_name, | |
4a4d2b1b WD |
627 | file, recv_ok, 1); |
628 | if (fnamecmp == partialptr) { | |
aec6b9f8 WD |
629 | do_unlink(partialptr); |
630 | handle_partial_dir(partialptr, PDIR_DELETE); | |
631 | } | |
632 | } else if (keep_partial && partialptr | |
633 | && handle_partial_dir(partialptr, PDIR_CREATE)) { | |
4a4d2b1b WD |
634 | finish_transfer(partialptr, fnametmp, NULL, |
635 | file, recv_ok, !partial_dir); | |
3e7934a5 | 636 | if (delay_updates && recv_ok) { |
10f994a5 | 637 | bitbag_set_bit(delayed_bits, i); |
3e7934a5 WD |
638 | recv_ok = -1; |
639 | } | |
aec6b9f8 | 640 | } else { |
a7260c40 | 641 | partialptr = NULL; |
55e50d89 | 642 | do_unlink(fnametmp); |
a7260c40 WD |
643 | } |
644 | ||
2f03f956 | 645 | cleanup_disable(); |
554e0a8d | 646 | |
22907b6b WD |
647 | if (recv_ok > 0) { |
648 | if (remove_sent_files | |
649 | || (preserve_hard_links && file->link_u.links)) { | |
3e870a64 WD |
650 | SIVAL(numbuf, 0, i); |
651 | send_msg(MSG_SUCCESS, numbuf, 4); | |
652 | } | |
22907b6b | 653 | } else if (!recv_ok) { |
ed7e7955 | 654 | int msgtype = phase || read_batch ? FERROR : FINFO; |
007e3c0e | 655 | if (msgtype == FERROR || verbose) { |
a7260c40 WD |
656 | char *errstr, *redostr, *keptstr; |
657 | if (!(keep_partial && partialptr) && !inplace) | |
658 | keptstr = "discarded"; | |
659 | else if (partial_dir) | |
660 | keptstr = "put into partial-dir"; | |
661 | else | |
662 | keptstr = "retained"; | |
007e3c0e WD |
663 | if (msgtype == FERROR) { |
664 | errstr = "ERROR"; | |
665 | redostr = ""; | |
666 | } else { | |
667 | errstr = "WARNING"; | |
668 | redostr = " (will try again)"; | |
669 | } | |
670 | rprintf(msgtype, | |
a7260c40 | 671 | "%s: %s failed verification -- update %s%s.\n", |
45c49b52 | 672 | errstr, fname, keptstr, redostr); |
007e3c0e | 673 | } |
ed7e7955 | 674 | if (!phase) { |
3e870a64 WD |
675 | SIVAL(numbuf, 0, i); |
676 | send_msg(MSG_REDO, numbuf, 4); | |
2f03f956 AT |
677 | } |
678 | } | |
679 | } | |
4e834af1 | 680 | make_backups = save_make_backups; |
2f03f956 | 681 | |
ac3f7b81 WD |
682 | if (phase == 2 && delay_updates) /* for protocol_version < 29 */ |
683 | handle_delayed_updates(flist, local_name); | |
48e1c8c6 | 684 | |
2f03f956 AT |
685 | if (verbose > 2) |
686 | rprintf(FINFO,"recv_files finished\n"); | |
17fadf7d | 687 | |
2f03f956 AT |
688 | return 0; |
689 | } |