Commit | Line | Data |
---|---|---|
e327acec | 1 | /* -*- c-file-style: "linux" -*- |
17fadf7d | 2 | |
e327acec | 3 | Copyright (C) 1996-2000 by Andrew Tridgell |
2f03f956 | 4 | Copyright (C) Paul Mackerras 1996 |
17fadf7d | 5 | |
2f03f956 AT |
6 | This program is free software; you can redistribute it and/or modify |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2 of the License, or | |
9 | (at your option) any later version. | |
17fadf7d | 10 | |
2f03f956 AT |
11 | This program is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
17fadf7d | 15 | |
2f03f956 AT |
16 | You should have received a copy of the GNU General Public License |
17 | along with this program; if not, write to the Free Software | |
18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
19 | */ | |
20 | ||
21 | #include "rsync.h" | |
22 | ||
23 | extern int verbose; | |
24 | extern int recurse; | |
92b9eb97 WD |
25 | extern int delete_after; |
26 | extern int max_delete; | |
2f03f956 AT |
27 | extern int csum_length; |
28 | extern struct stats stats; | |
29 | extern int dry_run; | |
16cc9ca2 WD |
30 | extern int read_batch; |
31 | extern int batch_gen_fd; | |
2f03f956 | 32 | extern int am_server; |
41cfde6b | 33 | extern int protocol_version; |
2f03f956 | 34 | extern int relative_paths; |
566fce32 | 35 | extern int keep_dirlinks; |
2f03f956 | 36 | extern int preserve_hard_links; |
92b9eb97 | 37 | extern int preserve_perms; |
2f03f956 AT |
38 | extern int cvs_exclude; |
39 | extern int io_error; | |
40 | extern char *tmpdir; | |
a7260c40 | 41 | extern char *partial_dir; |
ee297522 | 42 | extern char *basis_dir[]; |
c56595d7 | 43 | extern int basis_dir_cnt; |
53dd3135 | 44 | extern int make_backups; |
16417f8b | 45 | extern int do_progress; |
d74a2e3e | 46 | extern char *backup_dir; |
53dd3135 | 47 | extern char *backup_suffix; |
d74a2e3e | 48 | extern int backup_suffix_len; |
925c517f | 49 | extern int cleanup_got_literal; |
92b9eb97 WD |
50 | extern int module_id; |
51 | extern int ignore_errors; | |
52 | extern int orig_umask; | |
55e50d89 | 53 | extern int keep_partial; |
ba582f75 | 54 | extern int checksum_seed; |
a3221d2a | 55 | extern int inplace; |
2f03f956 | 56 | |
5ebab6c1 WD |
57 | extern struct exclude_list_struct server_exclude_list; |
58 | ||
59 | ||
d74a2e3e | 60 | static void delete_one(char *fn, int is_dir) |
2f03f956 | 61 | { |
d74a2e3e WD |
62 | if (!is_dir) { |
63 | if (robust_unlink(fn) != 0) { | |
d62bcc17 WD |
64 | rsyserr(FERROR, errno, "delete_one: unlink %s failed", |
65 | full_fname(fn)); | |
d2a918b4 | 66 | } else if (verbose) |
ecc81fce | 67 | rprintf(FINFO, "deleting %s\n", safe_fname(fn)); |
17fadf7d | 68 | } else { |
d74a2e3e WD |
69 | if (do_rmdir(fn) != 0) { |
70 | if (errno != ENOTEMPTY && errno != EEXIST) { | |
d62bcc17 WD |
71 | rsyserr(FERROR, errno, |
72 | "delete_one: rmdir %s failed", | |
73 | full_fname(fn)); | |
d74a2e3e | 74 | } |
ecc81fce WD |
75 | } else if (verbose) { |
76 | rprintf(FINFO, "deleting directory %s\n", | |
77 | safe_fname(fn)); | |
78 | } | |
2f03f956 AT |
79 | } |
80 | } | |
81 | ||
82 | ||
d74a2e3e WD |
83 | static int is_backup_file(char *fn) |
84 | { | |
85 | int k = strlen(fn) - backup_suffix_len; | |
86 | return k > 0 && strcmp(fn+k, backup_suffix) == 0; | |
87 | } | |
2f03f956 AT |
88 | |
89 | ||
1e82e2ce WD |
90 | /* This deletes any files on the receiving side that are not present |
91 | * on the sending side. */ | |
6957ae33 | 92 | void delete_files(struct file_list *flist) |
2f03f956 AT |
93 | { |
94 | struct file_list *local_file_list; | |
95 | int i, j; | |
a24639bb | 96 | char *argv[1], fbuf[MAXPATHLEN]; |
0b73ca12 | 97 | static int deletion_count; |
2f03f956 AT |
98 | |
99 | if (cvs_exclude) | |
100 | add_cvs_excludes(); | |
101 | ||
ef55c686 | 102 | if (io_error && !(lp_ignore_errors(module_id) || ignore_errors)) { |
2f03f956 AT |
103 | rprintf(FINFO,"IO error encountered - skipping file deletion\n"); |
104 | return; | |
105 | } | |
106 | ||
1e82e2ce | 107 | for (j = 0; j < flist->count; j++) { |
a5342644 WD |
108 | if (!(flist->files[j]->flags & FLAG_TOP_DIR) |
109 | || !S_ISDIR(flist->files[j]->mode)) | |
110 | continue; | |
2f03f956 | 111 | |
a24639bb | 112 | argv[0] = f_name_to(flist->files[j], fbuf); |
2f03f956 | 113 | |
a24639bb | 114 | if (!(local_file_list = send_file_list(-1, 1, argv))) |
2f03f956 | 115 | continue; |
2f03f956 AT |
116 | |
117 | if (verbose > 1) | |
ecc81fce | 118 | rprintf(FINFO, "deleting in %s\n", safe_fname(fbuf)); |
2f03f956 | 119 | |
446e239e | 120 | for (i = local_file_list->count-1; i >= 0; i--) { |
08b1b486 | 121 | if (max_delete && deletion_count >= max_delete) |
92b9eb97 WD |
122 | break; |
123 | if (!local_file_list->files[i]->basename) | |
124 | continue; | |
125 | if (flist_find(flist,local_file_list->files[i]) < 0) { | |
53dd3135 | 126 | char *f = f_name(local_file_list->files[i]); |
f80a8520 WD |
127 | int mode = local_file_list->files[i]->mode; |
128 | if (make_backups && (backup_dir || !is_backup_file(f)) | |
129 | && !S_ISDIR(mode)) { | |
d2a918b4 | 130 | make_backup(f); |
ecc81fce WD |
131 | if (verbose) { |
132 | rprintf(FINFO, "deleting %s\n", | |
133 | safe_fname(f)); | |
134 | } | |
f80a8520 | 135 | } else |
d74a2e3e | 136 | delete_one(f, S_ISDIR(mode) != 0); |
31f3b68a | 137 | deletion_count++; |
53dd3135 | 138 | } |
2f03f956 AT |
139 | } |
140 | flist_free(local_file_list); | |
2f03f956 AT |
141 | } |
142 | } | |
143 | ||
144 | ||
52d3e106 S |
145 | /* |
146 | * get_tmpname() - create a tmp filename for a given filename | |
147 | * | |
148 | * If a tmpdir is defined, use that as the directory to | |
149 | * put it in. Otherwise, the tmp filename is in the same | |
150 | * directory as the given name. Note that there may be no | |
151 | * directory at all in the given name! | |
17fadf7d | 152 | * |
52d3e106 S |
153 | * The tmp filename is basically the given filename with a |
154 | * dot prepended, and .XXXXXX appended (for mkstemp() to | |
155 | * put its unique gunk in). Take care to not exceed | |
156 | * either the MAXPATHLEN or NAME_MAX, esp. the last, as | |
157 | * the basename basically becomes 8 chars longer. In that | |
158 | * case, the original name is shortened sufficiently to | |
159 | * make it all fit. | |
17fadf7d | 160 | * |
52d3e106 S |
161 | * Of course, there's no real reason for the tmp name to |
162 | * look like the original, except to satisfy us humans. | |
163 | * As long as it's unique, rsync will work. | |
164 | */ | |
165 | ||
2f03f956 AT |
166 | static int get_tmpname(char *fnametmp, char *fname) |
167 | { | |
168 | char *f; | |
52d3e106 S |
169 | int length = 0; |
170 | int maxname; | |
2f03f956 | 171 | |
2f03f956 | 172 | if (tmpdir) { |
a24639bb | 173 | /* Note: this can't overflow, so the return value is safe */ |
b7cee949 | 174 | length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2); |
52d3e106 S |
175 | fnametmp[length++] = '/'; |
176 | fnametmp[length] = '\0'; /* always NULL terminated */ | |
0c2ef5f4 | 177 | } |
52d3e106 | 178 | |
0c2ef5f4 | 179 | if ((f = strrchr(fname, '/')) != NULL) { |
52d3e106 S |
180 | ++f; |
181 | if (!tmpdir) { | |
182 | length = f - fname; | |
0c2ef5f4 | 183 | /* copy up to and including the slash */ |
52d3e106 | 184 | strlcpy(fnametmp, fname, length + 1); |
0c2ef5f4 | 185 | } |
446e239e | 186 | } else |
52d3e106 | 187 | f = fname; |
52d3e106 S |
188 | fnametmp[length++] = '.'; |
189 | fnametmp[length] = '\0'; /* always NULL terminated */ | |
2f03f956 | 190 | |
52d3e106 | 191 | maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8); |
2f03f956 | 192 | |
0c2ef5f4 | 193 | if (maxname < 1) { |
52d3e106 S |
194 | rprintf(FERROR, "temporary filename too long: %s\n", fname); |
195 | fnametmp[0] = '\0'; | |
2f03f956 AT |
196 | return 0; |
197 | } | |
198 | ||
17fadf7d | 199 | strlcpy(fnametmp + length, f, maxname); |
52d3e106 | 200 | strcat(fnametmp + length, ".XXXXXX"); |
2f03f956 AT |
201 | |
202 | return 1; | |
203 | } | |
204 | ||
205 | ||
7e5fa372 WD |
206 | static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r, |
207 | char *fname, int fd, OFF_T total_size) | |
2f03f956 | 208 | { |
7e5fa372 WD |
209 | static char file_sum1[MD4_SUM_LENGTH]; |
210 | static char file_sum2[MD4_SUM_LENGTH]; | |
211 | struct map_struct *mapbuf; | |
fc0257c9 | 212 | struct sum_struct sum; |
6c495e0d | 213 | int32 len; |
2f03f956 | 214 | OFF_T offset = 0; |
89e540e6 | 215 | OFF_T offset2; |
2f03f956 | 216 | char *data; |
6c495e0d | 217 | int32 i; |
e1f67417 | 218 | char *map = NULL; |
17fadf7d | 219 | |
fc0257c9 | 220 | read_sum_head(f_in, &sum); |
17fadf7d | 221 | |
7e5fa372 | 222 | if (fd_r >= 0 && size_r > 0) { |
6c495e0d | 223 | OFF_T map_size = MAX((OFF_T)sum.blength * 2, 16*1024); |
96d910c7 | 224 | mapbuf = map_file(fd_r, size_r, map_size, sum.blength); |
7e5fa372 WD |
225 | if (verbose > 2) { |
226 | rprintf(FINFO, "recv mapped %s of size %.0f\n", | |
ecc81fce | 227 | safe_fname(fname_r), (double)size_r); |
7e5fa372 WD |
228 | } |
229 | } else | |
230 | mapbuf = NULL; | |
231 | ||
ba582f75 | 232 | sum_init(checksum_seed); |
17fadf7d | 233 | |
3f55bd5d | 234 | while ((i = recv_token(f_in, &data)) != 0) { |
16417f8b WD |
235 | if (do_progress) |
236 | show_progress(offset, total_size); | |
2f03f956 AT |
237 | |
238 | if (i > 0) { | |
2f03f956 | 239 | if (verbose > 3) { |
5f808dfb AT |
240 | rprintf(FINFO,"data recv %d at %.0f\n", |
241 | i,(double)offset); | |
2f03f956 AT |
242 | } |
243 | ||
244 | stats.literal_data += i; | |
245 | cleanup_got_literal = 1; | |
17fadf7d | 246 | |
6c495e0d | 247 | sum_update(data, i); |
2f03f956 | 248 | |
c52461f9 WD |
249 | if (fd != -1 && write_file(fd,data,i) != i) |
250 | goto report_write_error; | |
2f03f956 AT |
251 | offset += i; |
252 | continue; | |
17fadf7d | 253 | } |
2f03f956 AT |
254 | |
255 | i = -(i+1); | |
6c495e0d | 256 | offset2 = i * (OFF_T)sum.blength; |
fc0257c9 | 257 | len = sum.blength; |
d2a918b4 | 258 | if (i == (int)sum.count-1 && sum.remainder != 0) |
fc0257c9 | 259 | len = sum.remainder; |
17fadf7d | 260 | |
2f03f956 | 261 | stats.matched_data += len; |
17fadf7d | 262 | |
6c495e0d WD |
263 | if (verbose > 3) { |
264 | rprintf(FINFO, | |
265 | "chunk[%d] of size %ld at %.0f offset=%.0f\n", | |
266 | i, (long)len, (double)offset2, (double)offset); | |
267 | } | |
17fadf7d | 268 | |
446e239e WD |
269 | if (mapbuf) { |
270 | map = map_ptr(mapbuf,offset2,len); | |
17fadf7d | 271 | |
c55f7021 | 272 | see_token(map, len); |
6c495e0d | 273 | sum_update(map, len); |
c55f7021 | 274 | } |
17fadf7d | 275 | |
fab65a5b | 276 | if (inplace) { |
89e540e6 | 277 | if (offset == offset2 && fd != -1) { |
c52461f9 WD |
278 | if (flush_write_file(fd) < 0) |
279 | goto report_write_error; | |
89e540e6 WD |
280 | offset += len; |
281 | if (do_lseek(fd, len, SEEK_CUR) != offset) { | |
fab65a5b WD |
282 | rsyserr(FERROR, errno, |
283 | "lseek failed on %s", | |
284 | full_fname(fname)); | |
285 | exit_cleanup(RERR_FILEIO); | |
286 | } | |
89e540e6 | 287 | continue; |
a3221d2a | 288 | } |
2f03f956 | 289 | } |
c52461f9 WD |
290 | if (fd != -1 && write_file(fd, map, len) != (int)len) |
291 | goto report_write_error; | |
2f03f956 AT |
292 | offset += len; |
293 | } | |
294 | ||
85f14172 WD |
295 | if (flush_write_file(fd) < 0) |
296 | goto report_write_error; | |
76c21947 | 297 | |
a3221d2a | 298 | #ifdef HAVE_FTRUNCATE |
fab65a5b | 299 | if (inplace && fd != -1) |
a3221d2a WD |
300 | ftruncate(fd, offset); |
301 | #endif | |
302 | ||
16417f8b WD |
303 | if (do_progress) |
304 | end_progress(total_size); | |
2f03f956 AT |
305 | |
306 | if (fd != -1 && offset > 0 && sparse_end(fd) != 0) { | |
c52461f9 | 307 | report_write_error: |
d62bcc17 WD |
308 | rsyserr(FERROR, errno, "write failed on %s", |
309 | full_fname(fname)); | |
65417579 | 310 | exit_cleanup(RERR_FILEIO); |
2f03f956 AT |
311 | } |
312 | ||
313 | sum_end(file_sum1); | |
314 | ||
7e5fa372 WD |
315 | if (mapbuf) |
316 | unmap_file(mapbuf); | |
317 | ||
bc63ae3f | 318 | read_buf(f_in,file_sum2,MD4_SUM_LENGTH); |
d2a918b4 | 319 | if (verbose > 2) |
bc63ae3f | 320 | rprintf(FINFO,"got file_sum\n"); |
d2a918b4 | 321 | if (fd != -1 && memcmp(file_sum1, file_sum2, MD4_SUM_LENGTH) != 0) |
bc63ae3f | 322 | return 0; |
2f03f956 AT |
323 | return 1; |
324 | } | |
325 | ||
326 | ||
8ed9d849 WD |
327 | static void discard_receive_data(int f_in, OFF_T length) |
328 | { | |
7e5fa372 | 329 | receive_data(f_in, NULL, -1, 0, NULL, -1, length); |
8ed9d849 WD |
330 | } |
331 | ||
332 | ||
b35d0d8e MP |
333 | /** |
334 | * main routine for receiver process. | |
335 | * | |
336 | * Receiver process runs on the same host as the generator process. */ | |
41cfde6b WD |
337 | int recv_files(int f_in, struct file_list *flist, char *local_name, |
338 | int f_in_name) | |
17fadf7d | 339 | { |
16cc9ca2 | 340 | int next_gen_i = -1; |
2f03f956 AT |
341 | int fd1,fd2; |
342 | STRUCT_STAT st; | |
446e239e | 343 | char *fname, fbuf[MAXPATHLEN]; |
f62c17e3 | 344 | char template[MAXPATHLEN]; |
2f03f956 | 345 | char fnametmp[MAXPATHLEN]; |
a7260c40 | 346 | char *fnamecmp, *partialptr; |
375a4556 | 347 | char fnamecmpbuf[MAXPATHLEN]; |
2f03f956 | 348 | struct file_struct *file; |
1b7c47cb | 349 | struct stats initial_stats; |
4e834af1 WD |
350 | int save_make_backups = make_backups; |
351 | int i, recv_ok, phase = 0; | |
11a5a3c7 | 352 | |
d2a918b4 | 353 | if (verbose > 2) |
2f03f956 | 354 | rprintf(FINFO,"recv_files(%d) starting\n",flist->count); |
2f03f956 | 355 | |
cb869c26 | 356 | if (flist->hlink_pool) { |
9935066b S |
357 | pool_destroy(flist->hlink_pool); |
358 | flist->hlink_pool = NULL; | |
359 | } | |
360 | ||
17fadf7d | 361 | while (1) { |
2f03f956 AT |
362 | cleanup_disable(); |
363 | ||
364 | i = read_int(f_in); | |
365 | if (i == -1) { | |
16cc9ca2 | 366 | if (read_batch) { |
41cfde6b WD |
367 | if (next_gen_i != flist->count) { |
368 | do { | |
369 | if (f_in_name >= 0 | |
370 | && next_gen_i >= 0) | |
371 | read_byte(f_in_name); | |
372 | } while (read_int(batch_gen_fd) != -1); | |
373 | } | |
16cc9ca2 WD |
374 | next_gen_i = -1; |
375 | } | |
376 | ||
4e834af1 WD |
377 | if (phase) |
378 | break; | |
5ebab6c1 | 379 | |
4e834af1 WD |
380 | phase = 1; |
381 | csum_length = SUM_LENGTH; | |
382 | if (verbose > 2) | |
383 | rprintf(FINFO, "recv_files phase=%d\n", phase); | |
384 | send_msg(MSG_DONE, "", 0); | |
aec6b9f8 | 385 | if (keep_partial && !partial_dir) |
4e834af1 WD |
386 | make_backups = 0; /* prevents double backup */ |
387 | continue; | |
2f03f956 AT |
388 | } |
389 | ||
390 | if (i < 0 || i >= flist->count) { | |
17fadf7d | 391 | rprintf(FERROR,"Invalid file index %d in recv_files (count=%d)\n", |
2f03f956 | 392 | i, flist->count); |
65417579 | 393 | exit_cleanup(RERR_PROTOCOL); |
2f03f956 AT |
394 | } |
395 | ||
396 | file = flist->files[i]; | |
2f03f956 | 397 | |
97feb557 | 398 | stats.current_file_index = i; |
2f03f956 AT |
399 | stats.num_transferred_files++; |
400 | stats.total_transferred_size += file->length; | |
925c517f | 401 | cleanup_got_literal = 0; |
2f03f956 AT |
402 | |
403 | if (local_name) | |
404 | fname = local_name; | |
446e239e | 405 | else |
3fef5364 | 406 | fname = f_name_to(file, fbuf); |
2f03f956 AT |
407 | |
408 | if (dry_run) { | |
ecc81fce WD |
409 | if (!am_server && verbose) /* log the transfer */ |
410 | rprintf(FINFO, "%s\n", safe_fname(fname)); | |
2f03f956 AT |
411 | continue; |
412 | } | |
413 | ||
1b7c47cb AT |
414 | initial_stats = stats; |
415 | ||
2f03f956 | 416 | if (verbose > 2) |
ecc81fce | 417 | rprintf(FINFO, "recv_files(%s)\n", safe_fname(fname)); |
2f03f956 | 418 | |
16cc9ca2 WD |
419 | if (read_batch) { |
420 | while (i > next_gen_i) { | |
41cfde6b WD |
421 | if (f_in_name >= 0 && next_gen_i >= 0) |
422 | read_byte(f_in_name); | |
16cc9ca2 WD |
423 | next_gen_i = read_int(batch_gen_fd); |
424 | if (next_gen_i == -1) | |
425 | next_gen_i = flist->count; | |
426 | } | |
427 | if (i < next_gen_i) { | |
428 | rprintf(FINFO, "skipping update for \"%s\"\n", | |
ecc81fce | 429 | safe_fname(fname)); |
16cc9ca2 WD |
430 | discard_receive_data(f_in, file->length); |
431 | continue; | |
432 | } | |
41cfde6b | 433 | next_gen_i = -1; |
16cc9ca2 WD |
434 | } |
435 | ||
5ebab6c1 WD |
436 | if (server_exclude_list.head |
437 | && check_exclude(&server_exclude_list, fname, | |
438 | S_ISDIR(file->mode)) < 0) { | |
33eff8bf WD |
439 | rprintf(FERROR, "attempt to hack rsync failed.\n"); |
440 | exit_cleanup(RERR_PROTOCOL); | |
5ebab6c1 WD |
441 | } |
442 | ||
41cfde6b WD |
443 | partialptr = partial_dir ? partial_dir_fname(fname) : fname; |
444 | ||
445 | if (f_in_name >= 0) { | |
ee297522 WD |
446 | uchar j; |
447 | switch (j = read_byte(f_in_name)) { | |
41cfde6b | 448 | case FNAMECMP_FNAME: |
a7260c40 | 449 | fnamecmp = fname; |
41cfde6b WD |
450 | break; |
451 | case FNAMECMP_PARTIAL_DIR: | |
452 | fnamecmp = partialptr ? partialptr : fname; | |
453 | break; | |
454 | case FNAMECMP_BACKUP: | |
455 | fnamecmp = get_backup_name(fname); | |
456 | break; | |
41cfde6b | 457 | default: |
c56595d7 WD |
458 | if (j >= basis_dir_cnt) { |
459 | rprintf(FERROR, | |
460 | "invalid basis_dir index: %d.\n", | |
461 | j); | |
462 | exit_cleanup(RERR_PROTOCOL); | |
463 | } | |
41cfde6b | 464 | pathjoin(fnamecmpbuf, sizeof fnamecmpbuf, |
ee297522 | 465 | basis_dir[j], fname); |
41cfde6b WD |
466 | fnamecmp = fnamecmpbuf; |
467 | break; | |
468 | } | |
a7260c40 | 469 | } else |
41cfde6b | 470 | fnamecmp = fname; |
dc55d7bd | 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 | |
ecc81fce WD |
558 | if (!am_server && verbose) /* log the transfer */ |
559 | rprintf(FINFO, "%s\n", safe_fname(fname)); | |
11a5a3c7 | 560 | |
2f03f956 | 561 | /* recv file data */ |
7e5fa372 WD |
562 | recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size, |
563 | fname, fd2, file->length); | |
1b7c47cb AT |
564 | |
565 | log_recv(file, &initial_stats); | |
17fadf7d | 566 | |
d2a918b4 | 567 | if (fd1 != -1) |
2f03f956 | 568 | close(fd1); |
9f27cd8c | 569 | if (close(fd2) < 0) { |
d62bcc17 WD |
570 | rsyserr(FERROR, errno, "close failed on %s", |
571 | full_fname(fnametmp)); | |
9f27cd8c WD |
572 | exit_cleanup(RERR_FILEIO); |
573 | } | |
17fadf7d | 574 | |
aec6b9f8 WD |
575 | if (recv_ok || inplace) { |
576 | finish_transfer(fname, fnametmp, file, recv_ok, 1); | |
577 | if (partialptr != fname && fnamecmp == partialptr) { | |
578 | do_unlink(partialptr); | |
579 | handle_partial_dir(partialptr, PDIR_DELETE); | |
580 | } | |
581 | } else if (keep_partial && partialptr | |
582 | && handle_partial_dir(partialptr, PDIR_CREATE)) { | |
583 | finish_transfer(partialptr, fnametmp, file, recv_ok, | |
584 | !partial_dir); | |
585 | } else { | |
a7260c40 | 586 | partialptr = NULL; |
55e50d89 | 587 | do_unlink(fnametmp); |
a7260c40 WD |
588 | } |
589 | ||
2f03f956 | 590 | cleanup_disable(); |
554e0a8d | 591 | |
2f03f956 | 592 | if (!recv_ok) { |
007e3c0e WD |
593 | int msgtype = csum_length == SUM_LENGTH || read_batch ? |
594 | FERROR : FINFO; | |
595 | if (msgtype == FERROR || verbose) { | |
a7260c40 WD |
596 | char *errstr, *redostr, *keptstr; |
597 | if (!(keep_partial && partialptr) && !inplace) | |
598 | keptstr = "discarded"; | |
599 | else if (partial_dir) | |
600 | keptstr = "put into partial-dir"; | |
601 | else | |
602 | keptstr = "retained"; | |
007e3c0e WD |
603 | if (msgtype == FERROR) { |
604 | errstr = "ERROR"; | |
605 | redostr = ""; | |
606 | } else { | |
607 | errstr = "WARNING"; | |
608 | redostr = " (will try again)"; | |
609 | } | |
610 | rprintf(msgtype, | |
a7260c40 | 611 | "%s: %s failed verification -- update %s%s.\n", |
ecc81fce WD |
612 | errstr, safe_fname(fname), |
613 | keptstr, redostr); | |
007e3c0e | 614 | } |
e2bc4126 | 615 | if (csum_length != SUM_LENGTH) { |
0569a133 | 616 | char buf[4]; |
0569a133 WD |
617 | SIVAL(buf, 0, i); |
618 | send_msg(MSG_REDO, buf, 4); | |
2f03f956 AT |
619 | } |
620 | } | |
621 | } | |
4e834af1 | 622 | make_backups = save_make_backups; |
2f03f956 | 623 | |
e76ca145 | 624 | if (delete_after && recurse && !local_name && flist->count > 0) |
3f55bd5d | 625 | delete_files(flist); |
57df171b | 626 | |
2f03f956 AT |
627 | if (verbose > 2) |
628 | rprintf(FINFO,"recv_files finished\n"); | |
17fadf7d | 629 | |
2f03f956 AT |
630 | return 0; |
631 | } |