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 csum_length;
26 extern struct stats stats;
28 extern int read_batch;
29 extern int batch_gen_fd;
31 extern int protocol_version;
32 extern int relative_paths;
33 extern int keep_dirlinks;
34 extern int preserve_hard_links;
35 extern int preserve_perms;
38 extern char *partial_dir;
39 extern char *basis_dir[];
40 extern int basis_dir_cnt;
41 extern int make_backups;
42 extern int do_progress;
43 extern int cleanup_got_literal;
45 extern int ignore_errors;
46 extern int orig_umask;
47 extern int keep_partial;
48 extern int checksum_seed;
50 extern int delay_updates;
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 struct file_struct *file = flist->files[j];
65 if (!(file->flags & FLAG_DEL_HERE))
68 f_name_to(file, fbuf);
69 if (verbose > 1 && file->flags & FLAG_TOP_DIR)
70 rprintf(FINFO, "deleting in %s\n", safe_fname(fbuf));
72 delete_in_dir(flist, fbuf, file);
78 * get_tmpname() - create a tmp filename for a given filename
80 * If a tmpdir is defined, use that as the directory to
81 * put it in. Otherwise, the tmp filename is in the same
82 * directory as the given name. Note that there may be no
83 * directory at all in the given name!
85 * The tmp filename is basically the given filename with a
86 * dot prepended, and .XXXXXX appended (for mkstemp() to
87 * put its unique gunk in). Take care to not exceed
88 * either the MAXPATHLEN or NAME_MAX, esp. the last, as
89 * the basename basically becomes 8 chars longer. In that
90 * case, the original name is shortened sufficiently to
93 * Of course, there's no real reason for the tmp name to
94 * look like the original, except to satisfy us humans.
95 * As long as it's unique, rsync will work.
98 static int get_tmpname(char *fnametmp, char *fname)
105 /* Note: this can't overflow, so the return value is safe */
106 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
107 fnametmp[length++] = '/';
108 fnametmp[length] = '\0'; /* always NULL terminated */
111 if ((f = strrchr(fname, '/')) != NULL) {
115 /* copy up to and including the slash */
116 strlcpy(fnametmp, fname, length + 1);
120 fnametmp[length++] = '.';
121 fnametmp[length] = '\0'; /* always NULL terminated */
123 maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8);
126 rprintf(FERROR, "temporary filename too long: %s\n",
132 strlcpy(fnametmp + length, f, maxname);
133 strcat(fnametmp + length, ".XXXXXX");
139 static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
140 char *fname, int fd, OFF_T total_size)
142 static char file_sum1[MD4_SUM_LENGTH];
143 static char file_sum2[MD4_SUM_LENGTH];
144 struct map_struct *mapbuf;
145 struct sum_struct sum;
153 read_sum_head(f_in, &sum);
155 if (fd_r >= 0 && size_r > 0) {
156 int32 read_size = MAX(sum.blength * 2, 16*1024);
157 mapbuf = map_file(fd_r, size_r, read_size, sum.blength);
159 rprintf(FINFO, "recv mapped %s of size %.0f\n",
160 safe_fname(fname_r), (double)size_r);
165 sum_init(checksum_seed);
167 while ((i = recv_token(f_in, &data)) != 0) {
169 show_progress(offset, total_size);
173 rprintf(FINFO,"data recv %d at %.0f\n",
177 stats.literal_data += i;
178 cleanup_got_literal = 1;
182 if (fd != -1 && write_file(fd,data,i) != i)
183 goto report_write_error;
189 offset2 = i * (OFF_T)sum.blength;
191 if (i == (int)sum.count-1 && sum.remainder != 0)
194 stats.matched_data += len;
198 "chunk[%d] of size %ld at %.0f offset=%.0f\n",
199 i, (long)len, (double)offset2, (double)offset);
203 map = map_ptr(mapbuf,offset2,len);
206 sum_update(map, len);
210 if (offset == offset2 && fd != -1) {
211 if (flush_write_file(fd) < 0)
212 goto report_write_error;
214 if (do_lseek(fd, len, SEEK_CUR) != offset) {
215 rsyserr(FERROR, errno,
216 "lseek failed on %s",
218 exit_cleanup(RERR_FILEIO);
223 if (fd != -1 && write_file(fd, map, len) != (int)len)
224 goto report_write_error;
228 if (flush_write_file(fd) < 0)
229 goto report_write_error;
231 #ifdef HAVE_FTRUNCATE
232 if (inplace && fd != -1)
233 ftruncate(fd, offset);
237 end_progress(total_size);
239 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
241 rsyserr(FERROR, errno, "write failed on %s",
243 exit_cleanup(RERR_FILEIO);
251 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
253 rprintf(FINFO,"got file_sum\n");
254 if (fd != -1 && memcmp(file_sum1, file_sum2, MD4_SUM_LENGTH) != 0)
260 static void discard_receive_data(int f_in, OFF_T length)
262 receive_data(f_in, NULL, -1, 0, NULL, -1, length);
267 * main routine for receiver process.
269 * Receiver process runs on the same host as the generator process. */
270 int recv_files(int f_in, struct file_list *flist, char *local_name,
276 char *fname, fbuf[MAXPATHLEN];
277 char template[MAXPATHLEN];
278 char fnametmp[MAXPATHLEN];
279 char *fnamecmp, *partialptr;
280 char fnamecmpbuf[MAXPATHLEN];
281 uchar *delayed_bits = NULL;
282 struct file_struct *file;
283 struct stats initial_stats;
284 int save_make_backups = make_backups;
285 int i, recv_ok, phase = 0;
288 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
290 if (flist->hlink_pool) {
291 pool_destroy(flist->hlink_pool);
292 flist->hlink_pool = NULL;
296 int sz = (flist->count + 7) / 8;
297 if (!(delayed_bits = new_array(uchar, sz)))
298 out_of_memory("recv_files");
299 memset(delayed_bits, 0, sz);
308 if (next_gen_i != flist->count) {
312 read_byte(f_in_name);
313 } while (read_int(batch_gen_fd) != -1);
322 csum_length = SUM_LENGTH;
324 rprintf(FINFO, "recv_files phase=%d\n", phase);
325 send_msg(MSG_DONE, "", 0);
326 if (keep_partial && !partial_dir)
327 make_backups = 0; /* prevents double backup */
331 if (i < 0 || i >= flist->count) {
332 rprintf(FERROR,"Invalid file index %d in recv_files (count=%d)\n",
334 exit_cleanup(RERR_PROTOCOL);
337 file = flist->files[i];
338 if (S_ISDIR(file->mode)) {
339 rprintf(FERROR, "[%s] got index of directory: %d\n",
341 exit_cleanup(RERR_PROTOCOL);
344 stats.current_file_index = i;
345 stats.num_transferred_files++;
346 stats.total_transferred_size += file->length;
347 cleanup_got_literal = 0;
349 fname = local_name ? local_name : f_name_to(file, fbuf);
351 if (server_filter_list.head
352 && check_filter(&server_filter_list, fname, 0) < 0) {
353 rprintf(FERROR, "attempt to hack rsync failed.\n");
354 exit_cleanup(RERR_PROTOCOL);
358 rprintf(FINFO, "recv_files(%s)\n", safe_fname(fname));
361 if (!am_server && verbose) /* log the transfer */
362 rprintf(FINFO, "%s\n", safe_fname(fname));
366 initial_stats = stats;
369 while (i > next_gen_i) {
370 if (f_in_name >= 0 && next_gen_i >= 0)
371 read_byte(f_in_name);
372 next_gen_i = read_int(batch_gen_fd);
373 if (next_gen_i == -1)
374 next_gen_i = flist->count;
376 if (i < next_gen_i) {
377 rprintf(FINFO, "skipping update for \"%s\"\n",
379 discard_receive_data(f_in, file->length);
385 partialptr = partial_dir ? partial_dir_fname(fname) : fname;
387 if (f_in_name >= 0) {
389 switch (j = read_byte(f_in_name)) {
393 case FNAMECMP_PARTIAL_DIR:
394 fnamecmp = partialptr ? partialptr : fname;
396 case FNAMECMP_BACKUP:
397 fnamecmp = get_backup_name(fname);
400 if (j >= basis_dir_cnt) {
402 "invalid basis_dir index: %d.\n",
404 exit_cleanup(RERR_PROTOCOL);
406 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
407 basis_dir[j], fname);
408 fnamecmp = fnamecmpbuf;
415 fd1 = do_open(fnamecmp, O_RDONLY, 0);
417 if (fd1 != -1 && do_fstat(fd1,&st) != 0) {
418 rsyserr(FERROR, errno, "fstat %s failed",
419 full_fname(fnamecmp));
420 discard_receive_data(f_in, file->length);
425 if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
426 /* this special handling for directories
427 * wouldn't be necessary if robust_rename()
428 * and the underlying robust_unlink could cope
431 rprintf(FERROR,"recv_files: %s is a directory\n",
432 full_fname(fnamecmp));
433 discard_receive_data(f_in, file->length);
438 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
443 if (fd1 != -1 && !preserve_perms) {
444 /* if the file exists already and we aren't preserving
445 * permissions then act as though the remote end sent
446 * us the file permissions we already have */
447 file->mode = st.st_mode;
450 /* We now check to see if we are writing file "inplace" */
452 fd2 = do_open(fname, O_WRONLY|O_CREAT, 0);
454 rsyserr(FERROR, errno, "open %s failed",
456 discard_receive_data(f_in, file->length);
462 if (!get_tmpname(fnametmp,fname)) {
463 discard_receive_data(f_in, file->length);
469 strlcpy(template, fnametmp, sizeof template);
471 /* we initially set the perms without the
472 * setuid/setgid bits to ensure that there is no race
473 * condition. They are then correctly updated after
474 * the lchown. Thanks to snabb@epipe.fi for pointing
475 * this out. We also set it initially without group
476 * access because of a similar race condition. */
477 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
479 /* in most cases parent directories will already exist
480 * because their information should have been previously
481 * transferred, but that may not be the case with -R */
482 if (fd2 == -1 && relative_paths && errno == ENOENT
483 && create_directory_path(fnametmp, orig_umask) == 0) {
484 strlcpy(fnametmp, template, sizeof fnametmp);
485 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
488 rsyserr(FERROR, errno, "mkstemp %s failed",
489 full_fname(fnametmp));
490 discard_receive_data(f_in, file->length);
497 cleanup_set(fnametmp, partialptr, file, fd1, fd2);
500 if (!am_server && verbose) /* log the transfer */
501 rprintf(FINFO, "%s\n", safe_fname(fname));
504 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
505 fname, fd2, file->length);
507 log_recv(file, &initial_stats);
511 if (close(fd2) < 0) {
512 rsyserr(FERROR, errno, "close failed on %s",
513 full_fname(fnametmp));
514 exit_cleanup(RERR_FILEIO);
517 if ((recv_ok && (!delay_updates || !partialptr)) || inplace) {
518 finish_transfer(fname, fnametmp, file, recv_ok, 1);
519 if (partialptr != fname && fnamecmp == partialptr) {
520 do_unlink(partialptr);
521 handle_partial_dir(partialptr, PDIR_DELETE);
523 } else if (keep_partial && partialptr
524 && handle_partial_dir(partialptr, PDIR_CREATE)) {
525 finish_transfer(partialptr, fnametmp, file, recv_ok,
527 if (delay_updates && recv_ok)
528 delayed_bits[i/8] |= 1 << (i % 8);
537 int msgtype = csum_length == SUM_LENGTH || read_batch ?
539 if (msgtype == FERROR || verbose) {
540 char *errstr, *redostr, *keptstr;
541 if (!(keep_partial && partialptr) && !inplace)
542 keptstr = "discarded";
543 else if (partial_dir)
544 keptstr = "put into partial-dir";
546 keptstr = "retained";
547 if (msgtype == FERROR) {
552 redostr = " (will try again)";
555 "%s: %s failed verification -- update %s%s.\n",
556 errstr, safe_fname(fname),
559 if (csum_length != SUM_LENGTH) {
562 send_msg(MSG_REDO, buf, 4);
566 make_backups = save_make_backups;
569 for (i = 0; i < flist->count; i++) {
570 struct file_struct *file = flist->files[i];
572 || !(delayed_bits[i/8] & (1 << (i % 8))))
574 fname = local_name ? local_name : f_name(file);
575 partialptr = partial_dir_fname(fname);
577 if (make_backups && !make_backup(fname))
580 rprintf(FINFO, "renaming %s to %s\n",
581 safe_fname(partialptr),
584 if (do_rename(partialptr, fname) < 0) {
585 rsyserr(FERROR, errno,
586 "rename failed for %s (from %s)",
588 safe_fname(partialptr));
590 handle_partial_dir(partialptr,
597 if (delete_after && !local_name && flist->count > 0)
601 rprintf(FINFO,"recv_files finished\n");