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