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