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 err = got_error_from_errno();
150 goto done;
152 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
153 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
154 : got_error_from_errno());
155 goto done;
158 if (lstat(path, &sb) != 0) {
159 err = got_error_from_errno();
160 goto done;
162 *content = calloc(1, sb.st_size);
163 if (*content == NULL) {
164 err = got_error_from_errno();
165 goto done;
168 n = read(fd, *content, sb.st_size);
169 if (n != sb.st_size) {
170 err = (n == -1 ? got_error_from_errno() :
171 got_error(GOT_ERR_WORKTREE_META));
172 goto done;
174 if ((*content)[sb.st_size - 1] != '\n') {
175 err = got_error(GOT_ERR_WORKTREE_META);
176 goto done;
178 (*content)[sb.st_size - 1] = '\0';
180 done:
181 if (fd != -1 && close(fd) == -1 && err == NULL)
182 err = got_error_from_errno();
183 free(path);
184 if (err) {
185 free(*content);
186 *content = NULL;
188 return err;
191 const struct got_error *
192 got_worktree_init(const char *path, struct got_reference *head_ref,
193 const char *prefix, struct got_repository *repo)
195 const struct got_error *err = NULL;
196 struct got_object_id *commit_id = NULL;
197 uuid_t uuid;
198 uint32_t uuid_status;
199 int obj_type;
200 char *path_got = NULL;
201 char *refstr = NULL;
202 char *formatstr = NULL;
203 char *absprefix = NULL;
204 char *basestr = NULL;
205 char *uuidstr = NULL;
207 err = got_ref_resolve(&commit_id, repo, head_ref);
208 if (err)
209 return err;
210 err = got_object_get_type(&obj_type, repo, commit_id);
211 if (err)
212 return err;
213 if (obj_type != GOT_OBJ_TYPE_COMMIT)
214 return got_error(GOT_ERR_OBJ_TYPE);
216 if (!got_path_is_absolute(prefix)) {
217 if (asprintf(&absprefix, "/%s", prefix) == -1)
218 return got_error_from_errno();
221 /* Create top-level directory (may already exist). */
222 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
223 err = got_error_from_errno();
224 goto done;
227 /* Create .got directory (may already exist). */
228 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
229 err = got_error_from_errno();
230 goto done;
232 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
233 err = got_error_from_errno();
234 goto done;
237 /* Create an empty lock file. */
238 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
239 if (err)
240 goto done;
242 /* Create an empty file index. */
243 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
244 if (err)
245 goto done;
247 /* Write the HEAD reference. */
248 refstr = got_ref_to_str(head_ref);
249 if (refstr == NULL) {
250 err = got_error_from_errno();
251 goto done;
253 err = create_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
254 if (err)
255 goto done;
257 /* Record our base commit. */
258 err = got_object_id_str(&basestr, commit_id);
259 if (err)
260 goto done;
261 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
262 if (err)
263 goto done;
265 /* Store path to repository. */
266 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
267 got_repo_get_path(repo));
268 if (err)
269 goto done;
271 /* Store in-repository path prefix. */
272 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
273 absprefix ? absprefix : prefix);
274 if (err)
275 goto done;
277 /* Generate UUID. */
278 uuid_create(&uuid, &uuid_status);
279 if (uuid_status != uuid_s_ok) {
280 err = got_error_uuid(uuid_status);
281 goto done;
283 uuid_to_string(&uuid, &uuidstr, &uuid_status);
284 if (uuid_status != uuid_s_ok) {
285 err = got_error_uuid(uuid_status);
286 goto done;
288 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
289 if (err)
290 goto done;
292 /* Stamp work tree with format file. */
293 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
294 err = got_error_from_errno();
295 goto done;
297 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
298 if (err)
299 goto done;
301 done:
302 free(commit_id);
303 free(path_got);
304 free(formatstr);
305 free(refstr);
306 free(absprefix);
307 free(basestr);
308 free(uuidstr);
309 return err;
312 static const struct got_error *
313 open_worktree(struct got_worktree **worktree, const char *path)
315 const struct got_error *err = NULL;
316 char *path_got;
317 char *formatstr = NULL;
318 char *uuidstr = NULL;
319 char *path_lock = NULL;
320 char *base_commit_id_str = NULL;
321 char *head_ref_str = NULL;
322 int version, fd = -1;
323 const char *errstr;
324 struct got_repository *repo = NULL;
325 uint32_t uuid_status;
327 *worktree = NULL;
329 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
330 err = got_error_from_errno();
331 path_got = NULL;
332 goto done;
335 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
336 err = got_error_from_errno();
337 path_lock = NULL;
338 goto done;
341 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
342 if (fd == -1) {
343 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
344 : got_error_from_errno());
345 goto done;
348 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
349 if (err)
350 goto done;
352 version = strtonum(formatstr, 1, INT_MAX, &errstr);
353 if (errstr) {
354 err = got_error(GOT_ERR_WORKTREE_META);
355 goto done;
357 if (version != GOT_WORKTREE_FORMAT_VERSION) {
358 err = got_error(GOT_ERR_WORKTREE_VERS);
359 goto done;
362 *worktree = calloc(1, sizeof(**worktree));
363 if (*worktree == NULL) {
364 err = got_error_from_errno();
365 goto done;
367 (*worktree)->lockfd = -1;
369 (*worktree)->root_path = strdup(path);
370 if ((*worktree)->root_path == NULL) {
371 err = got_error_from_errno();
372 goto done;
374 err = read_meta_file(&(*worktree)->repo_path, path_got,
375 GOT_WORKTREE_REPOSITORY);
376 if (err)
377 goto done;
379 err = read_meta_file(&(*worktree)->path_prefix, path_got,
380 GOT_WORKTREE_PATH_PREFIX);
381 if (err)
382 goto done;
384 err = read_meta_file(&base_commit_id_str, path_got,
385 GOT_WORKTREE_BASE_COMMIT);
386 if (err)
387 goto done;
389 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
390 if (err)
391 goto done;
392 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
393 if (uuid_status != uuid_s_ok) {
394 err = got_error_uuid(uuid_status);
395 goto done;
398 err = got_repo_open(&repo, (*worktree)->repo_path);
399 if (err)
400 goto done;
402 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
403 base_commit_id_str);
404 if (err)
405 goto done;
407 err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
408 if (err)
409 goto done;
411 err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
412 done:
413 if (repo)
414 got_repo_close(repo);
415 free(path_got);
416 free(path_lock);
417 free(head_ref_str);
418 free(base_commit_id_str);
419 free(uuidstr);
420 free(formatstr);
421 if (err) {
422 if (fd != -1)
423 close(fd);
424 if (*worktree != NULL)
425 got_worktree_close(*worktree);
426 *worktree = NULL;
427 } else
428 (*worktree)->lockfd = fd;
430 return err;
433 const struct got_error *
434 got_worktree_open(struct got_worktree **worktree, const char *path)
436 const struct got_error *err = NULL;
438 do {
439 err = open_worktree(worktree, path);
440 if (err && (err->code != GOT_ERR_ERRNO && errno != ENOENT))
441 return err;
442 if (*worktree)
443 return NULL;
444 path = dirname(path);
445 if (path == NULL)
446 return got_error_from_errno();
447 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
449 return got_error(GOT_ERR_NOT_WORKTREE);
452 const struct got_error *
453 got_worktree_close(struct got_worktree *worktree)
455 const struct got_error *err = NULL;
456 free(worktree->root_path);
457 free(worktree->repo_path);
458 free(worktree->path_prefix);
459 free(worktree->base_commit_id);
460 if (worktree->head_ref)
461 got_ref_close(worktree->head_ref);
462 if (worktree->lockfd != -1)
463 if (close(worktree->lockfd) != 0)
464 err = got_error_from_errno();
465 free(worktree);
466 return err;
469 const char *
470 got_worktree_get_root_path(struct got_worktree *worktree)
472 return worktree->root_path;
475 const char *
476 got_worktree_get_repo_path(struct got_worktree *worktree)
478 return worktree->repo_path;
481 const char *
482 got_worktree_get_path_prefix(struct got_worktree *worktree)
484 return worktree->path_prefix;
487 const struct got_error *
488 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
489 const char *path_prefix)
491 char *absprefix = NULL;
493 if (!got_path_is_absolute(path_prefix)) {
494 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
495 return got_error_from_errno();
497 *match = (strcmp(absprefix ? absprefix : path_prefix,
498 worktree->path_prefix) == 0);
499 free(absprefix);
500 return NULL;
503 char *
504 got_worktree_get_head_ref_name(struct got_worktree *worktree)
506 return got_ref_to_str(worktree->head_ref);
509 struct got_reference *
510 got_worktree_get_head_ref(struct got_worktree *worktree)
512 return got_ref_dup(worktree->head_ref);
515 struct got_object_id *
516 got_worktree_get_base_commit_id(struct got_worktree *worktree)
518 return worktree->base_commit_id;
521 const struct got_error *
522 got_worktree_set_base_commit_id(struct got_worktree *worktree,
523 struct got_repository *repo, struct got_object_id *commit_id)
525 const struct got_error *err;
526 struct got_object *obj = NULL;
527 char *id_str = NULL;
528 char *path_got = NULL;
530 if (asprintf(&path_got, "%s/%s", worktree->root_path,
531 GOT_WORKTREE_GOT_DIR) == -1) {
532 err = got_error_from_errno();
533 path_got = NULL;
534 goto done;
537 err = got_object_open(&obj, repo, commit_id);
538 if (err)
539 return err;
541 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
542 err = got_error(GOT_ERR_OBJ_TYPE);
543 goto done;
546 /* Record our base commit. */
547 err = got_object_id_str(&id_str, commit_id);
548 if (err)
549 goto done;
550 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
551 if (err)
552 goto done;
554 free(worktree->base_commit_id);
555 worktree->base_commit_id = got_object_id_dup(commit_id);
556 if (worktree->base_commit_id == NULL) {
557 err = got_error_from_errno();
558 goto done;
560 done:
561 if (obj)
562 got_object_close(obj);
563 free(id_str);
564 free(path_got);
565 return err;
568 static const struct got_error *
569 lock_worktree(struct got_worktree *worktree, int operation)
571 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
572 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
573 : got_error_from_errno());
574 return NULL;
577 static const struct got_error *
578 make_parent_dirs(const char *abspath)
580 const struct got_error *err = NULL;
582 char *parent = dirname(abspath);
583 if (parent == NULL)
584 return NULL;
586 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
587 if (errno == ENOENT) {
588 err = make_parent_dirs(parent);
589 if (err)
590 return err;
591 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1)
592 return got_error_from_errno();
593 } else
594 err = got_error_from_errno();
597 return err;
600 static const struct got_error *
601 add_dir_on_disk(struct got_worktree *worktree, const char *path)
603 const struct got_error *err = NULL;
604 char *abspath;
606 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
607 return got_error_from_errno();
609 /* XXX queue work rather than editing disk directly? */
610 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
611 struct stat sb;
613 if (errno == EEXIST) {
614 if (lstat(abspath, &sb) == -1) {
615 err = got_error_from_errno();
616 goto done;
619 if (!S_ISDIR(sb.st_mode)) {
620 /* TODO directory is obstructed; do something */
621 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
622 goto done;
625 return NULL;
626 } else if (errno == ENOENT) {
627 err = make_parent_dirs(abspath);
628 if (err)
629 goto done;
630 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1)
631 err = got_error_from_errno();
632 } else
633 err = got_error_from_errno();
636 done:
637 free(abspath);
638 return err;
641 static const struct got_error *
642 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
644 const struct got_error *err = NULL;
645 uint8_t fbuf1[8192];
646 uint8_t fbuf2[8192];
647 size_t flen1 = 0, flen2 = 0;
649 *same = 1;
651 while (1) {
652 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
653 if (flen1 == 0 && ferror(f1)) {
654 err = got_error_from_errno();
655 break;
657 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
658 if (flen2 == 0 && ferror(f2)) {
659 err = got_error_from_errno();
660 break;
662 if (flen1 == 0) {
663 if (flen2 != 0)
664 *same = 0;
665 break;
666 } else if (flen2 == 0) {
667 if (flen1 != 0)
668 *same = 0;
669 break;
670 } else if (flen1 == flen2) {
671 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
672 *same = 0;
673 break;
675 } else {
676 *same = 0;
677 break;
681 return err;
684 static const struct got_error *
685 check_files_equal(int *same, const char *f1_path, const char *f2_path)
687 const struct got_error *err = NULL;
688 struct stat sb;
689 size_t size1, size2;
690 FILE *f1 = NULL, *f2 = NULL;
692 *same = 1;
694 if (lstat(f1_path, &sb) != 0) {
695 err = got_error_from_errno();
696 goto done;
698 size1 = sb.st_size;
700 if (lstat(f2_path, &sb) != 0) {
701 err = got_error_from_errno();
702 goto done;
704 size2 = sb.st_size;
706 if (size1 != size2) {
707 *same = 0;
708 return NULL;
711 f1 = fopen(f1_path, "r");
712 if (f1 == NULL)
713 return got_error_from_errno();
715 f2 = fopen(f2_path, "r");
716 if (f2 == NULL) {
717 err = got_error_from_errno();
718 goto done;
721 err = check_file_contents_equal(same, f1, f2);
722 done:
723 if (f1 && fclose(f1) != 0 && err == NULL)
724 err = got_error_from_errno();
725 if (f2 && fclose(f2) != 0 && err == NULL)
726 err = got_error_from_errno();
728 return err;
731 /*
732 * Perform a 3-way merge where the file's version in the file index (blob2)
733 * acts as the common ancestor, the incoming blob (blob1) acts as the first
734 * derived version, and the file on disk acts as the second derived version.
735 */
736 static const struct got_error *
737 merge_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
738 struct got_fileindex_entry *ie, const char *ondisk_path, const char *path,
739 uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob1,
740 struct got_repository *repo,
741 got_worktree_checkout_cb progress_cb, void *progress_arg)
743 const struct got_error *err = NULL;
744 int merged_fd = -1;
745 struct got_blob_object *blob2 = NULL;
746 FILE *f1 = NULL, *f2 = NULL;
747 char *blob1_path = NULL, *blob2_path = NULL;
748 char *merged_path = NULL, *base_path = NULL;
749 struct got_object_id id2;
750 char *id_str = NULL;
751 char *label1 = NULL;
752 int overlapcnt = 0, update_timestamps = 0;
753 char *parent;
755 parent = dirname(ondisk_path);
756 if (parent == NULL)
757 return got_error_from_errno();
759 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
760 return got_error_from_errno();
762 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
763 if (err)
764 goto done;
766 free(base_path);
767 if (asprintf(&base_path, "%s/got-merge-blob1", parent) == -1) {
768 err = got_error_from_errno();
769 base_path = NULL;
770 goto done;
773 err = got_opentemp_named(&blob1_path, &f1, base_path);
774 if (err)
775 goto done;
776 err = got_object_blob_dump_to_file(NULL, NULL, f1, blob1);
777 if (err)
778 goto done;
780 free(base_path);
781 if (asprintf(&base_path, "%s/got-merge-blob2", parent) == -1) {
782 err = got_error_from_errno();
783 base_path = NULL;
784 goto done;
787 err = got_opentemp_named(&blob2_path, &f2, base_path);
788 if (err)
789 goto done;
791 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
792 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
793 if (err)
794 goto done;
795 err = got_object_blob_dump_to_file(NULL, NULL, f2, blob2);
796 if (err)
797 goto done;
799 err = got_object_id_str(&id_str, worktree->base_commit_id);
800 if (err)
801 goto done;
802 if (asprintf(&label1, "commit %s", id_str) == -1) {
803 err = got_error_from_errno();
804 goto done;
807 err = got_merge_diff3(&overlapcnt, merged_fd, blob1_path,
808 blob2_path, ondisk_path, label1, path);
809 if (err)
810 goto done;
812 (*progress_cb)(progress_arg,
813 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
816 if (fsync(merged_fd) != 0) {
817 err = got_error_from_errno();
818 goto done;
821 /* Check if a clean merge has subsumed all local changes. */
822 if (overlapcnt == 0) {
823 err = check_files_equal(&update_timestamps, blob1_path,
824 merged_path);
825 if (err)
826 goto done;
829 if (chmod(merged_path, st_mode) != 0) {
830 err = got_error_from_errno();
831 goto done;
834 if (rename(merged_path, ondisk_path) != 0) {
835 err = got_error_from_errno();
836 unlink(merged_path);
837 goto done;
840 /*
841 * Do not update timestamps of already modified files. Otherwise,
842 * a future status walk would treat them as unmodified files again.
843 */
844 err = got_fileindex_entry_update(ie, ondisk_path,
845 blob1->id.sha1, worktree->base_commit_id->sha1, update_timestamps);
846 done:
847 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
848 err = got_error_from_errno();
849 if (f1 && fclose(f1) != 0 && err == NULL)
850 err = got_error_from_errno();
851 if (f2 && fclose(f2) != 0 && err == NULL)
852 err = got_error_from_errno();
853 if (blob2)
854 got_object_blob_close(blob2);
855 free(merged_path);
856 free(base_path);
857 if (blob1_path) {
858 unlink(blob1_path);
859 free(blob1_path);
861 if (blob2_path) {
862 unlink(blob2_path);
863 free(blob2_path);
865 free(id_str);
866 free(label1);
867 return err;
870 static const struct got_error *
871 install_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
872 struct got_fileindex_entry *entry, const char *ondisk_path, const char *path,
873 uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob,
874 int restoring_missing_file, struct got_repository *repo,
875 got_worktree_checkout_cb progress_cb, void *progress_arg)
877 const struct got_error *err = NULL;
878 int fd = -1;
879 size_t len, hdrlen;
880 int update = 0;
881 char *tmppath = NULL;
883 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
884 GOT_DEFAULT_FILE_MODE);
885 if (fd == -1) {
886 if (errno == ENOENT) {
887 char *parent = dirname(path);
888 if (parent == NULL)
889 return got_error_from_errno();
890 err = add_dir_on_disk(worktree, parent);
891 if (err)
892 return err;
893 fd = open(ondisk_path,
894 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
895 GOT_DEFAULT_FILE_MODE);
896 if (fd == -1)
897 return got_error_from_errno();
898 } else if (errno == EEXIST) {
899 if (!S_ISREG(st_mode)) {
900 /* TODO file is obstructed; do something */
901 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
902 goto done;
903 } else {
904 err = got_opentemp_named_fd(&tmppath, &fd,
905 ondisk_path);
906 if (err)
907 goto done;
908 update = 1;
910 } else
911 return got_error_from_errno();
914 if (restoring_missing_file)
915 (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
916 else
917 (*progress_cb)(progress_arg,
918 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
920 hdrlen = got_object_blob_get_hdrlen(blob);
921 do {
922 const uint8_t *buf = got_object_blob_get_read_buf(blob);
923 err = got_object_blob_read_block(&len, blob);
924 if (err)
925 break;
926 if (len > 0) {
927 /* Skip blob object header first time around. */
928 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
929 if (outlen == -1) {
930 err = got_error_from_errno();
931 goto done;
932 } else if (outlen != len - hdrlen) {
933 err = got_error(GOT_ERR_IO);
934 goto done;
936 hdrlen = 0;
938 } while (len != 0);
940 if (fsync(fd) != 0) {
941 err = got_error_from_errno();
942 goto done;
945 if (update) {
946 if (rename(tmppath, ondisk_path) != 0) {
947 err = got_error_from_errno();
948 unlink(tmppath);
949 goto done;
953 if (te_mode & S_IXUSR) {
954 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
955 err = got_error_from_errno();
956 goto done;
958 } else {
959 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
960 err = got_error_from_errno();
961 goto done;
965 if (entry == NULL)
966 entry = got_fileindex_entry_get(fileindex, path);
967 if (entry)
968 err = got_fileindex_entry_update(entry, ondisk_path,
969 blob->id.sha1, worktree->base_commit_id->sha1, 1);
970 else {
971 err = got_fileindex_entry_alloc(&entry, ondisk_path,
972 path, blob->id.sha1, worktree->base_commit_id->sha1);
973 if (err)
974 goto done;
975 err = got_fileindex_entry_add(fileindex, entry);
977 done:
978 if (fd != -1 && close(fd) != 0 && err == NULL)
979 err = got_error_from_errno();
980 free(tmppath);
981 return err;
984 static const struct got_error *
985 get_file_status(unsigned char *status, struct stat *sb,
986 struct got_fileindex_entry *ie, const char *abspath,
987 struct got_repository *repo)
989 const struct got_error *err = NULL;
990 struct got_object_id id;
991 size_t hdrlen;
992 FILE *f = NULL;
993 uint8_t fbuf[8192];
994 struct got_blob_object *blob = NULL;
995 size_t flen, blen;
997 *status = GOT_STATUS_NO_CHANGE;
999 if (lstat(abspath, sb) == -1) {
1000 if (errno == ENOENT) {
1001 if (ie) {
1002 *status = GOT_STATUS_MISSING;
1003 sb->st_mode =
1004 ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
1005 & (S_IRWXU | S_IRWXG | S_IRWXO));
1006 } else
1007 sb->st_mode = GOT_DEFAULT_FILE_MODE;
1008 return NULL;
1010 return got_error_from_errno();
1013 if (!S_ISREG(sb->st_mode)) {
1014 *status = GOT_STATUS_OBSTRUCTED;
1015 return NULL;
1018 if (ie == NULL)
1019 return NULL;
1021 if (ie->ctime_sec == sb->st_ctime &&
1022 ie->ctime_nsec == sb->st_ctimensec &&
1023 ie->mtime_sec == sb->st_mtime &&
1024 ie->mtime_sec == sb->st_mtime &&
1025 ie->mtime_nsec == sb->st_mtimensec &&
1026 ie->size == (sb->st_size & 0xffffffff))
1027 return NULL;
1029 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1030 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1031 if (err)
1032 return err;
1034 f = fopen(abspath, "r");
1035 if (f == NULL) {
1036 err = got_error_from_errno();
1037 goto done;
1039 hdrlen = got_object_blob_get_hdrlen(blob);
1040 while (1) {
1041 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1042 err = got_object_blob_read_block(&blen, blob);
1043 if (err)
1044 break;
1045 /* Skip length of blob object header first time around. */
1046 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1047 if (flen == 0 && ferror(f)) {
1048 err = got_error_from_errno();
1049 break;
1051 if (blen == 0) {
1052 if (flen != 0)
1053 *status = GOT_STATUS_MODIFY;
1054 break;
1055 } else if (flen == 0) {
1056 if (blen != 0)
1057 *status = GOT_STATUS_MODIFY;
1058 break;
1059 } else if (blen - hdrlen == flen) {
1060 /* Skip blob object header first time around. */
1061 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1062 *status = GOT_STATUS_MODIFY;
1063 break;
1065 } else {
1066 *status = GOT_STATUS_MODIFY;
1067 break;
1069 hdrlen = 0;
1071 done:
1072 if (blob)
1073 got_object_blob_close(blob);
1074 if (f)
1075 fclose(f);
1076 return err;
1079 static const struct got_error *
1080 update_blob(struct got_worktree *worktree,
1081 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1082 struct got_tree_entry *te, const char *path,
1083 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1084 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1086 const struct got_error *err = NULL;
1087 struct got_blob_object *blob = NULL;
1088 char *ondisk_path;
1089 unsigned char status = GOT_STATUS_NO_CHANGE;
1090 struct stat sb;
1092 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1093 return got_error_from_errno();
1095 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1096 if (err)
1097 goto done;
1099 if (status == GOT_STATUS_OBSTRUCTED) {
1100 (*progress_cb)(progress_arg, status, path);
1101 goto done;
1104 if (ie && status != GOT_STATUS_MISSING) {
1105 if (memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1106 SHA1_DIGEST_LENGTH) == 0) {
1107 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1108 path);
1109 goto done;
1111 if (memcmp(ie->blob_sha1,
1112 te->id->sha1, SHA1_DIGEST_LENGTH) == 0)
1113 goto done;
1116 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1117 if (err)
1118 goto done;
1120 if (status == GOT_STATUS_MODIFY)
1121 err = merge_blob(worktree, fileindex, ie, ondisk_path, path,
1122 te->mode, sb.st_mode, blob, repo, progress_cb,
1123 progress_arg);
1124 else
1125 err = install_blob(worktree, fileindex, ie, ondisk_path, path,
1126 te->mode, sb.st_mode, blob, status == GOT_STATUS_MISSING,
1127 repo, progress_cb, progress_arg);
1129 got_object_blob_close(blob);
1130 done:
1131 free(ondisk_path);
1132 return err;
1135 static const struct got_error *
1136 remove_ondisk_file(const char *root_path, const char *path)
1138 const struct got_error *err = NULL;
1139 char *ondisk_path = NULL;
1141 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1142 return got_error_from_errno();
1144 if (unlink(ondisk_path) == -1) {
1145 if (errno != ENOENT)
1146 err = got_error_from_errno();
1147 } else {
1148 char *parent = dirname(ondisk_path);
1149 while (parent && strcmp(parent, root_path) != 0) {
1150 if (rmdir(parent) == -1) {
1151 if (errno != ENOTEMPTY)
1152 err = got_error_from_errno();
1153 break;
1155 parent = dirname(parent);
1158 free(ondisk_path);
1159 return err;
1162 struct diff_cb_arg {
1163 struct got_fileindex *fileindex;
1164 struct got_worktree *worktree;
1165 struct got_repository *repo;
1166 got_worktree_checkout_cb progress_cb;
1167 void *progress_arg;
1168 got_worktree_cancel_cb cancel_cb;
1169 void *cancel_arg;
1172 static const struct got_error *
1173 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1174 struct got_tree_entry *te, const char *parent_path)
1176 struct diff_cb_arg *a = arg;
1178 return update_blob(a->worktree, a->fileindex, ie, te,
1179 ie->path, a->repo, a->progress_cb, a->progress_arg,
1180 a->cancel_cb, a->cancel_arg);
1183 static const struct got_error *
1184 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1186 const struct got_error *err;
1187 struct diff_cb_arg *a = arg;
1189 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE, ie->path);
1191 err = remove_ondisk_file(a->worktree->root_path, ie->path);
1192 if (err)
1193 return err;
1194 got_fileindex_entry_remove(a->fileindex, ie);
1195 return NULL;
1198 static const struct got_error *
1199 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1201 struct diff_cb_arg *a = arg;
1202 const struct got_error *err;
1203 char *path;
1205 if (asprintf(&path, "%s%s%s", parent_path,
1206 parent_path[0] ? "/" : "", te->name)
1207 == -1)
1208 return got_error_from_errno();
1210 if (S_ISDIR(te->mode))
1211 err = add_dir_on_disk(a->worktree, path);
1212 else
1213 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1214 a->repo, a->progress_cb, a->progress_arg,
1215 a->cancel_cb, a->cancel_arg);
1217 free(path);
1218 return err;
1221 const struct got_error *
1222 got_worktree_checkout_files(struct got_worktree *worktree,
1223 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1224 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1226 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1227 struct got_commit_object *commit = NULL;
1228 struct got_object_id *tree_id = NULL;
1229 struct got_tree_object *tree = NULL;
1230 char *fileindex_path = NULL, *new_fileindex_path = NULL;
1231 struct got_fileindex *fileindex = NULL;
1232 FILE *index = NULL, *new_index = NULL;
1233 struct got_fileindex_diff_tree_cb diff_cb;
1234 struct diff_cb_arg arg;
1236 err = lock_worktree(worktree, LOCK_EX);
1237 if (err)
1238 return err;
1240 fileindex = got_fileindex_alloc();
1241 if (fileindex == NULL) {
1242 err = got_error_from_errno();
1243 goto done;
1246 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1247 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1248 err = got_error_from_errno();
1249 fileindex_path = NULL;
1250 goto done;
1254 * Read the file index.
1255 * Checking out files is supposed to be an idempotent operation.
1256 * If the on-disk file index is incomplete we will try to complete it.
1258 index = fopen(fileindex_path, "rb");
1259 if (index == NULL) {
1260 if (errno != ENOENT) {
1261 err = got_error_from_errno();
1262 goto done;
1264 } else {
1265 err = got_fileindex_read(fileindex, index);
1266 fclose(index);
1267 if (err)
1268 goto done;
1271 err = got_opentemp_named(&new_fileindex_path, &new_index,
1272 fileindex_path);
1273 if (err)
1274 goto done;
1276 err = got_object_open_as_commit(&commit, repo,
1277 worktree->base_commit_id);
1278 if (err)
1279 goto done;
1281 err = got_object_id_by_path(&tree_id, repo,
1282 worktree->base_commit_id, worktree->path_prefix);
1283 if (err)
1284 goto done;
1286 err = got_object_open_as_tree(&tree, repo, tree_id);
1287 if (err)
1288 goto done;
1290 diff_cb.diff_old_new = diff_old_new;
1291 diff_cb.diff_old = diff_old;
1292 diff_cb.diff_new = diff_new;
1293 arg.fileindex = fileindex;
1294 arg.worktree = worktree;
1295 arg.repo = repo;
1296 arg.progress_cb = progress_cb;
1297 arg.progress_arg = progress_arg;
1298 arg.cancel_cb = cancel_cb;
1299 arg.cancel_arg = cancel_arg;
1300 checkout_err = got_fileindex_diff_tree(fileindex, tree, repo,
1301 &diff_cb, &arg);
1303 /* Try to sync the fileindex back to disk in any case. */
1304 err = got_fileindex_write(fileindex, new_index);
1305 if (err)
1306 goto done;
1308 if (rename(new_fileindex_path, fileindex_path) != 0) {
1309 err = got_error_from_errno();
1310 unlink(new_fileindex_path);
1311 goto done;
1314 free(new_fileindex_path);
1315 new_fileindex_path = NULL;
1317 done:
1318 if (tree)
1319 got_object_tree_close(tree);
1320 if (commit)
1321 got_object_commit_close(commit);
1322 if (new_fileindex_path)
1323 unlink(new_fileindex_path);
1324 if (new_index)
1325 fclose(new_index);
1326 free(new_fileindex_path);
1327 free(fileindex_path);
1328 got_fileindex_free(fileindex);
1329 if (checkout_err)
1330 err = checkout_err;
1331 unlockerr = lock_worktree(worktree, LOCK_SH);
1332 if (unlockerr && err == NULL)
1333 err = unlockerr;
1334 return err;
1337 struct diff_dir_cb_arg {
1338 struct got_fileindex *fileindex;
1339 struct got_worktree *worktree;
1340 const char *status_path;
1341 size_t status_path_len;
1342 struct got_repository *repo;
1343 got_worktree_status_cb status_cb;
1344 void *status_arg;
1345 got_worktree_cancel_cb cancel_cb;
1346 void *cancel_arg;
1349 static const struct got_error *
1350 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1351 got_worktree_status_cb status_cb, void *status_arg,
1352 struct got_repository *repo)
1354 const struct got_error *err = NULL;
1355 unsigned char status = GOT_STATUS_NO_CHANGE;
1356 struct stat sb;
1357 struct got_object_id id;
1359 err = get_file_status(&status, &sb, ie, abspath, repo);
1360 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1361 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1362 err = (*status_cb)(status_arg, status, ie->path, &id);
1364 return err;
1367 static const struct got_error *
1368 status_old_new(void *arg, struct got_fileindex_entry *ie,
1369 struct dirent *de, const char *parent_path)
1371 const struct got_error *err = NULL;
1372 struct diff_dir_cb_arg *a = arg;
1373 char *abspath;
1375 if (got_path_cmp(parent_path, a->status_path) != 0 &&
1376 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1377 return NULL;
1379 if (parent_path[0]) {
1380 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1381 parent_path, de->d_name) == -1)
1382 return got_error_from_errno();
1383 } else {
1384 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1385 de->d_name) == -1)
1386 return got_error_from_errno();
1389 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1390 a->repo);
1391 free(abspath);
1392 return err;
1395 static const struct got_error *
1396 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1398 struct diff_dir_cb_arg *a = arg;
1399 struct got_object_id id;
1401 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1402 return NULL;
1404 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1405 return (*a->status_cb)(a->status_arg, GOT_STATUS_MISSING, ie->path,
1406 &id);
1409 static const struct got_error *
1410 status_new(void *arg, struct dirent *de, const char *parent_path)
1412 const struct got_error *err = NULL;
1413 struct diff_dir_cb_arg *a = arg;
1414 char *path = NULL;
1416 if (de->d_type == DT_DIR)
1417 return NULL;
1419 /* XXX ignore symlinks for now */
1420 if (de->d_type == DT_LNK)
1421 return NULL;
1423 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1424 return NULL;
1426 if (parent_path[0]) {
1427 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1428 return got_error_from_errno();
1429 } else {
1430 path = de->d_name;
1433 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1434 NULL);
1435 if (parent_path[0])
1436 free(path);
1437 return err;
1440 const struct got_error *
1441 got_worktree_status(struct got_worktree *worktree, const char *path,
1442 struct got_repository *repo, got_worktree_status_cb status_cb,
1443 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1445 const struct got_error *err = NULL;
1446 DIR *workdir = NULL;
1447 char *fileindex_path = NULL;
1448 struct got_fileindex *fileindex = NULL;
1449 FILE *index = NULL;
1450 struct got_fileindex_diff_dir_cb fdiff_cb;
1451 struct diff_dir_cb_arg arg;
1452 char *ondisk_path = NULL;
1454 fileindex = got_fileindex_alloc();
1455 if (fileindex == NULL) {
1456 err = got_error_from_errno();
1457 goto done;
1460 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1461 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1462 err = got_error_from_errno();
1463 fileindex_path = NULL;
1464 goto done;
1467 index = fopen(fileindex_path, "rb");
1468 if (index == NULL) {
1469 if (errno != ENOENT) {
1470 err = got_error_from_errno();
1471 goto done;
1473 } else {
1474 err = got_fileindex_read(fileindex, index);
1475 fclose(index);
1476 if (err)
1477 goto done;
1480 if (asprintf(&ondisk_path, "%s%s%s",
1481 worktree->root_path, path[0] ? "/" : "", path) == -1) {
1482 err = got_error_from_errno();
1483 goto done;
1485 workdir = opendir(ondisk_path);
1486 if (workdir == NULL) {
1487 if (errno == ENOTDIR) {
1488 struct got_fileindex_entry *ie;
1489 ie = got_fileindex_entry_get(fileindex, path);
1490 if (ie == NULL) {
1491 err = got_error(GOT_ERR_BAD_PATH);
1492 goto done;
1494 err = report_file_status(ie, ondisk_path,
1495 status_cb, status_arg, repo);
1496 goto done;
1497 } else {
1498 err = got_error_from_errno();
1499 goto done;
1502 fdiff_cb.diff_old_new = status_old_new;
1503 fdiff_cb.diff_old = status_old;
1504 fdiff_cb.diff_new = status_new;
1505 arg.fileindex = fileindex;
1506 arg.worktree = worktree;
1507 arg.status_path = path;
1508 arg.status_path_len = strlen(path);
1509 arg.repo = repo;
1510 arg.status_cb = status_cb;
1511 arg.status_arg = status_arg;
1512 arg.cancel_cb = cancel_cb;
1513 arg.cancel_arg = cancel_arg;
1514 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1515 path, repo, &fdiff_cb, &arg);
1516 done:
1517 if (workdir)
1518 closedir(workdir);
1519 free(ondisk_path);
1520 free(fileindex_path);
1521 got_fileindex_free(fileindex);
1522 return err;