blob: 9eeb0d9b4be7636862ee2f45f417feb2f4675052 [file] [log] [blame]
Martin Poolef1aa912000-11-10 03:17:08 +00001/* -*- c-file-style: "linux" -*-
Martin Pool91262d52002-01-23 07:54:13 +00002
3 rsync -- fast file replication program
Wayne Davison2cda2562003-03-21 18:53:00 +00004
5 Copyright (C) 1996-2000 by Andrew Tridgell
Andrew Tridgell2f03f951998-07-25 02:25:22 +00006 Copyright (C) Paul Mackerras 1996
Martin Pool91262d52002-01-23 07:54:13 +00007 Copyright (C) 2002 by Martin Pool <mbp@samba.org>
Wayne Davison2cda2562003-03-21 18:53:00 +00008
Andrew Tridgell2f03f951998-07-25 02:25:22 +00009 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.
Wayne Davison2cda2562003-03-21 18:53:00 +000013
Andrew Tridgell2f03f951998-07-25 02:25:22 +000014 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.
Wayne Davison2cda2562003-03-21 18:53:00 +000018
Andrew Tridgell2f03f951998-07-25 02:25:22 +000019 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;
Wayne Davison716e73d2004-06-05 16:16:30 +000029extern int keep_dirlinks;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000030extern int preserve_links;
31extern int am_root;
32extern int preserve_devices;
33extern int preserve_hard_links;
Wayne Davison6744b622004-01-02 16:50:06 +000034extern int preserve_perms;
35extern int preserve_uid;
36extern int preserve_gid;
Wayne Davison3ea9bbd2005-01-24 06:19:58 +000037extern int preserve_times;
38extern int omit_dir_times;
Wayne Davisonfa13f392005-01-24 01:41:48 +000039extern int delete_during;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000040extern int update_only;
Martin Pool3d6fead2002-01-25 10:42:23 +000041extern int opt_ignore_existing;
Wayne Davisoncd6aa5b2004-09-07 21:44:02 +000042extern int inplace;
43extern int make_backups;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000044extern int csum_length;
45extern int ignore_times;
Andrew Tridgellf83f0541999-02-18 03:48:24 +000046extern int size_only;
Wayne Davison7d1bfaf2004-11-11 01:45:28 +000047extern OFF_T max_size;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000048extern int io_timeout;
Wayne Davisond04e9c52003-09-09 15:58:48 +000049extern int protocol_version;
Wayne Davison8e85be02005-02-14 02:41:27 +000050extern int fuzzy_basis;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000051extern int always_checksum;
Wayne Davisona7260c42004-07-29 16:06:38 +000052extern char *partial_dir;
Wayne Davisonb7e86282004-11-27 17:52:47 +000053extern char *basis_dir[];
Wayne Davison2be2fb32005-01-15 21:17:39 +000054extern int compare_dest;
David Dykstra59c95e42002-08-29 14:44:55 +000055extern int link_dest;
Wayne Davison57747862004-04-15 16:55:23 +000056extern int whole_file;
57extern int local_server;
Wayne Davison57747862004-04-15 16:55:23 +000058extern int list_only;
Wayne Davisonb9f592f2004-07-15 02:20:08 +000059extern int read_batch;
Wayne Davison57747862004-04-15 16:55:23 +000060extern int only_existing;
61extern int orig_umask;
62extern int safe_symlinks;
Wayne Davisona255c592005-01-01 21:08:11 +000063extern long block_size; /* "long" because popt can't set an int32. */
Andrew Tridgell2f03f951998-07-25 02:25:22 +000064
Wayne Davison78424182005-01-25 10:39:14 +000065extern struct filter_list_struct server_filter_list;
Wayne Davison97f9dca2004-05-18 00:14:10 +000066
Wayne Davisonb7e86282004-11-27 17:52:47 +000067static int unchanged_attrs(struct file_struct *file, STRUCT_STAT *st)
68{
69 if (preserve_perms
70 && (st->st_mode & CHMOD_BITS) != (file->mode & CHMOD_BITS))
71 return 0;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000072
Wayne Davisonb7e86282004-11-27 17:52:47 +000073 if (am_root && preserve_uid && st->st_uid != file->uid)
74 return 0;
75
76 if (preserve_gid && file->gid != GID_NONE && st->st_gid != file->gid)
77 return 0;
78
79 return 1;
80}
81
82/* Perform our quick-check heuristic for determining if a file is unchanged. */
83static int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
Andrew Tridgell2f03f951998-07-25 02:25:22 +000084{
Wayne Davisoncc1e9972004-06-23 16:51:21 +000085 if (st->st_size != file->length)
Wayne Davison84acca02004-06-23 21:21:19 +000086 return 0;
David Dykstra59c95e42002-08-29 14:44:55 +000087
Wayne Davison2cda2562003-03-21 18:53:00 +000088 /* if always checksum is set then we use the checksum instead
Andrew Tridgell2f03f951998-07-25 02:25:22 +000089 of the file time to determine whether to sync */
90 if (always_checksum && S_ISREG(st->st_mode)) {
91 char sum[MD4_SUM_LENGTH];
Wayne Davisonb7e86282004-11-27 17:52:47 +000092 file_checksum(fn, sum, st->st_size);
Wayne Davison728d0922004-01-11 07:28:45 +000093 return memcmp(sum, file->u.sum, protocol_version < 21 ? 2
Wayne Davison84acca02004-06-23 21:21:19 +000094 : MD4_SUM_LENGTH) == 0;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000095 }
96
Wayne Davisoncc1e9972004-06-23 16:51:21 +000097 if (size_only)
Wayne Davisoncc1e9972004-06-23 16:51:21 +000098 return 1;
99
Wayne Davison84acca02004-06-23 21:21:19 +0000100 if (ignore_times)
101 return 0;
102
103 return cmp_modtime(st->st_mtime, file->modtime) == 0;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000104}
105
106
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000107/*
J.W. Schultz195bd902003-04-10 02:04:58 +0000108 * set (initialize) the size entries in the per-file sum_struct
Wayne Davisonec8290c2004-06-18 16:30:24 +0000109 * calculating dynamic block and checksum sizes.
J.W. Schultz195bd902003-04-10 02:04:58 +0000110 *
Wayne Davisonec8290c2004-06-18 16:30:24 +0000111 * This is only called from generate_and_send_sums() but is a separate
J.W. Schultz195bd902003-04-10 02:04:58 +0000112 * function to encapsulate the logic.
113 *
114 * The block size is a rounded square root of file length.
115 *
116 * The checksum size is determined according to:
117 * blocksum_bits = BLOCKSUM_EXP + 2*log2(file_len) - log2(block_len)
118 * provided by Donovan Baarda which gives a probability of rsync
119 * algorithm corrupting data and falling back using the whole md4
120 * checksums.
121 *
122 * This might be made one of several selectable heuristics.
123 */
Wayne Davison14908122005-01-20 22:42:47 +0000124static void sum_sizes_sqroot(struct sum_struct *sum, int64 len)
J.W. Schultz195bd902003-04-10 02:04:58 +0000125{
Wayne Davisona255c592005-01-01 21:08:11 +0000126 int32 blength;
Wayne Davisonda9d12f2004-01-03 18:37:41 +0000127 int s2length;
J.W. Schultz195bd902003-04-10 02:04:58 +0000128
Wayne Davisona255c592005-01-01 21:08:11 +0000129 if (block_size)
J.W. Schultz195bd902003-04-10 02:04:58 +0000130 blength = block_size;
Wayne Davisona255c592005-01-01 21:08:11 +0000131 else if (len <= BLOCK_SIZE * BLOCK_SIZE)
J.W. Schultz195bd902003-04-10 02:04:58 +0000132 blength = BLOCK_SIZE;
Wayne Davisona255c592005-01-01 21:08:11 +0000133 else {
134 int32 c;
Wayne Davison14908122005-01-20 22:42:47 +0000135 int64 l;
Wayne Davisoneae71652005-01-17 22:51:21 +0000136 int cnt;
137 for (c = 1, l = len, cnt = 0; l >>= 2; c <<= 1, cnt++) {}
138 if (cnt >= 31 || c >= MAX_BLOCK_SIZE)
139 blength = MAX_BLOCK_SIZE;
140 else {
141 blength = 0;
142 do {
143 blength |= c;
Wayne Davison14908122005-01-20 22:42:47 +0000144 if (len < (int64)blength * blength)
Wayne Davisoneae71652005-01-17 22:51:21 +0000145 blength &= ~c;
146 c >>= 1;
147 } while (c >= 8); /* round to multiple of 8 */
148 blength = MAX(blength, BLOCK_SIZE);
J.W. Schultz195bd902003-04-10 02:04:58 +0000149 }
J.W. Schultz195bd902003-04-10 02:04:58 +0000150 }
151
Wayne Davisond04e9c52003-09-09 15:58:48 +0000152 if (protocol_version < 27) {
J.W. Schultz195bd902003-04-10 02:04:58 +0000153 s2length = csum_length;
154 } else if (csum_length == SUM_LENGTH) {
155 s2length = SUM_LENGTH;
156 } else {
Wayne Davisona255c592005-01-01 21:08:11 +0000157 int32 c;
Wayne Davison14908122005-01-20 22:42:47 +0000158 int64 l;
Wayne Davisonda9d12f2004-01-03 18:37:41 +0000159 int b = BLOCKSUM_BIAS;
Wayne Davisona255c592005-01-01 21:08:11 +0000160 for (l = len; l >>= 1; b += 2) {}
161 for (c = blength; c >>= 1 && b; b--) {}
162 /* add a bit, subtract rollsum, round up. */
163 s2length = (b + 1 - 32 + 7) / 8; /* --optimize in compiler-- */
J.W. Schultz195bd902003-04-10 02:04:58 +0000164 s2length = MAX(s2length, csum_length);
165 s2length = MIN(s2length, SUM_LENGTH);
166 }
167
168 sum->flength = len;
169 sum->blength = blength;
170 sum->s2length = s2length;
171 sum->count = (len + (blength - 1)) / blength;
172 sum->remainder = (len % blength);
173
174 if (sum->count && verbose > 2) {
Wayne Davisona255c592005-01-01 21:08:11 +0000175 rprintf(FINFO,
176 "count=%.0f rem=%ld blength=%ld s2length=%d flength=%.0f\n",
177 (double)sum->count, (long)sum->remainder, (long)sum->blength,
Wayne Davisonda9d12f2004-01-03 18:37:41 +0000178 sum->s2length, (double)sum->flength);
J.W. Schultz195bd902003-04-10 02:04:58 +0000179 }
180}
Wayne Davison80605142002-07-31 21:20:07 +0000181
Martin Poolbceec822002-03-19 03:39:42 +0000182
Wayne Davison80605142002-07-31 21:20:07 +0000183/*
184 * Generate and send a stream of signatures/checksums that describe a buffer
Martin Poole66dfd12002-04-08 09:05:26 +0000185 *
Wayne Davison80605142002-07-31 21:20:07 +0000186 * Generate approximately one checksum every block_len bytes.
187 */
Wayne Davisoncd6aa5b2004-09-07 21:44:02 +0000188static void generate_and_send_sums(int fd, OFF_T len, int f_out, int f_copy)
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000189{
Wayne Davison80605142002-07-31 21:20:07 +0000190 size_t i;
Wayne Davison6e45e1d2004-07-20 21:35:55 +0000191 struct map_struct *mapbuf;
Wayne Davison80605142002-07-31 21:20:07 +0000192 struct sum_struct sum;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000193 OFF_T offset = 0;
194
Wayne Davison423dba82003-12-24 01:14:16 +0000195 sum_sizes_sqroot(&sum, len);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000196
Wayne Davison6e45e1d2004-07-20 21:35:55 +0000197 if (len > 0)
Wayne Davison96d910c2004-08-03 08:05:27 +0000198 mapbuf = map_file(fd, len, MAX_MAP_SIZE, sum.blength);
Wayne Davison6e45e1d2004-07-20 21:35:55 +0000199 else
200 mapbuf = NULL;
201
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000202 write_sum_head(f_out, &sum);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000203
Wayne Davison80605142002-07-31 21:20:07 +0000204 for (i = 0; i < sum.count; i++) {
Wayne Davisona255c592005-01-01 21:08:11 +0000205 int32 n1 = (int32)MIN(len, (OFF_T)sum.blength);
Wayne Davison6e45e1d2004-07-20 21:35:55 +0000206 char *map = map_ptr(mapbuf, offset, n1);
Wayne Davison80605142002-07-31 21:20:07 +0000207 uint32 sum1 = get_checksum1(map, n1);
208 char sum2[SUM_LENGTH];
Martin Poole66dfd12002-04-08 09:05:26 +0000209
Wayne Davisoncd6aa5b2004-09-07 21:44:02 +0000210 if (f_copy >= 0)
211 full_write(f_copy, map, n1);
212
Wayne Davison80605142002-07-31 21:20:07 +0000213 get_checksum2(map, n1, sum2);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000214
Wayne Davison80605142002-07-31 21:20:07 +0000215 if (verbose > 3) {
Martin Poole66dfd12002-04-08 09:05:26 +0000216 rprintf(FINFO,
Wayne Davisona255c592005-01-01 21:08:11 +0000217 "chunk[%.0f] offset=%.0f len=%ld sum1=%08lx\n",
218 (double)i, (double)offset, (long)n1,
Wayne Davison0e36d9d2004-01-03 19:28:03 +0000219 (unsigned long)sum1);
Wayne Davison80605142002-07-31 21:20:07 +0000220 }
221 write_int(f_out, sum1);
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000222 write_buf(f_out, sum2, sum.s2length);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000223 len -= n1;
224 offset += n1;
225 }
Wayne Davison6e45e1d2004-07-20 21:35:55 +0000226
227 if (mapbuf)
228 unmap_file(mapbuf);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000229}
230
Wayne Davison8e85be02005-02-14 02:41:27 +0000231/* Try to find a filename in the same dir as "fname" with a similar name. */
232static int find_fuzzy(struct file_struct *file, struct file_list *dirlist)
233{
234 int fname_len, fname_suf_len;
235 const char *fname_suf, *fname = file->basename;
236 uint32 lowest_dist = 0x7FFFFFFF;
237 int j, lowest_j = -1;
238
239 fname_len = strlen(fname);
240 fname_suf = find_filename_suffix(fname, fname_len, &fname_suf_len);
241
242 for (j = 0; j < dirlist->count; j++) {
243 struct file_struct *fp = dirlist->files[j];
244 const char *suf, *name;
245 int len, suf_len;
246 uint32 dist;
247
248 if (!S_ISREG(fp->mode) || !fp->length
249 || fp->flags & FLAG_NO_FUZZY)
250 continue;
251
252 name = fp->basename;
253
254 if (fp->length == file->length
255 && fp->modtime == file->modtime) {
256 if (verbose > 4) {
257 rprintf(FINFO,
258 "fuzzy size/modtime match for %s\n",
259 name);
260 }
261 return j;
262 }
263
264 len = strlen(name);
265 suf = find_filename_suffix(name, len, &suf_len);
266
267 dist = fuzzy_distance(name, len, fname, fname_len);
268 /* Add some extra weight to how well the suffixes match. */
269 dist += fuzzy_distance(suf, suf_len, fname_suf, fname_suf_len)
270 * 10;
271 if (verbose > 4) {
272 rprintf(FINFO, "fuzzy distance for %s = %d.%05d\n",
273 name, (int)(dist>>16), (int)(dist&0xFFFF));
274 }
275 if (dist <= lowest_dist) {
276 lowest_dist = dist;
277 lowest_j = j;
278 }
279 }
280
281 return lowest_j;
282}
283
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000284
Wayne Davison0492fdf2005-02-03 01:19:01 +0000285/* Acts on flist->file's ndx'th item, whose name is fname. If a directory,
286 * make sure it exists, and has the right permissions/timestamp info. For
287 * all other non-regular files (symlinks, etc.) we create them here. For
288 * regular files that have changed, we try to find a basis file and then
289 * start sending checksums.
Martin Poolef1aa912000-11-10 03:17:08 +0000290 *
Wayne Davison0492fdf2005-02-03 01:19:01 +0000291 * Note that f_out is set to -1 when doing final directory-permission and
292 * modification-time repair. */
Wayne Davisonfa13f392005-01-24 01:41:48 +0000293static void recv_generator(char *fname, struct file_list *flist,
Wayne Davison0492fdf2005-02-03 01:19:01 +0000294 struct file_struct *file, int ndx,
Wayne Davison41cfde62004-11-03 20:30:31 +0000295 int f_out, int f_out_name)
Wayne Davison2cda2562003-03-21 18:53:00 +0000296{
Wayne Davisondf337832005-02-12 21:18:58 +0000297 static int missing_below = -1;
Wayne Davison8e85be02005-02-14 02:41:27 +0000298 static char *fuzzy_dirname = NULL;
299 static struct file_list *fuzzy_dirlist = NULL;
300 struct file_struct *fuzzy_file = NULL;
Wayne Davison41cfde62004-11-03 20:30:31 +0000301 int fd = -1, f_copy = -1;
Wayne Davison89f7eff2004-09-07 16:50:07 +0000302 STRUCT_STAT st, partial_st;
Wayne Davison41cfde62004-11-03 20:30:31 +0000303 struct file_struct *back_file = NULL;
Wayne Davisone7d13fe2004-07-28 10:04:06 +0000304 int statret, stat_errno;
Wayne Davison41cfde62004-11-03 20:30:31 +0000305 char *fnamecmp, *partialptr, *backupptr = NULL;
David Dykstra375a4551998-10-26 21:42:38 +0000306 char fnamecmpbuf[MAXPATHLEN];
Wayne Davison41cfde62004-11-03 20:30:31 +0000307 uchar fnamecmp_type;
Andrew Tridgellf7632fc1998-11-02 00:52:01 +0000308
Wayne Davisondfd5ba62004-01-04 18:43:18 +0000309 if (list_only)
310 return;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000311
Wayne Davison8e85be02005-02-14 02:41:27 +0000312 if (!fname) {
313 if (fuzzy_dirlist) {
314 flist_free(fuzzy_dirlist);
315 fuzzy_dirlist = NULL;
316 fuzzy_dirname = NULL;
317 }
318 if (missing_below >= 0) {
319 dry_run--;
320 missing_below = -1;
321 }
322 return;
323 }
324
Wayne Davison4875d6b2005-02-07 20:36:43 +0000325 if (verbose > 2) {
326 rprintf(FINFO, "recv_generator(%s,%d)\n",
327 safe_fname(fname), ndx);
328 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000329
Wayne Davison78424182005-01-25 10:39:14 +0000330 if (server_filter_list.head
331 && check_filter(&server_filter_list, fname,
332 S_ISDIR(file->mode)) < 0) {
Wayne Davison3e35c342004-05-18 08:50:17 +0000333 if (verbose) {
334 rprintf(FINFO, "skipping server-excluded file \"%s\"\n",
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000335 safe_fname(fname));
Wayne Davison3e35c342004-05-18 08:50:17 +0000336 }
Wayne Davison97f9dca2004-05-18 00:14:10 +0000337 return;
Wayne Davison3e35c342004-05-18 08:50:17 +0000338 }
Wayne Davison97f9dca2004-05-18 00:14:10 +0000339
Wayne Davison8e85be02005-02-14 02:41:27 +0000340 if (missing_below >= 0 && file->dir.depth <= missing_below) {
Wayne Davisondf337832005-02-12 21:18:58 +0000341 dry_run--;
342 missing_below = -1;
343 }
Wayne Davison73f7af02004-08-05 18:18:36 +0000344 if (dry_run > 1) {
345 statret = -1;
346 stat_errno = ENOENT;
347 } else {
Wayne Davison8e85be02005-02-14 02:41:27 +0000348 if (fuzzy_basis && S_ISREG(file->mode)) {
349 char *dn = file->dirname ? file->dirname : ".";
350 /* Yes, identical dirnames are guaranteed to have
351 * identical pointers at this point. */
352 if (fuzzy_dirname != dn) {
353 if (fuzzy_dirlist)
354 flist_free(fuzzy_dirlist);
355 fuzzy_dirname = dn;
356 fuzzy_dirlist = get_dirlist(fuzzy_dirname, 1);
357 }
358 }
359
Wayne Davison73f7af02004-08-05 18:18:36 +0000360 statret = link_stat(fname, &st,
361 keep_dirlinks && S_ISDIR(file->mode));
362 stat_errno = errno;
363 }
Martin Pool63787382001-12-14 02:16:20 +0000364
Wayne Davisona7260c42004-07-29 16:06:38 +0000365 if (only_existing && statret == -1 && stat_errno == ENOENT) {
Andrew Tridgell1347d512000-01-23 01:53:18 +0000366 /* we only want to update existing files */
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000367 if (verbose > 1) {
368 rprintf(FINFO, "not creating new file \"%s\"\n",
369 safe_fname(fname));
370 }
Andrew Tridgell1347d512000-01-23 01:53:18 +0000371 return;
372 }
373
Wayne Davisond9b4d262004-07-23 16:59:38 +0000374 if (statret == 0 && !preserve_perms
375 && S_ISDIR(st.st_mode) == S_ISDIR(file->mode)) {
Andrew Tridgell4df9f361999-10-31 04:28:03 +0000376 /* if the file exists already and we aren't perserving
Wayne Davison2cda2562003-03-21 18:53:00 +0000377 * permissions then act as though the remote end sent
378 * us the file permissions we already have */
Wayne Davison67e78a82004-03-07 20:29:59 +0000379 file->mode = (file->mode & ~CHMOD_BITS)
380 | (st.st_mode & CHMOD_BITS);
Andrew Tridgell4df9f361999-10-31 04:28:03 +0000381 }
382
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000383 if (S_ISDIR(file->mode)) {
Wayne Davison2cda2562003-03-21 18:53:00 +0000384 /* The file to be received is a directory, so we need
385 * to prepare appropriately. If there is already a
386 * file of that name and it is *not* a directory, then
387 * we need to delete it. If it doesn't exist, then
Wayne Davison027428e2005-01-20 22:59:54 +0000388 * (perhaps recursively) create it. */
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000389 if (statret == 0 && !S_ISDIR(st.st_mode)) {
Wayne Davison7e384102005-01-19 20:10:57 +0000390 delete_file(fname, DEL_TERSE);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000391 statret = -1;
392 }
Wayne Davisondf337832005-02-12 21:18:58 +0000393 if (dry_run && statret != 0 && missing_below < 0) {
394 missing_below = file->dir.depth;
395 dry_run++;
396 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000397 if (statret != 0 && do_mkdir(fname,file->mode) != 0 && errno != EEXIST) {
Wayne Davison027428e2005-01-20 22:59:54 +0000398 if (!relative_paths || errno != ENOENT
399 || create_directory_path(fname, orig_umask) < 0
400 || do_mkdir(fname, file->mode) < 0) {
Wayne Davisond62bcc12004-05-15 19:31:10 +0000401 rsyserr(FERROR, errno,
402 "recv_generator: mkdir %s failed",
403 full_fname(fname));
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000404 }
405 }
Wayne Davison716e73d2004-06-05 16:16:30 +0000406 if (set_perms(fname, file, statret ? NULL : &st, 0)
407 && verbose && f_out != -1)
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000408 rprintf(FINFO, "%s/\n", safe_fname(fname));
Wayne Davisonc93fad52005-01-30 10:06:18 +0000409 if (delete_during && f_out != -1 && csum_length != SUM_LENGTH
Wayne Davison31937d32005-02-03 02:01:11 +0000410 && (file->flags & FLAG_DEL_HERE))
411 delete_in_dir(flist, fname, file);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000412 return;
Wayne Davison7d1bfaf2004-11-11 01:45:28 +0000413 } else if (max_size && file->length > max_size) {
Wayne Davison4875d6b2005-02-07 20:36:43 +0000414 if (verbose > 1) {
415 rprintf(FINFO, "%s is over max-size\n",
416 safe_fname(fname));
417 }
Wayne Davison7d1bfaf2004-11-11 01:45:28 +0000418 return;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000419 }
420
421 if (preserve_links && S_ISLNK(file->mode)) {
Wayne Davison4f5b0752005-02-14 00:53:43 +0000422#ifdef SUPPORT_LINKS
Wayne Davison728d0922004-01-11 07:28:45 +0000423 if (safe_symlinks && unsafe_symlink(file->u.link, fname)) {
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000424 if (verbose) {
Wayne Davison4875d6b2005-02-07 20:36:43 +0000425 rprintf(FINFO,
426 "ignoring unsafe symlink %s -> \"%s\"\n",
427 full_fname(fname),
428 safe_fname(file->u.link));
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000429 }
430 return;
431 }
432 if (statret == 0) {
Wayne Davison7e384102005-01-19 20:10:57 +0000433 int dflag = S_ISDIR(st.st_mode) ? DEL_DIR : 0;
434 char lnk[MAXPATHLEN];
435 int len;
436
437 if (!dflag
438 && (len = readlink(fname, lnk, MAXPATHLEN-1)) > 0) {
439 lnk[len] = 0;
Martin Pool85d4d142001-12-14 02:14:58 +0000440 /* A link already pointing to the
441 * right place -- no further action
442 * required. */
Wayne Davison7e384102005-01-19 20:10:57 +0000443 if (strcmp(lnk, file->u.link) == 0) {
Wayne Davisonc41b52c2004-05-13 06:55:01 +0000444 set_perms(fname, file, &st,
445 PERMS_REPORT);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000446 return;
447 }
Wayne Davison2cda2562003-03-21 18:53:00 +0000448 }
Wayne Davison7e384102005-01-19 20:10:57 +0000449 /* Not the right symlink (or not a symlink), so
450 * delete it. */
451 delete_file(fname, dflag | DEL_TERSE);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000452 }
Wayne Davison728d0922004-01-11 07:28:45 +0000453 if (do_symlink(file->u.link,fname) != 0) {
Wayne Davisond62bcc12004-05-15 19:31:10 +0000454 rsyserr(FERROR, errno, "symlink %s -> \"%s\" failed",
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000455 full_fname(fname), safe_fname(file->u.link));
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000456 } else {
457 set_perms(fname,file,NULL,0);
458 if (verbose) {
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000459 rprintf(FINFO, "%s -> %s\n", safe_fname(fname),
460 safe_fname(file->u.link));
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000461 }
462 }
463#endif
464 return;
465 }
466
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000467 if (am_root && preserve_devices && IS_DEVICE(file->mode)) {
Wayne Davison2cda2562003-03-21 18:53:00 +0000468 if (statret != 0 ||
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000469 st.st_mode != file->mode ||
Wayne Davison3915fd72004-04-12 16:52:43 +0000470 st.st_rdev != file->u.rdev) {
Wayne Davison7e384102005-01-19 20:10:57 +0000471 int dflag = S_ISDIR(st.st_mode) ? DEL_DIR : 0;
472 delete_file(fname, dflag | DEL_TERSE);
Wayne Davisond62bcc12004-05-15 19:31:10 +0000473 if (verbose > 2) {
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000474 rprintf(FINFO,"mknod(%s,0%o,0x%x)\n",
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000475 safe_fname(fname),
476 (int)file->mode, (int)file->u.rdev);
Wayne Davisond62bcc12004-05-15 19:31:10 +0000477 }
Wayne Davison728d0922004-01-11 07:28:45 +0000478 if (do_mknod(fname,file->mode,file->u.rdev) != 0) {
Wayne Davisond62bcc12004-05-15 19:31:10 +0000479 rsyserr(FERROR, errno, "mknod %s failed",
480 full_fname(fname));
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000481 } else {
482 set_perms(fname,file,NULL,0);
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000483 if (verbose) {
484 rprintf(FINFO, "%s\n",
485 safe_fname(fname));
486 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000487 }
488 } else {
Wayne Davisonc41b52c2004-05-13 06:55:01 +0000489 set_perms(fname, file, &st, PERMS_REPORT);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000490 }
491 return;
492 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000493
Wayne Davison6dff5992004-01-25 22:32:09 +0000494 if (preserve_hard_links && hard_link_check(file, HL_CHECK_MASTER))
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000495 return;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000496
497 if (!S_ISREG(file->mode)) {
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000498 rprintf(FINFO, "skipping non-regular file \"%s\"\n",
499 safe_fname(fname));
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000500 return;
501 }
502
David Dykstra375a4551998-10-26 21:42:38 +0000503 fnamecmp = fname;
Wayne Davison41cfde62004-11-03 20:30:31 +0000504 fnamecmp_type = FNAMECMP_FNAME;
David Dykstra375a4551998-10-26 21:42:38 +0000505
Wayne Davisonb7e86282004-11-27 17:52:47 +0000506 if (statret == -1 && basis_dir[0] != NULL) {
507 int fallback_match = -1;
508 int match_level = 0;
509 int i = 0;
510 do {
511 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
512 basis_dir[i], fname);
513 if (link_stat(fnamecmpbuf, &st, 0) == 0
514 && S_ISREG(st.st_mode)) {
515 statret = 0;
516 if (link_dest) {
517 if (!match_level) {
518 fallback_match = i;
519 match_level = 1;
520 } else if (match_level == 2
521 && !unchanged_attrs(file, &st))
522 continue;
523 if (!unchanged_file(fnamecmpbuf, file, &st))
524 continue;
525 fallback_match = i;
526 match_level = 2;
527 if (!unchanged_attrs(file, &st))
528 continue;
529 }
530 match_level = 3;
531 break;
532 }
533 } while (basis_dir[++i] != NULL);
534 if (statret == 0) {
535 if (match_level < 3) {
536 i = fallback_match;
537 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
538 basis_dir[i], fname);
539 }
Wayne Davison4f5b0752005-02-14 00:53:43 +0000540#ifdef HAVE_LINK
Wayne Davisonb7e86282004-11-27 17:52:47 +0000541 if (link_dest && match_level == 3 && !dry_run) {
Wayne Davisone7d13fe2004-07-28 10:04:06 +0000542 if (do_link(fnamecmpbuf, fname) < 0) {
543 if (verbose) {
544 rsyserr(FINFO, errno,
545 "link %s => %s",
Wayne Davison4875d6b2005-02-07 20:36:43 +0000546 full_fname(fnamecmpbuf),
Wayne Davisone7d13fe2004-07-28 10:04:06 +0000547 safe_fname(fname));
548 }
549 fnamecmp = fnamecmpbuf;
Wayne Davison2be2fb32005-01-15 21:17:39 +0000550 fnamecmp_type = i;
Wayne Davisone7bc9b62004-01-02 08:05:20 +0000551 }
Wayne Davisone7d13fe2004-07-28 10:04:06 +0000552 } else
David Dykstra59c95e42002-08-29 14:44:55 +0000553#endif
Wayne Davison41cfde62004-11-03 20:30:31 +0000554 {
Wayne Davisone7d13fe2004-07-28 10:04:06 +0000555 fnamecmp = fnamecmpbuf;
Wayne Davison2be2fb32005-01-15 21:17:39 +0000556 fnamecmp_type = i;
Wayne Davison41cfde62004-11-03 20:30:31 +0000557 }
Wayne Davisone7d13fe2004-07-28 10:04:06 +0000558 }
559 }
560
561 if (statret == 0 && !S_ISREG(st.st_mode)) {
Wayne Davison7e384102005-01-19 20:10:57 +0000562 int dflag = S_ISDIR(st.st_mode) ? DEL_DIR : 0;
563 if (delete_file(fname, dflag | DEL_TERSE) != 0)
Wayne Davisone7d13fe2004-07-28 10:04:06 +0000564 return;
565 statret = -1;
566 stat_errno = ENOENT;
David Dykstra375a4551998-10-26 21:42:38 +0000567 }
568
Wayne Davison9d954dc2005-01-14 18:20:46 +0000569 if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL
Wayne Davison72c19bb2004-09-07 17:03:51 +0000570 && link_stat(partialptr, &partial_st, 0) == 0
571 && S_ISREG(partial_st.st_mode)) {
572 if (statret == -1)
573 goto prepare_to_open;
574 } else
575 partialptr = NULL;
Wayne Davison89f7eff2004-09-07 16:50:07 +0000576
Wayne Davison8e85be02005-02-14 02:41:27 +0000577 if (statret == -1 && fuzzy_basis && dry_run <= 1) {
578 int j = find_fuzzy(file, fuzzy_dirlist);
579 if (j >= 0) {
580 fuzzy_file = fuzzy_dirlist->files[j];
581 f_name_to(fuzzy_file, fnamecmpbuf);
582 if (verbose > 2) {
583 rprintf(FINFO, "fuzzy basis selected for %s: %s\n",
584 safe_fname(fname), safe_fname(fnamecmpbuf));
585 }
586 st.st_mode = fuzzy_file->mode;
587 st.st_size = fuzzy_file->length;
588 st.st_mtime = fuzzy_file->modtime;
589 statret = 0;
590 fnamecmp = fnamecmpbuf;
591 fnamecmp_type = FNAMECMP_FUZZY;
592 }
593 }
594
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000595 if (statret == -1) {
Wayne Davison6dff5992004-01-25 22:32:09 +0000596 if (preserve_hard_links && hard_link_check(file, HL_SKIP))
597 return;
Wayne Davison41cfde62004-11-03 20:30:31 +0000598 if (stat_errno == ENOENT)
599 goto notify_others;
600 if (verbose > 1) {
Wayne Davisone7d13fe2004-07-28 10:04:06 +0000601 rsyserr(FERROR, stat_errno,
602 "recv_generator: failed to stat %s",
Wayne Davisond62bcc12004-05-15 19:31:10 +0000603 full_fname(fname));
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000604 }
605 return;
606 }
607
Wayne Davison41cfde62004-11-03 20:30:31 +0000608 if (opt_ignore_existing && fnamecmp_type == FNAMECMP_FNAME) {
Martin Pool3d6fead2002-01-25 10:42:23 +0000609 if (verbose > 1)
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000610 rprintf(FINFO, "%s exists\n", safe_fname(fname));
Martin Pool3d6fead2002-01-25 10:42:23 +0000611 return;
Wayne Davison2cda2562003-03-21 18:53:00 +0000612 }
Martin Pool3d6fead2002-01-25 10:42:23 +0000613
Wayne Davison41cfde62004-11-03 20:30:31 +0000614 if (update_only && fnamecmp_type == FNAMECMP_FNAME
Wayne Davisond3a43752004-06-29 16:22:54 +0000615 && cmp_modtime(st.st_mtime, file->modtime) > 0) {
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000616 if (verbose > 1)
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000617 rprintf(FINFO, "%s is newer\n", safe_fname(fname));
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000618 return;
619 }
620
Wayne Davison2be2fb32005-01-15 21:17:39 +0000621 if (!compare_dest && fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
Wayne Davisonb7e86282004-11-27 17:52:47 +0000622 ;
Wayne Davison8e85be02005-02-14 02:41:27 +0000623 else if (fnamecmp_type == FNAMECMP_FUZZY)
624 ;
Wayne Davisonb7e86282004-11-27 17:52:47 +0000625 else if (unchanged_file(fnamecmp, file, &st)) {
Wayne Davison41cfde62004-11-03 20:30:31 +0000626 if (fnamecmp_type == FNAMECMP_FNAME)
Wayne Davisonc41b52c2004-05-13 06:55:01 +0000627 set_perms(fname, file, &st, PERMS_REPORT);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000628 return;
629 }
630
Wayne Davison89f7eff2004-09-07 16:50:07 +0000631prepare_to_open:
Wayne Davison9d954dc2005-01-14 18:20:46 +0000632 if (partialptr) {
633 st = partial_st;
634 fnamecmp = partialptr;
635 fnamecmp_type = FNAMECMP_PARTIAL_DIR;
636 statret = 0;
637 }
638
Wayne Davison3841a042005-02-11 20:26:08 +0000639 if (dry_run || read_batch)
640 goto notify_others;
641 if (whole_file > 0) {
Wayne Davison41cfde62004-11-03 20:30:31 +0000642 statret = -1;
643 goto notify_others;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000644 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000645
Wayne Davison8e85be02005-02-14 02:41:27 +0000646 if (fuzzy_basis) {
647 int j = flist_find(fuzzy_dirlist, file);
648 if (j >= 0) /* don't use changing file as future fuzzy basis */
649 fuzzy_dirlist->files[j]->flags |= FLAG_NO_FUZZY;
650 }
651
Wayne Davison2cda2562003-03-21 18:53:00 +0000652 /* open the file */
Andrew Tridgell8c9fd201999-10-25 22:04:09 +0000653 fd = do_open(fnamecmp, O_RDONLY, 0);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000654
655 if (fd == -1) {
Wayne Davisond62bcc12004-05-15 19:31:10 +0000656 rsyserr(FERROR, errno, "failed to open %s, continuing",
657 full_fname(fnamecmp));
Wayne Davisoncd6aa5b2004-09-07 21:44:02 +0000658 pretend_missing:
David Dykstra60be6ac2000-01-06 16:15:36 +0000659 /* pretend the file didn't exist */
Wayne Davison6dff5992004-01-25 22:32:09 +0000660 if (preserve_hard_links && hard_link_check(file, HL_SKIP))
661 return;
Wayne Davison41cfde62004-11-03 20:30:31 +0000662 statret = -1;
663 goto notify_others;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000664 }
665
Wayne Davisoncd6aa5b2004-09-07 21:44:02 +0000666 if (inplace && make_backups) {
667 if (!(backupptr = get_backup_name(fname))) {
668 close(fd);
669 return;
670 }
Wayne Davison78424182005-01-25 10:39:14 +0000671 if (!(back_file = make_file(fname, NULL, NO_FILTERS))) {
Wayne Davisoncd6aa5b2004-09-07 21:44:02 +0000672 close(fd);
673 goto pretend_missing;
674 }
675 if (robust_unlink(backupptr) && errno != ENOENT) {
676 rsyserr(FERROR, errno, "unlink %s",
677 full_fname(backupptr));
678 free(back_file);
679 close(fd);
680 return;
681 }
682 if ((f_copy = do_open(backupptr,
683 O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0) {
684 rsyserr(FERROR, errno, "open %s",
685 full_fname(backupptr));
686 free(back_file);
687 close(fd);
688 return;
689 }
Wayne Davison41cfde62004-11-03 20:30:31 +0000690 fnamecmp_type = FNAMECMP_BACKUP;
Wayne Davisoncd6aa5b2004-09-07 21:44:02 +0000691 }
692
Wayne Davisondfd5ba62004-01-04 18:43:18 +0000693 if (verbose > 3) {
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000694 rprintf(FINFO, "gen mapped %s of size %.0f\n",
695 safe_fname(fnamecmp), (double)st.st_size);
Wayne Davisondfd5ba62004-01-04 18:43:18 +0000696 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000697
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000698 if (verbose > 2)
Wayne Davison0492fdf2005-02-03 01:19:01 +0000699 rprintf(FINFO, "generating and sending sums for %d\n", ndx);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000700
Wayne Davison41cfde62004-11-03 20:30:31 +0000701notify_others:
Wayne Davison0492fdf2005-02-03 01:19:01 +0000702 write_int(f_out, ndx);
Wayne Davison17b5b322005-01-17 23:11:05 +0000703 if (protocol_version >= 29 && inplace && !read_batch)
704 write_byte(f_out, fnamecmp_type);
Wayne Davison8e85be02005-02-14 02:41:27 +0000705 if (f_out_name >= 0) {
Wayne Davison41cfde62004-11-03 20:30:31 +0000706 write_byte(f_out_name, fnamecmp_type);
Wayne Davison8e85be02005-02-14 02:41:27 +0000707 if (fnamecmp_type == FNAMECMP_FUZZY) {
708 uchar lenbuf[3], *lb = lenbuf;
709 int len = strlen(fuzzy_file->basename);
710 if (len > 0x7F) {
711#if MAXPATHLEN > 0x7FFF
712 *lb++ = len / 0x10000 + 0x80;
713 *lb++ = len / 0x100;
714#else
715 *lb++ = len / 0x100 + 0x80;
716#endif
717 }
718 *lb = len;
719 write_buf(f_out_name, lenbuf, lb - lenbuf + 1);
720 write_buf(f_out_name, fuzzy_file->basename, len);
721 }
722 }
Wayne Davisoncd6aa5b2004-09-07 21:44:02 +0000723
Wayne Davison41cfde62004-11-03 20:30:31 +0000724 if (dry_run || read_batch)
725 return;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000726
Wayne Davison41cfde62004-11-03 20:30:31 +0000727 if (statret == 0) {
728 generate_and_send_sums(fd, st.st_size, f_out, f_copy);
729
730 if (f_copy >= 0) {
731 close(f_copy);
732 set_perms(backupptr, back_file, NULL, 0);
733 if (verbose > 1) {
734 rprintf(FINFO, "backed up %s to %s\n",
Wayne Davison4875d6b2005-02-07 20:36:43 +0000735 safe_fname(fname), safe_fname(backupptr));
Wayne Davison41cfde62004-11-03 20:30:31 +0000736 }
737 free(back_file);
738 }
739
740 close(fd);
741 } else
742 write_sum_head(f_out, NULL);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000743}
744
745
Wayne Davison41cfde62004-11-03 20:30:31 +0000746void generate_files(int f_out, struct file_list *flist, char *local_name,
747 int f_out_name)
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000748{
749 int i;
Wayne Davisone1f67412004-06-30 07:27:30 +0000750 int phase = 0;
Wayne Davison968c8032004-01-02 08:19:26 +0000751 char fbuf[MAXPATHLEN];
Wayne Davison3ea9bbd2005-01-24 06:19:58 +0000752 int need_retouch_dir_times = preserve_times && !omit_dir_times;
753 int need_retouch_dir_perms = 0;
Wayne Davison0492fdf2005-02-03 01:19:01 +0000754 int save_only_existing = only_existing;
755 int save_opt_ignore_existing = opt_ignore_existing;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000756
Wayne Davison45e08ed2004-01-27 08:02:31 +0000757 if (verbose > 2) {
758 rprintf(FINFO, "generator starting pid=%ld count=%d\n",
759 (long)getpid(), flist->count);
760 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000761
Martin Pool3e7053a2002-04-08 08:06:18 +0000762 if (verbose >= 2) {
763 rprintf(FINFO,
Wayne Davisonf38bd4a2004-07-13 01:45:51 +0000764 whole_file > 0
Martin Pool3e7053a2002-04-08 08:06:18 +0000765 ? "delta-transmission disabled for local transfer or --whole-file\n"
766 : "delta transmission enabled\n");
767 }
Wayne Davison2cda2562003-03-21 18:53:00 +0000768
Wayne Davison0492fdf2005-02-03 01:19:01 +0000769 /* We expect to just sit around now, so don't exit on a timeout.
770 * If we really get a timeout then the other process should exit. */
Andrew Tridgella57873b2001-09-09 04:42:09 +0000771 io_timeout = 0;
772
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000773 for (i = 0; i < flist->count; i++) {
774 struct file_struct *file = flist->files[i];
Wayne Davisondfd5ba62004-01-04 18:43:18 +0000775 struct file_struct copy;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000776
Wayne Davisondfd5ba62004-01-04 18:43:18 +0000777 if (!file->basename)
778 continue;
Wayne Davison0492fdf2005-02-03 01:19:01 +0000779
780 /* We need to ensure that any dirs we create have writeable
781 * permissions during the time we are putting files within
782 * them. This is then fixed after the transfer is done. */
Wayne Davisondfd5ba62004-01-04 18:43:18 +0000783 if (!am_root && S_ISDIR(file->mode) && !(file->mode & S_IWUSR)) {
784 copy = *file;
Wayne Davisondfd5ba62004-01-04 18:43:18 +0000785 copy.mode |= S_IWUSR; /* user write */
786 file = &copy;
Wayne Davison3ea9bbd2005-01-24 06:19:58 +0000787 need_retouch_dir_perms = 1;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000788 }
789
Wayne Davison3fef5362004-01-22 04:38:18 +0000790 recv_generator(local_name ? local_name : f_name_to(file, fbuf),
Wayne Davisonfa13f392005-01-24 01:41:48 +0000791 flist, file, i, f_out, f_out_name);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000792 }
Wayne Davison8e85be02005-02-14 02:41:27 +0000793 recv_generator(NULL, NULL, NULL, 0, -1, -1);
Wayne Davisonc93fad52005-01-30 10:06:18 +0000794 if (delete_during)
Wayne Davison31937d32005-02-03 02:01:11 +0000795 delete_in_dir(NULL, NULL, NULL);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000796
797 phase++;
798 csum_length = SUM_LENGTH;
Wayne Davison0492fdf2005-02-03 01:19:01 +0000799 only_existing = max_size = opt_ignore_existing = 0;
800 update_only = always_checksum = size_only = 0;
Wayne Davisone1f67412004-06-30 07:27:30 +0000801 ignore_times = 1;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000802
803 if (verbose > 2)
804 rprintf(FINFO,"generate_files phase=%d\n",phase);
805
Wayne Davison7daccb82004-07-12 20:42:48 +0000806 write_int(f_out, -1);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000807
J.W. Schultzbc63ae32003-03-31 17:28:34 +0000808 /* files can cycle through the system more than once
809 * to catch initial checksum errors */
Wayne Davisonb9b15fb2004-01-15 07:42:15 +0000810 while ((i = get_redo_num()) != -1) {
J.W. Schultzbc63ae32003-03-31 17:28:34 +0000811 struct file_struct *file = flist->files[i];
Wayne Davison3fef5362004-01-22 04:38:18 +0000812 recv_generator(local_name ? local_name : f_name_to(file, fbuf),
Wayne Davisonfa13f392005-01-24 01:41:48 +0000813 flist, file, i, f_out, f_out_name);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000814 }
J.W. Schultzbc63ae32003-03-31 17:28:34 +0000815
816 phase++;
Wayne Davison0492fdf2005-02-03 01:19:01 +0000817 only_existing = save_only_existing;
818 opt_ignore_existing = save_opt_ignore_existing;
819
J.W. Schultzbc63ae32003-03-31 17:28:34 +0000820 if (verbose > 2)
821 rprintf(FINFO,"generate_files phase=%d\n",phase);
822
Wayne Davison7daccb82004-07-12 20:42:48 +0000823 write_int(f_out, -1);
Wayne Davison6dff5992004-01-25 22:32:09 +0000824
Wayne Davisonb0da4b22005-01-10 00:31:55 +0000825 /* Read post-redo-phase MSG_DONE and any prior messages. */
826 get_redo_num();
827
Wayne Davison6dff5992004-01-25 22:32:09 +0000828 if (preserve_hard_links)
829 do_hard_links();
830
Wayne Davison0492fdf2005-02-03 01:19:01 +0000831 if ((need_retouch_dir_perms || need_retouch_dir_times)
832 && !list_only && !local_name && !dry_run) {
Wayne Davison3ea9bbd2005-01-24 06:19:58 +0000833 /* Now we need to fix any directory permissions that were
834 * modified during the transfer and/or re-set any tweaked
835 * modified-time values. */
836 for (i = 0; i < flist->count; i++) {
837 struct file_struct *file = flist->files[i];
838 if (!file->basename || !S_ISDIR(file->mode))
839 continue;
840 if (!need_retouch_dir_times && file->mode & S_IWUSR)
841 continue;
842 recv_generator(local_name ? local_name : f_name(file),
843 flist, file, i, -1, -1);
844 }
Wayne Davison6dff5992004-01-25 22:32:09 +0000845 }
Wayne Davison8e85be02005-02-14 02:41:27 +0000846 recv_generator(NULL, NULL, NULL, 0, -1, -1);
Wayne Davison6dff5992004-01-25 22:32:09 +0000847
848 if (verbose > 2)
849 rprintf(FINFO,"generate_files finished\n");
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000850}