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