blob: 3bb37497f0cb0cc9089e6d0af5815b11e699979f [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 Davisonbeb51aa2005-04-09 18:59:52 +000028extern int do_xfers;
Wayne Davison6c3862f2005-02-19 22:16:45 +000029extern int log_format_has_i;
30extern int log_format_has_o_or_i;
31extern int daemon_log_format_has_i;
32extern int am_root;
33extern int am_server;
34extern int am_daemon;
Wayne Davison083acd42005-03-29 10:41:26 +000035extern int do_progress;
Wayne Davison59faec82005-02-26 18:03:49 +000036extern int recurse;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000037extern int relative_paths;
Wayne Davison716e73d2004-06-05 16:16:30 +000038extern int keep_dirlinks;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000039extern int preserve_links;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000040extern int preserve_devices;
41extern int preserve_hard_links;
Wayne Davison6744b622004-01-02 16:50:06 +000042extern int preserve_perms;
43extern int preserve_uid;
44extern int preserve_gid;
Wayne Davison3ea9bbd2005-01-24 06:19:58 +000045extern int preserve_times;
46extern int omit_dir_times;
Wayne Davison59faec82005-02-26 18:03:49 +000047extern int delete_before;
Wayne Davisonfa13f392005-01-24 01:41:48 +000048extern int delete_during;
Wayne Davison59faec82005-02-26 18:03:49 +000049extern int delete_after;
50extern int module_id;
51extern int ignore_errors;
Wayne Davisonfe960182005-02-20 01:04:43 +000052extern int remove_sent_files;
Wayne Davison70352262005-03-29 01:04:20 +000053extern int delay_updates;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000054extern int update_only;
Martin Pool3d6fead2002-01-25 10:42:23 +000055extern int opt_ignore_existing;
Wayne Davisoncd6aa5b2004-09-07 21:44:02 +000056extern int inplace;
57extern int make_backups;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000058extern int csum_length;
59extern int ignore_times;
Andrew Tridgellf83f0541999-02-18 03:48:24 +000060extern int size_only;
Wayne Davison7d1bfaf2004-11-11 01:45:28 +000061extern OFF_T max_size;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000062extern int io_timeout;
Wayne Davison59faec82005-02-26 18:03:49 +000063extern int io_error;
Wayne Davisonee1d11c2005-03-05 00:21:42 +000064extern int sock_f_out;
Wayne Davison9ac23952005-02-25 22:01:36 +000065extern int ignore_timeout;
Wayne Davisond04e9c52003-09-09 15:58:48 +000066extern int protocol_version;
Wayne Davison8e85be02005-02-14 02:41:27 +000067extern int fuzzy_basis;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000068extern int always_checksum;
Wayne Davisona7260c42004-07-29 16:06:38 +000069extern char *partial_dir;
Wayne Davisonb7e86282004-11-27 17:52:47 +000070extern char *basis_dir[];
Wayne Davison2be2fb32005-01-15 21:17:39 +000071extern int compare_dest;
Wayne Davison967866d2005-03-11 17:36:03 +000072extern int copy_dest;
David Dykstra59c95e42002-08-29 14:44:55 +000073extern int link_dest;
Wayne Davison57747862004-04-15 16:55:23 +000074extern int whole_file;
Wayne Davison57747862004-04-15 16:55:23 +000075extern int list_only;
Wayne Davisonb9f592f2004-07-15 02:20:08 +000076extern int read_batch;
Wayne Davison57747862004-04-15 16:55:23 +000077extern int only_existing;
78extern int orig_umask;
79extern int safe_symlinks;
Wayne Davisona255c592005-01-01 21:08:11 +000080extern long block_size; /* "long" because popt can't set an int32. */
Wayne Davison59faec82005-02-26 18:03:49 +000081extern int max_delete;
82extern int force_delete;
83extern int one_file_system;
Wayne Davison6c3862f2005-02-19 22:16:45 +000084extern struct stats stats;
Wayne Davison59faec82005-02-26 18:03:49 +000085extern dev_t filesystem_dev;
86extern char *backup_dir;
87extern char *backup_suffix;
88extern int backup_suffix_len;
Wayne Davisonee1d11c2005-03-05 00:21:42 +000089extern struct file_list *the_file_list;
Wayne Davison78424182005-01-25 10:39:14 +000090extern struct filter_list_struct server_filter_list;
Wayne Davison97f9dca2004-05-18 00:14:10 +000091
Wayne Davisonee1d11c2005-03-05 00:21:42 +000092int allowed_lull = 0;
93
Wayne Davison59faec82005-02-26 18:03:49 +000094static int deletion_count = 0; /* used to implement --max-delete */
95
96
97static int is_backup_file(char *fn)
98{
99 int k = strlen(fn) - backup_suffix_len;
100 return k > 0 && strcmp(fn+k, backup_suffix) == 0;
101}
102
103
104/* Delete a file or directory. If DEL_FORCE_RECURSE is set in the flags, or if
105 * force_delete is set, this will delete recursively as long as DEL_NO_RECURSE
106 * is not set in the flags. */
107static int delete_item(char *fname, int mode, int flags)
108{
109 struct file_list *dirlist;
110 char buf[MAXPATHLEN];
111 int j, dlen, zap_dir, ok;
112 void *save_filters;
113
Wayne Davison59faec82005-02-26 18:03:49 +0000114 if (!S_ISDIR(mode)) {
Wayne Davisonf75a53e2005-03-05 18:58:42 +0000115 if (max_delete && ++deletion_count > max_delete)
116 return 0;
Wayne Davison59faec82005-02-26 18:03:49 +0000117 if (make_backups && (backup_dir || !is_backup_file(fname)))
118 ok = make_backup(fname);
119 else
120 ok = robust_unlink(fname) == 0;
121 if (ok) {
122 if (!(flags & DEL_TERSE))
123 log_delete(fname, mode);
Wayne Davison59faec82005-02-26 18:03:49 +0000124 return 0;
125 }
Wayne Davisonf75a53e2005-03-05 18:58:42 +0000126 if (errno == ENOENT) {
127 deletion_count--;
Wayne Davison59faec82005-02-26 18:03:49 +0000128 return 0;
Wayne Davisonf75a53e2005-03-05 18:58:42 +0000129 }
Wayne Davison59faec82005-02-26 18:03:49 +0000130 rsyserr(FERROR, errno, "delete_file: unlink %s failed",
131 full_fname(fname));
132 return -1;
133 }
134
135 zap_dir = (flags & DEL_FORCE_RECURSE || (force_delete && recurse))
136 && !(flags & DEL_NO_RECURSE);
Wayne Davisonf75a53e2005-03-05 18:58:42 +0000137 if ((max_delete && ++deletion_count > max_delete)
138 || (dry_run && zap_dir)) {
Wayne Davison59faec82005-02-26 18:03:49 +0000139 ok = 0;
140 errno = ENOTEMPTY;
141 } else if (make_backups && !backup_dir && !is_backup_file(fname)
142 && !(flags & DEL_FORCE_RECURSE))
143 ok = make_backup(fname);
144 else
145 ok = do_rmdir(fname) == 0;
146 if (ok) {
147 if (!(flags & DEL_TERSE))
148 log_delete(fname, mode);
Wayne Davison59faec82005-02-26 18:03:49 +0000149 return 0;
150 }
Wayne Davisonf75a53e2005-03-05 18:58:42 +0000151 if (errno == ENOENT) {
152 deletion_count--;
Wayne Davison59faec82005-02-26 18:03:49 +0000153 return 0;
Wayne Davisonf75a53e2005-03-05 18:58:42 +0000154 }
Wayne Davison59faec82005-02-26 18:03:49 +0000155 if (!zap_dir || (errno != ENOTEMPTY && errno != EEXIST)) {
156 rsyserr(FERROR, errno, "delete_file: rmdir %s failed",
157 full_fname(fname));
158 return -1;
159 }
Wayne Davisonf62eaa22005-03-01 19:42:31 +0000160 flags |= DEL_FORCE_RECURSE; /* mark subdir dels as not "in the way" */
Wayne Davisonf75a53e2005-03-05 18:58:42 +0000161 deletion_count--;
Wayne Davison59faec82005-02-26 18:03:49 +0000162
163 dlen = strlcpy(buf, fname, MAXPATHLEN);
164 save_filters = push_local_filters(buf, dlen);
165
166 dirlist = get_dirlist(buf, dlen, 0);
167 for (j = dirlist->count; j--; ) {
168 struct file_struct *fp = dirlist->files[j];
169
170 if (fp->flags & FLAG_MOUNT_POINT)
171 continue;
172
173 f_name_to(fp, buf);
174 if (delete_item(buf, fp->mode, flags & ~DEL_TERSE) != 0) {
175 flist_free(dirlist);
176 return -1;
177 }
178 }
179 flist_free(dirlist);
180
181 pop_local_filters(save_filters);
182
Wayne Davisonf75a53e2005-03-05 18:58:42 +0000183 if (max_delete && ++deletion_count > max_delete)
184 return 0;
Wayne Davison59faec82005-02-26 18:03:49 +0000185
186 if (do_rmdir(fname) == 0) {
187 if (!(flags & DEL_TERSE))
188 log_delete(fname, mode);
Wayne Davison59faec82005-02-26 18:03:49 +0000189 } else if (errno != ENOTEMPTY && errno != ENOENT) {
190 rsyserr(FERROR, errno, "delete_file: rmdir %s failed",
191 full_fname(fname));
192 return -1;
193 }
194
195 return 0;
196}
197
198
199/* This function is used to implement per-directory deletion, and is used by
200 * all the --delete-WHEN options. Note that the fbuf pointer must point to a
201 * MAXPATHLEN buffer with the name of the directory in it (the functions we
202 * call will append names onto the end, but the old dir value will be restored
203 * on exit). */
204static void delete_in_dir(struct file_list *flist, char *fbuf,
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000205 struct file_struct *file)
Wayne Davison59faec82005-02-26 18:03:49 +0000206{
207 static int min_depth = MAXPATHLEN, cur_depth = -1;
208 static void *filt_array[MAXPATHLEN/2+1];
Wayne Davison717b0432005-03-12 23:52:08 +0000209 static int already_warned = 0;
Wayne Davison59faec82005-02-26 18:03:49 +0000210 struct file_list *dirlist;
211 char delbuf[MAXPATHLEN];
212 STRUCT_STAT st;
213 int dlen, i;
214
215 if (!flist) {
216 while (cur_depth >= min_depth)
217 pop_local_filters(filt_array[cur_depth--]);
218 min_depth = MAXPATHLEN;
219 cur_depth = -1;
220 return;
221 }
222
223 if (verbose > 2)
224 rprintf(FINFO, "delete_in_dir(%s)\n", safe_fname(fbuf));
225
226 if (allowed_lull)
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000227 maybe_send_keepalive();
Wayne Davison59faec82005-02-26 18:03:49 +0000228
229 if (file->dir.depth >= MAXPATHLEN/2+1)
230 return; /* Impossible... */
231
Wayne Davison59faec82005-02-26 18:03:49 +0000232 if (io_error && !(lp_ignore_errors(module_id) || ignore_errors)) {
Wayne Davison717b0432005-03-12 23:52:08 +0000233 if (already_warned)
Wayne Davisonf75a53e2005-03-05 18:58:42 +0000234 return;
Wayne Davison59faec82005-02-26 18:03:49 +0000235 rprintf(FINFO,
236 "IO error encountered -- skipping file deletion\n");
Wayne Davison717b0432005-03-12 23:52:08 +0000237 already_warned = 1;
Wayne Davison59faec82005-02-26 18:03:49 +0000238 return;
239 }
240
241 while (cur_depth >= file->dir.depth && cur_depth >= min_depth)
242 pop_local_filters(filt_array[cur_depth--]);
243 cur_depth = file->dir.depth;
244 if (min_depth > cur_depth)
245 min_depth = cur_depth;
246 dlen = strlen(fbuf);
247 filt_array[cur_depth] = push_local_filters(fbuf, dlen);
248
249 if (link_stat(fbuf, &st, keep_dirlinks) < 0)
250 return;
251
252 if (one_file_system && file->flags & FLAG_TOP_DIR)
253 filesystem_dev = st.st_dev;
254
255 dirlist = get_dirlist(fbuf, dlen, 0);
256
257 /* If an item in dirlist is not found in flist, delete it
258 * from the filesystem. */
259 for (i = dirlist->count; i--; ) {
260 if (!dirlist->files[i]->basename)
261 continue;
262 if (flist_find(flist, dirlist->files[i]) < 0) {
Wayne Davison59faec82005-02-26 18:03:49 +0000263 int mode = dirlist->files[i]->mode;
Wayne Davison251f22b2005-02-26 19:11:57 +0000264 f_name_to(dirlist->files[i], delbuf);
Wayne Davison59faec82005-02-26 18:03:49 +0000265 if (delete_item(delbuf, mode, DEL_FORCE_RECURSE) < 0)
266 break;
267 }
268 }
269
270 flist_free(dirlist);
271}
272
273/* This deletes any files on the receiving side that are not present on the
274 * sending side. This is used by --delete-before and --delete-after. */
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000275static void do_delete_pass(struct file_list *flist)
Wayne Davison59faec82005-02-26 18:03:49 +0000276{
277 char fbuf[MAXPATHLEN];
278 int j;
279
Wayne Davisonf75a53e2005-03-05 18:58:42 +0000280 if (dry_run > 1) /* destination doesn't exist yet */
281 return;
282
Wayne Davison59faec82005-02-26 18:03:49 +0000283 for (j = 0; j < flist->count; j++) {
284 struct file_struct *file = flist->files[j];
285
286 if (!(file->flags & FLAG_DEL_HERE))
287 continue;
288
289 f_name_to(file, fbuf);
290 if (verbose > 1 && file->flags & FLAG_TOP_DIR)
291 rprintf(FINFO, "deleting in %s\n", safe_fname(fbuf));
292
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000293 delete_in_dir(flist, fbuf, file);
Wayne Davison59faec82005-02-26 18:03:49 +0000294 }
Wayne Davisona06e2b72005-04-05 19:51:13 +0000295 if (do_progress && !am_server)
296 rprintf(FINFO, " \r");
Wayne Davison59faec82005-02-26 18:03:49 +0000297}
298
Wayne Davisonb7e86282004-11-27 17:52:47 +0000299static int unchanged_attrs(struct file_struct *file, STRUCT_STAT *st)
300{
301 if (preserve_perms
302 && (st->st_mode & CHMOD_BITS) != (file->mode & CHMOD_BITS))
303 return 0;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000304
Wayne Davisonb7e86282004-11-27 17:52:47 +0000305 if (am_root && preserve_uid && st->st_uid != file->uid)
306 return 0;
307
308 if (preserve_gid && file->gid != GID_NONE && st->st_gid != file->gid)
309 return 0;
310
311 return 1;
312}
313
Wayne Davison06a1dba2005-02-15 07:20:14 +0000314
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000315void itemize(struct file_struct *file, int ndx, int statret, STRUCT_STAT *st,
Wayne Davisonef20efc2005-03-09 18:54:12 +0000316 int32 iflags, uchar fnamecmp_type, char *xname)
Wayne Davison06a1dba2005-02-15 07:20:14 +0000317{
Wayne Davisone86ae6b2005-03-03 01:50:43 +0000318 if (statret == 0) {
Wayne Davisonc557eb82005-02-16 08:10:25 +0000319 if (S_ISREG(file->mode) && file->length != st->st_size)
320 iflags |= ITEM_REPORT_SIZE;
Wayne Davisona1d23b52005-03-03 00:14:35 +0000321 if (!(iflags & ITEM_NO_DEST_AND_NO_UPDATE)) {
Wayne Davison88b218f2005-02-18 20:17:20 +0000322 int keep_time = !preserve_times ? 0
323 : S_ISDIR(file->mode) ? !omit_dir_times
324 : !S_ISLNK(file->mode);
Wayne Davison06a1dba2005-02-15 07:20:14 +0000325
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000326 if ((iflags & (ITEM_TRANSFER|ITEM_LOCAL_CHANGE) && !keep_time)
Wayne Davison88b218f2005-02-18 20:17:20 +0000327 || (keep_time && file->modtime != st->st_mtime))
328 iflags |= ITEM_REPORT_TIME;
329 if (preserve_perms && file->mode != st->st_mode)
330 iflags |= ITEM_REPORT_PERMS;
331 if (preserve_uid && am_root && file->uid != st->st_uid)
332 iflags |= ITEM_REPORT_OWNER;
333 if (preserve_gid && file->gid != GID_NONE
334 && st->st_gid != file->gid)
335 iflags |= ITEM_REPORT_GROUP;
336 }
Wayne Davisonef20efc2005-03-09 18:54:12 +0000337 } else
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000338 iflags |= ITEM_IS_NEW;
Wayne Davison06a1dba2005-02-15 07:20:14 +0000339
Wayne Davisona1d23b52005-03-03 00:14:35 +0000340 iflags &= 0xffff;
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000341 if ((iflags & SIGNIFICANT_ITEM_FLAGS || verbose > 1
Wayne Davisonef20efc2005-03-09 18:54:12 +0000342 || (xname && *xname)) && !read_batch) {
Wayne Davison6c3862f2005-02-19 22:16:45 +0000343 if (protocol_version >= 29) {
344 if (ndx >= 0)
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000345 write_int(sock_f_out, ndx);
346 write_shortint(sock_f_out, iflags);
Wayne Davisonef20efc2005-03-09 18:54:12 +0000347 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
348 write_byte(sock_f_out, fnamecmp_type);
349 if (iflags & ITEM_XNAME_FOLLOWS)
350 write_vstring(sock_f_out, xname, strlen(xname));
Wayne Davison6c3862f2005-02-19 22:16:45 +0000351 } else if (ndx >= 0)
Wayne Davisonef20efc2005-03-09 18:54:12 +0000352 log_item(file, &stats, iflags, xname);
Wayne Davison06a1dba2005-02-15 07:20:14 +0000353 }
354}
355
356
Wayne Davisonb7e86282004-11-27 17:52:47 +0000357/* Perform our quick-check heuristic for determining if a file is unchanged. */
358static int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000359{
Wayne Davisoncc1e9972004-06-23 16:51:21 +0000360 if (st->st_size != file->length)
Wayne Davison84acca02004-06-23 21:21:19 +0000361 return 0;
David Dykstra59c95e42002-08-29 14:44:55 +0000362
Wayne Davison2cda2562003-03-21 18:53:00 +0000363 /* if always checksum is set then we use the checksum instead
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000364 of the file time to determine whether to sync */
365 if (always_checksum && S_ISREG(st->st_mode)) {
366 char sum[MD4_SUM_LENGTH];
Wayne Davisonb7e86282004-11-27 17:52:47 +0000367 file_checksum(fn, sum, st->st_size);
Wayne Davison728d0922004-01-11 07:28:45 +0000368 return memcmp(sum, file->u.sum, protocol_version < 21 ? 2
Wayne Davison84acca02004-06-23 21:21:19 +0000369 : MD4_SUM_LENGTH) == 0;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000370 }
371
Wayne Davisoncc1e9972004-06-23 16:51:21 +0000372 if (size_only)
Wayne Davisoncc1e9972004-06-23 16:51:21 +0000373 return 1;
374
Wayne Davison84acca02004-06-23 21:21:19 +0000375 if (ignore_times)
376 return 0;
377
378 return cmp_modtime(st->st_mtime, file->modtime) == 0;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000379}
380
381
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000382/*
J.W. Schultz195bd902003-04-10 02:04:58 +0000383 * set (initialize) the size entries in the per-file sum_struct
Wayne Davisonec8290c2004-06-18 16:30:24 +0000384 * calculating dynamic block and checksum sizes.
J.W. Schultz195bd902003-04-10 02:04:58 +0000385 *
Wayne Davisonec8290c2004-06-18 16:30:24 +0000386 * This is only called from generate_and_send_sums() but is a separate
J.W. Schultz195bd902003-04-10 02:04:58 +0000387 * function to encapsulate the logic.
388 *
389 * The block size is a rounded square root of file length.
390 *
391 * The checksum size is determined according to:
Wayne Davisoned7e7952005-03-05 16:42:52 +0000392 * blocksum_bits = BLOCKSUM_BIAS + 2*log2(file_len) - log2(block_len)
J.W. Schultz195bd902003-04-10 02:04:58 +0000393 * provided by Donovan Baarda which gives a probability of rsync
394 * algorithm corrupting data and falling back using the whole md4
395 * checksums.
396 *
397 * This might be made one of several selectable heuristics.
398 */
Wayne Davison14908122005-01-20 22:42:47 +0000399static void sum_sizes_sqroot(struct sum_struct *sum, int64 len)
J.W. Schultz195bd902003-04-10 02:04:58 +0000400{
Wayne Davisona255c592005-01-01 21:08:11 +0000401 int32 blength;
Wayne Davisonda9d12f2004-01-03 18:37:41 +0000402 int s2length;
J.W. Schultz195bd902003-04-10 02:04:58 +0000403
Wayne Davisona255c592005-01-01 21:08:11 +0000404 if (block_size)
J.W. Schultz195bd902003-04-10 02:04:58 +0000405 blength = block_size;
Wayne Davisona255c592005-01-01 21:08:11 +0000406 else if (len <= BLOCK_SIZE * BLOCK_SIZE)
J.W. Schultz195bd902003-04-10 02:04:58 +0000407 blength = BLOCK_SIZE;
Wayne Davisona255c592005-01-01 21:08:11 +0000408 else {
409 int32 c;
Wayne Davison14908122005-01-20 22:42:47 +0000410 int64 l;
Wayne Davisoneae71652005-01-17 22:51:21 +0000411 int cnt;
412 for (c = 1, l = len, cnt = 0; l >>= 2; c <<= 1, cnt++) {}
413 if (cnt >= 31 || c >= MAX_BLOCK_SIZE)
414 blength = MAX_BLOCK_SIZE;
415 else {
416 blength = 0;
417 do {
418 blength |= c;
Wayne Davison14908122005-01-20 22:42:47 +0000419 if (len < (int64)blength * blength)
Wayne Davisoneae71652005-01-17 22:51:21 +0000420 blength &= ~c;
421 c >>= 1;
422 } while (c >= 8); /* round to multiple of 8 */
423 blength = MAX(blength, BLOCK_SIZE);
J.W. Schultz195bd902003-04-10 02:04:58 +0000424 }
J.W. Schultz195bd902003-04-10 02:04:58 +0000425 }
426
Wayne Davisond04e9c52003-09-09 15:58:48 +0000427 if (protocol_version < 27) {
J.W. Schultz195bd902003-04-10 02:04:58 +0000428 s2length = csum_length;
429 } else if (csum_length == SUM_LENGTH) {
430 s2length = SUM_LENGTH;
431 } else {
Wayne Davisona255c592005-01-01 21:08:11 +0000432 int32 c;
Wayne Davison14908122005-01-20 22:42:47 +0000433 int64 l;
Wayne Davisonda9d12f2004-01-03 18:37:41 +0000434 int b = BLOCKSUM_BIAS;
Wayne Davisona255c592005-01-01 21:08:11 +0000435 for (l = len; l >>= 1; b += 2) {}
436 for (c = blength; c >>= 1 && b; b--) {}
437 /* add a bit, subtract rollsum, round up. */
438 s2length = (b + 1 - 32 + 7) / 8; /* --optimize in compiler-- */
J.W. Schultz195bd902003-04-10 02:04:58 +0000439 s2length = MAX(s2length, csum_length);
440 s2length = MIN(s2length, SUM_LENGTH);
441 }
442
443 sum->flength = len;
444 sum->blength = blength;
445 sum->s2length = s2length;
446 sum->count = (len + (blength - 1)) / blength;
447 sum->remainder = (len % blength);
448
449 if (sum->count && verbose > 2) {
Wayne Davisona255c592005-01-01 21:08:11 +0000450 rprintf(FINFO,
451 "count=%.0f rem=%ld blength=%ld s2length=%d flength=%.0f\n",
452 (double)sum->count, (long)sum->remainder, (long)sum->blength,
Wayne Davisonda9d12f2004-01-03 18:37:41 +0000453 sum->s2length, (double)sum->flength);
J.W. Schultz195bd902003-04-10 02:04:58 +0000454 }
455}
Wayne Davison80605142002-07-31 21:20:07 +0000456
Martin Poolbceec822002-03-19 03:39:42 +0000457
Wayne Davison80605142002-07-31 21:20:07 +0000458/*
459 * Generate and send a stream of signatures/checksums that describe a buffer
Martin Poole66dfd12002-04-08 09:05:26 +0000460 *
Wayne Davison80605142002-07-31 21:20:07 +0000461 * Generate approximately one checksum every block_len bytes.
462 */
Wayne Davisoncd6aa5b2004-09-07 21:44:02 +0000463static void generate_and_send_sums(int fd, OFF_T len, int f_out, int f_copy)
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000464{
Wayne Davisona1cbe762005-02-14 22:47:42 +0000465 int32 i;
Wayne Davison6e45e1d2004-07-20 21:35:55 +0000466 struct map_struct *mapbuf;
Wayne Davison80605142002-07-31 21:20:07 +0000467 struct sum_struct sum;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000468 OFF_T offset = 0;
469
Wayne Davison423dba82003-12-24 01:14:16 +0000470 sum_sizes_sqroot(&sum, len);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000471
Wayne Davison6e45e1d2004-07-20 21:35:55 +0000472 if (len > 0)
Wayne Davison96d910c2004-08-03 08:05:27 +0000473 mapbuf = map_file(fd, len, MAX_MAP_SIZE, sum.blength);
Wayne Davison6e45e1d2004-07-20 21:35:55 +0000474 else
475 mapbuf = NULL;
476
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000477 write_sum_head(f_out, &sum);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000478
Wayne Davison80605142002-07-31 21:20:07 +0000479 for (i = 0; i < sum.count; i++) {
Wayne Davisona255c592005-01-01 21:08:11 +0000480 int32 n1 = (int32)MIN(len, (OFF_T)sum.blength);
Wayne Davison6e45e1d2004-07-20 21:35:55 +0000481 char *map = map_ptr(mapbuf, offset, n1);
Wayne Davison80605142002-07-31 21:20:07 +0000482 uint32 sum1 = get_checksum1(map, n1);
483 char sum2[SUM_LENGTH];
Martin Poole66dfd12002-04-08 09:05:26 +0000484
Wayne Davisoncd6aa5b2004-09-07 21:44:02 +0000485 if (f_copy >= 0)
486 full_write(f_copy, map, n1);
487
Wayne Davison80605142002-07-31 21:20:07 +0000488 get_checksum2(map, n1, sum2);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000489
Wayne Davison80605142002-07-31 21:20:07 +0000490 if (verbose > 3) {
Martin Poole66dfd12002-04-08 09:05:26 +0000491 rprintf(FINFO,
Wayne Davisona255c592005-01-01 21:08:11 +0000492 "chunk[%.0f] offset=%.0f len=%ld sum1=%08lx\n",
493 (double)i, (double)offset, (long)n1,
Wayne Davison0e36d9d2004-01-03 19:28:03 +0000494 (unsigned long)sum1);
Wayne Davison80605142002-07-31 21:20:07 +0000495 }
496 write_int(f_out, sum1);
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000497 write_buf(f_out, sum2, sum.s2length);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000498 len -= n1;
499 offset += n1;
500 }
Wayne Davison6e45e1d2004-07-20 21:35:55 +0000501
502 if (mapbuf)
503 unmap_file(mapbuf);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000504}
505
Wayne Davison06a1dba2005-02-15 07:20:14 +0000506
Wayne Davison8e85be02005-02-14 02:41:27 +0000507/* Try to find a filename in the same dir as "fname" with a similar name. */
508static int find_fuzzy(struct file_struct *file, struct file_list *dirlist)
509{
510 int fname_len, fname_suf_len;
511 const char *fname_suf, *fname = file->basename;
Wayne Davisonf328e0f2005-03-17 08:59:48 +0000512 uint32 lowest_dist = 25 << 16; /* ignore a distance greater than 25 */
Wayne Davison8e85be02005-02-14 02:41:27 +0000513 int j, lowest_j = -1;
514
515 fname_len = strlen(fname);
516 fname_suf = find_filename_suffix(fname, fname_len, &fname_suf_len);
517
518 for (j = 0; j < dirlist->count; j++) {
519 struct file_struct *fp = dirlist->files[j];
520 const char *suf, *name;
521 int len, suf_len;
522 uint32 dist;
523
524 if (!S_ISREG(fp->mode) || !fp->length
525 || fp->flags & FLAG_NO_FUZZY)
526 continue;
527
528 name = fp->basename;
529
530 if (fp->length == file->length
531 && fp->modtime == file->modtime) {
532 if (verbose > 4) {
533 rprintf(FINFO,
534 "fuzzy size/modtime match for %s\n",
535 name);
536 }
537 return j;
538 }
539
540 len = strlen(name);
541 suf = find_filename_suffix(name, len, &suf_len);
542
543 dist = fuzzy_distance(name, len, fname, fname_len);
544 /* Add some extra weight to how well the suffixes match. */
545 dist += fuzzy_distance(suf, suf_len, fname_suf, fname_suf_len)
546 * 10;
547 if (verbose > 4) {
548 rprintf(FINFO, "fuzzy distance for %s = %d.%05d\n",
549 name, (int)(dist>>16), (int)(dist&0xFFFF));
550 }
551 if (dist <= lowest_dist) {
552 lowest_dist = dist;
553 lowest_j = j;
554 }
555 }
556
557 return lowest_j;
558}
559
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000560void check_for_finished_hlinks(int itemizing, enum logcode code)
561{
562 struct file_struct *file;
563 int ndx;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000564
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000565 while ((ndx = get_hlink_num()) != -1) {
566 if (ndx < 0 || ndx >= the_file_list->count)
567 continue;
568
569 file = the_file_list->files[ndx];
570 if (!file->link_u.links)
571 continue;
572
573 hard_link_cluster(file, ndx, itemizing, code);
574 }
575}
576
Wayne Davisoned7e7952005-03-05 16:42:52 +0000577static int phase = 0;
578
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000579/* Acts on the_file_list->file's ndx'th item, whose name is fname. If a dir,
Wayne Davison0492fdf2005-02-03 01:19:01 +0000580 * make sure it exists, and has the right permissions/timestamp info. For
581 * all other non-regular files (symlinks, etc.) we create them here. For
582 * regular files that have changed, we try to find a basis file and then
583 * start sending checksums.
Martin Poolef1aa912000-11-10 03:17:08 +0000584 *
Wayne Davison0492fdf2005-02-03 01:19:01 +0000585 * Note that f_out is set to -1 when doing final directory-permission and
586 * modification-time repair. */
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000587static void recv_generator(char *fname, struct file_struct *file, int ndx,
Wayne Davison7433d732005-02-26 06:52:55 +0000588 int itemizing, int maybe_PERMS_REPORT,
Wayne Davisonef20efc2005-03-09 18:54:12 +0000589 enum logcode code, int f_out)
Wayne Davison2cda2562003-03-21 18:53:00 +0000590{
Wayne Davison8174bc32005-02-19 23:01:52 +0000591 static int missing_below = -1, excluded_below = -1;
Wayne Davison7e9059d2005-03-14 22:22:42 +0000592 static char *fuzzy_dirname = "";
Wayne Davison8e85be02005-02-14 02:41:27 +0000593 static struct file_list *fuzzy_dirlist = NULL;
594 struct file_struct *fuzzy_file = NULL;
Wayne Davison41cfde62004-11-03 20:30:31 +0000595 int fd = -1, f_copy = -1;
Wayne Davison1f1d3682005-03-09 23:46:28 +0000596 STRUCT_STAT st, real_st, partial_st;
Wayne Davison41cfde62004-11-03 20:30:31 +0000597 struct file_struct *back_file = NULL;
Wayne Davison1f1d3682005-03-09 23:46:28 +0000598 int statret, real_ret, stat_errno;
Wayne Davison41cfde62004-11-03 20:30:31 +0000599 char *fnamecmp, *partialptr, *backupptr = NULL;
David Dykstra375a4551998-10-26 21:42:38 +0000600 char fnamecmpbuf[MAXPATHLEN];
Wayne Davison41cfde62004-11-03 20:30:31 +0000601 uchar fnamecmp_type;
Andrew Tridgellf7632fc1998-11-02 00:52:01 +0000602
Wayne Davisondfd5ba62004-01-04 18:43:18 +0000603 if (list_only)
604 return;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000605
Wayne Davison8e85be02005-02-14 02:41:27 +0000606 if (!fname) {
607 if (fuzzy_dirlist) {
608 flist_free(fuzzy_dirlist);
609 fuzzy_dirlist = NULL;
Wayne Davison7e9059d2005-03-14 22:22:42 +0000610 fuzzy_dirname = "";
Wayne Davison8e85be02005-02-14 02:41:27 +0000611 }
612 if (missing_below >= 0) {
613 dry_run--;
614 missing_below = -1;
615 }
616 return;
617 }
618
Wayne Davison4875d6b2005-02-07 20:36:43 +0000619 if (verbose > 2) {
620 rprintf(FINFO, "recv_generator(%s,%d)\n",
621 safe_fname(fname), ndx);
622 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000623
Wayne Davison8174bc32005-02-19 23:01:52 +0000624 if (server_filter_list.head) {
625 if (excluded_below >= 0) {
626 if (file->dir.depth > excluded_below)
627 goto skipping;
628 excluded_below = -1;
Wayne Davison3e35c342004-05-18 08:50:17 +0000629 }
Wayne Davison8174bc32005-02-19 23:01:52 +0000630 if (check_filter(&server_filter_list, fname,
631 S_ISDIR(file->mode)) < 0) {
632 if (S_ISDIR(file->mode))
633 excluded_below = file->dir.depth;
634 skipping:
635 if (verbose) {
636 rprintf(FINFO,
637 "skipping server-excluded file \"%s\"\n",
638 safe_fname(fname));
639 }
640 return;
641 }
Wayne Davison3e35c342004-05-18 08:50:17 +0000642 }
Wayne Davison97f9dca2004-05-18 00:14:10 +0000643
Wayne Davison8e85be02005-02-14 02:41:27 +0000644 if (missing_below >= 0 && file->dir.depth <= missing_below) {
Wayne Davisondf337832005-02-12 21:18:58 +0000645 dry_run--;
646 missing_below = -1;
647 }
Wayne Davison73f7af02004-08-05 18:18:36 +0000648 if (dry_run > 1) {
649 statret = -1;
650 stat_errno = ENOENT;
651 } else {
Wayne Davison8e85be02005-02-14 02:41:27 +0000652 if (fuzzy_basis && S_ISREG(file->mode)) {
653 char *dn = file->dirname ? file->dirname : ".";
Wayne Davisonccc51c82005-03-14 03:35:40 +0000654 if (fuzzy_dirname != dn
655 && strcmp(fuzzy_dirname, dn) != 0) {
Wayne Davison8e85be02005-02-14 02:41:27 +0000656 if (fuzzy_dirlist)
657 flist_free(fuzzy_dirlist);
Wayne Davisonccc51c82005-03-14 03:35:40 +0000658 fuzzy_dirlist = get_dirlist(dn, -1, 1);
Wayne Davison8e85be02005-02-14 02:41:27 +0000659 }
Wayne Davisonccc51c82005-03-14 03:35:40 +0000660 fuzzy_dirname = dn;
Wayne Davison8e85be02005-02-14 02:41:27 +0000661 }
662
Wayne Davison73f7af02004-08-05 18:18:36 +0000663 statret = link_stat(fname, &st,
664 keep_dirlinks && S_ISDIR(file->mode));
665 stat_errno = errno;
666 }
Martin Pool63787382001-12-14 02:16:20 +0000667
Wayne Davisona7260c42004-07-29 16:06:38 +0000668 if (only_existing && statret == -1 && stat_errno == ENOENT) {
Andrew Tridgell1347d512000-01-23 01:53:18 +0000669 /* we only want to update existing files */
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000670 if (verbose > 1) {
Wayne Davison15164c02005-04-10 19:44:16 +0000671 rprintf(FINFO, "not creating new %s \"%s\"\n",
672 S_ISDIR(file->mode) ? "directory" : "file",
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000673 safe_fname(fname));
674 }
Andrew Tridgell1347d512000-01-23 01:53:18 +0000675 return;
676 }
677
Wayne Davisond9b4d262004-07-23 16:59:38 +0000678 if (statret == 0 && !preserve_perms
679 && S_ISDIR(st.st_mode) == S_ISDIR(file->mode)) {
Andrew Tridgell4df9f361999-10-31 04:28:03 +0000680 /* if the file exists already and we aren't perserving
Wayne Davison2cda2562003-03-21 18:53:00 +0000681 * permissions then act as though the remote end sent
682 * us the file permissions we already have */
Wayne Davison67e78a82004-03-07 20:29:59 +0000683 file->mode = (file->mode & ~CHMOD_BITS)
684 | (st.st_mode & CHMOD_BITS);
Andrew Tridgell4df9f361999-10-31 04:28:03 +0000685 }
686
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000687 if (S_ISDIR(file->mode)) {
Wayne Davison2cda2562003-03-21 18:53:00 +0000688 /* The file to be received is a directory, so we need
689 * to prepare appropriately. If there is already a
690 * file of that name and it is *not* a directory, then
691 * we need to delete it. If it doesn't exist, then
Wayne Davison027428e2005-01-20 22:59:54 +0000692 * (perhaps recursively) create it. */
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000693 if (statret == 0 && !S_ISDIR(st.st_mode)) {
Wayne Davison1f1d3682005-03-09 23:46:28 +0000694 if (delete_item(fname, st.st_mode, DEL_TERSE) < 0)
695 return;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000696 statret = -1;
697 }
Wayne Davisondf337832005-02-12 21:18:58 +0000698 if (dry_run && statret != 0 && missing_below < 0) {
699 missing_below = file->dir.depth;
700 dry_run++;
701 }
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000702 if (itemizing && f_out != -1) {
703 itemize(file, ndx, statret, &st,
Wayne Davisonef20efc2005-03-09 18:54:12 +0000704 statret ? ITEM_LOCAL_CHANGE : 0, 0, NULL);
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000705 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000706 if (statret != 0 && do_mkdir(fname,file->mode) != 0 && errno != EEXIST) {
Wayne Davison027428e2005-01-20 22:59:54 +0000707 if (!relative_paths || errno != ENOENT
708 || create_directory_path(fname, orig_umask) < 0
709 || do_mkdir(fname, file->mode) < 0) {
Wayne Davisond62bcc12004-05-15 19:31:10 +0000710 rsyserr(FERROR, errno,
711 "recv_generator: mkdir %s failed",
712 full_fname(fname));
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000713 }
714 }
Wayne Davison716e73d2004-06-05 16:16:30 +0000715 if (set_perms(fname, file, statret ? NULL : &st, 0)
Wayne Davison6c3862f2005-02-19 22:16:45 +0000716 && verbose && code && f_out != -1)
717 rprintf(code, "%s/\n", safe_fname(fname));
Wayne Davisonf75a53e2005-03-05 18:58:42 +0000718 if (delete_during && f_out != -1 && !phase && dry_run < 2
Wayne Davison31937d32005-02-03 02:01:11 +0000719 && (file->flags & FLAG_DEL_HERE))
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000720 delete_in_dir(the_file_list, fname, file);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000721 return;
Wayne Davison06a1dba2005-02-15 07:20:14 +0000722 }
Wayne Davison6c3862f2005-02-19 22:16:45 +0000723
Wayne Davison06a1dba2005-02-15 07:20:14 +0000724 if (max_size && file->length > max_size) {
Wayne Davison4875d6b2005-02-07 20:36:43 +0000725 if (verbose > 1) {
Wayne Davison2a5d5a82005-04-22 16:45:33 +0000726 if (the_file_list->count == 1)
727 fname = f_name(file);
Wayne Davison4875d6b2005-02-07 20:36:43 +0000728 rprintf(FINFO, "%s is over max-size\n",
729 safe_fname(fname));
730 }
Wayne Davison7d1bfaf2004-11-11 01:45:28 +0000731 return;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000732 }
733
734 if (preserve_links && S_ISLNK(file->mode)) {
Wayne Davison4f5b0752005-02-14 00:53:43 +0000735#ifdef SUPPORT_LINKS
Wayne Davison728d0922004-01-11 07:28:45 +0000736 if (safe_symlinks && unsafe_symlink(file->u.link, fname)) {
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000737 if (verbose) {
Wayne Davison2a5d5a82005-04-22 16:45:33 +0000738 if (the_file_list->count == 1)
739 fname = f_name(file);
Wayne Davison4875d6b2005-02-07 20:36:43 +0000740 rprintf(FINFO,
741 "ignoring unsafe symlink %s -> \"%s\"\n",
742 full_fname(fname),
743 safe_fname(file->u.link));
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000744 }
745 return;
746 }
747 if (statret == 0) {
Wayne Davison7e384102005-01-19 20:10:57 +0000748 char lnk[MAXPATHLEN];
749 int len;
750
Wayne Davison8a8356b2005-02-19 02:39:21 +0000751 if (!S_ISDIR(st.st_mode)
Wayne Davison7e384102005-01-19 20:10:57 +0000752 && (len = readlink(fname, lnk, MAXPATHLEN-1)) > 0) {
753 lnk[len] = 0;
Martin Pool85d4d142001-12-14 02:14:58 +0000754 /* A link already pointing to the
755 * right place -- no further action
756 * required. */
Wayne Davison7e384102005-01-19 20:10:57 +0000757 if (strcmp(lnk, file->u.link) == 0) {
Wayne Davison6c3862f2005-02-19 22:16:45 +0000758 if (itemizing) {
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000759 itemize(file, ndx, 0, &st, 0,
Wayne Davisonef20efc2005-03-09 18:54:12 +0000760 0, NULL);
Wayne Davisonc557eb82005-02-16 08:10:25 +0000761 }
Wayne Davisonc41b52c2004-05-13 06:55:01 +0000762 set_perms(fname, file, &st,
Wayne Davison8a8356b2005-02-19 02:39:21 +0000763 maybe_PERMS_REPORT);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000764 return;
765 }
Wayne Davison2cda2562003-03-21 18:53:00 +0000766 }
Wayne Davison7e384102005-01-19 20:10:57 +0000767 /* Not the right symlink (or not a symlink), so
768 * delete it. */
Wayne Davison1f1d3682005-03-09 23:46:28 +0000769 if (delete_item(fname, st.st_mode, DEL_TERSE) < 0)
770 return;
771 if (!S_ISLNK(st.st_mode))
Wayne Davison88b218f2005-02-18 20:17:20 +0000772 statret = -1;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000773 }
Wayne Davison728d0922004-01-11 07:28:45 +0000774 if (do_symlink(file->u.link,fname) != 0) {
Wayne Davisond62bcc12004-05-15 19:31:10 +0000775 rsyserr(FERROR, errno, "symlink %s -> \"%s\" failed",
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000776 full_fname(fname), safe_fname(file->u.link));
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000777 } else {
778 set_perms(fname,file,NULL,0);
Wayne Davison6c3862f2005-02-19 22:16:45 +0000779 if (itemizing) {
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000780 itemize(file, ndx, statret, &st,
Wayne Davisonef20efc2005-03-09 18:54:12 +0000781 ITEM_LOCAL_CHANGE, 0, NULL);
Wayne Davison6c3862f2005-02-19 22:16:45 +0000782 }
783 if (code && verbose) {
784 rprintf(code, "%s -> %s\n", safe_fname(fname),
Wayne Davisonc557eb82005-02-16 08:10:25 +0000785 safe_fname(file->u.link));
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000786 }
Wayne Davisonfe960182005-02-20 01:04:43 +0000787 if (remove_sent_files && !dry_run) {
788 char numbuf[4];
789 SIVAL(numbuf, 0, ndx);
790 send_msg(MSG_SUCCESS, numbuf, 4);
791 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000792 }
793#endif
794 return;
795 }
796
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000797 if (am_root && preserve_devices && IS_DEVICE(file->mode)) {
Wayne Davison2cda2562003-03-21 18:53:00 +0000798 if (statret != 0 ||
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000799 st.st_mode != file->mode ||
Wayne Davison3915fd72004-04-12 16:52:43 +0000800 st.st_rdev != file->u.rdev) {
Wayne Davison1f1d3682005-03-09 23:46:28 +0000801 if (delete_item(fname, st.st_mode, DEL_TERSE) < 0)
802 return;
Wayne Davison59faec82005-02-26 18:03:49 +0000803 if (!IS_DEVICE(st.st_mode))
Wayne Davison88b218f2005-02-18 20:17:20 +0000804 statret = -1;
Wayne Davisond62bcc12004-05-15 19:31:10 +0000805 if (verbose > 2) {
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000806 rprintf(FINFO,"mknod(%s,0%o,0x%x)\n",
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000807 safe_fname(fname),
808 (int)file->mode, (int)file->u.rdev);
Wayne Davisond62bcc12004-05-15 19:31:10 +0000809 }
Wayne Davison728d0922004-01-11 07:28:45 +0000810 if (do_mknod(fname,file->mode,file->u.rdev) != 0) {
Wayne Davisond62bcc12004-05-15 19:31:10 +0000811 rsyserr(FERROR, errno, "mknod %s failed",
812 full_fname(fname));
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000813 } else {
814 set_perms(fname,file,NULL,0);
Wayne Davison6c3862f2005-02-19 22:16:45 +0000815 if (itemizing) {
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000816 itemize(file, ndx, statret, &st,
Wayne Davisonef20efc2005-03-09 18:54:12 +0000817 ITEM_LOCAL_CHANGE, 0, NULL);
Wayne Davison6c3862f2005-02-19 22:16:45 +0000818 }
819 if (code && verbose) {
820 rprintf(code, "%s\n",
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000821 safe_fname(fname));
822 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000823 }
824 } else {
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000825 if (itemizing)
Wayne Davisonef20efc2005-03-09 18:54:12 +0000826 itemize(file, ndx, statret, &st, 0, 0, NULL);
Wayne Davison8a8356b2005-02-19 02:39:21 +0000827 set_perms(fname, file, &st, maybe_PERMS_REPORT);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000828 }
829 return;
830 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000831
Wayne Davisond8169e62005-04-23 17:48:31 +0000832 if (preserve_hard_links
833 && hard_link_check(file, ndx, fname, statret, &st,
834 itemizing, code, HL_CHECK_MASTER))
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000835 return;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000836
837 if (!S_ISREG(file->mode)) {
Wayne Davison2a5d5a82005-04-22 16:45:33 +0000838 if (the_file_list->count == 1)
839 fname = f_name(file);
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000840 rprintf(FINFO, "skipping non-regular file \"%s\"\n",
841 safe_fname(fname));
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000842 return;
843 }
844
Wayne Davisonc3cbcfb2005-03-03 01:33:51 +0000845 if (opt_ignore_existing && statret == 0) {
846 if (verbose > 1)
847 rprintf(FINFO, "%s exists\n", safe_fname(fname));
848 return;
849 }
850
851 if (update_only && statret == 0
852 && cmp_modtime(st.st_mtime, file->modtime) > 0) {
853 if (verbose > 1)
854 rprintf(FINFO, "%s is newer\n", safe_fname(fname));
855 return;
856 }
857
David Dykstra375a4551998-10-26 21:42:38 +0000858 fnamecmp = fname;
Wayne Davison41cfde62004-11-03 20:30:31 +0000859 fnamecmp_type = FNAMECMP_FNAME;
David Dykstra375a4551998-10-26 21:42:38 +0000860
Wayne Davison1f1d3682005-03-09 23:46:28 +0000861 if (statret == 0 && !S_ISREG(st.st_mode)) {
862 if (delete_item(fname, st.st_mode, DEL_TERSE) != 0)
863 return;
864 statret = -1;
865 stat_errno = ENOENT;
866 }
867
Wayne Davison06a1dba2005-02-15 07:20:14 +0000868 if (statret != 0 && basis_dir[0] != NULL) {
Wayne Davisonaef98822005-03-02 09:09:15 +0000869 int best_match = -1;
Wayne Davisonb7e86282004-11-27 17:52:47 +0000870 int match_level = 0;
871 int i = 0;
872 do {
873 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
874 basis_dir[i], fname);
Wayne Davisonaef98822005-03-02 09:09:15 +0000875 if (link_stat(fnamecmpbuf, &st, 0) < 0
876 || !S_ISREG(st.st_mode))
877 continue;
878 switch (match_level) {
879 case 0:
880 best_match = i;
881 match_level = 1;
Wayne Davisonaef98822005-03-02 09:09:15 +0000882 /* FALL THROUGH */
883 case 1:
884 if (!unchanged_file(fnamecmpbuf, file, &st))
885 continue;
886 best_match = i;
887 match_level = 2;
Wayne Davison967866d2005-03-11 17:36:03 +0000888 if (copy_dest)
889 break;
Wayne Davisonaef98822005-03-02 09:09:15 +0000890 /* FALL THROUGH */
891 case 2:
892 if (!unchanged_attrs(file, &st))
893 continue;
894 best_match = i;
Wayne Davisonb7e86282004-11-27 17:52:47 +0000895 match_level = 3;
896 break;
897 }
Wayne Davisonaef98822005-03-02 09:09:15 +0000898 break;
Wayne Davisonb7e86282004-11-27 17:52:47 +0000899 } while (basis_dir[++i] != NULL);
Wayne Davisonaef98822005-03-02 09:09:15 +0000900 if (match_level) {
Wayne Davison9ba46342005-03-02 09:51:54 +0000901 statret = 0;
Wayne Davisonaef98822005-03-02 09:09:15 +0000902 if (i != best_match) {
903 i = best_match;
Wayne Davisonb7e86282004-11-27 17:52:47 +0000904 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
905 basis_dir[i], fname);
Wayne Davison9ba46342005-03-02 09:51:54 +0000906 if (link_stat(fnamecmpbuf, &st, 0) < 0) {
907 match_level = 0;
908 statret = -1;
Wayne Davisona1d23b52005-03-03 00:14:35 +0000909 stat_errno = errno;
Wayne Davison9ba46342005-03-02 09:51:54 +0000910 }
Wayne Davisonb7e86282004-11-27 17:52:47 +0000911 }
Wayne Davison4f5b0752005-02-14 00:53:43 +0000912#ifdef HAVE_LINK
Wayne Davisonee1d11c2005-03-05 00:21:42 +0000913 if (link_dest && match_level == 3) {
914 if (hard_link_one(file, ndx, fname, -1, &st,
915 fnamecmpbuf, 1,
916 itemizing && verbose > 1,
917 code) == 0)
918 return;
Wayne Davisone2243312005-03-02 17:48:36 +0000919 match_level = 2;
Wayne Davison70b54e42005-03-02 17:27:19 +0000920 }
David Dykstra59c95e42002-08-29 14:44:55 +0000921#endif
Wayne Davison967866d2005-03-11 17:36:03 +0000922 if (match_level == 2) {
923 /* Copy the file locally. */
924 if (copy_file(fnamecmpbuf, fname, file->mode) < 0) {
925 if (verbose) {
926 rsyserr(FINFO, errno,
927 "copy_file %s => %s",
928 full_fname(fnamecmpbuf),
929 safe_fname(fname));
930 }
931 match_level = 0;
932 statret = -1;
Wayne Davison552a2182005-04-01 18:12:22 +0000933 } else {
934 if (itemizing) {
935 itemize(file, ndx, 0, &st,
936 ITEM_LOCAL_CHANGE, 0,
937 NULL);
938 } else if (verbose && code) {
939 rprintf(code, "%s\n",
940 safe_fname(fname));
941 }
942 set_perms(fname, file, NULL,
943 maybe_PERMS_REPORT);
944 if (preserve_hard_links
945 && file->link_u.links) {
946 hard_link_cluster(file, ndx,
947 itemizing,
948 code);
949 }
950 return;
951 }
Wayne Davison967866d2005-03-11 17:36:03 +0000952 } else if (compare_dest || match_level == 1) {
Wayne Davisone7d13fe2004-07-28 10:04:06 +0000953 fnamecmp = fnamecmpbuf;
Wayne Davison2be2fb32005-01-15 21:17:39 +0000954 fnamecmp_type = i;
Wayne Davison41cfde62004-11-03 20:30:31 +0000955 }
Wayne Davisone7d13fe2004-07-28 10:04:06 +0000956 }
957 }
958
Wayne Davison1f1d3682005-03-09 23:46:28 +0000959 real_ret = statret;
960 real_st = st;
David Dykstra375a4551998-10-26 21:42:38 +0000961
Wayne Davison9d954dc2005-01-14 18:20:46 +0000962 if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL
Wayne Davison72c19bb2004-09-07 17:03:51 +0000963 && link_stat(partialptr, &partial_st, 0) == 0
964 && S_ISREG(partial_st.st_mode)) {
Wayne Davison06a1dba2005-02-15 07:20:14 +0000965 if (statret != 0)
Wayne Davison72c19bb2004-09-07 17:03:51 +0000966 goto prepare_to_open;
967 } else
968 partialptr = NULL;
Wayne Davison89f7eff2004-09-07 16:50:07 +0000969
Wayne Davison06a1dba2005-02-15 07:20:14 +0000970 if (statret != 0 && fuzzy_basis && dry_run <= 1) {
Wayne Davison8e85be02005-02-14 02:41:27 +0000971 int j = find_fuzzy(file, fuzzy_dirlist);
972 if (j >= 0) {
973 fuzzy_file = fuzzy_dirlist->files[j];
974 f_name_to(fuzzy_file, fnamecmpbuf);
975 if (verbose > 2) {
976 rprintf(FINFO, "fuzzy basis selected for %s: %s\n",
977 safe_fname(fname), safe_fname(fnamecmpbuf));
978 }
Wayne Davison8e85be02005-02-14 02:41:27 +0000979 st.st_size = fuzzy_file->length;
Wayne Davison8e85be02005-02-14 02:41:27 +0000980 statret = 0;
981 fnamecmp = fnamecmpbuf;
982 fnamecmp_type = FNAMECMP_FUZZY;
983 }
984 }
985
Wayne Davison06a1dba2005-02-15 07:20:14 +0000986 if (statret != 0) {
Wayne Davisond8169e62005-04-23 17:48:31 +0000987 if (preserve_hard_links
988 && hard_link_check(file, ndx, fname, statret, &st,
989 itemizing, code, HL_SKIP))
Wayne Davison6dff5992004-01-25 22:32:09 +0000990 return;
Wayne Davison41cfde62004-11-03 20:30:31 +0000991 if (stat_errno == ENOENT)
992 goto notify_others;
993 if (verbose > 1) {
Wayne Davisone7d13fe2004-07-28 10:04:06 +0000994 rsyserr(FERROR, stat_errno,
995 "recv_generator: failed to stat %s",
Wayne Davisond62bcc12004-05-15 19:31:10 +0000996 full_fname(fname));
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000997 }
998 return;
999 }
1000
Wayne Davison2be2fb32005-01-15 21:17:39 +00001001 if (!compare_dest && fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
Wayne Davisonb7e86282004-11-27 17:52:47 +00001002 ;
Wayne Davison8e85be02005-02-14 02:41:27 +00001003 else if (fnamecmp_type == FNAMECMP_FUZZY)
1004 ;
Wayne Davisonb7e86282004-11-27 17:52:47 +00001005 else if (unchanged_file(fnamecmp, file, &st)) {
Wayne Davisona1d23b52005-03-03 00:14:35 +00001006 if (fnamecmp_type == FNAMECMP_FNAME) {
Wayne Davison1f1d3682005-03-09 23:46:28 +00001007 if (itemizing) {
1008 itemize(file, ndx, real_ret, &real_st,
1009 0, 0, NULL);
1010 }
Wayne Davison8a8356b2005-02-19 02:39:21 +00001011 set_perms(fname, file, &st, maybe_PERMS_REPORT);
Wayne Davisonee1d11c2005-03-05 00:21:42 +00001012 if (preserve_hard_links && file->link_u.links)
1013 hard_link_cluster(file, ndx, itemizing, code);
Wayne Davisona1d23b52005-03-03 00:14:35 +00001014 return;
1015 }
1016 /* Only --compare-dest gets here. */
Wayne Davison967866d2005-03-11 17:36:03 +00001017 itemize(file, ndx, real_ret, &real_st,
1018 ITEM_NO_DEST_AND_NO_UPDATE, 0, NULL);
1019 return;
Andrew Tridgell2f03f951998-07-25 02:25:22 +00001020 }
1021
Wayne Davison89f7eff2004-09-07 16:50:07 +00001022prepare_to_open:
Wayne Davison9d954dc2005-01-14 18:20:46 +00001023 if (partialptr) {
1024 st = partial_st;
1025 fnamecmp = partialptr;
1026 fnamecmp_type = FNAMECMP_PARTIAL_DIR;
1027 statret = 0;
1028 }
1029
Wayne Davisonbeb51aa2005-04-09 18:59:52 +00001030 if (!do_xfers || read_batch || whole_file)
Wayne Davison3841a042005-02-11 20:26:08 +00001031 goto notify_others;
Andrew Tridgell2f03f951998-07-25 02:25:22 +00001032
Wayne Davison8e85be02005-02-14 02:41:27 +00001033 if (fuzzy_basis) {
1034 int j = flist_find(fuzzy_dirlist, file);
1035 if (j >= 0) /* don't use changing file as future fuzzy basis */
1036 fuzzy_dirlist->files[j]->flags |= FLAG_NO_FUZZY;
1037 }
1038
Wayne Davison2cda2562003-03-21 18:53:00 +00001039 /* open the file */
Andrew Tridgell8c9fd201999-10-25 22:04:09 +00001040 fd = do_open(fnamecmp, O_RDONLY, 0);
Andrew Tridgell2f03f951998-07-25 02:25:22 +00001041
1042 if (fd == -1) {
Wayne Davisond62bcc12004-05-15 19:31:10 +00001043 rsyserr(FERROR, errno, "failed to open %s, continuing",
1044 full_fname(fnamecmp));
Wayne Davisoncd6aa5b2004-09-07 21:44:02 +00001045 pretend_missing:
David Dykstra60be6ac2000-01-06 16:15:36 +00001046 /* pretend the file didn't exist */
Wayne Davisond8169e62005-04-23 17:48:31 +00001047 if (preserve_hard_links
1048 && hard_link_check(file, ndx, fname, statret, &st,
1049 itemizing, code, HL_SKIP))
Wayne Davison6dff5992004-01-25 22:32:09 +00001050 return;
Wayne Davison1f1d3682005-03-09 23:46:28 +00001051 statret = real_ret = -1;
Wayne Davison41cfde62004-11-03 20:30:31 +00001052 goto notify_others;
Andrew Tridgell2f03f951998-07-25 02:25:22 +00001053 }
1054
Wayne Davisonef20efc2005-03-09 18:54:12 +00001055 if (inplace && make_backups && fnamecmp_type == FNAMECMP_FNAME) {
Wayne Davisoncd6aa5b2004-09-07 21:44:02 +00001056 if (!(backupptr = get_backup_name(fname))) {
1057 close(fd);
1058 return;
1059 }
Wayne Davison78424182005-01-25 10:39:14 +00001060 if (!(back_file = make_file(fname, NULL, NO_FILTERS))) {
Wayne Davisoncd6aa5b2004-09-07 21:44:02 +00001061 close(fd);
1062 goto pretend_missing;
1063 }
1064 if (robust_unlink(backupptr) && errno != ENOENT) {
1065 rsyserr(FERROR, errno, "unlink %s",
1066 full_fname(backupptr));
1067 free(back_file);
1068 close(fd);
1069 return;
1070 }
1071 if ((f_copy = do_open(backupptr,
1072 O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0) {
1073 rsyserr(FERROR, errno, "open %s",
1074 full_fname(backupptr));
1075 free(back_file);
1076 close(fd);
1077 return;
1078 }
Wayne Davison41cfde62004-11-03 20:30:31 +00001079 fnamecmp_type = FNAMECMP_BACKUP;
Wayne Davisoncd6aa5b2004-09-07 21:44:02 +00001080 }
1081
Wayne Davisondfd5ba62004-01-04 18:43:18 +00001082 if (verbose > 3) {
Wayne Davisonecc81fc2004-07-26 16:36:59 +00001083 rprintf(FINFO, "gen mapped %s of size %.0f\n",
1084 safe_fname(fnamecmp), (double)st.st_size);
Wayne Davisondfd5ba62004-01-04 18:43:18 +00001085 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +00001086
Andrew Tridgell2f03f951998-07-25 02:25:22 +00001087 if (verbose > 2)
Wayne Davison0492fdf2005-02-03 01:19:01 +00001088 rprintf(FINFO, "generating and sending sums for %d\n", ndx);
Andrew Tridgell2f03f951998-07-25 02:25:22 +00001089
Wayne Davison41cfde62004-11-03 20:30:31 +00001090notify_others:
Wayne Davison0492fdf2005-02-03 01:19:01 +00001091 write_int(f_out, ndx);
Wayne Davison6c3862f2005-02-19 22:16:45 +00001092 if (itemizing) {
Wayne Davisonee1d11c2005-03-05 00:21:42 +00001093 int iflags = ITEM_TRANSFER;
Wayne Davison8237f932005-02-22 21:43:57 +00001094 if (always_checksum)
Wayne Davisona1d23b52005-03-03 00:14:35 +00001095 iflags |= ITEM_REPORT_CHECKSUM;
Wayne Davison352963d2005-02-22 22:12:59 +00001096 if (fnamecmp_type != FNAMECMP_FNAME)
Wayne Davisonef20efc2005-03-09 18:54:12 +00001097 iflags |= ITEM_BASIS_TYPE_FOLLOWS;
1098 if (fnamecmp_type == FNAMECMP_FUZZY)
1099 iflags |= ITEM_XNAME_FOLLOWS;
Wayne Davison1f1d3682005-03-09 23:46:28 +00001100 itemize(file, -1, real_ret, &real_st, iflags, fnamecmp_type,
Wayne Davisonef20efc2005-03-09 18:54:12 +00001101 fuzzy_file ? fuzzy_file->basename : NULL);
Wayne Davison8e85be02005-02-14 02:41:27 +00001102 }
Wayne Davisoncd6aa5b2004-09-07 21:44:02 +00001103
Wayne Davisonbeb51aa2005-04-09 18:59:52 +00001104 if (!do_xfers) {
Wayne Davisonee1d11c2005-03-05 00:21:42 +00001105 if (preserve_hard_links && file->link_u.links)
1106 hard_link_cluster(file, ndx, itemizing, code);
1107 return;
1108 }
1109 if (read_batch)
Wayne Davison41cfde62004-11-03 20:30:31 +00001110 return;
Andrew Tridgell2f03f951998-07-25 02:25:22 +00001111
Wayne Davison33ab4ad2005-03-03 02:58:48 +00001112 if (statret != 0 || whole_file) {
Wayne Davison41cfde62004-11-03 20:30:31 +00001113 write_sum_head(f_out, NULL);
Wayne Davisone86ae6b2005-03-03 01:50:43 +00001114 return;
1115 }
1116
1117 generate_and_send_sums(fd, st.st_size, f_out, f_copy);
1118
1119 if (f_copy >= 0) {
1120 close(f_copy);
1121 set_perms(backupptr, back_file, NULL, 0);
1122 if (verbose > 1) {
1123 rprintf(FINFO, "backed up %s to %s\n",
1124 safe_fname(fname), safe_fname(backupptr));
1125 }
1126 free(back_file);
1127 }
1128
1129 close(fd);
Andrew Tridgell2f03f951998-07-25 02:25:22 +00001130}
1131
1132
Wayne Davisonef20efc2005-03-09 18:54:12 +00001133void generate_files(int f_out, struct file_list *flist, char *local_name)
Andrew Tridgell2f03f951998-07-25 02:25:22 +00001134{
Wayne Davisonee1d11c2005-03-05 00:21:42 +00001135 int i, lull_mod;
Wayne Davison968c8032004-01-02 08:19:26 +00001136 char fbuf[MAXPATHLEN];
Wayne Davison7433d732005-02-26 06:52:55 +00001137 int itemizing, maybe_PERMS_REPORT;
1138 enum logcode code;
Wayne Davison3ea9bbd2005-01-24 06:19:58 +00001139 int need_retouch_dir_times = preserve_times && !omit_dir_times;
1140 int need_retouch_dir_perms = 0;
Wayne Davison0492fdf2005-02-03 01:19:01 +00001141 int save_only_existing = only_existing;
1142 int save_opt_ignore_existing = opt_ignore_existing;
Wayne Davison083acd42005-03-29 10:41:26 +00001143 int save_do_progress = do_progress;
Wayne Davisoncd908ef2005-04-07 08:13:44 +00001144 int save_make_backups = make_backups;
Wayne Davisonee1d11c2005-03-05 00:21:42 +00001145
1146 allowed_lull = read_batch ? 0 : (io_timeout + 1) / 2;
1147 lull_mod = allowed_lull * 5;
Wayne Davison7433d732005-02-26 06:52:55 +00001148
1149 if (protocol_version >= 29) {
1150 itemizing = 1;
1151 maybe_PERMS_REPORT = log_format_has_i ? 0 : PERMS_REPORT;
1152 code = daemon_log_format_has_i ? 0 : FLOG;
1153 } else if (am_daemon) {
Wayne Davisonbeb51aa2005-04-09 18:59:52 +00001154 itemizing = daemon_log_format_has_i && do_xfers;
Wayne Davison7433d732005-02-26 06:52:55 +00001155 maybe_PERMS_REPORT = PERMS_REPORT;
Wayne Davisonbeb51aa2005-04-09 18:59:52 +00001156 code = itemizing || !do_xfers ? FCLIENT : FINFO;
Wayne Davison7433d732005-02-26 06:52:55 +00001157 } else if (!am_server) {
1158 itemizing = log_format_has_i;
1159 maybe_PERMS_REPORT = log_format_has_i ? 0 : PERMS_REPORT;
1160 code = itemizing ? 0 : FINFO;
1161 } else {
1162 itemizing = 0;
1163 maybe_PERMS_REPORT = PERMS_REPORT;
1164 code = FINFO;
1165 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +00001166
Wayne Davison45e08ed2004-01-27 08:02:31 +00001167 if (verbose > 2) {
1168 rprintf(FINFO, "generator starting pid=%ld count=%d\n",
1169 (long)getpid(), flist->count);
1170 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +00001171
Wayne Davison59faec82005-02-26 18:03:49 +00001172 if (delete_before && !local_name && flist->count > 0)
Wayne Davisonee1d11c2005-03-05 00:21:42 +00001173 do_delete_pass(flist);
Wayne Davison083acd42005-03-29 10:41:26 +00001174 do_progress = 0;
Wayne Davison59faec82005-02-26 18:03:49 +00001175
Wayne Davison33ab4ad2005-03-03 02:58:48 +00001176 if (whole_file < 0)
1177 whole_file = 0;
Martin Pool3e7053a2002-04-08 08:06:18 +00001178 if (verbose >= 2) {
Wayne Davison1b1fef22005-02-20 22:11:43 +00001179 rprintf(FINFO, "delta-transmission %s\n",
Wayne Davison33ab4ad2005-03-03 02:58:48 +00001180 whole_file
Wayne Davison1b1fef22005-02-20 22:11:43 +00001181 ? "disabled for local transfer or --whole-file"
1182 : "enabled");
Martin Pool3e7053a2002-04-08 08:06:18 +00001183 }
Wayne Davison2cda2562003-03-21 18:53:00 +00001184
Wayne Davison9ac23952005-02-25 22:01:36 +00001185 if (protocol_version < 29)
Wayne Davison7433d732005-02-26 06:52:55 +00001186 ignore_timeout = 1;
Andrew Tridgella57873b2001-09-09 04:42:09 +00001187
Andrew Tridgell2f03f951998-07-25 02:25:22 +00001188 for (i = 0; i < flist->count; i++) {
1189 struct file_struct *file = flist->files[i];
Andrew Tridgell2f03f951998-07-25 02:25:22 +00001190
Wayne Davisondfd5ba62004-01-04 18:43:18 +00001191 if (!file->basename)
1192 continue;
Wayne Davison0492fdf2005-02-03 01:19:01 +00001193
Wayne Davison1f7e29b2005-03-09 03:49:07 +00001194 recv_generator(local_name ? local_name : f_name_to(file, fbuf),
1195 file, i, itemizing, maybe_PERMS_REPORT, code,
Wayne Davisonef20efc2005-03-09 18:54:12 +00001196 f_out);
Wayne Davison1f7e29b2005-03-09 03:49:07 +00001197
Wayne Davison0492fdf2005-02-03 01:19:01 +00001198 /* We need to ensure that any dirs we create have writeable
1199 * permissions during the time we are putting files within
1200 * them. This is then fixed after the transfer is done. */
Wayne Davison41b84ce2005-03-31 23:30:03 +00001201 if (!am_root && S_ISDIR(file->mode) && !(file->mode & S_IWUSR)
1202 && !list_only) {
Wayne Davison1f7e29b2005-03-09 03:49:07 +00001203 int mode = file->mode | S_IWUSR; /* user write */
1204 char *fname = local_name ? local_name : fbuf;
1205 if (do_chmod(fname, mode & CHMOD_BITS) < 0) {
1206 rsyserr(FERROR, errno,
1207 "failed to modify permissions on %s",
1208 full_fname(fname));
1209 }
Wayne Davison3ea9bbd2005-01-24 06:19:58 +00001210 need_retouch_dir_perms = 1;
Andrew Tridgell2f03f951998-07-25 02:25:22 +00001211 }
1212
Wayne Davisonee1d11c2005-03-05 00:21:42 +00001213 if (preserve_hard_links)
1214 check_for_finished_hlinks(itemizing, code);
Wayne Davison9ac23952005-02-25 22:01:36 +00001215
Wayne Davison18a11cf2005-02-27 09:02:32 +00001216 if (allowed_lull && !(i % lull_mod))
Wayne Davisonee1d11c2005-03-05 00:21:42 +00001217 maybe_send_keepalive();
Wayne Davisonc2523a02005-04-07 08:32:32 +00001218 else if (!(i % 200))
Wayne Davison417099f2005-04-05 20:08:51 +00001219 maybe_flush_socket();
Andrew Tridgell2f03f951998-07-25 02:25:22 +00001220 }
Wayne Davisonef20efc2005-03-09 18:54:12 +00001221 recv_generator(NULL, NULL, 0, 0, 0, code, -1);
Wayne Davisonc93fad52005-01-30 10:06:18 +00001222 if (delete_during)
Wayne Davisonee1d11c2005-03-05 00:21:42 +00001223 delete_in_dir(NULL, NULL, NULL);
Andrew Tridgell2f03f951998-07-25 02:25:22 +00001224
1225 phase++;
1226 csum_length = SUM_LENGTH;
Wayne Davison0492fdf2005-02-03 01:19:01 +00001227 only_existing = max_size = opt_ignore_existing = 0;
1228 update_only = always_checksum = size_only = 0;
Wayne Davisone1f67412004-06-30 07:27:30 +00001229 ignore_times = 1;
Wayne Davisone6bc6f42005-02-27 07:50:43 +00001230 make_backups = 0; /* avoid a duplicate backup for inplace processing */
Andrew Tridgell2f03f951998-07-25 02:25:22 +00001231
Wayne Davison9ac23952005-02-25 22:01:36 +00001232 /* We expect to just sit around now, so don't exit on a timeout.
1233 * If we really get a timeout then the other process should exit. */
1234 ignore_timeout = 1;
1235
Andrew Tridgell2f03f951998-07-25 02:25:22 +00001236 if (verbose > 2)
1237 rprintf(FINFO,"generate_files phase=%d\n",phase);
1238
Wayne Davison7daccb82004-07-12 20:42:48 +00001239 write_int(f_out, -1);
Andrew Tridgell2f03f951998-07-25 02:25:22 +00001240
J.W. Schultzbc63ae32003-03-31 17:28:34 +00001241 /* files can cycle through the system more than once
1242 * to catch initial checksum errors */
Wayne Davisonee1d11c2005-03-05 00:21:42 +00001243 while ((i = get_redo_num(itemizing, code)) != -1) {
J.W. Schultzbc63ae32003-03-31 17:28:34 +00001244 struct file_struct *file = flist->files[i];
Wayne Davison3fef5362004-01-22 04:38:18 +00001245 recv_generator(local_name ? local_name : f_name_to(file, fbuf),
Wayne Davisonee1d11c2005-03-05 00:21:42 +00001246 file, i, itemizing, maybe_PERMS_REPORT, code,
Wayne Davisonef20efc2005-03-09 18:54:12 +00001247 f_out);
Andrew Tridgell2f03f951998-07-25 02:25:22 +00001248 }
J.W. Schultzbc63ae32003-03-31 17:28:34 +00001249
1250 phase++;
Wayne Davison0492fdf2005-02-03 01:19:01 +00001251 only_existing = save_only_existing;
1252 opt_ignore_existing = save_opt_ignore_existing;
Wayne Davisoncd908ef2005-04-07 08:13:44 +00001253 make_backups = save_make_backups;
Wayne Davison0492fdf2005-02-03 01:19:01 +00001254
J.W. Schultzbc63ae32003-03-31 17:28:34 +00001255 if (verbose > 2)
1256 rprintf(FINFO,"generate_files phase=%d\n",phase);
1257
Wayne Davison7daccb82004-07-12 20:42:48 +00001258 write_int(f_out, -1);
Wayne Davison70352262005-03-29 01:04:20 +00001259 /* Reduce round-trip lag-time for a useless delay-updates phase. */
1260 if (protocol_version >= 29 && !delay_updates)
1261 write_int(f_out, -1);
Wayne Davison6dff5992004-01-25 22:32:09 +00001262
Wayne Davison0438f102005-03-17 00:41:18 +00001263 /* Read MSG_DONE for the redo phase (and any prior messages). */
Wayne Davisonee1d11c2005-03-05 00:21:42 +00001264 get_redo_num(itemizing, code);
Wayne Davison6dff5992004-01-25 22:32:09 +00001265
Wayne Davison0438f102005-03-17 00:41:18 +00001266 if (protocol_version >= 29) {
1267 phase++;
1268 if (verbose > 2)
1269 rprintf(FINFO, "generate_files phase=%d\n", phase);
Wayne Davison70352262005-03-29 01:04:20 +00001270 if (delay_updates)
1271 write_int(f_out, -1);
1272 /* Read MSG_DONE for delay-updates phase & prior messages. */
Wayne Davison0438f102005-03-17 00:41:18 +00001273 get_redo_num(itemizing, code);
1274 }
1275
Wayne Davison083acd42005-03-29 10:41:26 +00001276 do_progress = save_do_progress;
Wayne Davison59faec82005-02-26 18:03:49 +00001277 if (delete_after && !local_name && flist->count > 0)
Wayne Davisonee1d11c2005-03-05 00:21:42 +00001278 do_delete_pass(flist);
Wayne Davison59faec82005-02-26 18:03:49 +00001279
Wayne Davison0492fdf2005-02-03 01:19:01 +00001280 if ((need_retouch_dir_perms || need_retouch_dir_times)
1281 && !list_only && !local_name && !dry_run) {
Wayne Davison18a11cf2005-02-27 09:02:32 +00001282 int j = 0;
Wayne Davison3ea9bbd2005-01-24 06:19:58 +00001283 /* Now we need to fix any directory permissions that were
1284 * modified during the transfer and/or re-set any tweaked
1285 * modified-time values. */
1286 for (i = 0; i < flist->count; i++) {
1287 struct file_struct *file = flist->files[i];
1288 if (!file->basename || !S_ISDIR(file->mode))
1289 continue;
1290 if (!need_retouch_dir_times && file->mode & S_IWUSR)
1291 continue;
Wayne Davison62f95732005-03-15 19:19:38 +00001292 recv_generator(f_name(file), file, i, itemizing,
1293 maybe_PERMS_REPORT, code, -1);
Wayne Davison417099f2005-04-05 20:08:51 +00001294 if (allowed_lull && !(++j % lull_mod))
Wayne Davisonee1d11c2005-03-05 00:21:42 +00001295 maybe_send_keepalive();
Wayne Davisonc2523a02005-04-07 08:32:32 +00001296 else if (!(j % 200))
Wayne Davison417099f2005-04-05 20:08:51 +00001297 maybe_flush_socket();
Wayne Davison3ea9bbd2005-01-24 06:19:58 +00001298 }
Wayne Davison6dff5992004-01-25 22:32:09 +00001299 }
Wayne Davisonef20efc2005-03-09 18:54:12 +00001300 recv_generator(NULL, NULL, 0, 0, 0, code, -1);
Wayne Davison6dff5992004-01-25 22:32:09 +00001301
Wayne Davisonf75a53e2005-03-05 18:58:42 +00001302 if (max_delete > 0 && deletion_count > max_delete) {
1303 rprintf(FINFO,
1304 "Deletions stopped due to --max-delete limit (%d skipped)\n",
1305 deletion_count - max_delete);
1306 io_error |= IOERR_DEL_LIMIT;
1307 }
1308
Wayne Davison6dff5992004-01-25 22:32:09 +00001309 if (verbose > 2)
1310 rprintf(FINFO,"generate_files finished\n");
Andrew Tridgell2f03f951998-07-25 02:25:22 +00001311}