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