Hash :
71ec7eac
Author :
Date :
2020-01-28T14:52:27
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
/*
* Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/queue.h>
#include <sys/tree.h>
#include <sys/stat.h>
#include <errno.h>
#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sha1.h>
#include <endian.h>
#include <limits.h>
#include <unistd.h>
#include <uuid.h>
#include "got_error.h"
#include "got_object.h"
#include "got_path.h"
#include "got_lib_fileindex.h"
#include "got_lib_worktree.h"
/* got_fileindex_entry flags */
#define GOT_FILEIDX_F_PATH_LEN 0x00000fff
#define GOT_FILEIDX_F_STAGE 0x0000f000
#define GOT_FILEIDX_F_STAGE_SHIFT 12
#define GOT_FILEIDX_F_NOT_FLUSHED 0x00010000
#define GOT_FILEIDX_F_NO_BLOB 0x00020000
#define GOT_FILEIDX_F_NO_COMMIT 0x00040000
#define GOT_FILEIDX_F_NO_FILE_ON_DISK 0x00080000
#define GOT_FILEIDX_F_REMOVE_ON_FLUSH 0x00100000
struct got_fileindex {
struct got_fileindex_tree entries;
int nentries; /* Does not include entries marked for removal. */
#define GOT_FILEIDX_MAX_ENTRIES INT_MAX
};
uint16_t
got_fileindex_perms_from_st(struct stat *sb)
{
uint16_t perms = (sb->st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
return (perms << GOT_FILEIDX_MODE_PERMS_SHIFT);
}
mode_t
got_fileindex_perms_to_st(struct got_fileindex_entry *ie)
{
mode_t perms = (ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT);
return (S_IFREG | (perms & (S_IRWXU | S_IRWXG | S_IRWXO)));
}
const struct got_error *
got_fileindex_entry_update(struct got_fileindex_entry *ie,
const char *ondisk_path, uint8_t *blob_sha1, uint8_t *commit_sha1,
int update_timestamps)
{
struct stat sb;
if (lstat(ondisk_path, &sb) != 0) {
if (!((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) &&
errno == ENOENT))
return got_error_from_errno2("lstat", ondisk_path);
} else {
if (sb.st_mode & S_IFDIR)
return got_error_set_errno(EISDIR, ondisk_path);
ie->flags &= ~GOT_FILEIDX_F_NO_FILE_ON_DISK;
}
if ((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0) {
if (update_timestamps) {
ie->ctime_sec = sb.st_ctime;
ie->ctime_nsec = sb.st_ctimensec;
ie->mtime_sec = sb.st_mtime;
ie->mtime_nsec = sb.st_mtimensec;
}
ie->uid = sb.st_uid;
ie->gid = sb.st_gid;
ie->size = (sb.st_size & 0xffffffff);
if (sb.st_mode & S_IFLNK)
ie->mode = GOT_FILEIDX_MODE_SYMLINK;
else
ie->mode = GOT_FILEIDX_MODE_REGULAR_FILE;
ie->mode |= got_fileindex_perms_from_st(&sb);
}
if (blob_sha1) {
memcpy(ie->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
ie->flags &= ~GOT_FILEIDX_F_NO_BLOB;
} else
ie->flags |= GOT_FILEIDX_F_NO_BLOB;
if (commit_sha1) {
memcpy(ie->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
ie->flags &= ~GOT_FILEIDX_F_NO_COMMIT;
} else
ie->flags |= GOT_FILEIDX_F_NO_COMMIT;
return NULL;
}
void
got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *ie)
{
ie->flags |= GOT_FILEIDX_F_NO_FILE_ON_DISK;
}
const struct got_error *
got_fileindex_entry_alloc(struct got_fileindex_entry **ie,
const char *ondisk_path, const char *relpath, uint8_t *blob_sha1,
uint8_t *commit_sha1)
{
size_t len;
*ie = calloc(1, sizeof(**ie));
if (*ie == NULL)
return got_error_from_errno("calloc");
(*ie)->path = strdup(relpath);
if ((*ie)->path == NULL) {
const struct got_error *err = got_error_from_errno("strdup");
free(*ie);
*ie = NULL;
return err;
}
len = strlen(relpath);
if (len > GOT_FILEIDX_F_PATH_LEN)
len = GOT_FILEIDX_F_PATH_LEN;
(*ie)->flags |= len;
return got_fileindex_entry_update(*ie, ondisk_path, blob_sha1,
commit_sha1, 1);
}
void
got_fileindex_entry_free(struct got_fileindex_entry *ie)
{
free(ie->path);
free(ie);
}
size_t
got_fileindex_entry_path_len(const struct got_fileindex_entry *ie)
{
return (size_t)(ie->flags & GOT_FILEIDX_F_PATH_LEN);
}
uint32_t
got_fileindex_entry_stage_get(const struct got_fileindex_entry *ie)
{
return ((ie->flags & GOT_FILEIDX_F_STAGE) >> GOT_FILEIDX_F_STAGE_SHIFT);
}
void
got_fileindex_entry_stage_set(struct got_fileindex_entry *ie, uint32_t stage)
{
ie->flags &= ~GOT_FILEIDX_F_STAGE;
ie->flags |= ((stage << GOT_FILEIDX_F_STAGE_SHIFT) &
GOT_FILEIDX_F_STAGE);
}
int
got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
{
return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
}
int
got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
{
return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
}
int
got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *ie)
{
return (ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0;
}
static const struct got_error *
add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
{
if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
return got_error(GOT_ERR_NO_SPACE);
RB_INSERT(got_fileindex_tree, &fileindex->entries, ie);
fileindex->nentries++;
return NULL;
}
const struct got_error *
got_fileindex_entry_add(struct got_fileindex *fileindex,
struct got_fileindex_entry *ie)
{
/* Flag this entry until it gets written out to disk. */
ie->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
return add_entry(fileindex, ie);
}
void
got_fileindex_entry_remove(struct got_fileindex *fileindex,
struct got_fileindex_entry *ie)
{
/*
* Removing an entry from the RB tree immediately breaks
* in-progress iterations over file index entries.
* So flag this entry for removal and skip it once the index
* is written out to disk, and pretend this entry no longer
* exists if we get queried for it again before then.
*/
ie->flags |= GOT_FILEIDX_F_REMOVE_ON_FLUSH;
fileindex->nentries--;
}
struct got_fileindex_entry *
got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path,
size_t path_len)
{
struct got_fileindex_entry *ie;
struct got_fileindex_entry key;
memset(&key, 0, sizeof(key));
key.path = (char *)path;
key.flags = (path_len & GOT_FILEIDX_F_PATH_LEN);
ie = RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
if (ie && (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH))
return NULL;
return ie;
}
const struct got_error *
got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
got_fileindex_cb cb, void *cb_arg)
{
const struct got_error *err;
struct got_fileindex_entry *ie, *tmp;
RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH)
continue;
err = (*cb)(cb_arg, ie);
if (err)
return err;
}
return NULL;
}
struct got_fileindex *
got_fileindex_alloc(void)
{
struct got_fileindex *fileindex;
fileindex = calloc(1, sizeof(*fileindex));
if (fileindex == NULL)
return NULL;
RB_INIT(&fileindex->entries);
return fileindex;
}
void
got_fileindex_free(struct got_fileindex *fileindex)
{
struct got_fileindex_entry *ie;
while ((ie = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
got_fileindex_entry_free(ie);
}
free(fileindex);
}
static const struct got_error *
write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
{
size_t n;
val = htobe64(val);
SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
n = fwrite(&val, 1, sizeof(val), outfile);
if (n != sizeof(val))
return got_ferror(outfile, GOT_ERR_IO);
return NULL;
}
static const struct got_error *
write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
{
size_t n;
val = htobe32(val);
SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
n = fwrite(&val, 1, sizeof(val), outfile);
if (n != sizeof(val))
return got_ferror(outfile, GOT_ERR_IO);
return NULL;
}
static const struct got_error *
write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
{
size_t n;
val = htobe16(val);
SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
n = fwrite(&val, 1, sizeof(val), outfile);
if (n != sizeof(val))
return got_ferror(outfile, GOT_ERR_IO);
return NULL;
}
static const struct got_error *
write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
{
size_t n, len, pad = 0;
static const uint8_t zero[8] = { 0 };
len = strlen(path);
while ((len + pad) % 8 != 0)
pad++;
if (pad == 0)
pad = 8; /* NUL-terminate */
SHA1Update(ctx, path, len);
n = fwrite(path, 1, len, outfile);
if (n != len)
return got_ferror(outfile, GOT_ERR_IO);
SHA1Update(ctx, zero, pad);
n = fwrite(zero, 1, pad, outfile);
if (n != pad)
return got_ferror(outfile, GOT_ERR_IO);
return NULL;
}
static const struct got_error *
write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *ie,
FILE *outfile)
{
const struct got_error *err;
size_t n;
uint32_t stage;
err = write_fileindex_val64(ctx, ie->ctime_sec, outfile);
if (err)
return err;
err = write_fileindex_val64(ctx, ie->ctime_nsec, outfile);
if (err)
return err;
err = write_fileindex_val64(ctx, ie->mtime_sec, outfile);
if (err)
return err;
err = write_fileindex_val64(ctx, ie->mtime_nsec, outfile);
if (err)
return err;
err = write_fileindex_val32(ctx, ie->uid, outfile);
if (err)
return err;
err = write_fileindex_val32(ctx, ie->gid, outfile);
if (err)
return err;
err = write_fileindex_val32(ctx, ie->size, outfile);
if (err)
return err;
err = write_fileindex_val16(ctx, ie->mode, outfile);
if (err)
return err;
SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
n = fwrite(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
if (n != SHA1_DIGEST_LENGTH)
return got_ferror(outfile, GOT_ERR_IO);
SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
n = fwrite(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
if (n != SHA1_DIGEST_LENGTH)
return got_ferror(outfile, GOT_ERR_IO);
err = write_fileindex_val32(ctx, ie->flags, outfile);
if (err)
return err;
err = write_fileindex_path(ctx, ie->path, outfile);
if (err)
return err;
stage = got_fileindex_entry_stage_get(ie);
if (stage == GOT_FILEIDX_STAGE_MODIFY ||
stage == GOT_FILEIDX_STAGE_ADD) {
SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
n = fwrite(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
outfile);
if (n != SHA1_DIGEST_LENGTH)
return got_ferror(outfile, GOT_ERR_IO);
}
return NULL;
}
const struct got_error *
got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
{
const struct got_error *err = NULL;
struct got_fileindex_hdr hdr;
SHA1_CTX ctx;
uint8_t sha1[SHA1_DIGEST_LENGTH];
size_t n;
struct got_fileindex_entry *ie;
SHA1Init(&ctx);
hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
hdr.nentries = htobe32(fileindex->nentries);
SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
if (n != sizeof(hdr.signature))
return got_ferror(outfile, GOT_ERR_IO);
n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
if (n != sizeof(hdr.version))
return got_ferror(outfile, GOT_ERR_IO);
n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
if (n != sizeof(hdr.nentries))
return got_ferror(outfile, GOT_ERR_IO);
RB_FOREACH(ie, got_fileindex_tree, &fileindex->entries) {
ie->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH)
continue;
err = write_fileindex_entry(&ctx, ie, outfile);
if (err)
return err;
}
SHA1Final(sha1, &ctx);
n = fwrite(sha1, 1, sizeof(sha1), outfile);
if (n != sizeof(sha1))
return got_ferror(outfile, GOT_ERR_IO);
if (fflush(outfile) != 0)
return got_error_from_errno("fflush");
return NULL;
}
static const struct got_error *
read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
{
size_t n;
n = fread(val, 1, sizeof(*val), infile);
if (n != sizeof(*val))
return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
*val = be64toh(*val);
return NULL;
}
static const struct got_error *
read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
{
size_t n;
n = fread(val, 1, sizeof(*val), infile);
if (n != sizeof(*val))
return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
*val = be32toh(*val);
return NULL;
}
static const struct got_error *
read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
{
size_t n;
n = fread(val, 1, sizeof(*val), infile);
if (n != sizeof(*val))
return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
*val = be16toh(*val);
return NULL;
}
static const struct got_error *
read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
{
const struct got_error *err = NULL;
const size_t chunk_size = 8;
size_t n, len = 0, totlen = chunk_size;
*path = malloc(totlen);
if (*path == NULL)
return got_error_from_errno("malloc");
do {
if (len + chunk_size > totlen) {
char *p = reallocarray(*path, totlen + chunk_size, 1);
if (p == NULL) {
err = got_error_from_errno("reallocarray");
break;
}
totlen += chunk_size;
*path = p;
}
n = fread(*path + len, 1, chunk_size, infile);
if (n != chunk_size) {
err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
break;
}
SHA1Update(ctx, *path + len, chunk_size);
len += chunk_size;
} while (memchr(*path + len - chunk_size, '\0', chunk_size) == NULL);
if (err) {
free(*path);
*path = NULL;
}
return err;
}
static const struct got_error *
read_fileindex_entry(struct got_fileindex_entry **iep, SHA1_CTX *ctx,
FILE *infile, uint32_t version)
{
const struct got_error *err;
struct got_fileindex_entry *ie;
size_t n;
*iep = NULL;
ie = calloc(1, sizeof(*ie));
if (ie == NULL)
return got_error_from_errno("calloc");
err = read_fileindex_val64(&ie->ctime_sec, ctx, infile);
if (err)
goto done;
err = read_fileindex_val64(&ie->ctime_nsec, ctx, infile);
if (err)
goto done;
err = read_fileindex_val64(&ie->mtime_sec, ctx, infile);
if (err)
goto done;
err = read_fileindex_val64(&ie->mtime_nsec, ctx, infile);
if (err)
goto done;
err = read_fileindex_val32(&ie->uid, ctx, infile);
if (err)
goto done;
err = read_fileindex_val32(&ie->gid, ctx, infile);
if (err)
goto done;
err = read_fileindex_val32(&ie->size, ctx, infile);
if (err)
goto done;
err = read_fileindex_val16(&ie->mode, ctx, infile);
if (err)
goto done;
n = fread(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
if (n != SHA1_DIGEST_LENGTH) {
err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
goto done;
}
SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
n = fread(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
if (n != SHA1_DIGEST_LENGTH) {
err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
goto done;
}
SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
err = read_fileindex_val32(&ie->flags, ctx, infile);
if (err)
goto done;
err = read_fileindex_path(&ie->path, ctx, infile);
if (err)
goto done;
if (version >= 2) {
uint32_t stage = got_fileindex_entry_stage_get(ie);
if (stage == GOT_FILEIDX_STAGE_MODIFY ||
stage == GOT_FILEIDX_STAGE_ADD) {
n = fread(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
infile);
if (n != SHA1_DIGEST_LENGTH) {
err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
goto done;
}
SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
}
} else {
/* GOT_FILE_INDEX_VERSION 1 does not support staging. */
ie->flags &= ~GOT_FILEIDX_F_STAGE;
}
done:
if (err)
got_fileindex_entry_free(ie);
else
*iep = ie;
return err;
}
const struct got_error *
got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
{
const struct got_error *err = NULL;
struct got_fileindex_hdr hdr;
SHA1_CTX ctx;
struct got_fileindex_entry *ie;
uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
uint8_t sha1[SHA1_DIGEST_LENGTH];
size_t n;
int i;
SHA1Init(&ctx);
n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
if (n != sizeof(hdr.signature)) {
if (n == 0) /* EOF */
return NULL;
return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
}
n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
if (n != sizeof(hdr.version)) {
if (n == 0) /* EOF */
return NULL;
return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
}
n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
if (n != sizeof(hdr.nentries)) {
if (n == 0) /* EOF */
return NULL;
return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
}
SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
hdr.signature = be32toh(hdr.signature);
hdr.version = be32toh(hdr.version);
hdr.nentries = be32toh(hdr.nentries);
if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
return got_error(GOT_ERR_FILEIDX_SIG);
if (hdr.version > GOT_FILE_INDEX_VERSION)
return got_error(GOT_ERR_FILEIDX_VER);
for (i = 0; i < hdr.nentries; i++) {
err = read_fileindex_entry(&ie, &ctx, infile, hdr.version);
if (err)
return err;
err = add_entry(fileindex, ie);
if (err)
return err;
}
n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
if (n != sizeof(sha1_expected))
return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
SHA1Final(sha1, &ctx);
if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
return got_error(GOT_ERR_FILEIDX_CSUM);
return NULL;
}
static struct got_fileindex_entry *
walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
{
struct got_fileindex_entry *next;
next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
/* Skip entries which were added or removed by diff callbacks. */
while (next && (next->flags & (GOT_FILEIDX_F_NOT_FLUSHED |
GOT_FILEIDX_F_REMOVE_ON_FLUSH)))
next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
return next;
}
static const struct got_error *
diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **ie,
struct got_tree_object *tree, const char *, const char *,
struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
static const struct got_error *
walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
struct got_fileindex_entry **ie, struct got_tree_object *tree, int *tidx,
const char *path, const char *entry_name, struct got_repository *repo,
struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
{
const struct got_error *err = NULL;
struct got_tree_entry *te = got_object_tree_get_entry(tree, *tidx);
if (!got_object_tree_entry_is_submodule(te) &&
S_ISDIR(got_tree_entry_get_mode(te))) {
char *subpath;
struct got_tree_object *subtree;
if (asprintf(&subpath, "%s%s%s", path,
path[0] == '\0' ? "" : "/",
got_tree_entry_get_name(te)) == -1)
return got_error_from_errno("asprintf");
err = got_object_open_as_tree(&subtree, repo,
got_tree_entry_get_id(te));
if (err) {
free(subpath);
return err;
}
err = diff_fileindex_tree(fileindex, ie, subtree, subpath,
entry_name, repo, cb, cb_arg);
free(subpath);
got_object_tree_close(subtree);
if (err)
return err;
}
(*tidx)++;
*next = got_object_tree_get_entry(tree, *tidx);
return NULL;
}
static const struct got_error *
diff_fileindex_tree(struct got_fileindex *fileindex,
struct got_fileindex_entry **ie, struct got_tree_object *tree,
const char *path, const char *entry_name, struct got_repository *repo,
struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
{
const struct got_error *err = NULL;
struct got_tree_entry *te = NULL;
size_t path_len = strlen(path);
struct got_fileindex_entry *next;
int tidx = 0;
te = got_object_tree_get_entry(tree, tidx);
while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
if (te && *ie) {
char *te_path;
const char *te_name = got_tree_entry_get_name(te);
int cmp;
if (asprintf(&te_path, "%s/%s", path, te_name) == -1) {
err = got_error_from_errno("asprintf");
break;
}
cmp = got_path_cmp((*ie)->path, te_path,
got_fileindex_entry_path_len(*ie), strlen(te_path));
free(te_path);
if (cmp == 0) {
if (got_path_is_child((*ie)->path, path,
path_len) &&
!got_object_tree_entry_is_submodule(te) &&
(entry_name == NULL ||
strcmp(te_name, entry_name) == 0)) {
err = cb->diff_old_new(cb_arg, *ie, te,
path);
if (err || entry_name)
break;
}
*ie = walk_fileindex(fileindex, *ie);
err = walk_tree(&te, fileindex, ie, tree, &tidx,
path, entry_name, repo, cb, cb_arg);
} else if (cmp < 0) {
next = walk_fileindex(fileindex, *ie);
if (got_path_is_child((*ie)->path, path,
path_len) && (entry_name == NULL ||
strcmp(te_name, entry_name) == 0)) {
err = cb->diff_old(cb_arg, *ie, path);
if (err || entry_name)
break;
}
*ie = next;
} else {
if ((entry_name == NULL ||
strcmp(te_name, entry_name) == 0)) {
err = cb->diff_new(cb_arg, te, path);
if (err || entry_name)
break;
}
err = walk_tree(&te, fileindex, ie, tree, &tidx,
path, entry_name, repo, cb, cb_arg);
}
if (err)
break;
} else if (*ie) {
next = walk_fileindex(fileindex, *ie);
if (got_path_is_child((*ie)->path, path, path_len) &&
(entry_name == NULL ||
(te && strcmp(got_tree_entry_get_name(te),
entry_name) == 0))) {
err = cb->diff_old(cb_arg, *ie, path);
if (err || entry_name)
break;
}
*ie = next;
} else if (te) {
if (!got_object_tree_entry_is_submodule(te) &&
(entry_name == NULL ||
strcmp(got_tree_entry_get_name(te), entry_name)
== 0)) {
err = cb->diff_new(cb_arg, te, path);
if (err || entry_name)
break;
}
err = walk_tree(&te, fileindex, ie, tree, &tidx, path,
entry_name, repo, cb, cb_arg);
if (err)
break;
}
}
return err;
}
const struct got_error *
got_fileindex_diff_tree(struct got_fileindex *fileindex,
struct got_tree_object *tree, const char *path, const char *entry_name,
struct got_repository *repo,
struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
{
struct got_fileindex_entry *ie;
ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
while (ie && !got_path_is_child(ie->path, path, strlen(path)))
ie = walk_fileindex(fileindex, ie);
return diff_fileindex_tree(fileindex, &ie, tree, path, entry_name, repo,
cb, cb_arg);
}
static const struct got_error *
diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **,
struct got_pathlist_head *, int, const char *, const char *,
struct got_repository *, struct got_fileindex_diff_dir_cb *, void *);
static const struct got_error *
read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path)
{
const struct got_error *err = NULL;
struct got_pathlist_entry *new = NULL;
struct dirent *dep = NULL;
struct dirent *de = NULL;
for (;;) {
de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
if (de == NULL) {
err = got_error_from_errno("malloc");
break;
}
if (readdir_r(dir, de, &dep) != 0) {
err = got_error_from_errno("readdir_r");
free(de);
break;
}
if (dep == NULL) {
free(de);
break;
}
if (strcmp(de->d_name, ".") == 0 ||
strcmp(de->d_name, "..") == 0 ||
(path[0] == '\0' &&
strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
free(de);
continue;
}
err = got_pathlist_insert(&new, dirlist, de->d_name, de);
if (err) {
free(de);
break;
}
if (new == NULL) {
err = got_error(GOT_ERR_DIR_DUP_ENTRY);
free(de);
break;
}
}
return err;
}
void
free_dirlist(struct got_pathlist_head *dirlist)
{
struct got_pathlist_entry *dle;
TAILQ_FOREACH(dle, dirlist, entry)
free(dle->data);
got_pathlist_free(dirlist);
}
static const struct got_error *
walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
struct got_fileindex_entry **ie, struct got_pathlist_entry *dle, int fd,
const char *path, const char *rootpath, struct got_repository *repo,
struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
{
const struct got_error *err = NULL;
struct dirent *de = dle->data;
DIR *subdir = NULL;
int subdirfd = -1;
*next = NULL;
if (de->d_type == DT_DIR) {
char *subpath;
char *subdirpath;
struct got_pathlist_head subdirlist;
TAILQ_INIT(&subdirlist);
if (asprintf(&subpath, "%s%s%s", path,
path[0] == '\0' ? "" : "/", de->d_name) == -1)
return got_error_from_errno("asprintf");
if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
free(subpath);
return got_error_from_errno("asprintf");
}
subdirfd = openat(fd, de->d_name,
O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
if (subdirfd == -1) {
if (errno == EACCES) {
*next = TAILQ_NEXT(dle, entry);
return NULL;
}
err = got_error_from_errno2("openat", subdirpath);
free(subpath);
free(subdirpath);
return err;
}
subdir = fdopendir(subdirfd);
if (subdir == NULL)
return got_error_from_errno2("fdopendir", path);
subdirfd = -1;
err = read_dirlist(&subdirlist, subdir, subdirpath);
if (err) {
free(subpath);
free(subdirpath);
closedir(subdir);
return err;
}
err = diff_fileindex_dir(fileindex, ie, &subdirlist,
dirfd(subdir), rootpath, subpath, repo, cb, cb_arg);
if (subdir && closedir(subdir) == -1 && err == NULL)
err = got_error_from_errno2("closedir", subdirpath);
free(subpath);
free(subdirpath);
free_dirlist(&subdirlist);
if (err)
return err;
}
*next = TAILQ_NEXT(dle, entry);
return NULL;
}
static const struct got_error *
diff_fileindex_dir(struct got_fileindex *fileindex,
struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist,
int dirfd, const char *rootpath, const char *path,
struct got_repository *repo,
struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
{
const struct got_error *err = NULL;
struct dirent *de = NULL;
size_t path_len = strlen(path);
struct got_pathlist_entry *dle;
dle = TAILQ_FIRST(dirlist);
while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
if (dle && *ie) {
char *de_path;
int cmp;
de = dle->data;
if (asprintf(&de_path, "%s/%s", path,
de->d_name) == -1) {
err = got_error_from_errno("asprintf");
break;
}
cmp = got_path_cmp((*ie)->path, de_path,
got_fileindex_entry_path_len(*ie),
strlen(path) + 1 + de->d_namlen);
free(de_path);
if (cmp == 0) {
err = cb->diff_old_new(cb_arg, *ie, de, path,
dirfd);
if (err)
break;
*ie = walk_fileindex(fileindex, *ie);
err = walk_dir(&dle, fileindex, ie, dle, dirfd,
path, rootpath, repo, cb, cb_arg);
} else if (cmp < 0 ) {
err = cb->diff_old(cb_arg, *ie, path);
if (err)
break;
*ie = walk_fileindex(fileindex, *ie);
} else {
err = cb->diff_new(cb_arg, de, path, dirfd);
if (err)
break;
err = walk_dir(&dle, fileindex, ie, dle, dirfd,
path, rootpath, repo, cb, cb_arg);
}
if (err)
break;
} else if (*ie) {
err = cb->diff_old(cb_arg, *ie, path);
if (err)
break;
*ie = walk_fileindex(fileindex, *ie);
} else if (dle) {
de = dle->data;
err = cb->diff_new(cb_arg, de, path, dirfd);
if (err)
break;
err = walk_dir(&dle, fileindex, ie, dle, dirfd, path,
rootpath, repo, cb, cb_arg);
if (err)
break;
}
}
return err;
}
const struct got_error *
got_fileindex_diff_dir(struct got_fileindex *fileindex, int fd,
const char *rootpath, const char *path, struct got_repository *repo,
struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
{
const struct got_error *err;
struct got_fileindex_entry *ie;
struct got_pathlist_head dirlist;
int fd2;
DIR *dir;
TAILQ_INIT(&dirlist);
/*
* Duplicate the file descriptor so we can call closedir() below
* without closing the file descriptor passed in by our caller.
*/
fd2 = dup(fd);
if (fd2 == -1)
return got_error_from_errno2("dup", path);
if (lseek(fd2, 0, SEEK_SET) == -1) {
err = got_error_from_errno2("lseek", path);
close(fd2);
return err;
}
dir = fdopendir(fd2);
if (dir == NULL) {
err = got_error_from_errno2("fdopendir", path);
close(fd2);
return err;
}
err = read_dirlist(&dirlist, dir, path);
if (err) {
closedir(dir);
return err;
}
ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
while (ie && !got_path_is_child(ie->path, path, strlen(path)))
ie = walk_fileindex(fileindex, ie);
err = diff_fileindex_dir(fileindex, &ie, &dirlist, dirfd(dir),
rootpath, path, repo, cb, cb_arg);
if (closedir(dir) == -1 && err == NULL)
err = got_error_from_errno2("closedir", path);
free_dirlist(&dirlist);
return err;
}
RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);