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