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_repo_path(struct got_worktree *worktree)
428 return worktree->repo_path;
431 const char *
432 got_worktree_get_path_prefix(struct got_worktree *worktree)
434 return worktree->path_prefix;
437 const struct got_error *
438 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
439 const char *path_prefix)
441 char *absprefix = NULL;
443 if (!got_path_is_absolute(path_prefix)) {
444 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
445 return got_error_from_errno();
447 *match = (strcmp(absprefix ? absprefix : path_prefix,
448 worktree->path_prefix) == 0);
449 free(absprefix);
450 return NULL;
453 char *
454 got_worktree_get_head_ref_name(struct got_worktree *worktree)
456 return got_ref_to_str(worktree->head_ref);
459 struct got_reference *
460 got_worktree_get_head_ref(struct got_worktree *worktree)
462 return got_ref_dup(worktree->head_ref);
465 const struct got_object_id *
466 got_worktree_get_base_commit_id(struct got_worktree *worktree)
468 return worktree->base_commit_id;
471 const struct got_error *
472 got_worktree_set_base_commit_id(struct got_worktree *worktree,
473 struct got_repository *repo, struct got_object_id *commit_id)
475 const struct got_error *err;
476 struct got_object *obj = NULL;
477 char *id_str = NULL;
478 char *path_got = NULL;
480 if (asprintf(&path_got, "%s/%s", worktree->root_path,
481 GOT_WORKTREE_GOT_DIR) == -1) {
482 err = got_error_from_errno();
483 path_got = NULL;
484 goto done;
487 err = got_object_open(&obj, repo, commit_id);
488 if (err)
489 return err;
491 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
492 err = got_error(GOT_ERR_OBJ_TYPE);
493 goto done;
496 /* Record our base commit. */
497 err = got_object_id_str(&id_str, commit_id);
498 if (err)
499 goto done;
500 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
501 if (err)
502 goto done;
504 free(worktree->base_commit_id);
505 worktree->base_commit_id = got_object_id_dup(commit_id);
506 if (worktree->base_commit_id == NULL) {
507 err = got_error_from_errno();
508 goto done;
510 done:
511 if (obj)
512 got_object_close(obj);
513 free(id_str);
514 free(path_got);
515 return err;
518 static const struct got_error *
519 lock_worktree(struct got_worktree *worktree, int operation)
521 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
522 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
523 : got_error_from_errno());
524 return NULL;
527 static const struct got_error *
528 make_parent_dirs(const char *abspath)
530 const struct got_error *err = NULL;
532 char *parent = dirname(abspath);
533 if (parent == NULL)
534 return NULL;
536 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
537 if (errno == ENOENT) {
538 err = make_parent_dirs(parent);
539 if (err)
540 return err;
541 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1)
542 return got_error_from_errno();
543 } else
544 err = got_error_from_errno();
547 return err;
550 static const struct got_error *
551 add_dir_on_disk(struct got_worktree *worktree, const char *path)
553 const struct got_error *err = NULL;
554 char *abspath;
556 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
557 return got_error_from_errno();
559 /* XXX queue work rather than editing disk directly? */
560 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
561 struct stat sb;
563 if (errno == EEXIST) {
564 if (lstat(abspath, &sb) == -1) {
565 err = got_error_from_errno();
566 goto done;
569 if (!S_ISDIR(sb.st_mode)) {
570 /* TODO directory is obstructed; do something */
571 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
572 goto done;
575 return NULL;
576 } else if (errno == ENOENT) {
577 err = make_parent_dirs(abspath);
578 if (err)
579 goto done;
580 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1)
581 err = got_error_from_errno();
582 } else
583 err = got_error_from_errno();
586 done:
587 free(abspath);
588 return err;
591 static const struct got_error *
592 install_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
593 struct got_fileindex_entry *entry, const char *path,
594 struct got_blob_object *blob,
595 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
596 void *progress_arg)
598 const struct got_error *err = NULL;
599 char *ondisk_path;
600 int fd = -1;
601 size_t len, hdrlen;
602 int update = 0;
603 char *tmppath = NULL;
605 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
606 return got_error_from_errno();
608 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
609 GOT_DEFAULT_FILE_MODE);
610 if (fd == -1) {
611 if (errno == ENOENT) {
612 char *parent = dirname(path);
613 if (parent == NULL)
614 return got_error_from_errno();
615 err = add_dir_on_disk(worktree, parent);
616 if (err)
617 return err;
618 fd = open(ondisk_path,
619 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
620 GOT_DEFAULT_FILE_MODE);
621 if (fd == -1)
622 return got_error_from_errno();
623 } else if (errno == EEXIST) {
624 struct stat sb;
625 if (lstat(ondisk_path, &sb) == -1) {
626 err = got_error_from_errno();
627 goto done;
628 } else if (!S_ISREG(sb.st_mode)) {
629 /* TODO file is obstructed; do something */
630 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
631 goto done;
632 } else {
633 err = got_opentemp_named_fd(&tmppath, &fd,
634 ondisk_path);
635 if (err)
636 goto done;
637 update = 1;
639 } else
640 return got_error_from_errno();
643 (*progress_cb)(progress_arg,
644 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
646 hdrlen = got_object_blob_get_hdrlen(blob);
647 do {
648 const uint8_t *buf = got_object_blob_get_read_buf(blob);
649 err = got_object_blob_read_block(&len, blob);
650 if (err)
651 break;
652 if (len > 0) {
653 /* Skip blob object header first time around. */
654 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
655 if (outlen == -1) {
656 err = got_error_from_errno();
657 goto done;
658 } else if (outlen != len - hdrlen) {
659 err = got_error(GOT_ERR_IO);
660 goto done;
662 hdrlen = 0;
664 } while (len != 0);
666 fsync(fd);
668 if (update) {
669 if (rename(tmppath, ondisk_path) != 0) {
670 err = got_error_from_errno();
671 goto done;
675 if (entry == NULL)
676 entry = got_fileindex_entry_get(fileindex, path);
677 if (entry)
678 err = got_fileindex_entry_update(entry, ondisk_path,
679 blob->id.sha1, worktree->base_commit_id->sha1);
680 else {
681 err = got_fileindex_entry_alloc(&entry, ondisk_path,
682 path, blob->id.sha1, worktree->base_commit_id->sha1);
683 if (err)
684 goto done;
685 err = got_fileindex_entry_add(fileindex, entry);
687 done:
688 if (fd != -1)
689 close(fd);
690 free(ondisk_path);
691 free(tmppath);
692 return err;
695 static const struct got_error *
696 update_blob(struct got_worktree *worktree,
697 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
698 struct got_tree_entry *te, const char *path,
699 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
700 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
702 const struct got_error *err = NULL;
703 struct got_blob_object *blob = NULL;
705 if (ie) {
706 if (memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
707 SHA1_DIGEST_LENGTH) == 0) {
708 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
709 path);
710 return NULL;
712 if (memcmp(ie->blob_sha1,
713 te->id->sha1, SHA1_DIGEST_LENGTH) == 0)
714 return NULL;
717 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
718 if (err)
719 return err;
721 err = install_blob(worktree, fileindex, ie, path, blob, repo,
722 progress_cb, progress_arg);
723 got_object_blob_close(blob);
724 return err;
727 static const struct got_error *
728 remove_ondisk_file(const char *root_path, const char *path)
730 const struct got_error *err = NULL;
731 char *ondisk_path = NULL;
733 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
734 return got_error_from_errno();
736 if (unlink(ondisk_path) == -1) {
737 if (errno != ENOENT)
738 err = got_error_from_errno();
739 } else {
740 char *parent = dirname(ondisk_path);
741 while (parent && strcmp(parent, root_path) != 0) {
742 if (rmdir(parent) == -1) {
743 if (errno != ENOTEMPTY)
744 err = got_error_from_errno();
745 break;
747 parent = dirname(parent);
750 free(ondisk_path);
751 return err;
754 struct diff_cb_arg {
755 struct got_fileindex *fileindex;
756 struct got_worktree *worktree;
757 struct got_repository *repo;
758 got_worktree_checkout_cb progress_cb;
759 void *progress_arg;
760 got_worktree_cancel_cb cancel_cb;
761 void *cancel_arg;
762 };
764 static const struct got_error *
765 diff_old_new(void *arg, struct got_fileindex_entry *ie,
766 struct got_tree_entry *te, const char *parent_path)
768 struct diff_cb_arg *a = arg;
770 return update_blob(a->worktree, a->fileindex, ie, te,
771 ie->path, a->repo, a->progress_cb, a->progress_arg,
772 a->cancel_cb, a->cancel_arg);
775 static const struct got_error *
776 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
778 const struct got_error *err;
779 struct diff_cb_arg *a = arg;
781 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE, ie->path);
783 err = remove_ondisk_file(a->worktree->root_path, ie->path);
784 if (err)
785 return err;
786 got_fileindex_entry_remove(a->fileindex, ie);
787 return NULL;
790 static const struct got_error *
791 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
793 struct diff_cb_arg *a = arg;
794 const struct got_error *err;
795 char *path;
797 if (asprintf(&path, "%s%s%s", parent_path,
798 parent_path[0] ? "/" : "", te->name)
799 == -1)
800 return got_error_from_errno();
802 if (S_ISDIR(te->mode))
803 err = add_dir_on_disk(a->worktree, path);
804 else
805 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
806 a->repo, a->progress_cb, a->progress_arg,
807 a->cancel_cb, a->cancel_arg);
809 free(path);
810 return err;
813 const struct got_error *
814 got_worktree_checkout_files(struct got_worktree *worktree,
815 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
816 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
818 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
819 struct got_commit_object *commit = NULL;
820 struct got_object_id *tree_id = NULL;
821 struct got_tree_object *tree = NULL;
822 char *fileindex_path = NULL, *new_fileindex_path = NULL;
823 struct got_fileindex *fileindex = NULL;
824 FILE *index = NULL, *new_index = NULL;
825 struct got_fileindex_diff_tree_cb diff_cb;
826 struct diff_cb_arg arg;
828 err = lock_worktree(worktree, LOCK_EX);
829 if (err)
830 return err;
832 fileindex = got_fileindex_alloc();
833 if (fileindex == NULL) {
834 err = got_error_from_errno();
835 goto done;
838 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
839 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
840 err = got_error_from_errno();
841 fileindex_path = NULL;
842 goto done;
845 /*
846 * Read the file index.
847 * Checking out files is supposed to be an idempotent operation.
848 * If the on-disk file index is incomplete we will try to complete it.
849 */
850 index = fopen(fileindex_path, "rb");
851 if (index == NULL) {
852 if (errno != ENOENT) {
853 err = got_error_from_errno();
854 goto done;
856 } else {
857 err = got_fileindex_read(fileindex, index);
858 fclose(index);
859 if (err)
860 goto done;
863 err = got_opentemp_named(&new_fileindex_path, &new_index,
864 fileindex_path);
865 if (err)
866 goto done;
868 err = got_object_open_as_commit(&commit, repo,
869 worktree->base_commit_id);
870 if (err)
871 goto done;
873 err = got_object_id_by_path(&tree_id, repo,
874 worktree->base_commit_id, worktree->path_prefix);
875 if (err)
876 goto done;
878 err = got_object_open_as_tree(&tree, repo, tree_id);
879 if (err)
880 goto done;
882 diff_cb.diff_old_new = diff_old_new;
883 diff_cb.diff_old = diff_old;
884 diff_cb.diff_new = diff_new;
885 arg.fileindex = fileindex;
886 arg.worktree = worktree;
887 arg.repo = repo;
888 arg.progress_cb = progress_cb;
889 arg.progress_arg = progress_arg;
890 arg.cancel_cb = cancel_cb;
891 arg.cancel_arg = cancel_arg;
892 checkout_err = got_fileindex_diff_tree(fileindex, tree, repo,
893 &diff_cb, &arg);
895 /* Try to sync the fileindex back to disk in any case. */
896 err = got_fileindex_write(fileindex, new_index);
897 if (err)
898 goto done;
900 if (rename(new_fileindex_path, fileindex_path) != 0) {
901 err = got_error_from_errno();
902 goto done;
905 free(new_fileindex_path);
906 new_fileindex_path = NULL;
908 done:
909 if (tree)
910 got_object_tree_close(tree);
911 if (commit)
912 got_object_commit_close(commit);
913 if (new_fileindex_path)
914 unlink(new_fileindex_path);
915 if (new_index)
916 fclose(new_index);
917 free(new_fileindex_path);
918 free(fileindex_path);
919 got_fileindex_free(fileindex);
920 if (checkout_err)
921 err = checkout_err;
922 unlockerr = lock_worktree(worktree, LOCK_SH);
923 if (unlockerr && err == NULL)
924 err = unlockerr;
925 return err;
928 struct diff_dir_cb_arg {
929 struct got_fileindex *fileindex;
930 struct got_worktree *worktree;
931 struct got_repository *repo;
932 got_worktree_status_cb status_cb;
933 void *status_arg;
934 got_worktree_cancel_cb cancel_cb;
935 void *cancel_arg;
936 };
938 static const struct got_error *
939 get_file_status(unsigned char *status, struct got_fileindex_entry *ie,
940 const char *abspath, struct got_repository *repo)
942 const struct got_error *err = NULL;
943 struct got_object_id id;
944 size_t hdrlen;
945 FILE *f = NULL;
946 uint8_t fbuf[8192];
947 struct got_blob_object *blob = NULL;
948 size_t flen, blen;
949 struct stat sb;
951 *status = GOT_STATUS_NO_CHANGE;
953 if (lstat(abspath, &sb) == -1)
954 return got_error_from_errno();
956 if (!S_ISREG(sb.st_mode)) {
957 *status = GOT_STATUS_OBSTRUCTED;
958 return NULL;
961 if (ie->ctime_sec == sb.st_ctime &&
962 ie->ctime_nsec == sb.st_ctimensec &&
963 ie->mtime_sec == sb.st_mtime &&
964 ie->mtime_sec == sb.st_mtime &&
965 ie->mtime_nsec == sb.st_mtimensec &&
966 ie->size == (sb.st_size & 0xffffffff))
967 return NULL;
969 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
970 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
971 if (err)
972 return err;
974 f = fopen(abspath, "r");
975 if (f == NULL) {
976 err = got_error_from_errno();
977 goto done;
979 hdrlen = got_object_blob_get_hdrlen(blob);
980 while (1) {
981 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
982 err = got_object_blob_read_block(&blen, blob);
983 if (err)
984 break;
985 flen = fread(fbuf, 1, sizeof(fbuf), f);
986 if (blen == 0) {
987 if (flen != 0)
988 *status = GOT_STATUS_MODIFIY;
989 break;
990 } else if (flen == 0) {
991 if (blen != 0)
992 *status = GOT_STATUS_MODIFIY;
993 break;
994 } else if (blen == flen) {
995 /* Skip blob object header first time around. */
996 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
997 *status = GOT_STATUS_MODIFIY;
998 break;
1000 } else {
1001 *status = GOT_STATUS_MODIFIY;
1002 break;
1004 hdrlen = 0;
1006 done:
1007 if (blob)
1008 got_object_blob_close(blob);
1009 if (f)
1010 fclose(f);
1011 return err;
1014 static const struct got_error *
1015 status_old_new(void *arg, struct got_fileindex_entry *ie,
1016 struct dirent *de, const char *parent_path)
1018 const struct got_error *err = NULL;
1019 struct diff_dir_cb_arg *a = arg;
1020 char *abspath;
1021 unsigned char status = GOT_STATUS_NO_CHANGE;
1023 if (parent_path[0]) {
1024 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1025 parent_path, de->d_name) == -1)
1026 return got_error_from_errno();
1027 } else {
1028 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1029 de->d_name) == -1)
1030 return got_error_from_errno();
1033 err = get_file_status(&status, ie, abspath, a->repo);
1034 if (err == NULL && status != GOT_STATUS_NO_CHANGE)
1035 (*a->status_cb)(a->status_arg, status, ie->path);
1036 free(abspath);
1037 return err;
1040 static const struct got_error *
1041 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1043 struct diff_dir_cb_arg *a = arg;
1044 (*a->status_cb)(a->status_arg, GOT_STATUS_MISSING, ie->path);
1045 return NULL;
1048 static const struct got_error *
1049 status_new(void *arg, struct dirent *de, const char *parent_path)
1051 struct diff_dir_cb_arg *a = arg;
1052 char *path = NULL;
1054 if (de->d_type == DT_DIR)
1055 return NULL;
1057 if (parent_path[0]) {
1058 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1059 return got_error_from_errno();
1060 } else {
1061 path = de->d_name;
1064 (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path);
1065 if (parent_path[0])
1066 free(path);
1067 return NULL;
1070 const struct got_error *
1071 got_worktree_status(struct got_worktree *worktree,
1072 struct got_repository *repo, got_worktree_status_cb status_cb,
1073 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1075 const struct got_error *err = NULL;
1076 DIR *workdir = NULL;
1077 char *fileindex_path = NULL;
1078 struct got_fileindex *fileindex = NULL;
1079 FILE *index = NULL;
1080 struct got_fileindex_diff_dir_cb fdiff_cb;
1081 struct diff_dir_cb_arg arg;
1083 fileindex = got_fileindex_alloc();
1084 if (fileindex == NULL) {
1085 err = got_error_from_errno();
1086 goto done;
1089 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1090 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1091 err = got_error_from_errno();
1092 fileindex_path = NULL;
1093 goto done;
1096 index = fopen(fileindex_path, "rb");
1097 if (index == NULL) {
1098 if (errno != ENOENT) {
1099 err = got_error_from_errno();
1100 goto done;
1102 } else {
1103 err = got_fileindex_read(fileindex, index);
1104 fclose(index);
1105 if (err)
1106 goto done;
1109 workdir = opendir(worktree->root_path);
1110 if (workdir == NULL) {
1111 err = got_error_from_errno();
1112 goto done;
1114 fdiff_cb.diff_old_new = status_old_new;
1115 fdiff_cb.diff_old = status_old;
1116 fdiff_cb.diff_new = status_new;
1117 arg.fileindex = fileindex;
1118 arg.worktree = worktree;
1119 arg.repo = repo;
1120 arg.status_cb = status_cb;
1121 arg.status_arg = status_arg;
1122 arg.cancel_cb = cancel_cb;
1123 arg.cancel_arg = cancel_arg;
1124 err = got_fileindex_diff_dir(fileindex, workdir, repo, &fdiff_cb, &arg);
1125 done:
1126 if (workdir)
1127 closedir(workdir);
1128 free(fileindex_path);
1129 got_fileindex_free(fileindex);
1130 return err;