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 if (path == NULL)
407 return got_error_from_errno();
408 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
410 return got_error(GOT_ERR_NOT_WORKTREE);
413 void
414 got_worktree_close(struct got_worktree *worktree)
416 free(worktree->root_path);
417 free(worktree->repo_path);
418 free(worktree->path_prefix);
419 free(worktree->base_commit_id);
420 if (worktree->head_ref)
421 got_ref_close(worktree->head_ref);
422 if (worktree->lockfd != -1)
423 close(worktree->lockfd);
424 free(worktree);
427 const char *
428 got_worktree_get_root_path(struct got_worktree *worktree)
430 return worktree->root_path;
433 const char *
434 got_worktree_get_repo_path(struct got_worktree *worktree)
436 return worktree->repo_path;
439 const char *
440 got_worktree_get_path_prefix(struct got_worktree *worktree)
442 return worktree->path_prefix;
445 const struct got_error *
446 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
447 const char *path_prefix)
449 char *absprefix = NULL;
451 if (!got_path_is_absolute(path_prefix)) {
452 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
453 return got_error_from_errno();
455 *match = (strcmp(absprefix ? absprefix : path_prefix,
456 worktree->path_prefix) == 0);
457 free(absprefix);
458 return NULL;
461 char *
462 got_worktree_get_head_ref_name(struct got_worktree *worktree)
464 return got_ref_to_str(worktree->head_ref);
467 struct got_reference *
468 got_worktree_get_head_ref(struct got_worktree *worktree)
470 return got_ref_dup(worktree->head_ref);
473 struct got_object_id *
474 got_worktree_get_base_commit_id(struct got_worktree *worktree)
476 return worktree->base_commit_id;
479 const struct got_error *
480 got_worktree_set_base_commit_id(struct got_worktree *worktree,
481 struct got_repository *repo, struct got_object_id *commit_id)
483 const struct got_error *err;
484 struct got_object *obj = NULL;
485 char *id_str = NULL;
486 char *path_got = NULL;
488 if (asprintf(&path_got, "%s/%s", worktree->root_path,
489 GOT_WORKTREE_GOT_DIR) == -1) {
490 err = got_error_from_errno();
491 path_got = NULL;
492 goto done;
495 err = got_object_open(&obj, repo, commit_id);
496 if (err)
497 return err;
499 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
500 err = got_error(GOT_ERR_OBJ_TYPE);
501 goto done;
504 /* Record our base commit. */
505 err = got_object_id_str(&id_str, commit_id);
506 if (err)
507 goto done;
508 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
509 if (err)
510 goto done;
512 free(worktree->base_commit_id);
513 worktree->base_commit_id = got_object_id_dup(commit_id);
514 if (worktree->base_commit_id == NULL) {
515 err = got_error_from_errno();
516 goto done;
518 done:
519 if (obj)
520 got_object_close(obj);
521 free(id_str);
522 free(path_got);
523 return err;
526 static const struct got_error *
527 lock_worktree(struct got_worktree *worktree, int operation)
529 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
530 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
531 : got_error_from_errno());
532 return NULL;
535 static const struct got_error *
536 make_parent_dirs(const char *abspath)
538 const struct got_error *err = NULL;
540 char *parent = dirname(abspath);
541 if (parent == NULL)
542 return NULL;
544 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
545 if (errno == ENOENT) {
546 err = make_parent_dirs(parent);
547 if (err)
548 return err;
549 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1)
550 return got_error_from_errno();
551 } else
552 err = got_error_from_errno();
555 return err;
558 static const struct got_error *
559 add_dir_on_disk(struct got_worktree *worktree, const char *path)
561 const struct got_error *err = NULL;
562 char *abspath;
564 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
565 return got_error_from_errno();
567 /* XXX queue work rather than editing disk directly? */
568 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
569 struct stat sb;
571 if (errno == EEXIST) {
572 if (lstat(abspath, &sb) == -1) {
573 err = got_error_from_errno();
574 goto done;
577 if (!S_ISDIR(sb.st_mode)) {
578 /* TODO directory is obstructed; do something */
579 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
580 goto done;
583 return NULL;
584 } else if (errno == ENOENT) {
585 err = make_parent_dirs(abspath);
586 if (err)
587 goto done;
588 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1)
589 err = got_error_from_errno();
590 } else
591 err = got_error_from_errno();
594 done:
595 free(abspath);
596 return err;
599 static const struct got_error *
600 install_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
601 struct got_fileindex_entry *entry, const char *path,
602 struct got_blob_object *blob,
603 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
604 void *progress_arg)
606 const struct got_error *err = NULL;
607 char *ondisk_path;
608 int fd = -1;
609 size_t len, hdrlen;
610 int update = 0;
611 char *tmppath = NULL;
613 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
614 return got_error_from_errno();
616 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
617 GOT_DEFAULT_FILE_MODE);
618 if (fd == -1) {
619 if (errno == ENOENT) {
620 char *parent = dirname(path);
621 if (parent == NULL)
622 return got_error_from_errno();
623 err = add_dir_on_disk(worktree, parent);
624 if (err)
625 return err;
626 fd = open(ondisk_path,
627 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
628 GOT_DEFAULT_FILE_MODE);
629 if (fd == -1)
630 return got_error_from_errno();
631 } else if (errno == EEXIST) {
632 struct stat sb;
633 if (lstat(ondisk_path, &sb) == -1) {
634 err = got_error_from_errno();
635 goto done;
636 } else if (!S_ISREG(sb.st_mode)) {
637 /* TODO file is obstructed; do something */
638 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
639 goto done;
640 } else {
641 err = got_opentemp_named_fd(&tmppath, &fd,
642 ondisk_path);
643 if (err)
644 goto done;
645 update = 1;
647 } else
648 return got_error_from_errno();
651 (*progress_cb)(progress_arg,
652 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
654 hdrlen = got_object_blob_get_hdrlen(blob);
655 do {
656 const uint8_t *buf = got_object_blob_get_read_buf(blob);
657 err = got_object_blob_read_block(&len, blob);
658 if (err)
659 break;
660 if (len > 0) {
661 /* Skip blob object header first time around. */
662 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
663 if (outlen == -1) {
664 err = got_error_from_errno();
665 goto done;
666 } else if (outlen != len - hdrlen) {
667 err = got_error(GOT_ERR_IO);
668 goto done;
670 hdrlen = 0;
672 } while (len != 0);
674 fsync(fd);
676 if (update) {
677 if (rename(tmppath, ondisk_path) != 0) {
678 err = got_error_from_errno();
679 goto done;
683 if (entry == NULL)
684 entry = got_fileindex_entry_get(fileindex, path);
685 if (entry)
686 err = got_fileindex_entry_update(entry, ondisk_path,
687 blob->id.sha1, worktree->base_commit_id->sha1);
688 else {
689 err = got_fileindex_entry_alloc(&entry, ondisk_path,
690 path, blob->id.sha1, worktree->base_commit_id->sha1);
691 if (err)
692 goto done;
693 err = got_fileindex_entry_add(fileindex, entry);
695 done:
696 if (fd != -1)
697 close(fd);
698 free(ondisk_path);
699 free(tmppath);
700 return err;
703 static const struct got_error *
704 update_blob(struct got_worktree *worktree,
705 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
706 struct got_tree_entry *te, const char *path,
707 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
708 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
710 const struct got_error *err = NULL;
711 struct got_blob_object *blob = NULL;
713 if (ie) {
714 if (memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
715 SHA1_DIGEST_LENGTH) == 0) {
716 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
717 path);
718 return NULL;
720 if (memcmp(ie->blob_sha1,
721 te->id->sha1, SHA1_DIGEST_LENGTH) == 0)
722 return NULL;
725 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
726 if (err)
727 return err;
729 err = install_blob(worktree, fileindex, ie, path, blob, repo,
730 progress_cb, progress_arg);
731 got_object_blob_close(blob);
732 return err;
735 static const struct got_error *
736 remove_ondisk_file(const char *root_path, const char *path)
738 const struct got_error *err = NULL;
739 char *ondisk_path = NULL;
741 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
742 return got_error_from_errno();
744 if (unlink(ondisk_path) == -1) {
745 if (errno != ENOENT)
746 err = got_error_from_errno();
747 } else {
748 char *parent = dirname(ondisk_path);
749 while (parent && strcmp(parent, root_path) != 0) {
750 if (rmdir(parent) == -1) {
751 if (errno != ENOTEMPTY)
752 err = got_error_from_errno();
753 break;
755 parent = dirname(parent);
758 free(ondisk_path);
759 return err;
762 struct diff_cb_arg {
763 struct got_fileindex *fileindex;
764 struct got_worktree *worktree;
765 struct got_repository *repo;
766 got_worktree_checkout_cb progress_cb;
767 void *progress_arg;
768 got_worktree_cancel_cb cancel_cb;
769 void *cancel_arg;
770 };
772 static const struct got_error *
773 diff_old_new(void *arg, struct got_fileindex_entry *ie,
774 struct got_tree_entry *te, const char *parent_path)
776 struct diff_cb_arg *a = arg;
778 return update_blob(a->worktree, a->fileindex, ie, te,
779 ie->path, a->repo, a->progress_cb, a->progress_arg,
780 a->cancel_cb, a->cancel_arg);
783 static const struct got_error *
784 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
786 const struct got_error *err;
787 struct diff_cb_arg *a = arg;
789 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE, ie->path);
791 err = remove_ondisk_file(a->worktree->root_path, ie->path);
792 if (err)
793 return err;
794 got_fileindex_entry_remove(a->fileindex, ie);
795 return NULL;
798 static const struct got_error *
799 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
801 struct diff_cb_arg *a = arg;
802 const struct got_error *err;
803 char *path;
805 if (asprintf(&path, "%s%s%s", parent_path,
806 parent_path[0] ? "/" : "", te->name)
807 == -1)
808 return got_error_from_errno();
810 if (S_ISDIR(te->mode))
811 err = add_dir_on_disk(a->worktree, path);
812 else
813 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
814 a->repo, a->progress_cb, a->progress_arg,
815 a->cancel_cb, a->cancel_arg);
817 free(path);
818 return err;
821 const struct got_error *
822 got_worktree_checkout_files(struct got_worktree *worktree,
823 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
824 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
826 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
827 struct got_commit_object *commit = NULL;
828 struct got_object_id *tree_id = NULL;
829 struct got_tree_object *tree = NULL;
830 char *fileindex_path = NULL, *new_fileindex_path = NULL;
831 struct got_fileindex *fileindex = NULL;
832 FILE *index = NULL, *new_index = NULL;
833 struct got_fileindex_diff_tree_cb diff_cb;
834 struct diff_cb_arg arg;
836 err = lock_worktree(worktree, LOCK_EX);
837 if (err)
838 return err;
840 fileindex = got_fileindex_alloc();
841 if (fileindex == NULL) {
842 err = got_error_from_errno();
843 goto done;
846 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
847 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
848 err = got_error_from_errno();
849 fileindex_path = NULL;
850 goto done;
853 /*
854 * Read the file index.
855 * Checking out files is supposed to be an idempotent operation.
856 * If the on-disk file index is incomplete we will try to complete it.
857 */
858 index = fopen(fileindex_path, "rb");
859 if (index == NULL) {
860 if (errno != ENOENT) {
861 err = got_error_from_errno();
862 goto done;
864 } else {
865 err = got_fileindex_read(fileindex, index);
866 fclose(index);
867 if (err)
868 goto done;
871 err = got_opentemp_named(&new_fileindex_path, &new_index,
872 fileindex_path);
873 if (err)
874 goto done;
876 err = got_object_open_as_commit(&commit, repo,
877 worktree->base_commit_id);
878 if (err)
879 goto done;
881 err = got_object_id_by_path(&tree_id, repo,
882 worktree->base_commit_id, worktree->path_prefix);
883 if (err)
884 goto done;
886 err = got_object_open_as_tree(&tree, repo, tree_id);
887 if (err)
888 goto done;
890 diff_cb.diff_old_new = diff_old_new;
891 diff_cb.diff_old = diff_old;
892 diff_cb.diff_new = diff_new;
893 arg.fileindex = fileindex;
894 arg.worktree = worktree;
895 arg.repo = repo;
896 arg.progress_cb = progress_cb;
897 arg.progress_arg = progress_arg;
898 arg.cancel_cb = cancel_cb;
899 arg.cancel_arg = cancel_arg;
900 checkout_err = got_fileindex_diff_tree(fileindex, tree, repo,
901 &diff_cb, &arg);
903 /* Try to sync the fileindex back to disk in any case. */
904 err = got_fileindex_write(fileindex, new_index);
905 if (err)
906 goto done;
908 if (rename(new_fileindex_path, fileindex_path) != 0) {
909 err = got_error_from_errno();
910 goto done;
913 free(new_fileindex_path);
914 new_fileindex_path = NULL;
916 done:
917 if (tree)
918 got_object_tree_close(tree);
919 if (commit)
920 got_object_commit_close(commit);
921 if (new_fileindex_path)
922 unlink(new_fileindex_path);
923 if (new_index)
924 fclose(new_index);
925 free(new_fileindex_path);
926 free(fileindex_path);
927 got_fileindex_free(fileindex);
928 if (checkout_err)
929 err = checkout_err;
930 unlockerr = lock_worktree(worktree, LOCK_SH);
931 if (unlockerr && err == NULL)
932 err = unlockerr;
933 return err;
936 struct diff_dir_cb_arg {
937 struct got_fileindex *fileindex;
938 struct got_worktree *worktree;
939 struct got_repository *repo;
940 got_worktree_status_cb status_cb;
941 void *status_arg;
942 got_worktree_cancel_cb cancel_cb;
943 void *cancel_arg;
944 };
946 static const struct got_error *
947 get_file_status(unsigned char *status, struct got_fileindex_entry *ie,
948 const char *abspath, struct got_repository *repo)
950 const struct got_error *err = NULL;
951 struct got_object_id id;
952 size_t hdrlen;
953 FILE *f = NULL;
954 uint8_t fbuf[8192];
955 struct got_blob_object *blob = NULL;
956 size_t flen, blen;
957 struct stat sb;
959 *status = GOT_STATUS_NO_CHANGE;
961 if (lstat(abspath, &sb) == -1)
962 return got_error_from_errno();
964 if (!S_ISREG(sb.st_mode)) {
965 *status = GOT_STATUS_OBSTRUCTED;
966 return NULL;
969 if (ie->ctime_sec == sb.st_ctime &&
970 ie->ctime_nsec == sb.st_ctimensec &&
971 ie->mtime_sec == sb.st_mtime &&
972 ie->mtime_sec == sb.st_mtime &&
973 ie->mtime_nsec == sb.st_mtimensec &&
974 ie->size == (sb.st_size & 0xffffffff))
975 return NULL;
977 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
978 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
979 if (err)
980 return err;
982 f = fopen(abspath, "r");
983 if (f == NULL) {
984 err = got_error_from_errno();
985 goto done;
987 hdrlen = got_object_blob_get_hdrlen(blob);
988 while (1) {
989 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
990 err = got_object_blob_read_block(&blen, blob);
991 if (err)
992 break;
993 flen = fread(fbuf, 1, sizeof(fbuf), f);
994 if (blen == 0) {
995 if (flen != 0)
996 *status = GOT_STATUS_MODIFIY;
997 break;
998 } else if (flen == 0) {
999 if (blen != 0)
1000 *status = GOT_STATUS_MODIFIY;
1001 break;
1002 } else if (blen - hdrlen == flen) {
1003 /* Skip blob object header first time around. */
1004 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1005 *status = GOT_STATUS_MODIFIY;
1006 break;
1008 } else {
1009 *status = GOT_STATUS_MODIFIY;
1010 break;
1012 hdrlen = 0;
1014 done:
1015 if (blob)
1016 got_object_blob_close(blob);
1017 if (f)
1018 fclose(f);
1019 return err;
1022 static const struct got_error *
1023 status_old_new(void *arg, struct got_fileindex_entry *ie,
1024 struct dirent *de, const char *parent_path)
1026 const struct got_error *err = NULL;
1027 struct diff_dir_cb_arg *a = arg;
1028 char *abspath;
1029 unsigned char status = GOT_STATUS_NO_CHANGE;
1031 if (parent_path[0]) {
1032 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1033 parent_path, de->d_name) == -1)
1034 return got_error_from_errno();
1035 } else {
1036 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1037 de->d_name) == -1)
1038 return got_error_from_errno();
1041 err = get_file_status(&status, ie, abspath, a->repo);
1042 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1043 struct got_object_id id;
1044 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1045 err = (*a->status_cb)(a->status_arg, status, ie->path, &id);
1047 free(abspath);
1048 return err;
1051 static const struct got_error *
1052 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1054 struct diff_dir_cb_arg *a = arg;
1055 struct got_object_id id;
1056 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1057 return (*a->status_cb)(a->status_arg, GOT_STATUS_MISSING, ie->path,
1058 &id);
1061 static const struct got_error *
1062 status_new(void *arg, struct dirent *de, const char *parent_path)
1064 const struct got_error *err = NULL;
1065 struct diff_dir_cb_arg *a = arg;
1066 char *path = NULL;
1068 if (de->d_type == DT_DIR)
1069 return NULL;
1071 if (parent_path[0]) {
1072 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1073 return got_error_from_errno();
1074 } else {
1075 path = de->d_name;
1078 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1079 NULL);
1080 if (parent_path[0])
1081 free(path);
1082 return err;
1085 const struct got_error *
1086 got_worktree_status(struct got_worktree *worktree,
1087 struct got_repository *repo, got_worktree_status_cb status_cb,
1088 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1090 const struct got_error *err = NULL;
1091 DIR *workdir = NULL;
1092 char *fileindex_path = NULL;
1093 struct got_fileindex *fileindex = NULL;
1094 FILE *index = NULL;
1095 struct got_fileindex_diff_dir_cb fdiff_cb;
1096 struct diff_dir_cb_arg arg;
1098 fileindex = got_fileindex_alloc();
1099 if (fileindex == NULL) {
1100 err = got_error_from_errno();
1101 goto done;
1104 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1105 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1106 err = got_error_from_errno();
1107 fileindex_path = NULL;
1108 goto done;
1111 index = fopen(fileindex_path, "rb");
1112 if (index == NULL) {
1113 if (errno != ENOENT) {
1114 err = got_error_from_errno();
1115 goto done;
1117 } else {
1118 err = got_fileindex_read(fileindex, index);
1119 fclose(index);
1120 if (err)
1121 goto done;
1124 workdir = opendir(worktree->root_path);
1125 if (workdir == NULL) {
1126 err = got_error_from_errno();
1127 goto done;
1129 fdiff_cb.diff_old_new = status_old_new;
1130 fdiff_cb.diff_old = status_old;
1131 fdiff_cb.diff_new = status_new;
1132 arg.fileindex = fileindex;
1133 arg.worktree = worktree;
1134 arg.repo = repo;
1135 arg.status_cb = status_cb;
1136 arg.status_arg = status_arg;
1137 arg.cancel_cb = cancel_cb;
1138 arg.cancel_arg = cancel_arg;
1139 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1140 repo, &fdiff_cb, &arg);
1141 done:
1142 if (workdir)
1143 closedir(workdir);
1144 free(fileindex_path);
1145 got_fileindex_free(fileindex);
1146 return err;