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