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