Added /support/savetransfer to .gitignore.
[rsync/rsync.git] / receiver.c
... / ...
CommitLineData
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-2008 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 3 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 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, visit the http://fsf.org website.
20 */
21
22#include "rsync.h"
23
24extern int dry_run;
25extern int do_xfers;
26extern int am_server;
27extern int inc_recurse;
28extern int log_before_transfer;
29extern int stdout_format_has_i;
30extern int logfile_format_has_i;
31extern int csum_length;
32extern int read_batch;
33extern int write_batch;
34extern int batch_gen_fd;
35extern int protocol_version;
36extern int relative_paths;
37extern int preserve_hard_links;
38extern int preserve_perms;
39extern int preserve_xattrs;
40extern int basis_dir_cnt;
41extern int make_backups;
42extern int cleanup_got_literal;
43extern int remove_source_files;
44extern int append_mode;
45extern int sparse_files;
46extern int keep_partial;
47extern int checksum_len;
48extern int checksum_seed;
49extern int inplace;
50extern int delay_updates;
51extern mode_t orig_umask;
52extern struct stats stats;
53extern char *tmpdir;
54extern char *partial_dir;
55extern char *basis_dir[MAX_BASIS_DIRS+1];
56extern char sender_file_sum[MAX_DIGEST_LEN];
57extern struct file_list *cur_flist, *first_flist, *dir_flist;
58extern struct filter_list_struct daemon_filter_list;
59
60static struct bitbag *delayed_bits = NULL;
61static int phase = 0, redoing = 0;
62/* We're either updating the basis file or an identical copy: */
63static int updating_basis_or_equiv;
64
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!
72 *
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.
80 *
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
86int get_tmpname(char *fnametmp, const char *fname)
87{
88 int maxname, added, length = 0;
89 const char *f;
90
91 if (tmpdir) {
92 /* Note: this can't overflow, so the return value is safe */
93 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
94 fnametmp[length++] = '/';
95 }
96
97 if ((f = strrchr(fname, '/')) != NULL) {
98 ++f;
99 if (!tmpdir) {
100 length = f - fname;
101 /* copy up to and including the slash */
102 strlcpy(fnametmp, fname, length + 1);
103 }
104 } else
105 f = fname;
106 fnametmp[length++] = '.';
107
108 /* The maxname value is bufsize, and includes space for the '\0'.
109 * (Note that NAME_MAX get -8 for the leading '.' above.) */
110 maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8);
111
112 if (maxname < 1) {
113 rprintf(FERROR_XFER, "temporary filename too long: %s\n", fname);
114 fnametmp[0] = '\0';
115 return 0;
116 }
117
118 added = strlcpy(fnametmp + length, f, maxname);
119 if (added >= maxname)
120 added = maxname - 1;
121 memcpy(fnametmp + length + added, ".XXXXXX", 8);
122
123 return 1;
124}
125
126/* Opens a temporary file for writing.
127 * Success: Writes name into fnametmp, returns fd.
128 * Failure: Clobbers fnametmp, returns -1.
129 * Calling cleanup_set() is the caller's job. */
130int open_tmpfile(char *fnametmp, const char *fname, struct file_struct *file)
131{
132 int fd;
133
134 if (!get_tmpname(fnametmp, fname))
135 return -1;
136
137 /* We initially set the perms without the setuid/setgid bits or group
138 * access to ensure that there is no race condition. They will be
139 * correctly updated after the right owner and group info is set.
140 * (Thanks to snabb@epipe.fi for pointing this out.) */
141 fd = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
142
143#if 0
144 /* In most cases parent directories will already exist because their
145 * information should have been previously transferred, but that may
146 * not be the case with -R */
147 if (fd == -1 && relative_paths && errno == ENOENT
148 && create_directory_path(fnametmp) == 0) {
149 /* Get back to name with XXXXXX in it. */
150 get_tmpname(fnametmp, fname);
151 fd = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
152 }
153#endif
154
155 if (fd == -1) {
156 rsyserr(FERROR_XFER, errno, "mkstemp %s failed",
157 full_fname(fnametmp));
158 return -1;
159 }
160
161 return fd;
162}
163
164static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
165 const char *fname, int fd, OFF_T total_size)
166{
167 static char file_sum1[MAX_DIGEST_LEN];
168 struct map_struct *mapbuf;
169 struct sum_struct sum;
170 int32 len;
171 OFF_T offset = 0;
172 OFF_T offset2;
173 char *data;
174 int32 i;
175 char *map = NULL;
176
177 read_sum_head(f_in, &sum);
178
179 if (fd_r >= 0 && size_r > 0) {
180 int32 read_size = MAX(sum.blength * 2, 16*1024);
181 mapbuf = map_file(fd_r, size_r, read_size, sum.blength);
182 if (DEBUG_GTE(DELTASUM, 2)) {
183 rprintf(FINFO, "recv mapped %s of size %s\n",
184 fname_r, big_num(size_r, 0));
185 }
186 } else
187 mapbuf = NULL;
188
189 sum_init(checksum_seed);
190
191 if (append_mode > 0) {
192 OFF_T j;
193 sum.flength = (OFF_T)sum.count * sum.blength;
194 if (sum.remainder)
195 sum.flength -= sum.blength - sum.remainder;
196 if (append_mode == 2) {
197 for (j = CHUNK_SIZE; j < sum.flength; j += CHUNK_SIZE) {
198 if (INFO_GTE(PROGRESS, 1))
199 show_progress(offset, total_size);
200 sum_update(map_ptr(mapbuf, offset, CHUNK_SIZE),
201 CHUNK_SIZE);
202 offset = j;
203 }
204 if (offset < sum.flength) {
205 int32 len = (int32)(sum.flength - offset);
206 if (INFO_GTE(PROGRESS, 1))
207 show_progress(offset, total_size);
208 sum_update(map_ptr(mapbuf, offset, len), len);
209 }
210 }
211 offset = sum.flength;
212 if (fd != -1 && (j = do_lseek(fd, offset, SEEK_SET)) != offset) {
213 rsyserr(FERROR_XFER, errno, "lseek of %s returned %s, not %s",
214 full_fname(fname), big_num(j, 0), big_num(offset, 0));
215 exit_cleanup(RERR_FILEIO);
216 }
217 }
218
219 while ((i = recv_token(f_in, &data)) != 0) {
220 if (INFO_GTE(PROGRESS, 1))
221 show_progress(offset, total_size);
222
223 if (i > 0) {
224 if (DEBUG_GTE(DELTASUM, 3)) {
225 rprintf(FINFO,"data recv %d at %s\n",
226 i, big_num(offset, 0));
227 }
228
229 stats.literal_data += i;
230 cleanup_got_literal = 1;
231
232 sum_update(data, i);
233
234 if (fd != -1 && write_file(fd,data,i) != i)
235 goto report_write_error;
236 offset += i;
237 continue;
238 }
239
240 i = -(i+1);
241 offset2 = i * (OFF_T)sum.blength;
242 len = sum.blength;
243 if (i == (int)sum.count-1 && sum.remainder != 0)
244 len = sum.remainder;
245
246 stats.matched_data += len;
247
248 if (DEBUG_GTE(DELTASUM, 3)) {
249 rprintf(FINFO,
250 "chunk[%d] of size %ld at %s offset=%s\n",
251 i, (long)len, big_num(offset2, 0), big_num(offset, 0));
252 }
253
254 if (mapbuf) {
255 map = map_ptr(mapbuf,offset2,len);
256
257 see_token(map, len);
258 sum_update(map, len);
259 }
260
261 if (updating_basis_or_equiv) {
262 if (offset == offset2 && fd != -1) {
263 OFF_T pos;
264 if (flush_write_file(fd) < 0)
265 goto report_write_error;
266 offset += len;
267 if ((pos = do_lseek(fd, len, SEEK_CUR)) != offset) {
268 rsyserr(FERROR_XFER, errno,
269 "lseek of %s returned %s, not %s",
270 full_fname(fname),
271 big_num(pos, 0), big_num(offset, 0));
272 exit_cleanup(RERR_FILEIO);
273 }
274 continue;
275 }
276 }
277 if (fd != -1 && map && write_file(fd, map, len) != (int)len)
278 goto report_write_error;
279 offset += len;
280 }
281
282 if (flush_write_file(fd) < 0)
283 goto report_write_error;
284
285#ifdef HAVE_FTRUNCATE
286 if (inplace && fd != -1)
287 ftruncate(fd, offset);
288#endif
289
290 if (INFO_GTE(PROGRESS, 1))
291 end_progress(total_size);
292
293 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
294 report_write_error:
295 rsyserr(FERROR_XFER, errno, "write failed on %s",
296 full_fname(fname));
297 exit_cleanup(RERR_FILEIO);
298 }
299
300 if (sum_end(file_sum1) != checksum_len)
301 overflow_exit("checksum_len"); /* Impossible... */
302
303 if (mapbuf)
304 unmap_file(mapbuf);
305
306 read_buf(f_in, sender_file_sum, checksum_len);
307 if (DEBUG_GTE(DELTASUM, 2))
308 rprintf(FINFO,"got file_sum\n");
309 if (fd != -1 && memcmp(file_sum1, sender_file_sum, checksum_len) != 0)
310 return 0;
311 return 1;
312}
313
314
315static void discard_receive_data(int f_in, OFF_T length)
316{
317 receive_data(f_in, NULL, -1, 0, NULL, -1, length);
318}
319
320static void handle_delayed_updates(char *local_name)
321{
322 char *fname, *partialptr;
323 int ndx;
324
325 for (ndx = -1; (ndx = bitbag_next_bit(delayed_bits, ndx)) >= 0; ) {
326 struct file_struct *file = cur_flist->files[ndx];
327 fname = local_name ? local_name : f_name(file, NULL);
328 if ((partialptr = partial_dir_fname(fname)) != NULL) {
329 if (make_backups > 0 && !make_backup(fname))
330 continue;
331 if (DEBUG_GTE(RECV, 1)) {
332 rprintf(FINFO, "renaming %s to %s\n",
333 partialptr, fname);
334 }
335 /* We don't use robust_rename() here because the
336 * partial-dir must be on the same drive. */
337 if (do_rename(partialptr, fname) < 0) {
338 rsyserr(FERROR_XFER, errno,
339 "rename failed for %s (from %s)",
340 full_fname(fname), partialptr);
341 } else {
342 if (remove_source_files
343 || (preserve_hard_links && F_IS_HLINKED(file)))
344 send_msg_int(MSG_SUCCESS, ndx);
345 handle_partial_dir(partialptr, PDIR_DELETE);
346 }
347 }
348 }
349}
350
351static int get_next_gen_ndx(int fd, int next_gen_ndx, int desired_ndx)
352{
353 while (next_gen_ndx < desired_ndx) {
354 if (next_gen_ndx >= 0) {
355 struct file_struct *file = cur_flist->files[next_gen_ndx];
356 rprintf(FERROR_XFER,
357 "(No batched update for%s \"%s\")\n",
358 file->flags & FLAG_FILE_SENT ? " resend of" : "",
359 f_name(file, NULL));
360 }
361 next_gen_ndx = read_int(fd);
362 if (next_gen_ndx == -1) {
363 if (inc_recurse)
364 next_gen_ndx = first_flist->prev->used + first_flist->prev->ndx_start;
365 else
366 next_gen_ndx = cur_flist->used;
367 }
368 }
369 return next_gen_ndx;
370}
371
372/**
373 * main routine for receiver process.
374 *
375 * Receiver process runs on the same host as the generator process. */
376int recv_files(int f_in, char *local_name)
377{
378 int next_gen_ndx = -1;
379 int fd1,fd2;
380 STRUCT_STAT st;
381 int iflags, xlen;
382 char *fname, fbuf[MAXPATHLEN];
383 char xname[MAXPATHLEN];
384 char fnametmp[MAXPATHLEN];
385 char *fnamecmp, *partialptr;
386 char fnamecmpbuf[MAXPATHLEN];
387 uchar fnamecmp_type;
388 struct file_struct *file;
389 struct stats initial_stats;
390 int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i;
391 enum logcode log_code = log_before_transfer ? FLOG : FINFO;
392 int max_phase = protocol_version >= 29 ? 2 : 1;
393 int dflt_perms = (ACCESSPERMS & ~orig_umask);
394#ifdef SUPPORT_ACLS
395 const char *parent_dirname = "";
396#endif
397 int ndx, recv_ok;
398
399 if (DEBUG_GTE(RECV, 1))
400 rprintf(FINFO, "recv_files(%d) starting\n", cur_flist->used);
401
402 if (delay_updates)
403 delayed_bits = bitbag_create(cur_flist->used + 1);
404
405 while (1) {
406 cleanup_disable();
407
408 /* This call also sets cur_flist. */
409 ndx = read_ndx_and_attrs(f_in, &iflags, &fnamecmp_type,
410 xname, &xlen);
411 if (ndx == NDX_DONE) {
412 if (!am_server && INFO_GTE(PROGRESS, 2) && cur_flist) {
413 set_current_file_index(NULL, 0);
414 end_progress(0);
415 }
416 if (inc_recurse && first_flist) {
417 flist_free(first_flist);
418 if (first_flist)
419 continue;
420 }
421 if (read_batch && cur_flist) {
422 int high = inc_recurse
423 ? first_flist->prev->used + first_flist->prev->ndx_start
424 : cur_flist->used;
425 get_next_gen_ndx(batch_gen_fd, next_gen_ndx, high);
426 next_gen_ndx = -1;
427 }
428 if (++phase > max_phase)
429 break;
430 if (DEBUG_GTE(RECV, 1))
431 rprintf(FINFO, "recv_files phase=%d\n", phase);
432 if (phase == 2 && delay_updates)
433 handle_delayed_updates(local_name);
434 send_msg(MSG_DONE, "", 0, 0);
435 continue;
436 }
437
438 if (ndx - cur_flist->ndx_start >= 0)
439 file = cur_flist->files[ndx - cur_flist->ndx_start];
440 else
441 file = dir_flist->files[cur_flist->parent_ndx];
442 fname = local_name ? local_name : f_name(file, fbuf);
443
444 if (DEBUG_GTE(RECV, 1))
445 rprintf(FINFO, "recv_files(%s)\n", fname);
446
447#ifdef SUPPORT_XATTRS
448 if (iflags & ITEM_REPORT_XATTR && !dry_run)
449 recv_xattr_request(file, f_in);
450#endif
451
452 if (!(iflags & ITEM_TRANSFER)) {
453 maybe_log_item(file, iflags, itemizing, xname);
454#ifdef SUPPORT_XATTRS
455 if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && !dry_run)
456 set_file_attrs(fname, file, NULL, fname, 0);
457#endif
458 continue;
459 }
460 if (phase == 2) {
461 rprintf(FERROR,
462 "got transfer request in phase 2 [%s]\n",
463 who_am_i());
464 exit_cleanup(RERR_PROTOCOL);
465 }
466
467 if (file->flags & FLAG_FILE_SENT) {
468 if (csum_length == SHORT_SUM_LENGTH) {
469 if (keep_partial && !partial_dir)
470 make_backups = -make_backups; /* prevents double backup */
471 if (append_mode)
472 sparse_files = -sparse_files;
473 append_mode = -append_mode;
474 csum_length = SUM_LENGTH;
475 redoing = 1;
476 }
477 } else {
478 if (csum_length != SHORT_SUM_LENGTH) {
479 if (keep_partial && !partial_dir)
480 make_backups = -make_backups;
481 if (append_mode)
482 sparse_files = -sparse_files;
483 append_mode = -append_mode;
484 csum_length = SHORT_SUM_LENGTH;
485 redoing = 0;
486 }
487 }
488
489 if (!am_server && INFO_GTE(PROGRESS, 1))
490 set_current_file_index(file, ndx);
491 stats.num_transferred_files++;
492 stats.total_transferred_size += F_LENGTH(file);
493
494 cleanup_got_literal = 0;
495
496 if (daemon_filter_list.head
497 && check_filter(&daemon_filter_list, FLOG, fname, 0) < 0) {
498 rprintf(FERROR, "attempt to hack rsync failed.\n");
499 exit_cleanup(RERR_PROTOCOL);
500 }
501
502 if (!do_xfers) { /* log the transfer */
503 log_item(FCLIENT, file, &stats, iflags, NULL);
504 if (read_batch)
505 discard_receive_data(f_in, F_LENGTH(file));
506 continue;
507 }
508 if (write_batch < 0) {
509 log_item(FCLIENT, file, &stats, iflags, NULL);
510 if (!am_server)
511 discard_receive_data(f_in, F_LENGTH(file));
512 continue;
513 }
514
515 if (read_batch) {
516 next_gen_ndx = get_next_gen_ndx(batch_gen_fd, next_gen_ndx, ndx);
517 if (ndx < next_gen_ndx) {
518 rprintf(FINFO,
519 "(Skipping batched update for \"%s\")\n",
520 fname);
521 discard_receive_data(f_in, F_LENGTH(file));
522 if (inc_recurse)
523 send_msg_int(MSG_NO_SEND, ndx);
524 continue;
525 }
526 next_gen_ndx = -1;
527 }
528
529 partialptr = partial_dir ? partial_dir_fname(fname) : fname;
530
531 if (protocol_version >= 29) {
532 switch (fnamecmp_type) {
533 case FNAMECMP_FNAME:
534 fnamecmp = fname;
535 break;
536 case FNAMECMP_PARTIAL_DIR:
537 fnamecmp = partialptr;
538 break;
539 case FNAMECMP_BACKUP:
540 fnamecmp = get_backup_name(fname);
541 break;
542 case FNAMECMP_FUZZY:
543 if (file->dirname) {
544 pathjoin(fnamecmpbuf, MAXPATHLEN,
545 file->dirname, xname);
546 fnamecmp = fnamecmpbuf;
547 } else
548 fnamecmp = xname;
549 break;
550 default:
551 if (fnamecmp_type >= basis_dir_cnt) {
552 rprintf(FERROR,
553 "invalid basis_dir index: %d.\n",
554 fnamecmp_type);
555 exit_cleanup(RERR_PROTOCOL);
556 }
557 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
558 basis_dir[fnamecmp_type], fname);
559 fnamecmp = fnamecmpbuf;
560 break;
561 }
562 if (!fnamecmp || (daemon_filter_list.head
563 && check_filter(&daemon_filter_list, FLOG, fname, 0) < 0)) {
564 fnamecmp = fname;
565 fnamecmp_type = FNAMECMP_FNAME;
566 }
567 } else {
568 /* Reminder: --inplace && --partial-dir are never
569 * enabled at the same time. */
570 if (inplace && make_backups > 0) {
571 if (!(fnamecmp = get_backup_name(fname)))
572 fnamecmp = fname;
573 else
574 fnamecmp_type = FNAMECMP_BACKUP;
575 } else if (partial_dir && partialptr)
576 fnamecmp = partialptr;
577 else
578 fnamecmp = fname;
579 }
580
581 initial_stats = stats;
582
583 /* open the file */
584 fd1 = do_open(fnamecmp, O_RDONLY, 0);
585
586 if (fd1 == -1 && protocol_version < 29) {
587 if (fnamecmp != fname) {
588 fnamecmp = fname;
589 fd1 = do_open(fnamecmp, O_RDONLY, 0);
590 }
591
592 if (fd1 == -1 && basis_dir[0]) {
593 /* pre-29 allowed only one alternate basis */
594 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
595 basis_dir[0], fname);
596 fnamecmp = fnamecmpbuf;
597 fd1 = do_open(fnamecmp, O_RDONLY, 0);
598 }
599 }
600
601 updating_basis_or_equiv = inplace
602 && (fnamecmp == fname || fnamecmp_type == FNAMECMP_BACKUP);
603
604 if (fd1 == -1) {
605 st.st_mode = 0;
606 st.st_size = 0;
607 } else if (do_fstat(fd1,&st) != 0) {
608 rsyserr(FERROR_XFER, errno, "fstat %s failed",
609 full_fname(fnamecmp));
610 discard_receive_data(f_in, F_LENGTH(file));
611 close(fd1);
612 if (inc_recurse)
613 send_msg_int(MSG_NO_SEND, ndx);
614 continue;
615 }
616
617 if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
618 /* this special handling for directories
619 * wouldn't be necessary if robust_rename()
620 * and the underlying robust_unlink could cope
621 * with directories
622 */
623 rprintf(FERROR_XFER, "recv_files: %s is a directory\n",
624 full_fname(fnamecmp));
625 discard_receive_data(f_in, F_LENGTH(file));
626 close(fd1);
627 if (inc_recurse)
628 send_msg_int(MSG_NO_SEND, ndx);
629 continue;
630 }
631
632 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
633 close(fd1);
634 fd1 = -1;
635 }
636
637 /* If we're not preserving permissions, change the file-list's
638 * mode based on the local permissions and some heuristics. */
639 if (!preserve_perms) {
640 int exists = fd1 != -1;
641#ifdef SUPPORT_ACLS
642 const char *dn = file->dirname ? file->dirname : ".";
643 if (parent_dirname != dn
644 && strcmp(parent_dirname, dn) != 0) {
645 dflt_perms = default_perms_for_dir(dn);
646 parent_dirname = dn;
647 }
648#endif
649 file->mode = dest_mode(file->mode, st.st_mode,
650 dflt_perms, exists);
651 }
652
653 /* We now check to see if we are writing the file "inplace" */
654 if (inplace) {
655 fd2 = do_open(fname, O_WRONLY|O_CREAT, 0600);
656 if (fd2 == -1) {
657 rsyserr(FERROR_XFER, errno, "open %s failed",
658 full_fname(fname));
659 }
660 } else {
661 fd2 = open_tmpfile(fnametmp, fname, file);
662 if (fd2 != -1)
663 cleanup_set(fnametmp, partialptr, file, fd1, fd2);
664 }
665
666 if (fd2 == -1) {
667 discard_receive_data(f_in, F_LENGTH(file));
668 if (fd1 != -1)
669 close(fd1);
670 if (inc_recurse)
671 send_msg_int(MSG_NO_SEND, ndx);
672 continue;
673 }
674
675 /* log the transfer */
676 if (log_before_transfer)
677 log_item(FCLIENT, file, &initial_stats, iflags, NULL);
678 else if (!am_server && INFO_GTE(NAME, 1) && INFO_EQ(PROGRESS, 1))
679 rprintf(FINFO, "%s\n", fname);
680
681 /* recv file data */
682 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
683 fname, fd2, F_LENGTH(file));
684
685 log_item(log_code, file, &initial_stats, iflags, NULL);
686
687 if (fd1 != -1)
688 close(fd1);
689 if (close(fd2) < 0) {
690 rsyserr(FERROR, errno, "close failed on %s",
691 full_fname(fnametmp));
692 exit_cleanup(RERR_FILEIO);
693 }
694
695 if ((recv_ok && (!delay_updates || !partialptr)) || inplace) {
696 if (partialptr == fname)
697 partialptr = NULL;
698 if (!finish_transfer(fname, fnametmp, fnamecmp,
699 partialptr, file, recv_ok, 1))
700 recv_ok = -1;
701 else if (fnamecmp == partialptr) {
702 do_unlink(partialptr);
703 handle_partial_dir(partialptr, PDIR_DELETE);
704 }
705 } else if (keep_partial && partialptr) {
706 if (!handle_partial_dir(partialptr, PDIR_CREATE)) {
707 rprintf(FERROR,
708 "Unable to create partial-dir for %s -- discarding %s.\n",
709 local_name ? local_name : f_name(file, NULL),
710 recv_ok ? "completed file" : "partial file");
711 do_unlink(fnametmp);
712 recv_ok = -1;
713 } else if (!finish_transfer(partialptr, fnametmp, fnamecmp, NULL,
714 file, recv_ok, !partial_dir))
715 recv_ok = -1;
716 else if (delay_updates && recv_ok) {
717 bitbag_set_bit(delayed_bits, ndx);
718 recv_ok = 2;
719 } else
720 partialptr = NULL;
721 } else
722 do_unlink(fnametmp);
723
724 cleanup_disable();
725
726 switch (recv_ok) {
727 case 2:
728 break;
729 case 1:
730 if (remove_source_files || inc_recurse
731 || (preserve_hard_links && F_IS_HLINKED(file)))
732 send_msg_int(MSG_SUCCESS, ndx);
733 break;
734 case 0: {
735 enum logcode msgtype = redoing ? FERROR_XFER : FWARNING;
736 if (msgtype == FERROR_XFER || INFO_GTE(NAME, 1)) {
737 char *errstr, *redostr, *keptstr;
738 if (!(keep_partial && partialptr) && !inplace)
739 keptstr = "discarded";
740 else if (partial_dir)
741 keptstr = "put into partial-dir";
742 else
743 keptstr = "retained";
744 if (msgtype == FERROR_XFER) {
745 errstr = "ERROR";
746 redostr = "";
747 } else {
748 errstr = "WARNING";
749 redostr = read_batch ? " (may try again)"
750 : " (will try again)";
751 }
752 rprintf(msgtype,
753 "%s: %s failed verification -- update %s%s.\n",
754 errstr, local_name ? f_name(file, NULL) : fname,
755 keptstr, redostr);
756 }
757 if (!redoing) {
758 send_msg_int(MSG_REDO, ndx);
759 file->flags |= FLAG_FILE_SENT;
760 } else if (inc_recurse)
761 send_msg_int(MSG_NO_SEND, ndx);
762 break;
763 }
764 case -1:
765 if (inc_recurse)
766 send_msg_int(MSG_NO_SEND, ndx);
767 break;
768 }
769 }
770 if (make_backups < 0)
771 make_backups = -make_backups;
772
773 if (phase == 2 && delay_updates) /* for protocol_version < 29 */
774 handle_delayed_updates(local_name);
775
776 if (DEBUG_GTE(RECV, 1))
777 rprintf(FINFO,"recv_files finished\n");
778
779 return 0;
780}