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.
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 if (!(flist->files[j]->flags & FLAG_DEL_START)
65 || !S_ISDIR(flist->files[j]->mode))
68 delete_in_dir(flist, f_name_to(flist->files[j], fbuf));
74 * get_tmpname() - create a tmp filename for a given filename
76 * If a tmpdir is defined, use that as the directory to
77 * put it in. Otherwise, the tmp filename is in the same
78 * directory as the given name. Note that there may be no
79 * directory at all in the given name!
81 * The tmp filename is basically the given filename with a
82 * dot prepended, and .XXXXXX appended (for mkstemp() to
83 * put its unique gunk in). Take care to not exceed
84 * either the MAXPATHLEN or NAME_MAX, esp. the last, as
85 * the basename basically becomes 8 chars longer. In that
86 * case, the original name is shortened sufficiently to
89 * Of course, there's no real reason for the tmp name to
90 * look like the original, except to satisfy us humans.
91 * As long as it's unique, rsync will work.
94 static int get_tmpname(char *fnametmp, char *fname)
101 /* Note: this can't overflow, so the return value is safe */
102 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
103 fnametmp[length++] = '/';
104 fnametmp[length] = '\0'; /* always NULL terminated */
107 if ((f = strrchr(fname, '/')) != NULL) {
111 /* copy up to and including the slash */
112 strlcpy(fnametmp, fname, length + 1);
116 fnametmp[length++] = '.';
117 fnametmp[length] = '\0'; /* always NULL terminated */
119 maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8);
122 rprintf(FERROR, "temporary filename too long: %s\n", fname);
127 strlcpy(fnametmp + length, f, maxname);
128 strcat(fnametmp + length, ".XXXXXX");
134 static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
135 char *fname, int fd, OFF_T total_size)
137 static char file_sum1[MD4_SUM_LENGTH];
138 static char file_sum2[MD4_SUM_LENGTH];
139 struct map_struct *mapbuf;
140 struct sum_struct sum;
148 read_sum_head(f_in, &sum);
150 if (fd_r >= 0 && size_r > 0) {
151 int32 read_size = MAX(sum.blength * 2, 16*1024);
152 mapbuf = map_file(fd_r, size_r, read_size, sum.blength);
154 rprintf(FINFO, "recv mapped %s of size %.0f\n",
155 safe_fname(fname_r), (double)size_r);
160 sum_init(checksum_seed);
162 while ((i = recv_token(f_in, &data)) != 0) {
164 show_progress(offset, total_size);
168 rprintf(FINFO,"data recv %d at %.0f\n",
172 stats.literal_data += i;
173 cleanup_got_literal = 1;
177 if (fd != -1 && write_file(fd,data,i) != i)
178 goto report_write_error;
184 offset2 = i * (OFF_T)sum.blength;
186 if (i == (int)sum.count-1 && sum.remainder != 0)
189 stats.matched_data += len;
193 "chunk[%d] of size %ld at %.0f offset=%.0f\n",
194 i, (long)len, (double)offset2, (double)offset);
198 map = map_ptr(mapbuf,offset2,len);
201 sum_update(map, len);
205 if (offset == offset2 && fd != -1) {
206 if (flush_write_file(fd) < 0)
207 goto report_write_error;
209 if (do_lseek(fd, len, SEEK_CUR) != offset) {
210 rsyserr(FERROR, errno,
211 "lseek failed on %s",
213 exit_cleanup(RERR_FILEIO);
218 if (fd != -1 && write_file(fd, map, len) != (int)len)
219 goto report_write_error;
223 if (flush_write_file(fd) < 0)
224 goto report_write_error;
227 if (inplace && fd != -1)
228 ftruncate(fd, offset);
232 end_progress(total_size);
234 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
236 rsyserr(FERROR, errno, "write failed on %s",
238 exit_cleanup(RERR_FILEIO);
246 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
248 rprintf(FINFO,"got file_sum\n");
249 if (fd != -1 && memcmp(file_sum1, file_sum2, MD4_SUM_LENGTH) != 0)
255 static void discard_receive_data(int f_in, OFF_T length)
257 receive_data(f_in, NULL, -1, 0, NULL, -1, length);
262 * main routine for receiver process.
264 * Receiver process runs on the same host as the generator process. */
265 int recv_files(int f_in, struct file_list *flist, char *local_name,
271 char *fname, fbuf[MAXPATHLEN];
272 char template[MAXPATHLEN];
273 char fnametmp[MAXPATHLEN];
274 char *fnamecmp, *partialptr;
275 char fnamecmpbuf[MAXPATHLEN];
276 uchar *delayed_bits = NULL;
277 struct file_struct *file;
278 struct stats initial_stats;
279 int save_make_backups = make_backups;
280 int i, recv_ok, phase = 0;
283 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
285 if (flist->hlink_pool) {
286 pool_destroy(flist->hlink_pool);
287 flist->hlink_pool = NULL;
291 int sz = (flist->count + 7) / 8;
292 if (!(delayed_bits = new_array(uchar, sz)))
293 out_of_memory("recv_files");
294 memset(delayed_bits, 0, sz);
303 if (next_gen_i != flist->count) {
307 read_byte(f_in_name);
308 } while (read_int(batch_gen_fd) != -1);
317 csum_length = SUM_LENGTH;
319 rprintf(FINFO, "recv_files phase=%d\n", phase);
320 send_msg(MSG_DONE, "", 0);
321 if (keep_partial && !partial_dir)
322 make_backups = 0; /* prevents double backup */
326 if (i < 0 || i >= flist->count) {
327 rprintf(FERROR,"Invalid file index %d in recv_files (count=%d)\n",
329 exit_cleanup(RERR_PROTOCOL);
332 file = flist->files[i];
334 stats.current_file_index = i;
335 stats.num_transferred_files++;
336 stats.total_transferred_size += file->length;
337 cleanup_got_literal = 0;
342 fname = f_name_to(file, fbuf);
345 if (!am_server && verbose) /* log the transfer */
346 rprintf(FINFO, "%s\n", safe_fname(fname));
350 initial_stats = stats;
353 rprintf(FINFO, "recv_files(%s)\n", safe_fname(fname));
356 while (i > next_gen_i) {
357 if (f_in_name >= 0 && next_gen_i >= 0)
358 read_byte(f_in_name);
359 next_gen_i = read_int(batch_gen_fd);
360 if (next_gen_i == -1)
361 next_gen_i = flist->count;
363 if (i < next_gen_i) {
364 rprintf(FINFO, "skipping update for \"%s\"\n",
366 discard_receive_data(f_in, file->length);
372 if (server_filter_list.head
373 && check_filter(&server_filter_list, fname,
374 S_ISDIR(file->mode)) < 0) {
375 rprintf(FERROR, "attempt to hack rsync failed.\n");
376 exit_cleanup(RERR_PROTOCOL);
379 partialptr = partial_dir ? partial_dir_fname(fname) : fname;
381 if (f_in_name >= 0) {
383 switch (j = read_byte(f_in_name)) {
387 case FNAMECMP_PARTIAL_DIR:
388 fnamecmp = partialptr ? partialptr : fname;
390 case FNAMECMP_BACKUP:
391 fnamecmp = get_backup_name(fname);
394 if (j >= basis_dir_cnt) {
396 "invalid basis_dir index: %d.\n",
398 exit_cleanup(RERR_PROTOCOL);
400 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
401 basis_dir[j], fname);
402 fnamecmp = fnamecmpbuf;
409 fd1 = do_open(fnamecmp, O_RDONLY, 0);
411 if (fd1 != -1 && do_fstat(fd1,&st) != 0) {
412 rsyserr(FERROR, errno, "fstat %s failed",
413 full_fname(fnamecmp));
414 discard_receive_data(f_in, file->length);
419 if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
420 /* this special handling for directories
421 * wouldn't be necessary if robust_rename()
422 * and the underlying robust_unlink could cope
425 rprintf(FERROR,"recv_files: %s is a directory\n",
426 full_fname(fnamecmp));
427 discard_receive_data(f_in, file->length);
432 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
437 if (fd1 != -1 && !preserve_perms) {
438 /* if the file exists already and we aren't preserving
439 * permissions then act as though the remote end sent
440 * us the file permissions we already have */
441 file->mode = st.st_mode;
444 /* We now check to see if we are writing file "inplace" */
446 fd2 = do_open(fname, O_WRONLY|O_CREAT, 0);
448 rsyserr(FERROR, errno, "open %s failed",
450 discard_receive_data(f_in, file->length);
456 if (!get_tmpname(fnametmp,fname)) {
457 discard_receive_data(f_in, file->length);
463 strlcpy(template, fnametmp, sizeof template);
465 /* we initially set the perms without the
466 * setuid/setgid bits to ensure that there is no race
467 * condition. They are then correctly updated after
468 * the lchown. Thanks to snabb@epipe.fi for pointing
469 * this out. We also set it initially without group
470 * access because of a similar race condition. */
471 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
473 /* in most cases parent directories will already exist
474 * because their information should have been previously
475 * transferred, but that may not be the case with -R */
476 if (fd2 == -1 && relative_paths && errno == ENOENT
477 && create_directory_path(fnametmp, orig_umask) == 0) {
478 strlcpy(fnametmp, template, sizeof fnametmp);
479 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
482 rsyserr(FERROR, errno, "mkstemp %s failed",
483 full_fname(fnametmp));
484 discard_receive_data(f_in, file->length);
491 cleanup_set(fnametmp, partialptr, file, fd1, fd2);
494 if (!am_server && verbose) /* log the transfer */
495 rprintf(FINFO, "%s\n", safe_fname(fname));
498 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
499 fname, fd2, file->length);
501 log_recv(file, &initial_stats);
505 if (close(fd2) < 0) {
506 rsyserr(FERROR, errno, "close failed on %s",
507 full_fname(fnametmp));
508 exit_cleanup(RERR_FILEIO);
511 if ((recv_ok && !delay_updates) || inplace) {
512 finish_transfer(fname, fnametmp, file, recv_ok, 1);
513 if (partialptr != fname && fnamecmp == partialptr) {
514 do_unlink(partialptr);
515 handle_partial_dir(partialptr, PDIR_DELETE);
517 } else if (keep_partial && partialptr
518 && handle_partial_dir(partialptr, PDIR_CREATE)) {
519 finish_transfer(partialptr, fnametmp, file, recv_ok,
521 if (delay_updates && recv_ok)
522 delayed_bits[i/8] |= 1 << (i % 8);
531 int msgtype = csum_length == SUM_LENGTH || read_batch ?
533 if (msgtype == FERROR || verbose) {
534 char *errstr, *redostr, *keptstr;
535 if (!(keep_partial && partialptr) && !inplace)
536 keptstr = "discarded";
537 else if (partial_dir)
538 keptstr = "put into partial-dir";
540 keptstr = "retained";
541 if (msgtype == FERROR) {
546 redostr = " (will try again)";
549 "%s: %s failed verification -- update %s%s.\n",
550 errstr, safe_fname(fname),
553 if (csum_length != SUM_LENGTH) {
556 send_msg(MSG_REDO, buf, 4);
560 make_backups = save_make_backups;
563 for (i = 0; i < flist->count; i++) {
564 struct file_struct *file = flist->files[i];
566 || !(delayed_bits[i/8] & (1 << (i % 8))))
568 fname = local_name ? local_name : f_name(file);
569 partialptr = partial_dir_fname(fname);
571 if (make_backups && !make_backup(fname))
574 rprintf(FINFO, "renaming %s to %s\n",
577 if (do_rename(partialptr, fname) < 0) {
578 rsyserr(FERROR, errno,
579 "rename failed for %s (from %s)",
582 handle_partial_dir(partialptr,
589 if (delete_after && recurse && !local_name && flist->count > 0)
593 rprintf(FINFO,"recv_files finished\n");