The --delete-excluded code now also drops any merge file that was
[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 along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
21 */
22
23#include "rsync.h"
24
25extern int verbose;
26extern int do_xfers;
27extern int am_server;
28extern int do_progress;
29extern int log_before_transfer;
30extern int stdout_format_has_i;
31extern int logfile_format_has_i;
32extern int csum_length;
33extern int read_batch;
34extern int write_batch;
35extern int batch_gen_fd;
36extern int protocol_version;
37extern int relative_paths;
38extern int preserve_hard_links;
39extern int preserve_perms;
40extern int basis_dir_cnt;
41extern int make_backups;
42extern int cleanup_got_literal;
43extern int remove_source_files;
44extern int append_mode;
45extern int sparse_files;
46extern int keep_partial;
47extern int checksum_seed;
48extern int inplace;
49extern int delay_updates;
50extern struct stats stats;
51extern char *stdout_format;
52extern char *tmpdir;
53extern char *partial_dir;
54extern char *basis_dir[];
55extern struct file_list *the_file_list;
56extern struct filter_list_struct server_filter_list;
57
58static struct bitbag *delayed_bits = NULL;
59static int phase = 0;
60
61
62/*
63 * get_tmpname() - create a tmp filename for a given filename
64 *
65 * If a tmpdir is defined, use that as the directory to
66 * put it in. Otherwise, the tmp filename is in the same
67 * directory as the given name. Note that there may be no
68 * directory at all in the given name!
69 *
70 * The tmp filename is basically the given filename with a
71 * dot prepended, and .XXXXXX appended (for mkstemp() to
72 * put its unique gunk in). Take care to not exceed
73 * either the MAXPATHLEN or NAME_MAX, esp. the last, as
74 * the basename basically becomes 8 chars longer. In that
75 * case, the original name is shortened sufficiently to
76 * make it all fit.
77 *
78 * Of course, there's no real reason for the tmp name to
79 * look like the original, except to satisfy us humans.
80 * As long as it's unique, rsync will work.
81 */
82
83static int get_tmpname(char *fnametmp, char *fname)
84{
85 char *f;
86 int length = 0;
87 int maxname;
88
89 if (tmpdir) {
90 /* Note: this can't overflow, so the return value is safe */
91 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
92 fnametmp[length++] = '/';
93 fnametmp[length] = '\0'; /* always NULL terminated */
94 }
95
96 if ((f = strrchr(fname, '/')) != NULL) {
97 ++f;
98 if (!tmpdir) {
99 length = f - fname;
100 /* copy up to and including the slash */
101 strlcpy(fnametmp, fname, length + 1);
102 }
103 } else
104 f = fname;
105 fnametmp[length++] = '.';
106 fnametmp[length] = '\0'; /* always NULL terminated */
107
108 maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8);
109
110 if (maxname < 1) {
111 rprintf(FERROR, "temporary filename too long: %s\n", fname);
112 fnametmp[0] = '\0';
113 return 0;
114 }
115
116 strlcpy(fnametmp + length, f, maxname);
117 strcat(fnametmp + length, ".XXXXXX");
118
119 return 1;
120}
121
122
123static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
124 char *fname, int fd, OFF_T total_size)
125{
126 static char file_sum1[MD4_SUM_LENGTH];
127 static char file_sum2[MD4_SUM_LENGTH];
128 struct map_struct *mapbuf;
129 struct sum_struct sum;
130 int32 len;
131 OFF_T offset = 0;
132 OFF_T offset2;
133 char *data;
134 int32 i;
135 char *map = NULL;
136
137 read_sum_head(f_in, &sum);
138
139 if (fd_r >= 0 && size_r > 0) {
140 int32 read_size = MAX(sum.blength * 2, 16*1024);
141 mapbuf = map_file(fd_r, size_r, read_size, sum.blength);
142 if (verbose > 2) {
143 rprintf(FINFO, "recv mapped %s of size %.0f\n",
144 fname_r, (double)size_r);
145 }
146 } else
147 mapbuf = NULL;
148
149 sum_init(checksum_seed);
150
151 if (append_mode) {
152 OFF_T j;
153 sum.flength = (OFF_T)sum.count * sum.blength;
154 if (sum.remainder)
155 sum.flength -= sum.blength - sum.remainder;
156 for (j = CHUNK_SIZE; j < sum.flength; j += CHUNK_SIZE) {
157 if (do_progress)
158 show_progress(offset, total_size);
159 sum_update(map_ptr(mapbuf, offset, CHUNK_SIZE),
160 CHUNK_SIZE);
161 offset = j;
162 }
163 if (offset < sum.flength) {
164 int32 len = sum.flength - offset;
165 if (do_progress)
166 show_progress(offset, total_size);
167 sum_update(map_ptr(mapbuf, offset, len), len);
168 offset = sum.flength;
169 }
170 if (fd != -1 && (j = do_lseek(fd, offset, SEEK_SET)) != offset) {
171 rsyserr(FERROR, errno, "lseek of %s returned %.0f, not %.0f",
172 full_fname(fname), (double)j, (double)offset);
173 exit_cleanup(RERR_FILEIO);
174 }
175 }
176
177 while ((i = recv_token(f_in, &data)) != 0) {
178 if (do_progress)
179 show_progress(offset, total_size);
180
181 if (i > 0) {
182 if (verbose > 3) {
183 rprintf(FINFO,"data recv %d at %.0f\n",
184 i,(double)offset);
185 }
186
187 stats.literal_data += i;
188 cleanup_got_literal = 1;
189
190 sum_update(data, i);
191
192 if (fd != -1 && write_file(fd,data,i) != i)
193 goto report_write_error;
194 offset += i;
195 continue;
196 }
197
198 i = -(i+1);
199 offset2 = i * (OFF_T)sum.blength;
200 len = sum.blength;
201 if (i == (int)sum.count-1 && sum.remainder != 0)
202 len = sum.remainder;
203
204 stats.matched_data += len;
205
206 if (verbose > 3) {
207 rprintf(FINFO,
208 "chunk[%d] of size %ld at %.0f offset=%.0f\n",
209 i, (long)len, (double)offset2, (double)offset);
210 }
211
212 if (mapbuf) {
213 map = map_ptr(mapbuf,offset2,len);
214
215 see_token(map, len);
216 sum_update(map, len);
217 }
218
219 if (inplace) {
220 if (offset == offset2 && fd != -1) {
221 OFF_T pos;
222 if (flush_write_file(fd) < 0)
223 goto report_write_error;
224 offset += len;
225 if ((pos = do_lseek(fd, len, SEEK_CUR)) != offset) {
226 rsyserr(FERROR, errno,
227 "lseek of %s returned %.0f, not %.0f",
228 full_fname(fname),
229 (double)pos, (double)offset);
230 exit_cleanup(RERR_FILEIO);
231 }
232 continue;
233 }
234 }
235 if (fd != -1 && map && write_file(fd, map, len) != (int)len)
236 goto report_write_error;
237 offset += len;
238 }
239
240 if (flush_write_file(fd) < 0)
241 goto report_write_error;
242
243#ifdef HAVE_FTRUNCATE
244 if (inplace && fd != -1)
245 ftruncate(fd, offset);
246#endif
247
248 if (do_progress)
249 end_progress(total_size);
250
251 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
252 report_write_error:
253 rsyserr(FERROR, errno, "write failed on %s",
254 full_fname(fname));
255 exit_cleanup(RERR_FILEIO);
256 }
257
258 sum_end(file_sum1);
259
260 if (mapbuf)
261 unmap_file(mapbuf);
262
263 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
264 if (verbose > 2)
265 rprintf(FINFO,"got file_sum\n");
266 if (fd != -1 && memcmp(file_sum1, file_sum2, MD4_SUM_LENGTH) != 0)
267 return 0;
268 return 1;
269}
270
271
272static void discard_receive_data(int f_in, OFF_T length)
273{
274 receive_data(f_in, NULL, -1, 0, NULL, -1, length);
275}
276
277static void handle_delayed_updates(struct file_list *flist, char *local_name)
278{
279 char *fname, *partialptr, numbuf[4];
280 int i;
281
282 for (i = -1; (i = bitbag_next_bit(delayed_bits, i)) >= 0; ) {
283 struct file_struct *file = flist->files[i];
284 fname = local_name ? local_name : f_name(file, NULL);
285 if ((partialptr = partial_dir_fname(fname)) != NULL) {
286 if (make_backups && !make_backup(fname))
287 continue;
288 if (verbose > 2) {
289 rprintf(FINFO, "renaming %s to %s\n",
290 partialptr, fname);
291 }
292 /* We don't use robust_rename() here because the
293 * partial-dir must be on the same drive. */
294 if (do_rename(partialptr, fname) < 0) {
295 rsyserr(FERROR, errno,
296 "rename failed for %s (from %s)",
297 full_fname(fname), partialptr);
298 } else {
299 if (remove_source_files
300 || (preserve_hard_links
301 && file->link_u.links)) {
302 SIVAL(numbuf, 0, i);
303 send_msg(MSG_SUCCESS,numbuf,4);
304 }
305 handle_partial_dir(partialptr, PDIR_DELETE);
306 }
307 }
308 }
309}
310
311static int get_next_gen_i(int batch_gen_fd, int next_gen_i, int desired_i)
312{
313 while (next_gen_i < desired_i) {
314 if (next_gen_i >= 0) {
315 rprintf(FINFO,
316 "(No batched update for%s \"%s\")\n",
317 phase ? " resend of" : "",
318 f_name(the_file_list->files[next_gen_i], NULL));
319 }
320 next_gen_i = read_int(batch_gen_fd);
321 if (next_gen_i == -1)
322 next_gen_i = the_file_list->count;
323 }
324 return next_gen_i;
325}
326
327
328/**
329 * main routine for receiver process.
330 *
331 * Receiver process runs on the same host as the generator process. */
332int recv_files(int f_in, struct file_list *flist, char *local_name)
333{
334 int next_gen_i = -1;
335 int fd1,fd2;
336 STRUCT_STAT st;
337 int iflags, xlen;
338 char *fname, fbuf[MAXPATHLEN];
339 char xname[MAXPATHLEN];
340 char fnametmp[MAXPATHLEN];
341 char *fnamecmp, *partialptr, numbuf[4];
342 char fnamecmpbuf[MAXPATHLEN];
343 uchar fnamecmp_type;
344 struct file_struct *file;
345 struct stats initial_stats;
346 int save_make_backups = make_backups;
347 int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i;
348 enum logcode log_code = log_before_transfer ? FLOG : FINFO;
349 int max_phase = protocol_version >= 29 ? 2 : 1;
350 int i, recv_ok;
351
352 if (verbose > 2)
353 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
354
355 if (flist->hlink_pool) {
356 pool_destroy(flist->hlink_pool);
357 flist->hlink_pool = NULL;
358 }
359
360 if (delay_updates)
361 delayed_bits = bitbag_create(flist->count);
362
363 while (1) {
364 cleanup_disable();
365
366 i = read_int(f_in);
367 if (i == -1) {
368 if (read_batch) {
369 get_next_gen_i(batch_gen_fd, next_gen_i,
370 flist->count);
371 next_gen_i = -1;
372 }
373 if (++phase > max_phase)
374 break;
375 csum_length = SUM_LENGTH;
376 if (verbose > 2)
377 rprintf(FINFO, "recv_files phase=%d\n", phase);
378 if (phase == 2 && delay_updates)
379 handle_delayed_updates(flist, local_name);
380 send_msg(MSG_DONE, "", 0);
381 if (keep_partial && !partial_dir)
382 make_backups = 0; /* prevents double backup */
383 if (append_mode) {
384 append_mode = 0;
385 sparse_files = 0;
386 }
387 continue;
388 }
389
390 iflags = read_item_attrs(f_in, -1, i, &fnamecmp_type,
391 xname, &xlen);
392 if (iflags == ITEM_IS_NEW) /* no-op packet */
393 continue;
394
395 file = flist->files[i];
396 fname = local_name ? local_name : f_name(file, fbuf);
397
398 if (verbose > 2)
399 rprintf(FINFO, "recv_files(%s)\n", fname);
400
401 if (!(iflags & ITEM_TRANSFER)) {
402 maybe_log_item(file, iflags, itemizing, xname);
403 continue;
404 }
405 if (phase == 2) {
406 rprintf(FERROR,
407 "got transfer request in phase 2 [%s]\n",
408 who_am_i());
409 exit_cleanup(RERR_PROTOCOL);
410 }
411
412 stats.current_file_index = i;
413 stats.num_transferred_files++;
414 stats.total_transferred_size += file->length;
415 cleanup_got_literal = 0;
416
417 if (server_filter_list.head
418 && check_filter(&server_filter_list, fname, 0) < 0) {
419 rprintf(FERROR, "attempt to hack rsync failed.\n");
420 exit_cleanup(RERR_PROTOCOL);
421 }
422
423 if (!do_xfers) { /* log the transfer */
424 log_item(FCLIENT, 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(FINFO, 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 cleanup_set(fnametmp, partialptr, file, fd1, fd2);
595 }
596
597 /* log the transfer */
598 if (log_before_transfer)
599 log_item(FCLIENT, file, &initial_stats, iflags, NULL);
600 else if (!am_server && verbose && do_progress)
601 rprintf(FINFO, "%s\n", fname);
602
603 /* recv file data */
604 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
605 fname, fd2, file->length);
606
607 log_item(log_code, file, &initial_stats, iflags, NULL);
608
609 if (fd1 != -1)
610 close(fd1);
611 if (close(fd2) < 0) {
612 rsyserr(FERROR, errno, "close failed on %s",
613 full_fname(fnametmp));
614 exit_cleanup(RERR_FILEIO);
615 }
616
617 if ((recv_ok && (!delay_updates || !partialptr)) || inplace) {
618 char *temp_copy_name;
619 if (partialptr == fname)
620 partialptr = temp_copy_name = NULL;
621 else if (*partial_dir == '/')
622 temp_copy_name = NULL;
623 else
624 temp_copy_name = partialptr;
625 finish_transfer(fname, fnametmp, temp_copy_name,
626 file, recv_ok, 1);
627 if (fnamecmp == partialptr) {
628 do_unlink(partialptr);
629 handle_partial_dir(partialptr, PDIR_DELETE);
630 }
631 } else if (keep_partial && partialptr
632 && handle_partial_dir(partialptr, PDIR_CREATE)) {
633 finish_transfer(partialptr, fnametmp, NULL,
634 file, recv_ok, !partial_dir);
635 if (delay_updates && recv_ok) {
636 bitbag_set_bit(delayed_bits, i);
637 recv_ok = -1;
638 }
639 } else {
640 partialptr = NULL;
641 do_unlink(fnametmp);
642 }
643
644 cleanup_disable();
645
646 if (recv_ok > 0) {
647 if (remove_source_files
648 || (preserve_hard_links && file->link_u.links)) {
649 SIVAL(numbuf, 0, i);
650 send_msg(MSG_SUCCESS, numbuf, 4);
651 }
652 } else if (!recv_ok) {
653 int msgtype = phase || read_batch ? FERROR : FINFO;
654 if (msgtype == FERROR || verbose) {
655 char *errstr, *redostr, *keptstr;
656 if (!(keep_partial && partialptr) && !inplace)
657 keptstr = "discarded";
658 else if (partial_dir)
659 keptstr = "put into partial-dir";
660 else
661 keptstr = "retained";
662 if (msgtype == FERROR) {
663 errstr = "ERROR";
664 redostr = "";
665 } else {
666 errstr = "WARNING";
667 redostr = " (will try again)";
668 }
669 rprintf(msgtype,
670 "%s: %s failed verification -- update %s%s.\n",
671 errstr, fname, keptstr, redostr);
672 }
673 if (!phase) {
674 SIVAL(numbuf, 0, i);
675 send_msg(MSG_REDO, numbuf, 4);
676 }
677 }
678 }
679 make_backups = save_make_backups;
680
681 if (phase == 2 && delay_updates) /* for protocol_version < 29 */
682 handle_delayed_updates(flist, local_name);
683
684 if (verbose > 2)
685 rprintf(FINFO,"recv_files finished\n");
686
687 return 0;
688}