Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/limits.h>
19 #include <sys/queue.h>
20 #include <sys/tree.h>
22 #include <dirent.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <fcntl.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <sha1.h>
31 #include <zlib.h>
32 #include <fnmatch.h>
33 #include <libgen.h>
35 #include "got_error.h"
36 #include "got_repository.h"
37 #include "got_reference.h"
38 #include "got_object.h"
39 #include "got_worktree.h"
40 #include "got_opentemp.h"
42 #include "got_lib_worktree.h"
43 #include "got_lib_path.h"
44 #include "got_lib_sha1.h"
45 #include "got_lib_fileindex.h"
46 #include "got_lib_inflate.h"
47 #include "got_lib_delta.h"
48 #include "got_lib_object.h"
49 #include "got_lib_diff.h"
51 #ifndef MIN
52 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
53 #endif
55 static const struct got_error *
56 create_meta_file(const char *path_got, const char *name, const char *content)
57 {
58 const struct got_error *err = NULL;
59 char *path;
60 int fd = -1;
62 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
63 err = got_error_from_errno();
64 path = NULL;
65 goto done;
66 }
68 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
69 GOT_DEFAULT_FILE_MODE);
70 if (fd == -1) {
71 err = got_error_from_errno();
72 goto done;
73 }
75 if (content) {
76 int len = dprintf(fd, "%s\n", content);
77 if (len != strlen(content) + 1) {
78 err = got_error_from_errno();
79 goto done;
80 }
81 }
83 done:
84 if (fd != -1 && close(fd) == -1 && err == NULL)
85 err = got_error_from_errno();
86 free(path);
87 return err;
88 }
90 static const struct got_error *
91 update_meta_file(const char *path_got, const char *name, const char *content)
92 {
93 const struct got_error *err = NULL;
94 FILE *tmpfile = NULL;
95 char *tmppath = NULL;
96 char *path = NULL;
98 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
99 err = got_error_from_errno();
100 path = NULL;
101 goto done;
104 err = got_opentemp_named(&tmppath, &tmpfile, path);
105 if (err)
106 goto done;
108 if (content) {
109 int len = fprintf(tmpfile, "%s\n", content);
110 if (len != strlen(content) + 1) {
111 err = got_error_from_errno();
112 goto done;
116 if (rename(tmppath, path) != 0) {
117 err = got_error_from_errno();
118 unlink(tmppath);
119 goto done;
122 done:
123 free(tmppath);
124 if (fclose(tmpfile) != 0 && err == NULL)
125 err = got_error_from_errno();
126 return err;
129 static const struct got_error *
130 read_meta_file(char **content, const char *path_got, const char *name)
132 const struct got_error *err = NULL;
133 char *path;
134 int fd = -1;
135 ssize_t n;
136 struct stat sb;
138 *content = NULL;
140 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
141 err = got_error_from_errno();
142 path = NULL;
143 goto done;
146 fd = open(path, O_RDONLY | O_NOFOLLOW);
147 if (fd == -1) {
148 err = got_error_from_errno();
149 goto done;
151 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
152 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
153 : got_error_from_errno());
154 goto done;
157 if (lstat(path, &sb) != 0) {
158 err = got_error_from_errno();
159 goto done;
161 *content = calloc(1, sb.st_size);
162 if (*content == NULL) {
163 err = got_error_from_errno();
164 goto done;
167 n = read(fd, *content, sb.st_size);
168 if (n != sb.st_size) {
169 err = (n == -1 ? got_error_from_errno() :
170 got_error(GOT_ERR_WORKTREE_META));
171 goto done;
173 if ((*content)[sb.st_size - 1] != '\n') {
174 err = got_error(GOT_ERR_WORKTREE_META);
175 goto done;
177 (*content)[sb.st_size - 1] = '\0';
179 done:
180 if (fd != -1 && close(fd) == -1 && err == NULL)
181 err = got_error_from_errno();
182 free(path);
183 if (err) {
184 free(*content);
185 *content = NULL;
187 return err;
190 const struct got_error *
191 got_worktree_init(const char *path, struct got_reference *head_ref,
192 const char *prefix, struct got_repository *repo)
194 const struct got_error *err = NULL;
195 struct got_object_id *commit_id = NULL;
196 int obj_type;
197 char *path_got = NULL;
198 char *refstr = NULL;
199 char *formatstr = NULL;
200 char *absprefix = NULL;
201 char *basestr = NULL;
203 err = got_ref_resolve(&commit_id, repo, head_ref);
204 if (err)
205 return err;
206 err = got_object_get_type(&obj_type, repo, commit_id);
207 if (err)
208 return err;
209 if (obj_type != GOT_OBJ_TYPE_COMMIT)
210 return got_error(GOT_ERR_OBJ_TYPE);
212 if (!got_path_is_absolute(prefix)) {
213 if (asprintf(&absprefix, "/%s", prefix) == -1)
214 return got_error_from_errno();
217 /* Create top-level directory (may already exist). */
218 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
219 err = got_error_from_errno();
220 goto done;
223 /* Create .got directory (may already exist). */
224 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
225 err = got_error_from_errno();
226 goto done;
228 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
229 err = got_error_from_errno();
230 goto done;
233 /* Create an empty lock file. */
234 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
235 if (err)
236 goto done;
238 /* Create an empty file index. */
239 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
240 if (err)
241 goto done;
243 /* Write the HEAD reference. */
244 refstr = got_ref_to_str(head_ref);
245 if (refstr == NULL) {
246 err = got_error_from_errno();
247 goto done;
249 err = create_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
250 if (err)
251 goto done;
253 /* Record our base commit. */
254 err = got_object_id_str(&basestr, commit_id);
255 if (err)
256 goto done;
257 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
258 if (err)
259 goto done;
261 /* Store path to repository. */
262 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
263 got_repo_get_path(repo));
264 if (err)
265 goto done;
267 /* Store in-repository path prefix. */
268 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
269 absprefix ? absprefix : prefix);
270 if (err)
271 goto done;
273 /* Stamp work tree with format file. */
274 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
275 err = got_error_from_errno();
276 goto done;
278 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
279 if (err)
280 goto done;
282 done:
283 free(commit_id);
284 free(path_got);
285 free(formatstr);
286 free(refstr);
287 free(absprefix);
288 free(basestr);
289 return err;
292 static const struct got_error *
293 open_worktree(struct got_worktree **worktree, const char *path)
295 const struct got_error *err = NULL;
296 char *path_got;
297 char *formatstr = NULL;
298 char *path_lock = NULL;
299 char *base_commit_id_str = NULL;
300 char *head_ref_str = NULL;
301 int version, fd = -1;
302 const char *errstr;
303 struct got_repository *repo = NULL;
305 *worktree = NULL;
307 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
308 err = got_error_from_errno();
309 path_got = NULL;
310 goto done;
313 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
314 err = got_error_from_errno();
315 path_lock = NULL;
316 goto done;
319 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
320 if (fd == -1) {
321 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
322 : got_error_from_errno());
323 goto done;
326 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
327 if (err)
328 goto done;
330 version = strtonum(formatstr, 1, INT_MAX, &errstr);
331 if (errstr) {
332 err = got_error(GOT_ERR_WORKTREE_META);
333 goto done;
335 if (version != GOT_WORKTREE_FORMAT_VERSION) {
336 err = got_error(GOT_ERR_WORKTREE_VERS);
337 goto done;
340 *worktree = calloc(1, sizeof(**worktree));
341 if (*worktree == NULL) {
342 err = got_error_from_errno();
343 goto done;
345 (*worktree)->lockfd = -1;
347 (*worktree)->root_path = strdup(path);
348 if ((*worktree)->root_path == NULL) {
349 err = got_error_from_errno();
350 goto done;
352 err = read_meta_file(&(*worktree)->repo_path, path_got,
353 GOT_WORKTREE_REPOSITORY);
354 if (err)
355 goto done;
357 err = read_meta_file(&(*worktree)->path_prefix, path_got,
358 GOT_WORKTREE_PATH_PREFIX);
359 if (err)
360 goto done;
362 err = read_meta_file(&base_commit_id_str, path_got,
363 GOT_WORKTREE_BASE_COMMIT);
364 if (err)
365 goto done;
367 err = got_repo_open(&repo, (*worktree)->repo_path);
368 if (err)
369 goto done;
371 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
372 base_commit_id_str);
373 if (err)
374 goto done;
376 err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
377 if (err)
378 goto done;
380 err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
381 done:
382 if (repo)
383 got_repo_close(repo);
384 free(path_got);
385 free(path_lock);
386 free(head_ref_str);
387 free(base_commit_id_str);
388 if (err) {
389 if (fd != -1)
390 close(fd);
391 if (*worktree != NULL)
392 got_worktree_close(*worktree);
393 *worktree = NULL;
394 } else
395 (*worktree)->lockfd = fd;
397 return err;
400 const struct got_error *
401 got_worktree_open(struct got_worktree **worktree, const char *path)
403 const struct got_error *err = NULL;
405 do {
406 err = open_worktree(worktree, path);
407 if (err && (err->code != GOT_ERR_ERRNO && errno != ENOENT))
408 return err;
409 if (*worktree)
410 return NULL;
411 path = dirname(path);
412 if (path == NULL)
413 return got_error_from_errno();
414 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
416 return got_error(GOT_ERR_NOT_WORKTREE);
419 const struct got_error *
420 got_worktree_close(struct got_worktree *worktree)
422 const struct got_error *err = NULL;
423 free(worktree->root_path);
424 free(worktree->repo_path);
425 free(worktree->path_prefix);
426 free(worktree->base_commit_id);
427 if (worktree->head_ref)
428 got_ref_close(worktree->head_ref);
429 if (worktree->lockfd != -1)
430 if (close(worktree->lockfd) != 0)
431 err = got_error_from_errno();
432 free(worktree);
433 return err;
436 const char *
437 got_worktree_get_root_path(struct got_worktree *worktree)
439 return worktree->root_path;
442 const char *
443 got_worktree_get_repo_path(struct got_worktree *worktree)
445 return worktree->repo_path;
448 const char *
449 got_worktree_get_path_prefix(struct got_worktree *worktree)
451 return worktree->path_prefix;
454 const struct got_error *
455 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
456 const char *path_prefix)
458 char *absprefix = NULL;
460 if (!got_path_is_absolute(path_prefix)) {
461 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
462 return got_error_from_errno();
464 *match = (strcmp(absprefix ? absprefix : path_prefix,
465 worktree->path_prefix) == 0);
466 free(absprefix);
467 return NULL;
470 char *
471 got_worktree_get_head_ref_name(struct got_worktree *worktree)
473 return got_ref_to_str(worktree->head_ref);
476 struct got_reference *
477 got_worktree_get_head_ref(struct got_worktree *worktree)
479 return got_ref_dup(worktree->head_ref);
482 struct got_object_id *
483 got_worktree_get_base_commit_id(struct got_worktree *worktree)
485 return worktree->base_commit_id;
488 const struct got_error *
489 got_worktree_set_base_commit_id(struct got_worktree *worktree,
490 struct got_repository *repo, struct got_object_id *commit_id)
492 const struct got_error *err;
493 struct got_object *obj = NULL;
494 char *id_str = NULL;
495 char *path_got = NULL;
497 if (asprintf(&path_got, "%s/%s", worktree->root_path,
498 GOT_WORKTREE_GOT_DIR) == -1) {
499 err = got_error_from_errno();
500 path_got = NULL;
501 goto done;
504 err = got_object_open(&obj, repo, commit_id);
505 if (err)
506 return err;
508 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
509 err = got_error(GOT_ERR_OBJ_TYPE);
510 goto done;
513 /* Record our base commit. */
514 err = got_object_id_str(&id_str, commit_id);
515 if (err)
516 goto done;
517 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
518 if (err)
519 goto done;
521 free(worktree->base_commit_id);
522 worktree->base_commit_id = got_object_id_dup(commit_id);
523 if (worktree->base_commit_id == NULL) {
524 err = got_error_from_errno();
525 goto done;
527 done:
528 if (obj)
529 got_object_close(obj);
530 free(id_str);
531 free(path_got);
532 return err;
535 static const struct got_error *
536 lock_worktree(struct got_worktree *worktree, int operation)
538 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
539 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
540 : got_error_from_errno());
541 return NULL;
544 static const struct got_error *
545 make_parent_dirs(const char *abspath)
547 const struct got_error *err = NULL;
549 char *parent = dirname(abspath);
550 if (parent == NULL)
551 return NULL;
553 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
554 if (errno == ENOENT) {
555 err = make_parent_dirs(parent);
556 if (err)
557 return err;
558 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1)
559 return got_error_from_errno();
560 } else
561 err = got_error_from_errno();
564 return err;
567 static const struct got_error *
568 add_dir_on_disk(struct got_worktree *worktree, const char *path)
570 const struct got_error *err = NULL;
571 char *abspath;
573 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
574 return got_error_from_errno();
576 /* XXX queue work rather than editing disk directly? */
577 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
578 struct stat sb;
580 if (errno == EEXIST) {
581 if (lstat(abspath, &sb) == -1) {
582 err = got_error_from_errno();
583 goto done;
586 if (!S_ISDIR(sb.st_mode)) {
587 /* TODO directory is obstructed; do something */
588 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
589 goto done;
592 return NULL;
593 } else if (errno == ENOENT) {
594 err = make_parent_dirs(abspath);
595 if (err)
596 goto done;
597 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1)
598 err = got_error_from_errno();
599 } else
600 err = got_error_from_errno();
603 done:
604 free(abspath);
605 return err;
608 static const struct got_error *
609 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
611 const struct got_error *err = NULL;
612 uint8_t fbuf1[8192];
613 uint8_t fbuf2[8192];
614 size_t flen1 = 0, flen2 = 0;
616 *same = 1;
618 while (1) {
619 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
620 if (flen1 == 0 && ferror(f1)) {
621 err = got_error_from_errno();
622 break;
624 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
625 if (flen2 == 0 && ferror(f2)) {
626 err = got_error_from_errno();
627 break;
629 if (flen1 == 0) {
630 if (flen2 != 0)
631 *same = 0;
632 break;
633 } else if (flen2 == 0) {
634 if (flen1 != 0)
635 *same = 0;
636 break;
637 } else if (flen1 == flen2) {
638 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
639 *same = 0;
640 break;
642 } else {
643 *same = 0;
644 break;
648 return err;
651 static const struct got_error *
652 check_files_equal(int *same, const char *f1_path, const char *f2_path)
654 const struct got_error *err = NULL;
655 struct stat sb;
656 size_t size1, size2;
657 FILE *f1 = NULL, *f2 = NULL;
659 *same = 1;
661 if (lstat(f1_path, &sb) != 0) {
662 err = got_error_from_errno();
663 goto done;
665 size1 = sb.st_size;
667 if (lstat(f2_path, &sb) != 0) {
668 err = got_error_from_errno();
669 goto done;
671 size2 = sb.st_size;
673 if (size1 != size2) {
674 *same = 0;
675 return NULL;
678 f1 = fopen(f1_path, "r");
679 if (f1 == NULL)
680 return got_error_from_errno();
682 f2 = fopen(f2_path, "r");
683 if (f2 == NULL) {
684 err = got_error_from_errno();
685 goto done;
688 err = check_file_contents_equal(same, f1, f2);
689 done:
690 if (f1 && fclose(f1) != 0 && err == NULL)
691 err = got_error_from_errno();
692 if (f2 && fclose(f2) != 0 && err == NULL)
693 err = got_error_from_errno();
695 return err;
698 /*
699 * Perform a 3-way merge where the file's version in the file index (blob2)
700 * acts as the common ancestor, the incoming blob (blob1) acts as the first
701 * derived version, and the file on disk acts as the second derived version.
702 */
703 static const struct got_error *
704 merge_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
705 struct got_fileindex_entry *ie, const char *ondisk_path, const char *path,
706 uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob1,
707 struct got_repository *repo,
708 got_worktree_checkout_cb progress_cb, void *progress_arg)
710 const struct got_error *err = NULL;
711 int merged_fd = -1;
712 struct got_blob_object *blob2 = NULL;
713 FILE *f1 = NULL, *f2 = NULL;
714 char *blob1_path = NULL, *blob2_path = NULL;
715 char *merged_path = NULL, *base_path = NULL;
716 struct got_object_id id2;
717 char *id_str = NULL;
718 char *label1 = NULL;
719 int overlapcnt = 0, update_timestamps = 0;
720 char *parent;
722 parent = dirname(ondisk_path);
723 if (parent == NULL)
724 return got_error_from_errno();
726 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
727 return got_error_from_errno();
729 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
730 if (err)
731 goto done;
733 free(base_path);
734 if (asprintf(&base_path, "%s/got-merge-blob1", parent) == -1) {
735 err = got_error_from_errno();
736 base_path = NULL;
737 goto done;
740 err = got_opentemp_named(&blob1_path, &f1, base_path);
741 if (err)
742 goto done;
743 err = got_object_blob_dump_to_file(NULL, NULL, f1, blob1);
744 if (err)
745 goto done;
747 free(base_path);
748 if (asprintf(&base_path, "%s/got-merge-blob2", parent) == -1) {
749 err = got_error_from_errno();
750 base_path = NULL;
751 goto done;
754 err = got_opentemp_named(&blob2_path, &f2, base_path);
755 if (err)
756 goto done;
758 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
759 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
760 if (err)
761 goto done;
762 err = got_object_blob_dump_to_file(NULL, NULL, f2, blob2);
763 if (err)
764 goto done;
766 err = got_object_id_str(&id_str, worktree->base_commit_id);
767 if (err)
768 goto done;
769 if (asprintf(&label1, "commit %s", id_str) == -1) {
770 err = got_error_from_errno();
771 goto done;
774 err = got_merge_diff3(&overlapcnt, merged_fd, blob1_path,
775 blob2_path, ondisk_path, label1, path);
776 if (err)
777 goto done;
779 (*progress_cb)(progress_arg,
780 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
783 if (fsync(merged_fd) != 0) {
784 err = got_error_from_errno();
785 goto done;
788 /* Check if a clean merge has subsumed all local changes. */
789 if (overlapcnt == 0) {
790 err = check_files_equal(&update_timestamps, blob1_path,
791 merged_path);
792 if (err)
793 goto done;
796 if (rename(merged_path, ondisk_path) != 0) {
797 err = got_error_from_errno();
798 unlink(merged_path);
799 goto done;
802 /*
803 * Do not update timestamps of already modified files. Otherwise,
804 * a future status walk would treat them as unmodified files again.
805 */
806 err = got_fileindex_entry_update(ie, ondisk_path,
807 blob1->id.sha1, worktree->base_commit_id->sha1, update_timestamps);
808 done:
809 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
810 err = got_error_from_errno();
811 if (f1 && fclose(f1) != 0 && err == NULL)
812 err = got_error_from_errno();
813 if (f2 && fclose(f2) != 0 && err == NULL)
814 err = got_error_from_errno();
815 if (blob2)
816 got_object_blob_close(blob2);
817 free(merged_path);
818 free(base_path);
819 if (blob1_path) {
820 unlink(blob1_path);
821 free(blob1_path);
823 if (blob2_path) {
824 unlink(blob2_path);
825 free(blob2_path);
827 free(id_str);
828 free(label1);
829 return err;
832 static const struct got_error *
833 install_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
834 struct got_fileindex_entry *entry, const char *ondisk_path, const char *path,
835 uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob,
836 int restoring_missing_file, struct got_repository *repo,
837 got_worktree_checkout_cb progress_cb, void *progress_arg)
839 const struct got_error *err = NULL;
840 int fd = -1;
841 size_t len, hdrlen;
842 int update = 0;
843 char *tmppath = NULL;
845 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
846 GOT_DEFAULT_FILE_MODE);
847 if (fd == -1) {
848 if (errno == ENOENT) {
849 char *parent = dirname(path);
850 if (parent == NULL)
851 return got_error_from_errno();
852 err = add_dir_on_disk(worktree, parent);
853 if (err)
854 return err;
855 fd = open(ondisk_path,
856 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
857 GOT_DEFAULT_FILE_MODE);
858 if (fd == -1)
859 return got_error_from_errno();
860 } else if (errno == EEXIST) {
861 if (!S_ISREG(st_mode)) {
862 /* TODO file is obstructed; do something */
863 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
864 goto done;
865 } else {
866 err = got_opentemp_named_fd(&tmppath, &fd,
867 ondisk_path);
868 if (err)
869 goto done;
870 update = 1;
872 } else
873 return got_error_from_errno();
876 if (restoring_missing_file)
877 (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
878 else
879 (*progress_cb)(progress_arg,
880 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
882 hdrlen = got_object_blob_get_hdrlen(blob);
883 do {
884 const uint8_t *buf = got_object_blob_get_read_buf(blob);
885 err = got_object_blob_read_block(&len, blob);
886 if (err)
887 break;
888 if (len > 0) {
889 /* Skip blob object header first time around. */
890 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
891 if (outlen == -1) {
892 err = got_error_from_errno();
893 goto done;
894 } else if (outlen != len - hdrlen) {
895 err = got_error(GOT_ERR_IO);
896 goto done;
898 hdrlen = 0;
900 } while (len != 0);
902 if (fsync(fd) != 0) {
903 err = got_error_from_errno();
904 goto done;
907 if (update) {
908 if (rename(tmppath, ondisk_path) != 0) {
909 err = got_error_from_errno();
910 unlink(tmppath);
911 goto done;
915 if (te_mode & S_IXUSR) {
916 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
917 err = got_error_from_errno();
918 goto done;
920 } else {
921 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
922 err = got_error_from_errno();
923 goto done;
927 if (entry == NULL)
928 entry = got_fileindex_entry_get(fileindex, path);
929 if (entry)
930 err = got_fileindex_entry_update(entry, ondisk_path,
931 blob->id.sha1, worktree->base_commit_id->sha1, 1);
932 else {
933 err = got_fileindex_entry_alloc(&entry, ondisk_path,
934 path, blob->id.sha1, worktree->base_commit_id->sha1);
935 if (err)
936 goto done;
937 err = got_fileindex_entry_add(fileindex, entry);
939 done:
940 if (fd != -1 && close(fd) != 0 && err == NULL)
941 err = got_error_from_errno();
942 free(tmppath);
943 return err;
946 static const struct got_error *
947 get_file_status(unsigned char *status, struct stat *sb,
948 struct got_fileindex_entry *ie, const char *abspath,
949 struct got_repository *repo)
951 const struct got_error *err = NULL;
952 struct got_object_id id;
953 size_t hdrlen;
954 FILE *f = NULL;
955 uint8_t fbuf[8192];
956 struct got_blob_object *blob = NULL;
957 size_t flen, blen;
959 *status = GOT_STATUS_NO_CHANGE;
961 if (lstat(abspath, sb) == -1) {
962 if (errno == ENOENT) {
963 if (ie) {
964 *status = GOT_STATUS_MISSING;
965 sb->st_mode =
966 ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
967 & (S_IRWXU | S_IRWXG | S_IRWXO));
968 } else
969 sb->st_mode = GOT_DEFAULT_FILE_MODE;
970 return NULL;
972 return got_error_from_errno();
975 if (!S_ISREG(sb->st_mode)) {
976 *status = GOT_STATUS_OBSTRUCTED;
977 return NULL;
980 if (ie == NULL)
981 return NULL;
983 if (ie->ctime_sec == sb->st_ctime &&
984 ie->ctime_nsec == sb->st_ctimensec &&
985 ie->mtime_sec == sb->st_mtime &&
986 ie->mtime_sec == sb->st_mtime &&
987 ie->mtime_nsec == sb->st_mtimensec &&
988 ie->size == (sb->st_size & 0xffffffff))
989 return NULL;
991 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
992 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
993 if (err)
994 return err;
996 f = fopen(abspath, "r");
997 if (f == NULL) {
998 err = got_error_from_errno();
999 goto done;
1001 hdrlen = got_object_blob_get_hdrlen(blob);
1002 while (1) {
1003 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1004 err = got_object_blob_read_block(&blen, blob);
1005 if (err)
1006 break;
1007 /* Skip length of blob object header first time around. */
1008 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1009 if (flen == 0 && ferror(f)) {
1010 err = got_error_from_errno();
1011 break;
1013 if (blen == 0) {
1014 if (flen != 0)
1015 *status = GOT_STATUS_MODIFY;
1016 break;
1017 } else if (flen == 0) {
1018 if (blen != 0)
1019 *status = GOT_STATUS_MODIFY;
1020 break;
1021 } else if (blen - hdrlen == flen) {
1022 /* Skip blob object header first time around. */
1023 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1024 *status = GOT_STATUS_MODIFY;
1025 break;
1027 } else {
1028 *status = GOT_STATUS_MODIFY;
1029 break;
1031 hdrlen = 0;
1033 done:
1034 if (blob)
1035 got_object_blob_close(blob);
1036 if (f)
1037 fclose(f);
1038 return err;
1041 static const struct got_error *
1042 update_blob(struct got_worktree *worktree,
1043 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1044 struct got_tree_entry *te, const char *path,
1045 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1046 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1048 const struct got_error *err = NULL;
1049 struct got_blob_object *blob = NULL;
1050 char *ondisk_path;
1051 unsigned char status = GOT_STATUS_NO_CHANGE;
1052 struct stat sb;
1054 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1055 return got_error_from_errno();
1057 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1058 if (err)
1059 goto done;
1061 if (status == GOT_STATUS_OBSTRUCTED) {
1062 (*progress_cb)(progress_arg, status, path);
1063 goto done;
1066 if (ie && status != GOT_STATUS_MISSING) {
1067 if (memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1068 SHA1_DIGEST_LENGTH) == 0) {
1069 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1070 path);
1071 goto done;
1073 if (memcmp(ie->blob_sha1,
1074 te->id->sha1, SHA1_DIGEST_LENGTH) == 0)
1075 goto done;
1078 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1079 if (err)
1080 goto done;
1082 if (status == GOT_STATUS_MODIFY)
1083 err = merge_blob(worktree, fileindex, ie, ondisk_path, path,
1084 te->mode, sb.st_mode, blob, repo, progress_cb,
1085 progress_arg);
1086 else
1087 err = install_blob(worktree, fileindex, ie, ondisk_path, path,
1088 te->mode, sb.st_mode, blob, status == GOT_STATUS_MISSING,
1089 repo, progress_cb, progress_arg);
1091 got_object_blob_close(blob);
1092 done:
1093 free(ondisk_path);
1094 return err;
1097 static const struct got_error *
1098 remove_ondisk_file(const char *root_path, const char *path)
1100 const struct got_error *err = NULL;
1101 char *ondisk_path = NULL;
1103 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1104 return got_error_from_errno();
1106 if (unlink(ondisk_path) == -1) {
1107 if (errno != ENOENT)
1108 err = got_error_from_errno();
1109 } else {
1110 char *parent = dirname(ondisk_path);
1111 while (parent && strcmp(parent, root_path) != 0) {
1112 if (rmdir(parent) == -1) {
1113 if (errno != ENOTEMPTY)
1114 err = got_error_from_errno();
1115 break;
1117 parent = dirname(parent);
1120 free(ondisk_path);
1121 return err;
1124 struct diff_cb_arg {
1125 struct got_fileindex *fileindex;
1126 struct got_worktree *worktree;
1127 struct got_repository *repo;
1128 got_worktree_checkout_cb progress_cb;
1129 void *progress_arg;
1130 got_worktree_cancel_cb cancel_cb;
1131 void *cancel_arg;
1134 static const struct got_error *
1135 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1136 struct got_tree_entry *te, const char *parent_path)
1138 struct diff_cb_arg *a = arg;
1140 return update_blob(a->worktree, a->fileindex, ie, te,
1141 ie->path, a->repo, a->progress_cb, a->progress_arg,
1142 a->cancel_cb, a->cancel_arg);
1145 static const struct got_error *
1146 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1148 const struct got_error *err;
1149 struct diff_cb_arg *a = arg;
1151 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE, ie->path);
1153 err = remove_ondisk_file(a->worktree->root_path, ie->path);
1154 if (err)
1155 return err;
1156 got_fileindex_entry_remove(a->fileindex, ie);
1157 return NULL;
1160 static const struct got_error *
1161 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1163 struct diff_cb_arg *a = arg;
1164 const struct got_error *err;
1165 char *path;
1167 if (asprintf(&path, "%s%s%s", parent_path,
1168 parent_path[0] ? "/" : "", te->name)
1169 == -1)
1170 return got_error_from_errno();
1172 if (S_ISDIR(te->mode))
1173 err = add_dir_on_disk(a->worktree, path);
1174 else
1175 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1176 a->repo, a->progress_cb, a->progress_arg,
1177 a->cancel_cb, a->cancel_arg);
1179 free(path);
1180 return err;
1183 const struct got_error *
1184 got_worktree_checkout_files(struct got_worktree *worktree,
1185 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1186 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1188 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1189 struct got_commit_object *commit = NULL;
1190 struct got_object_id *tree_id = NULL;
1191 struct got_tree_object *tree = NULL;
1192 char *fileindex_path = NULL, *new_fileindex_path = NULL;
1193 struct got_fileindex *fileindex = NULL;
1194 FILE *index = NULL, *new_index = NULL;
1195 struct got_fileindex_diff_tree_cb diff_cb;
1196 struct diff_cb_arg arg;
1198 err = lock_worktree(worktree, LOCK_EX);
1199 if (err)
1200 return err;
1202 fileindex = got_fileindex_alloc();
1203 if (fileindex == NULL) {
1204 err = got_error_from_errno();
1205 goto done;
1208 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1209 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1210 err = got_error_from_errno();
1211 fileindex_path = NULL;
1212 goto done;
1216 * Read the file index.
1217 * Checking out files is supposed to be an idempotent operation.
1218 * If the on-disk file index is incomplete we will try to complete it.
1220 index = fopen(fileindex_path, "rb");
1221 if (index == NULL) {
1222 if (errno != ENOENT) {
1223 err = got_error_from_errno();
1224 goto done;
1226 } else {
1227 err = got_fileindex_read(fileindex, index);
1228 fclose(index);
1229 if (err)
1230 goto done;
1233 err = got_opentemp_named(&new_fileindex_path, &new_index,
1234 fileindex_path);
1235 if (err)
1236 goto done;
1238 err = got_object_open_as_commit(&commit, repo,
1239 worktree->base_commit_id);
1240 if (err)
1241 goto done;
1243 err = got_object_id_by_path(&tree_id, repo,
1244 worktree->base_commit_id, worktree->path_prefix);
1245 if (err)
1246 goto done;
1248 err = got_object_open_as_tree(&tree, repo, tree_id);
1249 if (err)
1250 goto done;
1252 diff_cb.diff_old_new = diff_old_new;
1253 diff_cb.diff_old = diff_old;
1254 diff_cb.diff_new = diff_new;
1255 arg.fileindex = fileindex;
1256 arg.worktree = worktree;
1257 arg.repo = repo;
1258 arg.progress_cb = progress_cb;
1259 arg.progress_arg = progress_arg;
1260 arg.cancel_cb = cancel_cb;
1261 arg.cancel_arg = cancel_arg;
1262 checkout_err = got_fileindex_diff_tree(fileindex, tree, repo,
1263 &diff_cb, &arg);
1265 /* Try to sync the fileindex back to disk in any case. */
1266 err = got_fileindex_write(fileindex, new_index);
1267 if (err)
1268 goto done;
1270 if (rename(new_fileindex_path, fileindex_path) != 0) {
1271 err = got_error_from_errno();
1272 unlink(new_fileindex_path);
1273 goto done;
1276 free(new_fileindex_path);
1277 new_fileindex_path = NULL;
1279 done:
1280 if (tree)
1281 got_object_tree_close(tree);
1282 if (commit)
1283 got_object_commit_close(commit);
1284 if (new_fileindex_path)
1285 unlink(new_fileindex_path);
1286 if (new_index)
1287 fclose(new_index);
1288 free(new_fileindex_path);
1289 free(fileindex_path);
1290 got_fileindex_free(fileindex);
1291 if (checkout_err)
1292 err = checkout_err;
1293 unlockerr = lock_worktree(worktree, LOCK_SH);
1294 if (unlockerr && err == NULL)
1295 err = unlockerr;
1296 return err;
1299 struct diff_dir_cb_arg {
1300 struct got_fileindex *fileindex;
1301 struct got_worktree *worktree;
1302 const char *status_path;
1303 size_t status_path_len;
1304 struct got_repository *repo;
1305 got_worktree_status_cb status_cb;
1306 void *status_arg;
1307 got_worktree_cancel_cb cancel_cb;
1308 void *cancel_arg;
1311 static const struct got_error *
1312 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1313 got_worktree_status_cb status_cb, void *status_arg,
1314 struct got_repository *repo)
1316 const struct got_error *err = NULL;
1317 unsigned char status = GOT_STATUS_NO_CHANGE;
1318 struct stat sb;
1319 struct got_object_id id;
1321 err = get_file_status(&status, &sb, ie, abspath, repo);
1322 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1323 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1324 err = (*status_cb)(status_arg, status, ie->path, &id);
1326 return err;
1329 static const struct got_error *
1330 status_old_new(void *arg, struct got_fileindex_entry *ie,
1331 struct dirent *de, const char *parent_path)
1333 const struct got_error *err = NULL;
1334 struct diff_dir_cb_arg *a = arg;
1335 char *abspath;
1337 if (got_path_cmp(parent_path, a->status_path) != 0 &&
1338 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1339 return NULL;
1341 if (parent_path[0]) {
1342 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1343 parent_path, de->d_name) == -1)
1344 return got_error_from_errno();
1345 } else {
1346 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1347 de->d_name) == -1)
1348 return got_error_from_errno();
1351 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1352 a->repo);
1353 free(abspath);
1354 return err;
1357 static const struct got_error *
1358 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1360 struct diff_dir_cb_arg *a = arg;
1361 struct got_object_id id;
1363 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1364 return NULL;
1366 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1367 return (*a->status_cb)(a->status_arg, GOT_STATUS_MISSING, ie->path,
1368 &id);
1371 static const struct got_error *
1372 status_new(void *arg, struct dirent *de, const char *parent_path)
1374 const struct got_error *err = NULL;
1375 struct diff_dir_cb_arg *a = arg;
1376 char *path = NULL;
1378 if (de->d_type == DT_DIR)
1379 return NULL;
1381 /* XXX ignore symlinks for now */
1382 if (de->d_type == DT_LNK)
1383 return NULL;
1385 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1386 return NULL;
1388 if (parent_path[0]) {
1389 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1390 return got_error_from_errno();
1391 } else {
1392 path = de->d_name;
1395 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1396 NULL);
1397 if (parent_path[0])
1398 free(path);
1399 return err;
1402 const struct got_error *
1403 got_worktree_status(struct got_worktree *worktree, const char *path,
1404 struct got_repository *repo, got_worktree_status_cb status_cb,
1405 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1407 const struct got_error *err = NULL;
1408 DIR *workdir = NULL;
1409 char *fileindex_path = NULL;
1410 struct got_fileindex *fileindex = NULL;
1411 FILE *index = NULL;
1412 struct got_fileindex_diff_dir_cb fdiff_cb;
1413 struct diff_dir_cb_arg arg;
1414 char *ondisk_path = NULL;
1416 fileindex = got_fileindex_alloc();
1417 if (fileindex == NULL) {
1418 err = got_error_from_errno();
1419 goto done;
1422 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1423 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1424 err = got_error_from_errno();
1425 fileindex_path = NULL;
1426 goto done;
1429 index = fopen(fileindex_path, "rb");
1430 if (index == NULL) {
1431 if (errno != ENOENT) {
1432 err = got_error_from_errno();
1433 goto done;
1435 } else {
1436 err = got_fileindex_read(fileindex, index);
1437 fclose(index);
1438 if (err)
1439 goto done;
1442 if (asprintf(&ondisk_path, "%s%s%s",
1443 worktree->root_path, path[0] ? "/" : "", path) == -1) {
1444 err = got_error_from_errno();
1445 goto done;
1447 workdir = opendir(ondisk_path);
1448 if (workdir == NULL) {
1449 if (errno == ENOTDIR) {
1450 struct got_fileindex_entry *ie;
1451 ie = got_fileindex_entry_get(fileindex, path);
1452 if (ie == NULL) {
1453 err = got_error(GOT_ERR_BAD_PATH);
1454 goto done;
1456 err = report_file_status(ie, ondisk_path,
1457 status_cb, status_arg, repo);
1458 goto done;
1459 } else {
1460 err = got_error_from_errno();
1461 goto done;
1464 fdiff_cb.diff_old_new = status_old_new;
1465 fdiff_cb.diff_old = status_old;
1466 fdiff_cb.diff_new = status_new;
1467 arg.fileindex = fileindex;
1468 arg.worktree = worktree;
1469 arg.status_path = path;
1470 arg.status_path_len = strlen(path);
1471 arg.repo = repo;
1472 arg.status_cb = status_cb;
1473 arg.status_arg = status_arg;
1474 arg.cancel_cb = cancel_cb;
1475 arg.cancel_arg = cancel_arg;
1476 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1477 path, repo, &fdiff_cb, &arg);
1478 done:
1479 if (workdir)
1480 closedir(workdir);
1481 free(ondisk_path);
1482 free(fileindex_path);
1483 got_fileindex_free(fileindex);
1484 return err;