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