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