- Added --remove-sent-files option.
[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;
6c3862fa
WD
28extern int log_format_has_i;
29extern int log_format_has_o_or_i;
30extern int daemon_log_format_has_i;
31extern int am_root;
32extern int am_server;
33extern int am_daemon;
2f03f956 34extern int relative_paths;
716e73d4 35extern int keep_dirlinks;
2f03f956 36extern int preserve_links;
2f03f956
AT
37extern int preserve_devices;
38extern int preserve_hard_links;
6744b62d
WD
39extern int preserve_perms;
40extern int preserve_uid;
41extern int preserve_gid;
3ea9bbd6
WD
42extern int preserve_times;
43extern int omit_dir_times;
fa13f396 44extern int delete_during;
2f03f956 45extern int update_only;
3d6feada 46extern int opt_ignore_existing;
cd6aa5b5
WD
47extern int inplace;
48extern int make_backups;
2f03f956
AT
49extern int csum_length;
50extern int ignore_times;
f83f0548 51extern int size_only;
7d1bfaf7 52extern OFF_T max_size;
2f03f956 53extern int io_timeout;
d04e9c51 54extern int protocol_version;
8e85be0a 55extern int fuzzy_basis;
2f03f956 56extern int always_checksum;
a7260c40 57extern char *partial_dir;
b7e8628c 58extern char *basis_dir[];
2be2fb3e 59extern int compare_dest;
59c95e42 60extern int link_dest;
5774786f
WD
61extern int whole_file;
62extern int local_server;
5774786f 63extern int list_only;
b9f592fb 64extern int read_batch;
5774786f
WD
65extern int only_existing;
66extern int orig_umask;
67extern int safe_symlinks;
a255c592 68extern long block_size; /* "long" because popt can't set an int32. */
6c3862fa 69extern struct stats stats;
2f03f956 70
7842418b 71extern struct filter_list_struct server_filter_list;
97f9dcae 72
b7e8628c 73static 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
93static 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. */
132static 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 173static 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 237static 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. */
282static 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 343static 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{
8174bc35 347 static int missing_below = -1, excluded_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];
8e3ead09 357 int itemizing, 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;
6c3862fa
WD
367 maybe_PERMS_REPORT = log_format_has_i ? 0 : PERMS_REPORT;
368 } else if (am_daemon) {
369 itemizing = daemon_log_format_has_i && !dry_run;
370 code = itemizing || dry_run ? FCLIENT : FINFO;
6c3862fa
WD
371 maybe_PERMS_REPORT = PERMS_REPORT;
372 } else if (!am_server) {
373 itemizing = log_format_has_i;
374 code = itemizing ? 0 : FINFO;
6c3862fa
WD
375 maybe_PERMS_REPORT = log_format_has_i ? 0 : PERMS_REPORT;
376 } else {
377 itemizing = 0;
378 code = FINFO;
6c3862fa
WD
379 maybe_PERMS_REPORT = PERMS_REPORT;
380 }
381
8e85be0a
WD
382 if (!fname) {
383 if (fuzzy_dirlist) {
384 flist_free(fuzzy_dirlist);
385 fuzzy_dirlist = NULL;
386 fuzzy_dirname = NULL;
387 }
388 if (missing_below >= 0) {
389 dry_run--;
390 missing_below = -1;
391 }
392 return;
393 }
394
4875d6b6
WD
395 if (verbose > 2) {
396 rprintf(FINFO, "recv_generator(%s,%d)\n",
397 safe_fname(fname), ndx);
398 }
2f03f956 399
8174bc35
WD
400 if (server_filter_list.head) {
401 if (excluded_below >= 0) {
402 if (file->dir.depth > excluded_below)
403 goto skipping;
404 excluded_below = -1;
405 }
406 if (check_filter(&server_filter_list, fname,
407 S_ISDIR(file->mode)) < 0) {
408 if (S_ISDIR(file->mode))
409 excluded_below = file->dir.depth;
410 skipping:
411 if (verbose) {
412 rprintf(FINFO,
413 "skipping server-excluded file \"%s\"\n",
414 safe_fname(fname));
415 }
416 return;
3e35c34b 417 }
3e35c34b 418 }
97f9dcae 419
8e85be0a 420 if (missing_below >= 0 && file->dir.depth <= missing_below) {
df337831
WD
421 dry_run--;
422 missing_below = -1;
423 }
73f7af0e
WD
424 if (dry_run > 1) {
425 statret = -1;
426 stat_errno = ENOENT;
427 } else {
8e85be0a
WD
428 if (fuzzy_basis && S_ISREG(file->mode)) {
429 char *dn = file->dirname ? file->dirname : ".";
430 /* Yes, identical dirnames are guaranteed to have
431 * identical pointers at this point. */
432 if (fuzzy_dirname != dn) {
433 if (fuzzy_dirlist)
434 flist_free(fuzzy_dirlist);
435 fuzzy_dirname = dn;
436 fuzzy_dirlist = get_dirlist(fuzzy_dirname, 1);
437 }
438 }
439
73f7af0e
WD
440 statret = link_stat(fname, &st,
441 keep_dirlinks && S_ISDIR(file->mode));
442 stat_errno = errno;
443 }
63787382 444
a7260c40 445 if (only_existing && statret == -1 && stat_errno == ENOENT) {
1347d512 446 /* we only want to update existing files */
ecc81fce
WD
447 if (verbose > 1) {
448 rprintf(FINFO, "not creating new file \"%s\"\n",
449 safe_fname(fname));
450 }
1347d512
AT
451 return;
452 }
453
d9b4d267
WD
454 if (statret == 0 && !preserve_perms
455 && S_ISDIR(st.st_mode) == S_ISDIR(file->mode)) {
4df9f368 456 /* if the file exists already and we aren't perserving
2cda2560
WD
457 * permissions then act as though the remote end sent
458 * us the file permissions we already have */
67e78a82
WD
459 file->mode = (file->mode & ~CHMOD_BITS)
460 | (st.st_mode & CHMOD_BITS);
4df9f368
AT
461 }
462
2f03f956 463 if (S_ISDIR(file->mode)) {
2cda2560
WD
464 /* The file to be received is a directory, so we need
465 * to prepare appropriately. If there is already a
466 * file of that name and it is *not* a directory, then
467 * we need to delete it. If it doesn't exist, then
027428eb 468 * (perhaps recursively) create it. */
2f03f956 469 if (statret == 0 && !S_ISDIR(st.st_mode)) {
8e3ead09 470 delete_file(fname, st.st_mode, DEL_TERSE);
2f03f956
AT
471 statret = -1;
472 }
df337831
WD
473 if (dry_run && statret != 0 && missing_below < 0) {
474 missing_below = file->dir.depth;
475 dry_run++;
476 }
6c3862fa 477 if (itemizing && f_out != -1)
c557eb8c 478 itemize(file, statret, &st, 0, f_out, ndx);
2f03f956 479 if (statret != 0 && do_mkdir(fname,file->mode) != 0 && errno != EEXIST) {
027428eb
WD
480 if (!relative_paths || errno != ENOENT
481 || create_directory_path(fname, orig_umask) < 0
482 || do_mkdir(fname, file->mode) < 0) {
d62bcc17
WD
483 rsyserr(FERROR, errno,
484 "recv_generator: mkdir %s failed",
485 full_fname(fname));
2f03f956
AT
486 }
487 }
716e73d4 488 if (set_perms(fname, file, statret ? NULL : &st, 0)
6c3862fa
WD
489 && verbose && code && f_out != -1)
490 rprintf(code, "%s/\n", safe_fname(fname));
c93fad5e 491 if (delete_during && f_out != -1 && csum_length != SUM_LENGTH
31937d36
WD
492 && (file->flags & FLAG_DEL_HERE))
493 delete_in_dir(flist, fname, file);
2f03f956 494 return;
06a1dbad 495 }
6c3862fa 496
06a1dbad 497 if (max_size && file->length > max_size) {
4875d6b6
WD
498 if (verbose > 1) {
499 rprintf(FINFO, "%s is over max-size\n",
500 safe_fname(fname));
501 }
7d1bfaf7 502 return;
2f03f956
AT
503 }
504
505 if (preserve_links && S_ISLNK(file->mode)) {
4f5b0756 506#ifdef SUPPORT_LINKS
728d0922 507 if (safe_symlinks && unsafe_symlink(file->u.link, fname)) {
2f03f956 508 if (verbose) {
4875d6b6
WD
509 rprintf(FINFO,
510 "ignoring unsafe symlink %s -> \"%s\"\n",
511 full_fname(fname),
512 safe_fname(file->u.link));
2f03f956
AT
513 }
514 return;
515 }
516 if (statret == 0) {
7e38410e
WD
517 char lnk[MAXPATHLEN];
518 int len;
519
8a8356b7 520 if (!S_ISDIR(st.st_mode)
7e38410e
WD
521 && (len = readlink(fname, lnk, MAXPATHLEN-1)) > 0) {
522 lnk[len] = 0;
85d4d142
MP
523 /* A link already pointing to the
524 * right place -- no further action
525 * required. */
7e38410e 526 if (strcmp(lnk, file->u.link) == 0) {
6c3862fa 527 if (itemizing) {
c557eb8c
WD
528 itemize(file, 0, &st, 0,
529 f_out, ndx);
530 }
c41b52c4 531 set_perms(fname, file, &st,
8a8356b7 532 maybe_PERMS_REPORT);
2f03f956
AT
533 return;
534 }
2cda2560 535 }
7e38410e
WD
536 /* Not the right symlink (or not a symlink), so
537 * delete it. */
88b218fa 538 if (S_ISLNK(st.st_mode))
8a8356b7 539 delete_file(fname, st.st_mode, DEL_TERSE);
88b218fa 540 else {
8e3ead09 541 delete_file(fname, st.st_mode, DEL_TERSE);
88b218fa
WD
542 statret = -1;
543 }
2f03f956 544 }
728d0922 545 if (do_symlink(file->u.link,fname) != 0) {
d62bcc17 546 rsyserr(FERROR, errno, "symlink %s -> \"%s\" failed",
ecc81fce 547 full_fname(fname), safe_fname(file->u.link));
2f03f956
AT
548 } else {
549 set_perms(fname,file,NULL,0);
6c3862fa 550 if (itemizing) {
c557eb8c
WD
551 itemize(file, statret, &st, SID_UPDATING,
552 f_out, ndx);
6c3862fa
WD
553 }
554 if (code && verbose) {
555 rprintf(code, "%s -> %s\n", safe_fname(fname),
c557eb8c 556 safe_fname(file->u.link));
2f03f956
AT
557 }
558 }
559#endif
560 return;
561 }
562
2f03f956 563 if (am_root && preserve_devices && IS_DEVICE(file->mode)) {
2cda2560 564 if (statret != 0 ||
2f03f956 565 st.st_mode != file->mode ||
3915fd75 566 st.st_rdev != file->u.rdev) {
88b218fa 567 if (IS_DEVICE(st.st_mode))
8a8356b7 568 delete_file(fname, st.st_mode, DEL_TERSE);
88b218fa 569 else {
8e3ead09 570 delete_file(fname, st.st_mode, DEL_TERSE);
88b218fa 571 statret = -1;
c557eb8c 572 }
d62bcc17 573 if (verbose > 2) {
2f03f956 574 rprintf(FINFO,"mknod(%s,0%o,0x%x)\n",
ecc81fce
WD
575 safe_fname(fname),
576 (int)file->mode, (int)file->u.rdev);
d62bcc17 577 }
728d0922 578 if (do_mknod(fname,file->mode,file->u.rdev) != 0) {
d62bcc17
WD
579 rsyserr(FERROR, errno, "mknod %s failed",
580 full_fname(fname));
2f03f956
AT
581 } else {
582 set_perms(fname,file,NULL,0);
6c3862fa 583 if (itemizing) {
88b218fa
WD
584 itemize(file, statret, &st, SID_UPDATING,
585 f_out, ndx);
6c3862fa
WD
586 }
587 if (code && verbose) {
588 rprintf(code, "%s\n",
ecc81fce
WD
589 safe_fname(fname));
590 }
2f03f956
AT
591 }
592 } else {
6c3862fa 593 if (itemizing) {
c557eb8c
WD
594 itemize(file, statret, &st, 0,
595 f_out, ndx);
596 }
8a8356b7 597 set_perms(fname, file, &st, maybe_PERMS_REPORT);
2f03f956
AT
598 }
599 return;
600 }
2f03f956 601
6dff5992 602 if (preserve_hard_links && hard_link_check(file, HL_CHECK_MASTER))
2f03f956 603 return;
2f03f956
AT
604
605 if (!S_ISREG(file->mode)) {
ecc81fce
WD
606 rprintf(FINFO, "skipping non-regular file \"%s\"\n",
607 safe_fname(fname));
2f03f956
AT
608 return;
609 }
610
375a4556 611 fnamecmp = fname;
41cfde6b 612 fnamecmp_type = FNAMECMP_FNAME;
375a4556 613
06a1dbad 614 if (statret != 0 && basis_dir[0] != NULL) {
b7e8628c
WD
615 int fallback_match = -1;
616 int match_level = 0;
617 int i = 0;
618 do {
619 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
620 basis_dir[i], fname);
621 if (link_stat(fnamecmpbuf, &st, 0) == 0
622 && S_ISREG(st.st_mode)) {
623 statret = 0;
624 if (link_dest) {
625 if (!match_level) {
626 fallback_match = i;
627 match_level = 1;
628 } else if (match_level == 2
629 && !unchanged_attrs(file, &st))
630 continue;
631 if (!unchanged_file(fnamecmpbuf, file, &st))
632 continue;
633 fallback_match = i;
634 match_level = 2;
635 if (!unchanged_attrs(file, &st))
636 continue;
637 }
638 match_level = 3;
639 break;
640 }
641 } while (basis_dir[++i] != NULL);
642 if (statret == 0) {
643 if (match_level < 3) {
644 i = fallback_match;
645 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
646 basis_dir[i], fname);
647 }
4f5b0756 648#ifdef HAVE_LINK
b7e8628c 649 if (link_dest && match_level == 3 && !dry_run) {
e7d13fe5
WD
650 if (do_link(fnamecmpbuf, fname) < 0) {
651 if (verbose) {
652 rsyserr(FINFO, errno,
653 "link %s => %s",
4875d6b6 654 full_fname(fnamecmpbuf),
e7d13fe5
WD
655 safe_fname(fname));
656 }
657 fnamecmp = fnamecmpbuf;
2be2fb3e 658 fnamecmp_type = i;
e7bc9b64 659 }
e7d13fe5 660 } else
59c95e42 661#endif
41cfde6b 662 {
e7d13fe5 663 fnamecmp = fnamecmpbuf;
2be2fb3e 664 fnamecmp_type = i;
41cfde6b 665 }
e7d13fe5
WD
666 }
667 }
668
669 if (statret == 0 && !S_ISREG(st.st_mode)) {
8e3ead09 670 if (delete_file(fname, st.st_mode, DEL_TERSE) != 0)
e7d13fe5
WD
671 return;
672 statret = -1;
673 stat_errno = ENOENT;
375a4556
DD
674 }
675
9d954dca 676 if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL
72c19bb3
WD
677 && link_stat(partialptr, &partial_st, 0) == 0
678 && S_ISREG(partial_st.st_mode)) {
06a1dbad 679 if (statret != 0)
72c19bb3
WD
680 goto prepare_to_open;
681 } else
682 partialptr = NULL;
89f7eff3 683
06a1dbad 684 if (statret != 0 && fuzzy_basis && dry_run <= 1) {
8e85be0a
WD
685 int j = find_fuzzy(file, fuzzy_dirlist);
686 if (j >= 0) {
687 fuzzy_file = fuzzy_dirlist->files[j];
688 f_name_to(fuzzy_file, fnamecmpbuf);
689 if (verbose > 2) {
690 rprintf(FINFO, "fuzzy basis selected for %s: %s\n",
691 safe_fname(fname), safe_fname(fnamecmpbuf));
692 }
693 st.st_mode = fuzzy_file->mode;
694 st.st_size = fuzzy_file->length;
695 st.st_mtime = fuzzy_file->modtime;
696 statret = 0;
697 fnamecmp = fnamecmpbuf;
698 fnamecmp_type = FNAMECMP_FUZZY;
699 }
700 }
701
06a1dbad 702 if (statret != 0) {
6dff5992
WD
703 if (preserve_hard_links && hard_link_check(file, HL_SKIP))
704 return;
41cfde6b
WD
705 if (stat_errno == ENOENT)
706 goto notify_others;
707 if (verbose > 1) {
e7d13fe5
WD
708 rsyserr(FERROR, stat_errno,
709 "recv_generator: failed to stat %s",
d62bcc17 710 full_fname(fname));
2f03f956
AT
711 }
712 return;
713 }
714
41cfde6b 715 if (opt_ignore_existing && fnamecmp_type == FNAMECMP_FNAME) {
3d6feada 716 if (verbose > 1)
ecc81fce 717 rprintf(FINFO, "%s exists\n", safe_fname(fname));
3d6feada 718 return;
2cda2560 719 }
3d6feada 720
41cfde6b 721 if (update_only && fnamecmp_type == FNAMECMP_FNAME
d3a4375f 722 && cmp_modtime(st.st_mtime, file->modtime) > 0) {
2f03f956 723 if (verbose > 1)
ecc81fce 724 rprintf(FINFO, "%s is newer\n", safe_fname(fname));
2f03f956
AT
725 return;
726 }
727
2be2fb3e 728 if (!compare_dest && fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
b7e8628c 729 ;
8e85be0a
WD
730 else if (fnamecmp_type == FNAMECMP_FUZZY)
731 ;
b7e8628c 732 else if (unchanged_file(fnamecmp, file, &st)) {
6c3862fa 733 if (itemizing) {
c557eb8c
WD
734 itemize(file, statret, &st,
735 fnamecmp_type == FNAMECMP_FNAME
736 ? 0 : SID_NO_DEST_AND_NO_UPDATE,
737 f_out, ndx);
06a1dbad 738 }
41cfde6b 739 if (fnamecmp_type == FNAMECMP_FNAME)
8a8356b7 740 set_perms(fname, file, &st, maybe_PERMS_REPORT);
2f03f956
AT
741 return;
742 }
743
89f7eff3 744prepare_to_open:
9d954dca
WD
745 if (partialptr) {
746 st = partial_st;
747 fnamecmp = partialptr;
748 fnamecmp_type = FNAMECMP_PARTIAL_DIR;
749 statret = 0;
750 }
751
3841a04e
WD
752 if (dry_run || read_batch)
753 goto notify_others;
754 if (whole_file > 0) {
06a1dbad
WD
755 if (statret == 0)
756 statret = 1;
41cfde6b 757 goto notify_others;
2f03f956
AT
758 }
759
8e85be0a
WD
760 if (fuzzy_basis) {
761 int j = flist_find(fuzzy_dirlist, file);
762 if (j >= 0) /* don't use changing file as future fuzzy basis */
763 fuzzy_dirlist->files[j]->flags |= FLAG_NO_FUZZY;
764 }
765
2cda2560 766 /* open the file */
8c9fd200 767 fd = do_open(fnamecmp, O_RDONLY, 0);
2f03f956
AT
768
769 if (fd == -1) {
d62bcc17
WD
770 rsyserr(FERROR, errno, "failed to open %s, continuing",
771 full_fname(fnamecmp));
cd6aa5b5 772 pretend_missing:
60be6acf 773 /* pretend the file didn't exist */
6dff5992
WD
774 if (preserve_hard_links && hard_link_check(file, HL_SKIP))
775 return;
41cfde6b
WD
776 statret = -1;
777 goto notify_others;
2f03f956
AT
778 }
779
cd6aa5b5
WD
780 if (inplace && make_backups) {
781 if (!(backupptr = get_backup_name(fname))) {
782 close(fd);
783 return;
784 }
7842418b 785 if (!(back_file = make_file(fname, NULL, NO_FILTERS))) {
cd6aa5b5
WD
786 close(fd);
787 goto pretend_missing;
788 }
789 if (robust_unlink(backupptr) && errno != ENOENT) {
790 rsyserr(FERROR, errno, "unlink %s",
791 full_fname(backupptr));
792 free(back_file);
793 close(fd);
794 return;
795 }
796 if ((f_copy = do_open(backupptr,
797 O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0) {
798 rsyserr(FERROR, errno, "open %s",
799 full_fname(backupptr));
800 free(back_file);
801 close(fd);
802 return;
803 }
41cfde6b 804 fnamecmp_type = FNAMECMP_BACKUP;
cd6aa5b5
WD
805 }
806
dfd5ba6a 807 if (verbose > 3) {
ecc81fce
WD
808 rprintf(FINFO, "gen mapped %s of size %.0f\n",
809 safe_fname(fnamecmp), (double)st.st_size);
dfd5ba6a 810 }
2f03f956 811
2f03f956 812 if (verbose > 2)
0492fdfb 813 rprintf(FINFO, "generating and sending sums for %d\n", ndx);
2f03f956 814
41cfde6b 815notify_others:
0492fdfb 816 write_int(f_out, ndx);
6c3862fa 817 if (itemizing) {
e3bcd893
WD
818 itemize(file, statret, &st, SID_UPDATING
819 | (always_checksum ? SID_REPORT_CHECKSUM : 0),
820 f_out, -1);
821 if (inplace && !read_batch)
822 write_byte(f_out, fnamecmp_type);
823 }
8e85be0a 824 if (f_out_name >= 0) {
41cfde6b 825 write_byte(f_out_name, fnamecmp_type);
8e85be0a
WD
826 if (fnamecmp_type == FNAMECMP_FUZZY) {
827 uchar lenbuf[3], *lb = lenbuf;
828 int len = strlen(fuzzy_file->basename);
829 if (len > 0x7F) {
830#if MAXPATHLEN > 0x7FFF
831 *lb++ = len / 0x10000 + 0x80;
832 *lb++ = len / 0x100;
833#else
834 *lb++ = len / 0x100 + 0x80;
835#endif
836 }
837 *lb = len;
838 write_buf(f_out_name, lenbuf, lb - lenbuf + 1);
839 write_buf(f_out_name, fuzzy_file->basename, len);
840 }
841 }
cd6aa5b5 842
41cfde6b
WD
843 if (dry_run || read_batch)
844 return;
845
846 if (statret == 0) {
847 generate_and_send_sums(fd, st.st_size, f_out, f_copy);
2f03f956 848
41cfde6b
WD
849 if (f_copy >= 0) {
850 close(f_copy);
851 set_perms(backupptr, back_file, NULL, 0);
852 if (verbose > 1) {
853 rprintf(FINFO, "backed up %s to %s\n",
4875d6b6 854 safe_fname(fname), safe_fname(backupptr));
41cfde6b
WD
855 }
856 free(back_file);
857 }
858
859 close(fd);
860 } else
861 write_sum_head(f_out, NULL);
2f03f956
AT
862}
863
864
41cfde6b
WD
865void generate_files(int f_out, struct file_list *flist, char *local_name,
866 int f_out_name)
2f03f956
AT
867{
868 int i;
e1f67417 869 int phase = 0;
968c8030 870 char fbuf[MAXPATHLEN];
3ea9bbd6
WD
871 int need_retouch_dir_times = preserve_times && !omit_dir_times;
872 int need_retouch_dir_perms = 0;
0492fdfb
WD
873 int save_only_existing = only_existing;
874 int save_opt_ignore_existing = opt_ignore_existing;
2f03f956 875
45e08edb
WD
876 if (verbose > 2) {
877 rprintf(FINFO, "generator starting pid=%ld count=%d\n",
878 (long)getpid(), flist->count);
879 }
2f03f956 880
3e7053ac
MP
881 if (verbose >= 2) {
882 rprintf(FINFO,
f38bd4a0 883 whole_file > 0
3e7053ac
MP
884 ? "delta-transmission disabled for local transfer or --whole-file\n"
885 : "delta transmission enabled\n");
886 }
2cda2560 887
0492fdfb
WD
888 /* We expect to just sit around now, so don't exit on a timeout.
889 * If we really get a timeout then the other process should exit. */
a57873b7
AT
890 io_timeout = 0;
891
2f03f956
AT
892 for (i = 0; i < flist->count; i++) {
893 struct file_struct *file = flist->files[i];
dfd5ba6a 894 struct file_struct copy;
2f03f956 895
dfd5ba6a
WD
896 if (!file->basename)
897 continue;
0492fdfb
WD
898
899 /* We need to ensure that any dirs we create have writeable
900 * permissions during the time we are putting files within
901 * them. This is then fixed after the transfer is done. */
dfd5ba6a
WD
902 if (!am_root && S_ISDIR(file->mode) && !(file->mode & S_IWUSR)) {
903 copy = *file;
dfd5ba6a
WD
904 copy.mode |= S_IWUSR; /* user write */
905 file = &copy;
3ea9bbd6 906 need_retouch_dir_perms = 1;
2f03f956
AT
907 }
908
3fef5364 909 recv_generator(local_name ? local_name : f_name_to(file, fbuf),
fa13f396 910 flist, file, i, f_out, f_out_name);
2f03f956 911 }
8e85be0a 912 recv_generator(NULL, NULL, NULL, 0, -1, -1);
c93fad5e 913 if (delete_during)
31937d36 914 delete_in_dir(NULL, NULL, NULL);
2f03f956
AT
915
916 phase++;
917 csum_length = SUM_LENGTH;
0492fdfb
WD
918 only_existing = max_size = opt_ignore_existing = 0;
919 update_only = always_checksum = size_only = 0;
e1f67417 920 ignore_times = 1;
2f03f956
AT
921
922 if (verbose > 2)
923 rprintf(FINFO,"generate_files phase=%d\n",phase);
924
7daccb8e 925 write_int(f_out, -1);
2f03f956 926
bc63ae3f
S
927 /* files can cycle through the system more than once
928 * to catch initial checksum errors */
b9b15fb1 929 while ((i = get_redo_num()) != -1) {
bc63ae3f 930 struct file_struct *file = flist->files[i];
3fef5364 931 recv_generator(local_name ? local_name : f_name_to(file, fbuf),
fa13f396 932 flist, file, i, f_out, f_out_name);
bc63ae3f 933 }
2f03f956 934
bc63ae3f 935 phase++;
0492fdfb
WD
936 only_existing = save_only_existing;
937 opt_ignore_existing = save_opt_ignore_existing;
938
bc63ae3f
S
939 if (verbose > 2)
940 rprintf(FINFO,"generate_files phase=%d\n",phase);
2f03f956 941
7daccb8e 942 write_int(f_out, -1);
6dff5992 943
b0da4b23
WD
944 /* Read post-redo-phase MSG_DONE and any prior messages. */
945 get_redo_num();
946
6dff5992
WD
947 if (preserve_hard_links)
948 do_hard_links();
949
0492fdfb
WD
950 if ((need_retouch_dir_perms || need_retouch_dir_times)
951 && !list_only && !local_name && !dry_run) {
3ea9bbd6
WD
952 /* Now we need to fix any directory permissions that were
953 * modified during the transfer and/or re-set any tweaked
954 * modified-time values. */
955 for (i = 0; i < flist->count; i++) {
956 struct file_struct *file = flist->files[i];
957 if (!file->basename || !S_ISDIR(file->mode))
958 continue;
959 if (!need_retouch_dir_times && file->mode & S_IWUSR)
960 continue;
961 recv_generator(local_name ? local_name : f_name(file),
962 flist, file, i, -1, -1);
963 }
6dff5992 964 }
8e85be0a 965 recv_generator(NULL, NULL, NULL, 0, -1, -1);
6dff5992
WD
966
967 if (verbose > 2)
968 rprintf(FINFO,"generate_files finished\n");
2f03f956 969}