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 | |
b3bf9b9d | 6 | * Copyright (C) 2003-2009 Wayne Davison |
0f78b815 WD |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify | |
8e41b68e WD |
9 | * it under the terms of the GNU General Public License as published by |
10 | * the Free Software Foundation; either version 3 of the License, or | |
11 | * (at your option) any later version. | |
0f78b815 WD |
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 | 18 | * You should have received a copy of the GNU General Public License along |
4fd842f9 | 19 | * with this program; if not, visit the http://fsf.org website. |
0f78b815 | 20 | */ |
2f03f956 AT |
21 | |
22 | #include "rsync.h" | |
5dd14f0c | 23 | #include "inums.h" |
2f03f956 | 24 | |
16edf865 | 25 | extern int dry_run; |
a0009fc3 | 26 | extern int do_xfers; |
8715db2c | 27 | extern int am_server; |
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; |
16edf865 | 40 | extern int preserve_xattrs; |
c56595d7 | 41 | extern int basis_dir_cnt; |
53dd3135 | 42 | extern int make_backups; |
925c517f | 43 | extern int cleanup_got_literal; |
744e63fb | 44 | extern int remove_source_files; |
6cc11982 | 45 | extern int append_mode; |
e7ee91de | 46 | extern int sparse_files; |
55e50d89 | 47 | extern int keep_partial; |
886df221 | 48 | extern int checksum_len; |
ba582f75 | 49 | extern int checksum_seed; |
a3221d2a | 50 | extern int inplace; |
05c36015 | 51 | extern int allowed_lull; |
48e1c8c6 | 52 | extern int delay_updates; |
1c3344a1 | 53 | extern mode_t orig_umask; |
8715db2c | 54 | extern struct stats stats; |
8715db2c WD |
55 | extern char *tmpdir; |
56 | extern char *partial_dir; | |
b791d680 WD |
57 | extern char *basis_dir[MAX_BASIS_DIRS+1]; |
58 | extern char sender_file_sum[MAX_DIGEST_LEN]; | |
6755a7d7 | 59 | extern struct file_list *cur_flist, *first_flist, *dir_flist; |
7b6c5c77 | 60 | extern filter_rule_list daemon_filter_list; |
5ebab6c1 | 61 | |
10f994a5 | 62 | static struct bitbag *delayed_bits = NULL; |
d0221f1d | 63 | static int phase = 0, redoing = 0; |
8ab02ccd | 64 | static flist_ndx_list batch_redo_list; |
48459ba1 | 65 | /* We're either updating the basis file or an identical copy: */ |
77943e69 | 66 | static int updating_basis_or_equiv; |
3e7934a5 | 67 | |
c55fb5e1 WD |
68 | #define TMPNAME_SUFFIX ".XXXXXX" |
69 | #define TMPNAME_SUFFIX_LEN ((int)sizeof TMPNAME_SUFFIX - 1) | |
70 | #define MAX_UNIQUE_NUMBER 999999 | |
71 | #define MAX_UNIQUE_LOOP 100 | |
72 | ||
73 | /* get_tmpname() - create a tmp filename for a given filename | |
52d3e106 | 74 | * |
c55fb5e1 WD |
75 | * If a tmpdir is defined, use that as the directory to put it in. Otherwise, |
76 | * the tmp filename is in the same directory as the given name. Note that | |
77 | * there may be no directory at all in the given name! | |
17fadf7d | 78 | * |
c55fb5e1 WD |
79 | * The tmp filename is basically the given filename with a dot prepended, and |
80 | * .XXXXXX appended (for mkstemp() to put its unique gunk in). We take care | |
81 | * to not exceed either the MAXPATHLEN or NAME_MAX, especially the last, as | |
82 | * the basename basically becomes 8 characters longer. In such a case, the | |
83 | * original name is shortened sufficiently to make it all fit. | |
17fadf7d | 84 | * |
c55fb5e1 WD |
85 | * If the make_unique arg is True, the XXXXXX string is replaced with a unique |
86 | * string that doesn't exist at the time of the check. This is intended to be | |
87 | * used for creating hard links, symlinks, devices, and special files, since | |
88 | * normal files should be handled by mkstemp() for safety. | |
89 | * | |
90 | * Of course, the only reason the file is based on the original name is to | |
91 | * make it easier to figure out what purpose a temp file is serving when a | |
92 | * transfer is in progress. */ | |
93 | int get_tmpname(char *fnametmp, const char *fname, BOOL make_unique) | |
2f03f956 | 94 | { |
d5dcb6f7 | 95 | int maxname, added, length = 0; |
93204cca | 96 | const char *f; |
c55fb5e1 | 97 | char *suf; |
2f03f956 | 98 | |
2f03f956 | 99 | if (tmpdir) { |
a24639bb | 100 | /* Note: this can't overflow, so the return value is safe */ |
b7cee949 | 101 | length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2); |
52d3e106 | 102 | fnametmp[length++] = '/'; |
0c2ef5f4 | 103 | } |
52d3e106 | 104 | |
0c2ef5f4 | 105 | if ((f = strrchr(fname, '/')) != NULL) { |
52d3e106 S |
106 | ++f; |
107 | if (!tmpdir) { | |
108 | length = f - fname; | |
0c2ef5f4 | 109 | /* copy up to and including the slash */ |
52d3e106 | 110 | strlcpy(fnametmp, fname, length + 1); |
0c2ef5f4 | 111 | } |
446e239e | 112 | } else |
52d3e106 | 113 | f = fname; |
52d3e106 | 114 | fnametmp[length++] = '.'; |
2f03f956 | 115 | |
d5dcb6f7 | 116 | /* The maxname value is bufsize, and includes space for the '\0'. |
c55fb5e1 WD |
117 | * NAME_MAX needs an extra -1 for the name's leading dot. */ |
118 | maxname = MIN(MAXPATHLEN - length - TMPNAME_SUFFIX_LEN, | |
119 | NAME_MAX - 1 - TMPNAME_SUFFIX_LEN); | |
2f03f956 | 120 | |
0c2ef5f4 | 121 | if (maxname < 1) { |
3f0211b6 | 122 | rprintf(FERROR_XFER, "temporary filename too long: %s\n", fname); |
52d3e106 | 123 | fnametmp[0] = '\0'; |
2f03f956 AT |
124 | return 0; |
125 | } | |
126 | ||
d5dcb6f7 WD |
127 | added = strlcpy(fnametmp + length, f, maxname); |
128 | if (added >= maxname) | |
129 | added = maxname - 1; | |
c55fb5e1 WD |
130 | suf = fnametmp + length + added; |
131 | ||
132 | if (make_unique) { | |
133 | static unsigned counter_limit; | |
134 | unsigned counter; | |
135 | ||
136 | if (!counter_limit) { | |
137 | counter_limit = (unsigned)getpid() + MAX_UNIQUE_LOOP; | |
138 | if (counter_limit > MAX_UNIQUE_NUMBER || counter_limit < MAX_UNIQUE_LOOP) | |
139 | counter_limit = MAX_UNIQUE_LOOP; | |
140 | } | |
141 | counter = counter_limit - MAX_UNIQUE_LOOP; | |
142 | ||
143 | /* This doesn't have to be very good because we don't need | |
144 | * to worry about someone trying to guess the values: all | |
145 | * a conflict will do is cause a device, special file, hard | |
146 | * link, or symlink to fail to be created. Also: avoid | |
147 | * using mktemp() due to gcc's annoying warning. */ | |
148 | while (1) { | |
149 | snprintf(suf, TMPNAME_SUFFIX_LEN+1, ".%d", counter); | |
150 | if (access(fnametmp, 0) < 0) | |
151 | break; | |
152 | if (++counter >= counter_limit) | |
153 | return 0; | |
154 | } | |
155 | } else | |
156 | memcpy(suf, TMPNAME_SUFFIX, TMPNAME_SUFFIX_LEN+1); | |
2f03f956 AT |
157 | |
158 | return 1; | |
159 | } | |
160 | ||
2cce7545 WD |
161 | /* Opens a temporary file for writing. |
162 | * Success: Writes name into fnametmp, returns fd. | |
163 | * Failure: Clobbers fnametmp, returns -1. | |
164 | * Calling cleanup_set() is the caller's job. */ | |
165 | int open_tmpfile(char *fnametmp, const char *fname, struct file_struct *file) | |
166 | { | |
167 | int fd; | |
168 | ||
c55fb5e1 | 169 | if (!get_tmpname(fnametmp, fname, False)) |
2cce7545 WD |
170 | return -1; |
171 | ||
172 | /* We initially set the perms without the setuid/setgid bits or group | |
173 | * access to ensure that there is no race condition. They will be | |
174 | * correctly updated after the right owner and group info is set. | |
175 | * (Thanks to snabb@epipe.fi for pointing this out.) */ | |
3b22184d | 176 | fd = do_mkstemp(fnametmp, (file->mode & INITACCESSPERMS) | S_IWUSR); |
2cce7545 WD |
177 | |
178 | #if 0 | |
179 | /* In most cases parent directories will already exist because their | |
180 | * information should have been previously transferred, but that may | |
181 | * not be the case with -R */ | |
182 | if (fd == -1 && relative_paths && errno == ENOENT | |
3696674b | 183 | && make_path(fnametmp, MKP_SKIP_SLASH | MKP_DROP_NAME) == 0) { |
2cce7545 | 184 | /* Get back to name with XXXXXX in it. */ |
c55fb5e1 | 185 | get_tmpname(fnametmp, fname, False); |
2cce7545 WD |
186 | fd = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS); |
187 | } | |
188 | #endif | |
189 | ||
190 | if (fd == -1) { | |
3f0211b6 | 191 | rsyserr(FERROR_XFER, errno, "mkstemp %s failed", |
2cce7545 WD |
192 | full_fname(fnametmp)); |
193 | return -1; | |
194 | } | |
195 | ||
196 | return fd; | |
197 | } | |
2f03f956 | 198 | |
7e5fa372 | 199 | static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r, |
55edf18c | 200 | const char *fname, int fd, OFF_T total_size) |
2f03f956 | 201 | { |
a0456b9c | 202 | static char file_sum1[MAX_DIGEST_LEN]; |
7e5fa372 | 203 | struct map_struct *mapbuf; |
fc0257c9 | 204 | struct sum_struct sum; |
886df221 | 205 | int32 len; |
2f03f956 | 206 | OFF_T offset = 0; |
89e540e6 | 207 | OFF_T offset2; |
2f03f956 | 208 | char *data; |
6c495e0d | 209 | int32 i; |
e1f67417 | 210 | char *map = NULL; |
17fadf7d | 211 | |
fc0257c9 | 212 | read_sum_head(f_in, &sum); |
17fadf7d | 213 | |
7e5fa372 | 214 | if (fd_r >= 0 && size_r > 0) { |
80264051 WD |
215 | int32 read_size = MAX(sum.blength * 2, 16*1024); |
216 | mapbuf = map_file(fd_r, size_r, read_size, sum.blength); | |
5a18b34d | 217 | if (DEBUG_GTE(DELTASUM, 2)) { |
6d56efa6 | 218 | rprintf(FINFO, "recv mapped %s of size %s\n", |
adc2476f | 219 | fname_r, big_num(size_r)); |
7e5fa372 WD |
220 | } |
221 | } else | |
222 | mapbuf = NULL; | |
223 | ||
ba582f75 | 224 | sum_init(checksum_seed); |
17fadf7d | 225 | |
f3d6d480 | 226 | if (append_mode > 0) { |
6cc11982 WD |
227 | OFF_T j; |
228 | sum.flength = (OFF_T)sum.count * sum.blength; | |
229 | if (sum.remainder) | |
230 | sum.flength -= sum.blength - sum.remainder; | |
b32fd634 | 231 | if (append_mode == 2 && mapbuf) { |
936fa865 | 232 | for (j = CHUNK_SIZE; j < sum.flength; j += CHUNK_SIZE) { |
951e826b | 233 | if (INFO_GTE(PROGRESS, 1)) |
936fa865 WD |
234 | show_progress(offset, total_size); |
235 | sum_update(map_ptr(mapbuf, offset, CHUNK_SIZE), | |
236 | CHUNK_SIZE); | |
237 | offset = j; | |
238 | } | |
239 | if (offset < sum.flength) { | |
240 | int32 len = (int32)(sum.flength - offset); | |
951e826b | 241 | if (INFO_GTE(PROGRESS, 1)) |
936fa865 WD |
242 | show_progress(offset, total_size); |
243 | sum_update(map_ptr(mapbuf, offset, len), len); | |
244 | } | |
6cc11982 | 245 | } |
936fa865 | 246 | offset = sum.flength; |
ffa8ab8e | 247 | if (fd != -1 && (j = do_lseek(fd, offset, SEEK_SET)) != offset) { |
6d56efa6 | 248 | rsyserr(FERROR_XFER, errno, "lseek of %s returned %s, not %s", |
adc2476f | 249 | full_fname(fname), big_num(j), big_num(offset)); |
6cc11982 WD |
250 | exit_cleanup(RERR_FILEIO); |
251 | } | |
252 | } | |
253 | ||
3f55bd5d | 254 | while ((i = recv_token(f_in, &data)) != 0) { |
951e826b | 255 | if (INFO_GTE(PROGRESS, 1)) |
16417f8b | 256 | show_progress(offset, total_size); |
2f03f956 | 257 | |
05c36015 WD |
258 | if (allowed_lull) |
259 | maybe_send_keepalive(time(NULL), MSK_ALLOW_FLUSH | MSK_ACTIVE_RECEIVER); | |
260 | ||
2f03f956 | 261 | if (i > 0) { |
5a18b34d | 262 | if (DEBUG_GTE(DELTASUM, 3)) { |
6d56efa6 | 263 | rprintf(FINFO,"data recv %d at %s\n", |
adc2476f | 264 | i, big_num(offset)); |
2f03f956 AT |
265 | } |
266 | ||
267 | stats.literal_data += i; | |
268 | cleanup_got_literal = 1; | |
17fadf7d | 269 | |
6c495e0d | 270 | sum_update(data, i); |
2f03f956 | 271 | |
c52461f9 WD |
272 | if (fd != -1 && write_file(fd,data,i) != i) |
273 | goto report_write_error; | |
2f03f956 AT |
274 | offset += i; |
275 | continue; | |
17fadf7d | 276 | } |
2f03f956 AT |
277 | |
278 | i = -(i+1); | |
6c495e0d | 279 | offset2 = i * (OFF_T)sum.blength; |
fc0257c9 | 280 | len = sum.blength; |
d2a918b4 | 281 | if (i == (int)sum.count-1 && sum.remainder != 0) |
fc0257c9 | 282 | len = sum.remainder; |
17fadf7d | 283 | |
2f03f956 | 284 | stats.matched_data += len; |
17fadf7d | 285 | |
5a18b34d | 286 | if (DEBUG_GTE(DELTASUM, 3)) { |
6c495e0d | 287 | rprintf(FINFO, |
55f767c5 WD |
288 | "chunk[%d] of size %ld at %s offset=%s%s\n", |
289 | i, (long)len, big_num(offset2), big_num(offset), | |
290 | updating_basis_or_equiv && offset == offset2 ? " (seek)" : ""); | |
6c495e0d | 291 | } |
17fadf7d | 292 | |
446e239e WD |
293 | if (mapbuf) { |
294 | map = map_ptr(mapbuf,offset2,len); | |
17fadf7d | 295 | |
c55f7021 | 296 | see_token(map, len); |
6c495e0d | 297 | sum_update(map, len); |
c55f7021 | 298 | } |
17fadf7d | 299 | |
77943e69 | 300 | if (updating_basis_or_equiv) { |
89e540e6 | 301 | if (offset == offset2 && fd != -1) { |
ffa8ab8e | 302 | OFF_T pos; |
c52461f9 WD |
303 | if (flush_write_file(fd) < 0) |
304 | goto report_write_error; | |
89e540e6 | 305 | offset += len; |
ffa8ab8e | 306 | if ((pos = do_lseek(fd, len, SEEK_CUR)) != offset) { |
3f0211b6 | 307 | rsyserr(FERROR_XFER, errno, |
6d56efa6 | 308 | "lseek of %s returned %s, not %s", |
ffa8ab8e | 309 | full_fname(fname), |
adc2476f | 310 | big_num(pos), big_num(offset)); |
fab65a5b WD |
311 | exit_cleanup(RERR_FILEIO); |
312 | } | |
89e540e6 | 313 | continue; |
a3221d2a | 314 | } |
2f03f956 | 315 | } |
1ed91a04 | 316 | if (fd != -1 && map && write_file(fd, map, len) != (int)len) |
c52461f9 | 317 | goto report_write_error; |
2f03f956 AT |
318 | offset += len; |
319 | } | |
320 | ||
85f14172 WD |
321 | if (flush_write_file(fd) < 0) |
322 | goto report_write_error; | |
76c21947 | 323 | |
4f5b0756 | 324 | #ifdef HAVE_FTRUNCATE |
aade88bf WD |
325 | if (inplace && fd != -1 |
326 | && ftruncate(fd, offset) < 0) { | |
327 | rsyserr(FERROR_XFER, errno, "ftruncate failed on %s", | |
328 | full_fname(fname)); | |
94112924 | 329 | } |
a3221d2a WD |
330 | #endif |
331 | ||
951e826b | 332 | if (INFO_GTE(PROGRESS, 1)) |
16417f8b | 333 | end_progress(total_size); |
2f03f956 AT |
334 | |
335 | if (fd != -1 && offset > 0 && sparse_end(fd) != 0) { | |
c52461f9 | 336 | report_write_error: |
3f0211b6 | 337 | rsyserr(FERROR_XFER, errno, "write failed on %s", |
d62bcc17 | 338 | full_fname(fname)); |
65417579 | 339 | exit_cleanup(RERR_FILEIO); |
2f03f956 AT |
340 | } |
341 | ||
886df221 WD |
342 | if (sum_end(file_sum1) != checksum_len) |
343 | overflow_exit("checksum_len"); /* Impossible... */ | |
2f03f956 | 344 | |
7e5fa372 WD |
345 | if (mapbuf) |
346 | unmap_file(mapbuf); | |
347 | ||
886df221 | 348 | read_buf(f_in, sender_file_sum, checksum_len); |
5a18b34d | 349 | if (DEBUG_GTE(DELTASUM, 2)) |
bc63ae3f | 350 | rprintf(FINFO,"got file_sum\n"); |
886df221 | 351 | if (fd != -1 && memcmp(file_sum1, sender_file_sum, checksum_len) != 0) |
bc63ae3f | 352 | return 0; |
2f03f956 AT |
353 | return 1; |
354 | } | |
355 | ||
356 | ||
8ed9d849 WD |
357 | static void discard_receive_data(int f_in, OFF_T length) |
358 | { | |
7e5fa372 | 359 | receive_data(f_in, NULL, -1, 0, NULL, -1, length); |
8ed9d849 WD |
360 | } |
361 | ||
f3d6d480 | 362 | static void handle_delayed_updates(char *local_name) |
42be5320 | 363 | { |
663b2857 | 364 | char *fname, *partialptr; |
0395130c | 365 | int ndx; |
42be5320 | 366 | |
0395130c | 367 | for (ndx = -1; (ndx = bitbag_next_bit(delayed_bits, ndx)) >= 0; ) { |
f3d6d480 | 368 | struct file_struct *file = cur_flist->files[ndx]; |
6cbde57d | 369 | fname = local_name ? local_name : f_name(file, NULL); |
42be5320 | 370 | if ((partialptr = partial_dir_fname(fname)) != NULL) { |
21cddef2 | 371 | if (make_backups > 0 && !make_backup(fname, False)) |
42be5320 | 372 | continue; |
951e826b | 373 | if (DEBUG_GTE(RECV, 1)) { |
42be5320 | 374 | rprintf(FINFO, "renaming %s to %s\n", |
45c49b52 | 375 | partialptr, fname); |
42be5320 | 376 | } |
6a94c58b WD |
377 | /* We don't use robust_rename() here because the |
378 | * partial-dir must be on the same drive. */ | |
42be5320 | 379 | if (do_rename(partialptr, fname) < 0) { |
3f0211b6 | 380 | rsyserr(FERROR_XFER, errno, |
42be5320 | 381 | "rename failed for %s (from %s)", |
45c49b52 | 382 | full_fname(fname), partialptr); |
42be5320 | 383 | } else { |
47c11975 | 384 | if (remove_source_files |
112d728f | 385 | || (preserve_hard_links && F_IS_HLINKED(file))) |
0395130c | 386 | send_msg_int(MSG_SUCCESS, ndx); |
45c49b52 | 387 | handle_partial_dir(partialptr, PDIR_DELETE); |
42be5320 WD |
388 | } |
389 | } | |
390 | } | |
391 | } | |
392 | ||
8ab02ccd | 393 | static void no_batched_update(int ndx, BOOL is_redo) |
154cdaaa | 394 | { |
8ab02ccd WD |
395 | struct file_list *flist = flist_for_ndx(ndx, "no_batched_update"); |
396 | struct file_struct *file = flist->files[ndx - flist->ndx_start]; | |
397 | ||
398 | rprintf(FERROR_XFER, "(No batched update for%s \"%s\")\n", | |
399 | is_redo ? " resend of" : "", f_name(file, NULL)); | |
400 | ||
83c32ca5 | 401 | if (inc_recurse && !dry_run) |
8ab02ccd WD |
402 | send_msg_int(MSG_NO_SEND, ndx); |
403 | } | |
404 | ||
405 | static int we_want_redo(int desired_ndx) | |
406 | { | |
407 | static int redo_ndx = -1; | |
408 | ||
409 | while (redo_ndx < desired_ndx) { | |
410 | if (redo_ndx >= 0) | |
411 | no_batched_update(redo_ndx, True); | |
412 | if ((redo_ndx = flist_ndx_pop(&batch_redo_list)) < 0) | |
413 | return 0; | |
414 | } | |
415 | ||
416 | if (redo_ndx == desired_ndx) { | |
417 | redo_ndx = -1; | |
418 | return 1; | |
419 | } | |
420 | ||
421 | return 0; | |
422 | } | |
423 | ||
7a498f5b | 424 | static int gen_wants_ndx(int desired_ndx, int flist_num) |
8ab02ccd WD |
425 | { |
426 | static int next_ndx = -1; | |
4079d6a6 WD |
427 | static int done_cnt = 0; |
428 | static BOOL got_eof = False; | |
8ab02ccd WD |
429 | |
430 | if (got_eof) | |
431 | return 0; | |
432 | ||
20caffd2 WD |
433 | /* TODO: integrate gen-reading I/O into perform_io() so this is not needed? */ |
434 | io_flush(FULL_FLUSH); | |
435 | ||
8ab02ccd | 436 | while (next_ndx < desired_ndx) { |
4079d6a6 WD |
437 | if (inc_recurse && flist_num <= done_cnt) |
438 | return 0; | |
8ab02ccd WD |
439 | if (next_ndx >= 0) |
440 | no_batched_update(next_ndx, False); | |
441 | if ((next_ndx = read_int(batch_gen_fd)) < 0) { | |
4079d6a6 WD |
442 | if (inc_recurse) { |
443 | done_cnt++; | |
444 | continue; | |
445 | } | |
8ab02ccd WD |
446 | got_eof = True; |
447 | return 0; | |
154cdaaa | 448 | } |
154cdaaa | 449 | } |
af2dea60 | 450 | |
8ab02ccd WD |
451 | if (next_ndx == desired_ndx) { |
452 | next_ndx = -1; | |
453 | return 1; | |
454 | } | |
455 | ||
456 | return 0; | |
154cdaaa WD |
457 | } |
458 | ||
b35d0d8e MP |
459 | /** |
460 | * main routine for receiver process. | |
461 | * | |
462 | * Receiver process runs on the same host as the generator process. */ | |
20caffd2 | 463 | int recv_files(int f_in, int f_out, char *local_name) |
17fadf7d | 464 | { |
2f03f956 AT |
465 | int fd1,fd2; |
466 | STRUCT_STAT st; | |
b9232d45 | 467 | int iflags, xlen; |
446e239e | 468 | char *fname, fbuf[MAXPATHLEN]; |
b9232d45 | 469 | char xname[MAXPATHLEN]; |
2f03f956 | 470 | char fnametmp[MAXPATHLEN]; |
663b2857 | 471 | char *fnamecmp, *partialptr; |
375a4556 | 472 | char fnamecmpbuf[MAXPATHLEN]; |
9e4a8d29 | 473 | uchar fnamecmp_type; |
2f03f956 | 474 | struct file_struct *file; |
2fedf3d5 WD |
475 | int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i; |
476 | enum logcode log_code = log_before_transfer ? FLOG : FINFO; | |
1ed91a04 | 477 | int max_phase = protocol_version >= 29 ? 2 : 1; |
1c3344a1 WD |
478 | int dflt_perms = (ACCESSPERMS & ~orig_umask); |
479 | #ifdef SUPPORT_ACLS | |
480 | const char *parent_dirname = ""; | |
481 | #endif | |
0395130c | 482 | int ndx, recv_ok; |
11a5a3c7 | 483 | |
951e826b | 484 | if (DEBUG_GTE(RECV, 1)) |
9decb4d2 | 485 | rprintf(FINFO, "recv_files(%d) starting\n", cur_flist->used); |
2f03f956 | 486 | |
3e7934a5 | 487 | if (delay_updates) |
9decb4d2 | 488 | delayed_bits = bitbag_create(cur_flist->used + 1); |
48e1c8c6 | 489 | |
17fadf7d | 490 | while (1) { |
2f03f956 AT |
491 | cleanup_disable(); |
492 | ||
f3d6d480 | 493 | /* This call also sets cur_flist. */ |
20caffd2 | 494 | ndx = read_ndx_and_attrs(f_in, f_out, &iflags, &fnamecmp_type, |
16edf865 | 495 | xname, &xlen); |
0395130c | 496 | if (ndx == NDX_DONE) { |
951e826b WD |
497 | if (!am_server && INFO_GTE(PROGRESS, 2) && cur_flist) { |
498 | set_current_file_index(NULL, 0); | |
499 | end_progress(0); | |
500 | } | |
3ea6e0e7 | 501 | if (inc_recurse && first_flist) { |
7a498f5b WD |
502 | if (read_batch) { |
503 | ndx = first_flist->used + first_flist->ndx_start; | |
504 | gen_wants_ndx(ndx, first_flist->flist_num); | |
505 | } | |
f3d6d480 WD |
506 | flist_free(first_flist); |
507 | if (first_flist) | |
508 | continue; | |
7a498f5b WD |
509 | } else if (read_batch && first_flist) { |
510 | ndx = first_flist->used; | |
511 | gen_wants_ndx(ndx, first_flist->flist_num); | |
512 | } | |
ac3f7b81 | 513 | if (++phase > max_phase) |
4e834af1 | 514 | break; |
951e826b | 515 | if (DEBUG_GTE(RECV, 1)) |
4e834af1 | 516 | rprintf(FINFO, "recv_files phase=%d\n", phase); |
ac3f7b81 | 517 | if (phase == 2 && delay_updates) |
f3d6d480 | 518 | handle_delayed_updates(local_name); |
20caffd2 | 519 | write_int(f_out, NDX_DONE); |
4e834af1 | 520 | continue; |
2f03f956 AT |
521 | } |
522 | ||
6755a7d7 WD |
523 | if (ndx - cur_flist->ndx_start >= 0) |
524 | file = cur_flist->files[ndx - cur_flist->ndx_start]; | |
525 | else | |
526 | file = dir_flist->files[cur_flist->parent_ndx]; | |
6cbde57d | 527 | fname = local_name ? local_name : f_name(file, fbuf); |
227a9c41 | 528 | |
951e826b | 529 | if (DEBUG_GTE(RECV, 1)) |
45c49b52 | 530 | rprintf(FINFO, "recv_files(%s)\n", fname); |
910e89b9 | 531 | |
16edf865 | 532 | #ifdef SUPPORT_XATTRS |
d510e82f | 533 | if (iflags & ITEM_REPORT_XATTR && do_xfers) |
16edf865 WD |
534 | recv_xattr_request(file, f_in); |
535 | #endif | |
536 | ||
22907b6b | 537 | if (!(iflags & ITEM_TRANSFER)) { |
b9232d45 | 538 | maybe_log_item(file, iflags, itemizing, xname); |
16edf865 | 539 | #ifdef SUPPORT_XATTRS |
d510e82f | 540 | if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers) |
16edf865 WD |
541 | set_file_attrs(fname, file, NULL, fname, 0); |
542 | #endif | |
e366e530 WD |
543 | if (iflags & ITEM_IS_NEW) { |
544 | stats.created_files++; | |
545 | if (S_ISREG(file->mode)) { | |
546 | /* Nothing further to count. */ | |
547 | } else if (S_ISDIR(file->mode)) | |
548 | stats.created_dirs++; | |
549 | #ifdef SUPPORT_LINKS | |
550 | else if (S_ISLNK(file->mode)) | |
551 | stats.created_symlinks++; | |
552 | #endif | |
553 | else if (IS_DEVICE(file->mode)) | |
554 | stats.created_devices++; | |
555 | else | |
556 | stats.created_specials++; | |
557 | } | |
58a14ed9 WD |
558 | continue; |
559 | } | |
ac3f7b81 WD |
560 | if (phase == 2) { |
561 | rprintf(FERROR, | |
562 | "got transfer request in phase 2 [%s]\n", | |
563 | who_am_i()); | |
564 | exit_cleanup(RERR_PROTOCOL); | |
565 | } | |
910e89b9 | 566 | |
f3d6d480 WD |
567 | if (file->flags & FLAG_FILE_SENT) { |
568 | if (csum_length == SHORT_SUM_LENGTH) { | |
569 | if (keep_partial && !partial_dir) | |
570 | make_backups = -make_backups; /* prevents double backup */ | |
936fa865 WD |
571 | if (append_mode) |
572 | sparse_files = -sparse_files; | |
f3d6d480 | 573 | append_mode = -append_mode; |
f3d6d480 | 574 | csum_length = SUM_LENGTH; |
d0221f1d | 575 | redoing = 1; |
f3d6d480 WD |
576 | } |
577 | } else { | |
578 | if (csum_length != SHORT_SUM_LENGTH) { | |
579 | if (keep_partial && !partial_dir) | |
580 | make_backups = -make_backups; | |
936fa865 WD |
581 | if (append_mode) |
582 | sparse_files = -sparse_files; | |
f3d6d480 | 583 | append_mode = -append_mode; |
f3d6d480 | 584 | csum_length = SHORT_SUM_LENGTH; |
d0221f1d | 585 | redoing = 0; |
f3d6d480 | 586 | } |
e366e530 WD |
587 | if (iflags & ITEM_IS_NEW) |
588 | stats.created_files++; | |
f3d6d480 WD |
589 | } |
590 | ||
951e826b | 591 | if (!am_server && INFO_GTE(PROGRESS, 1)) |
65b4e4b2 | 592 | set_current_file_index(file, ndx); |
e366e530 | 593 | stats.xferred_files++; |
112d728f | 594 | stats.total_transferred_size += F_LENGTH(file); |
65b4e4b2 | 595 | |
925c517f | 596 | cleanup_got_literal = 0; |
2f03f956 | 597 | |
819bfe45 | 598 | if (daemon_filter_list.head |
1df02d13 | 599 | && check_filter(&daemon_filter_list, FLOG, fname, 0) < 0) { |
8c2ffaf0 WD |
600 | rprintf(FERROR, "attempt to hack rsync failed.\n"); |
601 | exit_cleanup(RERR_PROTOCOL); | |
602 | } | |
603 | ||
16cc9ca2 | 604 | if (read_batch) { |
7a498f5b WD |
605 | int wanted = redoing |
606 | ? we_want_redo(ndx) | |
607 | : gen_wants_ndx(ndx, cur_flist->flist_num); | |
608 | if (!wanted) { | |
45c49b52 | 609 | rprintf(FINFO, |
8ab02ccd WD |
610 | "(Skipping batched update for%s \"%s\")\n", |
611 | redoing ? " resend of" : "", | |
45c49b52 | 612 | fname); |
112d728f | 613 | discard_receive_data(f_in, F_LENGTH(file)); |
8ab02ccd | 614 | file->flags |= FLAG_FILE_SENT; |
16cc9ca2 WD |
615 | continue; |
616 | } | |
617 | } | |
618 | ||
4dde3347 WD |
619 | if (!log_before_transfer) |
620 | remember_initial_stats(); | |
621 | ||
83c32ca5 | 622 | if (!do_xfers) { /* log the transfer */ |
4dde3347 | 623 | log_item(FCLIENT, file, iflags, NULL); |
83c32ca5 WD |
624 | if (read_batch) |
625 | discard_receive_data(f_in, F_LENGTH(file)); | |
626 | continue; | |
627 | } | |
628 | if (write_batch < 0) { | |
4dde3347 | 629 | log_item(FCLIENT, file, iflags, NULL); |
83c32ca5 WD |
630 | if (!am_server) |
631 | discard_receive_data(f_in, F_LENGTH(file)); | |
632 | continue; | |
633 | } | |
634 | ||
41cfde6b WD |
635 | partialptr = partial_dir ? partial_dir_fname(fname) : fname; |
636 | ||
9e4a8d29 WD |
637 | if (protocol_version >= 29) { |
638 | switch (fnamecmp_type) { | |
41cfde6b | 639 | case FNAMECMP_FNAME: |
a7260c40 | 640 | fnamecmp = fname; |
41cfde6b WD |
641 | break; |
642 | case FNAMECMP_PARTIAL_DIR: | |
9e4a8d29 | 643 | fnamecmp = partialptr; |
41cfde6b WD |
644 | break; |
645 | case FNAMECMP_BACKUP: | |
646 | fnamecmp = get_backup_name(fname); | |
647 | break; | |
73273075 | 648 | case FNAMECMP_FUZZY: |
b9232d45 WD |
649 | if (file->dirname) { |
650 | pathjoin(fnamecmpbuf, MAXPATHLEN, | |
651 | file->dirname, xname); | |
652 | fnamecmp = fnamecmpbuf; | |
653 | } else | |
654 | fnamecmp = xname; | |
73273075 | 655 | break; |
41cfde6b | 656 | default: |
9e4a8d29 | 657 | if (fnamecmp_type >= basis_dir_cnt) { |
c56595d7 WD |
658 | rprintf(FERROR, |
659 | "invalid basis_dir index: %d.\n", | |
9e4a8d29 | 660 | fnamecmp_type); |
c56595d7 WD |
661 | exit_cleanup(RERR_PROTOCOL); |
662 | } | |
41cfde6b | 663 | pathjoin(fnamecmpbuf, sizeof fnamecmpbuf, |
9e4a8d29 | 664 | basis_dir[fnamecmp_type], fname); |
41cfde6b WD |
665 | fnamecmp = fnamecmpbuf; |
666 | break; | |
667 | } | |
819bfe45 | 668 | if (!fnamecmp || (daemon_filter_list.head |
1df02d13 | 669 | && check_filter(&daemon_filter_list, FLOG, fname, 0) < 0)) { |
9e4a8d29 | 670 | fnamecmp = fname; |
77943e69 WD |
671 | fnamecmp_type = FNAMECMP_FNAME; |
672 | } | |
9e4a8d29 WD |
673 | } else { |
674 | /* Reminder: --inplace && --partial-dir are never | |
675 | * enabled at the same time. */ | |
f3d6d480 | 676 | if (inplace && make_backups > 0) { |
9e4a8d29 WD |
677 | if (!(fnamecmp = get_backup_name(fname))) |
678 | fnamecmp = fname; | |
77943e69 WD |
679 | else |
680 | fnamecmp_type = FNAMECMP_BACKUP; | |
9e4a8d29 WD |
681 | } else if (partial_dir && partialptr) |
682 | fnamecmp = partialptr; | |
683 | else | |
684 | fnamecmp = fname; | |
685 | } | |
dc55d7bd | 686 | |
17fadf7d | 687 | /* open the file */ |
8c9fd200 | 688 | fd1 = do_open(fnamecmp, O_RDONLY, 0); |
375a4556 | 689 | |
9e4a8d29 WD |
690 | if (fd1 == -1 && protocol_version < 29) { |
691 | if (fnamecmp != fname) { | |
692 | fnamecmp = fname; | |
693 | fd1 = do_open(fnamecmp, O_RDONLY, 0); | |
694 | } | |
695 | ||
696 | if (fd1 == -1 && basis_dir[0]) { | |
697 | /* pre-29 allowed only one alternate basis */ | |
698 | pathjoin(fnamecmpbuf, sizeof fnamecmpbuf, | |
699 | basis_dir[0], fname); | |
700 | fnamecmp = fnamecmpbuf; | |
701 | fd1 = do_open(fnamecmp, O_RDONLY, 0); | |
702 | } | |
703 | } | |
77943e69 WD |
704 | |
705 | updating_basis_or_equiv = inplace | |
706 | && (fnamecmp == fname || fnamecmp_type == FNAMECMP_BACKUP); | |
9e4a8d29 | 707 | |
d5dcb6f7 WD |
708 | if (fd1 == -1) { |
709 | st.st_mode = 0; | |
710 | st.st_size = 0; | |
711 | } else if (do_fstat(fd1,&st) != 0) { | |
3f0211b6 | 712 | rsyserr(FERROR_XFER, errno, "fstat %s failed", |
d62bcc17 | 713 | full_fname(fnamecmp)); |
112d728f | 714 | discard_receive_data(f_in, F_LENGTH(file)); |
2f03f956 | 715 | close(fd1); |
a430691d WD |
716 | if (inc_recurse) |
717 | send_msg_int(MSG_NO_SEND, ndx); | |
2f03f956 AT |
718 | continue; |
719 | } | |
720 | ||
350e4e4d S |
721 | if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) { |
722 | /* this special handling for directories | |
723 | * wouldn't be necessary if robust_rename() | |
724 | * and the underlying robust_unlink could cope | |
725 | * with directories | |
726 | */ | |
3f0211b6 | 727 | rprintf(FERROR_XFER, "recv_files: %s is a directory\n", |
ea42541f | 728 | full_fname(fnamecmp)); |
112d728f | 729 | discard_receive_data(f_in, F_LENGTH(file)); |
2f03f956 | 730 | close(fd1); |
a430691d WD |
731 | if (inc_recurse) |
732 | send_msg_int(MSG_NO_SEND, ndx); | |
2f03f956 AT |
733 | continue; |
734 | } | |
735 | ||
350e4e4d S |
736 | if (fd1 != -1 && !S_ISREG(st.st_mode)) { |
737 | close(fd1); | |
738 | fd1 = -1; | |
350e4e4d S |
739 | } |
740 | ||
a9d6e6fc WD |
741 | /* If we're not preserving permissions, change the file-list's |
742 | * mode based on the local permissions and some heuristics. */ | |
743 | if (!preserve_perms) { | |
744 | int exists = fd1 != -1; | |
1c3344a1 WD |
745 | #ifdef SUPPORT_ACLS |
746 | const char *dn = file->dirname ? file->dirname : "."; | |
747 | if (parent_dirname != dn | |
748 | && strcmp(parent_dirname, dn) != 0) { | |
749 | dflt_perms = default_perms_for_dir(dn); | |
750 | parent_dirname = dn; | |
751 | } | |
752 | #endif | |
753 | file->mode = dest_mode(file->mode, st.st_mode, | |
754 | dflt_perms, exists); | |
4df9f368 AT |
755 | } |
756 | ||
48459ba1 | 757 | /* We now check to see if we are writing the file "inplace" */ |
a3221d2a | 758 | if (inplace) { |
97894c64 | 759 | fd2 = do_open(fname, O_WRONLY|O_CREAT, 0600); |
a3221d2a | 760 | if (fd2 == -1) { |
3f0211b6 | 761 | rsyserr(FERROR_XFER, errno, "open %s failed", |
dc55d7bd | 762 | full_fname(fname)); |
a3221d2a WD |
763 | } |
764 | } else { | |
2cce7545 WD |
765 | fd2 = open_tmpfile(fnametmp, fname, file); |
766 | if (fd2 != -1) | |
767 | cleanup_set(fnametmp, partialptr, file, fd1, fd2); | |
768 | } | |
a3221d2a | 769 | |
2cce7545 WD |
770 | if (fd2 == -1) { |
771 | discard_receive_data(f_in, F_LENGTH(file)); | |
772 | if (fd1 != -1) | |
773 | close(fd1); | |
774 | if (inc_recurse) | |
775 | send_msg_int(MSG_NO_SEND, ndx); | |
776 | continue; | |
a3221d2a | 777 | } |
2f03f956 | 778 | |
a8ed4958 | 779 | /* log the transfer */ |
910e89b9 | 780 | if (log_before_transfer) |
4dde3347 | 781 | log_item(FCLIENT, file, iflags, NULL); |
951e826b | 782 | else if (!am_server && INFO_GTE(NAME, 1) && INFO_EQ(PROGRESS, 1)) |
45c49b52 | 783 | rprintf(FINFO, "%s\n", fname); |
11a5a3c7 | 784 | |
2f03f956 | 785 | /* recv file data */ |
7e5fa372 | 786 | recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size, |
112d728f | 787 | fname, fd2, F_LENGTH(file)); |
1b7c47cb | 788 | |
4dde3347 | 789 | log_item(log_code, file, iflags, NULL); |
17fadf7d | 790 | |
d2a918b4 | 791 | if (fd1 != -1) |
2f03f956 | 792 | close(fd1); |
9f27cd8c | 793 | if (close(fd2) < 0) { |
d62bcc17 WD |
794 | rsyserr(FERROR, errno, "close failed on %s", |
795 | full_fname(fnametmp)); | |
9f27cd8c WD |
796 | exit_cleanup(RERR_FILEIO); |
797 | } | |
17fadf7d | 798 | |
68795c64 | 799 | if ((recv_ok && (!delay_updates || !partialptr)) || inplace) { |
01d124d9 | 800 | if (partialptr == fname) |
83235dbc WD |
801 | partialptr = NULL; |
802 | if (!finish_transfer(fname, fnametmp, fnamecmp, | |
803 | partialptr, file, recv_ok, 1)) | |
804 | recv_ok = -1; | |
805 | else if (fnamecmp == partialptr) { | |
aec6b9f8 WD |
806 | do_unlink(partialptr); |
807 | handle_partial_dir(partialptr, PDIR_DELETE); | |
808 | } | |
459abd75 WD |
809 | } else if (keep_partial && partialptr) { |
810 | if (!handle_partial_dir(partialptr, PDIR_CREATE)) { | |
811 | rprintf(FERROR, | |
812 | "Unable to create partial-dir for %s -- discarding %s.\n", | |
813 | local_name ? local_name : f_name(file, NULL), | |
814 | recv_ok ? "completed file" : "partial file"); | |
815 | do_unlink(fnametmp); | |
816 | recv_ok = -1; | |
817 | } else if (!finish_transfer(partialptr, fnametmp, fnamecmp, NULL, | |
818 | file, recv_ok, !partial_dir)) | |
3e7934a5 | 819 | recv_ok = -1; |
83235dbc WD |
820 | else if (delay_updates && recv_ok) { |
821 | bitbag_set_bit(delayed_bits, ndx); | |
822 | recv_ok = 2; | |
459abd75 WD |
823 | } else |
824 | partialptr = NULL; | |
825 | } else | |
55e50d89 | 826 | do_unlink(fnametmp); |
a7260c40 | 827 | |
2f03f956 | 828 | cleanup_disable(); |
554e0a8d | 829 | |
8ab02ccd WD |
830 | if (read_batch) |
831 | file->flags |= FLAG_FILE_SENT; | |
832 | ||
83235dbc | 833 | switch (recv_ok) { |
459abd75 WD |
834 | case 2: |
835 | break; | |
83235dbc | 836 | case 1: |
3ea6e0e7 | 837 | if (remove_source_files || inc_recurse |
112d728f | 838 | || (preserve_hard_links && F_IS_HLINKED(file))) |
0395130c | 839 | send_msg_int(MSG_SUCCESS, ndx); |
83235dbc WD |
840 | break; |
841 | case 0: { | |
964244b9 | 842 | enum logcode msgtype = redoing ? FERROR_XFER : FWARNING; |
951e826b | 843 | if (msgtype == FERROR_XFER || INFO_GTE(NAME, 1)) { |
a7260c40 WD |
844 | char *errstr, *redostr, *keptstr; |
845 | if (!(keep_partial && partialptr) && !inplace) | |
846 | keptstr = "discarded"; | |
847 | else if (partial_dir) | |
848 | keptstr = "put into partial-dir"; | |
849 | else | |
850 | keptstr = "retained"; | |
964244b9 | 851 | if (msgtype == FERROR_XFER) { |
007e3c0e WD |
852 | errstr = "ERROR"; |
853 | redostr = ""; | |
854 | } else { | |
855 | errstr = "WARNING"; | |
964244b9 WD |
856 | redostr = read_batch ? " (may try again)" |
857 | : " (will try again)"; | |
007e3c0e WD |
858 | } |
859 | rprintf(msgtype, | |
a7260c40 | 860 | "%s: %s failed verification -- update %s%s.\n", |
964244b9 WD |
861 | errstr, local_name ? f_name(file, NULL) : fname, |
862 | keptstr, redostr); | |
007e3c0e | 863 | } |
d0221f1d | 864 | if (!redoing) { |
8ab02ccd WD |
865 | if (read_batch) |
866 | flist_ndx_push(&batch_redo_list, ndx); | |
0395130c | 867 | send_msg_int(MSG_REDO, ndx); |
f3d6d480 | 868 | file->flags |= FLAG_FILE_SENT; |
3ea6e0e7 | 869 | } else if (inc_recurse) |
d0221f1d | 870 | send_msg_int(MSG_NO_SEND, ndx); |
83235dbc WD |
871 | break; |
872 | } | |
873 | case -1: | |
874 | if (inc_recurse) | |
875 | send_msg_int(MSG_NO_SEND, ndx); | |
876 | break; | |
2f03f956 AT |
877 | } |
878 | } | |
f3d6d480 WD |
879 | if (make_backups < 0) |
880 | make_backups = -make_backups; | |
2f03f956 | 881 | |
ac3f7b81 | 882 | if (phase == 2 && delay_updates) /* for protocol_version < 29 */ |
f3d6d480 | 883 | handle_delayed_updates(local_name); |
48e1c8c6 | 884 | |
951e826b | 885 | if (DEBUG_GTE(RECV, 1)) |
2f03f956 | 886 | rprintf(FINFO,"recv_files finished\n"); |
17fadf7d | 887 | |
2f03f956 AT |
888 | return 0; |
889 | } |