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_pathset.h"
43 #include "got_lib_sha1.h"
44 #include "got_lib_fileindex.h"
45 #include "got_lib_inflate.h"
46 #include "got_lib_delta.h"
47 #include "got_lib_object.h"
49 #ifndef MIN
50 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
51 #endif
53 static const struct got_error *
54 create_meta_file(const char *path_got, const char *name, const char *content)
55 {
56 const struct got_error *err = NULL;
57 char *path;
58 int fd = -1;
60 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
61 err = got_error_from_errno();
62 path = NULL;
63 goto done;
64 }
66 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
67 GOT_DEFAULT_FILE_MODE);
68 if (fd == -1) {
69 err = got_error_from_errno();
70 goto done;
71 }
73 if (content) {
74 int len = dprintf(fd, "%s\n", content);
75 if (len != strlen(content) + 1) {
76 err = got_error_from_errno();
77 goto done;
78 }
79 }
81 done:
82 if (fd != -1 && close(fd) == -1 && err == NULL)
83 err = got_error_from_errno();
84 free(path);
85 return err;
86 }
88 static const struct got_error *
89 update_meta_file(const char *path_got, const char *name, const char *content)
90 {
91 const struct got_error *err = NULL;
92 FILE *tmpfile = NULL;
93 char *tmppath = NULL;
94 char *path = NULL;
96 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
97 err = got_error_from_errno();
98 path = NULL;
99 goto done;
102 err = got_opentemp_named(&tmppath, &tmpfile, path);
103 if (err)
104 goto done;
106 if (content) {
107 int len = fprintf(tmpfile, "%s\n", content);
108 if (len != strlen(content) + 1) {
109 err = got_error_from_errno();
110 goto done;
114 if (rename(tmppath, path) != 0) {
115 err = got_error_from_errno();
116 goto done;
119 done:
120 free(tmppath);
121 fclose(tmpfile);
122 return err;
125 static const struct got_error *
126 read_meta_file(char **content, const char *path_got, const char *name)
128 const struct got_error *err = NULL;
129 char *path;
130 int fd = -1;
131 ssize_t n;
132 struct stat sb;
134 *content = NULL;
136 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
137 err = got_error_from_errno();
138 path = NULL;
139 goto done;
142 fd = open(path, O_RDONLY | O_NOFOLLOW);
143 if (fd == -1) {
144 err = got_error_from_errno();
145 goto done;
147 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
148 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
149 : got_error_from_errno());
150 goto done;
153 stat(path, &sb);
154 *content = calloc(1, sb.st_size);
155 if (*content == NULL) {
156 err = got_error_from_errno();
157 goto done;
160 n = read(fd, *content, sb.st_size);
161 if (n != sb.st_size) {
162 err = (n == -1 ? got_error_from_errno() :
163 got_error(GOT_ERR_WORKTREE_META));
164 goto done;
166 if ((*content)[sb.st_size - 1] != '\n') {
167 err = got_error(GOT_ERR_WORKTREE_META);
168 goto done;
170 (*content)[sb.st_size - 1] = '\0';
172 done:
173 if (fd != -1 && close(fd) == -1 && err == NULL)
174 err = got_error_from_errno();
175 free(path);
176 if (err) {
177 free(*content);
178 *content = NULL;
180 return err;
183 const struct got_error *
184 got_worktree_init(const char *path, struct got_reference *head_ref,
185 const char *prefix, struct got_repository *repo)
187 const struct got_error *err = NULL;
188 struct got_object_id *commit_id = NULL;
189 int obj_type;
190 char *path_got = NULL;
191 char *refstr = NULL;
192 char *formatstr = NULL;
193 char *absprefix = NULL;
194 char *basestr = NULL;
196 err = got_ref_resolve(&commit_id, repo, head_ref);
197 if (err)
198 return err;
199 err = got_object_get_type(&obj_type, repo, commit_id);
200 if (err)
201 return err;
202 if (obj_type != GOT_OBJ_TYPE_COMMIT)
203 return got_error(GOT_ERR_OBJ_TYPE);
205 if (!got_path_is_absolute(prefix)) {
206 if (asprintf(&absprefix, "/%s", prefix) == -1)
207 return got_error_from_errno();
210 /* Create top-level directory (may already exist). */
211 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
212 err = got_error_from_errno();
213 goto done;
216 /* Create .got directory (may already exist). */
217 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
218 err = got_error_from_errno();
219 goto done;
221 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
222 err = got_error_from_errno();
223 goto done;
226 /* Create an empty lock file. */
227 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
228 if (err)
229 goto done;
231 /* Create an empty file index. */
232 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
233 if (err)
234 goto done;
236 /* Write the HEAD reference. */
237 refstr = got_ref_to_str(head_ref);
238 if (refstr == NULL) {
239 err = got_error_from_errno();
240 goto done;
242 err = create_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
243 if (err)
244 goto done;
246 /* Record our base commit. */
247 err = got_object_id_str(&basestr, commit_id);
248 if (err)
249 goto done;
250 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
251 if (err)
252 goto done;
254 /* Store path to repository. */
255 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
256 got_repo_get_path(repo));
257 if (err)
258 goto done;
260 /* Store in-repository path prefix. */
261 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
262 absprefix ? absprefix : prefix);
263 if (err)
264 goto done;
266 /* Stamp work tree with format file. */
267 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
268 err = got_error_from_errno();
269 goto done;
271 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
272 if (err)
273 goto done;
275 done:
276 free(commit_id);
277 free(path_got);
278 free(formatstr);
279 free(refstr);
280 free(absprefix);
281 free(basestr);
282 return err;
285 const struct got_error *
286 got_worktree_open(struct got_worktree **worktree, const char *path)
288 const struct got_error *err = NULL;
289 char *path_got;
290 char *formatstr = NULL;
291 char *path_lock = NULL;
292 char *base_commit_id_str = NULL;
293 char *head_ref_str = NULL;
294 int version, fd = -1;
295 const char *errstr;
296 struct got_repository *repo = NULL;
298 *worktree = NULL;
300 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
301 err = got_error_from_errno();
302 path_got = NULL;
303 goto done;
306 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
307 err = got_error_from_errno();
308 path_lock = NULL;
309 goto done;
312 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
313 if (fd == -1) {
314 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
315 : got_error_from_errno());
316 goto done;
319 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
320 if (err)
321 goto done;
323 version = strtonum(formatstr, 1, INT_MAX, &errstr);
324 if (errstr) {
325 err = got_error(GOT_ERR_WORKTREE_META);
326 goto done;
328 if (version != GOT_WORKTREE_FORMAT_VERSION) {
329 err = got_error(GOT_ERR_WORKTREE_VERS);
330 goto done;
333 *worktree = calloc(1, sizeof(**worktree));
334 if (*worktree == NULL) {
335 err = got_error_from_errno();
336 goto done;
338 (*worktree)->lockfd = -1;
340 (*worktree)->root_path = strdup(path);
341 if ((*worktree)->root_path == NULL) {
342 err = got_error_from_errno();
343 goto done;
345 err = read_meta_file(&(*worktree)->repo_path, path_got,
346 GOT_WORKTREE_REPOSITORY);
347 if (err)
348 goto done;
350 err = read_meta_file(&(*worktree)->path_prefix, path_got,
351 GOT_WORKTREE_PATH_PREFIX);
352 if (err)
353 goto done;
355 err = read_meta_file(&base_commit_id_str, path_got,
356 GOT_WORKTREE_BASE_COMMIT);
357 if (err)
358 goto done;
360 err = got_repo_open(&repo, (*worktree)->repo_path);
361 if (err)
362 goto done;
364 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
365 base_commit_id_str);
366 if (err)
367 goto done;
369 err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
370 if (err)
371 goto done;
373 err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
374 done:
375 if (repo)
376 got_repo_close(repo);
377 free(path_got);
378 free(path_lock);
379 free(head_ref_str);
380 free(base_commit_id_str);
381 if (err) {
382 if (fd != -1)
383 close(fd);
384 if (*worktree != NULL)
385 got_worktree_close(*worktree);
386 *worktree = NULL;
387 } else
388 (*worktree)->lockfd = fd;
390 return err;
393 void
394 got_worktree_close(struct got_worktree *worktree)
396 free(worktree->root_path);
397 free(worktree->repo_path);
398 free(worktree->path_prefix);
399 free(worktree->base_commit_id);
400 if (worktree->head_ref)
401 got_ref_close(worktree->head_ref);
402 if (worktree->lockfd != -1)
403 close(worktree->lockfd);
404 free(worktree);
407 const char *
408 got_worktree_get_repo_path(struct got_worktree *worktree)
410 return worktree->repo_path;
413 const char *
414 got_worktree_get_path_prefix(struct got_worktree *worktree)
416 return worktree->path_prefix;
419 const struct got_error *
420 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
421 const char *path_prefix)
423 char *absprefix = NULL;
425 if (!got_path_is_absolute(path_prefix)) {
426 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
427 return got_error_from_errno();
429 *match = (strcmp(absprefix ? absprefix : path_prefix,
430 worktree->path_prefix) == 0);
431 free(absprefix);
432 return NULL;
435 char *
436 got_worktree_get_head_ref_name(struct got_worktree *worktree)
438 return got_ref_to_str(worktree->head_ref);
441 struct got_reference *
442 got_worktree_get_head_ref(struct got_worktree *worktree)
444 return got_ref_dup(worktree->head_ref);
447 const struct got_object_id *
448 got_worktree_get_base_commit_id(struct got_worktree *worktree)
450 return worktree->base_commit_id;
453 const struct got_error *
454 got_worktree_set_base_commit_id(struct got_worktree *worktree,
455 struct got_repository *repo, struct got_object_id *commit_id)
457 const struct got_error *err;
458 struct got_object *obj = NULL;
459 char *id_str = NULL;
460 char *path_got = NULL;
462 if (asprintf(&path_got, "%s/%s", worktree->root_path,
463 GOT_WORKTREE_GOT_DIR) == -1) {
464 err = got_error_from_errno();
465 path_got = NULL;
466 goto done;
469 err = got_object_open(&obj, repo, commit_id);
470 if (err)
471 return err;
473 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
474 err = got_error(GOT_ERR_OBJ_TYPE);
475 goto done;
478 /* Record our base commit. */
479 err = got_object_id_str(&id_str, commit_id);
480 if (err)
481 goto done;
482 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
483 if (err)
484 goto done;
486 free(worktree->base_commit_id);
487 worktree->base_commit_id = got_object_id_dup(commit_id);
488 if (worktree->base_commit_id == NULL) {
489 err = got_error_from_errno();
490 goto done;
492 done:
493 if (obj)
494 got_object_close(obj);
495 free(id_str);
496 free(path_got);
497 return err;
500 static const struct got_error *
501 lock_worktree(struct got_worktree *worktree, int operation)
503 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
504 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
505 : got_error_from_errno());
506 return NULL;
509 static const char *
510 apply_path_prefix(struct got_worktree *worktree, const char *path)
512 const char *p = path;
513 p += strlen(worktree->path_prefix);
514 if (*p == '/')
515 p++;
516 return p;
519 static const struct got_error *
520 blob_checkout(struct got_worktree *worktree, struct got_fileindex *fileindex,
521 struct got_fileindex_entry *entry, const char *path,
522 struct got_blob_object *blob, struct got_repository *repo,
523 got_worktree_checkout_cb progress_cb, void *progress_arg,
524 const char *progress_path)
526 const struct got_error *err = NULL;
527 char *ondisk_path;
528 int fd = -1;
529 size_t len, hdrlen;
530 int update = 0;
531 char *tmppath = NULL;
533 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
534 apply_path_prefix(worktree, path)) == -1)
535 return got_error_from_errno();
537 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
538 GOT_DEFAULT_FILE_MODE);
539 if (fd == -1) {
540 err = got_error_from_errno();
541 if (errno == EEXIST) {
542 struct stat sb;
543 if (lstat(ondisk_path, &sb) == -1) {
544 err = got_error_from_errno();
545 goto done;
546 } else if (!S_ISREG(sb.st_mode)) {
547 /* TODO file is obstructed; do something */
548 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
549 goto done;
550 } else {
551 err = got_opentemp_named_fd(&tmppath, &fd,
552 ondisk_path);
553 if (err)
554 goto done;
555 update = 1;
557 } else
558 return err;
561 (*progress_cb)(progress_arg,
562 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, progress_path);
564 hdrlen = got_object_blob_get_hdrlen(blob);
565 do {
566 const uint8_t *buf = got_object_blob_get_read_buf(blob);
567 err = got_object_blob_read_block(&len, blob);
568 if (err)
569 break;
570 if (len > 0) {
571 /* Skip blob object header first time around. */
572 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
573 if (outlen == -1) {
574 err = got_error_from_errno();
575 goto done;
576 } else if (outlen != len - hdrlen) {
577 err = got_error(GOT_ERR_IO);
578 goto done;
580 hdrlen = 0;
582 } while (len != 0);
584 fsync(fd);
586 if (update) {
587 if (rename(tmppath, ondisk_path) != 0) {
588 err = got_error_from_errno();
589 goto done;
593 if (entry)
594 err = got_fileindex_entry_update(entry, ondisk_path,
595 blob->id.sha1, worktree->base_commit_id->sha1);
596 else {
597 err = got_fileindex_entry_alloc(&entry, ondisk_path,
598 apply_path_prefix(worktree, path), blob->id.sha1,
599 worktree->base_commit_id->sha1);
600 if (err)
601 goto done;
602 err = got_fileindex_entry_add(fileindex, entry);
604 if (err)
605 goto done;
606 done:
607 if (fd != -1)
608 close(fd);
609 free(ondisk_path);
610 free(tmppath);
611 return err;
614 static const struct got_error *
615 add_dir_on_disk(struct got_worktree *worktree, const char *path)
617 const struct got_error *err = NULL;
618 char *abspath;
620 if (asprintf(&abspath, "%s/%s", worktree->root_path,
621 apply_path_prefix(worktree, path)) == -1)
622 return got_error_from_errno();
624 /* XXX queue work rather than editing disk directly? */
625 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
626 struct stat sb;
628 if (errno != EEXIST) {
629 err = got_error_from_errno();
630 goto done;
633 if (lstat(abspath, &sb) == -1) {
634 err = got_error_from_errno();
635 goto done;
638 if (!S_ISDIR(sb.st_mode)) {
639 /* TODO directory is obstructed; do something */
640 return got_error(GOT_ERR_FILE_OBSTRUCTED);
644 done:
645 free(abspath);
646 return err;
649 static const struct got_error *
650 tree_checkout(struct got_worktree *, struct got_fileindex *,
651 struct got_tree_object *, const char *, struct got_repository *,
652 got_worktree_checkout_cb progress_cb, void *progress_arg,
653 got_worktree_cancel_cb cancel_cb, void *cancel_arg);
655 static const struct got_error *
656 tree_checkout_entry(struct got_worktree *worktree,
657 struct got_fileindex *fileindex, struct got_tree_entry *te,
658 const char *parent, struct got_repository *repo,
659 got_worktree_checkout_cb progress_cb, void *progress_arg,
660 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
662 const struct got_error *err = NULL;
663 struct got_object *obj = NULL;
664 struct got_blob_object *blob = NULL;
665 struct got_fileindex_entry *entry = NULL;
666 struct got_tree_object *tree = NULL;
667 char *path = NULL;
668 char *progress_path = NULL;
669 size_t len;
671 if (parent[0] == '/' && parent[1] == '\0')
672 parent = "";
673 if (asprintf(&path, "%s/%s", parent, te->name) == -1)
674 return got_error_from_errno();
676 /* Skip this entry if it is outside of our path prefix. */
677 len = MIN(strlen(worktree->path_prefix), strlen(path));
678 if (strncmp(path, worktree->path_prefix, len) != 0) {
679 free(path);
680 return NULL;
683 err = got_object_open(&obj, repo, te->id);
684 if (err)
685 goto done;
687 progress_path = path;
688 if (strncmp(progress_path, worktree->path_prefix, len) == 0)
689 progress_path += len;
691 switch (obj->type) {
692 case GOT_OBJ_TYPE_BLOB:
693 if (strlen(worktree->path_prefix) >= strlen(path))
694 break;
695 entry = got_fileindex_entry_get(fileindex,
696 apply_path_prefix(worktree, path));
697 if (entry &&
698 memcmp(entry->commit_sha1, worktree->base_commit_id->sha1,
699 SHA1_DIGEST_LENGTH) == 0) {
700 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
701 progress_path);
702 break;
704 if (entry && memcmp(entry->blob_sha1, obj->id.sha1,
705 SHA1_DIGEST_LENGTH) == 0)
706 break;
707 err = got_object_blob_open(&blob, repo, obj, 8192);
708 if (err)
709 goto done;
710 err = blob_checkout(worktree, fileindex, entry, path, blob,
711 repo, progress_cb, progress_arg, progress_path);
712 break;
713 case GOT_OBJ_TYPE_TREE:
714 if (strlen(worktree->path_prefix) < strlen(path)) {
715 err = add_dir_on_disk(worktree, path);
716 if (err)
717 break;
719 err = got_object_tree_open(&tree, repo, obj);
720 if (err)
721 goto done;
722 /* XXX infinite recursion possible */
723 err = tree_checkout(worktree, fileindex, tree, path, repo,
724 progress_cb, progress_arg, cancel_cb, cancel_arg);
725 break;
726 default:
727 break;
730 done:
731 if (blob)
732 got_object_blob_close(blob);
733 if (tree)
734 got_object_tree_close(tree);
735 if (obj)
736 got_object_close(obj);
737 free(path);
738 return err;
741 struct collect_missing_entry_args {
742 struct got_fileindex *fileindex;
743 const struct got_tree_entries *entries;
744 struct got_pathset *missing_entries;
745 const char *current_subdir;
746 };
748 static const struct got_error *
749 collect_missing_file(void *args, struct got_fileindex_entry *entry)
751 struct collect_missing_entry_args *a = args;
752 char *start, *end;
753 ptrdiff_t len;
754 struct got_tree_entry *te;
755 int found = 0;
757 if (a->current_subdir[0] != '\0' &&
758 strncmp(a->current_subdir, entry->path,
759 strlen(a->current_subdir)) != 0)
760 return NULL;
762 start = entry->path + strlen(a->current_subdir);
763 while (start[0] == '/')
764 start++;
765 end = strchr(start, '/');
766 if (end == NULL) {
767 end = strchr(start, '\0');
768 if (end == NULL)
769 return got_error(GOT_ERR_BAD_PATH);
771 len = end - start;
773 SIMPLEQ_FOREACH(te, &a->entries->head, entry) {
774 if (strncmp(start, te->name, len) == 0 &&
775 te->name[len] == '\0') {
776 found = 1;
777 break;
781 if (found)
782 return NULL;
784 return got_pathset_add(a->missing_entries, entry->path, entry);
787 struct remove_missing_file_args {
788 const char *root_path;
789 struct got_fileindex *fileindex;
790 got_worktree_checkout_cb progress_cb;
791 void *progress_arg;
792 got_worktree_cancel_cb cancel_cb;
793 void *cancel_arg;
794 };
796 static const struct got_error *
797 remove_missing_file(const char *path, void *data, void *arg)
799 const struct got_error *err = NULL;
800 char *ondisk_path = NULL;
801 struct remove_missing_file_args *a = arg;
802 struct got_fileindex_entry *entry = data;
804 if (a->cancel_cb) {
805 err = (*a->cancel_cb)(a->cancel_arg);
806 if (err)
807 return err;
810 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE, path);
812 if (asprintf(&ondisk_path, "%s/%s", a->root_path, path) == -1)
813 return got_error_from_errno();
815 if (unlink(ondisk_path) == -1)
816 err = got_error_from_errno();
817 else {
818 char *parent = dirname(ondisk_path);
819 while (parent && strcmp(parent, a->root_path) != 0) {
820 if (rmdir(parent) == -1) {
821 if (errno != ENOTEMPTY)
822 err = got_error_from_errno();
823 break;
825 parent = dirname(parent);
828 free(ondisk_path);
830 if (err == NULL) {
831 got_fileindex_entry_remove(a->fileindex, entry);
832 got_fileindex_entry_free(entry);
834 return err;
837 /* Remove files which exist in the file index but not in the tree. */
838 static const struct got_error *
839 remove_missing_files(struct got_worktree *worktree, const char *path,
840 struct got_fileindex *fileindex, const struct got_tree_entries *entries,
841 got_worktree_checkout_cb progress_cb, void *progress_arg,
842 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
844 const struct got_error *err = NULL;
845 struct collect_missing_entry_args a;
846 struct remove_missing_file_args a2;
848 a.fileindex = fileindex;
849 a.entries = entries;
850 a.missing_entries = got_pathset_alloc();
851 if (a.missing_entries == NULL)
852 return got_error_from_errno();
853 a.current_subdir = apply_path_prefix(worktree, path);
854 err = got_fileindex_for_each_entry_safe(fileindex,
855 collect_missing_file, &a);
856 if (err)
857 return err;
859 a2.root_path = worktree->root_path;
860 a2.fileindex = fileindex;
861 a2.cancel_cb = cancel_cb;
862 a2.cancel_arg = cancel_arg;
863 a2.progress_cb = progress_cb;
864 a2.progress_arg = progress_arg;
865 err = got_pathset_for_each_safe(a.missing_entries, remove_missing_file,
866 &a2);
867 got_pathset_free(a.missing_entries);
868 return err;
871 static const struct got_error *
872 tree_checkout(struct got_worktree *worktree,
873 struct got_fileindex *fileindex, struct got_tree_object *tree,
874 const char *path, struct got_repository *repo,
875 got_worktree_checkout_cb progress_cb, void *progress_arg,
876 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
878 const struct got_error *err = NULL;
879 const struct got_tree_entries *entries;
880 struct got_tree_entry *te;
881 size_t len;
883 /* Skip this tree if it shares no path components with the prefix. */
884 len = MIN(strlen(worktree->path_prefix), strlen(path));
885 if (strncmp(path, worktree->path_prefix, len) != 0)
886 return NULL;
888 entries = got_object_tree_get_entries(tree);
889 SIMPLEQ_FOREACH(te, &entries->head, entry) {
890 if (cancel_cb) {
891 err = (*cancel_cb)(cancel_arg);
892 if (err)
893 return err;
895 err = tree_checkout_entry(worktree, fileindex, te, path, repo,
896 progress_cb, progress_arg, cancel_cb, cancel_arg);
897 if (err)
898 return err;
901 len = strlen(worktree->path_prefix);
902 if (strncmp(worktree->path_prefix, path, len) == 0) {
903 err = remove_missing_files(worktree, path, fileindex, entries,
904 progress_cb, progress_arg, cancel_cb, cancel_arg);
905 if (err)
906 return err;
909 return err;
912 const struct got_error *
913 got_worktree_checkout_files(struct got_worktree *worktree,
914 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
915 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
917 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
918 struct got_commit_object *commit = NULL;
919 struct got_tree_object *tree = NULL;
920 char *fileindex_path = NULL, *new_fileindex_path = NULL;
921 struct got_fileindex *fileindex = NULL;
922 FILE *index = NULL, *new_index = NULL;
924 err = lock_worktree(worktree, LOCK_EX);
925 if (err)
926 return err;
928 fileindex = got_fileindex_alloc();
929 if (fileindex == NULL) {
930 err = got_error_from_errno();
931 goto done;
934 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
935 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
936 err = got_error_from_errno();
937 fileindex_path = NULL;
938 goto done;
941 /*
942 * Read the file index.
943 * Checking out files is supposed to be an idempotent operation.
944 * If the on-disk file index is incomplete we will try to complete it.
945 */
946 index = fopen(fileindex_path, "rb");
947 if (index == NULL) {
948 err = got_error_from_errno();
949 goto done;
951 err = got_fileindex_read(fileindex, index);
952 fclose(index);
953 if (err)
954 goto done;
956 err = got_opentemp_named(&new_fileindex_path, &new_index,
957 fileindex_path);
958 if (err)
959 goto done;
961 err = got_object_open_as_commit(&commit, repo,
962 worktree->base_commit_id);
963 if (err)
964 goto done;
966 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
967 if (err)
968 goto done;
970 checkout_err = tree_checkout(worktree, fileindex, tree, "/", repo,
971 progress_cb, progress_arg, cancel_cb, cancel_arg);
973 /* Try to sync the fileindex back to disk in any case. */
974 err = got_fileindex_write(fileindex, new_index);
975 if (err)
976 goto done;
978 if (rename(new_fileindex_path, fileindex_path) != 0) {
979 err = got_error_from_errno();
980 goto done;
983 free(new_fileindex_path);
984 new_fileindex_path = NULL;
986 done:
987 if (tree)
988 got_object_tree_close(tree);
989 if (commit)
990 got_object_commit_close(commit);
991 if (new_fileindex_path)
992 unlink(new_fileindex_path);
993 if (new_index)
994 fclose(new_index);
995 free(new_fileindex_path);
996 free(fileindex_path);
997 got_fileindex_free(fileindex);
998 if (checkout_err)
999 err = checkout_err;
1000 unlockerr = lock_worktree(worktree, LOCK_SH);
1001 if (unlockerr && err == NULL)
1002 err = unlockerr;
1003 return err;