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