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 append_mode;
48 extern int sparse_files;
49 extern int keep_partial;
50 extern int checksum_seed;
52 extern int delay_updates;
53 extern struct stats stats;
54 extern char *log_format;
56 extern char *partial_dir;
57 extern char *basis_dir[];
58 extern struct file_list *the_file_list;
59 extern struct filter_list_struct server_filter_list;
61 static struct bitbag *delayed_bits = NULL;
66 * get_tmpname() - create a tmp filename for a given filename
68 * If a tmpdir is defined, use that as the directory to
69 * put it in. Otherwise, the tmp filename is in the same
70 * directory as the given name. Note that there may be no
71 * directory at all in the given name!
73 * The tmp filename is basically the given filename with a
74 * dot prepended, and .XXXXXX appended (for mkstemp() to
75 * put its unique gunk in). Take care to not exceed
76 * either the MAXPATHLEN or NAME_MAX, esp. the last, as
77 * the basename basically becomes 8 chars longer. In that
78 * case, the original name is shortened sufficiently to
81 * Of course, there's no real reason for the tmp name to
82 * look like the original, except to satisfy us humans.
83 * As long as it's unique, rsync will work.
86 static int get_tmpname(char *fnametmp, char *fname)
93 /* Note: this can't overflow, so the return value is safe */
94 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
95 fnametmp[length++] = '/';
96 fnametmp[length] = '\0'; /* always NULL terminated */
99 if ((f = strrchr(fname, '/')) != NULL) {
103 /* copy up to and including the slash */
104 strlcpy(fnametmp, fname, length + 1);
108 fnametmp[length++] = '.';
109 fnametmp[length] = '\0'; /* always NULL terminated */
111 maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8);
114 rprintf(FERROR, "temporary filename too long: %s\n", fname);
119 strlcpy(fnametmp + length, f, maxname);
120 strcat(fnametmp + length, ".XXXXXX");
126 static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
127 char *fname, int fd, OFF_T total_size)
129 static char file_sum1[MD4_SUM_LENGTH];
130 static char file_sum2[MD4_SUM_LENGTH];
131 struct map_struct *mapbuf;
132 struct sum_struct sum;
140 read_sum_head(f_in, &sum);
142 if (fd_r >= 0 && size_r > 0) {
143 int32 read_size = MAX(sum.blength * 2, 16*1024);
144 mapbuf = map_file(fd_r, size_r, read_size, sum.blength);
146 rprintf(FINFO, "recv mapped %s of size %.0f\n",
147 fname_r, (double)size_r);
152 sum_init(checksum_seed);
156 sum.flength = (OFF_T)sum.count * sum.blength;
158 sum.flength -= sum.blength - sum.remainder;
159 for (j = CHUNK_SIZE; j < sum.flength; j += CHUNK_SIZE) {
161 show_progress(offset, total_size);
162 sum_update(map_ptr(mapbuf, offset, CHUNK_SIZE),
166 if (offset < sum.flength) {
167 int32 len = sum.flength - offset;
169 show_progress(offset, total_size);
170 sum_update(map_ptr(mapbuf, offset, len), len);
171 offset = sum.flength;
173 if (fd != -1 && do_lseek(fd, offset, SEEK_SET) != offset) {
174 rsyserr(FERROR, errno, "lseek failed on %s",
176 exit_cleanup(RERR_FILEIO);
180 while ((i = recv_token(f_in, &data)) != 0) {
182 show_progress(offset, total_size);
186 rprintf(FINFO,"data recv %d at %.0f\n",
190 stats.literal_data += i;
191 cleanup_got_literal = 1;
195 if (fd != -1 && write_file(fd,data,i) != i)
196 goto report_write_error;
202 offset2 = i * (OFF_T)sum.blength;
204 if (i == (int)sum.count-1 && sum.remainder != 0)
207 stats.matched_data += len;
211 "chunk[%d] of size %ld at %.0f offset=%.0f\n",
212 i, (long)len, (double)offset2, (double)offset);
216 map = map_ptr(mapbuf,offset2,len);
219 sum_update(map, len);
223 if (offset == offset2 && fd != -1) {
224 if (flush_write_file(fd) < 0)
225 goto report_write_error;
227 if (do_lseek(fd, len, SEEK_CUR) != offset) {
228 rsyserr(FERROR, errno,
229 "lseek failed on %s",
231 exit_cleanup(RERR_FILEIO);
236 if (fd != -1 && map && write_file(fd, map, len) != (int)len)
237 goto report_write_error;
241 if (flush_write_file(fd) < 0)
242 goto report_write_error;
244 #ifdef HAVE_FTRUNCATE
245 if (inplace && fd != -1)
246 ftruncate(fd, offset);
250 end_progress(total_size);
252 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
254 rsyserr(FERROR, errno, "write failed on %s",
256 exit_cleanup(RERR_FILEIO);
264 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
266 rprintf(FINFO,"got file_sum\n");
267 if (fd != -1 && memcmp(file_sum1, file_sum2, MD4_SUM_LENGTH) != 0)
273 static void discard_receive_data(int f_in, OFF_T length)
275 receive_data(f_in, NULL, -1, 0, NULL, -1, length);
278 static void handle_delayed_updates(struct file_list *flist, char *local_name)
280 char *fname, *partialptr, numbuf[4];
283 for (i = -1; (i = bitbag_next_bit(delayed_bits, i)) >= 0; ) {
284 struct file_struct *file = flist->files[i];
285 fname = local_name ? local_name : f_name(file, NULL);
286 if ((partialptr = partial_dir_fname(fname)) != NULL) {
287 if (make_backups && !make_backup(fname))
290 rprintf(FINFO, "renaming %s to %s\n",
293 /* We don't use robust_rename() here because the
294 * partial-dir must be on the same drive. */
295 if (do_rename(partialptr, fname) < 0) {
296 rsyserr(FERROR, errno,
297 "rename failed for %s (from %s)",
298 full_fname(fname), partialptr);
300 if (remove_sent_files
301 || (preserve_hard_links
302 && file->link_u.links)) {
304 send_msg(MSG_SUCCESS,numbuf,4);
306 handle_partial_dir(partialptr, PDIR_DELETE);
312 static int get_next_gen_i(int batch_gen_fd, int next_gen_i, int desired_i)
314 while (next_gen_i < desired_i) {
315 if (next_gen_i >= 0) {
317 "(No batched update for%s \"%s\")\n",
318 phase ? " resend of" : "",
319 f_name(the_file_list->files[next_gen_i], NULL));
321 next_gen_i = read_int(batch_gen_fd);
322 if (next_gen_i == -1)
323 next_gen_i = the_file_list->count;
330 * main routine for receiver process.
332 * Receiver process runs on the same host as the generator process. */
333 int recv_files(int f_in, struct file_list *flist, char *local_name)
339 char *fname, fbuf[MAXPATHLEN];
340 char xname[MAXPATHLEN];
341 char fnametmp[MAXPATHLEN];
342 char *fnamecmp, *partialptr, numbuf[4];
343 char fnamecmpbuf[MAXPATHLEN];
345 struct file_struct *file;
346 struct stats initial_stats;
347 int save_make_backups = make_backups;
348 int itemizing = am_daemon ? daemon_log_format_has_i
349 : !am_server && log_format_has_i;
350 int max_phase = protocol_version >= 29 ? 2 : 1;
354 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
356 if (flist->hlink_pool) {
357 pool_destroy(flist->hlink_pool);
358 flist->hlink_pool = NULL;
362 delayed_bits = bitbag_create(flist->count);
370 get_next_gen_i(batch_gen_fd, next_gen_i,
374 if (++phase > max_phase)
376 csum_length = SUM_LENGTH;
378 rprintf(FINFO, "recv_files phase=%d\n", phase);
379 if (phase == 2 && delay_updates)
380 handle_delayed_updates(flist, local_name);
381 send_msg(MSG_DONE, "", 0);
382 if (keep_partial && !partial_dir)
383 make_backups = 0; /* prevents double backup */
391 iflags = read_item_attrs(f_in, -1, i, &fnamecmp_type,
393 if (iflags == ITEM_IS_NEW) /* no-op packet */
396 file = flist->files[i];
397 fname = local_name ? local_name : f_name(file, fbuf);
400 rprintf(FINFO, "recv_files(%s)\n", fname);
402 if (!(iflags & ITEM_TRANSFER)) {
403 maybe_log_item(file, iflags, itemizing, xname);
408 "got transfer request in phase 2 [%s]\n",
410 exit_cleanup(RERR_PROTOCOL);
413 stats.current_file_index = i;
414 stats.num_transferred_files++;
415 stats.total_transferred_size += file->length;
416 cleanup_got_literal = 0;
418 if (server_filter_list.head
419 && check_filter(&server_filter_list, fname, 0) < 0) {
420 rprintf(FERROR, "attempt to hack rsync failed.\n");
421 exit_cleanup(RERR_PROTOCOL);
424 if (!do_xfers) { /* log the transfer */
425 if (!am_server && log_format)
426 log_item(file, &stats, iflags, NULL);
428 discard_receive_data(f_in, file->length);
431 if (write_batch < 0) {
432 log_item(file, &stats, iflags, NULL);
434 discard_receive_data(f_in, file->length);
439 next_gen_i = get_next_gen_i(batch_gen_fd, next_gen_i, i);
440 if (i < next_gen_i) {
442 "(Skipping batched update for \"%s\")\n",
444 discard_receive_data(f_in, file->length);
450 partialptr = partial_dir ? partial_dir_fname(fname) : fname;
452 if (protocol_version >= 29) {
453 switch (fnamecmp_type) {
457 case FNAMECMP_PARTIAL_DIR:
458 fnamecmp = partialptr;
460 case FNAMECMP_BACKUP:
461 fnamecmp = get_backup_name(fname);
465 pathjoin(fnamecmpbuf, MAXPATHLEN,
466 file->dirname, xname);
467 fnamecmp = fnamecmpbuf;
472 if (fnamecmp_type >= basis_dir_cnt) {
474 "invalid basis_dir index: %d.\n",
476 exit_cleanup(RERR_PROTOCOL);
478 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
479 basis_dir[fnamecmp_type], fname);
480 fnamecmp = fnamecmpbuf;
483 if (!fnamecmp || (server_filter_list.head
484 && check_filter(&server_filter_list, fname, 0) < 0))
487 /* Reminder: --inplace && --partial-dir are never
488 * enabled at the same time. */
489 if (inplace && make_backups) {
490 if (!(fnamecmp = get_backup_name(fname)))
492 } else if (partial_dir && partialptr)
493 fnamecmp = partialptr;
498 initial_stats = stats;
501 fd1 = do_open(fnamecmp, O_RDONLY, 0);
503 if (fd1 == -1 && protocol_version < 29) {
504 if (fnamecmp != fname) {
506 fd1 = do_open(fnamecmp, O_RDONLY, 0);
509 if (fd1 == -1 && basis_dir[0]) {
510 /* pre-29 allowed only one alternate basis */
511 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
512 basis_dir[0], fname);
513 fnamecmp = fnamecmpbuf;
514 fd1 = do_open(fnamecmp, O_RDONLY, 0);
518 if (fd1 != -1 && do_fstat(fd1,&st) != 0) {
519 rsyserr(FERROR, errno, "fstat %s failed",
520 full_fname(fnamecmp));
521 discard_receive_data(f_in, file->length);
526 if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
527 /* this special handling for directories
528 * wouldn't be necessary if robust_rename()
529 * and the underlying robust_unlink could cope
532 rprintf(FERROR,"recv_files: %s is a directory\n",
533 full_fname(fnamecmp));
534 discard_receive_data(f_in, file->length);
539 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
544 /* If we're not preserving permissions, change the file-list's
545 * mode based on the local permissions and some heuristics. */
546 if (!preserve_perms) {
547 int exists = fd1 != -1;
548 file->mode = dest_mode(file->mode, st.st_mode, exists);
551 /* We now check to see if we are writing file "inplace" */
553 fd2 = do_open(fname, O_WRONLY|O_CREAT, 0600);
555 rsyserr(FERROR, errno, "open %s failed",
557 discard_receive_data(f_in, file->length);
563 if (!get_tmpname(fnametmp,fname)) {
564 discard_receive_data(f_in, file->length);
570 /* we initially set the perms without the
571 * setuid/setgid bits to ensure that there is no race
572 * condition. They are then correctly updated after
573 * the lchown. Thanks to snabb@epipe.fi for pointing
574 * this out. We also set it initially without group
575 * access because of a similar race condition. */
576 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
578 /* in most cases parent directories will already exist
579 * because their information should have been previously
580 * transferred, but that may not be the case with -R */
581 if (fd2 == -1 && relative_paths && errno == ENOENT
582 && create_directory_path(fnametmp) == 0) {
583 /* Get back to name with XXXXXX in it. */
584 get_tmpname(fnametmp, fname);
585 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
588 rsyserr(FERROR, errno, "mkstemp %s failed",
589 full_fname(fnametmp));
590 discard_receive_data(f_in, file->length);
597 cleanup_set(fnametmp, partialptr, file, fd1, fd2);
600 /* log the transfer */
601 if (log_before_transfer)
602 log_item(file, &initial_stats, iflags, NULL);
603 else if (!am_server && verbose && do_progress)
604 rprintf(FINFO, "%s\n", fname);
607 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
608 fname, fd2, file->length);
610 if (!log_before_transfer)
611 log_item(file, &initial_stats, iflags, NULL);
615 if (close(fd2) < 0) {
616 rsyserr(FERROR, errno, "close failed on %s",
617 full_fname(fnametmp));
618 exit_cleanup(RERR_FILEIO);
621 if ((recv_ok && (!delay_updates || !partialptr)) || inplace) {
622 if (partialptr == fname || *partial_dir == '/')
624 finish_transfer(fname, fnametmp, partialptr,
626 if (fnamecmp == partialptr) {
627 do_unlink(partialptr);
628 handle_partial_dir(partialptr, PDIR_DELETE);
630 } else if (keep_partial && partialptr
631 && handle_partial_dir(partialptr, PDIR_CREATE)) {
632 finish_transfer(partialptr, fnametmp, NULL,
633 file, recv_ok, !partial_dir);
634 if (delay_updates && recv_ok) {
635 bitbag_set_bit(delayed_bits, i);
646 if (remove_sent_files
647 || (preserve_hard_links && file->link_u.links)) {
649 send_msg(MSG_SUCCESS, numbuf, 4);
651 } else if (!recv_ok) {
652 int msgtype = phase || read_batch ? FERROR : FINFO;
653 if (msgtype == FERROR || verbose) {
654 char *errstr, *redostr, *keptstr;
655 if (!(keep_partial && partialptr) && !inplace)
656 keptstr = "discarded";
657 else if (partial_dir)
658 keptstr = "put into partial-dir";
660 keptstr = "retained";
661 if (msgtype == FERROR) {
666 redostr = " (will try again)";
669 "%s: %s failed verification -- update %s%s.\n",
670 errstr, fname, keptstr, redostr);
674 send_msg(MSG_REDO, numbuf, 4);
678 make_backups = save_make_backups;
680 if (phase == 2 && delay_updates) /* for protocol_version < 29 */
681 handle_delayed_updates(flist, local_name);
684 rprintf(FINFO,"recv_files finished\n");