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