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