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