| 1 | /* |
| 2 | Copyright (C) Andrew Tridgell 1996 |
| 3 | Copyright (C) Paul Mackerras 1996 |
| 4 | Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org> |
| 5 | |
| 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program; if not, write to the Free Software |
| 18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | */ |
| 20 | |
| 21 | /** @file flist.c |
| 22 | * Generate and receive file lists |
| 23 | * |
| 24 | * @sa http://lists.samba.org/pipermail/rsync/2000-June/002351.html |
| 25 | * |
| 26 | **/ |
| 27 | |
| 28 | #include "rsync.h" |
| 29 | |
| 30 | extern struct stats stats; |
| 31 | |
| 32 | extern int verbose; |
| 33 | extern int do_progress; |
| 34 | extern int am_root; |
| 35 | extern int am_server; |
| 36 | extern int am_daemon; |
| 37 | extern int am_sender; |
| 38 | extern int always_checksum; |
| 39 | extern int module_id; |
| 40 | extern int ignore_errors; |
| 41 | extern int numeric_ids; |
| 42 | |
| 43 | extern int cvs_exclude; |
| 44 | |
| 45 | extern int recurse; |
| 46 | extern char curr_dir[MAXPATHLEN]; |
| 47 | extern char *files_from; |
| 48 | extern int filesfrom_fd; |
| 49 | |
| 50 | extern int one_file_system; |
| 51 | extern int keep_dirlinks; |
| 52 | extern int preserve_links; |
| 53 | extern int preserve_hard_links; |
| 54 | extern int preserve_perms; |
| 55 | extern int preserve_devices; |
| 56 | extern int preserve_uid; |
| 57 | extern int preserve_gid; |
| 58 | extern int relative_paths; |
| 59 | extern int implied_dirs; |
| 60 | extern int copy_links; |
| 61 | extern int copy_unsafe_links; |
| 62 | extern int protocol_version; |
| 63 | extern int sanitize_paths; |
| 64 | extern int delete_excluded; |
| 65 | extern int orig_umask; |
| 66 | extern int list_only; |
| 67 | |
| 68 | extern struct exclude_list_struct exclude_list; |
| 69 | extern struct exclude_list_struct server_exclude_list; |
| 70 | extern struct exclude_list_struct local_exclude_list; |
| 71 | |
| 72 | int io_error; |
| 73 | |
| 74 | static char empty_sum[MD4_SUM_LENGTH]; |
| 75 | static unsigned int file_struct_len; |
| 76 | static struct file_list *received_flist; |
| 77 | |
| 78 | static void clean_flist(struct file_list *flist, int strip_root, int no_dups); |
| 79 | static void output_flist(struct file_list *flist); |
| 80 | |
| 81 | void init_flist(void) |
| 82 | { |
| 83 | struct file_struct f; |
| 84 | |
| 85 | /* Figure out how big the file_struct is without trailing padding */ |
| 86 | file_struct_len = offsetof(struct file_struct, flags) + sizeof f.flags; |
| 87 | } |
| 88 | |
| 89 | |
| 90 | static int show_filelist_p(void) |
| 91 | { |
| 92 | return verbose && (recurse || files_from) && !am_server; |
| 93 | } |
| 94 | |
| 95 | static void start_filelist_progress(char *kind) |
| 96 | { |
| 97 | rprintf(FINFO, "%s ... ", kind); |
| 98 | if (verbose > 1 || do_progress) |
| 99 | rprintf(FINFO, "\n"); |
| 100 | rflush(FINFO); |
| 101 | } |
| 102 | |
| 103 | |
| 104 | static void emit_filelist_progress(const struct file_list *flist) |
| 105 | { |
| 106 | rprintf(FINFO, " %d files...\r", flist->count); |
| 107 | } |
| 108 | |
| 109 | |
| 110 | static void maybe_emit_filelist_progress(const struct file_list *flist) |
| 111 | { |
| 112 | if (do_progress && show_filelist_p() && (flist->count % 100) == 0) |
| 113 | emit_filelist_progress(flist); |
| 114 | } |
| 115 | |
| 116 | |
| 117 | static void finish_filelist_progress(const struct file_list *flist) |
| 118 | { |
| 119 | if (do_progress) { |
| 120 | /* This overwrites the progress line */ |
| 121 | rprintf(FINFO, "%d file%sto consider\n", |
| 122 | flist->count, flist->count == 1 ? " " : "s "); |
| 123 | } else |
| 124 | rprintf(FINFO, "done\n"); |
| 125 | } |
| 126 | |
| 127 | void show_flist_stats(void) |
| 128 | { |
| 129 | /* Nothing yet */ |
| 130 | } |
| 131 | |
| 132 | |
| 133 | static void list_file_entry(struct file_struct *f) |
| 134 | { |
| 135 | char perms[11]; |
| 136 | |
| 137 | if (!f->basename) { |
| 138 | /* this can happen if duplicate names were removed */ |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | permstring(perms, f->mode); |
| 143 | |
| 144 | #if SUPPORT_LINKS |
| 145 | if (preserve_links && S_ISLNK(f->mode)) { |
| 146 | rprintf(FINFO, "%s %11.0f %s %s -> %s\n", |
| 147 | perms, |
| 148 | (double)f->length, timestring(f->modtime), |
| 149 | f_name(f), f->u.link); |
| 150 | } else |
| 151 | #endif |
| 152 | { |
| 153 | rprintf(FINFO, "%s %11.0f %s %s\n", |
| 154 | perms, |
| 155 | (double)f->length, timestring(f->modtime), |
| 156 | f_name(f)); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | |
| 161 | /** |
| 162 | * Stat either a symlink or its referent, depending on the settings of |
| 163 | * copy_links, copy_unsafe_links, etc. |
| 164 | * |
| 165 | * @retval -1 on error |
| 166 | * |
| 167 | * @retval 0 for success |
| 168 | * |
| 169 | * @post If @p path is a symlink, then @p linkbuf (of size @c |
| 170 | * MAXPATHLEN) contains the symlink target. |
| 171 | * |
| 172 | * @post @p buffer contains information about the link or the |
| 173 | * referrent as appropriate, if they exist. |
| 174 | **/ |
| 175 | static int readlink_stat(const char *path, STRUCT_STAT *buffer, char *linkbuf) |
| 176 | { |
| 177 | #if SUPPORT_LINKS |
| 178 | if (copy_links) |
| 179 | return do_stat(path, buffer); |
| 180 | if (link_stat(path, buffer, 0) < 0) |
| 181 | return -1; |
| 182 | if (S_ISLNK(buffer->st_mode)) { |
| 183 | int l = readlink((char *)path, linkbuf, MAXPATHLEN - 1); |
| 184 | if (l == -1) |
| 185 | return -1; |
| 186 | linkbuf[l] = 0; |
| 187 | if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) { |
| 188 | if (verbose > 1) { |
| 189 | rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n", |
| 190 | path, linkbuf); |
| 191 | } |
| 192 | return do_stat(path, buffer); |
| 193 | } |
| 194 | } |
| 195 | return 0; |
| 196 | #else |
| 197 | return do_stat(path, buffer); |
| 198 | #endif |
| 199 | } |
| 200 | |
| 201 | int link_stat(const char *path, STRUCT_STAT *buffer, int follow_dirlinks) |
| 202 | { |
| 203 | #if SUPPORT_LINKS |
| 204 | if (copy_links) |
| 205 | return do_stat(path, buffer); |
| 206 | if (do_lstat(path, buffer) < 0) |
| 207 | return -1; |
| 208 | if (follow_dirlinks && S_ISLNK(buffer->st_mode)) { |
| 209 | STRUCT_STAT st; |
| 210 | if (do_stat(path, &st) == 0 && S_ISDIR(st.st_mode)) |
| 211 | *buffer = st; |
| 212 | } |
| 213 | return 0; |
| 214 | #else |
| 215 | return do_stat(path, buffer); |
| 216 | #endif |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * This function is used to check if a file should be included/excluded |
| 221 | * from the list of files based on its name and type etc. The value of |
| 222 | * exclude_level is set to either SERVER_EXCLUDES or ALL_EXCLUDES. |
| 223 | */ |
| 224 | static int check_exclude_file(char *fname, int is_dir, int exclude_level) |
| 225 | { |
| 226 | int rc; |
| 227 | |
| 228 | #if 0 /* This currently never happens, so avoid a useless compare. */ |
| 229 | if (exclude_level == NO_EXCLUDES) |
| 230 | return 0; |
| 231 | #endif |
| 232 | if (fname) { |
| 233 | /* never exclude '.', even if somebody does --exclude '*' */ |
| 234 | if (fname[0] == '.' && !fname[1]) |
| 235 | return 0; |
| 236 | /* Handle the -R version of the '.' dir. */ |
| 237 | if (fname[0] == '/') { |
| 238 | int len = strlen(fname); |
| 239 | if (fname[len-1] == '.' && fname[len-2] == '/') |
| 240 | return 0; |
| 241 | } |
| 242 | } |
| 243 | if (server_exclude_list.head |
| 244 | && check_exclude(&server_exclude_list, fname, is_dir) < 0) |
| 245 | return 1; |
| 246 | if (exclude_level != ALL_EXCLUDES) |
| 247 | return 0; |
| 248 | if (exclude_list.head |
| 249 | && (rc = check_exclude(&exclude_list, fname, is_dir)) != 0) |
| 250 | return rc < 0; |
| 251 | if (local_exclude_list.head |
| 252 | && check_exclude(&local_exclude_list, fname, is_dir) < 0) |
| 253 | return 1; |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | /* used by the one_file_system code */ |
| 258 | static dev_t filesystem_dev; |
| 259 | |
| 260 | static void set_filesystem(char *fname) |
| 261 | { |
| 262 | STRUCT_STAT st; |
| 263 | if (do_stat(fname, &st) != 0) |
| 264 | return; |
| 265 | filesystem_dev = st.st_dev; |
| 266 | } |
| 267 | |
| 268 | |
| 269 | static int to_wire_mode(mode_t mode) |
| 270 | { |
| 271 | #if SUPPORT_LINKS |
| 272 | if (S_ISLNK(mode) && (_S_IFLNK != 0120000)) |
| 273 | return (mode & ~(_S_IFMT)) | 0120000; |
| 274 | #endif |
| 275 | return (int)mode; |
| 276 | } |
| 277 | |
| 278 | static mode_t from_wire_mode(int mode) |
| 279 | { |
| 280 | if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000)) |
| 281 | return (mode & ~(_S_IFMT)) | _S_IFLNK; |
| 282 | return (mode_t)mode; |
| 283 | } |
| 284 | |
| 285 | |
| 286 | static void send_directory(int f, struct file_list *flist, char *dir); |
| 287 | |
| 288 | static char *flist_dir; |
| 289 | static int flist_dir_len; |
| 290 | |
| 291 | |
| 292 | /** |
| 293 | * Make sure @p flist is big enough to hold at least @p flist->count |
| 294 | * entries. |
| 295 | **/ |
| 296 | void flist_expand(struct file_list *flist) |
| 297 | { |
| 298 | struct file_struct **new_ptr; |
| 299 | |
| 300 | if (flist->count < flist->malloced) |
| 301 | return; |
| 302 | |
| 303 | if (flist->malloced < FLIST_START) |
| 304 | flist->malloced = FLIST_START; |
| 305 | else if (flist->malloced >= FLIST_LINEAR) |
| 306 | flist->malloced += FLIST_LINEAR; |
| 307 | else |
| 308 | flist->malloced *= 2; |
| 309 | |
| 310 | /* |
| 311 | * In case count jumped or we are starting the list |
| 312 | * with a known size just set it. |
| 313 | */ |
| 314 | if (flist->malloced < flist->count) |
| 315 | flist->malloced = flist->count; |
| 316 | |
| 317 | new_ptr = realloc_array(flist->files, struct file_struct *, |
| 318 | flist->malloced); |
| 319 | |
| 320 | if (verbose >= 2) { |
| 321 | rprintf(FINFO, "[%s] expand file_list to %.0f bytes, did%s move\n", |
| 322 | who_am_i(), |
| 323 | (double)sizeof flist->files[0] * flist->malloced, |
| 324 | (new_ptr == flist->files) ? " not" : ""); |
| 325 | } |
| 326 | |
| 327 | flist->files = new_ptr; |
| 328 | |
| 329 | if (!flist->files) |
| 330 | out_of_memory("flist_expand"); |
| 331 | } |
| 332 | |
| 333 | void send_file_entry(struct file_struct *file, int f, unsigned short base_flags) |
| 334 | { |
| 335 | unsigned short flags; |
| 336 | static time_t modtime; |
| 337 | static mode_t mode; |
| 338 | static uint64 dev; |
| 339 | static dev_t rdev; |
| 340 | static uint32 rdev_major; |
| 341 | static uid_t uid; |
| 342 | static gid_t gid; |
| 343 | static char lastname[MAXPATHLEN]; |
| 344 | char fname[MAXPATHLEN]; |
| 345 | int l1, l2; |
| 346 | |
| 347 | if (f == -1) |
| 348 | return; |
| 349 | |
| 350 | if (!file) { |
| 351 | write_byte(f, 0); |
| 352 | modtime = 0, mode = 0; |
| 353 | dev = 0, rdev = makedev(0, 0); |
| 354 | rdev_major = 0; |
| 355 | uid = 0, gid = 0; |
| 356 | *lastname = '\0'; |
| 357 | return; |
| 358 | } |
| 359 | |
| 360 | io_write_phase = "send_file_entry"; |
| 361 | |
| 362 | f_name_to(file, fname); |
| 363 | |
| 364 | flags = base_flags; |
| 365 | |
| 366 | if (file->mode == mode) |
| 367 | flags |= XMIT_SAME_MODE; |
| 368 | else |
| 369 | mode = file->mode; |
| 370 | if (preserve_devices) { |
| 371 | if (protocol_version < 28) { |
| 372 | if (IS_DEVICE(mode)) { |
| 373 | if (file->u.rdev == rdev) |
| 374 | flags |= XMIT_SAME_RDEV_pre28; |
| 375 | else |
| 376 | rdev = file->u.rdev; |
| 377 | } else |
| 378 | rdev = makedev(0, 0); |
| 379 | } else if (IS_DEVICE(mode)) { |
| 380 | rdev = file->u.rdev; |
| 381 | if ((uint32)major(rdev) == rdev_major) |
| 382 | flags |= XMIT_SAME_RDEV_MAJOR; |
| 383 | else |
| 384 | rdev_major = major(rdev); |
| 385 | if ((uint32)minor(rdev) <= 0xFFu) |
| 386 | flags |= XMIT_RDEV_MINOR_IS_SMALL; |
| 387 | } |
| 388 | } |
| 389 | if (file->uid == uid) |
| 390 | flags |= XMIT_SAME_UID; |
| 391 | else |
| 392 | uid = file->uid; |
| 393 | if (file->gid == gid) |
| 394 | flags |= XMIT_SAME_GID; |
| 395 | else |
| 396 | gid = file->gid; |
| 397 | if (file->modtime == modtime) |
| 398 | flags |= XMIT_SAME_TIME; |
| 399 | else |
| 400 | modtime = file->modtime; |
| 401 | |
| 402 | #if SUPPORT_HARD_LINKS |
| 403 | if (file->link_u.idev) { |
| 404 | if (file->F_DEV == dev) { |
| 405 | if (protocol_version >= 28) |
| 406 | flags |= XMIT_SAME_DEV; |
| 407 | } else |
| 408 | dev = file->F_DEV; |
| 409 | flags |= XMIT_HAS_IDEV_DATA; |
| 410 | } |
| 411 | #endif |
| 412 | |
| 413 | for (l1 = 0; |
| 414 | lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255); |
| 415 | l1++) {} |
| 416 | l2 = strlen(fname+l1); |
| 417 | |
| 418 | if (l1 > 0) |
| 419 | flags |= XMIT_SAME_NAME; |
| 420 | if (l2 > 255) |
| 421 | flags |= XMIT_LONG_NAME; |
| 422 | |
| 423 | /* We must make sure we don't send a zero flag byte or the |
| 424 | * other end will terminate the flist transfer. Note that |
| 425 | * the use of XMIT_TOP_DIR on a non-dir has no meaning, so |
| 426 | * it's harmless way to add a bit to the first flag byte. */ |
| 427 | if (protocol_version >= 28) { |
| 428 | if (!flags && !S_ISDIR(mode)) |
| 429 | flags |= XMIT_TOP_DIR; |
| 430 | if ((flags & 0xFF00) || !flags) { |
| 431 | flags |= XMIT_EXTENDED_FLAGS; |
| 432 | write_byte(f, flags); |
| 433 | write_byte(f, flags >> 8); |
| 434 | } else |
| 435 | write_byte(f, flags); |
| 436 | } else { |
| 437 | if (!(flags & 0xFF) && !S_ISDIR(mode)) |
| 438 | flags |= XMIT_TOP_DIR; |
| 439 | if (!(flags & 0xFF)) |
| 440 | flags |= XMIT_LONG_NAME; |
| 441 | write_byte(f, flags); |
| 442 | } |
| 443 | if (flags & XMIT_SAME_NAME) |
| 444 | write_byte(f, l1); |
| 445 | if (flags & XMIT_LONG_NAME) |
| 446 | write_int(f, l2); |
| 447 | else |
| 448 | write_byte(f, l2); |
| 449 | write_buf(f, fname + l1, l2); |
| 450 | |
| 451 | write_longint(f, file->length); |
| 452 | if (!(flags & XMIT_SAME_TIME)) |
| 453 | write_int(f, modtime); |
| 454 | if (!(flags & XMIT_SAME_MODE)) |
| 455 | write_int(f, to_wire_mode(mode)); |
| 456 | if (preserve_uid && !(flags & XMIT_SAME_UID)) { |
| 457 | if (!numeric_ids) |
| 458 | add_uid(uid); |
| 459 | write_int(f, uid); |
| 460 | } |
| 461 | if (preserve_gid && !(flags & XMIT_SAME_GID)) { |
| 462 | if (!numeric_ids) |
| 463 | add_gid(gid); |
| 464 | write_int(f, gid); |
| 465 | } |
| 466 | if (preserve_devices && IS_DEVICE(mode)) { |
| 467 | if (protocol_version < 28) { |
| 468 | if (!(flags & XMIT_SAME_RDEV_pre28)) |
| 469 | write_int(f, (int)rdev); |
| 470 | } else { |
| 471 | if (!(flags & XMIT_SAME_RDEV_MAJOR)) |
| 472 | write_int(f, major(rdev)); |
| 473 | if (flags & XMIT_RDEV_MINOR_IS_SMALL) |
| 474 | write_byte(f, minor(rdev)); |
| 475 | else |
| 476 | write_int(f, minor(rdev)); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | #if SUPPORT_LINKS |
| 481 | if (preserve_links && S_ISLNK(mode)) { |
| 482 | int len = strlen(file->u.link); |
| 483 | write_int(f, len); |
| 484 | write_buf(f, file->u.link, len); |
| 485 | } |
| 486 | #endif |
| 487 | |
| 488 | #if SUPPORT_HARD_LINKS |
| 489 | if (flags & XMIT_HAS_IDEV_DATA) { |
| 490 | if (protocol_version < 26) { |
| 491 | /* 32-bit dev_t and ino_t */ |
| 492 | write_int(f, dev); |
| 493 | write_int(f, file->F_INODE); |
| 494 | } else { |
| 495 | /* 64-bit dev_t and ino_t */ |
| 496 | if (!(flags & XMIT_SAME_DEV)) |
| 497 | write_longint(f, dev); |
| 498 | write_longint(f, file->F_INODE); |
| 499 | } |
| 500 | } |
| 501 | #endif |
| 502 | |
| 503 | if (always_checksum) { |
| 504 | char *sum; |
| 505 | if (S_ISREG(mode)) |
| 506 | sum = file->u.sum; |
| 507 | else if (protocol_version < 28) { |
| 508 | /* Prior to 28, we sent a useless set of nulls. */ |
| 509 | sum = empty_sum; |
| 510 | } else |
| 511 | sum = NULL; |
| 512 | if (sum) { |
| 513 | write_buf(f, sum, |
| 514 | protocol_version < 21 ? 2 : MD4_SUM_LENGTH); |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | strlcpy(lastname, fname, MAXPATHLEN); |
| 519 | |
| 520 | io_write_phase = "unknown"; |
| 521 | } |
| 522 | |
| 523 | |
| 524 | |
| 525 | void receive_file_entry(struct file_struct **fptr, unsigned short flags, |
| 526 | struct file_list *flist, int f) |
| 527 | { |
| 528 | static time_t modtime; |
| 529 | static mode_t mode; |
| 530 | static uint64 dev; |
| 531 | static dev_t rdev; |
| 532 | static uint32 rdev_major; |
| 533 | static uid_t uid; |
| 534 | static gid_t gid; |
| 535 | static char lastname[MAXPATHLEN], *lastdir; |
| 536 | static int lastdir_depth, lastdir_len = -1; |
| 537 | char thisname[MAXPATHLEN]; |
| 538 | unsigned int l1 = 0, l2 = 0; |
| 539 | int alloc_len, basename_len, dirname_len, linkname_len, sum_len; |
| 540 | OFF_T file_length; |
| 541 | char *basename, *dirname, *bp; |
| 542 | struct file_struct *file; |
| 543 | |
| 544 | if (!fptr) { |
| 545 | modtime = 0, mode = 0; |
| 546 | dev = 0, rdev = makedev(0, 0); |
| 547 | rdev_major = 0; |
| 548 | uid = 0, gid = 0; |
| 549 | *lastname = '\0'; |
| 550 | lastdir_len = -1; |
| 551 | return; |
| 552 | } |
| 553 | |
| 554 | if (flags & XMIT_SAME_NAME) |
| 555 | l1 = read_byte(f); |
| 556 | |
| 557 | if (flags & XMIT_LONG_NAME) |
| 558 | l2 = read_int(f); |
| 559 | else |
| 560 | l2 = read_byte(f); |
| 561 | |
| 562 | if (l2 >= MAXPATHLEN - l1) { |
| 563 | rprintf(FERROR, |
| 564 | "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n", |
| 565 | flags, l1, l2, lastname); |
| 566 | overflow("receive_file_entry"); |
| 567 | } |
| 568 | |
| 569 | strlcpy(thisname, lastname, l1 + 1); |
| 570 | read_sbuf(f, &thisname[l1], l2); |
| 571 | thisname[l1 + l2] = 0; |
| 572 | |
| 573 | strlcpy(lastname, thisname, MAXPATHLEN); |
| 574 | |
| 575 | clean_fname(thisname, 0); |
| 576 | |
| 577 | if (sanitize_paths) |
| 578 | sanitize_path(thisname, thisname, "", 0); |
| 579 | |
| 580 | if ((basename = strrchr(thisname, '/')) != NULL) { |
| 581 | dirname_len = ++basename - thisname; /* counts future '\0' */ |
| 582 | if (lastdir_len == dirname_len - 1 |
| 583 | && strncmp(thisname, lastdir, lastdir_len) == 0) { |
| 584 | dirname = lastdir; |
| 585 | dirname_len = 0; /* indicates no copy is needed */ |
| 586 | } else |
| 587 | dirname = thisname; |
| 588 | } else { |
| 589 | basename = thisname; |
| 590 | dirname = NULL; |
| 591 | dirname_len = 0; |
| 592 | } |
| 593 | basename_len = strlen(basename) + 1; /* count the '\0' */ |
| 594 | |
| 595 | file_length = read_longint(f); |
| 596 | if (!(flags & XMIT_SAME_TIME)) |
| 597 | modtime = (time_t)read_int(f); |
| 598 | if (!(flags & XMIT_SAME_MODE)) |
| 599 | mode = from_wire_mode(read_int(f)); |
| 600 | |
| 601 | if (preserve_uid && !(flags & XMIT_SAME_UID)) |
| 602 | uid = (uid_t)read_int(f); |
| 603 | if (preserve_gid && !(flags & XMIT_SAME_GID)) |
| 604 | gid = (gid_t)read_int(f); |
| 605 | |
| 606 | if (preserve_devices) { |
| 607 | if (protocol_version < 28) { |
| 608 | if (IS_DEVICE(mode)) { |
| 609 | if (!(flags & XMIT_SAME_RDEV_pre28)) |
| 610 | rdev = (dev_t)read_int(f); |
| 611 | } else |
| 612 | rdev = makedev(0, 0); |
| 613 | } else if (IS_DEVICE(mode)) { |
| 614 | uint32 rdev_minor; |
| 615 | if (!(flags & XMIT_SAME_RDEV_MAJOR)) |
| 616 | rdev_major = read_int(f); |
| 617 | if (flags & XMIT_RDEV_MINOR_IS_SMALL) |
| 618 | rdev_minor = read_byte(f); |
| 619 | else |
| 620 | rdev_minor = read_int(f); |
| 621 | rdev = makedev(rdev_major, rdev_minor); |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | #if SUPPORT_LINKS |
| 626 | if (preserve_links && S_ISLNK(mode)) { |
| 627 | linkname_len = read_int(f) + 1; /* count the '\0' */ |
| 628 | if (linkname_len <= 0 || linkname_len > MAXPATHLEN) { |
| 629 | rprintf(FERROR, "overflow: linkname_len=%d\n", |
| 630 | linkname_len - 1); |
| 631 | overflow("receive_file_entry"); |
| 632 | } |
| 633 | } |
| 634 | else |
| 635 | #endif |
| 636 | linkname_len = 0; |
| 637 | |
| 638 | sum_len = always_checksum && S_ISREG(mode) ? MD4_SUM_LENGTH : 0; |
| 639 | |
| 640 | alloc_len = file_struct_len + dirname_len + basename_len |
| 641 | + linkname_len + sum_len; |
| 642 | bp = pool_alloc(flist->file_pool, alloc_len, "receive_file_entry"); |
| 643 | |
| 644 | file = *fptr = (struct file_struct *)bp; |
| 645 | memset(bp, 0, file_struct_len); |
| 646 | bp += file_struct_len; |
| 647 | |
| 648 | file->flags = flags & XMIT_TOP_DIR ? FLAG_TOP_DIR : 0; |
| 649 | file->modtime = modtime; |
| 650 | file->length = file_length; |
| 651 | file->mode = mode; |
| 652 | file->uid = uid; |
| 653 | file->gid = gid; |
| 654 | |
| 655 | if (dirname_len) { |
| 656 | file->dirname = lastdir = bp; |
| 657 | lastdir_len = dirname_len - 1; |
| 658 | memcpy(bp, dirname, dirname_len - 1); |
| 659 | bp += dirname_len; |
| 660 | bp[-1] = '\0'; |
| 661 | if (sanitize_paths) |
| 662 | lastdir_depth = count_dir_elements(lastdir); |
| 663 | } else if (dirname) |
| 664 | file->dirname = dirname; |
| 665 | |
| 666 | file->basename = bp; |
| 667 | memcpy(bp, basename, basename_len); |
| 668 | bp += basename_len; |
| 669 | |
| 670 | if (preserve_devices && IS_DEVICE(mode)) |
| 671 | file->u.rdev = rdev; |
| 672 | |
| 673 | #if SUPPORT_LINKS |
| 674 | if (linkname_len) { |
| 675 | file->u.link = bp; |
| 676 | read_sbuf(f, bp, linkname_len - 1); |
| 677 | if (sanitize_paths) |
| 678 | sanitize_path(bp, bp, "", lastdir_depth); |
| 679 | bp += linkname_len; |
| 680 | } |
| 681 | #endif |
| 682 | |
| 683 | #if SUPPORT_HARD_LINKS |
| 684 | if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode)) |
| 685 | flags |= XMIT_HAS_IDEV_DATA; |
| 686 | if (flags & XMIT_HAS_IDEV_DATA) { |
| 687 | uint64 inode; |
| 688 | if (protocol_version < 26) { |
| 689 | dev = read_int(f); |
| 690 | inode = read_int(f); |
| 691 | } else { |
| 692 | if (!(flags & XMIT_SAME_DEV)) |
| 693 | dev = read_longint(f); |
| 694 | inode = read_longint(f); |
| 695 | } |
| 696 | if (flist->hlink_pool) { |
| 697 | file->link_u.idev = pool_talloc(flist->hlink_pool, |
| 698 | struct idev, 1, "inode_table"); |
| 699 | file->F_INODE = inode; |
| 700 | file->F_DEV = dev; |
| 701 | } |
| 702 | } |
| 703 | #endif |
| 704 | |
| 705 | if (always_checksum) { |
| 706 | char *sum; |
| 707 | if (sum_len) { |
| 708 | file->u.sum = sum = bp; |
| 709 | /*bp += sum_len;*/ |
| 710 | } else if (protocol_version < 28) { |
| 711 | /* Prior to 28, we get a useless set of nulls. */ |
| 712 | sum = empty_sum; |
| 713 | } else |
| 714 | sum = NULL; |
| 715 | if (sum) { |
| 716 | read_buf(f, sum, |
| 717 | protocol_version < 21 ? 2 : MD4_SUM_LENGTH); |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | if (!preserve_perms) { |
| 722 | /* set an appropriate set of permissions based on original |
| 723 | * permissions and umask. This emulates what GNU cp does */ |
| 724 | file->mode &= ~orig_umask; |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | |
| 729 | /** |
| 730 | * Create a file_struct for a named file by reading its stat() |
| 731 | * information and performing extensive checks against global |
| 732 | * options. |
| 733 | * |
| 734 | * @return the new file, or NULL if there was an error or this file |
| 735 | * should be excluded. |
| 736 | * |
| 737 | * @todo There is a small optimization opportunity here to avoid |
| 738 | * stat()ing the file in some circumstances, which has a certain cost. |
| 739 | * We are called immediately after doing readdir(), and so we may |
| 740 | * already know the d_type of the file. We could for example avoid |
| 741 | * statting directories if we're not recursing, but this is not a very |
| 742 | * important case. Some systems may not have d_type. |
| 743 | **/ |
| 744 | struct file_struct *make_file(char *fname, struct file_list *flist, |
| 745 | int exclude_level) |
| 746 | { |
| 747 | static char *lastdir; |
| 748 | static int lastdir_len = -1; |
| 749 | struct file_struct *file; |
| 750 | STRUCT_STAT st; |
| 751 | char sum[SUM_LENGTH]; |
| 752 | char thisname[MAXPATHLEN]; |
| 753 | char linkname[MAXPATHLEN]; |
| 754 | int alloc_len, basename_len, dirname_len, linkname_len, sum_len; |
| 755 | char *basename, *dirname, *bp; |
| 756 | unsigned short flags = 0; |
| 757 | |
| 758 | if (!flist || !flist->count) /* Ignore lastdir when invalid. */ |
| 759 | lastdir_len = -1; |
| 760 | |
| 761 | if (strlcpy(thisname, fname, sizeof thisname) |
| 762 | >= sizeof thisname - flist_dir_len) { |
| 763 | rprintf(FINFO, "skipping overly long name: %s\n", fname); |
| 764 | return NULL; |
| 765 | } |
| 766 | clean_fname(thisname, 0); |
| 767 | if (sanitize_paths) |
| 768 | sanitize_path(thisname, thisname, "", 0); |
| 769 | |
| 770 | memset(sum, 0, SUM_LENGTH); |
| 771 | |
| 772 | if (readlink_stat(thisname, &st, linkname) != 0) { |
| 773 | int save_errno = errno; |
| 774 | /* See if file is excluded before reporting an error. */ |
| 775 | if (exclude_level != NO_EXCLUDES |
| 776 | && check_exclude_file(thisname, 0, exclude_level)) |
| 777 | return NULL; |
| 778 | if (save_errno == ENOENT) { |
| 779 | #if SUPPORT_LINKS |
| 780 | /* Avoid "vanished" error if symlink points nowhere. */ |
| 781 | if (copy_links && do_lstat(thisname, &st) == 0 |
| 782 | && S_ISLNK(st.st_mode)) { |
| 783 | io_error |= IOERR_GENERAL; |
| 784 | rprintf(FERROR, "symlink has no referent: %s\n", |
| 785 | full_fname(thisname)); |
| 786 | } else |
| 787 | #endif |
| 788 | { |
| 789 | enum logcode c = am_daemon && protocol_version < 28 |
| 790 | ? FERROR : FINFO; |
| 791 | io_error |= IOERR_VANISHED; |
| 792 | rprintf(c, "file has vanished: %s\n", |
| 793 | full_fname(thisname)); |
| 794 | } |
| 795 | } else { |
| 796 | io_error |= IOERR_GENERAL; |
| 797 | rsyserr(FERROR, save_errno, "readlink %s failed", |
| 798 | full_fname(thisname)); |
| 799 | } |
| 800 | return NULL; |
| 801 | } |
| 802 | |
| 803 | /* backup.c calls us with exclude_level set to NO_EXCLUDES. */ |
| 804 | if (exclude_level == NO_EXCLUDES) |
| 805 | goto skip_excludes; |
| 806 | |
| 807 | if (S_ISDIR(st.st_mode) && !recurse && !files_from) { |
| 808 | rprintf(FINFO, "skipping directory %s\n", thisname); |
| 809 | return NULL; |
| 810 | } |
| 811 | |
| 812 | /* We only care about directories because we need to avoid recursing |
| 813 | * into a mount-point directory, not to avoid copying a symlinked |
| 814 | * file if -L (or similar) was specified. */ |
| 815 | if (one_file_system && st.st_dev != filesystem_dev |
| 816 | && S_ISDIR(st.st_mode)) |
| 817 | flags |= FLAG_MOUNT_POINT; |
| 818 | |
| 819 | if (check_exclude_file(thisname, S_ISDIR(st.st_mode) != 0, exclude_level)) |
| 820 | return NULL; |
| 821 | |
| 822 | if (lp_ignore_nonreadable(module_id) && access(thisname, R_OK) != 0) |
| 823 | return NULL; |
| 824 | |
| 825 | skip_excludes: |
| 826 | |
| 827 | if (verbose > 2) { |
| 828 | rprintf(FINFO, "[%s] make_file(%s,*,%d)\n", |
| 829 | who_am_i(), thisname, exclude_level); |
| 830 | } |
| 831 | |
| 832 | if ((basename = strrchr(thisname, '/')) != NULL) { |
| 833 | dirname_len = ++basename - thisname; /* counts future '\0' */ |
| 834 | if (lastdir_len == dirname_len - 1 |
| 835 | && strncmp(thisname, lastdir, lastdir_len) == 0) { |
| 836 | dirname = lastdir; |
| 837 | dirname_len = 0; /* indicates no copy is needed */ |
| 838 | } else |
| 839 | dirname = thisname; |
| 840 | } else { |
| 841 | basename = thisname; |
| 842 | dirname = NULL; |
| 843 | dirname_len = 0; |
| 844 | } |
| 845 | basename_len = strlen(basename) + 1; /* count the '\0' */ |
| 846 | |
| 847 | #if SUPPORT_LINKS |
| 848 | linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0; |
| 849 | #else |
| 850 | linkname_len = 0; |
| 851 | #endif |
| 852 | |
| 853 | sum_len = always_checksum && S_ISREG(st.st_mode) ? MD4_SUM_LENGTH : 0; |
| 854 | |
| 855 | alloc_len = file_struct_len + dirname_len + basename_len |
| 856 | + linkname_len + sum_len; |
| 857 | if (flist) { |
| 858 | bp = pool_alloc(flist->file_pool, alloc_len, |
| 859 | "receive_file_entry"); |
| 860 | } else { |
| 861 | if (!(bp = new_array(char, alloc_len))) |
| 862 | out_of_memory("receive_file_entry"); |
| 863 | } |
| 864 | |
| 865 | file = (struct file_struct *)bp; |
| 866 | memset(bp, 0, file_struct_len); |
| 867 | bp += file_struct_len; |
| 868 | |
| 869 | file->flags = flags; |
| 870 | file->modtime = st.st_mtime; |
| 871 | file->length = st.st_size; |
| 872 | file->mode = st.st_mode; |
| 873 | file->uid = st.st_uid; |
| 874 | file->gid = st.st_gid; |
| 875 | |
| 876 | #if SUPPORT_HARD_LINKS |
| 877 | if (flist && flist->hlink_pool) { |
| 878 | if (protocol_version < 28) { |
| 879 | if (S_ISREG(st.st_mode)) |
| 880 | file->link_u.idev = pool_talloc( |
| 881 | flist->hlink_pool, struct idev, 1, |
| 882 | "inode_table"); |
| 883 | } else { |
| 884 | if (!S_ISDIR(st.st_mode) && st.st_nlink > 1) |
| 885 | file->link_u.idev = pool_talloc( |
| 886 | flist->hlink_pool, struct idev, 1, |
| 887 | "inode_table"); |
| 888 | } |
| 889 | } |
| 890 | if (file->link_u.idev) { |
| 891 | file->F_DEV = st.st_dev; |
| 892 | file->F_INODE = st.st_ino; |
| 893 | } |
| 894 | #endif |
| 895 | |
| 896 | if (dirname_len) { |
| 897 | file->dirname = lastdir = bp; |
| 898 | lastdir_len = dirname_len - 1; |
| 899 | memcpy(bp, dirname, dirname_len - 1); |
| 900 | bp += dirname_len; |
| 901 | bp[-1] = '\0'; |
| 902 | } else if (dirname) |
| 903 | file->dirname = dirname; |
| 904 | |
| 905 | file->basename = bp; |
| 906 | memcpy(bp, basename, basename_len); |
| 907 | bp += basename_len; |
| 908 | |
| 909 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
| 910 | if (preserve_devices && IS_DEVICE(st.st_mode)) |
| 911 | file->u.rdev = st.st_rdev; |
| 912 | #endif |
| 913 | |
| 914 | #if SUPPORT_LINKS |
| 915 | if (linkname_len) { |
| 916 | file->u.link = bp; |
| 917 | memcpy(bp, linkname, linkname_len); |
| 918 | bp += linkname_len; |
| 919 | } |
| 920 | #endif |
| 921 | |
| 922 | if (sum_len) { |
| 923 | file->u.sum = bp; |
| 924 | file_checksum(thisname, bp, st.st_size); |
| 925 | /*bp += sum_len;*/ |
| 926 | } |
| 927 | |
| 928 | file->basedir = flist_dir; |
| 929 | |
| 930 | /* This code is only used by the receiver when it is building |
| 931 | * a list of files for a delete pass. */ |
| 932 | if (keep_dirlinks && linkname_len && flist) { |
| 933 | STRUCT_STAT st2; |
| 934 | int i = flist_find(received_flist, file); |
| 935 | if (i >= 0 && S_ISDIR(received_flist->files[i]->mode) |
| 936 | && do_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) { |
| 937 | file->modtime = st2.st_mtime; |
| 938 | file->length = st2.st_size; |
| 939 | file->mode = st2.st_mode; |
| 940 | file->uid = st2.st_uid; |
| 941 | file->gid = st2.st_gid; |
| 942 | file->u.link = NULL; |
| 943 | if (file->link_u.idev) { |
| 944 | pool_free(flist->hlink_pool, 0, file->link_u.idev); |
| 945 | file->link_u.idev = NULL; |
| 946 | } |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | if (!S_ISDIR(st.st_mode)) |
| 951 | stats.total_size += st.st_size; |
| 952 | |
| 953 | return file; |
| 954 | } |
| 955 | |
| 956 | |
| 957 | void send_file_name(int f, struct file_list *flist, char *fname, |
| 958 | int recursive, unsigned short base_flags) |
| 959 | { |
| 960 | struct file_struct *file; |
| 961 | char fbuf[MAXPATHLEN]; |
| 962 | |
| 963 | /* f is set to -1 when calculating deletion file list */ |
| 964 | file = make_file(fname, flist, |
| 965 | f == -1 && delete_excluded? SERVER_EXCLUDES : ALL_EXCLUDES); |
| 966 | |
| 967 | if (!file) |
| 968 | return; |
| 969 | |
| 970 | maybe_emit_filelist_progress(flist); |
| 971 | |
| 972 | flist_expand(flist); |
| 973 | |
| 974 | if (file->basename[0]) { |
| 975 | flist->files[flist->count++] = file; |
| 976 | send_file_entry(file, f, base_flags); |
| 977 | } |
| 978 | |
| 979 | if (recursive && S_ISDIR(file->mode) |
| 980 | && !(file->flags & FLAG_MOUNT_POINT)) { |
| 981 | struct exclude_list_struct last_list = local_exclude_list; |
| 982 | local_exclude_list.head = local_exclude_list.tail = NULL; |
| 983 | send_directory(f, flist, f_name_to(file, fbuf)); |
| 984 | if (verbose > 2) { |
| 985 | rprintf(FINFO, "[%s] popping %sexclude list\n", |
| 986 | who_am_i(), local_exclude_list.debug_type); |
| 987 | } |
| 988 | clear_exclude_list(&local_exclude_list); |
| 989 | local_exclude_list = last_list; |
| 990 | } |
| 991 | } |
| 992 | |
| 993 | |
| 994 | static void send_directory(int f, struct file_list *flist, char *dir) |
| 995 | { |
| 996 | DIR *d; |
| 997 | struct dirent *di; |
| 998 | char fname[MAXPATHLEN]; |
| 999 | unsigned int offset; |
| 1000 | char *p; |
| 1001 | |
| 1002 | d = opendir(dir); |
| 1003 | if (!d) { |
| 1004 | io_error |= IOERR_GENERAL; |
| 1005 | rsyserr(FERROR, errno, "opendir %s failed", full_fname(dir)); |
| 1006 | return; |
| 1007 | } |
| 1008 | |
| 1009 | offset = strlcpy(fname, dir, MAXPATHLEN); |
| 1010 | p = fname + offset; |
| 1011 | if (offset >= MAXPATHLEN || p[-1] != '/') { |
| 1012 | if (offset >= MAXPATHLEN - 1) { |
| 1013 | io_error |= IOERR_GENERAL; |
| 1014 | rprintf(FERROR, "skipping long-named directory: %s\n", |
| 1015 | full_fname(fname)); |
| 1016 | closedir(d); |
| 1017 | return; |
| 1018 | } |
| 1019 | *p++ = '/'; |
| 1020 | offset++; |
| 1021 | } |
| 1022 | |
| 1023 | if (cvs_exclude) { |
| 1024 | if (strlcpy(p, ".cvsignore", MAXPATHLEN - offset) |
| 1025 | < MAXPATHLEN - offset) { |
| 1026 | add_exclude_file(&local_exclude_list, fname, |
| 1027 | XFLG_WORD_SPLIT | XFLG_WORDS_ONLY); |
| 1028 | } else { |
| 1029 | io_error |= IOERR_GENERAL; |
| 1030 | rprintf(FINFO, |
| 1031 | "cannot cvs-exclude in long-named directory %s\n", |
| 1032 | full_fname(fname)); |
| 1033 | } |
| 1034 | } |
| 1035 | |
| 1036 | for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) { |
| 1037 | char *dname = d_name(di); |
| 1038 | if (dname[0] == '.' && (dname[1] == '\0' |
| 1039 | || (dname[1] == '.' && dname[2] == '\0'))) |
| 1040 | continue; |
| 1041 | if (strlcpy(p, dname, MAXPATHLEN - offset) < MAXPATHLEN - offset) |
| 1042 | send_file_name(f, flist, fname, recurse, 0); |
| 1043 | else { |
| 1044 | io_error |= IOERR_GENERAL; |
| 1045 | rprintf(FINFO, |
| 1046 | "cannot send long-named file %s\n", |
| 1047 | full_fname(fname)); |
| 1048 | } |
| 1049 | } |
| 1050 | if (errno) { |
| 1051 | io_error |= IOERR_GENERAL; |
| 1052 | rsyserr(FERROR, errno, "readdir(%s)", dir); |
| 1053 | } |
| 1054 | |
| 1055 | closedir(d); |
| 1056 | } |
| 1057 | |
| 1058 | |
| 1059 | /** |
| 1060 | * This function is normally called by the sender, but the receiver also |
| 1061 | * uses it to construct its own file list if --delete has been specified. |
| 1062 | * The delete_files() function in receiver.c sets f to -1 so that we just |
| 1063 | * construct the file list in memory without sending it over the wire. It |
| 1064 | * also has the side-effect of ignoring user-excludes if delete_excluded |
| 1065 | * is set (so that the delete list includes user-excluded files). |
| 1066 | **/ |
| 1067 | struct file_list *send_file_list(int f, int argc, char *argv[]) |
| 1068 | { |
| 1069 | int l; |
| 1070 | STRUCT_STAT st; |
| 1071 | char *p, *dir, olddir[sizeof curr_dir]; |
| 1072 | char lastpath[MAXPATHLEN] = ""; |
| 1073 | struct file_list *flist; |
| 1074 | int64 start_write; |
| 1075 | int use_ff_fd = 0; |
| 1076 | |
| 1077 | if (show_filelist_p() && f != -1) |
| 1078 | start_filelist_progress("building file list"); |
| 1079 | |
| 1080 | start_write = stats.total_written; |
| 1081 | |
| 1082 | flist = flist_new(f == -1 ? WITHOUT_HLINK : WITH_HLINK, |
| 1083 | "send_file_list"); |
| 1084 | |
| 1085 | if (f != -1) { |
| 1086 | io_start_buffering_out(); |
| 1087 | if (filesfrom_fd >= 0) { |
| 1088 | if (argv[0] && !push_dir(argv[0])) { |
| 1089 | rsyserr(FERROR, errno, "push_dir %s failed", |
| 1090 | full_fname(argv[0])); |
| 1091 | exit_cleanup(RERR_FILESELECT); |
| 1092 | } |
| 1093 | use_ff_fd = 1; |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | while (1) { |
| 1098 | char fname2[MAXPATHLEN]; |
| 1099 | char *fname = fname2; |
| 1100 | |
| 1101 | if (use_ff_fd) { |
| 1102 | if (read_filesfrom_line(filesfrom_fd, fname) == 0) |
| 1103 | break; |
| 1104 | sanitize_path(fname, fname, "", 0); |
| 1105 | } else { |
| 1106 | if (argc-- == 0) |
| 1107 | break; |
| 1108 | strlcpy(fname, *argv++, MAXPATHLEN); |
| 1109 | if (sanitize_paths) |
| 1110 | sanitize_path(fname, fname, "", 0); |
| 1111 | } |
| 1112 | |
| 1113 | l = strlen(fname); |
| 1114 | if (fname[l - 1] == '/') { |
| 1115 | if (l == 2 && fname[0] == '.') { |
| 1116 | /* Turn "./" into just "." rather than "./." */ |
| 1117 | fname[1] = '\0'; |
| 1118 | } else if (l < MAXPATHLEN) { |
| 1119 | fname[l++] = '.'; |
| 1120 | fname[l] = '\0'; |
| 1121 | } |
| 1122 | } |
| 1123 | |
| 1124 | if (link_stat(fname, &st, keep_dirlinks) != 0) { |
| 1125 | if (f != -1) { |
| 1126 | io_error |= IOERR_GENERAL; |
| 1127 | rsyserr(FERROR, errno, "link_stat %s failed", |
| 1128 | full_fname(fname)); |
| 1129 | } |
| 1130 | continue; |
| 1131 | } |
| 1132 | |
| 1133 | if (S_ISDIR(st.st_mode) && !recurse && !files_from) { |
| 1134 | rprintf(FINFO, "skipping directory %s\n", fname); |
| 1135 | continue; |
| 1136 | } |
| 1137 | |
| 1138 | dir = NULL; |
| 1139 | olddir[0] = '\0'; |
| 1140 | |
| 1141 | if (!relative_paths) { |
| 1142 | p = strrchr(fname, '/'); |
| 1143 | if (p) { |
| 1144 | *p = 0; |
| 1145 | if (p == fname) |
| 1146 | dir = "/"; |
| 1147 | else |
| 1148 | dir = fname; |
| 1149 | fname = p + 1; |
| 1150 | } |
| 1151 | } else if (f != -1 && implied_dirs && (p=strrchr(fname,'/')) && p != fname) { |
| 1152 | /* this ensures we send the intermediate directories, |
| 1153 | thus getting their permissions right */ |
| 1154 | char *lp = lastpath, *fn = fname, *slash = fname; |
| 1155 | *p = 0; |
| 1156 | /* Skip any initial directories in our path that we |
| 1157 | * have in common with lastpath. */ |
| 1158 | while (*fn && *lp == *fn) { |
| 1159 | if (*fn == '/') |
| 1160 | slash = fn; |
| 1161 | lp++, fn++; |
| 1162 | } |
| 1163 | *p = '/'; |
| 1164 | if (fn != p || (*lp && *lp != '/')) { |
| 1165 | int copy_links_saved = copy_links; |
| 1166 | int recurse_saved = recurse; |
| 1167 | copy_links = copy_unsafe_links; |
| 1168 | /* set recurse to 1 to prevent make_file |
| 1169 | * from ignoring directory, but still |
| 1170 | * turn off the recursive parameter to |
| 1171 | * send_file_name */ |
| 1172 | recurse = 1; |
| 1173 | while ((slash = strchr(slash+1, '/')) != 0) { |
| 1174 | *slash = 0; |
| 1175 | send_file_name(f, flist, fname, 0, 0); |
| 1176 | *slash = '/'; |
| 1177 | } |
| 1178 | copy_links = copy_links_saved; |
| 1179 | recurse = recurse_saved; |
| 1180 | *p = 0; |
| 1181 | strlcpy(lastpath, fname, sizeof lastpath); |
| 1182 | *p = '/'; |
| 1183 | } |
| 1184 | } |
| 1185 | |
| 1186 | if (!*fname) |
| 1187 | fname = "."; |
| 1188 | |
| 1189 | if (dir && *dir) { |
| 1190 | static char *lastdir; |
| 1191 | static int lastdir_len; |
| 1192 | |
| 1193 | strcpy(olddir, curr_dir); /* can't overflow */ |
| 1194 | |
| 1195 | if (!push_dir(dir)) { |
| 1196 | io_error |= IOERR_GENERAL; |
| 1197 | rsyserr(FERROR, errno, "push_dir %s failed", |
| 1198 | full_fname(dir)); |
| 1199 | continue; |
| 1200 | } |
| 1201 | |
| 1202 | if (lastdir && strcmp(lastdir, dir) == 0) { |
| 1203 | flist_dir = lastdir; |
| 1204 | flist_dir_len = lastdir_len; |
| 1205 | } else { |
| 1206 | flist_dir = lastdir = strdup(dir); |
| 1207 | flist_dir_len = lastdir_len = strlen(dir); |
| 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | if (one_file_system) |
| 1212 | set_filesystem(fname); |
| 1213 | |
| 1214 | send_file_name(f, flist, fname, recurse, XMIT_TOP_DIR); |
| 1215 | |
| 1216 | if (olddir[0]) { |
| 1217 | flist_dir = NULL; |
| 1218 | flist_dir_len = 0; |
| 1219 | if (!pop_dir(olddir)) { |
| 1220 | rsyserr(FERROR, errno, "pop_dir %s failed", |
| 1221 | full_fname(dir)); |
| 1222 | exit_cleanup(RERR_FILESELECT); |
| 1223 | } |
| 1224 | } |
| 1225 | } |
| 1226 | |
| 1227 | if (f != -1) { |
| 1228 | send_file_entry(NULL, f, 0); |
| 1229 | |
| 1230 | if (show_filelist_p()) |
| 1231 | finish_filelist_progress(flist); |
| 1232 | } |
| 1233 | |
| 1234 | if (flist->hlink_pool) { |
| 1235 | pool_destroy(flist->hlink_pool); |
| 1236 | flist->hlink_pool = NULL; |
| 1237 | } |
| 1238 | |
| 1239 | clean_flist(flist, 0, 0); |
| 1240 | |
| 1241 | if (f != -1) { |
| 1242 | /* Now send the uid/gid list. This was introduced in |
| 1243 | * protocol version 15 */ |
| 1244 | send_uid_list(f); |
| 1245 | |
| 1246 | /* send the io_error flag */ |
| 1247 | write_int(f, lp_ignore_errors(module_id) ? 0 : io_error); |
| 1248 | |
| 1249 | io_end_buffering(); |
| 1250 | stats.flist_size = stats.total_written - start_write; |
| 1251 | stats.num_files = flist->count; |
| 1252 | } |
| 1253 | |
| 1254 | if (verbose > 3) |
| 1255 | output_flist(flist); |
| 1256 | |
| 1257 | if (verbose > 2) |
| 1258 | rprintf(FINFO, "send_file_list done\n"); |
| 1259 | |
| 1260 | return flist; |
| 1261 | } |
| 1262 | |
| 1263 | |
| 1264 | struct file_list *recv_file_list(int f) |
| 1265 | { |
| 1266 | struct file_list *flist; |
| 1267 | unsigned short flags; |
| 1268 | int64 start_read; |
| 1269 | |
| 1270 | if (show_filelist_p()) |
| 1271 | start_filelist_progress("receiving file list"); |
| 1272 | |
| 1273 | start_read = stats.total_read; |
| 1274 | |
| 1275 | flist = flist_new(WITH_HLINK, "recv_file_list"); |
| 1276 | received_flist = flist; |
| 1277 | |
| 1278 | flist->count = 0; |
| 1279 | flist->malloced = 1000; |
| 1280 | flist->files = new_array(struct file_struct *, flist->malloced); |
| 1281 | if (!flist->files) |
| 1282 | goto oom; |
| 1283 | |
| 1284 | |
| 1285 | while ((flags = read_byte(f)) != 0) { |
| 1286 | int i = flist->count; |
| 1287 | |
| 1288 | flist_expand(flist); |
| 1289 | |
| 1290 | if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS)) |
| 1291 | flags |= read_byte(f) << 8; |
| 1292 | receive_file_entry(&flist->files[i], flags, flist, f); |
| 1293 | |
| 1294 | if (S_ISREG(flist->files[i]->mode)) |
| 1295 | stats.total_size += flist->files[i]->length; |
| 1296 | |
| 1297 | flist->count++; |
| 1298 | |
| 1299 | maybe_emit_filelist_progress(flist); |
| 1300 | |
| 1301 | if (verbose > 2) { |
| 1302 | rprintf(FINFO, "recv_file_name(%s)\n", |
| 1303 | f_name(flist->files[i])); |
| 1304 | } |
| 1305 | } |
| 1306 | receive_file_entry(NULL, 0, NULL, 0); /* Signal that we're done. */ |
| 1307 | |
| 1308 | if (verbose > 2) |
| 1309 | rprintf(FINFO, "received %d names\n", flist->count); |
| 1310 | |
| 1311 | if (show_filelist_p()) |
| 1312 | finish_filelist_progress(flist); |
| 1313 | |
| 1314 | clean_flist(flist, relative_paths, 1); |
| 1315 | |
| 1316 | if (f != -1) { |
| 1317 | /* Now send the uid/gid list. This was introduced in |
| 1318 | * protocol version 15 */ |
| 1319 | recv_uid_list(f, flist); |
| 1320 | |
| 1321 | /* Recv the io_error flag */ |
| 1322 | if (lp_ignore_errors(module_id) || ignore_errors) |
| 1323 | read_int(f); |
| 1324 | else |
| 1325 | io_error |= read_int(f); |
| 1326 | } |
| 1327 | |
| 1328 | if (verbose > 3) |
| 1329 | output_flist(flist); |
| 1330 | |
| 1331 | if (list_only) { |
| 1332 | int i; |
| 1333 | for (i = 0; i < flist->count; i++) |
| 1334 | list_file_entry(flist->files[i]); |
| 1335 | } |
| 1336 | |
| 1337 | if (verbose > 2) |
| 1338 | rprintf(FINFO, "recv_file_list done\n"); |
| 1339 | |
| 1340 | stats.flist_size = stats.total_read - start_read; |
| 1341 | stats.num_files = flist->count; |
| 1342 | |
| 1343 | return flist; |
| 1344 | |
| 1345 | oom: |
| 1346 | out_of_memory("recv_file_list"); |
| 1347 | return NULL; /* not reached */ |
| 1348 | } |
| 1349 | |
| 1350 | |
| 1351 | int file_compare(struct file_struct **file1, struct file_struct **file2) |
| 1352 | { |
| 1353 | struct file_struct *f1 = *file1; |
| 1354 | struct file_struct *f2 = *file2; |
| 1355 | |
| 1356 | if (!f1->basename && !f2->basename) |
| 1357 | return 0; |
| 1358 | if (!f1->basename) |
| 1359 | return -1; |
| 1360 | if (!f2->basename) |
| 1361 | return 1; |
| 1362 | if (f1->dirname == f2->dirname) |
| 1363 | return u_strcmp(f1->basename, f2->basename); |
| 1364 | return f_name_cmp(f1, f2); |
| 1365 | } |
| 1366 | |
| 1367 | |
| 1368 | int flist_find(struct file_list *flist, struct file_struct *f) |
| 1369 | { |
| 1370 | int low = 0, high = flist->count - 1; |
| 1371 | |
| 1372 | while (high >= 0 && !flist->files[high]->basename) high--; |
| 1373 | |
| 1374 | if (high < 0) |
| 1375 | return -1; |
| 1376 | |
| 1377 | while (low != high) { |
| 1378 | int mid = (low + high) / 2; |
| 1379 | int ret = file_compare(&flist->files[flist_up(flist, mid)],&f); |
| 1380 | if (ret == 0) |
| 1381 | return flist_up(flist, mid); |
| 1382 | if (ret > 0) |
| 1383 | high = mid; |
| 1384 | else |
| 1385 | low = mid + 1; |
| 1386 | } |
| 1387 | |
| 1388 | if (file_compare(&flist->files[flist_up(flist, low)], &f) == 0) |
| 1389 | return flist_up(flist, low); |
| 1390 | return -1; |
| 1391 | } |
| 1392 | |
| 1393 | /* |
| 1394 | * Free up any resources a file_struct has allocated |
| 1395 | * and clear the file. |
| 1396 | */ |
| 1397 | void clear_file(int i, struct file_list *flist) |
| 1398 | { |
| 1399 | if (flist->hlink_pool && flist->files[i]->link_u.idev) |
| 1400 | pool_free(flist->hlink_pool, 0, flist->files[i]->link_u.idev); |
| 1401 | memset(flist->files[i], 0, file_struct_len); |
| 1402 | } |
| 1403 | |
| 1404 | |
| 1405 | /* |
| 1406 | * allocate a new file list |
| 1407 | */ |
| 1408 | struct file_list *flist_new(int with_hlink, char *msg) |
| 1409 | { |
| 1410 | struct file_list *flist; |
| 1411 | |
| 1412 | flist = new(struct file_list); |
| 1413 | if (!flist) |
| 1414 | out_of_memory(msg); |
| 1415 | |
| 1416 | memset(flist, 0, sizeof (struct file_list)); |
| 1417 | |
| 1418 | if (!(flist->file_pool = pool_create(FILE_EXTENT, 0, |
| 1419 | out_of_memory, POOL_INTERN))) |
| 1420 | out_of_memory(msg); |
| 1421 | |
| 1422 | #if SUPPORT_HARD_LINKS |
| 1423 | if (with_hlink && preserve_hard_links) { |
| 1424 | if (!(flist->hlink_pool = pool_create(HLINK_EXTENT, |
| 1425 | sizeof (struct idev), out_of_memory, POOL_INTERN))) |
| 1426 | out_of_memory(msg); |
| 1427 | } |
| 1428 | #endif |
| 1429 | |
| 1430 | return flist; |
| 1431 | } |
| 1432 | |
| 1433 | /* |
| 1434 | * free up all elements in a flist |
| 1435 | */ |
| 1436 | void flist_free(struct file_list *flist) |
| 1437 | { |
| 1438 | pool_destroy(flist->file_pool); |
| 1439 | pool_destroy(flist->hlink_pool); |
| 1440 | free(flist->files); |
| 1441 | free(flist); |
| 1442 | } |
| 1443 | |
| 1444 | |
| 1445 | /* |
| 1446 | * This routine ensures we don't have any duplicate names in our file list. |
| 1447 | * duplicate names can cause corruption because of the pipelining |
| 1448 | */ |
| 1449 | static void clean_flist(struct file_list *flist, int strip_root, int no_dups) |
| 1450 | { |
| 1451 | int i, prev_i = 0; |
| 1452 | |
| 1453 | if (!flist || flist->count == 0) |
| 1454 | return; |
| 1455 | |
| 1456 | qsort(flist->files, flist->count, |
| 1457 | sizeof flist->files[0], (int (*)())file_compare); |
| 1458 | |
| 1459 | for (i = no_dups? 0 : flist->count; i < flist->count; i++) { |
| 1460 | if (flist->files[i]->basename) { |
| 1461 | prev_i = i; |
| 1462 | break; |
| 1463 | } |
| 1464 | } |
| 1465 | while (++i < flist->count) { |
| 1466 | if (!flist->files[i]->basename) |
| 1467 | continue; |
| 1468 | if (f_name_cmp(flist->files[i], flist->files[prev_i]) == 0) { |
| 1469 | if (verbose > 1 && !am_server) { |
| 1470 | rprintf(FINFO, |
| 1471 | "removing duplicate name %s from file list %d\n", |
| 1472 | f_name(flist->files[i]), i); |
| 1473 | } |
| 1474 | /* Make sure that if we unduplicate '.', that we don't |
| 1475 | * lose track of a user-specified starting point (or |
| 1476 | * else deletions will mysteriously fail with -R). */ |
| 1477 | if (flist->files[i]->flags & FLAG_TOP_DIR) |
| 1478 | flist->files[prev_i]->flags |= FLAG_TOP_DIR; |
| 1479 | |
| 1480 | clear_file(i, flist); |
| 1481 | } else |
| 1482 | prev_i = i; |
| 1483 | } |
| 1484 | |
| 1485 | if (strip_root) { |
| 1486 | /* we need to strip off the root directory in the case |
| 1487 | of relative paths, but this must be done _after_ |
| 1488 | the sorting phase */ |
| 1489 | for (i = 0; i < flist->count; i++) { |
| 1490 | if (flist->files[i]->dirname && |
| 1491 | flist->files[i]->dirname[0] == '/') { |
| 1492 | memmove(&flist->files[i]->dirname[0], |
| 1493 | &flist->files[i]->dirname[1], |
| 1494 | strlen(flist->files[i]->dirname)); |
| 1495 | } |
| 1496 | |
| 1497 | if (flist->files[i]->dirname && |
| 1498 | !flist->files[i]->dirname[0]) { |
| 1499 | flist->files[i]->dirname = NULL; |
| 1500 | } |
| 1501 | } |
| 1502 | } |
| 1503 | } |
| 1504 | |
| 1505 | static void output_flist(struct file_list *flist) |
| 1506 | { |
| 1507 | char uidbuf[16], gidbuf[16]; |
| 1508 | struct file_struct *file; |
| 1509 | int i; |
| 1510 | |
| 1511 | for (i = 0; i < flist->count; i++) { |
| 1512 | file = flist->files[i]; |
| 1513 | if ((am_root || am_sender) && preserve_uid) |
| 1514 | sprintf(uidbuf, " uid=%ld", (long)file->uid); |
| 1515 | else |
| 1516 | *uidbuf = '\0'; |
| 1517 | if (preserve_gid && file->gid != GID_NONE) |
| 1518 | sprintf(gidbuf, " gid=%ld", (long)file->gid); |
| 1519 | else |
| 1520 | *gidbuf = '\0'; |
| 1521 | rprintf(FINFO, "[%s] i=%d %s %s %s mode=0%o len=%.0f%s%s\n", |
| 1522 | who_am_i(), i, NS(file->basedir), NS(file->dirname), |
| 1523 | NS(file->basename), (int)file->mode, |
| 1524 | (double)file->length, uidbuf, gidbuf); |
| 1525 | } |
| 1526 | } |
| 1527 | |
| 1528 | |
| 1529 | enum fnc_state { fnc_DIR, fnc_SLASH, fnc_BASE }; |
| 1530 | |
| 1531 | /* Compare the names of two file_struct entities, just like strcmp() |
| 1532 | * would do if it were operating on the joined strings. We assume |
| 1533 | * that there are no 0-length strings. |
| 1534 | */ |
| 1535 | int f_name_cmp(struct file_struct *f1, struct file_struct *f2) |
| 1536 | { |
| 1537 | int dif; |
| 1538 | const uchar *c1, *c2; |
| 1539 | enum fnc_state state1, state2; |
| 1540 | |
| 1541 | if (!f1 || !f1->basename) { |
| 1542 | if (!f2 || !f2->basename) |
| 1543 | return 0; |
| 1544 | return -1; |
| 1545 | } |
| 1546 | if (!f2 || !f2->basename) |
| 1547 | return 1; |
| 1548 | |
| 1549 | if (!(c1 = (uchar*)f1->dirname)) { |
| 1550 | state1 = fnc_BASE; |
| 1551 | c1 = (uchar*)f1->basename; |
| 1552 | } else if (!*c1) { |
| 1553 | state1 = fnc_SLASH; |
| 1554 | c1 = (uchar*)"/"; |
| 1555 | } else |
| 1556 | state1 = fnc_DIR; |
| 1557 | if (!(c2 = (uchar*)f2->dirname)) { |
| 1558 | state2 = fnc_BASE; |
| 1559 | c2 = (uchar*)f2->basename; |
| 1560 | } else if (!*c2) { |
| 1561 | state2 = fnc_SLASH; |
| 1562 | c2 = (uchar*)"/"; |
| 1563 | } else |
| 1564 | state2 = fnc_DIR; |
| 1565 | |
| 1566 | while (1) { |
| 1567 | if ((dif = (int)*c1 - (int)*c2) != 0) |
| 1568 | break; |
| 1569 | if (!*++c1) { |
| 1570 | switch (state1) { |
| 1571 | case fnc_DIR: |
| 1572 | state1 = fnc_SLASH; |
| 1573 | c1 = (uchar*)"/"; |
| 1574 | break; |
| 1575 | case fnc_SLASH: |
| 1576 | state1 = fnc_BASE; |
| 1577 | c1 = (uchar*)f1->basename; |
| 1578 | break; |
| 1579 | case fnc_BASE: |
| 1580 | break; |
| 1581 | } |
| 1582 | } |
| 1583 | if (!*++c2) { |
| 1584 | switch (state2) { |
| 1585 | case fnc_DIR: |
| 1586 | state2 = fnc_SLASH; |
| 1587 | c2 = (uchar*)"/"; |
| 1588 | break; |
| 1589 | case fnc_SLASH: |
| 1590 | state2 = fnc_BASE; |
| 1591 | c2 = (uchar*)f2->basename; |
| 1592 | break; |
| 1593 | case fnc_BASE: |
| 1594 | if (!*c1) |
| 1595 | return 0; |
| 1596 | break; |
| 1597 | } |
| 1598 | } |
| 1599 | } |
| 1600 | |
| 1601 | return dif; |
| 1602 | } |
| 1603 | |
| 1604 | |
| 1605 | /* Return a copy of the full filename of a flist entry, using the indicated |
| 1606 | * buffer. No size-checking is done because we checked the size when creating |
| 1607 | * the file_struct entry. |
| 1608 | */ |
| 1609 | char *f_name_to(struct file_struct *f, char *fbuf) |
| 1610 | { |
| 1611 | if (!f || !f->basename) |
| 1612 | return NULL; |
| 1613 | |
| 1614 | if (f->dirname) { |
| 1615 | int len = strlen(f->dirname); |
| 1616 | memcpy(fbuf, f->dirname, len); |
| 1617 | fbuf[len] = '/'; |
| 1618 | strcpy(fbuf + len + 1, f->basename); |
| 1619 | } else |
| 1620 | strcpy(fbuf, f->basename); |
| 1621 | return fbuf; |
| 1622 | } |
| 1623 | |
| 1624 | |
| 1625 | /* Like f_name_to(), but we rotate through 5 static buffers of our own. |
| 1626 | */ |
| 1627 | char *f_name(struct file_struct *f) |
| 1628 | { |
| 1629 | static char names[5][MAXPATHLEN]; |
| 1630 | static unsigned int n; |
| 1631 | |
| 1632 | n = (n + 1) % (sizeof names / sizeof names[0]); |
| 1633 | |
| 1634 | return f_name_to(f, names[n]); |
| 1635 | } |