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