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;
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;
60 /* This deletes any files on the receiving side that are not present on the
61 * sending side. This is used by --delete-before and --delete-after. */
62 void delete_files(struct file_list *flist)
64 char fbuf[MAXPATHLEN];
67 for (j = 0; j < flist->count; j++) {
68 struct file_struct *file = flist->files[j];
70 if (!(file->flags & FLAG_DEL_HERE))
73 f_name_to(file, fbuf);
74 if (verbose > 1 && file->flags & FLAG_TOP_DIR)
75 rprintf(FINFO, "deleting in %s\n", safe_fname(fbuf));
77 delete_in_dir(flist, fbuf, file);
83 * get_tmpname() - create a tmp filename for a given filename
85 * If a tmpdir is defined, use that as the directory to
86 * put it in. Otherwise, the tmp filename is in the same
87 * directory as the given name. Note that there may be no
88 * directory at all in the given name!
90 * The tmp filename is basically the given filename with a
91 * dot prepended, and .XXXXXX appended (for mkstemp() to
92 * put its unique gunk in). Take care to not exceed
93 * either the MAXPATHLEN or NAME_MAX, esp. the last, as
94 * the basename basically becomes 8 chars longer. In that
95 * case, the original name is shortened sufficiently to
98 * Of course, there's no real reason for the tmp name to
99 * look like the original, except to satisfy us humans.
100 * As long as it's unique, rsync will work.
103 static int get_tmpname(char *fnametmp, char *fname)
110 /* Note: this can't overflow, so the return value is safe */
111 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
112 fnametmp[length++] = '/';
113 fnametmp[length] = '\0'; /* always NULL terminated */
116 if ((f = strrchr(fname, '/')) != NULL) {
120 /* copy up to and including the slash */
121 strlcpy(fnametmp, fname, length + 1);
125 fnametmp[length++] = '.';
126 fnametmp[length] = '\0'; /* always NULL terminated */
128 maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8);
131 rprintf(FERROR, "temporary filename too long: %s\n",
137 strlcpy(fnametmp + length, f, maxname);
138 strcat(fnametmp + length, ".XXXXXX");
144 static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
145 char *fname, int fd, OFF_T total_size)
147 static char file_sum1[MD4_SUM_LENGTH];
148 static char file_sum2[MD4_SUM_LENGTH];
149 struct map_struct *mapbuf;
150 struct sum_struct sum;
158 read_sum_head(f_in, &sum);
160 if (fd_r >= 0 && size_r > 0) {
161 int32 read_size = MAX(sum.blength * 2, 16*1024);
162 mapbuf = map_file(fd_r, size_r, read_size, sum.blength);
164 rprintf(FINFO, "recv mapped %s of size %.0f\n",
165 safe_fname(fname_r), (double)size_r);
170 sum_init(checksum_seed);
172 while ((i = recv_token(f_in, &data)) != 0) {
174 show_progress(offset, total_size);
178 rprintf(FINFO,"data recv %d at %.0f\n",
182 stats.literal_data += i;
183 cleanup_got_literal = 1;
187 if (fd != -1 && write_file(fd,data,i) != i)
188 goto report_write_error;
194 offset2 = i * (OFF_T)sum.blength;
196 if (i == (int)sum.count-1 && sum.remainder != 0)
199 stats.matched_data += len;
203 "chunk[%d] of size %ld at %.0f offset=%.0f\n",
204 i, (long)len, (double)offset2, (double)offset);
208 map = map_ptr(mapbuf,offset2,len);
211 sum_update(map, len);
215 if (offset == offset2 && fd != -1) {
216 if (flush_write_file(fd) < 0)
217 goto report_write_error;
219 if (do_lseek(fd, len, SEEK_CUR) != offset) {
220 rsyserr(FERROR, errno,
221 "lseek failed on %s",
223 exit_cleanup(RERR_FILEIO);
228 if (fd != -1 && write_file(fd, map, len) != (int)len)
229 goto report_write_error;
233 if (flush_write_file(fd) < 0)
234 goto report_write_error;
236 #ifdef HAVE_FTRUNCATE
237 if (inplace && fd != -1)
238 ftruncate(fd, offset);
242 end_progress(total_size);
244 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
246 rsyserr(FERROR, errno, "write failed on %s",
248 exit_cleanup(RERR_FILEIO);
256 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
258 rprintf(FINFO,"got file_sum\n");
259 if (fd != -1 && memcmp(file_sum1, file_sum2, MD4_SUM_LENGTH) != 0)
265 static void read_gen_name(int fd, char *dirname, char *buf)
268 int len = read_byte(fd);
271 #if MAXPATHLEN > 32767
273 read_buf(fd, (char *)lenbuf, 2);
274 len = (len & ~0x80) * 0x10000 + lenbuf[0] * 0x100 + lenbuf[1];
276 len = (len & ~0x80) * 0x100 + read_byte(fd);
281 dlen = strlcpy(buf, dirname, MAXPATHLEN);
286 if (dlen + len >= MAXPATHLEN) {
287 rprintf(FERROR, "bogus data on generator name pipe\n");
288 exit_cleanup(RERR_PROTOCOL);
291 read_sbuf(fd, buf + dlen, len);
295 static void discard_receive_data(int f_in, OFF_T length)
297 receive_data(f_in, NULL, -1, 0, NULL, -1, length);
302 * main routine for receiver process.
304 * Receiver process runs on the same host as the generator process. */
305 int recv_files(int f_in, struct file_list *flist, char *local_name,
312 char *fname, fbuf[MAXPATHLEN];
313 char template[MAXPATHLEN];
314 char fnametmp[MAXPATHLEN];
315 char *fnamecmp, *partialptr;
316 char fnamecmpbuf[MAXPATHLEN];
317 uchar *delayed_bits = NULL;
318 struct file_struct *file;
319 struct stats initial_stats;
320 int save_make_backups = make_backups;
321 int itemizing = am_daemon ? daemon_log_format_has_i
322 : !am_server && log_format_has_i;
323 int i, recv_ok, phase = 0;
326 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
328 if (flist->hlink_pool) {
329 pool_destroy(flist->hlink_pool);
330 flist->hlink_pool = NULL;
334 int sz = (flist->count + 7) / 8;
335 if (!(delayed_bits = new_array(uchar, sz)))
336 out_of_memory("recv_files");
337 memset(delayed_bits, 0, sz);
346 if (next_gen_i != flist->count) {
350 read_byte(f_in_name);
351 } while (read_int(batch_gen_fd) != -1);
360 csum_length = SUM_LENGTH;
362 rprintf(FINFO, "recv_files phase=%d\n", phase);
363 send_msg(MSG_DONE, "", 0);
364 if (keep_partial && !partial_dir)
365 make_backups = 0; /* prevents double backup */
369 if (i < 0 || i >= flist->count) {
370 rprintf(FERROR,"Invalid file index %d in recv_files (count=%d)\n",
372 exit_cleanup(RERR_PROTOCOL);
375 file = flist->files[i];
376 fname = local_name ? local_name : f_name_to(file, fbuf);
379 rprintf(FINFO, "recv_files(%s)\n", safe_fname(fname));
381 if (protocol_version >= 29) {
382 iflags = read_shortint(f_in);
383 if (!(iflags & ITEM_UPDATING) || !S_ISREG(file->mode)) {
384 int see_item = itemizing && (iflags || verbose > 1);
386 if (am_daemon && !dry_run && see_item)
387 log_recv(file, &stats, iflags);
388 } else if (see_item || iflags & ITEM_UPDATING
389 || (S_ISDIR(file->mode)
390 && iflags & ITEM_REPORT_TIME))
391 log_recv(file, &stats, iflags);
395 iflags = ITEM_UPDATING | ITEM_MISSING_DATA;
397 if (!S_ISREG(file->mode)) {
398 rprintf(FERROR, "[%s] got index of non-regular file: %d\n",
400 exit_cleanup(RERR_PROTOCOL);
403 stats.current_file_index = i;
404 stats.num_transferred_files++;
405 stats.total_transferred_size += file->length;
406 cleanup_got_literal = 0;
408 if (server_filter_list.head
409 && check_filter(&server_filter_list, fname, 0) < 0) {
410 rprintf(FERROR, "attempt to hack rsync failed.\n");
411 exit_cleanup(RERR_PROTOCOL);
414 if (dry_run) { /* log the transfer */
415 if (!am_server && log_format)
416 log_recv(file, &stats, iflags);
421 while (i > next_gen_i) {
422 if (f_in_name >= 0 && next_gen_i >= 0)
423 read_byte(f_in_name);
424 next_gen_i = read_int(batch_gen_fd);
425 if (next_gen_i == -1)
426 next_gen_i = flist->count;
428 if (i < next_gen_i) {
429 rprintf(FINFO, "skipping update for \"%s\"\n",
431 discard_receive_data(f_in, file->length);
437 partialptr = partial_dir ? partial_dir_fname(fname) : fname;
439 if (f_in_name >= 0) {
441 switch (j = read_byte(f_in_name)) {
445 case FNAMECMP_PARTIAL_DIR:
446 fnamecmp = partialptr ? partialptr : fname;
448 case FNAMECMP_BACKUP:
449 fnamecmp = get_backup_name(fname);
452 read_gen_name(f_in_name, file->dirname, fnamecmpbuf);
453 fnamecmp = fnamecmpbuf;
456 if (j >= basis_dir_cnt) {
458 "invalid basis_dir index: %d.\n",
460 exit_cleanup(RERR_PROTOCOL);
462 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
463 basis_dir[j], fname);
464 fnamecmp = fnamecmpbuf;
470 initial_stats = stats;
473 fd1 = do_open(fnamecmp, O_RDONLY, 0);
475 if (fd1 != -1 && do_fstat(fd1,&st) != 0) {
476 rsyserr(FERROR, errno, "fstat %s failed",
477 full_fname(fnamecmp));
478 discard_receive_data(f_in, file->length);
483 if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
484 /* this special handling for directories
485 * wouldn't be necessary if robust_rename()
486 * and the underlying robust_unlink could cope
489 rprintf(FERROR,"recv_files: %s is a directory\n",
490 full_fname(fnamecmp));
491 discard_receive_data(f_in, file->length);
496 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
501 if (fd1 != -1 && !preserve_perms) {
502 /* if the file exists already and we aren't preserving
503 * permissions then act as though the remote end sent
504 * us the file permissions we already have */
505 file->mode = st.st_mode;
508 /* We now check to see if we are writing file "inplace" */
510 fd2 = do_open(fname, O_WRONLY|O_CREAT, 0);
512 rsyserr(FERROR, errno, "open %s failed",
514 discard_receive_data(f_in, file->length);
520 if (!get_tmpname(fnametmp,fname)) {
521 discard_receive_data(f_in, file->length);
527 strlcpy(template, fnametmp, sizeof template);
529 /* we initially set the perms without the
530 * setuid/setgid bits to ensure that there is no race
531 * condition. They are then correctly updated after
532 * the lchown. Thanks to snabb@epipe.fi for pointing
533 * this out. We also set it initially without group
534 * access because of a similar race condition. */
535 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
537 /* in most cases parent directories will already exist
538 * because their information should have been previously
539 * transferred, but that may not be the case with -R */
540 if (fd2 == -1 && relative_paths && errno == ENOENT
541 && create_directory_path(fnametmp, orig_umask) == 0) {
542 strlcpy(fnametmp, template, sizeof fnametmp);
543 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
546 rsyserr(FERROR, errno, "mkstemp %s failed",
547 full_fname(fnametmp));
548 discard_receive_data(f_in, file->length);
555 cleanup_set(fnametmp, partialptr, file, fd1, fd2);
558 /* log the transfer */
559 if (log_before_transfer)
560 log_recv(file, &initial_stats, iflags);
561 else if (!am_server && verbose && do_progress)
562 rprintf(FINFO, "%s\n", safe_fname(fname));
565 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
566 fname, fd2, file->length);
568 if (!log_before_transfer)
569 log_recv(file, &initial_stats, iflags);
573 if (close(fd2) < 0) {
574 rsyserr(FERROR, errno, "close failed on %s",
575 full_fname(fnametmp));
576 exit_cleanup(RERR_FILEIO);
579 if ((recv_ok && (!delay_updates || !partialptr)) || inplace) {
580 finish_transfer(fname, fnametmp, file, recv_ok, 1);
581 if (partialptr != fname && fnamecmp == partialptr) {
582 do_unlink(partialptr);
583 handle_partial_dir(partialptr, PDIR_DELETE);
585 } else if (keep_partial && partialptr
586 && handle_partial_dir(partialptr, PDIR_CREATE)) {
587 finish_transfer(partialptr, fnametmp, file, recv_ok,
589 if (delay_updates && recv_ok)
590 delayed_bits[i/8] |= 1 << (i % 8);
599 int msgtype = csum_length == SUM_LENGTH || read_batch ?
601 if (msgtype == FERROR || verbose) {
602 char *errstr, *redostr, *keptstr;
603 if (!(keep_partial && partialptr) && !inplace)
604 keptstr = "discarded";
605 else if (partial_dir)
606 keptstr = "put into partial-dir";
608 keptstr = "retained";
609 if (msgtype == FERROR) {
614 redostr = " (will try again)";
617 "%s: %s failed verification -- update %s%s.\n",
618 errstr, safe_fname(fname),
621 if (csum_length != SUM_LENGTH) {
624 send_msg(MSG_REDO, buf, 4);
628 make_backups = save_make_backups;
631 for (i = 0; i < flist->count; i++) {
632 struct file_struct *file = flist->files[i];
634 || !(delayed_bits[i/8] & (1 << (i % 8))))
636 fname = local_name ? local_name : f_name(file);
637 partialptr = partial_dir_fname(fname);
639 if (make_backups && !make_backup(fname))
642 rprintf(FINFO, "renaming %s to %s\n",
643 safe_fname(partialptr),
646 if (do_rename(partialptr, fname) < 0) {
647 rsyserr(FERROR, errno,
648 "rename failed for %s (from %s)",
650 safe_fname(partialptr));
652 handle_partial_dir(partialptr,
659 if (delete_after && !local_name && flist->count > 0)
663 rprintf(FINFO,"recv_files finished\n");