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