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