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