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>
30 #include <libgen.h>
32 #include "got_error.h"
33 #include "got_repository.h"
34 #include "got_reference.h"
35 #include "got_object.h"
36 #include "got_worktree.h"
37 #include "got_opentemp.h"
39 #include "got_lib_worktree.h"
40 #include "got_lib_path.h"
41 #include "got_lib_sha1.h"
42 #include "got_lib_fileindex.h"
43 #include "got_lib_inflate.h"
44 #include "got_lib_delta.h"
45 #include "got_lib_object.h"
47 #ifndef MIN
48 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
49 #endif
51 static const struct got_error *
52 create_meta_file(const char *path_got, const char *name, const char *content)
53 {
54 const struct got_error *err = NULL;
55 char *path;
56 int fd = -1;
58 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
59 err = got_error_from_errno();
60 path = NULL;
61 goto done;
62 }
64 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
65 GOT_DEFAULT_FILE_MODE);
66 if (fd == -1) {
67 err = got_error_from_errno();
68 goto done;
69 }
71 if (content) {
72 int len = dprintf(fd, "%s\n", content);
73 if (len != strlen(content) + 1) {
74 err = got_error_from_errno();
75 goto done;
76 }
77 }
79 done:
80 if (fd != -1 && close(fd) == -1 && err == NULL)
81 err = got_error_from_errno();
82 free(path);
83 return err;
84 }
86 static const struct got_error *
87 update_meta_file(const char *path_got, const char *name, const char *content)
88 {
89 const struct got_error *err = NULL;
90 FILE *tmpfile = NULL;
91 char *tmppath = NULL;
92 char *path = NULL;
94 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
95 err = got_error_from_errno();
96 path = NULL;
97 goto done;
98 }
100 err = got_opentemp_named(&tmppath, &tmpfile, path);
101 if (err)
102 goto done;
104 if (content) {
105 int len = fprintf(tmpfile, "%s\n", content);
106 if (len != strlen(content) + 1) {
107 err = got_error_from_errno();
108 goto done;
112 if (rename(tmppath, path) != 0) {
113 err = got_error_from_errno();
114 goto done;
117 done:
118 free(tmppath);
119 fclose(tmpfile);
120 return err;
123 static const struct got_error *
124 read_meta_file(char **content, const char *path_got, const char *name)
126 const struct got_error *err = NULL;
127 char *path;
128 int fd = -1;
129 ssize_t n;
130 struct stat sb;
132 *content = NULL;
134 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
135 err = got_error_from_errno();
136 path = NULL;
137 goto done;
140 fd = open(path, O_RDONLY | O_NOFOLLOW);
141 if (fd == -1) {
142 err = got_error_from_errno();
143 goto done;
145 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
146 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
147 : got_error_from_errno());
148 goto done;
151 stat(path, &sb);
152 *content = calloc(1, sb.st_size);
153 if (*content == NULL) {
154 err = got_error_from_errno();
155 goto done;
158 n = read(fd, *content, sb.st_size);
159 if (n != sb.st_size) {
160 err = (n == -1 ? got_error_from_errno() :
161 got_error(GOT_ERR_WORKTREE_META));
162 goto done;
164 if ((*content)[sb.st_size - 1] != '\n') {
165 err = got_error(GOT_ERR_WORKTREE_META);
166 goto done;
168 (*content)[sb.st_size - 1] = '\0';
170 done:
171 if (fd != -1 && close(fd) == -1 && err == NULL)
172 err = got_error_from_errno();
173 free(path);
174 if (err) {
175 free(*content);
176 *content = NULL;
178 return err;
181 const struct got_error *
182 got_worktree_init(const char *path, struct got_reference *head_ref,
183 const char *prefix, struct got_repository *repo)
185 const struct got_error *err = NULL;
186 struct got_object_id *commit_id = NULL;
187 int obj_type;
188 char *path_got = NULL;
189 char *refstr = 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 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
254 got_repo_get_path(repo));
255 if (err)
256 goto done;
258 /* Store in-repository path prefix. */
259 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
260 absprefix ? absprefix : prefix);
261 if (err)
262 goto done;
264 /* Stamp work tree with format file. */
265 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
266 err = got_error_from_errno();
267 goto done;
269 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
270 if (err)
271 goto done;
273 done:
274 free(commit_id);
275 free(path_got);
276 free(formatstr);
277 free(refstr);
278 free(absprefix);
279 free(basestr);
280 return err;
283 const struct got_error *
284 got_worktree_open(struct got_worktree **worktree, const char *path)
286 const struct got_error *err = NULL;
287 char *path_got;
288 char *formatstr = NULL;
289 char *path_lock = NULL;
290 char *base_commit_id_str = NULL;
291 char *head_ref_str = NULL;
292 int version, fd = -1;
293 const char *errstr;
294 struct got_repository *repo = NULL;
296 *worktree = NULL;
298 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
299 err = got_error_from_errno();
300 path_got = NULL;
301 goto done;
304 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
305 err = got_error_from_errno();
306 path_lock = NULL;
307 goto done;
310 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
311 if (fd == -1) {
312 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
313 : got_error_from_errno());
314 goto done;
317 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
318 if (err)
319 goto done;
321 version = strtonum(formatstr, 1, INT_MAX, &errstr);
322 if (errstr) {
323 err = got_error(GOT_ERR_WORKTREE_META);
324 goto done;
326 if (version != GOT_WORKTREE_FORMAT_VERSION) {
327 err = got_error(GOT_ERR_WORKTREE_VERS);
328 goto done;
331 *worktree = calloc(1, sizeof(**worktree));
332 if (*worktree == NULL) {
333 err = got_error_from_errno();
334 goto done;
336 (*worktree)->lockfd = -1;
338 (*worktree)->root_path = strdup(path);
339 if ((*worktree)->root_path == NULL) {
340 err = got_error_from_errno();
341 goto done;
343 err = read_meta_file(&(*worktree)->repo_path, path_got,
344 GOT_WORKTREE_REPOSITORY);
345 if (err)
346 goto done;
348 err = read_meta_file(&(*worktree)->path_prefix, path_got,
349 GOT_WORKTREE_PATH_PREFIX);
350 if (err)
351 goto done;
353 err = read_meta_file(&base_commit_id_str, path_got,
354 GOT_WORKTREE_BASE_COMMIT);
355 if (err)
356 goto done;
358 err = got_repo_open(&repo, (*worktree)->repo_path);
359 if (err)
360 goto done;
362 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
363 base_commit_id_str);
364 if (err)
365 goto done;
367 err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
368 if (err)
369 goto done;
371 err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
372 done:
373 if (repo)
374 got_repo_close(repo);
375 free(path_got);
376 free(path_lock);
377 free(head_ref_str);
378 free(base_commit_id_str);
379 if (err) {
380 if (fd != -1)
381 close(fd);
382 if (*worktree != NULL)
383 got_worktree_close(*worktree);
384 *worktree = NULL;
385 } else
386 (*worktree)->lockfd = fd;
388 return err;
391 void
392 got_worktree_close(struct got_worktree *worktree)
394 free(worktree->root_path);
395 free(worktree->repo_path);
396 free(worktree->path_prefix);
397 free(worktree->base_commit_id);
398 if (worktree->head_ref)
399 got_ref_close(worktree->head_ref);
400 if (worktree->lockfd != -1)
401 close(worktree->lockfd);
402 free(worktree);
405 const char *
406 got_worktree_get_repo_path(struct got_worktree *worktree)
408 return worktree->repo_path;
411 const char *
412 got_worktree_get_path_prefix(struct got_worktree *worktree)
414 return worktree->path_prefix;
417 const struct got_error *
418 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
419 const char *path_prefix)
421 char *absprefix = NULL;
423 if (!got_path_is_absolute(path_prefix)) {
424 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
425 return got_error_from_errno();
427 *match = (strcmp(absprefix ? absprefix : path_prefix,
428 worktree->path_prefix) == 0);
429 free(absprefix);
430 return NULL;
433 char *
434 got_worktree_get_head_ref_name(struct got_worktree *worktree)
436 return got_ref_to_str(worktree->head_ref);
439 struct got_reference *
440 got_worktree_get_head_ref(struct got_worktree *worktree)
442 return got_ref_dup(worktree->head_ref);
445 const struct got_object_id *
446 got_worktree_get_base_commit_id(struct got_worktree *worktree)
448 return worktree->base_commit_id;
451 const struct got_error *
452 got_worktree_set_base_commit_id(struct got_worktree *worktree,
453 struct got_repository *repo, struct got_object_id *commit_id)
455 const struct got_error *err;
456 struct got_object *obj = NULL;
457 char *id_str = NULL;
458 char *path_got = NULL;
460 if (asprintf(&path_got, "%s/%s", worktree->root_path,
461 GOT_WORKTREE_GOT_DIR) == -1) {
462 err = got_error_from_errno();
463 path_got = NULL;
464 goto done;
467 err = got_object_open(&obj, repo, commit_id);
468 if (err)
469 return err;
471 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
472 err = got_error(GOT_ERR_OBJ_TYPE);
473 goto done;
476 /* Record our base commit. */
477 err = got_object_id_str(&id_str, commit_id);
478 if (err)
479 goto done;
480 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
481 if (err)
482 goto done;
484 free(worktree->base_commit_id);
485 worktree->base_commit_id = got_object_id_dup(commit_id);
486 if (worktree->base_commit_id == NULL) {
487 err = got_error_from_errno();
488 goto done;
490 done:
491 if (obj)
492 got_object_close(obj);
493 free(id_str);
494 free(path_got);
495 return err;
498 static const struct got_error *
499 lock_worktree(struct got_worktree *worktree, int operation)
501 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
502 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
503 : got_error_from_errno());
504 return NULL;
507 static const char *
508 apply_path_prefix(struct got_worktree *worktree, const char *path)
510 const char *p = path;
511 p += strlen(worktree->path_prefix);
512 if (*p == '/')
513 p++;
514 return p;
517 static const struct got_error *
518 blob_checkout(struct got_worktree *worktree, struct got_fileindex *fileindex,
519 struct got_fileindex_entry *entry, const char *path,
520 struct got_blob_object *blob, struct got_repository *repo,
521 got_worktree_checkout_cb progress_cb, void *progress_arg,
522 const char *progress_path)
524 const struct got_error *err = NULL;
525 char *ondisk_path;
526 int fd = -1;
527 size_t len, hdrlen;
528 int update = 0;
529 char *tmppath = NULL;
531 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
532 apply_path_prefix(worktree, path)) == -1)
533 return got_error_from_errno();
535 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
536 GOT_DEFAULT_FILE_MODE);
537 if (fd == -1) {
538 err = got_error_from_errno();
539 if (errno == EEXIST) {
540 struct stat sb;
541 if (lstat(ondisk_path, &sb) == -1) {
542 err = got_error_from_errno();
543 goto done;
544 } else if (!S_ISREG(sb.st_mode)) {
545 /* TODO file is obstructed; do something */
546 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
547 goto done;
548 } else {
549 err = got_opentemp_named_fd(&tmppath, &fd,
550 ondisk_path);
551 if (err)
552 goto done;
553 update = 1;
555 } else
556 return err;
559 (*progress_cb)(progress_arg,
560 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, progress_path);
562 hdrlen = got_object_blob_get_hdrlen(blob);
563 do {
564 const uint8_t *buf = got_object_blob_get_read_buf(blob);
565 err = got_object_blob_read_block(&len, blob);
566 if (err)
567 break;
568 if (len > 0) {
569 /* Skip blob object header first time around. */
570 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
571 if (outlen == -1) {
572 err = got_error_from_errno();
573 goto done;
574 } else if (outlen != len - hdrlen) {
575 err = got_error(GOT_ERR_IO);
576 goto done;
578 hdrlen = 0;
580 } while (len != 0);
582 fsync(fd);
584 if (update) {
585 if (rename(tmppath, ondisk_path) != 0) {
586 err = got_error_from_errno();
587 goto done;
591 if (entry)
592 err = got_fileindex_entry_update(entry, ondisk_path,
593 blob->id.sha1, worktree->base_commit_id->sha1);
594 else {
595 err = got_fileindex_entry_alloc(&entry, ondisk_path,
596 apply_path_prefix(worktree, path), blob->id.sha1,
597 worktree->base_commit_id->sha1);
598 if (err)
599 goto done;
600 err = got_fileindex_entry_add(fileindex, entry);
602 if (err)
603 goto done;
604 done:
605 if (fd != -1)
606 close(fd);
607 free(ondisk_path);
608 free(tmppath);
609 return err;
612 static const struct got_error *
613 add_dir_on_disk(struct got_worktree *worktree, const char *path)
615 const struct got_error *err = NULL;
616 char *abspath;
618 if (asprintf(&abspath, "%s/%s", worktree->root_path,
619 apply_path_prefix(worktree, path)) == -1)
620 return got_error_from_errno();
622 /* XXX queue work rather than editing disk directly? */
623 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
624 struct stat sb;
626 if (errno != EEXIST) {
627 err = got_error_from_errno();
628 goto done;
631 if (lstat(abspath, &sb) == -1) {
632 err = got_error_from_errno();
633 goto done;
636 if (!S_ISDIR(sb.st_mode)) {
637 /* TODO directory is obstructed; do something */
638 return got_error(GOT_ERR_FILE_OBSTRUCTED);
642 done:
643 free(abspath);
644 return err;
647 static const struct got_error *
648 tree_checkout(struct got_worktree *, struct got_fileindex *,
649 struct got_tree_object *, const char *, struct got_repository *,
650 got_worktree_checkout_cb progress_cb, void *progress_arg,
651 got_worktree_cancel_cb cancel_cb, void *cancel_arg);
653 static const struct got_error *
654 tree_checkout_entry(struct got_worktree *worktree,
655 struct got_fileindex *fileindex, struct got_tree_entry *te,
656 const char *parent, struct got_repository *repo,
657 got_worktree_checkout_cb progress_cb, void *progress_arg,
658 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
660 const struct got_error *err = NULL;
661 struct got_object *obj = NULL;
662 struct got_blob_object *blob = NULL;
663 struct got_fileindex_entry *entry = NULL;
664 struct got_tree_object *tree = NULL;
665 char *path = NULL;
666 char *progress_path = NULL;
667 size_t len;
669 if (parent[0] == '/' && parent[1] == '\0')
670 parent = "";
671 if (asprintf(&path, "%s/%s", parent, te->name) == -1)
672 return got_error_from_errno();
674 /* Skip this entry if it is outside of our path prefix. */
675 len = MIN(strlen(worktree->path_prefix), strlen(path));
676 if (strncmp(path, worktree->path_prefix, len) != 0) {
677 free(path);
678 return NULL;
681 err = got_object_open(&obj, repo, te->id);
682 if (err)
683 goto done;
685 progress_path = path;
686 if (strncmp(progress_path, worktree->path_prefix, len) == 0)
687 progress_path += len;
689 switch (obj->type) {
690 case GOT_OBJ_TYPE_BLOB:
691 if (strlen(worktree->path_prefix) >= strlen(path))
692 break;
693 entry = got_fileindex_entry_get(fileindex,
694 apply_path_prefix(worktree, path));
695 if (entry &&
696 memcmp(entry->commit_sha1, worktree->base_commit_id->sha1,
697 SHA1_DIGEST_LENGTH) == 0) {
698 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
699 progress_path);
700 break;
702 if (entry && memcmp(entry->blob_sha1, obj->id.sha1,
703 SHA1_DIGEST_LENGTH) == 0)
704 break;
705 err = got_object_blob_open(&blob, repo, obj, 8192);
706 if (err)
707 goto done;
708 err = blob_checkout(worktree, fileindex, entry, path, blob,
709 repo, progress_cb, progress_arg, progress_path);
710 break;
711 case GOT_OBJ_TYPE_TREE:
712 if (strlen(worktree->path_prefix) < strlen(path)) {
713 err = add_dir_on_disk(worktree, path);
714 if (err)
715 break;
717 err = got_object_tree_open(&tree, repo, obj);
718 if (err)
719 goto done;
720 /* XXX infinite recursion possible */
721 err = tree_checkout(worktree, fileindex, tree, path, repo,
722 progress_cb, progress_arg, cancel_cb, cancel_arg);
723 break;
724 default:
725 break;
728 done:
729 if (blob)
730 got_object_blob_close(blob);
731 if (tree)
732 got_object_tree_close(tree);
733 if (obj)
734 got_object_close(obj);
735 free(path);
736 return err;
739 struct collect_missing_entry_args {
740 struct got_fileindex *fileindex;
741 const struct got_tree_entries *entries;
742 struct got_fileindex missing_entries;
743 const char *path_prefix;
744 };
746 static const struct got_error *
747 collect_missing_file(void *args, struct got_fileindex_entry *entry)
749 struct collect_missing_entry_args *a = args;
750 char *name;
751 struct got_tree_entry *te;
752 int found = 0;
754 if (a->path_prefix[0] == '\0' && strchr(entry->path, '/') != NULL)
755 return NULL;
756 if (a->path_prefix[0] != '\0' &&
757 strncmp(a->path_prefix, entry->path, strlen(a->path_prefix)) != 0)
758 return NULL;
760 name = basename(entry->path);
761 if (name == NULL)
762 return got_error_from_errno();
764 SIMPLEQ_FOREACH(te, &a->entries->head, entry) {
765 if (strcmp(te->name, name) == 0) {
766 found = 1;
767 break;
771 if (found)
772 return NULL;
774 got_fileindex_entry_remove(a->fileindex, entry);
775 return got_fileindex_entry_add(&a->missing_entries, entry);
778 /* Remove files which exist in the file index but not in the tree. */
779 static const struct got_error *
780 remove_missing_files(struct got_worktree *worktree, const char *path,
781 struct got_fileindex *fileindex, const struct got_tree_entries *entries,
782 got_worktree_checkout_cb progress_cb, void *progress_arg,
783 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
785 const struct got_error *err = NULL;
786 struct collect_missing_entry_args a;
787 struct got_fileindex_entry *entry, *tmp;
789 a.fileindex = fileindex;
790 a.entries = entries;
791 a.missing_entries.nentries = 0;
792 a.path_prefix = path;
793 while (a.path_prefix[0] == '/')
794 a.path_prefix++;
795 TAILQ_INIT(&a.missing_entries.entries);
796 err = got_fileindex_for_each_entry(fileindex, collect_missing_file, &a);
797 if (err)
798 return err;
800 TAILQ_FOREACH_SAFE(entry, &a.missing_entries.entries, entry, tmp) {
801 char *ondisk_path = NULL;
803 if (cancel_cb) {
804 err = (*cancel_cb)(cancel_arg);
805 if (err)
806 break;
809 (*progress_cb)(progress_arg, GOT_STATUS_DELETE, entry->path);
811 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
812 entry->path) == -1) {
813 err = got_error_from_errno();
814 break;
817 if (unlink(ondisk_path) == -1)
818 err = got_error_from_errno();
819 free(ondisk_path);
820 if (err)
821 break;
823 TAILQ_REMOVE(&a.missing_entries.entries, entry, entry);
824 got_fileindex_entry_free(entry);
827 if (err) {
828 while (!TAILQ_EMPTY(&a.missing_entries.entries)) {
829 entry = TAILQ_FIRST(&a.missing_entries.entries);
830 TAILQ_REMOVE(&a.missing_entries.entries, entry, entry);
831 got_fileindex_entry_free(entry);
835 return err;
838 static const struct got_error *
839 tree_checkout(struct got_worktree *worktree,
840 struct got_fileindex *fileindex, struct got_tree_object *tree,
841 const char *path, struct got_repository *repo,
842 got_worktree_checkout_cb progress_cb, void *progress_arg,
843 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
845 const struct got_error *err = NULL;
846 const struct got_tree_entries *entries;
847 struct got_tree_entry *te;
848 size_t len;
850 /* Skip this tree if it is outside of our path prefix. */
851 len = MIN(strlen(worktree->path_prefix), strlen(path));
852 if (strncmp(path, worktree->path_prefix, len) != 0)
853 return NULL;
855 entries = got_object_tree_get_entries(tree);
856 SIMPLEQ_FOREACH(te, &entries->head, entry) {
857 if (cancel_cb) {
858 err = (*cancel_cb)(cancel_arg);
859 if (err)
860 return err;
862 err = tree_checkout_entry(worktree, fileindex, te, path, repo,
863 progress_cb, progress_arg, cancel_cb, cancel_arg);
864 if (err)
865 return err;
868 len = strlen(worktree->path_prefix);
869 if (strncmp(worktree->path_prefix, path, len) == 0) {
870 err = remove_missing_files(worktree, path, fileindex, entries,
871 progress_cb, progress_arg, cancel_cb, cancel_arg);
872 if (err)
873 return err;
876 return err;
879 const struct got_error *
880 got_worktree_checkout_files(struct got_worktree *worktree,
881 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
882 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
884 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
885 struct got_commit_object *commit = NULL;
886 struct got_tree_object *tree = NULL;
887 char *fileindex_path = NULL, *new_fileindex_path = NULL;
888 struct got_fileindex *fileindex = NULL;
889 FILE *index = NULL, *new_index = NULL;
891 err = lock_worktree(worktree, LOCK_EX);
892 if (err)
893 return err;
895 fileindex = got_fileindex_alloc();
896 if (fileindex == NULL) {
897 err = got_error_from_errno();
898 goto done;
901 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
902 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
903 err = got_error_from_errno();
904 fileindex_path = NULL;
905 goto done;
908 /*
909 * Read the file index.
910 * Checking out files is supposed to be an idempotent operation.
911 * If the on-disk file index is incomplete we will try to complete it.
912 */
913 index = fopen(fileindex_path, "rb");
914 if (index == NULL) {
915 err = got_error_from_errno();
916 goto done;
918 err = got_fileindex_read(fileindex, index);
919 fclose(index);
920 if (err)
921 goto done;
923 err = got_opentemp_named(&new_fileindex_path, &new_index,
924 fileindex_path);
925 if (err)
926 goto done;
928 err = got_object_open_as_commit(&commit, repo,
929 worktree->base_commit_id);
930 if (err)
931 goto done;
933 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
934 if (err)
935 goto done;
937 checkout_err = tree_checkout(worktree, fileindex, tree, "/", repo,
938 progress_cb, progress_arg, cancel_cb, cancel_arg);
940 /* Try to sync the fileindex back to disk in any case. */
941 err = got_fileindex_write(fileindex, new_index);
942 if (err)
943 goto done;
945 if (rename(new_fileindex_path, fileindex_path) != 0) {
946 err = got_error_from_errno();
947 goto done;
950 free(new_fileindex_path);
951 new_fileindex_path = NULL;
953 done:
954 if (tree)
955 got_object_tree_close(tree);
956 if (commit)
957 got_object_commit_close(commit);
958 if (new_fileindex_path)
959 unlink(new_fileindex_path);
960 if (new_index)
961 fclose(new_index);
962 free(new_fileindex_path);
963 free(fileindex_path);
964 got_fileindex_free(fileindex);
965 if (checkout_err)
966 err = checkout_err;
967 unlockerr = lock_worktree(worktree, LOCK_SH);
968 if (unlockerr && err == NULL)
969 err = unlockerr;
970 return err;