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