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 <stddef.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <fcntl.h>
26 #include <errno.h>
27 #include <unistd.h>
28 #include <sha1.h>
29 #include <zlib.h>
30 #include <fnmatch.h>
31 #include <libgen.h>
33 #include "got_error.h"
34 #include "got_repository.h"
35 #include "got_reference.h"
36 #include "got_object.h"
37 #include "got_worktree.h"
38 #include "got_opentemp.h"
40 #include "got_lib_worktree.h"
41 #include "got_lib_path.h"
42 #include "got_lib_sha1.h"
43 #include "got_lib_fileindex.h"
44 #include "got_lib_inflate.h"
45 #include "got_lib_delta.h"
46 #include "got_lib_object.h"
48 #ifndef MIN
49 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
50 #endif
52 static const struct got_error *
53 create_meta_file(const char *path_got, const char *name, const char *content)
54 {
55 const struct got_error *err = NULL;
56 char *path;
57 int fd = -1;
59 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
60 err = got_error_from_errno();
61 path = NULL;
62 goto done;
63 }
65 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
66 GOT_DEFAULT_FILE_MODE);
67 if (fd == -1) {
68 err = got_error_from_errno();
69 goto done;
70 }
72 if (content) {
73 int len = dprintf(fd, "%s\n", content);
74 if (len != strlen(content) + 1) {
75 err = got_error_from_errno();
76 goto done;
77 }
78 }
80 done:
81 if (fd != -1 && close(fd) == -1 && err == NULL)
82 err = got_error_from_errno();
83 free(path);
84 return err;
85 }
87 static const struct got_error *
88 update_meta_file(const char *path_got, const char *name, const char *content)
89 {
90 const struct got_error *err = NULL;
91 FILE *tmpfile = NULL;
92 char *tmppath = NULL;
93 char *path = NULL;
95 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
96 err = got_error_from_errno();
97 path = NULL;
98 goto done;
99 }
101 err = got_opentemp_named(&tmppath, &tmpfile, path);
102 if (err)
103 goto done;
105 if (content) {
106 int len = fprintf(tmpfile, "%s\n", content);
107 if (len != strlen(content) + 1) {
108 err = got_error_from_errno();
109 goto done;
113 if (rename(tmppath, path) != 0) {
114 err = got_error_from_errno();
115 goto done;
118 done:
119 free(tmppath);
120 fclose(tmpfile);
121 return err;
124 static const struct got_error *
125 read_meta_file(char **content, const char *path_got, const char *name)
127 const struct got_error *err = NULL;
128 char *path;
129 int fd = -1;
130 ssize_t n;
131 struct stat sb;
133 *content = NULL;
135 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
136 err = got_error_from_errno();
137 path = NULL;
138 goto done;
141 fd = open(path, O_RDONLY | O_NOFOLLOW);
142 if (fd == -1) {
143 err = got_error_from_errno();
144 goto done;
146 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
147 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
148 : got_error_from_errno());
149 goto done;
152 stat(path, &sb);
153 *content = calloc(1, sb.st_size);
154 if (*content == NULL) {
155 err = got_error_from_errno();
156 goto done;
159 n = read(fd, *content, sb.st_size);
160 if (n != sb.st_size) {
161 err = (n == -1 ? got_error_from_errno() :
162 got_error(GOT_ERR_WORKTREE_META));
163 goto done;
165 if ((*content)[sb.st_size - 1] != '\n') {
166 err = got_error(GOT_ERR_WORKTREE_META);
167 goto done;
169 (*content)[sb.st_size - 1] = '\0';
171 done:
172 if (fd != -1 && close(fd) == -1 && err == NULL)
173 err = got_error_from_errno();
174 free(path);
175 if (err) {
176 free(*content);
177 *content = NULL;
179 return err;
182 const struct got_error *
183 got_worktree_init(const char *path, struct got_reference *head_ref,
184 const char *prefix, struct got_repository *repo)
186 const struct got_error *err = NULL;
187 struct got_object_id *commit_id = NULL;
188 int obj_type;
189 char *path_got = NULL;
190 char *refstr = NULL;
191 char *formatstr = NULL;
192 char *absprefix = NULL;
193 char *basestr = NULL;
195 err = got_ref_resolve(&commit_id, repo, head_ref);
196 if (err)
197 return err;
198 err = got_object_get_type(&obj_type, repo, commit_id);
199 if (err)
200 return err;
201 if (obj_type != GOT_OBJ_TYPE_COMMIT)
202 return got_error(GOT_ERR_OBJ_TYPE);
204 if (!got_path_is_absolute(prefix)) {
205 if (asprintf(&absprefix, "/%s", prefix) == -1)
206 return got_error_from_errno();
209 /* Create top-level directory (may already exist). */
210 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
211 err = got_error_from_errno();
212 goto done;
215 /* Create .got directory (may already exist). */
216 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
217 err = got_error_from_errno();
218 goto done;
220 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
221 err = got_error_from_errno();
222 goto done;
225 /* Create an empty lock file. */
226 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
227 if (err)
228 goto done;
230 /* Create an empty file index. */
231 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
232 if (err)
233 goto done;
235 /* Write the HEAD reference. */
236 refstr = got_ref_to_str(head_ref);
237 if (refstr == NULL) {
238 err = got_error_from_errno();
239 goto done;
241 err = create_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
242 if (err)
243 goto done;
245 /* Record our base commit. */
246 err = got_object_id_str(&basestr, commit_id);
247 if (err)
248 goto done;
249 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
250 if (err)
251 goto done;
253 /* Store path to repository. */
254 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
255 got_repo_get_path(repo));
256 if (err)
257 goto done;
259 /* Store in-repository path prefix. */
260 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
261 absprefix ? absprefix : prefix);
262 if (err)
263 goto done;
265 /* Stamp work tree with format file. */
266 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
267 err = got_error_from_errno();
268 goto done;
270 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
271 if (err)
272 goto done;
274 done:
275 free(commit_id);
276 free(path_got);
277 free(formatstr);
278 free(refstr);
279 free(absprefix);
280 free(basestr);
281 return err;
284 const struct got_error *
285 got_worktree_open(struct got_worktree **worktree, const char *path)
287 const struct got_error *err = NULL;
288 char *path_got;
289 char *formatstr = NULL;
290 char *path_lock = NULL;
291 char *base_commit_id_str = NULL;
292 char *head_ref_str = NULL;
293 int version, fd = -1;
294 const char *errstr;
295 struct got_repository *repo = NULL;
297 *worktree = NULL;
299 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
300 err = got_error_from_errno();
301 path_got = NULL;
302 goto done;
305 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
306 err = got_error_from_errno();
307 path_lock = NULL;
308 goto done;
311 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
312 if (fd == -1) {
313 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
314 : got_error_from_errno());
315 goto done;
318 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
319 if (err)
320 goto done;
322 version = strtonum(formatstr, 1, INT_MAX, &errstr);
323 if (errstr) {
324 err = got_error(GOT_ERR_WORKTREE_META);
325 goto done;
327 if (version != GOT_WORKTREE_FORMAT_VERSION) {
328 err = got_error(GOT_ERR_WORKTREE_VERS);
329 goto done;
332 *worktree = calloc(1, sizeof(**worktree));
333 if (*worktree == NULL) {
334 err = got_error_from_errno();
335 goto done;
337 (*worktree)->lockfd = -1;
339 (*worktree)->root_path = strdup(path);
340 if ((*worktree)->root_path == NULL) {
341 err = got_error_from_errno();
342 goto done;
344 err = read_meta_file(&(*worktree)->repo_path, path_got,
345 GOT_WORKTREE_REPOSITORY);
346 if (err)
347 goto done;
349 err = read_meta_file(&(*worktree)->path_prefix, path_got,
350 GOT_WORKTREE_PATH_PREFIX);
351 if (err)
352 goto done;
354 err = read_meta_file(&base_commit_id_str, path_got,
355 GOT_WORKTREE_BASE_COMMIT);
356 if (err)
357 goto done;
359 err = got_repo_open(&repo, (*worktree)->repo_path);
360 if (err)
361 goto done;
363 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
364 base_commit_id_str);
365 if (err)
366 goto done;
368 err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
369 if (err)
370 goto done;
372 err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
373 done:
374 if (repo)
375 got_repo_close(repo);
376 free(path_got);
377 free(path_lock);
378 free(head_ref_str);
379 free(base_commit_id_str);
380 if (err) {
381 if (fd != -1)
382 close(fd);
383 if (*worktree != NULL)
384 got_worktree_close(*worktree);
385 *worktree = NULL;
386 } else
387 (*worktree)->lockfd = fd;
389 return err;
392 void
393 got_worktree_close(struct got_worktree *worktree)
395 free(worktree->root_path);
396 free(worktree->repo_path);
397 free(worktree->path_prefix);
398 free(worktree->base_commit_id);
399 if (worktree->head_ref)
400 got_ref_close(worktree->head_ref);
401 if (worktree->lockfd != -1)
402 close(worktree->lockfd);
403 free(worktree);
406 const char *
407 got_worktree_get_repo_path(struct got_worktree *worktree)
409 return worktree->repo_path;
412 const char *
413 got_worktree_get_path_prefix(struct got_worktree *worktree)
415 return worktree->path_prefix;
418 const struct got_error *
419 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
420 const char *path_prefix)
422 char *absprefix = NULL;
424 if (!got_path_is_absolute(path_prefix)) {
425 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
426 return got_error_from_errno();
428 *match = (strcmp(absprefix ? absprefix : path_prefix,
429 worktree->path_prefix) == 0);
430 free(absprefix);
431 return NULL;
434 char *
435 got_worktree_get_head_ref_name(struct got_worktree *worktree)
437 return got_ref_to_str(worktree->head_ref);
440 struct got_reference *
441 got_worktree_get_head_ref(struct got_worktree *worktree)
443 return got_ref_dup(worktree->head_ref);
446 const struct got_object_id *
447 got_worktree_get_base_commit_id(struct got_worktree *worktree)
449 return worktree->base_commit_id;
452 const struct got_error *
453 got_worktree_set_base_commit_id(struct got_worktree *worktree,
454 struct got_repository *repo, struct got_object_id *commit_id)
456 const struct got_error *err;
457 struct got_object *obj = NULL;
458 char *id_str = NULL;
459 char *path_got = NULL;
461 if (asprintf(&path_got, "%s/%s", worktree->root_path,
462 GOT_WORKTREE_GOT_DIR) == -1) {
463 err = got_error_from_errno();
464 path_got = NULL;
465 goto done;
468 err = got_object_open(&obj, repo, commit_id);
469 if (err)
470 return err;
472 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
473 err = got_error(GOT_ERR_OBJ_TYPE);
474 goto done;
477 /* Record our base commit. */
478 err = got_object_id_str(&id_str, commit_id);
479 if (err)
480 goto done;
481 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
482 if (err)
483 goto done;
485 free(worktree->base_commit_id);
486 worktree->base_commit_id = got_object_id_dup(commit_id);
487 if (worktree->base_commit_id == NULL) {
488 err = got_error_from_errno();
489 goto done;
491 done:
492 if (obj)
493 got_object_close(obj);
494 free(id_str);
495 free(path_got);
496 return err;
499 static const struct got_error *
500 lock_worktree(struct got_worktree *worktree, int operation)
502 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
503 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
504 : got_error_from_errno());
505 return NULL;
508 static const char *
509 apply_path_prefix(struct got_worktree *worktree, const char *path)
511 const char *p = path;
512 p += strlen(worktree->path_prefix);
513 if (*p == '/')
514 p++;
515 return p;
518 static const struct got_error *
519 blob_checkout(struct got_worktree *worktree, struct got_fileindex *fileindex,
520 struct got_fileindex_entry *entry, const char *path,
521 struct got_blob_object *blob, struct got_repository *repo,
522 got_worktree_checkout_cb progress_cb, void *progress_arg,
523 const char *progress_path)
525 const struct got_error *err = NULL;
526 char *ondisk_path;
527 int fd = -1;
528 size_t len, hdrlen;
529 int update = 0;
530 char *tmppath = NULL;
532 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
533 apply_path_prefix(worktree, path)) == -1)
534 return got_error_from_errno();
536 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
537 GOT_DEFAULT_FILE_MODE);
538 if (fd == -1) {
539 err = got_error_from_errno();
540 if (errno == EEXIST) {
541 struct stat sb;
542 if (lstat(ondisk_path, &sb) == -1) {
543 err = got_error_from_errno();
544 goto done;
545 } else if (!S_ISREG(sb.st_mode)) {
546 /* TODO file is obstructed; do something */
547 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
548 goto done;
549 } else {
550 err = got_opentemp_named_fd(&tmppath, &fd,
551 ondisk_path);
552 if (err)
553 goto done;
554 update = 1;
556 } else
557 return err;
560 (*progress_cb)(progress_arg,
561 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, progress_path);
563 hdrlen = got_object_blob_get_hdrlen(blob);
564 do {
565 const uint8_t *buf = got_object_blob_get_read_buf(blob);
566 err = got_object_blob_read_block(&len, blob);
567 if (err)
568 break;
569 if (len > 0) {
570 /* Skip blob object header first time around. */
571 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
572 if (outlen == -1) {
573 err = got_error_from_errno();
574 goto done;
575 } else if (outlen != len - hdrlen) {
576 err = got_error(GOT_ERR_IO);
577 goto done;
579 hdrlen = 0;
581 } while (len != 0);
583 fsync(fd);
585 if (update) {
586 if (rename(tmppath, ondisk_path) != 0) {
587 err = got_error_from_errno();
588 goto done;
592 if (entry)
593 err = got_fileindex_entry_update(entry, ondisk_path,
594 blob->id.sha1, worktree->base_commit_id->sha1);
595 else {
596 err = got_fileindex_entry_alloc(&entry, ondisk_path,
597 apply_path_prefix(worktree, path), blob->id.sha1,
598 worktree->base_commit_id->sha1);
599 if (err)
600 goto done;
601 err = got_fileindex_entry_add(fileindex, entry);
603 if (err)
604 goto done;
605 done:
606 if (fd != -1)
607 close(fd);
608 free(ondisk_path);
609 free(tmppath);
610 return err;
613 static const struct got_error *
614 add_dir_on_disk(struct got_worktree *worktree, const char *path)
616 const struct got_error *err = NULL;
617 char *abspath;
619 if (asprintf(&abspath, "%s/%s", worktree->root_path,
620 apply_path_prefix(worktree, path)) == -1)
621 return got_error_from_errno();
623 /* XXX queue work rather than editing disk directly? */
624 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
625 struct stat sb;
627 if (errno != EEXIST) {
628 err = got_error_from_errno();
629 goto done;
632 if (lstat(abspath, &sb) == -1) {
633 err = got_error_from_errno();
634 goto done;
637 if (!S_ISDIR(sb.st_mode)) {
638 /* TODO directory is obstructed; do something */
639 return got_error(GOT_ERR_FILE_OBSTRUCTED);
643 done:
644 free(abspath);
645 return err;
648 static const struct got_error *
649 tree_checkout(struct got_worktree *, struct got_fileindex *,
650 struct got_tree_object *, const char *, struct got_repository *,
651 got_worktree_checkout_cb progress_cb, void *progress_arg,
652 got_worktree_cancel_cb cancel_cb, void *cancel_arg);
654 static const struct got_error *
655 tree_checkout_entry(struct got_worktree *worktree,
656 struct got_fileindex *fileindex, struct got_tree_entry *te,
657 const char *parent, struct got_repository *repo,
658 got_worktree_checkout_cb progress_cb, void *progress_arg,
659 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
661 const struct got_error *err = NULL;
662 struct got_object *obj = NULL;
663 struct got_blob_object *blob = NULL;
664 struct got_fileindex_entry *entry = NULL;
665 struct got_tree_object *tree = NULL;
666 char *path = NULL;
667 char *progress_path = NULL;
668 size_t len;
670 if (parent[0] == '/' && parent[1] == '\0')
671 parent = "";
672 if (asprintf(&path, "%s/%s", parent, te->name) == -1)
673 return got_error_from_errno();
675 /* Skip this entry if it is outside of our path prefix. */
676 len = MIN(strlen(worktree->path_prefix), strlen(path));
677 if (strncmp(path, worktree->path_prefix, len) != 0) {
678 free(path);
679 return NULL;
682 err = got_object_open(&obj, repo, te->id);
683 if (err)
684 goto done;
686 progress_path = path;
687 if (strncmp(progress_path, worktree->path_prefix, len) == 0)
688 progress_path += len;
690 switch (obj->type) {
691 case GOT_OBJ_TYPE_BLOB:
692 if (strlen(worktree->path_prefix) >= strlen(path))
693 break;
694 entry = got_fileindex_entry_get(fileindex,
695 apply_path_prefix(worktree, path));
696 if (entry &&
697 memcmp(entry->commit_sha1, worktree->base_commit_id->sha1,
698 SHA1_DIGEST_LENGTH) == 0) {
699 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
700 progress_path);
701 break;
703 if (entry && memcmp(entry->blob_sha1, obj->id.sha1,
704 SHA1_DIGEST_LENGTH) == 0)
705 break;
706 err = got_object_blob_open(&blob, repo, obj, 8192);
707 if (err)
708 goto done;
709 err = blob_checkout(worktree, fileindex, entry, path, blob,
710 repo, progress_cb, progress_arg, progress_path);
711 break;
712 case GOT_OBJ_TYPE_TREE:
713 if (strlen(worktree->path_prefix) < strlen(path)) {
714 err = add_dir_on_disk(worktree, path);
715 if (err)
716 break;
718 err = got_object_tree_open(&tree, repo, obj);
719 if (err)
720 goto done;
721 /* XXX infinite recursion possible */
722 err = tree_checkout(worktree, fileindex, tree, path, repo,
723 progress_cb, progress_arg, cancel_cb, cancel_arg);
724 break;
725 default:
726 break;
729 done:
730 if (blob)
731 got_object_blob_close(blob);
732 if (tree)
733 got_object_tree_close(tree);
734 if (obj)
735 got_object_close(obj);
736 free(path);
737 return err;
740 struct collect_missing_entry_args {
741 struct got_fileindex *fileindex;
742 const struct got_tree_entries *entries;
743 struct got_fileindex missing_entries;
744 const char *current_subdir;
745 };
747 static const struct got_error *
748 collect_missing_file(void *args, struct got_fileindex_entry *entry)
750 struct collect_missing_entry_args *a = args;
751 char *start, *end;
752 ptrdiff_t len;
753 struct got_tree_entry *te;
754 int found = 0;
756 if (a->current_subdir[0] != '\0' &&
757 strncmp(a->current_subdir, entry->path,
758 strlen(a->current_subdir)) != 0)
759 return NULL;
761 start = entry->path + strlen(a->current_subdir);
762 while (start[0] == '/')
763 start++;
764 end = strchr(start, '/');
765 if (end == NULL) {
766 end = strchr(start, '\0');
767 if (end == NULL)
768 return got_error(GOT_ERR_BAD_PATH);
770 len = end - start;
772 SIMPLEQ_FOREACH(te, &a->entries->head, entry) {
773 if (strncmp(start, te->name, len) == 0 &&
774 te->name[len] == '\0') {
775 found = 1;
776 break;
780 if (found)
781 return NULL;
783 got_fileindex_entry_remove(a->fileindex, entry);
784 return got_fileindex_entry_add(&a->missing_entries, entry);
787 /* Remove files which exist in the file index but not in the tree. */
788 static const struct got_error *
789 remove_missing_files(struct got_worktree *worktree, const char *path,
790 struct got_fileindex *fileindex, const struct got_tree_entries *entries,
791 got_worktree_checkout_cb progress_cb, void *progress_arg,
792 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
794 const struct got_error *err = NULL;
795 struct collect_missing_entry_args a;
796 struct got_fileindex_entry *entry, *tmp;
798 a.fileindex = fileindex;
799 a.entries = entries;
800 a.missing_entries.nentries = 0;
801 a.current_subdir = apply_path_prefix(worktree, path);
802 TAILQ_INIT(&a.missing_entries.entries);
803 err = got_fileindex_for_each_entry_safe(fileindex,
804 collect_missing_file, &a);
805 if (err)
806 return err;
808 TAILQ_FOREACH_SAFE(entry, &a.missing_entries.entries, entry, tmp) {
809 char *ondisk_path = NULL;
811 if (cancel_cb) {
812 err = (*cancel_cb)(cancel_arg);
813 if (err)
814 break;
817 (*progress_cb)(progress_arg, GOT_STATUS_DELETE, entry->path);
819 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
820 entry->path) == -1) {
821 err = got_error_from_errno();
822 break;
825 if (unlink(ondisk_path) == -1)
826 err = got_error_from_errno();
827 else {
828 char *parent = dirname(ondisk_path);
829 if (rmdir(parent) == -1 && errno != ENOTEMPTY)
830 err = got_error_from_errno();
832 free(ondisk_path);
833 if (err)
834 break;
836 TAILQ_REMOVE(&a.missing_entries.entries, entry, entry);
837 got_fileindex_entry_free(entry);
840 if (err) {
841 while (!TAILQ_EMPTY(&a.missing_entries.entries)) {
842 entry = TAILQ_FIRST(&a.missing_entries.entries);
843 TAILQ_REMOVE(&a.missing_entries.entries, entry, entry);
844 got_fileindex_entry_free(entry);
848 return err;
851 static const struct got_error *
852 tree_checkout(struct got_worktree *worktree,
853 struct got_fileindex *fileindex, struct got_tree_object *tree,
854 const char *path, struct got_repository *repo,
855 got_worktree_checkout_cb progress_cb, void *progress_arg,
856 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
858 const struct got_error *err = NULL;
859 const struct got_tree_entries *entries;
860 struct got_tree_entry *te;
861 size_t len;
863 /* Skip this tree if it shares no path components with the prefix. */
864 len = MIN(strlen(worktree->path_prefix), strlen(path));
865 if (strncmp(path, worktree->path_prefix, len) != 0)
866 return NULL;
868 entries = got_object_tree_get_entries(tree);
869 SIMPLEQ_FOREACH(te, &entries->head, entry) {
870 if (cancel_cb) {
871 err = (*cancel_cb)(cancel_arg);
872 if (err)
873 return err;
875 err = tree_checkout_entry(worktree, fileindex, te, path, repo,
876 progress_cb, progress_arg, cancel_cb, cancel_arg);
877 if (err)
878 return err;
881 len = strlen(worktree->path_prefix);
882 if (strncmp(worktree->path_prefix, path, len) == 0) {
883 err = remove_missing_files(worktree, path, fileindex, entries,
884 progress_cb, progress_arg, cancel_cb, cancel_arg);
885 if (err)
886 return err;
889 return err;
892 const struct got_error *
893 got_worktree_checkout_files(struct got_worktree *worktree,
894 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
895 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
897 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
898 struct got_commit_object *commit = NULL;
899 struct got_tree_object *tree = NULL;
900 char *fileindex_path = NULL, *new_fileindex_path = NULL;
901 struct got_fileindex *fileindex = NULL;
902 FILE *index = NULL, *new_index = NULL;
904 err = lock_worktree(worktree, LOCK_EX);
905 if (err)
906 return err;
908 fileindex = got_fileindex_alloc();
909 if (fileindex == NULL) {
910 err = got_error_from_errno();
911 goto done;
914 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
915 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
916 err = got_error_from_errno();
917 fileindex_path = NULL;
918 goto done;
921 /*
922 * Read the file index.
923 * Checking out files is supposed to be an idempotent operation.
924 * If the on-disk file index is incomplete we will try to complete it.
925 */
926 index = fopen(fileindex_path, "rb");
927 if (index == NULL) {
928 err = got_error_from_errno();
929 goto done;
931 err = got_fileindex_read(fileindex, index);
932 fclose(index);
933 if (err)
934 goto done;
936 err = got_opentemp_named(&new_fileindex_path, &new_index,
937 fileindex_path);
938 if (err)
939 goto done;
941 err = got_object_open_as_commit(&commit, repo,
942 worktree->base_commit_id);
943 if (err)
944 goto done;
946 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
947 if (err)
948 goto done;
950 checkout_err = tree_checkout(worktree, fileindex, tree, "/", repo,
951 progress_cb, progress_arg, cancel_cb, cancel_arg);
953 /* Try to sync the fileindex back to disk in any case. */
954 err = got_fileindex_write(fileindex, new_index);
955 if (err)
956 goto done;
958 if (rename(new_fileindex_path, fileindex_path) != 0) {
959 err = got_error_from_errno();
960 goto done;
963 free(new_fileindex_path);
964 new_fileindex_path = NULL;
966 done:
967 if (tree)
968 got_object_tree_close(tree);
969 if (commit)
970 got_object_commit_close(commit);
971 if (new_fileindex_path)
972 unlink(new_fileindex_path);
973 if (new_index)
974 fclose(new_index);
975 free(new_fileindex_path);
976 free(fileindex_path);
977 got_fileindex_free(fileindex);
978 if (checkout_err)
979 err = checkout_err;
980 unlockerr = lock_worktree(worktree, LOCK_SH);
981 if (unlockerr && err == NULL)
982 err = unlockerr;
983 return err;