2 * Routines only used by the receiving process.
4 * Copyright (C) 1996-2000 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2003, 2004, 2005, 2006 Wayne Davison
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.
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.
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.
28 extern int do_progress;
29 extern int incremental;
30 extern int log_before_transfer;
31 extern int stdout_format_has_i;
32 extern int logfile_format_has_i;
33 extern int csum_length;
34 extern int read_batch;
35 extern int write_batch;
36 extern int batch_gen_fd;
37 extern int protocol_version;
38 extern int relative_paths;
39 extern int preserve_perms;
40 extern int basis_dir_cnt;
41 extern int make_backups;
42 extern int cleanup_got_literal;
43 extern int append_mode;
44 extern int sparse_files;
45 extern int keep_partial;
46 extern int checksum_seed;
48 extern int delay_updates;
49 extern struct stats stats;
51 extern char *partial_dir;
52 extern char *basis_dir[];
53 extern struct file_list *cur_flist, *first_flist;
54 extern struct filter_list_struct server_filter_list;
56 static struct bitbag *delayed_bits = NULL;
57 static int phase = 0, redoing = 0;
58 /* We're either updating the basis file or an identical copy: */
59 static int updating_basis;
62 * get_tmpname() - create a tmp filename for a given filename
64 * If a tmpdir is defined, use that as the directory to
65 * put it in. Otherwise, the tmp filename is in the same
66 * directory as the given name. Note that there may be no
67 * directory at all in the given name!
69 * The tmp filename is basically the given filename with a
70 * dot prepended, and .XXXXXX appended (for mkstemp() to
71 * put its unique gunk in). Take care to not exceed
72 * either the MAXPATHLEN or NAME_MAX, esp. the last, as
73 * the basename basically becomes 8 chars longer. In that
74 * case, the original name is shortened sufficiently to
77 * Of course, there's no real reason for the tmp name to
78 * look like the original, except to satisfy us humans.
79 * As long as it's unique, rsync will work.
82 int get_tmpname(char *fnametmp, char *fname)
84 int maxname, added, length = 0;
88 /* Note: this can't overflow, so the return value is safe */
89 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
90 fnametmp[length++] = '/';
93 if ((f = strrchr(fname, '/')) != NULL) {
97 /* copy up to and including the slash */
98 strlcpy(fnametmp, fname, length + 1);
102 fnametmp[length++] = '.';
104 /* The maxname value is bufsize, and includes space for the '\0'.
105 * (Note that NAME_MAX get -8 for the leading '.' above.) */
106 maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8);
109 rprintf(FERROR, "temporary filename too long: %s\n", fname);
114 added = strlcpy(fnametmp + length, f, maxname);
115 if (added >= maxname)
117 memcpy(fnametmp + length + added, ".XXXXXX", 8);
123 static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
124 char *fname, int fd, OFF_T total_size)
126 static char file_sum1[MD4_SUM_LENGTH];
127 static char file_sum2[MD4_SUM_LENGTH];
128 struct map_struct *mapbuf;
129 struct sum_struct sum;
137 read_sum_head(f_in, &sum);
139 if (fd_r >= 0 && size_r > 0) {
140 int32 read_size = MAX(sum.blength * 2, 16*1024);
141 mapbuf = map_file(fd_r, size_r, read_size, sum.blength);
143 rprintf(FINFO, "recv mapped %s of size %.0f\n",
144 fname_r, (double)size_r);
149 sum_init(checksum_seed);
151 if (append_mode > 0) {
153 sum.flength = (OFF_T)sum.count * sum.blength;
155 sum.flength -= sum.blength - sum.remainder;
156 for (j = CHUNK_SIZE; j < sum.flength; j += CHUNK_SIZE) {
158 show_progress(offset, total_size);
159 sum_update(map_ptr(mapbuf, offset, CHUNK_SIZE),
163 if (offset < sum.flength) {
164 int32 len = (int32)(sum.flength - offset);
166 show_progress(offset, total_size);
167 sum_update(map_ptr(mapbuf, offset, len), len);
168 offset = sum.flength;
170 if (fd != -1 && (j = do_lseek(fd, offset, SEEK_SET)) != offset) {
171 rsyserr(FERROR, errno, "lseek of %s returned %.0f, not %.0f",
172 full_fname(fname), (double)j, (double)offset);
173 exit_cleanup(RERR_FILEIO);
177 while ((i = recv_token(f_in, &data)) != 0) {
179 show_progress(offset, total_size);
183 rprintf(FINFO,"data recv %d at %.0f\n",
187 stats.literal_data += i;
188 cleanup_got_literal = 1;
192 if (fd != -1 && write_file(fd,data,i) != i)
193 goto report_write_error;
199 offset2 = i * (OFF_T)sum.blength;
201 if (i == (int)sum.count-1 && sum.remainder != 0)
204 stats.matched_data += len;
208 "chunk[%d] of size %ld at %.0f offset=%.0f\n",
209 i, (long)len, (double)offset2, (double)offset);
213 map = map_ptr(mapbuf,offset2,len);
216 sum_update(map, len);
219 if (updating_basis) {
220 if (offset == offset2 && fd != -1) {
222 if (flush_write_file(fd) < 0)
223 goto report_write_error;
225 if ((pos = do_lseek(fd, len, SEEK_CUR)) != offset) {
226 rsyserr(FERROR, errno,
227 "lseek of %s returned %.0f, not %.0f",
229 (double)pos, (double)offset);
230 exit_cleanup(RERR_FILEIO);
235 if (fd != -1 && map && write_file(fd, map, len) != (int)len)
236 goto report_write_error;
240 if (flush_write_file(fd) < 0)
241 goto report_write_error;
243 #ifdef HAVE_FTRUNCATE
244 if (inplace && fd != -1)
245 ftruncate(fd, offset);
249 end_progress(total_size);
251 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
253 rsyserr(FERROR, errno, "write failed on %s",
255 exit_cleanup(RERR_FILEIO);
263 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
265 rprintf(FINFO,"got file_sum\n");
266 if (fd != -1 && memcmp(file_sum1, file_sum2, MD4_SUM_LENGTH) != 0)
272 static void discard_receive_data(int f_in, OFF_T length)
274 receive_data(f_in, NULL, -1, 0, NULL, -1, length);
277 static void handle_delayed_updates(char *local_name)
279 char *fname, *partialptr;
282 for (ndx = -1; (ndx = bitbag_next_bit(delayed_bits, ndx)) >= 0; ) {
283 struct file_struct *file = cur_flist->files[ndx];
284 fname = local_name ? local_name : f_name(file, NULL);
285 if ((partialptr = partial_dir_fname(fname)) != NULL) {
286 if (make_backups > 0 && !make_backup(fname))
289 rprintf(FINFO, "renaming %s to %s\n",
292 /* We don't use robust_rename() here because the
293 * partial-dir must be on the same drive. */
294 if (do_rename(partialptr, fname) < 0) {
295 rsyserr(FERROR, errno,
296 "rename failed for %s (from %s)",
297 full_fname(fname), partialptr);
299 if (remove_source_files
300 || (preserve_hard_links && F_IS_HLINKED(file)))
301 send_msg_int(MSG_SUCCESS, ndx);
302 handle_partial_dir(partialptr, PDIR_DELETE);
308 static int get_next_gen_ndx(int fd, int next_gen_ndx, int desired_ndx)
310 while (next_gen_ndx < desired_ndx) {
311 if (next_gen_ndx >= 0) {
313 "(No batched update for%s \"%s\")\n",
314 redoing ? " resend of" : "",
315 f_name(cur_flist->files[next_gen_ndx], NULL));
317 next_gen_ndx = read_int(fd);
318 if (next_gen_ndx == -1) {
320 next_gen_ndx = first_flist->prev->count + first_flist->prev->ndx_start;
322 next_gen_ndx = cur_flist->count;
329 * main routine for receiver process.
331 * Receiver process runs on the same host as the generator process. */
332 int recv_files(int f_in, char *local_name)
334 int next_gen_ndx = -1;
338 char *fname, fbuf[MAXPATHLEN];
339 char xname[MAXPATHLEN];
340 char fnametmp[MAXPATHLEN];
341 char *fnamecmp, *partialptr;
342 char fnamecmpbuf[MAXPATHLEN];
344 struct file_struct *file;
345 struct stats initial_stats;
346 int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i;
347 enum logcode log_code = log_before_transfer ? FLOG : FINFO;
348 int max_phase = protocol_version >= 29 ? 2 : 1;
352 rprintf(FINFO, "recv_files(%d) starting\n", cur_flist->count);
355 delayed_bits = bitbag_create(cur_flist->count + 1);
357 updating_basis = inplace;
362 /* This call also sets cur_flist. */
363 ndx = read_ndx_and_attrs(f_in, -1, &iflags,
364 &fnamecmp_type, xname, &xlen);
365 if (ndx == NDX_DONE) {
366 if (incremental && first_flist) {
367 flist_free(first_flist);
371 if (read_batch && cur_flist) {
372 int high = incremental
373 ? first_flist->prev->count + first_flist->prev->ndx_start
375 get_next_gen_ndx(batch_gen_fd, next_gen_ndx, high);
378 if (++phase > max_phase)
381 rprintf(FINFO, "recv_files phase=%d\n", phase);
382 if (phase == 2 && delay_updates)
383 handle_delayed_updates(local_name);
384 send_msg(MSG_DONE, "", 0);
388 file = cur_flist->files[ndx - cur_flist->ndx_start];
389 fname = local_name ? local_name : f_name(file, fbuf);
392 rprintf(FINFO, "recv_files(%s)\n", fname);
394 if (!(iflags & ITEM_TRANSFER)) {
395 maybe_log_item(file, iflags, itemizing, xname);
400 "got transfer request in phase 2 [%s]\n",
402 exit_cleanup(RERR_PROTOCOL);
405 if (file->flags & FLAG_FILE_SENT) {
406 if (csum_length == SHORT_SUM_LENGTH) {
407 if (keep_partial && !partial_dir)
408 make_backups = -make_backups; /* prevents double backup */
409 append_mode = -append_mode;
410 sparse_files = -sparse_files;
411 csum_length = SUM_LENGTH;
415 if (csum_length != SHORT_SUM_LENGTH) {
416 if (keep_partial && !partial_dir)
417 make_backups = -make_backups;
418 append_mode = -append_mode;
419 sparse_files = -sparse_files;
420 csum_length = SHORT_SUM_LENGTH;
425 stats.current_file_index = ndx;
426 stats.num_transferred_files++;
427 stats.total_transferred_size += F_LENGTH(file);
428 cleanup_got_literal = 0;
430 if (server_filter_list.head
431 && check_filter(&server_filter_list, fname, 0) < 0) {
432 rprintf(FERROR, "attempt to hack rsync failed.\n");
433 exit_cleanup(RERR_PROTOCOL);
436 if (!do_xfers) { /* log the transfer */
437 log_item(FCLIENT, file, &stats, iflags, NULL);
439 discard_receive_data(f_in, F_LENGTH(file));
442 if (write_batch < 0) {
443 log_item(FINFO, file, &stats, iflags, NULL);
445 discard_receive_data(f_in, F_LENGTH(file));
450 next_gen_ndx = get_next_gen_ndx(batch_gen_fd, next_gen_ndx, ndx);
451 if (ndx < next_gen_ndx) {
453 "(Skipping batched update for \"%s\")\n",
455 discard_receive_data(f_in, F_LENGTH(file));
461 partialptr = partial_dir ? partial_dir_fname(fname) : fname;
463 if (protocol_version >= 29) {
464 switch (fnamecmp_type) {
468 case FNAMECMP_PARTIAL_DIR:
469 fnamecmp = partialptr;
471 case FNAMECMP_BACKUP:
472 fnamecmp = get_backup_name(fname);
477 pathjoin(fnamecmpbuf, MAXPATHLEN,
478 file->dirname, xname);
479 fnamecmp = fnamecmpbuf;
485 if (fnamecmp_type >= basis_dir_cnt) {
487 "invalid basis_dir index: %d.\n",
489 exit_cleanup(RERR_PROTOCOL);
491 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
492 basis_dir[fnamecmp_type], fname);
493 fnamecmp = fnamecmpbuf;
496 if (!fnamecmp || (server_filter_list.head
497 && check_filter(&server_filter_list, fname, 0) < 0))
500 /* Reminder: --inplace && --partial-dir are never
501 * enabled at the same time. */
502 if (inplace && make_backups > 0) {
503 if (!(fnamecmp = get_backup_name(fname)))
505 } else if (partial_dir && partialptr)
506 fnamecmp = partialptr;
511 initial_stats = stats;
514 fd1 = do_open(fnamecmp, O_RDONLY, 0);
516 if (fd1 == -1 && protocol_version < 29) {
517 if (fnamecmp != fname) {
519 fd1 = do_open(fnamecmp, O_RDONLY, 0);
522 if (fd1 == -1 && basis_dir[0]) {
523 /* pre-29 allowed only one alternate basis */
524 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
525 basis_dir[0], fname);
526 fnamecmp = fnamecmpbuf;
527 fd1 = do_open(fnamecmp, O_RDONLY, 0);
534 } else if (do_fstat(fd1,&st) != 0) {
535 rsyserr(FERROR, errno, "fstat %s failed",
536 full_fname(fnamecmp));
537 discard_receive_data(f_in, F_LENGTH(file));
542 if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
543 /* this special handling for directories
544 * wouldn't be necessary if robust_rename()
545 * and the underlying robust_unlink could cope
548 rprintf(FERROR,"recv_files: %s is a directory\n",
549 full_fname(fnamecmp));
550 discard_receive_data(f_in, F_LENGTH(file));
555 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
560 /* If we're not preserving permissions, change the file-list's
561 * mode based on the local permissions and some heuristics. */
562 if (!preserve_perms) {
563 int exists = fd1 != -1;
564 file->mode = dest_mode(file->mode, st.st_mode, exists);
567 /* We now check to see if we are writing the file "inplace" */
569 fd2 = do_open(fname, O_WRONLY|O_CREAT, 0600);
571 rsyserr(FERROR, errno, "open %s failed",
573 discard_receive_data(f_in, F_LENGTH(file));
579 if (!get_tmpname(fnametmp,fname)) {
580 discard_receive_data(f_in, F_LENGTH(file));
586 /* we initially set the perms without the
587 * setuid/setgid bits to ensure that there is no race
588 * condition. They are then correctly updated after
589 * the lchown. Thanks to snabb@epipe.fi for pointing
590 * this out. We also set it initially without group
591 * access because of a similar race condition. */
592 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
594 /* in most cases parent directories will already exist
595 * because their information should have been previously
596 * transferred, but that may not be the case with -R */
597 if (fd2 == -1 && relative_paths && errno == ENOENT
598 && create_directory_path(fnametmp) == 0) {
599 /* Get back to name with XXXXXX in it. */
600 get_tmpname(fnametmp, fname);
601 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
604 rsyserr(FERROR, errno, "mkstemp %s failed",
605 full_fname(fnametmp));
606 discard_receive_data(f_in, F_LENGTH(file));
612 cleanup_set(fnametmp, partialptr, file, fd1, fd2);
615 /* log the transfer */
616 if (log_before_transfer)
617 log_item(FCLIENT, file, &initial_stats, iflags, NULL);
618 else if (!am_server && verbose && do_progress)
619 rprintf(FINFO, "%s\n", fname);
622 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
623 fname, fd2, F_LENGTH(file));
625 log_item(log_code, file, &initial_stats, iflags, NULL);
629 if (close(fd2) < 0) {
630 rsyserr(FERROR, errno, "close failed on %s",
631 full_fname(fnametmp));
632 exit_cleanup(RERR_FILEIO);
635 if ((recv_ok && (!delay_updates || !partialptr)) || inplace) {
636 char *temp_copy_name;
637 if (partialptr == fname)
638 partialptr = temp_copy_name = NULL;
639 else if (*partial_dir == '/')
640 temp_copy_name = NULL;
642 temp_copy_name = partialptr;
643 finish_transfer(fname, fnametmp, temp_copy_name,
645 if (fnamecmp == partialptr) {
646 do_unlink(partialptr);
647 handle_partial_dir(partialptr, PDIR_DELETE);
649 } else if (keep_partial && partialptr
650 && handle_partial_dir(partialptr, PDIR_CREATE)) {
651 finish_transfer(partialptr, fnametmp, NULL,
652 file, recv_ok, !partial_dir);
653 if (delay_updates && recv_ok) {
654 bitbag_set_bit(delayed_bits, ndx);
665 if (remove_source_files || incremental
666 || (preserve_hard_links && F_IS_HLINKED(file)))
667 send_msg_int(MSG_SUCCESS, ndx);
668 } else if (!recv_ok) {
669 enum logcode msgtype = redoing || read_batch ? FERROR : FINFO;
670 if (msgtype == FERROR || verbose) {
671 char *errstr, *redostr, *keptstr;
672 if (!(keep_partial && partialptr) && !inplace)
673 keptstr = "discarded";
674 else if (partial_dir)
675 keptstr = "put into partial-dir";
677 keptstr = "retained";
678 if (msgtype == FERROR) {
683 redostr = " (will try again)";
686 "%s: %s failed verification -- update %s%s.\n",
687 errstr, fname, keptstr, redostr);
690 send_msg_int(MSG_REDO, ndx);
691 file->flags |= FLAG_FILE_SENT;
692 } else if (incremental)
693 send_msg_int(MSG_NO_SEND, ndx);
696 if (make_backups < 0)
697 make_backups = -make_backups;
699 if (phase == 2 && delay_updates) /* for protocol_version < 29 */
700 handle_delayed_updates(local_name);
703 rprintf(FINFO,"recv_files finished\n");