Changed human_num() to big_num() with an extra arg so that it can
[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
d3d07a5e 6 * Copyright (C) 2003-2008 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"
23
16edf865 24extern int dry_run;
a0009fc3 25extern int do_xfers;
8715db2c 26extern int am_server;
3ea6e0e7 27extern int inc_recurse;
910e89b9 28extern int log_before_transfer;
2fedf3d5 29extern int stdout_format_has_i;
ecc7623e 30extern int logfile_format_has_i;
2f03f956 31extern int csum_length;
16cc9ca2 32extern int read_batch;
a0009fc3 33extern int write_batch;
16cc9ca2 34extern int batch_gen_fd;
41cfde6b 35extern int protocol_version;
2f03f956 36extern int relative_paths;
744e63fb 37extern int preserve_hard_links;
92b9eb97 38extern int preserve_perms;
16edf865 39extern int preserve_xattrs;
c56595d7 40extern int basis_dir_cnt;
53dd3135 41extern int make_backups;
925c517f 42extern int cleanup_got_literal;
744e63fb 43extern int remove_source_files;
6cc11982 44extern int append_mode;
e7ee91de 45extern int sparse_files;
55e50d89 46extern int keep_partial;
ba582f75 47extern int checksum_seed;
a3221d2a 48extern int inplace;
48e1c8c6 49extern int delay_updates;
1c3344a1 50extern mode_t orig_umask;
8715db2c 51extern struct stats stats;
8715db2c
WD
52extern char *tmpdir;
53extern char *partial_dir;
54extern char *basis_dir[];
6755a7d7 55extern struct file_list *cur_flist, *first_flist, *dir_flist;
819bfe45 56extern struct filter_list_struct daemon_filter_list;
5ebab6c1 57
10f994a5 58static struct bitbag *delayed_bits = NULL;
d0221f1d 59static int phase = 0, redoing = 0;
48459ba1 60/* We're either updating the basis file or an identical copy: */
77943e69 61static int updating_basis_or_equiv;
3e7934a5 62
52d3e106
S
63/*
64 * get_tmpname() - create a tmp filename for a given filename
65 *
66 * If a tmpdir is defined, use that as the directory to
67 * put it in. Otherwise, the tmp filename is in the same
68 * directory as the given name. Note that there may be no
69 * directory at all in the given name!
17fadf7d 70 *
52d3e106
S
71 * The tmp filename is basically the given filename with a
72 * dot prepended, and .XXXXXX appended (for mkstemp() to
73 * put its unique gunk in). Take care to not exceed
74 * either the MAXPATHLEN or NAME_MAX, esp. the last, as
75 * the basename basically becomes 8 chars longer. In that
76 * case, the original name is shortened sufficiently to
77 * make it all fit.
17fadf7d 78 *
52d3e106
S
79 * Of course, there's no real reason for the tmp name to
80 * look like the original, except to satisfy us humans.
81 * As long as it's unique, rsync will work.
82 */
83
93204cca 84int get_tmpname(char *fnametmp, const char *fname)
2f03f956 85{
d5dcb6f7 86 int maxname, added, length = 0;
93204cca 87 const char *f;
2f03f956 88
2f03f956 89 if (tmpdir) {
a24639bb 90 /* Note: this can't overflow, so the return value is safe */
b7cee949 91 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
52d3e106 92 fnametmp[length++] = '/';
0c2ef5f4 93 }
52d3e106 94
0c2ef5f4 95 if ((f = strrchr(fname, '/')) != NULL) {
52d3e106
S
96 ++f;
97 if (!tmpdir) {
98 length = f - fname;
0c2ef5f4 99 /* copy up to and including the slash */
52d3e106 100 strlcpy(fnametmp, fname, length + 1);
0c2ef5f4 101 }
446e239e 102 } else
52d3e106 103 f = fname;
52d3e106 104 fnametmp[length++] = '.';
2f03f956 105
d5dcb6f7
WD
106 /* The maxname value is bufsize, and includes space for the '\0'.
107 * (Note that NAME_MAX get -8 for the leading '.' above.) */
52d3e106 108 maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8);
2f03f956 109
0c2ef5f4 110 if (maxname < 1) {
3f0211b6 111 rprintf(FERROR_XFER, "temporary filename too long: %s\n", fname);
52d3e106 112 fnametmp[0] = '\0';
2f03f956
AT
113 return 0;
114 }
115
d5dcb6f7
WD
116 added = strlcpy(fnametmp + length, f, maxname);
117 if (added >= maxname)
118 added = maxname - 1;
119 memcpy(fnametmp + length + added, ".XXXXXX", 8);
2f03f956
AT
120
121 return 1;
122}
123
2cce7545
WD
124/* Opens a temporary file for writing.
125 * Success: Writes name into fnametmp, returns fd.
126 * Failure: Clobbers fnametmp, returns -1.
127 * Calling cleanup_set() is the caller's job. */
128int open_tmpfile(char *fnametmp, const char *fname, struct file_struct *file)
129{
130 int fd;
131
132 if (!get_tmpname(fnametmp, fname))
133 return -1;
134
135 /* We initially set the perms without the setuid/setgid bits or group
136 * access to ensure that there is no race condition. They will be
137 * correctly updated after the right owner and group info is set.
138 * (Thanks to snabb@epipe.fi for pointing this out.) */
139 fd = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
140
141#if 0
142 /* In most cases parent directories will already exist because their
143 * information should have been previously transferred, but that may
144 * not be the case with -R */
145 if (fd == -1 && relative_paths && errno == ENOENT
146 && create_directory_path(fnametmp) == 0) {
147 /* Get back to name with XXXXXX in it. */
148 get_tmpname(fnametmp, fname);
149 fd = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
150 }
151#endif
152
153 if (fd == -1) {
3f0211b6 154 rsyserr(FERROR_XFER, errno, "mkstemp %s failed",
2cce7545
WD
155 full_fname(fnametmp));
156 return -1;
157 }
158
159 return fd;
160}
2f03f956 161
7e5fa372 162static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
55edf18c 163 const char *fname, int fd, OFF_T total_size)
2f03f956 164{
a0456b9c
WD
165 static char file_sum1[MAX_DIGEST_LEN];
166 static char file_sum2[MAX_DIGEST_LEN];
7e5fa372 167 struct map_struct *mapbuf;
fc0257c9 168 struct sum_struct sum;
a0456b9c 169 int32 len, sum_len;
2f03f956 170 OFF_T offset = 0;
89e540e6 171 OFF_T offset2;
2f03f956 172 char *data;
6c495e0d 173 int32 i;
e1f67417 174 char *map = NULL;
17fadf7d 175
fc0257c9 176 read_sum_head(f_in, &sum);
17fadf7d 177
7e5fa372 178 if (fd_r >= 0 && size_r > 0) {
80264051
WD
179 int32 read_size = MAX(sum.blength * 2, 16*1024);
180 mapbuf = map_file(fd_r, size_r, read_size, sum.blength);
951e826b 181 if (DEBUG_GTE(CHKSUM, 2)) {
6d56efa6
WD
182 rprintf(FINFO, "recv mapped %s of size %s\n",
183 fname_r, big_num(size_r, 0));
7e5fa372
WD
184 }
185 } else
186 mapbuf = NULL;
187
ba582f75 188 sum_init(checksum_seed);
17fadf7d 189
f3d6d480 190 if (append_mode > 0) {
6cc11982
WD
191 OFF_T j;
192 sum.flength = (OFF_T)sum.count * sum.blength;
193 if (sum.remainder)
194 sum.flength -= sum.blength - sum.remainder;
936fa865
WD
195 if (append_mode == 2) {
196 for (j = CHUNK_SIZE; j < sum.flength; j += CHUNK_SIZE) {
951e826b 197 if (INFO_GTE(PROGRESS, 1))
936fa865
WD
198 show_progress(offset, total_size);
199 sum_update(map_ptr(mapbuf, offset, CHUNK_SIZE),
200 CHUNK_SIZE);
201 offset = j;
202 }
203 if (offset < sum.flength) {
204 int32 len = (int32)(sum.flength - offset);
951e826b 205 if (INFO_GTE(PROGRESS, 1))
936fa865
WD
206 show_progress(offset, total_size);
207 sum_update(map_ptr(mapbuf, offset, len), len);
208 }
6cc11982 209 }
936fa865 210 offset = sum.flength;
ffa8ab8e 211 if (fd != -1 && (j = do_lseek(fd, offset, SEEK_SET)) != offset) {
6d56efa6
WD
212 rsyserr(FERROR_XFER, errno, "lseek of %s returned %s, not %s",
213 full_fname(fname), big_num(j, 0), big_num(offset, 0));
6cc11982
WD
214 exit_cleanup(RERR_FILEIO);
215 }
216 }
217
3f55bd5d 218 while ((i = recv_token(f_in, &data)) != 0) {
951e826b 219 if (INFO_GTE(PROGRESS, 1))
16417f8b 220 show_progress(offset, total_size);
2f03f956
AT
221
222 if (i > 0) {
951e826b 223 if (DEBUG_GTE(CHKSUM, 3)) {
6d56efa6
WD
224 rprintf(FINFO,"data recv %d at %s\n",
225 i, big_num(offset, 0));
2f03f956
AT
226 }
227
228 stats.literal_data += i;
229 cleanup_got_literal = 1;
17fadf7d 230
6c495e0d 231 sum_update(data, i);
2f03f956 232
c52461f9
WD
233 if (fd != -1 && write_file(fd,data,i) != i)
234 goto report_write_error;
2f03f956
AT
235 offset += i;
236 continue;
17fadf7d 237 }
2f03f956
AT
238
239 i = -(i+1);
6c495e0d 240 offset2 = i * (OFF_T)sum.blength;
fc0257c9 241 len = sum.blength;
d2a918b4 242 if (i == (int)sum.count-1 && sum.remainder != 0)
fc0257c9 243 len = sum.remainder;
17fadf7d 244
2f03f956 245 stats.matched_data += len;
17fadf7d 246
951e826b 247 if (DEBUG_GTE(CHKSUM, 3)) {
6c495e0d 248 rprintf(FINFO,
6d56efa6
WD
249 "chunk[%d] of size %ld at %s offset=%s\n",
250 i, (long)len, big_num(offset2, 0), big_num(offset, 0));
6c495e0d 251 }
17fadf7d 252
446e239e
WD
253 if (mapbuf) {
254 map = map_ptr(mapbuf,offset2,len);
17fadf7d 255
c55f7021 256 see_token(map, len);
6c495e0d 257 sum_update(map, len);
c55f7021 258 }
17fadf7d 259
77943e69 260 if (updating_basis_or_equiv) {
89e540e6 261 if (offset == offset2 && fd != -1) {
ffa8ab8e 262 OFF_T pos;
c52461f9
WD
263 if (flush_write_file(fd) < 0)
264 goto report_write_error;
89e540e6 265 offset += len;
ffa8ab8e 266 if ((pos = do_lseek(fd, len, SEEK_CUR)) != offset) {
3f0211b6 267 rsyserr(FERROR_XFER, errno,
6d56efa6 268 "lseek of %s returned %s, not %s",
ffa8ab8e 269 full_fname(fname),
6d56efa6 270 big_num(pos, 0), big_num(offset, 0));
fab65a5b
WD
271 exit_cleanup(RERR_FILEIO);
272 }
89e540e6 273 continue;
a3221d2a 274 }
2f03f956 275 }
1ed91a04 276 if (fd != -1 && map && write_file(fd, map, len) != (int)len)
c52461f9 277 goto report_write_error;
2f03f956
AT
278 offset += len;
279 }
280
85f14172
WD
281 if (flush_write_file(fd) < 0)
282 goto report_write_error;
76c21947 283
4f5b0756 284#ifdef HAVE_FTRUNCATE
fab65a5b 285 if (inplace && fd != -1)
a3221d2a
WD
286 ftruncate(fd, offset);
287#endif
288
951e826b 289 if (INFO_GTE(PROGRESS, 1))
16417f8b 290 end_progress(total_size);
2f03f956
AT
291
292 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
c52461f9 293 report_write_error:
3f0211b6 294 rsyserr(FERROR_XFER, errno, "write failed on %s",
d62bcc17 295 full_fname(fname));
65417579 296 exit_cleanup(RERR_FILEIO);
2f03f956
AT
297 }
298
a0456b9c 299 sum_len = sum_end(file_sum1);
2f03f956 300
7e5fa372
WD
301 if (mapbuf)
302 unmap_file(mapbuf);
303
a0456b9c 304 read_buf(f_in, file_sum2, sum_len);
951e826b 305 if (DEBUG_GTE(CHKSUM, 2))
bc63ae3f 306 rprintf(FINFO,"got file_sum\n");
a0456b9c 307 if (fd != -1 && memcmp(file_sum1, file_sum2, sum_len) != 0)
bc63ae3f 308 return 0;
2f03f956
AT
309 return 1;
310}
311
312
8ed9d849
WD
313static void discard_receive_data(int f_in, OFF_T length)
314{
7e5fa372 315 receive_data(f_in, NULL, -1, 0, NULL, -1, length);
8ed9d849
WD
316}
317
f3d6d480 318static void handle_delayed_updates(char *local_name)
42be5320 319{
663b2857 320 char *fname, *partialptr;
0395130c 321 int ndx;
42be5320 322
0395130c 323 for (ndx = -1; (ndx = bitbag_next_bit(delayed_bits, ndx)) >= 0; ) {
f3d6d480 324 struct file_struct *file = cur_flist->files[ndx];
6cbde57d 325 fname = local_name ? local_name : f_name(file, NULL);
42be5320 326 if ((partialptr = partial_dir_fname(fname)) != NULL) {
f3d6d480 327 if (make_backups > 0 && !make_backup(fname))
42be5320 328 continue;
951e826b 329 if (DEBUG_GTE(RECV, 1)) {
42be5320 330 rprintf(FINFO, "renaming %s to %s\n",
45c49b52 331 partialptr, fname);
42be5320 332 }
6a94c58b
WD
333 /* We don't use robust_rename() here because the
334 * partial-dir must be on the same drive. */
42be5320 335 if (do_rename(partialptr, fname) < 0) {
3f0211b6 336 rsyserr(FERROR_XFER, errno,
42be5320 337 "rename failed for %s (from %s)",
45c49b52 338 full_fname(fname), partialptr);
42be5320 339 } else {
47c11975 340 if (remove_source_files
112d728f 341 || (preserve_hard_links && F_IS_HLINKED(file)))
0395130c 342 send_msg_int(MSG_SUCCESS, ndx);
45c49b52 343 handle_partial_dir(partialptr, PDIR_DELETE);
42be5320
WD
344 }
345 }
346 }
347}
348
f3d6d480 349static int get_next_gen_ndx(int fd, int next_gen_ndx, int desired_ndx)
154cdaaa 350{
0395130c
WD
351 while (next_gen_ndx < desired_ndx) {
352 if (next_gen_ndx >= 0) {
964244b9
WD
353 struct file_struct *file = cur_flist->files[next_gen_ndx];
354 rprintf(FERROR_XFER,
1ed91a04 355 "(No batched update for%s \"%s\")\n",
964244b9
WD
356 file->flags & FLAG_FILE_SENT ? " resend of" : "",
357 f_name(file, NULL));
f3d6d480
WD
358 }
359 next_gen_ndx = read_int(fd);
360 if (next_gen_ndx == -1) {
3ea6e0e7 361 if (inc_recurse)
9decb4d2 362 next_gen_ndx = first_flist->prev->used + first_flist->prev->ndx_start;
f3d6d480 363 else
9decb4d2 364 next_gen_ndx = cur_flist->used;
154cdaaa 365 }
154cdaaa 366 }
0395130c 367 return next_gen_ndx;
154cdaaa
WD
368}
369
b35d0d8e
MP
370/**
371 * main routine for receiver process.
372 *
373 * Receiver process runs on the same host as the generator process. */
f3d6d480 374int recv_files(int f_in, char *local_name)
17fadf7d 375{
0395130c 376 int next_gen_ndx = -1;
2f03f956
AT
377 int fd1,fd2;
378 STRUCT_STAT st;
b9232d45 379 int iflags, xlen;
446e239e 380 char *fname, fbuf[MAXPATHLEN];
b9232d45 381 char xname[MAXPATHLEN];
2f03f956 382 char fnametmp[MAXPATHLEN];
663b2857 383 char *fnamecmp, *partialptr;
375a4556 384 char fnamecmpbuf[MAXPATHLEN];
9e4a8d29 385 uchar fnamecmp_type;
2f03f956 386 struct file_struct *file;
1b7c47cb 387 struct stats initial_stats;
2fedf3d5
WD
388 int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i;
389 enum logcode log_code = log_before_transfer ? FLOG : FINFO;
1ed91a04 390 int max_phase = protocol_version >= 29 ? 2 : 1;
1c3344a1
WD
391 int dflt_perms = (ACCESSPERMS & ~orig_umask);
392#ifdef SUPPORT_ACLS
393 const char *parent_dirname = "";
394#endif
0395130c 395 int ndx, recv_ok;
11a5a3c7 396
951e826b 397 if (DEBUG_GTE(RECV, 1))
9decb4d2 398 rprintf(FINFO, "recv_files(%d) starting\n", cur_flist->used);
2f03f956 399
3e7934a5 400 if (delay_updates)
9decb4d2 401 delayed_bits = bitbag_create(cur_flist->used + 1);
48e1c8c6 402
17fadf7d 403 while (1) {
2f03f956
AT
404 cleanup_disable();
405
f3d6d480 406 /* This call also sets cur_flist. */
16edf865
WD
407 ndx = read_ndx_and_attrs(f_in, &iflags, &fnamecmp_type,
408 xname, &xlen);
0395130c 409 if (ndx == NDX_DONE) {
951e826b
WD
410 if (!am_server && INFO_GTE(PROGRESS, 2) && cur_flist) {
411 set_current_file_index(NULL, 0);
412 end_progress(0);
413 }
3ea6e0e7 414 if (inc_recurse && first_flist) {
f3d6d480
WD
415 flist_free(first_flist);
416 if (first_flist)
417 continue;
418 }
419 if (read_batch && cur_flist) {
3ea6e0e7 420 int high = inc_recurse
9decb4d2
WD
421 ? first_flist->prev->used + first_flist->prev->ndx_start
422 : cur_flist->used;
f3d6d480 423 get_next_gen_ndx(batch_gen_fd, next_gen_ndx, high);
0395130c 424 next_gen_ndx = -1;
16cc9ca2 425 }
ac3f7b81 426 if (++phase > max_phase)
4e834af1 427 break;
951e826b 428 if (DEBUG_GTE(RECV, 1))
4e834af1 429 rprintf(FINFO, "recv_files phase=%d\n", phase);
ac3f7b81 430 if (phase == 2 && delay_updates)
f3d6d480 431 handle_delayed_updates(local_name);
332cf6df 432 send_msg(MSG_DONE, "", 0, 0);
4e834af1 433 continue;
2f03f956
AT
434 }
435
6755a7d7
WD
436 if (ndx - cur_flist->ndx_start >= 0)
437 file = cur_flist->files[ndx - cur_flist->ndx_start];
438 else
439 file = dir_flist->files[cur_flist->parent_ndx];
6cbde57d 440 fname = local_name ? local_name : f_name(file, fbuf);
227a9c41 441
951e826b 442 if (DEBUG_GTE(RECV, 1))
45c49b52 443 rprintf(FINFO, "recv_files(%s)\n", fname);
910e89b9 444
16edf865
WD
445#ifdef SUPPORT_XATTRS
446 if (iflags & ITEM_REPORT_XATTR && !dry_run)
447 recv_xattr_request(file, f_in);
448#endif
449
22907b6b 450 if (!(iflags & ITEM_TRANSFER)) {
b9232d45 451 maybe_log_item(file, iflags, itemizing, xname);
16edf865
WD
452#ifdef SUPPORT_XATTRS
453 if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && !dry_run)
454 set_file_attrs(fname, file, NULL, fname, 0);
455#endif
58a14ed9
WD
456 continue;
457 }
ac3f7b81
WD
458 if (phase == 2) {
459 rprintf(FERROR,
460 "got transfer request in phase 2 [%s]\n",
461 who_am_i());
462 exit_cleanup(RERR_PROTOCOL);
463 }
910e89b9 464
f3d6d480
WD
465 if (file->flags & FLAG_FILE_SENT) {
466 if (csum_length == SHORT_SUM_LENGTH) {
467 if (keep_partial && !partial_dir)
468 make_backups = -make_backups; /* prevents double backup */
936fa865
WD
469 if (append_mode)
470 sparse_files = -sparse_files;
f3d6d480 471 append_mode = -append_mode;
f3d6d480 472 csum_length = SUM_LENGTH;
d0221f1d 473 redoing = 1;
f3d6d480
WD
474 }
475 } else {
476 if (csum_length != SHORT_SUM_LENGTH) {
477 if (keep_partial && !partial_dir)
478 make_backups = -make_backups;
936fa865
WD
479 if (append_mode)
480 sparse_files = -sparse_files;
f3d6d480 481 append_mode = -append_mode;
f3d6d480 482 csum_length = SHORT_SUM_LENGTH;
d0221f1d 483 redoing = 0;
f3d6d480
WD
484 }
485 }
486
951e826b 487 if (!am_server && INFO_GTE(PROGRESS, 1))
65b4e4b2 488 set_current_file_index(file, ndx);
2f03f956 489 stats.num_transferred_files++;
112d728f 490 stats.total_transferred_size += F_LENGTH(file);
65b4e4b2 491
925c517f 492 cleanup_got_literal = 0;
2f03f956 493
819bfe45 494 if (daemon_filter_list.head
1df02d13 495 && check_filter(&daemon_filter_list, FLOG, fname, 0) < 0) {
8c2ffaf0
WD
496 rprintf(FERROR, "attempt to hack rsync failed.\n");
497 exit_cleanup(RERR_PROTOCOL);
498 }
499
a0009fc3 500 if (!do_xfers) { /* log the transfer */
85909931 501 log_item(FCLIENT, file, &stats, iflags, NULL);
f957e8fd 502 if (read_batch)
112d728f 503 discard_receive_data(f_in, F_LENGTH(file));
2f03f956
AT
504 continue;
505 }
a0009fc3 506 if (write_batch < 0) {
1c65a93d 507 log_item(FCLIENT, file, &stats, iflags, NULL);
b10917a4 508 if (!am_server)
112d728f 509 discard_receive_data(f_in, F_LENGTH(file));
a0009fc3
WD
510 continue;
511 }
2f03f956 512
16cc9ca2 513 if (read_batch) {
0395130c
WD
514 next_gen_ndx = get_next_gen_ndx(batch_gen_fd, next_gen_ndx, ndx);
515 if (ndx < next_gen_ndx) {
45c49b52
WD
516 rprintf(FINFO,
517 "(Skipping batched update for \"%s\")\n",
518 fname);
112d728f 519 discard_receive_data(f_in, F_LENGTH(file));
a430691d
WD
520 if (inc_recurse)
521 send_msg_int(MSG_NO_SEND, ndx);
16cc9ca2
WD
522 continue;
523 }
0395130c 524 next_gen_ndx = -1;
16cc9ca2
WD
525 }
526
41cfde6b
WD
527 partialptr = partial_dir ? partial_dir_fname(fname) : fname;
528
9e4a8d29
WD
529 if (protocol_version >= 29) {
530 switch (fnamecmp_type) {
41cfde6b 531 case FNAMECMP_FNAME:
a7260c40 532 fnamecmp = fname;
41cfde6b
WD
533 break;
534 case FNAMECMP_PARTIAL_DIR:
9e4a8d29 535 fnamecmp = partialptr;
41cfde6b
WD
536 break;
537 case FNAMECMP_BACKUP:
538 fnamecmp = get_backup_name(fname);
539 break;
73273075 540 case FNAMECMP_FUZZY:
b9232d45
WD
541 if (file->dirname) {
542 pathjoin(fnamecmpbuf, MAXPATHLEN,
543 file->dirname, xname);
544 fnamecmp = fnamecmpbuf;
545 } else
546 fnamecmp = xname;
73273075 547 break;
41cfde6b 548 default:
9e4a8d29 549 if (fnamecmp_type >= basis_dir_cnt) {
c56595d7
WD
550 rprintf(FERROR,
551 "invalid basis_dir index: %d.\n",
9e4a8d29 552 fnamecmp_type);
c56595d7
WD
553 exit_cleanup(RERR_PROTOCOL);
554 }
41cfde6b 555 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
9e4a8d29 556 basis_dir[fnamecmp_type], fname);
41cfde6b
WD
557 fnamecmp = fnamecmpbuf;
558 break;
559 }
819bfe45 560 if (!fnamecmp || (daemon_filter_list.head
1df02d13 561 && check_filter(&daemon_filter_list, FLOG, fname, 0) < 0)) {
9e4a8d29 562 fnamecmp = fname;
77943e69
WD
563 fnamecmp_type = FNAMECMP_FNAME;
564 }
9e4a8d29
WD
565 } else {
566 /* Reminder: --inplace && --partial-dir are never
567 * enabled at the same time. */
f3d6d480 568 if (inplace && make_backups > 0) {
9e4a8d29
WD
569 if (!(fnamecmp = get_backup_name(fname)))
570 fnamecmp = fname;
77943e69
WD
571 else
572 fnamecmp_type = FNAMECMP_BACKUP;
9e4a8d29
WD
573 } else if (partial_dir && partialptr)
574 fnamecmp = partialptr;
575 else
576 fnamecmp = fname;
577 }
dc55d7bd 578
910e89b9
WD
579 initial_stats = stats;
580
17fadf7d 581 /* open the file */
8c9fd200 582 fd1 = do_open(fnamecmp, O_RDONLY, 0);
375a4556 583
9e4a8d29
WD
584 if (fd1 == -1 && protocol_version < 29) {
585 if (fnamecmp != fname) {
586 fnamecmp = fname;
587 fd1 = do_open(fnamecmp, O_RDONLY, 0);
588 }
589
590 if (fd1 == -1 && basis_dir[0]) {
591 /* pre-29 allowed only one alternate basis */
592 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
593 basis_dir[0], fname);
594 fnamecmp = fnamecmpbuf;
595 fd1 = do_open(fnamecmp, O_RDONLY, 0);
596 }
597 }
77943e69
WD
598
599 updating_basis_or_equiv = inplace
600 && (fnamecmp == fname || fnamecmp_type == FNAMECMP_BACKUP);
9e4a8d29 601
d5dcb6f7
WD
602 if (fd1 == -1) {
603 st.st_mode = 0;
604 st.st_size = 0;
605 } else if (do_fstat(fd1,&st) != 0) {
3f0211b6 606 rsyserr(FERROR_XFER, errno, "fstat %s failed",
d62bcc17 607 full_fname(fnamecmp));
112d728f 608 discard_receive_data(f_in, F_LENGTH(file));
2f03f956 609 close(fd1);
a430691d
WD
610 if (inc_recurse)
611 send_msg_int(MSG_NO_SEND, ndx);
2f03f956
AT
612 continue;
613 }
614
350e4e4d
S
615 if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
616 /* this special handling for directories
617 * wouldn't be necessary if robust_rename()
618 * and the underlying robust_unlink could cope
619 * with directories
620 */
3f0211b6 621 rprintf(FERROR_XFER, "recv_files: %s is a directory\n",
ea42541f 622 full_fname(fnamecmp));
112d728f 623 discard_receive_data(f_in, F_LENGTH(file));
2f03f956 624 close(fd1);
a430691d
WD
625 if (inc_recurse)
626 send_msg_int(MSG_NO_SEND, ndx);
2f03f956
AT
627 continue;
628 }
629
350e4e4d
S
630 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
631 close(fd1);
632 fd1 = -1;
350e4e4d
S
633 }
634
a9d6e6fc
WD
635 /* If we're not preserving permissions, change the file-list's
636 * mode based on the local permissions and some heuristics. */
637 if (!preserve_perms) {
638 int exists = fd1 != -1;
1c3344a1
WD
639#ifdef SUPPORT_ACLS
640 const char *dn = file->dirname ? file->dirname : ".";
641 if (parent_dirname != dn
642 && strcmp(parent_dirname, dn) != 0) {
643 dflt_perms = default_perms_for_dir(dn);
644 parent_dirname = dn;
645 }
646#endif
647 file->mode = dest_mode(file->mode, st.st_mode,
648 dflt_perms, exists);
4df9f368
AT
649 }
650
48459ba1 651 /* We now check to see if we are writing the file "inplace" */
a3221d2a 652 if (inplace) {
97894c64 653 fd2 = do_open(fname, O_WRONLY|O_CREAT, 0600);
a3221d2a 654 if (fd2 == -1) {
3f0211b6 655 rsyserr(FERROR_XFER, errno, "open %s failed",
dc55d7bd 656 full_fname(fname));
a3221d2a
WD
657 }
658 } else {
2cce7545
WD
659 fd2 = open_tmpfile(fnametmp, fname, file);
660 if (fd2 != -1)
661 cleanup_set(fnametmp, partialptr, file, fd1, fd2);
662 }
a3221d2a 663
2cce7545
WD
664 if (fd2 == -1) {
665 discard_receive_data(f_in, F_LENGTH(file));
666 if (fd1 != -1)
667 close(fd1);
668 if (inc_recurse)
669 send_msg_int(MSG_NO_SEND, ndx);
670 continue;
a3221d2a 671 }
2f03f956 672
a8ed4958 673 /* log the transfer */
910e89b9 674 if (log_before_transfer)
85909931 675 log_item(FCLIENT, file, &initial_stats, iflags, NULL);
951e826b 676 else if (!am_server && INFO_GTE(NAME, 1) && INFO_EQ(PROGRESS, 1))
45c49b52 677 rprintf(FINFO, "%s\n", fname);
11a5a3c7 678
2f03f956 679 /* recv file data */
7e5fa372 680 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
112d728f 681 fname, fd2, F_LENGTH(file));
1b7c47cb 682
2fedf3d5 683 log_item(log_code, file, &initial_stats, iflags, NULL);
17fadf7d 684
d2a918b4 685 if (fd1 != -1)
2f03f956 686 close(fd1);
9f27cd8c 687 if (close(fd2) < 0) {
d62bcc17
WD
688 rsyserr(FERROR, errno, "close failed on %s",
689 full_fname(fnametmp));
9f27cd8c
WD
690 exit_cleanup(RERR_FILEIO);
691 }
17fadf7d 692
68795c64 693 if ((recv_ok && (!delay_updates || !partialptr)) || inplace) {
01d124d9 694 if (partialptr == fname)
83235dbc
WD
695 partialptr = NULL;
696 if (!finish_transfer(fname, fnametmp, fnamecmp,
697 partialptr, file, recv_ok, 1))
698 recv_ok = -1;
699 else if (fnamecmp == partialptr) {
aec6b9f8
WD
700 do_unlink(partialptr);
701 handle_partial_dir(partialptr, PDIR_DELETE);
702 }
703 } else if (keep_partial && partialptr
704 && handle_partial_dir(partialptr, PDIR_CREATE)) {
83235dbc
WD
705 if (!finish_transfer(partialptr, fnametmp, fnamecmp, NULL,
706 file, recv_ok, !partial_dir))
3e7934a5 707 recv_ok = -1;
83235dbc
WD
708 else if (delay_updates && recv_ok) {
709 bitbag_set_bit(delayed_bits, ndx);
710 recv_ok = 2;
3e7934a5 711 }
aec6b9f8 712 } else {
a7260c40 713 partialptr = NULL;
55e50d89 714 do_unlink(fnametmp);
a7260c40
WD
715 }
716
2f03f956 717 cleanup_disable();
554e0a8d 718
83235dbc
WD
719 switch (recv_ok) {
720 case 1:
3ea6e0e7 721 if (remove_source_files || inc_recurse
112d728f 722 || (preserve_hard_links && F_IS_HLINKED(file)))
0395130c 723 send_msg_int(MSG_SUCCESS, ndx);
83235dbc
WD
724 break;
725 case 0: {
964244b9 726 enum logcode msgtype = redoing ? FERROR_XFER : FWARNING;
951e826b 727 if (msgtype == FERROR_XFER || INFO_GTE(NAME, 1)) {
a7260c40
WD
728 char *errstr, *redostr, *keptstr;
729 if (!(keep_partial && partialptr) && !inplace)
730 keptstr = "discarded";
731 else if (partial_dir)
732 keptstr = "put into partial-dir";
733 else
734 keptstr = "retained";
964244b9 735 if (msgtype == FERROR_XFER) {
007e3c0e
WD
736 errstr = "ERROR";
737 redostr = "";
738 } else {
739 errstr = "WARNING";
964244b9
WD
740 redostr = read_batch ? " (may try again)"
741 : " (will try again)";
007e3c0e
WD
742 }
743 rprintf(msgtype,
a7260c40 744 "%s: %s failed verification -- update %s%s.\n",
964244b9
WD
745 errstr, local_name ? f_name(file, NULL) : fname,
746 keptstr, redostr);
007e3c0e 747 }
d0221f1d 748 if (!redoing) {
0395130c 749 send_msg_int(MSG_REDO, ndx);
f3d6d480 750 file->flags |= FLAG_FILE_SENT;
3ea6e0e7 751 } else if (inc_recurse)
d0221f1d 752 send_msg_int(MSG_NO_SEND, ndx);
83235dbc
WD
753 break;
754 }
755 case -1:
756 if (inc_recurse)
757 send_msg_int(MSG_NO_SEND, ndx);
758 break;
2f03f956
AT
759 }
760 }
f3d6d480
WD
761 if (make_backups < 0)
762 make_backups = -make_backups;
2f03f956 763
ac3f7b81 764 if (phase == 2 && delay_updates) /* for protocol_version < 29 */
f3d6d480 765 handle_delayed_updates(local_name);
48e1c8c6 766
951e826b 767 if (DEBUG_GTE(RECV, 1))
2f03f956 768 rprintf(FINFO,"recv_files finished\n");
17fadf7d 769
2f03f956
AT
770 return 0;
771}