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