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; | |
6c3862fa WD |
28 | extern int log_format_has_i; |
29 | extern int log_format_has_o_or_i; | |
30 | extern int daemon_log_format_has_i; | |
31 | extern int am_root; | |
32 | extern int am_server; | |
33 | extern int am_daemon; | |
2f03f956 | 34 | extern int relative_paths; |
716e73d4 | 35 | extern int keep_dirlinks; |
2f03f956 | 36 | extern int preserve_links; |
2f03f956 AT |
37 | extern int preserve_devices; |
38 | extern int preserve_hard_links; | |
6744b62d WD |
39 | extern int preserve_perms; |
40 | extern int preserve_uid; | |
41 | extern int preserve_gid; | |
3ea9bbd6 WD |
42 | extern int preserve_times; |
43 | extern int omit_dir_times; | |
fa13f396 | 44 | extern int delete_during; |
2f03f956 | 45 | extern int update_only; |
3d6feada | 46 | extern int opt_ignore_existing; |
cd6aa5b5 WD |
47 | extern int inplace; |
48 | extern int make_backups; | |
2f03f956 AT |
49 | extern int csum_length; |
50 | extern int ignore_times; | |
f83f0548 | 51 | extern int size_only; |
7d1bfaf7 | 52 | extern OFF_T max_size; |
2f03f956 | 53 | extern int io_timeout; |
d04e9c51 | 54 | extern int protocol_version; |
8e85be0a | 55 | extern int fuzzy_basis; |
2f03f956 | 56 | extern int always_checksum; |
a7260c40 | 57 | extern char *partial_dir; |
b7e8628c | 58 | extern char *basis_dir[]; |
2be2fb3e | 59 | extern int compare_dest; |
59c95e42 | 60 | extern int link_dest; |
5774786f WD |
61 | extern int whole_file; |
62 | extern int local_server; | |
5774786f | 63 | extern int list_only; |
b9f592fb | 64 | extern int read_batch; |
5774786f WD |
65 | extern int only_existing; |
66 | extern int orig_umask; | |
67 | extern int safe_symlinks; | |
a255c592 | 68 | extern long block_size; /* "long" because popt can't set an int32. */ |
6c3862fa | 69 | extern struct stats stats; |
2f03f956 | 70 | |
7842418b | 71 | extern struct filter_list_struct server_filter_list; |
97f9dcae | 72 | |
b7e8628c | 73 | static int unchanged_attrs(struct file_struct *file, STRUCT_STAT *st) |
2f03f956 | 74 | { |
b7e8628c WD |
75 | if (preserve_perms |
76 | && (st->st_mode & CHMOD_BITS) != (file->mode & CHMOD_BITS)) | |
84acca07 | 77 | return 0; |
bb24028f | 78 | |
b7e8628c WD |
79 | if (am_root && preserve_uid && st->st_uid != file->uid) |
80 | return 0; | |
bb24028f | 81 | |
b7e8628c WD |
82 | if (preserve_gid && file->gid != GID_NONE && st->st_gid != file->gid) |
83 | return 0; | |
84 | ||
85 | return 1; | |
86 | } | |
87 | ||
06a1dbad | 88 | |
c557eb8c WD |
89 | #define SID_UPDATING ITEM_UPDATING |
90 | #define SID_REPORT_CHECKSUM ITEM_REPORT_CHECKSUM | |
91 | #define SID_NO_DEST_AND_NO_UPDATE (1<<16) | |
06a1dbad | 92 | |
c557eb8c WD |
93 | static void itemize(struct file_struct *file, int statret, STRUCT_STAT *st, |
94 | int32 sflags, int f_out, int ndx) | |
06a1dbad | 95 | { |
c557eb8c | 96 | int iflags = sflags & (SID_UPDATING | SID_REPORT_CHECKSUM); |
06a1dbad | 97 | |
c557eb8c WD |
98 | if (statret >= 0) { |
99 | if (S_ISREG(file->mode) && file->length != st->st_size) | |
100 | iflags |= ITEM_REPORT_SIZE; | |
88b218fa WD |
101 | if (!(sflags & SID_NO_DEST_AND_NO_UPDATE)) { |
102 | int keep_time = !preserve_times ? 0 | |
103 | : S_ISDIR(file->mode) ? !omit_dir_times | |
104 | : !S_ISLNK(file->mode); | |
105 | ||
106 | if ((iflags & ITEM_UPDATING && !keep_time) | |
107 | || (keep_time && file->modtime != st->st_mtime)) | |
108 | iflags |= ITEM_REPORT_TIME; | |
109 | if (preserve_perms && file->mode != st->st_mode) | |
110 | iflags |= ITEM_REPORT_PERMS; | |
111 | if (preserve_uid && am_root && file->uid != st->st_uid) | |
112 | iflags |= ITEM_REPORT_OWNER; | |
113 | if (preserve_gid && file->gid != GID_NONE | |
114 | && st->st_gid != file->gid) | |
115 | iflags |= ITEM_REPORT_GROUP; | |
116 | } | |
c557eb8c | 117 | } else |
88b218fa | 118 | iflags |= ITEM_IS_NEW | ITEM_UPDATING; |
c557eb8c | 119 | |
8a8356b7 | 120 | if ((iflags || verbose > 1) && !read_batch) { |
6c3862fa WD |
121 | if (protocol_version >= 29) { |
122 | if (ndx >= 0) | |
123 | write_int(f_out, ndx); | |
124 | write_shortint(f_out, iflags); | |
125 | } else if (ndx >= 0) | |
126 | log_recv(file, &stats, iflags); | |
06a1dbad WD |
127 | } |
128 | } | |
129 | ||
130 | ||
b7e8628c WD |
131 | /* Perform our quick-check heuristic for determining if a file is unchanged. */ |
132 | static int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st) | |
133 | { | |
134 | if (st->st_size != file->length) | |
135 | return 0; | |
59c95e42 | 136 | |
2cda2560 | 137 | /* if always checksum is set then we use the checksum instead |
2f03f956 AT |
138 | of the file time to determine whether to sync */ |
139 | if (always_checksum && S_ISREG(st->st_mode)) { | |
140 | char sum[MD4_SUM_LENGTH]; | |
b7e8628c | 141 | file_checksum(fn, sum, st->st_size); |
728d0922 | 142 | return memcmp(sum, file->u.sum, protocol_version < 21 ? 2 |
84acca07 | 143 | : MD4_SUM_LENGTH) == 0; |
2f03f956 AT |
144 | } |
145 | ||
cc1e997d | 146 | if (size_only) |
84acca07 | 147 | return 1; |
2f03f956 | 148 | |
cc1e997d | 149 | if (ignore_times) |
84acca07 | 150 | return 0; |
cc1e997d | 151 | |
84acca07 | 152 | return cmp_modtime(st->st_mtime, file->modtime) == 0; |
2f03f956 AT |
153 | } |
154 | ||
155 | ||
ec8290c8 | 156 | /* |
195bd906 | 157 | * set (initialize) the size entries in the per-file sum_struct |
ec8290c8 | 158 | * calculating dynamic block and checksum sizes. |
195bd906 | 159 | * |
ec8290c8 | 160 | * This is only called from generate_and_send_sums() but is a separate |
195bd906 S |
161 | * function to encapsulate the logic. |
162 | * | |
163 | * The block size is a rounded square root of file length. | |
164 | * | |
165 | * The checksum size is determined according to: | |
166 | * blocksum_bits = BLOCKSUM_EXP + 2*log2(file_len) - log2(block_len) | |
167 | * provided by Donovan Baarda which gives a probability of rsync | |
168 | * algorithm corrupting data and falling back using the whole md4 | |
169 | * checksums. | |
170 | * | |
171 | * This might be made one of several selectable heuristics. | |
172 | */ | |
1490812a | 173 | static void sum_sizes_sqroot(struct sum_struct *sum, int64 len) |
195bd906 | 174 | { |
a255c592 | 175 | int32 blength; |
da9d12f5 | 176 | int s2length; |
195bd906 | 177 | |
a255c592 | 178 | if (block_size) |
195bd906 | 179 | blength = block_size; |
a255c592 | 180 | else if (len <= BLOCK_SIZE * BLOCK_SIZE) |
195bd906 | 181 | blength = BLOCK_SIZE; |
a255c592 WD |
182 | else { |
183 | int32 c; | |
1490812a | 184 | int64 l; |
eae7165c WD |
185 | int cnt; |
186 | for (c = 1, l = len, cnt = 0; l >>= 2; c <<= 1, cnt++) {} | |
187 | if (cnt >= 31 || c >= MAX_BLOCK_SIZE) | |
188 | blength = MAX_BLOCK_SIZE; | |
189 | else { | |
190 | blength = 0; | |
191 | do { | |
192 | blength |= c; | |
1490812a | 193 | if (len < (int64)blength * blength) |
eae7165c WD |
194 | blength &= ~c; |
195 | c >>= 1; | |
196 | } while (c >= 8); /* round to multiple of 8 */ | |
197 | blength = MAX(blength, BLOCK_SIZE); | |
195bd906 | 198 | } |
195bd906 S |
199 | } |
200 | ||
d04e9c51 | 201 | if (protocol_version < 27) { |
195bd906 S |
202 | s2length = csum_length; |
203 | } else if (csum_length == SUM_LENGTH) { | |
204 | s2length = SUM_LENGTH; | |
205 | } else { | |
a255c592 | 206 | int32 c; |
1490812a | 207 | int64 l; |
da9d12f5 | 208 | int b = BLOCKSUM_BIAS; |
a255c592 WD |
209 | for (l = len; l >>= 1; b += 2) {} |
210 | for (c = blength; c >>= 1 && b; b--) {} | |
211 | /* add a bit, subtract rollsum, round up. */ | |
212 | s2length = (b + 1 - 32 + 7) / 8; /* --optimize in compiler-- */ | |
195bd906 S |
213 | s2length = MAX(s2length, csum_length); |
214 | s2length = MIN(s2length, SUM_LENGTH); | |
215 | } | |
216 | ||
217 | sum->flength = len; | |
218 | sum->blength = blength; | |
219 | sum->s2length = s2length; | |
220 | sum->count = (len + (blength - 1)) / blength; | |
221 | sum->remainder = (len % blength); | |
222 | ||
223 | if (sum->count && verbose > 2) { | |
a255c592 WD |
224 | rprintf(FINFO, |
225 | "count=%.0f rem=%ld blength=%ld s2length=%d flength=%.0f\n", | |
226 | (double)sum->count, (long)sum->remainder, (long)sum->blength, | |
da9d12f5 | 227 | sum->s2length, (double)sum->flength); |
195bd906 S |
228 | } |
229 | } | |
80605142 | 230 | |
bceec82f | 231 | |
80605142 WD |
232 | /* |
233 | * Generate and send a stream of signatures/checksums that describe a buffer | |
e66dfd18 | 234 | * |
80605142 WD |
235 | * Generate approximately one checksum every block_len bytes. |
236 | */ | |
cd6aa5b5 | 237 | static void generate_and_send_sums(int fd, OFF_T len, int f_out, int f_copy) |
2f03f956 | 238 | { |
a1cbe76e | 239 | int32 i; |
6e45e1dd | 240 | struct map_struct *mapbuf; |
80605142 | 241 | struct sum_struct sum; |
2f03f956 AT |
242 | OFF_T offset = 0; |
243 | ||
423dba8e | 244 | sum_sizes_sqroot(&sum, len); |
e66dfd18 | 245 | |
6e45e1dd | 246 | if (len > 0) |
96d910c7 | 247 | mapbuf = map_file(fd, len, MAX_MAP_SIZE, sum.blength); |
6e45e1dd WD |
248 | else |
249 | mapbuf = NULL; | |
250 | ||
fc0257c9 | 251 | write_sum_head(f_out, &sum); |
2f03f956 | 252 | |
80605142 | 253 | for (i = 0; i < sum.count; i++) { |
a255c592 | 254 | int32 n1 = (int32)MIN(len, (OFF_T)sum.blength); |
6e45e1dd | 255 | char *map = map_ptr(mapbuf, offset, n1); |
80605142 WD |
256 | uint32 sum1 = get_checksum1(map, n1); |
257 | char sum2[SUM_LENGTH]; | |
2f03f956 | 258 | |
cd6aa5b5 WD |
259 | if (f_copy >= 0) |
260 | full_write(f_copy, map, n1); | |
261 | ||
80605142 | 262 | get_checksum2(map, n1, sum2); |
2f03f956 | 263 | |
80605142 | 264 | if (verbose > 3) { |
e66dfd18 | 265 | rprintf(FINFO, |
a255c592 WD |
266 | "chunk[%.0f] offset=%.0f len=%ld sum1=%08lx\n", |
267 | (double)i, (double)offset, (long)n1, | |
0e36d9da | 268 | (unsigned long)sum1); |
80605142 WD |
269 | } |
270 | write_int(f_out, sum1); | |
fc0257c9 | 271 | write_buf(f_out, sum2, sum.s2length); |
2f03f956 AT |
272 | len -= n1; |
273 | offset += n1; | |
274 | } | |
6e45e1dd WD |
275 | |
276 | if (mapbuf) | |
277 | unmap_file(mapbuf); | |
2f03f956 AT |
278 | } |
279 | ||
06a1dbad | 280 | |
8e85be0a WD |
281 | /* Try to find a filename in the same dir as "fname" with a similar name. */ |
282 | static int find_fuzzy(struct file_struct *file, struct file_list *dirlist) | |
283 | { | |
284 | int fname_len, fname_suf_len; | |
285 | const char *fname_suf, *fname = file->basename; | |
286 | uint32 lowest_dist = 0x7FFFFFFF; | |
287 | int j, lowest_j = -1; | |
288 | ||
289 | fname_len = strlen(fname); | |
290 | fname_suf = find_filename_suffix(fname, fname_len, &fname_suf_len); | |
291 | ||
292 | for (j = 0; j < dirlist->count; j++) { | |
293 | struct file_struct *fp = dirlist->files[j]; | |
294 | const char *suf, *name; | |
295 | int len, suf_len; | |
296 | uint32 dist; | |
297 | ||
298 | if (!S_ISREG(fp->mode) || !fp->length | |
299 | || fp->flags & FLAG_NO_FUZZY) | |
300 | continue; | |
301 | ||
302 | name = fp->basename; | |
303 | ||
304 | if (fp->length == file->length | |
305 | && fp->modtime == file->modtime) { | |
306 | if (verbose > 4) { | |
307 | rprintf(FINFO, | |
308 | "fuzzy size/modtime match for %s\n", | |
309 | name); | |
310 | } | |
311 | return j; | |
312 | } | |
313 | ||
314 | len = strlen(name); | |
315 | suf = find_filename_suffix(name, len, &suf_len); | |
316 | ||
317 | dist = fuzzy_distance(name, len, fname, fname_len); | |
318 | /* Add some extra weight to how well the suffixes match. */ | |
319 | dist += fuzzy_distance(suf, suf_len, fname_suf, fname_suf_len) | |
320 | * 10; | |
321 | if (verbose > 4) { | |
322 | rprintf(FINFO, "fuzzy distance for %s = %d.%05d\n", | |
323 | name, (int)(dist>>16), (int)(dist&0xFFFF)); | |
324 | } | |
325 | if (dist <= lowest_dist) { | |
326 | lowest_dist = dist; | |
327 | lowest_j = j; | |
328 | } | |
329 | } | |
330 | ||
331 | return lowest_j; | |
332 | } | |
333 | ||
2f03f956 | 334 | |
0492fdfb WD |
335 | /* Acts on flist->file's ndx'th item, whose name is fname. If a directory, |
336 | * make sure it exists, and has the right permissions/timestamp info. For | |
337 | * all other non-regular files (symlinks, etc.) we create them here. For | |
338 | * regular files that have changed, we try to find a basis file and then | |
339 | * start sending checksums. | |
ef1aa910 | 340 | * |
0492fdfb WD |
341 | * Note that f_out is set to -1 when doing final directory-permission and |
342 | * modification-time repair. */ | |
fa13f396 | 343 | static void recv_generator(char *fname, struct file_list *flist, |
0492fdfb | 344 | struct file_struct *file, int ndx, |
41cfde6b | 345 | int f_out, int f_out_name) |
2cda2560 | 346 | { |
df337831 | 347 | static int missing_below = -1; |
8e85be0a WD |
348 | static char *fuzzy_dirname = NULL; |
349 | static struct file_list *fuzzy_dirlist = NULL; | |
350 | struct file_struct *fuzzy_file = NULL; | |
41cfde6b | 351 | int fd = -1, f_copy = -1; |
89f7eff3 | 352 | STRUCT_STAT st, partial_st; |
41cfde6b | 353 | struct file_struct *back_file = NULL; |
e7d13fe5 | 354 | int statret, stat_errno; |
41cfde6b | 355 | char *fnamecmp, *partialptr, *backupptr = NULL; |
375a4556 | 356 | char fnamecmpbuf[MAXPATHLEN]; |
6c3862fa | 357 | int itemizing, maybe_DEL_TERSE, maybe_PERMS_REPORT; |
41cfde6b | 358 | uchar fnamecmp_type; |
6c3862fa | 359 | enum logcode code; |
f7632fc6 | 360 | |
dfd5ba6a WD |
361 | if (list_only) |
362 | return; | |
2f03f956 | 363 | |
6c3862fa WD |
364 | if (protocol_version >= 29) { |
365 | itemizing = 1; | |
366 | code = daemon_log_format_has_i ? 0 : FLOG; | |
367 | maybe_DEL_TERSE = log_format_has_o_or_i ? 0 : DEL_TERSE; | |
368 | maybe_PERMS_REPORT = log_format_has_i ? 0 : PERMS_REPORT; | |
369 | } else if (am_daemon) { | |
370 | itemizing = daemon_log_format_has_i && !dry_run; | |
371 | code = itemizing || dry_run ? FCLIENT : FINFO; | |
372 | maybe_DEL_TERSE = DEL_TERSE; | |
373 | maybe_PERMS_REPORT = PERMS_REPORT; | |
374 | } else if (!am_server) { | |
375 | itemizing = log_format_has_i; | |
376 | code = itemizing ? 0 : FINFO; | |
377 | maybe_DEL_TERSE = log_format_has_o_or_i ? 0 : DEL_TERSE; | |
378 | maybe_PERMS_REPORT = log_format_has_i ? 0 : PERMS_REPORT; | |
379 | } else { | |
380 | itemizing = 0; | |
381 | code = FINFO; | |
382 | maybe_DEL_TERSE = DEL_TERSE; | |
383 | maybe_PERMS_REPORT = PERMS_REPORT; | |
384 | } | |
385 | ||
8e85be0a WD |
386 | if (!fname) { |
387 | if (fuzzy_dirlist) { | |
388 | flist_free(fuzzy_dirlist); | |
389 | fuzzy_dirlist = NULL; | |
390 | fuzzy_dirname = NULL; | |
391 | } | |
392 | if (missing_below >= 0) { | |
393 | dry_run--; | |
394 | missing_below = -1; | |
395 | } | |
396 | return; | |
397 | } | |
398 | ||
4875d6b6 WD |
399 | if (verbose > 2) { |
400 | rprintf(FINFO, "recv_generator(%s,%d)\n", | |
401 | safe_fname(fname), ndx); | |
402 | } | |
2f03f956 | 403 | |
7842418b WD |
404 | if (server_filter_list.head |
405 | && check_filter(&server_filter_list, fname, | |
406 | S_ISDIR(file->mode)) < 0) { | |
3e35c34b WD |
407 | if (verbose) { |
408 | rprintf(FINFO, "skipping server-excluded file \"%s\"\n", | |
ecc81fce | 409 | safe_fname(fname)); |
3e35c34b | 410 | } |
97f9dcae | 411 | return; |
3e35c34b | 412 | } |
97f9dcae | 413 | |
8e85be0a | 414 | if (missing_below >= 0 && file->dir.depth <= missing_below) { |
df337831 WD |
415 | dry_run--; |
416 | missing_below = -1; | |
417 | } | |
73f7af0e WD |
418 | if (dry_run > 1) { |
419 | statret = -1; | |
420 | stat_errno = ENOENT; | |
421 | } else { | |
8e85be0a WD |
422 | if (fuzzy_basis && S_ISREG(file->mode)) { |
423 | char *dn = file->dirname ? file->dirname : "."; | |
424 | /* Yes, identical dirnames are guaranteed to have | |
425 | * identical pointers at this point. */ | |
426 | if (fuzzy_dirname != dn) { | |
427 | if (fuzzy_dirlist) | |
428 | flist_free(fuzzy_dirlist); | |
429 | fuzzy_dirname = dn; | |
430 | fuzzy_dirlist = get_dirlist(fuzzy_dirname, 1); | |
431 | } | |
432 | } | |
433 | ||
73f7af0e WD |
434 | statret = link_stat(fname, &st, |
435 | keep_dirlinks && S_ISDIR(file->mode)); | |
436 | stat_errno = errno; | |
437 | } | |
63787382 | 438 | |
a7260c40 | 439 | if (only_existing && statret == -1 && stat_errno == ENOENT) { |
1347d512 | 440 | /* we only want to update existing files */ |
ecc81fce WD |
441 | if (verbose > 1) { |
442 | rprintf(FINFO, "not creating new file \"%s\"\n", | |
443 | safe_fname(fname)); | |
444 | } | |
1347d512 AT |
445 | return; |
446 | } | |
447 | ||
d9b4d267 WD |
448 | if (statret == 0 && !preserve_perms |
449 | && S_ISDIR(st.st_mode) == S_ISDIR(file->mode)) { | |
4df9f368 | 450 | /* if the file exists already and we aren't perserving |
2cda2560 WD |
451 | * permissions then act as though the remote end sent |
452 | * us the file permissions we already have */ | |
67e78a82 WD |
453 | file->mode = (file->mode & ~CHMOD_BITS) |
454 | | (st.st_mode & CHMOD_BITS); | |
4df9f368 AT |
455 | } |
456 | ||
2f03f956 | 457 | if (S_ISDIR(file->mode)) { |
2cda2560 WD |
458 | /* The file to be received is a directory, so we need |
459 | * to prepare appropriately. If there is already a | |
460 | * file of that name and it is *not* a directory, then | |
461 | * we need to delete it. If it doesn't exist, then | |
027428eb | 462 | * (perhaps recursively) create it. */ |
2f03f956 | 463 | if (statret == 0 && !S_ISDIR(st.st_mode)) { |
8a8356b7 | 464 | delete_file(fname, st.st_mode, maybe_DEL_TERSE); |
2f03f956 AT |
465 | statret = -1; |
466 | } | |
df337831 WD |
467 | if (dry_run && statret != 0 && missing_below < 0) { |
468 | missing_below = file->dir.depth; | |
469 | dry_run++; | |
470 | } | |
6c3862fa | 471 | if (itemizing && f_out != -1) |
c557eb8c | 472 | itemize(file, statret, &st, 0, f_out, ndx); |
2f03f956 | 473 | if (statret != 0 && do_mkdir(fname,file->mode) != 0 && errno != EEXIST) { |
027428eb WD |
474 | if (!relative_paths || errno != ENOENT |
475 | || create_directory_path(fname, orig_umask) < 0 | |
476 | || do_mkdir(fname, file->mode) < 0) { | |
d62bcc17 WD |
477 | rsyserr(FERROR, errno, |
478 | "recv_generator: mkdir %s failed", | |
479 | full_fname(fname)); | |
2f03f956 AT |
480 | } |
481 | } | |
716e73d4 | 482 | if (set_perms(fname, file, statret ? NULL : &st, 0) |
6c3862fa WD |
483 | && verbose && code && f_out != -1) |
484 | rprintf(code, "%s/\n", safe_fname(fname)); | |
c93fad5e | 485 | if (delete_during && f_out != -1 && csum_length != SUM_LENGTH |
31937d36 WD |
486 | && (file->flags & FLAG_DEL_HERE)) |
487 | delete_in_dir(flist, fname, file); | |
2f03f956 | 488 | return; |
06a1dbad | 489 | } |
6c3862fa | 490 | |
06a1dbad | 491 | if (max_size && file->length > max_size) { |
4875d6b6 WD |
492 | if (verbose > 1) { |
493 | rprintf(FINFO, "%s is over max-size\n", | |
494 | safe_fname(fname)); | |
495 | } | |
7d1bfaf7 | 496 | return; |
2f03f956 AT |
497 | } |
498 | ||
499 | if (preserve_links && S_ISLNK(file->mode)) { | |
4f5b0756 | 500 | #ifdef SUPPORT_LINKS |
728d0922 | 501 | if (safe_symlinks && unsafe_symlink(file->u.link, fname)) { |
2f03f956 | 502 | if (verbose) { |
4875d6b6 WD |
503 | rprintf(FINFO, |
504 | "ignoring unsafe symlink %s -> \"%s\"\n", | |
505 | full_fname(fname), | |
506 | safe_fname(file->u.link)); | |
2f03f956 AT |
507 | } |
508 | return; | |
509 | } | |
510 | if (statret == 0) { | |
7e38410e WD |
511 | char lnk[MAXPATHLEN]; |
512 | int len; | |
513 | ||
8a8356b7 | 514 | if (!S_ISDIR(st.st_mode) |
7e38410e WD |
515 | && (len = readlink(fname, lnk, MAXPATHLEN-1)) > 0) { |
516 | lnk[len] = 0; | |
85d4d142 MP |
517 | /* A link already pointing to the |
518 | * right place -- no further action | |
519 | * required. */ | |
7e38410e | 520 | if (strcmp(lnk, file->u.link) == 0) { |
6c3862fa | 521 | if (itemizing) { |
c557eb8c WD |
522 | itemize(file, 0, &st, 0, |
523 | f_out, ndx); | |
524 | } | |
c41b52c4 | 525 | set_perms(fname, file, &st, |
8a8356b7 | 526 | maybe_PERMS_REPORT); |
2f03f956 AT |
527 | return; |
528 | } | |
2cda2560 | 529 | } |
7e38410e WD |
530 | /* Not the right symlink (or not a symlink), so |
531 | * delete it. */ | |
88b218fa | 532 | if (S_ISLNK(st.st_mode)) |
8a8356b7 | 533 | delete_file(fname, st.st_mode, DEL_TERSE); |
88b218fa | 534 | else { |
8a8356b7 | 535 | delete_file(fname, st.st_mode, maybe_DEL_TERSE); |
88b218fa WD |
536 | statret = -1; |
537 | } | |
2f03f956 | 538 | } |
728d0922 | 539 | if (do_symlink(file->u.link,fname) != 0) { |
d62bcc17 | 540 | rsyserr(FERROR, errno, "symlink %s -> \"%s\" failed", |
ecc81fce | 541 | full_fname(fname), safe_fname(file->u.link)); |
2f03f956 AT |
542 | } else { |
543 | set_perms(fname,file,NULL,0); | |
6c3862fa | 544 | if (itemizing) { |
c557eb8c WD |
545 | itemize(file, statret, &st, SID_UPDATING, |
546 | f_out, ndx); | |
6c3862fa WD |
547 | } |
548 | if (code && verbose) { | |
549 | rprintf(code, "%s -> %s\n", safe_fname(fname), | |
c557eb8c | 550 | safe_fname(file->u.link)); |
2f03f956 AT |
551 | } |
552 | } | |
553 | #endif | |
554 | return; | |
555 | } | |
556 | ||
2f03f956 | 557 | if (am_root && preserve_devices && IS_DEVICE(file->mode)) { |
2cda2560 | 558 | if (statret != 0 || |
2f03f956 | 559 | st.st_mode != file->mode || |
3915fd75 | 560 | st.st_rdev != file->u.rdev) { |
88b218fa | 561 | if (IS_DEVICE(st.st_mode)) |
8a8356b7 | 562 | delete_file(fname, st.st_mode, DEL_TERSE); |
88b218fa | 563 | else { |
8a8356b7 | 564 | delete_file(fname, st.st_mode, maybe_DEL_TERSE); |
88b218fa | 565 | statret = -1; |
c557eb8c | 566 | } |
d62bcc17 | 567 | if (verbose > 2) { |
2f03f956 | 568 | rprintf(FINFO,"mknod(%s,0%o,0x%x)\n", |
ecc81fce WD |
569 | safe_fname(fname), |
570 | (int)file->mode, (int)file->u.rdev); | |
d62bcc17 | 571 | } |
728d0922 | 572 | if (do_mknod(fname,file->mode,file->u.rdev) != 0) { |
d62bcc17 WD |
573 | rsyserr(FERROR, errno, "mknod %s failed", |
574 | full_fname(fname)); | |
2f03f956 AT |
575 | } else { |
576 | set_perms(fname,file,NULL,0); | |
6c3862fa | 577 | if (itemizing) { |
88b218fa WD |
578 | itemize(file, statret, &st, SID_UPDATING, |
579 | f_out, ndx); | |
6c3862fa WD |
580 | } |
581 | if (code && verbose) { | |
582 | rprintf(code, "%s\n", | |
ecc81fce WD |
583 | safe_fname(fname)); |
584 | } | |
2f03f956 AT |
585 | } |
586 | } else { | |
6c3862fa | 587 | if (itemizing) { |
c557eb8c WD |
588 | itemize(file, statret, &st, 0, |
589 | f_out, ndx); | |
590 | } | |
8a8356b7 | 591 | set_perms(fname, file, &st, maybe_PERMS_REPORT); |
2f03f956 AT |
592 | } |
593 | return; | |
594 | } | |
2f03f956 | 595 | |
6dff5992 | 596 | if (preserve_hard_links && hard_link_check(file, HL_CHECK_MASTER)) |
2f03f956 | 597 | return; |
2f03f956 AT |
598 | |
599 | if (!S_ISREG(file->mode)) { | |
ecc81fce WD |
600 | rprintf(FINFO, "skipping non-regular file \"%s\"\n", |
601 | safe_fname(fname)); | |
2f03f956 AT |
602 | return; |
603 | } | |
604 | ||
375a4556 | 605 | fnamecmp = fname; |
41cfde6b | 606 | fnamecmp_type = FNAMECMP_FNAME; |
375a4556 | 607 | |
06a1dbad | 608 | if (statret != 0 && basis_dir[0] != NULL) { |
b7e8628c WD |
609 | int fallback_match = -1; |
610 | int match_level = 0; | |
611 | int i = 0; | |
612 | do { | |
613 | pathjoin(fnamecmpbuf, sizeof fnamecmpbuf, | |
614 | basis_dir[i], fname); | |
615 | if (link_stat(fnamecmpbuf, &st, 0) == 0 | |
616 | && S_ISREG(st.st_mode)) { | |
617 | statret = 0; | |
618 | if (link_dest) { | |
619 | if (!match_level) { | |
620 | fallback_match = i; | |
621 | match_level = 1; | |
622 | } else if (match_level == 2 | |
623 | && !unchanged_attrs(file, &st)) | |
624 | continue; | |
625 | if (!unchanged_file(fnamecmpbuf, file, &st)) | |
626 | continue; | |
627 | fallback_match = i; | |
628 | match_level = 2; | |
629 | if (!unchanged_attrs(file, &st)) | |
630 | continue; | |
631 | } | |
632 | match_level = 3; | |
633 | break; | |
634 | } | |
635 | } while (basis_dir[++i] != NULL); | |
636 | if (statret == 0) { | |
637 | if (match_level < 3) { | |
638 | i = fallback_match; | |
639 | pathjoin(fnamecmpbuf, sizeof fnamecmpbuf, | |
640 | basis_dir[i], fname); | |
641 | } | |
4f5b0756 | 642 | #ifdef HAVE_LINK |
b7e8628c | 643 | if (link_dest && match_level == 3 && !dry_run) { |
e7d13fe5 WD |
644 | if (do_link(fnamecmpbuf, fname) < 0) { |
645 | if (verbose) { | |
646 | rsyserr(FINFO, errno, | |
647 | "link %s => %s", | |
4875d6b6 | 648 | full_fname(fnamecmpbuf), |
e7d13fe5 WD |
649 | safe_fname(fname)); |
650 | } | |
651 | fnamecmp = fnamecmpbuf; | |
2be2fb3e | 652 | fnamecmp_type = i; |
e7bc9b64 | 653 | } |
e7d13fe5 | 654 | } else |
59c95e42 | 655 | #endif |
41cfde6b | 656 | { |
e7d13fe5 | 657 | fnamecmp = fnamecmpbuf; |
2be2fb3e | 658 | fnamecmp_type = i; |
41cfde6b | 659 | } |
e7d13fe5 WD |
660 | } |
661 | } | |
662 | ||
663 | if (statret == 0 && !S_ISREG(st.st_mode)) { | |
8a8356b7 | 664 | if (delete_file(fname, st.st_mode, maybe_DEL_TERSE) != 0) |
e7d13fe5 WD |
665 | return; |
666 | statret = -1; | |
667 | stat_errno = ENOENT; | |
375a4556 DD |
668 | } |
669 | ||
9d954dca | 670 | if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL |
72c19bb3 WD |
671 | && link_stat(partialptr, &partial_st, 0) == 0 |
672 | && S_ISREG(partial_st.st_mode)) { | |
06a1dbad | 673 | if (statret != 0) |
72c19bb3 WD |
674 | goto prepare_to_open; |
675 | } else | |
676 | partialptr = NULL; | |
89f7eff3 | 677 | |
06a1dbad | 678 | if (statret != 0 && fuzzy_basis && dry_run <= 1) { |
8e85be0a WD |
679 | int j = find_fuzzy(file, fuzzy_dirlist); |
680 | if (j >= 0) { | |
681 | fuzzy_file = fuzzy_dirlist->files[j]; | |
682 | f_name_to(fuzzy_file, fnamecmpbuf); | |
683 | if (verbose > 2) { | |
684 | rprintf(FINFO, "fuzzy basis selected for %s: %s\n", | |
685 | safe_fname(fname), safe_fname(fnamecmpbuf)); | |
686 | } | |
687 | st.st_mode = fuzzy_file->mode; | |
688 | st.st_size = fuzzy_file->length; | |
689 | st.st_mtime = fuzzy_file->modtime; | |
690 | statret = 0; | |
691 | fnamecmp = fnamecmpbuf; | |
692 | fnamecmp_type = FNAMECMP_FUZZY; | |
693 | } | |
694 | } | |
695 | ||
06a1dbad | 696 | if (statret != 0) { |
6dff5992 WD |
697 | if (preserve_hard_links && hard_link_check(file, HL_SKIP)) |
698 | return; | |
41cfde6b WD |
699 | if (stat_errno == ENOENT) |
700 | goto notify_others; | |
701 | if (verbose > 1) { | |
e7d13fe5 WD |
702 | rsyserr(FERROR, stat_errno, |
703 | "recv_generator: failed to stat %s", | |
d62bcc17 | 704 | full_fname(fname)); |
2f03f956 AT |
705 | } |
706 | return; | |
707 | } | |
708 | ||
41cfde6b | 709 | if (opt_ignore_existing && fnamecmp_type == FNAMECMP_FNAME) { |
3d6feada | 710 | if (verbose > 1) |
ecc81fce | 711 | rprintf(FINFO, "%s exists\n", safe_fname(fname)); |
3d6feada | 712 | return; |
2cda2560 | 713 | } |
3d6feada | 714 | |
41cfde6b | 715 | if (update_only && fnamecmp_type == FNAMECMP_FNAME |
d3a4375f | 716 | && cmp_modtime(st.st_mtime, file->modtime) > 0) { |
2f03f956 | 717 | if (verbose > 1) |
ecc81fce | 718 | rprintf(FINFO, "%s is newer\n", safe_fname(fname)); |
2f03f956 AT |
719 | return; |
720 | } | |
721 | ||
2be2fb3e | 722 | if (!compare_dest && fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH) |
b7e8628c | 723 | ; |
8e85be0a WD |
724 | else if (fnamecmp_type == FNAMECMP_FUZZY) |
725 | ; | |
b7e8628c | 726 | else if (unchanged_file(fnamecmp, file, &st)) { |
6c3862fa | 727 | if (itemizing) { |
c557eb8c WD |
728 | itemize(file, statret, &st, |
729 | fnamecmp_type == FNAMECMP_FNAME | |
730 | ? 0 : SID_NO_DEST_AND_NO_UPDATE, | |
731 | f_out, ndx); | |
06a1dbad | 732 | } |
41cfde6b | 733 | if (fnamecmp_type == FNAMECMP_FNAME) |
8a8356b7 | 734 | set_perms(fname, file, &st, maybe_PERMS_REPORT); |
2f03f956 AT |
735 | return; |
736 | } | |
737 | ||
89f7eff3 | 738 | prepare_to_open: |
9d954dca WD |
739 | if (partialptr) { |
740 | st = partial_st; | |
741 | fnamecmp = partialptr; | |
742 | fnamecmp_type = FNAMECMP_PARTIAL_DIR; | |
743 | statret = 0; | |
744 | } | |
745 | ||
3841a04e WD |
746 | if (dry_run || read_batch) |
747 | goto notify_others; | |
748 | if (whole_file > 0) { | |
06a1dbad WD |
749 | if (statret == 0) |
750 | statret = 1; | |
41cfde6b | 751 | goto notify_others; |
2f03f956 AT |
752 | } |
753 | ||
8e85be0a WD |
754 | if (fuzzy_basis) { |
755 | int j = flist_find(fuzzy_dirlist, file); | |
756 | if (j >= 0) /* don't use changing file as future fuzzy basis */ | |
757 | fuzzy_dirlist->files[j]->flags |= FLAG_NO_FUZZY; | |
758 | } | |
759 | ||
2cda2560 | 760 | /* open the file */ |
8c9fd200 | 761 | fd = do_open(fnamecmp, O_RDONLY, 0); |
2f03f956 AT |
762 | |
763 | if (fd == -1) { | |
d62bcc17 WD |
764 | rsyserr(FERROR, errno, "failed to open %s, continuing", |
765 | full_fname(fnamecmp)); | |
cd6aa5b5 | 766 | pretend_missing: |
60be6acf | 767 | /* pretend the file didn't exist */ |
6dff5992 WD |
768 | if (preserve_hard_links && hard_link_check(file, HL_SKIP)) |
769 | return; | |
41cfde6b WD |
770 | statret = -1; |
771 | goto notify_others; | |
2f03f956 AT |
772 | } |
773 | ||
cd6aa5b5 WD |
774 | if (inplace && make_backups) { |
775 | if (!(backupptr = get_backup_name(fname))) { | |
776 | close(fd); | |
777 | return; | |
778 | } | |
7842418b | 779 | if (!(back_file = make_file(fname, NULL, NO_FILTERS))) { |
cd6aa5b5 WD |
780 | close(fd); |
781 | goto pretend_missing; | |
782 | } | |
783 | if (robust_unlink(backupptr) && errno != ENOENT) { | |
784 | rsyserr(FERROR, errno, "unlink %s", | |
785 | full_fname(backupptr)); | |
786 | free(back_file); | |
787 | close(fd); | |
788 | return; | |
789 | } | |
790 | if ((f_copy = do_open(backupptr, | |
791 | O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0) { | |
792 | rsyserr(FERROR, errno, "open %s", | |
793 | full_fname(backupptr)); | |
794 | free(back_file); | |
795 | close(fd); | |
796 | return; | |
797 | } | |
41cfde6b | 798 | fnamecmp_type = FNAMECMP_BACKUP; |
cd6aa5b5 WD |
799 | } |
800 | ||
dfd5ba6a | 801 | if (verbose > 3) { |
ecc81fce WD |
802 | rprintf(FINFO, "gen mapped %s of size %.0f\n", |
803 | safe_fname(fnamecmp), (double)st.st_size); | |
dfd5ba6a | 804 | } |
2f03f956 | 805 | |
2f03f956 | 806 | if (verbose > 2) |
0492fdfb | 807 | rprintf(FINFO, "generating and sending sums for %d\n", ndx); |
2f03f956 | 808 | |
41cfde6b | 809 | notify_others: |
0492fdfb | 810 | write_int(f_out, ndx); |
6c3862fa | 811 | if (itemizing) { |
e3bcd893 WD |
812 | itemize(file, statret, &st, SID_UPDATING |
813 | | (always_checksum ? SID_REPORT_CHECKSUM : 0), | |
814 | f_out, -1); | |
815 | if (inplace && !read_batch) | |
816 | write_byte(f_out, fnamecmp_type); | |
817 | } | |
8e85be0a | 818 | if (f_out_name >= 0) { |
41cfde6b | 819 | write_byte(f_out_name, fnamecmp_type); |
8e85be0a WD |
820 | if (fnamecmp_type == FNAMECMP_FUZZY) { |
821 | uchar lenbuf[3], *lb = lenbuf; | |
822 | int len = strlen(fuzzy_file->basename); | |
823 | if (len > 0x7F) { | |
824 | #if MAXPATHLEN > 0x7FFF | |
825 | *lb++ = len / 0x10000 + 0x80; | |
826 | *lb++ = len / 0x100; | |
827 | #else | |
828 | *lb++ = len / 0x100 + 0x80; | |
829 | #endif | |
830 | } | |
831 | *lb = len; | |
832 | write_buf(f_out_name, lenbuf, lb - lenbuf + 1); | |
833 | write_buf(f_out_name, fuzzy_file->basename, len); | |
834 | } | |
835 | } | |
cd6aa5b5 | 836 | |
41cfde6b WD |
837 | if (dry_run || read_batch) |
838 | return; | |
839 | ||
840 | if (statret == 0) { | |
841 | generate_and_send_sums(fd, st.st_size, f_out, f_copy); | |
2f03f956 | 842 | |
41cfde6b WD |
843 | if (f_copy >= 0) { |
844 | close(f_copy); | |
845 | set_perms(backupptr, back_file, NULL, 0); | |
846 | if (verbose > 1) { | |
847 | rprintf(FINFO, "backed up %s to %s\n", | |
4875d6b6 | 848 | safe_fname(fname), safe_fname(backupptr)); |
41cfde6b WD |
849 | } |
850 | free(back_file); | |
851 | } | |
852 | ||
853 | close(fd); | |
854 | } else | |
855 | write_sum_head(f_out, NULL); | |
2f03f956 AT |
856 | } |
857 | ||
858 | ||
41cfde6b WD |
859 | void generate_files(int f_out, struct file_list *flist, char *local_name, |
860 | int f_out_name) | |
2f03f956 AT |
861 | { |
862 | int i; | |
e1f67417 | 863 | int phase = 0; |
968c8030 | 864 | char fbuf[MAXPATHLEN]; |
3ea9bbd6 WD |
865 | int need_retouch_dir_times = preserve_times && !omit_dir_times; |
866 | int need_retouch_dir_perms = 0; | |
0492fdfb WD |
867 | int save_only_existing = only_existing; |
868 | int save_opt_ignore_existing = opt_ignore_existing; | |
2f03f956 | 869 | |
45e08edb WD |
870 | if (verbose > 2) { |
871 | rprintf(FINFO, "generator starting pid=%ld count=%d\n", | |
872 | (long)getpid(), flist->count); | |
873 | } | |
2f03f956 | 874 | |
3e7053ac MP |
875 | if (verbose >= 2) { |
876 | rprintf(FINFO, | |
f38bd4a0 | 877 | whole_file > 0 |
3e7053ac MP |
878 | ? "delta-transmission disabled for local transfer or --whole-file\n" |
879 | : "delta transmission enabled\n"); | |
880 | } | |
2cda2560 | 881 | |
0492fdfb WD |
882 | /* We expect to just sit around now, so don't exit on a timeout. |
883 | * If we really get a timeout then the other process should exit. */ | |
a57873b7 AT |
884 | io_timeout = 0; |
885 | ||
2f03f956 AT |
886 | for (i = 0; i < flist->count; i++) { |
887 | struct file_struct *file = flist->files[i]; | |
dfd5ba6a | 888 | struct file_struct copy; |
2f03f956 | 889 | |
dfd5ba6a WD |
890 | if (!file->basename) |
891 | continue; | |
0492fdfb WD |
892 | |
893 | /* We need to ensure that any dirs we create have writeable | |
894 | * permissions during the time we are putting files within | |
895 | * them. This is then fixed after the transfer is done. */ | |
dfd5ba6a WD |
896 | if (!am_root && S_ISDIR(file->mode) && !(file->mode & S_IWUSR)) { |
897 | copy = *file; | |
dfd5ba6a WD |
898 | copy.mode |= S_IWUSR; /* user write */ |
899 | file = © | |
3ea9bbd6 | 900 | need_retouch_dir_perms = 1; |
2f03f956 AT |
901 | } |
902 | ||
3fef5364 | 903 | recv_generator(local_name ? local_name : f_name_to(file, fbuf), |
fa13f396 | 904 | flist, file, i, f_out, f_out_name); |
2f03f956 | 905 | } |
8e85be0a | 906 | recv_generator(NULL, NULL, NULL, 0, -1, -1); |
c93fad5e | 907 | if (delete_during) |
31937d36 | 908 | delete_in_dir(NULL, NULL, NULL); |
2f03f956 AT |
909 | |
910 | phase++; | |
911 | csum_length = SUM_LENGTH; | |
0492fdfb WD |
912 | only_existing = max_size = opt_ignore_existing = 0; |
913 | update_only = always_checksum = size_only = 0; | |
e1f67417 | 914 | ignore_times = 1; |
2f03f956 AT |
915 | |
916 | if (verbose > 2) | |
917 | rprintf(FINFO,"generate_files phase=%d\n",phase); | |
918 | ||
7daccb8e | 919 | write_int(f_out, -1); |
2f03f956 | 920 | |
bc63ae3f S |
921 | /* files can cycle through the system more than once |
922 | * to catch initial checksum errors */ | |
b9b15fb1 | 923 | while ((i = get_redo_num()) != -1) { |
bc63ae3f | 924 | struct file_struct *file = flist->files[i]; |
3fef5364 | 925 | recv_generator(local_name ? local_name : f_name_to(file, fbuf), |
fa13f396 | 926 | flist, file, i, f_out, f_out_name); |
bc63ae3f | 927 | } |
2f03f956 | 928 | |
bc63ae3f | 929 | phase++; |
0492fdfb WD |
930 | only_existing = save_only_existing; |
931 | opt_ignore_existing = save_opt_ignore_existing; | |
932 | ||
bc63ae3f S |
933 | if (verbose > 2) |
934 | rprintf(FINFO,"generate_files phase=%d\n",phase); | |
2f03f956 | 935 | |
7daccb8e | 936 | write_int(f_out, -1); |
6dff5992 | 937 | |
b0da4b23 WD |
938 | /* Read post-redo-phase MSG_DONE and any prior messages. */ |
939 | get_redo_num(); | |
940 | ||
6dff5992 WD |
941 | if (preserve_hard_links) |
942 | do_hard_links(); | |
943 | ||
0492fdfb WD |
944 | if ((need_retouch_dir_perms || need_retouch_dir_times) |
945 | && !list_only && !local_name && !dry_run) { | |
3ea9bbd6 WD |
946 | /* Now we need to fix any directory permissions that were |
947 | * modified during the transfer and/or re-set any tweaked | |
948 | * modified-time values. */ | |
949 | for (i = 0; i < flist->count; i++) { | |
950 | struct file_struct *file = flist->files[i]; | |
951 | if (!file->basename || !S_ISDIR(file->mode)) | |
952 | continue; | |
953 | if (!need_retouch_dir_times && file->mode & S_IWUSR) | |
954 | continue; | |
955 | recv_generator(local_name ? local_name : f_name(file), | |
956 | flist, file, i, -1, -1); | |
957 | } | |
6dff5992 | 958 | } |
8e85be0a | 959 | recv_generator(NULL, NULL, NULL, 0, -1, -1); |
6dff5992 WD |
960 | |
961 | if (verbose > 2) | |
962 | rprintf(FINFO,"generate_files finished\n"); | |
2f03f956 | 963 | } |