Blob


1 /*
2 * Copyright (c) 2018, 2019 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>
20 #include <sys/tree.h>
22 #include <dirent.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <fcntl.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <sha1.h>
31 #include <zlib.h>
32 #include <fnmatch.h>
33 #include <libgen.h>
35 #include "got_error.h"
36 #include "got_repository.h"
37 #include "got_reference.h"
38 #include "got_object.h"
39 #include "got_worktree.h"
40 #include "got_opentemp.h"
42 #include "got_lib_worktree.h"
43 #include "got_lib_path.h"
44 #include "got_lib_sha1.h"
45 #include "got_lib_fileindex.h"
46 #include "got_lib_inflate.h"
47 #include "got_lib_delta.h"
48 #include "got_lib_object.h"
50 #ifndef MIN
51 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
52 #endif
54 static const struct got_error *
55 create_meta_file(const char *path_got, const char *name, const char *content)
56 {
57 const struct got_error *err = NULL;
58 char *path;
59 int fd = -1;
61 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
62 err = got_error_from_errno();
63 path = NULL;
64 goto done;
65 }
67 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
68 GOT_DEFAULT_FILE_MODE);
69 if (fd == -1) {
70 err = got_error_from_errno();
71 goto done;
72 }
74 if (content) {
75 int len = dprintf(fd, "%s\n", content);
76 if (len != strlen(content) + 1) {
77 err = got_error_from_errno();
78 goto done;
79 }
80 }
82 done:
83 if (fd != -1 && close(fd) == -1 && err == NULL)
84 err = got_error_from_errno();
85 free(path);
86 return err;
87 }
89 static const struct got_error *
90 update_meta_file(const char *path_got, const char *name, const char *content)
91 {
92 const struct got_error *err = NULL;
93 FILE *tmpfile = NULL;
94 char *tmppath = NULL;
95 char *path = NULL;
97 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
98 err = got_error_from_errno();
99 path = NULL;
100 goto done;
103 err = got_opentemp_named(&tmppath, &tmpfile, path);
104 if (err)
105 goto done;
107 if (content) {
108 int len = fprintf(tmpfile, "%s\n", content);
109 if (len != strlen(content) + 1) {
110 err = got_error_from_errno();
111 goto done;
115 if (rename(tmppath, path) != 0) {
116 err = got_error_from_errno();
117 goto done;
120 done:
121 free(tmppath);
122 fclose(tmpfile);
123 return err;
126 static const struct got_error *
127 read_meta_file(char **content, const char *path_got, const char *name)
129 const struct got_error *err = NULL;
130 char *path;
131 int fd = -1;
132 ssize_t n;
133 struct stat sb;
135 *content = NULL;
137 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
138 err = got_error_from_errno();
139 path = NULL;
140 goto done;
143 fd = open(path, O_RDONLY | O_NOFOLLOW);
144 if (fd == -1) {
145 err = got_error_from_errno();
146 goto done;
148 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
149 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
150 : got_error_from_errno());
151 goto done;
154 stat(path, &sb);
155 *content = calloc(1, sb.st_size);
156 if (*content == NULL) {
157 err = got_error_from_errno();
158 goto done;
161 n = read(fd, *content, sb.st_size);
162 if (n != sb.st_size) {
163 err = (n == -1 ? got_error_from_errno() :
164 got_error(GOT_ERR_WORKTREE_META));
165 goto done;
167 if ((*content)[sb.st_size - 1] != '\n') {
168 err = got_error(GOT_ERR_WORKTREE_META);
169 goto done;
171 (*content)[sb.st_size - 1] = '\0';
173 done:
174 if (fd != -1 && close(fd) == -1 && err == NULL)
175 err = got_error_from_errno();
176 free(path);
177 if (err) {
178 free(*content);
179 *content = NULL;
181 return err;
184 const struct got_error *
185 got_worktree_init(const char *path, struct got_reference *head_ref,
186 const char *prefix, struct got_repository *repo)
188 const struct got_error *err = NULL;
189 struct got_object_id *commit_id = NULL;
190 int obj_type;
191 char *path_got = NULL;
192 char *refstr = NULL;
193 char *formatstr = NULL;
194 char *absprefix = NULL;
195 char *basestr = NULL;
197 err = got_ref_resolve(&commit_id, repo, head_ref);
198 if (err)
199 return err;
200 err = got_object_get_type(&obj_type, repo, commit_id);
201 if (err)
202 return err;
203 if (obj_type != GOT_OBJ_TYPE_COMMIT)
204 return got_error(GOT_ERR_OBJ_TYPE);
206 if (!got_path_is_absolute(prefix)) {
207 if (asprintf(&absprefix, "/%s", prefix) == -1)
208 return got_error_from_errno();
211 /* Create top-level directory (may already exist). */
212 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
213 err = got_error_from_errno();
214 goto done;
217 /* Create .got directory (may already exist). */
218 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
219 err = got_error_from_errno();
220 goto done;
222 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
223 err = got_error_from_errno();
224 goto done;
227 /* Create an empty lock file. */
228 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
229 if (err)
230 goto done;
232 /* Create an empty file index. */
233 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
234 if (err)
235 goto done;
237 /* Write the HEAD reference. */
238 refstr = got_ref_to_str(head_ref);
239 if (refstr == NULL) {
240 err = got_error_from_errno();
241 goto done;
243 err = create_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
244 if (err)
245 goto done;
247 /* Record our base commit. */
248 err = got_object_id_str(&basestr, commit_id);
249 if (err)
250 goto done;
251 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
252 if (err)
253 goto done;
255 /* Store path to repository. */
256 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
257 got_repo_get_path(repo));
258 if (err)
259 goto done;
261 /* Store in-repository path prefix. */
262 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
263 absprefix ? absprefix : prefix);
264 if (err)
265 goto done;
267 /* Stamp work tree with format file. */
268 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
269 err = got_error_from_errno();
270 goto done;
272 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
273 if (err)
274 goto done;
276 done:
277 free(commit_id);
278 free(path_got);
279 free(formatstr);
280 free(refstr);
281 free(absprefix);
282 free(basestr);
283 return err;
286 static const struct got_error *
287 open_worktree(struct got_worktree **worktree, const char *path)
289 const struct got_error *err = NULL;
290 char *path_got;
291 char *formatstr = NULL;
292 char *path_lock = NULL;
293 char *base_commit_id_str = NULL;
294 char *head_ref_str = NULL;
295 int version, fd = -1;
296 const char *errstr;
297 struct got_repository *repo = NULL;
299 *worktree = NULL;
301 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
302 err = got_error_from_errno();
303 path_got = NULL;
304 goto done;
307 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
308 err = got_error_from_errno();
309 path_lock = NULL;
310 goto done;
313 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
314 if (fd == -1) {
315 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
316 : got_error_from_errno());
317 goto done;
320 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
321 if (err)
322 goto done;
324 version = strtonum(formatstr, 1, INT_MAX, &errstr);
325 if (errstr) {
326 err = got_error(GOT_ERR_WORKTREE_META);
327 goto done;
329 if (version != GOT_WORKTREE_FORMAT_VERSION) {
330 err = got_error(GOT_ERR_WORKTREE_VERS);
331 goto done;
334 *worktree = calloc(1, sizeof(**worktree));
335 if (*worktree == NULL) {
336 err = got_error_from_errno();
337 goto done;
339 (*worktree)->lockfd = -1;
341 (*worktree)->root_path = strdup(path);
342 if ((*worktree)->root_path == NULL) {
343 err = got_error_from_errno();
344 goto done;
346 err = read_meta_file(&(*worktree)->repo_path, path_got,
347 GOT_WORKTREE_REPOSITORY);
348 if (err)
349 goto done;
351 err = read_meta_file(&(*worktree)->path_prefix, path_got,
352 GOT_WORKTREE_PATH_PREFIX);
353 if (err)
354 goto done;
356 err = read_meta_file(&base_commit_id_str, path_got,
357 GOT_WORKTREE_BASE_COMMIT);
358 if (err)
359 goto done;
361 err = got_repo_open(&repo, (*worktree)->repo_path);
362 if (err)
363 goto done;
365 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
366 base_commit_id_str);
367 if (err)
368 goto done;
370 err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
371 if (err)
372 goto done;
374 err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
375 done:
376 if (repo)
377 got_repo_close(repo);
378 free(path_got);
379 free(path_lock);
380 free(head_ref_str);
381 free(base_commit_id_str);
382 if (err) {
383 if (fd != -1)
384 close(fd);
385 if (*worktree != NULL)
386 got_worktree_close(*worktree);
387 *worktree = NULL;
388 } else
389 (*worktree)->lockfd = fd;
391 return err;
394 const struct got_error *
395 got_worktree_open(struct got_worktree **worktree, const char *path)
397 const struct got_error *err = NULL;
399 do {
400 err = open_worktree(worktree, path);
401 if (err && (err->code != GOT_ERR_ERRNO && errno != ENOENT))
402 return err;
403 if (*worktree)
404 return NULL;
405 path = dirname(path);
406 } while (path && !(path[0] == '.' && path[1] == '\0'));
408 return got_error(GOT_ERR_NOT_WORKTREE);
411 void
412 got_worktree_close(struct got_worktree *worktree)
414 free(worktree->root_path);
415 free(worktree->repo_path);
416 free(worktree->path_prefix);
417 free(worktree->base_commit_id);
418 if (worktree->head_ref)
419 got_ref_close(worktree->head_ref);
420 if (worktree->lockfd != -1)
421 close(worktree->lockfd);
422 free(worktree);
425 const char *
426 got_worktree_get_root_path(struct got_worktree *worktree)
428 return worktree->root_path;
431 const char *
432 got_worktree_get_repo_path(struct got_worktree *worktree)
434 return worktree->repo_path;
437 const char *
438 got_worktree_get_path_prefix(struct got_worktree *worktree)
440 return worktree->path_prefix;
443 const struct got_error *
444 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
445 const char *path_prefix)
447 char *absprefix = NULL;
449 if (!got_path_is_absolute(path_prefix)) {
450 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
451 return got_error_from_errno();
453 *match = (strcmp(absprefix ? absprefix : path_prefix,
454 worktree->path_prefix) == 0);
455 free(absprefix);
456 return NULL;
459 char *
460 got_worktree_get_head_ref_name(struct got_worktree *worktree)
462 return got_ref_to_str(worktree->head_ref);
465 struct got_reference *
466 got_worktree_get_head_ref(struct got_worktree *worktree)
468 return got_ref_dup(worktree->head_ref);
471 const struct got_object_id *
472 got_worktree_get_base_commit_id(struct got_worktree *worktree)
474 return worktree->base_commit_id;
477 const struct got_error *
478 got_worktree_set_base_commit_id(struct got_worktree *worktree,
479 struct got_repository *repo, struct got_object_id *commit_id)
481 const struct got_error *err;
482 struct got_object *obj = NULL;
483 char *id_str = NULL;
484 char *path_got = NULL;
486 if (asprintf(&path_got, "%s/%s", worktree->root_path,
487 GOT_WORKTREE_GOT_DIR) == -1) {
488 err = got_error_from_errno();
489 path_got = NULL;
490 goto done;
493 err = got_object_open(&obj, repo, commit_id);
494 if (err)
495 return err;
497 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
498 err = got_error(GOT_ERR_OBJ_TYPE);
499 goto done;
502 /* Record our base commit. */
503 err = got_object_id_str(&id_str, commit_id);
504 if (err)
505 goto done;
506 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
507 if (err)
508 goto done;
510 free(worktree->base_commit_id);
511 worktree->base_commit_id = got_object_id_dup(commit_id);
512 if (worktree->base_commit_id == NULL) {
513 err = got_error_from_errno();
514 goto done;
516 done:
517 if (obj)
518 got_object_close(obj);
519 free(id_str);
520 free(path_got);
521 return err;
524 static const struct got_error *
525 lock_worktree(struct got_worktree *worktree, int operation)
527 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
528 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
529 : got_error_from_errno());
530 return NULL;
533 static const struct got_error *
534 make_parent_dirs(const char *abspath)
536 const struct got_error *err = NULL;
538 char *parent = dirname(abspath);
539 if (parent == NULL)
540 return NULL;
542 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
543 if (errno == ENOENT) {
544 err = make_parent_dirs(parent);
545 if (err)
546 return err;
547 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1)
548 return got_error_from_errno();
549 } else
550 err = got_error_from_errno();
553 return err;
556 static const struct got_error *
557 add_dir_on_disk(struct got_worktree *worktree, const char *path)
559 const struct got_error *err = NULL;
560 char *abspath;
562 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
563 return got_error_from_errno();
565 /* XXX queue work rather than editing disk directly? */
566 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
567 struct stat sb;
569 if (errno == EEXIST) {
570 if (lstat(abspath, &sb) == -1) {
571 err = got_error_from_errno();
572 goto done;
575 if (!S_ISDIR(sb.st_mode)) {
576 /* TODO directory is obstructed; do something */
577 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
578 goto done;
581 return NULL;
582 } else if (errno == ENOENT) {
583 err = make_parent_dirs(abspath);
584 if (err)
585 goto done;
586 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1)
587 err = got_error_from_errno();
588 } else
589 err = got_error_from_errno();
592 done:
593 free(abspath);
594 return err;
597 static const struct got_error *
598 install_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
599 struct got_fileindex_entry *entry, const char *path,
600 struct got_blob_object *blob,
601 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
602 void *progress_arg)
604 const struct got_error *err = NULL;
605 char *ondisk_path;
606 int fd = -1;
607 size_t len, hdrlen;
608 int update = 0;
609 char *tmppath = NULL;
611 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
612 return got_error_from_errno();
614 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
615 GOT_DEFAULT_FILE_MODE);
616 if (fd == -1) {
617 if (errno == ENOENT) {
618 char *parent = dirname(path);
619 if (parent == NULL)
620 return got_error_from_errno();
621 err = add_dir_on_disk(worktree, parent);
622 if (err)
623 return err;
624 fd = open(ondisk_path,
625 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
626 GOT_DEFAULT_FILE_MODE);
627 if (fd == -1)
628 return got_error_from_errno();
629 } else if (errno == EEXIST) {
630 struct stat sb;
631 if (lstat(ondisk_path, &sb) == -1) {
632 err = got_error_from_errno();
633 goto done;
634 } else if (!S_ISREG(sb.st_mode)) {
635 /* TODO file is obstructed; do something */
636 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
637 goto done;
638 } else {
639 err = got_opentemp_named_fd(&tmppath, &fd,
640 ondisk_path);
641 if (err)
642 goto done;
643 update = 1;
645 } else
646 return got_error_from_errno();
649 (*progress_cb)(progress_arg,
650 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
652 hdrlen = got_object_blob_get_hdrlen(blob);
653 do {
654 const uint8_t *buf = got_object_blob_get_read_buf(blob);
655 err = got_object_blob_read_block(&len, blob);
656 if (err)
657 break;
658 if (len > 0) {
659 /* Skip blob object header first time around. */
660 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
661 if (outlen == -1) {
662 err = got_error_from_errno();
663 goto done;
664 } else if (outlen != len - hdrlen) {
665 err = got_error(GOT_ERR_IO);
666 goto done;
668 hdrlen = 0;
670 } while (len != 0);
672 fsync(fd);
674 if (update) {
675 if (rename(tmppath, ondisk_path) != 0) {
676 err = got_error_from_errno();
677 goto done;
681 if (entry == NULL)
682 entry = got_fileindex_entry_get(fileindex, path);
683 if (entry)
684 err = got_fileindex_entry_update(entry, ondisk_path,
685 blob->id.sha1, worktree->base_commit_id->sha1);
686 else {
687 err = got_fileindex_entry_alloc(&entry, ondisk_path,
688 path, blob->id.sha1, worktree->base_commit_id->sha1);
689 if (err)
690 goto done;
691 err = got_fileindex_entry_add(fileindex, entry);
693 done:
694 if (fd != -1)
695 close(fd);
696 free(ondisk_path);
697 free(tmppath);
698 return err;
701 static const struct got_error *
702 update_blob(struct got_worktree *worktree,
703 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
704 struct got_tree_entry *te, const char *path,
705 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
706 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
708 const struct got_error *err = NULL;
709 struct got_blob_object *blob = NULL;
711 if (ie) {
712 if (memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
713 SHA1_DIGEST_LENGTH) == 0) {
714 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
715 path);
716 return NULL;
718 if (memcmp(ie->blob_sha1,
719 te->id->sha1, SHA1_DIGEST_LENGTH) == 0)
720 return NULL;
723 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
724 if (err)
725 return err;
727 err = install_blob(worktree, fileindex, ie, path, blob, repo,
728 progress_cb, progress_arg);
729 got_object_blob_close(blob);
730 return err;
733 static const struct got_error *
734 remove_ondisk_file(const char *root_path, const char *path)
736 const struct got_error *err = NULL;
737 char *ondisk_path = NULL;
739 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
740 return got_error_from_errno();
742 if (unlink(ondisk_path) == -1) {
743 if (errno != ENOENT)
744 err = got_error_from_errno();
745 } else {
746 char *parent = dirname(ondisk_path);
747 while (parent && strcmp(parent, root_path) != 0) {
748 if (rmdir(parent) == -1) {
749 if (errno != ENOTEMPTY)
750 err = got_error_from_errno();
751 break;
753 parent = dirname(parent);
756 free(ondisk_path);
757 return err;
760 struct diff_cb_arg {
761 struct got_fileindex *fileindex;
762 struct got_worktree *worktree;
763 struct got_repository *repo;
764 got_worktree_checkout_cb progress_cb;
765 void *progress_arg;
766 got_worktree_cancel_cb cancel_cb;
767 void *cancel_arg;
768 };
770 static const struct got_error *
771 diff_old_new(void *arg, struct got_fileindex_entry *ie,
772 struct got_tree_entry *te, const char *parent_path)
774 struct diff_cb_arg *a = arg;
776 return update_blob(a->worktree, a->fileindex, ie, te,
777 ie->path, a->repo, a->progress_cb, a->progress_arg,
778 a->cancel_cb, a->cancel_arg);
781 static const struct got_error *
782 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
784 const struct got_error *err;
785 struct diff_cb_arg *a = arg;
787 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE, ie->path);
789 err = remove_ondisk_file(a->worktree->root_path, ie->path);
790 if (err)
791 return err;
792 got_fileindex_entry_remove(a->fileindex, ie);
793 return NULL;
796 static const struct got_error *
797 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
799 struct diff_cb_arg *a = arg;
800 const struct got_error *err;
801 char *path;
803 if (asprintf(&path, "%s%s%s", parent_path,
804 parent_path[0] ? "/" : "", te->name)
805 == -1)
806 return got_error_from_errno();
808 if (S_ISDIR(te->mode))
809 err = add_dir_on_disk(a->worktree, path);
810 else
811 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
812 a->repo, a->progress_cb, a->progress_arg,
813 a->cancel_cb, a->cancel_arg);
815 free(path);
816 return err;
819 const struct got_error *
820 got_worktree_checkout_files(struct got_worktree *worktree,
821 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
822 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
824 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
825 struct got_commit_object *commit = NULL;
826 struct got_object_id *tree_id = NULL;
827 struct got_tree_object *tree = NULL;
828 char *fileindex_path = NULL, *new_fileindex_path = NULL;
829 struct got_fileindex *fileindex = NULL;
830 FILE *index = NULL, *new_index = NULL;
831 struct got_fileindex_diff_tree_cb diff_cb;
832 struct diff_cb_arg arg;
834 err = lock_worktree(worktree, LOCK_EX);
835 if (err)
836 return err;
838 fileindex = got_fileindex_alloc();
839 if (fileindex == NULL) {
840 err = got_error_from_errno();
841 goto done;
844 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
845 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
846 err = got_error_from_errno();
847 fileindex_path = NULL;
848 goto done;
851 /*
852 * Read the file index.
853 * Checking out files is supposed to be an idempotent operation.
854 * If the on-disk file index is incomplete we will try to complete it.
855 */
856 index = fopen(fileindex_path, "rb");
857 if (index == NULL) {
858 if (errno != ENOENT) {
859 err = got_error_from_errno();
860 goto done;
862 } else {
863 err = got_fileindex_read(fileindex, index);
864 fclose(index);
865 if (err)
866 goto done;
869 err = got_opentemp_named(&new_fileindex_path, &new_index,
870 fileindex_path);
871 if (err)
872 goto done;
874 err = got_object_open_as_commit(&commit, repo,
875 worktree->base_commit_id);
876 if (err)
877 goto done;
879 err = got_object_id_by_path(&tree_id, repo,
880 worktree->base_commit_id, worktree->path_prefix);
881 if (err)
882 goto done;
884 err = got_object_open_as_tree(&tree, repo, tree_id);
885 if (err)
886 goto done;
888 diff_cb.diff_old_new = diff_old_new;
889 diff_cb.diff_old = diff_old;
890 diff_cb.diff_new = diff_new;
891 arg.fileindex = fileindex;
892 arg.worktree = worktree;
893 arg.repo = repo;
894 arg.progress_cb = progress_cb;
895 arg.progress_arg = progress_arg;
896 arg.cancel_cb = cancel_cb;
897 arg.cancel_arg = cancel_arg;
898 checkout_err = got_fileindex_diff_tree(fileindex, tree, repo,
899 &diff_cb, &arg);
901 /* Try to sync the fileindex back to disk in any case. */
902 err = got_fileindex_write(fileindex, new_index);
903 if (err)
904 goto done;
906 if (rename(new_fileindex_path, fileindex_path) != 0) {
907 err = got_error_from_errno();
908 goto done;
911 free(new_fileindex_path);
912 new_fileindex_path = NULL;
914 done:
915 if (tree)
916 got_object_tree_close(tree);
917 if (commit)
918 got_object_commit_close(commit);
919 if (new_fileindex_path)
920 unlink(new_fileindex_path);
921 if (new_index)
922 fclose(new_index);
923 free(new_fileindex_path);
924 free(fileindex_path);
925 got_fileindex_free(fileindex);
926 if (checkout_err)
927 err = checkout_err;
928 unlockerr = lock_worktree(worktree, LOCK_SH);
929 if (unlockerr && err == NULL)
930 err = unlockerr;
931 return err;
934 struct diff_dir_cb_arg {
935 struct got_fileindex *fileindex;
936 struct got_worktree *worktree;
937 struct got_repository *repo;
938 got_worktree_status_cb status_cb;
939 void *status_arg;
940 got_worktree_cancel_cb cancel_cb;
941 void *cancel_arg;
942 };
944 static const struct got_error *
945 get_file_status(unsigned char *status, struct got_fileindex_entry *ie,
946 const char *abspath, struct got_repository *repo)
948 const struct got_error *err = NULL;
949 struct got_object_id id;
950 size_t hdrlen;
951 FILE *f = NULL;
952 uint8_t fbuf[8192];
953 struct got_blob_object *blob = NULL;
954 size_t flen, blen;
955 struct stat sb;
957 *status = GOT_STATUS_NO_CHANGE;
959 if (lstat(abspath, &sb) == -1)
960 return got_error_from_errno();
962 if (!S_ISREG(sb.st_mode)) {
963 *status = GOT_STATUS_OBSTRUCTED;
964 return NULL;
967 if (ie->ctime_sec == sb.st_ctime &&
968 ie->ctime_nsec == sb.st_ctimensec &&
969 ie->mtime_sec == sb.st_mtime &&
970 ie->mtime_sec == sb.st_mtime &&
971 ie->mtime_nsec == sb.st_mtimensec &&
972 ie->size == (sb.st_size & 0xffffffff))
973 return NULL;
975 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
976 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
977 if (err)
978 return err;
980 f = fopen(abspath, "r");
981 if (f == NULL) {
982 err = got_error_from_errno();
983 goto done;
985 hdrlen = got_object_blob_get_hdrlen(blob);
986 while (1) {
987 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
988 err = got_object_blob_read_block(&blen, blob);
989 if (err)
990 break;
991 flen = fread(fbuf, 1, sizeof(fbuf), f);
992 if (blen == 0) {
993 if (flen != 0)
994 *status = GOT_STATUS_MODIFIY;
995 break;
996 } else if (flen == 0) {
997 if (blen != 0)
998 *status = GOT_STATUS_MODIFIY;
999 break;
1000 } else if (blen == flen) {
1001 /* Skip blob object header first time around. */
1002 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1003 *status = GOT_STATUS_MODIFIY;
1004 break;
1006 } else {
1007 *status = GOT_STATUS_MODIFIY;
1008 break;
1010 hdrlen = 0;
1012 done:
1013 if (blob)
1014 got_object_blob_close(blob);
1015 if (f)
1016 fclose(f);
1017 return err;
1020 static const struct got_error *
1021 status_old_new(void *arg, struct got_fileindex_entry *ie,
1022 struct dirent *de, const char *parent_path)
1024 const struct got_error *err = NULL;
1025 struct diff_dir_cb_arg *a = arg;
1026 char *abspath;
1027 unsigned char status = GOT_STATUS_NO_CHANGE;
1029 if (parent_path[0]) {
1030 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1031 parent_path, de->d_name) == -1)
1032 return got_error_from_errno();
1033 } else {
1034 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1035 de->d_name) == -1)
1036 return got_error_from_errno();
1039 err = get_file_status(&status, ie, abspath, a->repo);
1040 if (err == NULL && status != GOT_STATUS_NO_CHANGE)
1041 (*a->status_cb)(a->status_arg, status, ie->path);
1042 free(abspath);
1043 return err;
1046 static const struct got_error *
1047 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1049 struct diff_dir_cb_arg *a = arg;
1050 (*a->status_cb)(a->status_arg, GOT_STATUS_MISSING, ie->path);
1051 return NULL;
1054 static const struct got_error *
1055 status_new(void *arg, struct dirent *de, const char *parent_path)
1057 struct diff_dir_cb_arg *a = arg;
1058 char *path = NULL;
1060 if (de->d_type == DT_DIR)
1061 return NULL;
1063 if (parent_path[0]) {
1064 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1065 return got_error_from_errno();
1066 } else {
1067 path = de->d_name;
1070 (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path);
1071 if (parent_path[0])
1072 free(path);
1073 return NULL;
1076 const struct got_error *
1077 got_worktree_status(struct got_worktree *worktree,
1078 struct got_repository *repo, got_worktree_status_cb status_cb,
1079 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1081 const struct got_error *err = NULL;
1082 DIR *workdir = NULL;
1083 char *fileindex_path = NULL;
1084 struct got_fileindex *fileindex = NULL;
1085 FILE *index = NULL;
1086 struct got_fileindex_diff_dir_cb fdiff_cb;
1087 struct diff_dir_cb_arg arg;
1089 fileindex = got_fileindex_alloc();
1090 if (fileindex == NULL) {
1091 err = got_error_from_errno();
1092 goto done;
1095 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1096 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1097 err = got_error_from_errno();
1098 fileindex_path = NULL;
1099 goto done;
1102 index = fopen(fileindex_path, "rb");
1103 if (index == NULL) {
1104 if (errno != ENOENT) {
1105 err = got_error_from_errno();
1106 goto done;
1108 } else {
1109 err = got_fileindex_read(fileindex, index);
1110 fclose(index);
1111 if (err)
1112 goto done;
1115 workdir = opendir(worktree->root_path);
1116 if (workdir == NULL) {
1117 err = got_error_from_errno();
1118 goto done;
1120 fdiff_cb.diff_old_new = status_old_new;
1121 fdiff_cb.diff_old = status_old;
1122 fdiff_cb.diff_new = status_new;
1123 arg.fileindex = fileindex;
1124 arg.worktree = worktree;
1125 arg.repo = repo;
1126 arg.status_cb = status_cb;
1127 arg.status_arg = status_arg;
1128 arg.cancel_cb = cancel_cb;
1129 arg.cancel_arg = cancel_arg;
1130 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1131 repo, &fdiff_cb, &arg);
1132 done:
1133 if (workdir)
1134 closedir(workdir);
1135 free(fileindex_path);
1136 got_fileindex_free(fileindex);
1137 return err;