1 /* -*- c-file-style: "linux" -*-
3 Copyright (C) 1996-2000 by Andrew Tridgell
4 Copyright (C) Paul Mackerras 1996
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.
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.
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.
27 extern int do_progress;
28 extern int log_before_transfer;
29 extern int log_format_has_i;
30 extern int daemon_log_format_has_i;
31 extern int csum_length;
32 extern int read_batch;
33 extern int write_batch;
34 extern int batch_gen_fd;
35 extern int protocol_version;
36 extern int relative_paths;
37 extern int keep_dirlinks;
38 extern int preserve_hard_links;
39 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_sent_files;
46 extern int ignore_errors;
47 extern int orig_umask;
48 extern int append_mode;
49 extern int sparse_files;
50 extern int keep_partial;
51 extern int checksum_seed;
53 extern int delay_updates;
54 extern struct stats stats;
55 extern char *log_format;
57 extern char *partial_dir;
58 extern char *basis_dir[];
59 extern struct file_list *the_file_list;
60 extern struct filter_list_struct server_filter_list;
62 static struct bitbag *delayed_bits = NULL;
67 * get_tmpname() - create a tmp filename for a given filename
69 * If a tmpdir is defined, use that as the directory to
70 * put it in. Otherwise, the tmp filename is in the same
71 * directory as the given name. Note that there may be no
72 * directory at all in the given name!
74 * The tmp filename is basically the given filename with a
75 * dot prepended, and .XXXXXX appended (for mkstemp() to
76 * put its unique gunk in). Take care to not exceed
77 * either the MAXPATHLEN or NAME_MAX, esp. the last, as
78 * the basename basically becomes 8 chars longer. In that
79 * case, the original name is shortened sufficiently to
82 * Of course, there's no real reason for the tmp name to
83 * look like the original, except to satisfy us humans.
84 * As long as it's unique, rsync will work.
87 static int get_tmpname(char *fnametmp, char *fname)
94 /* Note: this can't overflow, so the return value is safe */
95 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
96 fnametmp[length++] = '/';
97 fnametmp[length] = '\0'; /* always NULL terminated */
100 if ((f = strrchr(fname, '/')) != NULL) {
104 /* copy up to and including the slash */
105 strlcpy(fnametmp, fname, length + 1);
109 fnametmp[length++] = '.';
110 fnametmp[length] = '\0'; /* always NULL terminated */
112 maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8);
115 rprintf(FERROR, "temporary filename too long: %s\n", fname);
120 strlcpy(fnametmp + length, f, maxname);
121 strcat(fnametmp + length, ".XXXXXX");
127 static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
128 char *fname, int fd, OFF_T total_size)
130 static char file_sum1[MD4_SUM_LENGTH];
131 static char file_sum2[MD4_SUM_LENGTH];
132 struct map_struct *mapbuf;
133 struct sum_struct sum;
141 read_sum_head(f_in, &sum);
143 if (fd_r >= 0 && size_r > 0) {
144 int32 read_size = MAX(sum.blength * 2, 16*1024);
145 mapbuf = map_file(fd_r, size_r, read_size, sum.blength);
147 rprintf(FINFO, "recv mapped %s of size %.0f\n",
148 fname_r, (double)size_r);
153 sum_init(checksum_seed);
157 sum.flength = (OFF_T)sum.count * sum.blength;
159 sum.flength -= sum.blength - sum.remainder;
160 for (j = CHUNK_SIZE; j < sum.flength; j += CHUNK_SIZE) {
162 show_progress(offset, total_size);
163 sum_update(map_ptr(mapbuf, offset, CHUNK_SIZE),
167 if (offset < sum.flength) {
168 int32 len = sum.flength - offset;
170 show_progress(offset, total_size);
171 sum_update(map_ptr(mapbuf, offset, len), len);
172 offset = sum.flength;
174 if (fd != -1 && do_lseek(fd, offset, SEEK_SET) != offset) {
175 rsyserr(FERROR, errno, "lseek failed on %s",
177 exit_cleanup(RERR_FILEIO);
181 while ((i = recv_token(f_in, &data)) != 0) {
183 show_progress(offset, total_size);
187 rprintf(FINFO,"data recv %d at %.0f\n",
191 stats.literal_data += i;
192 cleanup_got_literal = 1;
196 if (fd != -1 && write_file(fd,data,i) != i)
197 goto report_write_error;
203 offset2 = i * (OFF_T)sum.blength;
205 if (i == (int)sum.count-1 && sum.remainder != 0)
208 stats.matched_data += len;
212 "chunk[%d] of size %ld at %.0f offset=%.0f\n",
213 i, (long)len, (double)offset2, (double)offset);
217 map = map_ptr(mapbuf,offset2,len);
220 sum_update(map, len);
224 if (offset == offset2 && fd != -1) {
225 if (flush_write_file(fd) < 0)
226 goto report_write_error;
228 if (do_lseek(fd, len, SEEK_CUR) != offset) {
229 rsyserr(FERROR, errno,
230 "lseek failed on %s",
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(struct file_list *flist, char *local_name)
281 char *fname, *partialptr, numbuf[4];
284 for (i = -1; (i = bitbag_next_bit(delayed_bits, i)) >= 0; ) {
285 struct file_struct *file = flist->files[i];
286 fname = local_name ? local_name : f_name(file, NULL);
287 if ((partialptr = partial_dir_fname(fname)) != NULL) {
288 if (make_backups && !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_sent_files
302 || (preserve_hard_links
303 && file->link_u.links)) {
305 send_msg(MSG_SUCCESS,numbuf,4);
307 handle_partial_dir(partialptr, PDIR_DELETE);
313 static int get_next_gen_i(int batch_gen_fd, int next_gen_i, int desired_i)
315 while (next_gen_i < desired_i) {
316 if (next_gen_i >= 0) {
318 "(No batched update for%s \"%s\")\n",
319 phase ? " resend of" : "",
320 f_name(the_file_list->files[next_gen_i], NULL));
322 next_gen_i = read_int(batch_gen_fd);
323 if (next_gen_i == -1)
324 next_gen_i = the_file_list->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, struct file_list *flist, char *local_name)
340 char *fname, fbuf[MAXPATHLEN];
341 char xname[MAXPATHLEN];
342 char fnametmp[MAXPATHLEN];
343 char *fnamecmp, *partialptr, numbuf[4];
344 char fnamecmpbuf[MAXPATHLEN];
346 struct file_struct *file;
347 struct stats initial_stats;
348 int save_make_backups = make_backups;
349 int itemizing = am_daemon ? daemon_log_format_has_i
350 : !am_server && log_format_has_i;
351 int max_phase = protocol_version >= 29 ? 2 : 1;
355 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
357 if (flist->hlink_pool) {
358 pool_destroy(flist->hlink_pool);
359 flist->hlink_pool = NULL;
363 delayed_bits = bitbag_create(flist->count);
371 get_next_gen_i(batch_gen_fd, next_gen_i,
375 if (++phase > max_phase)
377 csum_length = SUM_LENGTH;
379 rprintf(FINFO, "recv_files phase=%d\n", phase);
380 if (phase == 2 && delay_updates)
381 handle_delayed_updates(flist, local_name);
382 send_msg(MSG_DONE, "", 0);
383 if (keep_partial && !partial_dir)
384 make_backups = 0; /* prevents double backup */
392 iflags = read_item_attrs(f_in, -1, i, &fnamecmp_type,
394 if (iflags == ITEM_IS_NEW) /* no-op packet */
397 file = flist->files[i];
398 fname = local_name ? local_name : f_name(file, fbuf);
401 rprintf(FINFO, "recv_files(%s)\n", fname);
403 if (!(iflags & ITEM_TRANSFER)) {
404 maybe_log_item(file, iflags, itemizing, xname);
409 "got transfer request in phase 2 [%s]\n",
411 exit_cleanup(RERR_PROTOCOL);
414 stats.current_file_index = i;
415 stats.num_transferred_files++;
416 stats.total_transferred_size += file->length;
417 cleanup_got_literal = 0;
419 if (server_filter_list.head
420 && check_filter(&server_filter_list, fname, 0) < 0) {
421 rprintf(FERROR, "attempt to hack rsync failed.\n");
422 exit_cleanup(RERR_PROTOCOL);
425 if (!do_xfers) { /* log the transfer */
426 if (!am_server && log_format)
427 log_item(file, &stats, iflags, NULL);
429 discard_receive_data(f_in, file->length);
432 if (write_batch < 0) {
433 log_item(file, &stats, iflags, NULL);
435 discard_receive_data(f_in, file->length);
440 next_gen_i = get_next_gen_i(batch_gen_fd, next_gen_i, i);
441 if (i < next_gen_i) {
443 "(Skipping batched update for \"%s\")\n",
445 discard_receive_data(f_in, file->length);
451 partialptr = partial_dir ? partial_dir_fname(fname) : fname;
453 if (protocol_version >= 29) {
454 switch (fnamecmp_type) {
458 case FNAMECMP_PARTIAL_DIR:
459 fnamecmp = partialptr;
461 case FNAMECMP_BACKUP:
462 fnamecmp = get_backup_name(fname);
466 pathjoin(fnamecmpbuf, MAXPATHLEN,
467 file->dirname, xname);
468 fnamecmp = fnamecmpbuf;
473 if (fnamecmp_type >= basis_dir_cnt) {
475 "invalid basis_dir index: %d.\n",
477 exit_cleanup(RERR_PROTOCOL);
479 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
480 basis_dir[fnamecmp_type], fname);
481 fnamecmp = fnamecmpbuf;
484 if (!fnamecmp || (server_filter_list.head
485 && check_filter(&server_filter_list, fname, 0) < 0))
488 /* Reminder: --inplace && --partial-dir are never
489 * enabled at the same time. */
490 if (inplace && make_backups) {
491 if (!(fnamecmp = get_backup_name(fname)))
493 } else if (partial_dir && partialptr)
494 fnamecmp = partialptr;
499 initial_stats = stats;
502 fd1 = do_open(fnamecmp, O_RDONLY, 0);
504 if (fd1 == -1 && protocol_version < 29) {
505 if (fnamecmp != fname) {
507 fd1 = do_open(fnamecmp, O_RDONLY, 0);
510 if (fd1 == -1 && basis_dir[0]) {
511 /* pre-29 allowed only one alternate basis */
512 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
513 basis_dir[0], fname);
514 fnamecmp = fnamecmpbuf;
515 fd1 = do_open(fnamecmp, O_RDONLY, 0);
519 if (fd1 != -1 && do_fstat(fd1,&st) != 0) {
520 rsyserr(FERROR, errno, "fstat %s failed",
521 full_fname(fnamecmp));
522 discard_receive_data(f_in, file->length);
527 if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
528 /* this special handling for directories
529 * wouldn't be necessary if robust_rename()
530 * and the underlying robust_unlink could cope
533 rprintf(FERROR,"recv_files: %s is a directory\n",
534 full_fname(fnamecmp));
535 discard_receive_data(f_in, file->length);
540 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
545 /* If we're not preserving permissions, change the file-list's
546 * mode based on the local permissions and some heuristics. */
547 if (!preserve_perms) {
548 int exists = fd1 != -1;
549 file->mode = dest_mode(file->mode, st.st_mode, exists);
552 /* We now check to see if we are writing file "inplace" */
554 fd2 = do_open(fname, O_WRONLY|O_CREAT, 0600);
556 rsyserr(FERROR, errno, "open %s failed",
558 discard_receive_data(f_in, file->length);
564 if (!get_tmpname(fnametmp,fname)) {
565 discard_receive_data(f_in, file->length);
571 /* we initially set the perms without the
572 * setuid/setgid bits to ensure that there is no race
573 * condition. They are then correctly updated after
574 * the lchown. Thanks to snabb@epipe.fi for pointing
575 * this out. We also set it initially without group
576 * access because of a similar race condition. */
577 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
579 /* in most cases parent directories will already exist
580 * because their information should have been previously
581 * transferred, but that may not be the case with -R */
582 if (fd2 == -1 && relative_paths && errno == ENOENT
583 && create_directory_path(fnametmp, orig_umask) == 0) {
584 /* Get back to name with XXXXXX in it. */
585 get_tmpname(fnametmp, fname);
586 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
589 rsyserr(FERROR, errno, "mkstemp %s failed",
590 full_fname(fnametmp));
591 discard_receive_data(f_in, file->length);
598 cleanup_set(fnametmp, partialptr, file, fd1, fd2);
601 /* log the transfer */
602 if (log_before_transfer)
603 log_item(file, &initial_stats, iflags, NULL);
604 else if (!am_server && verbose && do_progress)
605 rprintf(FINFO, "%s\n", fname);
608 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
609 fname, fd2, file->length);
611 if (!log_before_transfer)
612 log_item(file, &initial_stats, iflags, NULL);
616 if (close(fd2) < 0) {
617 rsyserr(FERROR, errno, "close failed on %s",
618 full_fname(fnametmp));
619 exit_cleanup(RERR_FILEIO);
622 if ((recv_ok && (!delay_updates || !partialptr)) || inplace) {
623 if (partialptr == fname || *partial_dir == '/')
625 finish_transfer(fname, fnametmp, partialptr,
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");