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