blob: d33d5d06b1ce54c9706e4f9001cfba4185e29bd7 [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;
Wayne Davison6c3862f2005-02-19 22:16:45 +000028extern int log_format_has_i;
29extern int log_format_has_o_or_i;
30extern int daemon_log_format_has_i;
31extern int am_root;
32extern int am_server;
33extern int am_daemon;
Wayne Davison59faec82005-02-26 18:03:49 +000034extern int recurse;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000035extern int relative_paths;
Wayne Davison716e73d2004-06-05 16:16:30 +000036extern int keep_dirlinks;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000037extern int preserve_links;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000038extern int preserve_devices;
39extern int preserve_hard_links;
Wayne Davison6744b622004-01-02 16:50:06 +000040extern int preserve_perms;
41extern int preserve_uid;
42extern int preserve_gid;
Wayne Davison3ea9bbd2005-01-24 06:19:58 +000043extern int preserve_times;
44extern int omit_dir_times;
Wayne Davison59faec82005-02-26 18:03:49 +000045extern int delete_before;
Wayne Davisonfa13f392005-01-24 01:41:48 +000046extern int delete_during;
Wayne Davison59faec82005-02-26 18:03:49 +000047extern int delete_after;
48extern int module_id;
49extern int ignore_errors;
Wayne Davisonfe960182005-02-20 01:04:43 +000050extern int remove_sent_files;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000051extern int update_only;
Martin Pool3d6fead2002-01-25 10:42:23 +000052extern int opt_ignore_existing;
Wayne Davisoncd6aa5b2004-09-07 21:44:02 +000053extern int inplace;
54extern int make_backups;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000055extern int csum_length;
56extern int ignore_times;
Andrew Tridgellf83f0541999-02-18 03:48:24 +000057extern int size_only;
Wayne Davison7d1bfaf2004-11-11 01:45:28 +000058extern OFF_T max_size;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000059extern int io_timeout;
Wayne Davison59faec82005-02-26 18:03:49 +000060extern int io_error;
Wayne Davisonee1d11c2005-03-05 00:21:42 +000061extern int sock_f_out;
Wayne Davison9ac23952005-02-25 22:01:36 +000062extern int ignore_timeout;
Wayne Davisond04e9c52003-09-09 15:58:48 +000063extern int protocol_version;
Wayne Davison8e85be02005-02-14 02:41:27 +000064extern int fuzzy_basis;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000065extern int always_checksum;
Wayne Davisona7260c42004-07-29 16:06:38 +000066extern char *partial_dir;
Wayne Davisonb7e86282004-11-27 17:52:47 +000067extern char *basis_dir[];
Wayne Davison2be2fb32005-01-15 21:17:39 +000068extern int compare_dest;
Wayne Davison967866d2005-03-11 17:36:03 +000069extern int copy_dest;
David Dykstra59c95e42002-08-29 14:44:55 +000070extern int link_dest;
Wayne Davison57747862004-04-15 16:55:23 +000071extern int whole_file;
72extern int local_server;
Wayne Davison57747862004-04-15 16:55:23 +000073extern int list_only;
Wayne Davisonb9f592f2004-07-15 02:20:08 +000074extern int read_batch;
Wayne Davison57747862004-04-15 16:55:23 +000075extern int only_existing;
76extern int orig_umask;
77extern int safe_symlinks;
Wayne Davisona255c592005-01-01 21:08:11 +000078extern long block_size; /* "long" because popt can't set an int32. */
Wayne Davison59faec82005-02-26 18:03:49 +000079extern int max_delete;
80extern int force_delete;
81extern int one_file_system;
Wayne Davison6c3862f2005-02-19 22:16:45 +000082extern struct stats stats;
Wayne Davison59faec82005-02-26 18:03:49 +000083extern dev_t filesystem_dev;
84extern char *backup_dir;
85extern char *backup_suffix;
86extern int backup_suffix_len;
Wayne Davisonee1d11c2005-03-05 00:21:42 +000087extern struct file_list *the_file_list;
Wayne Davison78424182005-01-25 10:39:14 +000088extern struct filter_list_struct server_filter_list;
Wayne Davison97f9dca2004-05-18 00:14:10 +000089
Wayne Davisonee1d11c2005-03-05 00:21:42 +000090int allowed_lull = 0;
91
Wayne Davison59faec82005-02-26 18:03:49 +000092static int deletion_count = 0; /* used to implement --max-delete */
93
94
95static int is_backup_file(char *fn)
96{
97 int k = strlen(fn) - backup_suffix_len;
98 return k > 0 && strcmp(fn+k, backup_suffix) == 0;
99}
100
101
102/* Delete a file or directory. If DEL_FORCE_RECURSE is set in the flags, or if
103 * force_delete is set, this will delete recursively as long as DEL_NO_RECURSE
104 * is not set in the flags. */
105static int delete_item(char *fname, int mode, int flags)
106{
107 struct file_list *dirlist;
108 char buf[MAXPATHLEN];
109 int j, dlen, zap_dir, ok;
110 void *save_filters;
111
Wayne Davison59faec82005-02-26 18:03:49 +0000112 if (!S_ISDIR(mode)) {
Wayne Davisonf75a53e2005-03-05 18:58:42 +0000113 if (max_delete && ++deletion_count > max_delete)
114 return 0;
Wayne Davison59faec82005-02-26 18:03:49 +0000115 if (make_backups && (backup_dir || !is_backup_file(fname)))
116 ok = make_backup(fname);
117 else
118 ok = robust_unlink(fname) == 0;
119 if (ok) {
120 if (!(flags & DEL_TERSE))
121 log_delete(fname, mode);
Wayne Davison59faec82005-02-26 18:03:49 +0000122 return 0;
123 }
Wayne Davisonf75a53e2005-03-05 18:58:42 +0000124 if (errno == ENOENT) {
125 deletion_count--;
Wayne Davison59faec82005-02-26 18:03:49 +0000126 return 0;
Wayne Davisonf75a53e2005-03-05 18:58:42 +0000127 }
Wayne Davison59faec82005-02-26 18:03:49 +0000128 rsyserr(FERROR, errno, "delete_file: unlink %s failed",
129 full_fname(fname));
130 return -1;
131 }
132
133 zap_dir = (flags & DEL_FORCE_RECURSE || (force_delete && recurse))
134 && !(flags & DEL_NO_RECURSE);
Wayne Davisonf75a53e2005-03-05 18:58:42 +0000135 if ((max_delete && ++deletion_count > max_delete)
136 || (dry_run && zap_dir)) {
Wayne Davison59faec82005-02-26 18:03:49 +0000137 ok = 0;
138 errno = ENOTEMPTY;
139 } else if (make_backups && !backup_dir && !is_backup_file(fname)
140 && !(flags & DEL_FORCE_RECURSE))
141 ok = make_backup(fname);
142 else
143 ok = do_rmdir(fname) == 0;
144 if (ok) {
145 if (!(flags & DEL_TERSE))
146 log_delete(fname, mode);
Wayne Davison59faec82005-02-26 18:03:49 +0000147 return 0;
148 }
Wayne Davisonf75a53e2005-03-05 18:58:42 +0000149 if (errno == ENOENT) {
150 deletion_count--;
Wayne Davison59faec82005-02-26 18:03:49 +0000151 return 0;
Wayne Davisonf75a53e2005-03-05 18:58:42 +0000152 }
Wayne Davison59faec82005-02-26 18:03:49 +0000153 if (!zap_dir || (errno != ENOTEMPTY && errno != EEXIST)) {
154 rsyserr(FERROR, errno, "delete_file: rmdir %s failed",
155 full_fname(fname));
156 return -1;
157 }
Wayne Davisonf62eaa22005-03-01 19:42:31 +0000158 flags |= DEL_FORCE_RECURSE; /* mark subdir dels as not "in the way" */
Wayne Davisonf75a53e2005-03-05 18:58:42 +0000159 deletion_count--;
Wayne Davison59faec82005-02-26 18:03:49 +0000160
161 dlen = strlcpy(buf, fname, MAXPATHLEN);
162 save_filters = push_local_filters(buf, dlen);
163
164 dirlist = get_dirlist(buf, dlen, 0);
165 for (j = dirlist->count; j--; ) {
166 struct file_struct *fp = dirlist->files[j];
167
168 if (fp->flags & FLAG_MOUNT_POINT)
169 continue;
170
171 f_name_to(fp, buf);
172 if (delete_item(buf, fp->mode, flags & ~DEL_TERSE) != 0) {
173 flist_free(dirlist);
174 return -1;
175 }
176 }
177 flist_free(dirlist);
178
179 pop_local_filters(save_filters);
180
Wayne Davisonf75a53e2005-03-05 18:58:42 +0000181 if (max_delete && ++deletion_count > max_delete)
182 return 0;
Wayne Davison59faec82005-02-26 18:03:49 +0000183
184 if (do_rmdir(fname) == 0) {
185 if (!(flags & DEL_TERSE))
186 log_delete(fname, mode);
Wayne Davison59faec82005-02-26 18:03:49 +0000187 } else if (errno != ENOTEMPTY && errno != ENOENT) {
188 rsyserr(FERROR, errno, "delete_file: rmdir %s failed",
189 full_fname(fname));
190 return -1;
191 }
192
193 return 0;
194}
195
196
197/* This function is used to implement per-directory deletion, and is used by
198 * all the --delete-WHEN options. Note that the fbuf pointer must point to a
199 * MAXPATHLEN buffer with the name of the directory in it (the functions we
200 * call will append names onto the end, but the old dir value will be restored
201 * on exit). */
202static void delete_in_dir(struct file_list *flist, char *fbuf,
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000203 struct file_struct *file)
Wayne Davison59faec82005-02-26 18:03:49 +0000204{
205 static int min_depth = MAXPATHLEN, cur_depth = -1;
206 static void *filt_array[MAXPATHLEN/2+1];
Wayne Davison717b0432005-03-12 23:52:08 +0000207 static int already_warned = 0;
Wayne Davison59faec82005-02-26 18:03:49 +0000208 struct file_list *dirlist;
209 char delbuf[MAXPATHLEN];
210 STRUCT_STAT st;
211 int dlen, i;
212
213 if (!flist) {
214 while (cur_depth >= min_depth)
215 pop_local_filters(filt_array[cur_depth--]);
216 min_depth = MAXPATHLEN;
217 cur_depth = -1;
218 return;
219 }
220
221 if (verbose > 2)
222 rprintf(FINFO, "delete_in_dir(%s)\n", safe_fname(fbuf));
223
224 if (allowed_lull)
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000225 maybe_send_keepalive();
Wayne Davison59faec82005-02-26 18:03:49 +0000226
227 if (file->dir.depth >= MAXPATHLEN/2+1)
228 return; /* Impossible... */
229
Wayne Davison59faec82005-02-26 18:03:49 +0000230 if (io_error && !(lp_ignore_errors(module_id) || ignore_errors)) {
Wayne Davison717b0432005-03-12 23:52:08 +0000231 if (already_warned)
Wayne Davisonf75a53e2005-03-05 18:58:42 +0000232 return;
Wayne Davison59faec82005-02-26 18:03:49 +0000233 rprintf(FINFO,
234 "IO error encountered -- skipping file deletion\n");
Wayne Davison717b0432005-03-12 23:52:08 +0000235 already_warned = 1;
Wayne Davison59faec82005-02-26 18:03:49 +0000236 return;
237 }
238
239 while (cur_depth >= file->dir.depth && cur_depth >= min_depth)
240 pop_local_filters(filt_array[cur_depth--]);
241 cur_depth = file->dir.depth;
242 if (min_depth > cur_depth)
243 min_depth = cur_depth;
244 dlen = strlen(fbuf);
245 filt_array[cur_depth] = push_local_filters(fbuf, dlen);
246
247 if (link_stat(fbuf, &st, keep_dirlinks) < 0)
248 return;
249
250 if (one_file_system && file->flags & FLAG_TOP_DIR)
251 filesystem_dev = st.st_dev;
252
253 dirlist = get_dirlist(fbuf, dlen, 0);
254
255 /* If an item in dirlist is not found in flist, delete it
256 * from the filesystem. */
257 for (i = dirlist->count; i--; ) {
258 if (!dirlist->files[i]->basename)
259 continue;
260 if (flist_find(flist, dirlist->files[i]) < 0) {
Wayne Davison59faec82005-02-26 18:03:49 +0000261 int mode = dirlist->files[i]->mode;
Wayne Davison251f22b2005-02-26 19:11:57 +0000262 f_name_to(dirlist->files[i], delbuf);
Wayne Davison59faec82005-02-26 18:03:49 +0000263 if (delete_item(delbuf, mode, DEL_FORCE_RECURSE) < 0)
264 break;
265 }
266 }
267
268 flist_free(dirlist);
269}
270
271/* This deletes any files on the receiving side that are not present on the
272 * sending side. This is used by --delete-before and --delete-after. */
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000273static void do_delete_pass(struct file_list *flist)
Wayne Davison59faec82005-02-26 18:03:49 +0000274{
275 char fbuf[MAXPATHLEN];
276 int j;
277
Wayne Davisonf75a53e2005-03-05 18:58:42 +0000278 if (dry_run > 1) /* destination doesn't exist yet */
279 return;
280
Wayne Davison59faec82005-02-26 18:03:49 +0000281 for (j = 0; j < flist->count; j++) {
282 struct file_struct *file = flist->files[j];
283
284 if (!(file->flags & FLAG_DEL_HERE))
285 continue;
286
287 f_name_to(file, fbuf);
288 if (verbose > 1 && file->flags & FLAG_TOP_DIR)
289 rprintf(FINFO, "deleting in %s\n", safe_fname(fbuf));
290
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000291 delete_in_dir(flist, fbuf, file);
Wayne Davison59faec82005-02-26 18:03:49 +0000292 }
293}
294
Wayne Davisonb7e86282004-11-27 17:52:47 +0000295static int unchanged_attrs(struct file_struct *file, STRUCT_STAT *st)
296{
297 if (preserve_perms
298 && (st->st_mode & CHMOD_BITS) != (file->mode & CHMOD_BITS))
299 return 0;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000300
Wayne Davisonb7e86282004-11-27 17:52:47 +0000301 if (am_root && preserve_uid && st->st_uid != file->uid)
302 return 0;
303
304 if (preserve_gid && file->gid != GID_NONE && st->st_gid != file->gid)
305 return 0;
306
307 return 1;
308}
309
Wayne Davison06a1dba2005-02-15 07:20:14 +0000310
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000311void itemize(struct file_struct *file, int ndx, int statret, STRUCT_STAT *st,
Wayne Davisonef20efc2005-03-09 18:54:12 +0000312 int32 iflags, uchar fnamecmp_type, char *xname)
Wayne Davison06a1dba2005-02-15 07:20:14 +0000313{
Wayne Davisone86ae6b2005-03-03 01:50:43 +0000314 if (statret == 0) {
Wayne Davisonc557eb82005-02-16 08:10:25 +0000315 if (S_ISREG(file->mode) && file->length != st->st_size)
316 iflags |= ITEM_REPORT_SIZE;
Wayne Davisona1d23b52005-03-03 00:14:35 +0000317 if (!(iflags & ITEM_NO_DEST_AND_NO_UPDATE)) {
Wayne Davison88b218f2005-02-18 20:17:20 +0000318 int keep_time = !preserve_times ? 0
319 : S_ISDIR(file->mode) ? !omit_dir_times
320 : !S_ISLNK(file->mode);
Wayne Davison06a1dba2005-02-15 07:20:14 +0000321
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000322 if ((iflags & (ITEM_TRANSFER|ITEM_LOCAL_CHANGE) && !keep_time)
Wayne Davison88b218f2005-02-18 20:17:20 +0000323 || (keep_time && file->modtime != st->st_mtime))
324 iflags |= ITEM_REPORT_TIME;
325 if (preserve_perms && file->mode != st->st_mode)
326 iflags |= ITEM_REPORT_PERMS;
327 if (preserve_uid && am_root && file->uid != st->st_uid)
328 iflags |= ITEM_REPORT_OWNER;
329 if (preserve_gid && file->gid != GID_NONE
330 && st->st_gid != file->gid)
331 iflags |= ITEM_REPORT_GROUP;
332 }
Wayne Davisonef20efc2005-03-09 18:54:12 +0000333 } else
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000334 iflags |= ITEM_IS_NEW;
Wayne Davison06a1dba2005-02-15 07:20:14 +0000335
Wayne Davisona1d23b52005-03-03 00:14:35 +0000336 iflags &= 0xffff;
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000337 if ((iflags & SIGNIFICANT_ITEM_FLAGS || verbose > 1
Wayne Davisonef20efc2005-03-09 18:54:12 +0000338 || (xname && *xname)) && !read_batch) {
Wayne Davison6c3862f2005-02-19 22:16:45 +0000339 if (protocol_version >= 29) {
340 if (ndx >= 0)
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000341 write_int(sock_f_out, ndx);
342 write_shortint(sock_f_out, iflags);
Wayne Davisonef20efc2005-03-09 18:54:12 +0000343 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
344 write_byte(sock_f_out, fnamecmp_type);
345 if (iflags & ITEM_XNAME_FOLLOWS)
346 write_vstring(sock_f_out, xname, strlen(xname));
Wayne Davison6c3862f2005-02-19 22:16:45 +0000347 } else if (ndx >= 0)
Wayne Davisonef20efc2005-03-09 18:54:12 +0000348 log_item(file, &stats, iflags, xname);
Wayne Davison06a1dba2005-02-15 07:20:14 +0000349 }
350}
351
352
Wayne Davisonb7e86282004-11-27 17:52:47 +0000353/* Perform our quick-check heuristic for determining if a file is unchanged. */
354static int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000355{
Wayne Davisoncc1e9972004-06-23 16:51:21 +0000356 if (st->st_size != file->length)
Wayne Davison84acca02004-06-23 21:21:19 +0000357 return 0;
David Dykstra59c95e42002-08-29 14:44:55 +0000358
Wayne Davison2cda2562003-03-21 18:53:00 +0000359 /* if always checksum is set then we use the checksum instead
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000360 of the file time to determine whether to sync */
361 if (always_checksum && S_ISREG(st->st_mode)) {
362 char sum[MD4_SUM_LENGTH];
Wayne Davisonb7e86282004-11-27 17:52:47 +0000363 file_checksum(fn, sum, st->st_size);
Wayne Davison728d0922004-01-11 07:28:45 +0000364 return memcmp(sum, file->u.sum, protocol_version < 21 ? 2
Wayne Davison84acca02004-06-23 21:21:19 +0000365 : MD4_SUM_LENGTH) == 0;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000366 }
367
Wayne Davisoncc1e9972004-06-23 16:51:21 +0000368 if (size_only)
Wayne Davisoncc1e9972004-06-23 16:51:21 +0000369 return 1;
370
Wayne Davison84acca02004-06-23 21:21:19 +0000371 if (ignore_times)
372 return 0;
373
374 return cmp_modtime(st->st_mtime, file->modtime) == 0;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000375}
376
377
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000378/*
J.W. Schultz195bd902003-04-10 02:04:58 +0000379 * set (initialize) the size entries in the per-file sum_struct
Wayne Davisonec8290c2004-06-18 16:30:24 +0000380 * calculating dynamic block and checksum sizes.
J.W. Schultz195bd902003-04-10 02:04:58 +0000381 *
Wayne Davisonec8290c2004-06-18 16:30:24 +0000382 * This is only called from generate_and_send_sums() but is a separate
J.W. Schultz195bd902003-04-10 02:04:58 +0000383 * function to encapsulate the logic.
384 *
385 * The block size is a rounded square root of file length.
386 *
387 * The checksum size is determined according to:
Wayne Davisoned7e7952005-03-05 16:42:52 +0000388 * blocksum_bits = BLOCKSUM_BIAS + 2*log2(file_len) - log2(block_len)
J.W. Schultz195bd902003-04-10 02:04:58 +0000389 * provided by Donovan Baarda which gives a probability of rsync
390 * algorithm corrupting data and falling back using the whole md4
391 * checksums.
392 *
393 * This might be made one of several selectable heuristics.
394 */
Wayne Davison14908122005-01-20 22:42:47 +0000395static void sum_sizes_sqroot(struct sum_struct *sum, int64 len)
J.W. Schultz195bd902003-04-10 02:04:58 +0000396{
Wayne Davisona255c592005-01-01 21:08:11 +0000397 int32 blength;
Wayne Davisonda9d12f2004-01-03 18:37:41 +0000398 int s2length;
J.W. Schultz195bd902003-04-10 02:04:58 +0000399
Wayne Davisona255c592005-01-01 21:08:11 +0000400 if (block_size)
J.W. Schultz195bd902003-04-10 02:04:58 +0000401 blength = block_size;
Wayne Davisona255c592005-01-01 21:08:11 +0000402 else if (len <= BLOCK_SIZE * BLOCK_SIZE)
J.W. Schultz195bd902003-04-10 02:04:58 +0000403 blength = BLOCK_SIZE;
Wayne Davisona255c592005-01-01 21:08:11 +0000404 else {
405 int32 c;
Wayne Davison14908122005-01-20 22:42:47 +0000406 int64 l;
Wayne Davisoneae71652005-01-17 22:51:21 +0000407 int cnt;
408 for (c = 1, l = len, cnt = 0; l >>= 2; c <<= 1, cnt++) {}
409 if (cnt >= 31 || c >= MAX_BLOCK_SIZE)
410 blength = MAX_BLOCK_SIZE;
411 else {
412 blength = 0;
413 do {
414 blength |= c;
Wayne Davison14908122005-01-20 22:42:47 +0000415 if (len < (int64)blength * blength)
Wayne Davisoneae71652005-01-17 22:51:21 +0000416 blength &= ~c;
417 c >>= 1;
418 } while (c >= 8); /* round to multiple of 8 */
419 blength = MAX(blength, BLOCK_SIZE);
J.W. Schultz195bd902003-04-10 02:04:58 +0000420 }
J.W. Schultz195bd902003-04-10 02:04:58 +0000421 }
422
Wayne Davisond04e9c52003-09-09 15:58:48 +0000423 if (protocol_version < 27) {
J.W. Schultz195bd902003-04-10 02:04:58 +0000424 s2length = csum_length;
425 } else if (csum_length == SUM_LENGTH) {
426 s2length = SUM_LENGTH;
427 } else {
Wayne Davisona255c592005-01-01 21:08:11 +0000428 int32 c;
Wayne Davison14908122005-01-20 22:42:47 +0000429 int64 l;
Wayne Davisonda9d12f2004-01-03 18:37:41 +0000430 int b = BLOCKSUM_BIAS;
Wayne Davisona255c592005-01-01 21:08:11 +0000431 for (l = len; l >>= 1; b += 2) {}
432 for (c = blength; c >>= 1 && b; b--) {}
433 /* add a bit, subtract rollsum, round up. */
434 s2length = (b + 1 - 32 + 7) / 8; /* --optimize in compiler-- */
J.W. Schultz195bd902003-04-10 02:04:58 +0000435 s2length = MAX(s2length, csum_length);
436 s2length = MIN(s2length, SUM_LENGTH);
437 }
438
439 sum->flength = len;
440 sum->blength = blength;
441 sum->s2length = s2length;
442 sum->count = (len + (blength - 1)) / blength;
443 sum->remainder = (len % blength);
444
445 if (sum->count && verbose > 2) {
Wayne Davisona255c592005-01-01 21:08:11 +0000446 rprintf(FINFO,
447 "count=%.0f rem=%ld blength=%ld s2length=%d flength=%.0f\n",
448 (double)sum->count, (long)sum->remainder, (long)sum->blength,
Wayne Davisonda9d12f2004-01-03 18:37:41 +0000449 sum->s2length, (double)sum->flength);
J.W. Schultz195bd902003-04-10 02:04:58 +0000450 }
451}
Wayne Davison80605142002-07-31 21:20:07 +0000452
Martin Poolbceec822002-03-19 03:39:42 +0000453
Wayne Davison80605142002-07-31 21:20:07 +0000454/*
455 * Generate and send a stream of signatures/checksums that describe a buffer
Martin Poole66dfd12002-04-08 09:05:26 +0000456 *
Wayne Davison80605142002-07-31 21:20:07 +0000457 * Generate approximately one checksum every block_len bytes.
458 */
Wayne Davisoncd6aa5b2004-09-07 21:44:02 +0000459static void generate_and_send_sums(int fd, OFF_T len, int f_out, int f_copy)
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000460{
Wayne Davisona1cbe762005-02-14 22:47:42 +0000461 int32 i;
Wayne Davison6e45e1d2004-07-20 21:35:55 +0000462 struct map_struct *mapbuf;
Wayne Davison80605142002-07-31 21:20:07 +0000463 struct sum_struct sum;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000464 OFF_T offset = 0;
465
Wayne Davison423dba82003-12-24 01:14:16 +0000466 sum_sizes_sqroot(&sum, len);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000467
Wayne Davison6e45e1d2004-07-20 21:35:55 +0000468 if (len > 0)
Wayne Davison96d910c2004-08-03 08:05:27 +0000469 mapbuf = map_file(fd, len, MAX_MAP_SIZE, sum.blength);
Wayne Davison6e45e1d2004-07-20 21:35:55 +0000470 else
471 mapbuf = NULL;
472
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000473 write_sum_head(f_out, &sum);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000474
Wayne Davison80605142002-07-31 21:20:07 +0000475 for (i = 0; i < sum.count; i++) {
Wayne Davisona255c592005-01-01 21:08:11 +0000476 int32 n1 = (int32)MIN(len, (OFF_T)sum.blength);
Wayne Davison6e45e1d2004-07-20 21:35:55 +0000477 char *map = map_ptr(mapbuf, offset, n1);
Wayne Davison80605142002-07-31 21:20:07 +0000478 uint32 sum1 = get_checksum1(map, n1);
479 char sum2[SUM_LENGTH];
Martin Poole66dfd12002-04-08 09:05:26 +0000480
Wayne Davisoncd6aa5b2004-09-07 21:44:02 +0000481 if (f_copy >= 0)
482 full_write(f_copy, map, n1);
483
Wayne Davison80605142002-07-31 21:20:07 +0000484 get_checksum2(map, n1, sum2);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000485
Wayne Davison80605142002-07-31 21:20:07 +0000486 if (verbose > 3) {
Martin Poole66dfd12002-04-08 09:05:26 +0000487 rprintf(FINFO,
Wayne Davisona255c592005-01-01 21:08:11 +0000488 "chunk[%.0f] offset=%.0f len=%ld sum1=%08lx\n",
489 (double)i, (double)offset, (long)n1,
Wayne Davison0e36d9d2004-01-03 19:28:03 +0000490 (unsigned long)sum1);
Wayne Davison80605142002-07-31 21:20:07 +0000491 }
492 write_int(f_out, sum1);
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000493 write_buf(f_out, sum2, sum.s2length);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000494 len -= n1;
495 offset += n1;
496 }
Wayne Davison6e45e1d2004-07-20 21:35:55 +0000497
498 if (mapbuf)
499 unmap_file(mapbuf);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000500}
501
Wayne Davison06a1dba2005-02-15 07:20:14 +0000502
Wayne Davison8e85be02005-02-14 02:41:27 +0000503/* Try to find a filename in the same dir as "fname" with a similar name. */
504static int find_fuzzy(struct file_struct *file, struct file_list *dirlist)
505{
506 int fname_len, fname_suf_len;
507 const char *fname_suf, *fname = file->basename;
508 uint32 lowest_dist = 0x7FFFFFFF;
509 int j, lowest_j = -1;
510
511 fname_len = strlen(fname);
512 fname_suf = find_filename_suffix(fname, fname_len, &fname_suf_len);
513
514 for (j = 0; j < dirlist->count; j++) {
515 struct file_struct *fp = dirlist->files[j];
516 const char *suf, *name;
517 int len, suf_len;
518 uint32 dist;
519
520 if (!S_ISREG(fp->mode) || !fp->length
521 || fp->flags & FLAG_NO_FUZZY)
522 continue;
523
524 name = fp->basename;
525
526 if (fp->length == file->length
527 && fp->modtime == file->modtime) {
528 if (verbose > 4) {
529 rprintf(FINFO,
530 "fuzzy size/modtime match for %s\n",
531 name);
532 }
533 return j;
534 }
535
536 len = strlen(name);
537 suf = find_filename_suffix(name, len, &suf_len);
538
539 dist = fuzzy_distance(name, len, fname, fname_len);
540 /* Add some extra weight to how well the suffixes match. */
541 dist += fuzzy_distance(suf, suf_len, fname_suf, fname_suf_len)
542 * 10;
543 if (verbose > 4) {
544 rprintf(FINFO, "fuzzy distance for %s = %d.%05d\n",
545 name, (int)(dist>>16), (int)(dist&0xFFFF));
546 }
547 if (dist <= lowest_dist) {
548 lowest_dist = dist;
549 lowest_j = j;
550 }
551 }
552
553 return lowest_j;
554}
555
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000556void check_for_finished_hlinks(int itemizing, enum logcode code)
557{
558 struct file_struct *file;
559 int ndx;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000560
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000561 while ((ndx = get_hlink_num()) != -1) {
562 if (ndx < 0 || ndx >= the_file_list->count)
563 continue;
564
565 file = the_file_list->files[ndx];
566 if (!file->link_u.links)
567 continue;
568
569 hard_link_cluster(file, ndx, itemizing, code);
570 }
571}
572
Wayne Davisoned7e7952005-03-05 16:42:52 +0000573static int phase = 0;
574
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000575/* Acts on the_file_list->file's ndx'th item, whose name is fname. If a dir,
Wayne Davison0492fdf2005-02-03 01:19:01 +0000576 * make sure it exists, and has the right permissions/timestamp info. For
577 * all other non-regular files (symlinks, etc.) we create them here. For
578 * regular files that have changed, we try to find a basis file and then
579 * start sending checksums.
Martin Poolef1aa912000-11-10 03:17:08 +0000580 *
Wayne Davison0492fdf2005-02-03 01:19:01 +0000581 * Note that f_out is set to -1 when doing final directory-permission and
582 * modification-time repair. */
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000583static void recv_generator(char *fname, struct file_struct *file, int ndx,
Wayne Davison7433d732005-02-26 06:52:55 +0000584 int itemizing, int maybe_PERMS_REPORT,
Wayne Davisonef20efc2005-03-09 18:54:12 +0000585 enum logcode code, int f_out)
Wayne Davison2cda2562003-03-21 18:53:00 +0000586{
Wayne Davison8174bc32005-02-19 23:01:52 +0000587 static int missing_below = -1, excluded_below = -1;
Wayne Davison8e85be02005-02-14 02:41:27 +0000588 static char *fuzzy_dirname = NULL;
589 static struct file_list *fuzzy_dirlist = NULL;
590 struct file_struct *fuzzy_file = NULL;
Wayne Davison41cfde62004-11-03 20:30:31 +0000591 int fd = -1, f_copy = -1;
Wayne Davison1f1d3682005-03-09 23:46:28 +0000592 STRUCT_STAT st, real_st, partial_st;
Wayne Davison41cfde62004-11-03 20:30:31 +0000593 struct file_struct *back_file = NULL;
Wayne Davison1f1d3682005-03-09 23:46:28 +0000594 int statret, real_ret, stat_errno;
Wayne Davison41cfde62004-11-03 20:30:31 +0000595 char *fnamecmp, *partialptr, *backupptr = NULL;
David Dykstra375a4551998-10-26 21:42:38 +0000596 char fnamecmpbuf[MAXPATHLEN];
Wayne Davison41cfde62004-11-03 20:30:31 +0000597 uchar fnamecmp_type;
Andrew Tridgellf7632fc1998-11-02 00:52:01 +0000598
Wayne Davisondfd5ba62004-01-04 18:43:18 +0000599 if (list_only)
600 return;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000601
Wayne Davison8e85be02005-02-14 02:41:27 +0000602 if (!fname) {
603 if (fuzzy_dirlist) {
604 flist_free(fuzzy_dirlist);
605 fuzzy_dirlist = NULL;
606 fuzzy_dirname = NULL;
607 }
608 if (missing_below >= 0) {
609 dry_run--;
610 missing_below = -1;
611 }
612 return;
613 }
614
Wayne Davison4875d6b2005-02-07 20:36:43 +0000615 if (verbose > 2) {
616 rprintf(FINFO, "recv_generator(%s,%d)\n",
617 safe_fname(fname), ndx);
618 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000619
Wayne Davison8174bc32005-02-19 23:01:52 +0000620 if (server_filter_list.head) {
621 if (excluded_below >= 0) {
622 if (file->dir.depth > excluded_below)
623 goto skipping;
624 excluded_below = -1;
Wayne Davison3e35c342004-05-18 08:50:17 +0000625 }
Wayne Davison8174bc32005-02-19 23:01:52 +0000626 if (check_filter(&server_filter_list, fname,
627 S_ISDIR(file->mode)) < 0) {
628 if (S_ISDIR(file->mode))
629 excluded_below = file->dir.depth;
630 skipping:
631 if (verbose) {
632 rprintf(FINFO,
633 "skipping server-excluded file \"%s\"\n",
634 safe_fname(fname));
635 }
636 return;
637 }
Wayne Davison3e35c342004-05-18 08:50:17 +0000638 }
Wayne Davison97f9dca2004-05-18 00:14:10 +0000639
Wayne Davison8e85be02005-02-14 02:41:27 +0000640 if (missing_below >= 0 && file->dir.depth <= missing_below) {
Wayne Davisondf337832005-02-12 21:18:58 +0000641 dry_run--;
642 missing_below = -1;
643 }
Wayne Davison73f7af02004-08-05 18:18:36 +0000644 if (dry_run > 1) {
645 statret = -1;
646 stat_errno = ENOENT;
647 } else {
Wayne Davison8e85be02005-02-14 02:41:27 +0000648 if (fuzzy_basis && S_ISREG(file->mode)) {
649 char *dn = file->dirname ? file->dirname : ".";
Wayne Davisonccc51c82005-03-14 03:35:40 +0000650 if (fuzzy_dirname != dn
651 && strcmp(fuzzy_dirname, dn) != 0) {
Wayne Davison8e85be02005-02-14 02:41:27 +0000652 if (fuzzy_dirlist)
653 flist_free(fuzzy_dirlist);
Wayne Davisonccc51c82005-03-14 03:35:40 +0000654 fuzzy_dirlist = get_dirlist(dn, -1, 1);
Wayne Davison8e85be02005-02-14 02:41:27 +0000655 }
Wayne Davisonccc51c82005-03-14 03:35:40 +0000656 fuzzy_dirname = dn;
Wayne Davison8e85be02005-02-14 02:41:27 +0000657 }
658
Wayne Davison73f7af02004-08-05 18:18:36 +0000659 statret = link_stat(fname, &st,
660 keep_dirlinks && S_ISDIR(file->mode));
661 stat_errno = errno;
662 }
Martin Pool63787382001-12-14 02:16:20 +0000663
Wayne Davisona7260c42004-07-29 16:06:38 +0000664 if (only_existing && statret == -1 && stat_errno == ENOENT) {
Andrew Tridgell1347d512000-01-23 01:53:18 +0000665 /* we only want to update existing files */
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000666 if (verbose > 1) {
667 rprintf(FINFO, "not creating new file \"%s\"\n",
668 safe_fname(fname));
669 }
Andrew Tridgell1347d512000-01-23 01:53:18 +0000670 return;
671 }
672
Wayne Davisond9b4d262004-07-23 16:59:38 +0000673 if (statret == 0 && !preserve_perms
674 && S_ISDIR(st.st_mode) == S_ISDIR(file->mode)) {
Andrew Tridgell4df9f361999-10-31 04:28:03 +0000675 /* if the file exists already and we aren't perserving
Wayne Davison2cda2562003-03-21 18:53:00 +0000676 * permissions then act as though the remote end sent
677 * us the file permissions we already have */
Wayne Davison67e78a82004-03-07 20:29:59 +0000678 file->mode = (file->mode & ~CHMOD_BITS)
679 | (st.st_mode & CHMOD_BITS);
Andrew Tridgell4df9f361999-10-31 04:28:03 +0000680 }
681
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000682 if (S_ISDIR(file->mode)) {
Wayne Davison2cda2562003-03-21 18:53:00 +0000683 /* The file to be received is a directory, so we need
684 * to prepare appropriately. If there is already a
685 * file of that name and it is *not* a directory, then
686 * we need to delete it. If it doesn't exist, then
Wayne Davison027428e2005-01-20 22:59:54 +0000687 * (perhaps recursively) create it. */
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000688 if (statret == 0 && !S_ISDIR(st.st_mode)) {
Wayne Davison1f1d3682005-03-09 23:46:28 +0000689 if (delete_item(fname, st.st_mode, DEL_TERSE) < 0)
690 return;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000691 statret = -1;
692 }
Wayne Davisondf337832005-02-12 21:18:58 +0000693 if (dry_run && statret != 0 && missing_below < 0) {
694 missing_below = file->dir.depth;
695 dry_run++;
696 }
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000697 if (itemizing && f_out != -1) {
698 itemize(file, ndx, statret, &st,
Wayne Davisonef20efc2005-03-09 18:54:12 +0000699 statret ? ITEM_LOCAL_CHANGE : 0, 0, NULL);
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000700 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000701 if (statret != 0 && do_mkdir(fname,file->mode) != 0 && errno != EEXIST) {
Wayne Davison027428e2005-01-20 22:59:54 +0000702 if (!relative_paths || errno != ENOENT
703 || create_directory_path(fname, orig_umask) < 0
704 || do_mkdir(fname, file->mode) < 0) {
Wayne Davisond62bcc12004-05-15 19:31:10 +0000705 rsyserr(FERROR, errno,
706 "recv_generator: mkdir %s failed",
707 full_fname(fname));
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000708 }
709 }
Wayne Davison716e73d2004-06-05 16:16:30 +0000710 if (set_perms(fname, file, statret ? NULL : &st, 0)
Wayne Davison6c3862f2005-02-19 22:16:45 +0000711 && verbose && code && f_out != -1)
712 rprintf(code, "%s/\n", safe_fname(fname));
Wayne Davisonf75a53e2005-03-05 18:58:42 +0000713 if (delete_during && f_out != -1 && !phase && dry_run < 2
Wayne Davison31937d32005-02-03 02:01:11 +0000714 && (file->flags & FLAG_DEL_HERE))
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000715 delete_in_dir(the_file_list, fname, file);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000716 return;
Wayne Davison06a1dba2005-02-15 07:20:14 +0000717 }
Wayne Davison6c3862f2005-02-19 22:16:45 +0000718
Wayne Davison06a1dba2005-02-15 07:20:14 +0000719 if (max_size && file->length > max_size) {
Wayne Davison4875d6b2005-02-07 20:36:43 +0000720 if (verbose > 1) {
721 rprintf(FINFO, "%s is over max-size\n",
722 safe_fname(fname));
723 }
Wayne Davison7d1bfaf2004-11-11 01:45:28 +0000724 return;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000725 }
726
727 if (preserve_links && S_ISLNK(file->mode)) {
Wayne Davison4f5b0752005-02-14 00:53:43 +0000728#ifdef SUPPORT_LINKS
Wayne Davison728d0922004-01-11 07:28:45 +0000729 if (safe_symlinks && unsafe_symlink(file->u.link, fname)) {
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000730 if (verbose) {
Wayne Davison4875d6b2005-02-07 20:36:43 +0000731 rprintf(FINFO,
732 "ignoring unsafe symlink %s -> \"%s\"\n",
733 full_fname(fname),
734 safe_fname(file->u.link));
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000735 }
736 return;
737 }
738 if (statret == 0) {
Wayne Davison7e384102005-01-19 20:10:57 +0000739 char lnk[MAXPATHLEN];
740 int len;
741
Wayne Davison8a8356b2005-02-19 02:39:21 +0000742 if (!S_ISDIR(st.st_mode)
Wayne Davison7e384102005-01-19 20:10:57 +0000743 && (len = readlink(fname, lnk, MAXPATHLEN-1)) > 0) {
744 lnk[len] = 0;
Martin Pool85d4d142001-12-14 02:14:58 +0000745 /* A link already pointing to the
746 * right place -- no further action
747 * required. */
Wayne Davison7e384102005-01-19 20:10:57 +0000748 if (strcmp(lnk, file->u.link) == 0) {
Wayne Davison6c3862f2005-02-19 22:16:45 +0000749 if (itemizing) {
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000750 itemize(file, ndx, 0, &st, 0,
Wayne Davisonef20efc2005-03-09 18:54:12 +0000751 0, NULL);
Wayne Davisonc557eb82005-02-16 08:10:25 +0000752 }
Wayne Davisonc41b52c2004-05-13 06:55:01 +0000753 set_perms(fname, file, &st,
Wayne Davison8a8356b2005-02-19 02:39:21 +0000754 maybe_PERMS_REPORT);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000755 return;
756 }
Wayne Davison2cda2562003-03-21 18:53:00 +0000757 }
Wayne Davison7e384102005-01-19 20:10:57 +0000758 /* Not the right symlink (or not a symlink), so
759 * delete it. */
Wayne Davison1f1d3682005-03-09 23:46:28 +0000760 if (delete_item(fname, st.st_mode, DEL_TERSE) < 0)
761 return;
762 if (!S_ISLNK(st.st_mode))
Wayne Davison88b218f2005-02-18 20:17:20 +0000763 statret = -1;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000764 }
Wayne Davison728d0922004-01-11 07:28:45 +0000765 if (do_symlink(file->u.link,fname) != 0) {
Wayne Davisond62bcc12004-05-15 19:31:10 +0000766 rsyserr(FERROR, errno, "symlink %s -> \"%s\" failed",
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000767 full_fname(fname), safe_fname(file->u.link));
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000768 } else {
769 set_perms(fname,file,NULL,0);
Wayne Davison6c3862f2005-02-19 22:16:45 +0000770 if (itemizing) {
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000771 itemize(file, ndx, statret, &st,
Wayne Davisonef20efc2005-03-09 18:54:12 +0000772 ITEM_LOCAL_CHANGE, 0, NULL);
Wayne Davison6c3862f2005-02-19 22:16:45 +0000773 }
774 if (code && verbose) {
775 rprintf(code, "%s -> %s\n", safe_fname(fname),
Wayne Davisonc557eb82005-02-16 08:10:25 +0000776 safe_fname(file->u.link));
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000777 }
Wayne Davisonfe960182005-02-20 01:04:43 +0000778 if (remove_sent_files && !dry_run) {
779 char numbuf[4];
780 SIVAL(numbuf, 0, ndx);
781 send_msg(MSG_SUCCESS, numbuf, 4);
782 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000783 }
784#endif
785 return;
786 }
787
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000788 if (am_root && preserve_devices && IS_DEVICE(file->mode)) {
Wayne Davison2cda2562003-03-21 18:53:00 +0000789 if (statret != 0 ||
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000790 st.st_mode != file->mode ||
Wayne Davison3915fd72004-04-12 16:52:43 +0000791 st.st_rdev != file->u.rdev) {
Wayne Davison1f1d3682005-03-09 23:46:28 +0000792 if (delete_item(fname, st.st_mode, DEL_TERSE) < 0)
793 return;
Wayne Davison59faec82005-02-26 18:03:49 +0000794 if (!IS_DEVICE(st.st_mode))
Wayne Davison88b218f2005-02-18 20:17:20 +0000795 statret = -1;
Wayne Davisond62bcc12004-05-15 19:31:10 +0000796 if (verbose > 2) {
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000797 rprintf(FINFO,"mknod(%s,0%o,0x%x)\n",
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000798 safe_fname(fname),
799 (int)file->mode, (int)file->u.rdev);
Wayne Davisond62bcc12004-05-15 19:31:10 +0000800 }
Wayne Davison728d0922004-01-11 07:28:45 +0000801 if (do_mknod(fname,file->mode,file->u.rdev) != 0) {
Wayne Davisond62bcc12004-05-15 19:31:10 +0000802 rsyserr(FERROR, errno, "mknod %s failed",
803 full_fname(fname));
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000804 } else {
805 set_perms(fname,file,NULL,0);
Wayne Davison6c3862f2005-02-19 22:16:45 +0000806 if (itemizing) {
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000807 itemize(file, ndx, statret, &st,
Wayne Davisonef20efc2005-03-09 18:54:12 +0000808 ITEM_LOCAL_CHANGE, 0, NULL);
Wayne Davison6c3862f2005-02-19 22:16:45 +0000809 }
810 if (code && verbose) {
811 rprintf(code, "%s\n",
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000812 safe_fname(fname));
813 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000814 }
815 } else {
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000816 if (itemizing)
Wayne Davisonef20efc2005-03-09 18:54:12 +0000817 itemize(file, ndx, statret, &st, 0, 0, NULL);
Wayne Davison8a8356b2005-02-19 02:39:21 +0000818 set_perms(fname, file, &st, maybe_PERMS_REPORT);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000819 }
820 return;
821 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000822
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000823 if (preserve_hard_links && hard_link_check(file, ndx, HL_CHECK_MASTER))
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000824 return;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000825
826 if (!S_ISREG(file->mode)) {
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000827 rprintf(FINFO, "skipping non-regular file \"%s\"\n",
828 safe_fname(fname));
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000829 return;
830 }
831
Wayne Davisonc3cbcfb2005-03-03 01:33:51 +0000832 if (opt_ignore_existing && statret == 0) {
833 if (verbose > 1)
834 rprintf(FINFO, "%s exists\n", safe_fname(fname));
835 return;
836 }
837
838 if (update_only && statret == 0
839 && cmp_modtime(st.st_mtime, file->modtime) > 0) {
840 if (verbose > 1)
841 rprintf(FINFO, "%s is newer\n", safe_fname(fname));
842 return;
843 }
844
David Dykstra375a4551998-10-26 21:42:38 +0000845 fnamecmp = fname;
Wayne Davison41cfde62004-11-03 20:30:31 +0000846 fnamecmp_type = FNAMECMP_FNAME;
David Dykstra375a4551998-10-26 21:42:38 +0000847
Wayne Davison1f1d3682005-03-09 23:46:28 +0000848 if (statret == 0 && !S_ISREG(st.st_mode)) {
849 if (delete_item(fname, st.st_mode, DEL_TERSE) != 0)
850 return;
851 statret = -1;
852 stat_errno = ENOENT;
853 }
854
Wayne Davison06a1dba2005-02-15 07:20:14 +0000855 if (statret != 0 && basis_dir[0] != NULL) {
Wayne Davisonaef98822005-03-02 09:09:15 +0000856 int best_match = -1;
Wayne Davisonb7e86282004-11-27 17:52:47 +0000857 int match_level = 0;
858 int i = 0;
859 do {
860 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
861 basis_dir[i], fname);
Wayne Davisonaef98822005-03-02 09:09:15 +0000862 if (link_stat(fnamecmpbuf, &st, 0) < 0
863 || !S_ISREG(st.st_mode))
864 continue;
865 switch (match_level) {
866 case 0:
867 best_match = i;
868 match_level = 1;
Wayne Davisonaef98822005-03-02 09:09:15 +0000869 /* FALL THROUGH */
870 case 1:
871 if (!unchanged_file(fnamecmpbuf, file, &st))
872 continue;
873 best_match = i;
874 match_level = 2;
Wayne Davison967866d2005-03-11 17:36:03 +0000875 if (copy_dest)
876 break;
Wayne Davisonaef98822005-03-02 09:09:15 +0000877 /* FALL THROUGH */
878 case 2:
879 if (!unchanged_attrs(file, &st))
880 continue;
881 best_match = i;
Wayne Davisonb7e86282004-11-27 17:52:47 +0000882 match_level = 3;
883 break;
884 }
Wayne Davisonaef98822005-03-02 09:09:15 +0000885 break;
Wayne Davisonb7e86282004-11-27 17:52:47 +0000886 } while (basis_dir[++i] != NULL);
Wayne Davisonaef98822005-03-02 09:09:15 +0000887 if (match_level) {
Wayne Davison9ba46342005-03-02 09:51:54 +0000888 statret = 0;
Wayne Davisonaef98822005-03-02 09:09:15 +0000889 if (i != best_match) {
890 i = best_match;
Wayne Davisonb7e86282004-11-27 17:52:47 +0000891 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
892 basis_dir[i], fname);
Wayne Davison9ba46342005-03-02 09:51:54 +0000893 if (link_stat(fnamecmpbuf, &st, 0) < 0) {
894 match_level = 0;
895 statret = -1;
Wayne Davisona1d23b52005-03-03 00:14:35 +0000896 stat_errno = errno;
Wayne Davison9ba46342005-03-02 09:51:54 +0000897 }
Wayne Davisonb7e86282004-11-27 17:52:47 +0000898 }
Wayne Davison4f5b0752005-02-14 00:53:43 +0000899#ifdef HAVE_LINK
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000900 if (link_dest && match_level == 3) {
901 if (hard_link_one(file, ndx, fname, -1, &st,
902 fnamecmpbuf, 1,
903 itemizing && verbose > 1,
904 code) == 0)
905 return;
Wayne Davison70b54e42005-03-02 17:27:19 +0000906 if (verbose) {
907 rsyserr(FINFO, errno, "link %s => %s",
908 full_fname(fnamecmpbuf),
909 safe_fname(fname));
Wayne Davisone7bc9b62004-01-02 08:05:20 +0000910 }
Wayne Davisone2243312005-03-02 17:48:36 +0000911 match_level = 2;
Wayne Davison70b54e42005-03-02 17:27:19 +0000912 }
David Dykstra59c95e42002-08-29 14:44:55 +0000913#endif
Wayne Davison967866d2005-03-11 17:36:03 +0000914 if (match_level == 2) {
915 /* Copy the file locally. */
916 if (copy_file(fnamecmpbuf, fname, file->mode) < 0) {
917 if (verbose) {
918 rsyserr(FINFO, errno,
919 "copy_file %s => %s",
920 full_fname(fnamecmpbuf),
921 safe_fname(fname));
922 }
923 match_level = 0;
924 statret = -1;
925 } else
926 set_perms(fname, file, NULL, 0);
927 } else if (compare_dest || match_level == 1) {
Wayne Davisone7d13fe2004-07-28 10:04:06 +0000928 fnamecmp = fnamecmpbuf;
Wayne Davison2be2fb32005-01-15 21:17:39 +0000929 fnamecmp_type = i;
Wayne Davison41cfde62004-11-03 20:30:31 +0000930 }
Wayne Davisone7d13fe2004-07-28 10:04:06 +0000931 }
932 }
933
Wayne Davison1f1d3682005-03-09 23:46:28 +0000934 real_ret = statret;
935 real_st = st;
David Dykstra375a4551998-10-26 21:42:38 +0000936
Wayne Davison9d954dc2005-01-14 18:20:46 +0000937 if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL
Wayne Davison72c19bb2004-09-07 17:03:51 +0000938 && link_stat(partialptr, &partial_st, 0) == 0
939 && S_ISREG(partial_st.st_mode)) {
Wayne Davison06a1dba2005-02-15 07:20:14 +0000940 if (statret != 0)
Wayne Davison72c19bb2004-09-07 17:03:51 +0000941 goto prepare_to_open;
942 } else
943 partialptr = NULL;
Wayne Davison89f7eff2004-09-07 16:50:07 +0000944
Wayne Davison06a1dba2005-02-15 07:20:14 +0000945 if (statret != 0 && fuzzy_basis && dry_run <= 1) {
Wayne Davison8e85be02005-02-14 02:41:27 +0000946 int j = find_fuzzy(file, fuzzy_dirlist);
947 if (j >= 0) {
948 fuzzy_file = fuzzy_dirlist->files[j];
949 f_name_to(fuzzy_file, fnamecmpbuf);
950 if (verbose > 2) {
951 rprintf(FINFO, "fuzzy basis selected for %s: %s\n",
952 safe_fname(fname), safe_fname(fnamecmpbuf));
953 }
Wayne Davison8e85be02005-02-14 02:41:27 +0000954 st.st_size = fuzzy_file->length;
Wayne Davison8e85be02005-02-14 02:41:27 +0000955 statret = 0;
956 fnamecmp = fnamecmpbuf;
957 fnamecmp_type = FNAMECMP_FUZZY;
958 }
959 }
960
Wayne Davison06a1dba2005-02-15 07:20:14 +0000961 if (statret != 0) {
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000962 if (preserve_hard_links && hard_link_check(file, ndx, HL_SKIP))
Wayne Davison6dff5992004-01-25 22:32:09 +0000963 return;
Wayne Davison41cfde62004-11-03 20:30:31 +0000964 if (stat_errno == ENOENT)
965 goto notify_others;
966 if (verbose > 1) {
Wayne Davisone7d13fe2004-07-28 10:04:06 +0000967 rsyserr(FERROR, stat_errno,
968 "recv_generator: failed to stat %s",
Wayne Davisond62bcc12004-05-15 19:31:10 +0000969 full_fname(fname));
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000970 }
971 return;
972 }
973
Wayne Davison2be2fb32005-01-15 21:17:39 +0000974 if (!compare_dest && fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
Wayne Davisonb7e86282004-11-27 17:52:47 +0000975 ;
Wayne Davison8e85be02005-02-14 02:41:27 +0000976 else if (fnamecmp_type == FNAMECMP_FUZZY)
977 ;
Wayne Davisonb7e86282004-11-27 17:52:47 +0000978 else if (unchanged_file(fnamecmp, file, &st)) {
Wayne Davisona1d23b52005-03-03 00:14:35 +0000979 if (fnamecmp_type == FNAMECMP_FNAME) {
Wayne Davison1f1d3682005-03-09 23:46:28 +0000980 if (itemizing) {
981 itemize(file, ndx, real_ret, &real_st,
982 0, 0, NULL);
983 }
Wayne Davison8a8356b2005-02-19 02:39:21 +0000984 set_perms(fname, file, &st, maybe_PERMS_REPORT);
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000985 if (preserve_hard_links && file->link_u.links)
986 hard_link_cluster(file, ndx, itemizing, code);
Wayne Davisona1d23b52005-03-03 00:14:35 +0000987 return;
988 }
989 /* Only --compare-dest gets here. */
Wayne Davison967866d2005-03-11 17:36:03 +0000990 itemize(file, ndx, real_ret, &real_st,
991 ITEM_NO_DEST_AND_NO_UPDATE, 0, NULL);
992 return;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000993 }
994
Wayne Davison89f7eff2004-09-07 16:50:07 +0000995prepare_to_open:
Wayne Davison9d954dc2005-01-14 18:20:46 +0000996 if (partialptr) {
997 st = partial_st;
998 fnamecmp = partialptr;
999 fnamecmp_type = FNAMECMP_PARTIAL_DIR;
1000 statret = 0;
1001 }
1002
Wayne Davison33ab4ad2005-03-03 02:58:48 +00001003 if (dry_run || read_batch || whole_file)
Wayne Davison3841a042005-02-11 20:26:08 +00001004 goto notify_others;
Andrew Tridgell2f03f951998-07-25 02:25:22 +00001005
Wayne Davison8e85be02005-02-14 02:41:27 +00001006 if (fuzzy_basis) {
1007 int j = flist_find(fuzzy_dirlist, file);
1008 if (j >= 0) /* don't use changing file as future fuzzy basis */
1009 fuzzy_dirlist->files[j]->flags |= FLAG_NO_FUZZY;
1010 }
1011
Wayne Davison2cda2562003-03-21 18:53:00 +00001012 /* open the file */
Andrew Tridgell8c9fd201999-10-25 22:04:09 +00001013 fd = do_open(fnamecmp, O_RDONLY, 0);
Andrew Tridgell2f03f951998-07-25 02:25:22 +00001014
1015 if (fd == -1) {
Wayne Davisond62bcc12004-05-15 19:31:10 +00001016 rsyserr(FERROR, errno, "failed to open %s, continuing",
1017 full_fname(fnamecmp));
Wayne Davisoncd6aa5b2004-09-07 21:44:02 +00001018 pretend_missing:
David Dykstra60be6ac2000-01-06 16:15:36 +00001019 /* pretend the file didn't exist */
Wayne Davisonee1d11c2005-03-05 00:21:42 +00001020 if (preserve_hard_links && hard_link_check(file, ndx, HL_SKIP))
Wayne Davison6dff5992004-01-25 22:32:09 +00001021 return;
Wayne Davison1f1d3682005-03-09 23:46:28 +00001022 statret = real_ret = -1;
Wayne Davison41cfde62004-11-03 20:30:31 +00001023 goto notify_others;
Andrew Tridgell2f03f951998-07-25 02:25:22 +00001024 }
1025
Wayne Davisonef20efc2005-03-09 18:54:12 +00001026 if (inplace && make_backups && fnamecmp_type == FNAMECMP_FNAME) {
Wayne Davisoncd6aa5b2004-09-07 21:44:02 +00001027 if (!(backupptr = get_backup_name(fname))) {
1028 close(fd);
1029 return;
1030 }
Wayne Davison78424182005-01-25 10:39:14 +00001031 if (!(back_file = make_file(fname, NULL, NO_FILTERS))) {
Wayne Davisoncd6aa5b2004-09-07 21:44:02 +00001032 close(fd);
1033 goto pretend_missing;
1034 }
1035 if (robust_unlink(backupptr) && errno != ENOENT) {
1036 rsyserr(FERROR, errno, "unlink %s",
1037 full_fname(backupptr));
1038 free(back_file);
1039 close(fd);
1040 return;
1041 }
1042 if ((f_copy = do_open(backupptr,
1043 O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0) {
1044 rsyserr(FERROR, errno, "open %s",
1045 full_fname(backupptr));
1046 free(back_file);
1047 close(fd);
1048 return;
1049 }
Wayne Davison41cfde62004-11-03 20:30:31 +00001050 fnamecmp_type = FNAMECMP_BACKUP;
Wayne Davisoncd6aa5b2004-09-07 21:44:02 +00001051 }
1052
Wayne Davisondfd5ba62004-01-04 18:43:18 +00001053 if (verbose > 3) {
Wayne Davisonecc81fc2004-07-26 16:36:59 +00001054 rprintf(FINFO, "gen mapped %s of size %.0f\n",
1055 safe_fname(fnamecmp), (double)st.st_size);
Wayne Davisondfd5ba62004-01-04 18:43:18 +00001056 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +00001057
Andrew Tridgell2f03f951998-07-25 02:25:22 +00001058 if (verbose > 2)
Wayne Davison0492fdf2005-02-03 01:19:01 +00001059 rprintf(FINFO, "generating and sending sums for %d\n", ndx);
Andrew Tridgell2f03f951998-07-25 02:25:22 +00001060
Wayne Davison41cfde62004-11-03 20:30:31 +00001061notify_others:
Wayne Davison0492fdf2005-02-03 01:19:01 +00001062 write_int(f_out, ndx);
Wayne Davison6c3862f2005-02-19 22:16:45 +00001063 if (itemizing) {
Wayne Davisonee1d11c2005-03-05 00:21:42 +00001064 int iflags = ITEM_TRANSFER;
Wayne Davison8237f932005-02-22 21:43:57 +00001065 if (always_checksum)
Wayne Davisona1d23b52005-03-03 00:14:35 +00001066 iflags |= ITEM_REPORT_CHECKSUM;
Wayne Davison352963d2005-02-22 22:12:59 +00001067 if (fnamecmp_type != FNAMECMP_FNAME)
Wayne Davisonef20efc2005-03-09 18:54:12 +00001068 iflags |= ITEM_BASIS_TYPE_FOLLOWS;
1069 if (fnamecmp_type == FNAMECMP_FUZZY)
1070 iflags |= ITEM_XNAME_FOLLOWS;
Wayne Davison1f1d3682005-03-09 23:46:28 +00001071 itemize(file, -1, real_ret, &real_st, iflags, fnamecmp_type,
Wayne Davisonef20efc2005-03-09 18:54:12 +00001072 fuzzy_file ? fuzzy_file->basename : NULL);
Wayne Davison8e85be02005-02-14 02:41:27 +00001073 }
Wayne Davisoncd6aa5b2004-09-07 21:44:02 +00001074
Wayne Davisonee1d11c2005-03-05 00:21:42 +00001075 if (dry_run) {
1076 if (preserve_hard_links && file->link_u.links)
1077 hard_link_cluster(file, ndx, itemizing, code);
1078 return;
1079 }
1080 if (read_batch)
Wayne Davison41cfde62004-11-03 20:30:31 +00001081 return;
Andrew Tridgell2f03f951998-07-25 02:25:22 +00001082
Wayne Davison33ab4ad2005-03-03 02:58:48 +00001083 if (statret != 0 || whole_file) {
Wayne Davison41cfde62004-11-03 20:30:31 +00001084 write_sum_head(f_out, NULL);
Wayne Davisone86ae6b2005-03-03 01:50:43 +00001085 return;
1086 }
1087
1088 generate_and_send_sums(fd, st.st_size, f_out, f_copy);
1089
1090 if (f_copy >= 0) {
1091 close(f_copy);
1092 set_perms(backupptr, back_file, NULL, 0);
1093 if (verbose > 1) {
1094 rprintf(FINFO, "backed up %s to %s\n",
1095 safe_fname(fname), safe_fname(backupptr));
1096 }
1097 free(back_file);
1098 }
1099
1100 close(fd);
Andrew Tridgell2f03f951998-07-25 02:25:22 +00001101}
1102
1103
Wayne Davisonef20efc2005-03-09 18:54:12 +00001104void generate_files(int f_out, struct file_list *flist, char *local_name)
Andrew Tridgell2f03f951998-07-25 02:25:22 +00001105{
Wayne Davisonee1d11c2005-03-05 00:21:42 +00001106 int i, lull_mod;
Wayne Davison968c8032004-01-02 08:19:26 +00001107 char fbuf[MAXPATHLEN];
Wayne Davison7433d732005-02-26 06:52:55 +00001108 int itemizing, maybe_PERMS_REPORT;
1109 enum logcode code;
Wayne Davison3ea9bbd2005-01-24 06:19:58 +00001110 int need_retouch_dir_times = preserve_times && !omit_dir_times;
1111 int need_retouch_dir_perms = 0;
Wayne Davison0492fdf2005-02-03 01:19:01 +00001112 int save_only_existing = only_existing;
1113 int save_opt_ignore_existing = opt_ignore_existing;
Wayne Davisonee1d11c2005-03-05 00:21:42 +00001114
1115 allowed_lull = read_batch ? 0 : (io_timeout + 1) / 2;
1116 lull_mod = allowed_lull * 5;
Wayne Davison7433d732005-02-26 06:52:55 +00001117
1118 if (protocol_version >= 29) {
1119 itemizing = 1;
1120 maybe_PERMS_REPORT = log_format_has_i ? 0 : PERMS_REPORT;
1121 code = daemon_log_format_has_i ? 0 : FLOG;
1122 } else if (am_daemon) {
1123 itemizing = daemon_log_format_has_i && !dry_run;
1124 maybe_PERMS_REPORT = PERMS_REPORT;
1125 code = itemizing || dry_run ? FCLIENT : FINFO;
1126 } else if (!am_server) {
1127 itemizing = log_format_has_i;
1128 maybe_PERMS_REPORT = log_format_has_i ? 0 : PERMS_REPORT;
1129 code = itemizing ? 0 : FINFO;
1130 } else {
1131 itemizing = 0;
1132 maybe_PERMS_REPORT = PERMS_REPORT;
1133 code = FINFO;
1134 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +00001135
Wayne Davison45e08ed2004-01-27 08:02:31 +00001136 if (verbose > 2) {
1137 rprintf(FINFO, "generator starting pid=%ld count=%d\n",
1138 (long)getpid(), flist->count);
1139 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +00001140
Wayne Davison59faec82005-02-26 18:03:49 +00001141 if (delete_before && !local_name && flist->count > 0)
Wayne Davisonee1d11c2005-03-05 00:21:42 +00001142 do_delete_pass(flist);
Wayne Davison59faec82005-02-26 18:03:49 +00001143
Wayne Davison33ab4ad2005-03-03 02:58:48 +00001144 if (whole_file < 0)
1145 whole_file = 0;
Martin Pool3e7053a2002-04-08 08:06:18 +00001146 if (verbose >= 2) {
Wayne Davison1b1fef22005-02-20 22:11:43 +00001147 rprintf(FINFO, "delta-transmission %s\n",
Wayne Davison33ab4ad2005-03-03 02:58:48 +00001148 whole_file
Wayne Davison1b1fef22005-02-20 22:11:43 +00001149 ? "disabled for local transfer or --whole-file"
1150 : "enabled");
Martin Pool3e7053a2002-04-08 08:06:18 +00001151 }
Wayne Davison2cda2562003-03-21 18:53:00 +00001152
Wayne Davison9ac23952005-02-25 22:01:36 +00001153 if (protocol_version < 29)
Wayne Davison7433d732005-02-26 06:52:55 +00001154 ignore_timeout = 1;
Andrew Tridgella57873b2001-09-09 04:42:09 +00001155
Andrew Tridgell2f03f951998-07-25 02:25:22 +00001156 for (i = 0; i < flist->count; i++) {
1157 struct file_struct *file = flist->files[i];
Andrew Tridgell2f03f951998-07-25 02:25:22 +00001158
Wayne Davisondfd5ba62004-01-04 18:43:18 +00001159 if (!file->basename)
1160 continue;
Wayne Davison0492fdf2005-02-03 01:19:01 +00001161
Wayne Davison1f7e29b2005-03-09 03:49:07 +00001162 recv_generator(local_name ? local_name : f_name_to(file, fbuf),
1163 file, i, itemizing, maybe_PERMS_REPORT, code,
Wayne Davisonef20efc2005-03-09 18:54:12 +00001164 f_out);
Wayne Davison1f7e29b2005-03-09 03:49:07 +00001165
Wayne Davison0492fdf2005-02-03 01:19:01 +00001166 /* We need to ensure that any dirs we create have writeable
1167 * permissions during the time we are putting files within
1168 * them. This is then fixed after the transfer is done. */
Wayne Davisondfd5ba62004-01-04 18:43:18 +00001169 if (!am_root && S_ISDIR(file->mode) && !(file->mode & S_IWUSR)) {
Wayne Davison1f7e29b2005-03-09 03:49:07 +00001170 int mode = file->mode | S_IWUSR; /* user write */
1171 char *fname = local_name ? local_name : fbuf;
1172 if (do_chmod(fname, mode & CHMOD_BITS) < 0) {
1173 rsyserr(FERROR, errno,
1174 "failed to modify permissions on %s",
1175 full_fname(fname));
1176 }
Wayne Davison3ea9bbd2005-01-24 06:19:58 +00001177 need_retouch_dir_perms = 1;
Andrew Tridgell2f03f951998-07-25 02:25:22 +00001178 }
1179
Wayne Davisonee1d11c2005-03-05 00:21:42 +00001180 if (preserve_hard_links)
1181 check_for_finished_hlinks(itemizing, code);
Wayne Davison9ac23952005-02-25 22:01:36 +00001182
Wayne Davison18a11cf2005-02-27 09:02:32 +00001183 if (allowed_lull && !(i % lull_mod))
Wayne Davisonee1d11c2005-03-05 00:21:42 +00001184 maybe_send_keepalive();
Andrew Tridgell2f03f951998-07-25 02:25:22 +00001185 }
Wayne Davisonef20efc2005-03-09 18:54:12 +00001186 recv_generator(NULL, NULL, 0, 0, 0, code, -1);
Wayne Davisonc93fad52005-01-30 10:06:18 +00001187 if (delete_during)
Wayne Davisonee1d11c2005-03-05 00:21:42 +00001188 delete_in_dir(NULL, NULL, NULL);
Andrew Tridgell2f03f951998-07-25 02:25:22 +00001189
1190 phase++;
1191 csum_length = SUM_LENGTH;
Wayne Davison0492fdf2005-02-03 01:19:01 +00001192 only_existing = max_size = opt_ignore_existing = 0;
1193 update_only = always_checksum = size_only = 0;
Wayne Davisone1f67412004-06-30 07:27:30 +00001194 ignore_times = 1;
Wayne Davisone6bc6f42005-02-27 07:50:43 +00001195 make_backups = 0; /* avoid a duplicate backup for inplace processing */
Andrew Tridgell2f03f951998-07-25 02:25:22 +00001196
Wayne Davison9ac23952005-02-25 22:01:36 +00001197 /* We expect to just sit around now, so don't exit on a timeout.
1198 * If we really get a timeout then the other process should exit. */
1199 ignore_timeout = 1;
1200
Andrew Tridgell2f03f951998-07-25 02:25:22 +00001201 if (verbose > 2)
1202 rprintf(FINFO,"generate_files phase=%d\n",phase);
1203
Wayne Davison7daccb82004-07-12 20:42:48 +00001204 write_int(f_out, -1);
Andrew Tridgell2f03f951998-07-25 02:25:22 +00001205
J.W. Schultzbc63ae32003-03-31 17:28:34 +00001206 /* files can cycle through the system more than once
1207 * to catch initial checksum errors */
Wayne Davisonee1d11c2005-03-05 00:21:42 +00001208 while ((i = get_redo_num(itemizing, code)) != -1) {
J.W. Schultzbc63ae32003-03-31 17:28:34 +00001209 struct file_struct *file = flist->files[i];
Wayne Davison3fef5362004-01-22 04:38:18 +00001210 recv_generator(local_name ? local_name : f_name_to(file, fbuf),
Wayne Davisonee1d11c2005-03-05 00:21:42 +00001211 file, i, itemizing, maybe_PERMS_REPORT, code,
Wayne Davisonef20efc2005-03-09 18:54:12 +00001212 f_out);
Andrew Tridgell2f03f951998-07-25 02:25:22 +00001213 }
J.W. Schultzbc63ae32003-03-31 17:28:34 +00001214
1215 phase++;
Wayne Davison0492fdf2005-02-03 01:19:01 +00001216 only_existing = save_only_existing;
1217 opt_ignore_existing = save_opt_ignore_existing;
1218
J.W. Schultzbc63ae32003-03-31 17:28:34 +00001219 if (verbose > 2)
1220 rprintf(FINFO,"generate_files phase=%d\n",phase);
1221
Wayne Davison7daccb82004-07-12 20:42:48 +00001222 write_int(f_out, -1);
Wayne Davison6dff5992004-01-25 22:32:09 +00001223
Wayne Davisonb0da4b22005-01-10 00:31:55 +00001224 /* Read post-redo-phase MSG_DONE and any prior messages. */
Wayne Davisonee1d11c2005-03-05 00:21:42 +00001225 get_redo_num(itemizing, code);
Wayne Davison6dff5992004-01-25 22:32:09 +00001226
Wayne Davison59faec82005-02-26 18:03:49 +00001227 if (delete_after && !local_name && flist->count > 0)
Wayne Davisonee1d11c2005-03-05 00:21:42 +00001228 do_delete_pass(flist);
Wayne Davison59faec82005-02-26 18:03:49 +00001229
Wayne Davison0492fdf2005-02-03 01:19:01 +00001230 if ((need_retouch_dir_perms || need_retouch_dir_times)
1231 && !list_only && !local_name && !dry_run) {
Wayne Davison18a11cf2005-02-27 09:02:32 +00001232 int j = 0;
Wayne Davison3ea9bbd2005-01-24 06:19:58 +00001233 /* Now we need to fix any directory permissions that were
1234 * modified during the transfer and/or re-set any tweaked
1235 * modified-time values. */
1236 for (i = 0; i < flist->count; i++) {
1237 struct file_struct *file = flist->files[i];
1238 if (!file->basename || !S_ISDIR(file->mode))
1239 continue;
1240 if (!need_retouch_dir_times && file->mode & S_IWUSR)
1241 continue;
1242 recv_generator(local_name ? local_name : f_name(file),
Wayne Davisonee1d11c2005-03-05 00:21:42 +00001243 file, i, itemizing, maybe_PERMS_REPORT,
Wayne Davisonef20efc2005-03-09 18:54:12 +00001244 code, -1);
Wayne Davison18a11cf2005-02-27 09:02:32 +00001245 if (allowed_lull && !(j++ % lull_mod))
Wayne Davisonee1d11c2005-03-05 00:21:42 +00001246 maybe_send_keepalive();
Wayne Davison3ea9bbd2005-01-24 06:19:58 +00001247 }
Wayne Davison6dff5992004-01-25 22:32:09 +00001248 }
Wayne Davisonef20efc2005-03-09 18:54:12 +00001249 recv_generator(NULL, NULL, 0, 0, 0, code, -1);
Wayne Davison6dff5992004-01-25 22:32:09 +00001250
Wayne Davisonf75a53e2005-03-05 18:58:42 +00001251 if (max_delete > 0 && deletion_count > max_delete) {
1252 rprintf(FINFO,
1253 "Deletions stopped due to --max-delete limit (%d skipped)\n",
1254 deletion_count - max_delete);
1255 io_error |= IOERR_DEL_LIMIT;
1256 }
1257
Wayne Davison6dff5992004-01-25 22:32:09 +00001258 if (verbose > 2)
1259 rprintf(FINFO,"generate_files finished\n");
Andrew Tridgell2f03f951998-07-25 02:25:22 +00001260}