| 1 | /* |
| 2 | * Generate and receive file lists. |
| 3 | * |
| 4 | * Copyright (C) 1996 Andrew Tridgell |
| 5 | * Copyright (C) 1996 Paul Mackerras |
| 6 | * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org> |
| 7 | * Copyright (C) 2002, 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 list_only; |
| 28 | extern int am_root; |
| 29 | extern int am_server; |
| 30 | extern int am_daemon; |
| 31 | extern int am_sender; |
| 32 | extern int do_progress; |
| 33 | extern int always_checksum; |
| 34 | extern int module_id; |
| 35 | extern int ignore_errors; |
| 36 | extern int numeric_ids; |
| 37 | extern int recurse; |
| 38 | extern int xfer_dirs; |
| 39 | extern int filesfrom_fd; |
| 40 | extern int one_file_system; |
| 41 | extern int copy_dirlinks; |
| 42 | extern int keep_dirlinks; |
| 43 | extern int preserve_links; |
| 44 | extern int preserve_hard_links; |
| 45 | extern int preserve_devices; |
| 46 | extern int preserve_specials; |
| 47 | extern int preserve_uid; |
| 48 | extern int preserve_gid; |
| 49 | extern int relative_paths; |
| 50 | extern int implied_dirs; |
| 51 | extern int prune_empty_dirs; |
| 52 | extern int copy_links; |
| 53 | extern int copy_unsafe_links; |
| 54 | extern int protocol_version; |
| 55 | extern int sanitize_paths; |
| 56 | extern struct stats stats; |
| 57 | extern struct file_list *the_file_list; |
| 58 | |
| 59 | extern char curr_dir[MAXPATHLEN]; |
| 60 | |
| 61 | extern struct chmod_mode_struct *chmod_modes; |
| 62 | |
| 63 | extern struct filter_list_struct filter_list; |
| 64 | extern struct filter_list_struct server_filter_list; |
| 65 | |
| 66 | int io_error; |
| 67 | int checksum_len; |
| 68 | dev_t filesystem_dev; /* used to implement -x */ |
| 69 | unsigned int file_struct_len; |
| 70 | |
| 71 | static char empty_sum[MD4_SUM_LENGTH]; |
| 72 | static int flist_count_offset; |
| 73 | |
| 74 | static void clean_flist(struct file_list *flist, int strip_root, int no_dups); |
| 75 | static void output_flist(struct file_list *flist); |
| 76 | |
| 77 | void init_flist(void) |
| 78 | { |
| 79 | struct file_struct f; |
| 80 | |
| 81 | /* Figure out how big the file_struct is without trailing padding */ |
| 82 | file_struct_len = offsetof(struct file_struct, flags) + sizeof f.flags; |
| 83 | checksum_len = protocol_version < 21 ? 2 : MD4_SUM_LENGTH; |
| 84 | } |
| 85 | |
| 86 | static int show_filelist_p(void) |
| 87 | { |
| 88 | return verbose && xfer_dirs && !am_server; |
| 89 | } |
| 90 | |
| 91 | static void start_filelist_progress(char *kind) |
| 92 | { |
| 93 | rprintf(FCLIENT, "%s ... ", kind); |
| 94 | if (verbose > 1 || do_progress) |
| 95 | rprintf(FCLIENT, "\n"); |
| 96 | rflush(FINFO); |
| 97 | } |
| 98 | |
| 99 | static void emit_filelist_progress(int count) |
| 100 | { |
| 101 | rprintf(FCLIENT, " %d files...\r", count); |
| 102 | } |
| 103 | |
| 104 | static void maybe_emit_filelist_progress(int count) |
| 105 | { |
| 106 | if (do_progress && show_filelist_p() && (count % 100) == 0) |
| 107 | emit_filelist_progress(count); |
| 108 | } |
| 109 | |
| 110 | static void finish_filelist_progress(const struct file_list *flist) |
| 111 | { |
| 112 | if (do_progress) { |
| 113 | /* This overwrites the progress line */ |
| 114 | rprintf(FINFO, "%d file%sto consider\n", |
| 115 | flist->count, flist->count == 1 ? " " : "s "); |
| 116 | } else |
| 117 | rprintf(FINFO, "done\n"); |
| 118 | } |
| 119 | |
| 120 | void show_flist_stats(void) |
| 121 | { |
| 122 | /* Nothing yet */ |
| 123 | } |
| 124 | |
| 125 | static void list_file_entry(struct file_struct *f) |
| 126 | { |
| 127 | char permbuf[PERMSTRING_SIZE]; |
| 128 | |
| 129 | if (!f->basename) { |
| 130 | /* this can happen if duplicate names were removed */ |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | permstring(permbuf, f->mode); |
| 135 | |
| 136 | #ifdef SUPPORT_LINKS |
| 137 | if (preserve_links && S_ISLNK(f->mode)) { |
| 138 | rprintf(FINFO, "%s %11.0f %s %s -> %s\n", |
| 139 | permbuf, |
| 140 | (double)f->length, timestring(f->modtime), |
| 141 | f_name(f, NULL), f->u.link); |
| 142 | } else |
| 143 | #endif |
| 144 | { |
| 145 | rprintf(FINFO, "%s %11.0f %s %s\n", |
| 146 | permbuf, |
| 147 | (double)f->length, timestring(f->modtime), |
| 148 | f_name(f, NULL)); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /* Stat either a symlink or its referent, depending on the settings of |
| 153 | * copy_links, copy_unsafe_links, etc. Returns -1 on error, 0 on success. |
| 154 | * |
| 155 | * If path is the name of a symlink, then the linkbuf buffer (which must hold |
| 156 | * MAXPATHLEN chars) will be set to the symlink's target string. |
| 157 | * |
| 158 | * The stat structure pointed to by stp will contain information about the |
| 159 | * link or the referent as appropriate, if they exist. */ |
| 160 | static int readlink_stat(const char *path, STRUCT_STAT *stp, char *linkbuf) |
| 161 | { |
| 162 | #ifdef SUPPORT_LINKS |
| 163 | if (link_stat(path, stp, copy_dirlinks) < 0) |
| 164 | return -1; |
| 165 | if (S_ISLNK(stp->st_mode)) { |
| 166 | int llen = readlink(path, linkbuf, MAXPATHLEN - 1); |
| 167 | if (llen < 0) |
| 168 | return -1; |
| 169 | linkbuf[llen] = '\0'; |
| 170 | if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) { |
| 171 | if (verbose > 1) { |
| 172 | rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n", |
| 173 | path, linkbuf); |
| 174 | } |
| 175 | return do_stat(path, stp); |
| 176 | } |
| 177 | } |
| 178 | return 0; |
| 179 | #else |
| 180 | return do_stat(path, stp); |
| 181 | #endif |
| 182 | } |
| 183 | |
| 184 | int link_stat(const char *path, STRUCT_STAT *stp, int follow_dirlinks) |
| 185 | { |
| 186 | #ifdef SUPPORT_LINKS |
| 187 | if (copy_links) |
| 188 | return do_stat(path, stp); |
| 189 | if (do_lstat(path, stp) < 0) |
| 190 | return -1; |
| 191 | if (follow_dirlinks && S_ISLNK(stp->st_mode)) { |
| 192 | STRUCT_STAT st; |
| 193 | if (do_stat(path, &st) == 0 && S_ISDIR(st.st_mode)) |
| 194 | *stp = st; |
| 195 | } |
| 196 | return 0; |
| 197 | #else |
| 198 | return do_stat(path, stp); |
| 199 | #endif |
| 200 | } |
| 201 | |
| 202 | /* This function is used to check if a file should be included/excluded |
| 203 | * from the list of files based on its name and type etc. The value of |
| 204 | * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */ |
| 205 | static int is_excluded(char *fname, int is_dir, int filter_level) |
| 206 | { |
| 207 | #if 0 /* This currently never happens, so avoid a useless compare. */ |
| 208 | if (filter_level == NO_FILTERS) |
| 209 | return 0; |
| 210 | #endif |
| 211 | if (fname) { |
| 212 | /* never exclude '.', even if somebody does --exclude '*' */ |
| 213 | if (fname[0] == '.' && !fname[1]) |
| 214 | return 0; |
| 215 | /* Handle the -R version of the '.' dir. */ |
| 216 | if (fname[0] == '/') { |
| 217 | int len = strlen(fname); |
| 218 | if (fname[len-1] == '.' && fname[len-2] == '/') |
| 219 | return 0; |
| 220 | } |
| 221 | } |
| 222 | if (server_filter_list.head |
| 223 | && check_filter(&server_filter_list, fname, is_dir) < 0) |
| 224 | return 1; |
| 225 | if (filter_level != ALL_FILTERS) |
| 226 | return 0; |
| 227 | if (filter_list.head |
| 228 | && check_filter(&filter_list, fname, is_dir) < 0) |
| 229 | return 1; |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | static int to_wire_mode(mode_t mode) |
| 234 | { |
| 235 | #ifdef SUPPORT_LINKS |
| 236 | #if _S_IFLNK != 0120000 |
| 237 | if (S_ISLNK(mode)) |
| 238 | return (mode & ~(_S_IFMT)) | 0120000; |
| 239 | #endif |
| 240 | #endif |
| 241 | return mode; |
| 242 | } |
| 243 | |
| 244 | static mode_t from_wire_mode(int mode) |
| 245 | { |
| 246 | #if _S_IFLNK != 0120000 |
| 247 | if ((mode & (_S_IFMT)) == 0120000) |
| 248 | return (mode & ~(_S_IFMT)) | _S_IFLNK; |
| 249 | #endif |
| 250 | return mode; |
| 251 | } |
| 252 | |
| 253 | static void send_directory(int f, struct file_list *flist, |
| 254 | char *fbuf, int len); |
| 255 | |
| 256 | static char *flist_dir; |
| 257 | static int flist_dir_len; |
| 258 | |
| 259 | |
| 260 | /** |
| 261 | * Make sure @p flist is big enough to hold at least @p flist->count |
| 262 | * entries. |
| 263 | **/ |
| 264 | void flist_expand(struct file_list *flist) |
| 265 | { |
| 266 | struct file_struct **new_ptr; |
| 267 | |
| 268 | if (flist->count < flist->malloced) |
| 269 | return; |
| 270 | |
| 271 | if (flist->malloced < FLIST_START) |
| 272 | flist->malloced = FLIST_START; |
| 273 | else if (flist->malloced >= FLIST_LINEAR) |
| 274 | flist->malloced += FLIST_LINEAR; |
| 275 | else |
| 276 | flist->malloced *= 2; |
| 277 | |
| 278 | /* |
| 279 | * In case count jumped or we are starting the list |
| 280 | * with a known size just set it. |
| 281 | */ |
| 282 | if (flist->malloced < flist->count) |
| 283 | flist->malloced = flist->count; |
| 284 | |
| 285 | new_ptr = realloc_array(flist->files, struct file_struct *, |
| 286 | flist->malloced); |
| 287 | |
| 288 | if (verbose >= 2 && flist->malloced != FLIST_START) { |
| 289 | rprintf(FCLIENT, "[%s] expand file_list to %.0f bytes, did%s move\n", |
| 290 | who_am_i(), |
| 291 | (double)sizeof flist->files[0] * flist->malloced, |
| 292 | (new_ptr == flist->files) ? " not" : ""); |
| 293 | } |
| 294 | |
| 295 | flist->files = new_ptr; |
| 296 | |
| 297 | if (!flist->files) |
| 298 | out_of_memory("flist_expand"); |
| 299 | } |
| 300 | |
| 301 | static void send_file_entry(struct file_struct *file, int f) |
| 302 | { |
| 303 | unsigned short flags; |
| 304 | static time_t modtime; |
| 305 | static mode_t mode; |
| 306 | static int64 dev; |
| 307 | static dev_t rdev; |
| 308 | static uint32 rdev_major; |
| 309 | static uid_t uid; |
| 310 | static gid_t gid; |
| 311 | static char lastname[MAXPATHLEN]; |
| 312 | char fname[MAXPATHLEN]; |
| 313 | int l1, l2; |
| 314 | |
| 315 | if (f < 0) |
| 316 | return; |
| 317 | |
| 318 | if (!file) { |
| 319 | write_byte(f, 0); |
| 320 | modtime = 0, mode = 0; |
| 321 | dev = 0, rdev = MAKEDEV(0, 0); |
| 322 | rdev_major = 0; |
| 323 | uid = 0, gid = 0; |
| 324 | *lastname = '\0'; |
| 325 | return; |
| 326 | } |
| 327 | |
| 328 | f_name(file, fname); |
| 329 | |
| 330 | flags = file->flags & XMIT_TOP_DIR; |
| 331 | |
| 332 | if (file->mode == mode) |
| 333 | flags |= XMIT_SAME_MODE; |
| 334 | else |
| 335 | mode = file->mode; |
| 336 | if ((preserve_devices && IS_DEVICE(mode)) |
| 337 | || (preserve_specials && IS_SPECIAL(mode))) { |
| 338 | if (protocol_version < 28) { |
| 339 | if (file->u.rdev == rdev) |
| 340 | flags |= XMIT_SAME_RDEV_pre28; |
| 341 | else |
| 342 | rdev = file->u.rdev; |
| 343 | } else { |
| 344 | rdev = file->u.rdev; |
| 345 | if ((uint32)major(rdev) == rdev_major) |
| 346 | flags |= XMIT_SAME_RDEV_MAJOR; |
| 347 | else |
| 348 | rdev_major = major(rdev); |
| 349 | if ((uint32)minor(rdev) <= 0xFFu) |
| 350 | flags |= XMIT_RDEV_MINOR_IS_SMALL; |
| 351 | } |
| 352 | } else if (protocol_version < 28) |
| 353 | rdev = MAKEDEV(0, 0); |
| 354 | if (file->uid == uid) |
| 355 | flags |= XMIT_SAME_UID; |
| 356 | else |
| 357 | uid = file->uid; |
| 358 | if (file->gid == gid) |
| 359 | flags |= XMIT_SAME_GID; |
| 360 | else |
| 361 | gid = file->gid; |
| 362 | if (file->modtime == modtime) |
| 363 | flags |= XMIT_SAME_TIME; |
| 364 | else |
| 365 | modtime = file->modtime; |
| 366 | |
| 367 | #ifdef SUPPORT_HARD_LINKS |
| 368 | if (file->link_u.idev) { |
| 369 | if (file->F_DEV == dev) { |
| 370 | if (protocol_version >= 28) |
| 371 | flags |= XMIT_SAME_DEV; |
| 372 | } else |
| 373 | dev = file->F_DEV; |
| 374 | flags |= XMIT_HAS_IDEV_DATA; |
| 375 | } |
| 376 | #endif |
| 377 | |
| 378 | for (l1 = 0; |
| 379 | lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255); |
| 380 | l1++) {} |
| 381 | l2 = strlen(fname+l1); |
| 382 | |
| 383 | if (l1 > 0) |
| 384 | flags |= XMIT_SAME_NAME; |
| 385 | if (l2 > 255) |
| 386 | flags |= XMIT_LONG_NAME; |
| 387 | |
| 388 | /* We must make sure we don't send a zero flag byte or the |
| 389 | * other end will terminate the flist transfer. Note that |
| 390 | * the use of XMIT_TOP_DIR on a non-dir has no meaning, so |
| 391 | * it's harmless way to add a bit to the first flag byte. */ |
| 392 | if (protocol_version >= 28) { |
| 393 | if (!flags && !S_ISDIR(mode)) |
| 394 | flags |= XMIT_TOP_DIR; |
| 395 | if ((flags & 0xFF00) || !flags) { |
| 396 | flags |= XMIT_EXTENDED_FLAGS; |
| 397 | write_byte(f, flags); |
| 398 | write_byte(f, flags >> 8); |
| 399 | } else |
| 400 | write_byte(f, flags); |
| 401 | } else { |
| 402 | if (!(flags & 0xFF)) |
| 403 | flags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR; |
| 404 | write_byte(f, flags); |
| 405 | } |
| 406 | if (flags & XMIT_SAME_NAME) |
| 407 | write_byte(f, l1); |
| 408 | if (flags & XMIT_LONG_NAME) |
| 409 | write_int(f, l2); |
| 410 | else |
| 411 | write_byte(f, l2); |
| 412 | write_buf(f, fname + l1, l2); |
| 413 | |
| 414 | write_longint(f, file->length); |
| 415 | if (!(flags & XMIT_SAME_TIME)) |
| 416 | write_int(f, modtime); |
| 417 | if (!(flags & XMIT_SAME_MODE)) |
| 418 | write_int(f, to_wire_mode(mode)); |
| 419 | if (preserve_uid && !(flags & XMIT_SAME_UID)) { |
| 420 | if (!numeric_ids) |
| 421 | add_uid(uid); |
| 422 | write_int(f, uid); |
| 423 | } |
| 424 | if (preserve_gid && !(flags & XMIT_SAME_GID)) { |
| 425 | if (!numeric_ids) |
| 426 | add_gid(gid); |
| 427 | write_int(f, gid); |
| 428 | } |
| 429 | if ((preserve_devices && IS_DEVICE(mode)) |
| 430 | || (preserve_specials && IS_SPECIAL(mode))) { |
| 431 | if (protocol_version < 28) { |
| 432 | if (!(flags & XMIT_SAME_RDEV_pre28)) |
| 433 | write_int(f, (int)rdev); |
| 434 | } else { |
| 435 | if (!(flags & XMIT_SAME_RDEV_MAJOR)) |
| 436 | write_int(f, major(rdev)); |
| 437 | if (flags & XMIT_RDEV_MINOR_IS_SMALL) |
| 438 | write_byte(f, minor(rdev)); |
| 439 | else |
| 440 | write_int(f, minor(rdev)); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | #ifdef SUPPORT_LINKS |
| 445 | if (preserve_links && S_ISLNK(mode)) { |
| 446 | int len = strlen(file->u.link); |
| 447 | write_int(f, len); |
| 448 | write_buf(f, file->u.link, len); |
| 449 | } |
| 450 | #endif |
| 451 | |
| 452 | #ifdef SUPPORT_HARD_LINKS |
| 453 | if (file->link_u.idev) { |
| 454 | if (protocol_version < 26) { |
| 455 | /* 32-bit dev_t and ino_t */ |
| 456 | write_int(f, dev); |
| 457 | write_int(f, file->F_INODE); |
| 458 | } else { |
| 459 | /* 64-bit dev_t and ino_t */ |
| 460 | if (!(flags & XMIT_SAME_DEV)) |
| 461 | write_longint(f, dev); |
| 462 | write_longint(f, file->F_INODE); |
| 463 | } |
| 464 | } |
| 465 | #endif |
| 466 | |
| 467 | if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) { |
| 468 | char *sum; |
| 469 | if (S_ISREG(mode)) |
| 470 | sum = file->u.sum; |
| 471 | else { |
| 472 | /* Prior to 28, we sent a useless set of nulls. */ |
| 473 | sum = empty_sum; |
| 474 | } |
| 475 | write_buf(f, sum, checksum_len); |
| 476 | } |
| 477 | |
| 478 | strlcpy(lastname, fname, MAXPATHLEN); |
| 479 | } |
| 480 | |
| 481 | static struct file_struct *receive_file_entry(struct file_list *flist, |
| 482 | unsigned short flags, int f) |
| 483 | { |
| 484 | static time_t modtime; |
| 485 | static mode_t mode; |
| 486 | static int64 dev; |
| 487 | static dev_t rdev; |
| 488 | static uint32 rdev_major; |
| 489 | static uid_t uid; |
| 490 | static gid_t gid; |
| 491 | static char lastname[MAXPATHLEN], *lastdir; |
| 492 | static int lastdir_depth, lastdir_len = -1; |
| 493 | static unsigned int del_hier_name_len = 0; |
| 494 | static int in_del_hier = 0; |
| 495 | char thisname[MAXPATHLEN]; |
| 496 | unsigned int l1 = 0, l2 = 0; |
| 497 | int alloc_len, basename_len, dirname_len, linkname_len, sum_len; |
| 498 | OFF_T file_length; |
| 499 | char *basename, *dirname, *bp; |
| 500 | struct file_struct *file; |
| 501 | |
| 502 | if (!flist) { |
| 503 | modtime = 0, mode = 0; |
| 504 | dev = 0, rdev = MAKEDEV(0, 0); |
| 505 | rdev_major = 0; |
| 506 | uid = 0, gid = 0; |
| 507 | *lastname = '\0'; |
| 508 | lastdir_len = -1; |
| 509 | in_del_hier = 0; |
| 510 | return NULL; |
| 511 | } |
| 512 | |
| 513 | if (flags & XMIT_SAME_NAME) |
| 514 | l1 = read_byte(f); |
| 515 | |
| 516 | if (flags & XMIT_LONG_NAME) |
| 517 | l2 = read_int(f); |
| 518 | else |
| 519 | l2 = read_byte(f); |
| 520 | |
| 521 | if (l2 >= MAXPATHLEN - l1) { |
| 522 | rprintf(FERROR, |
| 523 | "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n", |
| 524 | flags, l1, l2, lastname); |
| 525 | overflow_exit("receive_file_entry"); |
| 526 | } |
| 527 | |
| 528 | strlcpy(thisname, lastname, l1 + 1); |
| 529 | read_sbuf(f, &thisname[l1], l2); |
| 530 | thisname[l1 + l2] = 0; |
| 531 | |
| 532 | strlcpy(lastname, thisname, MAXPATHLEN); |
| 533 | |
| 534 | clean_fname(thisname, 0); |
| 535 | |
| 536 | if (sanitize_paths) |
| 537 | sanitize_path(thisname, thisname, "", 0, NULL); |
| 538 | |
| 539 | if ((basename = strrchr(thisname, '/')) != NULL) { |
| 540 | dirname_len = ++basename - thisname; /* counts future '\0' */ |
| 541 | if (lastdir_len == dirname_len - 1 |
| 542 | && strncmp(thisname, lastdir, lastdir_len) == 0) { |
| 543 | dirname = lastdir; |
| 544 | dirname_len = 0; /* indicates no copy is needed */ |
| 545 | } else |
| 546 | dirname = thisname; |
| 547 | } else { |
| 548 | basename = thisname; |
| 549 | dirname = NULL; |
| 550 | dirname_len = 0; |
| 551 | } |
| 552 | basename_len = strlen(basename) + 1; /* count the '\0' */ |
| 553 | |
| 554 | file_length = read_longint(f); |
| 555 | if (!(flags & XMIT_SAME_TIME)) |
| 556 | modtime = (time_t)read_int(f); |
| 557 | if (!(flags & XMIT_SAME_MODE)) |
| 558 | mode = from_wire_mode(read_int(f)); |
| 559 | |
| 560 | if (chmod_modes && !S_ISLNK(mode)) |
| 561 | mode = tweak_mode(mode, chmod_modes); |
| 562 | |
| 563 | if (preserve_uid && !(flags & XMIT_SAME_UID)) |
| 564 | uid = (uid_t)read_int(f); |
| 565 | if (preserve_gid && !(flags & XMIT_SAME_GID)) |
| 566 | gid = (gid_t)read_int(f); |
| 567 | |
| 568 | if ((preserve_devices && IS_DEVICE(mode)) |
| 569 | || (preserve_specials && IS_SPECIAL(mode))) { |
| 570 | if (protocol_version < 28) { |
| 571 | if (!(flags & XMIT_SAME_RDEV_pre28)) |
| 572 | rdev = (dev_t)read_int(f); |
| 573 | } else { |
| 574 | uint32 rdev_minor; |
| 575 | if (!(flags & XMIT_SAME_RDEV_MAJOR)) |
| 576 | rdev_major = read_int(f); |
| 577 | if (flags & XMIT_RDEV_MINOR_IS_SMALL) |
| 578 | rdev_minor = read_byte(f); |
| 579 | else |
| 580 | rdev_minor = read_int(f); |
| 581 | rdev = MAKEDEV(rdev_major, rdev_minor); |
| 582 | } |
| 583 | } else if (protocol_version < 28) |
| 584 | rdev = MAKEDEV(0, 0); |
| 585 | |
| 586 | #ifdef SUPPORT_LINKS |
| 587 | if (preserve_links && S_ISLNK(mode)) { |
| 588 | linkname_len = read_int(f) + 1; /* count the '\0' */ |
| 589 | if (linkname_len <= 0 || linkname_len > MAXPATHLEN) { |
| 590 | rprintf(FERROR, "overflow: linkname_len=%d\n", |
| 591 | linkname_len - 1); |
| 592 | overflow_exit("receive_file_entry"); |
| 593 | } |
| 594 | } |
| 595 | else |
| 596 | #endif |
| 597 | linkname_len = 0; |
| 598 | |
| 599 | sum_len = always_checksum && S_ISREG(mode) ? MD4_SUM_LENGTH : 0; |
| 600 | |
| 601 | alloc_len = file_struct_len + dirname_len + basename_len |
| 602 | + linkname_len + sum_len; |
| 603 | bp = pool_alloc(flist->file_pool, alloc_len, "receive_file_entry"); |
| 604 | |
| 605 | file = (struct file_struct *)bp; |
| 606 | memset(bp, 0, file_struct_len); |
| 607 | bp += file_struct_len; |
| 608 | |
| 609 | file->modtime = modtime; |
| 610 | file->length = file_length; |
| 611 | file->mode = mode; |
| 612 | file->uid = uid; |
| 613 | file->gid = gid; |
| 614 | |
| 615 | if (dirname_len) { |
| 616 | file->dirname = lastdir = bp; |
| 617 | lastdir_len = dirname_len - 1; |
| 618 | memcpy(bp, dirname, dirname_len - 1); |
| 619 | bp += dirname_len; |
| 620 | bp[-1] = '\0'; |
| 621 | lastdir_depth = count_dir_elements(lastdir); |
| 622 | file->dir.depth = lastdir_depth + 1; |
| 623 | } else if (dirname) { |
| 624 | file->dirname = dirname; /* we're reusing lastname */ |
| 625 | file->dir.depth = lastdir_depth + 1; |
| 626 | } else |
| 627 | file->dir.depth = 1; |
| 628 | |
| 629 | if (S_ISDIR(mode)) { |
| 630 | if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */ |
| 631 | file->dir.depth--; |
| 632 | if (flags & XMIT_TOP_DIR) { |
| 633 | in_del_hier = recurse; |
| 634 | del_hier_name_len = file->dir.depth == 0 ? 0 : l1 + l2; |
| 635 | if (relative_paths && del_hier_name_len > 2 |
| 636 | && lastname[del_hier_name_len-1] == '.' |
| 637 | && lastname[del_hier_name_len-2] == '/') |
| 638 | del_hier_name_len -= 2; |
| 639 | file->flags |= FLAG_TOP_DIR | FLAG_DEL_HERE; |
| 640 | } else if (in_del_hier) { |
| 641 | if (!relative_paths || !del_hier_name_len |
| 642 | || (l1 >= del_hier_name_len |
| 643 | && lastname[del_hier_name_len] == '/')) |
| 644 | file->flags |= FLAG_DEL_HERE; |
| 645 | else |
| 646 | in_del_hier = 0; |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | file->basename = bp; |
| 651 | memcpy(bp, basename, basename_len); |
| 652 | bp += basename_len; |
| 653 | |
| 654 | if ((preserve_devices && IS_DEVICE(mode)) |
| 655 | || (preserve_specials && IS_SPECIAL(mode))) |
| 656 | file->u.rdev = rdev; |
| 657 | |
| 658 | #ifdef SUPPORT_LINKS |
| 659 | if (linkname_len) { |
| 660 | file->u.link = bp; |
| 661 | read_sbuf(f, bp, linkname_len - 1); |
| 662 | if (sanitize_paths) |
| 663 | sanitize_path(bp, bp, "", lastdir_depth, NULL); |
| 664 | bp += linkname_len; |
| 665 | } |
| 666 | #endif |
| 667 | |
| 668 | #ifdef SUPPORT_HARD_LINKS |
| 669 | if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode)) |
| 670 | flags |= XMIT_HAS_IDEV_DATA; |
| 671 | if (flags & XMIT_HAS_IDEV_DATA) { |
| 672 | int64 inode; |
| 673 | if (protocol_version < 26) { |
| 674 | dev = read_int(f); |
| 675 | inode = read_int(f); |
| 676 | } else { |
| 677 | if (!(flags & XMIT_SAME_DEV)) |
| 678 | dev = read_longint(f); |
| 679 | inode = read_longint(f); |
| 680 | } |
| 681 | if (flist->hlink_pool) { |
| 682 | file->link_u.idev = pool_talloc(flist->hlink_pool, |
| 683 | struct idev, 1, "inode_table"); |
| 684 | file->F_INODE = inode; |
| 685 | file->F_DEV = dev; |
| 686 | } |
| 687 | } |
| 688 | #endif |
| 689 | |
| 690 | if (always_checksum && (sum_len || protocol_version < 28)) { |
| 691 | char *sum; |
| 692 | if (sum_len) { |
| 693 | file->u.sum = sum = bp; |
| 694 | /*bp += sum_len;*/ |
| 695 | } else { |
| 696 | /* Prior to 28, we get a useless set of nulls. */ |
| 697 | sum = empty_sum; |
| 698 | } |
| 699 | read_buf(f, sum, checksum_len); |
| 700 | } |
| 701 | |
| 702 | return file; |
| 703 | } |
| 704 | |
| 705 | /** |
| 706 | * Create a file_struct for a named file by reading its stat() |
| 707 | * information and performing extensive checks against global |
| 708 | * options. |
| 709 | * |
| 710 | * @return the new file, or NULL if there was an error or this file |
| 711 | * should be excluded. |
| 712 | * |
| 713 | * @todo There is a small optimization opportunity here to avoid |
| 714 | * stat()ing the file in some circumstances, which has a certain cost. |
| 715 | * We are called immediately after doing readdir(), and so we may |
| 716 | * already know the d_type of the file. We could for example avoid |
| 717 | * statting directories if we're not recursing, but this is not a very |
| 718 | * important case. Some systems may not have d_type. |
| 719 | **/ |
| 720 | struct file_struct *make_file(char *fname, struct file_list *flist, |
| 721 | STRUCT_STAT *stp, unsigned short flags, |
| 722 | int filter_level) |
| 723 | { |
| 724 | static char *lastdir; |
| 725 | static int lastdir_len = -1; |
| 726 | struct file_struct *file; |
| 727 | STRUCT_STAT st; |
| 728 | char sum[SUM_LENGTH]; |
| 729 | char thisname[MAXPATHLEN]; |
| 730 | char linkname[MAXPATHLEN]; |
| 731 | int alloc_len, basename_len, dirname_len, linkname_len, sum_len; |
| 732 | char *basename, *dirname, *bp; |
| 733 | |
| 734 | if (!flist || !flist->count) /* Ignore lastdir when invalid. */ |
| 735 | lastdir_len = -1; |
| 736 | |
| 737 | if (strlcpy(thisname, fname, sizeof thisname) |
| 738 | >= sizeof thisname - flist_dir_len) { |
| 739 | rprintf(FINFO, "skipping overly long name: %s\n", fname); |
| 740 | return NULL; |
| 741 | } |
| 742 | clean_fname(thisname, 0); |
| 743 | if (sanitize_paths) |
| 744 | sanitize_path(thisname, thisname, "", 0, NULL); |
| 745 | |
| 746 | memset(sum, 0, SUM_LENGTH); |
| 747 | |
| 748 | if (stp && S_ISDIR(stp->st_mode)) { |
| 749 | st = *stp; /* Needed for "symlink/." with --relative. */ |
| 750 | *linkname = '\0'; /* make IBM code checker happy */ |
| 751 | } else if (readlink_stat(thisname, &st, linkname) != 0) { |
| 752 | int save_errno = errno; |
| 753 | /* See if file is excluded before reporting an error. */ |
| 754 | if (filter_level != NO_FILTERS |
| 755 | && is_excluded(thisname, 0, filter_level)) |
| 756 | return NULL; |
| 757 | if (save_errno == ENOENT) { |
| 758 | #ifdef SUPPORT_LINKS |
| 759 | /* Avoid "vanished" error if symlink points nowhere. */ |
| 760 | if (copy_links && do_lstat(thisname, &st) == 0 |
| 761 | && S_ISLNK(st.st_mode)) { |
| 762 | io_error |= IOERR_GENERAL; |
| 763 | rprintf(FERROR, "symlink has no referent: %s\n", |
| 764 | full_fname(thisname)); |
| 765 | } else |
| 766 | #endif |
| 767 | { |
| 768 | enum logcode c = am_daemon && protocol_version < 28 |
| 769 | ? FERROR : FINFO; |
| 770 | io_error |= IOERR_VANISHED; |
| 771 | rprintf(c, "file has vanished: %s\n", |
| 772 | full_fname(thisname)); |
| 773 | } |
| 774 | } else { |
| 775 | io_error |= IOERR_GENERAL; |
| 776 | rsyserr(FERROR, save_errno, "readlink %s failed", |
| 777 | full_fname(thisname)); |
| 778 | } |
| 779 | return NULL; |
| 780 | } |
| 781 | |
| 782 | /* backup.c calls us with filter_level set to NO_FILTERS. */ |
| 783 | if (filter_level == NO_FILTERS) |
| 784 | goto skip_filters; |
| 785 | |
| 786 | if (S_ISDIR(st.st_mode) && !xfer_dirs) { |
| 787 | rprintf(FINFO, "skipping directory %s\n", thisname); |
| 788 | return NULL; |
| 789 | } |
| 790 | |
| 791 | /* We only care about directories because we need to avoid recursing |
| 792 | * into a mount-point directory, not to avoid copying a symlinked |
| 793 | * file if -L (or similar) was specified. */ |
| 794 | if (one_file_system && st.st_dev != filesystem_dev |
| 795 | && S_ISDIR(st.st_mode)) { |
| 796 | if (one_file_system > 1) { |
| 797 | if (verbose > 2) { |
| 798 | rprintf(FINFO, "skipping mount-point dir %s\n", |
| 799 | thisname); |
| 800 | } |
| 801 | return NULL; |
| 802 | } |
| 803 | flags |= FLAG_MOUNT_POINT; |
| 804 | } |
| 805 | |
| 806 | if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) |
| 807 | return NULL; |
| 808 | |
| 809 | if (lp_ignore_nonreadable(module_id)) { |
| 810 | #ifdef SUPPORT_LINKS |
| 811 | if (!S_ISLNK(st.st_mode)) |
| 812 | #endif |
| 813 | if (access(thisname, R_OK) != 0) |
| 814 | return NULL; |
| 815 | } |
| 816 | |
| 817 | skip_filters: |
| 818 | |
| 819 | if (verbose > 2) { |
| 820 | rprintf(FINFO, "[%s] make_file(%s,*,%d)\n", |
| 821 | who_am_i(), thisname, filter_level); |
| 822 | } |
| 823 | |
| 824 | if ((basename = strrchr(thisname, '/')) != NULL) { |
| 825 | dirname_len = ++basename - thisname; /* counts future '\0' */ |
| 826 | if (lastdir_len == dirname_len - 1 |
| 827 | && strncmp(thisname, lastdir, lastdir_len) == 0) { |
| 828 | dirname = lastdir; |
| 829 | dirname_len = 0; /* indicates no copy is needed */ |
| 830 | } else |
| 831 | dirname = thisname; |
| 832 | } else { |
| 833 | basename = thisname; |
| 834 | dirname = NULL; |
| 835 | dirname_len = 0; |
| 836 | } |
| 837 | basename_len = strlen(basename) + 1; /* count the '\0' */ |
| 838 | |
| 839 | #ifdef SUPPORT_LINKS |
| 840 | linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0; |
| 841 | #else |
| 842 | linkname_len = 0; |
| 843 | #endif |
| 844 | |
| 845 | sum_len = always_checksum && am_sender && S_ISREG(st.st_mode) |
| 846 | ? MD4_SUM_LENGTH : 0; |
| 847 | |
| 848 | alloc_len = file_struct_len + dirname_len + basename_len |
| 849 | + linkname_len + sum_len; |
| 850 | if (flist) |
| 851 | bp = pool_alloc(flist->file_pool, alloc_len, "make_file"); |
| 852 | else { |
| 853 | if (!(bp = new_array(char, alloc_len))) |
| 854 | out_of_memory("make_file"); |
| 855 | } |
| 856 | |
| 857 | file = (struct file_struct *)bp; |
| 858 | memset(bp, 0, file_struct_len); |
| 859 | bp += file_struct_len; |
| 860 | |
| 861 | file->flags = flags; |
| 862 | file->modtime = st.st_mtime; |
| 863 | file->length = st.st_size; |
| 864 | file->mode = st.st_mode; |
| 865 | file->uid = st.st_uid; |
| 866 | file->gid = st.st_gid; |
| 867 | |
| 868 | #ifdef SUPPORT_HARD_LINKS |
| 869 | if (flist && flist->hlink_pool) { |
| 870 | if (protocol_version < 28) { |
| 871 | if (S_ISREG(st.st_mode)) |
| 872 | file->link_u.idev = pool_talloc( |
| 873 | flist->hlink_pool, struct idev, 1, |
| 874 | "inode_table"); |
| 875 | } else { |
| 876 | if (!S_ISDIR(st.st_mode) && st.st_nlink > 1) |
| 877 | file->link_u.idev = pool_talloc( |
| 878 | flist->hlink_pool, struct idev, 1, |
| 879 | "inode_table"); |
| 880 | } |
| 881 | } |
| 882 | if (file->link_u.idev) { |
| 883 | file->F_DEV = st.st_dev; |
| 884 | file->F_INODE = st.st_ino; |
| 885 | } |
| 886 | #endif |
| 887 | |
| 888 | if (dirname_len) { |
| 889 | file->dirname = lastdir = bp; |
| 890 | lastdir_len = dirname_len - 1; |
| 891 | memcpy(bp, dirname, dirname_len - 1); |
| 892 | bp += dirname_len; |
| 893 | bp[-1] = '\0'; |
| 894 | } else if (dirname) |
| 895 | file->dirname = dirname; |
| 896 | |
| 897 | file->basename = bp; |
| 898 | memcpy(bp, basename, basename_len); |
| 899 | bp += basename_len; |
| 900 | |
| 901 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
| 902 | if ((preserve_devices && IS_DEVICE(st.st_mode)) |
| 903 | || (preserve_specials && IS_SPECIAL(st.st_mode))) |
| 904 | file->u.rdev = st.st_rdev; |
| 905 | #endif |
| 906 | |
| 907 | #ifdef SUPPORT_LINKS |
| 908 | if (linkname_len) { |
| 909 | file->u.link = bp; |
| 910 | memcpy(bp, linkname, linkname_len); |
| 911 | bp += linkname_len; |
| 912 | } |
| 913 | #endif |
| 914 | |
| 915 | if (sum_len) { |
| 916 | file->u.sum = bp; |
| 917 | file_checksum(thisname, bp, st.st_size); |
| 918 | /*bp += sum_len;*/ |
| 919 | } |
| 920 | |
| 921 | file->dir.root = flist_dir; |
| 922 | |
| 923 | /* This code is only used by the receiver when it is building |
| 924 | * a list of files for a delete pass. */ |
| 925 | if (keep_dirlinks && linkname_len && flist) { |
| 926 | STRUCT_STAT st2; |
| 927 | int save_mode = file->mode; |
| 928 | file->mode = S_IFDIR; /* Find a directory with our name. */ |
| 929 | if (flist_find(the_file_list, file) >= 0 |
| 930 | && do_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) { |
| 931 | file->modtime = st2.st_mtime; |
| 932 | file->length = st2.st_size; |
| 933 | file->mode = st2.st_mode; |
| 934 | file->uid = st2.st_uid; |
| 935 | file->gid = st2.st_gid; |
| 936 | file->u.link = NULL; |
| 937 | } else |
| 938 | file->mode = save_mode; |
| 939 | } |
| 940 | |
| 941 | if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) |
| 942 | stats.total_size += st.st_size; |
| 943 | |
| 944 | return file; |
| 945 | } |
| 946 | |
| 947 | static struct file_struct *send_file_name(int f, struct file_list *flist, |
| 948 | char *fname, STRUCT_STAT *stp, |
| 949 | unsigned short flags) |
| 950 | { |
| 951 | struct file_struct *file; |
| 952 | |
| 953 | file = make_file(fname, flist, stp, flags, |
| 954 | f == -2 ? SERVER_FILTERS : ALL_FILTERS); |
| 955 | if (!file) |
| 956 | return NULL; |
| 957 | |
| 958 | if (chmod_modes && !S_ISLNK(file->mode)) |
| 959 | file->mode = tweak_mode(file->mode, chmod_modes); |
| 960 | |
| 961 | maybe_emit_filelist_progress(flist->count + flist_count_offset); |
| 962 | |
| 963 | flist_expand(flist); |
| 964 | |
| 965 | if (file->basename[0]) { |
| 966 | flist->files[flist->count++] = file; |
| 967 | send_file_entry(file, f); |
| 968 | } |
| 969 | return file; |
| 970 | } |
| 971 | |
| 972 | static void send_if_directory(int f, struct file_list *flist, |
| 973 | struct file_struct *file, |
| 974 | char *fbuf, unsigned int ol) |
| 975 | { |
| 976 | char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/'); |
| 977 | |
| 978 | if (S_ISDIR(file->mode) |
| 979 | && !(file->flags & FLAG_MOUNT_POINT) && f_name(file, fbuf)) { |
| 980 | void *save_filters; |
| 981 | unsigned int len = strlen(fbuf); |
| 982 | if (len > 1 && fbuf[len-1] == '/') |
| 983 | fbuf[--len] = '\0'; |
| 984 | if (len >= MAXPATHLEN - 1) { |
| 985 | io_error |= IOERR_GENERAL; |
| 986 | rprintf(FERROR, "skipping long-named directory: %s\n", |
| 987 | full_fname(fbuf)); |
| 988 | return; |
| 989 | } |
| 990 | save_filters = push_local_filters(fbuf, len); |
| 991 | send_directory(f, flist, fbuf, len); |
| 992 | pop_local_filters(save_filters); |
| 993 | fbuf[ol] = '\0'; |
| 994 | if (is_dot_dir) |
| 995 | fbuf[ol-1] = '.'; |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | /* This function is normally called by the sender, but the receiving side also |
| 1000 | * calls it from get_dirlist() with f set to -1 so that we just construct the |
| 1001 | * file list in memory without sending it over the wire. Also, get_dirlist() |
| 1002 | * might call this with f set to -2, which also indicates that local filter |
| 1003 | * rules should be ignored. */ |
| 1004 | static void send_directory(int f, struct file_list *flist, |
| 1005 | char *fbuf, int len) |
| 1006 | { |
| 1007 | struct dirent *di; |
| 1008 | unsigned remainder; |
| 1009 | char *p; |
| 1010 | DIR *d; |
| 1011 | int start = flist->count; |
| 1012 | |
| 1013 | if (!(d = opendir(fbuf))) { |
| 1014 | io_error |= IOERR_GENERAL; |
| 1015 | rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf)); |
| 1016 | return; |
| 1017 | } |
| 1018 | |
| 1019 | p = fbuf + len; |
| 1020 | if (len != 1 || *fbuf != '/') |
| 1021 | *p++ = '/'; |
| 1022 | *p = '\0'; |
| 1023 | remainder = MAXPATHLEN - (p - fbuf); |
| 1024 | |
| 1025 | for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) { |
| 1026 | char *dname = d_name(di); |
| 1027 | if (dname[0] == '.' && (dname[1] == '\0' |
| 1028 | || (dname[1] == '.' && dname[2] == '\0'))) |
| 1029 | continue; |
| 1030 | if (strlcpy(p, dname, remainder) >= remainder) { |
| 1031 | io_error |= IOERR_GENERAL; |
| 1032 | rprintf(FINFO, |
| 1033 | "cannot send long-named file %s\n", |
| 1034 | full_fname(fbuf)); |
| 1035 | continue; |
| 1036 | } |
| 1037 | |
| 1038 | send_file_name(f, flist, fbuf, NULL, 0); |
| 1039 | } |
| 1040 | |
| 1041 | fbuf[len] = '\0'; |
| 1042 | |
| 1043 | if (errno) { |
| 1044 | io_error |= IOERR_GENERAL; |
| 1045 | rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf)); |
| 1046 | } |
| 1047 | |
| 1048 | closedir(d); |
| 1049 | |
| 1050 | if (recurse) { |
| 1051 | int i, end = flist->count - 1; |
| 1052 | for (i = start; i <= end; i++) |
| 1053 | send_if_directory(f, flist, flist->files[i], fbuf, len); |
| 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | struct file_list *send_file_list(int f, int argc, char *argv[]) |
| 1058 | { |
| 1059 | int len; |
| 1060 | STRUCT_STAT st; |
| 1061 | char *p, *dir, olddir[sizeof curr_dir]; |
| 1062 | char lastpath[MAXPATHLEN] = ""; |
| 1063 | struct file_list *flist; |
| 1064 | struct timeval start_tv, end_tv; |
| 1065 | int64 start_write; |
| 1066 | int use_ff_fd = 0; |
| 1067 | |
| 1068 | rprintf(FLOG, "building file list\n"); |
| 1069 | if (show_filelist_p()) |
| 1070 | start_filelist_progress("building file list"); |
| 1071 | |
| 1072 | start_write = stats.total_written; |
| 1073 | gettimeofday(&start_tv, NULL); |
| 1074 | |
| 1075 | flist = flist_new(WITH_HLINK, "send_file_list"); |
| 1076 | |
| 1077 | io_start_buffering_out(); |
| 1078 | if (filesfrom_fd >= 0) { |
| 1079 | if (argv[0] && !push_dir(argv[0], 0)) { |
| 1080 | rsyserr(FERROR, errno, "push_dir %s failed", |
| 1081 | full_fname(argv[0])); |
| 1082 | exit_cleanup(RERR_FILESELECT); |
| 1083 | } |
| 1084 | use_ff_fd = 1; |
| 1085 | } |
| 1086 | |
| 1087 | while (1) { |
| 1088 | char fbuf[MAXPATHLEN]; |
| 1089 | char *fn; |
| 1090 | int is_dot_dir; |
| 1091 | |
| 1092 | if (use_ff_fd) { |
| 1093 | if (read_filesfrom_line(filesfrom_fd, fbuf) == 0) |
| 1094 | break; |
| 1095 | sanitize_path(fbuf, fbuf, "", 0, NULL); |
| 1096 | } else { |
| 1097 | if (argc-- == 0) |
| 1098 | break; |
| 1099 | strlcpy(fbuf, *argv++, MAXPATHLEN); |
| 1100 | if (sanitize_paths) |
| 1101 | sanitize_path(fbuf, fbuf, "", 0, NULL); |
| 1102 | } |
| 1103 | |
| 1104 | len = strlen(fbuf); |
| 1105 | if (relative_paths) { |
| 1106 | /* We clean up fbuf below. */ |
| 1107 | is_dot_dir = 0; |
| 1108 | } else if (!len || fbuf[len - 1] == '/') { |
| 1109 | if (len == 2 && fbuf[0] == '.') { |
| 1110 | /* Turn "./" into just "." rather than "./." */ |
| 1111 | fbuf[1] = '\0'; |
| 1112 | } else { |
| 1113 | if (len + 1 >= MAXPATHLEN) |
| 1114 | overflow_exit("send_file_list"); |
| 1115 | fbuf[len++] = '.'; |
| 1116 | fbuf[len] = '\0'; |
| 1117 | } |
| 1118 | is_dot_dir = 1; |
| 1119 | } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.' |
| 1120 | && (len == 2 || fbuf[len-3] == '/')) { |
| 1121 | if (len + 2 >= MAXPATHLEN) |
| 1122 | overflow_exit("send_file_list"); |
| 1123 | fbuf[len++] = '/'; |
| 1124 | fbuf[len++] = '.'; |
| 1125 | fbuf[len] = '\0'; |
| 1126 | is_dot_dir = 1; |
| 1127 | } else { |
| 1128 | is_dot_dir = fbuf[len-1] == '.' |
| 1129 | && (len == 1 || fbuf[len-2] == '/'); |
| 1130 | } |
| 1131 | |
| 1132 | if (link_stat(fbuf, &st, copy_dirlinks) != 0) { |
| 1133 | io_error |= IOERR_GENERAL; |
| 1134 | rsyserr(FERROR, errno, "link_stat %s failed", |
| 1135 | full_fname(fbuf)); |
| 1136 | continue; |
| 1137 | } |
| 1138 | |
| 1139 | if (S_ISDIR(st.st_mode) && !xfer_dirs) { |
| 1140 | rprintf(FINFO, "skipping directory %s\n", fbuf); |
| 1141 | continue; |
| 1142 | } |
| 1143 | |
| 1144 | dir = NULL; |
| 1145 | olddir[0] = '\0'; |
| 1146 | |
| 1147 | if (!relative_paths) { |
| 1148 | p = strrchr(fbuf, '/'); |
| 1149 | if (p) { |
| 1150 | *p = '\0'; |
| 1151 | if (p == fbuf) |
| 1152 | dir = "/"; |
| 1153 | else |
| 1154 | dir = fbuf; |
| 1155 | len -= p - fbuf + 1; |
| 1156 | fn = p + 1; |
| 1157 | } else |
| 1158 | fn = fbuf; |
| 1159 | } else { |
| 1160 | if ((p = strstr(fbuf, "/./")) != NULL) { |
| 1161 | *p = '\0'; |
| 1162 | if (p == fbuf) |
| 1163 | dir = "/"; |
| 1164 | else |
| 1165 | dir = fbuf; |
| 1166 | len -= p - fbuf + 3; |
| 1167 | fn = p + 3; |
| 1168 | } else |
| 1169 | fn = fbuf; |
| 1170 | /* Get rid of trailing "/" and "/.". */ |
| 1171 | while (len) { |
| 1172 | if (fn[len - 1] == '/') { |
| 1173 | is_dot_dir = 1; |
| 1174 | if (!--len && !dir) { |
| 1175 | len++; |
| 1176 | break; |
| 1177 | } |
| 1178 | } |
| 1179 | else if (len >= 2 && fn[len - 1] == '.' |
| 1180 | && fn[len - 2] == '/') { |
| 1181 | is_dot_dir = 1; |
| 1182 | if (!(len -= 2) && !dir) { |
| 1183 | len++; |
| 1184 | break; |
| 1185 | } |
| 1186 | } else |
| 1187 | break; |
| 1188 | } |
| 1189 | if (len == 1 && fn[0] == '/') |
| 1190 | fn[len++] = '.'; |
| 1191 | fn[len] = '\0'; |
| 1192 | /* Reject a ".." dir in the active part of the path. */ |
| 1193 | for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) { |
| 1194 | if ((p[2] == '/' || p[2] == '\0') |
| 1195 | && (p == fn || p[-1] == '/')) { |
| 1196 | rprintf(FERROR, |
| 1197 | "found \"..\" dir in relative path: %s\n", |
| 1198 | fbuf); |
| 1199 | exit_cleanup(RERR_SYNTAX); |
| 1200 | } |
| 1201 | } |
| 1202 | } |
| 1203 | |
| 1204 | if (!*fn) { |
| 1205 | len = 1; |
| 1206 | fn = "."; |
| 1207 | } |
| 1208 | |
| 1209 | if (dir && *dir) { |
| 1210 | static char *lastdir; |
| 1211 | static int lastdir_len; |
| 1212 | |
| 1213 | strlcpy(olddir, curr_dir, sizeof olddir); |
| 1214 | |
| 1215 | if (!push_dir(dir, 0)) { |
| 1216 | io_error |= IOERR_GENERAL; |
| 1217 | rsyserr(FERROR, errno, "push_dir %s failed", |
| 1218 | full_fname(dir)); |
| 1219 | continue; |
| 1220 | } |
| 1221 | |
| 1222 | if (lastdir && strcmp(lastdir, dir) == 0) { |
| 1223 | flist_dir = lastdir; |
| 1224 | flist_dir_len = lastdir_len; |
| 1225 | } else { |
| 1226 | flist_dir = lastdir = strdup(dir); |
| 1227 | flist_dir_len = lastdir_len = strlen(dir); |
| 1228 | } |
| 1229 | } |
| 1230 | |
| 1231 | if (fn != fbuf) |
| 1232 | memmove(fbuf, fn, len + 1); |
| 1233 | |
| 1234 | if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) { |
| 1235 | /* Send the implied directories at the start of the |
| 1236 | * source spec, so we get their permissions right. */ |
| 1237 | char *lp = lastpath, *slash = fbuf; |
| 1238 | *p = '\0'; |
| 1239 | /* Skip any initial directories in our path that we |
| 1240 | * have in common with lastpath. */ |
| 1241 | for (fn = fbuf; *fn && *lp == *fn; lp++, fn++) { |
| 1242 | if (*fn == '/') |
| 1243 | slash = fn; |
| 1244 | } |
| 1245 | *p = '/'; |
| 1246 | if (fn != p || (*lp && *lp != '/')) { |
| 1247 | int save_copy_links = copy_links; |
| 1248 | int save_xfer_dirs = xfer_dirs; |
| 1249 | copy_links |= copy_unsafe_links; |
| 1250 | xfer_dirs = 1; |
| 1251 | while ((slash = strchr(slash+1, '/')) != 0) { |
| 1252 | *slash = '\0'; |
| 1253 | send_file_name(f, flist, fbuf, NULL, 0); |
| 1254 | *slash = '/'; |
| 1255 | } |
| 1256 | copy_links = save_copy_links; |
| 1257 | xfer_dirs = save_xfer_dirs; |
| 1258 | *p = '\0'; |
| 1259 | strlcpy(lastpath, fbuf, sizeof lastpath); |
| 1260 | *p = '/'; |
| 1261 | } |
| 1262 | } |
| 1263 | |
| 1264 | if (one_file_system) |
| 1265 | filesystem_dev = st.st_dev; |
| 1266 | |
| 1267 | if (recurse || (xfer_dirs && is_dot_dir)) { |
| 1268 | struct file_struct *file; |
| 1269 | file = send_file_name(f, flist, fbuf, &st, FLAG_TOP_DIR); |
| 1270 | if (file) |
| 1271 | send_if_directory(f, flist, file, fbuf, len); |
| 1272 | } else |
| 1273 | send_file_name(f, flist, fbuf, &st, 0); |
| 1274 | |
| 1275 | if (olddir[0]) { |
| 1276 | flist_dir = NULL; |
| 1277 | flist_dir_len = 0; |
| 1278 | if (!pop_dir(olddir)) { |
| 1279 | rsyserr(FERROR, errno, "pop_dir %s failed", |
| 1280 | full_fname(olddir)); |
| 1281 | exit_cleanup(RERR_FILESELECT); |
| 1282 | } |
| 1283 | } |
| 1284 | } |
| 1285 | |
| 1286 | gettimeofday(&end_tv, NULL); |
| 1287 | stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000 |
| 1288 | + (end_tv.tv_usec - start_tv.tv_usec) / 1000; |
| 1289 | if (stats.flist_buildtime == 0) |
| 1290 | stats.flist_buildtime = 1; |
| 1291 | start_tv = end_tv; |
| 1292 | |
| 1293 | send_file_entry(NULL, f); |
| 1294 | |
| 1295 | if (show_filelist_p()) |
| 1296 | finish_filelist_progress(flist); |
| 1297 | |
| 1298 | gettimeofday(&end_tv, NULL); |
| 1299 | stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000 |
| 1300 | + (end_tv.tv_usec - start_tv.tv_usec) / 1000; |
| 1301 | |
| 1302 | if (flist->hlink_pool) { |
| 1303 | pool_destroy(flist->hlink_pool); |
| 1304 | flist->hlink_pool = NULL; |
| 1305 | } |
| 1306 | |
| 1307 | /* Sort the list without removing any duplicates. This allows the |
| 1308 | * receiving side to ask for any name they like, which gives us the |
| 1309 | * flexibility to change the way we unduplicate names in the future |
| 1310 | * without causing a compatibility problem with older versions. */ |
| 1311 | clean_flist(flist, 0, 0); |
| 1312 | |
| 1313 | send_uid_list(f); |
| 1314 | |
| 1315 | /* send the io_error flag */ |
| 1316 | write_int(f, lp_ignore_errors(module_id) ? 0 : io_error); |
| 1317 | |
| 1318 | io_end_buffering(); |
| 1319 | stats.flist_size = stats.total_written - start_write; |
| 1320 | stats.num_files = flist->count; |
| 1321 | |
| 1322 | if (verbose > 3) |
| 1323 | output_flist(flist); |
| 1324 | |
| 1325 | if (verbose > 2) |
| 1326 | rprintf(FINFO, "send_file_list done\n"); |
| 1327 | |
| 1328 | return flist; |
| 1329 | } |
| 1330 | |
| 1331 | struct file_list *recv_file_list(int f) |
| 1332 | { |
| 1333 | struct file_list *flist; |
| 1334 | unsigned short flags; |
| 1335 | int64 start_read; |
| 1336 | |
| 1337 | rprintf(FLOG, "receiving file list\n"); |
| 1338 | if (show_filelist_p()) |
| 1339 | start_filelist_progress("receiving file list"); |
| 1340 | |
| 1341 | start_read = stats.total_read; |
| 1342 | |
| 1343 | flist = flist_new(WITH_HLINK, "recv_file_list"); |
| 1344 | |
| 1345 | flist->count = 0; |
| 1346 | flist->malloced = 1000; |
| 1347 | flist->files = new_array(struct file_struct *, flist->malloced); |
| 1348 | if (!flist->files) |
| 1349 | goto oom; |
| 1350 | |
| 1351 | while ((flags = read_byte(f)) != 0) { |
| 1352 | struct file_struct *file; |
| 1353 | |
| 1354 | flist_expand(flist); |
| 1355 | |
| 1356 | if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS)) |
| 1357 | flags |= read_byte(f) << 8; |
| 1358 | file = receive_file_entry(flist, flags, f); |
| 1359 | |
| 1360 | if (S_ISREG(file->mode) || S_ISLNK(file->mode)) |
| 1361 | stats.total_size += file->length; |
| 1362 | |
| 1363 | flist->files[flist->count++] = file; |
| 1364 | |
| 1365 | maybe_emit_filelist_progress(flist->count); |
| 1366 | |
| 1367 | if (verbose > 2) { |
| 1368 | rprintf(FINFO, "recv_file_name(%s)\n", |
| 1369 | f_name(file, NULL)); |
| 1370 | } |
| 1371 | } |
| 1372 | receive_file_entry(NULL, 0, 0); /* Signal that we're done. */ |
| 1373 | |
| 1374 | if (verbose > 2) |
| 1375 | rprintf(FINFO, "received %d names\n", flist->count); |
| 1376 | |
| 1377 | if (show_filelist_p()) |
| 1378 | finish_filelist_progress(flist); |
| 1379 | |
| 1380 | clean_flist(flist, relative_paths, 1); |
| 1381 | |
| 1382 | if (f >= 0) { |
| 1383 | recv_uid_list(f, flist); |
| 1384 | |
| 1385 | /* Recv the io_error flag */ |
| 1386 | if (lp_ignore_errors(module_id) || ignore_errors) |
| 1387 | read_int(f); |
| 1388 | else |
| 1389 | io_error |= read_int(f); |
| 1390 | } |
| 1391 | |
| 1392 | if (verbose > 3) |
| 1393 | output_flist(flist); |
| 1394 | |
| 1395 | if (list_only) { |
| 1396 | int i; |
| 1397 | for (i = 0; i < flist->count; i++) |
| 1398 | list_file_entry(flist->files[i]); |
| 1399 | } |
| 1400 | |
| 1401 | if (verbose > 2) |
| 1402 | rprintf(FINFO, "recv_file_list done\n"); |
| 1403 | |
| 1404 | stats.flist_size = stats.total_read - start_read; |
| 1405 | stats.num_files = flist->count; |
| 1406 | |
| 1407 | return flist; |
| 1408 | |
| 1409 | oom: |
| 1410 | out_of_memory("recv_file_list"); |
| 1411 | return NULL; /* not reached */ |
| 1412 | } |
| 1413 | |
| 1414 | static int file_compare(struct file_struct **file1, struct file_struct **file2) |
| 1415 | { |
| 1416 | return f_name_cmp(*file1, *file2); |
| 1417 | } |
| 1418 | |
| 1419 | /* Search for an identically-named item in the file list. Note that the |
| 1420 | * items must agree in their directory-ness, or no match is returned. */ |
| 1421 | int flist_find(struct file_list *flist, struct file_struct *f) |
| 1422 | { |
| 1423 | int low = flist->low, high = flist->high; |
| 1424 | int diff, mid, mid_up; |
| 1425 | |
| 1426 | while (low <= high) { |
| 1427 | mid = (low + high) / 2; |
| 1428 | if (flist->files[mid]->basename) |
| 1429 | mid_up = mid; |
| 1430 | else { |
| 1431 | /* Scan for the next non-empty entry using the cached |
| 1432 | * distance values. If the value isn't fully up-to- |
| 1433 | * date, update it. */ |
| 1434 | mid_up = mid + flist->files[mid]->dir.depth; |
| 1435 | if (!flist->files[mid_up]->basename) { |
| 1436 | do { |
| 1437 | mid_up += flist->files[mid_up]->dir.depth; |
| 1438 | } while (!flist->files[mid_up]->basename); |
| 1439 | flist->files[mid]->dir.depth = mid_up - mid; |
| 1440 | } |
| 1441 | if (mid_up > high) { |
| 1442 | /* If there's nothing left above us, set high to |
| 1443 | * a non-empty entry below us and continue. */ |
| 1444 | high = mid - flist->files[mid]->length; |
| 1445 | if (!flist->files[high]->basename) { |
| 1446 | do { |
| 1447 | high -= flist->files[high]->length; |
| 1448 | } while (!flist->files[high]->basename); |
| 1449 | flist->files[mid]->length = mid - high; |
| 1450 | } |
| 1451 | continue; |
| 1452 | } |
| 1453 | } |
| 1454 | diff = f_name_cmp(flist->files[mid_up], f); |
| 1455 | if (diff == 0) { |
| 1456 | if (protocol_version < 29 |
| 1457 | && S_ISDIR(flist->files[mid_up]->mode) |
| 1458 | != S_ISDIR(f->mode)) |
| 1459 | return -1; |
| 1460 | return mid_up; |
| 1461 | } |
| 1462 | if (diff < 0) |
| 1463 | low = mid_up + 1; |
| 1464 | else |
| 1465 | high = mid - 1; |
| 1466 | } |
| 1467 | return -1; |
| 1468 | } |
| 1469 | |
| 1470 | /* |
| 1471 | * Free up any resources a file_struct has allocated |
| 1472 | * and clear the file. |
| 1473 | */ |
| 1474 | void clear_file(struct file_struct *file, struct file_list *flist) |
| 1475 | { |
| 1476 | if (flist->hlink_pool && file->link_u.idev) |
| 1477 | pool_free(flist->hlink_pool, 0, file->link_u.idev); |
| 1478 | memset(file, 0, file_struct_len); |
| 1479 | /* In an empty entry, dir.depth is an offset to the next non-empty |
| 1480 | * entry. Likewise for length in the opposite direction. We assume |
| 1481 | * that we're alone for now since flist_find() will adjust the counts |
| 1482 | * it runs into that aren't up-to-date. */ |
| 1483 | file->length = file->dir.depth = 1; |
| 1484 | } |
| 1485 | |
| 1486 | /* |
| 1487 | * allocate a new file list |
| 1488 | */ |
| 1489 | struct file_list *flist_new(int with_hlink, char *msg) |
| 1490 | { |
| 1491 | struct file_list *flist; |
| 1492 | |
| 1493 | flist = new(struct file_list); |
| 1494 | if (!flist) |
| 1495 | out_of_memory(msg); |
| 1496 | |
| 1497 | memset(flist, 0, sizeof (struct file_list)); |
| 1498 | |
| 1499 | if (!(flist->file_pool = pool_create(FILE_EXTENT, 0, |
| 1500 | out_of_memory, POOL_INTERN))) |
| 1501 | out_of_memory(msg); |
| 1502 | |
| 1503 | #ifdef SUPPORT_HARD_LINKS |
| 1504 | if (with_hlink && preserve_hard_links) { |
| 1505 | if (!(flist->hlink_pool = pool_create(HLINK_EXTENT, |
| 1506 | sizeof (struct idev), out_of_memory, POOL_INTERN))) |
| 1507 | out_of_memory(msg); |
| 1508 | } |
| 1509 | #endif |
| 1510 | |
| 1511 | return flist; |
| 1512 | } |
| 1513 | |
| 1514 | /* |
| 1515 | * free up all elements in a flist |
| 1516 | */ |
| 1517 | void flist_free(struct file_list *flist) |
| 1518 | { |
| 1519 | pool_destroy(flist->file_pool); |
| 1520 | pool_destroy(flist->hlink_pool); |
| 1521 | free(flist->files); |
| 1522 | free(flist); |
| 1523 | } |
| 1524 | |
| 1525 | /* |
| 1526 | * This routine ensures we don't have any duplicate names in our file list. |
| 1527 | * duplicate names can cause corruption because of the pipelining |
| 1528 | */ |
| 1529 | static void clean_flist(struct file_list *flist, int strip_root, int no_dups) |
| 1530 | { |
| 1531 | char fbuf[MAXPATHLEN]; |
| 1532 | int i, prev_i = 0; |
| 1533 | |
| 1534 | if (!flist) |
| 1535 | return; |
| 1536 | if (flist->count == 0) { |
| 1537 | flist->high = -1; |
| 1538 | return; |
| 1539 | } |
| 1540 | |
| 1541 | qsort(flist->files, flist->count, |
| 1542 | sizeof flist->files[0], (int (*)())file_compare); |
| 1543 | |
| 1544 | for (i = no_dups? 0 : flist->count; i < flist->count; i++) { |
| 1545 | if (flist->files[i]->basename) { |
| 1546 | prev_i = i; |
| 1547 | break; |
| 1548 | } |
| 1549 | } |
| 1550 | flist->low = prev_i; |
| 1551 | while (++i < flist->count) { |
| 1552 | int j; |
| 1553 | struct file_struct *file = flist->files[i]; |
| 1554 | |
| 1555 | if (!file->basename) |
| 1556 | continue; |
| 1557 | if (f_name_cmp(file, flist->files[prev_i]) == 0) |
| 1558 | j = prev_i; |
| 1559 | else if (protocol_version >= 29 && S_ISDIR(file->mode)) { |
| 1560 | int save_mode = file->mode; |
| 1561 | /* Make sure that this directory doesn't duplicate a |
| 1562 | * non-directory earlier in the list. */ |
| 1563 | flist->high = prev_i; |
| 1564 | file->mode = S_IFREG; |
| 1565 | j = flist_find(flist, file); |
| 1566 | file->mode = save_mode; |
| 1567 | } else |
| 1568 | j = -1; |
| 1569 | if (j >= 0) { |
| 1570 | struct file_struct *fp = flist->files[j]; |
| 1571 | int keep, drop; |
| 1572 | /* If one is a dir and the other is not, we want to |
| 1573 | * keep the dir because it might have contents in the |
| 1574 | * list. */ |
| 1575 | if (S_ISDIR(file->mode) != S_ISDIR(fp->mode)) { |
| 1576 | if (S_ISDIR(file->mode)) |
| 1577 | keep = i, drop = j; |
| 1578 | else |
| 1579 | keep = j, drop = i; |
| 1580 | } else |
| 1581 | keep = j, drop = i; |
| 1582 | if (verbose > 1 && !am_server) { |
| 1583 | rprintf(FINFO, |
| 1584 | "removing duplicate name %s from file list (%d)\n", |
| 1585 | f_name(file, fbuf), drop); |
| 1586 | } |
| 1587 | /* Make sure we don't lose track of a user-specified |
| 1588 | * top directory. */ |
| 1589 | flist->files[keep]->flags |= flist->files[drop]->flags |
| 1590 | & (FLAG_TOP_DIR|FLAG_DEL_HERE); |
| 1591 | |
| 1592 | clear_file(flist->files[drop], flist); |
| 1593 | |
| 1594 | if (keep == i) { |
| 1595 | if (flist->low == drop) { |
| 1596 | for (j = drop + 1; |
| 1597 | j < i && !flist->files[j]->basename; |
| 1598 | j++) {} |
| 1599 | flist->low = j; |
| 1600 | } |
| 1601 | prev_i = i; |
| 1602 | } |
| 1603 | } else |
| 1604 | prev_i = i; |
| 1605 | } |
| 1606 | flist->high = no_dups ? prev_i : flist->count - 1; |
| 1607 | |
| 1608 | if (strip_root) { |
| 1609 | /* We need to strip off the leading slashes for relative |
| 1610 | * paths, but this must be done _after_ the sorting phase. */ |
| 1611 | for (i = flist->low; i <= flist->high; i++) { |
| 1612 | struct file_struct *file = flist->files[i]; |
| 1613 | |
| 1614 | if (!file->dirname) |
| 1615 | continue; |
| 1616 | while (*file->dirname == '/') |
| 1617 | file->dirname++; |
| 1618 | if (!*file->dirname) |
| 1619 | file->dirname = NULL; |
| 1620 | } |
| 1621 | } |
| 1622 | |
| 1623 | if (prune_empty_dirs && no_dups) { |
| 1624 | int j, prev_depth = 0; |
| 1625 | |
| 1626 | prev_i = 0; /* It's OK that this isn't really true. */ |
| 1627 | |
| 1628 | for (i = flist->low; i <= flist->high; i++) { |
| 1629 | struct file_struct *fp, *file = flist->files[i]; |
| 1630 | |
| 1631 | /* This temporarily abuses the dir.depth value for a |
| 1632 | * directory that is in a chain that might get pruned. |
| 1633 | * We restore the old value if it gets a reprieve. */ |
| 1634 | if (S_ISDIR(file->mode) && file->dir.depth) { |
| 1635 | /* Dump empty dirs when coming back down. */ |
| 1636 | for (j = prev_depth; j >= file->dir.depth; j--) { |
| 1637 | fp = flist->files[prev_i]; |
| 1638 | if (fp->dir.depth >= 0) |
| 1639 | break; |
| 1640 | prev_i = -fp->dir.depth-1; |
| 1641 | clear_file(fp, flist); |
| 1642 | } |
| 1643 | prev_depth = file->dir.depth; |
| 1644 | if (is_excluded(f_name(file, fbuf), 1, |
| 1645 | ALL_FILTERS)) { |
| 1646 | /* Keep dirs through this dir. */ |
| 1647 | for (j = prev_depth-1; ; j--) { |
| 1648 | fp = flist->files[prev_i]; |
| 1649 | if (fp->dir.depth >= 0) |
| 1650 | break; |
| 1651 | prev_i = -fp->dir.depth-1; |
| 1652 | fp->dir.depth = j; |
| 1653 | } |
| 1654 | } else |
| 1655 | file->dir.depth = -prev_i-1; |
| 1656 | prev_i = i; |
| 1657 | } else { |
| 1658 | /* Keep dirs through this non-dir. */ |
| 1659 | for (j = prev_depth; ; j--) { |
| 1660 | fp = flist->files[prev_i]; |
| 1661 | if (fp->dir.depth >= 0) |
| 1662 | break; |
| 1663 | prev_i = -fp->dir.depth-1; |
| 1664 | fp->dir.depth = j; |
| 1665 | } |
| 1666 | } |
| 1667 | } |
| 1668 | /* Dump empty all remaining empty dirs. */ |
| 1669 | while (1) { |
| 1670 | struct file_struct *fp = flist->files[prev_i]; |
| 1671 | if (fp->dir.depth >= 0) |
| 1672 | break; |
| 1673 | prev_i = -fp->dir.depth-1; |
| 1674 | clear_file(fp, flist); |
| 1675 | } |
| 1676 | |
| 1677 | for (i = flist->low; i <= flist->high; i++) { |
| 1678 | if (flist->files[i]->basename) |
| 1679 | break; |
| 1680 | } |
| 1681 | flist->low = i; |
| 1682 | for (i = flist->high; i >= flist->low; i--) { |
| 1683 | if (flist->files[i]->basename) |
| 1684 | break; |
| 1685 | } |
| 1686 | flist->high = i; |
| 1687 | } |
| 1688 | } |
| 1689 | |
| 1690 | static void output_flist(struct file_list *flist) |
| 1691 | { |
| 1692 | char uidbuf[16], gidbuf[16], depthbuf[16]; |
| 1693 | struct file_struct *file; |
| 1694 | const char *who = who_am_i(); |
| 1695 | int i; |
| 1696 | |
| 1697 | for (i = 0; i < flist->count; i++) { |
| 1698 | file = flist->files[i]; |
| 1699 | if ((am_root || am_sender) && preserve_uid) |
| 1700 | snprintf(uidbuf, sizeof uidbuf, " uid=%ld", (long)file->uid); |
| 1701 | else |
| 1702 | *uidbuf = '\0'; |
| 1703 | if (preserve_gid && file->gid != GID_NONE) |
| 1704 | snprintf(gidbuf, sizeof gidbuf, " gid=%ld", (long)file->gid); |
| 1705 | else |
| 1706 | *gidbuf = '\0'; |
| 1707 | if (!am_sender) |
| 1708 | snprintf(depthbuf, sizeof depthbuf, "%d", file->dir.depth); |
| 1709 | rprintf(FINFO, "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n", |
| 1710 | who, i, am_sender ? NS(file->dir.root) : depthbuf, |
| 1711 | file->dirname ? file->dirname : "", |
| 1712 | file->dirname ? "/" : "", NS(file->basename), |
| 1713 | S_ISDIR(file->mode) ? "/" : "", (int)file->mode, |
| 1714 | (double)file->length, uidbuf, gidbuf, file->flags); |
| 1715 | } |
| 1716 | } |
| 1717 | |
| 1718 | enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING }; |
| 1719 | enum fnc_type { t_PATH, t_ITEM }; |
| 1720 | |
| 1721 | /* Compare the names of two file_struct entities, similar to how strcmp() |
| 1722 | * would do if it were operating on the joined strings. |
| 1723 | * |
| 1724 | * Some differences beginning with protocol_version 29: (1) directory names |
| 1725 | * are compared with an assumed trailing slash so that they compare in a |
| 1726 | * way that would cause them to sort immediately prior to any content they |
| 1727 | * may have; (2) a directory of any name compares after a non-directory of |
| 1728 | * any name at the same depth; (3) a directory with name "." compares prior |
| 1729 | * to anything else. These changes mean that a directory and a non-dir |
| 1730 | * with the same name will not compare as equal (protocol_version >= 29). |
| 1731 | * |
| 1732 | * The dirname component can be an empty string, but the basename component |
| 1733 | * cannot (and never is in the current codebase). The basename component |
| 1734 | * may be NULL (for a removed item), in which case it is considered to be |
| 1735 | * after any existing item. */ |
| 1736 | int f_name_cmp(struct file_struct *f1, struct file_struct *f2) |
| 1737 | { |
| 1738 | int dif; |
| 1739 | const uchar *c1, *c2; |
| 1740 | enum fnc_state state1, state2; |
| 1741 | enum fnc_type type1, type2; |
| 1742 | enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM; |
| 1743 | |
| 1744 | if (!f1 || !f1->basename) { |
| 1745 | if (!f2 || !f2->basename) |
| 1746 | return 0; |
| 1747 | return -1; |
| 1748 | } |
| 1749 | if (!f2 || !f2->basename) |
| 1750 | return 1; |
| 1751 | |
| 1752 | c1 = (uchar*)f1->dirname; |
| 1753 | c2 = (uchar*)f2->dirname; |
| 1754 | if (c1 == c2) |
| 1755 | c1 = c2 = NULL; |
| 1756 | if (!c1) { |
| 1757 | type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM; |
| 1758 | c1 = (uchar*)f1->basename; |
| 1759 | if (type1 == t_PATH && *c1 == '.' && !c1[1]) { |
| 1760 | type1 = t_ITEM; |
| 1761 | state1 = s_TRAILING; |
| 1762 | c1 = (uchar*)""; |
| 1763 | } else |
| 1764 | state1 = s_BASE; |
| 1765 | } else { |
| 1766 | type1 = t_path; |
| 1767 | state1 = s_DIR; |
| 1768 | } |
| 1769 | if (!c2) { |
| 1770 | type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM; |
| 1771 | c2 = (uchar*)f2->basename; |
| 1772 | if (type2 == t_PATH && *c2 == '.' && !c2[1]) { |
| 1773 | type2 = t_ITEM; |
| 1774 | state2 = s_TRAILING; |
| 1775 | c2 = (uchar*)""; |
| 1776 | } else |
| 1777 | state2 = s_BASE; |
| 1778 | } else { |
| 1779 | type2 = t_path; |
| 1780 | state2 = s_DIR; |
| 1781 | } |
| 1782 | |
| 1783 | if (type1 != type2) |
| 1784 | return type1 == t_PATH ? 1 : -1; |
| 1785 | |
| 1786 | do { |
| 1787 | if (!*c1) { |
| 1788 | switch (state1) { |
| 1789 | case s_DIR: |
| 1790 | state1 = s_SLASH; |
| 1791 | c1 = (uchar*)"/"; |
| 1792 | break; |
| 1793 | case s_SLASH: |
| 1794 | type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM; |
| 1795 | c1 = (uchar*)f1->basename; |
| 1796 | if (type1 == t_PATH && *c1 == '.' && !c1[1]) { |
| 1797 | type1 = t_ITEM; |
| 1798 | state1 = s_TRAILING; |
| 1799 | c1 = (uchar*)""; |
| 1800 | } else |
| 1801 | state1 = s_BASE; |
| 1802 | break; |
| 1803 | case s_BASE: |
| 1804 | state1 = s_TRAILING; |
| 1805 | if (type1 == t_PATH) { |
| 1806 | c1 = (uchar*)"/"; |
| 1807 | break; |
| 1808 | } |
| 1809 | /* FALL THROUGH */ |
| 1810 | case s_TRAILING: |
| 1811 | type1 = t_ITEM; |
| 1812 | break; |
| 1813 | } |
| 1814 | if (*c2 && type1 != type2) |
| 1815 | return type1 == t_PATH ? 1 : -1; |
| 1816 | } |
| 1817 | if (!*c2) { |
| 1818 | switch (state2) { |
| 1819 | case s_DIR: |
| 1820 | state2 = s_SLASH; |
| 1821 | c2 = (uchar*)"/"; |
| 1822 | break; |
| 1823 | case s_SLASH: |
| 1824 | type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM; |
| 1825 | c2 = (uchar*)f2->basename; |
| 1826 | if (type2 == t_PATH && *c2 == '.' && !c2[1]) { |
| 1827 | type2 = t_ITEM; |
| 1828 | state2 = s_TRAILING; |
| 1829 | c2 = (uchar*)""; |
| 1830 | } else |
| 1831 | state2 = s_BASE; |
| 1832 | break; |
| 1833 | case s_BASE: |
| 1834 | state2 = s_TRAILING; |
| 1835 | if (type2 == t_PATH) { |
| 1836 | c2 = (uchar*)"/"; |
| 1837 | break; |
| 1838 | } |
| 1839 | /* FALL THROUGH */ |
| 1840 | case s_TRAILING: |
| 1841 | if (!*c1) |
| 1842 | return 0; |
| 1843 | type2 = t_ITEM; |
| 1844 | break; |
| 1845 | } |
| 1846 | if (type1 != type2) |
| 1847 | return type1 == t_PATH ? 1 : -1; |
| 1848 | } |
| 1849 | } while ((dif = (int)*c1++ - (int)*c2++) == 0); |
| 1850 | |
| 1851 | return dif; |
| 1852 | } |
| 1853 | |
| 1854 | /* Return a copy of the full filename of a flist entry, using the indicated |
| 1855 | * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is |
| 1856 | * done because we checked the size when creating the file_struct entry. |
| 1857 | */ |
| 1858 | char *f_name(struct file_struct *f, char *fbuf) |
| 1859 | { |
| 1860 | if (!f || !f->basename) |
| 1861 | return NULL; |
| 1862 | |
| 1863 | if (!fbuf) { |
| 1864 | static char names[5][MAXPATHLEN]; |
| 1865 | static unsigned int n; |
| 1866 | |
| 1867 | n = (n + 1) % (sizeof names / sizeof names[0]); |
| 1868 | |
| 1869 | fbuf = names[n]; |
| 1870 | } |
| 1871 | |
| 1872 | if (f->dirname) { |
| 1873 | int len = strlen(f->dirname); |
| 1874 | memcpy(fbuf, f->dirname, len); |
| 1875 | fbuf[len] = '/'; |
| 1876 | strlcpy(fbuf + len + 1, f->basename, MAXPATHLEN - (len + 1)); |
| 1877 | } else |
| 1878 | strlcpy(fbuf, f->basename, MAXPATHLEN); |
| 1879 | |
| 1880 | return fbuf; |
| 1881 | } |
| 1882 | |
| 1883 | /* Do a non-recursive scan of the named directory, possibly ignoring all |
| 1884 | * exclude rules except for the daemon's. If "dlen" is >=0, it is the length |
| 1885 | * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN |
| 1886 | * buffer (the functions we call will append names onto the end, but the old |
| 1887 | * dir value will be restored on exit). */ |
| 1888 | struct file_list *get_dirlist(char *dirname, int dlen, |
| 1889 | int ignore_filter_rules) |
| 1890 | { |
| 1891 | struct file_list *dirlist; |
| 1892 | char dirbuf[MAXPATHLEN]; |
| 1893 | int save_recurse = recurse; |
| 1894 | int save_xfer_dirs = xfer_dirs; |
| 1895 | |
| 1896 | if (dlen < 0) { |
| 1897 | dlen = strlcpy(dirbuf, dirname, MAXPATHLEN); |
| 1898 | if (dlen >= MAXPATHLEN) |
| 1899 | return NULL; |
| 1900 | dirname = dirbuf; |
| 1901 | } |
| 1902 | |
| 1903 | dirlist = flist_new(WITHOUT_HLINK, "get_dirlist"); |
| 1904 | |
| 1905 | recurse = 0; |
| 1906 | xfer_dirs = 1; |
| 1907 | send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen); |
| 1908 | xfer_dirs = save_xfer_dirs; |
| 1909 | recurse = save_recurse; |
| 1910 | if (do_progress) |
| 1911 | flist_count_offset += dirlist->count; |
| 1912 | |
| 1913 | clean_flist(dirlist, 0, 0); |
| 1914 | |
| 1915 | if (verbose > 3) |
| 1916 | output_flist(dirlist); |
| 1917 | |
| 1918 | return dirlist; |
| 1919 | } |