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 err = got_ref_resolve(&commit_id, repo, head_ref);
211 if (err)
212 return err;
213 err = got_object_get_type(&obj_type, repo, commit_id);
214 if (err)
215 return err;
216 if (obj_type != GOT_OBJ_TYPE_COMMIT)
217 return got_error(GOT_ERR_OBJ_TYPE);
219 if (!got_path_is_absolute(prefix)) {
220 if (asprintf(&absprefix, "/%s", prefix) == -1)
221 return got_error_from_errno();
224 /* Create top-level directory (may already exist). */
225 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
226 err = got_error_from_errno();
227 goto done;
230 /* Create .got directory (may already exist). */
231 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
232 err = got_error_from_errno();
233 goto done;
235 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
236 err = got_error_from_errno();
237 goto done;
240 /* Create an empty lock file. */
241 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
242 if (err)
243 goto done;
245 /* Create an empty file index. */
246 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
247 if (err)
248 goto done;
250 /* Write the HEAD reference. */
251 refstr = got_ref_to_str(head_ref);
252 if (refstr == NULL) {
253 err = got_error_from_errno();
254 goto done;
256 err = create_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
257 if (err)
258 goto done;
260 /* Record our base commit. */
261 err = got_object_id_str(&basestr, commit_id);
262 if (err)
263 goto done;
264 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
265 if (err)
266 goto done;
268 /* Store path to repository. */
269 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
270 got_repo_get_path(repo));
271 if (err)
272 goto done;
274 /* Store in-repository path prefix. */
275 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
276 absprefix ? absprefix : prefix);
277 if (err)
278 goto done;
280 /* Generate UUID. */
281 uuid_create(&uuid, &uuid_status);
282 if (uuid_status != uuid_s_ok) {
283 err = got_error_uuid(uuid_status);
284 goto done;
286 uuid_to_string(&uuid, &uuidstr, &uuid_status);
287 if (uuid_status != uuid_s_ok) {
288 err = got_error_uuid(uuid_status);
289 goto done;
291 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
292 if (err)
293 goto done;
295 /* Stamp work tree with format file. */
296 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
297 err = got_error_from_errno();
298 goto done;
300 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
301 if (err)
302 goto done;
304 done:
305 free(commit_id);
306 free(path_got);
307 free(formatstr);
308 free(refstr);
309 free(absprefix);
310 free(basestr);
311 free(uuidstr);
312 return err;
315 static const struct got_error *
316 open_worktree(struct got_worktree **worktree, const char *path)
318 const struct got_error *err = NULL;
319 char *path_got;
320 char *formatstr = NULL;
321 char *uuidstr = NULL;
322 char *path_lock = NULL;
323 char *base_commit_id_str = NULL;
324 char *head_ref_str = NULL;
325 int version, fd = -1;
326 const char *errstr;
327 struct got_repository *repo = NULL;
328 uint32_t uuid_status;
330 *worktree = NULL;
332 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
333 err = got_error_from_errno();
334 path_got = NULL;
335 goto done;
338 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
339 err = got_error_from_errno();
340 path_lock = NULL;
341 goto done;
344 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
345 if (fd == -1) {
346 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
347 : got_error_from_errno());
348 goto done;
351 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
352 if (err)
353 goto done;
355 version = strtonum(formatstr, 1, INT_MAX, &errstr);
356 if (errstr) {
357 err = got_error(GOT_ERR_WORKTREE_META);
358 goto done;
360 if (version != GOT_WORKTREE_FORMAT_VERSION) {
361 err = got_error(GOT_ERR_WORKTREE_VERS);
362 goto done;
365 *worktree = calloc(1, sizeof(**worktree));
366 if (*worktree == NULL) {
367 err = got_error_from_errno();
368 goto done;
370 (*worktree)->lockfd = -1;
372 (*worktree)->root_path = strdup(path);
373 if ((*worktree)->root_path == NULL) {
374 err = got_error_from_errno();
375 goto done;
377 err = read_meta_file(&(*worktree)->repo_path, path_got,
378 GOT_WORKTREE_REPOSITORY);
379 if (err)
380 goto done;
382 err = read_meta_file(&(*worktree)->path_prefix, path_got,
383 GOT_WORKTREE_PATH_PREFIX);
384 if (err)
385 goto done;
387 err = read_meta_file(&base_commit_id_str, path_got,
388 GOT_WORKTREE_BASE_COMMIT);
389 if (err)
390 goto done;
392 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
393 if (err)
394 goto done;
395 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
396 if (uuid_status != uuid_s_ok) {
397 err = got_error_uuid(uuid_status);
398 goto done;
401 err = got_repo_open(&repo, (*worktree)->repo_path);
402 if (err)
403 goto done;
405 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
406 base_commit_id_str);
407 if (err)
408 goto done;
410 err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
411 if (err)
412 goto done;
414 err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
415 done:
416 if (repo)
417 got_repo_close(repo);
418 free(path_got);
419 free(path_lock);
420 free(head_ref_str);
421 free(base_commit_id_str);
422 free(uuidstr);
423 free(formatstr);
424 if (err) {
425 if (fd != -1)
426 close(fd);
427 if (*worktree != NULL)
428 got_worktree_close(*worktree);
429 *worktree = NULL;
430 } else
431 (*worktree)->lockfd = fd;
433 return err;
436 const struct got_error *
437 got_worktree_open(struct got_worktree **worktree, const char *path)
439 const struct got_error *err = NULL;
441 do {
442 err = open_worktree(worktree, path);
443 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
444 return err;
445 if (*worktree)
446 return NULL;
447 path = dirname(path);
448 if (path == NULL)
449 return got_error_from_errno();
450 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
452 return got_error(GOT_ERR_NOT_WORKTREE);
455 const struct got_error *
456 got_worktree_close(struct got_worktree *worktree)
458 const struct got_error *err = NULL;
459 free(worktree->root_path);
460 free(worktree->repo_path);
461 free(worktree->path_prefix);
462 free(worktree->base_commit_id);
463 if (worktree->head_ref)
464 got_ref_close(worktree->head_ref);
465 if (worktree->lockfd != -1)
466 if (close(worktree->lockfd) != 0)
467 err = got_error_from_errno();
468 free(worktree);
469 return err;
472 const char *
473 got_worktree_get_root_path(struct got_worktree *worktree)
475 return worktree->root_path;
478 const char *
479 got_worktree_get_repo_path(struct got_worktree *worktree)
481 return worktree->repo_path;
484 const char *
485 got_worktree_get_path_prefix(struct got_worktree *worktree)
487 return worktree->path_prefix;
490 const struct got_error *
491 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
492 const char *path_prefix)
494 char *absprefix = NULL;
496 if (!got_path_is_absolute(path_prefix)) {
497 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
498 return got_error_from_errno();
500 *match = (strcmp(absprefix ? absprefix : path_prefix,
501 worktree->path_prefix) == 0);
502 free(absprefix);
503 return NULL;
506 char *
507 got_worktree_get_head_ref_name(struct got_worktree *worktree)
509 return got_ref_to_str(worktree->head_ref);
512 struct got_reference *
513 got_worktree_get_head_ref(struct got_worktree *worktree)
515 return got_ref_dup(worktree->head_ref);
518 struct got_object_id *
519 got_worktree_get_base_commit_id(struct got_worktree *worktree)
521 return worktree->base_commit_id;
524 const struct got_error *
525 got_worktree_set_base_commit_id(struct got_worktree *worktree,
526 struct got_repository *repo, struct got_object_id *commit_id)
528 const struct got_error *err;
529 struct got_object *obj = NULL;
530 char *id_str = NULL;
531 char *path_got = NULL;
533 if (asprintf(&path_got, "%s/%s", worktree->root_path,
534 GOT_WORKTREE_GOT_DIR) == -1) {
535 err = got_error_from_errno();
536 path_got = NULL;
537 goto done;
540 err = got_object_open(&obj, repo, commit_id);
541 if (err)
542 return err;
544 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
545 err = got_error(GOT_ERR_OBJ_TYPE);
546 goto done;
549 /* Record our base commit. */
550 err = got_object_id_str(&id_str, commit_id);
551 if (err)
552 goto done;
553 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
554 if (err)
555 goto done;
557 free(worktree->base_commit_id);
558 worktree->base_commit_id = got_object_id_dup(commit_id);
559 if (worktree->base_commit_id == NULL) {
560 err = got_error_from_errno();
561 goto done;
563 done:
564 if (obj)
565 got_object_close(obj);
566 free(id_str);
567 free(path_got);
568 return err;
571 static const struct got_error *
572 lock_worktree(struct got_worktree *worktree, int operation)
574 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
575 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
576 : got_error_from_errno());
577 return NULL;
580 static const struct got_error *
581 make_parent_dirs(const char *abspath)
583 const struct got_error *err = NULL;
585 char *parent = dirname(abspath);
586 if (parent == NULL)
587 return NULL;
589 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
590 if (errno == ENOENT) {
591 err = make_parent_dirs(parent);
592 if (err)
593 return err;
594 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1)
595 return got_error_from_errno();
596 } else
597 err = got_error_from_errno();
600 return err;
603 static const struct got_error *
604 add_dir_on_disk(struct got_worktree *worktree, const char *path)
606 const struct got_error *err = NULL;
607 char *abspath;
609 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
610 return got_error_from_errno();
612 /* XXX queue work rather than editing disk directly? */
613 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
614 struct stat sb;
616 if (errno == EEXIST) {
617 if (lstat(abspath, &sb) == -1) {
618 err = got_error_from_errno();
619 goto done;
622 if (!S_ISDIR(sb.st_mode)) {
623 /* TODO directory is obstructed; do something */
624 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
625 goto done;
628 return NULL;
629 } else if (errno == ENOENT) {
630 err = make_parent_dirs(abspath);
631 if (err)
632 goto done;
633 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1)
634 err = got_error_from_errno();
635 } else
636 err = got_error_from_errno();
639 done:
640 free(abspath);
641 return err;
644 static const struct got_error *
645 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
647 const struct got_error *err = NULL;
648 uint8_t fbuf1[8192];
649 uint8_t fbuf2[8192];
650 size_t flen1 = 0, flen2 = 0;
652 *same = 1;
654 while (1) {
655 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
656 if (flen1 == 0 && ferror(f1)) {
657 err = got_error_from_errno();
658 break;
660 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
661 if (flen2 == 0 && ferror(f2)) {
662 err = got_error_from_errno();
663 break;
665 if (flen1 == 0) {
666 if (flen2 != 0)
667 *same = 0;
668 break;
669 } else if (flen2 == 0) {
670 if (flen1 != 0)
671 *same = 0;
672 break;
673 } else if (flen1 == flen2) {
674 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
675 *same = 0;
676 break;
678 } else {
679 *same = 0;
680 break;
684 return err;
687 static const struct got_error *
688 check_files_equal(int *same, const char *f1_path, const char *f2_path)
690 const struct got_error *err = NULL;
691 struct stat sb;
692 size_t size1, size2;
693 FILE *f1 = NULL, *f2 = NULL;
695 *same = 1;
697 if (lstat(f1_path, &sb) != 0) {
698 err = got_error_from_errno();
699 goto done;
701 size1 = sb.st_size;
703 if (lstat(f2_path, &sb) != 0) {
704 err = got_error_from_errno();
705 goto done;
707 size2 = sb.st_size;
709 if (size1 != size2) {
710 *same = 0;
711 return NULL;
714 f1 = fopen(f1_path, "r");
715 if (f1 == NULL)
716 return got_error_from_errno();
718 f2 = fopen(f2_path, "r");
719 if (f2 == NULL) {
720 err = got_error_from_errno();
721 goto done;
724 err = check_file_contents_equal(same, f1, f2);
725 done:
726 if (f1 && fclose(f1) != 0 && err == NULL)
727 err = got_error_from_errno();
728 if (f2 && fclose(f2) != 0 && err == NULL)
729 err = got_error_from_errno();
731 return err;
734 /*
735 * Perform a 3-way merge where the file's version in the file index (blob2)
736 * acts as the common ancestor, the incoming blob (blob1) acts as the first
737 * derived version, and the file on disk acts as the second derived version.
738 */
739 static const struct got_error *
740 merge_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
741 struct got_fileindex_entry *ie, const char *ondisk_path, const char *path,
742 uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob1,
743 struct got_repository *repo,
744 got_worktree_checkout_cb progress_cb, void *progress_arg)
746 const struct got_error *err = NULL;
747 int merged_fd = -1;
748 struct got_blob_object *blob2 = NULL;
749 FILE *f1 = NULL, *f2 = NULL;
750 char *blob1_path = NULL, *blob2_path = NULL;
751 char *merged_path = NULL, *base_path = NULL;
752 struct got_object_id id2;
753 char *id_str = NULL;
754 char *label1 = NULL;
755 int overlapcnt = 0, update_timestamps = 0;
756 char *parent;
758 parent = dirname(ondisk_path);
759 if (parent == NULL)
760 return got_error_from_errno();
762 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
763 return got_error_from_errno();
765 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
766 if (err)
767 goto done;
769 free(base_path);
770 if (asprintf(&base_path, "%s/got-merge-blob1", parent) == -1) {
771 err = got_error_from_errno();
772 base_path = NULL;
773 goto done;
776 err = got_opentemp_named(&blob1_path, &f1, base_path);
777 if (err)
778 goto done;
779 err = got_object_blob_dump_to_file(NULL, NULL, f1, blob1);
780 if (err)
781 goto done;
783 free(base_path);
784 if (asprintf(&base_path, "%s/got-merge-blob2", parent) == -1) {
785 err = got_error_from_errno();
786 base_path = NULL;
787 goto done;
790 err = got_opentemp_named(&blob2_path, &f2, base_path);
791 if (err)
792 goto done;
794 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
795 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
796 if (err)
797 goto done;
798 err = got_object_blob_dump_to_file(NULL, NULL, f2, blob2);
799 if (err)
800 goto done;
802 err = got_object_id_str(&id_str, worktree->base_commit_id);
803 if (err)
804 goto done;
805 if (asprintf(&label1, "commit %s", id_str) == -1) {
806 err = got_error_from_errno();
807 goto done;
810 err = got_merge_diff3(&overlapcnt, merged_fd, blob1_path,
811 blob2_path, ondisk_path, label1, path);
812 if (err)
813 goto done;
815 (*progress_cb)(progress_arg,
816 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
819 if (fsync(merged_fd) != 0) {
820 err = got_error_from_errno();
821 goto done;
824 /* Check if a clean merge has subsumed all local changes. */
825 if (overlapcnt == 0) {
826 err = check_files_equal(&update_timestamps, blob1_path,
827 merged_path);
828 if (err)
829 goto done;
832 if (chmod(merged_path, st_mode) != 0) {
833 err = got_error_from_errno();
834 goto done;
837 if (rename(merged_path, ondisk_path) != 0) {
838 err = got_error_from_errno();
839 unlink(merged_path);
840 goto done;
843 /*
844 * Do not update timestamps of already modified files. Otherwise,
845 * a future status walk would treat them as unmodified files again.
846 */
847 err = got_fileindex_entry_update(ie, ondisk_path,
848 blob1->id.sha1, worktree->base_commit_id->sha1, update_timestamps);
849 done:
850 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
851 err = got_error_from_errno();
852 if (f1 && fclose(f1) != 0 && err == NULL)
853 err = got_error_from_errno();
854 if (f2 && fclose(f2) != 0 && err == NULL)
855 err = got_error_from_errno();
856 if (blob2)
857 got_object_blob_close(blob2);
858 free(merged_path);
859 free(base_path);
860 if (blob1_path) {
861 unlink(blob1_path);
862 free(blob1_path);
864 if (blob2_path) {
865 unlink(blob2_path);
866 free(blob2_path);
868 free(id_str);
869 free(label1);
870 return err;
873 static const struct got_error *
874 install_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
875 struct got_fileindex_entry *entry, const char *ondisk_path, const char *path,
876 uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob,
877 int restoring_missing_file, struct got_repository *repo,
878 got_worktree_checkout_cb progress_cb, void *progress_arg)
880 const struct got_error *err = NULL;
881 int fd = -1;
882 size_t len, hdrlen;
883 int update = 0;
884 char *tmppath = NULL;
886 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
887 GOT_DEFAULT_FILE_MODE);
888 if (fd == -1) {
889 if (errno == ENOENT) {
890 char *parent = dirname(path);
891 if (parent == NULL)
892 return got_error_from_errno();
893 err = add_dir_on_disk(worktree, parent);
894 if (err)
895 return err;
896 fd = open(ondisk_path,
897 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
898 GOT_DEFAULT_FILE_MODE);
899 if (fd == -1)
900 return got_error_from_errno();
901 } else if (errno == EEXIST) {
902 if (!S_ISREG(st_mode)) {
903 /* TODO file is obstructed; do something */
904 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
905 goto done;
906 } else {
907 err = got_opentemp_named_fd(&tmppath, &fd,
908 ondisk_path);
909 if (err)
910 goto done;
911 update = 1;
913 } else
914 return got_error_from_errno();
917 if (restoring_missing_file)
918 (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
919 else
920 (*progress_cb)(progress_arg,
921 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
923 hdrlen = got_object_blob_get_hdrlen(blob);
924 do {
925 const uint8_t *buf = got_object_blob_get_read_buf(blob);
926 err = got_object_blob_read_block(&len, blob);
927 if (err)
928 break;
929 if (len > 0) {
930 /* Skip blob object header first time around. */
931 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
932 if (outlen == -1) {
933 err = got_error_from_errno();
934 goto done;
935 } else if (outlen != len - hdrlen) {
936 err = got_error(GOT_ERR_IO);
937 goto done;
939 hdrlen = 0;
941 } while (len != 0);
943 if (fsync(fd) != 0) {
944 err = got_error_from_errno();
945 goto done;
948 if (update) {
949 if (rename(tmppath, ondisk_path) != 0) {
950 err = got_error_from_errno();
951 unlink(tmppath);
952 goto done;
956 if (te_mode & S_IXUSR) {
957 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
958 err = got_error_from_errno();
959 goto done;
961 } else {
962 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
963 err = got_error_from_errno();
964 goto done;
968 if (entry == NULL)
969 entry = got_fileindex_entry_get(fileindex, path);
970 if (entry)
971 err = got_fileindex_entry_update(entry, ondisk_path,
972 blob->id.sha1, worktree->base_commit_id->sha1, 1);
973 else {
974 err = got_fileindex_entry_alloc(&entry, ondisk_path,
975 path, blob->id.sha1, worktree->base_commit_id->sha1);
976 if (err)
977 goto done;
978 err = got_fileindex_entry_add(fileindex, entry);
980 done:
981 if (fd != -1 && close(fd) != 0 && err == NULL)
982 err = got_error_from_errno();
983 free(tmppath);
984 return err;
987 static const struct got_error *
988 get_file_status(unsigned char *status, struct stat *sb,
989 struct got_fileindex_entry *ie, const char *abspath,
990 struct got_repository *repo)
992 const struct got_error *err = NULL;
993 struct got_object_id id;
994 size_t hdrlen;
995 FILE *f = NULL;
996 uint8_t fbuf[8192];
997 struct got_blob_object *blob = NULL;
998 size_t flen, blen;
1000 *status = GOT_STATUS_NO_CHANGE;
1002 if (lstat(abspath, sb) == -1) {
1003 if (errno == ENOENT) {
1004 if (ie) {
1005 *status = GOT_STATUS_MISSING;
1006 sb->st_mode =
1007 ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
1008 & (S_IRWXU | S_IRWXG | S_IRWXO));
1009 } else
1010 sb->st_mode = GOT_DEFAULT_FILE_MODE;
1011 return NULL;
1013 return got_error_from_errno();
1016 if (!S_ISREG(sb->st_mode)) {
1017 *status = GOT_STATUS_OBSTRUCTED;
1018 return NULL;
1021 if (ie == NULL)
1022 return NULL;
1024 if (ie->ctime_sec == sb->st_ctime &&
1025 ie->ctime_nsec == sb->st_ctimensec &&
1026 ie->mtime_sec == sb->st_mtime &&
1027 ie->mtime_sec == sb->st_mtime &&
1028 ie->mtime_nsec == sb->st_mtimensec &&
1029 ie->size == (sb->st_size & 0xffffffff))
1030 return NULL;
1032 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1033 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1034 if (err)
1035 return err;
1037 f = fopen(abspath, "r");
1038 if (f == NULL) {
1039 err = got_error_from_errno();
1040 goto done;
1042 hdrlen = got_object_blob_get_hdrlen(blob);
1043 while (1) {
1044 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1045 err = got_object_blob_read_block(&blen, blob);
1046 if (err)
1047 break;
1048 /* Skip length of blob object header first time around. */
1049 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1050 if (flen == 0 && ferror(f)) {
1051 err = got_error_from_errno();
1052 break;
1054 if (blen == 0) {
1055 if (flen != 0)
1056 *status = GOT_STATUS_MODIFY;
1057 break;
1058 } else if (flen == 0) {
1059 if (blen != 0)
1060 *status = GOT_STATUS_MODIFY;
1061 break;
1062 } else if (blen - hdrlen == flen) {
1063 /* Skip blob object header first time around. */
1064 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1065 *status = GOT_STATUS_MODIFY;
1066 break;
1068 } else {
1069 *status = GOT_STATUS_MODIFY;
1070 break;
1072 hdrlen = 0;
1074 done:
1075 if (blob)
1076 got_object_blob_close(blob);
1077 if (f)
1078 fclose(f);
1079 return err;
1082 static const struct got_error *
1083 update_blob(struct got_worktree *worktree,
1084 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1085 struct got_tree_entry *te, const char *path,
1086 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1087 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1089 const struct got_error *err = NULL;
1090 struct got_blob_object *blob = NULL;
1091 char *ondisk_path;
1092 unsigned char status = GOT_STATUS_NO_CHANGE;
1093 struct stat sb;
1095 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1096 return got_error_from_errno();
1098 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1099 if (err)
1100 goto done;
1102 if (status == GOT_STATUS_OBSTRUCTED) {
1103 (*progress_cb)(progress_arg, status, path);
1104 goto done;
1107 if (ie && status != GOT_STATUS_MISSING) {
1108 if (memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1109 SHA1_DIGEST_LENGTH) == 0) {
1110 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1111 path);
1112 goto done;
1114 if (memcmp(ie->blob_sha1,
1115 te->id->sha1, SHA1_DIGEST_LENGTH) == 0)
1116 goto done;
1119 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1120 if (err)
1121 goto done;
1123 if (status == GOT_STATUS_MODIFY)
1124 err = merge_blob(worktree, fileindex, ie, ondisk_path, path,
1125 te->mode, sb.st_mode, blob, repo, progress_cb,
1126 progress_arg);
1127 else
1128 err = install_blob(worktree, fileindex, ie, ondisk_path, path,
1129 te->mode, sb.st_mode, blob, status == GOT_STATUS_MISSING,
1130 repo, progress_cb, progress_arg);
1132 got_object_blob_close(blob);
1133 done:
1134 free(ondisk_path);
1135 return err;
1138 static const struct got_error *
1139 remove_ondisk_file(const char *root_path, const char *path)
1141 const struct got_error *err = NULL;
1142 char *ondisk_path = NULL;
1144 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1145 return got_error_from_errno();
1147 if (unlink(ondisk_path) == -1) {
1148 if (errno != ENOENT)
1149 err = got_error_from_errno();
1150 } else {
1151 char *parent = dirname(ondisk_path);
1152 while (parent && strcmp(parent, root_path) != 0) {
1153 if (rmdir(parent) == -1) {
1154 if (errno != ENOTEMPTY)
1155 err = got_error_from_errno();
1156 break;
1158 parent = dirname(parent);
1161 free(ondisk_path);
1162 return err;
1165 struct diff_cb_arg {
1166 struct got_fileindex *fileindex;
1167 struct got_worktree *worktree;
1168 struct got_repository *repo;
1169 got_worktree_checkout_cb progress_cb;
1170 void *progress_arg;
1171 got_worktree_cancel_cb cancel_cb;
1172 void *cancel_arg;
1175 static const struct got_error *
1176 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1177 struct got_tree_entry *te, const char *parent_path)
1179 struct diff_cb_arg *a = arg;
1181 return update_blob(a->worktree, a->fileindex, ie, te,
1182 ie->path, a->repo, a->progress_cb, a->progress_arg,
1183 a->cancel_cb, a->cancel_arg);
1186 static const struct got_error *
1187 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1189 const struct got_error *err;
1190 struct diff_cb_arg *a = arg;
1192 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE, ie->path);
1194 err = remove_ondisk_file(a->worktree->root_path, ie->path);
1195 if (err)
1196 return err;
1197 got_fileindex_entry_remove(a->fileindex, ie);
1198 return NULL;
1201 static const struct got_error *
1202 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1204 struct diff_cb_arg *a = arg;
1205 const struct got_error *err;
1206 char *path;
1208 if (asprintf(&path, "%s%s%s", parent_path,
1209 parent_path[0] ? "/" : "", te->name)
1210 == -1)
1211 return got_error_from_errno();
1213 if (S_ISDIR(te->mode))
1214 err = add_dir_on_disk(a->worktree, path);
1215 else
1216 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1217 a->repo, a->progress_cb, a->progress_arg,
1218 a->cancel_cb, a->cancel_arg);
1220 free(path);
1221 return err;
1224 const struct got_error *
1225 got_worktree_checkout_files(struct got_worktree *worktree,
1226 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1227 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1229 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1230 struct got_commit_object *commit = NULL;
1231 struct got_object_id *tree_id = NULL;
1232 struct got_tree_object *tree = NULL;
1233 char *fileindex_path = NULL, *new_fileindex_path = NULL;
1234 struct got_fileindex *fileindex = NULL;
1235 FILE *index = NULL, *new_index = NULL;
1236 struct got_fileindex_diff_tree_cb diff_cb;
1237 struct diff_cb_arg arg;
1239 err = lock_worktree(worktree, LOCK_EX);
1240 if (err)
1241 return err;
1243 fileindex = got_fileindex_alloc();
1244 if (fileindex == NULL) {
1245 err = got_error_from_errno();
1246 goto done;
1249 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1250 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1251 err = got_error_from_errno();
1252 fileindex_path = NULL;
1253 goto done;
1257 * Read the file index.
1258 * Checking out files is supposed to be an idempotent operation.
1259 * If the on-disk file index is incomplete we will try to complete it.
1261 index = fopen(fileindex_path, "rb");
1262 if (index == NULL) {
1263 if (errno != ENOENT) {
1264 err = got_error_from_errno();
1265 goto done;
1267 } else {
1268 err = got_fileindex_read(fileindex, index);
1269 fclose(index);
1270 if (err)
1271 goto done;
1274 err = got_opentemp_named(&new_fileindex_path, &new_index,
1275 fileindex_path);
1276 if (err)
1277 goto done;
1279 err = got_object_open_as_commit(&commit, repo,
1280 worktree->base_commit_id);
1281 if (err)
1282 goto done;
1284 err = got_object_id_by_path(&tree_id, repo,
1285 worktree->base_commit_id, worktree->path_prefix);
1286 if (err)
1287 goto done;
1289 err = got_object_open_as_tree(&tree, repo, tree_id);
1290 if (err)
1291 goto done;
1293 diff_cb.diff_old_new = diff_old_new;
1294 diff_cb.diff_old = diff_old;
1295 diff_cb.diff_new = diff_new;
1296 arg.fileindex = fileindex;
1297 arg.worktree = worktree;
1298 arg.repo = repo;
1299 arg.progress_cb = progress_cb;
1300 arg.progress_arg = progress_arg;
1301 arg.cancel_cb = cancel_cb;
1302 arg.cancel_arg = cancel_arg;
1303 checkout_err = got_fileindex_diff_tree(fileindex, tree, repo,
1304 &diff_cb, &arg);
1306 /* Try to sync the fileindex back to disk in any case. */
1307 err = got_fileindex_write(fileindex, new_index);
1308 if (err)
1309 goto done;
1311 if (rename(new_fileindex_path, fileindex_path) != 0) {
1312 err = got_error_from_errno();
1313 unlink(new_fileindex_path);
1314 goto done;
1317 free(new_fileindex_path);
1318 new_fileindex_path = NULL;
1320 done:
1321 if (tree)
1322 got_object_tree_close(tree);
1323 if (commit)
1324 got_object_commit_close(commit);
1325 if (new_fileindex_path)
1326 unlink(new_fileindex_path);
1327 if (new_index)
1328 fclose(new_index);
1329 free(new_fileindex_path);
1330 free(fileindex_path);
1331 got_fileindex_free(fileindex);
1332 if (checkout_err)
1333 err = checkout_err;
1334 unlockerr = lock_worktree(worktree, LOCK_SH);
1335 if (unlockerr && err == NULL)
1336 err = unlockerr;
1337 return err;
1340 struct diff_dir_cb_arg {
1341 struct got_fileindex *fileindex;
1342 struct got_worktree *worktree;
1343 const char *status_path;
1344 size_t status_path_len;
1345 struct got_repository *repo;
1346 got_worktree_status_cb status_cb;
1347 void *status_arg;
1348 got_worktree_cancel_cb cancel_cb;
1349 void *cancel_arg;
1352 static const struct got_error *
1353 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1354 got_worktree_status_cb status_cb, void *status_arg,
1355 struct got_repository *repo)
1357 const struct got_error *err = NULL;
1358 unsigned char status = GOT_STATUS_NO_CHANGE;
1359 struct stat sb;
1360 struct got_object_id id;
1362 err = get_file_status(&status, &sb, ie, abspath, repo);
1363 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1364 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1365 err = (*status_cb)(status_arg, status, ie->path, &id);
1367 return err;
1370 static const struct got_error *
1371 status_old_new(void *arg, struct got_fileindex_entry *ie,
1372 struct dirent *de, const char *parent_path)
1374 const struct got_error *err = NULL;
1375 struct diff_dir_cb_arg *a = arg;
1376 char *abspath;
1378 if (got_path_cmp(parent_path, a->status_path) != 0 &&
1379 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1380 return NULL;
1382 if (parent_path[0]) {
1383 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1384 parent_path, de->d_name) == -1)
1385 return got_error_from_errno();
1386 } else {
1387 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1388 de->d_name) == -1)
1389 return got_error_from_errno();
1392 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1393 a->repo);
1394 free(abspath);
1395 return err;
1398 static const struct got_error *
1399 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1401 struct diff_dir_cb_arg *a = arg;
1402 struct got_object_id id;
1404 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1405 return NULL;
1407 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1408 return (*a->status_cb)(a->status_arg, GOT_STATUS_MISSING, ie->path,
1409 &id);
1412 static const struct got_error *
1413 status_new(void *arg, struct dirent *de, const char *parent_path)
1415 const struct got_error *err = NULL;
1416 struct diff_dir_cb_arg *a = arg;
1417 char *path = NULL;
1419 if (de->d_type == DT_DIR)
1420 return NULL;
1422 /* XXX ignore symlinks for now */
1423 if (de->d_type == DT_LNK)
1424 return NULL;
1426 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1427 return NULL;
1429 if (parent_path[0]) {
1430 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1431 return got_error_from_errno();
1432 } else {
1433 path = de->d_name;
1436 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1437 NULL);
1438 if (parent_path[0])
1439 free(path);
1440 return err;
1443 const struct got_error *
1444 got_worktree_status(struct got_worktree *worktree, const char *path,
1445 struct got_repository *repo, got_worktree_status_cb status_cb,
1446 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1448 const struct got_error *err = NULL;
1449 DIR *workdir = NULL;
1450 char *fileindex_path = NULL;
1451 struct got_fileindex *fileindex = NULL;
1452 FILE *index = NULL;
1453 struct got_fileindex_diff_dir_cb fdiff_cb;
1454 struct diff_dir_cb_arg arg;
1455 char *ondisk_path = NULL;
1457 fileindex = got_fileindex_alloc();
1458 if (fileindex == NULL) {
1459 err = got_error_from_errno();
1460 goto done;
1463 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1464 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1465 err = got_error_from_errno();
1466 fileindex_path = NULL;
1467 goto done;
1470 index = fopen(fileindex_path, "rb");
1471 if (index == NULL) {
1472 if (errno != ENOENT) {
1473 err = got_error_from_errno();
1474 goto done;
1476 } else {
1477 err = got_fileindex_read(fileindex, index);
1478 fclose(index);
1479 if (err)
1480 goto done;
1483 if (asprintf(&ondisk_path, "%s%s%s",
1484 worktree->root_path, path[0] ? "/" : "", path) == -1) {
1485 err = got_error_from_errno();
1486 goto done;
1488 workdir = opendir(ondisk_path);
1489 if (workdir == NULL) {
1490 if (errno == ENOTDIR) {
1491 struct got_fileindex_entry *ie;
1492 ie = got_fileindex_entry_get(fileindex, path);
1493 if (ie == NULL) {
1494 err = got_error(GOT_ERR_BAD_PATH);
1495 goto done;
1497 err = report_file_status(ie, ondisk_path,
1498 status_cb, status_arg, repo);
1499 goto done;
1500 } else {
1501 err = got_error_from_errno();
1502 goto done;
1505 fdiff_cb.diff_old_new = status_old_new;
1506 fdiff_cb.diff_old = status_old;
1507 fdiff_cb.diff_new = status_new;
1508 arg.fileindex = fileindex;
1509 arg.worktree = worktree;
1510 arg.status_path = path;
1511 arg.status_path_len = strlen(path);
1512 arg.repo = repo;
1513 arg.status_cb = status_cb;
1514 arg.status_arg = status_arg;
1515 arg.cancel_cb = cancel_cb;
1516 arg.cancel_arg = cancel_arg;
1517 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1518 path, repo, &fdiff_cb, &arg);
1519 done:
1520 if (workdir)
1521 closedir(workdir);
1522 free(ondisk_path);
1523 free(fileindex_path);
1524 got_fileindex_free(fileindex);
1525 return err;