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