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