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