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 log_before_transfer;
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;
52 extern char *log_format;
54 extern struct filter_list_struct server_filter_list;
57 /* This deletes any files on the receiving side that are not present on the
58 * sending side. This is used by --delete-before and --delete-after. */
59 void delete_files(struct file_list *flist)
61 char fbuf[MAXPATHLEN];
64 for (j = 0; j < flist->count; j++) {
65 struct file_struct *file = flist->files[j];
67 if (!(file->flags & FLAG_DEL_HERE))
70 f_name_to(file, fbuf);
71 if (verbose > 1 && file->flags & FLAG_TOP_DIR)
72 rprintf(FINFO, "deleting in %s\n", safe_fname(fbuf));
74 delete_in_dir(flist, fbuf, file);
80 * get_tmpname() - create a tmp filename for a given filename
82 * If a tmpdir is defined, use that as the directory to
83 * put it in. Otherwise, the tmp filename is in the same
84 * directory as the given name. Note that there may be no
85 * directory at all in the given name!
87 * The tmp filename is basically the given filename with a
88 * dot prepended, and .XXXXXX appended (for mkstemp() to
89 * put its unique gunk in). Take care to not exceed
90 * either the MAXPATHLEN or NAME_MAX, esp. the last, as
91 * the basename basically becomes 8 chars longer. In that
92 * case, the original name is shortened sufficiently to
95 * Of course, there's no real reason for the tmp name to
96 * look like the original, except to satisfy us humans.
97 * As long as it's unique, rsync will work.
100 static int get_tmpname(char *fnametmp, char *fname)
107 /* Note: this can't overflow, so the return value is safe */
108 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
109 fnametmp[length++] = '/';
110 fnametmp[length] = '\0'; /* always NULL terminated */
113 if ((f = strrchr(fname, '/')) != NULL) {
117 /* copy up to and including the slash */
118 strlcpy(fnametmp, fname, length + 1);
122 fnametmp[length++] = '.';
123 fnametmp[length] = '\0'; /* always NULL terminated */
125 maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8);
128 rprintf(FERROR, "temporary filename too long: %s\n",
134 strlcpy(fnametmp + length, f, maxname);
135 strcat(fnametmp + length, ".XXXXXX");
141 static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
142 char *fname, int fd, OFF_T total_size)
144 static char file_sum1[MD4_SUM_LENGTH];
145 static char file_sum2[MD4_SUM_LENGTH];
146 struct map_struct *mapbuf;
147 struct sum_struct sum;
155 read_sum_head(f_in, &sum);
157 if (fd_r >= 0 && size_r > 0) {
158 int32 read_size = MAX(sum.blength * 2, 16*1024);
159 mapbuf = map_file(fd_r, size_r, read_size, sum.blength);
161 rprintf(FINFO, "recv mapped %s of size %.0f\n",
162 safe_fname(fname_r), (double)size_r);
167 sum_init(checksum_seed);
169 while ((i = recv_token(f_in, &data)) != 0) {
171 show_progress(offset, total_size);
175 rprintf(FINFO,"data recv %d at %.0f\n",
179 stats.literal_data += i;
180 cleanup_got_literal = 1;
184 if (fd != -1 && write_file(fd,data,i) != i)
185 goto report_write_error;
191 offset2 = i * (OFF_T)sum.blength;
193 if (i == (int)sum.count-1 && sum.remainder != 0)
196 stats.matched_data += len;
200 "chunk[%d] of size %ld at %.0f offset=%.0f\n",
201 i, (long)len, (double)offset2, (double)offset);
205 map = map_ptr(mapbuf,offset2,len);
208 sum_update(map, len);
212 if (offset == offset2 && fd != -1) {
213 if (flush_write_file(fd) < 0)
214 goto report_write_error;
216 if (do_lseek(fd, len, SEEK_CUR) != offset) {
217 rsyserr(FERROR, errno,
218 "lseek failed on %s",
220 exit_cleanup(RERR_FILEIO);
225 if (fd != -1 && write_file(fd, map, len) != (int)len)
226 goto report_write_error;
230 if (flush_write_file(fd) < 0)
231 goto report_write_error;
233 #ifdef HAVE_FTRUNCATE
234 if (inplace && fd != -1)
235 ftruncate(fd, offset);
239 end_progress(total_size);
241 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
243 rsyserr(FERROR, errno, "write failed on %s",
245 exit_cleanup(RERR_FILEIO);
253 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
255 rprintf(FINFO,"got file_sum\n");
256 if (fd != -1 && memcmp(file_sum1, file_sum2, MD4_SUM_LENGTH) != 0)
262 static void read_gen_name(int fd, char *dirname, char *buf)
265 int len = read_byte(fd);
268 #if MAXPATHLEN > 32767
270 read_buf(fd, (char *)lenbuf, 2);
271 len = (len & ~0x80) * 0x10000 + lenbuf[0] * 0x100 + lenbuf[1];
273 len = (len & ~0x80) * 0x100 + read_byte(fd);
278 dlen = strlcpy(buf, dirname, MAXPATHLEN);
283 if (dlen + len >= MAXPATHLEN) {
284 rprintf(FERROR, "bogus data on generator name pipe\n");
285 exit_cleanup(RERR_PROTOCOL);
288 read_sbuf(fd, buf + dlen, len);
292 static void discard_receive_data(int f_in, OFF_T length)
294 receive_data(f_in, NULL, -1, 0, NULL, -1, length);
299 * main routine for receiver process.
301 * Receiver process runs on the same host as the generator process. */
302 int recv_files(int f_in, struct file_list *flist, char *local_name,
309 char *fname, fbuf[MAXPATHLEN];
310 char template[MAXPATHLEN];
311 char fnametmp[MAXPATHLEN];
312 char *fnamecmp, *partialptr;
313 char fnamecmpbuf[MAXPATHLEN];
314 uchar *delayed_bits = NULL;
315 struct file_struct *file;
316 struct stats initial_stats;
317 int save_make_backups = make_backups;
318 int i, recv_ok, phase = 0;
321 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
323 if (flist->hlink_pool) {
324 pool_destroy(flist->hlink_pool);
325 flist->hlink_pool = NULL;
329 int sz = (flist->count + 7) / 8;
330 if (!(delayed_bits = new_array(uchar, sz)))
331 out_of_memory("recv_files");
332 memset(delayed_bits, 0, sz);
341 if (next_gen_i != flist->count) {
345 read_byte(f_in_name);
346 } while (read_int(batch_gen_fd) != -1);
355 csum_length = SUM_LENGTH;
357 rprintf(FINFO, "recv_files phase=%d\n", phase);
358 send_msg(MSG_DONE, "", 0);
359 if (keep_partial && !partial_dir)
360 make_backups = 0; /* prevents double backup */
364 if (i < 0 || i >= flist->count) {
365 rprintf(FERROR,"Invalid file index %d in recv_files (count=%d)\n",
367 exit_cleanup(RERR_PROTOCOL);
370 file = flist->files[i];
372 if (protocol_version >= 29) {
373 iflags = read_byte(f_in);
374 iflags |= read_byte(f_in) << 8;
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 && log_format)
407 log_recv(file, &stats, iflags);
412 while (i > next_gen_i) {
413 if (f_in_name >= 0 && next_gen_i >= 0)
414 read_byte(f_in_name);
415 next_gen_i = read_int(batch_gen_fd);
416 if (next_gen_i == -1)
417 next_gen_i = flist->count;
419 if (i < next_gen_i) {
420 rprintf(FINFO, "skipping update for \"%s\"\n",
422 discard_receive_data(f_in, file->length);
428 partialptr = partial_dir ? partial_dir_fname(fname) : fname;
430 if (f_in_name >= 0) {
432 switch (j = read_byte(f_in_name)) {
436 case FNAMECMP_PARTIAL_DIR:
437 fnamecmp = partialptr ? partialptr : fname;
439 case FNAMECMP_BACKUP:
440 fnamecmp = get_backup_name(fname);
443 read_gen_name(f_in_name, file->dirname, fnamecmpbuf);
444 fnamecmp = fnamecmpbuf;
447 if (j >= basis_dir_cnt) {
449 "invalid basis_dir index: %d.\n",
451 exit_cleanup(RERR_PROTOCOL);
453 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
454 basis_dir[j], fname);
455 fnamecmp = fnamecmpbuf;
461 initial_stats = stats;
464 fd1 = do_open(fnamecmp, O_RDONLY, 0);
466 if (fd1 != -1 && do_fstat(fd1,&st) != 0) {
467 rsyserr(FERROR, errno, "fstat %s failed",
468 full_fname(fnamecmp));
469 discard_receive_data(f_in, file->length);
474 if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
475 /* this special handling for directories
476 * wouldn't be necessary if robust_rename()
477 * and the underlying robust_unlink could cope
480 rprintf(FERROR,"recv_files: %s is a directory\n",
481 full_fname(fnamecmp));
482 discard_receive_data(f_in, file->length);
487 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
492 if (fd1 != -1 && !preserve_perms) {
493 /* if the file exists already and we aren't preserving
494 * permissions then act as though the remote end sent
495 * us the file permissions we already have */
496 file->mode = st.st_mode;
499 /* We now check to see if we are writing file "inplace" */
501 fd2 = do_open(fname, O_WRONLY|O_CREAT, 0);
503 rsyserr(FERROR, errno, "open %s failed",
505 discard_receive_data(f_in, file->length);
511 if (!get_tmpname(fnametmp,fname)) {
512 discard_receive_data(f_in, file->length);
518 strlcpy(template, fnametmp, sizeof template);
520 /* we initially set the perms without the
521 * setuid/setgid bits to ensure that there is no race
522 * condition. They are then correctly updated after
523 * the lchown. Thanks to snabb@epipe.fi for pointing
524 * this out. We also set it initially without group
525 * access because of a similar race condition. */
526 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
528 /* in most cases parent directories will already exist
529 * because their information should have been previously
530 * transferred, but that may not be the case with -R */
531 if (fd2 == -1 && relative_paths && errno == ENOENT
532 && create_directory_path(fnametmp, orig_umask) == 0) {
533 strlcpy(fnametmp, template, sizeof fnametmp);
534 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
537 rsyserr(FERROR, errno, "mkstemp %s failed",
538 full_fname(fnametmp));
539 discard_receive_data(f_in, file->length);
546 cleanup_set(fnametmp, partialptr, file, fd1, fd2);
549 /* log the transfer */
550 if (log_before_transfer)
551 log_recv(file, &initial_stats, iflags);
552 else if (!am_server && verbose && do_progress)
553 rprintf(FINFO, "%s\n", safe_fname(fname));
556 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
557 fname, fd2, file->length);
559 if (!log_before_transfer)
560 log_recv(file, &initial_stats, iflags);
564 if (close(fd2) < 0) {
565 rsyserr(FERROR, errno, "close failed on %s",
566 full_fname(fnametmp));
567 exit_cleanup(RERR_FILEIO);
570 if ((recv_ok && (!delay_updates || !partialptr)) || inplace) {
571 finish_transfer(fname, fnametmp, file, recv_ok, 1);
572 if (partialptr != fname && fnamecmp == partialptr) {
573 do_unlink(partialptr);
574 handle_partial_dir(partialptr, PDIR_DELETE);
576 } else if (keep_partial && partialptr
577 && handle_partial_dir(partialptr, PDIR_CREATE)) {
578 finish_transfer(partialptr, fnametmp, file, recv_ok,
580 if (delay_updates && recv_ok)
581 delayed_bits[i/8] |= 1 << (i % 8);
590 int msgtype = csum_length == SUM_LENGTH || read_batch ?
592 if (msgtype == FERROR || verbose) {
593 char *errstr, *redostr, *keptstr;
594 if (!(keep_partial && partialptr) && !inplace)
595 keptstr = "discarded";
596 else if (partial_dir)
597 keptstr = "put into partial-dir";
599 keptstr = "retained";
600 if (msgtype == FERROR) {
605 redostr = " (will try again)";
608 "%s: %s failed verification -- update %s%s.\n",
609 errstr, safe_fname(fname),
612 if (csum_length != SUM_LENGTH) {
615 send_msg(MSG_REDO, buf, 4);
619 make_backups = save_make_backups;
622 for (i = 0; i < flist->count; i++) {
623 struct file_struct *file = flist->files[i];
625 || !(delayed_bits[i/8] & (1 << (i % 8))))
627 fname = local_name ? local_name : f_name(file);
628 partialptr = partial_dir_fname(fname);
630 if (make_backups && !make_backup(fname))
633 rprintf(FINFO, "renaming %s to %s\n",
634 safe_fname(partialptr),
637 if (do_rename(partialptr, fname) < 0) {
638 rsyserr(FERROR, errno,
639 "rename failed for %s (from %s)",
641 safe_fname(partialptr));
643 handle_partial_dir(partialptr,
650 if (delete_after && !local_name && flist->count > 0)
654 rprintf(FINFO,"recv_files finished\n");