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