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 delete_after;
32 extern int csum_length;
33 extern int read_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 keep_partial;
49 extern int checksum_seed;
51 extern int delay_updates;
52 extern struct stats stats;
53 extern char *log_format;
55 extern char *partial_dir;
56 extern char *basis_dir[];
58 extern struct filter_list_struct server_filter_list;
61 /* This deletes any files on the receiving side that are not present on the
62 * sending side. This is used by --delete-before and --delete-after. */
63 void delete_files(struct file_list *flist)
65 char fbuf[MAXPATHLEN];
68 for (j = 0; j < flist->count; j++) {
69 struct file_struct *file = flist->files[j];
71 if (!(file->flags & FLAG_DEL_HERE))
74 f_name_to(file, fbuf);
75 if (verbose > 1 && file->flags & FLAG_TOP_DIR)
76 rprintf(FINFO, "deleting in %s\n", safe_fname(fbuf));
78 delete_in_dir(flist, fbuf, file);
82 #define SLOT_SIZE (16*1024) /* Desired size in bytes */
83 #define PER_SLOT_BITS (SLOT_SIZE * 8) /* Number of bits per slot */
84 #define PER_SLOT_INTS (SLOT_SIZE / 4) /* Number of int32s per slot */
86 static uint32 **delayed_bits = NULL;
87 static int delayed_slot_cnt = 0;
89 static void init_delayed_bits(int max_ndx)
91 delayed_slot_cnt = (max_ndx + PER_SLOT_BITS - 1) / PER_SLOT_BITS;
93 if (!(delayed_bits = (uint32**)calloc(delayed_slot_cnt, sizeof (uint32*))))
94 out_of_memory("set_delayed_bit");
97 static void set_delayed_bit(int ndx)
99 int slot = ndx / PER_SLOT_BITS;
100 ndx %= PER_SLOT_BITS;
102 if (!delayed_bits[slot]) {
103 if (!(delayed_bits[slot] = (uint32*)calloc(PER_SLOT_INTS, 4)))
104 out_of_memory("set_delayed_bit");
107 delayed_bits[slot][ndx/32] |= 1u << (ndx % 32);
110 /* Call this with -1 to start checking from 0. Returns -1 at the end. */
111 static int next_delayed_bit(int after)
114 int i, ndx = after + 1;
115 int slot = ndx / PER_SLOT_BITS;
116 ndx %= PER_SLOT_BITS;
118 mask = (1u << (ndx % 32)) - 1;
119 for (i = ndx / 32; slot < delayed_slot_cnt; slot++, i = mask = 0) {
120 if (!delayed_bits[slot])
122 for ( ; i < PER_SLOT_INTS; i++, mask = 0) {
123 if (!(bits = delayed_bits[slot][i] & ~mask))
125 /* The xor magic figures out the lowest enabled bit in
126 * bits, and the switch quickly computes log2(bit). */
127 switch (bits ^ (bits & (bits-1))) {
128 #define LOG2(n) case 1u << n: return slot*PER_SLOT_BITS + i*32 + n
129 LOG2(0); LOG2(1); LOG2(2); LOG2(3);
130 LOG2(4); LOG2(5); LOG2(6); LOG2(7);
131 LOG2(8); LOG2(9); LOG2(10); LOG2(11);
132 LOG2(12); LOG2(13); LOG2(14); LOG2(15);
133 LOG2(16); LOG2(17); LOG2(18); LOG2(19);
134 LOG2(20); LOG2(21); LOG2(22); LOG2(23);
135 LOG2(24); LOG2(25); LOG2(26); LOG2(27);
136 LOG2(28); LOG2(29); LOG2(30); LOG2(31);
138 return -1; /* impossible... */
147 * get_tmpname() - create a tmp filename for a given filename
149 * If a tmpdir is defined, use that as the directory to
150 * put it in. Otherwise, the tmp filename is in the same
151 * directory as the given name. Note that there may be no
152 * directory at all in the given name!
154 * The tmp filename is basically the given filename with a
155 * dot prepended, and .XXXXXX appended (for mkstemp() to
156 * put its unique gunk in). Take care to not exceed
157 * either the MAXPATHLEN or NAME_MAX, esp. the last, as
158 * the basename basically becomes 8 chars longer. In that
159 * case, the original name is shortened sufficiently to
162 * Of course, there's no real reason for the tmp name to
163 * look like the original, except to satisfy us humans.
164 * As long as it's unique, rsync will work.
167 static int get_tmpname(char *fnametmp, char *fname)
174 /* Note: this can't overflow, so the return value is safe */
175 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
176 fnametmp[length++] = '/';
177 fnametmp[length] = '\0'; /* always NULL terminated */
180 if ((f = strrchr(fname, '/')) != NULL) {
184 /* copy up to and including the slash */
185 strlcpy(fnametmp, fname, length + 1);
189 fnametmp[length++] = '.';
190 fnametmp[length] = '\0'; /* always NULL terminated */
192 maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8);
195 rprintf(FERROR, "temporary filename too long: %s\n",
201 strlcpy(fnametmp + length, f, maxname);
202 strcat(fnametmp + length, ".XXXXXX");
208 static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
209 char *fname, int fd, OFF_T total_size)
211 static char file_sum1[MD4_SUM_LENGTH];
212 static char file_sum2[MD4_SUM_LENGTH];
213 struct map_struct *mapbuf;
214 struct sum_struct sum;
222 read_sum_head(f_in, &sum);
224 if (fd_r >= 0 && size_r > 0) {
225 int32 read_size = MAX(sum.blength * 2, 16*1024);
226 mapbuf = map_file(fd_r, size_r, read_size, sum.blength);
228 rprintf(FINFO, "recv mapped %s of size %.0f\n",
229 safe_fname(fname_r), (double)size_r);
234 sum_init(checksum_seed);
236 while ((i = recv_token(f_in, &data)) != 0) {
238 show_progress(offset, total_size);
242 rprintf(FINFO,"data recv %d at %.0f\n",
246 stats.literal_data += i;
247 cleanup_got_literal = 1;
251 if (fd != -1 && write_file(fd,data,i) != i)
252 goto report_write_error;
258 offset2 = i * (OFF_T)sum.blength;
260 if (i == (int)sum.count-1 && sum.remainder != 0)
263 stats.matched_data += len;
267 "chunk[%d] of size %ld at %.0f offset=%.0f\n",
268 i, (long)len, (double)offset2, (double)offset);
272 map = map_ptr(mapbuf,offset2,len);
275 sum_update(map, len);
279 if (offset == offset2 && fd != -1) {
280 if (flush_write_file(fd) < 0)
281 goto report_write_error;
283 if (do_lseek(fd, len, SEEK_CUR) != offset) {
284 rsyserr(FERROR, errno,
285 "lseek failed on %s",
287 exit_cleanup(RERR_FILEIO);
292 if (fd != -1 && write_file(fd, map, len) != (int)len)
293 goto report_write_error;
297 if (flush_write_file(fd) < 0)
298 goto report_write_error;
300 #ifdef HAVE_FTRUNCATE
301 if (inplace && fd != -1)
302 ftruncate(fd, offset);
306 end_progress(total_size);
308 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
310 rsyserr(FERROR, errno, "write failed on %s",
312 exit_cleanup(RERR_FILEIO);
320 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
322 rprintf(FINFO,"got file_sum\n");
323 if (fd != -1 && memcmp(file_sum1, file_sum2, MD4_SUM_LENGTH) != 0)
329 static void read_gen_name(int fd, char *dirname, char *buf)
332 int len = read_byte(fd);
335 #if MAXPATHLEN > 32767
337 read_buf(fd, (char *)lenbuf, 2);
338 len = (len & ~0x80) * 0x10000 + lenbuf[0] * 0x100 + lenbuf[1];
340 len = (len & ~0x80) * 0x100 + read_byte(fd);
345 dlen = strlcpy(buf, dirname, MAXPATHLEN);
350 if (dlen + len >= MAXPATHLEN) {
351 rprintf(FERROR, "bogus data on generator name pipe\n");
352 exit_cleanup(RERR_PROTOCOL);
355 read_sbuf(fd, buf + dlen, len);
359 static void discard_receive_data(int f_in, OFF_T length)
361 receive_data(f_in, NULL, -1, 0, NULL, -1, length);
366 * main routine for receiver process.
368 * Receiver process runs on the same host as the generator process. */
369 int recv_files(int f_in, struct file_list *flist, char *local_name,
376 char *fname, fbuf[MAXPATHLEN];
377 char template[MAXPATHLEN];
378 char fnametmp[MAXPATHLEN];
379 char *fnamecmp, *partialptr, numbuf[4];
380 char fnamecmpbuf[MAXPATHLEN];
381 struct file_struct *file;
382 struct stats initial_stats;
383 int save_make_backups = make_backups;
384 int itemizing = am_daemon ? daemon_log_format_has_i
385 : !am_server && log_format_has_i;
386 int i, recv_ok, phase = 0;
389 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
391 if (flist->hlink_pool) {
392 pool_destroy(flist->hlink_pool);
393 flist->hlink_pool = NULL;
397 init_delayed_bits(flist->count);
405 if (next_gen_i != flist->count) {
409 read_byte(f_in_name);
410 } while (read_int(batch_gen_fd) != -1);
419 csum_length = SUM_LENGTH;
421 rprintf(FINFO, "recv_files phase=%d\n", phase);
422 send_msg(MSG_DONE, "", 0);
423 if (keep_partial && !partial_dir)
424 make_backups = 0; /* prevents double backup */
428 if (i < 0 || i >= flist->count) {
429 /* Handle the new keep-alive (no-op) packet. */
430 if (i == flist->count && protocol_version >= 29
431 && read_shortint(f_in) == ITEM_IS_NEW)
433 rprintf(FERROR,"Invalid file index %d in recv_files (count=%d)\n",
435 exit_cleanup(RERR_PROTOCOL);
438 file = flist->files[i];
439 fname = local_name ? local_name : f_name_to(file, fbuf);
442 rprintf(FINFO, "recv_files(%s)\n", safe_fname(fname));
444 if (protocol_version >= 29) {
445 iflags = read_shortint(f_in);
446 if (!(iflags & ITEM_UPDATING) || !S_ISREG(file->mode)) {
447 int see_item = itemizing && (iflags || verbose > 1);
449 if (am_daemon && !dry_run && see_item)
450 log_recv(file, &stats, iflags);
451 } else if (see_item || iflags & ITEM_UPDATING
452 || (S_ISDIR(file->mode)
453 && iflags & ITEM_REPORT_TIME))
454 log_recv(file, &stats, iflags);
458 iflags = ITEM_UPDATING | ITEM_MISSING_DATA;
460 if (!S_ISREG(file->mode)) {
461 rprintf(FERROR, "[%s] got index of non-regular file: %d\n",
463 exit_cleanup(RERR_PROTOCOL);
466 stats.current_file_index = i;
467 stats.num_transferred_files++;
468 stats.total_transferred_size += file->length;
469 cleanup_got_literal = 0;
471 if (server_filter_list.head
472 && check_filter(&server_filter_list, fname, 0) < 0) {
473 rprintf(FERROR, "attempt to hack rsync failed.\n");
474 exit_cleanup(RERR_PROTOCOL);
477 if (dry_run) { /* log the transfer */
478 if (!am_server && log_format)
479 log_recv(file, &stats, iflags);
484 while (i > next_gen_i) {
485 if (f_in_name >= 0 && next_gen_i >= 0)
486 read_byte(f_in_name);
487 next_gen_i = read_int(batch_gen_fd);
488 if (next_gen_i == -1)
489 next_gen_i = flist->count;
491 if (i < next_gen_i) {
492 rprintf(FINFO, "skipping update for \"%s\"\n",
494 discard_receive_data(f_in, file->length);
500 partialptr = partial_dir ? partial_dir_fname(fname) : fname;
502 if (f_in_name >= 0) {
504 switch (j = read_byte(f_in_name)) {
508 case FNAMECMP_PARTIAL_DIR:
509 fnamecmp = partialptr ? partialptr : fname;
511 case FNAMECMP_BACKUP:
512 fnamecmp = get_backup_name(fname);
515 read_gen_name(f_in_name, file->dirname, fnamecmpbuf);
516 fnamecmp = fnamecmpbuf;
519 if (j >= basis_dir_cnt) {
521 "invalid basis_dir index: %d.\n",
523 exit_cleanup(RERR_PROTOCOL);
525 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
526 basis_dir[j], fname);
527 fnamecmp = fnamecmpbuf;
533 initial_stats = stats;
536 fd1 = do_open(fnamecmp, O_RDONLY, 0);
538 if (fd1 != -1 && do_fstat(fd1,&st) != 0) {
539 rsyserr(FERROR, errno, "fstat %s failed",
540 full_fname(fnamecmp));
541 discard_receive_data(f_in, file->length);
546 if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
547 /* this special handling for directories
548 * wouldn't be necessary if robust_rename()
549 * and the underlying robust_unlink could cope
552 rprintf(FERROR,"recv_files: %s is a directory\n",
553 full_fname(fnamecmp));
554 discard_receive_data(f_in, file->length);
559 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
564 if (fd1 != -1 && !preserve_perms) {
565 /* if the file exists already and we aren't preserving
566 * permissions then act as though the remote end sent
567 * us the file permissions we already have */
568 file->mode = st.st_mode;
571 /* We now check to see if we are writing file "inplace" */
573 fd2 = do_open(fname, O_WRONLY|O_CREAT, 0);
575 rsyserr(FERROR, errno, "open %s failed",
577 discard_receive_data(f_in, file->length);
583 if (!get_tmpname(fnametmp,fname)) {
584 discard_receive_data(f_in, file->length);
590 strlcpy(template, fnametmp, sizeof template);
592 /* we initially set the perms without the
593 * setuid/setgid bits to ensure that there is no race
594 * condition. They are then correctly updated after
595 * the lchown. Thanks to snabb@epipe.fi for pointing
596 * this out. We also set it initially without group
597 * access because of a similar race condition. */
598 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
600 /* in most cases parent directories will already exist
601 * because their information should have been previously
602 * transferred, but that may not be the case with -R */
603 if (fd2 == -1 && relative_paths && errno == ENOENT
604 && create_directory_path(fnametmp, orig_umask) == 0) {
605 strlcpy(fnametmp, template, sizeof fnametmp);
606 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
609 rsyserr(FERROR, errno, "mkstemp %s failed",
610 full_fname(fnametmp));
611 discard_receive_data(f_in, file->length);
618 cleanup_set(fnametmp, partialptr, file, fd1, fd2);
621 /* log the transfer */
622 if (log_before_transfer)
623 log_recv(file, &initial_stats, iflags);
624 else if (!am_server && verbose && do_progress)
625 rprintf(FINFO, "%s\n", safe_fname(fname));
628 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
629 fname, fd2, file->length);
631 if (!log_before_transfer)
632 log_recv(file, &initial_stats, iflags);
636 if (close(fd2) < 0) {
637 rsyserr(FERROR, errno, "close failed on %s",
638 full_fname(fnametmp));
639 exit_cleanup(RERR_FILEIO);
642 if ((recv_ok && (!delay_updates || !partialptr)) || inplace) {
643 finish_transfer(fname, fnametmp, file, recv_ok, 1);
644 if (partialptr != fname && fnamecmp == partialptr) {
645 do_unlink(partialptr);
646 handle_partial_dir(partialptr, PDIR_DELETE);
648 } else if (keep_partial && partialptr
649 && handle_partial_dir(partialptr, PDIR_CREATE)) {
650 finish_transfer(partialptr, fnametmp, file, recv_ok,
652 if (delay_updates && recv_ok) {
664 if (remove_sent_files && recv_ok > 0) {
666 send_msg(MSG_SUCCESS, numbuf, 4);
669 int msgtype = csum_length == SUM_LENGTH || read_batch ?
671 if (msgtype == FERROR || verbose) {
672 char *errstr, *redostr, *keptstr;
673 if (!(keep_partial && partialptr) && !inplace)
674 keptstr = "discarded";
675 else if (partial_dir)
676 keptstr = "put into partial-dir";
678 keptstr = "retained";
679 if (msgtype == FERROR) {
684 redostr = " (will try again)";
687 "%s: %s failed verification -- update %s%s.\n",
688 errstr, safe_fname(fname),
691 if (csum_length != SUM_LENGTH) {
693 send_msg(MSG_REDO, numbuf, 4);
697 make_backups = save_make_backups;
700 for (i = -1; (i = next_delayed_bit(i)) >= 0; ) {
701 struct file_struct *file = flist->files[i];
702 fname = local_name ? local_name : f_name(file);
703 partialptr = partial_dir_fname(fname);
705 if (make_backups && !make_backup(fname))
708 rprintf(FINFO, "renaming %s to %s\n",
709 safe_fname(partialptr),
712 if (do_rename(partialptr, fname) < 0) {
713 rsyserr(FERROR, errno,
714 "rename failed for %s (from %s)",
716 safe_fname(partialptr));
718 if (remove_sent_files) {
720 send_msg(MSG_SUCCESS,numbuf,4);
722 handle_partial_dir(partialptr,
729 if (delete_after && !local_name && flist->count > 0)
733 rprintf(FINFO,"recv_files finished\n");