Hash :
0dbc2271
Author :
Date :
2019-02-05T16:16:13
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
/*
* 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/stat.h>
#include <sys/limits.h>
#include <sys/queue.h>
#include <sys/tree.h>
#include <dirent.h>
#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <sha1.h>
#include <zlib.h>
#include <fnmatch.h>
#include <libgen.h>
#include "got_error.h"
#include "got_repository.h"
#include "got_reference.h"
#include "got_object.h"
#include "got_worktree.h"
#include "got_opentemp.h"
#include "got_lib_worktree.h"
#include "got_lib_path.h"
#include "got_lib_sha1.h"
#include "got_lib_fileindex.h"
#include "got_lib_inflate.h"
#include "got_lib_delta.h"
#include "got_lib_object.h"
#ifndef MIN
#define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
#endif
static const struct got_error *
create_meta_file(const char *path_got, const char *name, const char *content)
{
const struct got_error *err = NULL;
char *path;
int fd = -1;
if (asprintf(&path, "%s/%s", path_got, name) == -1) {
err = got_error_from_errno();
path = NULL;
goto done;
}
fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
GOT_DEFAULT_FILE_MODE);
if (fd == -1) {
err = got_error_from_errno();
goto done;
}
if (content) {
int len = dprintf(fd, "%s\n", content);
if (len != strlen(content) + 1) {
err = got_error_from_errno();
goto done;
}
}
done:
if (fd != -1 && close(fd) == -1 && err == NULL)
err = got_error_from_errno();
free(path);
return err;
}
static const struct got_error *
update_meta_file(const char *path_got, const char *name, const char *content)
{
const struct got_error *err = NULL;
FILE *tmpfile = NULL;
char *tmppath = NULL;
char *path = NULL;
if (asprintf(&path, "%s/%s", path_got, name) == -1) {
err = got_error_from_errno();
path = NULL;
goto done;
}
err = got_opentemp_named(&tmppath, &tmpfile, path);
if (err)
goto done;
if (content) {
int len = fprintf(tmpfile, "%s\n", content);
if (len != strlen(content) + 1) {
err = got_error_from_errno();
goto done;
}
}
if (rename(tmppath, path) != 0) {
err = got_error_from_errno();
goto done;
}
done:
free(tmppath);
fclose(tmpfile);
return err;
}
static const struct got_error *
read_meta_file(char **content, const char *path_got, const char *name)
{
const struct got_error *err = NULL;
char *path;
int fd = -1;
ssize_t n;
struct stat sb;
*content = NULL;
if (asprintf(&path, "%s/%s", path_got, name) == -1) {
err = got_error_from_errno();
path = NULL;
goto done;
}
fd = open(path, O_RDONLY | O_NOFOLLOW);
if (fd == -1) {
err = got_error_from_errno();
goto done;
}
if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
: got_error_from_errno());
goto done;
}
stat(path, &sb);
*content = calloc(1, sb.st_size);
if (*content == NULL) {
err = got_error_from_errno();
goto done;
}
n = read(fd, *content, sb.st_size);
if (n != sb.st_size) {
err = (n == -1 ? got_error_from_errno() :
got_error(GOT_ERR_WORKTREE_META));
goto done;
}
if ((*content)[sb.st_size - 1] != '\n') {
err = got_error(GOT_ERR_WORKTREE_META);
goto done;
}
(*content)[sb.st_size - 1] = '\0';
done:
if (fd != -1 && close(fd) == -1 && err == NULL)
err = got_error_from_errno();
free(path);
if (err) {
free(*content);
*content = NULL;
}
return err;
}
const struct got_error *
got_worktree_init(const char *path, struct got_reference *head_ref,
const char *prefix, struct got_repository *repo)
{
const struct got_error *err = NULL;
struct got_object_id *commit_id = NULL;
int obj_type;
char *path_got = NULL;
char *refstr = NULL;
char *formatstr = NULL;
char *absprefix = NULL;
char *basestr = NULL;
err = got_ref_resolve(&commit_id, repo, head_ref);
if (err)
return err;
err = got_object_get_type(&obj_type, repo, commit_id);
if (err)
return err;
if (obj_type != GOT_OBJ_TYPE_COMMIT)
return got_error(GOT_ERR_OBJ_TYPE);
if (!got_path_is_absolute(prefix)) {
if (asprintf(&absprefix, "/%s", prefix) == -1)
return got_error_from_errno();
}
/* Create top-level directory (may already exist). */
if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
err = got_error_from_errno();
goto done;
}
/* Create .got directory (may already exist). */
if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
err = got_error_from_errno();
goto done;
}
if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
err = got_error_from_errno();
goto done;
}
/* Create an empty lock file. */
err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
if (err)
goto done;
/* Create an empty file index. */
err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
if (err)
goto done;
/* Write the HEAD reference. */
refstr = got_ref_to_str(head_ref);
if (refstr == NULL) {
err = got_error_from_errno();
goto done;
}
err = create_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
if (err)
goto done;
/* Record our base commit. */
err = got_object_id_str(&basestr, commit_id);
if (err)
goto done;
err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
if (err)
goto done;
/* Store path to repository. */
err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
got_repo_get_path(repo));
if (err)
goto done;
/* Store in-repository path prefix. */
err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
absprefix ? absprefix : prefix);
if (err)
goto done;
/* Stamp work tree with format file. */
if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
err = got_error_from_errno();
goto done;
}
err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
if (err)
goto done;
done:
free(commit_id);
free(path_got);
free(formatstr);
free(refstr);
free(absprefix);
free(basestr);
return err;
}
const struct got_error *
got_worktree_open(struct got_worktree **worktree, const char *path)
{
const struct got_error *err = NULL;
char *path_got;
char *formatstr = NULL;
char *path_lock = NULL;
char *base_commit_id_str = NULL;
char *head_ref_str = NULL;
int version, fd = -1;
const char *errstr;
struct got_repository *repo = NULL;
*worktree = NULL;
if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
err = got_error_from_errno();
path_got = NULL;
goto done;
}
if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
err = got_error_from_errno();
path_lock = NULL;
goto done;
}
fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
if (fd == -1) {
err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
: got_error_from_errno());
goto done;
}
err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
if (err)
goto done;
version = strtonum(formatstr, 1, INT_MAX, &errstr);
if (errstr) {
err = got_error(GOT_ERR_WORKTREE_META);
goto done;
}
if (version != GOT_WORKTREE_FORMAT_VERSION) {
err = got_error(GOT_ERR_WORKTREE_VERS);
goto done;
}
*worktree = calloc(1, sizeof(**worktree));
if (*worktree == NULL) {
err = got_error_from_errno();
goto done;
}
(*worktree)->lockfd = -1;
(*worktree)->root_path = strdup(path);
if ((*worktree)->root_path == NULL) {
err = got_error_from_errno();
goto done;
}
err = read_meta_file(&(*worktree)->repo_path, path_got,
GOT_WORKTREE_REPOSITORY);
if (err)
goto done;
err = read_meta_file(&(*worktree)->path_prefix, path_got,
GOT_WORKTREE_PATH_PREFIX);
if (err)
goto done;
err = read_meta_file(&base_commit_id_str, path_got,
GOT_WORKTREE_BASE_COMMIT);
if (err)
goto done;
err = got_repo_open(&repo, (*worktree)->repo_path);
if (err)
goto done;
err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
base_commit_id_str);
if (err)
goto done;
err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
if (err)
goto done;
err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
done:
if (repo)
got_repo_close(repo);
free(path_got);
free(path_lock);
free(head_ref_str);
free(base_commit_id_str);
if (err) {
if (fd != -1)
close(fd);
if (*worktree != NULL)
got_worktree_close(*worktree);
*worktree = NULL;
} else
(*worktree)->lockfd = fd;
return err;
}
void
got_worktree_close(struct got_worktree *worktree)
{
free(worktree->root_path);
free(worktree->repo_path);
free(worktree->path_prefix);
free(worktree->base_commit_id);
if (worktree->head_ref)
got_ref_close(worktree->head_ref);
if (worktree->lockfd != -1)
close(worktree->lockfd);
free(worktree);
}
const char *
got_worktree_get_repo_path(struct got_worktree *worktree)
{
return worktree->repo_path;
}
const char *
got_worktree_get_path_prefix(struct got_worktree *worktree)
{
return worktree->path_prefix;
}
const struct got_error *
got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
const char *path_prefix)
{
char *absprefix = NULL;
if (!got_path_is_absolute(path_prefix)) {
if (asprintf(&absprefix, "/%s", path_prefix) == -1)
return got_error_from_errno();
}
*match = (strcmp(absprefix ? absprefix : path_prefix,
worktree->path_prefix) == 0);
free(absprefix);
return NULL;
}
char *
got_worktree_get_head_ref_name(struct got_worktree *worktree)
{
return got_ref_to_str(worktree->head_ref);
}
struct got_reference *
got_worktree_get_head_ref(struct got_worktree *worktree)
{
return got_ref_dup(worktree->head_ref);
}
const struct got_object_id *
got_worktree_get_base_commit_id(struct got_worktree *worktree)
{
return worktree->base_commit_id;
}
const struct got_error *
got_worktree_set_base_commit_id(struct got_worktree *worktree,
struct got_repository *repo, struct got_object_id *commit_id)
{
const struct got_error *err;
struct got_object *obj = NULL;
char *id_str = NULL;
char *path_got = NULL;
if (asprintf(&path_got, "%s/%s", worktree->root_path,
GOT_WORKTREE_GOT_DIR) == -1) {
err = got_error_from_errno();
path_got = NULL;
goto done;
}
err = got_object_open(&obj, repo, commit_id);
if (err)
return err;
if (obj->type != GOT_OBJ_TYPE_COMMIT) {
err = got_error(GOT_ERR_OBJ_TYPE);
goto done;
}
/* Record our base commit. */
err = got_object_id_str(&id_str, commit_id);
if (err)
goto done;
err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
if (err)
goto done;
free(worktree->base_commit_id);
worktree->base_commit_id = got_object_id_dup(commit_id);
if (worktree->base_commit_id == NULL) {
err = got_error_from_errno();
goto done;
}
done:
if (obj)
got_object_close(obj);
free(id_str);
free(path_got);
return err;
}
static const struct got_error *
lock_worktree(struct got_worktree *worktree, int operation)
{
if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
: got_error_from_errno());
return NULL;
}
static const struct got_error *
make_parent_dirs(const char *abspath)
{
const struct got_error *err = NULL;
char *parent = dirname(abspath);
if (parent == NULL)
return NULL;
if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
if (errno == ENOENT) {
err = make_parent_dirs(parent);
if (err)
return err;
if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1)
return got_error_from_errno();
} else
err = got_error_from_errno();
}
return err;
}
static const struct got_error *
add_dir_on_disk(struct got_worktree *worktree, const char *path)
{
const struct got_error *err = NULL;
char *abspath;
if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
return got_error_from_errno();
/* XXX queue work rather than editing disk directly? */
if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
struct stat sb;
if (errno == EEXIST) {
if (lstat(abspath, &sb) == -1) {
err = got_error_from_errno();
goto done;
}
if (!S_ISDIR(sb.st_mode)) {
/* TODO directory is obstructed; do something */
err = got_error(GOT_ERR_FILE_OBSTRUCTED);
goto done;
}
return NULL;
} else if (errno == ENOENT) {
err = make_parent_dirs(abspath);
if (err)
goto done;
if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1)
err = got_error_from_errno();
} else
err = got_error_from_errno();
}
done:
free(abspath);
return err;
}
static const struct got_error *
install_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
struct got_fileindex_entry *entry, const char *path,
struct got_blob_object *blob,
struct got_repository *repo, got_worktree_checkout_cb progress_cb,
void *progress_arg)
{
const struct got_error *err = NULL;
char *ondisk_path;
int fd = -1;
size_t len, hdrlen;
int update = 0;
char *tmppath = NULL;
if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
return got_error_from_errno();
fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
GOT_DEFAULT_FILE_MODE);
if (fd == -1) {
if (errno == ENOENT) {
char *parent = dirname(path);
if (parent == NULL)
return got_error_from_errno();
err = add_dir_on_disk(worktree, parent);
if (err)
return err;
fd = open(ondisk_path,
O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
GOT_DEFAULT_FILE_MODE);
if (fd == -1)
return got_error_from_errno();
} else if (errno == EEXIST) {
struct stat sb;
if (lstat(ondisk_path, &sb) == -1) {
err = got_error_from_errno();
goto done;
} else if (!S_ISREG(sb.st_mode)) {
/* TODO file is obstructed; do something */
err = got_error(GOT_ERR_FILE_OBSTRUCTED);
goto done;
} else {
err = got_opentemp_named_fd(&tmppath, &fd,
ondisk_path);
if (err)
goto done;
update = 1;
}
} else
return got_error_from_errno();
}
(*progress_cb)(progress_arg,
update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
hdrlen = got_object_blob_get_hdrlen(blob);
do {
const uint8_t *buf = got_object_blob_get_read_buf(blob);
err = got_object_blob_read_block(&len, blob);
if (err)
break;
if (len > 0) {
/* Skip blob object header first time around. */
ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
if (outlen == -1) {
err = got_error_from_errno();
goto done;
} else if (outlen != len - hdrlen) {
err = got_error(GOT_ERR_IO);
goto done;
}
hdrlen = 0;
}
} while (len != 0);
fsync(fd);
if (update) {
if (rename(tmppath, ondisk_path) != 0) {
err = got_error_from_errno();
goto done;
}
}
if (entry == NULL)
entry = got_fileindex_entry_get(fileindex, path);
if (entry)
err = got_fileindex_entry_update(entry, ondisk_path,
blob->id.sha1, worktree->base_commit_id->sha1);
else {
err = got_fileindex_entry_alloc(&entry, ondisk_path,
path, blob->id.sha1, worktree->base_commit_id->sha1);
if (err)
goto done;
err = got_fileindex_entry_add(fileindex, entry);
}
done:
if (fd != -1)
close(fd);
free(ondisk_path);
free(tmppath);
return err;
}
static const struct got_error *
update_blob(struct got_worktree *worktree,
struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
struct got_tree_entry *te, const char *path,
struct got_repository *repo, got_worktree_checkout_cb progress_cb,
void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
{
const struct got_error *err = NULL;
struct got_blob_object *blob = NULL;
if (ie) {
if (memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
SHA1_DIGEST_LENGTH) == 0) {
(*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
path);
return NULL;
}
if (memcmp(ie->blob_sha1,
te->id->sha1, SHA1_DIGEST_LENGTH) == 0)
return NULL;
}
err = got_object_open_as_blob(&blob, repo, te->id, 8192);
if (err)
return err;
err = install_blob(worktree, fileindex, ie, path, blob, repo,
progress_cb, progress_arg);
got_object_blob_close(blob);
return err;
}
static const struct got_error *
remove_ondisk_file(const char *root_path, const char *path)
{
const struct got_error *err = NULL;
char *ondisk_path = NULL;
if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
return got_error_from_errno();
if (unlink(ondisk_path) == -1) {
if (errno != ENOENT)
err = got_error_from_errno();
} else {
char *parent = dirname(ondisk_path);
while (parent && strcmp(parent, root_path) != 0) {
if (rmdir(parent) == -1) {
if (errno != ENOTEMPTY)
err = got_error_from_errno();
break;
}
parent = dirname(parent);
}
}
free(ondisk_path);
return err;
}
struct diff_cb_arg {
struct got_fileindex *fileindex;
struct got_worktree *worktree;
struct got_repository *repo;
got_worktree_checkout_cb progress_cb;
void *progress_arg;
got_worktree_cancel_cb cancel_cb;
void *cancel_arg;
};
static const struct got_error *
diff_old_new(void *arg, struct got_fileindex_entry *ie,
struct got_tree_entry *te, const char *parent_path)
{
struct diff_cb_arg *a = arg;
return update_blob(a->worktree, a->fileindex, ie, te,
ie->path, a->repo, a->progress_cb, a->progress_arg,
a->cancel_cb, a->cancel_arg);
}
static const struct got_error *
diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
{
const struct got_error *err;
struct diff_cb_arg *a = arg;
(*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE, ie->path);
err = remove_ondisk_file(a->worktree->root_path, ie->path);
if (err)
return err;
got_fileindex_entry_remove(a->fileindex, ie);
return NULL;
}
static const struct got_error *
diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
{
struct diff_cb_arg *a = arg;
const struct got_error *err;
char *path;
if (asprintf(&path, "%s%s%s", parent_path,
parent_path[0] ? "/" : "", te->name)
== -1)
return got_error_from_errno();
if (S_ISDIR(te->mode))
err = add_dir_on_disk(a->worktree, path);
else
err = update_blob(a->worktree, a->fileindex, NULL, te, path,
a->repo, a->progress_cb, a->progress_arg,
a->cancel_cb, a->cancel_arg);
free(path);
return err;
}
const struct got_error *
got_worktree_checkout_files(struct got_worktree *worktree,
struct got_repository *repo, got_worktree_checkout_cb progress_cb,
void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
{
const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
struct got_commit_object *commit = NULL;
struct got_object_id *tree_id = NULL;
struct got_tree_object *tree = NULL;
char *fileindex_path = NULL, *new_fileindex_path = NULL;
struct got_fileindex *fileindex = NULL;
FILE *index = NULL, *new_index = NULL;
struct got_fileindex_diff_tree_cb diff_cb;
struct diff_cb_arg arg;
err = lock_worktree(worktree, LOCK_EX);
if (err)
return err;
fileindex = got_fileindex_alloc();
if (fileindex == NULL) {
err = got_error_from_errno();
goto done;
}
if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
err = got_error_from_errno();
fileindex_path = NULL;
goto done;
}
/*
* Read the file index.
* Checking out files is supposed to be an idempotent operation.
* If the on-disk file index is incomplete we will try to complete it.
*/
index = fopen(fileindex_path, "rb");
if (index == NULL) {
if (errno != ENOENT) {
err = got_error_from_errno();
goto done;
}
} else {
err = got_fileindex_read(fileindex, index);
fclose(index);
if (err)
goto done;
}
err = got_opentemp_named(&new_fileindex_path, &new_index,
fileindex_path);
if (err)
goto done;
err = got_object_open_as_commit(&commit, repo,
worktree->base_commit_id);
if (err)
goto done;
err = got_object_id_by_path(&tree_id, repo,
worktree->base_commit_id, worktree->path_prefix);
if (err)
goto done;
err = got_object_open_as_tree(&tree, repo, tree_id);
if (err)
goto done;
diff_cb.diff_old_new = diff_old_new;
diff_cb.diff_old = diff_old;
diff_cb.diff_new = diff_new;
arg.fileindex = fileindex;
arg.worktree = worktree;
arg.repo = repo;
arg.progress_cb = progress_cb;
arg.progress_arg = progress_arg;
arg.cancel_cb = cancel_cb;
arg.cancel_arg = cancel_arg;
checkout_err = got_fileindex_diff_tree(fileindex, tree, repo,
&diff_cb, &arg);
/* Try to sync the fileindex back to disk in any case. */
err = got_fileindex_write(fileindex, new_index);
if (err)
goto done;
if (rename(new_fileindex_path, fileindex_path) != 0) {
err = got_error_from_errno();
goto done;
}
free(new_fileindex_path);
new_fileindex_path = NULL;
done:
if (tree)
got_object_tree_close(tree);
if (commit)
got_object_commit_close(commit);
if (new_fileindex_path)
unlink(new_fileindex_path);
if (new_index)
fclose(new_index);
free(new_fileindex_path);
free(fileindex_path);
got_fileindex_free(fileindex);
if (checkout_err)
err = checkout_err;
unlockerr = lock_worktree(worktree, LOCK_SH);
if (unlockerr && err == NULL)
err = unlockerr;
return err;
}
struct diff_dir_cb_arg {
struct got_fileindex *fileindex;
struct got_worktree *worktree;
struct got_repository *repo;
got_worktree_status_cb status_cb;
void *status_arg;
got_worktree_cancel_cb cancel_cb;
void *cancel_arg;
};
static const struct got_error *
get_file_status(unsigned char *status, struct got_fileindex_entry *ie,
const char *abspath, struct got_repository *repo)
{
const struct got_error *err = NULL;
struct got_object_id id;
size_t hdrlen;
FILE *f = NULL;
uint8_t fbuf[8192];
struct got_blob_object *blob = NULL;
size_t flen, blen;
struct stat sb;
*status = GOT_STATUS_NO_CHANGE;
if (lstat(abspath, &sb) == -1)
return got_error_from_errno();
if (!S_ISREG(sb.st_mode)) {
*status = GOT_STATUS_OBSTRUCTED;
return NULL;
}
if (ie->ctime_sec == sb.st_ctime &&
ie->ctime_nsec == sb.st_ctimensec &&
ie->mtime_sec == sb.st_mtime &&
ie->mtime_sec == sb.st_mtime &&
ie->mtime_nsec == sb.st_mtimensec &&
ie->size == (sb.st_size & 0xffffffff))
return NULL;
memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
if (err)
return err;
f = fopen(abspath, "r");
if (f == NULL) {
err = got_error_from_errno();
goto done;
}
hdrlen = got_object_blob_get_hdrlen(blob);
while (1) {
const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
err = got_object_blob_read_block(&blen, blob);
if (err)
break;
flen = fread(fbuf, 1, sizeof(fbuf), f);
if (blen == 0) {
if (flen != 0)
*status = GOT_STATUS_MODIFIY;
break;
} else if (flen == 0) {
if (blen != 0)
*status = GOT_STATUS_MODIFIY;
break;
} else if (blen == flen) {
/* Skip blob object header first time around. */
if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
*status = GOT_STATUS_MODIFIY;
break;
}
} else {
*status = GOT_STATUS_MODIFIY;
break;
}
hdrlen = 0;
}
done:
if (blob)
got_object_blob_close(blob);
if (f)
fclose(f);
return err;
}
static const struct got_error *
status_old_new(void *arg, struct got_fileindex_entry *ie,
struct dirent *de, const char *parent_path)
{
const struct got_error *err = NULL;
struct diff_dir_cb_arg *a = arg;
char *abspath;
unsigned char status = GOT_STATUS_NO_CHANGE;
if (parent_path[0]) {
if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
parent_path, de->d_name) == -1)
return got_error_from_errno();
} else {
if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
de->d_name) == -1)
return got_error_from_errno();
}
err = get_file_status(&status, ie, abspath, a->repo);
if (err == NULL && status != GOT_STATUS_NO_CHANGE)
(*a->status_cb)(a->status_arg, status, ie->path);
free(abspath);
return err;
}
static const struct got_error *
status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
{
struct diff_dir_cb_arg *a = arg;
(*a->status_cb)(a->status_arg, GOT_STATUS_MISSING, ie->path);
return NULL;
}
static const struct got_error *
status_new(void *arg, struct dirent *de, const char *parent_path)
{
struct diff_dir_cb_arg *a = arg;
char *path = NULL;
if (de->d_type == DT_DIR)
return NULL;
if (parent_path[0]) {
if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
return got_error_from_errno();
} else {
path = de->d_name;
}
(*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path);
if (parent_path[0])
free(path);
return NULL;
}
const struct got_error *
got_worktree_status(struct got_worktree *worktree,
struct got_repository *repo, got_worktree_status_cb status_cb,
void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
{
const struct got_error *err = NULL;
DIR *workdir = NULL;
char *fileindex_path = NULL;
struct got_fileindex *fileindex = NULL;
FILE *index = NULL;
struct got_fileindex_diff_dir_cb diff_cb;
struct diff_dir_cb_arg arg;
fileindex = got_fileindex_alloc();
if (fileindex == NULL) {
err = got_error_from_errno();
goto done;
}
if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
err = got_error_from_errno();
fileindex_path = NULL;
goto done;
}
index = fopen(fileindex_path, "rb");
if (index == NULL) {
if (errno != ENOENT) {
err = got_error_from_errno();
goto done;
}
} else {
err = got_fileindex_read(fileindex, index);
fclose(index);
if (err)
goto done;
}
workdir = opendir(worktree->root_path);
diff_cb.diff_old_new = status_old_new;
diff_cb.diff_old = status_old;
diff_cb.diff_new = status_new;
arg.fileindex = fileindex;
arg.worktree = worktree;
arg.repo = repo;
arg.status_cb = status_cb;
arg.status_arg = status_arg;
arg.cancel_cb = cancel_cb;
arg.cancel_arg = cancel_arg;
err = got_fileindex_diff_dir(fileindex, workdir, repo, &diff_cb, &arg);
done:
if (workdir)
closedir(workdir);
free(fileindex_path);
got_fileindex_free(fileindex);
return err;
}