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