Commit | Line | Data |
---|---|---|
0f78b815 WD |
1 | /* |
2 | * Routines that are exclusive to the generator process. | |
3 | * | |
4 | * Copyright (C) 1996-2000 Andrew Tridgell | |
5 | * Copyright (C) 1996 Paul Mackerras | |
6 | * Copyright (C) 2002 Martin Pool <mbp@samba.org> | |
7 | * Copyright (C) 2003, 2004, 2005, 2006 Wayne Davison | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
e7c67065 WD |
19 | * You should have received a copy of the GNU General Public License along |
20 | * with this program; if not, write to the Free Software Foundation, Inc., | |
21 | * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. | |
0f78b815 | 22 | */ |
2f03f956 AT |
23 | |
24 | #include "rsync.h" | |
25 | ||
26 | extern int verbose; | |
27 | extern int dry_run; | |
beb51aa0 | 28 | extern int do_xfers; |
17bda2d1 | 29 | extern int stdout_format_has_i; |
ecc7623e | 30 | extern int logfile_format_has_i; |
6c3862fa WD |
31 | extern int am_root; |
32 | extern int am_server; | |
33 | extern int am_daemon; | |
083acd49 | 34 | extern int do_progress; |
2f03f956 | 35 | extern int relative_paths; |
ccb16b46 | 36 | extern int implied_dirs; |
716e73d4 | 37 | extern int keep_dirlinks; |
2f03f956 | 38 | extern int preserve_links; |
2f03f956 | 39 | extern int preserve_devices; |
b5c6a6ae | 40 | extern int preserve_specials; |
2f03f956 | 41 | extern int preserve_hard_links; |
6744b62d WD |
42 | extern int preserve_perms; |
43 | extern int preserve_uid; | |
44 | extern int preserve_gid; | |
3ea9bbd6 WD |
45 | extern int preserve_times; |
46 | extern int omit_dir_times; | |
e35d9f2d | 47 | extern int delete_mode; |
59faec8b | 48 | extern int delete_before; |
fa13f396 | 49 | extern int delete_during; |
59faec8b WD |
50 | extern int delete_after; |
51 | extern int module_id; | |
52 | extern int ignore_errors; | |
47c11975 | 53 | extern int remove_source_files; |
70352269 | 54 | extern int delay_updates; |
2f03f956 | 55 | extern int update_only; |
e90aab49 WD |
56 | extern int ignore_existing; |
57 | extern int ignore_non_existing; | |
cd6aa5b5 | 58 | extern int inplace; |
6cc11982 | 59 | extern int append_mode; |
cd6aa5b5 | 60 | extern int make_backups; |
2f03f956 AT |
61 | extern int csum_length; |
62 | extern int ignore_times; | |
f83f0548 | 63 | extern int size_only; |
7d1bfaf7 | 64 | extern OFF_T max_size; |
02b5cb23 | 65 | extern OFF_T min_size; |
59faec8b | 66 | extern int io_error; |
ac40b747 | 67 | extern int allowed_lull; |
ee1d11c4 | 68 | extern int sock_f_out; |
9ac2395b | 69 | extern int ignore_timeout; |
d04e9c51 | 70 | extern int protocol_version; |
8e85be0a | 71 | extern int fuzzy_basis; |
2f03f956 | 72 | extern int always_checksum; |
cfe39780 | 73 | extern int checksum_len; |
a7260c40 | 74 | extern char *partial_dir; |
b7e8628c | 75 | extern char *basis_dir[]; |
2be2fb3e | 76 | extern int compare_dest; |
967866d4 | 77 | extern int copy_dest; |
59c95e42 | 78 | extern int link_dest; |
5774786f | 79 | extern int whole_file; |
5774786f | 80 | extern int list_only; |
1ef5bf3c | 81 | extern int new_root_dir; |
b9f592fb | 82 | extern int read_batch; |
5774786f | 83 | extern int safe_symlinks; |
a255c592 | 84 | extern long block_size; /* "long" because popt can't set an int32. */ |
59faec8b WD |
85 | extern int max_delete; |
86 | extern int force_delete; | |
0e82af2d | 87 | extern int one_file_system; |
6c3862fa | 88 | extern struct stats stats; |
0e82af2d | 89 | extern dev_t filesystem_dev; |
59faec8b WD |
90 | extern char *backup_dir; |
91 | extern char *backup_suffix; | |
92 | extern int backup_suffix_len; | |
ee1d11c4 | 93 | extern struct file_list *the_file_list; |
7842418b | 94 | extern struct filter_list_struct server_filter_list; |
97f9dcae | 95 | |
59faec8b WD |
96 | static int deletion_count = 0; /* used to implement --max-delete */ |
97 | ||
c6fadc0e WD |
98 | /* For calling delete_item() */ |
99 | #define DEL_RECURSE (1<<1) /* recurse */ | |
d71dad3b | 100 | |
116a4769 WD |
101 | enum nonregtype { |
102 | TYPE_DIR, TYPE_SPECIAL, TYPE_DEVICE, TYPE_SYMLINK | |
103 | }; | |
59faec8b | 104 | |
c6fadc0e WD |
105 | enum delret { |
106 | DR_SUCCESS = 0, DR_FAILURE, DR_AT_LIMIT, DR_PINNED, DR_NOT_EMPTY | |
107 | }; | |
108 | ||
109 | /* Forward declaration for delete_item(). */ | |
110 | static enum delret delete_dir_contents(char *fname, int flags); | |
111 | ||
112 | ||
59faec8b WD |
113 | static int is_backup_file(char *fn) |
114 | { | |
115 | int k = strlen(fn) - backup_suffix_len; | |
116 | return k > 0 && strcmp(fn+k, backup_suffix) == 0; | |
117 | } | |
118 | ||
c6fadc0e WD |
119 | /* Delete a file or directory. If DEL_RECURSE is set in the flags, this will |
120 | * delete recursively. | |
0e5665d3 WD |
121 | * |
122 | * Note that fname must point to a MAXPATHLEN buffer if the mode indicates it's | |
123 | * a directory! (The buffer is used for recursion, but returned unchanged.) | |
124 | */ | |
c6fadc0e | 125 | static enum delret delete_item(char *fname, int mode, char *replace, int flags) |
59faec8b | 126 | { |
c6fadc0e WD |
127 | enum delret ret; |
128 | char *what; | |
129 | int ok; | |
59faec8b | 130 | |
c6fadc0e WD |
131 | if (verbose > 2) { |
132 | rprintf(FINFO, "delete_item(%s) mode=%o flags=%d\n", | |
133 | fname, mode, flags); | |
59faec8b WD |
134 | } |
135 | ||
c6fadc0e WD |
136 | if (S_ISDIR(mode) && flags & DEL_RECURSE) { |
137 | ret = delete_dir_contents(fname, flags); | |
138 | if (ret == DR_PINNED || ret == DR_NOT_EMPTY | |
139 | || ret == DR_AT_LIMIT) | |
140 | goto check_ret; | |
141 | /* OK: try to delete the directory. */ | |
142 | } | |
143 | ||
144 | if (!replace && max_delete >= 0 && ++deletion_count > max_delete) | |
145 | return DR_AT_LIMIT; | |
146 | ||
147 | if (S_ISDIR(mode)) { | |
148 | what = "rmdir"; | |
59faec8b | 149 | ok = do_rmdir(fname) == 0; |
c6fadc0e WD |
150 | } else if (make_backups && (backup_dir || !is_backup_file(fname))) { |
151 | what = "make_backup"; | |
152 | ok = make_backup(fname); | |
153 | } else { | |
154 | what = "unlink"; | |
155 | ok = robust_unlink(fname) == 0; | |
156 | } | |
157 | ||
59faec8b | 158 | if (ok) { |
c6fadc0e | 159 | if (!replace) |
59faec8b | 160 | log_delete(fname, mode); |
c6fadc0e WD |
161 | ret = DR_SUCCESS; |
162 | } else { | |
163 | if (S_ISDIR(mode) && errno == ENOTEMPTY) { | |
164 | rprintf(FINFO, "non-empty directory, %s, not deleted\n", | |
165 | fname); | |
166 | ret = DR_NOT_EMPTY; | |
167 | } else if (errno != ENOENT) { | |
168 | rsyserr(FERROR, errno, "delete_file: %s(%s) failed", | |
169 | what, full_fname(fname)); | |
170 | ret = DR_FAILURE; | |
171 | } else { | |
172 | deletion_count--; | |
173 | ret = DR_SUCCESS; | |
174 | } | |
f75a53e7 | 175 | } |
c6fadc0e WD |
176 | |
177 | check_ret: | |
178 | if (replace && ret != DR_SUCCESS) { | |
179 | rprintf(FERROR, "could not make way for new %s: %s\n", | |
180 | replace, fname); | |
59faec8b | 181 | } |
c6fadc0e WD |
182 | return ret; |
183 | } | |
184 | ||
f5761a34 | 185 | /* The directory is to be deleted, so delete all its contents. Note |
c6fadc0e WD |
186 | * that fname must point to a MAXPATHLEN buffer! (The buffer is used |
187 | * for recursion, but returned unchanged.) | |
188 | */ | |
189 | static enum delret delete_dir_contents(char *fname, int flags) | |
190 | { | |
191 | struct file_list *dirlist; | |
192 | enum delret ret, result; | |
193 | unsigned remainder; | |
194 | void *save_filters; | |
195 | int j, dlen; | |
196 | char *p; | |
59faec8b | 197 | |
f5761a34 WD |
198 | if (verbose > 3) { |
199 | rprintf(FINFO, "delete_dir_contents(%s) flags=%d\n", | |
200 | fname, flags); | |
201 | } | |
202 | ||
0e5665d3 WD |
203 | dlen = strlen(fname); |
204 | save_filters = push_local_filters(fname, dlen); | |
205 | ||
206 | dirlist = get_dirlist(fname, dlen, 0); | |
207 | ||
208 | p = fname + dlen; | |
209 | if (dlen != 1 || *fname != '/') | |
210 | *p++ = '/'; | |
211 | remainder = MAXPATHLEN - (p - fname); | |
59faec8b | 212 | |
f5761a34 WD |
213 | /* We do our own recursion, so make delete_item() non-recursive. */ |
214 | flags &= ~DEL_RECURSE; | |
c6fadc0e WD |
215 | ret = DR_SUCCESS; |
216 | ||
59faec8b WD |
217 | for (j = dirlist->count; j--; ) { |
218 | struct file_struct *fp = dirlist->files[j]; | |
219 | ||
c6fadc0e WD |
220 | if (fp->flags & FLAG_MOUNT_POINT) { |
221 | if (verbose > 1) { | |
222 | rprintf(FINFO, | |
223 | "mount point, %s, pins parent directory\n", | |
224 | f_name(fp, NULL)); | |
225 | } | |
226 | ret = DR_PINNED; | |
59faec8b | 227 | continue; |
c6fadc0e | 228 | } |
59faec8b | 229 | |
0e5665d3 | 230 | strlcpy(p, fp->basename, remainder); |
f5761a34 WD |
231 | if (S_ISDIR(fp->mode)) { |
232 | /* Save stack by recursing to ourself directly. */ | |
233 | result = delete_dir_contents(fname, flags); | |
234 | if (result == DR_PINNED) | |
235 | ret = result; | |
236 | else if (result != DR_SUCCESS && ret == DR_SUCCESS) | |
237 | ret = DR_NOT_EMPTY; | |
238 | } | |
c6fadc0e | 239 | result = delete_item(fname, fp->mode, NULL, flags); |
f5761a34 WD |
240 | if (result == DR_PINNED) |
241 | ret = result; | |
242 | else if (result != DR_SUCCESS && ret == DR_SUCCESS) | |
243 | ret = DR_NOT_EMPTY; | |
59faec8b | 244 | } |
59faec8b | 245 | |
0e5665d3 WD |
246 | fname[dlen] = '\0'; |
247 | ||
59faec8b | 248 | pop_local_filters(save_filters); |
c6fadc0e | 249 | flist_free(dirlist); |
59faec8b | 250 | |
c6fadc0e | 251 | return ret; |
59faec8b WD |
252 | } |
253 | ||
254 | ||
255 | /* This function is used to implement per-directory deletion, and is used by | |
256 | * all the --delete-WHEN options. Note that the fbuf pointer must point to a | |
257 | * MAXPATHLEN buffer with the name of the directory in it (the functions we | |
258 | * call will append names onto the end, but the old dir value will be restored | |
259 | * on exit). */ | |
260 | static void delete_in_dir(struct file_list *flist, char *fbuf, | |
0e82af2d | 261 | struct file_struct *file, STRUCT_STAT *stp) |
59faec8b WD |
262 | { |
263 | static int min_depth = MAXPATHLEN, cur_depth = -1; | |
264 | static void *filt_array[MAXPATHLEN/2+1]; | |
717b0430 | 265 | static int already_warned = 0; |
59faec8b WD |
266 | struct file_list *dirlist; |
267 | char delbuf[MAXPATHLEN]; | |
468d7668 | 268 | int dlen, i; |
59faec8b WD |
269 | |
270 | if (!flist) { | |
271 | while (cur_depth >= min_depth) | |
272 | pop_local_filters(filt_array[cur_depth--]); | |
273 | min_depth = MAXPATHLEN; | |
274 | cur_depth = -1; | |
275 | return; | |
276 | } | |
277 | ||
278 | if (verbose > 2) | |
45c49b52 | 279 | rprintf(FINFO, "delete_in_dir(%s)\n", fbuf); |
59faec8b WD |
280 | |
281 | if (allowed_lull) | |
ee1d11c4 | 282 | maybe_send_keepalive(); |
59faec8b WD |
283 | |
284 | if (file->dir.depth >= MAXPATHLEN/2+1) | |
285 | return; /* Impossible... */ | |
286 | ||
59faec8b | 287 | if (io_error && !(lp_ignore_errors(module_id) || ignore_errors)) { |
717b0430 | 288 | if (already_warned) |
f75a53e7 | 289 | return; |
59faec8b WD |
290 | rprintf(FINFO, |
291 | "IO error encountered -- skipping file deletion\n"); | |
717b0430 | 292 | already_warned = 1; |
59faec8b WD |
293 | return; |
294 | } | |
295 | ||
296 | while (cur_depth >= file->dir.depth && cur_depth >= min_depth) | |
297 | pop_local_filters(filt_array[cur_depth--]); | |
298 | cur_depth = file->dir.depth; | |
299 | if (min_depth > cur_depth) | |
300 | min_depth = cur_depth; | |
301 | dlen = strlen(fbuf); | |
302 | filt_array[cur_depth] = push_local_filters(fbuf, dlen); | |
303 | ||
0e82af2d WD |
304 | if (one_file_system) { |
305 | if (file->flags & FLAG_TOP_DIR) | |
306 | filesystem_dev = stp->st_dev; | |
307 | else if (filesystem_dev != stp->st_dev) | |
308 | return; | |
309 | } | |
59faec8b | 310 | |
59faec8b WD |
311 | dirlist = get_dirlist(fbuf, dlen, 0); |
312 | ||
313 | /* If an item in dirlist is not found in flist, delete it | |
314 | * from the filesystem. */ | |
315 | for (i = dirlist->count; i--; ) { | |
f3ab64d3 | 316 | struct file_struct *fp = dirlist->files[i]; |
c6fadc0e WD |
317 | if (!fp->basename) |
318 | continue; | |
319 | if (fp->flags & FLAG_MOUNT_POINT) { | |
320 | if (verbose > 1) | |
321 | rprintf(FINFO, "mount point %s not deleted\n", | |
322 | f_name(fp, NULL)); | |
bb0d8edf | 323 | continue; |
c6fadc0e | 324 | } |
468d7668 | 325 | if (flist_find(flist, fp) < 0) { |
6cbde57d | 326 | f_name(fp, delbuf); |
c6fadc0e | 327 | delete_item(delbuf, fp->mode, NULL, DEL_RECURSE); |
468d7668 | 328 | } |
59faec8b WD |
329 | } |
330 | ||
331 | flist_free(dirlist); | |
332 | } | |
333 | ||
334 | /* This deletes any files on the receiving side that are not present on the | |
335 | * sending side. This is used by --delete-before and --delete-after. */ | |
ee1d11c4 | 336 | static void do_delete_pass(struct file_list *flist) |
59faec8b WD |
337 | { |
338 | char fbuf[MAXPATHLEN]; | |
bb0d8edf | 339 | STRUCT_STAT st; |
59faec8b WD |
340 | int j; |
341 | ||
caff3322 WD |
342 | /* dry_run is incremented when the destination doesn't exist yet. */ |
343 | if (dry_run > 1 || list_only) | |
f75a53e7 WD |
344 | return; |
345 | ||
59faec8b WD |
346 | for (j = 0; j < flist->count; j++) { |
347 | struct file_struct *file = flist->files[j]; | |
348 | ||
349 | if (!(file->flags & FLAG_DEL_HERE)) | |
350 | continue; | |
351 | ||
6cbde57d | 352 | f_name(file, fbuf); |
59faec8b | 353 | if (verbose > 1 && file->flags & FLAG_TOP_DIR) |
45c49b52 | 354 | rprintf(FINFO, "deleting in %s\n", fbuf); |
59faec8b | 355 | |
bb0d8edf WD |
356 | if (link_stat(fbuf, &st, keep_dirlinks) < 0 |
357 | || !S_ISDIR(st.st_mode)) | |
358 | continue; | |
359 | ||
0e82af2d | 360 | delete_in_dir(flist, fbuf, file, &st); |
59faec8b | 361 | } |
0e82af2d | 362 | delete_in_dir(NULL, NULL, NULL, NULL); |
0e5665d3 | 363 | |
a06e2b7c WD |
364 | if (do_progress && !am_server) |
365 | rprintf(FINFO, " \r"); | |
59faec8b WD |
366 | } |
367 | ||
48224e4c | 368 | int unchanged_attrs(struct file_struct *file, STRUCT_STAT *st) |
2f03f956 | 369 | { |
b7e8628c WD |
370 | if (preserve_perms |
371 | && (st->st_mode & CHMOD_BITS) != (file->mode & CHMOD_BITS)) | |
84acca07 | 372 | return 0; |
bb24028f | 373 | |
7b6fa00f | 374 | if (am_root && preserve_uid && st->st_uid != file->uid) |
b7e8628c | 375 | return 0; |
bb24028f | 376 | |
7b6fa00f | 377 | if (preserve_gid && file->gid != GID_NONE && st->st_gid != file->gid) |
b7e8628c WD |
378 | return 0; |
379 | ||
380 | return 1; | |
381 | } | |
382 | ||
ee1d11c4 | 383 | void itemize(struct file_struct *file, int ndx, int statret, STRUCT_STAT *st, |
ef20efcb | 384 | int32 iflags, uchar fnamecmp_type, char *xname) |
06a1dbad | 385 | { |
48224e4c WD |
386 | if (statret >= 0) { /* A from-dest-dir statret can == 1! */ |
387 | int keep_time = !preserve_times ? 0 | |
388 | : S_ISDIR(file->mode) ? !omit_dir_times | |
389 | : !S_ISLNK(file->mode); | |
390 | ||
c557eb8c WD |
391 | if (S_ISREG(file->mode) && file->length != st->st_size) |
392 | iflags |= ITEM_REPORT_SIZE; | |
48224e4c | 393 | if ((iflags & (ITEM_TRANSFER|ITEM_LOCAL_CHANGE) && !keep_time |
116a4769 WD |
394 | && !(iflags & ITEM_MATCHED) |
395 | && (!(iflags & ITEM_XNAME_FOLLOWS) || *xname)) | |
396 | || (keep_time && cmp_time(file->modtime, st->st_mtime) != 0)) | |
48224e4c | 397 | iflags |= ITEM_REPORT_TIME; |
b9887818 | 398 | if ((file->mode & CHMOD_BITS) != (st->st_mode & CHMOD_BITS)) |
48224e4c | 399 | iflags |= ITEM_REPORT_PERMS; |
7b6fa00f | 400 | if (preserve_uid && am_root && file->uid != st->st_uid) |
48224e4c | 401 | iflags |= ITEM_REPORT_OWNER; |
7b6fa00f WD |
402 | if (preserve_gid && file->gid != GID_NONE |
403 | && st->st_gid != file->gid) | |
48224e4c | 404 | iflags |= ITEM_REPORT_GROUP; |
ef20efcb | 405 | } else |
ee1d11c4 | 406 | iflags |= ITEM_IS_NEW; |
c557eb8c | 407 | |
a1d23b53 | 408 | iflags &= 0xffff; |
ee1d11c4 | 409 | if ((iflags & SIGNIFICANT_ITEM_FLAGS || verbose > 1 |
17bda2d1 | 410 | || stdout_format_has_i > 1 || (xname && *xname)) && !read_batch) { |
6c3862fa WD |
411 | if (protocol_version >= 29) { |
412 | if (ndx >= 0) | |
ee1d11c4 WD |
413 | write_int(sock_f_out, ndx); |
414 | write_shortint(sock_f_out, iflags); | |
ef20efcb WD |
415 | if (iflags & ITEM_BASIS_TYPE_FOLLOWS) |
416 | write_byte(sock_f_out, fnamecmp_type); | |
417 | if (iflags & ITEM_XNAME_FOLLOWS) | |
418 | write_vstring(sock_f_out, xname, strlen(xname)); | |
1925c344 WD |
419 | } else if (ndx >= 0) { |
420 | enum logcode code = logfile_format_has_i ? FINFO : FCLIENT; | |
421 | log_item(code, file, &stats, iflags, xname); | |
422 | } | |
06a1dbad WD |
423 | } |
424 | } | |
425 | ||
426 | ||
b7e8628c | 427 | /* Perform our quick-check heuristic for determining if a file is unchanged. */ |
48224e4c | 428 | int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st) |
b7e8628c WD |
429 | { |
430 | if (st->st_size != file->length) | |
431 | return 0; | |
59c95e42 | 432 | |
2cda2560 | 433 | /* if always checksum is set then we use the checksum instead |
2f03f956 AT |
434 | of the file time to determine whether to sync */ |
435 | if (always_checksum && S_ISREG(st->st_mode)) { | |
436 | char sum[MD4_SUM_LENGTH]; | |
b7e8628c | 437 | file_checksum(fn, sum, st->st_size); |
cfe39780 | 438 | return memcmp(sum, file->u.sum, checksum_len) == 0; |
2f03f956 AT |
439 | } |
440 | ||
cc1e997d | 441 | if (size_only) |
84acca07 | 442 | return 1; |
2f03f956 | 443 | |
cc1e997d | 444 | if (ignore_times) |
84acca07 | 445 | return 0; |
cc1e997d | 446 | |
0f86c74e | 447 | return cmp_time(st->st_mtime, file->modtime) == 0; |
2f03f956 AT |
448 | } |
449 | ||
450 | ||
ec8290c8 | 451 | /* |
195bd906 | 452 | * set (initialize) the size entries in the per-file sum_struct |
ec8290c8 | 453 | * calculating dynamic block and checksum sizes. |
195bd906 | 454 | * |
ec8290c8 | 455 | * This is only called from generate_and_send_sums() but is a separate |
195bd906 S |
456 | * function to encapsulate the logic. |
457 | * | |
458 | * The block size is a rounded square root of file length. | |
459 | * | |
460 | * The checksum size is determined according to: | |
ed7e7955 | 461 | * blocksum_bits = BLOCKSUM_BIAS + 2*log2(file_len) - log2(block_len) |
195bd906 S |
462 | * provided by Donovan Baarda which gives a probability of rsync |
463 | * algorithm corrupting data and falling back using the whole md4 | |
464 | * checksums. | |
465 | * | |
466 | * This might be made one of several selectable heuristics. | |
467 | */ | |
1490812a | 468 | static void sum_sizes_sqroot(struct sum_struct *sum, int64 len) |
195bd906 | 469 | { |
a255c592 | 470 | int32 blength; |
da9d12f5 | 471 | int s2length; |
195bd906 | 472 | |
a255c592 | 473 | if (block_size) |
195bd906 | 474 | blength = block_size; |
a255c592 | 475 | else if (len <= BLOCK_SIZE * BLOCK_SIZE) |
195bd906 | 476 | blength = BLOCK_SIZE; |
a255c592 WD |
477 | else { |
478 | int32 c; | |
1490812a | 479 | int64 l; |
eae7165c WD |
480 | int cnt; |
481 | for (c = 1, l = len, cnt = 0; l >>= 2; c <<= 1, cnt++) {} | |
482 | if (cnt >= 31 || c >= MAX_BLOCK_SIZE) | |
483 | blength = MAX_BLOCK_SIZE; | |
484 | else { | |
485 | blength = 0; | |
486 | do { | |
487 | blength |= c; | |
1490812a | 488 | if (len < (int64)blength * blength) |
eae7165c WD |
489 | blength &= ~c; |
490 | c >>= 1; | |
491 | } while (c >= 8); /* round to multiple of 8 */ | |
492 | blength = MAX(blength, BLOCK_SIZE); | |
195bd906 | 493 | } |
195bd906 S |
494 | } |
495 | ||
d04e9c51 | 496 | if (protocol_version < 27) { |
195bd906 S |
497 | s2length = csum_length; |
498 | } else if (csum_length == SUM_LENGTH) { | |
499 | s2length = SUM_LENGTH; | |
500 | } else { | |
a255c592 | 501 | int32 c; |
1490812a | 502 | int64 l; |
da9d12f5 | 503 | int b = BLOCKSUM_BIAS; |
a255c592 | 504 | for (l = len; l >>= 1; b += 2) {} |
f4164b73 | 505 | for (c = blength; (c >>= 1) && b; b--) {} |
a255c592 WD |
506 | /* add a bit, subtract rollsum, round up. */ |
507 | s2length = (b + 1 - 32 + 7) / 8; /* --optimize in compiler-- */ | |
195bd906 S |
508 | s2length = MAX(s2length, csum_length); |
509 | s2length = MIN(s2length, SUM_LENGTH); | |
510 | } | |
511 | ||
512 | sum->flength = len; | |
513 | sum->blength = blength; | |
514 | sum->s2length = s2length; | |
a5b786d8 WD |
515 | sum->remainder = len % blength; |
516 | sum->count = len / blength + (sum->remainder != 0); | |
195bd906 S |
517 | |
518 | if (sum->count && verbose > 2) { | |
a255c592 WD |
519 | rprintf(FINFO, |
520 | "count=%.0f rem=%ld blength=%ld s2length=%d flength=%.0f\n", | |
521 | (double)sum->count, (long)sum->remainder, (long)sum->blength, | |
da9d12f5 | 522 | sum->s2length, (double)sum->flength); |
195bd906 S |
523 | } |
524 | } | |
80605142 | 525 | |
bceec82f | 526 | |
80605142 WD |
527 | /* |
528 | * Generate and send a stream of signatures/checksums that describe a buffer | |
e66dfd18 | 529 | * |
80605142 WD |
530 | * Generate approximately one checksum every block_len bytes. |
531 | */ | |
cd6aa5b5 | 532 | static void generate_and_send_sums(int fd, OFF_T len, int f_out, int f_copy) |
2f03f956 | 533 | { |
a1cbe76e | 534 | int32 i; |
6e45e1dd | 535 | struct map_struct *mapbuf; |
80605142 | 536 | struct sum_struct sum; |
2f03f956 AT |
537 | OFF_T offset = 0; |
538 | ||
423dba8e | 539 | sum_sizes_sqroot(&sum, len); |
6cc11982 WD |
540 | write_sum_head(f_out, &sum); |
541 | ||
542 | if (append_mode > 0 && f_copy < 0) | |
543 | return; | |
e66dfd18 | 544 | |
6e45e1dd | 545 | if (len > 0) |
96d910c7 | 546 | mapbuf = map_file(fd, len, MAX_MAP_SIZE, sum.blength); |
6e45e1dd WD |
547 | else |
548 | mapbuf = NULL; | |
549 | ||
80605142 | 550 | for (i = 0; i < sum.count; i++) { |
a255c592 | 551 | int32 n1 = (int32)MIN(len, (OFF_T)sum.blength); |
6e45e1dd | 552 | char *map = map_ptr(mapbuf, offset, n1); |
80605142 | 553 | char sum2[SUM_LENGTH]; |
6cc11982 WD |
554 | uint32 sum1; |
555 | ||
556 | len -= n1; | |
557 | offset += n1; | |
2f03f956 | 558 | |
6cc11982 | 559 | if (f_copy >= 0) { |
cd6aa5b5 | 560 | full_write(f_copy, map, n1); |
6cc11982 WD |
561 | if (append_mode > 0) |
562 | continue; | |
563 | } | |
cd6aa5b5 | 564 | |
6cc11982 | 565 | sum1 = get_checksum1(map, n1); |
80605142 | 566 | get_checksum2(map, n1, sum2); |
2f03f956 | 567 | |
80605142 | 568 | if (verbose > 3) { |
e66dfd18 | 569 | rprintf(FINFO, |
a255c592 | 570 | "chunk[%.0f] offset=%.0f len=%ld sum1=%08lx\n", |
6cc11982 | 571 | (double)i, (double)offset - n1, (long)n1, |
0e36d9da | 572 | (unsigned long)sum1); |
80605142 WD |
573 | } |
574 | write_int(f_out, sum1); | |
fc0257c9 | 575 | write_buf(f_out, sum2, sum.s2length); |
2f03f956 | 576 | } |
6e45e1dd WD |
577 | |
578 | if (mapbuf) | |
579 | unmap_file(mapbuf); | |
2f03f956 AT |
580 | } |
581 | ||
06a1dbad | 582 | |
8e85be0a WD |
583 | /* Try to find a filename in the same dir as "fname" with a similar name. */ |
584 | static int find_fuzzy(struct file_struct *file, struct file_list *dirlist) | |
585 | { | |
586 | int fname_len, fname_suf_len; | |
587 | const char *fname_suf, *fname = file->basename; | |
f328e0f3 | 588 | uint32 lowest_dist = 25 << 16; /* ignore a distance greater than 25 */ |
8e85be0a WD |
589 | int j, lowest_j = -1; |
590 | ||
591 | fname_len = strlen(fname); | |
592 | fname_suf = find_filename_suffix(fname, fname_len, &fname_suf_len); | |
593 | ||
594 | for (j = 0; j < dirlist->count; j++) { | |
595 | struct file_struct *fp = dirlist->files[j]; | |
596 | const char *suf, *name; | |
597 | int len, suf_len; | |
598 | uint32 dist; | |
599 | ||
600 | if (!S_ISREG(fp->mode) || !fp->length | |
601 | || fp->flags & FLAG_NO_FUZZY) | |
602 | continue; | |
603 | ||
604 | name = fp->basename; | |
605 | ||
606 | if (fp->length == file->length | |
0f86c74e | 607 | && cmp_time(fp->modtime, file->modtime) == 0) { |
8e85be0a WD |
608 | if (verbose > 4) { |
609 | rprintf(FINFO, | |
610 | "fuzzy size/modtime match for %s\n", | |
611 | name); | |
612 | } | |
613 | return j; | |
614 | } | |
615 | ||
616 | len = strlen(name); | |
617 | suf = find_filename_suffix(name, len, &suf_len); | |
618 | ||
619 | dist = fuzzy_distance(name, len, fname, fname_len); | |
620 | /* Add some extra weight to how well the suffixes match. */ | |
621 | dist += fuzzy_distance(suf, suf_len, fname_suf, fname_suf_len) | |
622 | * 10; | |
623 | if (verbose > 4) { | |
624 | rprintf(FINFO, "fuzzy distance for %s = %d.%05d\n", | |
625 | name, (int)(dist>>16), (int)(dist&0xFFFF)); | |
626 | } | |
627 | if (dist <= lowest_dist) { | |
628 | lowest_dist = dist; | |
629 | lowest_j = j; | |
630 | } | |
631 | } | |
632 | ||
633 | return lowest_j; | |
634 | } | |
635 | ||
ee1d11c4 WD |
636 | void check_for_finished_hlinks(int itemizing, enum logcode code) |
637 | { | |
638 | struct file_struct *file; | |
639 | int ndx; | |
640 | ||
641 | while ((ndx = get_hlink_num()) != -1) { | |
642 | if (ndx < 0 || ndx >= the_file_list->count) | |
643 | continue; | |
644 | ||
645 | file = the_file_list->files[ndx]; | |
646 | if (!file->link_u.links) | |
647 | continue; | |
648 | ||
649 | hard_link_cluster(file, ndx, itemizing, code); | |
650 | } | |
651 | } | |
2f03f956 | 652 | |
48224e4c WD |
653 | /* This is only called for regular files. We return -2 if we've finished |
654 | * handling the file, -1 if no dest-linking occurred, or a non-negative | |
655 | * value if we found an alternate basis file. */ | |
656 | static int try_dests_reg(struct file_struct *file, char *fname, int ndx, | |
657 | char *cmpbuf, STRUCT_STAT *stp, int itemizing, | |
e912bd4d | 658 | int maybe_ATTRS_REPORT, enum logcode code) |
48224e4c WD |
659 | { |
660 | int best_match = -1; | |
661 | int match_level = 0; | |
662 | int j = 0; | |
663 | ||
664 | do { | |
665 | pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname); | |
666 | if (link_stat(cmpbuf, stp, 0) < 0 || !S_ISREG(stp->st_mode)) | |
667 | continue; | |
668 | switch (match_level) { | |
669 | case 0: | |
670 | best_match = j; | |
671 | match_level = 1; | |
672 | /* FALL THROUGH */ | |
673 | case 1: | |
674 | if (!unchanged_file(cmpbuf, file, stp)) | |
675 | continue; | |
676 | best_match = j; | |
677 | match_level = 2; | |
678 | /* FALL THROUGH */ | |
679 | case 2: | |
680 | if (!unchanged_attrs(file, stp)) | |
681 | continue; | |
6bfc7b4d | 682 | if (always_checksum && preserve_times |
0f86c74e | 683 | && cmp_time(stp->st_mtime, file->modtime)) |
48224e4c WD |
684 | continue; |
685 | best_match = j; | |
686 | match_level = 3; | |
687 | break; | |
688 | } | |
689 | break; | |
690 | } while (basis_dir[++j] != NULL); | |
691 | ||
692 | if (!match_level) | |
693 | return -1; | |
694 | ||
695 | if (j != best_match) { | |
696 | j = best_match; | |
697 | pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname); | |
698 | if (link_stat(cmpbuf, stp, 0) < 0) | |
f964ac5e | 699 | return -1; |
48224e4c WD |
700 | } |
701 | ||
48224e4c | 702 | if (match_level == 3 && !copy_dest) { |
40410a38 | 703 | #ifdef SUPPORT_HARD_LINKS |
48224e4c | 704 | if (link_dest) { |
116a4769 | 705 | int i = itemizing && (verbose > 1 || stdout_format_has_i > 1); |
48224e4c | 706 | if (hard_link_one(file, ndx, fname, 0, stp, |
116a4769 | 707 | cmpbuf, 1, i, code) < 0) |
48224e4c | 708 | goto try_a_copy; |
d9163a4c WD |
709 | if (preserve_hard_links && file->link_u.links) { |
710 | if (dry_run) | |
711 | file->link_u.links->link_dest_used = j + 1; | |
3447d610 | 712 | hard_link_cluster(file, ndx, itemizing, code); |
d9163a4c | 713 | } |
40410a38 WD |
714 | } else |
715 | #endif | |
716 | if (itemizing) | |
48224e4c | 717 | itemize(file, ndx, 0, stp, 0, 0, NULL); |
e912bd4d | 718 | if (verbose > 1 && maybe_ATTRS_REPORT) { |
1925c344 | 719 | rprintf(FCLIENT, "%s is uptodate\n", fname); |
48224e4c WD |
720 | } |
721 | return -2; | |
722 | } | |
48224e4c WD |
723 | |
724 | if (match_level >= 2) { | |
725 | try_a_copy: /* Copy the file locally. */ | |
726 | if (copy_file(cmpbuf, fname, file->mode) < 0) { | |
727 | if (verbose) { | |
3447d610 | 728 | rsyserr(FINFO, errno, "copy_file %s => %s", |
45c49b52 | 729 | full_fname(cmpbuf), fname); |
48224e4c WD |
730 | } |
731 | return -1; | |
732 | } | |
3447d610 WD |
733 | if (itemizing) |
734 | itemize(file, ndx, 0, stp, ITEM_LOCAL_CHANGE, 0, NULL); | |
e912bd4d WD |
735 | set_file_attrs(fname, file, NULL, 0); |
736 | if (maybe_ATTRS_REPORT | |
48224e4c WD |
737 | && ((!itemizing && verbose && match_level == 2) |
738 | || (verbose > 1 && match_level == 3))) { | |
1925c344 | 739 | code = match_level == 3 ? FCLIENT : FINFO; |
45c49b52 | 740 | rprintf(code, "%s%s\n", fname, |
48224e4c WD |
741 | match_level == 3 ? " is uptodate" : ""); |
742 | } | |
3447d610 WD |
743 | if (preserve_hard_links && file->link_u.links) |
744 | hard_link_cluster(file, ndx, itemizing, code); | |
48224e4c WD |
745 | return -2; |
746 | } | |
747 | ||
748 | return FNAMECMP_BASIS_DIR_LOW + j; | |
749 | } | |
750 | ||
751 | /* This is only called for non-regular files. We return -2 if we've finished | |
116a4769 WD |
752 | * handling the file, or -1 if no dest-linking occurred, or a non-negative |
753 | * value if we found an alternate basis file. */ | |
48224e4c | 754 | static int try_dests_non(struct file_struct *file, char *fname, int ndx, |
116a4769 WD |
755 | char *cmpbuf, STRUCT_STAT *stp, int itemizing, |
756 | int maybe_ATTRS_REPORT, enum logcode code) | |
48224e4c | 757 | { |
116a4769 WD |
758 | char lnk[MAXPATHLEN]; |
759 | int best_match = -1; | |
760 | int match_level = 0; | |
761 | enum nonregtype type; | |
762 | int len, j = 0; | |
763 | ||
764 | #ifndef SUPPORT_LINKS | |
765 | if (S_ISLNK(file->mode)) | |
766 | return -1; | |
767 | #endif | |
768 | if (S_ISDIR(file->mode)) { | |
769 | type = TYPE_DIR; | |
770 | } else if (IS_SPECIAL(file->mode)) | |
771 | type = TYPE_SPECIAL; | |
772 | else if (IS_DEVICE(file->mode)) | |
773 | type = TYPE_DEVICE; | |
774 | #ifdef SUPPORT_LINKS | |
775 | else if (S_ISLNK(file->mode)) | |
776 | type = TYPE_SYMLINK; | |
777 | #endif | |
778 | else { | |
779 | rprintf(FERROR, | |
780 | "internal: try_dests_non() called with invalid mode (%o)\n", | |
781 | (int)file->mode); | |
782 | exit_cleanup(RERR_UNSUPPORTED); | |
783 | } | |
48224e4c WD |
784 | |
785 | do { | |
116a4769 WD |
786 | pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname); |
787 | if (link_stat(cmpbuf, stp, 0) < 0) | |
48224e4c | 788 | continue; |
116a4769 WD |
789 | switch (type) { |
790 | case TYPE_DIR: | |
791 | if (!S_ISDIR(stp->st_mode)) | |
792 | continue; | |
793 | break; | |
794 | case TYPE_SPECIAL: | |
795 | if (!IS_SPECIAL(stp->st_mode)) | |
796 | continue; | |
797 | break; | |
798 | case TYPE_DEVICE: | |
799 | if (!IS_DEVICE(stp->st_mode)) | |
800 | continue; | |
801 | break; | |
bb0d8edf | 802 | #ifdef SUPPORT_LINKS |
116a4769 WD |
803 | case TYPE_SYMLINK: |
804 | if (!S_ISLNK(stp->st_mode)) | |
48224e4c | 805 | continue; |
116a4769 | 806 | break; |
bb0d8edf | 807 | #endif |
116a4769 WD |
808 | } |
809 | if (match_level < 1) { | |
810 | match_level = 1; | |
811 | best_match = j; | |
812 | } | |
813 | switch (type) { | |
814 | case TYPE_DIR: | |
815 | break; | |
816 | case TYPE_SPECIAL: | |
817 | case TYPE_DEVICE: | |
818 | if (stp->st_rdev != file->u.rdev) | |
48224e4c | 819 | continue; |
116a4769 WD |
820 | break; |
821 | #ifdef SUPPORT_LINKS | |
822 | case TYPE_SYMLINK: | |
823 | if ((len = readlink(cmpbuf, lnk, MAXPATHLEN-1)) <= 0) | |
ba212fe0 | 824 | continue; |
116a4769 WD |
825 | lnk[len] = '\0'; |
826 | if (strcmp(lnk, file->u.link) != 0) | |
ba212fe0 | 827 | continue; |
116a4769 WD |
828 | break; |
829 | #endif | |
830 | } | |
831 | if (match_level < 2) { | |
832 | match_level = 2; | |
833 | best_match = j; | |
834 | } | |
835 | if (unchanged_attrs(file, stp)) { | |
836 | match_level = 3; | |
837 | best_match = j; | |
838 | break; | |
5f93b4d3 | 839 | } |
116a4769 WD |
840 | } while (basis_dir[++j] != NULL); |
841 | ||
842 | if (!match_level) | |
843 | return -1; | |
844 | ||
845 | if (j != best_match) { | |
846 | j = best_match; | |
847 | pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname); | |
848 | if (link_stat(cmpbuf, stp, 0) < 0) | |
849 | return -1; | |
850 | } | |
851 | ||
852 | if (match_level == 3) { | |
40410a38 | 853 | #ifdef SUPPORT_HARD_LINKS |
ba212fe0 WD |
854 | if (link_dest |
855 | #ifndef CAN_HARDLINK_SYMLINK | |
856 | && !S_ISLNK(file->mode) | |
857 | #endif | |
858 | #ifndef CAN_HARDLINK_SPECIAL | |
43476426 | 859 | && !IS_SPECIAL(file->mode) && !IS_DEVICE(file->mode) |
ba212fe0 | 860 | #endif |
116a4769 WD |
861 | && !S_ISDIR(file->mode)) { |
862 | if (do_link(cmpbuf, fname) < 0) { | |
1c6e9dfa WD |
863 | rsyserr(FERROR, errno, |
864 | "failed to hard-link %s with %s", | |
116a4769 WD |
865 | cmpbuf, fname); |
866 | return j; | |
48224e4c | 867 | } |
3447d610 WD |
868 | if (preserve_hard_links && file->link_u.links) |
869 | hard_link_cluster(file, ndx, itemizing, code); | |
116a4769 | 870 | } else |
40410a38 | 871 | #endif |
116a4769 WD |
872 | match_level = 2; |
873 | if (itemizing && stdout_format_has_i | |
874 | && (verbose > 1 || stdout_format_has_i > 1)) { | |
875 | int chg = compare_dest && type != TYPE_DIR ? 0 | |
876 | : ITEM_LOCAL_CHANGE | |
877 | + (match_level == 3 ? ITEM_XNAME_FOLLOWS : 0); | |
878 | char *lp = match_level == 3 ? "" : NULL; | |
879 | itemize(file, ndx, 0, stp, chg + ITEM_MATCHED, 0, lp); | |
48224e4c | 880 | } |
e912bd4d | 881 | if (verbose > 1 && maybe_ATTRS_REPORT) { |
116a4769 WD |
882 | rprintf(FCLIENT, "%s%s is uptodate\n", |
883 | fname, type == TYPE_DIR ? "/" : ""); | |
48224e4c WD |
884 | } |
885 | return -2; | |
116a4769 | 886 | } |
48224e4c | 887 | |
116a4769 | 888 | return j; |
48224e4c WD |
889 | } |
890 | ||
ed7e7955 WD |
891 | static int phase = 0; |
892 | ||
ee1d11c4 | 893 | /* Acts on the_file_list->file's ndx'th item, whose name is fname. If a dir, |
0492fdfb WD |
894 | * make sure it exists, and has the right permissions/timestamp info. For |
895 | * all other non-regular files (symlinks, etc.) we create them here. For | |
896 | * regular files that have changed, we try to find a basis file and then | |
897 | * start sending checksums. | |
ef1aa910 | 898 | * |
0e5665d3 WD |
899 | * When fname is non-null, it must point to a MAXPATHLEN buffer! |
900 | * | |
0492fdfb WD |
901 | * Note that f_out is set to -1 when doing final directory-permission and |
902 | * modification-time repair. */ | |
ee1d11c4 | 903 | static void recv_generator(char *fname, struct file_struct *file, int ndx, |
e912bd4d | 904 | int itemizing, int maybe_ATTRS_REPORT, |
ef20efcb | 905 | enum logcode code, int f_out) |
2cda2560 | 906 | { |
8174bc35 | 907 | static int missing_below = -1, excluded_below = -1; |
a9cc1283 | 908 | static char *parent_dirname = ""; |
8e85be0a | 909 | static struct file_list *fuzzy_dirlist = NULL; |
8470a515 | 910 | static int need_fuzzy_dirlist = 0; |
8e85be0a | 911 | struct file_struct *fuzzy_file = NULL; |
41cfde6b | 912 | int fd = -1, f_copy = -1; |
1f1d368a | 913 | STRUCT_STAT st, real_st, partial_st; |
41cfde6b | 914 | struct file_struct *back_file = NULL; |
1f1d368a | 915 | int statret, real_ret, stat_errno; |
41cfde6b | 916 | char *fnamecmp, *partialptr, *backupptr = NULL; |
375a4556 | 917 | char fnamecmpbuf[MAXPATHLEN]; |
41cfde6b | 918 | uchar fnamecmp_type; |
c6fadc0e | 919 | int del_opts = delete_mode || force_delete ? DEL_RECURSE : 0; |
f7632fc6 | 920 | |
dfd5ba6a WD |
921 | if (list_only) |
922 | return; | |
2f03f956 | 923 | |
8e85be0a WD |
924 | if (!fname) { |
925 | if (fuzzy_dirlist) { | |
8470a515 | 926 | flist_free(fuzzy_dirlist); |
8e85be0a | 927 | fuzzy_dirlist = NULL; |
8e85be0a WD |
928 | } |
929 | if (missing_below >= 0) { | |
caff3322 WD |
930 | if (dry_run) |
931 | dry_run--; | |
8e85be0a WD |
932 | missing_below = -1; |
933 | } | |
8c9e06d7 | 934 | parent_dirname = ""; |
8e85be0a WD |
935 | return; |
936 | } | |
937 | ||
45c49b52 WD |
938 | if (verbose > 2) |
939 | rprintf(FINFO, "recv_generator(%s,%d)\n", fname, ndx); | |
2f03f956 | 940 | |
8174bc35 WD |
941 | if (server_filter_list.head) { |
942 | if (excluded_below >= 0) { | |
943 | if (file->dir.depth > excluded_below) | |
944 | goto skipping; | |
945 | excluded_below = -1; | |
946 | } | |
947 | if (check_filter(&server_filter_list, fname, | |
948 | S_ISDIR(file->mode)) < 0) { | |
949 | if (S_ISDIR(file->mode)) | |
950 | excluded_below = file->dir.depth; | |
b2e7c913 | 951 | skipping: |
8174bc35 WD |
952 | if (verbose) { |
953 | rprintf(FINFO, | |
954 | "skipping server-excluded file \"%s\"\n", | |
45c49b52 | 955 | fname); |
8174bc35 WD |
956 | } |
957 | return; | |
3e35c34b | 958 | } |
3e35c34b | 959 | } |
97f9dcae | 960 | |
caff3322 WD |
961 | if (missing_below >= 0) { |
962 | if (file->dir.depth <= missing_below) { | |
963 | if (dry_run) | |
964 | dry_run--; | |
965 | missing_below = -1; | |
966 | } else if (!dry_run) | |
967 | return; | |
df337831 | 968 | } |
73f7af0e WD |
969 | if (dry_run > 1) { |
970 | statret = -1; | |
971 | stat_errno = ENOENT; | |
972 | } else { | |
8c9e06d7 WD |
973 | char *dn = file->dirname ? file->dirname : "."; |
974 | if (parent_dirname != dn && strcmp(parent_dirname, dn) != 0) { | |
aa37022e | 975 | if (relative_paths && !implied_dirs |
1a7f3d99 | 976 | && do_stat(dn, &st) < 0 |
904e5af1 | 977 | && create_directory_path(fname) < 0) { |
8c9e06d7 WD |
978 | rsyserr(FERROR, errno, |
979 | "recv_generator: mkdir %s failed", | |
980 | full_fname(dn)); | |
8e85be0a | 981 | } |
8c9e06d7 | 982 | if (fuzzy_dirlist) { |
8470a515 | 983 | flist_free(fuzzy_dirlist); |
8c9e06d7 WD |
984 | fuzzy_dirlist = NULL; |
985 | } | |
986 | if (fuzzy_basis) | |
8470a515 | 987 | need_fuzzy_dirlist = 1; |
8e85be0a | 988 | } |
8c9e06d7 | 989 | parent_dirname = dn; |
8e85be0a | 990 | |
6f2a222c | 991 | if (need_fuzzy_dirlist && S_ISREG(file->mode)) { |
865c3b5f | 992 | fuzzy_dirlist = get_dirlist(dn, -1, 1); |
6f2a222c WD |
993 | need_fuzzy_dirlist = 0; |
994 | } | |
865c3b5f | 995 | |
73f7af0e WD |
996 | statret = link_stat(fname, &st, |
997 | keep_dirlinks && S_ISDIR(file->mode)); | |
998 | stat_errno = errno; | |
999 | } | |
63787382 | 1000 | |
e90aab49 | 1001 | if (ignore_non_existing && statret == -1 && stat_errno == ENOENT) { |
ecc81fce | 1002 | if (verbose > 1) { |
15164c0a WD |
1003 | rprintf(FINFO, "not creating new %s \"%s\"\n", |
1004 | S_ISDIR(file->mode) ? "directory" : "file", | |
45c49b52 | 1005 | fname); |
ecc81fce | 1006 | } |
1347d512 AT |
1007 | return; |
1008 | } | |
1009 | ||
a9d6e6fc WD |
1010 | /* If we're not preserving permissions, change the file-list's |
1011 | * mode based on the local permissions and some heuristics. */ | |
1012 | if (!preserve_perms) { | |
1013 | int exists = statret == 0 | |
1014 | && S_ISDIR(st.st_mode) == S_ISDIR(file->mode); | |
1015 | file->mode = dest_mode(file->mode, st.st_mode, exists); | |
4df9f368 AT |
1016 | } |
1017 | ||
2f03f956 | 1018 | if (S_ISDIR(file->mode)) { |
2cda2560 WD |
1019 | /* The file to be received is a directory, so we need |
1020 | * to prepare appropriately. If there is already a | |
1021 | * file of that name and it is *not* a directory, then | |
1022 | * we need to delete it. If it doesn't exist, then | |
027428eb | 1023 | * (perhaps recursively) create it. */ |
2f03f956 | 1024 | if (statret == 0 && !S_ISDIR(st.st_mode)) { |
c6fadc0e | 1025 | if (delete_item(fname, st.st_mode, "directory", del_opts) != 0) |
1f1d368a | 1026 | return; |
2f03f956 AT |
1027 | statret = -1; |
1028 | } | |
df337831 WD |
1029 | if (dry_run && statret != 0 && missing_below < 0) { |
1030 | missing_below = file->dir.depth; | |
1031 | dry_run++; | |
1032 | } | |
b467495c WD |
1033 | real_ret = statret; |
1034 | real_st = st; | |
116a4769 WD |
1035 | if (new_root_dir) { |
1036 | if (*fname == '.' && fname[1] == '\0') | |
b467495c | 1037 | statret = -1; |
116a4769 WD |
1038 | new_root_dir = 0; |
1039 | } | |
b467495c | 1040 | if (statret != 0 && basis_dir[0] != NULL) { |
116a4769 WD |
1041 | int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &st, |
1042 | itemizing, maybe_ATTRS_REPORT, code); | |
1043 | if (j == -2) { | |
1044 | itemizing = 0; | |
1045 | code = FNONE; | |
1046 | } else if (j >= 0) | |
b467495c | 1047 | statret = 1; |
116a4769 | 1048 | } |
ee1d11c4 | 1049 | if (itemizing && f_out != -1) { |
b467495c WD |
1050 | itemize(file, ndx, statret, &st, |
1051 | statret ? ITEM_LOCAL_CHANGE : 0, 0, NULL); | |
ee1d11c4 | 1052 | } |
b467495c | 1053 | if (real_ret != 0 && do_mkdir(fname,file->mode) < 0 && errno != EEXIST) { |
027428eb | 1054 | if (!relative_paths || errno != ENOENT |
904e5af1 | 1055 | || create_directory_path(fname) < 0 |
09290693 | 1056 | || (do_mkdir(fname, file->mode) < 0 && errno != EEXIST)) { |
d62bcc17 WD |
1057 | rsyserr(FERROR, errno, |
1058 | "recv_generator: mkdir %s failed", | |
1059 | full_fname(fname)); | |
caff3322 WD |
1060 | file->flags |= FLAG_MISSING; |
1061 | if (ndx+1 < the_file_list->count | |
1062 | && the_file_list->files[ndx+1]->dir.depth > file->dir.depth) { | |
1063 | rprintf(FERROR, | |
1064 | "*** Skipping everything below this failed directory ***\n"); | |
1065 | missing_below = file->dir.depth; | |
1066 | } | |
1067 | return; | |
2f03f956 AT |
1068 | } |
1069 | } | |
b467495c | 1070 | if (set_file_attrs(fname, file, real_ret ? NULL : &real_st, 0) |
f4164b73 | 1071 | && verbose && code != FNONE && f_out != -1) |
45c49b52 | 1072 | rprintf(code, "%s/\n", fname); |
b467495c | 1073 | if (real_ret != 0 && one_file_system) |
b2e4811d | 1074 | real_st.st_dev = filesystem_dev; |
f75a53e7 | 1075 | if (delete_during && f_out != -1 && !phase && dry_run < 2 |
31937d36 | 1076 | && (file->flags & FLAG_DEL_HERE)) |
b2e4811d | 1077 | delete_in_dir(the_file_list, fname, file, &real_st); |
2f03f956 | 1078 | return; |
06a1dbad | 1079 | } |
6c3862fa | 1080 | |
273a7ed5 WD |
1081 | if (preserve_hard_links && file->link_u.links |
1082 | && hard_link_check(file, ndx, fname, statret, &st, | |
1083 | itemizing, code, HL_CHECK_MASTER)) | |
1084 | return; | |
1085 | ||
2f03f956 | 1086 | if (preserve_links && S_ISLNK(file->mode)) { |
4f5b0756 | 1087 | #ifdef SUPPORT_LINKS |
728d0922 | 1088 | if (safe_symlinks && unsafe_symlink(file->u.link, fname)) { |
2f03f956 | 1089 | if (verbose) { |
2a5d5a8c | 1090 | if (the_file_list->count == 1) |
6cbde57d | 1091 | fname = f_name(file, NULL); |
4875d6b6 WD |
1092 | rprintf(FINFO, |
1093 | "ignoring unsafe symlink %s -> \"%s\"\n", | |
45c49b52 | 1094 | full_fname(fname), file->u.link); |
2f03f956 AT |
1095 | } |
1096 | return; | |
1097 | } | |
1098 | if (statret == 0) { | |
7e38410e WD |
1099 | char lnk[MAXPATHLEN]; |
1100 | int len; | |
1101 | ||
116a4769 WD |
1102 | if (!S_ISLNK(st.st_mode)) |
1103 | statret = -1; | |
1104 | else if ((len = readlink(fname, lnk, MAXPATHLEN-1)) > 0 | |
1105 | && strncmp(lnk, file->u.link, len) == 0 | |
1106 | && file->u.link[len] == '\0') { | |
1107 | /* The link is pointing to the right place. */ | |
1108 | if (itemizing) | |
1109 | itemize(file, ndx, 0, &st, 0, 0, NULL); | |
1110 | set_file_attrs(fname, file, &st, maybe_ATTRS_REPORT); | |
1111 | if (preserve_hard_links && file->link_u.links) | |
1112 | hard_link_cluster(file, ndx, itemizing, code); | |
1113 | if (remove_source_files == 1) | |
1114 | goto return_with_success; | |
1115 | return; | |
2cda2560 | 1116 | } |
7e38410e WD |
1117 | /* Not the right symlink (or not a symlink), so |
1118 | * delete it. */ | |
c6fadc0e | 1119 | if (delete_item(fname, st.st_mode, "symlink", del_opts) != 0) |
1f1d368a | 1120 | return; |
1c6e9dfa | 1121 | } else if (basis_dir[0] != NULL) { |
116a4769 WD |
1122 | int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &st, |
1123 | itemizing, maybe_ATTRS_REPORT, code); | |
1124 | if (j == -2) { | |
ba212fe0 WD |
1125 | #ifndef CAN_HARDLINK_SYMLINK |
1126 | if (link_dest) { | |
1127 | /* Resort to --copy-dest behavior. */ | |
1128 | } else | |
1129 | #endif | |
48224e4c WD |
1130 | if (!copy_dest) |
1131 | return; | |
f4164b73 WD |
1132 | itemizing = 0; |
1133 | code = FNONE; | |
116a4769 WD |
1134 | } else if (j >= 0) |
1135 | statret = 1; | |
2f03f956 | 1136 | } |
273a7ed5 WD |
1137 | if (preserve_hard_links && file->link_u.links |
1138 | && hard_link_check(file, ndx, fname, -1, &st, | |
1139 | itemizing, code, HL_SKIP)) | |
1140 | return; | |
116a4769 | 1141 | if (do_symlink(file->u.link, fname) != 0) { |
d62bcc17 | 1142 | rsyserr(FERROR, errno, "symlink %s -> \"%s\" failed", |
45c49b52 | 1143 | full_fname(fname), file->u.link); |
2f03f956 | 1144 | } else { |
e912bd4d | 1145 | set_file_attrs(fname, file, NULL, 0); |
6c3862fa | 1146 | if (itemizing) { |
ee1d11c4 | 1147 | itemize(file, ndx, statret, &st, |
ef20efcb | 1148 | ITEM_LOCAL_CHANGE, 0, NULL); |
6c3862fa | 1149 | } |
116a4769 WD |
1150 | if (code != FNONE && verbose) |
1151 | rprintf(code, "%s -> %s\n", fname, file->u.link); | |
273a7ed5 WD |
1152 | if (preserve_hard_links && file->link_u.links) |
1153 | hard_link_cluster(file, ndx, itemizing, code); | |
f964ac5e WD |
1154 | /* This does not check remove_source_files == 1 |
1155 | * because this is one of the items that the old | |
1156 | * --remove-sent-files option would remove. */ | |
47c11975 | 1157 | if (remove_source_files) |
04cd8789 | 1158 | goto return_with_success; |
2f03f956 AT |
1159 | } |
1160 | #endif | |
1161 | return; | |
1162 | } | |
1163 | ||
b5c6a6ae WD |
1164 | if ((am_root && preserve_devices && IS_DEVICE(file->mode)) |
1165 | || (preserve_specials && IS_SPECIAL(file->mode))) { | |
116a4769 | 1166 | if (statret == 0) { |
c6fadc0e WD |
1167 | char *t; |
1168 | if (IS_DEVICE(file->mode)) { | |
1169 | if (!IS_DEVICE(st.st_mode)) | |
1170 | statret = -1; | |
1171 | t = "device file"; | |
1172 | } else { | |
1173 | if (!IS_SPECIAL(st.st_mode)) | |
1174 | statret = -1; | |
1175 | t = "special file"; | |
1176 | } | |
1177 | if (statret == 0 | |
1178 | && (st.st_mode & ~CHMOD_BITS) == (file->mode & ~CHMOD_BITS) | |
1179 | && st.st_rdev == file->u.rdev) { | |
116a4769 WD |
1180 | /* The device or special file is identical. */ |
1181 | if (itemizing) | |
1182 | itemize(file, ndx, 0, &st, 0, 0, NULL); | |
1183 | set_file_attrs(fname, file, &st, maybe_ATTRS_REPORT); | |
1184 | if (preserve_hard_links && file->link_u.links) | |
1185 | hard_link_cluster(file, ndx, itemizing, code); | |
1186 | if (remove_source_files == 1) | |
1187 | goto return_with_success; | |
1188 | return; | |
1189 | } | |
c6fadc0e | 1190 | if (delete_item(fname, st.st_mode, t, del_opts) != 0) |
116a4769 WD |
1191 | return; |
1192 | } else if (basis_dir[0] != NULL) { | |
1193 | int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &st, | |
1194 | itemizing, maybe_ATTRS_REPORT, code); | |
1195 | if (j == -2) { | |
ba212fe0 WD |
1196 | #ifndef CAN_HARDLINK_SPECIAL |
1197 | if (link_dest) { | |
1198 | /* Resort to --copy-dest behavior. */ | |
1199 | } else | |
1200 | #endif | |
48224e4c WD |
1201 | if (!copy_dest) |
1202 | return; | |
f4164b73 WD |
1203 | itemizing = 0; |
1204 | code = FNONE; | |
116a4769 WD |
1205 | } else if (j >= 0) |
1206 | statret = 1; | |
48224e4c | 1207 | } |
116a4769 WD |
1208 | if (preserve_hard_links && file->link_u.links |
1209 | && hard_link_check(file, ndx, fname, -1, &st, | |
1210 | itemizing, code, HL_SKIP)) | |
1211 | return; | |
1212 | if (verbose > 2) { | |
1213 | rprintf(FINFO,"mknod(%s,0%o,0x%x)\n", | |
1214 | fname, (int)file->mode, (int)file->u.rdev); | |
1215 | } | |
1216 | if (do_mknod(fname, file->mode, file->u.rdev) < 0) { | |
1217 | rsyserr(FERROR, errno, "mknod %s failed", | |
1218 | full_fname(fname)); | |
2f03f956 | 1219 | } else { |
116a4769 WD |
1220 | set_file_attrs(fname, file, NULL, 0); |
1221 | if (itemizing) { | |
1222 | itemize(file, ndx, statret, &st, | |
1223 | ITEM_LOCAL_CHANGE, 0, NULL); | |
1224 | } | |
1225 | if (code != FNONE && verbose) | |
1226 | rprintf(code, "%s\n", fname); | |
273a7ed5 WD |
1227 | if (preserve_hard_links && file->link_u.links) |
1228 | hard_link_cluster(file, ndx, itemizing, code); | |
47c11975 | 1229 | if (remove_source_files == 1) |
04cd8789 | 1230 | goto return_with_success; |
2f03f956 AT |
1231 | } |
1232 | return; | |
1233 | } | |
2f03f956 | 1234 | |
2f03f956 | 1235 | if (!S_ISREG(file->mode)) { |
2a5d5a8c | 1236 | if (the_file_list->count == 1) |
6cbde57d | 1237 | fname = f_name(file, NULL); |
45c49b52 | 1238 | rprintf(FINFO, "skipping non-regular file \"%s\"\n", fname); |
2f03f956 AT |
1239 | return; |
1240 | } | |
1241 | ||
289a3216 WD |
1242 | if (max_size && file->length > max_size) { |
1243 | if (verbose > 1) { | |
1244 | if (the_file_list->count == 1) | |
6cbde57d | 1245 | fname = f_name(file, NULL); |
45c49b52 | 1246 | rprintf(FINFO, "%s is over max-size\n", fname); |
289a3216 WD |
1247 | } |
1248 | return; | |
1249 | } | |
02b5cb23 WD |
1250 | if (min_size && file->length < min_size) { |
1251 | if (verbose > 1) { | |
1252 | if (the_file_list->count == 1) | |
6cbde57d | 1253 | fname = f_name(file, NULL); |
45c49b52 | 1254 | rprintf(FINFO, "%s is under min-size\n", fname); |
02b5cb23 WD |
1255 | } |
1256 | return; | |
1257 | } | |
289a3216 | 1258 | |
e90aab49 | 1259 | if (ignore_existing && statret == 0) { |
c3cbcfb8 | 1260 | if (verbose > 1) |
45c49b52 | 1261 | rprintf(FINFO, "%s exists\n", fname); |
c3cbcfb8 WD |
1262 | return; |
1263 | } | |
1264 | ||
1265 | if (update_only && statret == 0 | |
0f86c74e | 1266 | && cmp_time(st.st_mtime, file->modtime) > 0) { |
c3cbcfb8 | 1267 | if (verbose > 1) |
45c49b52 | 1268 | rprintf(FINFO, "%s is newer\n", fname); |
c3cbcfb8 WD |
1269 | return; |
1270 | } | |
1271 | ||
375a4556 | 1272 | fnamecmp = fname; |
41cfde6b | 1273 | fnamecmp_type = FNAMECMP_FNAME; |
375a4556 | 1274 | |
1f1d368a | 1275 | if (statret == 0 && !S_ISREG(st.st_mode)) { |
c6fadc0e | 1276 | if (delete_item(fname, st.st_mode, "regular file", del_opts) != 0) |
1f1d368a WD |
1277 | return; |
1278 | statret = -1; | |
1279 | stat_errno = ENOENT; | |
1280 | } | |
1281 | ||
06a1dbad | 1282 | if (statret != 0 && basis_dir[0] != NULL) { |
48224e4c | 1283 | int j = try_dests_reg(file, fname, ndx, fnamecmpbuf, &st, |
e912bd4d | 1284 | itemizing, maybe_ATTRS_REPORT, code); |
04cd8789 | 1285 | if (j == -2) { |
47c11975 | 1286 | if (remove_source_files == 1) |
04cd8789 | 1287 | goto return_with_success; |
48224e4c | 1288 | return; |
04cd8789 | 1289 | } |
b06050f9 | 1290 | if (j >= 0) { |
48224e4c WD |
1291 | fnamecmp = fnamecmpbuf; |
1292 | fnamecmp_type = j; | |
9ba46343 | 1293 | statret = 0; |
e7d13fe5 WD |
1294 | } |
1295 | } | |
1296 | ||
1f1d368a WD |
1297 | real_ret = statret; |
1298 | real_st = st; | |
375a4556 | 1299 | |
9d954dca | 1300 | if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL |
72c19bb3 WD |
1301 | && link_stat(partialptr, &partial_st, 0) == 0 |
1302 | && S_ISREG(partial_st.st_mode)) { | |
06a1dbad | 1303 | if (statret != 0) |
72c19bb3 WD |
1304 | goto prepare_to_open; |
1305 | } else | |
1306 | partialptr = NULL; | |
89f7eff3 | 1307 | |
ccb16b46 | 1308 | if (statret != 0 && fuzzy_dirlist && dry_run <= 1) { |
8e85be0a WD |
1309 | int j = find_fuzzy(file, fuzzy_dirlist); |
1310 | if (j >= 0) { | |
1311 | fuzzy_file = fuzzy_dirlist->files[j]; | |
6cbde57d | 1312 | f_name(fuzzy_file, fnamecmpbuf); |
8e85be0a WD |
1313 | if (verbose > 2) { |
1314 | rprintf(FINFO, "fuzzy basis selected for %s: %s\n", | |
45c49b52 | 1315 | fname, fnamecmpbuf); |
8e85be0a | 1316 | } |
8e85be0a | 1317 | st.st_size = fuzzy_file->length; |
8e85be0a WD |
1318 | statret = 0; |
1319 | fnamecmp = fnamecmpbuf; | |
1320 | fnamecmp_type = FNAMECMP_FUZZY; | |
1321 | } | |
1322 | } | |
1323 | ||
06a1dbad | 1324 | if (statret != 0) { |
273a7ed5 | 1325 | if (preserve_hard_links && file->link_u.links |
d8169e6f WD |
1326 | && hard_link_check(file, ndx, fname, statret, &st, |
1327 | itemizing, code, HL_SKIP)) | |
6dff5992 | 1328 | return; |
41cfde6b WD |
1329 | if (stat_errno == ENOENT) |
1330 | goto notify_others; | |
991daf00 WD |
1331 | rsyserr(FERROR, stat_errno, "recv_generator: failed to stat %s", |
1332 | full_fname(fname)); | |
2f03f956 AT |
1333 | return; |
1334 | } | |
1335 | ||
6cc11982 WD |
1336 | if (append_mode && st.st_size > file->length) |
1337 | return; | |
1338 | ||
48224e4c | 1339 | if (fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH) |
b7e8628c | 1340 | ; |
8e85be0a WD |
1341 | else if (fnamecmp_type == FNAMECMP_FUZZY) |
1342 | ; | |
b7e8628c | 1343 | else if (unchanged_file(fnamecmp, file, &st)) { |
d8b108c2 WD |
1344 | if (partialptr) { |
1345 | do_unlink(partialptr); | |
1346 | handle_partial_dir(partialptr, PDIR_DELETE); | |
1347 | } | |
48224e4c WD |
1348 | if (itemizing) { |
1349 | itemize(file, ndx, real_ret, &real_st, | |
1350 | 0, 0, NULL); | |
a1d23b53 | 1351 | } |
e912bd4d | 1352 | set_file_attrs(fname, file, &st, maybe_ATTRS_REPORT); |
48224e4c WD |
1353 | if (preserve_hard_links && file->link_u.links) |
1354 | hard_link_cluster(file, ndx, itemizing, code); | |
47c11975 | 1355 | if (remove_source_files != 1) |
04cd8789 WD |
1356 | return; |
1357 | return_with_success: | |
1358 | if (!dry_run) { | |
1359 | char numbuf[4]; | |
1360 | SIVAL(numbuf, 0, ndx); | |
1361 | send_msg(MSG_SUCCESS, numbuf, 4); | |
1362 | } | |
967866d4 | 1363 | return; |
2f03f956 AT |
1364 | } |
1365 | ||
b2e7c913 | 1366 | prepare_to_open: |
9d954dca WD |
1367 | if (partialptr) { |
1368 | st = partial_st; | |
1369 | fnamecmp = partialptr; | |
1370 | fnamecmp_type = FNAMECMP_PARTIAL_DIR; | |
1371 | statret = 0; | |
1372 | } | |
1373 | ||
beb51aa0 | 1374 | if (!do_xfers || read_batch || whole_file) |
3841a04e | 1375 | goto notify_others; |
2f03f956 | 1376 | |
ccb16b46 | 1377 | if (fuzzy_dirlist) { |
8e85be0a WD |
1378 | int j = flist_find(fuzzy_dirlist, file); |
1379 | if (j >= 0) /* don't use changing file as future fuzzy basis */ | |
1380 | fuzzy_dirlist->files[j]->flags |= FLAG_NO_FUZZY; | |
1381 | } | |
1382 | ||
2cda2560 | 1383 | /* open the file */ |
8c9fd200 | 1384 | fd = do_open(fnamecmp, O_RDONLY, 0); |
2f03f956 AT |
1385 | |
1386 | if (fd == -1) { | |
d62bcc17 WD |
1387 | rsyserr(FERROR, errno, "failed to open %s, continuing", |
1388 | full_fname(fnamecmp)); | |
b2e7c913 | 1389 | pretend_missing: |
60be6acf | 1390 | /* pretend the file didn't exist */ |
273a7ed5 | 1391 | if (preserve_hard_links && file->link_u.links |
d8169e6f WD |
1392 | && hard_link_check(file, ndx, fname, statret, &st, |
1393 | itemizing, code, HL_SKIP)) | |
6dff5992 | 1394 | return; |
1f1d368a | 1395 | statret = real_ret = -1; |
41cfde6b | 1396 | goto notify_others; |
2f03f956 AT |
1397 | } |
1398 | ||
ef20efcb | 1399 | if (inplace && make_backups && fnamecmp_type == FNAMECMP_FNAME) { |
cd6aa5b5 WD |
1400 | if (!(backupptr = get_backup_name(fname))) { |
1401 | close(fd); | |
1402 | return; | |
1403 | } | |
3de73827 | 1404 | if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS))) { |
cd6aa5b5 WD |
1405 | close(fd); |
1406 | goto pretend_missing; | |
1407 | } | |
1408 | if (robust_unlink(backupptr) && errno != ENOENT) { | |
1409 | rsyserr(FERROR, errno, "unlink %s", | |
1410 | full_fname(backupptr)); | |
1411 | free(back_file); | |
1412 | close(fd); | |
1413 | return; | |
1414 | } | |
1415 | if ((f_copy = do_open(backupptr, | |
1416 | O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0) { | |
1417 | rsyserr(FERROR, errno, "open %s", | |
1418 | full_fname(backupptr)); | |
1419 | free(back_file); | |
1420 | close(fd); | |
1421 | return; | |
1422 | } | |
41cfde6b | 1423 | fnamecmp_type = FNAMECMP_BACKUP; |
cd6aa5b5 WD |
1424 | } |
1425 | ||
dfd5ba6a | 1426 | if (verbose > 3) { |
ecc81fce | 1427 | rprintf(FINFO, "gen mapped %s of size %.0f\n", |
45c49b52 | 1428 | fnamecmp, (double)st.st_size); |
dfd5ba6a | 1429 | } |
2f03f956 | 1430 | |
2f03f956 | 1431 | if (verbose > 2) |
0492fdfb | 1432 | rprintf(FINFO, "generating and sending sums for %d\n", ndx); |
2f03f956 | 1433 | |
b2e7c913 | 1434 | notify_others: |
47c11975 | 1435 | if (remove_source_files && !delay_updates && !phase) |
04cd8789 | 1436 | increment_active_files(ndx, itemizing, code); |
0492fdfb | 1437 | write_int(f_out, ndx); |
6c3862fa | 1438 | if (itemizing) { |
ee1d11c4 | 1439 | int iflags = ITEM_TRANSFER; |
8237f930 | 1440 | if (always_checksum) |
a1d23b53 | 1441 | iflags |= ITEM_REPORT_CHECKSUM; |
352963dd | 1442 | if (fnamecmp_type != FNAMECMP_FNAME) |
ef20efcb WD |
1443 | iflags |= ITEM_BASIS_TYPE_FOLLOWS; |
1444 | if (fnamecmp_type == FNAMECMP_FUZZY) | |
1445 | iflags |= ITEM_XNAME_FOLLOWS; | |
1f1d368a | 1446 | itemize(file, -1, real_ret, &real_st, iflags, fnamecmp_type, |
ef20efcb | 1447 | fuzzy_file ? fuzzy_file->basename : NULL); |
8e85be0a | 1448 | } |
cd6aa5b5 | 1449 | |
beb51aa0 | 1450 | if (!do_xfers) { |
ee1d11c4 WD |
1451 | if (preserve_hard_links && file->link_u.links) |
1452 | hard_link_cluster(file, ndx, itemizing, code); | |
1453 | return; | |
1454 | } | |
1455 | if (read_batch) | |
41cfde6b WD |
1456 | return; |
1457 | ||
33ab4ad8 | 1458 | if (statret != 0 || whole_file) { |
e86ae6bc WD |
1459 | write_sum_head(f_out, NULL); |
1460 | return; | |
1461 | } | |
1462 | ||
1463 | generate_and_send_sums(fd, st.st_size, f_out, f_copy); | |
1464 | ||
1465 | if (f_copy >= 0) { | |
1466 | close(f_copy); | |
e912bd4d | 1467 | set_file_attrs(backupptr, back_file, NULL, 0); |
e86ae6bc WD |
1468 | if (verbose > 1) { |
1469 | rprintf(FINFO, "backed up %s to %s\n", | |
45c49b52 | 1470 | fname, backupptr); |
41cfde6b | 1471 | } |
e86ae6bc WD |
1472 | free(back_file); |
1473 | } | |
41cfde6b | 1474 | |
e86ae6bc | 1475 | close(fd); |
2f03f956 AT |
1476 | } |
1477 | ||
ef20efcb | 1478 | void generate_files(int f_out, struct file_list *flist, char *local_name) |
2f03f956 | 1479 | { |
ac40b747 | 1480 | int i; |
968c8030 | 1481 | char fbuf[MAXPATHLEN]; |
e912bd4d | 1482 | int itemizing, maybe_ATTRS_REPORT; |
7433d73a | 1483 | enum logcode code; |
ac40b747 | 1484 | int lull_mod = allowed_lull * 5; |
3ea9bbd6 WD |
1485 | int need_retouch_dir_times = preserve_times && !omit_dir_times; |
1486 | int need_retouch_dir_perms = 0; | |
e90aab49 WD |
1487 | int save_ignore_existing = ignore_existing; |
1488 | int save_ignore_non_existing = ignore_non_existing; | |
083acd49 | 1489 | int save_do_progress = do_progress; |
cd908ef4 | 1490 | int save_make_backups = make_backups; |
88a16c8b | 1491 | int dir_tweaking = !(list_only || local_name || dry_run); |
ee1d11c4 | 1492 | |
7433d73a WD |
1493 | if (protocol_version >= 29) { |
1494 | itemizing = 1; | |
17bda2d1 | 1495 | maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT; |
f4164b73 | 1496 | code = logfile_format_has_i ? FNONE : FLOG; |
7433d73a | 1497 | } else if (am_daemon) { |
ecc7623e | 1498 | itemizing = logfile_format_has_i && do_xfers; |
e912bd4d | 1499 | maybe_ATTRS_REPORT = ATTRS_REPORT; |
beb51aa0 | 1500 | code = itemizing || !do_xfers ? FCLIENT : FINFO; |
7433d73a | 1501 | } else if (!am_server) { |
17bda2d1 WD |
1502 | itemizing = stdout_format_has_i; |
1503 | maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT; | |
f4164b73 | 1504 | code = itemizing ? FNONE : FINFO; |
7433d73a WD |
1505 | } else { |
1506 | itemizing = 0; | |
e912bd4d | 1507 | maybe_ATTRS_REPORT = ATTRS_REPORT; |
7433d73a WD |
1508 | code = FINFO; |
1509 | } | |
2f03f956 | 1510 | |
45e08edb WD |
1511 | if (verbose > 2) { |
1512 | rprintf(FINFO, "generator starting pid=%ld count=%d\n", | |
1513 | (long)getpid(), flist->count); | |
1514 | } | |
2f03f956 | 1515 | |
59faec8b | 1516 | if (delete_before && !local_name && flist->count > 0) |
ee1d11c4 | 1517 | do_delete_pass(flist); |
083acd49 | 1518 | do_progress = 0; |
59faec8b | 1519 | |
6cc11982 | 1520 | if (append_mode || whole_file < 0) |
33ab4ad8 | 1521 | whole_file = 0; |
3e7053ac | 1522 | if (verbose >= 2) { |
1b1fef20 | 1523 | rprintf(FINFO, "delta-transmission %s\n", |
33ab4ad8 | 1524 | whole_file |
1b1fef20 WD |
1525 | ? "disabled for local transfer or --whole-file" |
1526 | : "enabled"); | |
3e7053ac | 1527 | } |
2cda2560 | 1528 | |
513fd04d WD |
1529 | /* Since we often fill up the outgoing socket and then just sit around |
1530 | * waiting for the other 2 processes to do their thing, we don't want | |
1531 | * to exit on a timeout. If the data stops flowing, the receiver will | |
1532 | * notice that and let us know via the redo pipe (or its closing). */ | |
1533 | ignore_timeout = 1; | |
a57873b7 | 1534 | |
2f03f956 AT |
1535 | for (i = 0; i < flist->count; i++) { |
1536 | struct file_struct *file = flist->files[i]; | |
2f03f956 | 1537 | |
dfd5ba6a WD |
1538 | if (!file->basename) |
1539 | continue; | |
0492fdfb | 1540 | |
0e5665d3 WD |
1541 | if (local_name) |
1542 | strlcpy(fbuf, local_name, sizeof fbuf); | |
1543 | else | |
6cbde57d | 1544 | f_name(file, fbuf); |
e912bd4d | 1545 | recv_generator(fbuf, file, i, itemizing, maybe_ATTRS_REPORT, |
0e5665d3 | 1546 | code, f_out); |
1f7e29b9 | 1547 | |
0492fdfb WD |
1548 | /* We need to ensure that any dirs we create have writeable |
1549 | * permissions during the time we are putting files within | |
1550 | * them. This is then fixed after the transfer is done. */ | |
00b96184 | 1551 | #ifdef HAVE_CHMOD |
41b84ce0 | 1552 | if (!am_root && S_ISDIR(file->mode) && !(file->mode & S_IWUSR) |
88a16c8b | 1553 | && dir_tweaking) { |
e912bd4d | 1554 | mode_t mode = file->mode | S_IWUSR; /* user write */ |
1f7e29b9 | 1555 | char *fname = local_name ? local_name : fbuf; |
00b96184 | 1556 | if (do_chmod(fname, mode) < 0) { |
1f7e29b9 WD |
1557 | rsyserr(FERROR, errno, |
1558 | "failed to modify permissions on %s", | |
1559 | full_fname(fname)); | |
1560 | } | |
3ea9bbd6 | 1561 | need_retouch_dir_perms = 1; |
2f03f956 | 1562 | } |
00b96184 | 1563 | #endif |
2f03f956 | 1564 | |
ee1d11c4 WD |
1565 | if (preserve_hard_links) |
1566 | check_for_finished_hlinks(itemizing, code); | |
9ac2395b | 1567 | |
18a11cfd | 1568 | if (allowed_lull && !(i % lull_mod)) |
ee1d11c4 | 1569 | maybe_send_keepalive(); |
c2523a05 | 1570 | else if (!(i % 200)) |
417099fa | 1571 | maybe_flush_socket(); |
2f03f956 | 1572 | } |
ef20efcb | 1573 | recv_generator(NULL, NULL, 0, 0, 0, code, -1); |
c93fad5e | 1574 | if (delete_during) |
0e82af2d | 1575 | delete_in_dir(NULL, NULL, NULL, NULL); |
2f03f956 AT |
1576 | |
1577 | phase++; | |
1578 | csum_length = SUM_LENGTH; | |
02b5cb23 | 1579 | max_size = min_size = ignore_existing = ignore_non_existing = 0; |
0492fdfb | 1580 | update_only = always_checksum = size_only = 0; |
e1f67417 | 1581 | ignore_times = 1; |
6cc11982 WD |
1582 | if (append_mode) /* resend w/o append mode */ |
1583 | append_mode = -1; /* ... but only longer files */ | |
e6bc6f42 | 1584 | make_backups = 0; /* avoid a duplicate backup for inplace processing */ |
2f03f956 AT |
1585 | |
1586 | if (verbose > 2) | |
1587 | rprintf(FINFO,"generate_files phase=%d\n",phase); | |
1588 | ||
7daccb8e | 1589 | write_int(f_out, -1); |
2f03f956 | 1590 | |
bc63ae3f S |
1591 | /* files can cycle through the system more than once |
1592 | * to catch initial checksum errors */ | |
ee1d11c4 | 1593 | while ((i = get_redo_num(itemizing, code)) != -1) { |
bc63ae3f | 1594 | struct file_struct *file = flist->files[i]; |
0e5665d3 WD |
1595 | if (local_name) |
1596 | strlcpy(fbuf, local_name, sizeof fbuf); | |
1597 | else | |
6cbde57d | 1598 | f_name(file, fbuf); |
e912bd4d | 1599 | recv_generator(fbuf, file, i, itemizing, maybe_ATTRS_REPORT, |
0e5665d3 | 1600 | code, f_out); |
bc63ae3f | 1601 | } |
2f03f956 | 1602 | |
bc63ae3f | 1603 | phase++; |
e90aab49 WD |
1604 | ignore_non_existing = save_ignore_non_existing; |
1605 | ignore_existing = save_ignore_existing; | |
cd908ef4 | 1606 | make_backups = save_make_backups; |
0492fdfb | 1607 | |
bc63ae3f S |
1608 | if (verbose > 2) |
1609 | rprintf(FINFO,"generate_files phase=%d\n",phase); | |
2f03f956 | 1610 | |
7daccb8e | 1611 | write_int(f_out, -1); |
70352269 WD |
1612 | /* Reduce round-trip lag-time for a useless delay-updates phase. */ |
1613 | if (protocol_version >= 29 && !delay_updates) | |
1614 | write_int(f_out, -1); | |
6dff5992 | 1615 | |
0438f100 | 1616 | /* Read MSG_DONE for the redo phase (and any prior messages). */ |
ee1d11c4 | 1617 | get_redo_num(itemizing, code); |
6dff5992 | 1618 | |
0438f100 WD |
1619 | if (protocol_version >= 29) { |
1620 | phase++; | |
1621 | if (verbose > 2) | |
1622 | rprintf(FINFO, "generate_files phase=%d\n", phase); | |
70352269 WD |
1623 | if (delay_updates) |
1624 | write_int(f_out, -1); | |
1625 | /* Read MSG_DONE for delay-updates phase & prior messages. */ | |
0438f100 WD |
1626 | get_redo_num(itemizing, code); |
1627 | } | |
1628 | ||
083acd49 | 1629 | do_progress = save_do_progress; |
59faec8b | 1630 | if (delete_after && !local_name && flist->count > 0) |
ee1d11c4 | 1631 | do_delete_pass(flist); |
59faec8b | 1632 | |
88a16c8b | 1633 | if ((need_retouch_dir_perms || need_retouch_dir_times) && dir_tweaking) { |
18a11cfd | 1634 | int j = 0; |
3ea9bbd6 WD |
1635 | /* Now we need to fix any directory permissions that were |
1636 | * modified during the transfer and/or re-set any tweaked | |
1637 | * modified-time values. */ | |
1638 | for (i = 0; i < flist->count; i++) { | |
1639 | struct file_struct *file = flist->files[i]; | |
b435d717 | 1640 | |
3ea9bbd6 WD |
1641 | if (!file->basename || !S_ISDIR(file->mode)) |
1642 | continue; | |
1643 | if (!need_retouch_dir_times && file->mode & S_IWUSR) | |
1644 | continue; | |
caff3322 WD |
1645 | if (file->flags & FLAG_MISSING) { |
1646 | int missing = file->dir.depth; | |
1647 | while (++i < flist->count) { | |
1648 | file = flist->files[i]; | |
1649 | if (file->dir.depth <= missing) | |
1650 | break; | |
1651 | } | |
1652 | i--; | |
1653 | continue; | |
1654 | } | |
6cbde57d | 1655 | recv_generator(f_name(file, NULL), file, i, itemizing, |
e912bd4d | 1656 | maybe_ATTRS_REPORT, code, -1); |
417099fa | 1657 | if (allowed_lull && !(++j % lull_mod)) |
ee1d11c4 | 1658 | maybe_send_keepalive(); |
c2523a05 | 1659 | else if (!(j % 200)) |
417099fa | 1660 | maybe_flush_socket(); |
3ea9bbd6 | 1661 | } |
6dff5992 | 1662 | } |
ef20efcb | 1663 | recv_generator(NULL, NULL, 0, 0, 0, code, -1); |
6dff5992 | 1664 | |
e5e85283 | 1665 | if (max_delete >= 0 && deletion_count > max_delete) { |
f75a53e7 WD |
1666 | rprintf(FINFO, |
1667 | "Deletions stopped due to --max-delete limit (%d skipped)\n", | |
1668 | deletion_count - max_delete); | |
1669 | io_error |= IOERR_DEL_LIMIT; | |
1670 | } | |
1671 | ||
6dff5992 WD |
1672 | if (verbose > 2) |
1673 | rprintf(FINFO,"generate_files finished\n"); | |
2f03f956 | 1674 | } |