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.
24 extern int itemize_changes;
25 extern int log_before_transfer;
26 extern int delete_after;
27 extern int csum_length;
28 extern struct stats stats;
30 extern int read_batch;
31 extern int batch_gen_fd;
33 extern int protocol_version;
34 extern int relative_paths;
35 extern int keep_dirlinks;
36 extern int preserve_hard_links;
37 extern int preserve_perms;
40 extern char *partial_dir;
41 extern char *basis_dir[];
42 extern int basis_dir_cnt;
43 extern int make_backups;
44 extern int do_progress;
45 extern int cleanup_got_literal;
47 extern int ignore_errors;
48 extern int orig_umask;
49 extern int keep_partial;
50 extern int checksum_seed;
52 extern int delay_updates;
53 extern char *log_format;
55 extern struct filter_list_struct server_filter_list;
58 /* This deletes any files on the receiving side that are not present on the
59 * sending side. This is used by --delete-before and --delete-after. */
60 void delete_files(struct file_list *flist)
62 char fbuf[MAXPATHLEN];
65 for (j = 0; j < flist->count; j++) {
66 struct file_struct *file = flist->files[j];
68 if (!(file->flags & FLAG_DEL_HERE))
71 f_name_to(file, fbuf);
72 if (verbose > 1 && file->flags & FLAG_TOP_DIR)
73 rprintf(FINFO, "deleting in %s\n", safe_fname(fbuf));
75 delete_in_dir(flist, fbuf, file);
81 * get_tmpname() - create a tmp filename for a given filename
83 * If a tmpdir is defined, use that as the directory to
84 * put it in. Otherwise, the tmp filename is in the same
85 * directory as the given name. Note that there may be no
86 * directory at all in the given name!
88 * The tmp filename is basically the given filename with a
89 * dot prepended, and .XXXXXX appended (for mkstemp() to
90 * put its unique gunk in). Take care to not exceed
91 * either the MAXPATHLEN or NAME_MAX, esp. the last, as
92 * the basename basically becomes 8 chars longer. In that
93 * case, the original name is shortened sufficiently to
96 * Of course, there's no real reason for the tmp name to
97 * look like the original, except to satisfy us humans.
98 * As long as it's unique, rsync will work.
101 static int get_tmpname(char *fnametmp, char *fname)
108 /* Note: this can't overflow, so the return value is safe */
109 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
110 fnametmp[length++] = '/';
111 fnametmp[length] = '\0'; /* always NULL terminated */
114 if ((f = strrchr(fname, '/')) != NULL) {
118 /* copy up to and including the slash */
119 strlcpy(fnametmp, fname, length + 1);
123 fnametmp[length++] = '.';
124 fnametmp[length] = '\0'; /* always NULL terminated */
126 maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8);
129 rprintf(FERROR, "temporary filename too long: %s\n",
135 strlcpy(fnametmp + length, f, maxname);
136 strcat(fnametmp + length, ".XXXXXX");
142 static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
143 char *fname, int fd, OFF_T total_size)
145 static char file_sum1[MD4_SUM_LENGTH];
146 static char file_sum2[MD4_SUM_LENGTH];
147 struct map_struct *mapbuf;
148 struct sum_struct sum;
156 read_sum_head(f_in, &sum);
158 if (fd_r >= 0 && size_r > 0) {
159 int32 read_size = MAX(sum.blength * 2, 16*1024);
160 mapbuf = map_file(fd_r, size_r, read_size, sum.blength);
162 rprintf(FINFO, "recv mapped %s of size %.0f\n",
163 safe_fname(fname_r), (double)size_r);
168 sum_init(checksum_seed);
170 while ((i = recv_token(f_in, &data)) != 0) {
172 show_progress(offset, total_size);
176 rprintf(FINFO,"data recv %d at %.0f\n",
180 stats.literal_data += i;
181 cleanup_got_literal = 1;
185 if (fd != -1 && write_file(fd,data,i) != i)
186 goto report_write_error;
192 offset2 = i * (OFF_T)sum.blength;
194 if (i == (int)sum.count-1 && sum.remainder != 0)
197 stats.matched_data += len;
201 "chunk[%d] of size %ld at %.0f offset=%.0f\n",
202 i, (long)len, (double)offset2, (double)offset);
206 map = map_ptr(mapbuf,offset2,len);
209 sum_update(map, len);
213 if (offset == offset2 && fd != -1) {
214 if (flush_write_file(fd) < 0)
215 goto report_write_error;
217 if (do_lseek(fd, len, SEEK_CUR) != offset) {
218 rsyserr(FERROR, errno,
219 "lseek failed on %s",
221 exit_cleanup(RERR_FILEIO);
226 if (fd != -1 && write_file(fd, map, len) != (int)len)
227 goto report_write_error;
231 if (flush_write_file(fd) < 0)
232 goto report_write_error;
234 #ifdef HAVE_FTRUNCATE
235 if (inplace && fd != -1)
236 ftruncate(fd, offset);
240 end_progress(total_size);
242 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
244 rsyserr(FERROR, errno, "write failed on %s",
246 exit_cleanup(RERR_FILEIO);
254 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
256 rprintf(FINFO,"got file_sum\n");
257 if (fd != -1 && memcmp(file_sum1, file_sum2, MD4_SUM_LENGTH) != 0)
263 static void read_gen_name(int fd, char *dirname, char *buf)
266 int len = read_byte(fd);
269 #if MAXPATHLEN > 32767
271 read_buf(fd, (char *)lenbuf, 2);
272 len = (len & ~0x80) * 0x10000 + lenbuf[0] * 0x100 + lenbuf[1];
274 len = (len & ~0x80) * 0x100 + read_byte(fd);
279 dlen = strlcpy(buf, dirname, MAXPATHLEN);
284 if (dlen + len >= MAXPATHLEN) {
285 rprintf(FERROR, "bogus data on generator name pipe\n");
286 exit_cleanup(RERR_PROTOCOL);
289 read_sbuf(fd, buf + dlen, len);
293 static void discard_receive_data(int f_in, OFF_T length)
295 receive_data(f_in, NULL, -1, 0, NULL, -1, length);
300 * main routine for receiver process.
302 * Receiver process runs on the same host as the generator process. */
303 int recv_files(int f_in, struct file_list *flist, char *local_name,
310 char *fname, fbuf[MAXPATHLEN];
311 char template[MAXPATHLEN];
312 char fnametmp[MAXPATHLEN];
313 char *fnamecmp, *partialptr;
314 char fnamecmpbuf[MAXPATHLEN];
315 uchar *delayed_bits = NULL;
316 struct file_struct *file;
317 struct stats initial_stats;
318 int save_make_backups = make_backups;
319 int i, recv_ok, phase = 0;
322 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
324 if (flist->hlink_pool) {
325 pool_destroy(flist->hlink_pool);
326 flist->hlink_pool = NULL;
330 int sz = (flist->count + 7) / 8;
331 if (!(delayed_bits = new_array(uchar, sz)))
332 out_of_memory("recv_files");
333 memset(delayed_bits, 0, sz);
342 if (next_gen_i != flist->count) {
346 read_byte(f_in_name);
347 } while (read_int(batch_gen_fd) != -1);
356 csum_length = SUM_LENGTH;
358 rprintf(FINFO, "recv_files phase=%d\n", phase);
359 send_msg(MSG_DONE, "", 0);
360 if (keep_partial && !partial_dir)
361 make_backups = 0; /* prevents double backup */
365 if (i < 0 || i >= flist->count) {
366 rprintf(FERROR,"Invalid file index %d in recv_files (count=%d)\n",
368 exit_cleanup(RERR_PROTOCOL);
371 file = flist->files[i];
373 if (itemize_changes) {
374 iflags = read_byte(f_in);
375 if (!(iflags & ITEM_UPDATING) || !S_ISREG(file->mode)) {
376 if (!dry_run || !am_server)
377 log_recv(file, &stats, iflags);
381 iflags = ITEM_UPDATING | ITEM_MISSING_DATA;
383 if (!S_ISREG(file->mode)) {
384 rprintf(FERROR, "[%s] got index of non-regular file: %d\n",
386 exit_cleanup(RERR_PROTOCOL);
389 stats.current_file_index = i;
390 stats.num_transferred_files++;
391 stats.total_transferred_size += file->length;
392 cleanup_got_literal = 0;
394 fname = local_name ? local_name : f_name_to(file, fbuf);
396 if (server_filter_list.head
397 && check_filter(&server_filter_list, fname, 0) < 0) {
398 rprintf(FERROR, "attempt to hack rsync failed.\n");
399 exit_cleanup(RERR_PROTOCOL);
403 rprintf(FINFO, "recv_files(%s)\n", safe_fname(fname));
405 if (dry_run) { /* log the transfer */
406 if (!am_server && verbose && !log_format)
407 rprintf(FINFO, "%s\n", safe_fname(fname));
409 log_recv(file, &stats, iflags);
414 while (i > next_gen_i) {
415 if (f_in_name >= 0 && next_gen_i >= 0)
416 read_byte(f_in_name);
417 next_gen_i = read_int(batch_gen_fd);
418 if (next_gen_i == -1)
419 next_gen_i = flist->count;
421 if (i < next_gen_i) {
422 rprintf(FINFO, "skipping update for \"%s\"\n",
424 discard_receive_data(f_in, file->length);
430 partialptr = partial_dir ? partial_dir_fname(fname) : fname;
432 if (f_in_name >= 0) {
434 switch (j = read_byte(f_in_name)) {
438 case FNAMECMP_PARTIAL_DIR:
439 fnamecmp = partialptr ? partialptr : fname;
441 case FNAMECMP_BACKUP:
442 fnamecmp = get_backup_name(fname);
445 read_gen_name(f_in_name, file->dirname, fnamecmpbuf);
446 fnamecmp = fnamecmpbuf;
449 if (j >= basis_dir_cnt) {
451 "invalid basis_dir index: %d.\n",
453 exit_cleanup(RERR_PROTOCOL);
455 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
456 basis_dir[j], fname);
457 fnamecmp = fnamecmpbuf;
463 initial_stats = stats;
466 fd1 = do_open(fnamecmp, O_RDONLY, 0);
468 if (fd1 != -1 && do_fstat(fd1,&st) != 0) {
469 rsyserr(FERROR, errno, "fstat %s failed",
470 full_fname(fnamecmp));
471 discard_receive_data(f_in, file->length);
476 if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
477 /* this special handling for directories
478 * wouldn't be necessary if robust_rename()
479 * and the underlying robust_unlink could cope
482 rprintf(FERROR,"recv_files: %s is a directory\n",
483 full_fname(fnamecmp));
484 discard_receive_data(f_in, file->length);
489 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
494 if (fd1 != -1 && !preserve_perms) {
495 /* if the file exists already and we aren't preserving
496 * permissions then act as though the remote end sent
497 * us the file permissions we already have */
498 file->mode = st.st_mode;
501 /* We now check to see if we are writing file "inplace" */
503 fd2 = do_open(fname, O_WRONLY|O_CREAT, 0);
505 rsyserr(FERROR, errno, "open %s failed",
507 discard_receive_data(f_in, file->length);
513 if (!get_tmpname(fnametmp,fname)) {
514 discard_receive_data(f_in, file->length);
520 strlcpy(template, fnametmp, sizeof template);
522 /* we initially set the perms without the
523 * setuid/setgid bits to ensure that there is no race
524 * condition. They are then correctly updated after
525 * the lchown. Thanks to snabb@epipe.fi for pointing
526 * this out. We also set it initially without group
527 * access because of a similar race condition. */
528 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
530 /* in most cases parent directories will already exist
531 * because their information should have been previously
532 * transferred, but that may not be the case with -R */
533 if (fd2 == -1 && relative_paths && errno == ENOENT
534 && create_directory_path(fnametmp, orig_umask) == 0) {
535 strlcpy(fnametmp, template, sizeof fnametmp);
536 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
539 rsyserr(FERROR, errno, "mkstemp %s failed",
540 full_fname(fnametmp));
541 discard_receive_data(f_in, file->length);
548 cleanup_set(fnametmp, partialptr, file, fd1, fd2);
551 /* log the transfer */
552 if (log_before_transfer)
553 log_recv(file, &initial_stats, iflags);
554 else if (!am_server && verbose && (!log_format || do_progress))
555 rprintf(FINFO, "%s\n", safe_fname(fname));
558 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
559 fname, fd2, file->length);
561 if (!log_before_transfer)
562 log_recv(file, &initial_stats, iflags);
566 if (close(fd2) < 0) {
567 rsyserr(FERROR, errno, "close failed on %s",
568 full_fname(fnametmp));
569 exit_cleanup(RERR_FILEIO);
572 if ((recv_ok && (!delay_updates || !partialptr)) || inplace) {
573 finish_transfer(fname, fnametmp, file, recv_ok, 1);
574 if (partialptr != fname && fnamecmp == partialptr) {
575 do_unlink(partialptr);
576 handle_partial_dir(partialptr, PDIR_DELETE);
578 } else if (keep_partial && partialptr
579 && handle_partial_dir(partialptr, PDIR_CREATE)) {
580 finish_transfer(partialptr, fnametmp, file, recv_ok,
582 if (delay_updates && recv_ok)
583 delayed_bits[i/8] |= 1 << (i % 8);
592 int msgtype = csum_length == SUM_LENGTH || read_batch ?
594 if (msgtype == FERROR || verbose) {
595 char *errstr, *redostr, *keptstr;
596 if (!(keep_partial && partialptr) && !inplace)
597 keptstr = "discarded";
598 else if (partial_dir)
599 keptstr = "put into partial-dir";
601 keptstr = "retained";
602 if (msgtype == FERROR) {
607 redostr = " (will try again)";
610 "%s: %s failed verification -- update %s%s.\n",
611 errstr, safe_fname(fname),
614 if (csum_length != SUM_LENGTH) {
617 send_msg(MSG_REDO, buf, 4);
621 make_backups = save_make_backups;
624 for (i = 0; i < flist->count; i++) {
625 struct file_struct *file = flist->files[i];
627 || !(delayed_bits[i/8] & (1 << (i % 8))))
629 fname = local_name ? local_name : f_name(file);
630 partialptr = partial_dir_fname(fname);
632 if (make_backups && !make_backup(fname))
635 rprintf(FINFO, "renaming %s to %s\n",
636 safe_fname(partialptr),
639 if (do_rename(partialptr, fname) < 0) {
640 rsyserr(FERROR, errno,
641 "rename failed for %s (from %s)",
643 safe_fname(partialptr));
645 handle_partial_dir(partialptr,
652 if (delete_after && !local_name && flist->count > 0)
656 rprintf(FINFO,"recv_files finished\n");