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", fname);
131 strlcpy(fnametmp + length, f, maxname);
132 strcat(fnametmp + length, ".XXXXXX");
138 static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
139 char *fname, int fd, OFF_T total_size)
141 static char file_sum1[MD4_SUM_LENGTH];
142 static char file_sum2[MD4_SUM_LENGTH];
143 struct map_struct *mapbuf;
144 struct sum_struct sum;
152 read_sum_head(f_in, &sum);
154 if (fd_r >= 0 && size_r > 0) {
155 int32 read_size = MAX(sum.blength * 2, 16*1024);
156 mapbuf = map_file(fd_r, size_r, read_size, sum.blength);
158 rprintf(FINFO, "recv mapped %s of size %.0f\n",
159 safe_fname(fname_r), (double)size_r);
164 sum_init(checksum_seed);
166 while ((i = recv_token(f_in, &data)) != 0) {
168 show_progress(offset, total_size);
172 rprintf(FINFO,"data recv %d at %.0f\n",
176 stats.literal_data += i;
177 cleanup_got_literal = 1;
181 if (fd != -1 && write_file(fd,data,i) != i)
182 goto report_write_error;
188 offset2 = i * (OFF_T)sum.blength;
190 if (i == (int)sum.count-1 && sum.remainder != 0)
193 stats.matched_data += len;
197 "chunk[%d] of size %ld at %.0f offset=%.0f\n",
198 i, (long)len, (double)offset2, (double)offset);
202 map = map_ptr(mapbuf,offset2,len);
205 sum_update(map, len);
209 if (offset == offset2 && fd != -1) {
210 if (flush_write_file(fd) < 0)
211 goto report_write_error;
213 if (do_lseek(fd, len, SEEK_CUR) != offset) {
214 rsyserr(FERROR, errno,
215 "lseek failed on %s",
217 exit_cleanup(RERR_FILEIO);
222 if (fd != -1 && write_file(fd, map, len) != (int)len)
223 goto report_write_error;
227 if (flush_write_file(fd) < 0)
228 goto report_write_error;
231 if (inplace && fd != -1)
232 ftruncate(fd, offset);
236 end_progress(total_size);
238 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
240 rsyserr(FERROR, errno, "write failed on %s",
242 exit_cleanup(RERR_FILEIO);
250 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
252 rprintf(FINFO,"got file_sum\n");
253 if (fd != -1 && memcmp(file_sum1, file_sum2, MD4_SUM_LENGTH) != 0)
259 static void discard_receive_data(int f_in, OFF_T length)
261 receive_data(f_in, NULL, -1, 0, NULL, -1, length);
266 * main routine for receiver process.
268 * Receiver process runs on the same host as the generator process. */
269 int recv_files(int f_in, struct file_list *flist, char *local_name,
275 char *fname, fbuf[MAXPATHLEN];
276 char template[MAXPATHLEN];
277 char fnametmp[MAXPATHLEN];
278 char *fnamecmp, *partialptr;
279 char fnamecmpbuf[MAXPATHLEN];
280 uchar *delayed_bits = NULL;
281 struct file_struct *file;
282 struct stats initial_stats;
283 int save_make_backups = make_backups;
284 int i, recv_ok, phase = 0;
287 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
289 if (flist->hlink_pool) {
290 pool_destroy(flist->hlink_pool);
291 flist->hlink_pool = NULL;
295 int sz = (flist->count + 7) / 8;
296 if (!(delayed_bits = new_array(uchar, sz)))
297 out_of_memory("recv_files");
298 memset(delayed_bits, 0, sz);
307 if (next_gen_i != flist->count) {
311 read_byte(f_in_name);
312 } while (read_int(batch_gen_fd) != -1);
321 csum_length = SUM_LENGTH;
323 rprintf(FINFO, "recv_files phase=%d\n", phase);
324 send_msg(MSG_DONE, "", 0);
325 if (keep_partial && !partial_dir)
326 make_backups = 0; /* prevents double backup */
330 if (i < 0 || i >= flist->count) {
331 rprintf(FERROR,"Invalid file index %d in recv_files (count=%d)\n",
333 exit_cleanup(RERR_PROTOCOL);
336 file = flist->files[i];
337 if (S_ISDIR(file->mode)) {
338 rprintf(FERROR, "[%s] got index of directory: %d\n",
340 exit_cleanup(RERR_PROTOCOL);
343 stats.current_file_index = i;
344 stats.num_transferred_files++;
345 stats.total_transferred_size += file->length;
346 cleanup_got_literal = 0;
348 fname = local_name ? local_name : f_name_to(file, fbuf);
350 if (server_filter_list.head
351 && check_filter(&server_filter_list, fname, 0) < 0) {
352 rprintf(FERROR, "attempt to hack rsync failed.\n");
353 exit_cleanup(RERR_PROTOCOL);
357 rprintf(FINFO, "recv_files(%s)\n", safe_fname(fname));
360 if (!am_server && verbose) /* log the transfer */
361 rprintf(FINFO, "%s\n", safe_fname(fname));
365 initial_stats = stats;
368 while (i > next_gen_i) {
369 if (f_in_name >= 0 && next_gen_i >= 0)
370 read_byte(f_in_name);
371 next_gen_i = read_int(batch_gen_fd);
372 if (next_gen_i == -1)
373 next_gen_i = flist->count;
375 if (i < next_gen_i) {
376 rprintf(FINFO, "skipping update for \"%s\"\n",
378 discard_receive_data(f_in, file->length);
384 partialptr = partial_dir ? partial_dir_fname(fname) : fname;
386 if (f_in_name >= 0) {
388 switch (j = read_byte(f_in_name)) {
392 case FNAMECMP_PARTIAL_DIR:
393 fnamecmp = partialptr ? partialptr : fname;
395 case FNAMECMP_BACKUP:
396 fnamecmp = get_backup_name(fname);
399 if (j >= basis_dir_cnt) {
401 "invalid basis_dir index: %d.\n",
403 exit_cleanup(RERR_PROTOCOL);
405 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
406 basis_dir[j], fname);
407 fnamecmp = fnamecmpbuf;
414 fd1 = do_open(fnamecmp, O_RDONLY, 0);
416 if (fd1 != -1 && do_fstat(fd1,&st) != 0) {
417 rsyserr(FERROR, errno, "fstat %s failed",
418 full_fname(fnamecmp));
419 discard_receive_data(f_in, file->length);
424 if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
425 /* this special handling for directories
426 * wouldn't be necessary if robust_rename()
427 * and the underlying robust_unlink could cope
430 rprintf(FERROR,"recv_files: %s is a directory\n",
431 full_fname(fnamecmp));
432 discard_receive_data(f_in, file->length);
437 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
442 if (fd1 != -1 && !preserve_perms) {
443 /* if the file exists already and we aren't preserving
444 * permissions then act as though the remote end sent
445 * us the file permissions we already have */
446 file->mode = st.st_mode;
449 /* We now check to see if we are writing file "inplace" */
451 fd2 = do_open(fname, O_WRONLY|O_CREAT, 0);
453 rsyserr(FERROR, errno, "open %s failed",
455 discard_receive_data(f_in, file->length);
461 if (!get_tmpname(fnametmp,fname)) {
462 discard_receive_data(f_in, file->length);
468 strlcpy(template, fnametmp, sizeof template);
470 /* we initially set the perms without the
471 * setuid/setgid bits to ensure that there is no race
472 * condition. They are then correctly updated after
473 * the lchown. Thanks to snabb@epipe.fi for pointing
474 * this out. We also set it initially without group
475 * access because of a similar race condition. */
476 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
478 /* in most cases parent directories will already exist
479 * because their information should have been previously
480 * transferred, but that may not be the case with -R */
481 if (fd2 == -1 && relative_paths && errno == ENOENT
482 && create_directory_path(fnametmp, orig_umask) == 0) {
483 strlcpy(fnametmp, template, sizeof fnametmp);
484 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
487 rsyserr(FERROR, errno, "mkstemp %s failed",
488 full_fname(fnametmp));
489 discard_receive_data(f_in, file->length);
496 cleanup_set(fnametmp, partialptr, file, fd1, fd2);
499 if (!am_server && verbose) /* log the transfer */
500 rprintf(FINFO, "%s\n", safe_fname(fname));
503 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
504 fname, fd2, file->length);
506 log_recv(file, &initial_stats);
510 if (close(fd2) < 0) {
511 rsyserr(FERROR, errno, "close failed on %s",
512 full_fname(fnametmp));
513 exit_cleanup(RERR_FILEIO);
516 if ((recv_ok && !delay_updates) || inplace) {
517 finish_transfer(fname, fnametmp, file, recv_ok, 1);
518 if (partialptr != fname && fnamecmp == partialptr) {
519 do_unlink(partialptr);
520 handle_partial_dir(partialptr, PDIR_DELETE);
522 } else if (keep_partial && partialptr
523 && handle_partial_dir(partialptr, PDIR_CREATE)) {
524 finish_transfer(partialptr, fnametmp, file, recv_ok,
526 if (delay_updates && recv_ok)
527 delayed_bits[i/8] |= 1 << (i % 8);
536 int msgtype = csum_length == SUM_LENGTH || read_batch ?
538 if (msgtype == FERROR || verbose) {
539 char *errstr, *redostr, *keptstr;
540 if (!(keep_partial && partialptr) && !inplace)
541 keptstr = "discarded";
542 else if (partial_dir)
543 keptstr = "put into partial-dir";
545 keptstr = "retained";
546 if (msgtype == FERROR) {
551 redostr = " (will try again)";
554 "%s: %s failed verification -- update %s%s.\n",
555 errstr, safe_fname(fname),
558 if (csum_length != SUM_LENGTH) {
561 send_msg(MSG_REDO, buf, 4);
565 make_backups = save_make_backups;
568 for (i = 0; i < flist->count; i++) {
569 struct file_struct *file = flist->files[i];
571 || !(delayed_bits[i/8] & (1 << (i % 8))))
573 fname = local_name ? local_name : f_name(file);
574 partialptr = partial_dir_fname(fname);
576 if (make_backups && !make_backup(fname))
579 rprintf(FINFO, "renaming %s to %s\n",
582 if (do_rename(partialptr, fname) < 0) {
583 rsyserr(FERROR, errno,
584 "rename failed for %s (from %s)",
587 handle_partial_dir(partialptr,
594 if (delete_after && !local_name && flist->count > 0)
598 rprintf(FINFO,"recv_files finished\n");