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