- Send the itemized data for every file, changed or not, if verbose > 1.
[rsync/rsync.git] / generator.c
1 /* -*- c-file-style: "linux" -*-
2
3    rsync -- fast file replication program
4
5    Copyright (C) 1996-2000 by Andrew Tridgell
6    Copyright (C) Paul Mackerras 1996
7    Copyright (C) 2002 by Martin Pool <mbp@samba.org>
8
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.
13
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.
18
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 itemize_changes;
29 extern int relative_paths;
30 extern int keep_dirlinks;
31 extern int preserve_links;
32 extern int am_root;
33 extern int preserve_devices;
34 extern int preserve_hard_links;
35 extern int preserve_perms;
36 extern int preserve_uid;
37 extern int preserve_gid;
38 extern int preserve_times;
39 extern int omit_dir_times;
40 extern int delete_during;
41 extern int update_only;
42 extern int opt_ignore_existing;
43 extern int inplace;
44 extern int make_backups;
45 extern int csum_length;
46 extern int ignore_times;
47 extern int size_only;
48 extern OFF_T max_size;
49 extern int io_timeout;
50 extern int protocol_version;
51 extern int fuzzy_basis;
52 extern int always_checksum;
53 extern char *partial_dir;
54 extern char *basis_dir[];
55 extern int compare_dest;
56 extern int link_dest;
57 extern int whole_file;
58 extern int local_server;
59 extern int list_only;
60 extern int read_batch;
61 extern int only_existing;
62 extern int orig_umask;
63 extern int safe_symlinks;
64 extern long block_size; /* "long" because popt can't set an int32. */
65
66 extern struct filter_list_struct server_filter_list;
67
68 static int unchanged_attrs(struct file_struct *file, STRUCT_STAT *st)
69 {
70         if (preserve_perms
71          && (st->st_mode & CHMOD_BITS) != (file->mode & CHMOD_BITS))
72                 return 0;
73
74         if (am_root && preserve_uid && st->st_uid != file->uid)
75                 return 0;
76
77         if (preserve_gid && file->gid != GID_NONE && st->st_gid != file->gid)
78                 return 0;
79
80         return 1;
81 }
82
83
84 #define SID_UPDATING              ITEM_UPDATING
85 #define SID_REPORT_CHECKSUM       ITEM_REPORT_CHECKSUM
86 #define SID_NO_DEST_AND_NO_UPDATE (1<<16)
87
88 static void itemize(struct file_struct *file, int statret, STRUCT_STAT *st,
89                     int32 sflags, int f_out, int ndx)
90 {
91         int iflags = sflags & (SID_UPDATING | SID_REPORT_CHECKSUM);
92
93         if (statret >= 0) {
94                 if (S_ISREG(file->mode) && file->length != st->st_size)
95                         iflags |= ITEM_REPORT_SIZE;
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                 }
112         } else
113                 iflags |= ITEM_IS_NEW | ITEM_UPDATING;
114
115         if ((iflags || verbose > 1) && !read_batch) {
116                 if (ndx >= 0)
117                         write_int(f_out, ndx);
118                 write_shortint(f_out, iflags);
119         }
120 }
121
122
123 /* Perform our quick-check heuristic for determining if a file is unchanged. */
124 static int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
125 {
126         if (st->st_size != file->length)
127                 return 0;
128
129         /* if always checksum is set then we use the checksum instead
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];
133                 file_checksum(fn, sum, st->st_size);
134                 return memcmp(sum, file->u.sum, protocol_version < 21 ? 2
135                                                         : MD4_SUM_LENGTH) == 0;
136         }
137
138         if (size_only)
139                 return 1;
140
141         if (ignore_times)
142                 return 0;
143
144         return cmp_modtime(st->st_mtime, file->modtime) == 0;
145 }
146
147
148 /*
149  * set (initialize) the size entries in the per-file sum_struct
150  * calculating dynamic block and checksum sizes.
151  *
152  * This is only called from generate_and_send_sums() but is a separate
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  */
165 static void sum_sizes_sqroot(struct sum_struct *sum, int64 len)
166 {
167         int32 blength;
168         int s2length;
169
170         if (block_size)
171                 blength = block_size;
172         else if (len <= BLOCK_SIZE * BLOCK_SIZE)
173                 blength = BLOCK_SIZE;
174         else {
175                 int32 c;
176                 int64 l;
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;
185                             if (len < (int64)blength * blength)
186                                     blength &= ~c;
187                             c >>= 1;
188                     } while (c >= 8);   /* round to multiple of 8 */
189                     blength = MAX(blength, BLOCK_SIZE);
190                 }
191         }
192
193         if (protocol_version < 27) {
194                 s2length = csum_length;
195         } else if (csum_length == SUM_LENGTH) {
196                 s2length = SUM_LENGTH;
197         } else {
198                 int32 c;
199                 int64 l;
200                 int b = BLOCKSUM_BIAS;
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-- */
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) {
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,
219                         sum->s2length, (double)sum->flength);
220         }
221 }
222
223
224 /*
225  * Generate and send a stream of signatures/checksums that describe a buffer
226  *
227  * Generate approximately one checksum every block_len bytes.
228  */
229 static void generate_and_send_sums(int fd, OFF_T len, int f_out, int f_copy)
230 {
231         int32 i;
232         struct map_struct *mapbuf;
233         struct sum_struct sum;
234         OFF_T offset = 0;
235
236         sum_sizes_sqroot(&sum, len);
237
238         if (len > 0)
239                 mapbuf = map_file(fd, len, MAX_MAP_SIZE, sum.blength);
240         else
241                 mapbuf = NULL;
242
243         write_sum_head(f_out, &sum);
244
245         for (i = 0; i < sum.count; i++) {
246                 int32 n1 = (int32)MIN(len, (OFF_T)sum.blength);
247                 char *map = map_ptr(mapbuf, offset, n1);
248                 uint32 sum1 = get_checksum1(map, n1);
249                 char sum2[SUM_LENGTH];
250
251                 if (f_copy >= 0)
252                         full_write(f_copy, map, n1);
253
254                 get_checksum2(map, n1, sum2);
255
256                 if (verbose > 3) {
257                         rprintf(FINFO,
258                                 "chunk[%.0f] offset=%.0f len=%ld sum1=%08lx\n",
259                                 (double)i, (double)offset, (long)n1,
260                                 (unsigned long)sum1);
261                 }
262                 write_int(f_out, sum1);
263                 write_buf(f_out, sum2, sum.s2length);
264                 len -= n1;
265                 offset += n1;
266         }
267
268         if (mapbuf)
269                 unmap_file(mapbuf);
270 }
271
272
273 /* Try to find a filename in the same dir as "fname" with a similar name. */
274 static 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
326
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.
332  *
333  * Note that f_out is set to -1 when doing final directory-permission and
334  * modification-time repair. */
335 static void recv_generator(char *fname, struct file_list *flist,
336                            struct file_struct *file, int ndx,
337                            int f_out, int f_out_name)
338 {
339         static int missing_below = -1;
340         static char *fuzzy_dirname = NULL;
341         static struct file_list *fuzzy_dirlist = NULL;
342         struct file_struct *fuzzy_file = NULL;
343         int fd = -1, f_copy = -1;
344         STRUCT_STAT st, partial_st;
345         struct file_struct *back_file = NULL;
346         int statret, stat_errno;
347         char *fnamecmp, *partialptr, *backupptr = NULL;
348         char fnamecmpbuf[MAXPATHLEN];
349         uchar fnamecmp_type;
350         int maybe_DEL_TERSE = itemize_changes ? 0 : DEL_TERSE;
351         int maybe_PERMS_REPORT = itemize_changes ? 0 : PERMS_REPORT;
352
353         if (list_only)
354                 return;
355
356         if (!fname) {
357                 if (fuzzy_dirlist) {
358                         flist_free(fuzzy_dirlist);
359                         fuzzy_dirlist = NULL;
360                         fuzzy_dirname = NULL;
361                 }
362                 if (missing_below >= 0) {
363                         dry_run--;
364                         missing_below = -1;
365                 }
366                 return;
367         }
368
369         if (verbose > 2) {
370                 rprintf(FINFO, "recv_generator(%s,%d)\n",
371                         safe_fname(fname), ndx);
372         }
373
374         if (server_filter_list.head
375             && check_filter(&server_filter_list, fname,
376                             S_ISDIR(file->mode)) < 0) {
377                 if (verbose) {
378                         rprintf(FINFO, "skipping server-excluded file \"%s\"\n",
379                                 safe_fname(fname));
380                 }
381                 return;
382         }
383
384         if (missing_below >= 0 && file->dir.depth <= missing_below) {
385                 dry_run--;
386                 missing_below = -1;
387         }
388         if (dry_run > 1) {
389                 statret = -1;
390                 stat_errno = ENOENT;
391         } else {
392                 if (fuzzy_basis && S_ISREG(file->mode)) {
393                         char *dn = file->dirname ? file->dirname : ".";
394                         /* Yes, identical dirnames are guaranteed to have
395                          * identical pointers at this point. */
396                         if (fuzzy_dirname != dn) {
397                                 if (fuzzy_dirlist)
398                                         flist_free(fuzzy_dirlist);
399                                 fuzzy_dirname = dn;
400                                 fuzzy_dirlist = get_dirlist(fuzzy_dirname, 1);
401                         }
402                 }
403
404                 statret = link_stat(fname, &st,
405                                     keep_dirlinks && S_ISDIR(file->mode));
406                 stat_errno = errno;
407         }
408
409         if (only_existing && statret == -1 && stat_errno == ENOENT) {
410                 /* we only want to update existing files */
411                 if (verbose > 1) {
412                         rprintf(FINFO, "not creating new file \"%s\"\n",
413                                 safe_fname(fname));
414                 }
415                 return;
416         }
417
418         if (statret == 0 && !preserve_perms
419             && S_ISDIR(st.st_mode) == S_ISDIR(file->mode)) {
420                 /* if the file exists already and we aren't perserving
421                  * permissions then act as though the remote end sent
422                  * us the file permissions we already have */
423                 file->mode = (file->mode & ~CHMOD_BITS)
424                            | (st.st_mode & CHMOD_BITS);
425         }
426
427         if (S_ISDIR(file->mode)) {
428                 /* The file to be received is a directory, so we need
429                  * to prepare appropriately.  If there is already a
430                  * file of that name and it is *not* a directory, then
431                  * we need to delete it.  If it doesn't exist, then
432                  * (perhaps recursively) create it. */
433                 if (statret == 0 && !S_ISDIR(st.st_mode)) {
434                         delete_file(fname, st.st_mode, maybe_DEL_TERSE);
435                         statret = -1;
436                 }
437                 if (dry_run && statret != 0 && missing_below < 0) {
438                         missing_below = file->dir.depth;
439                         dry_run++;
440                 }
441                 if (protocol_version >= 29 && f_out != -1)
442                         itemize(file, statret, &st, 0, f_out, ndx);
443                 if (statret != 0 && do_mkdir(fname,file->mode) != 0 && errno != EEXIST) {
444                         if (!relative_paths || errno != ENOENT
445                             || create_directory_path(fname, orig_umask) < 0
446                             || do_mkdir(fname, file->mode) < 0) {
447                                 rsyserr(FERROR, errno,
448                                         "recv_generator: mkdir %s failed",
449                                         full_fname(fname));
450                         }
451                 }
452                 if (set_perms(fname, file, statret ? NULL : &st, 0)
453                     && verbose && protocol_version < 29 && f_out != -1)
454                         rprintf(FINFO, "%s/\n", safe_fname(fname));
455                 if (delete_during && f_out != -1 && csum_length != SUM_LENGTH
456                     && (file->flags & FLAG_DEL_HERE))
457                         delete_in_dir(flist, fname, file);
458                 return;
459         }
460         
461         if (max_size && file->length > max_size) {
462                 if (verbose > 1) {
463                         rprintf(FINFO, "%s is over max-size\n",
464                                 safe_fname(fname));
465                 }
466                 return;
467         }
468
469         if (preserve_links && S_ISLNK(file->mode)) {
470 #ifdef SUPPORT_LINKS
471                 if (safe_symlinks && unsafe_symlink(file->u.link, fname)) {
472                         if (verbose) {
473                                 rprintf(FINFO,
474                                         "ignoring unsafe symlink %s -> \"%s\"\n",
475                                         full_fname(fname),
476                                         safe_fname(file->u.link));
477                         }
478                         return;
479                 }
480                 if (statret == 0) {
481                         char lnk[MAXPATHLEN];
482                         int len;
483
484                         if (!S_ISDIR(st.st_mode)
485                             && (len = readlink(fname, lnk, MAXPATHLEN-1)) > 0) {
486                                 lnk[len] = 0;
487                                 /* A link already pointing to the
488                                  * right place -- no further action
489                                  * required. */
490                                 if (strcmp(lnk, file->u.link) == 0) {
491                                         if (protocol_version >= 29) {
492                                                 itemize(file, 0, &st, 0,
493                                                         f_out, ndx);
494                                         }
495                                         set_perms(fname, file, &st,
496                                                   maybe_PERMS_REPORT);
497                                         return;
498                                 }
499                         }
500                         /* Not the right symlink (or not a symlink), so
501                          * delete it. */
502                         if (S_ISLNK(st.st_mode))
503                                 delete_file(fname, st.st_mode, DEL_TERSE);
504                         else {
505                                 delete_file(fname, st.st_mode, maybe_DEL_TERSE);
506                                 statret = -1;
507                         }
508                 }
509                 if (do_symlink(file->u.link,fname) != 0) {
510                         rsyserr(FERROR, errno, "symlink %s -> \"%s\" failed",
511                                 full_fname(fname), safe_fname(file->u.link));
512                 } else {
513                         set_perms(fname,file,NULL,0);
514                         if (protocol_version >= 29) {
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));
520                         }
521                 }
522 #endif
523                 return;
524         }
525
526         if (am_root && preserve_devices && IS_DEVICE(file->mode)) {
527                 if (statret != 0 ||
528                     st.st_mode != file->mode ||
529                     st.st_rdev != file->u.rdev) {
530                         if (IS_DEVICE(st.st_mode))
531                                 delete_file(fname, st.st_mode, DEL_TERSE);
532                         else {
533                                 delete_file(fname, st.st_mode, maybe_DEL_TERSE);
534                                 statret = -1;
535                         }
536                         if (verbose > 2) {
537                                 rprintf(FINFO,"mknod(%s,0%o,0x%x)\n",
538                                         safe_fname(fname),
539                                         (int)file->mode, (int)file->u.rdev);
540                         }
541                         if (do_mknod(fname,file->mode,file->u.rdev) != 0) {
542                                 rsyserr(FERROR, errno, "mknod %s failed",
543                                         full_fname(fname));
544                         } else {
545                                 set_perms(fname,file,NULL,0);
546                                 if (protocol_version >= 29) {
547                                         itemize(file, statret, &st, SID_UPDATING,
548                                                 f_out, ndx);
549                                 } else if (verbose) {
550                                         rprintf(FINFO, "%s\n",
551                                                 safe_fname(fname));
552                                 }
553                         }
554                 } else {
555                         if (protocol_version >= 29) {
556                                 itemize(file, statret, &st, 0,
557                                         f_out, ndx);
558                         }
559                         set_perms(fname, file, &st, maybe_PERMS_REPORT);
560                 }
561                 return;
562         }
563
564         if (preserve_hard_links && hard_link_check(file, HL_CHECK_MASTER))
565                 return;
566
567         if (!S_ISREG(file->mode)) {
568                 rprintf(FINFO, "skipping non-regular file \"%s\"\n",
569                         safe_fname(fname));
570                 return;
571         }
572
573         fnamecmp = fname;
574         fnamecmp_type = FNAMECMP_FNAME;
575
576         if (statret != 0 && basis_dir[0] != NULL) {
577                 int fallback_match = -1;
578                 int match_level = 0;
579                 int i = 0;
580                 do {
581                         pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
582                                  basis_dir[i], fname);
583                         if (link_stat(fnamecmpbuf, &st, 0) == 0
584                             && S_ISREG(st.st_mode)) {
585                                 statret = 0;
586                                 if (link_dest) {
587                                         if (!match_level) {
588                                                 fallback_match = i;
589                                                 match_level = 1;
590                                         } else if (match_level == 2
591                                             && !unchanged_attrs(file, &st))
592                                                 continue;
593                                         if (!unchanged_file(fnamecmpbuf, file, &st))
594                                                 continue;
595                                         fallback_match = i;
596                                         match_level = 2;
597                                         if (!unchanged_attrs(file, &st))
598                                                 continue;
599                                 }
600                                 match_level = 3;
601                                 break;
602                         }
603                 } while (basis_dir[++i] != NULL);
604                 if (statret == 0) {
605                         if (match_level < 3) {
606                                 i = fallback_match;
607                                 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
608                                          basis_dir[i], fname);
609                         }
610 #ifdef HAVE_LINK
611                         if (link_dest && match_level == 3 && !dry_run) {
612                                 if (do_link(fnamecmpbuf, fname) < 0) {
613                                         if (verbose) {
614                                                 rsyserr(FINFO, errno,
615                                                         "link %s => %s",
616                                                         full_fname(fnamecmpbuf),
617                                                         safe_fname(fname));
618                                         }
619                                         fnamecmp = fnamecmpbuf;
620                                         fnamecmp_type = i;
621                                 }
622                         } else
623 #endif
624                         {
625                                 fnamecmp = fnamecmpbuf;
626                                 fnamecmp_type = i;
627                         }
628                 }
629         }
630
631         if (statret == 0 && !S_ISREG(st.st_mode)) {
632                 if (delete_file(fname, st.st_mode, maybe_DEL_TERSE) != 0)
633                         return;
634                 statret = -1;
635                 stat_errno = ENOENT;
636         }
637
638         if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL
639             && link_stat(partialptr, &partial_st, 0) == 0
640             && S_ISREG(partial_st.st_mode)) {
641                 if (statret != 0)
642                         goto prepare_to_open;
643         } else
644                 partialptr = NULL;
645
646         if (statret != 0 && fuzzy_basis && dry_run <= 1) {
647                 int j = find_fuzzy(file, fuzzy_dirlist);
648                 if (j >= 0) {
649                         fuzzy_file = fuzzy_dirlist->files[j];
650                         f_name_to(fuzzy_file, fnamecmpbuf);
651                         if (verbose > 2) {
652                                 rprintf(FINFO, "fuzzy basis selected for %s: %s\n",
653                                         safe_fname(fname), safe_fname(fnamecmpbuf));
654                         }
655                         st.st_mode = fuzzy_file->mode;
656                         st.st_size = fuzzy_file->length;
657                         st.st_mtime = fuzzy_file->modtime;
658                         statret = 0;
659                         fnamecmp = fnamecmpbuf;
660                         fnamecmp_type = FNAMECMP_FUZZY;
661                 }
662         }
663
664         if (statret != 0) {
665                 if (preserve_hard_links && hard_link_check(file, HL_SKIP))
666                         return;
667                 if (stat_errno == ENOENT)
668                         goto notify_others;
669                 if (verbose > 1) {
670                         rsyserr(FERROR, stat_errno,
671                                 "recv_generator: failed to stat %s",
672                                 full_fname(fname));
673                 }
674                 return;
675         }
676
677         if (opt_ignore_existing && fnamecmp_type == FNAMECMP_FNAME) {
678                 if (verbose > 1)
679                         rprintf(FINFO, "%s exists\n", safe_fname(fname));
680                 return;
681         }
682
683         if (update_only && fnamecmp_type == FNAMECMP_FNAME
684             && cmp_modtime(st.st_mtime, file->modtime) > 0) {
685                 if (verbose > 1)
686                         rprintf(FINFO, "%s is newer\n", safe_fname(fname));
687                 return;
688         }
689
690         if (!compare_dest && fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
691                 ;
692         else if (fnamecmp_type == FNAMECMP_FUZZY)
693                 ;
694         else if (unchanged_file(fnamecmp, file, &st)) {
695                 if (protocol_version >= 29) {
696                         itemize(file, statret, &st,
697                                 fnamecmp_type == FNAMECMP_FNAME
698                                                ? 0 : SID_NO_DEST_AND_NO_UPDATE,
699                                 f_out, ndx);
700                 }
701                 if (fnamecmp_type == FNAMECMP_FNAME)
702                         set_perms(fname, file, &st, maybe_PERMS_REPORT);
703                 return;
704         }
705
706 prepare_to_open:
707         if (partialptr) {
708                 st = partial_st;
709                 fnamecmp = partialptr;
710                 fnamecmp_type = FNAMECMP_PARTIAL_DIR;
711                 statret = 0;
712         }
713
714         if (dry_run || read_batch)
715                 goto notify_others;
716         if (whole_file > 0) {
717                 if (statret == 0)
718                         statret = 1;
719                 goto notify_others;
720         }
721
722         if (fuzzy_basis) {
723                 int j = flist_find(fuzzy_dirlist, file);
724                 if (j >= 0) /* don't use changing file as future fuzzy basis */
725                         fuzzy_dirlist->files[j]->flags |= FLAG_NO_FUZZY;
726         }
727
728         /* open the file */
729         fd = do_open(fnamecmp, O_RDONLY, 0);
730
731         if (fd == -1) {
732                 rsyserr(FERROR, errno, "failed to open %s, continuing",
733                         full_fname(fnamecmp));
734             pretend_missing:
735                 /* pretend the file didn't exist */
736                 if (preserve_hard_links && hard_link_check(file, HL_SKIP))
737                         return;
738                 statret = -1;
739                 goto notify_others;
740         }
741
742         if (inplace && make_backups) {
743                 if (!(backupptr = get_backup_name(fname))) {
744                         close(fd);
745                         return;
746                 }
747                 if (!(back_file = make_file(fname, NULL, NO_FILTERS))) {
748                         close(fd);
749                         goto pretend_missing;
750                 }
751                 if (robust_unlink(backupptr) && errno != ENOENT) {
752                         rsyserr(FERROR, errno, "unlink %s",
753                                 full_fname(backupptr));
754                         free(back_file);
755                         close(fd);
756                         return;
757                 }
758                 if ((f_copy = do_open(backupptr,
759                     O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0) {
760                         rsyserr(FERROR, errno, "open %s",
761                                 full_fname(backupptr));
762                         free(back_file);
763                         close(fd);
764                         return;
765                 }
766                 fnamecmp_type = FNAMECMP_BACKUP;
767         }
768
769         if (verbose > 3) {
770                 rprintf(FINFO, "gen mapped %s of size %.0f\n",
771                         safe_fname(fnamecmp), (double)st.st_size);
772         }
773
774         if (verbose > 2)
775                 rprintf(FINFO, "generating and sending sums for %d\n", ndx);
776
777 notify_others:
778         write_int(f_out, ndx);
779         if (protocol_version >= 29) {
780                 itemize(file, statret, &st, SID_UPDATING
781                         | (always_checksum ? SID_REPORT_CHECKSUM : 0),
782                         f_out, -1);
783                 if (inplace && !read_batch)
784                         write_byte(f_out, fnamecmp_type);
785         }
786         if (f_out_name >= 0) {
787                 write_byte(f_out_name, fnamecmp_type);
788                 if (fnamecmp_type == FNAMECMP_FUZZY) {
789                         uchar lenbuf[3], *lb = lenbuf;
790                         int len = strlen(fuzzy_file->basename);
791                         if (len > 0x7F) {
792 #if MAXPATHLEN > 0x7FFF
793                                 *lb++ = len / 0x10000 + 0x80;
794                                 *lb++ = len / 0x100;
795 #else
796                                 *lb++ = len / 0x100 + 0x80;
797 #endif
798                         }
799                         *lb = len;
800                         write_buf(f_out_name, lenbuf, lb - lenbuf + 1);
801                         write_buf(f_out_name, fuzzy_file->basename, len);
802                 }
803         }
804
805         if (dry_run || read_batch)
806                 return;
807
808         if (statret == 0) {
809                 generate_and_send_sums(fd, st.st_size, f_out, f_copy);
810
811                 if (f_copy >= 0) {
812                         close(f_copy);
813                         set_perms(backupptr, back_file, NULL, 0);
814                         if (verbose > 1) {
815                                 rprintf(FINFO, "backed up %s to %s\n",
816                                         safe_fname(fname), safe_fname(backupptr));
817                         }
818                         free(back_file);
819                 }
820
821                 close(fd);
822         } else
823                 write_sum_head(f_out, NULL);
824 }
825
826
827 void generate_files(int f_out, struct file_list *flist, char *local_name,
828                     int f_out_name)
829 {
830         int i;
831         int phase = 0;
832         char fbuf[MAXPATHLEN];
833         int need_retouch_dir_times = preserve_times && !omit_dir_times;
834         int need_retouch_dir_perms = 0;
835         int save_only_existing = only_existing;
836         int save_opt_ignore_existing = opt_ignore_existing;
837
838         if (verbose > 2) {
839                 rprintf(FINFO, "generator starting pid=%ld count=%d\n",
840                         (long)getpid(), flist->count);
841         }
842
843         if (verbose >= 2) {
844                 rprintf(FINFO,
845                         whole_file > 0
846                         ? "delta-transmission disabled for local transfer or --whole-file\n"
847                         : "delta transmission enabled\n");
848         }
849
850         /* We expect to just sit around now, so don't exit on a timeout.
851          * If we really get a timeout then the other process should exit. */
852         io_timeout = 0;
853
854         for (i = 0; i < flist->count; i++) {
855                 struct file_struct *file = flist->files[i];
856                 struct file_struct copy;
857
858                 if (!file->basename)
859                         continue;
860
861                 /* We need to ensure that any dirs we create have writeable
862                  * permissions during the time we are putting files within
863                  * them.  This is then fixed after the transfer is done. */
864                 if (!am_root && S_ISDIR(file->mode) && !(file->mode & S_IWUSR)) {
865                         copy = *file;
866                         copy.mode |= S_IWUSR; /* user write */
867                         file = &copy;
868                         need_retouch_dir_perms = 1;
869                 }
870
871                 recv_generator(local_name ? local_name : f_name_to(file, fbuf),
872                                flist, file, i, f_out, f_out_name);
873         }
874         recv_generator(NULL, NULL, NULL, 0, -1, -1);
875         if (delete_during)
876                 delete_in_dir(NULL, NULL, NULL);
877
878         phase++;
879         csum_length = SUM_LENGTH;
880         only_existing = max_size = opt_ignore_existing = 0;
881         update_only = always_checksum = size_only = 0;
882         ignore_times = 1;
883
884         if (verbose > 2)
885                 rprintf(FINFO,"generate_files phase=%d\n",phase);
886
887         write_int(f_out, -1);
888
889         /* files can cycle through the system more than once
890          * to catch initial checksum errors */
891         while ((i = get_redo_num()) != -1) {
892                 struct file_struct *file = flist->files[i];
893                 recv_generator(local_name ? local_name : f_name_to(file, fbuf),
894                                flist, file, i, f_out, f_out_name);
895         }
896
897         phase++;
898         only_existing = save_only_existing;
899         opt_ignore_existing = save_opt_ignore_existing;
900
901         if (verbose > 2)
902                 rprintf(FINFO,"generate_files phase=%d\n",phase);
903
904         write_int(f_out, -1);
905
906         /* Read post-redo-phase MSG_DONE and any prior messages. */
907         get_redo_num();
908
909         if (preserve_hard_links)
910                 do_hard_links();
911
912         if ((need_retouch_dir_perms || need_retouch_dir_times)
913             && !list_only && !local_name && !dry_run) {
914                 /* Now we need to fix any directory permissions that were
915                  * modified during the transfer and/or re-set any tweaked
916                  * modified-time values. */
917                 for (i = 0; i < flist->count; i++) {
918                         struct file_struct *file = flist->files[i];
919                         if (!file->basename || !S_ISDIR(file->mode))
920                                 continue;
921                         if (!need_retouch_dir_times && file->mode & S_IWUSR)
922                                 continue;
923                         recv_generator(local_name ? local_name : f_name(file),
924                                        flist, file, i, -1, -1);
925                 }
926         }
927         recv_generator(NULL, NULL, NULL, 0, -1, -1);
928
929         if (verbose > 2)
930                 rprintf(FINFO,"generate_files finished\n");
931 }