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