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 *basis_dir[];
43 extern int basis_dir_cnt;
44 extern int make_backups;
45 extern int do_progress;
46 extern char *backup_dir;
47 extern char *backup_suffix;
48 extern int backup_suffix_len;
49 extern int cleanup_got_literal;
51 extern int ignore_errors;
52 extern int orig_umask;
53 extern int keep_partial;
54 extern int checksum_seed;
57 extern struct exclude_list_struct server_exclude_list;
60 static void delete_one(char *fn, int is_dir)
63 if (robust_unlink(fn) != 0) {
64 rsyserr(FERROR, errno, "delete_one: unlink %s failed",
67 rprintf(FINFO, "deleting %s\n", safe_fname(fn));
69 if (do_rmdir(fn) != 0) {
70 if (errno != ENOTEMPTY && errno != EEXIST) {
71 rsyserr(FERROR, errno,
72 "delete_one: rmdir %s failed",
76 rprintf(FINFO, "deleting directory %s\n",
83 static int is_backup_file(char *fn)
85 int k = strlen(fn) - backup_suffix_len;
86 return k > 0 && strcmp(fn+k, backup_suffix) == 0;
90 /* This deletes any files on the receiving side that are not present
91 * on the sending side. */
92 void delete_files(struct file_list *flist)
94 struct file_list *local_file_list;
96 char *argv[1], fbuf[MAXPATHLEN];
97 static int deletion_count;
102 if (io_error && !(lp_ignore_errors(module_id) || ignore_errors)) {
103 rprintf(FINFO,"IO error encountered - skipping file deletion\n");
107 for (j = 0; j < flist->count; j++) {
108 if (!(flist->files[j]->flags & FLAG_TOP_DIR)
109 || !S_ISDIR(flist->files[j]->mode))
112 argv[0] = f_name_to(flist->files[j], fbuf);
114 if (!(local_file_list = send_file_list(-1, 1, argv)))
118 rprintf(FINFO, "deleting in %s\n", safe_fname(fbuf));
120 for (i = local_file_list->count-1; i >= 0; i--) {
121 if (max_delete && deletion_count >= max_delete)
123 if (!local_file_list->files[i]->basename)
125 if (flist_find(flist,local_file_list->files[i]) < 0) {
126 char *f = f_name(local_file_list->files[i]);
127 int mode = local_file_list->files[i]->mode;
128 if (make_backups && (backup_dir || !is_backup_file(f))
132 rprintf(FINFO, "deleting %s\n",
136 delete_one(f, S_ISDIR(mode) != 0);
140 flist_free(local_file_list);
146 * get_tmpname() - create a tmp filename for a given filename
148 * If a tmpdir is defined, use that as the directory to
149 * put it in. Otherwise, the tmp filename is in the same
150 * directory as the given name. Note that there may be no
151 * directory at all in the given name!
153 * The tmp filename is basically the given filename with a
154 * dot prepended, and .XXXXXX appended (for mkstemp() to
155 * put its unique gunk in). Take care to not exceed
156 * either the MAXPATHLEN or NAME_MAX, esp. the last, as
157 * the basename basically becomes 8 chars longer. In that
158 * case, the original name is shortened sufficiently to
161 * Of course, there's no real reason for the tmp name to
162 * look like the original, except to satisfy us humans.
163 * As long as it's unique, rsync will work.
166 static int get_tmpname(char *fnametmp, char *fname)
173 /* Note: this can't overflow, so the return value is safe */
174 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
175 fnametmp[length++] = '/';
176 fnametmp[length] = '\0'; /* always NULL terminated */
179 if ((f = strrchr(fname, '/')) != NULL) {
183 /* copy up to and including the slash */
184 strlcpy(fnametmp, fname, length + 1);
188 fnametmp[length++] = '.';
189 fnametmp[length] = '\0'; /* always NULL terminated */
191 maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8);
194 rprintf(FERROR, "temporary filename too long: %s\n", fname);
199 strlcpy(fnametmp + length, f, maxname);
200 strcat(fnametmp + length, ".XXXXXX");
206 static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
207 char *fname, int fd, OFF_T total_size)
209 static char file_sum1[MD4_SUM_LENGTH];
210 static char file_sum2[MD4_SUM_LENGTH];
211 struct map_struct *mapbuf;
212 struct sum_struct sum;
220 read_sum_head(f_in, &sum);
222 if (fd_r >= 0 && size_r > 0) {
223 OFF_T map_size = MAX((OFF_T)sum.blength * 2, 16*1024);
224 mapbuf = map_file(fd_r, size_r, map_size, sum.blength);
226 rprintf(FINFO, "recv mapped %s of size %.0f\n",
227 safe_fname(fname_r), (double)size_r);
232 sum_init(checksum_seed);
234 while ((i = recv_token(f_in, &data)) != 0) {
236 show_progress(offset, total_size);
240 rprintf(FINFO,"data recv %d at %.0f\n",
244 stats.literal_data += i;
245 cleanup_got_literal = 1;
249 if (fd != -1 && write_file(fd,data,i) != i)
250 goto report_write_error;
256 offset2 = i * (OFF_T)sum.blength;
258 if (i == (int)sum.count-1 && sum.remainder != 0)
261 stats.matched_data += len;
265 "chunk[%d] of size %ld at %.0f offset=%.0f\n",
266 i, (long)len, (double)offset2, (double)offset);
270 map = map_ptr(mapbuf,offset2,len);
273 sum_update(map, 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 if (flush_write_file(fd) < 0)
296 goto report_write_error;
298 #ifdef HAVE_FTRUNCATE
299 if (inplace && fd != -1)
300 ftruncate(fd, offset);
304 end_progress(total_size);
306 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
308 rsyserr(FERROR, errno, "write failed on %s",
310 exit_cleanup(RERR_FILEIO);
318 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
320 rprintf(FINFO,"got file_sum\n");
321 if (fd != -1 && memcmp(file_sum1, file_sum2, MD4_SUM_LENGTH) != 0)
327 static void discard_receive_data(int f_in, OFF_T length)
329 receive_data(f_in, NULL, -1, 0, NULL, -1, length);
334 * main routine for receiver process.
336 * Receiver process runs on the same host as the generator process. */
337 int recv_files(int f_in, struct file_list *flist, char *local_name,
343 char *fname, fbuf[MAXPATHLEN];
344 char template[MAXPATHLEN];
345 char fnametmp[MAXPATHLEN];
346 char *fnamecmp, *partialptr;
347 char fnamecmpbuf[MAXPATHLEN];
348 struct file_struct *file;
349 struct stats initial_stats;
350 int save_make_backups = make_backups;
351 int i, recv_ok, phase = 0;
354 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
356 if (flist->hlink_pool) {
357 pool_destroy(flist->hlink_pool);
358 flist->hlink_pool = NULL;
367 if (next_gen_i != flist->count) {
371 read_byte(f_in_name);
372 } while (read_int(batch_gen_fd) != -1);
381 csum_length = SUM_LENGTH;
383 rprintf(FINFO, "recv_files phase=%d\n", phase);
384 send_msg(MSG_DONE, "", 0);
385 if (keep_partial && !partial_dir)
386 make_backups = 0; /* prevents double backup */
390 if (i < 0 || i >= flist->count) {
391 rprintf(FERROR,"Invalid file index %d in recv_files (count=%d)\n",
393 exit_cleanup(RERR_PROTOCOL);
396 file = flist->files[i];
398 stats.current_file_index = i;
399 stats.num_transferred_files++;
400 stats.total_transferred_size += file->length;
401 cleanup_got_literal = 0;
406 fname = f_name_to(file, fbuf);
409 if (!am_server && verbose) /* log the transfer */
410 rprintf(FINFO, "%s\n", safe_fname(fname));
414 initial_stats = stats;
417 rprintf(FINFO, "recv_files(%s)\n", safe_fname(fname));
420 while (i > next_gen_i) {
421 if (f_in_name >= 0 && next_gen_i >= 0)
422 read_byte(f_in_name);
423 next_gen_i = read_int(batch_gen_fd);
424 if (next_gen_i == -1)
425 next_gen_i = flist->count;
427 if (i < next_gen_i) {
428 rprintf(FINFO, "skipping update for \"%s\"\n",
430 discard_receive_data(f_in, file->length);
436 if (server_exclude_list.head
437 && check_exclude(&server_exclude_list, fname,
438 S_ISDIR(file->mode)) < 0) {
439 rprintf(FERROR, "attempt to hack rsync failed.\n");
440 exit_cleanup(RERR_PROTOCOL);
443 partialptr = partial_dir ? partial_dir_fname(fname) : fname;
445 if (f_in_name >= 0) {
447 switch (j = read_byte(f_in_name)) {
451 case FNAMECMP_PARTIAL_DIR:
452 fnamecmp = partialptr ? partialptr : fname;
454 case FNAMECMP_BACKUP:
455 fnamecmp = get_backup_name(fname);
458 if (j >= basis_dir_cnt) {
460 "invalid basis_dir index: %d.\n",
462 exit_cleanup(RERR_PROTOCOL);
464 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
465 basis_dir[j], fname);
466 fnamecmp = fnamecmpbuf;
473 fd1 = do_open(fnamecmp, O_RDONLY, 0);
475 if (fd1 != -1 && do_fstat(fd1,&st) != 0) {
476 rsyserr(FERROR, errno, "fstat %s failed",
477 full_fname(fnamecmp));
478 discard_receive_data(f_in, file->length);
483 if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
484 /* this special handling for directories
485 * wouldn't be necessary if robust_rename()
486 * and the underlying robust_unlink could cope
489 rprintf(FERROR,"recv_files: %s is a directory\n",
490 full_fname(fnamecmp));
491 discard_receive_data(f_in, file->length);
496 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
501 if (fd1 != -1 && !preserve_perms) {
502 /* if the file exists already and we aren't preserving
503 * permissions then act as though the remote end sent
504 * us the file permissions we already have */
505 file->mode = st.st_mode;
508 /* We now check to see if we are writing file "inplace" */
510 fd2 = do_open(fname, O_WRONLY|O_CREAT, 0);
512 rsyserr(FERROR, errno, "open %s failed",
514 discard_receive_data(f_in, file->length);
520 if (!get_tmpname(fnametmp,fname)) {
521 discard_receive_data(f_in, file->length);
527 strlcpy(template, fnametmp, sizeof template);
529 /* we initially set the perms without the
530 * setuid/setgid bits to ensure that there is no race
531 * condition. They are then correctly updated after
532 * the lchown. Thanks to snabb@epipe.fi for pointing
533 * this out. We also set it initially without group
534 * access because of a similar race condition. */
535 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
537 /* in most cases parent directories will already exist
538 * because their information should have been previously
539 * transferred, but that may not be the case with -R */
540 if (fd2 == -1 && relative_paths && errno == ENOENT
541 && create_directory_path(fnametmp, orig_umask) == 0) {
542 strlcpy(fnametmp, template, sizeof fnametmp);
543 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
546 rsyserr(FERROR, errno, "mkstemp %s failed",
547 full_fname(fnametmp));
548 discard_receive_data(f_in, file->length);
555 cleanup_set(fnametmp, partialptr, file, fd1, fd2);
558 if (!am_server && verbose) /* log the transfer */
559 rprintf(FINFO, "%s\n", safe_fname(fname));
562 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
563 fname, fd2, file->length);
565 log_recv(file, &initial_stats);
569 if (close(fd2) < 0) {
570 rsyserr(FERROR, errno, "close failed on %s",
571 full_fname(fnametmp));
572 exit_cleanup(RERR_FILEIO);
575 if (recv_ok || inplace) {
576 finish_transfer(fname, fnametmp, file, recv_ok, 1);
577 if (partialptr != fname && fnamecmp == partialptr) {
578 do_unlink(partialptr);
579 handle_partial_dir(partialptr, PDIR_DELETE);
581 } else if (keep_partial && partialptr
582 && handle_partial_dir(partialptr, PDIR_CREATE)) {
583 finish_transfer(partialptr, fnametmp, file, recv_ok,
593 int msgtype = csum_length == SUM_LENGTH || read_batch ?
595 if (msgtype == FERROR || verbose) {
596 char *errstr, *redostr, *keptstr;
597 if (!(keep_partial && partialptr) && !inplace)
598 keptstr = "discarded";
599 else if (partial_dir)
600 keptstr = "put into partial-dir";
602 keptstr = "retained";
603 if (msgtype == FERROR) {
608 redostr = " (will try again)";
611 "%s: %s failed verification -- update %s%s.\n",
612 errstr, safe_fname(fname),
615 if (csum_length != SUM_LENGTH) {
618 send_msg(MSG_REDO, buf, 4);
622 make_backups = save_make_backups;
624 if (delete_after && recurse && !local_name && flist->count > 0)
628 rprintf(FINFO,"recv_files finished\n");