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