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