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 goto done;
121 done:
122 free(tmppath);
123 if (fclose(tmpfile) != 0 && err == NULL)
124 err = got_error_from_errno();
125 return err;
128 static const struct got_error *
129 read_meta_file(char **content, const char *path_got, const char *name)
131 const struct got_error *err = NULL;
132 char *path;
133 int fd = -1;
134 ssize_t n;
135 struct stat sb;
137 *content = NULL;
139 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
140 err = got_error_from_errno();
141 path = NULL;
142 goto done;
145 fd = open(path, O_RDONLY | O_NOFOLLOW);
146 if (fd == -1) {
147 err = got_error_from_errno();
148 goto done;
150 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
151 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
152 : got_error_from_errno());
153 goto done;
156 if (lstat(path, &sb) != 0) {
157 err = got_error_from_errno();
158 goto done;
160 *content = calloc(1, sb.st_size);
161 if (*content == NULL) {
162 err = got_error_from_errno();
163 goto done;
166 n = read(fd, *content, sb.st_size);
167 if (n != sb.st_size) {
168 err = (n == -1 ? got_error_from_errno() :
169 got_error(GOT_ERR_WORKTREE_META));
170 goto done;
172 if ((*content)[sb.st_size - 1] != '\n') {
173 err = got_error(GOT_ERR_WORKTREE_META);
174 goto done;
176 (*content)[sb.st_size - 1] = '\0';
178 done:
179 if (fd != -1 && close(fd) == -1 && err == NULL)
180 err = got_error_from_errno();
181 free(path);
182 if (err) {
183 free(*content);
184 *content = NULL;
186 return err;
189 const struct got_error *
190 got_worktree_init(const char *path, struct got_reference *head_ref,
191 const char *prefix, struct got_repository *repo)
193 const struct got_error *err = NULL;
194 struct got_object_id *commit_id = NULL;
195 int obj_type;
196 char *path_got = NULL;
197 char *refstr = NULL;
198 char *formatstr = NULL;
199 char *absprefix = NULL;
200 char *basestr = NULL;
202 err = got_ref_resolve(&commit_id, repo, head_ref);
203 if (err)
204 return err;
205 err = got_object_get_type(&obj_type, repo, commit_id);
206 if (err)
207 return err;
208 if (obj_type != GOT_OBJ_TYPE_COMMIT)
209 return got_error(GOT_ERR_OBJ_TYPE);
211 if (!got_path_is_absolute(prefix)) {
212 if (asprintf(&absprefix, "/%s", prefix) == -1)
213 return got_error_from_errno();
216 /* Create top-level directory (may already exist). */
217 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
218 err = got_error_from_errno();
219 goto done;
222 /* Create .got directory (may already exist). */
223 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
224 err = got_error_from_errno();
225 goto done;
227 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
228 err = got_error_from_errno();
229 goto done;
232 /* Create an empty lock file. */
233 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
234 if (err)
235 goto done;
237 /* Create an empty file index. */
238 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
239 if (err)
240 goto done;
242 /* Write the HEAD reference. */
243 refstr = got_ref_to_str(head_ref);
244 if (refstr == NULL) {
245 err = got_error_from_errno();
246 goto done;
248 err = create_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
249 if (err)
250 goto done;
252 /* Record our base commit. */
253 err = got_object_id_str(&basestr, commit_id);
254 if (err)
255 goto done;
256 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
257 if (err)
258 goto done;
260 /* Store path to repository. */
261 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
262 got_repo_get_path(repo));
263 if (err)
264 goto done;
266 /* Store in-repository path prefix. */
267 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
268 absprefix ? absprefix : prefix);
269 if (err)
270 goto done;
272 /* Stamp work tree with format file. */
273 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
274 err = got_error_from_errno();
275 goto done;
277 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
278 if (err)
279 goto done;
281 done:
282 free(commit_id);
283 free(path_got);
284 free(formatstr);
285 free(refstr);
286 free(absprefix);
287 free(basestr);
288 return err;
291 static const struct got_error *
292 open_worktree(struct got_worktree **worktree, const char *path)
294 const struct got_error *err = NULL;
295 char *path_got;
296 char *formatstr = NULL;
297 char *path_lock = NULL;
298 char *base_commit_id_str = NULL;
299 char *head_ref_str = NULL;
300 int version, fd = -1;
301 const char *errstr;
302 struct got_repository *repo = NULL;
304 *worktree = NULL;
306 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
307 err = got_error_from_errno();
308 path_got = NULL;
309 goto done;
312 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
313 err = got_error_from_errno();
314 path_lock = NULL;
315 goto done;
318 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
319 if (fd == -1) {
320 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
321 : got_error_from_errno());
322 goto done;
325 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
326 if (err)
327 goto done;
329 version = strtonum(formatstr, 1, INT_MAX, &errstr);
330 if (errstr) {
331 err = got_error(GOT_ERR_WORKTREE_META);
332 goto done;
334 if (version != GOT_WORKTREE_FORMAT_VERSION) {
335 err = got_error(GOT_ERR_WORKTREE_VERS);
336 goto done;
339 *worktree = calloc(1, sizeof(**worktree));
340 if (*worktree == NULL) {
341 err = got_error_from_errno();
342 goto done;
344 (*worktree)->lockfd = -1;
346 (*worktree)->root_path = strdup(path);
347 if ((*worktree)->root_path == NULL) {
348 err = got_error_from_errno();
349 goto done;
351 err = read_meta_file(&(*worktree)->repo_path, path_got,
352 GOT_WORKTREE_REPOSITORY);
353 if (err)
354 goto done;
356 err = read_meta_file(&(*worktree)->path_prefix, path_got,
357 GOT_WORKTREE_PATH_PREFIX);
358 if (err)
359 goto done;
361 err = read_meta_file(&base_commit_id_str, path_got,
362 GOT_WORKTREE_BASE_COMMIT);
363 if (err)
364 goto done;
366 err = got_repo_open(&repo, (*worktree)->repo_path);
367 if (err)
368 goto done;
370 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
371 base_commit_id_str);
372 if (err)
373 goto done;
375 err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
376 if (err)
377 goto done;
379 err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
380 done:
381 if (repo)
382 got_repo_close(repo);
383 free(path_got);
384 free(path_lock);
385 free(head_ref_str);
386 free(base_commit_id_str);
387 if (err) {
388 if (fd != -1)
389 close(fd);
390 if (*worktree != NULL)
391 got_worktree_close(*worktree);
392 *worktree = NULL;
393 } else
394 (*worktree)->lockfd = fd;
396 return err;
399 const struct got_error *
400 got_worktree_open(struct got_worktree **worktree, const char *path)
402 const struct got_error *err = NULL;
404 do {
405 err = open_worktree(worktree, path);
406 if (err && (err->code != GOT_ERR_ERRNO && errno != ENOENT))
407 return err;
408 if (*worktree)
409 return NULL;
410 path = dirname(path);
411 if (path == NULL)
412 return got_error_from_errno();
413 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
415 return got_error(GOT_ERR_NOT_WORKTREE);
418 const struct got_error *
419 got_worktree_close(struct got_worktree *worktree)
421 const struct got_error *err = NULL;
422 free(worktree->root_path);
423 free(worktree->repo_path);
424 free(worktree->path_prefix);
425 free(worktree->base_commit_id);
426 if (worktree->head_ref)
427 got_ref_close(worktree->head_ref);
428 if (worktree->lockfd != -1)
429 if (close(worktree->lockfd) != 0)
430 err = got_error_from_errno();
431 free(worktree);
432 return err;
435 const char *
436 got_worktree_get_root_path(struct got_worktree *worktree)
438 return worktree->root_path;
441 const char *
442 got_worktree_get_repo_path(struct got_worktree *worktree)
444 return worktree->repo_path;
447 const char *
448 got_worktree_get_path_prefix(struct got_worktree *worktree)
450 return worktree->path_prefix;
453 const struct got_error *
454 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
455 const char *path_prefix)
457 char *absprefix = NULL;
459 if (!got_path_is_absolute(path_prefix)) {
460 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
461 return got_error_from_errno();
463 *match = (strcmp(absprefix ? absprefix : path_prefix,
464 worktree->path_prefix) == 0);
465 free(absprefix);
466 return NULL;
469 char *
470 got_worktree_get_head_ref_name(struct got_worktree *worktree)
472 return got_ref_to_str(worktree->head_ref);
475 struct got_reference *
476 got_worktree_get_head_ref(struct got_worktree *worktree)
478 return got_ref_dup(worktree->head_ref);
481 struct got_object_id *
482 got_worktree_get_base_commit_id(struct got_worktree *worktree)
484 return worktree->base_commit_id;
487 const struct got_error *
488 got_worktree_set_base_commit_id(struct got_worktree *worktree,
489 struct got_repository *repo, struct got_object_id *commit_id)
491 const struct got_error *err;
492 struct got_object *obj = NULL;
493 char *id_str = NULL;
494 char *path_got = NULL;
496 if (asprintf(&path_got, "%s/%s", worktree->root_path,
497 GOT_WORKTREE_GOT_DIR) == -1) {
498 err = got_error_from_errno();
499 path_got = NULL;
500 goto done;
503 err = got_object_open(&obj, repo, commit_id);
504 if (err)
505 return err;
507 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
508 err = got_error(GOT_ERR_OBJ_TYPE);
509 goto done;
512 /* Record our base commit. */
513 err = got_object_id_str(&id_str, commit_id);
514 if (err)
515 goto done;
516 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
517 if (err)
518 goto done;
520 free(worktree->base_commit_id);
521 worktree->base_commit_id = got_object_id_dup(commit_id);
522 if (worktree->base_commit_id == NULL) {
523 err = got_error_from_errno();
524 goto done;
526 done:
527 if (obj)
528 got_object_close(obj);
529 free(id_str);
530 free(path_got);
531 return err;
534 static const struct got_error *
535 lock_worktree(struct got_worktree *worktree, int operation)
537 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
538 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
539 : got_error_from_errno());
540 return NULL;
543 static const struct got_error *
544 make_parent_dirs(const char *abspath)
546 const struct got_error *err = NULL;
548 char *parent = dirname(abspath);
549 if (parent == NULL)
550 return NULL;
552 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
553 if (errno == ENOENT) {
554 err = make_parent_dirs(parent);
555 if (err)
556 return err;
557 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1)
558 return got_error_from_errno();
559 } else
560 err = got_error_from_errno();
563 return err;
566 static const struct got_error *
567 add_dir_on_disk(struct got_worktree *worktree, const char *path)
569 const struct got_error *err = NULL;
570 char *abspath;
572 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
573 return got_error_from_errno();
575 /* XXX queue work rather than editing disk directly? */
576 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
577 struct stat sb;
579 if (errno == EEXIST) {
580 if (lstat(abspath, &sb) == -1) {
581 err = got_error_from_errno();
582 goto done;
585 if (!S_ISDIR(sb.st_mode)) {
586 /* TODO directory is obstructed; do something */
587 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
588 goto done;
591 return NULL;
592 } else if (errno == ENOENT) {
593 err = make_parent_dirs(abspath);
594 if (err)
595 goto done;
596 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1)
597 err = got_error_from_errno();
598 } else
599 err = got_error_from_errno();
602 done:
603 free(abspath);
604 return err;
607 static const struct got_error *
608 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
610 const struct got_error *err = NULL;
611 uint8_t fbuf1[8192];
612 uint8_t fbuf2[8192];
613 size_t flen1 = 0, flen2 = 0;
615 *same = 1;
617 while (1) {
618 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
619 if (flen1 == 0 && ferror(f1)) {
620 err = got_error_from_errno();
621 break;
623 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
624 if (flen2 == 0 && ferror(f2)) {
625 err = got_error_from_errno();
626 break;
628 if (flen1 == 0) {
629 if (flen2 != 0)
630 *same = 0;
631 break;
632 } else if (flen2 == 0) {
633 if (flen1 != 0)
634 *same = 0;
635 break;
636 } else if (flen1 == flen2) {
637 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
638 *same = 0;
639 break;
641 } else {
642 *same = 0;
643 break;
647 return err;
650 static const struct got_error *
651 check_files_equal(int *same, const char *f1_path, const char *f2_path)
653 const struct got_error *err = NULL;
654 struct stat sb;
655 size_t size1, size2;
656 FILE *f1 = NULL, *f2 = NULL;
658 *same = 1;
660 if (lstat(f1_path, &sb) != 0) {
661 err = got_error_from_errno();
662 goto done;
664 size1 = sb.st_size;
666 if (lstat(f2_path, &sb) != 0) {
667 err = got_error_from_errno();
668 goto done;
670 size2 = sb.st_size;
672 if (size1 != size2) {
673 *same = 0;
674 return NULL;
677 f1 = fopen(f1_path, "r");
678 if (f1 == NULL)
679 return got_error_from_errno();
681 f2 = fopen(f2_path, "r");
682 if (f2 == NULL) {
683 err = got_error_from_errno();
684 goto done;
687 err = check_file_contents_equal(same, f1, f2);
688 done:
689 if (f1 && fclose(f1) != 0 && err == NULL)
690 err = got_error_from_errno();
691 if (f2 && fclose(f2) != 0 && err == NULL)
692 err = got_error_from_errno();
694 return err;
697 /*
698 * Perform a 3-way merge where the file's version in the file index (blob2)
699 * acts as the common ancestor, the incoming blob (blob1) acts as the first
700 * derived version, and the file on disk acts as the second derived version.
701 */
702 static const struct got_error *
703 merge_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
704 struct got_fileindex_entry *ie, const char *ondisk_path, const char *path,
705 uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob1,
706 struct got_repository *repo,
707 got_worktree_checkout_cb progress_cb, void *progress_arg)
709 const struct got_error *err = NULL;
710 int merged_fd = -1;
711 struct got_blob_object *blob2 = NULL;
712 FILE *f1 = NULL, *f2 = NULL;
713 char *blob1_path = NULL, *blob2_path = NULL;
714 char *merged_path = NULL, *base_path = NULL;
715 struct got_object_id id2;
716 char *id_str = NULL;
717 char *label1 = NULL;
718 int overlapcnt = 0, update_timestamps = 0;
719 char *parent;
721 parent = dirname(ondisk_path);
722 if (parent == NULL)
723 return got_error_from_errno();
725 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
726 return got_error_from_errno();
728 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
729 if (err)
730 goto done;
732 free(base_path);
733 if (asprintf(&base_path, "%s/got-merge-blob1", parent) == -1) {
734 err = got_error_from_errno();
735 base_path = NULL;
736 goto done;
739 err = got_opentemp_named(&blob1_path, &f1, base_path);
740 if (err)
741 goto done;
742 err = got_object_blob_dump_to_file(NULL, NULL, f1, blob1);
743 if (err)
744 goto done;
746 free(base_path);
747 if (asprintf(&base_path, "%s/got-merge-blob2", parent) == -1) {
748 err = got_error_from_errno();
749 base_path = NULL;
750 goto done;
753 err = got_opentemp_named(&blob2_path, &f2, base_path);
754 if (err)
755 goto done;
757 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
758 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
759 if (err)
760 goto done;
761 err = got_object_blob_dump_to_file(NULL, NULL, f2, blob2);
762 if (err)
763 goto done;
765 err = got_object_id_str(&id_str, worktree->base_commit_id);
766 if (err)
767 goto done;
768 if (asprintf(&label1, "commit %s", id_str) == -1) {
769 err = got_error_from_errno();
770 goto done;
773 err = got_merge_diff3(&overlapcnt, merged_fd, blob1_path,
774 blob2_path, ondisk_path, label1, path);
775 if (err)
776 goto done;
778 (*progress_cb)(progress_arg,
779 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
782 if (fsync(merged_fd) != 0) {
783 err = got_error_from_errno();
784 goto done;
787 /* Check if a clean merge has subsumed all local changes. */
788 if (overlapcnt == 0) {
789 err = check_files_equal(&update_timestamps, blob1_path,
790 merged_path);
791 if (err)
792 goto done;
795 if (rename(merged_path, ondisk_path) != 0) {
796 err = got_error_from_errno();
797 goto done;
800 /*
801 * Do not update timestamps of already modified files. Otherwise,
802 * a future status walk would treat them as unmodified files again.
803 */
804 err = got_fileindex_entry_update(ie, ondisk_path,
805 blob1->id.sha1, worktree->base_commit_id->sha1, update_timestamps);
806 done:
807 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
808 err = got_error_from_errno();
809 if (f1 && fclose(f1) != 0 && err == NULL)
810 err = got_error_from_errno();
811 if (f2 && fclose(f2) != 0 && err == NULL)
812 err = got_error_from_errno();
813 if (blob2)
814 got_object_blob_close(blob2);
815 free(merged_path);
816 free(base_path);
817 if (blob1_path) {
818 unlink(blob1_path);
819 free(blob1_path);
821 if (blob2_path) {
822 unlink(blob2_path);
823 free(blob2_path);
825 free(id_str);
826 free(label1);
827 return err;
830 static const struct got_error *
831 install_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
832 struct got_fileindex_entry *entry, const char *ondisk_path, const char *path,
833 uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob,
834 int restoring_missing_file, struct got_repository *repo,
835 got_worktree_checkout_cb progress_cb, void *progress_arg)
837 const struct got_error *err = NULL;
838 int fd = -1;
839 size_t len, hdrlen;
840 int update = 0;
841 char *tmppath = NULL;
843 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
844 GOT_DEFAULT_FILE_MODE);
845 if (fd == -1) {
846 if (errno == ENOENT) {
847 char *parent = dirname(path);
848 if (parent == NULL)
849 return got_error_from_errno();
850 err = add_dir_on_disk(worktree, parent);
851 if (err)
852 return err;
853 fd = open(ondisk_path,
854 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
855 GOT_DEFAULT_FILE_MODE);
856 if (fd == -1)
857 return got_error_from_errno();
858 } else if (errno == EEXIST) {
859 if (!S_ISREG(st_mode)) {
860 /* TODO file is obstructed; do something */
861 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
862 goto done;
863 } else {
864 err = got_opentemp_named_fd(&tmppath, &fd,
865 ondisk_path);
866 if (err)
867 goto done;
868 update = 1;
870 } else
871 return got_error_from_errno();
874 if (restoring_missing_file)
875 (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
876 else
877 (*progress_cb)(progress_arg,
878 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
880 hdrlen = got_object_blob_get_hdrlen(blob);
881 do {
882 const uint8_t *buf = got_object_blob_get_read_buf(blob);
883 err = got_object_blob_read_block(&len, blob);
884 if (err)
885 break;
886 if (len > 0) {
887 /* Skip blob object header first time around. */
888 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
889 if (outlen == -1) {
890 err = got_error_from_errno();
891 goto done;
892 } else if (outlen != len - hdrlen) {
893 err = got_error(GOT_ERR_IO);
894 goto done;
896 hdrlen = 0;
898 } while (len != 0);
900 if (fsync(fd) != 0) {
901 err = got_error_from_errno();
902 goto done;
905 if (update) {
906 if (rename(tmppath, ondisk_path) != 0) {
907 err = got_error_from_errno();
908 goto done;
912 if (te_mode & S_IXUSR) {
913 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
914 err = got_error_from_errno();
915 goto done;
917 } else {
918 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
919 err = got_error_from_errno();
920 goto done;
924 if (entry == NULL)
925 entry = got_fileindex_entry_get(fileindex, path);
926 if (entry)
927 err = got_fileindex_entry_update(entry, ondisk_path,
928 blob->id.sha1, worktree->base_commit_id->sha1, 1);
929 else {
930 err = got_fileindex_entry_alloc(&entry, ondisk_path,
931 path, blob->id.sha1, worktree->base_commit_id->sha1);
932 if (err)
933 goto done;
934 err = got_fileindex_entry_add(fileindex, entry);
936 done:
937 if (fd != -1 && close(fd) != 0 && err == NULL)
938 err = got_error_from_errno();
939 free(tmppath);
940 return err;
943 static const struct got_error *
944 get_file_status(unsigned char *status, struct stat *sb,
945 struct got_fileindex_entry *ie, const char *abspath,
946 struct got_repository *repo)
948 const struct got_error *err = NULL;
949 struct got_object_id id;
950 size_t hdrlen;
951 FILE *f = NULL;
952 uint8_t fbuf[8192];
953 struct got_blob_object *blob = NULL;
954 size_t flen, blen;
956 *status = GOT_STATUS_NO_CHANGE;
958 if (lstat(abspath, sb) == -1) {
959 if (errno == ENOENT) {
960 if (ie) {
961 *status = GOT_STATUS_MISSING;
962 sb->st_mode =
963 ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
964 & (S_IRWXU | S_IRWXG | S_IRWXO));
965 } else
966 sb->st_mode = GOT_DEFAULT_FILE_MODE;
967 return NULL;
969 return got_error_from_errno();
972 if (!S_ISREG(sb->st_mode)) {
973 *status = GOT_STATUS_OBSTRUCTED;
974 return NULL;
977 if (ie == NULL)
978 return NULL;
980 if (ie->ctime_sec == sb->st_ctime &&
981 ie->ctime_nsec == sb->st_ctimensec &&
982 ie->mtime_sec == sb->st_mtime &&
983 ie->mtime_sec == sb->st_mtime &&
984 ie->mtime_nsec == sb->st_mtimensec &&
985 ie->size == (sb->st_size & 0xffffffff))
986 return NULL;
988 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
989 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
990 if (err)
991 return err;
993 f = fopen(abspath, "r");
994 if (f == NULL) {
995 err = got_error_from_errno();
996 goto done;
998 hdrlen = got_object_blob_get_hdrlen(blob);
999 while (1) {
1000 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1001 err = got_object_blob_read_block(&blen, blob);
1002 if (err)
1003 break;
1004 /* Skip length of blob object header first time around. */
1005 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1006 if (flen == 0 && ferror(f)) {
1007 err = got_error_from_errno();
1008 break;
1010 if (blen == 0) {
1011 if (flen != 0)
1012 *status = GOT_STATUS_MODIFY;
1013 break;
1014 } else if (flen == 0) {
1015 if (blen != 0)
1016 *status = GOT_STATUS_MODIFY;
1017 break;
1018 } else if (blen - hdrlen == flen) {
1019 /* Skip blob object header first time around. */
1020 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1021 *status = GOT_STATUS_MODIFY;
1022 break;
1024 } else {
1025 *status = GOT_STATUS_MODIFY;
1026 break;
1028 hdrlen = 0;
1030 done:
1031 if (blob)
1032 got_object_blob_close(blob);
1033 if (f)
1034 fclose(f);
1035 return err;
1038 static const struct got_error *
1039 update_blob(struct got_worktree *worktree,
1040 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1041 struct got_tree_entry *te, const char *path,
1042 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1043 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1045 const struct got_error *err = NULL;
1046 struct got_blob_object *blob = NULL;
1047 char *ondisk_path;
1048 unsigned char status = GOT_STATUS_NO_CHANGE;
1049 struct stat sb;
1051 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1052 return got_error_from_errno();
1054 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1055 if (err)
1056 goto done;
1058 if (status == GOT_STATUS_OBSTRUCTED) {
1059 (*progress_cb)(progress_arg, status, path);
1060 goto done;
1063 if (ie && status != GOT_STATUS_MISSING) {
1064 if (memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1065 SHA1_DIGEST_LENGTH) == 0) {
1066 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1067 path);
1068 goto done;
1070 if (memcmp(ie->blob_sha1,
1071 te->id->sha1, SHA1_DIGEST_LENGTH) == 0)
1072 goto done;
1075 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1076 if (err)
1077 goto done;
1079 if (status == GOT_STATUS_MODIFY)
1080 err = merge_blob(worktree, fileindex, ie, ondisk_path, path,
1081 te->mode, sb.st_mode, blob, repo, progress_cb,
1082 progress_arg);
1083 else
1084 err = install_blob(worktree, fileindex, ie, ondisk_path, path,
1085 te->mode, sb.st_mode, blob, status == GOT_STATUS_MISSING,
1086 repo, progress_cb, progress_arg);
1088 got_object_blob_close(blob);
1089 done:
1090 free(ondisk_path);
1091 return err;
1094 static const struct got_error *
1095 remove_ondisk_file(const char *root_path, const char *path)
1097 const struct got_error *err = NULL;
1098 char *ondisk_path = NULL;
1100 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1101 return got_error_from_errno();
1103 if (unlink(ondisk_path) == -1) {
1104 if (errno != ENOENT)
1105 err = got_error_from_errno();
1106 } else {
1107 char *parent = dirname(ondisk_path);
1108 while (parent && strcmp(parent, root_path) != 0) {
1109 if (rmdir(parent) == -1) {
1110 if (errno != ENOTEMPTY)
1111 err = got_error_from_errno();
1112 break;
1114 parent = dirname(parent);
1117 free(ondisk_path);
1118 return err;
1121 struct diff_cb_arg {
1122 struct got_fileindex *fileindex;
1123 struct got_worktree *worktree;
1124 struct got_repository *repo;
1125 got_worktree_checkout_cb progress_cb;
1126 void *progress_arg;
1127 got_worktree_cancel_cb cancel_cb;
1128 void *cancel_arg;
1131 static const struct got_error *
1132 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1133 struct got_tree_entry *te, const char *parent_path)
1135 struct diff_cb_arg *a = arg;
1137 return update_blob(a->worktree, a->fileindex, ie, te,
1138 ie->path, a->repo, a->progress_cb, a->progress_arg,
1139 a->cancel_cb, a->cancel_arg);
1142 static const struct got_error *
1143 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1145 const struct got_error *err;
1146 struct diff_cb_arg *a = arg;
1148 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE, ie->path);
1150 err = remove_ondisk_file(a->worktree->root_path, ie->path);
1151 if (err)
1152 return err;
1153 got_fileindex_entry_remove(a->fileindex, ie);
1154 return NULL;
1157 static const struct got_error *
1158 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1160 struct diff_cb_arg *a = arg;
1161 const struct got_error *err;
1162 char *path;
1164 if (asprintf(&path, "%s%s%s", parent_path,
1165 parent_path[0] ? "/" : "", te->name)
1166 == -1)
1167 return got_error_from_errno();
1169 if (S_ISDIR(te->mode))
1170 err = add_dir_on_disk(a->worktree, path);
1171 else
1172 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1173 a->repo, a->progress_cb, a->progress_arg,
1174 a->cancel_cb, a->cancel_arg);
1176 free(path);
1177 return err;
1180 const struct got_error *
1181 got_worktree_checkout_files(struct got_worktree *worktree,
1182 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1183 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1185 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1186 struct got_commit_object *commit = NULL;
1187 struct got_object_id *tree_id = NULL;
1188 struct got_tree_object *tree = NULL;
1189 char *fileindex_path = NULL, *new_fileindex_path = NULL;
1190 struct got_fileindex *fileindex = NULL;
1191 FILE *index = NULL, *new_index = NULL;
1192 struct got_fileindex_diff_tree_cb diff_cb;
1193 struct diff_cb_arg arg;
1195 err = lock_worktree(worktree, LOCK_EX);
1196 if (err)
1197 return err;
1199 fileindex = got_fileindex_alloc();
1200 if (fileindex == NULL) {
1201 err = got_error_from_errno();
1202 goto done;
1205 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1206 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1207 err = got_error_from_errno();
1208 fileindex_path = NULL;
1209 goto done;
1213 * Read the file index.
1214 * Checking out files is supposed to be an idempotent operation.
1215 * If the on-disk file index is incomplete we will try to complete it.
1217 index = fopen(fileindex_path, "rb");
1218 if (index == NULL) {
1219 if (errno != ENOENT) {
1220 err = got_error_from_errno();
1221 goto done;
1223 } else {
1224 err = got_fileindex_read(fileindex, index);
1225 fclose(index);
1226 if (err)
1227 goto done;
1230 err = got_opentemp_named(&new_fileindex_path, &new_index,
1231 fileindex_path);
1232 if (err)
1233 goto done;
1235 err = got_object_open_as_commit(&commit, repo,
1236 worktree->base_commit_id);
1237 if (err)
1238 goto done;
1240 err = got_object_id_by_path(&tree_id, repo,
1241 worktree->base_commit_id, worktree->path_prefix);
1242 if (err)
1243 goto done;
1245 err = got_object_open_as_tree(&tree, repo, tree_id);
1246 if (err)
1247 goto done;
1249 diff_cb.diff_old_new = diff_old_new;
1250 diff_cb.diff_old = diff_old;
1251 diff_cb.diff_new = diff_new;
1252 arg.fileindex = fileindex;
1253 arg.worktree = worktree;
1254 arg.repo = repo;
1255 arg.progress_cb = progress_cb;
1256 arg.progress_arg = progress_arg;
1257 arg.cancel_cb = cancel_cb;
1258 arg.cancel_arg = cancel_arg;
1259 checkout_err = got_fileindex_diff_tree(fileindex, tree, repo,
1260 &diff_cb, &arg);
1262 /* Try to sync the fileindex back to disk in any case. */
1263 err = got_fileindex_write(fileindex, new_index);
1264 if (err)
1265 goto done;
1267 if (rename(new_fileindex_path, fileindex_path) != 0) {
1268 err = got_error_from_errno();
1269 goto done;
1272 free(new_fileindex_path);
1273 new_fileindex_path = NULL;
1275 done:
1276 if (tree)
1277 got_object_tree_close(tree);
1278 if (commit)
1279 got_object_commit_close(commit);
1280 if (new_fileindex_path)
1281 unlink(new_fileindex_path);
1282 if (new_index)
1283 fclose(new_index);
1284 free(new_fileindex_path);
1285 free(fileindex_path);
1286 got_fileindex_free(fileindex);
1287 if (checkout_err)
1288 err = checkout_err;
1289 unlockerr = lock_worktree(worktree, LOCK_SH);
1290 if (unlockerr && err == NULL)
1291 err = unlockerr;
1292 return err;
1295 struct diff_dir_cb_arg {
1296 struct got_fileindex *fileindex;
1297 struct got_worktree *worktree;
1298 const char *status_path;
1299 size_t status_path_len;
1300 struct got_repository *repo;
1301 got_worktree_status_cb status_cb;
1302 void *status_arg;
1303 got_worktree_cancel_cb cancel_cb;
1304 void *cancel_arg;
1307 static const struct got_error *
1308 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1309 got_worktree_status_cb status_cb, void *status_arg,
1310 struct got_repository *repo)
1312 const struct got_error *err = NULL;
1313 unsigned char status = GOT_STATUS_NO_CHANGE;
1314 struct stat sb;
1315 struct got_object_id id;
1317 err = get_file_status(&status, &sb, ie, abspath, repo);
1318 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1319 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1320 err = (*status_cb)(status_arg, status, ie->path, &id);
1322 return err;
1325 static const struct got_error *
1326 status_old_new(void *arg, struct got_fileindex_entry *ie,
1327 struct dirent *de, const char *parent_path)
1329 const struct got_error *err = NULL;
1330 struct diff_dir_cb_arg *a = arg;
1331 char *abspath;
1333 if (got_path_cmp(parent_path, a->status_path) != 0 &&
1334 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1335 return NULL;
1337 if (parent_path[0]) {
1338 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1339 parent_path, de->d_name) == -1)
1340 return got_error_from_errno();
1341 } else {
1342 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1343 de->d_name) == -1)
1344 return got_error_from_errno();
1347 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1348 a->repo);
1349 free(abspath);
1350 return err;
1353 static const struct got_error *
1354 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1356 struct diff_dir_cb_arg *a = arg;
1357 struct got_object_id id;
1359 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1360 return NULL;
1362 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1363 return (*a->status_cb)(a->status_arg, GOT_STATUS_MISSING, ie->path,
1364 &id);
1367 static const struct got_error *
1368 status_new(void *arg, struct dirent *de, const char *parent_path)
1370 const struct got_error *err = NULL;
1371 struct diff_dir_cb_arg *a = arg;
1372 char *path = NULL;
1374 if (de->d_type == DT_DIR)
1375 return NULL;
1377 /* XXX ignore symlinks for now */
1378 if (de->d_type == DT_LNK)
1379 return NULL;
1381 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1382 return NULL;
1384 if (parent_path[0]) {
1385 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1386 return got_error_from_errno();
1387 } else {
1388 path = de->d_name;
1391 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1392 NULL);
1393 if (parent_path[0])
1394 free(path);
1395 return err;
1398 const struct got_error *
1399 got_worktree_status(struct got_worktree *worktree, const char *path,
1400 struct got_repository *repo, got_worktree_status_cb status_cb,
1401 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1403 const struct got_error *err = NULL;
1404 DIR *workdir = NULL;
1405 char *fileindex_path = NULL;
1406 struct got_fileindex *fileindex = NULL;
1407 FILE *index = NULL;
1408 struct got_fileindex_diff_dir_cb fdiff_cb;
1409 struct diff_dir_cb_arg arg;
1410 char *ondisk_path = NULL;
1412 fileindex = got_fileindex_alloc();
1413 if (fileindex == NULL) {
1414 err = got_error_from_errno();
1415 goto done;
1418 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1419 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1420 err = got_error_from_errno();
1421 fileindex_path = NULL;
1422 goto done;
1425 index = fopen(fileindex_path, "rb");
1426 if (index == NULL) {
1427 if (errno != ENOENT) {
1428 err = got_error_from_errno();
1429 goto done;
1431 } else {
1432 err = got_fileindex_read(fileindex, index);
1433 fclose(index);
1434 if (err)
1435 goto done;
1438 if (asprintf(&ondisk_path, "%s%s%s",
1439 worktree->root_path, path[0] ? "/" : "", path) == -1) {
1440 err = got_error_from_errno();
1441 goto done;
1443 workdir = opendir(ondisk_path);
1444 if (workdir == NULL) {
1445 if (errno == ENOTDIR) {
1446 struct got_fileindex_entry *ie;
1447 ie = got_fileindex_entry_get(fileindex, path);
1448 if (ie == NULL) {
1449 err = got_error(GOT_ERR_BAD_PATH);
1450 goto done;
1452 err = report_file_status(ie, ondisk_path,
1453 status_cb, status_arg, repo);
1454 goto done;
1455 } else {
1456 err = got_error_from_errno();
1457 goto done;
1460 fdiff_cb.diff_old_new = status_old_new;
1461 fdiff_cb.diff_old = status_old;
1462 fdiff_cb.diff_new = status_new;
1463 arg.fileindex = fileindex;
1464 arg.worktree = worktree;
1465 arg.status_path = path;
1466 arg.status_path_len = strlen(path);
1467 arg.repo = repo;
1468 arg.status_cb = status_cb;
1469 arg.status_arg = status_arg;
1470 arg.cancel_cb = cancel_cb;
1471 arg.cancel_arg = cancel_arg;
1472 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1473 path, repo, &fdiff_cb, &arg);
1474 done:
1475 if (workdir)
1476 closedir(workdir);
1477 free(ondisk_path);
1478 free(fileindex_path);
1479 got_fileindex_free(fileindex);
1480 return err;