Commit | Line | Data |
---|---|---|
0f78b815 WD |
1 | /* |
2 | * Routines only used by the receiving process. | |
3 | * | |
4 | * Copyright (C) 1996-2000 Andrew Tridgell | |
5 | * Copyright (C) 1996 Paul Mackerras | |
ba2133d6 | 6 | * Copyright (C) 2003-2007 Wayne Davison |
0f78b815 WD |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify | |
ba2133d6 WD |
9 | * it under the terms of the GNU General Public License version 2 as |
10 | * published by the Free Software Foundation. | |
0f78b815 WD |
11 | * |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
e7c67065 WD |
17 | * You should have received a copy of the GNU General Public License along |
18 | * with this program; if not, write to the Free Software Foundation, Inc., | |
19 | * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. | |
0f78b815 | 20 | */ |
2f03f956 AT |
21 | |
22 | #include "rsync.h" | |
23 | ||
24 | extern int verbose; | |
a0009fc3 | 25 | extern int do_xfers; |
8715db2c WD |
26 | extern int am_server; |
27 | extern int do_progress; | |
3ea6e0e7 | 28 | extern int inc_recurse; |
910e89b9 | 29 | extern int log_before_transfer; |
2fedf3d5 | 30 | extern int stdout_format_has_i; |
ecc7623e | 31 | extern int logfile_format_has_i; |
2f03f956 | 32 | extern int csum_length; |
16cc9ca2 | 33 | extern int read_batch; |
a0009fc3 | 34 | extern int write_batch; |
16cc9ca2 | 35 | extern int batch_gen_fd; |
41cfde6b | 36 | extern int protocol_version; |
2f03f956 | 37 | extern int relative_paths; |
744e63fb | 38 | extern int preserve_hard_links; |
92b9eb97 | 39 | extern int preserve_perms; |
c56595d7 | 40 | extern int basis_dir_cnt; |
53dd3135 | 41 | extern int make_backups; |
925c517f | 42 | extern int cleanup_got_literal; |
744e63fb | 43 | extern int remove_source_files; |
6cc11982 | 44 | extern int append_mode; |
e7ee91de | 45 | extern int sparse_files; |
55e50d89 | 46 | extern int keep_partial; |
ba582f75 | 47 | extern int checksum_seed; |
a3221d2a | 48 | extern int inplace; |
48e1c8c6 | 49 | extern int delay_updates; |
8715db2c | 50 | extern struct stats stats; |
8715db2c WD |
51 | extern char *tmpdir; |
52 | extern char *partial_dir; | |
53 | extern char *basis_dir[]; | |
f3d6d480 | 54 | extern struct file_list *cur_flist, *first_flist; |
7842418b | 55 | extern struct filter_list_struct server_filter_list; |
5ebab6c1 | 56 | |
10f994a5 | 57 | static struct bitbag *delayed_bits = NULL; |
d0221f1d | 58 | static int phase = 0, redoing = 0; |
48459ba1 WD |
59 | /* We're either updating the basis file or an identical copy: */ |
60 | static int updating_basis; | |
3e7934a5 | 61 | |
52d3e106 S |
62 | /* |
63 | * get_tmpname() - create a tmp filename for a given filename | |
64 | * | |
65 | * If a tmpdir is defined, use that as the directory to | |
66 | * put it in. Otherwise, the tmp filename is in the same | |
67 | * directory as the given name. Note that there may be no | |
68 | * directory at all in the given name! | |
17fadf7d | 69 | * |
52d3e106 S |
70 | * The tmp filename is basically the given filename with a |
71 | * dot prepended, and .XXXXXX appended (for mkstemp() to | |
72 | * put its unique gunk in). Take care to not exceed | |
73 | * either the MAXPATHLEN or NAME_MAX, esp. the last, as | |
74 | * the basename basically becomes 8 chars longer. In that | |
75 | * case, the original name is shortened sufficiently to | |
76 | * make it all fit. | |
17fadf7d | 77 | * |
52d3e106 S |
78 | * Of course, there's no real reason for the tmp name to |
79 | * look like the original, except to satisfy us humans. | |
80 | * As long as it's unique, rsync will work. | |
81 | */ | |
82 | ||
b20830b3 | 83 | int get_tmpname(char *fnametmp, char *fname) |
2f03f956 | 84 | { |
d5dcb6f7 | 85 | int maxname, added, length = 0; |
2f03f956 AT |
86 | char *f; |
87 | ||
2f03f956 | 88 | if (tmpdir) { |
a24639bb | 89 | /* Note: this can't overflow, so the return value is safe */ |
b7cee949 | 90 | length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2); |
52d3e106 | 91 | fnametmp[length++] = '/'; |
0c2ef5f4 | 92 | } |
52d3e106 | 93 | |
0c2ef5f4 | 94 | if ((f = strrchr(fname, '/')) != NULL) { |
52d3e106 S |
95 | ++f; |
96 | if (!tmpdir) { | |
97 | length = f - fname; | |
0c2ef5f4 | 98 | /* copy up to and including the slash */ |
52d3e106 | 99 | strlcpy(fnametmp, fname, length + 1); |
0c2ef5f4 | 100 | } |
446e239e | 101 | } else |
52d3e106 | 102 | f = fname; |
52d3e106 | 103 | fnametmp[length++] = '.'; |
2f03f956 | 104 | |
d5dcb6f7 WD |
105 | /* The maxname value is bufsize, and includes space for the '\0'. |
106 | * (Note that NAME_MAX get -8 for the leading '.' above.) */ | |
52d3e106 | 107 | maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8); |
2f03f956 | 108 | |
0c2ef5f4 | 109 | if (maxname < 1) { |
45c49b52 | 110 | rprintf(FERROR, "temporary filename too long: %s\n", fname); |
52d3e106 | 111 | fnametmp[0] = '\0'; |
2f03f956 AT |
112 | return 0; |
113 | } | |
114 | ||
d5dcb6f7 WD |
115 | added = strlcpy(fnametmp + length, f, maxname); |
116 | if (added >= maxname) | |
117 | added = maxname - 1; | |
118 | memcpy(fnametmp + length + added, ".XXXXXX", 8); | |
2f03f956 AT |
119 | |
120 | return 1; | |
121 | } | |
122 | ||
123 | ||
7e5fa372 WD |
124 | static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r, |
125 | char *fname, int fd, OFF_T total_size) | |
2f03f956 | 126 | { |
7e5fa372 WD |
127 | static char file_sum1[MD4_SUM_LENGTH]; |
128 | static char file_sum2[MD4_SUM_LENGTH]; | |
129 | struct map_struct *mapbuf; | |
fc0257c9 | 130 | struct sum_struct sum; |
6c495e0d | 131 | int32 len; |
2f03f956 | 132 | OFF_T offset = 0; |
89e540e6 | 133 | OFF_T offset2; |
2f03f956 | 134 | char *data; |
6c495e0d | 135 | int32 i; |
e1f67417 | 136 | char *map = NULL; |
17fadf7d | 137 | |
fc0257c9 | 138 | read_sum_head(f_in, &sum); |
17fadf7d | 139 | |
7e5fa372 | 140 | if (fd_r >= 0 && size_r > 0) { |
80264051 WD |
141 | int32 read_size = MAX(sum.blength * 2, 16*1024); |
142 | mapbuf = map_file(fd_r, size_r, read_size, sum.blength); | |
7e5fa372 WD |
143 | if (verbose > 2) { |
144 | rprintf(FINFO, "recv mapped %s of size %.0f\n", | |
45c49b52 | 145 | fname_r, (double)size_r); |
7e5fa372 WD |
146 | } |
147 | } else | |
148 | mapbuf = NULL; | |
149 | ||
ba582f75 | 150 | sum_init(checksum_seed); |
17fadf7d | 151 | |
f3d6d480 | 152 | if (append_mode > 0) { |
6cc11982 WD |
153 | OFF_T j; |
154 | sum.flength = (OFF_T)sum.count * sum.blength; | |
155 | if (sum.remainder) | |
156 | sum.flength -= sum.blength - sum.remainder; | |
157 | for (j = CHUNK_SIZE; j < sum.flength; j += CHUNK_SIZE) { | |
97894c64 WD |
158 | if (do_progress) |
159 | show_progress(offset, total_size); | |
6cc11982 WD |
160 | sum_update(map_ptr(mapbuf, offset, CHUNK_SIZE), |
161 | CHUNK_SIZE); | |
162 | offset = j; | |
163 | } | |
164 | if (offset < sum.flength) { | |
53593085 | 165 | int32 len = (int32)(sum.flength - offset); |
97894c64 WD |
166 | if (do_progress) |
167 | show_progress(offset, total_size); | |
6cc11982 WD |
168 | sum_update(map_ptr(mapbuf, offset, len), len); |
169 | offset = sum.flength; | |
170 | } | |
ffa8ab8e WD |
171 | if (fd != -1 && (j = do_lseek(fd, offset, SEEK_SET)) != offset) { |
172 | rsyserr(FERROR, errno, "lseek of %s returned %.0f, not %.0f", | |
173 | full_fname(fname), (double)j, (double)offset); | |
6cc11982 WD |
174 | exit_cleanup(RERR_FILEIO); |
175 | } | |
176 | } | |
177 | ||
3f55bd5d | 178 | while ((i = recv_token(f_in, &data)) != 0) { |
16417f8b WD |
179 | if (do_progress) |
180 | show_progress(offset, total_size); | |
2f03f956 AT |
181 | |
182 | if (i > 0) { | |
2f03f956 | 183 | if (verbose > 3) { |
5f808dfb AT |
184 | rprintf(FINFO,"data recv %d at %.0f\n", |
185 | i,(double)offset); | |
2f03f956 AT |
186 | } |
187 | ||
188 | stats.literal_data += i; | |
189 | cleanup_got_literal = 1; | |
17fadf7d | 190 | |
6c495e0d | 191 | sum_update(data, i); |
2f03f956 | 192 | |
c52461f9 WD |
193 | if (fd != -1 && write_file(fd,data,i) != i) |
194 | goto report_write_error; | |
2f03f956 AT |
195 | offset += i; |
196 | continue; | |
17fadf7d | 197 | } |
2f03f956 AT |
198 | |
199 | i = -(i+1); | |
6c495e0d | 200 | offset2 = i * (OFF_T)sum.blength; |
fc0257c9 | 201 | len = sum.blength; |
d2a918b4 | 202 | if (i == (int)sum.count-1 && sum.remainder != 0) |
fc0257c9 | 203 | len = sum.remainder; |
17fadf7d | 204 | |
2f03f956 | 205 | stats.matched_data += len; |
17fadf7d | 206 | |
6c495e0d WD |
207 | if (verbose > 3) { |
208 | rprintf(FINFO, | |
209 | "chunk[%d] of size %ld at %.0f offset=%.0f\n", | |
210 | i, (long)len, (double)offset2, (double)offset); | |
211 | } | |
17fadf7d | 212 | |
446e239e WD |
213 | if (mapbuf) { |
214 | map = map_ptr(mapbuf,offset2,len); | |
17fadf7d | 215 | |
c55f7021 | 216 | see_token(map, len); |
6c495e0d | 217 | sum_update(map, len); |
c55f7021 | 218 | } |
17fadf7d | 219 | |
48459ba1 | 220 | if (updating_basis) { |
89e540e6 | 221 | if (offset == offset2 && fd != -1) { |
ffa8ab8e | 222 | OFF_T pos; |
c52461f9 WD |
223 | if (flush_write_file(fd) < 0) |
224 | goto report_write_error; | |
89e540e6 | 225 | offset += len; |
ffa8ab8e | 226 | if ((pos = do_lseek(fd, len, SEEK_CUR)) != offset) { |
fab65a5b | 227 | rsyserr(FERROR, errno, |
ffa8ab8e WD |
228 | "lseek of %s returned %.0f, not %.0f", |
229 | full_fname(fname), | |
230 | (double)pos, (double)offset); | |
fab65a5b WD |
231 | exit_cleanup(RERR_FILEIO); |
232 | } | |
89e540e6 | 233 | continue; |
a3221d2a | 234 | } |
2f03f956 | 235 | } |
1ed91a04 | 236 | if (fd != -1 && map && write_file(fd, map, len) != (int)len) |
c52461f9 | 237 | goto report_write_error; |
2f03f956 AT |
238 | offset += len; |
239 | } | |
240 | ||
85f14172 WD |
241 | if (flush_write_file(fd) < 0) |
242 | goto report_write_error; | |
76c21947 | 243 | |
4f5b0756 | 244 | #ifdef HAVE_FTRUNCATE |
fab65a5b | 245 | if (inplace && fd != -1) |
a3221d2a WD |
246 | ftruncate(fd, offset); |
247 | #endif | |
248 | ||
16417f8b WD |
249 | if (do_progress) |
250 | end_progress(total_size); | |
2f03f956 AT |
251 | |
252 | if (fd != -1 && offset > 0 && sparse_end(fd) != 0) { | |
c52461f9 | 253 | report_write_error: |
d62bcc17 WD |
254 | rsyserr(FERROR, errno, "write failed on %s", |
255 | full_fname(fname)); | |
65417579 | 256 | exit_cleanup(RERR_FILEIO); |
2f03f956 AT |
257 | } |
258 | ||
259 | sum_end(file_sum1); | |
260 | ||
7e5fa372 WD |
261 | if (mapbuf) |
262 | unmap_file(mapbuf); | |
263 | ||
bc63ae3f | 264 | read_buf(f_in,file_sum2,MD4_SUM_LENGTH); |
d2a918b4 | 265 | if (verbose > 2) |
bc63ae3f | 266 | rprintf(FINFO,"got file_sum\n"); |
d2a918b4 | 267 | if (fd != -1 && memcmp(file_sum1, file_sum2, MD4_SUM_LENGTH) != 0) |
bc63ae3f | 268 | return 0; |
2f03f956 AT |
269 | return 1; |
270 | } | |
271 | ||
272 | ||
8ed9d849 WD |
273 | static void discard_receive_data(int f_in, OFF_T length) |
274 | { | |
7e5fa372 | 275 | receive_data(f_in, NULL, -1, 0, NULL, -1, length); |
8ed9d849 WD |
276 | } |
277 | ||
f3d6d480 | 278 | static void handle_delayed_updates(char *local_name) |
42be5320 | 279 | { |
663b2857 | 280 | char *fname, *partialptr; |
0395130c | 281 | int ndx; |
42be5320 | 282 | |
0395130c | 283 | for (ndx = -1; (ndx = bitbag_next_bit(delayed_bits, ndx)) >= 0; ) { |
f3d6d480 | 284 | struct file_struct *file = cur_flist->files[ndx]; |
6cbde57d | 285 | fname = local_name ? local_name : f_name(file, NULL); |
42be5320 | 286 | if ((partialptr = partial_dir_fname(fname)) != NULL) { |
f3d6d480 | 287 | if (make_backups > 0 && !make_backup(fname)) |
42be5320 WD |
288 | continue; |
289 | if (verbose > 2) { | |
290 | rprintf(FINFO, "renaming %s to %s\n", | |
45c49b52 | 291 | partialptr, fname); |
42be5320 | 292 | } |
6a94c58b WD |
293 | /* We don't use robust_rename() here because the |
294 | * partial-dir must be on the same drive. */ | |
42be5320 WD |
295 | if (do_rename(partialptr, fname) < 0) { |
296 | rsyserr(FERROR, errno, | |
297 | "rename failed for %s (from %s)", | |
45c49b52 | 298 | full_fname(fname), partialptr); |
42be5320 | 299 | } else { |
47c11975 | 300 | if (remove_source_files |
112d728f | 301 | || (preserve_hard_links && F_IS_HLINKED(file))) |
0395130c | 302 | send_msg_int(MSG_SUCCESS, ndx); |
45c49b52 | 303 | handle_partial_dir(partialptr, PDIR_DELETE); |
42be5320 WD |
304 | } |
305 | } | |
306 | } | |
307 | } | |
308 | ||
f3d6d480 | 309 | static int get_next_gen_ndx(int fd, int next_gen_ndx, int desired_ndx) |
154cdaaa | 310 | { |
0395130c WD |
311 | while (next_gen_ndx < desired_ndx) { |
312 | if (next_gen_ndx >= 0) { | |
154cdaaa | 313 | rprintf(FINFO, |
1ed91a04 | 314 | "(No batched update for%s \"%s\")\n", |
d0221f1d | 315 | redoing ? " resend of" : "", |
f3d6d480 WD |
316 | f_name(cur_flist->files[next_gen_ndx], NULL)); |
317 | } | |
318 | next_gen_ndx = read_int(fd); | |
319 | if (next_gen_ndx == -1) { | |
3ea6e0e7 | 320 | if (inc_recurse) |
f3d6d480 WD |
321 | next_gen_ndx = first_flist->prev->count + first_flist->prev->ndx_start; |
322 | else | |
323 | next_gen_ndx = cur_flist->count; | |
154cdaaa | 324 | } |
154cdaaa | 325 | } |
0395130c | 326 | return next_gen_ndx; |
154cdaaa WD |
327 | } |
328 | ||
b35d0d8e MP |
329 | /** |
330 | * main routine for receiver process. | |
331 | * | |
332 | * Receiver process runs on the same host as the generator process. */ | |
f3d6d480 | 333 | int recv_files(int f_in, char *local_name) |
17fadf7d | 334 | { |
0395130c | 335 | int next_gen_ndx = -1; |
2f03f956 AT |
336 | int fd1,fd2; |
337 | STRUCT_STAT st; | |
b9232d45 | 338 | int iflags, xlen; |
446e239e | 339 | char *fname, fbuf[MAXPATHLEN]; |
b9232d45 | 340 | char xname[MAXPATHLEN]; |
2f03f956 | 341 | char fnametmp[MAXPATHLEN]; |
663b2857 | 342 | char *fnamecmp, *partialptr; |
375a4556 | 343 | char fnamecmpbuf[MAXPATHLEN]; |
9e4a8d29 | 344 | uchar fnamecmp_type; |
2f03f956 | 345 | struct file_struct *file; |
1b7c47cb | 346 | struct stats initial_stats; |
2fedf3d5 WD |
347 | int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i; |
348 | enum logcode log_code = log_before_transfer ? FLOG : FINFO; | |
1ed91a04 | 349 | int max_phase = protocol_version >= 29 ? 2 : 1; |
0395130c | 350 | int ndx, recv_ok; |
11a5a3c7 | 351 | |
d2a918b4 | 352 | if (verbose > 2) |
f3d6d480 | 353 | rprintf(FINFO, "recv_files(%d) starting\n", cur_flist->count); |
2f03f956 | 354 | |
3e7934a5 | 355 | if (delay_updates) |
f3d6d480 | 356 | delayed_bits = bitbag_create(cur_flist->count + 1); |
48e1c8c6 | 357 | |
48459ba1 WD |
358 | updating_basis = inplace; |
359 | ||
17fadf7d | 360 | while (1) { |
2f03f956 AT |
361 | cleanup_disable(); |
362 | ||
f3d6d480 WD |
363 | /* This call also sets cur_flist. */ |
364 | ndx = read_ndx_and_attrs(f_in, -1, &iflags, | |
365 | &fnamecmp_type, xname, &xlen); | |
0395130c | 366 | if (ndx == NDX_DONE) { |
3ea6e0e7 | 367 | if (inc_recurse && first_flist) { |
f3d6d480 WD |
368 | flist_free(first_flist); |
369 | if (first_flist) | |
370 | continue; | |
371 | } | |
372 | if (read_batch && cur_flist) { | |
3ea6e0e7 | 373 | int high = inc_recurse |
f3d6d480 WD |
374 | ? first_flist->prev->count + first_flist->prev->ndx_start |
375 | : cur_flist->count; | |
376 | get_next_gen_ndx(batch_gen_fd, next_gen_ndx, high); | |
0395130c | 377 | next_gen_ndx = -1; |
16cc9ca2 | 378 | } |
ac3f7b81 | 379 | if (++phase > max_phase) |
4e834af1 | 380 | break; |
4e834af1 WD |
381 | if (verbose > 2) |
382 | rprintf(FINFO, "recv_files phase=%d\n", phase); | |
ac3f7b81 | 383 | if (phase == 2 && delay_updates) |
f3d6d480 | 384 | handle_delayed_updates(local_name); |
4e834af1 | 385 | send_msg(MSG_DONE, "", 0); |
4e834af1 | 386 | continue; |
2f03f956 AT |
387 | } |
388 | ||
f3d6d480 | 389 | file = cur_flist->files[ndx - cur_flist->ndx_start]; |
6cbde57d | 390 | fname = local_name ? local_name : f_name(file, fbuf); |
227a9c41 WD |
391 | |
392 | if (verbose > 2) | |
45c49b52 | 393 | rprintf(FINFO, "recv_files(%s)\n", fname); |
910e89b9 | 394 | |
22907b6b | 395 | if (!(iflags & ITEM_TRANSFER)) { |
b9232d45 | 396 | maybe_log_item(file, iflags, itemizing, xname); |
58a14ed9 WD |
397 | continue; |
398 | } | |
ac3f7b81 WD |
399 | if (phase == 2) { |
400 | rprintf(FERROR, | |
401 | "got transfer request in phase 2 [%s]\n", | |
402 | who_am_i()); | |
403 | exit_cleanup(RERR_PROTOCOL); | |
404 | } | |
910e89b9 | 405 | |
f3d6d480 WD |
406 | if (file->flags & FLAG_FILE_SENT) { |
407 | if (csum_length == SHORT_SUM_LENGTH) { | |
408 | if (keep_partial && !partial_dir) | |
409 | make_backups = -make_backups; /* prevents double backup */ | |
410 | append_mode = -append_mode; | |
411 | sparse_files = -sparse_files; | |
412 | csum_length = SUM_LENGTH; | |
d0221f1d | 413 | redoing = 1; |
f3d6d480 WD |
414 | } |
415 | } else { | |
416 | if (csum_length != SHORT_SUM_LENGTH) { | |
417 | if (keep_partial && !partial_dir) | |
418 | make_backups = -make_backups; | |
419 | append_mode = -append_mode; | |
420 | sparse_files = -sparse_files; | |
421 | csum_length = SHORT_SUM_LENGTH; | |
d0221f1d | 422 | redoing = 0; |
f3d6d480 WD |
423 | } |
424 | } | |
425 | ||
0395130c | 426 | stats.current_file_index = ndx; |
2f03f956 | 427 | stats.num_transferred_files++; |
112d728f | 428 | stats.total_transferred_size += F_LENGTH(file); |
925c517f | 429 | cleanup_got_literal = 0; |
2f03f956 | 430 | |
8c2ffaf0 WD |
431 | if (server_filter_list.head |
432 | && check_filter(&server_filter_list, fname, 0) < 0) { | |
433 | rprintf(FERROR, "attempt to hack rsync failed.\n"); | |
434 | exit_cleanup(RERR_PROTOCOL); | |
435 | } | |
436 | ||
a0009fc3 | 437 | if (!do_xfers) { /* log the transfer */ |
85909931 | 438 | log_item(FCLIENT, file, &stats, iflags, NULL); |
f957e8fd | 439 | if (read_batch) |
112d728f | 440 | discard_receive_data(f_in, F_LENGTH(file)); |
2f03f956 AT |
441 | continue; |
442 | } | |
a0009fc3 | 443 | if (write_batch < 0) { |
2fedf3d5 | 444 | log_item(FINFO, file, &stats, iflags, NULL); |
b10917a4 | 445 | if (!am_server) |
112d728f | 446 | discard_receive_data(f_in, F_LENGTH(file)); |
a0009fc3 WD |
447 | continue; |
448 | } | |
2f03f956 | 449 | |
16cc9ca2 | 450 | if (read_batch) { |
0395130c WD |
451 | next_gen_ndx = get_next_gen_ndx(batch_gen_fd, next_gen_ndx, ndx); |
452 | if (ndx < next_gen_ndx) { | |
45c49b52 WD |
453 | rprintf(FINFO, |
454 | "(Skipping batched update for \"%s\")\n", | |
455 | fname); | |
112d728f | 456 | discard_receive_data(f_in, F_LENGTH(file)); |
16cc9ca2 WD |
457 | continue; |
458 | } | |
0395130c | 459 | next_gen_ndx = -1; |
16cc9ca2 WD |
460 | } |
461 | ||
41cfde6b WD |
462 | partialptr = partial_dir ? partial_dir_fname(fname) : fname; |
463 | ||
9e4a8d29 WD |
464 | if (protocol_version >= 29) { |
465 | switch (fnamecmp_type) { | |
41cfde6b | 466 | case FNAMECMP_FNAME: |
a7260c40 | 467 | fnamecmp = fname; |
41cfde6b WD |
468 | break; |
469 | case FNAMECMP_PARTIAL_DIR: | |
9e4a8d29 | 470 | fnamecmp = partialptr; |
41cfde6b WD |
471 | break; |
472 | case FNAMECMP_BACKUP: | |
473 | fnamecmp = get_backup_name(fname); | |
474 | break; | |
73273075 | 475 | case FNAMECMP_FUZZY: |
48459ba1 | 476 | updating_basis = 0; |
b9232d45 WD |
477 | if (file->dirname) { |
478 | pathjoin(fnamecmpbuf, MAXPATHLEN, | |
479 | file->dirname, xname); | |
480 | fnamecmp = fnamecmpbuf; | |
481 | } else | |
482 | fnamecmp = xname; | |
73273075 | 483 | break; |
41cfde6b | 484 | default: |
48459ba1 | 485 | updating_basis = 0; |
9e4a8d29 | 486 | if (fnamecmp_type >= basis_dir_cnt) { |
c56595d7 WD |
487 | rprintf(FERROR, |
488 | "invalid basis_dir index: %d.\n", | |
9e4a8d29 | 489 | fnamecmp_type); |
c56595d7 WD |
490 | exit_cleanup(RERR_PROTOCOL); |
491 | } | |
41cfde6b | 492 | pathjoin(fnamecmpbuf, sizeof fnamecmpbuf, |
9e4a8d29 | 493 | basis_dir[fnamecmp_type], fname); |
41cfde6b WD |
494 | fnamecmp = fnamecmpbuf; |
495 | break; | |
496 | } | |
9e4a8d29 WD |
497 | if (!fnamecmp || (server_filter_list.head |
498 | && check_filter(&server_filter_list, fname, 0) < 0)) | |
499 | fnamecmp = fname; | |
500 | } else { | |
501 | /* Reminder: --inplace && --partial-dir are never | |
502 | * enabled at the same time. */ | |
f3d6d480 | 503 | if (inplace && make_backups > 0) { |
9e4a8d29 WD |
504 | if (!(fnamecmp = get_backup_name(fname))) |
505 | fnamecmp = fname; | |
506 | } else if (partial_dir && partialptr) | |
507 | fnamecmp = partialptr; | |
508 | else | |
509 | fnamecmp = fname; | |
510 | } | |
dc55d7bd | 511 | |
910e89b9 WD |
512 | initial_stats = stats; |
513 | ||
17fadf7d | 514 | /* open the file */ |
8c9fd200 | 515 | fd1 = do_open(fnamecmp, O_RDONLY, 0); |
375a4556 | 516 | |
9e4a8d29 WD |
517 | if (fd1 == -1 && protocol_version < 29) { |
518 | if (fnamecmp != fname) { | |
519 | fnamecmp = fname; | |
520 | fd1 = do_open(fnamecmp, O_RDONLY, 0); | |
521 | } | |
522 | ||
523 | if (fd1 == -1 && basis_dir[0]) { | |
524 | /* pre-29 allowed only one alternate basis */ | |
525 | pathjoin(fnamecmpbuf, sizeof fnamecmpbuf, | |
526 | basis_dir[0], fname); | |
527 | fnamecmp = fnamecmpbuf; | |
528 | fd1 = do_open(fnamecmp, O_RDONLY, 0); | |
529 | } | |
530 | } | |
531 | ||
d5dcb6f7 WD |
532 | if (fd1 == -1) { |
533 | st.st_mode = 0; | |
534 | st.st_size = 0; | |
535 | } else if (do_fstat(fd1,&st) != 0) { | |
d62bcc17 WD |
536 | rsyserr(FERROR, errno, "fstat %s failed", |
537 | full_fname(fnamecmp)); | |
112d728f | 538 | discard_receive_data(f_in, F_LENGTH(file)); |
2f03f956 AT |
539 | close(fd1); |
540 | continue; | |
541 | } | |
542 | ||
350e4e4d S |
543 | if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) { |
544 | /* this special handling for directories | |
545 | * wouldn't be necessary if robust_rename() | |
546 | * and the underlying robust_unlink could cope | |
547 | * with directories | |
548 | */ | |
ea42541f WD |
549 | rprintf(FERROR,"recv_files: %s is a directory\n", |
550 | full_fname(fnamecmp)); | |
112d728f | 551 | discard_receive_data(f_in, F_LENGTH(file)); |
2f03f956 AT |
552 | close(fd1); |
553 | continue; | |
554 | } | |
555 | ||
350e4e4d S |
556 | if (fd1 != -1 && !S_ISREG(st.st_mode)) { |
557 | close(fd1); | |
558 | fd1 = -1; | |
350e4e4d S |
559 | } |
560 | ||
a9d6e6fc WD |
561 | /* If we're not preserving permissions, change the file-list's |
562 | * mode based on the local permissions and some heuristics. */ | |
563 | if (!preserve_perms) { | |
564 | int exists = fd1 != -1; | |
565 | file->mode = dest_mode(file->mode, st.st_mode, exists); | |
4df9f368 AT |
566 | } |
567 | ||
48459ba1 | 568 | /* We now check to see if we are writing the file "inplace" */ |
a3221d2a | 569 | if (inplace) { |
97894c64 | 570 | fd2 = do_open(fname, O_WRONLY|O_CREAT, 0600); |
a3221d2a WD |
571 | if (fd2 == -1) { |
572 | rsyserr(FERROR, errno, "open %s failed", | |
dc55d7bd | 573 | full_fname(fname)); |
112d728f | 574 | discard_receive_data(f_in, F_LENGTH(file)); |
a3221d2a WD |
575 | if (fd1 != -1) |
576 | close(fd1); | |
577 | continue; | |
578 | } | |
579 | } else { | |
580 | if (!get_tmpname(fnametmp,fname)) { | |
112d728f | 581 | discard_receive_data(f_in, F_LENGTH(file)); |
a3221d2a WD |
582 | if (fd1 != -1) |
583 | close(fd1); | |
584 | continue; | |
585 | } | |
586 | ||
a3221d2a WD |
587 | /* we initially set the perms without the |
588 | * setuid/setgid bits to ensure that there is no race | |
589 | * condition. They are then correctly updated after | |
590 | * the lchown. Thanks to snabb@epipe.fi for pointing | |
591 | * this out. We also set it initially without group | |
592 | * access because of a similar race condition. */ | |
f62c17e3 | 593 | fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS); |
17fadf7d | 594 | |
a3221d2a WD |
595 | /* in most cases parent directories will already exist |
596 | * because their information should have been previously | |
597 | * transferred, but that may not be the case with -R */ | |
598 | if (fd2 == -1 && relative_paths && errno == ENOENT | |
904e5af1 | 599 | && create_directory_path(fnametmp) == 0) { |
b9232d45 WD |
600 | /* Get back to name with XXXXXX in it. */ |
601 | get_tmpname(fnametmp, fname); | |
a3221d2a WD |
602 | fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS); |
603 | } | |
604 | if (fd2 == -1) { | |
605 | rsyserr(FERROR, errno, "mkstemp %s failed", | |
606 | full_fname(fnametmp)); | |
112d728f | 607 | discard_receive_data(f_in, F_LENGTH(file)); |
a3221d2a WD |
608 | if (fd1 != -1) |
609 | close(fd1); | |
610 | continue; | |
611 | } | |
612 | ||
95ae5224 | 613 | cleanup_set(fnametmp, partialptr, file, fd1, fd2); |
a3221d2a | 614 | } |
2f03f956 | 615 | |
a8ed4958 | 616 | /* log the transfer */ |
910e89b9 | 617 | if (log_before_transfer) |
85909931 | 618 | log_item(FCLIENT, file, &initial_stats, iflags, NULL); |
4bb0af79 | 619 | else if (!am_server && verbose && do_progress) |
45c49b52 | 620 | rprintf(FINFO, "%s\n", fname); |
11a5a3c7 | 621 | |
2f03f956 | 622 | /* recv file data */ |
7e5fa372 | 623 | recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size, |
112d728f | 624 | fname, fd2, F_LENGTH(file)); |
1b7c47cb | 625 | |
2fedf3d5 | 626 | log_item(log_code, file, &initial_stats, iflags, NULL); |
17fadf7d | 627 | |
d2a918b4 | 628 | if (fd1 != -1) |
2f03f956 | 629 | close(fd1); |
9f27cd8c | 630 | if (close(fd2) < 0) { |
d62bcc17 WD |
631 | rsyserr(FERROR, errno, "close failed on %s", |
632 | full_fname(fnametmp)); | |
9f27cd8c WD |
633 | exit_cleanup(RERR_FILEIO); |
634 | } | |
17fadf7d | 635 | |
68795c64 | 636 | if ((recv_ok && (!delay_updates || !partialptr)) || inplace) { |
01d124d9 WD |
637 | char *temp_copy_name; |
638 | if (partialptr == fname) | |
639 | partialptr = temp_copy_name = NULL; | |
640 | else if (*partial_dir == '/') | |
641 | temp_copy_name = NULL; | |
642 | else | |
643 | temp_copy_name = partialptr; | |
644 | finish_transfer(fname, fnametmp, temp_copy_name, | |
4a4d2b1b WD |
645 | file, recv_ok, 1); |
646 | if (fnamecmp == partialptr) { | |
aec6b9f8 WD |
647 | do_unlink(partialptr); |
648 | handle_partial_dir(partialptr, PDIR_DELETE); | |
649 | } | |
650 | } else if (keep_partial && partialptr | |
651 | && handle_partial_dir(partialptr, PDIR_CREATE)) { | |
4a4d2b1b WD |
652 | finish_transfer(partialptr, fnametmp, NULL, |
653 | file, recv_ok, !partial_dir); | |
3e7934a5 | 654 | if (delay_updates && recv_ok) { |
0395130c | 655 | bitbag_set_bit(delayed_bits, ndx); |
3e7934a5 WD |
656 | recv_ok = -1; |
657 | } | |
aec6b9f8 | 658 | } else { |
a7260c40 | 659 | partialptr = NULL; |
55e50d89 | 660 | do_unlink(fnametmp); |
a7260c40 WD |
661 | } |
662 | ||
2f03f956 | 663 | cleanup_disable(); |
554e0a8d | 664 | |
22907b6b | 665 | if (recv_ok > 0) { |
3ea6e0e7 | 666 | if (remove_source_files || inc_recurse |
112d728f | 667 | || (preserve_hard_links && F_IS_HLINKED(file))) |
0395130c | 668 | send_msg_int(MSG_SUCCESS, ndx); |
22907b6b | 669 | } else if (!recv_ok) { |
d0221f1d | 670 | enum logcode msgtype = redoing || read_batch ? FERROR : FINFO; |
007e3c0e | 671 | if (msgtype == FERROR || verbose) { |
a7260c40 WD |
672 | char *errstr, *redostr, *keptstr; |
673 | if (!(keep_partial && partialptr) && !inplace) | |
674 | keptstr = "discarded"; | |
675 | else if (partial_dir) | |
676 | keptstr = "put into partial-dir"; | |
677 | else | |
678 | keptstr = "retained"; | |
007e3c0e WD |
679 | if (msgtype == FERROR) { |
680 | errstr = "ERROR"; | |
681 | redostr = ""; | |
682 | } else { | |
683 | errstr = "WARNING"; | |
684 | redostr = " (will try again)"; | |
685 | } | |
686 | rprintf(msgtype, | |
a7260c40 | 687 | "%s: %s failed verification -- update %s%s.\n", |
45c49b52 | 688 | errstr, fname, keptstr, redostr); |
007e3c0e | 689 | } |
d0221f1d | 690 | if (!redoing) { |
0395130c | 691 | send_msg_int(MSG_REDO, ndx); |
f3d6d480 | 692 | file->flags |= FLAG_FILE_SENT; |
3ea6e0e7 | 693 | } else if (inc_recurse) |
d0221f1d | 694 | send_msg_int(MSG_NO_SEND, ndx); |
2f03f956 AT |
695 | } |
696 | } | |
f3d6d480 WD |
697 | if (make_backups < 0) |
698 | make_backups = -make_backups; | |
2f03f956 | 699 | |
ac3f7b81 | 700 | if (phase == 2 && delay_updates) /* for protocol_version < 29 */ |
f3d6d480 | 701 | handle_delayed_updates(local_name); |
48e1c8c6 | 702 | |
2f03f956 AT |
703 | if (verbose > 2) |
704 | rprintf(FINFO,"recv_files finished\n"); | |
17fadf7d | 705 | |
2f03f956 AT |
706 | return 0; |
707 | } |