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 *repo_path = 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 repo_path = got_repo_get_path(repo);
255 if (repo_path == NULL) {
256 err = got_error_from_errno();
257 goto done;
259 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY, repo_path);
260 if (err)
261 goto done;
263 /* Store in-repository path prefix. */
264 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
265 absprefix ? absprefix : prefix);
266 if (err)
267 goto done;
269 /* Stamp work tree with format file. */
270 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
271 err = got_error_from_errno();
272 goto done;
274 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
275 if (err)
276 goto done;
278 done:
279 free(commit_id);
280 free(path_got);
281 free(formatstr);
282 free(refstr);
283 free(repo_path);
284 free(absprefix);
285 free(basestr);
286 return err;
289 const struct got_error *
290 got_worktree_open(struct got_worktree **worktree, const char *path)
292 const struct got_error *err = NULL;
293 char *path_got;
294 char *formatstr = NULL;
295 char *path_lock = NULL;
296 char *base_commit_id_str = NULL;
297 char *head_ref_str = NULL;
298 int version, fd = -1;
299 const char *errstr;
300 struct got_repository *repo = NULL;
302 *worktree = NULL;
304 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
305 err = got_error_from_errno();
306 path_got = NULL;
307 goto done;
310 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
311 err = got_error_from_errno();
312 path_lock = NULL;
313 goto done;
316 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
317 if (fd == -1) {
318 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
319 : got_error_from_errno());
320 goto done;
323 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
324 if (err)
325 goto done;
327 version = strtonum(formatstr, 1, INT_MAX, &errstr);
328 if (errstr) {
329 err = got_error(GOT_ERR_WORKTREE_META);
330 goto done;
332 if (version != GOT_WORKTREE_FORMAT_VERSION) {
333 err = got_error(GOT_ERR_WORKTREE_VERS);
334 goto done;
337 *worktree = calloc(1, sizeof(**worktree));
338 if (*worktree == NULL) {
339 err = got_error_from_errno();
340 goto done;
342 (*worktree)->lockfd = -1;
344 (*worktree)->root_path = strdup(path);
345 if ((*worktree)->root_path == NULL) {
346 err = got_error_from_errno();
347 goto done;
349 err = read_meta_file(&(*worktree)->repo_path, path_got,
350 GOT_WORKTREE_REPOSITORY);
351 if (err)
352 goto done;
354 err = read_meta_file(&(*worktree)->path_prefix, path_got,
355 GOT_WORKTREE_PATH_PREFIX);
356 if (err)
357 goto done;
359 err = read_meta_file(&base_commit_id_str, path_got,
360 GOT_WORKTREE_BASE_COMMIT);
361 if (err)
362 goto done;
364 err = got_repo_open(&repo, (*worktree)->repo_path);
365 if (err)
366 goto done;
368 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
369 base_commit_id_str);
370 if (err)
371 goto done;
373 err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
374 if (err)
375 goto done;
377 err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
378 done:
379 if (repo)
380 got_repo_close(repo);
381 free(path_got);
382 free(path_lock);
383 free(head_ref_str);
384 free(base_commit_id_str);
385 if (err) {
386 if (fd != -1)
387 close(fd);
388 if (*worktree != NULL)
389 got_worktree_close(*worktree);
390 *worktree = NULL;
391 } else
392 (*worktree)->lockfd = fd;
394 return err;
397 void
398 got_worktree_close(struct got_worktree *worktree)
400 free(worktree->root_path);
401 free(worktree->repo_path);
402 free(worktree->path_prefix);
403 free(worktree->base_commit_id);
404 if (worktree->head_ref)
405 got_ref_close(worktree->head_ref);
406 if (worktree->lockfd != -1)
407 close(worktree->lockfd);
408 free(worktree);
411 const char *
412 got_worktree_get_repo_path(struct got_worktree *worktree)
414 return worktree->repo_path;
417 const char *
418 got_worktree_get_path_prefix(struct got_worktree *worktree)
420 return worktree->path_prefix;
423 const struct got_error *
424 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
425 const char *path_prefix)
427 char *absprefix = NULL;
429 if (!got_path_is_absolute(path_prefix)) {
430 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
431 return got_error_from_errno();
433 *match = (strcmp(absprefix ? absprefix : path_prefix,
434 worktree->path_prefix) == 0);
435 free(absprefix);
436 return NULL;
439 char *
440 got_worktree_get_head_ref_name(struct got_worktree *worktree)
442 return got_ref_to_str(worktree->head_ref);
445 struct got_reference *
446 got_worktree_get_head_ref(struct got_worktree *worktree)
448 return got_ref_dup(worktree->head_ref);
451 const struct got_object_id *
452 got_worktree_get_base_commit_id(struct got_worktree *worktree)
454 return worktree->base_commit_id;
457 const struct got_error *
458 got_worktree_set_base_commit_id(struct got_worktree *worktree,
459 struct got_repository *repo, struct got_object_id *commit_id)
461 const struct got_error *err;
462 struct got_object *obj = NULL;
463 char *id_str = NULL;
464 char *path_got = NULL;
466 if (asprintf(&path_got, "%s/%s", worktree->root_path,
467 GOT_WORKTREE_GOT_DIR) == -1) {
468 err = got_error_from_errno();
469 path_got = NULL;
470 goto done;
473 err = got_object_open(&obj, repo, commit_id);
474 if (err)
475 return err;
477 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
478 err = got_error(GOT_ERR_OBJ_TYPE);
479 goto done;
482 /* Record our base commit. */
483 err = got_object_id_str(&id_str, commit_id);
484 if (err)
485 goto done;
486 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
487 if (err)
488 goto done;
490 free(worktree->base_commit_id);
491 worktree->base_commit_id = got_object_id_dup(commit_id);
492 if (worktree->base_commit_id == NULL) {
493 err = got_error_from_errno();
494 goto done;
496 done:
497 if (obj)
498 got_object_close(obj);
499 free(id_str);
500 free(path_got);
501 return err;
504 static const struct got_error *
505 lock_worktree(struct got_worktree *worktree, int operation)
507 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
508 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
509 : got_error_from_errno());
510 return NULL;
513 static const char *
514 apply_path_prefix(struct got_worktree *worktree, const char *path)
516 const char *p = path;
517 p += strlen(worktree->path_prefix);
518 if (*p == '/')
519 p++;
520 return p;
523 static const struct got_error *
524 blob_checkout(struct got_worktree *worktree, struct got_fileindex *fileindex,
525 struct got_fileindex_entry *entry, const char *path,
526 struct got_blob_object *blob, struct got_repository *repo,
527 got_worktree_checkout_cb progress_cb, void *progress_arg,
528 const char *progress_path)
530 const struct got_error *err = NULL;
531 char *ondisk_path;
532 int fd = -1;
533 size_t len, hdrlen;
534 int update = 0;
535 char *tmppath = NULL;
537 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
538 apply_path_prefix(worktree, path)) == -1)
539 return got_error_from_errno();
541 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
542 GOT_DEFAULT_FILE_MODE);
543 if (fd == -1) {
544 err = got_error_from_errno();
545 if (errno == EEXIST) {
546 struct stat sb;
547 if (lstat(ondisk_path, &sb) == -1) {
548 err = got_error_from_errno();
549 goto done;
550 } else if (!S_ISREG(sb.st_mode)) {
551 /* TODO file is obstructed; do something */
552 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
553 goto done;
554 } else {
555 err = got_opentemp_named_fd(&tmppath, &fd,
556 ondisk_path);
557 if (err)
558 goto done;
559 update = 1;
561 } else
562 return err;
565 (*progress_cb)(progress_arg,
566 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, progress_path);
568 hdrlen = got_object_blob_get_hdrlen(blob);
569 do {
570 const uint8_t *buf = got_object_blob_get_read_buf(blob);
571 err = got_object_blob_read_block(&len, blob);
572 if (err)
573 break;
574 if (len > 0) {
575 /* Skip blob object header first time around. */
576 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
577 if (outlen == -1) {
578 err = got_error_from_errno();
579 goto done;
580 } else if (outlen != len - hdrlen) {
581 err = got_error(GOT_ERR_IO);
582 goto done;
584 hdrlen = 0;
586 } while (len != 0);
588 fsync(fd);
590 if (update) {
591 if (rename(tmppath, ondisk_path) != 0) {
592 err = got_error_from_errno();
593 goto done;
597 if (entry)
598 err = got_fileindex_entry_update(entry, ondisk_path,
599 blob->id.sha1, worktree->base_commit_id->sha1);
600 else {
601 err = got_fileindex_entry_alloc(&entry, ondisk_path,
602 apply_path_prefix(worktree, path), blob->id.sha1,
603 worktree->base_commit_id->sha1);
604 if (err)
605 goto done;
606 err = got_fileindex_entry_add(fileindex, entry);
608 if (err)
609 goto done;
610 done:
611 if (fd != -1)
612 close(fd);
613 free(ondisk_path);
614 free(tmppath);
615 return err;
618 static const struct got_error *
619 add_dir_on_disk(struct got_worktree *worktree, const char *path)
621 const struct got_error *err = NULL;
622 char *abspath;
624 if (asprintf(&abspath, "%s/%s", worktree->root_path,
625 apply_path_prefix(worktree, path)) == -1)
626 return got_error_from_errno();
628 /* XXX queue work rather than editing disk directly? */
629 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
630 struct stat sb;
632 if (errno != EEXIST) {
633 err = got_error_from_errno();
634 goto done;
637 if (lstat(abspath, &sb) == -1) {
638 err = got_error_from_errno();
639 goto done;
642 if (!S_ISDIR(sb.st_mode)) {
643 /* TODO directory is obstructed; do something */
644 return got_error(GOT_ERR_FILE_OBSTRUCTED);
648 done:
649 free(abspath);
650 return err;
653 static const struct got_error *
654 tree_checkout(struct got_worktree *, struct got_fileindex *,
655 struct got_tree_object *, const char *, struct got_repository *,
656 got_worktree_checkout_cb progress_cb, void *progress_arg,
657 got_worktree_cancel_cb cancel_cb, void *cancel_arg);
659 static const struct got_error *
660 tree_checkout_entry(struct got_worktree *worktree,
661 struct got_fileindex *fileindex, struct got_tree_entry *te,
662 const char *parent, struct got_repository *repo,
663 got_worktree_checkout_cb progress_cb, void *progress_arg,
664 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
666 const struct got_error *err = NULL;
667 struct got_object *obj = NULL;
668 struct got_blob_object *blob = NULL;
669 struct got_fileindex_entry *entry = NULL;
670 struct got_tree_object *tree = NULL;
671 char *path = NULL;
672 char *progress_path = NULL;
673 size_t len;
675 if (parent[0] == '/' && parent[1] == '\0')
676 parent = "";
677 if (asprintf(&path, "%s/%s", parent, te->name) == -1)
678 return got_error_from_errno();
680 /* Skip this entry if it is outside of our path prefix. */
681 len = MIN(strlen(worktree->path_prefix), strlen(path));
682 if (strncmp(path, worktree->path_prefix, len) != 0) {
683 free(path);
684 return NULL;
687 err = got_object_open(&obj, repo, te->id);
688 if (err)
689 goto done;
691 progress_path = path;
692 if (strncmp(progress_path, worktree->path_prefix, len) == 0)
693 progress_path += len;
695 switch (obj->type) {
696 case GOT_OBJ_TYPE_BLOB:
697 if (strlen(worktree->path_prefix) >= strlen(path))
698 break;
699 entry = got_fileindex_entry_get(fileindex,
700 apply_path_prefix(worktree, path));
701 if (entry &&
702 memcmp(entry->commit_sha1, worktree->base_commit_id->sha1,
703 SHA1_DIGEST_LENGTH) == 0) {
704 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
705 progress_path);
706 break;
708 if (entry && memcmp(entry->blob_sha1, obj->id.sha1,
709 SHA1_DIGEST_LENGTH) == 0)
710 break;
711 err = got_object_blob_open(&blob, repo, obj, 8192);
712 if (err)
713 goto done;
714 err = blob_checkout(worktree, fileindex, entry, path, blob,
715 repo, progress_cb, progress_arg, progress_path);
716 break;
717 case GOT_OBJ_TYPE_TREE:
718 if (strlen(worktree->path_prefix) < strlen(path)) {
719 err = add_dir_on_disk(worktree, path);
720 if (err)
721 break;
723 err = got_object_tree_open(&tree, repo, obj);
724 if (err)
725 goto done;
726 /* XXX infinite recursion possible */
727 err = tree_checkout(worktree, fileindex, tree, path, repo,
728 progress_cb, progress_arg, cancel_cb, cancel_arg);
729 break;
730 default:
731 break;
734 done:
735 if (blob)
736 got_object_blob_close(blob);
737 if (tree)
738 got_object_tree_close(tree);
739 if (obj)
740 got_object_close(obj);
741 free(path);
742 return err;
745 struct collect_missing_entry_args {
746 struct got_fileindex *fileindex;
747 const struct got_tree_entries *entries;
748 struct got_fileindex missing_entries;
749 const char *path_prefix;
750 };
752 static const struct got_error *
753 collect_missing_file(void *args, struct got_fileindex_entry *entry)
755 struct collect_missing_entry_args *a = args;
756 char *name;
757 struct got_tree_entry *te;
758 int found = 0;
760 if (a->path_prefix[0] == '\0' && strchr(entry->path, '/') != NULL)
761 return NULL;
762 if (a->path_prefix[0] != '\0' &&
763 strncmp(a->path_prefix, entry->path, strlen(a->path_prefix)) != 0)
764 return NULL;
766 name = basename(entry->path);
767 if (name == NULL)
768 return got_error_from_errno();
770 SIMPLEQ_FOREACH(te, &a->entries->head, entry) {
771 if (strcmp(te->name, name) == 0) {
772 found = 1;
773 break;
777 if (found)
778 return NULL;
780 got_fileindex_entry_remove(a->fileindex, entry);
781 return got_fileindex_entry_add(&a->missing_entries, entry);
784 /* Remove files which exist in the file index but not in the tree. */
785 static const struct got_error *
786 remove_missing_files(struct got_worktree *worktree, const char *path,
787 struct got_fileindex *fileindex, const struct got_tree_entries *entries,
788 got_worktree_checkout_cb progress_cb, void *progress_arg,
789 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
791 const struct got_error *err = NULL;
792 struct collect_missing_entry_args a;
793 struct got_fileindex_entry *entry, *tmp;
795 a.fileindex = fileindex;
796 a.entries = entries;
797 a.missing_entries.nentries = 0;
798 a.path_prefix = path;
799 while (a.path_prefix[0] == '/')
800 a.path_prefix++;
801 TAILQ_INIT(&a.missing_entries.entries);
802 err = got_fileindex_for_each_entry(fileindex, collect_missing_file, &a);
803 if (err)
804 return err;
806 TAILQ_FOREACH_SAFE(entry, &a.missing_entries.entries, entry, tmp) {
807 char *ondisk_path = NULL;
809 if (cancel_cb) {
810 err = (*cancel_cb)(cancel_arg);
811 if (err)
812 break;
815 (*progress_cb)(progress_arg, GOT_STATUS_DELETE, entry->path);
817 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
818 entry->path) == -1) {
819 err = got_error_from_errno();
820 break;
823 if (unlink(ondisk_path) == -1)
824 err = got_error_from_errno();
825 free(ondisk_path);
826 if (err)
827 break;
829 TAILQ_REMOVE(&a.missing_entries.entries, entry, entry);
830 got_fileindex_entry_free(entry);
833 if (err) {
834 while (!TAILQ_EMPTY(&a.missing_entries.entries)) {
835 entry = TAILQ_FIRST(&a.missing_entries.entries);
836 TAILQ_REMOVE(&a.missing_entries.entries, entry, entry);
837 got_fileindex_entry_free(entry);
841 return err;
844 static const struct got_error *
845 tree_checkout(struct got_worktree *worktree,
846 struct got_fileindex *fileindex, struct got_tree_object *tree,
847 const char *path, struct got_repository *repo,
848 got_worktree_checkout_cb progress_cb, void *progress_arg,
849 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
851 const struct got_error *err = NULL;
852 const struct got_tree_entries *entries;
853 struct got_tree_entry *te;
854 size_t len;
856 /* Skip this tree if it is outside of our path prefix. */
857 len = MIN(strlen(worktree->path_prefix), strlen(path));
858 if (strncmp(path, worktree->path_prefix, len) != 0)
859 return NULL;
861 entries = got_object_tree_get_entries(tree);
862 SIMPLEQ_FOREACH(te, &entries->head, entry) {
863 if (cancel_cb) {
864 err = (*cancel_cb)(cancel_arg);
865 if (err)
866 return err;
868 err = tree_checkout_entry(worktree, fileindex, te, path, repo,
869 progress_cb, progress_arg, cancel_cb, cancel_arg);
870 if (err)
871 return err;
874 len = strlen(worktree->path_prefix);
875 if (strncmp(worktree->path_prefix, path, len) == 0) {
876 err = remove_missing_files(worktree, path, fileindex, entries,
877 progress_cb, progress_arg, cancel_cb, cancel_arg);
878 if (err)
879 return err;
882 return err;
885 const struct got_error *
886 got_worktree_checkout_files(struct got_worktree *worktree,
887 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
888 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
890 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
891 struct got_commit_object *commit = NULL;
892 struct got_tree_object *tree = NULL;
893 char *fileindex_path = NULL, *new_fileindex_path = NULL;
894 struct got_fileindex *fileindex = NULL;
895 FILE *index = NULL, *new_index = NULL;
897 err = lock_worktree(worktree, LOCK_EX);
898 if (err)
899 return err;
901 fileindex = got_fileindex_alloc();
902 if (fileindex == NULL) {
903 err = got_error_from_errno();
904 goto done;
907 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
908 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
909 err = got_error_from_errno();
910 fileindex_path = NULL;
911 goto done;
914 /*
915 * Read the file index.
916 * Checking out files is supposed to be an idempotent operation.
917 * If the on-disk file index is incomplete we will try to complete it.
918 */
919 index = fopen(fileindex_path, "rb");
920 if (index == NULL) {
921 err = got_error_from_errno();
922 goto done;
924 err = got_fileindex_read(fileindex, index);
925 fclose(index);
926 if (err)
927 goto done;
929 err = got_opentemp_named(&new_fileindex_path, &new_index,
930 fileindex_path);
931 if (err)
932 goto done;
934 err = got_object_open_as_commit(&commit, repo,
935 worktree->base_commit_id);
936 if (err)
937 goto done;
939 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
940 if (err)
941 goto done;
943 checkout_err = tree_checkout(worktree, fileindex, tree, "/", repo,
944 progress_cb, progress_arg, cancel_cb, cancel_arg);
946 /* Try to sync the fileindex back to disk in any case. */
947 err = got_fileindex_write(fileindex, new_index);
948 if (err)
949 goto done;
951 if (rename(new_fileindex_path, fileindex_path) != 0) {
952 err = got_error_from_errno();
953 goto done;
956 free(new_fileindex_path);
957 new_fileindex_path = NULL;
959 done:
960 if (tree)
961 got_object_tree_close(tree);
962 if (commit)
963 got_object_commit_close(commit);
964 if (new_fileindex_path)
965 unlink(new_fileindex_path);
966 if (new_index)
967 fclose(new_index);
968 free(new_fileindex_path);
969 free(fileindex_path);
970 got_fileindex_free(fileindex);
971 if (checkout_err)
972 err = checkout_err;
973 unlockerr = lock_worktree(worktree, LOCK_SH);
974 if (unlockerr && err == NULL)
975 err = unlockerr;
976 return err;