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