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