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, 2007 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 inc_recurse;
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_hard_links;
40 extern int preserve_perms;
41 extern int basis_dir_cnt;
42 extern int make_backups;
43 extern int cleanup_got_literal;
44 extern int remove_source_files;
45 extern int append_mode;
46 extern int sparse_files;
47 extern int keep_partial;
48 extern int checksum_seed;
50 extern int delay_updates;
51 extern struct stats stats;
53 extern char *partial_dir;
54 extern char *basis_dir[];
55 extern struct file_list *cur_flist, *first_flist;
56 extern struct filter_list_struct server_filter_list;
58 static struct bitbag *delayed_bits = NULL;
59 static int phase = 0, redoing = 0;
60 /* We're either updating the basis file or an identical copy: */
61 static int updating_basis;
64 * get_tmpname() - create a tmp filename for a given filename
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!
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
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.
84 int get_tmpname(char *fnametmp, char *fname)
86 int maxname, added, length = 0;
90 /* Note: this can't overflow, so the return value is safe */
91 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
92 fnametmp[length++] = '/';
95 if ((f = strrchr(fname, '/')) != NULL) {
99 /* copy up to and including the slash */
100 strlcpy(fnametmp, fname, length + 1);
104 fnametmp[length++] = '.';
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);
111 rprintf(FERROR, "temporary filename too long: %s\n", fname);
116 added = strlcpy(fnametmp + length, f, maxname);
117 if (added >= maxname)
119 memcpy(fnametmp + length + added, ".XXXXXX", 8);
125 static 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)
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;
139 read_sum_head(f_in, &sum);
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);
145 rprintf(FINFO, "recv mapped %s of size %.0f\n",
146 fname_r, (double)size_r);
151 sum_init(checksum_seed);
153 if (append_mode > 0) {
155 sum.flength = (OFF_T)sum.count * sum.blength;
157 sum.flength -= sum.blength - sum.remainder;
158 for (j = CHUNK_SIZE; j < sum.flength; j += CHUNK_SIZE) {
160 show_progress(offset, total_size);
161 sum_update(map_ptr(mapbuf, offset, CHUNK_SIZE),
165 if (offset < sum.flength) {
166 int32 len = (int32)(sum.flength - offset);
168 show_progress(offset, total_size);
169 sum_update(map_ptr(mapbuf, offset, len), len);
170 offset = sum.flength;
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);
179 while ((i = recv_token(f_in, &data)) != 0) {
181 show_progress(offset, total_size);
185 rprintf(FINFO,"data recv %d at %.0f\n",
189 stats.literal_data += i;
190 cleanup_got_literal = 1;
194 if (fd != -1 && write_file(fd,data,i) != i)
195 goto report_write_error;
201 offset2 = i * (OFF_T)sum.blength;
203 if (i == (int)sum.count-1 && sum.remainder != 0)
206 stats.matched_data += len;
210 "chunk[%d] of size %ld at %.0f offset=%.0f\n",
211 i, (long)len, (double)offset2, (double)offset);
215 map = map_ptr(mapbuf,offset2,len);
218 sum_update(map, len);
221 if (updating_basis) {
222 if (offset == offset2 && fd != -1) {
224 if (flush_write_file(fd) < 0)
225 goto report_write_error;
227 if ((pos = do_lseek(fd, len, SEEK_CUR)) != offset) {
228 rsyserr(FERROR, errno,
229 "lseek of %s returned %.0f, not %.0f",
231 (double)pos, (double)offset);
232 exit_cleanup(RERR_FILEIO);
237 if (fd != -1 && map && write_file(fd, map, len) != (int)len)
238 goto report_write_error;
242 if (flush_write_file(fd) < 0)
243 goto report_write_error;
245 #ifdef HAVE_FTRUNCATE
246 if (inplace && fd != -1)
247 ftruncate(fd, offset);
251 end_progress(total_size);
253 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
255 rsyserr(FERROR, errno, "write failed on %s",
257 exit_cleanup(RERR_FILEIO);
265 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
267 rprintf(FINFO,"got file_sum\n");
268 if (fd != -1 && memcmp(file_sum1, file_sum2, MD4_SUM_LENGTH) != 0)
274 static void discard_receive_data(int f_in, OFF_T length)
276 receive_data(f_in, NULL, -1, 0, NULL, -1, length);
279 static void handle_delayed_updates(char *local_name)
281 char *fname, *partialptr;
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))
291 rprintf(FINFO, "renaming %s to %s\n",
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);
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);
310 static int get_next_gen_ndx(int fd, int next_gen_ndx, int desired_ndx)
312 while (next_gen_ndx < desired_ndx) {
313 if (next_gen_ndx >= 0) {
315 "(No batched update for%s \"%s\")\n",
316 redoing ? " resend of" : "",
317 f_name(cur_flist->files[next_gen_ndx], NULL));
319 next_gen_ndx = read_int(fd);
320 if (next_gen_ndx == -1) {
322 next_gen_ndx = first_flist->prev->count + first_flist->prev->ndx_start;
324 next_gen_ndx = cur_flist->count;
331 * main routine for receiver process.
333 * Receiver process runs on the same host as the generator process. */
334 int recv_files(int f_in, char *local_name)
336 int next_gen_ndx = -1;
340 char *fname, fbuf[MAXPATHLEN];
341 char xname[MAXPATHLEN];
342 char fnametmp[MAXPATHLEN];
343 char *fnamecmp, *partialptr;
344 char fnamecmpbuf[MAXPATHLEN];
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;
354 rprintf(FINFO, "recv_files(%d) starting\n", cur_flist->count);
357 delayed_bits = bitbag_create(cur_flist->count + 1);
359 updating_basis = inplace;
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 (inc_recurse && first_flist) {
369 flist_free(first_flist);
373 if (read_batch && cur_flist) {
374 int high = inc_recurse
375 ? first_flist->prev->count + first_flist->prev->ndx_start
377 get_next_gen_ndx(batch_gen_fd, next_gen_ndx, high);
380 if (++phase > max_phase)
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);
390 file = cur_flist->files[ndx - cur_flist->ndx_start];
391 fname = local_name ? local_name : f_name(file, fbuf);
394 rprintf(FINFO, "recv_files(%s)\n", fname);
396 if (!(iflags & ITEM_TRANSFER)) {
397 maybe_log_item(file, iflags, itemizing, xname);
402 "got transfer request in phase 2 [%s]\n",
404 exit_cleanup(RERR_PROTOCOL);
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;
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;
427 stats.current_file_index = ndx;
428 stats.num_transferred_files++;
429 stats.total_transferred_size += F_LENGTH(file);
430 cleanup_got_literal = 0;
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);
438 if (!do_xfers) { /* log the transfer */
439 log_item(FCLIENT, file, &stats, iflags, NULL);
441 discard_receive_data(f_in, F_LENGTH(file));
444 if (write_batch < 0) {
445 log_item(FINFO, file, &stats, iflags, NULL);
447 discard_receive_data(f_in, F_LENGTH(file));
452 next_gen_ndx = get_next_gen_ndx(batch_gen_fd, next_gen_ndx, ndx);
453 if (ndx < next_gen_ndx) {
455 "(Skipping batched update for \"%s\")\n",
457 discard_receive_data(f_in, F_LENGTH(file));
463 partialptr = partial_dir ? partial_dir_fname(fname) : fname;
465 if (protocol_version >= 29) {
466 switch (fnamecmp_type) {
470 case FNAMECMP_PARTIAL_DIR:
471 fnamecmp = partialptr;
473 case FNAMECMP_BACKUP:
474 fnamecmp = get_backup_name(fname);
479 pathjoin(fnamecmpbuf, MAXPATHLEN,
480 file->dirname, xname);
481 fnamecmp = fnamecmpbuf;
487 if (fnamecmp_type >= basis_dir_cnt) {
489 "invalid basis_dir index: %d.\n",
491 exit_cleanup(RERR_PROTOCOL);
493 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
494 basis_dir[fnamecmp_type], fname);
495 fnamecmp = fnamecmpbuf;
498 if (!fnamecmp || (server_filter_list.head
499 && check_filter(&server_filter_list, fname, 0) < 0))
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)))
507 } else if (partial_dir && partialptr)
508 fnamecmp = partialptr;
513 initial_stats = stats;
516 fd1 = do_open(fnamecmp, O_RDONLY, 0);
518 if (fd1 == -1 && protocol_version < 29) {
519 if (fnamecmp != fname) {
521 fd1 = do_open(fnamecmp, O_RDONLY, 0);
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);
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));
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
550 rprintf(FERROR,"recv_files: %s is a directory\n",
551 full_fname(fnamecmp));
552 discard_receive_data(f_in, F_LENGTH(file));
557 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
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);
569 /* We now check to see if we are writing the file "inplace" */
571 fd2 = do_open(fname, O_WRONLY|O_CREAT, 0600);
573 rsyserr(FERROR, errno, "open %s failed",
575 discard_receive_data(f_in, F_LENGTH(file));
581 if (!get_tmpname(fnametmp,fname)) {
582 discard_receive_data(f_in, F_LENGTH(file));
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);
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);
606 rsyserr(FERROR, errno, "mkstemp %s failed",
607 full_fname(fnametmp));
608 discard_receive_data(f_in, F_LENGTH(file));
614 cleanup_set(fnametmp, partialptr, file, fd1, fd2);
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);
624 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
625 fname, fd2, F_LENGTH(file));
627 log_item(log_code, file, &initial_stats, iflags, NULL);
631 if (close(fd2) < 0) {
632 rsyserr(FERROR, errno, "close failed on %s",
633 full_fname(fnametmp));
634 exit_cleanup(RERR_FILEIO);
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;
644 temp_copy_name = partialptr;
645 finish_transfer(fname, fnametmp, temp_copy_name,
647 if (fnamecmp == partialptr) {
648 do_unlink(partialptr);
649 handle_partial_dir(partialptr, PDIR_DELETE);
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);
667 if (remove_source_files || inc_recurse
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";
679 keptstr = "retained";
680 if (msgtype == FERROR) {
685 redostr = " (will try again)";
688 "%s: %s failed verification -- update %s%s.\n",
689 errstr, fname, keptstr, redostr);
692 send_msg_int(MSG_REDO, ndx);
693 file->flags |= FLAG_FILE_SENT;
694 } else if (inc_recurse)
695 send_msg_int(MSG_NO_SEND, ndx);
698 if (make_backups < 0)
699 make_backups = -make_backups;
701 if (phase == 2 && delay_updates) /* for protocol_version < 29 */
702 handle_delayed_updates(local_name);
705 rprintf(FINFO,"recv_files finished\n");