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 delete_after;
25 extern int max_delete;
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;
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 int is_backup_file(char *fn)
62 int k = strlen(fn) - backup_suffix_len;
63 return k > 0 && strcmp(fn+k, backup_suffix) == 0;
67 /* This deletes any files on the receiving side that are not present
68 * on the sending side. */
69 void delete_files(struct file_list *flist)
71 struct file_list *local_file_list;
73 char *argv[1], fbuf[MAXPATHLEN];
74 static int deletion_count;
79 if (io_error && !(lp_ignore_errors(module_id) || ignore_errors)) {
80 rprintf(FINFO,"IO error encountered - skipping file deletion\n");
84 for (j = 0; j < flist->count; j++) {
85 if (!(flist->files[j]->flags & FLAG_DEL_START)
86 || !S_ISDIR(flist->files[j]->mode))
89 argv[0] = f_name_to(flist->files[j], fbuf);
91 if (!(local_file_list = send_file_list(-1, 1, argv)))
95 rprintf(FINFO, "deleting in %s\n", safe_fname(fbuf));
97 for (i = local_file_list->count-1; i >= 0; i--) {
98 if (max_delete && deletion_count >= max_delete)
100 if (!local_file_list->files[i]->basename)
102 if (flist_find(flist,local_file_list->files[i]) < 0) {
103 char *f = f_name(local_file_list->files[i]);
104 int mode = local_file_list->files[i]->mode;
105 if (make_backups && (backup_dir || !is_backup_file(f))
109 rprintf(FINFO, "deleting %s\n",
113 delete_file(f, S_ISDIR(mode)
114 ? DEL_DIR | DEL_RECURSE : 0);
119 flist_free(local_file_list);
125 * get_tmpname() - create a tmp filename for a given filename
127 * If a tmpdir is defined, use that as the directory to
128 * put it in. Otherwise, the tmp filename is in the same
129 * directory as the given name. Note that there may be no
130 * directory at all in the given name!
132 * The tmp filename is basically the given filename with a
133 * dot prepended, and .XXXXXX appended (for mkstemp() to
134 * put its unique gunk in). Take care to not exceed
135 * either the MAXPATHLEN or NAME_MAX, esp. the last, as
136 * the basename basically becomes 8 chars longer. In that
137 * case, the original name is shortened sufficiently to
140 * Of course, there's no real reason for the tmp name to
141 * look like the original, except to satisfy us humans.
142 * As long as it's unique, rsync will work.
145 static int get_tmpname(char *fnametmp, char *fname)
152 /* Note: this can't overflow, so the return value is safe */
153 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
154 fnametmp[length++] = '/';
155 fnametmp[length] = '\0'; /* always NULL terminated */
158 if ((f = strrchr(fname, '/')) != NULL) {
162 /* copy up to and including the slash */
163 strlcpy(fnametmp, fname, length + 1);
167 fnametmp[length++] = '.';
168 fnametmp[length] = '\0'; /* always NULL terminated */
170 maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8);
173 rprintf(FERROR, "temporary filename too long: %s\n", fname);
178 strlcpy(fnametmp + length, f, maxname);
179 strcat(fnametmp + length, ".XXXXXX");
185 static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
186 char *fname, int fd, OFF_T total_size)
188 static char file_sum1[MD4_SUM_LENGTH];
189 static char file_sum2[MD4_SUM_LENGTH];
190 struct map_struct *mapbuf;
191 struct sum_struct sum;
199 read_sum_head(f_in, &sum);
201 if (fd_r >= 0 && size_r > 0) {
202 int32 read_size = MAX(sum.blength * 2, 16*1024);
203 mapbuf = map_file(fd_r, size_r, read_size, sum.blength);
205 rprintf(FINFO, "recv mapped %s of size %.0f\n",
206 safe_fname(fname_r), (double)size_r);
211 sum_init(checksum_seed);
213 while ((i = recv_token(f_in, &data)) != 0) {
215 show_progress(offset, total_size);
219 rprintf(FINFO,"data recv %d at %.0f\n",
223 stats.literal_data += i;
224 cleanup_got_literal = 1;
228 if (fd != -1 && write_file(fd,data,i) != i)
229 goto report_write_error;
235 offset2 = i * (OFF_T)sum.blength;
237 if (i == (int)sum.count-1 && sum.remainder != 0)
240 stats.matched_data += len;
244 "chunk[%d] of size %ld at %.0f offset=%.0f\n",
245 i, (long)len, (double)offset2, (double)offset);
249 map = map_ptr(mapbuf,offset2,len);
252 sum_update(map, len);
256 if (offset == offset2 && fd != -1) {
257 if (flush_write_file(fd) < 0)
258 goto report_write_error;
260 if (do_lseek(fd, len, SEEK_CUR) != offset) {
261 rsyserr(FERROR, errno,
262 "lseek failed on %s",
264 exit_cleanup(RERR_FILEIO);
269 if (fd != -1 && write_file(fd, map, len) != (int)len)
270 goto report_write_error;
274 if (flush_write_file(fd) < 0)
275 goto report_write_error;
277 #ifdef HAVE_FTRUNCATE
278 if (inplace && fd != -1)
279 ftruncate(fd, offset);
283 end_progress(total_size);
285 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
287 rsyserr(FERROR, errno, "write failed on %s",
289 exit_cleanup(RERR_FILEIO);
297 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
299 rprintf(FINFO,"got file_sum\n");
300 if (fd != -1 && memcmp(file_sum1, file_sum2, MD4_SUM_LENGTH) != 0)
306 static void discard_receive_data(int f_in, OFF_T length)
308 receive_data(f_in, NULL, -1, 0, NULL, -1, length);
313 * main routine for receiver process.
315 * Receiver process runs on the same host as the generator process. */
316 int recv_files(int f_in, struct file_list *flist, char *local_name,
322 char *fname, fbuf[MAXPATHLEN];
323 char template[MAXPATHLEN];
324 char fnametmp[MAXPATHLEN];
325 char *fnamecmp, *partialptr;
326 char fnamecmpbuf[MAXPATHLEN];
327 struct file_struct *file;
328 struct stats initial_stats;
329 int save_make_backups = make_backups;
330 int i, recv_ok, phase = 0;
333 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
335 if (flist->hlink_pool) {
336 pool_destroy(flist->hlink_pool);
337 flist->hlink_pool = NULL;
346 if (next_gen_i != flist->count) {
350 read_byte(f_in_name);
351 } while (read_int(batch_gen_fd) != -1);
360 csum_length = SUM_LENGTH;
362 rprintf(FINFO, "recv_files phase=%d\n", phase);
363 send_msg(MSG_DONE, "", 0);
364 if (keep_partial && !partial_dir)
365 make_backups = 0; /* prevents double backup */
369 if (i < 0 || i >= flist->count) {
370 rprintf(FERROR,"Invalid file index %d in recv_files (count=%d)\n",
372 exit_cleanup(RERR_PROTOCOL);
375 file = flist->files[i];
377 stats.current_file_index = i;
378 stats.num_transferred_files++;
379 stats.total_transferred_size += file->length;
380 cleanup_got_literal = 0;
385 fname = f_name_to(file, fbuf);
388 if (!am_server && verbose) /* log the transfer */
389 rprintf(FINFO, "%s\n", safe_fname(fname));
393 initial_stats = stats;
396 rprintf(FINFO, "recv_files(%s)\n", safe_fname(fname));
399 while (i > next_gen_i) {
400 if (f_in_name >= 0 && next_gen_i >= 0)
401 read_byte(f_in_name);
402 next_gen_i = read_int(batch_gen_fd);
403 if (next_gen_i == -1)
404 next_gen_i = flist->count;
406 if (i < next_gen_i) {
407 rprintf(FINFO, "skipping update for \"%s\"\n",
409 discard_receive_data(f_in, file->length);
415 if (server_exclude_list.head
416 && check_exclude(&server_exclude_list, fname,
417 S_ISDIR(file->mode)) < 0) {
418 rprintf(FERROR, "attempt to hack rsync failed.\n");
419 exit_cleanup(RERR_PROTOCOL);
422 partialptr = partial_dir ? partial_dir_fname(fname) : fname;
424 if (f_in_name >= 0) {
426 switch (j = read_byte(f_in_name)) {
430 case FNAMECMP_PARTIAL_DIR:
431 fnamecmp = partialptr ? partialptr : fname;
433 case FNAMECMP_BACKUP:
434 fnamecmp = get_backup_name(fname);
437 if (j >= basis_dir_cnt) {
439 "invalid basis_dir index: %d.\n",
441 exit_cleanup(RERR_PROTOCOL);
443 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
444 basis_dir[j], fname);
445 fnamecmp = fnamecmpbuf;
452 fd1 = do_open(fnamecmp, O_RDONLY, 0);
454 if (fd1 != -1 && do_fstat(fd1,&st) != 0) {
455 rsyserr(FERROR, errno, "fstat %s failed",
456 full_fname(fnamecmp));
457 discard_receive_data(f_in, file->length);
462 if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
463 /* this special handling for directories
464 * wouldn't be necessary if robust_rename()
465 * and the underlying robust_unlink could cope
468 rprintf(FERROR,"recv_files: %s is a directory\n",
469 full_fname(fnamecmp));
470 discard_receive_data(f_in, file->length);
475 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
480 if (fd1 != -1 && !preserve_perms) {
481 /* if the file exists already and we aren't preserving
482 * permissions then act as though the remote end sent
483 * us the file permissions we already have */
484 file->mode = st.st_mode;
487 /* We now check to see if we are writing file "inplace" */
489 fd2 = do_open(fname, O_WRONLY|O_CREAT, 0);
491 rsyserr(FERROR, errno, "open %s failed",
493 discard_receive_data(f_in, file->length);
499 if (!get_tmpname(fnametmp,fname)) {
500 discard_receive_data(f_in, file->length);
506 strlcpy(template, fnametmp, sizeof template);
508 /* we initially set the perms without the
509 * setuid/setgid bits to ensure that there is no race
510 * condition. They are then correctly updated after
511 * the lchown. Thanks to snabb@epipe.fi for pointing
512 * this out. We also set it initially without group
513 * access because of a similar race condition. */
514 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
516 /* in most cases parent directories will already exist
517 * because their information should have been previously
518 * transferred, but that may not be the case with -R */
519 if (fd2 == -1 && relative_paths && errno == ENOENT
520 && create_directory_path(fnametmp, orig_umask) == 0) {
521 strlcpy(fnametmp, template, sizeof fnametmp);
522 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
525 rsyserr(FERROR, errno, "mkstemp %s failed",
526 full_fname(fnametmp));
527 discard_receive_data(f_in, file->length);
534 cleanup_set(fnametmp, partialptr, file, fd1, fd2);
537 if (!am_server && verbose) /* log the transfer */
538 rprintf(FINFO, "%s\n", safe_fname(fname));
541 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
542 fname, fd2, file->length);
544 log_recv(file, &initial_stats);
548 if (close(fd2) < 0) {
549 rsyserr(FERROR, errno, "close failed on %s",
550 full_fname(fnametmp));
551 exit_cleanup(RERR_FILEIO);
554 if (recv_ok || inplace) {
555 finish_transfer(fname, fnametmp, file, recv_ok, 1);
556 if (partialptr != fname && fnamecmp == partialptr) {
557 do_unlink(partialptr);
558 handle_partial_dir(partialptr, PDIR_DELETE);
560 } else if (keep_partial && partialptr
561 && handle_partial_dir(partialptr, PDIR_CREATE)) {
562 finish_transfer(partialptr, fnametmp, file, recv_ok,
572 int msgtype = csum_length == SUM_LENGTH || read_batch ?
574 if (msgtype == FERROR || verbose) {
575 char *errstr, *redostr, *keptstr;
576 if (!(keep_partial && partialptr) && !inplace)
577 keptstr = "discarded";
578 else if (partial_dir)
579 keptstr = "put into partial-dir";
581 keptstr = "retained";
582 if (msgtype == FERROR) {
587 redostr = " (will try again)";
590 "%s: %s failed verification -- update %s%s.\n",
591 errstr, safe_fname(fname),
594 if (csum_length != SUM_LENGTH) {
597 send_msg(MSG_REDO, buf, 4);
601 make_backups = save_make_backups;
603 if (delete_after && keep_dirs && !local_name && flist->count > 0)
607 rprintf(FINFO,"recv_files finished\n");