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