Added a warning message when a file fails to verify, letting the user
[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;
24extern int recurse;
25extern int delete_mode;
92b9eb97
WD
26extern int delete_after;
27extern int max_delete;
2f03f956
AT
28extern int csum_length;
29extern struct stats stats;
30extern int dry_run;
16cc9ca2
WD
31extern int read_batch;
32extern int batch_gen_fd;
2f03f956
AT
33extern int am_server;
34extern int relative_paths;
566fce32 35extern int keep_dirlinks;
2f03f956 36extern int preserve_hard_links;
92b9eb97 37extern int preserve_perms;
2f03f956
AT
38extern int cvs_exclude;
39extern int io_error;
40extern char *tmpdir;
375a4556 41extern char *compare_dest;
53dd3135 42extern int make_backups;
16417f8b 43extern int do_progress;
d74a2e3e 44extern char *backup_dir;
53dd3135 45extern char *backup_suffix;
d74a2e3e 46extern int backup_suffix_len;
925c517f 47extern int cleanup_got_literal;
92b9eb97
WD
48extern int module_id;
49extern int ignore_errors;
50extern int orig_umask;
55e50d89 51extern int keep_partial;
ba582f75 52extern int checksum_seed;
a3221d2a 53extern int inplace;
2f03f956 54
5ebab6c1
WD
55extern struct exclude_list_struct server_exclude_list;
56
57
d74a2e3e 58static void delete_one(char *fn, int is_dir)
2f03f956 59{
d74a2e3e
WD
60 if (!is_dir) {
61 if (robust_unlink(fn) != 0) {
d62bcc17
WD
62 rsyserr(FERROR, errno, "delete_one: unlink %s failed",
63 full_fname(fn));
d2a918b4 64 } else if (verbose)
d74a2e3e 65 rprintf(FINFO, "deleting %s\n", fn);
17fadf7d 66 } else {
d74a2e3e 67 if (do_rmdir(fn) != 0) {
eb84a83b
WD
68 if (errno == ENOTDIR && keep_dirlinks) {
69 delete_one(fn, 0);
70 return;
71 }
d74a2e3e 72 if (errno != ENOTEMPTY && errno != EEXIST) {
d62bcc17
WD
73 rsyserr(FERROR, errno,
74 "delete_one: rmdir %s failed",
75 full_fname(fn));
d74a2e3e 76 }
d2a918b4 77 } else if (verbose)
d74a2e3e 78 rprintf(FINFO, "deleting directory %s\n", fn);
2f03f956
AT
79 }
80}
81
82
d74a2e3e
WD
83static int is_backup_file(char *fn)
84{
85 int k = strlen(fn) - backup_suffix_len;
86 return k > 0 && strcmp(fn+k, backup_suffix) == 0;
87}
2f03f956
AT
88
89
1e82e2ce
WD
90/* This deletes any files on the receiving side that are not present
91 * on the sending side. */
6957ae33 92void delete_files(struct file_list *flist)
2f03f956
AT
93{
94 struct file_list *local_file_list;
95 int i, j;
a24639bb 96 char *argv[1], fbuf[MAXPATHLEN];
0b73ca12 97 static int deletion_count;
2f03f956
AT
98
99 if (cvs_exclude)
100 add_cvs_excludes();
101
ef55c686 102 if (io_error && !(lp_ignore_errors(module_id) || ignore_errors)) {
2f03f956
AT
103 rprintf(FINFO,"IO error encountered - skipping file deletion\n");
104 return;
105 }
106
1e82e2ce 107 for (j = 0; j < flist->count; j++) {
a5342644
WD
108 if (!(flist->files[j]->flags & FLAG_TOP_DIR)
109 || !S_ISDIR(flist->files[j]->mode))
110 continue;
2f03f956 111
a24639bb 112 argv[0] = f_name_to(flist->files[j], fbuf);
2f03f956 113
a24639bb 114 if (!(local_file_list = send_file_list(-1, 1, argv)))
2f03f956 115 continue;
2f03f956
AT
116
117 if (verbose > 1)
92b9eb97 118 rprintf(FINFO, "deleting in %s\n", fbuf);
2f03f956 119
446e239e 120 for (i = local_file_list->count-1; i >= 0; i--) {
92b9eb97
WD
121 if (max_delete && deletion_count > max_delete)
122 break;
123 if (!local_file_list->files[i]->basename)
124 continue;
125 if (flist_find(flist,local_file_list->files[i]) < 0) {
53dd3135 126 char *f = f_name(local_file_list->files[i]);
d74a2e3e 127 if (make_backups && (backup_dir || !is_backup_file(f))) {
d2a918b4 128 make_backup(f);
d74a2e3e
WD
129 if (verbose)
130 rprintf(FINFO, "deleting %s\n", f);
53dd3135 131 } else {
d74a2e3e
WD
132 int mode = local_file_list->files[i]->mode;
133 delete_one(f, S_ISDIR(mode) != 0);
53dd3135 134 }
31f3b68a 135 deletion_count++;
53dd3135 136 }
2f03f956
AT
137 }
138 flist_free(local_file_list);
2f03f956
AT
139 }
140}
141
142
52d3e106
S
143/*
144 * get_tmpname() - create a tmp filename for a given filename
145 *
146 * If a tmpdir is defined, use that as the directory to
147 * put it in. Otherwise, the tmp filename is in the same
148 * directory as the given name. Note that there may be no
149 * directory at all in the given name!
17fadf7d 150 *
52d3e106
S
151 * The tmp filename is basically the given filename with a
152 * dot prepended, and .XXXXXX appended (for mkstemp() to
153 * put its unique gunk in). Take care to not exceed
154 * either the MAXPATHLEN or NAME_MAX, esp. the last, as
155 * the basename basically becomes 8 chars longer. In that
156 * case, the original name is shortened sufficiently to
157 * make it all fit.
17fadf7d 158 *
52d3e106
S
159 * Of course, there's no real reason for the tmp name to
160 * look like the original, except to satisfy us humans.
161 * As long as it's unique, rsync will work.
162 */
163
2f03f956
AT
164static int get_tmpname(char *fnametmp, char *fname)
165{
166 char *f;
52d3e106
S
167 int length = 0;
168 int maxname;
2f03f956 169
2f03f956 170 if (tmpdir) {
a24639bb 171 /* Note: this can't overflow, so the return value is safe */
b7cee949 172 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
52d3e106
S
173 fnametmp[length++] = '/';
174 fnametmp[length] = '\0'; /* always NULL terminated */
0c2ef5f4 175 }
52d3e106 176
0c2ef5f4 177 if ((f = strrchr(fname, '/')) != NULL) {
52d3e106
S
178 ++f;
179 if (!tmpdir) {
180 length = f - fname;
0c2ef5f4 181 /* copy up to and including the slash */
52d3e106 182 strlcpy(fnametmp, fname, length + 1);
0c2ef5f4 183 }
446e239e 184 } else
52d3e106 185 f = fname;
52d3e106
S
186 fnametmp[length++] = '.';
187 fnametmp[length] = '\0'; /* always NULL terminated */
2f03f956 188
52d3e106 189 maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8);
2f03f956 190
0c2ef5f4 191 if (maxname < 1) {
52d3e106
S
192 rprintf(FERROR, "temporary filename too long: %s\n", fname);
193 fnametmp[0] = '\0';
2f03f956
AT
194 return 0;
195 }
196
17fadf7d 197 strlcpy(fnametmp + length, f, maxname);
52d3e106 198 strcat(fnametmp + length, ".XXXXXX");
2f03f956
AT
199
200 return 1;
201}
202
203
7e5fa372
WD
204static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
205 char *fname, int fd, OFF_T total_size)
2f03f956 206{
7e5fa372
WD
207 static char file_sum1[MD4_SUM_LENGTH];
208 static char file_sum2[MD4_SUM_LENGTH];
209 struct map_struct *mapbuf;
fc0257c9
S
210 struct sum_struct sum;
211 unsigned int len;
2f03f956
AT
212 OFF_T offset = 0;
213 OFF_T offset2;
214 char *data;
7e5fa372 215 int i;
e1f67417 216 char *map = NULL;
17fadf7d 217
fc0257c9 218 read_sum_head(f_in, &sum);
17fadf7d 219
7e5fa372
WD
220 if (fd_r >= 0 && size_r > 0) {
221 mapbuf = map_file(fd_r, size_r, sum.blength);
222 if (verbose > 2) {
223 rprintf(FINFO, "recv mapped %s of size %.0f\n",
224 fname_r, (double)size_r);
225 }
226 } else
227 mapbuf = NULL;
228
ba582f75 229 sum_init(checksum_seed);
17fadf7d 230
3f55bd5d 231 while ((i = recv_token(f_in, &data)) != 0) {
16417f8b
WD
232 if (do_progress)
233 show_progress(offset, total_size);
2f03f956
AT
234
235 if (i > 0) {
2f03f956 236 if (verbose > 3) {
5f808dfb
AT
237 rprintf(FINFO,"data recv %d at %.0f\n",
238 i,(double)offset);
2f03f956
AT
239 }
240
241 stats.literal_data += i;
242 cleanup_got_literal = 1;
17fadf7d 243
2f03f956
AT
244 sum_update(data,i);
245
246 if (fd != -1 && write_file(fd,data,i) != i) {
d62bcc17
WD
247 rsyserr(FERROR, errno, "write failed on %s",
248 full_fname(fname));
65417579 249 exit_cleanup(RERR_FILEIO);
2f03f956
AT
250 }
251 offset += i;
252 continue;
17fadf7d 253 }
2f03f956
AT
254
255 i = -(i+1);
fc0257c9
S
256 offset2 = i*(OFF_T)sum.blength;
257 len = sum.blength;
d2a918b4 258 if (i == (int)sum.count-1 && sum.remainder != 0)
fc0257c9 259 len = sum.remainder;
17fadf7d 260
2f03f956 261 stats.matched_data += len;
17fadf7d 262
2f03f956 263 if (verbose > 3)
5f808dfb
AT
264 rprintf(FINFO,"chunk[%d] of size %d at %.0f offset=%.0f\n",
265 i,len,(double)offset2,(double)offset);
17fadf7d 266
446e239e
WD
267 if (mapbuf) {
268 map = map_ptr(mapbuf,offset2,len);
17fadf7d 269
c55f7021
AT
270 see_token(map, len);
271 sum_update(map,len);
272 }
17fadf7d 273
a3221d2a
WD
274 if (!inplace || offset != offset2) {
275 if (fd != -1 && write_file(fd, map, len) != (int)len) {
276 rsyserr(FERROR, errno, "write failed on %s",
277 full_fname(fname));
278 exit_cleanup(RERR_FILEIO);
279 }
280 } else {
281 flush_write_file(fd);
282 if (do_lseek(fd,(OFF_T)len,SEEK_CUR) != offset+len) {
283 rprintf(FERROR, "lseek failed on %s: %s, %lli, %lli, %i\n",
7e5fa372
WD
284 full_fname(fname), strerror(errno),
285 do_lseek(fd, 0, SEEK_CUR),
286 offset + len, i);
a3221d2a
WD
287 exit_cleanup(RERR_FILEIO);
288 }
2f03f956
AT
289 }
290 offset += len;
291 }
292
76c21947
WD
293 flush_write_file(fd);
294
a3221d2a
WD
295#ifdef HAVE_FTRUNCATE
296 if (inplace)
297 ftruncate(fd, offset);
298#endif
299
16417f8b
WD
300 if (do_progress)
301 end_progress(total_size);
2f03f956
AT
302
303 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
d62bcc17
WD
304 rsyserr(FERROR, errno, "write failed on %s",
305 full_fname(fname));
65417579 306 exit_cleanup(RERR_FILEIO);
2f03f956
AT
307 }
308
309 sum_end(file_sum1);
310
7e5fa372
WD
311 if (mapbuf)
312 unmap_file(mapbuf);
313
bc63ae3f 314 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
d2a918b4 315 if (verbose > 2)
bc63ae3f 316 rprintf(FINFO,"got file_sum\n");
d2a918b4 317 if (fd != -1 && memcmp(file_sum1, file_sum2, MD4_SUM_LENGTH) != 0)
bc63ae3f 318 return 0;
2f03f956
AT
319 return 1;
320}
321
322
8ed9d849
WD
323static void discard_receive_data(int f_in, OFF_T length)
324{
7e5fa372 325 receive_data(f_in, NULL, -1, 0, NULL, -1, length);
8ed9d849
WD
326}
327
328
b35d0d8e
MP
329/**
330 * main routine for receiver process.
331 *
332 * Receiver process runs on the same host as the generator process. */
d3979b02 333int recv_files(int f_in, struct file_list *flist, char *local_name)
17fadf7d 334{
16cc9ca2 335 int next_gen_i = -1;
2f03f956
AT
336 int fd1,fd2;
337 STRUCT_STAT st;
446e239e 338 char *fname, fbuf[MAXPATHLEN];
f62c17e3 339 char template[MAXPATHLEN];
2f03f956 340 char fnametmp[MAXPATHLEN];
375a4556
DD
341 char *fnamecmp;
342 char fnamecmpbuf[MAXPATHLEN];
2f03f956 343 struct file_struct *file;
1b7c47cb 344 struct stats initial_stats;
4e834af1
WD
345 int save_make_backups = make_backups;
346 int i, recv_ok, phase = 0;
11a5a3c7 347
d2a918b4 348 if (verbose > 2)
2f03f956 349 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
2f03f956 350
cb869c26 351 if (flist->hlink_pool) {
9935066b
S
352 pool_destroy(flist->hlink_pool);
353 flist->hlink_pool = NULL;
354 }
355
17fadf7d 356 while (1) {
2f03f956
AT
357 cleanup_disable();
358
359 i = read_int(f_in);
360 if (i == -1) {
16cc9ca2
WD
361 if (read_batch) {
362 if (next_gen_i != flist->count)
363 while (read_int(batch_gen_fd) != -1) {}
364 next_gen_i = -1;
365 }
366
4e834af1
WD
367 if (phase)
368 break;
5ebab6c1 369
4e834af1
WD
370 phase = 1;
371 csum_length = SUM_LENGTH;
372 if (verbose > 2)
373 rprintf(FINFO, "recv_files phase=%d\n", phase);
374 send_msg(MSG_DONE, "", 0);
375 if (keep_partial)
376 make_backups = 0; /* prevents double backup */
377 continue;
2f03f956
AT
378 }
379
380 if (i < 0 || i >= flist->count) {
17fadf7d 381 rprintf(FERROR,"Invalid file index %d in recv_files (count=%d)\n",
2f03f956 382 i, flist->count);
65417579 383 exit_cleanup(RERR_PROTOCOL);
2f03f956
AT
384 }
385
386 file = flist->files[i];
2f03f956 387
97feb557 388 stats.current_file_index = i;
2f03f956
AT
389 stats.num_transferred_files++;
390 stats.total_transferred_size += file->length;
925c517f 391 cleanup_got_literal = 0;
2f03f956
AT
392
393 if (local_name)
394 fname = local_name;
446e239e 395 else
3fef5364 396 fname = f_name_to(file, fbuf);
2f03f956
AT
397
398 if (dry_run) {
d2a918b4 399 if (!am_server && verbose)
42d4edc0 400 rprintf(FINFO, "%s\n", fname);
2f03f956
AT
401 continue;
402 }
403
1b7c47cb
AT
404 initial_stats = stats;
405
2f03f956
AT
406 if (verbose > 2)
407 rprintf(FINFO,"recv_files(%s)\n",fname);
408
375a4556
DD
409 fnamecmp = fname;
410
16cc9ca2
WD
411 if (read_batch) {
412 while (i > next_gen_i) {
413 next_gen_i = read_int(batch_gen_fd);
414 if (next_gen_i == -1)
415 next_gen_i = flist->count;
416 }
417 if (i < next_gen_i) {
418 rprintf(FINFO, "skipping update for \"%s\"\n",
419 fname);
420 discard_receive_data(f_in, file->length);
421 continue;
422 }
423 }
424
5ebab6c1
WD
425 if (server_exclude_list.head
426 && check_exclude(&server_exclude_list, fname,
427 S_ISDIR(file->mode)) < 0) {
428 if (verbose) {
429 rprintf(FINFO,
430 "skipping server-excluded update for \"%s\"\n",
431 fname);
432 }
8ed9d849 433 discard_receive_data(f_in, file->length);
5ebab6c1
WD
434 continue;
435 }
436
17fadf7d 437 /* open the file */
8c9fd200 438 fd1 = do_open(fnamecmp, O_RDONLY, 0);
375a4556 439
c338460d 440 if (fd1 == -1 && compare_dest != NULL) {
375a4556 441 /* try the file at compare_dest instead */
f91e01d9
WD
442 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
443 compare_dest, fname);
375a4556 444 fnamecmp = fnamecmpbuf;
8c9fd200 445 fd1 = do_open(fnamecmp, O_RDONLY, 0);
375a4556 446 }
2f03f956
AT
447
448 if (fd1 != -1 && do_fstat(fd1,&st) != 0) {
d62bcc17
WD
449 rsyserr(FERROR, errno, "fstat %s failed",
450 full_fname(fnamecmp));
8ed9d849 451 discard_receive_data(f_in, file->length);
2f03f956
AT
452 close(fd1);
453 continue;
454 }
455
350e4e4d
S
456 if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
457 /* this special handling for directories
458 * wouldn't be necessary if robust_rename()
459 * and the underlying robust_unlink could cope
460 * with directories
461 */
ea42541f
WD
462 rprintf(FERROR,"recv_files: %s is a directory\n",
463 full_fname(fnamecmp));
8ed9d849 464 discard_receive_data(f_in, file->length);
2f03f956
AT
465 close(fd1);
466 continue;
467 }
468
350e4e4d
S
469 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
470 close(fd1);
471 fd1 = -1;
350e4e4d
S
472 }
473
4df9f368 474 if (fd1 != -1 && !preserve_perms) {
85ed0aa3 475 /* if the file exists already and we aren't preserving
17fadf7d
WD
476 * permissions then act as though the remote end sent
477 * us the file permissions we already have */
4df9f368
AT
478 file->mode = st.st_mode;
479 }
480
a3221d2a
WD
481 /* We now check to see if we are writing file "inplace" */
482 if (inplace) {
483 fd2 = do_open(fnamecmp, O_WRONLY|O_CREAT, 0);
484 if (fd2 == -1) {
485 rsyserr(FERROR, errno, "open %s failed",
486 full_fname(fnamecmp));
8ed9d849 487 discard_receive_data(f_in, file->length);
a3221d2a
WD
488 if (fd1 != -1)
489 close(fd1);
490 continue;
491 }
492 } else {
493 if (!get_tmpname(fnametmp,fname)) {
8ed9d849 494 discard_receive_data(f_in, file->length);
a3221d2a
WD
495 if (fd1 != -1)
496 close(fd1);
497 continue;
498 }
499
500 strlcpy(template, fnametmp, sizeof template);
2f03f956 501
a3221d2a
WD
502 /* we initially set the perms without the
503 * setuid/setgid bits to ensure that there is no race
504 * condition. They are then correctly updated after
505 * the lchown. Thanks to snabb@epipe.fi for pointing
506 * this out. We also set it initially without group
507 * access because of a similar race condition. */
f62c17e3 508 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
17fadf7d 509
a3221d2a
WD
510 /* in most cases parent directories will already exist
511 * because their information should have been previously
512 * transferred, but that may not be the case with -R */
513 if (fd2 == -1 && relative_paths && errno == ENOENT
514 && create_directory_path(fnametmp, orig_umask) == 0) {
515 strlcpy(fnametmp, template, sizeof fnametmp);
516 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
517 }
518 if (fd2 == -1) {
519 rsyserr(FERROR, errno, "mkstemp %s failed",
520 full_fname(fnametmp));
8ed9d849 521 discard_receive_data(f_in, file->length);
a3221d2a
WD
522 if (fd1 != -1)
523 close(fd1);
524 continue;
525 }
526
7e5fa372 527 cleanup_set(fnametmp, fname, file, fd1, fd2);
a3221d2a 528 }
2f03f956 529
d2a918b4 530 if (!am_server && verbose)
42d4edc0 531 rprintf(FINFO, "%s\n", fname);
11a5a3c7 532
2f03f956 533 /* recv file data */
7e5fa372
WD
534 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
535 fname, fd2, file->length);
1b7c47cb
AT
536
537 log_recv(file, &initial_stats);
17fadf7d 538
d2a918b4 539 if (fd1 != -1)
2f03f956 540 close(fd1);
9f27cd8c 541 if (close(fd2) < 0) {
d62bcc17
WD
542 rsyserr(FERROR, errno, "close failed on %s",
543 full_fname(fnametmp));
9f27cd8c
WD
544 exit_cleanup(RERR_FILEIO);
545 }
17fadf7d 546
55e50d89
WD
547 if (recv_ok || keep_partial)
548 finish_transfer(fname, fnametmp, file, recv_ok);
549 else
550 do_unlink(fnametmp);
c6b81a98 551
2f03f956 552 cleanup_disable();
554e0a8d 553
2f03f956 554 if (!recv_ok) {
e2bc4126
WD
555 int msgtype;
556 char *redostr;
557 if (csum_length != SUM_LENGTH) {
0569a133 558 char buf[4];
0569a133
WD
559 SIVAL(buf, 0, i);
560 send_msg(MSG_REDO, buf, 4);
e2bc4126
WD
561 msgtype = read_batch ? FERROR : FINFO;
562 redostr = read_batch ? " Redo doubtful."
563 : " Redo pending.";
564 } else {
565 msgtype = FERROR;
566 redostr = "";
567 }
568 if (verbose || read_batch) {
569 rprintf(msgtype,
570 "%s: %s failed verification. Update %sed.%s\n",
571 msgtype == FERROR ? "ERROR" : "WARNING",
572 fname, keep_partial || inplace ?
573 "retain" : "discard", redostr);
2f03f956
AT
574 }
575 }
576 }
4e834af1 577 make_backups = save_make_backups;
2f03f956 578
e76ca145 579 if (delete_after && recurse && !local_name && flist->count > 0)
3f55bd5d 580 delete_files(flist);
57df171b 581
2f03f956
AT
582 if (verbose > 2)
583 rprintf(FINFO,"recv_files finished\n");
17fadf7d 584
2f03f956
AT
585 return 0;
586}