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