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