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 delete_after;
26 extern int csum_length;
27 extern struct stats stats;
29 extern int read_batch;
30 extern int batch_gen_fd;
32 extern int protocol_version;
33 extern int relative_paths;
34 extern int keep_dirlinks;
35 extern int preserve_hard_links;
36 extern int preserve_perms;
39 extern char *partial_dir;
40 extern char *basis_dir[];
41 extern int basis_dir_cnt;
42 extern int make_backups;
43 extern int do_progress;
44 extern int cleanup_got_literal;
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;
53 extern struct filter_list_struct server_filter_list;
56 /* This deletes any files on the receiving side that are not present on the
57 * sending side. This is used by --delete-before and --delete-after. */
58 void delete_files(struct file_list *flist)
60 char fbuf[MAXPATHLEN];
63 for (j = 0; j < flist->count; j++) {
64 struct file_struct *file = flist->files[j];
66 if (!(file->flags & FLAG_DEL_HERE))
69 f_name_to(file, fbuf);
70 if (verbose > 1 && file->flags & FLAG_TOP_DIR)
71 rprintf(FINFO, "deleting in %s\n", safe_fname(fbuf));
73 delete_in_dir(flist, fbuf, file);
79 * get_tmpname() - create a tmp filename for a given filename
81 * If a tmpdir is defined, use that as the directory to
82 * put it in. Otherwise, the tmp filename is in the same
83 * directory as the given name. Note that there may be no
84 * directory at all in the given name!
86 * The tmp filename is basically the given filename with a
87 * dot prepended, and .XXXXXX appended (for mkstemp() to
88 * put its unique gunk in). Take care to not exceed
89 * either the MAXPATHLEN or NAME_MAX, esp. the last, as
90 * the basename basically becomes 8 chars longer. In that
91 * case, the original name is shortened sufficiently to
94 * Of course, there's no real reason for the tmp name to
95 * look like the original, except to satisfy us humans.
96 * As long as it's unique, rsync will work.
99 static int get_tmpname(char *fnametmp, char *fname)
106 /* Note: this can't overflow, so the return value is safe */
107 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
108 fnametmp[length++] = '/';
109 fnametmp[length] = '\0'; /* always NULL terminated */
112 if ((f = strrchr(fname, '/')) != NULL) {
116 /* copy up to and including the slash */
117 strlcpy(fnametmp, fname, length + 1);
121 fnametmp[length++] = '.';
122 fnametmp[length] = '\0'; /* always NULL terminated */
124 maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8);
127 rprintf(FERROR, "temporary filename too long: %s\n",
133 strlcpy(fnametmp + length, f, maxname);
134 strcat(fnametmp + length, ".XXXXXX");
140 static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
141 char *fname, int fd, OFF_T total_size)
143 static char file_sum1[MD4_SUM_LENGTH];
144 static char file_sum2[MD4_SUM_LENGTH];
145 struct map_struct *mapbuf;
146 struct sum_struct sum;
154 read_sum_head(f_in, &sum);
156 if (fd_r >= 0 && size_r > 0) {
157 int32 read_size = MAX(sum.blength * 2, 16*1024);
158 mapbuf = map_file(fd_r, size_r, read_size, sum.blength);
160 rprintf(FINFO, "recv mapped %s of size %.0f\n",
161 safe_fname(fname_r), (double)size_r);
166 sum_init(checksum_seed);
168 while ((i = recv_token(f_in, &data)) != 0) {
170 show_progress(offset, total_size);
174 rprintf(FINFO,"data recv %d at %.0f\n",
178 stats.literal_data += i;
179 cleanup_got_literal = 1;
183 if (fd != -1 && write_file(fd,data,i) != i)
184 goto report_write_error;
190 offset2 = i * (OFF_T)sum.blength;
192 if (i == (int)sum.count-1 && sum.remainder != 0)
195 stats.matched_data += len;
199 "chunk[%d] of size %ld at %.0f offset=%.0f\n",
200 i, (long)len, (double)offset2, (double)offset);
204 map = map_ptr(mapbuf,offset2,len);
207 sum_update(map, len);
211 if (offset == offset2 && fd != -1) {
212 if (flush_write_file(fd) < 0)
213 goto report_write_error;
215 if (do_lseek(fd, len, SEEK_CUR) != offset) {
216 rsyserr(FERROR, errno,
217 "lseek failed on %s",
219 exit_cleanup(RERR_FILEIO);
224 if (fd != -1 && write_file(fd, map, len) != (int)len)
225 goto report_write_error;
229 if (flush_write_file(fd) < 0)
230 goto report_write_error;
232 #ifdef HAVE_FTRUNCATE
233 if (inplace && fd != -1)
234 ftruncate(fd, offset);
238 end_progress(total_size);
240 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
242 rsyserr(FERROR, errno, "write failed on %s",
244 exit_cleanup(RERR_FILEIO);
252 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
254 rprintf(FINFO,"got file_sum\n");
255 if (fd != -1 && memcmp(file_sum1, file_sum2, MD4_SUM_LENGTH) != 0)
261 static void read_gen_name(int fd, char *dirname, char *buf)
264 int len = read_byte(fd);
267 #if MAXPATHLEN > 32767
269 read_buf(fd, (char *)lenbuf, 2);
270 len = (len & ~0x80) * 0x10000 + lenbuf[0] * 0x100 + lenbuf[1];
272 len = (len & ~0x80) * 0x100 + read_byte(fd);
277 dlen = strlcpy(buf, dirname, MAXPATHLEN);
282 if (dlen + len >= MAXPATHLEN) {
283 rprintf(FERROR, "bogus data on generator name pipe\n");
284 exit_cleanup(RERR_PROTOCOL);
287 read_sbuf(fd, buf + dlen, len);
291 static void discard_receive_data(int f_in, OFF_T length)
293 receive_data(f_in, NULL, -1, 0, NULL, -1, length);
298 * main routine for receiver process.
300 * Receiver process runs on the same host as the generator process. */
301 int recv_files(int f_in, struct file_list *flist, char *local_name,
307 char *fname, fbuf[MAXPATHLEN];
308 char template[MAXPATHLEN];
309 char fnametmp[MAXPATHLEN];
310 char *fnamecmp, *partialptr;
311 char fnamecmpbuf[MAXPATHLEN];
312 uchar *delayed_bits = NULL;
313 struct file_struct *file;
314 struct stats initial_stats;
315 int save_make_backups = make_backups;
316 int i, recv_ok, phase = 0;
319 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
321 if (flist->hlink_pool) {
322 pool_destroy(flist->hlink_pool);
323 flist->hlink_pool = NULL;
327 int sz = (flist->count + 7) / 8;
328 if (!(delayed_bits = new_array(uchar, sz)))
329 out_of_memory("recv_files");
330 memset(delayed_bits, 0, sz);
339 if (next_gen_i != flist->count) {
343 read_byte(f_in_name);
344 } while (read_int(batch_gen_fd) != -1);
353 csum_length = SUM_LENGTH;
355 rprintf(FINFO, "recv_files phase=%d\n", phase);
356 send_msg(MSG_DONE, "", 0);
357 if (keep_partial && !partial_dir)
358 make_backups = 0; /* prevents double backup */
362 if (i < 0 || i >= flist->count) {
363 rprintf(FERROR,"Invalid file index %d in recv_files (count=%d)\n",
365 exit_cleanup(RERR_PROTOCOL);
368 file = flist->files[i];
369 if (S_ISDIR(file->mode)) {
370 rprintf(FERROR, "[%s] got index of directory: %d\n",
372 exit_cleanup(RERR_PROTOCOL);
375 stats.current_file_index = i;
376 stats.num_transferred_files++;
377 stats.total_transferred_size += file->length;
378 cleanup_got_literal = 0;
380 fname = local_name ? local_name : f_name_to(file, fbuf);
382 if (server_filter_list.head
383 && check_filter(&server_filter_list, fname, 0) < 0) {
384 rprintf(FERROR, "attempt to hack rsync failed.\n");
385 exit_cleanup(RERR_PROTOCOL);
389 rprintf(FINFO, "recv_files(%s)\n", safe_fname(fname));
391 if (dry_run) { /* log the transfer */
392 if (!am_server && verbose && !itemize_changes)
393 rprintf(FINFO, "%s\n", safe_fname(fname));
397 initial_stats = stats;
400 while (i > next_gen_i) {
401 if (f_in_name >= 0 && next_gen_i >= 0)
402 read_byte(f_in_name);
403 next_gen_i = read_int(batch_gen_fd);
404 if (next_gen_i == -1)
405 next_gen_i = flist->count;
407 if (i < next_gen_i) {
408 rprintf(FINFO, "skipping update for \"%s\"\n",
410 discard_receive_data(f_in, file->length);
416 partialptr = partial_dir ? partial_dir_fname(fname) : fname;
418 if (f_in_name >= 0) {
420 switch (j = read_byte(f_in_name)) {
424 case FNAMECMP_PARTIAL_DIR:
425 fnamecmp = partialptr ? partialptr : fname;
427 case FNAMECMP_BACKUP:
428 fnamecmp = get_backup_name(fname);
431 read_gen_name(f_in_name, file->dirname, fnamecmpbuf);
432 fnamecmp = fnamecmpbuf;
435 if (j >= basis_dir_cnt) {
437 "invalid basis_dir index: %d.\n",
439 exit_cleanup(RERR_PROTOCOL);
441 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
442 basis_dir[j], fname);
443 fnamecmp = fnamecmpbuf;
450 fd1 = do_open(fnamecmp, O_RDONLY, 0);
452 if (fd1 != -1 && do_fstat(fd1,&st) != 0) {
453 rsyserr(FERROR, errno, "fstat %s failed",
454 full_fname(fnamecmp));
455 discard_receive_data(f_in, file->length);
460 if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
461 /* this special handling for directories
462 * wouldn't be necessary if robust_rename()
463 * and the underlying robust_unlink could cope
466 rprintf(FERROR,"recv_files: %s is a directory\n",
467 full_fname(fnamecmp));
468 discard_receive_data(f_in, file->length);
473 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
478 if (fd1 != -1 && !preserve_perms) {
479 /* if the file exists already and we aren't preserving
480 * permissions then act as though the remote end sent
481 * us the file permissions we already have */
482 file->mode = st.st_mode;
485 /* We now check to see if we are writing file "inplace" */
487 fd2 = do_open(fname, O_WRONLY|O_CREAT, 0);
489 rsyserr(FERROR, errno, "open %s failed",
491 discard_receive_data(f_in, file->length);
497 if (!get_tmpname(fnametmp,fname)) {
498 discard_receive_data(f_in, file->length);
504 strlcpy(template, fnametmp, sizeof template);
506 /* we initially set the perms without the
507 * setuid/setgid bits to ensure that there is no race
508 * condition. They are then correctly updated after
509 * the lchown. Thanks to snabb@epipe.fi for pointing
510 * this out. We also set it initially without group
511 * access because of a similar race condition. */
512 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
514 /* in most cases parent directories will already exist
515 * because their information should have been previously
516 * transferred, but that may not be the case with -R */
517 if (fd2 == -1 && relative_paths && errno == ENOENT
518 && create_directory_path(fnametmp, orig_umask) == 0) {
519 strlcpy(fnametmp, template, sizeof fnametmp);
520 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
523 rsyserr(FERROR, errno, "mkstemp %s failed",
524 full_fname(fnametmp));
525 discard_receive_data(f_in, file->length);
532 cleanup_set(fnametmp, partialptr, file, fd1, fd2);
535 /* log the transfer */
536 if (!am_server && verbose && !itemize_changes)
537 rprintf(FINFO, "%s\n", safe_fname(fname));
540 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
541 fname, fd2, file->length);
543 log_recv(file, &initial_stats);
547 if (close(fd2) < 0) {
548 rsyserr(FERROR, errno, "close failed on %s",
549 full_fname(fnametmp));
550 exit_cleanup(RERR_FILEIO);
553 if ((recv_ok && (!delay_updates || !partialptr)) || inplace) {
554 finish_transfer(fname, fnametmp, file, recv_ok, 1);
555 if (partialptr != fname && fnamecmp == partialptr) {
556 do_unlink(partialptr);
557 handle_partial_dir(partialptr, PDIR_DELETE);
559 } else if (keep_partial && partialptr
560 && handle_partial_dir(partialptr, PDIR_CREATE)) {
561 finish_transfer(partialptr, fnametmp, file, recv_ok,
563 if (delay_updates && recv_ok)
564 delayed_bits[i/8] |= 1 << (i % 8);
573 int msgtype = csum_length == SUM_LENGTH || read_batch ?
575 if (msgtype == FERROR || verbose) {
576 char *errstr, *redostr, *keptstr;
577 if (!(keep_partial && partialptr) && !inplace)
578 keptstr = "discarded";
579 else if (partial_dir)
580 keptstr = "put into partial-dir";
582 keptstr = "retained";
583 if (msgtype == FERROR) {
588 redostr = " (will try again)";
591 "%s: %s failed verification -- update %s%s.\n",
592 errstr, safe_fname(fname),
595 if (csum_length != SUM_LENGTH) {
598 send_msg(MSG_REDO, buf, 4);
602 make_backups = save_make_backups;
605 for (i = 0; i < flist->count; i++) {
606 struct file_struct *file = flist->files[i];
608 || !(delayed_bits[i/8] & (1 << (i % 8))))
610 fname = local_name ? local_name : f_name(file);
611 partialptr = partial_dir_fname(fname);
613 if (make_backups && !make_backup(fname))
616 rprintf(FINFO, "renaming %s to %s\n",
617 safe_fname(partialptr),
620 if (do_rename(partialptr, fname) < 0) {
621 rsyserr(FERROR, errno,
622 "rename failed for %s (from %s)",
624 safe_fname(partialptr));
626 handle_partial_dir(partialptr,
633 if (delete_after && !local_name && flist->count > 0)
637 rprintf(FINFO,"recv_files finished\n");