Accept the new keep-alive (no-op) packet.
[rsync/rsync.git] / receiver.c
CommitLineData
e327acec 1/* -*- c-file-style: "linux" -*-
17fadf7d 2
e327acec 3 Copyright (C) 1996-2000 by Andrew Tridgell
2f03f956 4 Copyright (C) Paul Mackerras 1996
17fadf7d 5
2f03f956
AT
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
17fadf7d 10
2f03f956
AT
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
17fadf7d 15
2f03f956
AT
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19*/
20
21#include "rsync.h"
22
23extern int verbose;
227a9c41 24extern int dry_run;
8715db2c
WD
25extern int am_daemon;
26extern int am_server;
27extern int do_progress;
910e89b9 28extern int log_before_transfer;
227a9c41
WD
29extern int log_format_has_i;
30extern int daemon_log_format_has_i;
92b9eb97 31extern int delete_after;
2f03f956 32extern int csum_length;
16cc9ca2
WD
33extern int read_batch;
34extern int batch_gen_fd;
41cfde6b 35extern int protocol_version;
2f03f956 36extern int relative_paths;
566fce32 37extern int keep_dirlinks;
2f03f956 38extern int preserve_hard_links;
92b9eb97 39extern int preserve_perms;
2f03f956 40extern int io_error;
c56595d7 41extern int basis_dir_cnt;
53dd3135 42extern int make_backups;
925c517f 43extern int cleanup_got_literal;
3e870a64 44extern int remove_sent_files;
92b9eb97
WD
45extern int module_id;
46extern int ignore_errors;
47extern int orig_umask;
55e50d89 48extern int keep_partial;
ba582f75 49extern int checksum_seed;
a3221d2a 50extern int inplace;
48e1c8c6 51extern int delay_updates;
8715db2c 52extern struct stats stats;
910e89b9 53extern char *log_format;
8715db2c
WD
54extern char *tmpdir;
55extern char *partial_dir;
56extern char *basis_dir[];
2f03f956 57
7842418b 58extern struct filter_list_struct server_filter_list;
5ebab6c1
WD
59
60
3ab56a20
WD
61/* This deletes any files on the receiving side that are not present on the
62 * sending side. This is used by --delete-before and --delete-after. */
6957ae33 63void delete_files(struct file_list *flist)
2f03f956 64{
68a94ac3 65 char fbuf[MAXPATHLEN];
3ab56a20 66 int j;
2f03f956 67
1e82e2ce 68 for (j = 0; j < flist->count; j++) {
68a94ac3 69 struct file_struct *file = flist->files[j];
462c51d9 70
68a94ac3 71 if (!(file->flags & FLAG_DEL_HERE))
462c51d9 72 continue;
2430e984 73
68a94ac3
WD
74 f_name_to(file, fbuf);
75 if (verbose > 1 && file->flags & FLAG_TOP_DIR)
76 rprintf(FINFO, "deleting in %s\n", safe_fname(fbuf));
2430e984 77
68a94ac3 78 delete_in_dir(flist, fbuf, file);
2f03f956
AT
79 }
80}
81
3e7934a5
WD
82#define SLOT_SIZE (16*1024) /* Desired size in bytes */
83#define PER_SLOT_BITS (SLOT_SIZE * 8) /* Number of bits per slot */
84#define PER_SLOT_INTS (SLOT_SIZE / 4) /* Number of int32s per slot */
85
86static uint32 **delayed_bits = NULL;
87static int delayed_slot_cnt = 0;
88
89static void init_delayed_bits(int max_ndx)
90{
91 delayed_slot_cnt = (max_ndx + PER_SLOT_BITS - 1) / PER_SLOT_BITS;
92
93 if (!(delayed_bits = (uint32**)calloc(delayed_slot_cnt, sizeof (uint32*))))
94 out_of_memory("set_delayed_bit");
95}
96
97static void set_delayed_bit(int ndx)
98{
99 int slot = ndx / PER_SLOT_BITS;
100 ndx %= PER_SLOT_BITS;
101
102 if (!delayed_bits[slot]) {
103 if (!(delayed_bits[slot] = (uint32*)calloc(PER_SLOT_INTS, 4)))
104 out_of_memory("set_delayed_bit");
105 }
106
107 delayed_bits[slot][ndx/32] |= 1u << (ndx % 32);
108}
109
110/* Call this with -1 to start checking from 0. Returns -1 at the end. */
111static int next_delayed_bit(int after)
112{
113 uint32 bits, mask;
114 int i, ndx = after + 1;
115 int slot = ndx / PER_SLOT_BITS;
116 ndx %= PER_SLOT_BITS;
117
118 mask = (1u << (ndx % 32)) - 1;
119 for (i = ndx / 32; slot < delayed_slot_cnt; slot++, i = mask = 0) {
120 if (!delayed_bits[slot])
121 continue;
122 for ( ; i < PER_SLOT_INTS; i++, mask = 0) {
123 if (!(bits = delayed_bits[slot][i] & ~mask))
124 continue;
125 /* The xor magic figures out the lowest enabled bit in
126 * bits, and the switch quickly computes log2(bit). */
127 switch (bits ^ (bits & (bits-1))) {
128#define LOG2(n) case 1u << n: return slot*PER_SLOT_BITS + i*32 + n
129 LOG2(0); LOG2(1); LOG2(2); LOG2(3);
130 LOG2(4); LOG2(5); LOG2(6); LOG2(7);
131 LOG2(8); LOG2(9); LOG2(10); LOG2(11);
132 LOG2(12); LOG2(13); LOG2(14); LOG2(15);
133 LOG2(16); LOG2(17); LOG2(18); LOG2(19);
134 LOG2(20); LOG2(21); LOG2(22); LOG2(23);
135 LOG2(24); LOG2(25); LOG2(26); LOG2(27);
136 LOG2(28); LOG2(29); LOG2(30); LOG2(31);
137 }
138 return -1; /* impossible... */
139 }
140 }
141
142 return -1;
143}
144
2f03f956 145
52d3e106
S
146/*
147 * get_tmpname() - create a tmp filename for a given filename
148 *
149 * If a tmpdir is defined, use that as the directory to
150 * put it in. Otherwise, the tmp filename is in the same
151 * directory as the given name. Note that there may be no
152 * directory at all in the given name!
17fadf7d 153 *
52d3e106
S
154 * The tmp filename is basically the given filename with a
155 * dot prepended, and .XXXXXX appended (for mkstemp() to
156 * put its unique gunk in). Take care to not exceed
157 * either the MAXPATHLEN or NAME_MAX, esp. the last, as
158 * the basename basically becomes 8 chars longer. In that
159 * case, the original name is shortened sufficiently to
160 * make it all fit.
17fadf7d 161 *
52d3e106
S
162 * Of course, there's no real reason for the tmp name to
163 * look like the original, except to satisfy us humans.
164 * As long as it's unique, rsync will work.
165 */
166
2f03f956
AT
167static int get_tmpname(char *fnametmp, char *fname)
168{
169 char *f;
52d3e106
S
170 int length = 0;
171 int maxname;
2f03f956 172
2f03f956 173 if (tmpdir) {
a24639bb 174 /* Note: this can't overflow, so the return value is safe */
b7cee949 175 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
52d3e106
S
176 fnametmp[length++] = '/';
177 fnametmp[length] = '\0'; /* always NULL terminated */
0c2ef5f4 178 }
52d3e106 179
0c2ef5f4 180 if ((f = strrchr(fname, '/')) != NULL) {
52d3e106
S
181 ++f;
182 if (!tmpdir) {
183 length = f - fname;
0c2ef5f4 184 /* copy up to and including the slash */
52d3e106 185 strlcpy(fnametmp, fname, length + 1);
0c2ef5f4 186 }
446e239e 187 } else
52d3e106 188 f = fname;
52d3e106
S
189 fnametmp[length++] = '.';
190 fnametmp[length] = '\0'; /* always NULL terminated */
2f03f956 191
52d3e106 192 maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8);
2f03f956 193
0c2ef5f4 194 if (maxname < 1) {
71903f60
WD
195 rprintf(FERROR, "temporary filename too long: %s\n",
196 safe_fname(fname));
52d3e106 197 fnametmp[0] = '\0';
2f03f956
AT
198 return 0;
199 }
200
17fadf7d 201 strlcpy(fnametmp + length, f, maxname);
52d3e106 202 strcat(fnametmp + length, ".XXXXXX");
2f03f956
AT
203
204 return 1;
205}
206
207
7e5fa372
WD
208static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
209 char *fname, int fd, OFF_T total_size)
2f03f956 210{
7e5fa372
WD
211 static char file_sum1[MD4_SUM_LENGTH];
212 static char file_sum2[MD4_SUM_LENGTH];
213 struct map_struct *mapbuf;
fc0257c9 214 struct sum_struct sum;
6c495e0d 215 int32 len;
2f03f956 216 OFF_T offset = 0;
89e540e6 217 OFF_T offset2;
2f03f956 218 char *data;
6c495e0d 219 int32 i;
e1f67417 220 char *map = NULL;
17fadf7d 221
fc0257c9 222 read_sum_head(f_in, &sum);
17fadf7d 223
7e5fa372 224 if (fd_r >= 0 && size_r > 0) {
80264051
WD
225 int32 read_size = MAX(sum.blength * 2, 16*1024);
226 mapbuf = map_file(fd_r, size_r, read_size, sum.blength);
7e5fa372
WD
227 if (verbose > 2) {
228 rprintf(FINFO, "recv mapped %s of size %.0f\n",
ecc81fce 229 safe_fname(fname_r), (double)size_r);
7e5fa372
WD
230 }
231 } else
232 mapbuf = NULL;
233
ba582f75 234 sum_init(checksum_seed);
17fadf7d 235
3f55bd5d 236 while ((i = recv_token(f_in, &data)) != 0) {
16417f8b
WD
237 if (do_progress)
238 show_progress(offset, total_size);
2f03f956
AT
239
240 if (i > 0) {
2f03f956 241 if (verbose > 3) {
5f808dfb
AT
242 rprintf(FINFO,"data recv %d at %.0f\n",
243 i,(double)offset);
2f03f956
AT
244 }
245
246 stats.literal_data += i;
247 cleanup_got_literal = 1;
17fadf7d 248
6c495e0d 249 sum_update(data, i);
2f03f956 250
c52461f9
WD
251 if (fd != -1 && write_file(fd,data,i) != i)
252 goto report_write_error;
2f03f956
AT
253 offset += i;
254 continue;
17fadf7d 255 }
2f03f956
AT
256
257 i = -(i+1);
6c495e0d 258 offset2 = i * (OFF_T)sum.blength;
fc0257c9 259 len = sum.blength;
d2a918b4 260 if (i == (int)sum.count-1 && sum.remainder != 0)
fc0257c9 261 len = sum.remainder;
17fadf7d 262
2f03f956 263 stats.matched_data += len;
17fadf7d 264
6c495e0d
WD
265 if (verbose > 3) {
266 rprintf(FINFO,
267 "chunk[%d] of size %ld at %.0f offset=%.0f\n",
268 i, (long)len, (double)offset2, (double)offset);
269 }
17fadf7d 270
446e239e
WD
271 if (mapbuf) {
272 map = map_ptr(mapbuf,offset2,len);
17fadf7d 273
c55f7021 274 see_token(map, len);
6c495e0d 275 sum_update(map, len);
c55f7021 276 }
17fadf7d 277
fab65a5b 278 if (inplace) {
89e540e6 279 if (offset == offset2 && fd != -1) {
c52461f9
WD
280 if (flush_write_file(fd) < 0)
281 goto report_write_error;
89e540e6
WD
282 offset += len;
283 if (do_lseek(fd, len, SEEK_CUR) != offset) {
fab65a5b
WD
284 rsyserr(FERROR, errno,
285 "lseek failed on %s",
286 full_fname(fname));
287 exit_cleanup(RERR_FILEIO);
288 }
89e540e6 289 continue;
a3221d2a 290 }
2f03f956 291 }
c52461f9
WD
292 if (fd != -1 && write_file(fd, map, len) != (int)len)
293 goto report_write_error;
2f03f956
AT
294 offset += len;
295 }
296
85f14172
WD
297 if (flush_write_file(fd) < 0)
298 goto report_write_error;
76c21947 299
4f5b0756 300#ifdef HAVE_FTRUNCATE
fab65a5b 301 if (inplace && fd != -1)
a3221d2a
WD
302 ftruncate(fd, offset);
303#endif
304
16417f8b
WD
305 if (do_progress)
306 end_progress(total_size);
2f03f956
AT
307
308 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
c52461f9 309 report_write_error:
d62bcc17
WD
310 rsyserr(FERROR, errno, "write failed on %s",
311 full_fname(fname));
65417579 312 exit_cleanup(RERR_FILEIO);
2f03f956
AT
313 }
314
315 sum_end(file_sum1);
316
7e5fa372
WD
317 if (mapbuf)
318 unmap_file(mapbuf);
319
bc63ae3f 320 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
d2a918b4 321 if (verbose > 2)
bc63ae3f 322 rprintf(FINFO,"got file_sum\n");
d2a918b4 323 if (fd != -1 && memcmp(file_sum1, file_sum2, MD4_SUM_LENGTH) != 0)
bc63ae3f 324 return 0;
2f03f956
AT
325 return 1;
326}
327
328
73273075
WD
329static void read_gen_name(int fd, char *dirname, char *buf)
330{
331 int dlen;
332 int len = read_byte(fd);
333
334 if (len & 0x80) {
335#if MAXPATHLEN > 32767
336 uchar lenbuf[2];
337 read_buf(fd, (char *)lenbuf, 2);
338 len = (len & ~0x80) * 0x10000 + lenbuf[0] * 0x100 + lenbuf[1];
339#else
340 len = (len & ~0x80) * 0x100 + read_byte(fd);
341#endif
342 }
343
344 if (dirname) {
345 dlen = strlcpy(buf, dirname, MAXPATHLEN);
346 buf[dlen++] = '/';
347 } else
348 dlen = 0;
349
350 if (dlen + len >= MAXPATHLEN) {
351 rprintf(FERROR, "bogus data on generator name pipe\n");
352 exit_cleanup(RERR_PROTOCOL);
353 }
354
355 read_sbuf(fd, buf + dlen, len);
356}
357
358
8ed9d849
WD
359static void discard_receive_data(int f_in, OFF_T length)
360{
7e5fa372 361 receive_data(f_in, NULL, -1, 0, NULL, -1, length);
8ed9d849
WD
362}
363
364
b35d0d8e
MP
365/**
366 * main routine for receiver process.
367 *
368 * Receiver process runs on the same host as the generator process. */
41cfde6b
WD
369int recv_files(int f_in, struct file_list *flist, char *local_name,
370 int f_in_name)
17fadf7d 371{
16cc9ca2 372 int next_gen_i = -1;
2f03f956
AT
373 int fd1,fd2;
374 STRUCT_STAT st;
910e89b9 375 int iflags;
446e239e 376 char *fname, fbuf[MAXPATHLEN];
f62c17e3 377 char template[MAXPATHLEN];
2f03f956 378 char fnametmp[MAXPATHLEN];
3e870a64 379 char *fnamecmp, *partialptr, numbuf[4];
375a4556 380 char fnamecmpbuf[MAXPATHLEN];
2f03f956 381 struct file_struct *file;
1b7c47cb 382 struct stats initial_stats;
4e834af1 383 int save_make_backups = make_backups;
227a9c41
WD
384 int itemizing = am_daemon ? daemon_log_format_has_i
385 : !am_server && log_format_has_i;
4e834af1 386 int i, recv_ok, phase = 0;
11a5a3c7 387
d2a918b4 388 if (verbose > 2)
2f03f956 389 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
2f03f956 390
cb869c26 391 if (flist->hlink_pool) {
9935066b
S
392 pool_destroy(flist->hlink_pool);
393 flist->hlink_pool = NULL;
394 }
395
3e7934a5
WD
396 if (delay_updates)
397 init_delayed_bits(flist->count);
48e1c8c6 398
17fadf7d 399 while (1) {
2f03f956
AT
400 cleanup_disable();
401
402 i = read_int(f_in);
403 if (i == -1) {
16cc9ca2 404 if (read_batch) {
41cfde6b
WD
405 if (next_gen_i != flist->count) {
406 do {
407 if (f_in_name >= 0
408 && next_gen_i >= 0)
409 read_byte(f_in_name);
410 } while (read_int(batch_gen_fd) != -1);
411 }
16cc9ca2
WD
412 next_gen_i = -1;
413 }
414
4e834af1
WD
415 if (phase)
416 break;
5ebab6c1 417
4e834af1
WD
418 phase = 1;
419 csum_length = SUM_LENGTH;
420 if (verbose > 2)
421 rprintf(FINFO, "recv_files phase=%d\n", phase);
422 send_msg(MSG_DONE, "", 0);
aec6b9f8 423 if (keep_partial && !partial_dir)
4e834af1
WD
424 make_backups = 0; /* prevents double backup */
425 continue;
2f03f956
AT
426 }
427
428 if (i < 0 || i >= flist->count) {
8e6cf5d1
WD
429 /* Handle the new keep-alive (no-op) packet. */
430 if (i == flist->count && protocol_version >= 29
431 && read_shortint(f_in) == ITEM_IS_NEW)
432 continue;
17fadf7d 433 rprintf(FERROR,"Invalid file index %d in recv_files (count=%d)\n",
2f03f956 434 i, flist->count);
65417579 435 exit_cleanup(RERR_PROTOCOL);
2f03f956
AT
436 }
437
438 file = flist->files[i];
227a9c41
WD
439 fname = local_name ? local_name : f_name_to(file, fbuf);
440
441 if (verbose > 2)
442 rprintf(FINFO, "recv_files(%s)\n", safe_fname(fname));
910e89b9 443
4bb0af79 444 if (protocol_version >= 29) {
742be97c 445 iflags = read_shortint(f_in);
910e89b9 446 if (!(iflags & ITEM_UPDATING) || !S_ISREG(file->mode)) {
227a9c41
WD
447 int see_item = itemizing && (iflags || verbose > 1);
448 if (am_server) {
449 if (am_daemon && !dry_run && see_item)
450 log_recv(file, &stats, iflags);
451 } else if (see_item || iflags & ITEM_UPDATING
e844be4e
WD
452 || (S_ISDIR(file->mode)
453 && iflags & ITEM_REPORT_TIME))
910e89b9
WD
454 log_recv(file, &stats, iflags);
455 continue;
456 }
457 } else
458 iflags = ITEM_UPDATING | ITEM_MISSING_DATA;
459
460 if (!S_ISREG(file->mode)) {
461 rprintf(FERROR, "[%s] got index of non-regular file: %d\n",
8c2ffaf0
WD
462 who_am_i(), i);
463 exit_cleanup(RERR_PROTOCOL);
464 }
2f03f956 465
97feb557 466 stats.current_file_index = i;
2f03f956
AT
467 stats.num_transferred_files++;
468 stats.total_transferred_size += file->length;
925c517f 469 cleanup_got_literal = 0;
2f03f956 470
8c2ffaf0
WD
471 if (server_filter_list.head
472 && check_filter(&server_filter_list, fname, 0) < 0) {
473 rprintf(FERROR, "attempt to hack rsync failed.\n");
474 exit_cleanup(RERR_PROTOCOL);
475 }
476
a8ed4958 477 if (dry_run) { /* log the transfer */
4bb0af79 478 if (!am_server && log_format)
910e89b9 479 log_recv(file, &stats, iflags);
2f03f956
AT
480 continue;
481 }
482
16cc9ca2
WD
483 if (read_batch) {
484 while (i > next_gen_i) {
41cfde6b
WD
485 if (f_in_name >= 0 && next_gen_i >= 0)
486 read_byte(f_in_name);
16cc9ca2
WD
487 next_gen_i = read_int(batch_gen_fd);
488 if (next_gen_i == -1)
489 next_gen_i = flist->count;
490 }
491 if (i < next_gen_i) {
492 rprintf(FINFO, "skipping update for \"%s\"\n",
ecc81fce 493 safe_fname(fname));
16cc9ca2
WD
494 discard_receive_data(f_in, file->length);
495 continue;
496 }
41cfde6b 497 next_gen_i = -1;
16cc9ca2
WD
498 }
499
41cfde6b
WD
500 partialptr = partial_dir ? partial_dir_fname(fname) : fname;
501
502 if (f_in_name >= 0) {
ee297522
WD
503 uchar j;
504 switch (j = read_byte(f_in_name)) {
41cfde6b 505 case FNAMECMP_FNAME:
a7260c40 506 fnamecmp = fname;
41cfde6b
WD
507 break;
508 case FNAMECMP_PARTIAL_DIR:
509 fnamecmp = partialptr ? partialptr : fname;
510 break;
511 case FNAMECMP_BACKUP:
512 fnamecmp = get_backup_name(fname);
513 break;
73273075
WD
514 case FNAMECMP_FUZZY:
515 read_gen_name(f_in_name, file->dirname, fnamecmpbuf);
516 fnamecmp = fnamecmpbuf;
517 break;
41cfde6b 518 default:
c56595d7
WD
519 if (j >= basis_dir_cnt) {
520 rprintf(FERROR,
521 "invalid basis_dir index: %d.\n",
522 j);
523 exit_cleanup(RERR_PROTOCOL);
524 }
41cfde6b 525 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
ee297522 526 basis_dir[j], fname);
41cfde6b
WD
527 fnamecmp = fnamecmpbuf;
528 break;
529 }
a7260c40 530 } else
41cfde6b 531 fnamecmp = fname;
dc55d7bd 532
910e89b9
WD
533 initial_stats = stats;
534
17fadf7d 535 /* open the file */
8c9fd200 536 fd1 = do_open(fnamecmp, O_RDONLY, 0);
375a4556 537
2f03f956 538 if (fd1 != -1 && do_fstat(fd1,&st) != 0) {
d62bcc17
WD
539 rsyserr(FERROR, errno, "fstat %s failed",
540 full_fname(fnamecmp));
8ed9d849 541 discard_receive_data(f_in, file->length);
2f03f956
AT
542 close(fd1);
543 continue;
544 }
545
350e4e4d
S
546 if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
547 /* this special handling for directories
548 * wouldn't be necessary if robust_rename()
549 * and the underlying robust_unlink could cope
550 * with directories
551 */
ea42541f
WD
552 rprintf(FERROR,"recv_files: %s is a directory\n",
553 full_fname(fnamecmp));
8ed9d849 554 discard_receive_data(f_in, file->length);
2f03f956
AT
555 close(fd1);
556 continue;
557 }
558
350e4e4d
S
559 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
560 close(fd1);
561 fd1 = -1;
350e4e4d
S
562 }
563
4df9f368 564 if (fd1 != -1 && !preserve_perms) {
85ed0aa3 565 /* if the file exists already and we aren't preserving
17fadf7d
WD
566 * permissions then act as though the remote end sent
567 * us the file permissions we already have */
4df9f368
AT
568 file->mode = st.st_mode;
569 }
570
a3221d2a
WD
571 /* We now check to see if we are writing file "inplace" */
572 if (inplace) {
dc55d7bd 573 fd2 = do_open(fname, O_WRONLY|O_CREAT, 0);
a3221d2a
WD
574 if (fd2 == -1) {
575 rsyserr(FERROR, errno, "open %s failed",
dc55d7bd 576 full_fname(fname));
8ed9d849 577 discard_receive_data(f_in, file->length);
a3221d2a
WD
578 if (fd1 != -1)
579 close(fd1);
580 continue;
581 }
582 } else {
583 if (!get_tmpname(fnametmp,fname)) {
8ed9d849 584 discard_receive_data(f_in, file->length);
a3221d2a
WD
585 if (fd1 != -1)
586 close(fd1);
587 continue;
588 }
589
590 strlcpy(template, fnametmp, sizeof template);
2f03f956 591
a3221d2a
WD
592 /* we initially set the perms without the
593 * setuid/setgid bits to ensure that there is no race
594 * condition. They are then correctly updated after
595 * the lchown. Thanks to snabb@epipe.fi for pointing
596 * this out. We also set it initially without group
597 * access because of a similar race condition. */
f62c17e3 598 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
17fadf7d 599
a3221d2a
WD
600 /* in most cases parent directories will already exist
601 * because their information should have been previously
602 * transferred, but that may not be the case with -R */
603 if (fd2 == -1 && relative_paths && errno == ENOENT
604 && create_directory_path(fnametmp, orig_umask) == 0) {
605 strlcpy(fnametmp, template, sizeof fnametmp);
606 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
607 }
608 if (fd2 == -1) {
609 rsyserr(FERROR, errno, "mkstemp %s failed",
610 full_fname(fnametmp));
8ed9d849 611 discard_receive_data(f_in, file->length);
a3221d2a
WD
612 if (fd1 != -1)
613 close(fd1);
614 continue;
615 }
616
a7260c40
WD
617 if (partialptr)
618 cleanup_set(fnametmp, partialptr, file, fd1, fd2);
a3221d2a 619 }
2f03f956 620
a8ed4958 621 /* log the transfer */
910e89b9
WD
622 if (log_before_transfer)
623 log_recv(file, &initial_stats, iflags);
4bb0af79 624 else if (!am_server && verbose && do_progress)
ecc81fce 625 rprintf(FINFO, "%s\n", safe_fname(fname));
11a5a3c7 626
2f03f956 627 /* recv file data */
7e5fa372
WD
628 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
629 fname, fd2, file->length);
1b7c47cb 630
910e89b9
WD
631 if (!log_before_transfer)
632 log_recv(file, &initial_stats, iflags);
17fadf7d 633
d2a918b4 634 if (fd1 != -1)
2f03f956 635 close(fd1);
9f27cd8c 636 if (close(fd2) < 0) {
d62bcc17
WD
637 rsyserr(FERROR, errno, "close failed on %s",
638 full_fname(fnametmp));
9f27cd8c
WD
639 exit_cleanup(RERR_FILEIO);
640 }
17fadf7d 641
68795c64 642 if ((recv_ok && (!delay_updates || !partialptr)) || inplace) {
aec6b9f8
WD
643 finish_transfer(fname, fnametmp, file, recv_ok, 1);
644 if (partialptr != fname && fnamecmp == partialptr) {
645 do_unlink(partialptr);
646 handle_partial_dir(partialptr, PDIR_DELETE);
647 }
648 } else if (keep_partial && partialptr
649 && handle_partial_dir(partialptr, PDIR_CREATE)) {
650 finish_transfer(partialptr, fnametmp, file, recv_ok,
651 !partial_dir);
3e7934a5
WD
652 if (delay_updates && recv_ok) {
653 set_delayed_bit(i);
654 recv_ok = -1;
655 }
aec6b9f8 656 } else {
a7260c40 657 partialptr = NULL;
55e50d89 658 do_unlink(fnametmp);
a7260c40
WD
659 }
660
2f03f956 661 cleanup_disable();
554e0a8d 662
3e870a64 663 if (recv_ok) {
3e7934a5 664 if (remove_sent_files && recv_ok > 0) {
3e870a64
WD
665 SIVAL(numbuf, 0, i);
666 send_msg(MSG_SUCCESS, numbuf, 4);
667 }
668 } else {
007e3c0e
WD
669 int msgtype = csum_length == SUM_LENGTH || read_batch ?
670 FERROR : FINFO;
671 if (msgtype == FERROR || verbose) {
a7260c40
WD
672 char *errstr, *redostr, *keptstr;
673 if (!(keep_partial && partialptr) && !inplace)
674 keptstr = "discarded";
675 else if (partial_dir)
676 keptstr = "put into partial-dir";
677 else
678 keptstr = "retained";
007e3c0e
WD
679 if (msgtype == FERROR) {
680 errstr = "ERROR";
681 redostr = "";
682 } else {
683 errstr = "WARNING";
684 redostr = " (will try again)";
685 }
686 rprintf(msgtype,
a7260c40 687 "%s: %s failed verification -- update %s%s.\n",
ecc81fce
WD
688 errstr, safe_fname(fname),
689 keptstr, redostr);
007e3c0e 690 }
e2bc4126 691 if (csum_length != SUM_LENGTH) {
3e870a64
WD
692 SIVAL(numbuf, 0, i);
693 send_msg(MSG_REDO, numbuf, 4);
2f03f956
AT
694 }
695 }
696 }
4e834af1 697 make_backups = save_make_backups;
2f03f956 698
48e1c8c6 699 if (delay_updates) {
3e7934a5 700 for (i = -1; (i = next_delayed_bit(i)) >= 0; ) {
48e1c8c6 701 struct file_struct *file = flist->files[i];
48e1c8c6
WD
702 fname = local_name ? local_name : f_name(file);
703 partialptr = partial_dir_fname(fname);
704 if (partialptr) {
705 if (make_backups && !make_backup(fname))
706 continue;
707 if (verbose > 2) {
708 rprintf(FINFO, "renaming %s to %s\n",
71903f60
WD
709 safe_fname(partialptr),
710 safe_fname(fname));
48e1c8c6
WD
711 }
712 if (do_rename(partialptr, fname) < 0) {
713 rsyserr(FERROR, errno,
714 "rename failed for %s (from %s)",
71903f60
WD
715 full_fname(fname),
716 safe_fname(partialptr));
48e1c8c6 717 } else {
cd6becec
WD
718 if (remove_sent_files) {
719 SIVAL(numbuf, 0, i);
720 send_msg(MSG_SUCCESS,numbuf,4);
721 }
48e1c8c6
WD
722 handle_partial_dir(partialptr,
723 PDIR_DELETE);
724 }
725 }
726 }
727 }
728
2430e984 729 if (delete_after && !local_name && flist->count > 0)
3f55bd5d 730 delete_files(flist);
57df171b 731
2f03f956
AT
732 if (verbose > 2)
733 rprintf(FINFO,"recv_files finished\n");
17fadf7d 734
2f03f956
AT
735 return 0;
736}