A major overhaul of I/O routines, creating perform_io().
[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;
8715db2c 27extern int am_server;
3ea6e0e7 28extern int inc_recurse;
910e89b9 29extern int log_before_transfer;
2fedf3d5 30extern int stdout_format_has_i;
ecc7623e 31extern int logfile_format_has_i;
2f03f956 32extern int csum_length;
16cc9ca2 33extern int read_batch;
a0009fc3 34extern int write_batch;
16cc9ca2 35extern int batch_gen_fd;
41cfde6b 36extern int protocol_version;
2f03f956 37extern int relative_paths;
744e63fb 38extern int preserve_hard_links;
92b9eb97 39extern int preserve_perms;
16edf865 40extern int preserve_xattrs;
c56595d7 41extern int basis_dir_cnt;
53dd3135 42extern int make_backups;
925c517f 43extern int cleanup_got_literal;
744e63fb 44extern int remove_source_files;
6cc11982 45extern int append_mode;
e7ee91de 46extern int sparse_files;
55e50d89 47extern int keep_partial;
886df221 48extern int checksum_len;
ba582f75 49extern int checksum_seed;
a3221d2a 50extern int inplace;
48e1c8c6 51extern int delay_updates;
1c3344a1 52extern mode_t orig_umask;
8715db2c 53extern struct stats stats;
8715db2c
WD
54extern char *tmpdir;
55extern char *partial_dir;
b791d680
WD
56extern char *basis_dir[MAX_BASIS_DIRS+1];
57extern char sender_file_sum[MAX_DIGEST_LEN];
6755a7d7 58extern struct file_list *cur_flist, *first_flist, *dir_flist;
7b6c5c77 59extern filter_rule_list daemon_filter_list;
5ebab6c1 60
10f994a5 61static struct bitbag *delayed_bits = NULL;
d0221f1d 62static int phase = 0, redoing = 0;
8ab02ccd 63static flist_ndx_list batch_redo_list;
48459ba1 64/* We're either updating the basis file or an identical copy: */
77943e69 65static int updating_basis_or_equiv;
3e7934a5 66
c55fb5e1
WD
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
52d3e106 73 *
c55fb5e1
WD
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!
17fadf7d 77 *
c55fb5e1
WD
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.
17fadf7d 83 *
c55fb5e1
WD
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)
2f03f956 93{
d5dcb6f7 94 int maxname, added, length = 0;
93204cca 95 const char *f;
c55fb5e1 96 char *suf;
2f03f956 97
2f03f956 98 if (tmpdir) {
a24639bb 99 /* Note: this can't overflow, so the return value is safe */
b7cee949 100 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
52d3e106 101 fnametmp[length++] = '/';
0c2ef5f4 102 }
52d3e106 103
0c2ef5f4 104 if ((f = strrchr(fname, '/')) != NULL) {
52d3e106
S
105 ++f;
106 if (!tmpdir) {
107 length = f - fname;
0c2ef5f4 108 /* copy up to and including the slash */
52d3e106 109 strlcpy(fnametmp, fname, length + 1);
0c2ef5f4 110 }
446e239e 111 } else
52d3e106 112 f = fname;
52d3e106 113 fnametmp[length++] = '.';
2f03f956 114
d5dcb6f7 115 /* The maxname value is bufsize, and includes space for the '\0'.
c55fb5e1
WD
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);
2f03f956 119
0c2ef5f4 120 if (maxname < 1) {
3f0211b6 121 rprintf(FERROR_XFER, "temporary filename too long: %s\n", fname);
52d3e106 122 fnametmp[0] = '\0';
2f03f956
AT
123 return 0;
124 }
125
d5dcb6f7
WD
126 added = strlcpy(fnametmp + length, f, maxname);
127 if (added >= maxname)
128 added = maxname - 1;
c55fb5e1
WD
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);
2f03f956
AT
156
157 return 1;
158}
159
2cce7545
WD
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
c55fb5e1 168 if (!get_tmpname(fnametmp, fname, False))
2cce7545
WD
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
3696674b 182 && make_path(fnametmp, MKP_SKIP_SLASH | MKP_DROP_NAME) == 0) {
2cce7545 183 /* Get back to name with XXXXXX in it. */
c55fb5e1 184 get_tmpname(fnametmp, fname, False);
2cce7545
WD
185 fd = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
186 }
187#endif
188
189 if (fd == -1) {
3f0211b6 190 rsyserr(FERROR_XFER, errno, "mkstemp %s failed",
2cce7545
WD
191 full_fname(fnametmp));
192 return -1;
193 }
194
195 return fd;
196}
2f03f956 197
7e5fa372 198static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
55edf18c 199 const char *fname, int fd, OFF_T total_size)
2f03f956 200{
a0456b9c 201 static char file_sum1[MAX_DIGEST_LEN];
7e5fa372 202 struct map_struct *mapbuf;
fc0257c9 203 struct sum_struct sum;
886df221 204 int32 len;
2f03f956 205 OFF_T offset = 0;
89e540e6 206 OFF_T offset2;
2f03f956 207 char *data;
6c495e0d 208 int32 i;
e1f67417 209 char *map = NULL;
17fadf7d 210
fc0257c9 211 read_sum_head(f_in, &sum);
17fadf7d 212
7e5fa372 213 if (fd_r >= 0 && size_r > 0) {
80264051
WD
214 int32 read_size = MAX(sum.blength * 2, 16*1024);
215 mapbuf = map_file(fd_r, size_r, read_size, sum.blength);
5a18b34d 216 if (DEBUG_GTE(DELTASUM, 2)) {
6d56efa6 217 rprintf(FINFO, "recv mapped %s of size %s\n",
adc2476f 218 fname_r, big_num(size_r));
7e5fa372
WD
219 }
220 } else
221 mapbuf = NULL;
222
ba582f75 223 sum_init(checksum_seed);
17fadf7d 224
f3d6d480 225 if (append_mode > 0) {
6cc11982
WD
226 OFF_T j;
227 sum.flength = (OFF_T)sum.count * sum.blength;
228 if (sum.remainder)
229 sum.flength -= sum.blength - sum.remainder;
936fa865
WD
230 if (append_mode == 2) {
231 for (j = CHUNK_SIZE; j < sum.flength; j += CHUNK_SIZE) {
951e826b 232 if (INFO_GTE(PROGRESS, 1))
936fa865
WD
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);
951e826b 240 if (INFO_GTE(PROGRESS, 1))
936fa865
WD
241 show_progress(offset, total_size);
242 sum_update(map_ptr(mapbuf, offset, len), len);
243 }
6cc11982 244 }
936fa865 245 offset = sum.flength;
ffa8ab8e 246 if (fd != -1 && (j = do_lseek(fd, offset, SEEK_SET)) != offset) {
6d56efa6 247 rsyserr(FERROR_XFER, errno, "lseek of %s returned %s, not %s",
adc2476f 248 full_fname(fname), big_num(j), big_num(offset));
6cc11982
WD
249 exit_cleanup(RERR_FILEIO);
250 }
251 }
252
3f55bd5d 253 while ((i = recv_token(f_in, &data)) != 0) {
951e826b 254 if (INFO_GTE(PROGRESS, 1))
16417f8b 255 show_progress(offset, total_size);
2f03f956
AT
256
257 if (i > 0) {
5a18b34d 258 if (DEBUG_GTE(DELTASUM, 3)) {
6d56efa6 259 rprintf(FINFO,"data recv %d at %s\n",
adc2476f 260 i, big_num(offset));
2f03f956
AT
261 }
262
263 stats.literal_data += i;
264 cleanup_got_literal = 1;
17fadf7d 265
6c495e0d 266 sum_update(data, i);
2f03f956 267
c52461f9
WD
268 if (fd != -1 && write_file(fd,data,i) != i)
269 goto report_write_error;
2f03f956
AT
270 offset += i;
271 continue;
17fadf7d 272 }
2f03f956
AT
273
274 i = -(i+1);
6c495e0d 275 offset2 = i * (OFF_T)sum.blength;
fc0257c9 276 len = sum.blength;
d2a918b4 277 if (i == (int)sum.count-1 && sum.remainder != 0)
fc0257c9 278 len = sum.remainder;
17fadf7d 279
2f03f956 280 stats.matched_data += len;
17fadf7d 281
5a18b34d 282 if (DEBUG_GTE(DELTASUM, 3)) {
6c495e0d 283 rprintf(FINFO,
6d56efa6 284 "chunk[%d] of size %ld at %s offset=%s\n",
adc2476f 285 i, (long)len, big_num(offset2), big_num(offset));
6c495e0d 286 }
17fadf7d 287
446e239e
WD
288 if (mapbuf) {
289 map = map_ptr(mapbuf,offset2,len);
17fadf7d 290
c55f7021 291 see_token(map, len);
6c495e0d 292 sum_update(map, len);
c55f7021 293 }
17fadf7d 294
77943e69 295 if (updating_basis_or_equiv) {
89e540e6 296 if (offset == offset2 && fd != -1) {
ffa8ab8e 297 OFF_T pos;
c52461f9
WD
298 if (flush_write_file(fd) < 0)
299 goto report_write_error;
89e540e6 300 offset += len;
ffa8ab8e 301 if ((pos = do_lseek(fd, len, SEEK_CUR)) != offset) {
3f0211b6 302 rsyserr(FERROR_XFER, errno,
6d56efa6 303 "lseek of %s returned %s, not %s",
ffa8ab8e 304 full_fname(fname),
adc2476f 305 big_num(pos), big_num(offset));
fab65a5b
WD
306 exit_cleanup(RERR_FILEIO);
307 }
89e540e6 308 continue;
a3221d2a 309 }
2f03f956 310 }
1ed91a04 311 if (fd != -1 && map && write_file(fd, map, len) != (int)len)
c52461f9 312 goto report_write_error;
2f03f956
AT
313 offset += len;
314 }
315
85f14172
WD
316 if (flush_write_file(fd) < 0)
317 goto report_write_error;
76c21947 318
4f5b0756 319#ifdef HAVE_FTRUNCATE
aade88bf
WD
320 if (inplace && fd != -1
321 && ftruncate(fd, offset) < 0) {
322 rsyserr(FERROR_XFER, errno, "ftruncate failed on %s",
323 full_fname(fname));
94112924 324 }
a3221d2a
WD
325#endif
326
951e826b 327 if (INFO_GTE(PROGRESS, 1))
16417f8b 328 end_progress(total_size);
2f03f956
AT
329
330 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
c52461f9 331 report_write_error:
3f0211b6 332 rsyserr(FERROR_XFER, errno, "write failed on %s",
d62bcc17 333 full_fname(fname));
65417579 334 exit_cleanup(RERR_FILEIO);
2f03f956
AT
335 }
336
886df221
WD
337 if (sum_end(file_sum1) != checksum_len)
338 overflow_exit("checksum_len"); /* Impossible... */
2f03f956 339
7e5fa372
WD
340 if (mapbuf)
341 unmap_file(mapbuf);
342
886df221 343 read_buf(f_in, sender_file_sum, checksum_len);
5a18b34d 344 if (DEBUG_GTE(DELTASUM, 2))
bc63ae3f 345 rprintf(FINFO,"got file_sum\n");
886df221 346 if (fd != -1 && memcmp(file_sum1, sender_file_sum, checksum_len) != 0)
bc63ae3f 347 return 0;
2f03f956
AT
348 return 1;
349}
350
351
8ed9d849
WD
352static void discard_receive_data(int f_in, OFF_T length)
353{
7e5fa372 354 receive_data(f_in, NULL, -1, 0, NULL, -1, length);
8ed9d849
WD
355}
356
f3d6d480 357static void handle_delayed_updates(char *local_name)
42be5320 358{
663b2857 359 char *fname, *partialptr;
0395130c 360 int ndx;
42be5320 361
0395130c 362 for (ndx = -1; (ndx = bitbag_next_bit(delayed_bits, ndx)) >= 0; ) {
f3d6d480 363 struct file_struct *file = cur_flist->files[ndx];
6cbde57d 364 fname = local_name ? local_name : f_name(file, NULL);
42be5320 365 if ((partialptr = partial_dir_fname(fname)) != NULL) {
21cddef2 366 if (make_backups > 0 && !make_backup(fname, False))
42be5320 367 continue;
951e826b 368 if (DEBUG_GTE(RECV, 1)) {
42be5320 369 rprintf(FINFO, "renaming %s to %s\n",
45c49b52 370 partialptr, fname);
42be5320 371 }
6a94c58b
WD
372 /* We don't use robust_rename() here because the
373 * partial-dir must be on the same drive. */
42be5320 374 if (do_rename(partialptr, fname) < 0) {
3f0211b6 375 rsyserr(FERROR_XFER, errno,
42be5320 376 "rename failed for %s (from %s)",
45c49b52 377 full_fname(fname), partialptr);
42be5320 378 } else {
47c11975 379 if (remove_source_files
112d728f 380 || (preserve_hard_links && F_IS_HLINKED(file)))
0395130c 381 send_msg_int(MSG_SUCCESS, ndx);
45c49b52 382 handle_partial_dir(partialptr, PDIR_DELETE);
42be5320
WD
383 }
384 }
385 }
386}
387
8ab02ccd 388static void no_batched_update(int ndx, BOOL is_redo)
154cdaaa 389{
8ab02ccd
WD
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
83c32ca5 396 if (inc_recurse && !dry_run)
8ab02ccd
WD
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
7a498f5b 419static int gen_wants_ndx(int desired_ndx, int flist_num)
8ab02ccd
WD
420{
421 static int next_ndx = -1;
4079d6a6
WD
422 static int done_cnt = 0;
423 static BOOL got_eof = False;
8ab02ccd
WD
424
425 if (got_eof)
426 return 0;
427
20caffd2
WD
428 /* TODO: integrate gen-reading I/O into perform_io() so this is not needed? */
429 io_flush(FULL_FLUSH);
430
8ab02ccd 431 while (next_ndx < desired_ndx) {
4079d6a6
WD
432 if (inc_recurse && flist_num <= done_cnt)
433 return 0;
8ab02ccd
WD
434 if (next_ndx >= 0)
435 no_batched_update(next_ndx, False);
436 if ((next_ndx = read_int(batch_gen_fd)) < 0) {
4079d6a6
WD
437 if (inc_recurse) {
438 done_cnt++;
439 continue;
440 }
8ab02ccd
WD
441 got_eof = True;
442 return 0;
154cdaaa 443 }
154cdaaa 444 }
af2dea60 445
8ab02ccd
WD
446 if (next_ndx == desired_ndx) {
447 next_ndx = -1;
448 return 1;
449 }
450
451 return 0;
154cdaaa
WD
452}
453
b35d0d8e
MP
454/**
455 * main routine for receiver process.
456 *
457 * Receiver process runs on the same host as the generator process. */
20caffd2 458int recv_files(int f_in, int f_out, char *local_name)
17fadf7d 459{
2f03f956
AT
460 int fd1,fd2;
461 STRUCT_STAT st;
b9232d45 462 int iflags, xlen;
446e239e 463 char *fname, fbuf[MAXPATHLEN];
b9232d45 464 char xname[MAXPATHLEN];
2f03f956 465 char fnametmp[MAXPATHLEN];
663b2857 466 char *fnamecmp, *partialptr;
375a4556 467 char fnamecmpbuf[MAXPATHLEN];
9e4a8d29 468 uchar fnamecmp_type;
2f03f956 469 struct file_struct *file;
1b7c47cb 470 struct stats initial_stats;
2fedf3d5
WD
471 int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i;
472 enum logcode log_code = log_before_transfer ? FLOG : FINFO;
1ed91a04 473 int max_phase = protocol_version >= 29 ? 2 : 1;
1c3344a1
WD
474 int dflt_perms = (ACCESSPERMS & ~orig_umask);
475#ifdef SUPPORT_ACLS
476 const char *parent_dirname = "";
477#endif
0395130c 478 int ndx, recv_ok;
11a5a3c7 479
951e826b 480 if (DEBUG_GTE(RECV, 1))
9decb4d2 481 rprintf(FINFO, "recv_files(%d) starting\n", cur_flist->used);
2f03f956 482
3e7934a5 483 if (delay_updates)
9decb4d2 484 delayed_bits = bitbag_create(cur_flist->used + 1);
48e1c8c6 485
17fadf7d 486 while (1) {
2f03f956
AT
487 cleanup_disable();
488
f3d6d480 489 /* This call also sets cur_flist. */
20caffd2 490 ndx = read_ndx_and_attrs(f_in, f_out, &iflags, &fnamecmp_type,
16edf865 491 xname, &xlen);
0395130c 492 if (ndx == NDX_DONE) {
951e826b
WD
493 if (!am_server && INFO_GTE(PROGRESS, 2) && cur_flist) {
494 set_current_file_index(NULL, 0);
495 end_progress(0);
496 }
3ea6e0e7 497 if (inc_recurse && first_flist) {
7a498f5b
WD
498 if (read_batch) {
499 ndx = first_flist->used + first_flist->ndx_start;
500 gen_wants_ndx(ndx, first_flist->flist_num);
501 }
f3d6d480
WD
502 flist_free(first_flist);
503 if (first_flist)
504 continue;
7a498f5b
WD
505 } else if (read_batch && first_flist) {
506 ndx = first_flist->used;
507 gen_wants_ndx(ndx, first_flist->flist_num);
508 }
ac3f7b81 509 if (++phase > max_phase)
4e834af1 510 break;
951e826b 511 if (DEBUG_GTE(RECV, 1))
4e834af1 512 rprintf(FINFO, "recv_files phase=%d\n", phase);
ac3f7b81 513 if (phase == 2 && delay_updates)
f3d6d480 514 handle_delayed_updates(local_name);
20caffd2 515 write_int(f_out, NDX_DONE);
4e834af1 516 continue;
2f03f956
AT
517 }
518
6755a7d7
WD
519 if (ndx - cur_flist->ndx_start >= 0)
520 file = cur_flist->files[ndx - cur_flist->ndx_start];
521 else
522 file = dir_flist->files[cur_flist->parent_ndx];
6cbde57d 523 fname = local_name ? local_name : f_name(file, fbuf);
227a9c41 524
951e826b 525 if (DEBUG_GTE(RECV, 1))
45c49b52 526 rprintf(FINFO, "recv_files(%s)\n", fname);
910e89b9 527
16edf865 528#ifdef SUPPORT_XATTRS
d510e82f 529 if (iflags & ITEM_REPORT_XATTR && do_xfers)
16edf865
WD
530 recv_xattr_request(file, f_in);
531#endif
532
22907b6b 533 if (!(iflags & ITEM_TRANSFER)) {
b9232d45 534 maybe_log_item(file, iflags, itemizing, xname);
16edf865 535#ifdef SUPPORT_XATTRS
d510e82f 536 if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers)
16edf865
WD
537 set_file_attrs(fname, file, NULL, fname, 0);
538#endif
e366e530
WD
539 if (iflags & ITEM_IS_NEW) {
540 stats.created_files++;
541 if (S_ISREG(file->mode)) {
542 /* Nothing further to count. */
543 } else if (S_ISDIR(file->mode))
544 stats.created_dirs++;
545#ifdef SUPPORT_LINKS
546 else if (S_ISLNK(file->mode))
547 stats.created_symlinks++;
548#endif
549 else if (IS_DEVICE(file->mode))
550 stats.created_devices++;
551 else
552 stats.created_specials++;
553 }
58a14ed9
WD
554 continue;
555 }
ac3f7b81
WD
556 if (phase == 2) {
557 rprintf(FERROR,
558 "got transfer request in phase 2 [%s]\n",
559 who_am_i());
560 exit_cleanup(RERR_PROTOCOL);
561 }
910e89b9 562
f3d6d480
WD
563 if (file->flags & FLAG_FILE_SENT) {
564 if (csum_length == SHORT_SUM_LENGTH) {
565 if (keep_partial && !partial_dir)
566 make_backups = -make_backups; /* prevents double backup */
936fa865
WD
567 if (append_mode)
568 sparse_files = -sparse_files;
f3d6d480 569 append_mode = -append_mode;
f3d6d480 570 csum_length = SUM_LENGTH;
d0221f1d 571 redoing = 1;
f3d6d480
WD
572 }
573 } else {
574 if (csum_length != SHORT_SUM_LENGTH) {
575 if (keep_partial && !partial_dir)
576 make_backups = -make_backups;
936fa865
WD
577 if (append_mode)
578 sparse_files = -sparse_files;
f3d6d480 579 append_mode = -append_mode;
f3d6d480 580 csum_length = SHORT_SUM_LENGTH;
d0221f1d 581 redoing = 0;
f3d6d480 582 }
e366e530
WD
583 if (iflags & ITEM_IS_NEW)
584 stats.created_files++;
f3d6d480
WD
585 }
586
951e826b 587 if (!am_server && INFO_GTE(PROGRESS, 1))
65b4e4b2 588 set_current_file_index(file, ndx);
e366e530 589 stats.xferred_files++;
112d728f 590 stats.total_transferred_size += F_LENGTH(file);
65b4e4b2 591
925c517f 592 cleanup_got_literal = 0;
2f03f956 593
819bfe45 594 if (daemon_filter_list.head
1df02d13 595 && check_filter(&daemon_filter_list, FLOG, fname, 0) < 0) {
8c2ffaf0
WD
596 rprintf(FERROR, "attempt to hack rsync failed.\n");
597 exit_cleanup(RERR_PROTOCOL);
598 }
599
16cc9ca2 600 if (read_batch) {
7a498f5b
WD
601 int wanted = redoing
602 ? we_want_redo(ndx)
603 : gen_wants_ndx(ndx, cur_flist->flist_num);
604 if (!wanted) {
45c49b52 605 rprintf(FINFO,
8ab02ccd
WD
606 "(Skipping batched update for%s \"%s\")\n",
607 redoing ? " resend of" : "",
45c49b52 608 fname);
112d728f 609 discard_receive_data(f_in, F_LENGTH(file));
8ab02ccd 610 file->flags |= FLAG_FILE_SENT;
16cc9ca2
WD
611 continue;
612 }
613 }
614
83c32ca5
WD
615 if (!do_xfers) { /* log the transfer */
616 log_item(FCLIENT, file, &stats, iflags, NULL);
617 if (read_batch)
618 discard_receive_data(f_in, F_LENGTH(file));
619 continue;
620 }
621 if (write_batch < 0) {
622 log_item(FCLIENT, file, &stats, iflags, NULL);
623 if (!am_server)
624 discard_receive_data(f_in, F_LENGTH(file));
625 continue;
626 }
627
41cfde6b
WD
628 partialptr = partial_dir ? partial_dir_fname(fname) : fname;
629
9e4a8d29
WD
630 if (protocol_version >= 29) {
631 switch (fnamecmp_type) {
41cfde6b 632 case FNAMECMP_FNAME:
a7260c40 633 fnamecmp = fname;
41cfde6b
WD
634 break;
635 case FNAMECMP_PARTIAL_DIR:
9e4a8d29 636 fnamecmp = partialptr;
41cfde6b
WD
637 break;
638 case FNAMECMP_BACKUP:
639 fnamecmp = get_backup_name(fname);
640 break;
73273075 641 case FNAMECMP_FUZZY:
b9232d45
WD
642 if (file->dirname) {
643 pathjoin(fnamecmpbuf, MAXPATHLEN,
644 file->dirname, xname);
645 fnamecmp = fnamecmpbuf;
646 } else
647 fnamecmp = xname;
73273075 648 break;
41cfde6b 649 default:
9e4a8d29 650 if (fnamecmp_type >= basis_dir_cnt) {
c56595d7
WD
651 rprintf(FERROR,
652 "invalid basis_dir index: %d.\n",
9e4a8d29 653 fnamecmp_type);
c56595d7
WD
654 exit_cleanup(RERR_PROTOCOL);
655 }
41cfde6b 656 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
9e4a8d29 657 basis_dir[fnamecmp_type], fname);
41cfde6b
WD
658 fnamecmp = fnamecmpbuf;
659 break;
660 }
819bfe45 661 if (!fnamecmp || (daemon_filter_list.head
1df02d13 662 && check_filter(&daemon_filter_list, FLOG, fname, 0) < 0)) {
9e4a8d29 663 fnamecmp = fname;
77943e69
WD
664 fnamecmp_type = FNAMECMP_FNAME;
665 }
9e4a8d29
WD
666 } else {
667 /* Reminder: --inplace && --partial-dir are never
668 * enabled at the same time. */
f3d6d480 669 if (inplace && make_backups > 0) {
9e4a8d29
WD
670 if (!(fnamecmp = get_backup_name(fname)))
671 fnamecmp = fname;
77943e69
WD
672 else
673 fnamecmp_type = FNAMECMP_BACKUP;
9e4a8d29
WD
674 } else if (partial_dir && partialptr)
675 fnamecmp = partialptr;
676 else
677 fnamecmp = fname;
678 }
dc55d7bd 679
910e89b9
WD
680 initial_stats = stats;
681
17fadf7d 682 /* open the file */
8c9fd200 683 fd1 = do_open(fnamecmp, O_RDONLY, 0);
375a4556 684
9e4a8d29
WD
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 }
77943e69
WD
699
700 updating_basis_or_equiv = inplace
701 && (fnamecmp == fname || fnamecmp_type == FNAMECMP_BACKUP);
9e4a8d29 702
d5dcb6f7
WD
703 if (fd1 == -1) {
704 st.st_mode = 0;
705 st.st_size = 0;
706 } else if (do_fstat(fd1,&st) != 0) {
3f0211b6 707 rsyserr(FERROR_XFER, errno, "fstat %s failed",
d62bcc17 708 full_fname(fnamecmp));
112d728f 709 discard_receive_data(f_in, F_LENGTH(file));
2f03f956 710 close(fd1);
a430691d
WD
711 if (inc_recurse)
712 send_msg_int(MSG_NO_SEND, ndx);
2f03f956
AT
713 continue;
714 }
715
350e4e4d
S
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 */
3f0211b6 722 rprintf(FERROR_XFER, "recv_files: %s is a directory\n",
ea42541f 723 full_fname(fnamecmp));
112d728f 724 discard_receive_data(f_in, F_LENGTH(file));
2f03f956 725 close(fd1);
a430691d
WD
726 if (inc_recurse)
727 send_msg_int(MSG_NO_SEND, ndx);
2f03f956
AT
728 continue;
729 }
730
350e4e4d
S
731 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
732 close(fd1);
733 fd1 = -1;
350e4e4d
S
734 }
735
a9d6e6fc
WD
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;
1c3344a1
WD
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);
4df9f368
AT
750 }
751
48459ba1 752 /* We now check to see if we are writing the file "inplace" */
a3221d2a 753 if (inplace) {
97894c64 754 fd2 = do_open(fname, O_WRONLY|O_CREAT, 0600);
a3221d2a 755 if (fd2 == -1) {
3f0211b6 756 rsyserr(FERROR_XFER, errno, "open %s failed",
dc55d7bd 757 full_fname(fname));
a3221d2a
WD
758 }
759 } else {
2cce7545
WD
760 fd2 = open_tmpfile(fnametmp, fname, file);
761 if (fd2 != -1)
762 cleanup_set(fnametmp, partialptr, file, fd1, fd2);
763 }
a3221d2a 764
2cce7545
WD
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;
a3221d2a 772 }
2f03f956 773
a8ed4958 774 /* log the transfer */
910e89b9 775 if (log_before_transfer)
85909931 776 log_item(FCLIENT, file, &initial_stats, iflags, NULL);
951e826b 777 else if (!am_server && INFO_GTE(NAME, 1) && INFO_EQ(PROGRESS, 1))
45c49b52 778 rprintf(FINFO, "%s\n", fname);
11a5a3c7 779
2f03f956 780 /* recv file data */
7e5fa372 781 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
112d728f 782 fname, fd2, F_LENGTH(file));
1b7c47cb 783
2fedf3d5 784 log_item(log_code, file, &initial_stats, iflags, NULL);
17fadf7d 785
d2a918b4 786 if (fd1 != -1)
2f03f956 787 close(fd1);
9f27cd8c 788 if (close(fd2) < 0) {
d62bcc17
WD
789 rsyserr(FERROR, errno, "close failed on %s",
790 full_fname(fnametmp));
9f27cd8c
WD
791 exit_cleanup(RERR_FILEIO);
792 }
17fadf7d 793
68795c64 794 if ((recv_ok && (!delay_updates || !partialptr)) || inplace) {
01d124d9 795 if (partialptr == fname)
83235dbc
WD
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) {
aec6b9f8
WD
801 do_unlink(partialptr);
802 handle_partial_dir(partialptr, PDIR_DELETE);
803 }
459abd75
WD
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))
3e7934a5 814 recv_ok = -1;
83235dbc
WD
815 else if (delay_updates && recv_ok) {
816 bitbag_set_bit(delayed_bits, ndx);
817 recv_ok = 2;
459abd75
WD
818 } else
819 partialptr = NULL;
820 } else
55e50d89 821 do_unlink(fnametmp);
a7260c40 822
2f03f956 823 cleanup_disable();
554e0a8d 824
8ab02ccd
WD
825 if (read_batch)
826 file->flags |= FLAG_FILE_SENT;
827
83235dbc 828 switch (recv_ok) {
459abd75
WD
829 case 2:
830 break;
83235dbc 831 case 1:
3ea6e0e7 832 if (remove_source_files || inc_recurse
112d728f 833 || (preserve_hard_links && F_IS_HLINKED(file)))
0395130c 834 send_msg_int(MSG_SUCCESS, ndx);
83235dbc
WD
835 break;
836 case 0: {
964244b9 837 enum logcode msgtype = redoing ? FERROR_XFER : FWARNING;
951e826b 838 if (msgtype == FERROR_XFER || INFO_GTE(NAME, 1)) {
a7260c40
WD
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";
964244b9 846 if (msgtype == FERROR_XFER) {
007e3c0e
WD
847 errstr = "ERROR";
848 redostr = "";
849 } else {
850 errstr = "WARNING";
964244b9
WD
851 redostr = read_batch ? " (may try again)"
852 : " (will try again)";
007e3c0e
WD
853 }
854 rprintf(msgtype,
a7260c40 855 "%s: %s failed verification -- update %s%s.\n",
964244b9
WD
856 errstr, local_name ? f_name(file, NULL) : fname,
857 keptstr, redostr);
007e3c0e 858 }
d0221f1d 859 if (!redoing) {
8ab02ccd
WD
860 if (read_batch)
861 flist_ndx_push(&batch_redo_list, ndx);
0395130c 862 send_msg_int(MSG_REDO, ndx);
f3d6d480 863 file->flags |= FLAG_FILE_SENT;
3ea6e0e7 864 } else if (inc_recurse)
d0221f1d 865 send_msg_int(MSG_NO_SEND, ndx);
83235dbc
WD
866 break;
867 }
868 case -1:
869 if (inc_recurse)
870 send_msg_int(MSG_NO_SEND, ndx);
871 break;
2f03f956
AT
872 }
873 }
f3d6d480
WD
874 if (make_backups < 0)
875 make_backups = -make_backups;
2f03f956 876
ac3f7b81 877 if (phase == 2 && delay_updates) /* for protocol_version < 29 */
f3d6d480 878 handle_delayed_updates(local_name);
48e1c8c6 879
951e826b 880 if (DEBUG_GTE(RECV, 1))
2f03f956 881 rprintf(FINFO,"recv_files finished\n");
17fadf7d 882
2f03f956
AT
883 return 0;
884}