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 log_before_transfer;
30 extern int log_format_has_i;
31 extern int logfile_format_has_i;
32 extern int csum_length;
33 extern int read_batch;
34 extern int write_batch;
35 extern int batch_gen_fd;
36 extern int protocol_version;
37 extern int relative_paths;
38 extern int preserve_hard_links;
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 remove_sent_files;
44 extern int append_mode;
45 extern int sparse_files;
46 extern int keep_partial;
47 extern int checksum_seed;
49 extern int delay_updates;
50 extern struct stats stats;
51 extern char *log_format;
53 extern char *partial_dir;
54 extern char *basis_dir[];
55 extern struct file_list *the_file_list;
56 extern struct filter_list_struct server_filter_list;
58 static struct bitbag *delayed_bits = NULL;
63 * get_tmpname() - create a tmp filename for a given filename
65 * If a tmpdir is defined, use that as the directory to
66 * put it in. Otherwise, the tmp filename is in the same
67 * directory as the given name. Note that there may be no
68 * directory at all in the given name!
70 * The tmp filename is basically the given filename with a
71 * dot prepended, and .XXXXXX appended (for mkstemp() to
72 * put its unique gunk in). Take care to not exceed
73 * either the MAXPATHLEN or NAME_MAX, esp. the last, as
74 * the basename basically becomes 8 chars longer. In that
75 * case, the original name is shortened sufficiently to
78 * Of course, there's no real reason for the tmp name to
79 * look like the original, except to satisfy us humans.
80 * As long as it's unique, rsync will work.
83 static int get_tmpname(char *fnametmp, char *fname)
90 /* Note: this can't overflow, so the return value is safe */
91 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
92 fnametmp[length++] = '/';
93 fnametmp[length] = '\0'; /* always NULL terminated */
96 if ((f = strrchr(fname, '/')) != NULL) {
100 /* copy up to and including the slash */
101 strlcpy(fnametmp, fname, length + 1);
105 fnametmp[length++] = '.';
106 fnametmp[length] = '\0'; /* always NULL terminated */
108 maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8);
111 rprintf(FERROR, "temporary filename too long: %s\n", fname);
116 strlcpy(fnametmp + length, f, maxname);
117 strcat(fnametmp + length, ".XXXXXX");
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);
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 = 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 && do_lseek(fd, offset, SEEK_SET) != offset) {
171 rsyserr(FERROR, errno, "lseek failed on %s",
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);
220 if (offset == offset2 && fd != -1) {
221 if (flush_write_file(fd) < 0)
222 goto report_write_error;
224 if (do_lseek(fd, len, SEEK_CUR) != offset) {
225 rsyserr(FERROR, errno,
226 "lseek failed on %s",
228 exit_cleanup(RERR_FILEIO);
233 if (fd != -1 && map && write_file(fd, map, len) != (int)len)
234 goto report_write_error;
238 if (flush_write_file(fd) < 0)
239 goto report_write_error;
241 #ifdef HAVE_FTRUNCATE
242 if (inplace && fd != -1)
243 ftruncate(fd, offset);
247 end_progress(total_size);
249 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
251 rsyserr(FERROR, errno, "write failed on %s",
253 exit_cleanup(RERR_FILEIO);
261 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
263 rprintf(FINFO,"got file_sum\n");
264 if (fd != -1 && memcmp(file_sum1, file_sum2, MD4_SUM_LENGTH) != 0)
270 static void discard_receive_data(int f_in, OFF_T length)
272 receive_data(f_in, NULL, -1, 0, NULL, -1, length);
275 static void handle_delayed_updates(struct file_list *flist, char *local_name)
277 char *fname, *partialptr, numbuf[4];
280 for (i = -1; (i = bitbag_next_bit(delayed_bits, i)) >= 0; ) {
281 struct file_struct *file = flist->files[i];
282 fname = local_name ? local_name : f_name(file, NULL);
283 if ((partialptr = partial_dir_fname(fname)) != NULL) {
284 if (make_backups && !make_backup(fname))
287 rprintf(FINFO, "renaming %s to %s\n",
290 /* We don't use robust_rename() here because the
291 * partial-dir must be on the same drive. */
292 if (do_rename(partialptr, fname) < 0) {
293 rsyserr(FERROR, errno,
294 "rename failed for %s (from %s)",
295 full_fname(fname), partialptr);
297 if (remove_sent_files
298 || (preserve_hard_links
299 && file->link_u.links)) {
301 send_msg(MSG_SUCCESS,numbuf,4);
303 handle_partial_dir(partialptr, PDIR_DELETE);
309 static int get_next_gen_i(int batch_gen_fd, int next_gen_i, int desired_i)
311 while (next_gen_i < desired_i) {
312 if (next_gen_i >= 0) {
314 "(No batched update for%s \"%s\")\n",
315 phase ? " resend of" : "",
316 f_name(the_file_list->files[next_gen_i], NULL));
318 next_gen_i = read_int(batch_gen_fd);
319 if (next_gen_i == -1)
320 next_gen_i = the_file_list->count;
327 * main routine for receiver process.
329 * Receiver process runs on the same host as the generator process. */
330 int recv_files(int f_in, struct file_list *flist, char *local_name)
336 char *fname, fbuf[MAXPATHLEN];
337 char xname[MAXPATHLEN];
338 char fnametmp[MAXPATHLEN];
339 char *fnamecmp, *partialptr, numbuf[4];
340 char fnamecmpbuf[MAXPATHLEN];
342 struct file_struct *file;
343 struct stats initial_stats;
344 int save_make_backups = make_backups;
345 int itemizing = am_server ? logfile_format_has_i : log_format_has_i;
346 int max_phase = protocol_version >= 29 ? 2 : 1;
350 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
352 if (flist->hlink_pool) {
353 pool_destroy(flist->hlink_pool);
354 flist->hlink_pool = NULL;
358 delayed_bits = bitbag_create(flist->count);
366 get_next_gen_i(batch_gen_fd, next_gen_i,
370 if (++phase > max_phase)
372 csum_length = SUM_LENGTH;
374 rprintf(FINFO, "recv_files phase=%d\n", phase);
375 if (phase == 2 && delay_updates)
376 handle_delayed_updates(flist, local_name);
377 send_msg(MSG_DONE, "", 0);
378 if (keep_partial && !partial_dir)
379 make_backups = 0; /* prevents double backup */
387 iflags = read_item_attrs(f_in, -1, i, &fnamecmp_type,
389 if (iflags == ITEM_IS_NEW) /* no-op packet */
392 file = flist->files[i];
393 fname = local_name ? local_name : f_name(file, fbuf);
396 rprintf(FINFO, "recv_files(%s)\n", fname);
398 if (!(iflags & ITEM_TRANSFER)) {
399 maybe_log_item(file, iflags, itemizing, xname);
404 "got transfer request in phase 2 [%s]\n",
406 exit_cleanup(RERR_PROTOCOL);
409 stats.current_file_index = i;
410 stats.num_transferred_files++;
411 stats.total_transferred_size += file->length;
412 cleanup_got_literal = 0;
414 if (server_filter_list.head
415 && check_filter(&server_filter_list, fname, 0) < 0) {
416 rprintf(FERROR, "attempt to hack rsync failed.\n");
417 exit_cleanup(RERR_PROTOCOL);
420 if (!do_xfers) { /* log the transfer */
421 if (!am_server && log_format)
422 log_item(file, &stats, iflags, NULL);
424 discard_receive_data(f_in, file->length);
427 if (write_batch < 0) {
428 log_item(file, &stats, iflags, NULL);
430 discard_receive_data(f_in, file->length);
435 next_gen_i = get_next_gen_i(batch_gen_fd, next_gen_i, i);
436 if (i < next_gen_i) {
438 "(Skipping batched update for \"%s\")\n",
440 discard_receive_data(f_in, file->length);
446 partialptr = partial_dir ? partial_dir_fname(fname) : fname;
448 if (protocol_version >= 29) {
449 switch (fnamecmp_type) {
453 case FNAMECMP_PARTIAL_DIR:
454 fnamecmp = partialptr;
456 case FNAMECMP_BACKUP:
457 fnamecmp = get_backup_name(fname);
461 pathjoin(fnamecmpbuf, MAXPATHLEN,
462 file->dirname, xname);
463 fnamecmp = fnamecmpbuf;
468 if (fnamecmp_type >= basis_dir_cnt) {
470 "invalid basis_dir index: %d.\n",
472 exit_cleanup(RERR_PROTOCOL);
474 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
475 basis_dir[fnamecmp_type], fname);
476 fnamecmp = fnamecmpbuf;
479 if (!fnamecmp || (server_filter_list.head
480 && check_filter(&server_filter_list, fname, 0) < 0))
483 /* Reminder: --inplace && --partial-dir are never
484 * enabled at the same time. */
485 if (inplace && make_backups) {
486 if (!(fnamecmp = get_backup_name(fname)))
488 } else if (partial_dir && partialptr)
489 fnamecmp = partialptr;
494 initial_stats = stats;
497 fd1 = do_open(fnamecmp, O_RDONLY, 0);
499 if (fd1 == -1 && protocol_version < 29) {
500 if (fnamecmp != fname) {
502 fd1 = do_open(fnamecmp, O_RDONLY, 0);
505 if (fd1 == -1 && basis_dir[0]) {
506 /* pre-29 allowed only one alternate basis */
507 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
508 basis_dir[0], fname);
509 fnamecmp = fnamecmpbuf;
510 fd1 = do_open(fnamecmp, O_RDONLY, 0);
514 if (fd1 != -1 && do_fstat(fd1,&st) != 0) {
515 rsyserr(FERROR, errno, "fstat %s failed",
516 full_fname(fnamecmp));
517 discard_receive_data(f_in, file->length);
522 if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
523 /* this special handling for directories
524 * wouldn't be necessary if robust_rename()
525 * and the underlying robust_unlink could cope
528 rprintf(FERROR,"recv_files: %s is a directory\n",
529 full_fname(fnamecmp));
530 discard_receive_data(f_in, file->length);
535 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
540 /* If we're not preserving permissions, change the file-list's
541 * mode based on the local permissions and some heuristics. */
542 if (!preserve_perms) {
543 int exists = fd1 != -1;
544 file->mode = dest_mode(file->mode, st.st_mode, exists);
547 /* We now check to see if we are writing file "inplace" */
549 fd2 = do_open(fname, O_WRONLY|O_CREAT, 0600);
551 rsyserr(FERROR, errno, "open %s failed",
553 discard_receive_data(f_in, file->length);
559 if (!get_tmpname(fnametmp,fname)) {
560 discard_receive_data(f_in, file->length);
566 /* we initially set the perms without the
567 * setuid/setgid bits to ensure that there is no race
568 * condition. They are then correctly updated after
569 * the lchown. Thanks to snabb@epipe.fi for pointing
570 * this out. We also set it initially without group
571 * access because of a similar race condition. */
572 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
574 /* in most cases parent directories will already exist
575 * because their information should have been previously
576 * transferred, but that may not be the case with -R */
577 if (fd2 == -1 && relative_paths && errno == ENOENT
578 && create_directory_path(fnametmp) == 0) {
579 /* Get back to name with XXXXXX in it. */
580 get_tmpname(fnametmp, fname);
581 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
584 rsyserr(FERROR, errno, "mkstemp %s failed",
585 full_fname(fnametmp));
586 discard_receive_data(f_in, file->length);
593 cleanup_set(fnametmp, partialptr, file, fd1, fd2);
596 /* log the transfer */
597 if (log_before_transfer)
598 log_item(file, &initial_stats, iflags, NULL);
599 else if (!am_server && verbose && do_progress)
600 rprintf(FINFO, "%s\n", fname);
603 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
604 fname, fd2, file->length);
606 if (!log_before_transfer)
607 log_item(file, &initial_stats, iflags, NULL);
611 if (close(fd2) < 0) {
612 rsyserr(FERROR, errno, "close failed on %s",
613 full_fname(fnametmp));
614 exit_cleanup(RERR_FILEIO);
617 if ((recv_ok && (!delay_updates || !partialptr)) || inplace) {
618 char *temp_copy_name;
619 if (partialptr == fname)
620 partialptr = temp_copy_name = NULL;
621 else if (*partial_dir == '/')
622 temp_copy_name = NULL;
624 temp_copy_name = partialptr;
625 finish_transfer(fname, fnametmp, temp_copy_name,
627 if (fnamecmp == partialptr) {
628 do_unlink(partialptr);
629 handle_partial_dir(partialptr, PDIR_DELETE);
631 } else if (keep_partial && partialptr
632 && handle_partial_dir(partialptr, PDIR_CREATE)) {
633 finish_transfer(partialptr, fnametmp, NULL,
634 file, recv_ok, !partial_dir);
635 if (delay_updates && recv_ok) {
636 bitbag_set_bit(delayed_bits, i);
647 if (remove_sent_files
648 || (preserve_hard_links && file->link_u.links)) {
650 send_msg(MSG_SUCCESS, numbuf, 4);
652 } else if (!recv_ok) {
653 int msgtype = phase || read_batch ? FERROR : FINFO;
654 if (msgtype == FERROR || verbose) {
655 char *errstr, *redostr, *keptstr;
656 if (!(keep_partial && partialptr) && !inplace)
657 keptstr = "discarded";
658 else if (partial_dir)
659 keptstr = "put into partial-dir";
661 keptstr = "retained";
662 if (msgtype == FERROR) {
667 redostr = " (will try again)";
670 "%s: %s failed verification -- update %s%s.\n",
671 errstr, fname, keptstr, redostr);
675 send_msg(MSG_REDO, numbuf, 4);
679 make_backups = save_make_backups;
681 if (phase == 2 && delay_updates) /* for protocol_version < 29 */
682 handle_delayed_updates(flist, local_name);
685 rprintf(FINFO,"recv_files finished\n");