Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/limits.h>
19 #include <sys/queue.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <fcntl.h>
25 #include <errno.h>
26 #include <unistd.h>
27 #include <sha1.h>
28 #include <zlib.h>
29 #include <fnmatch.h>
31 #include "got_error.h"
32 #include "got_repository.h"
33 #include "got_reference.h"
34 #include "got_object.h"
35 #include "got_worktree.h"
36 #include "got_opentemp.h"
38 #include "got_lib_worktree.h"
39 #include "got_lib_path.h"
40 #include "got_lib_sha1.h"
41 #include "got_lib_fileindex.h"
42 #include "got_lib_inflate.h"
43 #include "got_lib_delta.h"
44 #include "got_lib_object.h"
46 #ifndef MIN
47 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
48 #endif
50 static const struct got_error *
51 create_meta_file(const char *path_got, const char *name, const char *content)
52 {
53 const struct got_error *err = NULL;
54 char *path;
55 int fd = -1;
57 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
58 err = got_error_from_errno();
59 path = NULL;
60 goto done;
61 }
63 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
64 GOT_DEFAULT_FILE_MODE);
65 if (fd == -1) {
66 err = got_error_from_errno();
67 goto done;
68 }
70 if (content) {
71 int len = dprintf(fd, "%s\n", content);
72 if (len != strlen(content) + 1) {
73 err = got_error_from_errno();
74 goto done;
75 }
76 }
78 done:
79 if (fd != -1 && close(fd) == -1 && err == NULL)
80 err = got_error_from_errno();
81 free(path);
82 return err;
83 }
85 static const struct got_error *
86 update_meta_file(const char *path_got, const char *name, const char *content)
87 {
88 const struct got_error *err = NULL;
89 FILE *tmpfile = NULL;
90 char *tmppath = NULL;
91 char *path = NULL;
93 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
94 err = got_error_from_errno();
95 path = NULL;
96 goto done;
97 }
99 err = got_opentemp_named(&tmppath, &tmpfile, path);
100 if (err)
101 goto done;
103 if (content) {
104 int len = fprintf(tmpfile, "%s\n", content);
105 if (len != strlen(content) + 1) {
106 err = got_error_from_errno();
107 goto done;
111 if (rename(tmppath, path) != 0) {
112 err = got_error_from_errno();
113 goto done;
116 done:
117 free(tmppath);
118 fclose(tmpfile);
119 return err;
122 static const struct got_error *
123 read_meta_file(char **content, const char *path_got, const char *name)
125 const struct got_error *err = NULL;
126 char *path;
127 int fd = -1;
128 ssize_t n;
129 struct stat sb;
131 *content = NULL;
133 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
134 err = got_error_from_errno();
135 path = NULL;
136 goto done;
139 fd = open(path, O_RDONLY | O_NOFOLLOW);
140 if (fd == -1) {
141 err = got_error_from_errno();
142 goto done;
144 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
145 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
146 : got_error_from_errno());
147 goto done;
150 stat(path, &sb);
151 *content = calloc(1, sb.st_size);
152 if (*content == NULL) {
153 err = got_error_from_errno();
154 goto done;
157 n = read(fd, *content, sb.st_size);
158 if (n != sb.st_size) {
159 err = (n == -1 ? got_error_from_errno() :
160 got_error(GOT_ERR_WORKTREE_META));
161 goto done;
163 if ((*content)[sb.st_size - 1] != '\n') {
164 err = got_error(GOT_ERR_WORKTREE_META);
165 goto done;
167 (*content)[sb.st_size - 1] = '\0';
169 done:
170 if (fd != -1 && close(fd) == -1 && err == NULL)
171 err = got_error_from_errno();
172 free(path);
173 if (err) {
174 free(*content);
175 *content = NULL;
177 return err;
180 const struct got_error *
181 got_worktree_init(const char *path, struct got_reference *head_ref,
182 const char *prefix, struct got_repository *repo)
184 const struct got_error *err = NULL;
185 struct got_object_id *commit_id = NULL;
186 int obj_type;
187 char *path_got = NULL;
188 char *refstr = NULL;
189 char *repo_path = NULL;
190 char *formatstr = NULL;
191 char *absprefix = NULL;
192 char *basestr = NULL;
194 err = got_ref_resolve(&commit_id, repo, head_ref);
195 if (err)
196 return err;
197 err = got_object_get_type(&obj_type, repo, commit_id);
198 if (err)
199 return err;
200 if (obj_type != GOT_OBJ_TYPE_COMMIT)
201 return got_error(GOT_ERR_OBJ_TYPE);
203 if (!got_path_is_absolute(prefix)) {
204 if (asprintf(&absprefix, "/%s", prefix) == -1)
205 return got_error_from_errno();
208 /* Create top-level directory (may already exist). */
209 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
210 err = got_error_from_errno();
211 goto done;
214 /* Create .got directory (may already exist). */
215 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
216 err = got_error_from_errno();
217 goto done;
219 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
220 err = got_error_from_errno();
221 goto done;
224 /* Create an empty lock file. */
225 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
226 if (err)
227 goto done;
229 /* Create an empty file index. */
230 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
231 if (err)
232 goto done;
234 /* Write the HEAD reference. */
235 refstr = got_ref_to_str(head_ref);
236 if (refstr == NULL) {
237 err = got_error_from_errno();
238 goto done;
240 err = create_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
241 if (err)
242 goto done;
244 /* Record our base commit. */
245 err = got_object_id_str(&basestr, commit_id);
246 if (err)
247 goto done;
248 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
249 if (err)
250 goto done;
252 /* Store path to repository. */
253 repo_path = got_repo_get_path(repo);
254 if (repo_path == NULL) {
255 err = got_error_from_errno();
256 goto done;
258 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY, repo_path);
259 if (err)
260 goto done;
262 /* Store in-repository path prefix. */
263 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
264 absprefix ? absprefix : prefix);
265 if (err)
266 goto done;
268 /* Stamp work tree with format file. */
269 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
270 err = got_error_from_errno();
271 goto done;
273 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
274 if (err)
275 goto done;
277 done:
278 free(commit_id);
279 free(path_got);
280 free(formatstr);
281 free(refstr);
282 free(repo_path);
283 free(absprefix);
284 free(basestr);
285 return err;
288 const struct got_error *
289 got_worktree_open(struct got_worktree **worktree, const char *path)
291 const struct got_error *err = NULL;
292 char *path_got;
293 char *formatstr = NULL;
294 char *path_lock = NULL;
295 char *base_commit_id_str = NULL;
296 char *head_ref_str = NULL;
297 int version, fd = -1;
298 const char *errstr;
299 struct got_repository *repo = NULL;
301 *worktree = NULL;
303 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
304 err = got_error_from_errno();
305 path_got = NULL;
306 goto done;
309 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
310 err = got_error_from_errno();
311 path_lock = NULL;
312 goto done;
315 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
316 if (fd == -1) {
317 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
318 : got_error_from_errno());
319 goto done;
322 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
323 if (err)
324 goto done;
326 version = strtonum(formatstr, 1, INT_MAX, &errstr);
327 if (errstr) {
328 err = got_error(GOT_ERR_WORKTREE_META);
329 goto done;
331 if (version != GOT_WORKTREE_FORMAT_VERSION) {
332 err = got_error(GOT_ERR_WORKTREE_VERS);
333 goto done;
336 *worktree = calloc(1, sizeof(**worktree));
337 if (*worktree == NULL) {
338 err = got_error_from_errno();
339 goto done;
341 (*worktree)->lockfd = -1;
343 (*worktree)->root_path = strdup(path);
344 if ((*worktree)->root_path == NULL) {
345 err = got_error_from_errno();
346 goto done;
348 err = read_meta_file(&(*worktree)->repo_path, path_got,
349 GOT_WORKTREE_REPOSITORY);
350 if (err)
351 goto done;
353 err = read_meta_file(&(*worktree)->path_prefix, path_got,
354 GOT_WORKTREE_PATH_PREFIX);
355 if (err)
356 goto done;
358 err = read_meta_file(&base_commit_id_str, path_got,
359 GOT_WORKTREE_BASE_COMMIT);
360 if (err)
361 goto done;
363 err = got_repo_open(&repo, (*worktree)->repo_path);
364 if (err)
365 goto done;
367 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
368 base_commit_id_str);
369 if (err)
370 goto done;
372 err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
373 if (err)
374 goto done;
376 err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
377 done:
378 if (repo)
379 got_repo_close(repo);
380 free(path_got);
381 free(path_lock);
382 free(head_ref_str);
383 free(base_commit_id_str);
384 if (err) {
385 if (fd != -1)
386 close(fd);
387 if (*worktree != NULL)
388 got_worktree_close(*worktree);
389 *worktree = NULL;
390 } else
391 (*worktree)->lockfd = fd;
393 return err;
396 void
397 got_worktree_close(struct got_worktree *worktree)
399 free(worktree->root_path);
400 free(worktree->repo_path);
401 free(worktree->path_prefix);
402 free(worktree->base_commit_id);
403 if (worktree->head_ref)
404 got_ref_close(worktree->head_ref);
405 if (worktree->lockfd != -1)
406 close(worktree->lockfd);
407 free(worktree);
410 const char *
411 got_worktree_get_repo_path(struct got_worktree *worktree)
413 return worktree->repo_path;
416 const char *
417 got_worktree_get_path_prefix(struct got_worktree *worktree)
419 return worktree->path_prefix;
422 const struct got_error *
423 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
424 const char *path_prefix)
426 char *absprefix = NULL;
428 if (!got_path_is_absolute(path_prefix)) {
429 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
430 return got_error_from_errno();
432 *match = (strcmp(absprefix ? absprefix : path_prefix,
433 worktree->path_prefix) == 0);
434 free(absprefix);
435 return NULL;
438 char *
439 got_worktree_get_head_ref_name(struct got_worktree *worktree)
441 return got_ref_to_str(worktree->head_ref);
444 struct got_reference *
445 got_worktree_get_head_ref(struct got_worktree *worktree)
447 return got_ref_dup(worktree->head_ref);
450 const struct got_object_id *
451 got_worktree_get_base_commit_id(struct got_worktree *worktree)
453 return worktree->base_commit_id;
456 const struct got_error *
457 got_worktree_set_base_commit_id(struct got_worktree *worktree,
458 struct got_repository *repo, struct got_object_id *commit_id)
460 const struct got_error *err;
461 struct got_object *obj = NULL;
462 char *id_str = NULL;
463 char *path_got = NULL;
465 if (asprintf(&path_got, "%s/%s", worktree->root_path,
466 GOT_WORKTREE_GOT_DIR) == -1) {
467 err = got_error_from_errno();
468 path_got = NULL;
469 goto done;
472 err = got_object_open(&obj, repo, commit_id);
473 if (err)
474 return err;
476 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
477 err = got_error(GOT_ERR_OBJ_TYPE);
478 goto done;
481 /* Record our base commit. */
482 err = got_object_id_str(&id_str, commit_id);
483 if (err)
484 goto done;
485 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
486 if (err)
487 goto done;
489 free(worktree->base_commit_id);
490 worktree->base_commit_id = got_object_id_dup(commit_id);
491 if (worktree->base_commit_id == NULL) {
492 err = got_error_from_errno();
493 goto done;
495 done:
496 if (obj)
497 got_object_close(obj);
498 free(id_str);
499 free(path_got);
500 return err;
503 static const struct got_error *
504 lock_worktree(struct got_worktree *worktree, int operation)
506 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
507 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
508 : got_error_from_errno());
509 return NULL;
512 static const char *
513 apply_path_prefix(struct got_worktree *worktree, const char *path)
515 const char *p = path;
516 p += strlen(worktree->path_prefix);
517 if (*p == '/')
518 p++;
519 return p;
522 static const struct got_error *
523 blob_checkout(struct got_worktree *worktree, struct got_fileindex *fileindex,
524 struct got_fileindex_entry *entry, const char *path,
525 struct got_blob_object *blob, struct got_repository *repo,
526 got_worktree_checkout_cb progress_cb, void *progress_arg,
527 const char *progress_path)
529 const struct got_error *err = NULL;
530 char *ondisk_path;
531 int fd = -1;
532 size_t len, hdrlen;
533 int update = 0;
534 char *tmppath = NULL;
536 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
537 apply_path_prefix(worktree, path)) == -1)
538 return got_error_from_errno();
540 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
541 GOT_DEFAULT_FILE_MODE);
542 if (fd == -1) {
543 err = got_error_from_errno();
544 if (errno == EEXIST) {
545 struct stat sb;
546 if (lstat(ondisk_path, &sb) == -1) {
547 err = got_error_from_errno();
548 goto done;
549 } else if (!S_ISREG(sb.st_mode)) {
550 /* TODO file is obstructed; do something */
551 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
552 goto done;
553 } else {
554 err = got_opentemp_named_fd(&tmppath, &fd,
555 ondisk_path);
556 if (err)
557 goto done;
558 update = 1;
560 } else
561 return err;
564 (*progress_cb)(progress_arg,
565 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, progress_path);
567 hdrlen = got_object_blob_get_hdrlen(blob);
568 do {
569 const uint8_t *buf = got_object_blob_get_read_buf(blob);
570 err = got_object_blob_read_block(&len, blob);
571 if (err)
572 break;
573 if (len > 0) {
574 /* Skip blob object header first time around. */
575 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
576 if (outlen == -1) {
577 err = got_error_from_errno();
578 goto done;
579 } else if (outlen != len - hdrlen) {
580 err = got_error(GOT_ERR_IO);
581 goto done;
583 hdrlen = 0;
585 } while (len != 0);
587 fsync(fd);
589 if (update) {
590 if (rename(tmppath, ondisk_path) != 0) {
591 err = got_error_from_errno();
592 goto done;
596 if (entry)
597 err = got_fileindex_entry_update(entry, ondisk_path,
598 blob->id.sha1, worktree->base_commit_id->sha1);
599 else {
600 err = got_fileindex_entry_alloc(&entry, ondisk_path,
601 apply_path_prefix(worktree, path), blob->id.sha1,
602 worktree->base_commit_id->sha1);
603 if (err)
604 goto done;
605 err = got_fileindex_entry_add(fileindex, entry);
607 if (err)
608 goto done;
609 done:
610 if (fd != -1)
611 close(fd);
612 free(ondisk_path);
613 free(tmppath);
614 return err;
617 static const struct got_error *
618 add_dir_on_disk(struct got_worktree *worktree, const char *path)
620 const struct got_error *err = NULL;
621 char *abspath;
623 if (asprintf(&abspath, "%s/%s", worktree->root_path,
624 apply_path_prefix(worktree, path)) == -1)
625 return got_error_from_errno();
627 /* XXX queue work rather than editing disk directly? */
628 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
629 struct stat sb;
631 if (errno != EEXIST) {
632 err = got_error_from_errno();
633 goto done;
636 if (lstat(abspath, &sb) == -1) {
637 err = got_error_from_errno();
638 goto done;
641 if (!S_ISDIR(sb.st_mode)) {
642 /* TODO directory is obstructed; do something */
643 return got_error(GOT_ERR_FILE_OBSTRUCTED);
647 done:
648 free(abspath);
649 return err;
652 static const struct got_error *
653 tree_checkout(struct got_worktree *, struct got_fileindex *,
654 struct got_tree_object *, const char *, struct got_repository *,
655 got_worktree_checkout_cb progress_cb, void *progress_arg,
656 got_worktree_cancel_cb cancel_cb, void *cancel_arg);
658 static const struct got_error *
659 tree_checkout_entry(struct got_worktree *worktree,
660 struct got_fileindex *fileindex, struct got_tree_entry *te,
661 const char *parent, struct got_repository *repo,
662 got_worktree_checkout_cb progress_cb, void *progress_arg,
663 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
665 const struct got_error *err = NULL;
666 struct got_object *obj = NULL;
667 struct got_blob_object *blob = NULL;
668 struct got_fileindex_entry *entry = NULL;
669 struct got_tree_object *tree = NULL;
670 char *path = NULL;
671 char *progress_path = NULL;
672 size_t len;
674 if (parent[0] == '/' && parent[1] == '\0')
675 parent = "";
676 if (asprintf(&path, "%s/%s", parent, te->name) == -1)
677 return got_error_from_errno();
679 /* Skip this entry if it is outside of our path prefix. */
680 len = MIN(strlen(worktree->path_prefix), strlen(path));
681 if (strncmp(path, worktree->path_prefix, len) != 0) {
682 free(path);
683 return NULL;
686 err = got_object_open(&obj, repo, te->id);
687 if (err)
688 goto done;
690 progress_path = path;
691 if (strncmp(progress_path, worktree->path_prefix, len) == 0)
692 progress_path += len;
694 switch (obj->type) {
695 case GOT_OBJ_TYPE_BLOB:
696 if (strlen(worktree->path_prefix) >= strlen(path))
697 break;
698 entry = got_fileindex_entry_get(fileindex,
699 apply_path_prefix(worktree, path));
700 if (entry &&
701 memcmp(entry->commit_sha1, worktree->base_commit_id->sha1,
702 SHA1_DIGEST_LENGTH) == 0) {
703 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
704 progress_path);
705 break;
707 if (entry && memcmp(entry->blob_sha1, obj->id.sha1,
708 SHA1_DIGEST_LENGTH) == 0)
709 break;
710 err = got_object_blob_open(&blob, repo, obj, 8192);
711 if (err)
712 goto done;
713 err = blob_checkout(worktree, fileindex, entry, path, blob,
714 repo, progress_cb, progress_arg, progress_path);
715 break;
716 case GOT_OBJ_TYPE_TREE:
717 if (strlen(worktree->path_prefix) < strlen(path)) {
718 err = add_dir_on_disk(worktree, path);
719 if (err)
720 break;
722 err = got_object_tree_open(&tree, repo, obj);
723 if (err)
724 goto done;
725 /* XXX infinite recursion possible */
726 err = tree_checkout(worktree, fileindex, tree, path, repo,
727 progress_cb, progress_arg, cancel_cb, cancel_arg);
728 break;
729 default:
730 break;
733 done:
734 if (blob)
735 got_object_blob_close(blob);
736 if (tree)
737 got_object_tree_close(tree);
738 if (obj)
739 got_object_close(obj);
740 free(path);
741 return err;
744 static const struct got_error *
745 tree_checkout(struct got_worktree *worktree,
746 struct got_fileindex *fileindex, struct got_tree_object *tree,
747 const char *path, struct got_repository *repo,
748 got_worktree_checkout_cb progress_cb, void *progress_arg,
749 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
751 const struct got_error *err = NULL;
752 const struct got_tree_entries *entries;
753 struct got_tree_entry *te;
754 size_t len;
756 /* Skip this tree if it is outside of our path prefix. */
757 len = MIN(strlen(worktree->path_prefix), strlen(path));
758 if (strncmp(path, worktree->path_prefix, len) != 0)
759 return NULL;
761 entries = got_object_tree_get_entries(tree);
762 SIMPLEQ_FOREACH(te, &entries->head, entry) {
763 if (cancel_cb) {
764 err = (*cancel_cb)(cancel_arg);
765 if (err)
766 break;
768 err = tree_checkout_entry(worktree, fileindex, te, path, repo,
769 progress_cb, progress_arg, cancel_cb, cancel_arg);
770 if (err)
771 break;
774 return err;
777 const struct got_error *
778 got_worktree_checkout_files(struct got_worktree *worktree,
779 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
780 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
782 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
783 struct got_commit_object *commit = NULL;
784 struct got_tree_object *tree = NULL;
785 char *fileindex_path = NULL, *new_fileindex_path = NULL;
786 struct got_fileindex *fileindex = NULL;
787 FILE *index = NULL, *new_index = NULL;
789 err = lock_worktree(worktree, LOCK_EX);
790 if (err)
791 return err;
793 fileindex = got_fileindex_alloc();
794 if (fileindex == NULL) {
795 err = got_error_from_errno();
796 goto done;
799 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
800 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
801 err = got_error_from_errno();
802 fileindex_path = NULL;
803 goto done;
806 /*
807 * Read the file index.
808 * Checking out files is supposed to be an idempotent operation.
809 * If the on-disk file index is incomplete we will try to complete it.
810 */
811 index = fopen(fileindex_path, "rb");
812 if (index == NULL) {
813 err = got_error_from_errno();
814 goto done;
816 err = got_fileindex_read(fileindex, index);
817 fclose(index);
818 if (err)
819 goto done;
821 err = got_opentemp_named(&new_fileindex_path, &new_index,
822 fileindex_path);
823 if (err)
824 goto done;
826 err = got_object_open_as_commit(&commit, repo,
827 worktree->base_commit_id);
828 if (err)
829 goto done;
831 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
832 if (err)
833 goto done;
835 checkout_err = tree_checkout(worktree, fileindex, tree, "/", repo,
836 progress_cb, progress_arg, cancel_cb, cancel_arg);
838 /* Try to sync the fileindex back to disk in any case. */
839 err = got_fileindex_write(fileindex, new_index);
840 if (err)
841 goto done;
843 if (rename(new_fileindex_path, fileindex_path) != 0) {
844 err = got_error_from_errno();
845 goto done;
848 free(new_fileindex_path);
849 new_fileindex_path = NULL;
851 done:
852 if (tree)
853 got_object_tree_close(tree);
854 if (commit)
855 got_object_commit_close(commit);
856 if (new_fileindex_path)
857 unlink(new_fileindex_path);
858 if (new_index)
859 fclose(new_index);
860 free(new_fileindex_path);
861 free(fileindex_path);
862 got_fileindex_free(fileindex);
863 if (checkout_err)
864 err = checkout_err;
865 unlockerr = lock_worktree(worktree, LOCK_SH);
866 if (unlockerr && err == NULL)
867 err = unlockerr;
868 return err;