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;
52 extern struct filter_list_struct server_filter_list;
55 /* This deletes any files on the receiving side that are not present on the
56 * sending side. This is used by --delete-before and --delete-after. */
57 void delete_files(struct file_list *flist)
59 char fbuf[MAXPATHLEN];
62 for (j = 0; j < flist->count; j++) {
63 if (!(flist->files[j]->flags & FLAG_DEL_START)
64 || !S_ISDIR(flist->files[j]->mode))
67 delete_in_dir(flist, f_name_to(flist->files[j], fbuf));
73 * get_tmpname() - create a tmp filename for a given filename
75 * If a tmpdir is defined, use that as the directory to
76 * put it in. Otherwise, the tmp filename is in the same
77 * directory as the given name. Note that there may be no
78 * directory at all in the given name!
80 * The tmp filename is basically the given filename with a
81 * dot prepended, and .XXXXXX appended (for mkstemp() to
82 * put its unique gunk in). Take care to not exceed
83 * either the MAXPATHLEN or NAME_MAX, esp. the last, as
84 * the basename basically becomes 8 chars longer. In that
85 * case, the original name is shortened sufficiently to
88 * Of course, there's no real reason for the tmp name to
89 * look like the original, except to satisfy us humans.
90 * As long as it's unique, rsync will work.
93 static int get_tmpname(char *fnametmp, char *fname)
100 /* Note: this can't overflow, so the return value is safe */
101 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
102 fnametmp[length++] = '/';
103 fnametmp[length] = '\0'; /* always NULL terminated */
106 if ((f = strrchr(fname, '/')) != NULL) {
110 /* copy up to and including the slash */
111 strlcpy(fnametmp, fname, length + 1);
115 fnametmp[length++] = '.';
116 fnametmp[length] = '\0'; /* always NULL terminated */
118 maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8);
121 rprintf(FERROR, "temporary filename too long: %s\n", fname);
126 strlcpy(fnametmp + length, f, maxname);
127 strcat(fnametmp + length, ".XXXXXX");
133 static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
134 char *fname, int fd, OFF_T total_size)
136 static char file_sum1[MD4_SUM_LENGTH];
137 static char file_sum2[MD4_SUM_LENGTH];
138 struct map_struct *mapbuf;
139 struct sum_struct sum;
147 read_sum_head(f_in, &sum);
149 if (fd_r >= 0 && size_r > 0) {
150 int32 read_size = MAX(sum.blength * 2, 16*1024);
151 mapbuf = map_file(fd_r, size_r, read_size, sum.blength);
153 rprintf(FINFO, "recv mapped %s of size %.0f\n",
154 safe_fname(fname_r), (double)size_r);
159 sum_init(checksum_seed);
161 while ((i = recv_token(f_in, &data)) != 0) {
163 show_progress(offset, total_size);
167 rprintf(FINFO,"data recv %d at %.0f\n",
171 stats.literal_data += i;
172 cleanup_got_literal = 1;
176 if (fd != -1 && write_file(fd,data,i) != i)
177 goto report_write_error;
183 offset2 = i * (OFF_T)sum.blength;
185 if (i == (int)sum.count-1 && sum.remainder != 0)
188 stats.matched_data += len;
192 "chunk[%d] of size %ld at %.0f offset=%.0f\n",
193 i, (long)len, (double)offset2, (double)offset);
197 map = map_ptr(mapbuf,offset2,len);
200 sum_update(map, len);
204 if (offset == offset2 && fd != -1) {
205 if (flush_write_file(fd) < 0)
206 goto report_write_error;
208 if (do_lseek(fd, len, SEEK_CUR) != offset) {
209 rsyserr(FERROR, errno,
210 "lseek failed on %s",
212 exit_cleanup(RERR_FILEIO);
217 if (fd != -1 && write_file(fd, map, len) != (int)len)
218 goto report_write_error;
222 if (flush_write_file(fd) < 0)
223 goto report_write_error;
225 #ifdef HAVE_FTRUNCATE
226 if (inplace && fd != -1)
227 ftruncate(fd, offset);
231 end_progress(total_size);
233 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
235 rsyserr(FERROR, errno, "write failed on %s",
237 exit_cleanup(RERR_FILEIO);
245 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
247 rprintf(FINFO,"got file_sum\n");
248 if (fd != -1 && memcmp(file_sum1, file_sum2, MD4_SUM_LENGTH) != 0)
254 static void discard_receive_data(int f_in, OFF_T length)
256 receive_data(f_in, NULL, -1, 0, NULL, -1, length);
261 * main routine for receiver process.
263 * Receiver process runs on the same host as the generator process. */
264 int recv_files(int f_in, struct file_list *flist, char *local_name,
270 char *fname, fbuf[MAXPATHLEN];
271 char template[MAXPATHLEN];
272 char fnametmp[MAXPATHLEN];
273 char *fnamecmp, *partialptr;
274 char fnamecmpbuf[MAXPATHLEN];
275 struct file_struct *file;
276 struct stats initial_stats;
277 int save_make_backups = make_backups;
278 int i, recv_ok, phase = 0;
281 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
283 if (flist->hlink_pool) {
284 pool_destroy(flist->hlink_pool);
285 flist->hlink_pool = NULL;
294 if (next_gen_i != flist->count) {
298 read_byte(f_in_name);
299 } while (read_int(batch_gen_fd) != -1);
308 csum_length = SUM_LENGTH;
310 rprintf(FINFO, "recv_files phase=%d\n", phase);
311 send_msg(MSG_DONE, "", 0);
312 if (keep_partial && !partial_dir)
313 make_backups = 0; /* prevents double backup */
317 if (i < 0 || i >= flist->count) {
318 rprintf(FERROR,"Invalid file index %d in recv_files (count=%d)\n",
320 exit_cleanup(RERR_PROTOCOL);
323 file = flist->files[i];
325 stats.current_file_index = i;
326 stats.num_transferred_files++;
327 stats.total_transferred_size += file->length;
328 cleanup_got_literal = 0;
333 fname = f_name_to(file, fbuf);
336 if (!am_server && verbose) /* log the transfer */
337 rprintf(FINFO, "%s\n", safe_fname(fname));
341 initial_stats = stats;
344 rprintf(FINFO, "recv_files(%s)\n", safe_fname(fname));
347 while (i > next_gen_i) {
348 if (f_in_name >= 0 && next_gen_i >= 0)
349 read_byte(f_in_name);
350 next_gen_i = read_int(batch_gen_fd);
351 if (next_gen_i == -1)
352 next_gen_i = flist->count;
354 if (i < next_gen_i) {
355 rprintf(FINFO, "skipping update for \"%s\"\n",
357 discard_receive_data(f_in, file->length);
363 if (server_filter_list.head
364 && check_filter(&server_filter_list, fname,
365 S_ISDIR(file->mode)) < 0) {
366 rprintf(FERROR, "attempt to hack rsync failed.\n");
367 exit_cleanup(RERR_PROTOCOL);
370 partialptr = partial_dir ? partial_dir_fname(fname) : fname;
372 if (f_in_name >= 0) {
374 switch (j = read_byte(f_in_name)) {
378 case FNAMECMP_PARTIAL_DIR:
379 fnamecmp = partialptr ? partialptr : fname;
381 case FNAMECMP_BACKUP:
382 fnamecmp = get_backup_name(fname);
385 if (j >= basis_dir_cnt) {
387 "invalid basis_dir index: %d.\n",
389 exit_cleanup(RERR_PROTOCOL);
391 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
392 basis_dir[j], fname);
393 fnamecmp = fnamecmpbuf;
400 fd1 = do_open(fnamecmp, O_RDONLY, 0);
402 if (fd1 != -1 && do_fstat(fd1,&st) != 0) {
403 rsyserr(FERROR, errno, "fstat %s failed",
404 full_fname(fnamecmp));
405 discard_receive_data(f_in, file->length);
410 if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
411 /* this special handling for directories
412 * wouldn't be necessary if robust_rename()
413 * and the underlying robust_unlink could cope
416 rprintf(FERROR,"recv_files: %s is a directory\n",
417 full_fname(fnamecmp));
418 discard_receive_data(f_in, file->length);
423 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
428 if (fd1 != -1 && !preserve_perms) {
429 /* if the file exists already and we aren't preserving
430 * permissions then act as though the remote end sent
431 * us the file permissions we already have */
432 file->mode = st.st_mode;
435 /* We now check to see if we are writing file "inplace" */
437 fd2 = do_open(fname, O_WRONLY|O_CREAT, 0);
439 rsyserr(FERROR, errno, "open %s failed",
441 discard_receive_data(f_in, file->length);
447 if (!get_tmpname(fnametmp,fname)) {
448 discard_receive_data(f_in, file->length);
454 strlcpy(template, fnametmp, sizeof template);
456 /* we initially set the perms without the
457 * setuid/setgid bits to ensure that there is no race
458 * condition. They are then correctly updated after
459 * the lchown. Thanks to snabb@epipe.fi for pointing
460 * this out. We also set it initially without group
461 * access because of a similar race condition. */
462 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
464 /* in most cases parent directories will already exist
465 * because their information should have been previously
466 * transferred, but that may not be the case with -R */
467 if (fd2 == -1 && relative_paths && errno == ENOENT
468 && create_directory_path(fnametmp, orig_umask) == 0) {
469 strlcpy(fnametmp, template, sizeof fnametmp);
470 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
473 rsyserr(FERROR, errno, "mkstemp %s failed",
474 full_fname(fnametmp));
475 discard_receive_data(f_in, file->length);
482 cleanup_set(fnametmp, partialptr, file, fd1, fd2);
485 if (!am_server && verbose) /* log the transfer */
486 rprintf(FINFO, "%s\n", safe_fname(fname));
489 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
490 fname, fd2, file->length);
492 log_recv(file, &initial_stats);
496 if (close(fd2) < 0) {
497 rsyserr(FERROR, errno, "close failed on %s",
498 full_fname(fnametmp));
499 exit_cleanup(RERR_FILEIO);
502 if (recv_ok || inplace) {
503 finish_transfer(fname, fnametmp, file, recv_ok, 1);
504 if (partialptr != fname && fnamecmp == partialptr) {
505 do_unlink(partialptr);
506 handle_partial_dir(partialptr, PDIR_DELETE);
508 } else if (keep_partial && partialptr
509 && handle_partial_dir(partialptr, PDIR_CREATE)) {
510 finish_transfer(partialptr, fnametmp, file, recv_ok,
520 int msgtype = csum_length == SUM_LENGTH || read_batch ?
522 if (msgtype == FERROR || verbose) {
523 char *errstr, *redostr, *keptstr;
524 if (!(keep_partial && partialptr) && !inplace)
525 keptstr = "discarded";
526 else if (partial_dir)
527 keptstr = "put into partial-dir";
529 keptstr = "retained";
530 if (msgtype == FERROR) {
535 redostr = " (will try again)";
538 "%s: %s failed verification -- update %s%s.\n",
539 errstr, safe_fname(fname),
542 if (csum_length != SUM_LENGTH) {
545 send_msg(MSG_REDO, buf, 4);
549 make_backups = save_make_backups;
551 if (delete_after && recurse && !local_name && flist->count > 0)
555 rprintf(FINFO,"recv_files finished\n");