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>
34 #include <uuid.h>
36 #include "got_error.h"
37 #include "got_repository.h"
38 #include "got_reference.h"
39 #include "got_object.h"
40 #include "got_worktree.h"
41 #include "got_opentemp.h"
43 #include "got_lib_worktree.h"
44 #include "got_lib_path.h"
45 #include "got_lib_sha1.h"
46 #include "got_lib_fileindex.h"
47 #include "got_lib_inflate.h"
48 #include "got_lib_delta.h"
49 #include "got_lib_object.h"
50 #include "got_lib_diff.h"
52 #ifndef MIN
53 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
54 #endif
56 static const struct got_error *
57 create_meta_file(const char *path_got, const char *name, const char *content)
58 {
59 const struct got_error *err = NULL;
60 char *path;
61 int fd = -1;
63 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
64 err = got_error_from_errno();
65 path = NULL;
66 goto done;
67 }
69 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
70 GOT_DEFAULT_FILE_MODE);
71 if (fd == -1) {
72 err = got_error_from_errno();
73 goto done;
74 }
76 if (content) {
77 int len = dprintf(fd, "%s\n", content);
78 if (len != strlen(content) + 1) {
79 err = got_error_from_errno();
80 goto done;
81 }
82 }
84 done:
85 if (fd != -1 && close(fd) == -1 && err == NULL)
86 err = got_error_from_errno();
87 free(path);
88 return err;
89 }
91 static const struct got_error *
92 update_meta_file(const char *path_got, const char *name, const char *content)
93 {
94 const struct got_error *err = NULL;
95 FILE *tmpfile = NULL;
96 char *tmppath = NULL;
97 char *path = NULL;
99 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
100 err = got_error_from_errno();
101 path = NULL;
102 goto done;
105 err = got_opentemp_named(&tmppath, &tmpfile, path);
106 if (err)
107 goto done;
109 if (content) {
110 int len = fprintf(tmpfile, "%s\n", content);
111 if (len != strlen(content) + 1) {
112 err = got_error_from_errno();
113 goto done;
117 if (rename(tmppath, path) != 0) {
118 err = got_error_from_errno();
119 unlink(tmppath);
120 goto done;
123 done:
124 free(tmppath);
125 if (fclose(tmpfile) != 0 && err == NULL)
126 err = got_error_from_errno();
127 return err;
130 static const struct got_error *
131 read_meta_file(char **content, const char *path_got, const char *name)
133 const struct got_error *err = NULL;
134 char *path;
135 int fd = -1;
136 ssize_t n;
137 struct stat sb;
139 *content = NULL;
141 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
142 err = got_error_from_errno();
143 path = NULL;
144 goto done;
147 fd = open(path, O_RDONLY | O_NOFOLLOW);
148 if (fd == -1) {
149 if (errno == ENOENT)
150 err = got_error(GOT_ERR_WORKTREE_META);
151 else
152 err = got_error_from_errno();
153 goto done;
155 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
156 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
157 : got_error_from_errno());
158 goto done;
161 if (lstat(path, &sb) != 0) {
162 err = got_error_from_errno();
163 goto done;
165 *content = calloc(1, sb.st_size);
166 if (*content == NULL) {
167 err = got_error_from_errno();
168 goto done;
171 n = read(fd, *content, sb.st_size);
172 if (n != sb.st_size) {
173 err = (n == -1 ? got_error_from_errno() :
174 got_error(GOT_ERR_WORKTREE_META));
175 goto done;
177 if ((*content)[sb.st_size - 1] != '\n') {
178 err = got_error(GOT_ERR_WORKTREE_META);
179 goto done;
181 (*content)[sb.st_size - 1] = '\0';
183 done:
184 if (fd != -1 && close(fd) == -1 && err == NULL)
185 err = got_error_from_errno();
186 free(path);
187 if (err) {
188 free(*content);
189 *content = NULL;
191 return err;
194 const struct got_error *
195 got_worktree_init(const char *path, struct got_reference *head_ref,
196 const char *prefix, struct got_repository *repo)
198 const struct got_error *err = NULL;
199 struct got_object_id *commit_id = NULL;
200 uuid_t uuid;
201 uint32_t uuid_status;
202 int obj_type;
203 char *path_got = NULL;
204 char *refstr = NULL;
205 char *formatstr = NULL;
206 char *absprefix = NULL;
207 char *basestr = NULL;
208 char *uuidstr = NULL;
210 if (strcmp(path, got_repo_get_path(repo)) == 0) {
211 err = got_error(GOT_ERR_WORKTREE_REPO);
212 goto done;
215 err = got_ref_resolve(&commit_id, repo, head_ref);
216 if (err)
217 return err;
218 err = got_object_get_type(&obj_type, repo, commit_id);
219 if (err)
220 return err;
221 if (obj_type != GOT_OBJ_TYPE_COMMIT)
222 return got_error(GOT_ERR_OBJ_TYPE);
224 if (!got_path_is_absolute(prefix)) {
225 if (asprintf(&absprefix, "/%s", prefix) == -1)
226 return got_error_from_errno();
229 /* Create top-level directory (may already exist). */
230 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
231 err = got_error_from_errno();
232 goto done;
235 /* Create .got directory (may already exist). */
236 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
237 err = got_error_from_errno();
238 goto done;
240 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
241 err = got_error_from_errno();
242 goto done;
245 /* Create an empty lock file. */
246 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
247 if (err)
248 goto done;
250 /* Create an empty file index. */
251 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
252 if (err)
253 goto done;
255 /* Write the HEAD reference. */
256 refstr = got_ref_to_str(head_ref);
257 if (refstr == NULL) {
258 err = got_error_from_errno();
259 goto done;
261 err = create_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
262 if (err)
263 goto done;
265 /* Record our base commit. */
266 err = got_object_id_str(&basestr, commit_id);
267 if (err)
268 goto done;
269 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
270 if (err)
271 goto done;
273 /* Store path to repository. */
274 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
275 got_repo_get_path(repo));
276 if (err)
277 goto done;
279 /* Store in-repository path prefix. */
280 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
281 absprefix ? absprefix : prefix);
282 if (err)
283 goto done;
285 /* Generate UUID. */
286 uuid_create(&uuid, &uuid_status);
287 if (uuid_status != uuid_s_ok) {
288 err = got_error_uuid(uuid_status);
289 goto done;
291 uuid_to_string(&uuid, &uuidstr, &uuid_status);
292 if (uuid_status != uuid_s_ok) {
293 err = got_error_uuid(uuid_status);
294 goto done;
296 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
297 if (err)
298 goto done;
300 /* Stamp work tree with format file. */
301 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
302 err = got_error_from_errno();
303 goto done;
305 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
306 if (err)
307 goto done;
309 done:
310 free(commit_id);
311 free(path_got);
312 free(formatstr);
313 free(refstr);
314 free(absprefix);
315 free(basestr);
316 free(uuidstr);
317 return err;
320 static const struct got_error *
321 open_worktree(struct got_worktree **worktree, const char *path)
323 const struct got_error *err = NULL;
324 char *path_got;
325 char *formatstr = NULL;
326 char *uuidstr = NULL;
327 char *path_lock = NULL;
328 char *base_commit_id_str = NULL;
329 char *head_ref_str = NULL;
330 int version, fd = -1;
331 const char *errstr;
332 struct got_repository *repo = NULL;
333 uint32_t uuid_status;
335 *worktree = NULL;
337 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
338 err = got_error_from_errno();
339 path_got = NULL;
340 goto done;
343 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
344 err = got_error_from_errno();
345 path_lock = NULL;
346 goto done;
349 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
350 if (fd == -1) {
351 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
352 : got_error_from_errno());
353 goto done;
356 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
357 if (err)
358 goto done;
360 version = strtonum(formatstr, 1, INT_MAX, &errstr);
361 if (errstr) {
362 err = got_error(GOT_ERR_WORKTREE_META);
363 goto done;
365 if (version != GOT_WORKTREE_FORMAT_VERSION) {
366 err = got_error(GOT_ERR_WORKTREE_VERS);
367 goto done;
370 *worktree = calloc(1, sizeof(**worktree));
371 if (*worktree == NULL) {
372 err = got_error_from_errno();
373 goto done;
375 (*worktree)->lockfd = -1;
377 (*worktree)->root_path = realpath(path, NULL);
378 if ((*worktree)->root_path == NULL) {
379 err = got_error_from_errno();
380 goto done;
382 err = read_meta_file(&(*worktree)->repo_path, path_got,
383 GOT_WORKTREE_REPOSITORY);
384 if (err)
385 goto done;
387 err = read_meta_file(&(*worktree)->path_prefix, path_got,
388 GOT_WORKTREE_PATH_PREFIX);
389 if (err)
390 goto done;
392 err = read_meta_file(&base_commit_id_str, path_got,
393 GOT_WORKTREE_BASE_COMMIT);
394 if (err)
395 goto done;
397 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
398 if (err)
399 goto done;
400 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
401 if (uuid_status != uuid_s_ok) {
402 err = got_error_uuid(uuid_status);
403 goto done;
406 err = got_repo_open(&repo, (*worktree)->repo_path);
407 if (err)
408 goto done;
410 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
411 base_commit_id_str);
412 if (err)
413 goto done;
415 err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
416 if (err)
417 goto done;
419 err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
420 done:
421 if (repo)
422 got_repo_close(repo);
423 free(path_got);
424 free(path_lock);
425 free(head_ref_str);
426 free(base_commit_id_str);
427 free(uuidstr);
428 free(formatstr);
429 if (err) {
430 if (fd != -1)
431 close(fd);
432 if (*worktree != NULL)
433 got_worktree_close(*worktree);
434 *worktree = NULL;
435 } else
436 (*worktree)->lockfd = fd;
438 return err;
441 const struct got_error *
442 got_worktree_open(struct got_worktree **worktree, const char *path)
444 const struct got_error *err = NULL;
446 do {
447 err = open_worktree(worktree, path);
448 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
449 return err;
450 if (*worktree)
451 return NULL;
452 path = dirname(path);
453 if (path == NULL)
454 return got_error_from_errno();
455 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
457 return got_error(GOT_ERR_NOT_WORKTREE);
460 const struct got_error *
461 got_worktree_close(struct got_worktree *worktree)
463 const struct got_error *err = NULL;
464 free(worktree->root_path);
465 free(worktree->repo_path);
466 free(worktree->path_prefix);
467 free(worktree->base_commit_id);
468 if (worktree->head_ref)
469 got_ref_close(worktree->head_ref);
470 if (worktree->lockfd != -1)
471 if (close(worktree->lockfd) != 0)
472 err = got_error_from_errno();
473 free(worktree);
474 return err;
477 const char *
478 got_worktree_get_root_path(struct got_worktree *worktree)
480 return worktree->root_path;
483 const char *
484 got_worktree_get_repo_path(struct got_worktree *worktree)
486 return worktree->repo_path;
489 const char *
490 got_worktree_get_path_prefix(struct got_worktree *worktree)
492 return worktree->path_prefix;
495 const struct got_error *
496 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
497 const char *path_prefix)
499 char *absprefix = NULL;
501 if (!got_path_is_absolute(path_prefix)) {
502 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
503 return got_error_from_errno();
505 *match = (strcmp(absprefix ? absprefix : path_prefix,
506 worktree->path_prefix) == 0);
507 free(absprefix);
508 return NULL;
511 char *
512 got_worktree_get_head_ref_name(struct got_worktree *worktree)
514 return got_ref_to_str(worktree->head_ref);
517 struct got_reference *
518 got_worktree_get_head_ref(struct got_worktree *worktree)
520 return got_ref_dup(worktree->head_ref);
523 struct got_object_id *
524 got_worktree_get_base_commit_id(struct got_worktree *worktree)
526 return worktree->base_commit_id;
529 const struct got_error *
530 got_worktree_set_base_commit_id(struct got_worktree *worktree,
531 struct got_repository *repo, struct got_object_id *commit_id)
533 const struct got_error *err;
534 struct got_object *obj = NULL;
535 char *id_str = NULL;
536 char *path_got = NULL;
538 if (asprintf(&path_got, "%s/%s", worktree->root_path,
539 GOT_WORKTREE_GOT_DIR) == -1) {
540 err = got_error_from_errno();
541 path_got = NULL;
542 goto done;
545 err = got_object_open(&obj, repo, commit_id);
546 if (err)
547 return err;
549 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
550 err = got_error(GOT_ERR_OBJ_TYPE);
551 goto done;
554 /* Record our base commit. */
555 err = got_object_id_str(&id_str, commit_id);
556 if (err)
557 goto done;
558 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
559 if (err)
560 goto done;
562 free(worktree->base_commit_id);
563 worktree->base_commit_id = got_object_id_dup(commit_id);
564 if (worktree->base_commit_id == NULL) {
565 err = got_error_from_errno();
566 goto done;
568 done:
569 if (obj)
570 got_object_close(obj);
571 free(id_str);
572 free(path_got);
573 return err;
576 static const struct got_error *
577 lock_worktree(struct got_worktree *worktree, int operation)
579 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
580 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
581 : got_error_from_errno());
582 return NULL;
585 static const struct got_error *
586 add_dir_on_disk(struct got_worktree *worktree, const char *path)
588 const struct got_error *err = NULL;
589 char *abspath;
591 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
592 return got_error_from_errno();
594 err = got_path_mkdir(abspath);
595 free(abspath);
596 return err;
599 static const struct got_error *
600 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
602 const struct got_error *err = NULL;
603 uint8_t fbuf1[8192];
604 uint8_t fbuf2[8192];
605 size_t flen1 = 0, flen2 = 0;
607 *same = 1;
609 while (1) {
610 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
611 if (flen1 == 0 && ferror(f1)) {
612 err = got_error_from_errno();
613 break;
615 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
616 if (flen2 == 0 && ferror(f2)) {
617 err = got_error_from_errno();
618 break;
620 if (flen1 == 0) {
621 if (flen2 != 0)
622 *same = 0;
623 break;
624 } else if (flen2 == 0) {
625 if (flen1 != 0)
626 *same = 0;
627 break;
628 } else if (flen1 == flen2) {
629 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
630 *same = 0;
631 break;
633 } else {
634 *same = 0;
635 break;
639 return err;
642 static const struct got_error *
643 check_files_equal(int *same, const char *f1_path, const char *f2_path)
645 const struct got_error *err = NULL;
646 struct stat sb;
647 size_t size1, size2;
648 FILE *f1 = NULL, *f2 = NULL;
650 *same = 1;
652 if (lstat(f1_path, &sb) != 0) {
653 err = got_error_from_errno();
654 goto done;
656 size1 = sb.st_size;
658 if (lstat(f2_path, &sb) != 0) {
659 err = got_error_from_errno();
660 goto done;
662 size2 = sb.st_size;
664 if (size1 != size2) {
665 *same = 0;
666 return NULL;
669 f1 = fopen(f1_path, "r");
670 if (f1 == NULL)
671 return got_error_from_errno();
673 f2 = fopen(f2_path, "r");
674 if (f2 == NULL) {
675 err = got_error_from_errno();
676 goto done;
679 err = check_file_contents_equal(same, f1, f2);
680 done:
681 if (f1 && fclose(f1) != 0 && err == NULL)
682 err = got_error_from_errno();
683 if (f2 && fclose(f2) != 0 && err == NULL)
684 err = got_error_from_errno();
686 return err;
689 /*
690 * Perform a 3-way merge where the file's version in the file index (blob2)
691 * acts as the common ancestor, the incoming blob (blob1) acts as the first
692 * derived version, and the file on disk acts as the second derived version.
693 */
694 static const struct got_error *
695 merge_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
696 struct got_fileindex_entry *ie, const char *ondisk_path, const char *path,
697 uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob1,
698 struct got_repository *repo,
699 got_worktree_checkout_cb progress_cb, void *progress_arg)
701 const struct got_error *err = NULL;
702 int merged_fd = -1;
703 struct got_blob_object *blob2 = NULL;
704 FILE *f1 = NULL, *f2 = NULL;
705 char *blob1_path = NULL, *blob2_path = NULL;
706 char *merged_path = NULL, *base_path = NULL;
707 struct got_object_id id2;
708 char *id_str = NULL;
709 char *label1 = NULL;
710 int overlapcnt = 0, update_timestamps = 0;
711 char *parent;
713 parent = dirname(ondisk_path);
714 if (parent == NULL)
715 return got_error_from_errno();
717 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
718 return got_error_from_errno();
720 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
721 if (err)
722 goto done;
724 free(base_path);
725 if (asprintf(&base_path, "%s/got-merge-blob1", parent) == -1) {
726 err = got_error_from_errno();
727 base_path = NULL;
728 goto done;
731 err = got_opentemp_named(&blob1_path, &f1, base_path);
732 if (err)
733 goto done;
734 err = got_object_blob_dump_to_file(NULL, NULL, f1, blob1);
735 if (err)
736 goto done;
738 free(base_path);
739 if (asprintf(&base_path, "%s/got-merge-blob2", parent) == -1) {
740 err = got_error_from_errno();
741 base_path = NULL;
742 goto done;
745 err = got_opentemp_named(&blob2_path, &f2, base_path);
746 if (err)
747 goto done;
749 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
750 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
751 if (err)
752 goto done;
753 err = got_object_blob_dump_to_file(NULL, NULL, f2, blob2);
754 if (err)
755 goto done;
757 err = got_object_id_str(&id_str, worktree->base_commit_id);
758 if (err)
759 goto done;
760 if (asprintf(&label1, "commit %s", id_str) == -1) {
761 err = got_error_from_errno();
762 goto done;
765 err = got_merge_diff3(&overlapcnt, merged_fd, blob1_path,
766 blob2_path, ondisk_path, label1, path);
767 if (err)
768 goto done;
770 (*progress_cb)(progress_arg,
771 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
774 if (fsync(merged_fd) != 0) {
775 err = got_error_from_errno();
776 goto done;
779 /* Check if a clean merge has subsumed all local changes. */
780 if (overlapcnt == 0) {
781 err = check_files_equal(&update_timestamps, blob1_path,
782 merged_path);
783 if (err)
784 goto done;
787 if (chmod(merged_path, st_mode) != 0) {
788 err = got_error_from_errno();
789 goto done;
792 if (rename(merged_path, ondisk_path) != 0) {
793 err = got_error_from_errno();
794 unlink(merged_path);
795 goto done;
798 /*
799 * Do not update timestamps of already modified files. Otherwise,
800 * a future status walk would treat them as unmodified files again.
801 */
802 err = got_fileindex_entry_update(ie, ondisk_path,
803 blob1->id.sha1, worktree->base_commit_id->sha1, update_timestamps);
804 done:
805 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
806 err = got_error_from_errno();
807 if (f1 && fclose(f1) != 0 && err == NULL)
808 err = got_error_from_errno();
809 if (f2 && fclose(f2) != 0 && err == NULL)
810 err = got_error_from_errno();
811 if (blob2)
812 got_object_blob_close(blob2);
813 free(merged_path);
814 free(base_path);
815 if (blob1_path) {
816 unlink(blob1_path);
817 free(blob1_path);
819 if (blob2_path) {
820 unlink(blob2_path);
821 free(blob2_path);
823 free(id_str);
824 free(label1);
825 return err;
828 static const struct got_error *
829 install_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
830 struct got_fileindex_entry *entry, const char *ondisk_path, const char *path,
831 uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob,
832 int restoring_missing_file, struct got_repository *repo,
833 got_worktree_checkout_cb progress_cb, void *progress_arg)
835 const struct got_error *err = NULL;
836 int fd = -1;
837 size_t len, hdrlen;
838 int update = 0;
839 char *tmppath = NULL;
841 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
842 GOT_DEFAULT_FILE_MODE);
843 if (fd == -1) {
844 if (errno == ENOENT) {
845 char *parent = dirname(path);
846 if (parent == NULL)
847 return got_error_from_errno();
848 err = add_dir_on_disk(worktree, parent);
849 if (err)
850 return err;
851 fd = open(ondisk_path,
852 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
853 GOT_DEFAULT_FILE_MODE);
854 if (fd == -1)
855 return got_error_from_errno();
856 } else if (errno == EEXIST) {
857 if (!S_ISREG(st_mode)) {
858 /* TODO file is obstructed; do something */
859 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
860 goto done;
861 } else {
862 err = got_opentemp_named_fd(&tmppath, &fd,
863 ondisk_path);
864 if (err)
865 goto done;
866 update = 1;
868 } else
869 return got_error_from_errno();
872 if (restoring_missing_file)
873 (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
874 else
875 (*progress_cb)(progress_arg,
876 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
878 hdrlen = got_object_blob_get_hdrlen(blob);
879 do {
880 const uint8_t *buf = got_object_blob_get_read_buf(blob);
881 err = got_object_blob_read_block(&len, blob);
882 if (err)
883 break;
884 if (len > 0) {
885 /* Skip blob object header first time around. */
886 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
887 if (outlen == -1) {
888 err = got_error_from_errno();
889 goto done;
890 } else if (outlen != len - hdrlen) {
891 err = got_error(GOT_ERR_IO);
892 goto done;
894 hdrlen = 0;
896 } while (len != 0);
898 if (fsync(fd) != 0) {
899 err = got_error_from_errno();
900 goto done;
903 if (update) {
904 if (rename(tmppath, ondisk_path) != 0) {
905 err = got_error_from_errno();
906 unlink(tmppath);
907 goto done;
911 if (te_mode & S_IXUSR) {
912 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
913 err = got_error_from_errno();
914 goto done;
916 } else {
917 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
918 err = got_error_from_errno();
919 goto done;
923 if (entry == NULL)
924 entry = got_fileindex_entry_get(fileindex, path);
925 if (entry)
926 err = got_fileindex_entry_update(entry, ondisk_path,
927 blob->id.sha1, worktree->base_commit_id->sha1, 1);
928 else {
929 err = got_fileindex_entry_alloc(&entry, ondisk_path,
930 path, blob->id.sha1, worktree->base_commit_id->sha1);
931 if (err)
932 goto done;
933 err = got_fileindex_entry_add(fileindex, entry);
935 done:
936 if (fd != -1 && close(fd) != 0 && err == NULL)
937 err = got_error_from_errno();
938 free(tmppath);
939 return err;
942 static const struct got_error *
943 get_file_status(unsigned char *status, struct stat *sb,
944 struct got_fileindex_entry *ie, const char *abspath,
945 struct got_repository *repo)
947 const struct got_error *err = NULL;
948 struct got_object_id id;
949 size_t hdrlen;
950 FILE *f = NULL;
951 uint8_t fbuf[8192];
952 struct got_blob_object *blob = NULL;
953 size_t flen, blen;
955 *status = GOT_STATUS_NO_CHANGE;
957 if (lstat(abspath, sb) == -1) {
958 if (errno == ENOENT) {
959 if (ie) {
960 *status = GOT_STATUS_MISSING;
961 sb->st_mode =
962 ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
963 & (S_IRWXU | S_IRWXG | S_IRWXO));
964 } else
965 sb->st_mode = GOT_DEFAULT_FILE_MODE;
966 return NULL;
968 return got_error_from_errno();
971 if (!S_ISREG(sb->st_mode)) {
972 *status = GOT_STATUS_OBSTRUCTED;
973 return NULL;
976 if (ie == NULL)
977 return NULL;
979 if (ie->ctime_sec == sb->st_ctime &&
980 ie->ctime_nsec == sb->st_ctimensec &&
981 ie->mtime_sec == sb->st_mtime &&
982 ie->mtime_sec == sb->st_mtime &&
983 ie->mtime_nsec == sb->st_mtimensec &&
984 ie->size == (sb->st_size & 0xffffffff))
985 return NULL;
987 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
988 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
989 if (err)
990 return err;
992 f = fopen(abspath, "r");
993 if (f == NULL) {
994 err = got_error_from_errno();
995 goto done;
997 hdrlen = got_object_blob_get_hdrlen(blob);
998 while (1) {
999 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1000 err = got_object_blob_read_block(&blen, blob);
1001 if (err)
1002 break;
1003 /* Skip length of blob object header first time around. */
1004 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1005 if (flen == 0 && ferror(f)) {
1006 err = got_error_from_errno();
1007 break;
1009 if (blen == 0) {
1010 if (flen != 0)
1011 *status = GOT_STATUS_MODIFY;
1012 break;
1013 } else if (flen == 0) {
1014 if (blen != 0)
1015 *status = GOT_STATUS_MODIFY;
1016 break;
1017 } else if (blen - hdrlen == flen) {
1018 /* Skip blob object header first time around. */
1019 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1020 *status = GOT_STATUS_MODIFY;
1021 break;
1023 } else {
1024 *status = GOT_STATUS_MODIFY;
1025 break;
1027 hdrlen = 0;
1029 done:
1030 if (blob)
1031 got_object_blob_close(blob);
1032 if (f)
1033 fclose(f);
1034 return err;
1037 static const struct got_error *
1038 update_blob(struct got_worktree *worktree,
1039 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1040 struct got_tree_entry *te, const char *path,
1041 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1042 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1044 const struct got_error *err = NULL;
1045 struct got_blob_object *blob = NULL;
1046 char *ondisk_path;
1047 unsigned char status = GOT_STATUS_NO_CHANGE;
1048 struct stat sb;
1050 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1051 return got_error_from_errno();
1053 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1054 if (err)
1055 goto done;
1057 if (status == GOT_STATUS_OBSTRUCTED) {
1058 (*progress_cb)(progress_arg, status, path);
1059 goto done;
1062 if (ie && status != GOT_STATUS_MISSING) {
1063 if (memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1064 SHA1_DIGEST_LENGTH) == 0) {
1065 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1066 path);
1067 goto done;
1069 if (memcmp(ie->blob_sha1,
1070 te->id->sha1, SHA1_DIGEST_LENGTH) == 0)
1071 goto done;
1074 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1075 if (err)
1076 goto done;
1078 if (status == GOT_STATUS_MODIFY)
1079 err = merge_blob(worktree, fileindex, ie, ondisk_path, path,
1080 te->mode, sb.st_mode, blob, repo, progress_cb,
1081 progress_arg);
1082 else
1083 err = install_blob(worktree, fileindex, ie, ondisk_path, path,
1084 te->mode, sb.st_mode, blob, status == GOT_STATUS_MISSING,
1085 repo, progress_cb, progress_arg);
1087 got_object_blob_close(blob);
1088 done:
1089 free(ondisk_path);
1090 return err;
1093 static const struct got_error *
1094 remove_ondisk_file(const char *root_path, const char *path)
1096 const struct got_error *err = NULL;
1097 char *ondisk_path = NULL;
1099 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1100 return got_error_from_errno();
1102 if (unlink(ondisk_path) == -1) {
1103 if (errno != ENOENT)
1104 err = got_error_from_errno();
1105 } else {
1106 char *parent = dirname(ondisk_path);
1107 while (parent && strcmp(parent, root_path) != 0) {
1108 if (rmdir(parent) == -1) {
1109 if (errno != ENOTEMPTY)
1110 err = got_error_from_errno();
1111 break;
1113 parent = dirname(parent);
1116 free(ondisk_path);
1117 return err;
1120 struct diff_cb_arg {
1121 struct got_fileindex *fileindex;
1122 struct got_worktree *worktree;
1123 struct got_repository *repo;
1124 got_worktree_checkout_cb progress_cb;
1125 void *progress_arg;
1126 got_worktree_cancel_cb cancel_cb;
1127 void *cancel_arg;
1130 static const struct got_error *
1131 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1132 struct got_tree_entry *te, const char *parent_path)
1134 struct diff_cb_arg *a = arg;
1136 return update_blob(a->worktree, a->fileindex, ie, te,
1137 ie->path, a->repo, a->progress_cb, a->progress_arg,
1138 a->cancel_cb, a->cancel_arg);
1141 static const struct got_error *
1142 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1144 const struct got_error *err;
1145 struct diff_cb_arg *a = arg;
1147 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE, ie->path);
1149 err = remove_ondisk_file(a->worktree->root_path, ie->path);
1150 if (err)
1151 return err;
1152 got_fileindex_entry_remove(a->fileindex, ie);
1153 return NULL;
1156 static const struct got_error *
1157 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1159 struct diff_cb_arg *a = arg;
1160 const struct got_error *err;
1161 char *path;
1163 if (asprintf(&path, "%s%s%s", parent_path,
1164 parent_path[0] ? "/" : "", te->name)
1165 == -1)
1166 return got_error_from_errno();
1168 if (S_ISDIR(te->mode))
1169 err = add_dir_on_disk(a->worktree, path);
1170 else
1171 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1172 a->repo, a->progress_cb, a->progress_arg,
1173 a->cancel_cb, a->cancel_arg);
1175 free(path);
1176 return err;
1180 * Prevent Git's garbage collector from deleting our base commit by
1181 * setting a reference to our base commit's ID.
1183 static const struct got_error *
1184 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1186 const struct got_error *err = NULL;
1187 struct got_reference *ref = NULL;
1188 const char *root_path;
1189 char *refname = NULL, *uuidstr = NULL, *s;
1190 uint32_t uuid_status;
1192 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1193 if (uuid_status != uuid_s_ok)
1194 return got_error_uuid(uuid_status);
1196 root_path = got_worktree_get_root_path(worktree);
1197 while (root_path[0] == '/')
1198 root_path++;
1199 if (asprintf(&refname, "%s-%s-%s", GOT_WORKTREE_BASE_REF_PREFIX,
1200 root_path, uuidstr) == -1) {
1201 err = got_error_from_errno();
1202 goto done;
1205 /* Replace slashes from worktree's on-disk path with dashes. */
1206 s = refname + sizeof(GOT_WORKTREE_BASE_REF_PREFIX) - 1;
1207 while (*s) {
1208 if (*s == '/')
1209 *s = '-';
1210 s++;
1213 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1214 if (err)
1215 goto done;
1217 err = got_ref_write(ref, repo);
1218 done:
1219 free(uuidstr);
1220 free(refname);
1221 if (ref)
1222 got_ref_close(ref);
1223 return err;
1227 const struct got_error *
1228 got_worktree_checkout_files(struct got_worktree *worktree,
1229 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1230 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1232 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1233 struct got_commit_object *commit = NULL;
1234 struct got_object_id *tree_id = NULL;
1235 struct got_tree_object *tree = NULL;
1236 char *fileindex_path = NULL, *new_fileindex_path = NULL;
1237 struct got_fileindex *fileindex = NULL;
1238 FILE *index = NULL, *new_index = NULL;
1239 struct got_fileindex_diff_tree_cb diff_cb;
1240 struct diff_cb_arg arg;
1242 err = lock_worktree(worktree, LOCK_EX);
1243 if (err)
1244 return err;
1246 fileindex = got_fileindex_alloc();
1247 if (fileindex == NULL) {
1248 err = got_error_from_errno();
1249 goto done;
1252 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1253 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1254 err = got_error_from_errno();
1255 fileindex_path = NULL;
1256 goto done;
1260 * Read the file index.
1261 * Checking out files is supposed to be an idempotent operation.
1262 * If the on-disk file index is incomplete we will try to complete it.
1264 index = fopen(fileindex_path, "rb");
1265 if (index == NULL) {
1266 if (errno != ENOENT) {
1267 err = got_error_from_errno();
1268 goto done;
1270 } else {
1271 err = got_fileindex_read(fileindex, index);
1272 fclose(index);
1273 if (err)
1274 goto done;
1277 err = got_opentemp_named(&new_fileindex_path, &new_index,
1278 fileindex_path);
1279 if (err)
1280 goto done;
1282 err = ref_base_commit(worktree, repo);
1283 if (err)
1284 goto done;
1286 err = got_object_open_as_commit(&commit, repo,
1287 worktree->base_commit_id);
1288 if (err)
1289 goto done;
1291 err = got_object_id_by_path(&tree_id, repo,
1292 worktree->base_commit_id, worktree->path_prefix);
1293 if (err)
1294 goto done;
1296 err = got_object_open_as_tree(&tree, repo, tree_id);
1297 if (err)
1298 goto done;
1300 diff_cb.diff_old_new = diff_old_new;
1301 diff_cb.diff_old = diff_old;
1302 diff_cb.diff_new = diff_new;
1303 arg.fileindex = fileindex;
1304 arg.worktree = worktree;
1305 arg.repo = repo;
1306 arg.progress_cb = progress_cb;
1307 arg.progress_arg = progress_arg;
1308 arg.cancel_cb = cancel_cb;
1309 arg.cancel_arg = cancel_arg;
1310 checkout_err = got_fileindex_diff_tree(fileindex, tree, repo,
1311 &diff_cb, &arg);
1313 /* Try to sync the fileindex back to disk in any case. */
1314 err = got_fileindex_write(fileindex, new_index);
1315 if (err)
1316 goto done;
1318 if (rename(new_fileindex_path, fileindex_path) != 0) {
1319 err = got_error_from_errno();
1320 unlink(new_fileindex_path);
1321 goto done;
1324 free(new_fileindex_path);
1325 new_fileindex_path = NULL;
1327 done:
1328 if (tree)
1329 got_object_tree_close(tree);
1330 if (commit)
1331 got_object_commit_close(commit);
1332 if (new_fileindex_path)
1333 unlink(new_fileindex_path);
1334 if (new_index)
1335 fclose(new_index);
1336 free(new_fileindex_path);
1337 free(fileindex_path);
1338 got_fileindex_free(fileindex);
1339 if (checkout_err)
1340 err = checkout_err;
1341 unlockerr = lock_worktree(worktree, LOCK_SH);
1342 if (unlockerr && err == NULL)
1343 err = unlockerr;
1344 return err;
1347 struct diff_dir_cb_arg {
1348 struct got_fileindex *fileindex;
1349 struct got_worktree *worktree;
1350 const char *status_path;
1351 size_t status_path_len;
1352 struct got_repository *repo;
1353 got_worktree_status_cb status_cb;
1354 void *status_arg;
1355 got_worktree_cancel_cb cancel_cb;
1356 void *cancel_arg;
1359 static const struct got_error *
1360 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1361 got_worktree_status_cb status_cb, void *status_arg,
1362 struct got_repository *repo)
1364 const struct got_error *err = NULL;
1365 unsigned char status = GOT_STATUS_NO_CHANGE;
1366 struct stat sb;
1367 struct got_object_id id;
1369 err = get_file_status(&status, &sb, ie, abspath, repo);
1370 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1371 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1372 err = (*status_cb)(status_arg, status, ie->path, &id);
1374 return err;
1377 static const struct got_error *
1378 status_old_new(void *arg, struct got_fileindex_entry *ie,
1379 struct dirent *de, const char *parent_path)
1381 const struct got_error *err = NULL;
1382 struct diff_dir_cb_arg *a = arg;
1383 char *abspath;
1385 if (got_path_cmp(parent_path, a->status_path) != 0 &&
1386 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1387 return NULL;
1389 if (parent_path[0]) {
1390 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1391 parent_path, de->d_name) == -1)
1392 return got_error_from_errno();
1393 } else {
1394 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1395 de->d_name) == -1)
1396 return got_error_from_errno();
1399 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1400 a->repo);
1401 free(abspath);
1402 return err;
1405 static const struct got_error *
1406 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1408 struct diff_dir_cb_arg *a = arg;
1409 struct got_object_id id;
1411 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1412 return NULL;
1414 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1415 return (*a->status_cb)(a->status_arg, GOT_STATUS_MISSING, ie->path,
1416 &id);
1419 static const struct got_error *
1420 status_new(void *arg, struct dirent *de, const char *parent_path)
1422 const struct got_error *err = NULL;
1423 struct diff_dir_cb_arg *a = arg;
1424 char *path = NULL;
1426 if (de->d_type == DT_DIR)
1427 return NULL;
1429 /* XXX ignore symlinks for now */
1430 if (de->d_type == DT_LNK)
1431 return NULL;
1433 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1434 return NULL;
1436 if (parent_path[0]) {
1437 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1438 return got_error_from_errno();
1439 } else {
1440 path = de->d_name;
1443 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1444 NULL);
1445 if (parent_path[0])
1446 free(path);
1447 return err;
1450 const struct got_error *
1451 got_worktree_status(struct got_worktree *worktree, const char *path,
1452 struct got_repository *repo, got_worktree_status_cb status_cb,
1453 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1455 const struct got_error *err = NULL;
1456 DIR *workdir = NULL;
1457 char *fileindex_path = NULL;
1458 struct got_fileindex *fileindex = NULL;
1459 FILE *index = NULL;
1460 struct got_fileindex_diff_dir_cb fdiff_cb;
1461 struct diff_dir_cb_arg arg;
1462 char *ondisk_path = NULL;
1464 fileindex = got_fileindex_alloc();
1465 if (fileindex == NULL) {
1466 err = got_error_from_errno();
1467 goto done;
1470 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1471 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1472 err = got_error_from_errno();
1473 fileindex_path = NULL;
1474 goto done;
1477 index = fopen(fileindex_path, "rb");
1478 if (index == NULL) {
1479 if (errno != ENOENT) {
1480 err = got_error_from_errno();
1481 goto done;
1483 } else {
1484 err = got_fileindex_read(fileindex, index);
1485 fclose(index);
1486 if (err)
1487 goto done;
1490 if (asprintf(&ondisk_path, "%s%s%s",
1491 worktree->root_path, path[0] ? "/" : "", path) == -1) {
1492 err = got_error_from_errno();
1493 goto done;
1495 workdir = opendir(ondisk_path);
1496 if (workdir == NULL) {
1497 if (errno == ENOTDIR) {
1498 struct got_fileindex_entry *ie;
1499 ie = got_fileindex_entry_get(fileindex, path);
1500 if (ie == NULL) {
1501 err = got_error(GOT_ERR_BAD_PATH);
1502 goto done;
1504 err = report_file_status(ie, ondisk_path,
1505 status_cb, status_arg, repo);
1506 goto done;
1507 } else {
1508 err = got_error_from_errno();
1509 goto done;
1512 fdiff_cb.diff_old_new = status_old_new;
1513 fdiff_cb.diff_old = status_old;
1514 fdiff_cb.diff_new = status_new;
1515 arg.fileindex = fileindex;
1516 arg.worktree = worktree;
1517 arg.status_path = path;
1518 arg.status_path_len = strlen(path);
1519 arg.repo = repo;
1520 arg.status_cb = status_cb;
1521 arg.status_arg = status_arg;
1522 arg.cancel_cb = cancel_cb;
1523 arg.cancel_arg = cancel_arg;
1524 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1525 path, repo, &fdiff_cb, &arg);
1526 done:
1527 if (workdir)
1528 closedir(workdir);
1529 free(ondisk_path);
1530 free(fileindex_path);
1531 got_fileindex_free(fileindex);
1532 return err;