Fix the daemon test when running it as root.
[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-2009 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 3 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, visit the http://fsf.org website.
20 */
21
22#include "rsync.h"
23#include "inums.h"
24
25extern int dry_run;
26extern int do_xfers;
27extern int am_server;
28extern int inc_recurse;
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 preserve_xattrs;
41extern int basis_dir_cnt;
42extern int make_backups;
43extern int cleanup_got_literal;
44extern int remove_source_files;
45extern int append_mode;
46extern int sparse_files;
47extern int keep_partial;
48extern int checksum_len;
49extern int checksum_seed;
50extern int inplace;
51extern int delay_updates;
52extern mode_t orig_umask;
53extern struct stats stats;
54extern char *tmpdir;
55extern char *partial_dir;
56extern char *basis_dir[MAX_BASIS_DIRS+1];
57extern char sender_file_sum[MAX_DIGEST_LEN];
58extern struct file_list *cur_flist, *first_flist, *dir_flist;
59extern filter_rule_list daemon_filter_list;
60
61static struct bitbag *delayed_bits = NULL;
62static int phase = 0, redoing = 0;
63static flist_ndx_list batch_redo_list;
64/* We're either updating the basis file or an identical copy: */
65static int updating_basis_or_equiv;
66
67#define TMPNAME_SUFFIX ".XXXXXX"
68#define TMPNAME_SUFFIX_LEN ((int)sizeof TMPNAME_SUFFIX - 1)
69#define MAX_UNIQUE_NUMBER 999999
70#define MAX_UNIQUE_LOOP 100
71
72/* get_tmpname() - create a tmp filename for a given filename
73 *
74 * If a tmpdir is defined, use that as the directory to put it in. Otherwise,
75 * the tmp filename is in the same directory as the given name. Note that
76 * there may be no directory at all in the given name!
77 *
78 * The tmp filename is basically the given filename with a dot prepended, and
79 * .XXXXXX appended (for mkstemp() to put its unique gunk in). We take care
80 * to not exceed either the MAXPATHLEN or NAME_MAX, especially the last, as
81 * the basename basically becomes 8 characters longer. In such a case, the
82 * original name is shortened sufficiently to make it all fit.
83 *
84 * If the make_unique arg is True, the XXXXXX string is replaced with a unique
85 * string that doesn't exist at the time of the check. This is intended to be
86 * used for creating hard links, symlinks, devices, and special files, since
87 * normal files should be handled by mkstemp() for safety.
88 *
89 * Of course, the only reason the file is based on the original name is to
90 * make it easier to figure out what purpose a temp file is serving when a
91 * transfer is in progress. */
92int get_tmpname(char *fnametmp, const char *fname, BOOL make_unique)
93{
94 int maxname, added, length = 0;
95 const char *f;
96 char *suf;
97
98 if (tmpdir) {
99 /* Note: this can't overflow, so the return value is safe */
100 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
101 fnametmp[length++] = '/';
102 }
103
104 if ((f = strrchr(fname, '/')) != NULL) {
105 ++f;
106 if (!tmpdir) {
107 length = f - fname;
108 /* copy up to and including the slash */
109 strlcpy(fnametmp, fname, length + 1);
110 }
111 } else
112 f = fname;
113 fnametmp[length++] = '.';
114
115 /* The maxname value is bufsize, and includes space for the '\0'.
116 * NAME_MAX needs an extra -1 for the name's leading dot. */
117 maxname = MIN(MAXPATHLEN - length - TMPNAME_SUFFIX_LEN,
118 NAME_MAX - 1 - TMPNAME_SUFFIX_LEN);
119
120 if (maxname < 1) {
121 rprintf(FERROR_XFER, "temporary filename too long: %s\n", fname);
122 fnametmp[0] = '\0';
123 return 0;
124 }
125
126 added = strlcpy(fnametmp + length, f, maxname);
127 if (added >= maxname)
128 added = maxname - 1;
129 suf = fnametmp + length + added;
130
131 if (make_unique) {
132 static unsigned counter_limit;
133 unsigned counter;
134
135 if (!counter_limit) {
136 counter_limit = (unsigned)getpid() + MAX_UNIQUE_LOOP;
137 if (counter_limit > MAX_UNIQUE_NUMBER || counter_limit < MAX_UNIQUE_LOOP)
138 counter_limit = MAX_UNIQUE_LOOP;
139 }
140 counter = counter_limit - MAX_UNIQUE_LOOP;
141
142 /* This doesn't have to be very good because we don't need
143 * to worry about someone trying to guess the values: all
144 * a conflict will do is cause a device, special file, hard
145 * link, or symlink to fail to be created. Also: avoid
146 * using mktemp() due to gcc's annoying warning. */
147 while (1) {
148 snprintf(suf, TMPNAME_SUFFIX_LEN+1, ".%d", counter);
149 if (access(fnametmp, 0) < 0)
150 break;
151 if (++counter >= counter_limit)
152 return 0;
153 }
154 } else
155 memcpy(suf, TMPNAME_SUFFIX, TMPNAME_SUFFIX_LEN+1);
156
157 return 1;
158}
159
160/* Opens a temporary file for writing.
161 * Success: Writes name into fnametmp, returns fd.
162 * Failure: Clobbers fnametmp, returns -1.
163 * Calling cleanup_set() is the caller's job. */
164int open_tmpfile(char *fnametmp, const char *fname, struct file_struct *file)
165{
166 int fd;
167
168 if (!get_tmpname(fnametmp, fname, False))
169 return -1;
170
171 /* We initially set the perms without the setuid/setgid bits or group
172 * access to ensure that there is no race condition. They will be
173 * correctly updated after the right owner and group info is set.
174 * (Thanks to snabb@epipe.fi for pointing this out.) */
175 fd = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
176
177#if 0
178 /* In most cases parent directories will already exist because their
179 * information should have been previously transferred, but that may
180 * not be the case with -R */
181 if (fd == -1 && relative_paths && errno == ENOENT
182 && make_path(fnametmp, MKP_SKIP_SLASH | MKP_DROP_NAME) == 0) {
183 /* Get back to name with XXXXXX in it. */
184 get_tmpname(fnametmp, fname, False);
185 fd = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
186 }
187#endif
188
189 if (fd == -1) {
190 rsyserr(FERROR_XFER, errno, "mkstemp %s failed",
191 full_fname(fnametmp));
192 return -1;
193 }
194
195 return fd;
196}
197
198static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
199 const char *fname, int fd, OFF_T total_size)
200{
201 static char file_sum1[MAX_DIGEST_LEN];
202 struct map_struct *mapbuf;
203 struct sum_struct sum;
204 int32 len;
205 OFF_T offset = 0;
206 OFF_T offset2;
207 char *data;
208 int32 i;
209 char *map = NULL;
210
211 read_sum_head(f_in, &sum);
212
213 if (fd_r >= 0 && size_r > 0) {
214 int32 read_size = MAX(sum.blength * 2, 16*1024);
215 mapbuf = map_file(fd_r, size_r, read_size, sum.blength);
216 if (DEBUG_GTE(DELTASUM, 2)) {
217 rprintf(FINFO, "recv mapped %s of size %s\n",
218 fname_r, big_num(size_r));
219 }
220 } else
221 mapbuf = NULL;
222
223 sum_init(checksum_seed);
224
225 if (append_mode > 0) {
226 OFF_T j;
227 sum.flength = (OFF_T)sum.count * sum.blength;
228 if (sum.remainder)
229 sum.flength -= sum.blength - sum.remainder;
230 if (append_mode == 2) {
231 for (j = CHUNK_SIZE; j < sum.flength; j += CHUNK_SIZE) {
232 if (INFO_GTE(PROGRESS, 1))
233 show_progress(offset, total_size);
234 sum_update(map_ptr(mapbuf, offset, CHUNK_SIZE),
235 CHUNK_SIZE);
236 offset = j;
237 }
238 if (offset < sum.flength) {
239 int32 len = (int32)(sum.flength - offset);
240 if (INFO_GTE(PROGRESS, 1))
241 show_progress(offset, total_size);
242 sum_update(map_ptr(mapbuf, offset, len), len);
243 }
244 }
245 offset = sum.flength;
246 if (fd != -1 && (j = do_lseek(fd, offset, SEEK_SET)) != offset) {
247 rsyserr(FERROR_XFER, errno, "lseek of %s returned %s, not %s",
248 full_fname(fname), big_num(j), big_num(offset));
249 exit_cleanup(RERR_FILEIO);
250 }
251 }
252
253 while ((i = recv_token(f_in, &data)) != 0) {
254 if (INFO_GTE(PROGRESS, 1))
255 show_progress(offset, total_size);
256
257 if (i > 0) {
258 if (DEBUG_GTE(DELTASUM, 3)) {
259 rprintf(FINFO,"data recv %d at %s\n",
260 i, big_num(offset));
261 }
262
263 stats.literal_data += i;
264 cleanup_got_literal = 1;
265
266 sum_update(data, i);
267
268 if (fd != -1 && write_file(fd,data,i) != i)
269 goto report_write_error;
270 offset += i;
271 continue;
272 }
273
274 i = -(i+1);
275 offset2 = i * (OFF_T)sum.blength;
276 len = sum.blength;
277 if (i == (int)sum.count-1 && sum.remainder != 0)
278 len = sum.remainder;
279
280 stats.matched_data += len;
281
282 if (DEBUG_GTE(DELTASUM, 3)) {
283 rprintf(FINFO,
284 "chunk[%d] of size %ld at %s offset=%s\n",
285 i, (long)len, big_num(offset2), big_num(offset));
286 }
287
288 if (mapbuf) {
289 map = map_ptr(mapbuf,offset2,len);
290
291 see_token(map, len);
292 sum_update(map, len);
293 }
294
295 if (updating_basis_or_equiv) {
296 if (offset == offset2 && fd != -1) {
297 OFF_T pos;
298 if (flush_write_file(fd) < 0)
299 goto report_write_error;
300 offset += len;
301 if ((pos = do_lseek(fd, len, SEEK_CUR)) != offset) {
302 rsyserr(FERROR_XFER, errno,
303 "lseek of %s returned %s, not %s",
304 full_fname(fname),
305 big_num(pos), big_num(offset));
306 exit_cleanup(RERR_FILEIO);
307 }
308 continue;
309 }
310 }
311 if (fd != -1 && map && write_file(fd, map, len) != (int)len)
312 goto report_write_error;
313 offset += len;
314 }
315
316 if (flush_write_file(fd) < 0)
317 goto report_write_error;
318
319#ifdef HAVE_FTRUNCATE
320 if (inplace && fd != -1
321 && ftruncate(fd, offset) < 0) {
322 rsyserr(FERROR_XFER, errno, "ftruncate failed on %s",
323 full_fname(fname));
324 }
325#endif
326
327 if (INFO_GTE(PROGRESS, 1))
328 end_progress(total_size);
329
330 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
331 report_write_error:
332 rsyserr(FERROR_XFER, errno, "write failed on %s",
333 full_fname(fname));
334 exit_cleanup(RERR_FILEIO);
335 }
336
337 if (sum_end(file_sum1) != checksum_len)
338 overflow_exit("checksum_len"); /* Impossible... */
339
340 if (mapbuf)
341 unmap_file(mapbuf);
342
343 read_buf(f_in, sender_file_sum, checksum_len);
344 if (DEBUG_GTE(DELTASUM, 2))
345 rprintf(FINFO,"got file_sum\n");
346 if (fd != -1 && memcmp(file_sum1, sender_file_sum, checksum_len) != 0)
347 return 0;
348 return 1;
349}
350
351
352static void discard_receive_data(int f_in, OFF_T length)
353{
354 receive_data(f_in, NULL, -1, 0, NULL, -1, length);
355}
356
357static void handle_delayed_updates(char *local_name)
358{
359 char *fname, *partialptr;
360 int ndx;
361
362 for (ndx = -1; (ndx = bitbag_next_bit(delayed_bits, ndx)) >= 0; ) {
363 struct file_struct *file = cur_flist->files[ndx];
364 fname = local_name ? local_name : f_name(file, NULL);
365 if ((partialptr = partial_dir_fname(fname)) != NULL) {
366 if (make_backups > 0 && !make_backup(fname, False))
367 continue;
368 if (DEBUG_GTE(RECV, 1)) {
369 rprintf(FINFO, "renaming %s to %s\n",
370 partialptr, fname);
371 }
372 /* We don't use robust_rename() here because the
373 * partial-dir must be on the same drive. */
374 if (do_rename(partialptr, fname) < 0) {
375 rsyserr(FERROR_XFER, errno,
376 "rename failed for %s (from %s)",
377 full_fname(fname), partialptr);
378 } else {
379 if (remove_source_files
380 || (preserve_hard_links && F_IS_HLINKED(file)))
381 send_msg_int(MSG_SUCCESS, ndx);
382 handle_partial_dir(partialptr, PDIR_DELETE);
383 }
384 }
385 }
386}
387
388static void no_batched_update(int ndx, BOOL is_redo)
389{
390 struct file_list *flist = flist_for_ndx(ndx, "no_batched_update");
391 struct file_struct *file = flist->files[ndx - flist->ndx_start];
392
393 rprintf(FERROR_XFER, "(No batched update for%s \"%s\")\n",
394 is_redo ? " resend of" : "", f_name(file, NULL));
395
396 if (inc_recurse && !dry_run)
397 send_msg_int(MSG_NO_SEND, ndx);
398}
399
400static int we_want_redo(int desired_ndx)
401{
402 static int redo_ndx = -1;
403
404 while (redo_ndx < desired_ndx) {
405 if (redo_ndx >= 0)
406 no_batched_update(redo_ndx, True);
407 if ((redo_ndx = flist_ndx_pop(&batch_redo_list)) < 0)
408 return 0;
409 }
410
411 if (redo_ndx == desired_ndx) {
412 redo_ndx = -1;
413 return 1;
414 }
415
416 return 0;
417}
418
419static int gen_wants_ndx(int desired_ndx, int flist_num)
420{
421 static int next_ndx = -1;
422 static int done_cnt = 0;
423 static BOOL got_eof = False;
424
425 if (got_eof)
426 return 0;
427
428 /* TODO: integrate gen-reading I/O into perform_io() so this is not needed? */
429 io_flush(FULL_FLUSH);
430
431 while (next_ndx < desired_ndx) {
432 if (inc_recurse && flist_num <= done_cnt)
433 return 0;
434 if (next_ndx >= 0)
435 no_batched_update(next_ndx, False);
436 if ((next_ndx = read_int(batch_gen_fd)) < 0) {
437 if (inc_recurse) {
438 done_cnt++;
439 continue;
440 }
441 got_eof = True;
442 return 0;
443 }
444 }
445
446 if (next_ndx == desired_ndx) {
447 next_ndx = -1;
448 return 1;
449 }
450
451 return 0;
452}
453
454/**
455 * main routine for receiver process.
456 *
457 * Receiver process runs on the same host as the generator process. */
458int recv_files(int f_in, int f_out, char *local_name)
459{
460 int fd1,fd2;
461 STRUCT_STAT st;
462 int iflags, xlen;
463 char *fname, fbuf[MAXPATHLEN];
464 char xname[MAXPATHLEN];
465 char fnametmp[MAXPATHLEN];
466 char *fnamecmp, *partialptr;
467 char fnamecmpbuf[MAXPATHLEN];
468 uchar fnamecmp_type;
469 struct file_struct *file;
470 int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i;
471 enum logcode log_code = log_before_transfer ? FLOG : FINFO;
472 int max_phase = protocol_version >= 29 ? 2 : 1;
473 int dflt_perms = (ACCESSPERMS & ~orig_umask);
474#ifdef SUPPORT_ACLS
475 const char *parent_dirname = "";
476#endif
477 int ndx, recv_ok;
478
479 if (DEBUG_GTE(RECV, 1))
480 rprintf(FINFO, "recv_files(%d) starting\n", cur_flist->used);
481
482 if (delay_updates)
483 delayed_bits = bitbag_create(cur_flist->used + 1);
484
485 while (1) {
486 cleanup_disable();
487
488 /* This call also sets cur_flist. */
489 ndx = read_ndx_and_attrs(f_in, f_out, &iflags, &fnamecmp_type,
490 xname, &xlen);
491 if (ndx == NDX_DONE) {
492 if (!am_server && INFO_GTE(PROGRESS, 2) && cur_flist) {
493 set_current_file_index(NULL, 0);
494 end_progress(0);
495 }
496 if (inc_recurse && first_flist) {
497 if (read_batch) {
498 ndx = first_flist->used + first_flist->ndx_start;
499 gen_wants_ndx(ndx, first_flist->flist_num);
500 }
501 flist_free(first_flist);
502 if (first_flist)
503 continue;
504 } else if (read_batch && first_flist) {
505 ndx = first_flist->used;
506 gen_wants_ndx(ndx, first_flist->flist_num);
507 }
508 if (++phase > max_phase)
509 break;
510 if (DEBUG_GTE(RECV, 1))
511 rprintf(FINFO, "recv_files phase=%d\n", phase);
512 if (phase == 2 && delay_updates)
513 handle_delayed_updates(local_name);
514 write_int(f_out, NDX_DONE);
515 continue;
516 }
517
518 if (ndx - cur_flist->ndx_start >= 0)
519 file = cur_flist->files[ndx - cur_flist->ndx_start];
520 else
521 file = dir_flist->files[cur_flist->parent_ndx];
522 fname = local_name ? local_name : f_name(file, fbuf);
523
524 if (DEBUG_GTE(RECV, 1))
525 rprintf(FINFO, "recv_files(%s)\n", fname);
526
527#ifdef SUPPORT_XATTRS
528 if (iflags & ITEM_REPORT_XATTR && do_xfers)
529 recv_xattr_request(file, f_in);
530#endif
531
532 if (!(iflags & ITEM_TRANSFER)) {
533 maybe_log_item(file, iflags, itemizing, xname);
534#ifdef SUPPORT_XATTRS
535 if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers)
536 set_file_attrs(fname, file, NULL, fname, 0);
537#endif
538 if (iflags & ITEM_IS_NEW) {
539 stats.created_files++;
540 if (S_ISREG(file->mode)) {
541 /* Nothing further to count. */
542 } else if (S_ISDIR(file->mode))
543 stats.created_dirs++;
544#ifdef SUPPORT_LINKS
545 else if (S_ISLNK(file->mode))
546 stats.created_symlinks++;
547#endif
548 else if (IS_DEVICE(file->mode))
549 stats.created_devices++;
550 else
551 stats.created_specials++;
552 }
553 continue;
554 }
555 if (phase == 2) {
556 rprintf(FERROR,
557 "got transfer request in phase 2 [%s]\n",
558 who_am_i());
559 exit_cleanup(RERR_PROTOCOL);
560 }
561
562 if (file->flags & FLAG_FILE_SENT) {
563 if (csum_length == SHORT_SUM_LENGTH) {
564 if (keep_partial && !partial_dir)
565 make_backups = -make_backups; /* prevents double backup */
566 if (append_mode)
567 sparse_files = -sparse_files;
568 append_mode = -append_mode;
569 csum_length = SUM_LENGTH;
570 redoing = 1;
571 }
572 } else {
573 if (csum_length != SHORT_SUM_LENGTH) {
574 if (keep_partial && !partial_dir)
575 make_backups = -make_backups;
576 if (append_mode)
577 sparse_files = -sparse_files;
578 append_mode = -append_mode;
579 csum_length = SHORT_SUM_LENGTH;
580 redoing = 0;
581 }
582 if (iflags & ITEM_IS_NEW)
583 stats.created_files++;
584 }
585
586 if (!am_server && INFO_GTE(PROGRESS, 1))
587 set_current_file_index(file, ndx);
588 stats.xferred_files++;
589 stats.total_transferred_size += F_LENGTH(file);
590
591 cleanup_got_literal = 0;
592
593 if (daemon_filter_list.head
594 && check_filter(&daemon_filter_list, FLOG, fname, 0) < 0) {
595 rprintf(FERROR, "attempt to hack rsync failed.\n");
596 exit_cleanup(RERR_PROTOCOL);
597 }
598
599 if (read_batch) {
600 int wanted = redoing
601 ? we_want_redo(ndx)
602 : gen_wants_ndx(ndx, cur_flist->flist_num);
603 if (!wanted) {
604 rprintf(FINFO,
605 "(Skipping batched update for%s \"%s\")\n",
606 redoing ? " resend of" : "",
607 fname);
608 discard_receive_data(f_in, F_LENGTH(file));
609 file->flags |= FLAG_FILE_SENT;
610 continue;
611 }
612 }
613
614 if (!log_before_transfer)
615 remember_initial_stats();
616
617 if (!do_xfers) { /* log the transfer */
618 log_item(FCLIENT, file, iflags, NULL);
619 if (read_batch)
620 discard_receive_data(f_in, F_LENGTH(file));
621 continue;
622 }
623 if (write_batch < 0) {
624 log_item(FCLIENT, file, iflags, NULL);
625 if (!am_server)
626 discard_receive_data(f_in, F_LENGTH(file));
627 continue;
628 }
629
630 partialptr = partial_dir ? partial_dir_fname(fname) : fname;
631
632 if (protocol_version >= 29) {
633 switch (fnamecmp_type) {
634 case FNAMECMP_FNAME:
635 fnamecmp = fname;
636 break;
637 case FNAMECMP_PARTIAL_DIR:
638 fnamecmp = partialptr;
639 break;
640 case FNAMECMP_BACKUP:
641 fnamecmp = get_backup_name(fname);
642 break;
643 case FNAMECMP_FUZZY:
644 if (file->dirname) {
645 pathjoin(fnamecmpbuf, MAXPATHLEN,
646 file->dirname, xname);
647 fnamecmp = fnamecmpbuf;
648 } else
649 fnamecmp = xname;
650 break;
651 default:
652 if (fnamecmp_type >= basis_dir_cnt) {
653 rprintf(FERROR,
654 "invalid basis_dir index: %d.\n",
655 fnamecmp_type);
656 exit_cleanup(RERR_PROTOCOL);
657 }
658 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
659 basis_dir[fnamecmp_type], fname);
660 fnamecmp = fnamecmpbuf;
661 break;
662 }
663 if (!fnamecmp || (daemon_filter_list.head
664 && check_filter(&daemon_filter_list, FLOG, fname, 0) < 0)) {
665 fnamecmp = fname;
666 fnamecmp_type = FNAMECMP_FNAME;
667 }
668 } else {
669 /* Reminder: --inplace && --partial-dir are never
670 * enabled at the same time. */
671 if (inplace && make_backups > 0) {
672 if (!(fnamecmp = get_backup_name(fname)))
673 fnamecmp = fname;
674 else
675 fnamecmp_type = FNAMECMP_BACKUP;
676 } else if (partial_dir && partialptr)
677 fnamecmp = partialptr;
678 else
679 fnamecmp = fname;
680 }
681
682 /* open the file */
683 fd1 = do_open(fnamecmp, O_RDONLY, 0);
684
685 if (fd1 == -1 && protocol_version < 29) {
686 if (fnamecmp != fname) {
687 fnamecmp = fname;
688 fd1 = do_open(fnamecmp, O_RDONLY, 0);
689 }
690
691 if (fd1 == -1 && basis_dir[0]) {
692 /* pre-29 allowed only one alternate basis */
693 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
694 basis_dir[0], fname);
695 fnamecmp = fnamecmpbuf;
696 fd1 = do_open(fnamecmp, O_RDONLY, 0);
697 }
698 }
699
700 updating_basis_or_equiv = inplace
701 && (fnamecmp == fname || fnamecmp_type == FNAMECMP_BACKUP);
702
703 if (fd1 == -1) {
704 st.st_mode = 0;
705 st.st_size = 0;
706 } else if (do_fstat(fd1,&st) != 0) {
707 rsyserr(FERROR_XFER, errno, "fstat %s failed",
708 full_fname(fnamecmp));
709 discard_receive_data(f_in, F_LENGTH(file));
710 close(fd1);
711 if (inc_recurse)
712 send_msg_int(MSG_NO_SEND, ndx);
713 continue;
714 }
715
716 if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
717 /* this special handling for directories
718 * wouldn't be necessary if robust_rename()
719 * and the underlying robust_unlink could cope
720 * with directories
721 */
722 rprintf(FERROR_XFER, "recv_files: %s is a directory\n",
723 full_fname(fnamecmp));
724 discard_receive_data(f_in, F_LENGTH(file));
725 close(fd1);
726 if (inc_recurse)
727 send_msg_int(MSG_NO_SEND, ndx);
728 continue;
729 }
730
731 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
732 close(fd1);
733 fd1 = -1;
734 }
735
736 /* If we're not preserving permissions, change the file-list's
737 * mode based on the local permissions and some heuristics. */
738 if (!preserve_perms) {
739 int exists = fd1 != -1;
740#ifdef SUPPORT_ACLS
741 const char *dn = file->dirname ? file->dirname : ".";
742 if (parent_dirname != dn
743 && strcmp(parent_dirname, dn) != 0) {
744 dflt_perms = default_perms_for_dir(dn);
745 parent_dirname = dn;
746 }
747#endif
748 file->mode = dest_mode(file->mode, st.st_mode,
749 dflt_perms, exists);
750 }
751
752 /* We now check to see if we are writing the file "inplace" */
753 if (inplace) {
754 fd2 = do_open(fname, O_WRONLY|O_CREAT, 0600);
755 if (fd2 == -1) {
756 rsyserr(FERROR_XFER, errno, "open %s failed",
757 full_fname(fname));
758 }
759 } else {
760 fd2 = open_tmpfile(fnametmp, fname, file);
761 if (fd2 != -1)
762 cleanup_set(fnametmp, partialptr, file, fd1, fd2);
763 }
764
765 if (fd2 == -1) {
766 discard_receive_data(f_in, F_LENGTH(file));
767 if (fd1 != -1)
768 close(fd1);
769 if (inc_recurse)
770 send_msg_int(MSG_NO_SEND, ndx);
771 continue;
772 }
773
774 /* log the transfer */
775 if (log_before_transfer)
776 log_item(FCLIENT, file, iflags, NULL);
777 else if (!am_server && INFO_GTE(NAME, 1) && INFO_EQ(PROGRESS, 1))
778 rprintf(FINFO, "%s\n", fname);
779
780 /* recv file data */
781 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
782 fname, fd2, F_LENGTH(file));
783
784 log_item(log_code, file, iflags, NULL);
785
786 if (fd1 != -1)
787 close(fd1);
788 if (close(fd2) < 0) {
789 rsyserr(FERROR, errno, "close failed on %s",
790 full_fname(fnametmp));
791 exit_cleanup(RERR_FILEIO);
792 }
793
794 if ((recv_ok && (!delay_updates || !partialptr)) || inplace) {
795 if (partialptr == fname)
796 partialptr = NULL;
797 if (!finish_transfer(fname, fnametmp, fnamecmp,
798 partialptr, file, recv_ok, 1))
799 recv_ok = -1;
800 else if (fnamecmp == partialptr) {
801 do_unlink(partialptr);
802 handle_partial_dir(partialptr, PDIR_DELETE);
803 }
804 } else if (keep_partial && partialptr) {
805 if (!handle_partial_dir(partialptr, PDIR_CREATE)) {
806 rprintf(FERROR,
807 "Unable to create partial-dir for %s -- discarding %s.\n",
808 local_name ? local_name : f_name(file, NULL),
809 recv_ok ? "completed file" : "partial file");
810 do_unlink(fnametmp);
811 recv_ok = -1;
812 } else if (!finish_transfer(partialptr, fnametmp, fnamecmp, NULL,
813 file, recv_ok, !partial_dir))
814 recv_ok = -1;
815 else if (delay_updates && recv_ok) {
816 bitbag_set_bit(delayed_bits, ndx);
817 recv_ok = 2;
818 } else
819 partialptr = NULL;
820 } else
821 do_unlink(fnametmp);
822
823 cleanup_disable();
824
825 if (read_batch)
826 file->flags |= FLAG_FILE_SENT;
827
828 switch (recv_ok) {
829 case 2:
830 break;
831 case 1:
832 if (remove_source_files || inc_recurse
833 || (preserve_hard_links && F_IS_HLINKED(file)))
834 send_msg_int(MSG_SUCCESS, ndx);
835 break;
836 case 0: {
837 enum logcode msgtype = redoing ? FERROR_XFER : FWARNING;
838 if (msgtype == FERROR_XFER || INFO_GTE(NAME, 1)) {
839 char *errstr, *redostr, *keptstr;
840 if (!(keep_partial && partialptr) && !inplace)
841 keptstr = "discarded";
842 else if (partial_dir)
843 keptstr = "put into partial-dir";
844 else
845 keptstr = "retained";
846 if (msgtype == FERROR_XFER) {
847 errstr = "ERROR";
848 redostr = "";
849 } else {
850 errstr = "WARNING";
851 redostr = read_batch ? " (may try again)"
852 : " (will try again)";
853 }
854 rprintf(msgtype,
855 "%s: %s failed verification -- update %s%s.\n",
856 errstr, local_name ? f_name(file, NULL) : fname,
857 keptstr, redostr);
858 }
859 if (!redoing) {
860 if (read_batch)
861 flist_ndx_push(&batch_redo_list, ndx);
862 send_msg_int(MSG_REDO, ndx);
863 file->flags |= FLAG_FILE_SENT;
864 } else if (inc_recurse)
865 send_msg_int(MSG_NO_SEND, ndx);
866 break;
867 }
868 case -1:
869 if (inc_recurse)
870 send_msg_int(MSG_NO_SEND, ndx);
871 break;
872 }
873 }
874 if (make_backups < 0)
875 make_backups = -make_backups;
876
877 if (phase == 2 && delay_updates) /* for protocol_version < 29 */
878 handle_delayed_updates(local_name);
879
880 if (DEBUG_GTE(RECV, 1))
881 rprintf(FINFO,"recv_files finished\n");
882
883 return 0;
884}