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);
84 * get_tmpname() - create a tmp filename for a given filename
86 * If a tmpdir is defined, use that as the directory to
87 * put it in. Otherwise, the tmp filename is in the same
88 * directory as the given name. Note that there may be no
89 * directory at all in the given name!
91 * The tmp filename is basically the given filename with a
92 * dot prepended, and .XXXXXX appended (for mkstemp() to
93 * put its unique gunk in). Take care to not exceed
94 * either the MAXPATHLEN or NAME_MAX, esp. the last, as
95 * the basename basically becomes 8 chars longer. In that
96 * case, the original name is shortened sufficiently to
99 * Of course, there's no real reason for the tmp name to
100 * look like the original, except to satisfy us humans.
101 * As long as it's unique, rsync will work.
104 static int get_tmpname(char *fnametmp, char *fname)
111 /* Note: this can't overflow, so the return value is safe */
112 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
113 fnametmp[length++] = '/';
114 fnametmp[length] = '\0'; /* always NULL terminated */
117 if ((f = strrchr(fname, '/')) != NULL) {
121 /* copy up to and including the slash */
122 strlcpy(fnametmp, fname, length + 1);
126 fnametmp[length++] = '.';
127 fnametmp[length] = '\0'; /* always NULL terminated */
129 maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8);
132 rprintf(FERROR, "temporary filename too long: %s\n",
138 strlcpy(fnametmp + length, f, maxname);
139 strcat(fnametmp + length, ".XXXXXX");
145 static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
146 char *fname, int fd, OFF_T total_size)
148 static char file_sum1[MD4_SUM_LENGTH];
149 static char file_sum2[MD4_SUM_LENGTH];
150 struct map_struct *mapbuf;
151 struct sum_struct sum;
159 read_sum_head(f_in, &sum);
161 if (fd_r >= 0 && size_r > 0) {
162 int32 read_size = MAX(sum.blength * 2, 16*1024);
163 mapbuf = map_file(fd_r, size_r, read_size, sum.blength);
165 rprintf(FINFO, "recv mapped %s of size %.0f\n",
166 safe_fname(fname_r), (double)size_r);
171 sum_init(checksum_seed);
173 while ((i = recv_token(f_in, &data)) != 0) {
175 show_progress(offset, total_size);
179 rprintf(FINFO,"data recv %d at %.0f\n",
183 stats.literal_data += i;
184 cleanup_got_literal = 1;
188 if (fd != -1 && write_file(fd,data,i) != i)
189 goto report_write_error;
195 offset2 = i * (OFF_T)sum.blength;
197 if (i == (int)sum.count-1 && sum.remainder != 0)
200 stats.matched_data += len;
204 "chunk[%d] of size %ld at %.0f offset=%.0f\n",
205 i, (long)len, (double)offset2, (double)offset);
209 map = map_ptr(mapbuf,offset2,len);
212 sum_update(map, len);
216 if (offset == offset2 && fd != -1) {
217 if (flush_write_file(fd) < 0)
218 goto report_write_error;
220 if (do_lseek(fd, len, SEEK_CUR) != offset) {
221 rsyserr(FERROR, errno,
222 "lseek failed on %s",
224 exit_cleanup(RERR_FILEIO);
229 if (fd != -1 && write_file(fd, map, len) != (int)len)
230 goto report_write_error;
234 if (flush_write_file(fd) < 0)
235 goto report_write_error;
237 #ifdef HAVE_FTRUNCATE
238 if (inplace && fd != -1)
239 ftruncate(fd, offset);
243 end_progress(total_size);
245 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
247 rsyserr(FERROR, errno, "write failed on %s",
249 exit_cleanup(RERR_FILEIO);
257 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
259 rprintf(FINFO,"got file_sum\n");
260 if (fd != -1 && memcmp(file_sum1, file_sum2, MD4_SUM_LENGTH) != 0)
266 static void read_gen_name(int fd, char *dirname, char *buf)
269 int len = read_byte(fd);
272 #if MAXPATHLEN > 32767
274 read_buf(fd, (char *)lenbuf, 2);
275 len = (len & ~0x80) * 0x10000 + lenbuf[0] * 0x100 + lenbuf[1];
277 len = (len & ~0x80) * 0x100 + read_byte(fd);
282 dlen = strlcpy(buf, dirname, MAXPATHLEN);
287 if (dlen + len >= MAXPATHLEN) {
288 rprintf(FERROR, "bogus data on generator name pipe\n");
289 exit_cleanup(RERR_PROTOCOL);
292 read_sbuf(fd, buf + dlen, len);
296 static void discard_receive_data(int f_in, OFF_T length)
298 receive_data(f_in, NULL, -1, 0, NULL, -1, length);
303 * main routine for receiver process.
305 * Receiver process runs on the same host as the generator process. */
306 int recv_files(int f_in, struct file_list *flist, char *local_name,
313 char *fname, fbuf[MAXPATHLEN];
314 char template[MAXPATHLEN];
315 char fnametmp[MAXPATHLEN];
316 char *fnamecmp, *partialptr, numbuf[4];
317 char fnamecmpbuf[MAXPATHLEN];
318 uchar *delayed_bits = NULL;
319 struct file_struct *file;
320 struct stats initial_stats;
321 int save_make_backups = make_backups;
322 int itemizing = am_daemon ? daemon_log_format_has_i
323 : !am_server && log_format_has_i;
324 int i, recv_ok, phase = 0;
327 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
329 if (flist->hlink_pool) {
330 pool_destroy(flist->hlink_pool);
331 flist->hlink_pool = NULL;
335 int sz = (flist->count + 7) / 8;
336 if (!(delayed_bits = new_array(uchar, sz)))
337 out_of_memory("recv_files");
338 memset(delayed_bits, 0, sz);
347 if (next_gen_i != flist->count) {
351 read_byte(f_in_name);
352 } while (read_int(batch_gen_fd) != -1);
361 csum_length = SUM_LENGTH;
363 rprintf(FINFO, "recv_files phase=%d\n", phase);
364 send_msg(MSG_DONE, "", 0);
365 if (keep_partial && !partial_dir)
366 make_backups = 0; /* prevents double backup */
370 if (i < 0 || i >= flist->count) {
371 rprintf(FERROR,"Invalid file index %d in recv_files (count=%d)\n",
373 exit_cleanup(RERR_PROTOCOL);
376 file = flist->files[i];
377 fname = local_name ? local_name : f_name_to(file, fbuf);
380 rprintf(FINFO, "recv_files(%s)\n", safe_fname(fname));
382 if (protocol_version >= 29) {
383 iflags = read_shortint(f_in);
384 if (!(iflags & ITEM_UPDATING) || !S_ISREG(file->mode)) {
385 int see_item = itemizing && (iflags || verbose > 1);
387 if (am_daemon && !dry_run && see_item)
388 log_recv(file, &stats, iflags);
389 } else if (see_item || iflags & ITEM_UPDATING
390 || (S_ISDIR(file->mode)
391 && iflags & ITEM_REPORT_TIME))
392 log_recv(file, &stats, iflags);
396 iflags = ITEM_UPDATING | ITEM_MISSING_DATA;
398 if (!S_ISREG(file->mode)) {
399 rprintf(FERROR, "[%s] got index of non-regular file: %d\n",
401 exit_cleanup(RERR_PROTOCOL);
404 stats.current_file_index = i;
405 stats.num_transferred_files++;
406 stats.total_transferred_size += file->length;
407 cleanup_got_literal = 0;
409 if (server_filter_list.head
410 && check_filter(&server_filter_list, fname, 0) < 0) {
411 rprintf(FERROR, "attempt to hack rsync failed.\n");
412 exit_cleanup(RERR_PROTOCOL);
415 if (dry_run) { /* log the transfer */
416 if (!am_server && log_format)
417 log_recv(file, &stats, iflags);
422 while (i > next_gen_i) {
423 if (f_in_name >= 0 && next_gen_i >= 0)
424 read_byte(f_in_name);
425 next_gen_i = read_int(batch_gen_fd);
426 if (next_gen_i == -1)
427 next_gen_i = flist->count;
429 if (i < next_gen_i) {
430 rprintf(FINFO, "skipping update for \"%s\"\n",
432 discard_receive_data(f_in, file->length);
438 partialptr = partial_dir ? partial_dir_fname(fname) : fname;
440 if (f_in_name >= 0) {
442 switch (j = read_byte(f_in_name)) {
446 case FNAMECMP_PARTIAL_DIR:
447 fnamecmp = partialptr ? partialptr : fname;
449 case FNAMECMP_BACKUP:
450 fnamecmp = get_backup_name(fname);
453 read_gen_name(f_in_name, file->dirname, fnamecmpbuf);
454 fnamecmp = fnamecmpbuf;
457 if (j >= basis_dir_cnt) {
459 "invalid basis_dir index: %d.\n",
461 exit_cleanup(RERR_PROTOCOL);
463 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
464 basis_dir[j], fname);
465 fnamecmp = fnamecmpbuf;
471 initial_stats = stats;
474 fd1 = do_open(fnamecmp, O_RDONLY, 0);
476 if (fd1 != -1 && do_fstat(fd1,&st) != 0) {
477 rsyserr(FERROR, errno, "fstat %s failed",
478 full_fname(fnamecmp));
479 discard_receive_data(f_in, file->length);
484 if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
485 /* this special handling for directories
486 * wouldn't be necessary if robust_rename()
487 * and the underlying robust_unlink could cope
490 rprintf(FERROR,"recv_files: %s is a directory\n",
491 full_fname(fnamecmp));
492 discard_receive_data(f_in, file->length);
497 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
502 if (fd1 != -1 && !preserve_perms) {
503 /* if the file exists already and we aren't preserving
504 * permissions then act as though the remote end sent
505 * us the file permissions we already have */
506 file->mode = st.st_mode;
509 /* We now check to see if we are writing file "inplace" */
511 fd2 = do_open(fname, O_WRONLY|O_CREAT, 0);
513 rsyserr(FERROR, errno, "open %s failed",
515 discard_receive_data(f_in, file->length);
521 if (!get_tmpname(fnametmp,fname)) {
522 discard_receive_data(f_in, file->length);
528 strlcpy(template, fnametmp, sizeof template);
530 /* we initially set the perms without the
531 * setuid/setgid bits to ensure that there is no race
532 * condition. They are then correctly updated after
533 * the lchown. Thanks to snabb@epipe.fi for pointing
534 * this out. We also set it initially without group
535 * access because of a similar race condition. */
536 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
538 /* in most cases parent directories will already exist
539 * because their information should have been previously
540 * transferred, but that may not be the case with -R */
541 if (fd2 == -1 && relative_paths && errno == ENOENT
542 && create_directory_path(fnametmp, orig_umask) == 0) {
543 strlcpy(fnametmp, template, sizeof fnametmp);
544 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
547 rsyserr(FERROR, errno, "mkstemp %s failed",
548 full_fname(fnametmp));
549 discard_receive_data(f_in, file->length);
556 cleanup_set(fnametmp, partialptr, file, fd1, fd2);
559 /* log the transfer */
560 if (log_before_transfer)
561 log_recv(file, &initial_stats, iflags);
562 else if (!am_server && verbose && do_progress)
563 rprintf(FINFO, "%s\n", safe_fname(fname));
566 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
567 fname, fd2, file->length);
569 if (!log_before_transfer)
570 log_recv(file, &initial_stats, iflags);
574 if (close(fd2) < 0) {
575 rsyserr(FERROR, errno, "close failed on %s",
576 full_fname(fnametmp));
577 exit_cleanup(RERR_FILEIO);
580 if ((recv_ok && (!delay_updates || !partialptr)) || inplace) {
581 finish_transfer(fname, fnametmp, file, recv_ok, 1);
582 if (partialptr != fname && fnamecmp == partialptr) {
583 do_unlink(partialptr);
584 handle_partial_dir(partialptr, PDIR_DELETE);
586 } else if (keep_partial && partialptr
587 && handle_partial_dir(partialptr, PDIR_CREATE)) {
588 finish_transfer(partialptr, fnametmp, file, recv_ok,
590 if (delay_updates && recv_ok)
591 delayed_bits[i/8] |= 1 << (i % 8);
600 if (remove_sent_files) {
602 send_msg(MSG_SUCCESS, numbuf, 4);
605 int msgtype = csum_length == SUM_LENGTH || read_batch ?
607 if (msgtype == FERROR || verbose) {
608 char *errstr, *redostr, *keptstr;
609 if (!(keep_partial && partialptr) && !inplace)
610 keptstr = "discarded";
611 else if (partial_dir)
612 keptstr = "put into partial-dir";
614 keptstr = "retained";
615 if (msgtype == FERROR) {
620 redostr = " (will try again)";
623 "%s: %s failed verification -- update %s%s.\n",
624 errstr, safe_fname(fname),
627 if (csum_length != SUM_LENGTH) {
629 send_msg(MSG_REDO, numbuf, 4);
633 make_backups = save_make_backups;
636 for (i = 0; i < flist->count; i++) {
637 struct file_struct *file = flist->files[i];
639 || !(delayed_bits[i/8] & (1 << (i % 8))))
641 fname = local_name ? local_name : f_name(file);
642 partialptr = partial_dir_fname(fname);
644 if (make_backups && !make_backup(fname))
647 rprintf(FINFO, "renaming %s to %s\n",
648 safe_fname(partialptr),
651 if (do_rename(partialptr, fname) < 0) {
652 rsyserr(FERROR, errno,
653 "rename failed for %s (from %s)",
655 safe_fname(partialptr));
657 handle_partial_dir(partialptr,
664 if (delete_after && !local_name && flist->count > 0)
668 rprintf(FINFO,"recv_files finished\n");