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 (chmod(merged_path, st_mode) != 0) {
797 err = got_error_from_errno();
798 goto done;
801 if (rename(merged_path, ondisk_path) != 0) {
802 err = got_error_from_errno();
803 unlink(merged_path);
804 goto done;
807 /*
808 * Do not update timestamps of already modified files. Otherwise,
809 * a future status walk would treat them as unmodified files again.
810 */
811 err = got_fileindex_entry_update(ie, ondisk_path,
812 blob1->id.sha1, worktree->base_commit_id->sha1, update_timestamps);
813 done:
814 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
815 err = got_error_from_errno();
816 if (f1 && fclose(f1) != 0 && err == NULL)
817 err = got_error_from_errno();
818 if (f2 && fclose(f2) != 0 && err == NULL)
819 err = got_error_from_errno();
820 if (blob2)
821 got_object_blob_close(blob2);
822 free(merged_path);
823 free(base_path);
824 if (blob1_path) {
825 unlink(blob1_path);
826 free(blob1_path);
828 if (blob2_path) {
829 unlink(blob2_path);
830 free(blob2_path);
832 free(id_str);
833 free(label1);
834 return err;
837 static const struct got_error *
838 install_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
839 struct got_fileindex_entry *entry, const char *ondisk_path, const char *path,
840 uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob,
841 int restoring_missing_file, struct got_repository *repo,
842 got_worktree_checkout_cb progress_cb, void *progress_arg)
844 const struct got_error *err = NULL;
845 int fd = -1;
846 size_t len, hdrlen;
847 int update = 0;
848 char *tmppath = NULL;
850 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
851 GOT_DEFAULT_FILE_MODE);
852 if (fd == -1) {
853 if (errno == ENOENT) {
854 char *parent = dirname(path);
855 if (parent == NULL)
856 return got_error_from_errno();
857 err = add_dir_on_disk(worktree, parent);
858 if (err)
859 return err;
860 fd = open(ondisk_path,
861 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
862 GOT_DEFAULT_FILE_MODE);
863 if (fd == -1)
864 return got_error_from_errno();
865 } else if (errno == EEXIST) {
866 if (!S_ISREG(st_mode)) {
867 /* TODO file is obstructed; do something */
868 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
869 goto done;
870 } else {
871 err = got_opentemp_named_fd(&tmppath, &fd,
872 ondisk_path);
873 if (err)
874 goto done;
875 update = 1;
877 } else
878 return got_error_from_errno();
881 if (restoring_missing_file)
882 (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
883 else
884 (*progress_cb)(progress_arg,
885 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
887 hdrlen = got_object_blob_get_hdrlen(blob);
888 do {
889 const uint8_t *buf = got_object_blob_get_read_buf(blob);
890 err = got_object_blob_read_block(&len, blob);
891 if (err)
892 break;
893 if (len > 0) {
894 /* Skip blob object header first time around. */
895 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
896 if (outlen == -1) {
897 err = got_error_from_errno();
898 goto done;
899 } else if (outlen != len - hdrlen) {
900 err = got_error(GOT_ERR_IO);
901 goto done;
903 hdrlen = 0;
905 } while (len != 0);
907 if (fsync(fd) != 0) {
908 err = got_error_from_errno();
909 goto done;
912 if (update) {
913 if (rename(tmppath, ondisk_path) != 0) {
914 err = got_error_from_errno();
915 unlink(tmppath);
916 goto done;
920 if (te_mode & S_IXUSR) {
921 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
922 err = got_error_from_errno();
923 goto done;
925 } else {
926 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
927 err = got_error_from_errno();
928 goto done;
932 if (entry == NULL)
933 entry = got_fileindex_entry_get(fileindex, path);
934 if (entry)
935 err = got_fileindex_entry_update(entry, ondisk_path,
936 blob->id.sha1, worktree->base_commit_id->sha1, 1);
937 else {
938 err = got_fileindex_entry_alloc(&entry, ondisk_path,
939 path, blob->id.sha1, worktree->base_commit_id->sha1);
940 if (err)
941 goto done;
942 err = got_fileindex_entry_add(fileindex, entry);
944 done:
945 if (fd != -1 && close(fd) != 0 && err == NULL)
946 err = got_error_from_errno();
947 free(tmppath);
948 return err;
951 static const struct got_error *
952 get_file_status(unsigned char *status, struct stat *sb,
953 struct got_fileindex_entry *ie, const char *abspath,
954 struct got_repository *repo)
956 const struct got_error *err = NULL;
957 struct got_object_id id;
958 size_t hdrlen;
959 FILE *f = NULL;
960 uint8_t fbuf[8192];
961 struct got_blob_object *blob = NULL;
962 size_t flen, blen;
964 *status = GOT_STATUS_NO_CHANGE;
966 if (lstat(abspath, sb) == -1) {
967 if (errno == ENOENT) {
968 if (ie) {
969 *status = GOT_STATUS_MISSING;
970 sb->st_mode =
971 ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
972 & (S_IRWXU | S_IRWXG | S_IRWXO));
973 } else
974 sb->st_mode = GOT_DEFAULT_FILE_MODE;
975 return NULL;
977 return got_error_from_errno();
980 if (!S_ISREG(sb->st_mode)) {
981 *status = GOT_STATUS_OBSTRUCTED;
982 return NULL;
985 if (ie == NULL)
986 return NULL;
988 if (ie->ctime_sec == sb->st_ctime &&
989 ie->ctime_nsec == sb->st_ctimensec &&
990 ie->mtime_sec == sb->st_mtime &&
991 ie->mtime_sec == sb->st_mtime &&
992 ie->mtime_nsec == sb->st_mtimensec &&
993 ie->size == (sb->st_size & 0xffffffff))
994 return NULL;
996 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
997 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
998 if (err)
999 return err;
1001 f = fopen(abspath, "r");
1002 if (f == NULL) {
1003 err = got_error_from_errno();
1004 goto done;
1006 hdrlen = got_object_blob_get_hdrlen(blob);
1007 while (1) {
1008 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1009 err = got_object_blob_read_block(&blen, blob);
1010 if (err)
1011 break;
1012 /* Skip length of blob object header first time around. */
1013 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1014 if (flen == 0 && ferror(f)) {
1015 err = got_error_from_errno();
1016 break;
1018 if (blen == 0) {
1019 if (flen != 0)
1020 *status = GOT_STATUS_MODIFY;
1021 break;
1022 } else if (flen == 0) {
1023 if (blen != 0)
1024 *status = GOT_STATUS_MODIFY;
1025 break;
1026 } else if (blen - hdrlen == flen) {
1027 /* Skip blob object header first time around. */
1028 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1029 *status = GOT_STATUS_MODIFY;
1030 break;
1032 } else {
1033 *status = GOT_STATUS_MODIFY;
1034 break;
1036 hdrlen = 0;
1038 done:
1039 if (blob)
1040 got_object_blob_close(blob);
1041 if (f)
1042 fclose(f);
1043 return err;
1046 static const struct got_error *
1047 update_blob(struct got_worktree *worktree,
1048 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1049 struct got_tree_entry *te, const char *path,
1050 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1051 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1053 const struct got_error *err = NULL;
1054 struct got_blob_object *blob = NULL;
1055 char *ondisk_path;
1056 unsigned char status = GOT_STATUS_NO_CHANGE;
1057 struct stat sb;
1059 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1060 return got_error_from_errno();
1062 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1063 if (err)
1064 goto done;
1066 if (status == GOT_STATUS_OBSTRUCTED) {
1067 (*progress_cb)(progress_arg, status, path);
1068 goto done;
1071 if (ie && status != GOT_STATUS_MISSING) {
1072 if (memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1073 SHA1_DIGEST_LENGTH) == 0) {
1074 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1075 path);
1076 goto done;
1078 if (memcmp(ie->blob_sha1,
1079 te->id->sha1, SHA1_DIGEST_LENGTH) == 0)
1080 goto done;
1083 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1084 if (err)
1085 goto done;
1087 if (status == GOT_STATUS_MODIFY)
1088 err = merge_blob(worktree, fileindex, ie, ondisk_path, path,
1089 te->mode, sb.st_mode, blob, repo, progress_cb,
1090 progress_arg);
1091 else
1092 err = install_blob(worktree, fileindex, ie, ondisk_path, path,
1093 te->mode, sb.st_mode, blob, status == GOT_STATUS_MISSING,
1094 repo, progress_cb, progress_arg);
1096 got_object_blob_close(blob);
1097 done:
1098 free(ondisk_path);
1099 return err;
1102 static const struct got_error *
1103 remove_ondisk_file(const char *root_path, const char *path)
1105 const struct got_error *err = NULL;
1106 char *ondisk_path = NULL;
1108 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1109 return got_error_from_errno();
1111 if (unlink(ondisk_path) == -1) {
1112 if (errno != ENOENT)
1113 err = got_error_from_errno();
1114 } else {
1115 char *parent = dirname(ondisk_path);
1116 while (parent && strcmp(parent, root_path) != 0) {
1117 if (rmdir(parent) == -1) {
1118 if (errno != ENOTEMPTY)
1119 err = got_error_from_errno();
1120 break;
1122 parent = dirname(parent);
1125 free(ondisk_path);
1126 return err;
1129 struct diff_cb_arg {
1130 struct got_fileindex *fileindex;
1131 struct got_worktree *worktree;
1132 struct got_repository *repo;
1133 got_worktree_checkout_cb progress_cb;
1134 void *progress_arg;
1135 got_worktree_cancel_cb cancel_cb;
1136 void *cancel_arg;
1139 static const struct got_error *
1140 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1141 struct got_tree_entry *te, const char *parent_path)
1143 struct diff_cb_arg *a = arg;
1145 return update_blob(a->worktree, a->fileindex, ie, te,
1146 ie->path, a->repo, a->progress_cb, a->progress_arg,
1147 a->cancel_cb, a->cancel_arg);
1150 static const struct got_error *
1151 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1153 const struct got_error *err;
1154 struct diff_cb_arg *a = arg;
1156 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE, ie->path);
1158 err = remove_ondisk_file(a->worktree->root_path, ie->path);
1159 if (err)
1160 return err;
1161 got_fileindex_entry_remove(a->fileindex, ie);
1162 return NULL;
1165 static const struct got_error *
1166 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1168 struct diff_cb_arg *a = arg;
1169 const struct got_error *err;
1170 char *path;
1172 if (asprintf(&path, "%s%s%s", parent_path,
1173 parent_path[0] ? "/" : "", te->name)
1174 == -1)
1175 return got_error_from_errno();
1177 if (S_ISDIR(te->mode))
1178 err = add_dir_on_disk(a->worktree, path);
1179 else
1180 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1181 a->repo, a->progress_cb, a->progress_arg,
1182 a->cancel_cb, a->cancel_arg);
1184 free(path);
1185 return err;
1188 const struct got_error *
1189 got_worktree_checkout_files(struct got_worktree *worktree,
1190 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1191 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1193 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1194 struct got_commit_object *commit = NULL;
1195 struct got_object_id *tree_id = NULL;
1196 struct got_tree_object *tree = NULL;
1197 char *fileindex_path = NULL, *new_fileindex_path = NULL;
1198 struct got_fileindex *fileindex = NULL;
1199 FILE *index = NULL, *new_index = NULL;
1200 struct got_fileindex_diff_tree_cb diff_cb;
1201 struct diff_cb_arg arg;
1203 err = lock_worktree(worktree, LOCK_EX);
1204 if (err)
1205 return err;
1207 fileindex = got_fileindex_alloc();
1208 if (fileindex == NULL) {
1209 err = got_error_from_errno();
1210 goto done;
1213 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1214 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1215 err = got_error_from_errno();
1216 fileindex_path = NULL;
1217 goto done;
1221 * Read the file index.
1222 * Checking out files is supposed to be an idempotent operation.
1223 * If the on-disk file index is incomplete we will try to complete it.
1225 index = fopen(fileindex_path, "rb");
1226 if (index == NULL) {
1227 if (errno != ENOENT) {
1228 err = got_error_from_errno();
1229 goto done;
1231 } else {
1232 err = got_fileindex_read(fileindex, index);
1233 fclose(index);
1234 if (err)
1235 goto done;
1238 err = got_opentemp_named(&new_fileindex_path, &new_index,
1239 fileindex_path);
1240 if (err)
1241 goto done;
1243 err = got_object_open_as_commit(&commit, repo,
1244 worktree->base_commit_id);
1245 if (err)
1246 goto done;
1248 err = got_object_id_by_path(&tree_id, repo,
1249 worktree->base_commit_id, worktree->path_prefix);
1250 if (err)
1251 goto done;
1253 err = got_object_open_as_tree(&tree, repo, tree_id);
1254 if (err)
1255 goto done;
1257 diff_cb.diff_old_new = diff_old_new;
1258 diff_cb.diff_old = diff_old;
1259 diff_cb.diff_new = diff_new;
1260 arg.fileindex = fileindex;
1261 arg.worktree = worktree;
1262 arg.repo = repo;
1263 arg.progress_cb = progress_cb;
1264 arg.progress_arg = progress_arg;
1265 arg.cancel_cb = cancel_cb;
1266 arg.cancel_arg = cancel_arg;
1267 checkout_err = got_fileindex_diff_tree(fileindex, tree, repo,
1268 &diff_cb, &arg);
1270 /* Try to sync the fileindex back to disk in any case. */
1271 err = got_fileindex_write(fileindex, new_index);
1272 if (err)
1273 goto done;
1275 if (rename(new_fileindex_path, fileindex_path) != 0) {
1276 err = got_error_from_errno();
1277 unlink(new_fileindex_path);
1278 goto done;
1281 free(new_fileindex_path);
1282 new_fileindex_path = NULL;
1284 done:
1285 if (tree)
1286 got_object_tree_close(tree);
1287 if (commit)
1288 got_object_commit_close(commit);
1289 if (new_fileindex_path)
1290 unlink(new_fileindex_path);
1291 if (new_index)
1292 fclose(new_index);
1293 free(new_fileindex_path);
1294 free(fileindex_path);
1295 got_fileindex_free(fileindex);
1296 if (checkout_err)
1297 err = checkout_err;
1298 unlockerr = lock_worktree(worktree, LOCK_SH);
1299 if (unlockerr && err == NULL)
1300 err = unlockerr;
1301 return err;
1304 struct diff_dir_cb_arg {
1305 struct got_fileindex *fileindex;
1306 struct got_worktree *worktree;
1307 const char *status_path;
1308 size_t status_path_len;
1309 struct got_repository *repo;
1310 got_worktree_status_cb status_cb;
1311 void *status_arg;
1312 got_worktree_cancel_cb cancel_cb;
1313 void *cancel_arg;
1316 static const struct got_error *
1317 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1318 got_worktree_status_cb status_cb, void *status_arg,
1319 struct got_repository *repo)
1321 const struct got_error *err = NULL;
1322 unsigned char status = GOT_STATUS_NO_CHANGE;
1323 struct stat sb;
1324 struct got_object_id id;
1326 err = get_file_status(&status, &sb, ie, abspath, repo);
1327 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1328 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1329 err = (*status_cb)(status_arg, status, ie->path, &id);
1331 return err;
1334 static const struct got_error *
1335 status_old_new(void *arg, struct got_fileindex_entry *ie,
1336 struct dirent *de, const char *parent_path)
1338 const struct got_error *err = NULL;
1339 struct diff_dir_cb_arg *a = arg;
1340 char *abspath;
1342 if (got_path_cmp(parent_path, a->status_path) != 0 &&
1343 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1344 return NULL;
1346 if (parent_path[0]) {
1347 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1348 parent_path, de->d_name) == -1)
1349 return got_error_from_errno();
1350 } else {
1351 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1352 de->d_name) == -1)
1353 return got_error_from_errno();
1356 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1357 a->repo);
1358 free(abspath);
1359 return err;
1362 static const struct got_error *
1363 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1365 struct diff_dir_cb_arg *a = arg;
1366 struct got_object_id id;
1368 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1369 return NULL;
1371 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1372 return (*a->status_cb)(a->status_arg, GOT_STATUS_MISSING, ie->path,
1373 &id);
1376 static const struct got_error *
1377 status_new(void *arg, struct dirent *de, const char *parent_path)
1379 const struct got_error *err = NULL;
1380 struct diff_dir_cb_arg *a = arg;
1381 char *path = NULL;
1383 if (de->d_type == DT_DIR)
1384 return NULL;
1386 /* XXX ignore symlinks for now */
1387 if (de->d_type == DT_LNK)
1388 return NULL;
1390 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1391 return NULL;
1393 if (parent_path[0]) {
1394 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1395 return got_error_from_errno();
1396 } else {
1397 path = de->d_name;
1400 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1401 NULL);
1402 if (parent_path[0])
1403 free(path);
1404 return err;
1407 const struct got_error *
1408 got_worktree_status(struct got_worktree *worktree, const char *path,
1409 struct got_repository *repo, got_worktree_status_cb status_cb,
1410 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1412 const struct got_error *err = NULL;
1413 DIR *workdir = NULL;
1414 char *fileindex_path = NULL;
1415 struct got_fileindex *fileindex = NULL;
1416 FILE *index = NULL;
1417 struct got_fileindex_diff_dir_cb fdiff_cb;
1418 struct diff_dir_cb_arg arg;
1419 char *ondisk_path = NULL;
1421 fileindex = got_fileindex_alloc();
1422 if (fileindex == NULL) {
1423 err = got_error_from_errno();
1424 goto done;
1427 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1428 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1429 err = got_error_from_errno();
1430 fileindex_path = NULL;
1431 goto done;
1434 index = fopen(fileindex_path, "rb");
1435 if (index == NULL) {
1436 if (errno != ENOENT) {
1437 err = got_error_from_errno();
1438 goto done;
1440 } else {
1441 err = got_fileindex_read(fileindex, index);
1442 fclose(index);
1443 if (err)
1444 goto done;
1447 if (asprintf(&ondisk_path, "%s%s%s",
1448 worktree->root_path, path[0] ? "/" : "", path) == -1) {
1449 err = got_error_from_errno();
1450 goto done;
1452 workdir = opendir(ondisk_path);
1453 if (workdir == NULL) {
1454 if (errno == ENOTDIR) {
1455 struct got_fileindex_entry *ie;
1456 ie = got_fileindex_entry_get(fileindex, path);
1457 if (ie == NULL) {
1458 err = got_error(GOT_ERR_BAD_PATH);
1459 goto done;
1461 err = report_file_status(ie, ondisk_path,
1462 status_cb, status_arg, repo);
1463 goto done;
1464 } else {
1465 err = got_error_from_errno();
1466 goto done;
1469 fdiff_cb.diff_old_new = status_old_new;
1470 fdiff_cb.diff_old = status_old;
1471 fdiff_cb.diff_new = status_new;
1472 arg.fileindex = fileindex;
1473 arg.worktree = worktree;
1474 arg.status_path = path;
1475 arg.status_path_len = strlen(path);
1476 arg.repo = repo;
1477 arg.status_cb = status_cb;
1478 arg.status_arg = status_arg;
1479 arg.cancel_cb = cancel_cb;
1480 arg.cancel_arg = cancel_arg;
1481 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1482 path, repo, &fdiff_cb, &arg);
1483 done:
1484 if (workdir)
1485 closedir(workdir);
1486 free(ondisk_path);
1487 free(fileindex_path);
1488 got_fileindex_free(fileindex);
1489 return err;