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 batch_gen_fd;
34 extern int protocol_version;
35 extern int relative_paths;
36 extern int keep_dirlinks;
37 extern int preserve_hard_links;
38 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;
45 extern int ignore_errors;
46 extern int orig_umask;
47 extern int keep_partial;
48 extern int checksum_seed;
50 extern int delay_updates;
51 extern struct stats stats;
52 extern char *log_format;
54 extern char *partial_dir;
55 extern char *basis_dir[];
57 extern struct filter_list_struct server_filter_list;
59 #define SLOT_SIZE (16*1024) /* Desired size in bytes */
60 #define PER_SLOT_BITS (SLOT_SIZE * 8) /* Number of bits per slot */
61 #define PER_SLOT_INTS (SLOT_SIZE / 4) /* Number of int32s per slot */
63 static uint32 **delayed_bits = NULL;
64 static int delayed_slot_cnt = 0;
66 static void init_delayed_bits(int max_ndx)
68 delayed_slot_cnt = (max_ndx + PER_SLOT_BITS - 1) / PER_SLOT_BITS;
70 if (!(delayed_bits = (uint32**)calloc(delayed_slot_cnt, sizeof (uint32*))))
71 out_of_memory("set_delayed_bit");
74 static void set_delayed_bit(int ndx)
76 int slot = ndx / PER_SLOT_BITS;
79 if (!delayed_bits[slot]) {
80 if (!(delayed_bits[slot] = (uint32*)calloc(PER_SLOT_INTS, 4)))
81 out_of_memory("set_delayed_bit");
84 delayed_bits[slot][ndx/32] |= 1u << (ndx % 32);
87 /* Call this with -1 to start checking from 0. Returns -1 at the end. */
88 static int next_delayed_bit(int after)
91 int i, ndx = after + 1;
92 int slot = ndx / PER_SLOT_BITS;
95 mask = (1u << (ndx % 32)) - 1;
96 for (i = ndx / 32; slot < delayed_slot_cnt; slot++, i = mask = 0) {
97 if (!delayed_bits[slot])
99 for ( ; i < PER_SLOT_INTS; i++, mask = 0) {
100 if (!(bits = delayed_bits[slot][i] & ~mask))
102 /* The xor magic figures out the lowest enabled bit in
103 * bits, and the switch quickly computes log2(bit). */
104 switch (bits ^ (bits & (bits-1))) {
105 #define LOG2(n) case 1u << n: return slot*PER_SLOT_BITS + i*32 + n
106 LOG2(0); LOG2(1); LOG2(2); LOG2(3);
107 LOG2(4); LOG2(5); LOG2(6); LOG2(7);
108 LOG2(8); LOG2(9); LOG2(10); LOG2(11);
109 LOG2(12); LOG2(13); LOG2(14); LOG2(15);
110 LOG2(16); LOG2(17); LOG2(18); LOG2(19);
111 LOG2(20); LOG2(21); LOG2(22); LOG2(23);
112 LOG2(24); LOG2(25); LOG2(26); LOG2(27);
113 LOG2(28); LOG2(29); LOG2(30); LOG2(31);
115 return -1; /* impossible... */
124 * get_tmpname() - create a tmp filename for a given filename
126 * If a tmpdir is defined, use that as the directory to
127 * put it in. Otherwise, the tmp filename is in the same
128 * directory as the given name. Note that there may be no
129 * directory at all in the given name!
131 * The tmp filename is basically the given filename with a
132 * dot prepended, and .XXXXXX appended (for mkstemp() to
133 * put its unique gunk in). Take care to not exceed
134 * either the MAXPATHLEN or NAME_MAX, esp. the last, as
135 * the basename basically becomes 8 chars longer. In that
136 * case, the original name is shortened sufficiently to
139 * Of course, there's no real reason for the tmp name to
140 * look like the original, except to satisfy us humans.
141 * As long as it's unique, rsync will work.
144 static int get_tmpname(char *fnametmp, char *fname)
151 /* Note: this can't overflow, so the return value is safe */
152 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
153 fnametmp[length++] = '/';
154 fnametmp[length] = '\0'; /* always NULL terminated */
157 if ((f = strrchr(fname, '/')) != NULL) {
161 /* copy up to and including the slash */
162 strlcpy(fnametmp, fname, length + 1);
166 fnametmp[length++] = '.';
167 fnametmp[length] = '\0'; /* always NULL terminated */
169 maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8);
172 rprintf(FERROR, "temporary filename too long: %s\n",
178 strlcpy(fnametmp + length, f, maxname);
179 strcat(fnametmp + length, ".XXXXXX");
185 static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
186 char *fname, int fd, OFF_T total_size)
188 static char file_sum1[MD4_SUM_LENGTH];
189 static char file_sum2[MD4_SUM_LENGTH];
190 struct map_struct *mapbuf;
191 struct sum_struct sum;
199 read_sum_head(f_in, &sum);
201 if (fd_r >= 0 && size_r > 0) {
202 int32 read_size = MAX(sum.blength * 2, 16*1024);
203 mapbuf = map_file(fd_r, size_r, read_size, sum.blength);
205 rprintf(FINFO, "recv mapped %s of size %.0f\n",
206 safe_fname(fname_r), (double)size_r);
211 sum_init(checksum_seed);
213 while ((i = recv_token(f_in, &data)) != 0) {
215 show_progress(offset, total_size);
219 rprintf(FINFO,"data recv %d at %.0f\n",
223 stats.literal_data += i;
224 cleanup_got_literal = 1;
228 if (fd != -1 && write_file(fd,data,i) != i)
229 goto report_write_error;
235 offset2 = i * (OFF_T)sum.blength;
237 if (i == (int)sum.count-1 && sum.remainder != 0)
240 stats.matched_data += len;
244 "chunk[%d] of size %ld at %.0f offset=%.0f\n",
245 i, (long)len, (double)offset2, (double)offset);
249 map = map_ptr(mapbuf,offset2,len);
252 sum_update(map, len);
256 if (offset == offset2 && fd != -1) {
257 if (flush_write_file(fd) < 0)
258 goto report_write_error;
260 if (do_lseek(fd, len, SEEK_CUR) != offset) {
261 rsyserr(FERROR, errno,
262 "lseek failed on %s",
264 exit_cleanup(RERR_FILEIO);
269 if (fd != -1 && write_file(fd, map, len) != (int)len)
270 goto report_write_error;
274 if (flush_write_file(fd) < 0)
275 goto report_write_error;
277 #ifdef HAVE_FTRUNCATE
278 if (inplace && fd != -1)
279 ftruncate(fd, offset);
283 end_progress(total_size);
285 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
287 rsyserr(FERROR, errno, "write failed on %s",
289 exit_cleanup(RERR_FILEIO);
297 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
299 rprintf(FINFO,"got file_sum\n");
300 if (fd != -1 && memcmp(file_sum1, file_sum2, MD4_SUM_LENGTH) != 0)
306 static void read_gen_name(int fd, char *dirname, char *buf)
309 int len = read_byte(fd);
312 #if MAXPATHLEN > 32767
314 read_buf(fd, (char *)lenbuf, 2);
315 len = (len & ~0x80) * 0x10000 + lenbuf[0] * 0x100 + lenbuf[1];
317 len = (len & ~0x80) * 0x100 + read_byte(fd);
322 dlen = strlcpy(buf, dirname, MAXPATHLEN);
327 if (dlen + len >= MAXPATHLEN) {
328 rprintf(FERROR, "bogus data on generator name pipe\n");
329 exit_cleanup(RERR_PROTOCOL);
332 read_sbuf(fd, buf + dlen, len);
336 static void discard_receive_data(int f_in, OFF_T length)
338 receive_data(f_in, NULL, -1, 0, NULL, -1, length);
343 * main routine for receiver process.
345 * Receiver process runs on the same host as the generator process. */
346 int recv_files(int f_in, struct file_list *flist, char *local_name,
353 char *fname, fbuf[MAXPATHLEN];
354 char template[MAXPATHLEN];
355 char fnametmp[MAXPATHLEN];
356 char *fnamecmp, *partialptr, numbuf[4];
357 char fnamecmpbuf[MAXPATHLEN];
358 struct file_struct *file;
359 struct stats initial_stats;
360 int save_make_backups = make_backups;
361 int itemizing = am_daemon ? daemon_log_format_has_i
362 : !am_server && log_format_has_i;
363 int i, recv_ok, phase = 0;
366 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
368 if (flist->hlink_pool) {
369 pool_destroy(flist->hlink_pool);
370 flist->hlink_pool = NULL;
374 init_delayed_bits(flist->count);
382 if (next_gen_i != flist->count) {
386 read_byte(f_in_name);
387 } while (read_int(batch_gen_fd) != -1);
396 csum_length = SUM_LENGTH;
398 rprintf(FINFO, "recv_files phase=%d\n", phase);
399 send_msg(MSG_DONE, "", 0);
400 if (keep_partial && !partial_dir)
401 make_backups = 0; /* prevents double backup */
405 if (i < 0 || i >= flist->count) {
406 /* Handle the new keep-alive (no-op) packet. */
407 if (i == flist->count && protocol_version >= 29
408 && read_shortint(f_in) == ITEM_IS_NEW)
410 rprintf(FERROR,"Invalid file index %d in recv_files (count=%d)\n",
412 exit_cleanup(RERR_PROTOCOL);
415 file = flist->files[i];
416 fname = local_name ? local_name : f_name_to(file, fbuf);
419 rprintf(FINFO, "recv_files(%s)\n", safe_fname(fname));
421 if (protocol_version >= 29) {
422 iflags = read_shortint(f_in);
423 if (!(iflags & ITEM_UPDATING) || !S_ISREG(file->mode)) {
424 int see_item = itemizing && (iflags || verbose > 1);
426 if (am_daemon && !dry_run && see_item)
427 log_recv(file, &stats, iflags);
428 } else if (see_item || iflags & ITEM_UPDATING
429 || (S_ISDIR(file->mode)
430 && iflags & ITEM_REPORT_TIME))
431 log_recv(file, &stats, iflags);
435 iflags = ITEM_UPDATING | ITEM_MISSING_DATA;
437 if (!S_ISREG(file->mode)) {
438 rprintf(FERROR, "[%s] got index of non-regular file: %d\n",
440 exit_cleanup(RERR_PROTOCOL);
443 stats.current_file_index = i;
444 stats.num_transferred_files++;
445 stats.total_transferred_size += file->length;
446 cleanup_got_literal = 0;
448 if (server_filter_list.head
449 && check_filter(&server_filter_list, fname, 0) < 0) {
450 rprintf(FERROR, "attempt to hack rsync failed.\n");
451 exit_cleanup(RERR_PROTOCOL);
454 if (dry_run) { /* log the transfer */
455 if (!am_server && log_format)
456 log_recv(file, &stats, iflags);
461 while (i > next_gen_i) {
462 if (f_in_name >= 0 && next_gen_i >= 0)
463 read_byte(f_in_name);
464 next_gen_i = read_int(batch_gen_fd);
465 if (next_gen_i == -1)
466 next_gen_i = flist->count;
468 if (i < next_gen_i) {
469 rprintf(FINFO, "skipping update for \"%s\"\n",
471 discard_receive_data(f_in, file->length);
477 partialptr = partial_dir ? partial_dir_fname(fname) : fname;
479 if (f_in_name >= 0) {
481 switch (j = read_byte(f_in_name)) {
485 case FNAMECMP_PARTIAL_DIR:
486 fnamecmp = partialptr ? partialptr : fname;
488 case FNAMECMP_BACKUP:
489 fnamecmp = get_backup_name(fname);
492 read_gen_name(f_in_name, file->dirname, fnamecmpbuf);
493 fnamecmp = fnamecmpbuf;
496 if (j >= basis_dir_cnt) {
498 "invalid basis_dir index: %d.\n",
500 exit_cleanup(RERR_PROTOCOL);
502 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
503 basis_dir[j], fname);
504 fnamecmp = fnamecmpbuf;
510 initial_stats = stats;
513 fd1 = do_open(fnamecmp, O_RDONLY, 0);
515 if (fd1 != -1 && do_fstat(fd1,&st) != 0) {
516 rsyserr(FERROR, errno, "fstat %s failed",
517 full_fname(fnamecmp));
518 discard_receive_data(f_in, file->length);
523 if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
524 /* this special handling for directories
525 * wouldn't be necessary if robust_rename()
526 * and the underlying robust_unlink could cope
529 rprintf(FERROR,"recv_files: %s is a directory\n",
530 full_fname(fnamecmp));
531 discard_receive_data(f_in, file->length);
536 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
541 if (fd1 != -1 && !preserve_perms) {
542 /* if the file exists already and we aren't preserving
543 * permissions then act as though the remote end sent
544 * us the file permissions we already have */
545 file->mode = st.st_mode;
548 /* We now check to see if we are writing file "inplace" */
550 fd2 = do_open(fname, O_WRONLY|O_CREAT, 0);
552 rsyserr(FERROR, errno, "open %s failed",
554 discard_receive_data(f_in, file->length);
560 if (!get_tmpname(fnametmp,fname)) {
561 discard_receive_data(f_in, file->length);
567 strlcpy(template, fnametmp, sizeof template);
569 /* we initially set the perms without the
570 * setuid/setgid bits to ensure that there is no race
571 * condition. They are then correctly updated after
572 * the lchown. Thanks to snabb@epipe.fi for pointing
573 * this out. We also set it initially without group
574 * access because of a similar race condition. */
575 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
577 /* in most cases parent directories will already exist
578 * because their information should have been previously
579 * transferred, but that may not be the case with -R */
580 if (fd2 == -1 && relative_paths && errno == ENOENT
581 && create_directory_path(fnametmp, orig_umask) == 0) {
582 strlcpy(fnametmp, template, sizeof fnametmp);
583 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
586 rsyserr(FERROR, errno, "mkstemp %s failed",
587 full_fname(fnametmp));
588 discard_receive_data(f_in, file->length);
595 cleanup_set(fnametmp, partialptr, file, fd1, fd2);
598 /* log the transfer */
599 if (log_before_transfer)
600 log_recv(file, &initial_stats, iflags);
601 else if (!am_server && verbose && do_progress)
602 rprintf(FINFO, "%s\n", safe_fname(fname));
605 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
606 fname, fd2, file->length);
608 if (!log_before_transfer)
609 log_recv(file, &initial_stats, iflags);
613 if (close(fd2) < 0) {
614 rsyserr(FERROR, errno, "close failed on %s",
615 full_fname(fnametmp));
616 exit_cleanup(RERR_FILEIO);
619 if ((recv_ok && (!delay_updates || !partialptr)) || inplace) {
620 finish_transfer(fname, fnametmp, file, recv_ok, 1);
621 if (partialptr != fname && fnamecmp == partialptr) {
622 do_unlink(partialptr);
623 handle_partial_dir(partialptr, PDIR_DELETE);
625 } else if (keep_partial && partialptr
626 && handle_partial_dir(partialptr, PDIR_CREATE)) {
627 finish_transfer(partialptr, fnametmp, file, recv_ok,
629 if (delay_updates && recv_ok) {
641 if (remove_sent_files && recv_ok > 0) {
643 send_msg(MSG_SUCCESS, numbuf, 4);
646 int msgtype = csum_length == SUM_LENGTH || read_batch ?
648 if (msgtype == FERROR || verbose) {
649 char *errstr, *redostr, *keptstr;
650 if (!(keep_partial && partialptr) && !inplace)
651 keptstr = "discarded";
652 else if (partial_dir)
653 keptstr = "put into partial-dir";
655 keptstr = "retained";
656 if (msgtype == FERROR) {
661 redostr = " (will try again)";
664 "%s: %s failed verification -- update %s%s.\n",
665 errstr, safe_fname(fname),
668 if (csum_length != SUM_LENGTH) {
670 send_msg(MSG_REDO, numbuf, 4);
674 make_backups = save_make_backups;
677 for (i = -1; (i = next_delayed_bit(i)) >= 0; ) {
678 struct file_struct *file = flist->files[i];
679 fname = local_name ? local_name : f_name(file);
680 partialptr = partial_dir_fname(fname);
682 if (make_backups && !make_backup(fname))
685 rprintf(FINFO, "renaming %s to %s\n",
686 safe_fname(partialptr),
689 if (do_rename(partialptr, fname) < 0) {
690 rsyserr(FERROR, errno,
691 "rename failed for %s (from %s)",
693 safe_fname(partialptr));
695 if (remove_sent_files) {
697 send_msg(MSG_SUCCESS,numbuf,4);
699 handle_partial_dir(partialptr,
707 rprintf(FINFO,"recv_files finished\n");