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