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