- Updated the address for the FSF in the opening comment.
[rsync/rsync.git] / receiver.c
... / ...
CommitLineData
1/*
2 * Routines only used by the receiving process.
3 *
4 * Copyright (C) 1996-2000 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2003, 2004, 2005, 2006 Wayne Davison
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23#include "rsync.h"
24
25extern int verbose;
26extern int do_xfers;
27extern int am_daemon;
28extern int am_server;
29extern int do_progress;
30extern int log_before_transfer;
31extern int log_format_has_i;
32extern int daemon_log_format_has_i;
33extern int csum_length;
34extern int read_batch;
35extern int write_batch;
36extern int batch_gen_fd;
37extern int protocol_version;
38extern int relative_paths;
39extern int preserve_hard_links;
40extern int preserve_perms;
41extern int basis_dir_cnt;
42extern int make_backups;
43extern int cleanup_got_literal;
44extern int remove_sent_files;
45extern int append_mode;
46extern int sparse_files;
47extern int keep_partial;
48extern int checksum_seed;
49extern int inplace;
50extern int delay_updates;
51extern struct stats stats;
52extern char *log_format;
53extern char *tmpdir;
54extern char *partial_dir;
55extern char *basis_dir[];
56extern struct file_list *the_file_list;
57extern struct filter_list_struct server_filter_list;
58
59static struct bitbag *delayed_bits = NULL;
60static int phase = 0;
61
62
63/*
64 * get_tmpname() - create a tmp filename for a given filename
65 *
66 * If a tmpdir is defined, use that as the directory to
67 * put it in. Otherwise, the tmp filename is in the same
68 * directory as the given name. Note that there may be no
69 * directory at all in the given name!
70 *
71 * The tmp filename is basically the given filename with a
72 * dot prepended, and .XXXXXX appended (for mkstemp() to
73 * put its unique gunk in). Take care to not exceed
74 * either the MAXPATHLEN or NAME_MAX, esp. the last, as
75 * the basename basically becomes 8 chars longer. In that
76 * case, the original name is shortened sufficiently to
77 * make it all fit.
78 *
79 * Of course, there's no real reason for the tmp name to
80 * look like the original, except to satisfy us humans.
81 * As long as it's unique, rsync will work.
82 */
83
84static int get_tmpname(char *fnametmp, char *fname)
85{
86 char *f;
87 int length = 0;
88 int maxname;
89
90 if (tmpdir) {
91 /* Note: this can't overflow, so the return value is safe */
92 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
93 fnametmp[length++] = '/';
94 fnametmp[length] = '\0'; /* always NULL terminated */
95 }
96
97 if ((f = strrchr(fname, '/')) != NULL) {
98 ++f;
99 if (!tmpdir) {
100 length = f - fname;
101 /* copy up to and including the slash */
102 strlcpy(fnametmp, fname, length + 1);
103 }
104 } else
105 f = fname;
106 fnametmp[length++] = '.';
107 fnametmp[length] = '\0'; /* always NULL terminated */
108
109 maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8);
110
111 if (maxname < 1) {
112 rprintf(FERROR, "temporary filename too long: %s\n", fname);
113 fnametmp[0] = '\0';
114 return 0;
115 }
116
117 strlcpy(fnametmp + length, f, maxname);
118 strcat(fnametmp + length, ".XXXXXX");
119
120 return 1;
121}
122
123
124static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
125 char *fname, int fd, OFF_T total_size)
126{
127 static char file_sum1[MD4_SUM_LENGTH];
128 static char file_sum2[MD4_SUM_LENGTH];
129 struct map_struct *mapbuf;
130 struct sum_struct sum;
131 int32 len;
132 OFF_T offset = 0;
133 OFF_T offset2;
134 char *data;
135 int32 i;
136 char *map = NULL;
137
138 read_sum_head(f_in, &sum);
139
140 if (fd_r >= 0 && size_r > 0) {
141 int32 read_size = MAX(sum.blength * 2, 16*1024);
142 mapbuf = map_file(fd_r, size_r, read_size, sum.blength);
143 if (verbose > 2) {
144 rprintf(FINFO, "recv mapped %s of size %.0f\n",
145 fname_r, (double)size_r);
146 }
147 } else
148 mapbuf = NULL;
149
150 sum_init(checksum_seed);
151
152 if (append_mode) {
153 OFF_T j;
154 sum.flength = (OFF_T)sum.count * sum.blength;
155 if (sum.remainder)
156 sum.flength -= sum.blength - sum.remainder;
157 for (j = CHUNK_SIZE; j < sum.flength; j += CHUNK_SIZE) {
158 if (do_progress)
159 show_progress(offset, total_size);
160 sum_update(map_ptr(mapbuf, offset, CHUNK_SIZE),
161 CHUNK_SIZE);
162 offset = j;
163 }
164 if (offset < sum.flength) {
165 int32 len = sum.flength - offset;
166 if (do_progress)
167 show_progress(offset, total_size);
168 sum_update(map_ptr(mapbuf, offset, len), len);
169 offset = sum.flength;
170 }
171 if (fd != -1 && do_lseek(fd, offset, SEEK_SET) != offset) {
172 rsyserr(FERROR, errno, "lseek failed on %s",
173 full_fname(fname));
174 exit_cleanup(RERR_FILEIO);
175 }
176 }
177
178 while ((i = recv_token(f_in, &data)) != 0) {
179 if (do_progress)
180 show_progress(offset, total_size);
181
182 if (i > 0) {
183 if (verbose > 3) {
184 rprintf(FINFO,"data recv %d at %.0f\n",
185 i,(double)offset);
186 }
187
188 stats.literal_data += i;
189 cleanup_got_literal = 1;
190
191 sum_update(data, i);
192
193 if (fd != -1 && write_file(fd,data,i) != i)
194 goto report_write_error;
195 offset += i;
196 continue;
197 }
198
199 i = -(i+1);
200 offset2 = i * (OFF_T)sum.blength;
201 len = sum.blength;
202 if (i == (int)sum.count-1 && sum.remainder != 0)
203 len = sum.remainder;
204
205 stats.matched_data += len;
206
207 if (verbose > 3) {
208 rprintf(FINFO,
209 "chunk[%d] of size %ld at %.0f offset=%.0f\n",
210 i, (long)len, (double)offset2, (double)offset);
211 }
212
213 if (mapbuf) {
214 map = map_ptr(mapbuf,offset2,len);
215
216 see_token(map, len);
217 sum_update(map, len);
218 }
219
220 if (inplace) {
221 if (offset == offset2 && fd != -1) {
222 if (flush_write_file(fd) < 0)
223 goto report_write_error;
224 offset += len;
225 if (do_lseek(fd, len, SEEK_CUR) != offset) {
226 rsyserr(FERROR, errno,
227 "lseek failed on %s",
228 full_fname(fname));
229 exit_cleanup(RERR_FILEIO);
230 }
231 continue;
232 }
233 }
234 if (fd != -1 && map && write_file(fd, map, len) != (int)len)
235 goto report_write_error;
236 offset += len;
237 }
238
239 if (flush_write_file(fd) < 0)
240 goto report_write_error;
241
242#ifdef HAVE_FTRUNCATE
243 if (inplace && fd != -1)
244 ftruncate(fd, offset);
245#endif
246
247 if (do_progress)
248 end_progress(total_size);
249
250 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
251 report_write_error:
252 rsyserr(FERROR, errno, "write failed on %s",
253 full_fname(fname));
254 exit_cleanup(RERR_FILEIO);
255 }
256
257 sum_end(file_sum1);
258
259 if (mapbuf)
260 unmap_file(mapbuf);
261
262 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
263 if (verbose > 2)
264 rprintf(FINFO,"got file_sum\n");
265 if (fd != -1 && memcmp(file_sum1, file_sum2, MD4_SUM_LENGTH) != 0)
266 return 0;
267 return 1;
268}
269
270
271static void discard_receive_data(int f_in, OFF_T length)
272{
273 receive_data(f_in, NULL, -1, 0, NULL, -1, length);
274}
275
276static void handle_delayed_updates(struct file_list *flist, char *local_name)
277{
278 char *fname, *partialptr, numbuf[4];
279 int i;
280
281 for (i = -1; (i = bitbag_next_bit(delayed_bits, i)) >= 0; ) {
282 struct file_struct *file = flist->files[i];
283 fname = local_name ? local_name : f_name(file, NULL);
284 if ((partialptr = partial_dir_fname(fname)) != NULL) {
285 if (make_backups && !make_backup(fname))
286 continue;
287 if (verbose > 2) {
288 rprintf(FINFO, "renaming %s to %s\n",
289 partialptr, fname);
290 }
291 /* We don't use robust_rename() here because the
292 * partial-dir must be on the same drive. */
293 if (do_rename(partialptr, fname) < 0) {
294 rsyserr(FERROR, errno,
295 "rename failed for %s (from %s)",
296 full_fname(fname), partialptr);
297 } else {
298 if (remove_sent_files
299 || (preserve_hard_links
300 && file->link_u.links)) {
301 SIVAL(numbuf, 0, i);
302 send_msg(MSG_SUCCESS,numbuf,4);
303 }
304 handle_partial_dir(partialptr, PDIR_DELETE);
305 }
306 }
307 }
308}
309
310static int get_next_gen_i(int batch_gen_fd, int next_gen_i, int desired_i)
311{
312 while (next_gen_i < desired_i) {
313 if (next_gen_i >= 0) {
314 rprintf(FINFO,
315 "(No batched update for%s \"%s\")\n",
316 phase ? " resend of" : "",
317 f_name(the_file_list->files[next_gen_i], NULL));
318 }
319 next_gen_i = read_int(batch_gen_fd);
320 if (next_gen_i == -1)
321 next_gen_i = the_file_list->count;
322 }
323 return next_gen_i;
324}
325
326
327/**
328 * main routine for receiver process.
329 *
330 * Receiver process runs on the same host as the generator process. */
331int recv_files(int f_in, struct file_list *flist, char *local_name)
332{
333 int next_gen_i = -1;
334 int fd1,fd2;
335 STRUCT_STAT st;
336 int iflags, xlen;
337 char *fname, fbuf[MAXPATHLEN];
338 char xname[MAXPATHLEN];
339 char fnametmp[MAXPATHLEN];
340 char *fnamecmp, *partialptr, numbuf[4];
341 char fnamecmpbuf[MAXPATHLEN];
342 uchar fnamecmp_type;
343 struct file_struct *file;
344 struct stats initial_stats;
345 int save_make_backups = make_backups;
346 int itemizing = am_daemon ? daemon_log_format_has_i
347 : !am_server && log_format_has_i;
348 int max_phase = protocol_version >= 29 ? 2 : 1;
349 int i, recv_ok;
350
351 if (verbose > 2)
352 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
353
354 if (flist->hlink_pool) {
355 pool_destroy(flist->hlink_pool);
356 flist->hlink_pool = NULL;
357 }
358
359 if (delay_updates)
360 delayed_bits = bitbag_create(flist->count);
361
362 while (1) {
363 cleanup_disable();
364
365 i = read_int(f_in);
366 if (i == -1) {
367 if (read_batch) {
368 get_next_gen_i(batch_gen_fd, next_gen_i,
369 flist->count);
370 next_gen_i = -1;
371 }
372 if (++phase > max_phase)
373 break;
374 csum_length = SUM_LENGTH;
375 if (verbose > 2)
376 rprintf(FINFO, "recv_files phase=%d\n", phase);
377 if (phase == 2 && delay_updates)
378 handle_delayed_updates(flist, local_name);
379 send_msg(MSG_DONE, "", 0);
380 if (keep_partial && !partial_dir)
381 make_backups = 0; /* prevents double backup */
382 if (append_mode) {
383 append_mode = 0;
384 sparse_files = 0;
385 }
386 continue;
387 }
388
389 iflags = read_item_attrs(f_in, -1, i, &fnamecmp_type,
390 xname, &xlen);
391 if (iflags == ITEM_IS_NEW) /* no-op packet */
392 continue;
393
394 file = flist->files[i];
395 fname = local_name ? local_name : f_name(file, fbuf);
396
397 if (verbose > 2)
398 rprintf(FINFO, "recv_files(%s)\n", fname);
399
400 if (!(iflags & ITEM_TRANSFER)) {
401 maybe_log_item(file, iflags, itemizing, xname);
402 continue;
403 }
404 if (phase == 2) {
405 rprintf(FERROR,
406 "got transfer request in phase 2 [%s]\n",
407 who_am_i());
408 exit_cleanup(RERR_PROTOCOL);
409 }
410
411 stats.current_file_index = i;
412 stats.num_transferred_files++;
413 stats.total_transferred_size += file->length;
414 cleanup_got_literal = 0;
415
416 if (server_filter_list.head
417 && check_filter(&server_filter_list, fname, 0) < 0) {
418 rprintf(FERROR, "attempt to hack rsync failed.\n");
419 exit_cleanup(RERR_PROTOCOL);
420 }
421
422 if (!do_xfers) { /* log the transfer */
423 if (!am_server && log_format)
424 log_item(file, &stats, iflags, NULL);
425 if (read_batch)
426 discard_receive_data(f_in, file->length);
427 continue;
428 }
429 if (write_batch < 0) {
430 log_item(file, &stats, iflags, NULL);
431 if (!am_server)
432 discard_receive_data(f_in, file->length);
433 continue;
434 }
435
436 if (read_batch) {
437 next_gen_i = get_next_gen_i(batch_gen_fd, next_gen_i, i);
438 if (i < next_gen_i) {
439 rprintf(FINFO,
440 "(Skipping batched update for \"%s\")\n",
441 fname);
442 discard_receive_data(f_in, file->length);
443 continue;
444 }
445 next_gen_i = -1;
446 }
447
448 partialptr = partial_dir ? partial_dir_fname(fname) : fname;
449
450 if (protocol_version >= 29) {
451 switch (fnamecmp_type) {
452 case FNAMECMP_FNAME:
453 fnamecmp = fname;
454 break;
455 case FNAMECMP_PARTIAL_DIR:
456 fnamecmp = partialptr;
457 break;
458 case FNAMECMP_BACKUP:
459 fnamecmp = get_backup_name(fname);
460 break;
461 case FNAMECMP_FUZZY:
462 if (file->dirname) {
463 pathjoin(fnamecmpbuf, MAXPATHLEN,
464 file->dirname, xname);
465 fnamecmp = fnamecmpbuf;
466 } else
467 fnamecmp = xname;
468 break;
469 default:
470 if (fnamecmp_type >= basis_dir_cnt) {
471 rprintf(FERROR,
472 "invalid basis_dir index: %d.\n",
473 fnamecmp_type);
474 exit_cleanup(RERR_PROTOCOL);
475 }
476 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
477 basis_dir[fnamecmp_type], fname);
478 fnamecmp = fnamecmpbuf;
479 break;
480 }
481 if (!fnamecmp || (server_filter_list.head
482 && check_filter(&server_filter_list, fname, 0) < 0))
483 fnamecmp = fname;
484 } else {
485 /* Reminder: --inplace && --partial-dir are never
486 * enabled at the same time. */
487 if (inplace && make_backups) {
488 if (!(fnamecmp = get_backup_name(fname)))
489 fnamecmp = fname;
490 } else if (partial_dir && partialptr)
491 fnamecmp = partialptr;
492 else
493 fnamecmp = fname;
494 }
495
496 initial_stats = stats;
497
498 /* open the file */
499 fd1 = do_open(fnamecmp, O_RDONLY, 0);
500
501 if (fd1 == -1 && protocol_version < 29) {
502 if (fnamecmp != fname) {
503 fnamecmp = fname;
504 fd1 = do_open(fnamecmp, O_RDONLY, 0);
505 }
506
507 if (fd1 == -1 && basis_dir[0]) {
508 /* pre-29 allowed only one alternate basis */
509 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
510 basis_dir[0], fname);
511 fnamecmp = fnamecmpbuf;
512 fd1 = do_open(fnamecmp, O_RDONLY, 0);
513 }
514 }
515
516 if (fd1 != -1 && do_fstat(fd1,&st) != 0) {
517 rsyserr(FERROR, errno, "fstat %s failed",
518 full_fname(fnamecmp));
519 discard_receive_data(f_in, file->length);
520 close(fd1);
521 continue;
522 }
523
524 if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
525 /* this special handling for directories
526 * wouldn't be necessary if robust_rename()
527 * and the underlying robust_unlink could cope
528 * with directories
529 */
530 rprintf(FERROR,"recv_files: %s is a directory\n",
531 full_fname(fnamecmp));
532 discard_receive_data(f_in, file->length);
533 close(fd1);
534 continue;
535 }
536
537 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
538 close(fd1);
539 fd1 = -1;
540 }
541
542 /* If we're not preserving permissions, change the file-list's
543 * mode based on the local permissions and some heuristics. */
544 if (!preserve_perms) {
545 int exists = fd1 != -1;
546 file->mode = dest_mode(file->mode, st.st_mode, exists);
547 }
548
549 /* We now check to see if we are writing file "inplace" */
550 if (inplace) {
551 fd2 = do_open(fname, O_WRONLY|O_CREAT, 0600);
552 if (fd2 == -1) {
553 rsyserr(FERROR, errno, "open %s failed",
554 full_fname(fname));
555 discard_receive_data(f_in, file->length);
556 if (fd1 != -1)
557 close(fd1);
558 continue;
559 }
560 } else {
561 if (!get_tmpname(fnametmp,fname)) {
562 discard_receive_data(f_in, file->length);
563 if (fd1 != -1)
564 close(fd1);
565 continue;
566 }
567
568 /* we initially set the perms without the
569 * setuid/setgid bits to ensure that there is no race
570 * condition. They are then correctly updated after
571 * the lchown. Thanks to snabb@epipe.fi for pointing
572 * this out. We also set it initially without group
573 * access because of a similar race condition. */
574 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
575
576 /* in most cases parent directories will already exist
577 * because their information should have been previously
578 * transferred, but that may not be the case with -R */
579 if (fd2 == -1 && relative_paths && errno == ENOENT
580 && create_directory_path(fnametmp) == 0) {
581 /* Get back to name with XXXXXX in it. */
582 get_tmpname(fnametmp, fname);
583 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
584 }
585 if (fd2 == -1) {
586 rsyserr(FERROR, errno, "mkstemp %s failed",
587 full_fname(fnametmp));
588 discard_receive_data(f_in, file->length);
589 if (fd1 != -1)
590 close(fd1);
591 continue;
592 }
593
594 if (keep_partial)
595 cleanup_set(fnametmp, partialptr, file, fd1, fd2);
596 }
597
598 /* log the transfer */
599 if (log_before_transfer)
600 log_item(file, &initial_stats, iflags, NULL);
601 else if (!am_server && verbose && do_progress)
602 rprintf(FINFO, "%s\n", fname);
603
604 /* recv file data */
605 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
606 fname, fd2, file->length);
607
608 if (!log_before_transfer)
609 log_item(file, &initial_stats, iflags, NULL);
610
611 if (fd1 != -1)
612 close(fd1);
613 if (close(fd2) < 0) {
614 rsyserr(FERROR, errno, "close failed on %s",
615 full_fname(fnametmp));
616 exit_cleanup(RERR_FILEIO);
617 }
618
619 if ((recv_ok && (!delay_updates || !partialptr)) || inplace) {
620 if (partialptr == fname || *partial_dir == '/')
621 partialptr = NULL;
622 finish_transfer(fname, fnametmp, partialptr,
623 file, recv_ok, 1);
624 if (fnamecmp == partialptr) {
625 do_unlink(partialptr);
626 handle_partial_dir(partialptr, PDIR_DELETE);
627 }
628 } else if (keep_partial && partialptr
629 && handle_partial_dir(partialptr, PDIR_CREATE)) {
630 finish_transfer(partialptr, fnametmp, NULL,
631 file, recv_ok, !partial_dir);
632 if (delay_updates && recv_ok) {
633 bitbag_set_bit(delayed_bits, i);
634 recv_ok = -1;
635 }
636 } else {
637 partialptr = NULL;
638 do_unlink(fnametmp);
639 }
640
641 cleanup_disable();
642
643 if (recv_ok > 0) {
644 if (remove_sent_files
645 || (preserve_hard_links && file->link_u.links)) {
646 SIVAL(numbuf, 0, i);
647 send_msg(MSG_SUCCESS, numbuf, 4);
648 }
649 } else if (!recv_ok) {
650 int msgtype = phase || read_batch ? FERROR : FINFO;
651 if (msgtype == FERROR || verbose) {
652 char *errstr, *redostr, *keptstr;
653 if (!(keep_partial && partialptr) && !inplace)
654 keptstr = "discarded";
655 else if (partial_dir)
656 keptstr = "put into partial-dir";
657 else
658 keptstr = "retained";
659 if (msgtype == FERROR) {
660 errstr = "ERROR";
661 redostr = "";
662 } else {
663 errstr = "WARNING";
664 redostr = " (will try again)";
665 }
666 rprintf(msgtype,
667 "%s: %s failed verification -- update %s%s.\n",
668 errstr, fname, keptstr, redostr);
669 }
670 if (!phase) {
671 SIVAL(numbuf, 0, i);
672 send_msg(MSG_REDO, numbuf, 4);
673 }
674 }
675 }
676 make_backups = save_make_backups;
677
678 if (phase == 2 && delay_updates) /* for protocol_version < 29 */
679 handle_delayed_updates(flist, local_name);
680
681 if (verbose > 2)
682 rprintf(FINFO,"recv_files finished\n");
683
684 return 0;
685}