Commit | Line | Data |
---|---|---|
ef1aa910 | 1 | /* -*- c-file-style: "linux" -*- |
91262d5d MP |
2 | |
3 | rsync -- fast file replication program | |
2cda2560 WD |
4 | |
5 | Copyright (C) 1996-2000 by Andrew Tridgell | |
2f03f956 | 6 | Copyright (C) Paul Mackerras 1996 |
91262d5d | 7 | Copyright (C) 2002 by Martin Pool <mbp@samba.org> |
2cda2560 | 8 | |
2f03f956 AT |
9 | This program is free software; you can redistribute it and/or modify |
10 | it under the terms of the GNU General Public License as published by | |
11 | the Free Software Foundation; either version 2 of the License, or | |
12 | (at your option) any later version. | |
2cda2560 | 13 | |
2f03f956 AT |
14 | This program is distributed in the hope that it will be useful, |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
2cda2560 | 18 | |
2f03f956 AT |
19 | You should have received a copy of the GNU General Public License |
20 | along with this program; if not, write to the Free Software | |
21 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
22 | */ | |
23 | ||
24 | #include "rsync.h" | |
25 | ||
26 | extern int verbose; | |
27 | extern int dry_run; | |
28 | extern int relative_paths; | |
716e73d4 | 29 | extern int keep_dirlinks; |
2f03f956 AT |
30 | extern int preserve_links; |
31 | extern int am_root; | |
32 | extern int preserve_devices; | |
33 | extern int preserve_hard_links; | |
6744b62d WD |
34 | extern int preserve_perms; |
35 | extern int preserve_uid; | |
36 | extern int preserve_gid; | |
3ea9bbd6 WD |
37 | extern int preserve_times; |
38 | extern int omit_dir_times; | |
fa13f396 | 39 | extern int delete_during; |
2f03f956 | 40 | extern int update_only; |
3d6feada | 41 | extern int opt_ignore_existing; |
cd6aa5b5 WD |
42 | extern int inplace; |
43 | extern int make_backups; | |
2f03f956 AT |
44 | extern int csum_length; |
45 | extern int ignore_times; | |
f83f0548 | 46 | extern int size_only; |
7d1bfaf7 | 47 | extern OFF_T max_size; |
2f03f956 | 48 | extern int io_timeout; |
d04e9c51 | 49 | extern int protocol_version; |
2f03f956 | 50 | extern int always_checksum; |
a7260c40 | 51 | extern char *partial_dir; |
b7e8628c | 52 | extern char *basis_dir[]; |
2be2fb3e | 53 | extern int compare_dest; |
59c95e42 | 54 | extern int link_dest; |
5774786f WD |
55 | extern int whole_file; |
56 | extern int local_server; | |
5774786f | 57 | extern int list_only; |
b9f592fb | 58 | extern int read_batch; |
5774786f WD |
59 | extern int only_existing; |
60 | extern int orig_umask; | |
61 | extern int safe_symlinks; | |
a255c592 | 62 | extern long block_size; /* "long" because popt can't set an int32. */ |
2f03f956 | 63 | |
97f9dcae WD |
64 | extern struct exclude_list_struct server_exclude_list; |
65 | ||
b7e8628c | 66 | static int unchanged_attrs(struct file_struct *file, STRUCT_STAT *st) |
2f03f956 | 67 | { |
b7e8628c WD |
68 | if (preserve_perms |
69 | && (st->st_mode & CHMOD_BITS) != (file->mode & CHMOD_BITS)) | |
84acca07 | 70 | return 0; |
bb24028f | 71 | |
b7e8628c WD |
72 | if (am_root && preserve_uid && st->st_uid != file->uid) |
73 | return 0; | |
bb24028f | 74 | |
b7e8628c WD |
75 | if (preserve_gid && file->gid != GID_NONE && st->st_gid != file->gid) |
76 | return 0; | |
77 | ||
78 | return 1; | |
79 | } | |
80 | ||
81 | /* Perform our quick-check heuristic for determining if a file is unchanged. */ | |
82 | static int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st) | |
83 | { | |
84 | if (st->st_size != file->length) | |
85 | return 0; | |
59c95e42 | 86 | |
2cda2560 | 87 | /* if always checksum is set then we use the checksum instead |
2f03f956 AT |
88 | of the file time to determine whether to sync */ |
89 | if (always_checksum && S_ISREG(st->st_mode)) { | |
90 | char sum[MD4_SUM_LENGTH]; | |
b7e8628c | 91 | file_checksum(fn, sum, st->st_size); |
728d0922 | 92 | return memcmp(sum, file->u.sum, protocol_version < 21 ? 2 |
84acca07 | 93 | : MD4_SUM_LENGTH) == 0; |
2f03f956 AT |
94 | } |
95 | ||
cc1e997d | 96 | if (size_only) |
84acca07 | 97 | return 1; |
2f03f956 | 98 | |
cc1e997d | 99 | if (ignore_times) |
84acca07 | 100 | return 0; |
cc1e997d | 101 | |
84acca07 | 102 | return cmp_modtime(st->st_mtime, file->modtime) == 0; |
2f03f956 AT |
103 | } |
104 | ||
105 | ||
ec8290c8 | 106 | /* |
195bd906 | 107 | * set (initialize) the size entries in the per-file sum_struct |
ec8290c8 | 108 | * calculating dynamic block and checksum sizes. |
195bd906 | 109 | * |
ec8290c8 | 110 | * This is only called from generate_and_send_sums() but is a separate |
195bd906 S |
111 | * function to encapsulate the logic. |
112 | * | |
113 | * The block size is a rounded square root of file length. | |
114 | * | |
115 | * The checksum size is determined according to: | |
116 | * blocksum_bits = BLOCKSUM_EXP + 2*log2(file_len) - log2(block_len) | |
117 | * provided by Donovan Baarda which gives a probability of rsync | |
118 | * algorithm corrupting data and falling back using the whole md4 | |
119 | * checksums. | |
120 | * | |
121 | * This might be made one of several selectable heuristics. | |
122 | */ | |
1490812a | 123 | static void sum_sizes_sqroot(struct sum_struct *sum, int64 len) |
195bd906 | 124 | { |
a255c592 | 125 | int32 blength; |
da9d12f5 | 126 | int s2length; |
195bd906 | 127 | |
a255c592 | 128 | if (block_size) |
195bd906 | 129 | blength = block_size; |
a255c592 | 130 | else if (len <= BLOCK_SIZE * BLOCK_SIZE) |
195bd906 | 131 | blength = BLOCK_SIZE; |
a255c592 WD |
132 | else { |
133 | int32 c; | |
1490812a | 134 | int64 l; |
eae7165c WD |
135 | int cnt; |
136 | for (c = 1, l = len, cnt = 0; l >>= 2; c <<= 1, cnt++) {} | |
137 | if (cnt >= 31 || c >= MAX_BLOCK_SIZE) | |
138 | blength = MAX_BLOCK_SIZE; | |
139 | else { | |
140 | blength = 0; | |
141 | do { | |
142 | blength |= c; | |
1490812a | 143 | if (len < (int64)blength * blength) |
eae7165c WD |
144 | blength &= ~c; |
145 | c >>= 1; | |
146 | } while (c >= 8); /* round to multiple of 8 */ | |
147 | blength = MAX(blength, BLOCK_SIZE); | |
195bd906 | 148 | } |
195bd906 S |
149 | } |
150 | ||
d04e9c51 | 151 | if (protocol_version < 27) { |
195bd906 S |
152 | s2length = csum_length; |
153 | } else if (csum_length == SUM_LENGTH) { | |
154 | s2length = SUM_LENGTH; | |
155 | } else { | |
a255c592 | 156 | int32 c; |
1490812a | 157 | int64 l; |
da9d12f5 | 158 | int b = BLOCKSUM_BIAS; |
a255c592 WD |
159 | for (l = len; l >>= 1; b += 2) {} |
160 | for (c = blength; c >>= 1 && b; b--) {} | |
161 | /* add a bit, subtract rollsum, round up. */ | |
162 | s2length = (b + 1 - 32 + 7) / 8; /* --optimize in compiler-- */ | |
195bd906 S |
163 | s2length = MAX(s2length, csum_length); |
164 | s2length = MIN(s2length, SUM_LENGTH); | |
165 | } | |
166 | ||
167 | sum->flength = len; | |
168 | sum->blength = blength; | |
169 | sum->s2length = s2length; | |
170 | sum->count = (len + (blength - 1)) / blength; | |
171 | sum->remainder = (len % blength); | |
172 | ||
173 | if (sum->count && verbose > 2) { | |
a255c592 WD |
174 | rprintf(FINFO, |
175 | "count=%.0f rem=%ld blength=%ld s2length=%d flength=%.0f\n", | |
176 | (double)sum->count, (long)sum->remainder, (long)sum->blength, | |
da9d12f5 | 177 | sum->s2length, (double)sum->flength); |
195bd906 S |
178 | } |
179 | } | |
80605142 | 180 | |
bceec82f | 181 | |
80605142 WD |
182 | /* |
183 | * Generate and send a stream of signatures/checksums that describe a buffer | |
e66dfd18 | 184 | * |
80605142 WD |
185 | * Generate approximately one checksum every block_len bytes. |
186 | */ | |
cd6aa5b5 | 187 | static void generate_and_send_sums(int fd, OFF_T len, int f_out, int f_copy) |
2f03f956 | 188 | { |
80605142 | 189 | size_t i; |
6e45e1dd | 190 | struct map_struct *mapbuf; |
80605142 | 191 | struct sum_struct sum; |
2f03f956 AT |
192 | OFF_T offset = 0; |
193 | ||
423dba8e | 194 | sum_sizes_sqroot(&sum, len); |
e66dfd18 | 195 | |
6e45e1dd | 196 | if (len > 0) |
96d910c7 | 197 | mapbuf = map_file(fd, len, MAX_MAP_SIZE, sum.blength); |
6e45e1dd WD |
198 | else |
199 | mapbuf = NULL; | |
200 | ||
fc0257c9 | 201 | write_sum_head(f_out, &sum); |
2f03f956 | 202 | |
80605142 | 203 | for (i = 0; i < sum.count; i++) { |
a255c592 | 204 | int32 n1 = (int32)MIN(len, (OFF_T)sum.blength); |
6e45e1dd | 205 | char *map = map_ptr(mapbuf, offset, n1); |
80605142 WD |
206 | uint32 sum1 = get_checksum1(map, n1); |
207 | char sum2[SUM_LENGTH]; | |
2f03f956 | 208 | |
cd6aa5b5 WD |
209 | if (f_copy >= 0) |
210 | full_write(f_copy, map, n1); | |
211 | ||
80605142 | 212 | get_checksum2(map, n1, sum2); |
2f03f956 | 213 | |
80605142 | 214 | if (verbose > 3) { |
e66dfd18 | 215 | rprintf(FINFO, |
a255c592 WD |
216 | "chunk[%.0f] offset=%.0f len=%ld sum1=%08lx\n", |
217 | (double)i, (double)offset, (long)n1, | |
0e36d9da | 218 | (unsigned long)sum1); |
80605142 WD |
219 | } |
220 | write_int(f_out, sum1); | |
fc0257c9 | 221 | write_buf(f_out, sum2, sum.s2length); |
2f03f956 AT |
222 | len -= n1; |
223 | offset += n1; | |
224 | } | |
6e45e1dd WD |
225 | |
226 | if (mapbuf) | |
227 | unmap_file(mapbuf); | |
2f03f956 AT |
228 | } |
229 | ||
230 | ||
fd322eef | 231 | /* |
420ef2c4 | 232 | * Acts on file number @p i from @p flist, whose name is @p fname. |
ef1aa910 MP |
233 | * |
234 | * First fixes up permissions, then generates checksums for the file. | |
235 | * | |
420ef2c4 MP |
236 | * @note This comment was added later by mbp who was trying to work it |
237 | * out. It might be wrong. | |
fd322eef | 238 | */ |
fa13f396 WD |
239 | static void recv_generator(char *fname, struct file_list *flist, |
240 | struct file_struct *file, int i, | |
41cfde6b | 241 | int f_out, int f_out_name) |
2cda2560 | 242 | { |
41cfde6b | 243 | int fd = -1, f_copy = -1; |
89f7eff3 | 244 | STRUCT_STAT st, partial_st; |
41cfde6b | 245 | struct file_struct *back_file = NULL; |
e7d13fe5 | 246 | int statret, stat_errno; |
41cfde6b | 247 | char *fnamecmp, *partialptr, *backupptr = NULL; |
375a4556 | 248 | char fnamecmpbuf[MAXPATHLEN]; |
41cfde6b | 249 | uchar fnamecmp_type; |
f7632fc6 | 250 | |
dfd5ba6a WD |
251 | if (list_only) |
252 | return; | |
2f03f956 AT |
253 | |
254 | if (verbose > 2) | |
ecc81fce | 255 | rprintf(FINFO, "recv_generator(%s,%d)\n", safe_fname(fname), i); |
2f03f956 | 256 | |
97f9dcae WD |
257 | if (server_exclude_list.head |
258 | && check_exclude(&server_exclude_list, fname, | |
3e35c34b WD |
259 | S_ISDIR(file->mode)) < 0) { |
260 | if (verbose) { | |
261 | rprintf(FINFO, "skipping server-excluded file \"%s\"\n", | |
ecc81fce | 262 | safe_fname(fname)); |
3e35c34b | 263 | } |
97f9dcae | 264 | return; |
3e35c34b | 265 | } |
97f9dcae | 266 | |
73f7af0e WD |
267 | if (dry_run > 1) { |
268 | statret = -1; | |
269 | stat_errno = ENOENT; | |
270 | } else { | |
271 | statret = link_stat(fname, &st, | |
272 | keep_dirlinks && S_ISDIR(file->mode)); | |
273 | stat_errno = errno; | |
274 | } | |
63787382 | 275 | |
a7260c40 | 276 | if (only_existing && statret == -1 && stat_errno == ENOENT) { |
1347d512 | 277 | /* we only want to update existing files */ |
ecc81fce WD |
278 | if (verbose > 1) { |
279 | rprintf(FINFO, "not creating new file \"%s\"\n", | |
280 | safe_fname(fname)); | |
281 | } | |
1347d512 AT |
282 | return; |
283 | } | |
284 | ||
d9b4d267 WD |
285 | if (statret == 0 && !preserve_perms |
286 | && S_ISDIR(st.st_mode) == S_ISDIR(file->mode)) { | |
4df9f368 | 287 | /* if the file exists already and we aren't perserving |
2cda2560 WD |
288 | * permissions then act as though the remote end sent |
289 | * us the file permissions we already have */ | |
67e78a82 WD |
290 | file->mode = (file->mode & ~CHMOD_BITS) |
291 | | (st.st_mode & CHMOD_BITS); | |
4df9f368 AT |
292 | } |
293 | ||
2f03f956 | 294 | if (S_ISDIR(file->mode)) { |
2cda2560 WD |
295 | /* The file to be received is a directory, so we need |
296 | * to prepare appropriately. If there is already a | |
297 | * file of that name and it is *not* a directory, then | |
298 | * we need to delete it. If it doesn't exist, then | |
027428eb | 299 | * (perhaps recursively) create it. */ |
2f03f956 | 300 | if (statret == 0 && !S_ISDIR(st.st_mode)) { |
7e38410e | 301 | delete_file(fname, DEL_TERSE); |
2f03f956 AT |
302 | statret = -1; |
303 | } | |
304 | if (statret != 0 && do_mkdir(fname,file->mode) != 0 && errno != EEXIST) { | |
027428eb WD |
305 | if (!relative_paths || errno != ENOENT |
306 | || create_directory_path(fname, orig_umask) < 0 | |
307 | || do_mkdir(fname, file->mode) < 0) { | |
d62bcc17 WD |
308 | rsyserr(FERROR, errno, |
309 | "recv_generator: mkdir %s failed", | |
310 | full_fname(fname)); | |
2f03f956 AT |
311 | } |
312 | } | |
716e73d4 WD |
313 | /* f_out is set to -1 when doing final directory-permission |
314 | * and modification-time repair. */ | |
315 | if (set_perms(fname, file, statret ? NULL : &st, 0) | |
316 | && verbose && f_out != -1) | |
ecc81fce | 317 | rprintf(FINFO, "%s/\n", safe_fname(fname)); |
fa13f396 WD |
318 | if (delete_during && f_out != -1 |
319 | && (file->flags & FLAG_DEL_START)) | |
320 | delete_in_dir(flist, fname); | |
2f03f956 | 321 | return; |
7d1bfaf7 WD |
322 | } else if (max_size && file->length > max_size) { |
323 | if (verbose > 1) | |
324 | rprintf(FINFO, "%s is over max-size\n", fname); | |
325 | return; | |
2f03f956 AT |
326 | } |
327 | ||
328 | if (preserve_links && S_ISLNK(file->mode)) { | |
329 | #if SUPPORT_LINKS | |
728d0922 | 330 | if (safe_symlinks && unsafe_symlink(file->u.link, fname)) { |
2f03f956 | 331 | if (verbose) { |
ea42541f | 332 | rprintf(FINFO, "ignoring unsafe symlink %s -> \"%s\"\n", |
728d0922 | 333 | full_fname(fname), file->u.link); |
2f03f956 AT |
334 | } |
335 | return; | |
336 | } | |
337 | if (statret == 0) { | |
7e38410e WD |
338 | int dflag = S_ISDIR(st.st_mode) ? DEL_DIR : 0; |
339 | char lnk[MAXPATHLEN]; | |
340 | int len; | |
341 | ||
342 | if (!dflag | |
343 | && (len = readlink(fname, lnk, MAXPATHLEN-1)) > 0) { | |
344 | lnk[len] = 0; | |
85d4d142 MP |
345 | /* A link already pointing to the |
346 | * right place -- no further action | |
347 | * required. */ | |
7e38410e | 348 | if (strcmp(lnk, file->u.link) == 0) { |
c41b52c4 WD |
349 | set_perms(fname, file, &st, |
350 | PERMS_REPORT); | |
2f03f956 AT |
351 | return; |
352 | } | |
2cda2560 | 353 | } |
7e38410e WD |
354 | /* Not the right symlink (or not a symlink), so |
355 | * delete it. */ | |
356 | delete_file(fname, dflag | DEL_TERSE); | |
2f03f956 | 357 | } |
728d0922 | 358 | if (do_symlink(file->u.link,fname) != 0) { |
d62bcc17 | 359 | rsyserr(FERROR, errno, "symlink %s -> \"%s\" failed", |
ecc81fce | 360 | full_fname(fname), safe_fname(file->u.link)); |
2f03f956 AT |
361 | } else { |
362 | set_perms(fname,file,NULL,0); | |
363 | if (verbose) { | |
ecc81fce WD |
364 | rprintf(FINFO, "%s -> %s\n", safe_fname(fname), |
365 | safe_fname(file->u.link)); | |
2f03f956 AT |
366 | } |
367 | } | |
368 | #endif | |
369 | return; | |
370 | } | |
371 | ||
2f03f956 | 372 | if (am_root && preserve_devices && IS_DEVICE(file->mode)) { |
2cda2560 | 373 | if (statret != 0 || |
2f03f956 | 374 | st.st_mode != file->mode || |
3915fd75 | 375 | st.st_rdev != file->u.rdev) { |
7e38410e WD |
376 | int dflag = S_ISDIR(st.st_mode) ? DEL_DIR : 0; |
377 | delete_file(fname, dflag | DEL_TERSE); | |
d62bcc17 | 378 | if (verbose > 2) { |
2f03f956 | 379 | rprintf(FINFO,"mknod(%s,0%o,0x%x)\n", |
ecc81fce WD |
380 | safe_fname(fname), |
381 | (int)file->mode, (int)file->u.rdev); | |
d62bcc17 | 382 | } |
728d0922 | 383 | if (do_mknod(fname,file->mode,file->u.rdev) != 0) { |
d62bcc17 WD |
384 | rsyserr(FERROR, errno, "mknod %s failed", |
385 | full_fname(fname)); | |
2f03f956 AT |
386 | } else { |
387 | set_perms(fname,file,NULL,0); | |
ecc81fce WD |
388 | if (verbose) { |
389 | rprintf(FINFO, "%s\n", | |
390 | safe_fname(fname)); | |
391 | } | |
2f03f956 AT |
392 | } |
393 | } else { | |
c41b52c4 | 394 | set_perms(fname, file, &st, PERMS_REPORT); |
2f03f956 AT |
395 | } |
396 | return; | |
397 | } | |
2f03f956 | 398 | |
6dff5992 | 399 | if (preserve_hard_links && hard_link_check(file, HL_CHECK_MASTER)) |
2f03f956 | 400 | return; |
2f03f956 AT |
401 | |
402 | if (!S_ISREG(file->mode)) { | |
ecc81fce WD |
403 | rprintf(FINFO, "skipping non-regular file \"%s\"\n", |
404 | safe_fname(fname)); | |
2f03f956 AT |
405 | return; |
406 | } | |
407 | ||
375a4556 | 408 | fnamecmp = fname; |
41cfde6b | 409 | fnamecmp_type = FNAMECMP_FNAME; |
375a4556 | 410 | |
b7e8628c WD |
411 | if (statret == -1 && basis_dir[0] != NULL) { |
412 | int fallback_match = -1; | |
413 | int match_level = 0; | |
414 | int i = 0; | |
415 | do { | |
416 | pathjoin(fnamecmpbuf, sizeof fnamecmpbuf, | |
417 | basis_dir[i], fname); | |
418 | if (link_stat(fnamecmpbuf, &st, 0) == 0 | |
419 | && S_ISREG(st.st_mode)) { | |
420 | statret = 0; | |
421 | if (link_dest) { | |
422 | if (!match_level) { | |
423 | fallback_match = i; | |
424 | match_level = 1; | |
425 | } else if (match_level == 2 | |
426 | && !unchanged_attrs(file, &st)) | |
427 | continue; | |
428 | if (!unchanged_file(fnamecmpbuf, file, &st)) | |
429 | continue; | |
430 | fallback_match = i; | |
431 | match_level = 2; | |
432 | if (!unchanged_attrs(file, &st)) | |
433 | continue; | |
434 | } | |
435 | match_level = 3; | |
436 | break; | |
437 | } | |
438 | } while (basis_dir[++i] != NULL); | |
439 | if (statret == 0) { | |
440 | if (match_level < 3) { | |
441 | i = fallback_match; | |
442 | pathjoin(fnamecmpbuf, sizeof fnamecmpbuf, | |
443 | basis_dir[i], fname); | |
444 | } | |
59c95e42 | 445 | #if HAVE_LINK |
b7e8628c | 446 | if (link_dest && match_level == 3 && !dry_run) { |
e7d13fe5 WD |
447 | if (do_link(fnamecmpbuf, fname) < 0) { |
448 | if (verbose) { | |
449 | rsyserr(FINFO, errno, | |
450 | "link %s => %s", | |
451 | fnamecmpbuf, | |
452 | safe_fname(fname)); | |
453 | } | |
454 | fnamecmp = fnamecmpbuf; | |
2be2fb3e | 455 | fnamecmp_type = i; |
e7bc9b64 | 456 | } |
e7d13fe5 | 457 | } else |
59c95e42 | 458 | #endif |
41cfde6b | 459 | { |
e7d13fe5 | 460 | fnamecmp = fnamecmpbuf; |
2be2fb3e | 461 | fnamecmp_type = i; |
41cfde6b | 462 | } |
e7d13fe5 WD |
463 | } |
464 | } | |
465 | ||
466 | if (statret == 0 && !S_ISREG(st.st_mode)) { | |
7e38410e WD |
467 | int dflag = S_ISDIR(st.st_mode) ? DEL_DIR : 0; |
468 | if (delete_file(fname, dflag | DEL_TERSE) != 0) | |
e7d13fe5 WD |
469 | return; |
470 | statret = -1; | |
471 | stat_errno = ENOENT; | |
375a4556 DD |
472 | } |
473 | ||
9d954dca | 474 | if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL |
72c19bb3 WD |
475 | && link_stat(partialptr, &partial_st, 0) == 0 |
476 | && S_ISREG(partial_st.st_mode)) { | |
477 | if (statret == -1) | |
478 | goto prepare_to_open; | |
479 | } else | |
480 | partialptr = NULL; | |
89f7eff3 | 481 | |
2f03f956 | 482 | if (statret == -1) { |
6dff5992 WD |
483 | if (preserve_hard_links && hard_link_check(file, HL_SKIP)) |
484 | return; | |
41cfde6b WD |
485 | if (stat_errno == ENOENT) |
486 | goto notify_others; | |
487 | if (verbose > 1) { | |
e7d13fe5 WD |
488 | rsyserr(FERROR, stat_errno, |
489 | "recv_generator: failed to stat %s", | |
d62bcc17 | 490 | full_fname(fname)); |
2f03f956 AT |
491 | } |
492 | return; | |
493 | } | |
494 | ||
41cfde6b | 495 | if (opt_ignore_existing && fnamecmp_type == FNAMECMP_FNAME) { |
3d6feada | 496 | if (verbose > 1) |
ecc81fce | 497 | rprintf(FINFO, "%s exists\n", safe_fname(fname)); |
3d6feada | 498 | return; |
2cda2560 | 499 | } |
3d6feada | 500 | |
41cfde6b | 501 | if (update_only && fnamecmp_type == FNAMECMP_FNAME |
d3a4375f | 502 | && cmp_modtime(st.st_mtime, file->modtime) > 0) { |
2f03f956 | 503 | if (verbose > 1) |
ecc81fce | 504 | rprintf(FINFO, "%s is newer\n", safe_fname(fname)); |
2f03f956 AT |
505 | return; |
506 | } | |
507 | ||
2be2fb3e | 508 | if (!compare_dest && fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH) |
b7e8628c WD |
509 | ; |
510 | else if (unchanged_file(fnamecmp, file, &st)) { | |
41cfde6b | 511 | if (fnamecmp_type == FNAMECMP_FNAME) |
c41b52c4 | 512 | set_perms(fname, file, &st, PERMS_REPORT); |
2f03f956 AT |
513 | return; |
514 | } | |
515 | ||
89f7eff3 | 516 | prepare_to_open: |
9d954dca WD |
517 | if (partialptr) { |
518 | st = partial_st; | |
519 | fnamecmp = partialptr; | |
520 | fnamecmp_type = FNAMECMP_PARTIAL_DIR; | |
521 | statret = 0; | |
522 | } | |
523 | ||
41cfde6b WD |
524 | if (dry_run || whole_file > 0) { |
525 | statret = -1; | |
526 | goto notify_others; | |
2f03f956 | 527 | } |
41cfde6b WD |
528 | if (read_batch) |
529 | goto notify_others; | |
2f03f956 | 530 | |
2cda2560 | 531 | /* open the file */ |
8c9fd200 | 532 | fd = do_open(fnamecmp, O_RDONLY, 0); |
2f03f956 AT |
533 | |
534 | if (fd == -1) { | |
d62bcc17 WD |
535 | rsyserr(FERROR, errno, "failed to open %s, continuing", |
536 | full_fname(fnamecmp)); | |
cd6aa5b5 | 537 | pretend_missing: |
60be6acf | 538 | /* pretend the file didn't exist */ |
6dff5992 WD |
539 | if (preserve_hard_links && hard_link_check(file, HL_SKIP)) |
540 | return; | |
41cfde6b WD |
541 | statret = -1; |
542 | goto notify_others; | |
2f03f956 AT |
543 | } |
544 | ||
cd6aa5b5 WD |
545 | if (inplace && make_backups) { |
546 | if (!(backupptr = get_backup_name(fname))) { | |
547 | close(fd); | |
548 | return; | |
549 | } | |
550 | if (!(back_file = make_file(fname, NULL, NO_EXCLUDES))) { | |
551 | close(fd); | |
552 | goto pretend_missing; | |
553 | } | |
554 | if (robust_unlink(backupptr) && errno != ENOENT) { | |
555 | rsyserr(FERROR, errno, "unlink %s", | |
556 | full_fname(backupptr)); | |
557 | free(back_file); | |
558 | close(fd); | |
559 | return; | |
560 | } | |
561 | if ((f_copy = do_open(backupptr, | |
562 | O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0) { | |
563 | rsyserr(FERROR, errno, "open %s", | |
564 | full_fname(backupptr)); | |
565 | free(back_file); | |
566 | close(fd); | |
567 | return; | |
568 | } | |
41cfde6b | 569 | fnamecmp_type = FNAMECMP_BACKUP; |
cd6aa5b5 WD |
570 | } |
571 | ||
dfd5ba6a | 572 | if (verbose > 3) { |
ecc81fce WD |
573 | rprintf(FINFO, "gen mapped %s of size %.0f\n", |
574 | safe_fname(fnamecmp), (double)st.st_size); | |
dfd5ba6a | 575 | } |
2f03f956 | 576 | |
2f03f956 | 577 | if (verbose > 2) |
80605142 | 578 | rprintf(FINFO, "generating and sending sums for %d\n", i); |
2f03f956 | 579 | |
41cfde6b WD |
580 | notify_others: |
581 | write_int(f_out, i); | |
17b5b32f WD |
582 | if (protocol_version >= 29 && inplace && !read_batch) |
583 | write_byte(f_out, fnamecmp_type); | |
41cfde6b WD |
584 | if (f_out_name >= 0) |
585 | write_byte(f_out_name, fnamecmp_type); | |
cd6aa5b5 | 586 | |
41cfde6b WD |
587 | if (dry_run || read_batch) |
588 | return; | |
589 | ||
590 | if (statret == 0) { | |
591 | generate_and_send_sums(fd, st.st_size, f_out, f_copy); | |
2f03f956 | 592 | |
41cfde6b WD |
593 | if (f_copy >= 0) { |
594 | close(f_copy); | |
595 | set_perms(backupptr, back_file, NULL, 0); | |
596 | if (verbose > 1) { | |
597 | rprintf(FINFO, "backed up %s to %s\n", | |
598 | fname, backupptr); | |
599 | } | |
600 | free(back_file); | |
601 | } | |
602 | ||
603 | close(fd); | |
604 | } else | |
605 | write_sum_head(f_out, NULL); | |
2f03f956 AT |
606 | } |
607 | ||
608 | ||
41cfde6b WD |
609 | void generate_files(int f_out, struct file_list *flist, char *local_name, |
610 | int f_out_name) | |
2f03f956 AT |
611 | { |
612 | int i; | |
e1f67417 | 613 | int phase = 0; |
968c8030 | 614 | char fbuf[MAXPATHLEN]; |
3ea9bbd6 WD |
615 | int need_retouch_dir_times = preserve_times && !omit_dir_times; |
616 | int need_retouch_dir_perms = 0; | |
2f03f956 | 617 | |
45e08edb WD |
618 | if (verbose > 2) { |
619 | rprintf(FINFO, "generator starting pid=%ld count=%d\n", | |
620 | (long)getpid(), flist->count); | |
621 | } | |
2f03f956 | 622 | |
3e7053ac MP |
623 | if (verbose >= 2) { |
624 | rprintf(FINFO, | |
f38bd4a0 | 625 | whole_file > 0 |
3e7053ac MP |
626 | ? "delta-transmission disabled for local transfer or --whole-file\n" |
627 | : "delta transmission enabled\n"); | |
628 | } | |
2cda2560 | 629 | |
a57873b7 AT |
630 | /* we expect to just sit around now, so don't exit on a |
631 | timeout. If we really get a timeout then the other process should | |
632 | exit */ | |
633 | io_timeout = 0; | |
634 | ||
2f03f956 AT |
635 | for (i = 0; i < flist->count; i++) { |
636 | struct file_struct *file = flist->files[i]; | |
dfd5ba6a | 637 | struct file_struct copy; |
2f03f956 | 638 | |
dfd5ba6a WD |
639 | if (!file->basename) |
640 | continue; | |
2f03f956 AT |
641 | /* we need to ensure that any directories we create have writeable |
642 | permissions initially so that we can create the files within | |
643 | them. This is then fixed after the files are transferred */ | |
dfd5ba6a WD |
644 | if (!am_root && S_ISDIR(file->mode) && !(file->mode & S_IWUSR)) { |
645 | copy = *file; | |
dfd5ba6a WD |
646 | copy.mode |= S_IWUSR; /* user write */ |
647 | file = © | |
3ea9bbd6 | 648 | need_retouch_dir_perms = 1; |
2f03f956 AT |
649 | } |
650 | ||
3fef5364 | 651 | recv_generator(local_name ? local_name : f_name_to(file, fbuf), |
fa13f396 | 652 | flist, file, i, f_out, f_out_name); |
2f03f956 AT |
653 | } |
654 | ||
655 | phase++; | |
656 | csum_length = SUM_LENGTH; | |
e1f67417 | 657 | ignore_times = 1; |
2f03f956 AT |
658 | |
659 | if (verbose > 2) | |
660 | rprintf(FINFO,"generate_files phase=%d\n",phase); | |
661 | ||
7daccb8e | 662 | write_int(f_out, -1); |
2f03f956 | 663 | |
bc63ae3f S |
664 | /* files can cycle through the system more than once |
665 | * to catch initial checksum errors */ | |
b9b15fb1 | 666 | while ((i = get_redo_num()) != -1) { |
bc63ae3f | 667 | struct file_struct *file = flist->files[i]; |
3fef5364 | 668 | recv_generator(local_name ? local_name : f_name_to(file, fbuf), |
fa13f396 | 669 | flist, file, i, f_out, f_out_name); |
bc63ae3f | 670 | } |
2f03f956 | 671 | |
bc63ae3f S |
672 | phase++; |
673 | if (verbose > 2) | |
674 | rprintf(FINFO,"generate_files phase=%d\n",phase); | |
2f03f956 | 675 | |
7daccb8e | 676 | write_int(f_out, -1); |
6dff5992 | 677 | |
b0da4b23 WD |
678 | /* Read post-redo-phase MSG_DONE and any prior messages. */ |
679 | get_redo_num(); | |
680 | ||
6dff5992 WD |
681 | if (preserve_hard_links) |
682 | do_hard_links(); | |
683 | ||
3ea9bbd6 WD |
684 | if (need_retouch_dir_perms || need_retouch_dir_times) { |
685 | /* Now we need to fix any directory permissions that were | |
686 | * modified during the transfer and/or re-set any tweaked | |
687 | * modified-time values. */ | |
688 | for (i = 0; i < flist->count; i++) { | |
689 | struct file_struct *file = flist->files[i]; | |
690 | if (!file->basename || !S_ISDIR(file->mode)) | |
691 | continue; | |
692 | if (!need_retouch_dir_times && file->mode & S_IWUSR) | |
693 | continue; | |
694 | recv_generator(local_name ? local_name : f_name(file), | |
695 | flist, file, i, -1, -1); | |
696 | } | |
6dff5992 WD |
697 | } |
698 | ||
699 | if (verbose > 2) | |
700 | rprintf(FINFO,"generate_files finished\n"); | |
2f03f956 | 701 | } |