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