| 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 | |
| 41 | extern int cvs_exclude; |
| 42 | |
| 43 | extern int recurse; |
| 44 | |
| 45 | extern int one_file_system; |
| 46 | extern int make_backups; |
| 47 | extern int preserve_links; |
| 48 | extern int preserve_hard_links; |
| 49 | extern int preserve_perms; |
| 50 | extern int preserve_devices; |
| 51 | extern int preserve_uid; |
| 52 | extern int preserve_gid; |
| 53 | extern int preserve_times; |
| 54 | extern int relative_paths; |
| 55 | extern int copy_links; |
| 56 | extern int copy_unsafe_links; |
| 57 | extern int remote_version; |
| 58 | extern int io_error; |
| 59 | extern int sanitize_paths; |
| 60 | |
| 61 | extern int read_batch; |
| 62 | extern int write_batch; |
| 63 | |
| 64 | static char topsrcname[MAXPATHLEN]; |
| 65 | |
| 66 | static struct exclude_struct **local_exclude_list; |
| 67 | |
| 68 | static struct file_struct null_file; |
| 69 | |
| 70 | static void clean_flist(struct file_list *flist, int strip_root); |
| 71 | |
| 72 | |
| 73 | static int show_filelist_p(void) |
| 74 | { |
| 75 | return verbose && recurse && !am_server; |
| 76 | } |
| 77 | |
| 78 | static void start_filelist_progress(char *kind) |
| 79 | { |
| 80 | rprintf(FINFO, "%s ... ", kind); |
| 81 | if ((verbose > 1) || do_progress) |
| 82 | rprintf(FINFO, "\n"); |
| 83 | rflush(FINFO); |
| 84 | } |
| 85 | |
| 86 | |
| 87 | static void emit_filelist_progress(const struct file_list *flist) |
| 88 | { |
| 89 | rprintf(FINFO, " %d files...\r", flist->count); |
| 90 | } |
| 91 | |
| 92 | |
| 93 | static void maybe_emit_filelist_progress(const struct file_list *flist) |
| 94 | { |
| 95 | if (do_progress && show_filelist_p() && ((flist->count % 100) == 0)) |
| 96 | emit_filelist_progress(flist); |
| 97 | } |
| 98 | |
| 99 | |
| 100 | static void finish_filelist_progress(const struct file_list *flist) |
| 101 | { |
| 102 | if (do_progress) { |
| 103 | /* This overwrites the progress line */ |
| 104 | rprintf(FINFO, "%d file%sto consider\n", |
| 105 | flist->count, flist->count == 1 ? " " : "s "); |
| 106 | } else { |
| 107 | rprintf(FINFO, "done\n"); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | void show_flist_stats(void) |
| 112 | { |
| 113 | /* Nothing yet */ |
| 114 | } |
| 115 | |
| 116 | |
| 117 | static struct string_area *string_area_new(int size) |
| 118 | { |
| 119 | struct string_area *a; |
| 120 | |
| 121 | if (size <= 0) |
| 122 | size = ARENA_SIZE; |
| 123 | a = malloc(sizeof(*a)); |
| 124 | if (!a) |
| 125 | out_of_memory("string_area_new"); |
| 126 | a->current = a->base = malloc(size); |
| 127 | if (!a->current) |
| 128 | out_of_memory("string_area_new buffer"); |
| 129 | a->end = a->base + size; |
| 130 | a->next = NULL; |
| 131 | |
| 132 | return a; |
| 133 | } |
| 134 | |
| 135 | static void string_area_free(struct string_area *a) |
| 136 | { |
| 137 | struct string_area *next; |
| 138 | |
| 139 | for (; a; a = next) { |
| 140 | next = a->next; |
| 141 | free(a->base); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | static char *string_area_malloc(struct string_area **ap, int size) |
| 146 | { |
| 147 | char *p; |
| 148 | struct string_area *a; |
| 149 | |
| 150 | /* does the request fit into the current space? */ |
| 151 | a = *ap; |
| 152 | if (a->current + size >= a->end) { |
| 153 | /* no; get space, move new string_area to front of the list */ |
| 154 | a = string_area_new(size > ARENA_SIZE ? size : ARENA_SIZE); |
| 155 | a->next = *ap; |
| 156 | *ap = a; |
| 157 | } |
| 158 | |
| 159 | /* have space; do the "allocation." */ |
| 160 | p = a->current; |
| 161 | a->current += size; |
| 162 | return p; |
| 163 | } |
| 164 | |
| 165 | static char *string_area_strdup(struct string_area **ap, const char *src) |
| 166 | { |
| 167 | char *dest = string_area_malloc(ap, strlen(src) + 1); |
| 168 | return strcpy(dest, src); |
| 169 | } |
| 170 | |
| 171 | static void list_file_entry(struct file_struct *f) |
| 172 | { |
| 173 | char perms[11]; |
| 174 | |
| 175 | if (!f->basename) |
| 176 | /* this can happen if duplicate names were removed */ |
| 177 | return; |
| 178 | |
| 179 | permstring(perms, f->mode); |
| 180 | |
| 181 | if (preserve_links && S_ISLNK(f->mode)) { |
| 182 | rprintf(FINFO, "%s %11.0f %s %s -> %s\n", |
| 183 | perms, |
| 184 | (double) f->length, timestring(f->modtime), |
| 185 | f_name(f), f->link); |
| 186 | } else { |
| 187 | rprintf(FINFO, "%s %11.0f %s %s\n", |
| 188 | perms, |
| 189 | (double) f->length, timestring(f->modtime), |
| 190 | f_name(f)); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | |
| 195 | int readlink_stat(const char *Path, STRUCT_STAT * Buffer, char *Linkbuf) |
| 196 | { |
| 197 | #if SUPPORT_LINKS |
| 198 | if (copy_links) { |
| 199 | return do_stat(Path, Buffer); |
| 200 | } |
| 201 | if (do_lstat(Path, Buffer) == -1) { |
| 202 | return -1; |
| 203 | } |
| 204 | if (S_ISLNK(Buffer->st_mode)) { |
| 205 | int l; |
| 206 | if ((l = |
| 207 | readlink((char *) Path, Linkbuf, |
| 208 | MAXPATHLEN - 1)) == -1) { |
| 209 | return -1; |
| 210 | } |
| 211 | Linkbuf[l] = 0; |
| 212 | if (copy_unsafe_links && (topsrcname[0] != '\0') && |
| 213 | unsafe_symlink(Linkbuf, topsrcname)) { |
| 214 | return do_stat(Path, Buffer); |
| 215 | } |
| 216 | } |
| 217 | return 0; |
| 218 | #else |
| 219 | return do_stat(Path, Buffer); |
| 220 | #endif |
| 221 | } |
| 222 | |
| 223 | int link_stat(const char *Path, STRUCT_STAT * Buffer) |
| 224 | { |
| 225 | #if SUPPORT_LINKS |
| 226 | if (copy_links) { |
| 227 | return do_stat(Path, Buffer); |
| 228 | } else { |
| 229 | return do_lstat(Path, Buffer); |
| 230 | } |
| 231 | #else |
| 232 | return do_stat(Path, Buffer); |
| 233 | #endif |
| 234 | } |
| 235 | |
| 236 | /* |
| 237 | This function is used to check if a file should be included/excluded |
| 238 | from the list of files based on its name and type etc |
| 239 | */ |
| 240 | static int check_exclude_file(int f, char *fname, STRUCT_STAT * st) |
| 241 | { |
| 242 | extern int delete_excluded; |
| 243 | |
| 244 | /* f is set to -1 when calculating deletion file list */ |
| 245 | if ((f == -1) && delete_excluded) { |
| 246 | return 0; |
| 247 | } |
| 248 | if (check_exclude(fname, local_exclude_list, st)) { |
| 249 | return 1; |
| 250 | } |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | /* used by the one_file_system code */ |
| 255 | static dev_t filesystem_dev; |
| 256 | |
| 257 | static void set_filesystem(char *fname) |
| 258 | { |
| 259 | STRUCT_STAT st; |
| 260 | if (link_stat(fname, &st) != 0) |
| 261 | return; |
| 262 | filesystem_dev = st.st_dev; |
| 263 | } |
| 264 | |
| 265 | |
| 266 | static int to_wire_mode(mode_t mode) |
| 267 | { |
| 268 | if (S_ISLNK(mode) && (_S_IFLNK != 0120000)) { |
| 269 | return (mode & ~(_S_IFMT)) | 0120000; |
| 270 | } |
| 271 | return (int) mode; |
| 272 | } |
| 273 | |
| 274 | static mode_t from_wire_mode(int mode) |
| 275 | { |
| 276 | if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000)) { |
| 277 | return (mode & ~(_S_IFMT)) | _S_IFLNK; |
| 278 | } |
| 279 | return (mode_t) mode; |
| 280 | } |
| 281 | |
| 282 | |
| 283 | static void send_directory(int f, struct file_list *flist, char *dir); |
| 284 | |
| 285 | static char *flist_dir; |
| 286 | |
| 287 | |
| 288 | /** |
| 289 | * Make sure @p flist is big enough to hold at least @p flist->count |
| 290 | * entries. |
| 291 | **/ |
| 292 | static void flist_expand(struct file_list *flist) |
| 293 | { |
| 294 | if (flist->count >= flist->malloced) { |
| 295 | size_t new_bytes; |
| 296 | void *new_ptr; |
| 297 | |
| 298 | if (flist->malloced < 1000) |
| 299 | flist->malloced += 1000; |
| 300 | else |
| 301 | flist->malloced *= 2; |
| 302 | |
| 303 | new_bytes = sizeof(flist->files[0]) * flist->malloced; |
| 304 | |
| 305 | if (flist->files) |
| 306 | new_ptr = realloc(flist->files, new_bytes); |
| 307 | else |
| 308 | new_ptr = malloc(new_bytes); |
| 309 | |
| 310 | if (verbose >= 2) { |
| 311 | rprintf(FINFO, "expand file_list to %.0f bytes, did%s move\n", |
| 312 | (double) new_bytes, |
| 313 | (new_ptr == flist->files) ? " not" : ""); |
| 314 | } |
| 315 | |
| 316 | flist->files = (struct file_struct **) new_ptr; |
| 317 | |
| 318 | if (!flist->files) |
| 319 | out_of_memory("flist_expand"); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | |
| 324 | static void send_file_entry(struct file_struct *file, int f, |
| 325 | unsigned base_flags) |
| 326 | { |
| 327 | unsigned char flags; |
| 328 | static time_t last_time; |
| 329 | static mode_t last_mode; |
| 330 | static DEV64_T last_rdev; |
| 331 | static uid_t last_uid; |
| 332 | static gid_t last_gid; |
| 333 | static char lastname[MAXPATHLEN]; |
| 334 | char *fname; |
| 335 | int l1, l2; |
| 336 | |
| 337 | if (f == -1) |
| 338 | return; |
| 339 | |
| 340 | if (!file) { |
| 341 | write_byte(f, 0); |
| 342 | return; |
| 343 | } |
| 344 | |
| 345 | fname = f_name(file); |
| 346 | |
| 347 | flags = base_flags; |
| 348 | |
| 349 | if (file->mode == last_mode) |
| 350 | flags |= SAME_MODE; |
| 351 | if (file->rdev == last_rdev) |
| 352 | flags |= SAME_RDEV; |
| 353 | if (file->uid == last_uid) |
| 354 | flags |= SAME_UID; |
| 355 | if (file->gid == last_gid) |
| 356 | flags |= SAME_GID; |
| 357 | if (file->modtime == last_time) |
| 358 | flags |= SAME_TIME; |
| 359 | |
| 360 | for (l1 = 0; |
| 361 | lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255); |
| 362 | l1++); |
| 363 | l2 = strlen(fname) - l1; |
| 364 | |
| 365 | if (l1 > 0) |
| 366 | flags |= SAME_NAME; |
| 367 | if (l2 > 255) |
| 368 | flags |= LONG_NAME; |
| 369 | |
| 370 | /* we must make sure we don't send a zero flags byte or the other |
| 371 | end will terminate the flist transfer */ |
| 372 | if (flags == 0 && !S_ISDIR(file->mode)) |
| 373 | flags |= FLAG_DELETE; |
| 374 | if (flags == 0) |
| 375 | flags |= LONG_NAME; |
| 376 | |
| 377 | write_byte(f, flags); |
| 378 | if (flags & SAME_NAME) |
| 379 | write_byte(f, l1); |
| 380 | if (flags & LONG_NAME) |
| 381 | write_int(f, l2); |
| 382 | else |
| 383 | write_byte(f, l2); |
| 384 | write_buf(f, fname + l1, l2); |
| 385 | |
| 386 | write_longint(f, file->length); |
| 387 | if (!(flags & SAME_TIME)) |
| 388 | write_int(f, (int) file->modtime); |
| 389 | if (!(flags & SAME_MODE)) |
| 390 | write_int(f, to_wire_mode(file->mode)); |
| 391 | if (preserve_uid && !(flags & SAME_UID)) { |
| 392 | add_uid(file->uid); |
| 393 | write_int(f, (int) file->uid); |
| 394 | } |
| 395 | if (preserve_gid && !(flags & SAME_GID)) { |
| 396 | add_gid(file->gid); |
| 397 | write_int(f, (int) file->gid); |
| 398 | } |
| 399 | if (preserve_devices && IS_DEVICE(file->mode) |
| 400 | && !(flags & SAME_RDEV)) |
| 401 | write_int(f, (int) file->rdev); |
| 402 | |
| 403 | #if SUPPORT_LINKS |
| 404 | if (preserve_links && S_ISLNK(file->mode)) { |
| 405 | write_int(f, strlen(file->link)); |
| 406 | write_buf(f, file->link, strlen(file->link)); |
| 407 | } |
| 408 | #endif |
| 409 | |
| 410 | #if SUPPORT_HARD_LINKS |
| 411 | if (preserve_hard_links && S_ISREG(file->mode)) { |
| 412 | if (remote_version < 26) { |
| 413 | /* 32-bit dev_t and ino_t */ |
| 414 | write_int(f, (int) file->dev); |
| 415 | write_int(f, (int) file->inode); |
| 416 | } else { |
| 417 | /* 64-bit dev_t and ino_t */ |
| 418 | write_longint(f, file->dev); |
| 419 | write_longint(f, file->inode); |
| 420 | } |
| 421 | } |
| 422 | #endif |
| 423 | |
| 424 | if (always_checksum) { |
| 425 | if (remote_version < 21) { |
| 426 | write_buf(f, file->sum, 2); |
| 427 | } else { |
| 428 | write_buf(f, file->sum, MD4_SUM_LENGTH); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | last_mode = file->mode; |
| 433 | last_rdev = file->rdev; |
| 434 | last_uid = file->uid; |
| 435 | last_gid = file->gid; |
| 436 | last_time = file->modtime; |
| 437 | |
| 438 | strlcpy(lastname, fname, MAXPATHLEN); |
| 439 | lastname[MAXPATHLEN - 1] = 0; |
| 440 | } |
| 441 | |
| 442 | |
| 443 | |
| 444 | static void receive_file_entry(struct file_struct **fptr, |
| 445 | unsigned flags, int f) |
| 446 | { |
| 447 | static time_t last_time; |
| 448 | static mode_t last_mode; |
| 449 | static DEV64_T last_rdev; |
| 450 | static uid_t last_uid; |
| 451 | static gid_t last_gid; |
| 452 | static char lastname[MAXPATHLEN]; |
| 453 | char thisname[MAXPATHLEN]; |
| 454 | unsigned int l1 = 0, l2 = 0; |
| 455 | char *p; |
| 456 | struct file_struct *file; |
| 457 | |
| 458 | if (flags & SAME_NAME) |
| 459 | l1 = read_byte(f); |
| 460 | |
| 461 | if (flags & LONG_NAME) |
| 462 | l2 = read_int(f); |
| 463 | else |
| 464 | l2 = read_byte(f); |
| 465 | |
| 466 | file = (struct file_struct *) malloc(sizeof(*file)); |
| 467 | if (!file) |
| 468 | out_of_memory("receive_file_entry"); |
| 469 | memset((char *) file, 0, sizeof(*file)); |
| 470 | (*fptr) = file; |
| 471 | |
| 472 | if (l2 >= MAXPATHLEN - l1) { |
| 473 | rprintf(FERROR, |
| 474 | "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n", |
| 475 | flags, l1, l2, lastname); |
| 476 | overflow("receive_file_entry"); |
| 477 | } |
| 478 | |
| 479 | strlcpy(thisname, lastname, l1 + 1); |
| 480 | read_sbuf(f, &thisname[l1], l2); |
| 481 | thisname[l1 + l2] = 0; |
| 482 | |
| 483 | strlcpy(lastname, thisname, MAXPATHLEN); |
| 484 | lastname[MAXPATHLEN - 1] = 0; |
| 485 | |
| 486 | clean_fname(thisname); |
| 487 | |
| 488 | if (sanitize_paths) { |
| 489 | sanitize_path(thisname, NULL); |
| 490 | } |
| 491 | |
| 492 | if ((p = strrchr(thisname, '/'))) { |
| 493 | static char *lastdir; |
| 494 | *p = 0; |
| 495 | if (lastdir && strcmp(thisname, lastdir) == 0) { |
| 496 | file->dirname = lastdir; |
| 497 | } else { |
| 498 | file->dirname = strdup(thisname); |
| 499 | lastdir = file->dirname; |
| 500 | } |
| 501 | file->basename = strdup(p + 1); |
| 502 | } else { |
| 503 | file->dirname = NULL; |
| 504 | file->basename = strdup(thisname); |
| 505 | } |
| 506 | |
| 507 | if (!file->basename) |
| 508 | out_of_memory("receive_file_entry 1"); |
| 509 | |
| 510 | |
| 511 | file->flags = flags; |
| 512 | file->length = read_longint(f); |
| 513 | file->modtime = |
| 514 | (flags & SAME_TIME) ? last_time : (time_t) read_int(f); |
| 515 | file->mode = |
| 516 | (flags & SAME_MODE) ? last_mode : from_wire_mode(read_int(f)); |
| 517 | if (preserve_uid) |
| 518 | file->uid = |
| 519 | (flags & SAME_UID) ? last_uid : (uid_t) read_int(f); |
| 520 | if (preserve_gid) |
| 521 | file->gid = |
| 522 | (flags & SAME_GID) ? last_gid : (gid_t) read_int(f); |
| 523 | if (preserve_devices && IS_DEVICE(file->mode)) |
| 524 | file->rdev = |
| 525 | (flags & SAME_RDEV) ? last_rdev : (dev_t) read_int(f); |
| 526 | |
| 527 | if (preserve_links && S_ISLNK(file->mode)) { |
| 528 | int l = read_int(f); |
| 529 | if (l < 0) { |
| 530 | rprintf(FERROR, "overflow: l=%d\n", l); |
| 531 | overflow("receive_file_entry"); |
| 532 | } |
| 533 | file->link = (char *) malloc(l + 1); |
| 534 | if (!file->link) |
| 535 | out_of_memory("receive_file_entry 2"); |
| 536 | read_sbuf(f, file->link, l); |
| 537 | if (sanitize_paths) { |
| 538 | sanitize_path(file->link, file->dirname); |
| 539 | } |
| 540 | } |
| 541 | #if SUPPORT_HARD_LINKS |
| 542 | if (preserve_hard_links && S_ISREG(file->mode)) { |
| 543 | if (remote_version < 26) { |
| 544 | file->dev = read_int(f); |
| 545 | file->inode = read_int(f); |
| 546 | } else { |
| 547 | file->dev = read_longint(f); |
| 548 | file->inode = read_longint(f); |
| 549 | } |
| 550 | } |
| 551 | #endif |
| 552 | |
| 553 | if (always_checksum) { |
| 554 | file->sum = (char *) malloc(MD4_SUM_LENGTH); |
| 555 | if (!file->sum) |
| 556 | out_of_memory("md4 sum"); |
| 557 | if (remote_version < 21) { |
| 558 | read_buf(f, file->sum, 2); |
| 559 | } else { |
| 560 | read_buf(f, file->sum, MD4_SUM_LENGTH); |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | last_mode = file->mode; |
| 565 | last_rdev = file->rdev; |
| 566 | last_uid = file->uid; |
| 567 | last_gid = file->gid; |
| 568 | last_time = file->modtime; |
| 569 | |
| 570 | if (!preserve_perms) { |
| 571 | extern int orig_umask; |
| 572 | /* set an appropriate set of permissions based on original |
| 573 | permissions and umask. This emulates what GNU cp does */ |
| 574 | file->mode &= ~orig_umask; |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | |
| 579 | /* determine if a file in a different filesstem should be skipped |
| 580 | when one_file_system is set. We bascally only want to include |
| 581 | the mount points - but they can be hard to find! */ |
| 582 | static int skip_filesystem(char *fname, STRUCT_STAT * st) |
| 583 | { |
| 584 | STRUCT_STAT st2; |
| 585 | char *p = strrchr(fname, '/'); |
| 586 | |
| 587 | /* skip all but directories */ |
| 588 | if (!S_ISDIR(st->st_mode)) |
| 589 | return 1; |
| 590 | |
| 591 | /* if its not a subdirectory then allow */ |
| 592 | if (!p) |
| 593 | return 0; |
| 594 | |
| 595 | *p = 0; |
| 596 | if (link_stat(fname, &st2)) { |
| 597 | *p = '/'; |
| 598 | return 0; |
| 599 | } |
| 600 | *p = '/'; |
| 601 | |
| 602 | return (st2.st_dev != filesystem_dev); |
| 603 | } |
| 604 | |
| 605 | #define STRDUP(ap, p) (ap ? string_area_strdup(ap, p) : strdup(p)) |
| 606 | /* IRIX cc cares that the operands to the ternary have the same type. */ |
| 607 | #define MALLOC(ap, i) (ap ? (void*) string_area_malloc(ap, i) : malloc(i)) |
| 608 | |
| 609 | /** |
| 610 | * Create a file_struct for a named file by reading its stat() |
| 611 | * information and performing extensive checks against global |
| 612 | * options. |
| 613 | * |
| 614 | * @return the new file, or NULL if there was an error or this file |
| 615 | * should be excluded. |
| 616 | * |
| 617 | * @todo There is a small optimization opportunity here to avoid |
| 618 | * stat()ing the file in some circumstances, which has a certain cost. |
| 619 | * We are called immediately after doing readdir(), and so we may |
| 620 | * already know the d_type of the file. We could for example avoid |
| 621 | * statting directories if we're not recursing, but this is not a very |
| 622 | * important case. Some systems may not have d_type. |
| 623 | **/ |
| 624 | struct file_struct *make_file(int f, char *fname, struct string_area **ap, |
| 625 | int noexcludes) |
| 626 | { |
| 627 | struct file_struct *file; |
| 628 | STRUCT_STAT st; |
| 629 | char sum[SUM_LENGTH]; |
| 630 | char *p; |
| 631 | char cleaned_name[MAXPATHLEN]; |
| 632 | char linkbuf[MAXPATHLEN]; |
| 633 | extern int module_id; |
| 634 | |
| 635 | strlcpy(cleaned_name, fname, MAXPATHLEN); |
| 636 | cleaned_name[MAXPATHLEN - 1] = 0; |
| 637 | clean_fname(cleaned_name); |
| 638 | if (sanitize_paths) { |
| 639 | sanitize_path(cleaned_name, NULL); |
| 640 | } |
| 641 | fname = cleaned_name; |
| 642 | |
| 643 | memset(sum, 0, SUM_LENGTH); |
| 644 | |
| 645 | if (readlink_stat(fname, &st, linkbuf) != 0) { |
| 646 | int save_errno = errno; |
| 647 | if ((errno == ENOENT) && copy_links && !noexcludes) { |
| 648 | /* symlink pointing nowhere, see if excluded */ |
| 649 | memset((char *) &st, 0, sizeof(st)); |
| 650 | if (check_exclude_file(f, fname, &st)) { |
| 651 | /* file is excluded anyway, ignore silently */ |
| 652 | return NULL; |
| 653 | } |
| 654 | } |
| 655 | io_error = 1; |
| 656 | rprintf(FERROR, "readlink %s: %s\n", |
| 657 | fname, strerror(save_errno)); |
| 658 | return NULL; |
| 659 | } |
| 660 | |
| 661 | /* we use noexcludes from backup.c */ |
| 662 | if (noexcludes) |
| 663 | goto skip_excludes; |
| 664 | |
| 665 | if (S_ISDIR(st.st_mode) && !recurse) { |
| 666 | rprintf(FINFO, "skipping directory %s\n", fname); |
| 667 | return NULL; |
| 668 | } |
| 669 | |
| 670 | if (one_file_system && st.st_dev != filesystem_dev) { |
| 671 | if (skip_filesystem(fname, &st)) |
| 672 | return NULL; |
| 673 | } |
| 674 | |
| 675 | if (check_exclude_file(f, fname, &st)) |
| 676 | return NULL; |
| 677 | |
| 678 | |
| 679 | if (lp_ignore_nonreadable(module_id) && access(fname, R_OK) != 0) |
| 680 | return NULL; |
| 681 | |
| 682 | skip_excludes: |
| 683 | |
| 684 | if (verbose > 2) |
| 685 | rprintf(FINFO, "make_file(%d,%s)\n", f, fname); |
| 686 | |
| 687 | file = (struct file_struct *) malloc(sizeof(*file)); |
| 688 | if (!file) |
| 689 | out_of_memory("make_file"); |
| 690 | memset((char *) file, 0, sizeof(*file)); |
| 691 | |
| 692 | if ((p = strrchr(fname, '/'))) { |
| 693 | static char *lastdir; |
| 694 | *p = 0; |
| 695 | if (lastdir && strcmp(fname, lastdir) == 0) { |
| 696 | file->dirname = lastdir; |
| 697 | } else { |
| 698 | file->dirname = strdup(fname); |
| 699 | lastdir = file->dirname; |
| 700 | } |
| 701 | file->basename = STRDUP(ap, p + 1); |
| 702 | *p = '/'; |
| 703 | } else { |
| 704 | file->dirname = NULL; |
| 705 | file->basename = STRDUP(ap, fname); |
| 706 | } |
| 707 | |
| 708 | file->modtime = st.st_mtime; |
| 709 | file->length = st.st_size; |
| 710 | file->mode = st.st_mode; |
| 711 | file->uid = st.st_uid; |
| 712 | file->gid = st.st_gid; |
| 713 | file->dev = st.st_dev; |
| 714 | file->inode = st.st_ino; |
| 715 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
| 716 | file->rdev = st.st_rdev; |
| 717 | #endif |
| 718 | |
| 719 | #if SUPPORT_LINKS |
| 720 | if (S_ISLNK(st.st_mode)) { |
| 721 | file->link = STRDUP(ap, linkbuf); |
| 722 | } |
| 723 | #endif |
| 724 | |
| 725 | if (always_checksum) { |
| 726 | file->sum = (char *) MALLOC(ap, MD4_SUM_LENGTH); |
| 727 | if (!file->sum) |
| 728 | out_of_memory("md4 sum"); |
| 729 | /* drat. we have to provide a null checksum for non-regular |
| 730 | files in order to be compatible with earlier versions |
| 731 | of rsync */ |
| 732 | if (S_ISREG(st.st_mode)) { |
| 733 | file_checksum(fname, file->sum, st.st_size); |
| 734 | } else { |
| 735 | memset(file->sum, 0, MD4_SUM_LENGTH); |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | if (flist_dir) { |
| 740 | static char *lastdir; |
| 741 | if (lastdir && strcmp(lastdir, flist_dir) == 0) { |
| 742 | file->basedir = lastdir; |
| 743 | } else { |
| 744 | file->basedir = strdup(flist_dir); |
| 745 | lastdir = file->basedir; |
| 746 | } |
| 747 | } else { |
| 748 | file->basedir = NULL; |
| 749 | } |
| 750 | |
| 751 | if (!S_ISDIR(st.st_mode)) |
| 752 | stats.total_size += st.st_size; |
| 753 | |
| 754 | return file; |
| 755 | } |
| 756 | |
| 757 | |
| 758 | |
| 759 | void send_file_name(int f, struct file_list *flist, char *fname, |
| 760 | int recursive, unsigned base_flags) |
| 761 | { |
| 762 | struct file_struct *file; |
| 763 | |
| 764 | file = make_file(f, fname, &flist->string_area, 0); |
| 765 | |
| 766 | if (!file) |
| 767 | return; |
| 768 | |
| 769 | maybe_emit_filelist_progress(flist); |
| 770 | |
| 771 | flist_expand(flist); |
| 772 | |
| 773 | if (write_batch) /* dw */ |
| 774 | file->flags = FLAG_DELETE; |
| 775 | |
| 776 | if (strcmp(file->basename, "")) { |
| 777 | flist->files[flist->count++] = file; |
| 778 | send_file_entry(file, f, base_flags); |
| 779 | } |
| 780 | |
| 781 | if (S_ISDIR(file->mode) && recursive) { |
| 782 | struct exclude_struct **last_exclude_list = |
| 783 | local_exclude_list; |
| 784 | send_directory(f, flist, f_name(file)); |
| 785 | local_exclude_list = last_exclude_list; |
| 786 | return; |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | |
| 791 | |
| 792 | static void send_directory(int f, struct file_list *flist, char *dir) |
| 793 | { |
| 794 | DIR *d; |
| 795 | struct dirent *di; |
| 796 | char fname[MAXPATHLEN]; |
| 797 | int l; |
| 798 | char *p; |
| 799 | |
| 800 | d = opendir(dir); |
| 801 | if (!d) { |
| 802 | io_error = 1; |
| 803 | rprintf(FERROR, "opendir(%s): %s\n", dir, strerror(errno)); |
| 804 | return; |
| 805 | } |
| 806 | |
| 807 | strlcpy(fname, dir, MAXPATHLEN); |
| 808 | l = strlen(fname); |
| 809 | if (fname[l - 1] != '/') { |
| 810 | if (l == MAXPATHLEN - 1) { |
| 811 | io_error = 1; |
| 812 | rprintf(FERROR, |
| 813 | "skipping long-named directory %s\n", |
| 814 | fname); |
| 815 | closedir(d); |
| 816 | return; |
| 817 | } |
| 818 | strlcat(fname, "/", MAXPATHLEN); |
| 819 | l++; |
| 820 | } |
| 821 | p = fname + strlen(fname); |
| 822 | |
| 823 | local_exclude_list = NULL; |
| 824 | |
| 825 | if (cvs_exclude) { |
| 826 | if (strlen(fname) + strlen(".cvsignore") <= MAXPATHLEN - 1) { |
| 827 | strcpy(p, ".cvsignore"); |
| 828 | local_exclude_list = |
| 829 | make_exclude_list(fname, NULL, 0, 0); |
| 830 | } else { |
| 831 | io_error = 1; |
| 832 | rprintf(FINFO, |
| 833 | "cannot cvs-exclude in long-named directory %s\n", |
| 834 | fname); |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | for (di = readdir(d); di; di = readdir(d)) { |
| 839 | char *dname = d_name(di); |
| 840 | if (strcmp(dname, ".") == 0 || strcmp(dname, "..") == 0) |
| 841 | continue; |
| 842 | strlcpy(p, dname, MAXPATHLEN - l); |
| 843 | send_file_name(f, flist, fname, recurse, 0); |
| 844 | } |
| 845 | |
| 846 | if (local_exclude_list) { |
| 847 | add_exclude_list("!", &local_exclude_list, 0); |
| 848 | } |
| 849 | |
| 850 | closedir(d); |
| 851 | } |
| 852 | |
| 853 | |
| 854 | /* |
| 855 | * |
| 856 | * I *think* f==-1 means that the list should just be built in memory |
| 857 | * and not transmitted. But who can tell? -- mbp |
| 858 | */ |
| 859 | struct file_list *send_file_list(int f, int argc, char *argv[]) |
| 860 | { |
| 861 | int i, l; |
| 862 | STRUCT_STAT st; |
| 863 | char *p, *dir, *olddir; |
| 864 | char lastpath[MAXPATHLEN] = ""; |
| 865 | struct file_list *flist; |
| 866 | int64 start_write; |
| 867 | |
| 868 | if (show_filelist_p() && f != -1) |
| 869 | start_filelist_progress("building file list"); |
| 870 | |
| 871 | start_write = stats.total_written; |
| 872 | |
| 873 | flist = flist_new(); |
| 874 | |
| 875 | if (f != -1) { |
| 876 | io_start_buffering(f); |
| 877 | } |
| 878 | |
| 879 | for (i = 0; i < argc; i++) { |
| 880 | char *fname = topsrcname; |
| 881 | |
| 882 | strlcpy(fname, argv[i], MAXPATHLEN); |
| 883 | |
| 884 | l = strlen(fname); |
| 885 | if (l != 1 && fname[l - 1] == '/') { |
| 886 | if ((l == 2) && (fname[0] == '.')) { |
| 887 | /* Turn ./ into just . rather than ./. |
| 888 | This was put in to avoid a problem with |
| 889 | rsync -aR --delete from ./ |
| 890 | The send_file_name() below of ./ was |
| 891 | mysteriously preventing deletes */ |
| 892 | fname[1] = 0; |
| 893 | } else { |
| 894 | strlcat(fname, ".", MAXPATHLEN); |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | if (link_stat(fname, &st) != 0) { |
| 899 | if (f != -1) { |
| 900 | io_error = 1; |
| 901 | rprintf(FERROR, "link_stat %s : %s\n", |
| 902 | fname, strerror(errno)); |
| 903 | } |
| 904 | continue; |
| 905 | } |
| 906 | |
| 907 | if (S_ISDIR(st.st_mode) && !recurse) { |
| 908 | rprintf(FINFO, "skipping directory %s\n", fname); |
| 909 | continue; |
| 910 | } |
| 911 | |
| 912 | dir = NULL; |
| 913 | olddir = NULL; |
| 914 | |
| 915 | if (!relative_paths) { |
| 916 | p = strrchr(fname, '/'); |
| 917 | if (p) { |
| 918 | *p = 0; |
| 919 | if (p == fname) |
| 920 | dir = "/"; |
| 921 | else |
| 922 | dir = fname; |
| 923 | fname = p + 1; |
| 924 | } |
| 925 | } else if (f != -1 && (p = strrchr(fname, '/'))) { |
| 926 | /* this ensures we send the intermediate directories, |
| 927 | thus getting their permissions right */ |
| 928 | *p = 0; |
| 929 | if (strcmp(lastpath, fname)) { |
| 930 | strlcpy(lastpath, fname, sizeof(lastpath)); |
| 931 | *p = '/'; |
| 932 | for (p = fname + 1; (p = strchr(p, '/')); |
| 933 | p++) { |
| 934 | int copy_links_saved = copy_links; |
| 935 | int recurse_saved = recurse; |
| 936 | *p = 0; |
| 937 | copy_links = copy_unsafe_links; |
| 938 | /* set recurse to 1 to prevent make_file |
| 939 | from ignoring directory, but still |
| 940 | turn off the recursive parameter to |
| 941 | send_file_name */ |
| 942 | recurse = 1; |
| 943 | send_file_name(f, flist, fname, 0, |
| 944 | 0); |
| 945 | copy_links = copy_links_saved; |
| 946 | recurse = recurse_saved; |
| 947 | *p = '/'; |
| 948 | } |
| 949 | } else { |
| 950 | *p = '/'; |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | if (!*fname) |
| 955 | fname = "."; |
| 956 | |
| 957 | if (dir && *dir) { |
| 958 | olddir = push_dir(dir, 1); |
| 959 | |
| 960 | if (!olddir) { |
| 961 | io_error = 1; |
| 962 | rprintf(FERROR, "push_dir %s : %s\n", |
| 963 | dir, strerror(errno)); |
| 964 | continue; |
| 965 | } |
| 966 | |
| 967 | flist_dir = dir; |
| 968 | } |
| 969 | |
| 970 | if (one_file_system) |
| 971 | set_filesystem(fname); |
| 972 | |
| 973 | send_file_name(f, flist, fname, recurse, FLAG_DELETE); |
| 974 | |
| 975 | if (olddir != NULL) { |
| 976 | flist_dir = NULL; |
| 977 | if (pop_dir(olddir) != 0) { |
| 978 | rprintf(FERROR, "pop_dir %s : %s\n", |
| 979 | dir, strerror(errno)); |
| 980 | exit_cleanup(RERR_FILESELECT); |
| 981 | } |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | topsrcname[0] = '\0'; |
| 986 | |
| 987 | if (f != -1) { |
| 988 | send_file_entry(NULL, f, 0); |
| 989 | } |
| 990 | |
| 991 | if (show_filelist_p() && f != -1) { |
| 992 | finish_filelist_progress(flist); |
| 993 | } |
| 994 | |
| 995 | clean_flist(flist, 0); |
| 996 | |
| 997 | /* now send the uid/gid list. This was introduced in protocol |
| 998 | version 15 */ |
| 999 | if (f != -1 && remote_version >= 15) { |
| 1000 | send_uid_list(f); |
| 1001 | } |
| 1002 | |
| 1003 | /* if protocol version is >= 17 then send the io_error flag */ |
| 1004 | if (f != -1 && remote_version >= 17) { |
| 1005 | extern int module_id; |
| 1006 | write_int(f, lp_ignore_errors(module_id) ? 0 : io_error); |
| 1007 | } |
| 1008 | |
| 1009 | if (f != -1) { |
| 1010 | io_end_buffering(); |
| 1011 | stats.flist_size = stats.total_written - start_write; |
| 1012 | stats.num_files = flist->count; |
| 1013 | if (write_batch) /* dw */ |
| 1014 | write_batch_flist_info(flist->count, flist->files); |
| 1015 | } |
| 1016 | |
| 1017 | if (verbose > 2) |
| 1018 | rprintf(FINFO, "send_file_list done\n"); |
| 1019 | |
| 1020 | return flist; |
| 1021 | } |
| 1022 | |
| 1023 | |
| 1024 | struct file_list *recv_file_list(int f) |
| 1025 | { |
| 1026 | struct file_list *flist; |
| 1027 | unsigned char flags; |
| 1028 | int64 start_read; |
| 1029 | extern int list_only; |
| 1030 | |
| 1031 | if (show_filelist_p()) |
| 1032 | start_filelist_progress("receiving file list"); |
| 1033 | |
| 1034 | start_read = stats.total_read; |
| 1035 | |
| 1036 | flist = (struct file_list *) malloc(sizeof(flist[0])); |
| 1037 | if (!flist) |
| 1038 | goto oom; |
| 1039 | |
| 1040 | flist->count = 0; |
| 1041 | flist->malloced = 1000; |
| 1042 | flist->files = |
| 1043 | (struct file_struct **) malloc(sizeof(flist->files[0]) * |
| 1044 | flist->malloced); |
| 1045 | if (!flist->files) |
| 1046 | goto oom; |
| 1047 | |
| 1048 | |
| 1049 | for (flags = read_byte(f); flags; flags = read_byte(f)) { |
| 1050 | int i = flist->count; |
| 1051 | |
| 1052 | flist_expand(flist); |
| 1053 | |
| 1054 | receive_file_entry(&flist->files[i], flags, f); |
| 1055 | |
| 1056 | if (S_ISREG(flist->files[i]->mode)) |
| 1057 | stats.total_size += flist->files[i]->length; |
| 1058 | |
| 1059 | flist->count++; |
| 1060 | |
| 1061 | maybe_emit_filelist_progress(flist); |
| 1062 | |
| 1063 | if (verbose > 2) |
| 1064 | rprintf(FINFO, "recv_file_name(%s)\n", |
| 1065 | f_name(flist->files[i])); |
| 1066 | } |
| 1067 | |
| 1068 | |
| 1069 | if (verbose > 2) |
| 1070 | rprintf(FINFO, "received %d names\n", flist->count); |
| 1071 | |
| 1072 | clean_flist(flist, relative_paths); |
| 1073 | |
| 1074 | if (show_filelist_p()) { |
| 1075 | finish_filelist_progress(flist); |
| 1076 | } |
| 1077 | |
| 1078 | /* now recv the uid/gid list. This was introduced in protocol version 15 */ |
| 1079 | if (f != -1 && remote_version >= 15) { |
| 1080 | recv_uid_list(f, flist); |
| 1081 | } |
| 1082 | |
| 1083 | /* if protocol version is >= 17 then recv the io_error flag */ |
| 1084 | if (f != -1 && remote_version >= 17 && !read_batch) { /* dw-added readbatch */ |
| 1085 | extern int module_id; |
| 1086 | extern int ignore_errors; |
| 1087 | if (lp_ignore_errors(module_id) || ignore_errors) { |
| 1088 | read_int(f); |
| 1089 | } else { |
| 1090 | io_error |= read_int(f); |
| 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | if (list_only) { |
| 1095 | int i; |
| 1096 | for (i = 0; i < flist->count; i++) { |
| 1097 | list_file_entry(flist->files[i]); |
| 1098 | } |
| 1099 | } |
| 1100 | |
| 1101 | |
| 1102 | if (verbose > 2) |
| 1103 | rprintf(FINFO, "recv_file_list done\n"); |
| 1104 | |
| 1105 | stats.flist_size = stats.total_read - start_read; |
| 1106 | stats.num_files = flist->count; |
| 1107 | |
| 1108 | return flist; |
| 1109 | |
| 1110 | oom: |
| 1111 | out_of_memory("recv_file_list"); |
| 1112 | return NULL; /* not reached */ |
| 1113 | } |
| 1114 | |
| 1115 | |
| 1116 | /* |
| 1117 | * XXX: This is currently the hottest function while building the file |
| 1118 | * list, because building f_name()s every time is expensive. |
| 1119 | **/ |
| 1120 | int file_compare(struct file_struct **f1, struct file_struct **f2) |
| 1121 | { |
| 1122 | if (!(*f1)->basename && !(*f2)->basename) |
| 1123 | return 0; |
| 1124 | if (!(*f1)->basename) |
| 1125 | return -1; |
| 1126 | if (!(*f2)->basename) |
| 1127 | return 1; |
| 1128 | if ((*f1)->dirname == (*f2)->dirname) |
| 1129 | return u_strcmp((*f1)->basename, (*f2)->basename); |
| 1130 | return u_strcmp(f_name(*f1), f_name(*f2)); |
| 1131 | } |
| 1132 | |
| 1133 | |
| 1134 | int flist_find(struct file_list *flist, struct file_struct *f) |
| 1135 | { |
| 1136 | int low = 0, high = flist->count - 1; |
| 1137 | |
| 1138 | if (flist->count <= 0) |
| 1139 | return -1; |
| 1140 | |
| 1141 | while (low != high) { |
| 1142 | int mid = (low + high) / 2; |
| 1143 | int ret = |
| 1144 | file_compare(&flist->files[flist_up(flist, mid)], &f); |
| 1145 | if (ret == 0) |
| 1146 | return flist_up(flist, mid); |
| 1147 | if (ret > 0) { |
| 1148 | high = mid; |
| 1149 | } else { |
| 1150 | low = mid + 1; |
| 1151 | } |
| 1152 | } |
| 1153 | |
| 1154 | if (file_compare(&flist->files[flist_up(flist, low)], &f) == 0) |
| 1155 | return flist_up(flist, low); |
| 1156 | return -1; |
| 1157 | } |
| 1158 | |
| 1159 | |
| 1160 | /* |
| 1161 | * free up one file |
| 1162 | */ |
| 1163 | void free_file(struct file_struct *file) |
| 1164 | { |
| 1165 | if (!file) |
| 1166 | return; |
| 1167 | if (file->basename) |
| 1168 | free(file->basename); |
| 1169 | if (file->link) |
| 1170 | free(file->link); |
| 1171 | if (file->sum) |
| 1172 | free(file->sum); |
| 1173 | *file = null_file; |
| 1174 | } |
| 1175 | |
| 1176 | |
| 1177 | /* |
| 1178 | * allocate a new file list |
| 1179 | */ |
| 1180 | struct file_list *flist_new() |
| 1181 | { |
| 1182 | struct file_list *flist; |
| 1183 | |
| 1184 | flist = (struct file_list *) malloc(sizeof(flist[0])); |
| 1185 | if (!flist) |
| 1186 | out_of_memory("send_file_list"); |
| 1187 | |
| 1188 | flist->count = 0; |
| 1189 | flist->malloced = 0; |
| 1190 | flist->files = NULL; |
| 1191 | |
| 1192 | #if ARENA_SIZE > 0 |
| 1193 | flist->string_area = string_area_new(0); |
| 1194 | #else |
| 1195 | flist->string_area = NULL; |
| 1196 | #endif |
| 1197 | return flist; |
| 1198 | } |
| 1199 | |
| 1200 | /* |
| 1201 | * free up all elements in a flist |
| 1202 | */ |
| 1203 | void flist_free(struct file_list *flist) |
| 1204 | { |
| 1205 | int i; |
| 1206 | for (i = 1; i < flist->count; i++) { |
| 1207 | if (!flist->string_area) |
| 1208 | free_file(flist->files[i]); |
| 1209 | free(flist->files[i]); |
| 1210 | } |
| 1211 | /* FIXME: I don't think we generally need to blank the flist |
| 1212 | * since it's about to be freed. This will just cause more |
| 1213 | * memory traffic. If you want a freed-memory debugger, you |
| 1214 | * know where to get it. */ |
| 1215 | memset((char *) flist->files, 0, |
| 1216 | sizeof(flist->files[0]) * flist->count); |
| 1217 | free(flist->files); |
| 1218 | if (flist->string_area) |
| 1219 | string_area_free(flist->string_area); |
| 1220 | memset((char *) flist, 0, sizeof(*flist)); |
| 1221 | free(flist); |
| 1222 | } |
| 1223 | |
| 1224 | |
| 1225 | /* |
| 1226 | * This routine ensures we don't have any duplicate names in our file list. |
| 1227 | * duplicate names can cause corruption because of the pipelining |
| 1228 | */ |
| 1229 | static void clean_flist(struct file_list *flist, int strip_root) |
| 1230 | { |
| 1231 | int i; |
| 1232 | |
| 1233 | if (!flist || flist->count == 0) |
| 1234 | return; |
| 1235 | |
| 1236 | qsort(flist->files, flist->count, |
| 1237 | sizeof(flist->files[0]), (int (*)()) file_compare); |
| 1238 | |
| 1239 | for (i = 1; i < flist->count; i++) { |
| 1240 | if (flist->files[i]->basename && |
| 1241 | flist->files[i - 1]->basename && |
| 1242 | strcmp(f_name(flist->files[i]), |
| 1243 | f_name(flist->files[i - 1])) == 0) { |
| 1244 | if (verbose > 1 && !am_server) |
| 1245 | rprintf(FINFO, |
| 1246 | "removing duplicate name %s from file list %d\n", |
| 1247 | f_name(flist->files[i - 1]), |
| 1248 | i - 1); |
| 1249 | /* it's not great that the flist knows the semantics of the |
| 1250 | * file memory usage, but i'd rather not add a flag byte |
| 1251 | * to that struct. XXX can i use a bit in the flags field? */ |
| 1252 | if (flist->string_area) |
| 1253 | flist->files[i][0] = null_file; |
| 1254 | else |
| 1255 | free_file(flist->files[i]); |
| 1256 | } |
| 1257 | } |
| 1258 | |
| 1259 | /* FIXME: There is a bug here when filenames are repeated more |
| 1260 | * than once, because we don't handle freed files when doing |
| 1261 | * the comparison. */ |
| 1262 | |
| 1263 | if (strip_root) { |
| 1264 | /* we need to strip off the root directory in the case |
| 1265 | of relative paths, but this must be done _after_ |
| 1266 | the sorting phase */ |
| 1267 | for (i = 0; i < flist->count; i++) { |
| 1268 | if (flist->files[i]->dirname && |
| 1269 | flist->files[i]->dirname[0] == '/') { |
| 1270 | memmove(&flist->files[i]->dirname[0], |
| 1271 | &flist->files[i]->dirname[1], |
| 1272 | strlen(flist->files[i]->dirname)); |
| 1273 | } |
| 1274 | |
| 1275 | if (flist->files[i]->dirname && |
| 1276 | !flist->files[i]->dirname[0]) { |
| 1277 | flist->files[i]->dirname = NULL; |
| 1278 | } |
| 1279 | } |
| 1280 | } |
| 1281 | |
| 1282 | |
| 1283 | if (verbose <= 3) |
| 1284 | return; |
| 1285 | |
| 1286 | for (i = 0; i < flist->count; i++) { |
| 1287 | rprintf(FINFO, "[%d] i=%d %s %s mode=0%o len=%.0f\n", |
| 1288 | (int) getpid(), i, |
| 1289 | NS(flist->files[i]->dirname), |
| 1290 | NS(flist->files[i]->basename), |
| 1291 | (int) flist->files[i]->mode, |
| 1292 | (double) flist->files[i]->length); |
| 1293 | } |
| 1294 | } |
| 1295 | |
| 1296 | |
| 1297 | /* |
| 1298 | * return the full filename of a flist entry |
| 1299 | * |
| 1300 | * This function is too expensive at the moment, because it copies |
| 1301 | * strings when often we only want to compare them. In any case, |
| 1302 | * using strlcat is silly because it will walk the string repeatedly. |
| 1303 | */ |
| 1304 | char *f_name(struct file_struct *f) |
| 1305 | { |
| 1306 | static char names[10][MAXPATHLEN]; |
| 1307 | static int n; |
| 1308 | char *p = names[n]; |
| 1309 | |
| 1310 | if (!f || !f->basename) |
| 1311 | return NULL; |
| 1312 | |
| 1313 | n = (n + 1) % 10; |
| 1314 | |
| 1315 | if (f->dirname) { |
| 1316 | int off; |
| 1317 | |
| 1318 | off = strlcpy(p, f->dirname, MAXPATHLEN); |
| 1319 | off += strlcpy(p + off, "/", MAXPATHLEN - off); |
| 1320 | off += strlcpy(p + off, f->basename, MAXPATHLEN - off); |
| 1321 | } else { |
| 1322 | strlcpy(p, f->basename, MAXPATHLEN); |
| 1323 | } |
| 1324 | |
| 1325 | return p; |
| 1326 | } |