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 max_delete;
27 extern int csum_length;
28 extern struct stats stats;
30 extern int read_batch;
31 extern int batch_gen_fd;
33 extern int relative_paths;
34 extern int keep_dirlinks;
35 extern int preserve_hard_links;
36 extern int preserve_perms;
37 extern int cvs_exclude;
40 extern char *partial_dir;
41 extern char *compare_dest;
42 extern int make_backups;
43 extern int do_progress;
44 extern char *backup_dir;
45 extern char *backup_suffix;
46 extern int backup_suffix_len;
47 extern int cleanup_got_literal;
49 extern int ignore_errors;
50 extern int orig_umask;
51 extern int keep_partial;
52 extern int checksum_seed;
55 extern struct exclude_list_struct server_exclude_list;
58 static void delete_one(char *fn, int is_dir)
61 if (robust_unlink(fn) != 0) {
62 rsyserr(FERROR, errno, "delete_one: unlink %s failed",
65 rprintf(FINFO, "deleting %s\n", safe_fname(fn));
67 if (do_rmdir(fn) != 0) {
68 if (errno == ENOTDIR && keep_dirlinks) {
72 if (errno != ENOTEMPTY && errno != EEXIST) {
73 rsyserr(FERROR, errno,
74 "delete_one: rmdir %s failed",
78 rprintf(FINFO, "deleting directory %s\n",
85 static int is_backup_file(char *fn)
87 int k = strlen(fn) - backup_suffix_len;
88 return k > 0 && strcmp(fn+k, backup_suffix) == 0;
92 /* This deletes any files on the receiving side that are not present
93 * on the sending side. */
94 void delete_files(struct file_list *flist)
96 struct file_list *local_file_list;
98 char *argv[1], fbuf[MAXPATHLEN];
99 static int deletion_count;
104 if (io_error && !(lp_ignore_errors(module_id) || ignore_errors)) {
105 rprintf(FINFO,"IO error encountered - skipping file deletion\n");
109 for (j = 0; j < flist->count; j++) {
110 if (!(flist->files[j]->flags & FLAG_TOP_DIR)
111 || !S_ISDIR(flist->files[j]->mode))
114 argv[0] = f_name_to(flist->files[j], fbuf);
116 if (!(local_file_list = send_file_list(-1, 1, argv)))
120 rprintf(FINFO, "deleting in %s\n", safe_fname(fbuf));
122 for (i = local_file_list->count-1; i >= 0; i--) {
123 if (max_delete && deletion_count > max_delete)
125 if (!local_file_list->files[i]->basename)
127 if (flist_find(flist,local_file_list->files[i]) < 0) {
128 char *f = f_name(local_file_list->files[i]);
129 if (make_backups && (backup_dir || !is_backup_file(f))) {
132 rprintf(FINFO, "deleting %s\n",
136 int mode = local_file_list->files[i]->mode;
137 delete_one(f, S_ISDIR(mode) != 0);
142 flist_free(local_file_list);
148 * get_tmpname() - create a tmp filename for a given filename
150 * If a tmpdir is defined, use that as the directory to
151 * put it in. Otherwise, the tmp filename is in the same
152 * directory as the given name. Note that there may be no
153 * directory at all in the given name!
155 * The tmp filename is basically the given filename with a
156 * dot prepended, and .XXXXXX appended (for mkstemp() to
157 * put its unique gunk in). Take care to not exceed
158 * either the MAXPATHLEN or NAME_MAX, esp. the last, as
159 * the basename basically becomes 8 chars longer. In that
160 * case, the original name is shortened sufficiently to
163 * Of course, there's no real reason for the tmp name to
164 * look like the original, except to satisfy us humans.
165 * As long as it's unique, rsync will work.
168 static int get_tmpname(char *fnametmp, char *fname)
175 /* Note: this can't overflow, so the return value is safe */
176 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
177 fnametmp[length++] = '/';
178 fnametmp[length] = '\0'; /* always NULL terminated */
181 if ((f = strrchr(fname, '/')) != NULL) {
185 /* copy up to and including the slash */
186 strlcpy(fnametmp, fname, length + 1);
190 fnametmp[length++] = '.';
191 fnametmp[length] = '\0'; /* always NULL terminated */
193 maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8);
196 rprintf(FERROR, "temporary filename too long: %s\n", fname);
201 strlcpy(fnametmp + length, f, maxname);
202 strcat(fnametmp + length, ".XXXXXX");
208 static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
209 char *fname, int fd, OFF_T total_size)
211 static char file_sum1[MD4_SUM_LENGTH];
212 static char file_sum2[MD4_SUM_LENGTH];
213 struct map_struct *mapbuf;
214 struct sum_struct sum;
222 read_sum_head(f_in, &sum);
224 if (fd_r >= 0 && size_r > 0) {
225 OFF_T map_size = MAX(sum.blength * 2, 16*1024);
226 mapbuf = map_file(fd_r, size_r, map_size, sum.blength);
228 rprintf(FINFO, "recv mapped %s of size %.0f\n",
229 safe_fname(fname_r), (double)size_r);
234 sum_init(checksum_seed);
236 while ((i = recv_token(f_in, &data)) != 0) {
238 show_progress(offset, total_size);
242 rprintf(FINFO,"data recv %d at %.0f\n",
246 stats.literal_data += i;
247 cleanup_got_literal = 1;
251 if (fd != -1 && write_file(fd,data,i) != i)
252 goto report_write_error;
258 offset2 = i*(OFF_T)sum.blength;
260 if (i == (int)sum.count-1 && sum.remainder != 0)
263 stats.matched_data += len;
266 rprintf(FINFO,"chunk[%d] of size %d at %.0f offset=%.0f\n",
267 i,len,(double)offset2,(double)offset);
270 map = map_ptr(mapbuf,offset2,len);
277 if (offset == offset2 && fd != -1) {
278 if (flush_write_file(fd) < 0)
279 goto report_write_error;
281 if (do_lseek(fd, len, SEEK_CUR) != offset) {
282 rsyserr(FERROR, errno,
283 "lseek failed on %s",
285 exit_cleanup(RERR_FILEIO);
290 if (fd != -1 && write_file(fd, map, len) != (int)len)
291 goto report_write_error;
295 flush_write_file(fd);
297 #ifdef HAVE_FTRUNCATE
298 if (inplace && fd != -1)
299 ftruncate(fd, offset);
303 end_progress(total_size);
305 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
307 rsyserr(FERROR, errno, "write failed on %s",
309 exit_cleanup(RERR_FILEIO);
317 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
319 rprintf(FINFO,"got file_sum\n");
320 if (fd != -1 && memcmp(file_sum1, file_sum2, MD4_SUM_LENGTH) != 0)
326 static void discard_receive_data(int f_in, OFF_T length)
328 receive_data(f_in, NULL, -1, 0, NULL, -1, length);
333 * main routine for receiver process.
335 * Receiver process runs on the same host as the generator process. */
336 int recv_files(int f_in, struct file_list *flist, char *local_name)
341 char *fname, fbuf[MAXPATHLEN];
342 char template[MAXPATHLEN];
343 char fnametmp[MAXPATHLEN];
344 char *fnamecmp, *partialptr;
345 char fnamecmpbuf[MAXPATHLEN];
346 struct file_struct *file;
347 struct stats initial_stats;
348 int save_make_backups = make_backups;
349 int i, recv_ok, phase = 0;
352 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
354 if (flist->hlink_pool) {
355 pool_destroy(flist->hlink_pool);
356 flist->hlink_pool = NULL;
365 if (next_gen_i != flist->count)
366 while (read_int(batch_gen_fd) != -1) {}
374 csum_length = SUM_LENGTH;
376 rprintf(FINFO, "recv_files phase=%d\n", phase);
377 send_msg(MSG_DONE, "", 0);
379 make_backups = 0; /* prevents double backup */
383 if (i < 0 || i >= flist->count) {
384 rprintf(FERROR,"Invalid file index %d in recv_files (count=%d)\n",
386 exit_cleanup(RERR_PROTOCOL);
389 file = flist->files[i];
391 stats.current_file_index = i;
392 stats.num_transferred_files++;
393 stats.total_transferred_size += file->length;
394 cleanup_got_literal = 0;
399 fname = f_name_to(file, fbuf);
402 if (!am_server && verbose) /* log the transfer */
403 rprintf(FINFO, "%s\n", safe_fname(fname));
407 initial_stats = stats;
410 rprintf(FINFO, "recv_files(%s)\n", safe_fname(fname));
413 while (i > next_gen_i) {
414 next_gen_i = read_int(batch_gen_fd);
415 if (next_gen_i == -1)
416 next_gen_i = flist->count;
418 if (i < next_gen_i) {
419 rprintf(FINFO, "skipping update for \"%s\"\n",
421 discard_receive_data(f_in, file->length);
426 if (server_exclude_list.head
427 && check_exclude(&server_exclude_list, fname,
428 S_ISDIR(file->mode)) < 0) {
429 rprintf(FERROR, "attempt to hack rsync failed.\n");
430 exit_cleanup(RERR_PROTOCOL);
434 if ((partialptr = partial_dir_fname(fname)) != NULL)
435 fnamecmp = partialptr;
439 fnamecmp = partialptr = fname;
442 fd1 = do_open(fnamecmp, O_RDONLY, 0);
444 if (fd1 == -1 && fnamecmp != fname) {
446 fd1 = do_open(fnamecmp, O_RDONLY, 0);
449 if (fd1 == -1 && compare_dest != NULL) {
450 /* try the file at compare_dest instead */
451 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
452 compare_dest, fname);
453 fnamecmp = fnamecmpbuf;
454 fd1 = do_open(fnamecmp, O_RDONLY, 0);
457 if (fd1 != -1 && do_fstat(fd1,&st) != 0) {
458 rsyserr(FERROR, errno, "fstat %s failed",
459 full_fname(fnamecmp));
460 discard_receive_data(f_in, file->length);
465 if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
466 /* this special handling for directories
467 * wouldn't be necessary if robust_rename()
468 * and the underlying robust_unlink could cope
471 rprintf(FERROR,"recv_files: %s is a directory\n",
472 full_fname(fnamecmp));
473 discard_receive_data(f_in, file->length);
478 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
483 if (fd1 != -1 && !preserve_perms) {
484 /* if the file exists already and we aren't preserving
485 * permissions then act as though the remote end sent
486 * us the file permissions we already have */
487 file->mode = st.st_mode;
490 /* We now check to see if we are writing file "inplace" */
492 fd2 = do_open(fnamecmp, O_WRONLY|O_CREAT, 0);
494 rsyserr(FERROR, errno, "open %s failed",
495 full_fname(fnamecmp));
496 discard_receive_data(f_in, file->length);
502 if (!get_tmpname(fnametmp,fname)) {
503 discard_receive_data(f_in, file->length);
509 strlcpy(template, fnametmp, sizeof template);
511 /* we initially set the perms without the
512 * setuid/setgid bits to ensure that there is no race
513 * condition. They are then correctly updated after
514 * the lchown. Thanks to snabb@epipe.fi for pointing
515 * this out. We also set it initially without group
516 * access because of a similar race condition. */
517 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
519 /* in most cases parent directories will already exist
520 * because their information should have been previously
521 * transferred, but that may not be the case with -R */
522 if (fd2 == -1 && relative_paths && errno == ENOENT
523 && create_directory_path(fnametmp, orig_umask) == 0) {
524 strlcpy(fnametmp, template, sizeof fnametmp);
525 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
528 rsyserr(FERROR, errno, "mkstemp %s failed",
529 full_fname(fnametmp));
530 discard_receive_data(f_in, file->length);
537 cleanup_set(fnametmp, partialptr, file, fd1, fd2);
540 if (!am_server && verbose) /* log the transfer */
541 rprintf(FINFO, "%s\n", safe_fname(fname));
544 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
545 fname, fd2, file->length);
547 log_recv(file, &initial_stats);
551 if (close(fd2) < 0) {
552 rsyserr(FERROR, errno, "close failed on %s",
553 full_fname(fnametmp));
554 exit_cleanup(RERR_FILEIO);
557 if (recv_ok || inplace)
558 finish_transfer(fname, fnametmp, file, recv_ok);
559 else if (keep_partial && partialptr
560 && handle_partial_dir(partialptr, PDIR_CREATE))
561 finish_transfer(partialptr, fnametmp, file, 0);
567 if (partialptr != fname && fnamecmp == partialptr && recv_ok) {
568 do_unlink(partialptr);
569 handle_partial_dir(partialptr, PDIR_DELETE);
575 int msgtype = csum_length == SUM_LENGTH || read_batch ?
577 if (msgtype == FERROR || verbose) {
578 char *errstr, *redostr, *keptstr;
579 if (!(keep_partial && partialptr) && !inplace)
580 keptstr = "discarded";
581 else if (partial_dir)
582 keptstr = "put into partial-dir";
584 keptstr = "retained";
585 if (msgtype == FERROR) {
590 redostr = " (will try again)";
593 "%s: %s failed verification -- update %s%s.\n",
594 errstr, safe_fname(fname),
597 if (csum_length != SUM_LENGTH) {
600 send_msg(MSG_REDO, buf, 4);
604 make_backups = save_make_backups;
606 if (delete_after && recurse && !local_name && flist->count > 0)
610 rprintf(FINFO,"recv_files finished\n");