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