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 protocol_version;
34 extern int relative_paths;
35 extern int keep_dirlinks;
36 extern int preserve_hard_links;
37 extern int preserve_perms;
38 extern int cvs_exclude;
41 extern char *partial_dir;
42 extern char *compare_dest;
43 extern int make_backups;
44 extern int do_progress;
45 extern char *backup_dir;
46 extern char *backup_suffix;
47 extern int backup_suffix_len;
48 extern int cleanup_got_literal;
50 extern int ignore_errors;
51 extern int orig_umask;
52 extern int keep_partial;
53 extern int checksum_seed;
56 extern struct exclude_list_struct server_exclude_list;
59 static void delete_one(char *fn, int is_dir)
62 if (robust_unlink(fn) != 0) {
63 rsyserr(FERROR, errno, "delete_one: unlink %s failed",
66 rprintf(FINFO, "deleting %s\n", safe_fname(fn));
68 if (do_rmdir(fn) != 0) {
69 if (errno != ENOTEMPTY && errno != EEXIST) {
70 rsyserr(FERROR, errno,
71 "delete_one: rmdir %s failed",
75 rprintf(FINFO, "deleting directory %s\n",
82 static int is_backup_file(char *fn)
84 int k = strlen(fn) - backup_suffix_len;
85 return k > 0 && strcmp(fn+k, backup_suffix) == 0;
89 /* This deletes any files on the receiving side that are not present
90 * on the sending side. */
91 void delete_files(struct file_list *flist)
93 struct file_list *local_file_list;
95 char *argv[1], fbuf[MAXPATHLEN];
96 static int deletion_count;
101 if (io_error && !(lp_ignore_errors(module_id) || ignore_errors)) {
102 rprintf(FINFO,"IO error encountered - skipping file deletion\n");
106 for (j = 0; j < flist->count; j++) {
107 if (!(flist->files[j]->flags & FLAG_TOP_DIR)
108 || !S_ISDIR(flist->files[j]->mode))
111 argv[0] = f_name_to(flist->files[j], fbuf);
113 if (!(local_file_list = send_file_list(-1, 1, argv)))
117 rprintf(FINFO, "deleting in %s\n", safe_fname(fbuf));
119 for (i = local_file_list->count-1; i >= 0; i--) {
120 if (max_delete && deletion_count >= max_delete)
122 if (!local_file_list->files[i]->basename)
124 if (flist_find(flist,local_file_list->files[i]) < 0) {
125 char *f = f_name(local_file_list->files[i]);
126 int mode = local_file_list->files[i]->mode;
127 if (make_backups && (backup_dir || !is_backup_file(f))
131 rprintf(FINFO, "deleting %s\n",
135 delete_one(f, S_ISDIR(mode) != 0);
139 flist_free(local_file_list);
145 * get_tmpname() - create a tmp filename for a given filename
147 * If a tmpdir is defined, use that as the directory to
148 * put it in. Otherwise, the tmp filename is in the same
149 * directory as the given name. Note that there may be no
150 * directory at all in the given name!
152 * The tmp filename is basically the given filename with a
153 * dot prepended, and .XXXXXX appended (for mkstemp() to
154 * put its unique gunk in). Take care to not exceed
155 * either the MAXPATHLEN or NAME_MAX, esp. the last, as
156 * the basename basically becomes 8 chars longer. In that
157 * case, the original name is shortened sufficiently to
160 * Of course, there's no real reason for the tmp name to
161 * look like the original, except to satisfy us humans.
162 * As long as it's unique, rsync will work.
165 static int get_tmpname(char *fnametmp, char *fname)
172 /* Note: this can't overflow, so the return value is safe */
173 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
174 fnametmp[length++] = '/';
175 fnametmp[length] = '\0'; /* always NULL terminated */
178 if ((f = strrchr(fname, '/')) != NULL) {
182 /* copy up to and including the slash */
183 strlcpy(fnametmp, fname, length + 1);
187 fnametmp[length++] = '.';
188 fnametmp[length] = '\0'; /* always NULL terminated */
190 maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8);
193 rprintf(FERROR, "temporary filename too long: %s\n", fname);
198 strlcpy(fnametmp + length, f, maxname);
199 strcat(fnametmp + length, ".XXXXXX");
205 static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
206 char *fname, int fd, OFF_T total_size)
208 static char file_sum1[MD4_SUM_LENGTH];
209 static char file_sum2[MD4_SUM_LENGTH];
210 struct map_struct *mapbuf;
211 struct sum_struct sum;
219 read_sum_head(f_in, &sum);
221 if (fd_r >= 0 && size_r > 0) {
222 OFF_T map_size = MAX(sum.blength * 2, 16*1024);
223 mapbuf = map_file(fd_r, size_r, map_size, sum.blength);
225 rprintf(FINFO, "recv mapped %s of size %.0f\n",
226 safe_fname(fname_r), (double)size_r);
231 sum_init(checksum_seed);
233 while ((i = recv_token(f_in, &data)) != 0) {
235 show_progress(offset, total_size);
239 rprintf(FINFO,"data recv %d at %.0f\n",
243 stats.literal_data += i;
244 cleanup_got_literal = 1;
248 if (fd != -1 && write_file(fd,data,i) != i)
249 goto report_write_error;
255 offset2 = i*(OFF_T)sum.blength;
257 if (i == (int)sum.count-1 && sum.remainder != 0)
260 stats.matched_data += len;
263 rprintf(FINFO,"chunk[%d] of size %d at %.0f offset=%.0f\n",
264 i,len,(double)offset2,(double)offset);
267 map = map_ptr(mapbuf,offset2,len);
274 if (offset == offset2 && fd != -1) {
275 if (flush_write_file(fd) < 0)
276 goto report_write_error;
278 if (do_lseek(fd, len, SEEK_CUR) != offset) {
279 rsyserr(FERROR, errno,
280 "lseek failed on %s",
282 exit_cleanup(RERR_FILEIO);
287 if (fd != -1 && write_file(fd, map, len) != (int)len)
288 goto report_write_error;
292 flush_write_file(fd);
294 #ifdef HAVE_FTRUNCATE
295 if (inplace && fd != -1)
296 ftruncate(fd, offset);
300 end_progress(total_size);
302 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
304 rsyserr(FERROR, errno, "write failed on %s",
306 exit_cleanup(RERR_FILEIO);
314 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
316 rprintf(FINFO,"got file_sum\n");
317 if (fd != -1 && memcmp(file_sum1, file_sum2, MD4_SUM_LENGTH) != 0)
323 static void discard_receive_data(int f_in, OFF_T length)
325 receive_data(f_in, NULL, -1, 0, NULL, -1, length);
330 * main routine for receiver process.
332 * Receiver process runs on the same host as the generator process. */
333 int recv_files(int f_in, struct file_list *flist, char *local_name,
339 char *fname, fbuf[MAXPATHLEN];
340 char template[MAXPATHLEN];
341 char fnametmp[MAXPATHLEN];
342 char *fnamecmp, *partialptr;
343 char fnamecmpbuf[MAXPATHLEN];
344 struct file_struct *file;
345 struct stats initial_stats;
346 int save_make_backups = make_backups;
347 int i, recv_ok, phase = 0;
350 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
352 if (flist->hlink_pool) {
353 pool_destroy(flist->hlink_pool);
354 flist->hlink_pool = NULL;
363 if (next_gen_i != flist->count) {
367 read_byte(f_in_name);
368 } while (read_int(batch_gen_fd) != -1);
377 csum_length = SUM_LENGTH;
379 rprintf(FINFO, "recv_files phase=%d\n", phase);
380 send_msg(MSG_DONE, "", 0);
382 make_backups = 0; /* prevents double backup */
386 if (i < 0 || i >= flist->count) {
387 rprintf(FERROR,"Invalid file index %d in recv_files (count=%d)\n",
389 exit_cleanup(RERR_PROTOCOL);
392 file = flist->files[i];
394 stats.current_file_index = i;
395 stats.num_transferred_files++;
396 stats.total_transferred_size += file->length;
397 cleanup_got_literal = 0;
402 fname = f_name_to(file, fbuf);
405 if (!am_server && verbose) /* log the transfer */
406 rprintf(FINFO, "%s\n", safe_fname(fname));
410 initial_stats = stats;
413 rprintf(FINFO, "recv_files(%s)\n", safe_fname(fname));
416 while (i > next_gen_i) {
417 if (f_in_name >= 0 && next_gen_i >= 0)
418 read_byte(f_in_name);
419 next_gen_i = read_int(batch_gen_fd);
420 if (next_gen_i == -1)
421 next_gen_i = flist->count;
423 if (i < next_gen_i) {
424 rprintf(FINFO, "skipping update for \"%s\"\n",
426 discard_receive_data(f_in, file->length);
432 if (server_exclude_list.head
433 && check_exclude(&server_exclude_list, fname,
434 S_ISDIR(file->mode)) < 0) {
435 rprintf(FERROR, "attempt to hack rsync failed.\n");
436 exit_cleanup(RERR_PROTOCOL);
439 partialptr = partial_dir ? partial_dir_fname(fname) : fname;
441 if (f_in_name >= 0) {
442 switch (read_byte(f_in_name)) {
446 case FNAMECMP_PARTIAL_DIR:
447 fnamecmp = partialptr ? partialptr : fname;
449 case FNAMECMP_BACKUP:
450 fnamecmp = get_backup_name(fname);
452 case FNAMECMP_CMPDEST:
454 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
455 compare_dest, fname);
456 fnamecmp = fnamecmpbuf;
463 fd1 = do_open(fnamecmp, O_RDONLY, 0);
465 if (fd1 != -1 && do_fstat(fd1,&st) != 0) {
466 rsyserr(FERROR, errno, "fstat %s failed",
467 full_fname(fnamecmp));
468 discard_receive_data(f_in, file->length);
473 if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
474 /* this special handling for directories
475 * wouldn't be necessary if robust_rename()
476 * and the underlying robust_unlink could cope
479 rprintf(FERROR,"recv_files: %s is a directory\n",
480 full_fname(fnamecmp));
481 discard_receive_data(f_in, file->length);
486 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
491 if (fd1 != -1 && !preserve_perms) {
492 /* if the file exists already and we aren't preserving
493 * permissions then act as though the remote end sent
494 * us the file permissions we already have */
495 file->mode = st.st_mode;
498 /* We now check to see if we are writing file "inplace" */
500 fd2 = do_open(fname, O_WRONLY|O_CREAT, 0);
502 rsyserr(FERROR, errno, "open %s failed",
504 discard_receive_data(f_in, file->length);
510 if (!get_tmpname(fnametmp,fname)) {
511 discard_receive_data(f_in, file->length);
517 strlcpy(template, fnametmp, sizeof template);
519 /* we initially set the perms without the
520 * setuid/setgid bits to ensure that there is no race
521 * condition. They are then correctly updated after
522 * the lchown. Thanks to snabb@epipe.fi for pointing
523 * this out. We also set it initially without group
524 * access because of a similar race condition. */
525 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
527 /* in most cases parent directories will already exist
528 * because their information should have been previously
529 * transferred, but that may not be the case with -R */
530 if (fd2 == -1 && relative_paths && errno == ENOENT
531 && create_directory_path(fnametmp, orig_umask) == 0) {
532 strlcpy(fnametmp, template, sizeof fnametmp);
533 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
536 rsyserr(FERROR, errno, "mkstemp %s failed",
537 full_fname(fnametmp));
538 discard_receive_data(f_in, file->length);
545 cleanup_set(fnametmp, partialptr, file, fd1, fd2);
548 if (!am_server && verbose) /* log the transfer */
549 rprintf(FINFO, "%s\n", safe_fname(fname));
552 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
553 fname, fd2, file->length);
555 log_recv(file, &initial_stats);
559 if (close(fd2) < 0) {
560 rsyserr(FERROR, errno, "close failed on %s",
561 full_fname(fnametmp));
562 exit_cleanup(RERR_FILEIO);
565 if (recv_ok || inplace)
566 finish_transfer(fname, fnametmp, file, recv_ok);
567 else if (keep_partial && partialptr
568 && handle_partial_dir(partialptr, PDIR_CREATE))
569 finish_transfer(partialptr, fnametmp, file, 0);
575 if (partialptr != fname && fnamecmp == partialptr && recv_ok) {
576 do_unlink(partialptr);
577 handle_partial_dir(partialptr, PDIR_DELETE);
583 int msgtype = csum_length == SUM_LENGTH || read_batch ?
585 if (msgtype == FERROR || verbose) {
586 char *errstr, *redostr, *keptstr;
587 if (!(keep_partial && partialptr) && !inplace)
588 keptstr = "discarded";
589 else if (partial_dir)
590 keptstr = "put into partial-dir";
592 keptstr = "retained";
593 if (msgtype == FERROR) {
598 redostr = " (will try again)";
601 "%s: %s failed verification -- update %s%s.\n",
602 errstr, safe_fname(fname),
605 if (csum_length != SUM_LENGTH) {
608 send_msg(MSG_REDO, buf, 4);
612 make_backups = save_make_backups;
614 if (delete_after && recurse && !local_name && flist->count > 0)
618 rprintf(FINFO,"recv_files finished\n");