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 fclose(tmpfile);
124 return err;
127 static const struct got_error *
128 read_meta_file(char **content, const char *path_got, const char *name)
130 const struct got_error *err = NULL;
131 char *path;
132 int fd = -1;
133 ssize_t n;
134 struct stat sb;
136 *content = NULL;
138 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
139 err = got_error_from_errno();
140 path = NULL;
141 goto done;
144 fd = open(path, O_RDONLY | O_NOFOLLOW);
145 if (fd == -1) {
146 err = got_error_from_errno();
147 goto done;
149 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
150 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
151 : got_error_from_errno());
152 goto done;
155 stat(path, &sb);
156 *content = calloc(1, sb.st_size);
157 if (*content == NULL) {
158 err = got_error_from_errno();
159 goto done;
162 n = read(fd, *content, sb.st_size);
163 if (n != sb.st_size) {
164 err = (n == -1 ? got_error_from_errno() :
165 got_error(GOT_ERR_WORKTREE_META));
166 goto done;
168 if ((*content)[sb.st_size - 1] != '\n') {
169 err = got_error(GOT_ERR_WORKTREE_META);
170 goto done;
172 (*content)[sb.st_size - 1] = '\0';
174 done:
175 if (fd != -1 && close(fd) == -1 && err == NULL)
176 err = got_error_from_errno();
177 free(path);
178 if (err) {
179 free(*content);
180 *content = NULL;
182 return err;
185 const struct got_error *
186 got_worktree_init(const char *path, struct got_reference *head_ref,
187 const char *prefix, struct got_repository *repo)
189 const struct got_error *err = NULL;
190 struct got_object_id *commit_id = NULL;
191 int obj_type;
192 char *path_got = NULL;
193 char *refstr = NULL;
194 char *formatstr = NULL;
195 char *absprefix = NULL;
196 char *basestr = NULL;
198 err = got_ref_resolve(&commit_id, repo, head_ref);
199 if (err)
200 return err;
201 err = got_object_get_type(&obj_type, repo, commit_id);
202 if (err)
203 return err;
204 if (obj_type != GOT_OBJ_TYPE_COMMIT)
205 return got_error(GOT_ERR_OBJ_TYPE);
207 if (!got_path_is_absolute(prefix)) {
208 if (asprintf(&absprefix, "/%s", prefix) == -1)
209 return got_error_from_errno();
212 /* Create top-level directory (may already exist). */
213 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
214 err = got_error_from_errno();
215 goto done;
218 /* Create .got directory (may already exist). */
219 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
220 err = got_error_from_errno();
221 goto done;
223 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
224 err = got_error_from_errno();
225 goto done;
228 /* Create an empty lock file. */
229 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
230 if (err)
231 goto done;
233 /* Create an empty file index. */
234 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
235 if (err)
236 goto done;
238 /* Write the HEAD reference. */
239 refstr = got_ref_to_str(head_ref);
240 if (refstr == NULL) {
241 err = got_error_from_errno();
242 goto done;
244 err = create_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
245 if (err)
246 goto done;
248 /* Record our base commit. */
249 err = got_object_id_str(&basestr, commit_id);
250 if (err)
251 goto done;
252 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
253 if (err)
254 goto done;
256 /* Store path to repository. */
257 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
258 got_repo_get_path(repo));
259 if (err)
260 goto done;
262 /* Store in-repository path prefix. */
263 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
264 absprefix ? absprefix : prefix);
265 if (err)
266 goto done;
268 /* Stamp work tree with format file. */
269 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
270 err = got_error_from_errno();
271 goto done;
273 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
274 if (err)
275 goto done;
277 done:
278 free(commit_id);
279 free(path_got);
280 free(formatstr);
281 free(refstr);
282 free(absprefix);
283 free(basestr);
284 return err;
287 static const struct got_error *
288 open_worktree(struct got_worktree **worktree, const char *path)
290 const struct got_error *err = NULL;
291 char *path_got;
292 char *formatstr = NULL;
293 char *path_lock = NULL;
294 char *base_commit_id_str = NULL;
295 char *head_ref_str = NULL;
296 int version, fd = -1;
297 const char *errstr;
298 struct got_repository *repo = NULL;
300 *worktree = NULL;
302 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
303 err = got_error_from_errno();
304 path_got = NULL;
305 goto done;
308 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
309 err = got_error_from_errno();
310 path_lock = NULL;
311 goto done;
314 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
315 if (fd == -1) {
316 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
317 : got_error_from_errno());
318 goto done;
321 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
322 if (err)
323 goto done;
325 version = strtonum(formatstr, 1, INT_MAX, &errstr);
326 if (errstr) {
327 err = got_error(GOT_ERR_WORKTREE_META);
328 goto done;
330 if (version != GOT_WORKTREE_FORMAT_VERSION) {
331 err = got_error(GOT_ERR_WORKTREE_VERS);
332 goto done;
335 *worktree = calloc(1, sizeof(**worktree));
336 if (*worktree == NULL) {
337 err = got_error_from_errno();
338 goto done;
340 (*worktree)->lockfd = -1;
342 (*worktree)->root_path = strdup(path);
343 if ((*worktree)->root_path == NULL) {
344 err = got_error_from_errno();
345 goto done;
347 err = read_meta_file(&(*worktree)->repo_path, path_got,
348 GOT_WORKTREE_REPOSITORY);
349 if (err)
350 goto done;
352 err = read_meta_file(&(*worktree)->path_prefix, path_got,
353 GOT_WORKTREE_PATH_PREFIX);
354 if (err)
355 goto done;
357 err = read_meta_file(&base_commit_id_str, path_got,
358 GOT_WORKTREE_BASE_COMMIT);
359 if (err)
360 goto done;
362 err = got_repo_open(&repo, (*worktree)->repo_path);
363 if (err)
364 goto done;
366 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
367 base_commit_id_str);
368 if (err)
369 goto done;
371 err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
372 if (err)
373 goto done;
375 err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
376 done:
377 if (repo)
378 got_repo_close(repo);
379 free(path_got);
380 free(path_lock);
381 free(head_ref_str);
382 free(base_commit_id_str);
383 if (err) {
384 if (fd != -1)
385 close(fd);
386 if (*worktree != NULL)
387 got_worktree_close(*worktree);
388 *worktree = NULL;
389 } else
390 (*worktree)->lockfd = fd;
392 return err;
395 const struct got_error *
396 got_worktree_open(struct got_worktree **worktree, const char *path)
398 const struct got_error *err = NULL;
400 do {
401 err = open_worktree(worktree, path);
402 if (err && (err->code != GOT_ERR_ERRNO && errno != ENOENT))
403 return err;
404 if (*worktree)
405 return NULL;
406 path = dirname(path);
407 if (path == NULL)
408 return got_error_from_errno();
409 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
411 return got_error(GOT_ERR_NOT_WORKTREE);
414 void
415 got_worktree_close(struct got_worktree *worktree)
417 free(worktree->root_path);
418 free(worktree->repo_path);
419 free(worktree->path_prefix);
420 free(worktree->base_commit_id);
421 if (worktree->head_ref)
422 got_ref_close(worktree->head_ref);
423 if (worktree->lockfd != -1)
424 close(worktree->lockfd);
425 free(worktree);
428 const char *
429 got_worktree_get_root_path(struct got_worktree *worktree)
431 return worktree->root_path;
434 const char *
435 got_worktree_get_repo_path(struct got_worktree *worktree)
437 return worktree->repo_path;
440 const char *
441 got_worktree_get_path_prefix(struct got_worktree *worktree)
443 return worktree->path_prefix;
446 const struct got_error *
447 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
448 const char *path_prefix)
450 char *absprefix = NULL;
452 if (!got_path_is_absolute(path_prefix)) {
453 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
454 return got_error_from_errno();
456 *match = (strcmp(absprefix ? absprefix : path_prefix,
457 worktree->path_prefix) == 0);
458 free(absprefix);
459 return NULL;
462 char *
463 got_worktree_get_head_ref_name(struct got_worktree *worktree)
465 return got_ref_to_str(worktree->head_ref);
468 struct got_reference *
469 got_worktree_get_head_ref(struct got_worktree *worktree)
471 return got_ref_dup(worktree->head_ref);
474 struct got_object_id *
475 got_worktree_get_base_commit_id(struct got_worktree *worktree)
477 return worktree->base_commit_id;
480 const struct got_error *
481 got_worktree_set_base_commit_id(struct got_worktree *worktree,
482 struct got_repository *repo, struct got_object_id *commit_id)
484 const struct got_error *err;
485 struct got_object *obj = NULL;
486 char *id_str = NULL;
487 char *path_got = NULL;
489 if (asprintf(&path_got, "%s/%s", worktree->root_path,
490 GOT_WORKTREE_GOT_DIR) == -1) {
491 err = got_error_from_errno();
492 path_got = NULL;
493 goto done;
496 err = got_object_open(&obj, repo, commit_id);
497 if (err)
498 return err;
500 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
501 err = got_error(GOT_ERR_OBJ_TYPE);
502 goto done;
505 /* Record our base commit. */
506 err = got_object_id_str(&id_str, commit_id);
507 if (err)
508 goto done;
509 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
510 if (err)
511 goto done;
513 free(worktree->base_commit_id);
514 worktree->base_commit_id = got_object_id_dup(commit_id);
515 if (worktree->base_commit_id == NULL) {
516 err = got_error_from_errno();
517 goto done;
519 done:
520 if (obj)
521 got_object_close(obj);
522 free(id_str);
523 free(path_got);
524 return err;
527 static const struct got_error *
528 lock_worktree(struct got_worktree *worktree, int operation)
530 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
531 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
532 : got_error_from_errno());
533 return NULL;
536 static const struct got_error *
537 make_parent_dirs(const char *abspath)
539 const struct got_error *err = NULL;
541 char *parent = dirname(abspath);
542 if (parent == NULL)
543 return NULL;
545 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
546 if (errno == ENOENT) {
547 err = make_parent_dirs(parent);
548 if (err)
549 return err;
550 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1)
551 return got_error_from_errno();
552 } else
553 err = got_error_from_errno();
556 return err;
559 static const struct got_error *
560 add_dir_on_disk(struct got_worktree *worktree, const char *path)
562 const struct got_error *err = NULL;
563 char *abspath;
565 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
566 return got_error_from_errno();
568 /* XXX queue work rather than editing disk directly? */
569 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
570 struct stat sb;
572 if (errno == EEXIST) {
573 if (lstat(abspath, &sb) == -1) {
574 err = got_error_from_errno();
575 goto done;
578 if (!S_ISDIR(sb.st_mode)) {
579 /* TODO directory is obstructed; do something */
580 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
581 goto done;
584 return NULL;
585 } else if (errno == ENOENT) {
586 err = make_parent_dirs(abspath);
587 if (err)
588 goto done;
589 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1)
590 err = got_error_from_errno();
591 } else
592 err = got_error_from_errno();
595 done:
596 free(abspath);
597 return err;
600 /*
601 * Perform a 3-way merge where the file's version in the file index (blob2)
602 * acts as the common ancestor, the incoming blob (blob1) acts as the first
603 * derived version, and the file on disk acts as the second derived version.
604 */
605 static const struct got_error *
606 merge_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
607 struct got_fileindex_entry *ie, const char *ondisk_path, const char *path,
608 struct got_blob_object *blob1, struct got_repository *repo,
609 got_worktree_checkout_cb progress_cb, void *progress_arg)
611 const struct got_error *err = NULL;
612 int merged_fd = -1;
613 struct got_blob_object *blob2 = NULL;
614 FILE *f1 = NULL, *f2 = NULL;
615 char *blob1_path = NULL, *blob2_path = NULL;
616 char *merged_path = NULL;
617 struct got_object_id id2;
618 char *id_str = NULL;
619 char *label1 = NULL;
620 int overlapcnt = 0;
622 err = got_opentemp_named_fd(&merged_path, &merged_fd, "/tmp/got-merged");
623 if (err)
624 return err;
625 err = got_opentemp_named(&blob1_path, &f1, "/tmp/got-merge-blob1");
626 if (err)
627 goto done;
628 err = got_object_blob_dump_to_file(NULL, NULL, f1, blob1);
629 if (err)
630 goto done;
632 err = got_opentemp_named(&blob2_path, &f2, "/tmp/got-merge-blob2");
633 if (err)
634 goto done;
636 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
637 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
638 if (err)
639 goto done;
640 err = got_object_blob_dump_to_file(NULL, NULL, f2, blob2);
641 if (err)
642 goto done;
644 err = got_object_id_str(&id_str, worktree->base_commit_id);
645 if (err)
646 goto done;
647 if (asprintf(&label1, "commit %s", id_str) == -1) {
648 err = got_error_from_errno();
649 goto done;
652 err = got_merge_diff3(&overlapcnt, merged_fd, blob1_path,
653 blob2_path, ondisk_path, label1, path);
654 if (err)
655 goto done;
657 (*progress_cb)(progress_arg,
658 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
661 fsync(merged_fd);
663 if (rename(merged_path, ondisk_path) != 0) {
664 err = got_error_from_errno();
665 goto done;
668 err = got_fileindex_entry_update(ie, ondisk_path,
669 blob1->id.sha1, worktree->base_commit_id->sha1);
670 done:
671 if (merged_fd != -1)
672 close(merged_fd);
673 if (f1)
674 fclose(f1);
675 if (f2)
676 fclose(f2);
677 if (blob2)
678 got_object_blob_close(blob2);
679 free(merged_path);
680 if (blob1_path) {
681 unlink(blob1_path);
682 free(blob1_path);
684 if (blob2_path) {
685 unlink(blob2_path);
686 free(blob2_path);
688 free(id_str);
689 free(label1);
690 return err;
693 static const struct got_error *
694 install_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
695 struct got_fileindex_entry *entry, const char *ondisk_path, const char *path,
696 struct got_blob_object *blob,
697 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
698 void *progress_arg)
700 const struct got_error *err = NULL;
701 int fd = -1;
702 size_t len, hdrlen;
703 int update = 0;
704 char *tmppath = NULL;
706 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
707 GOT_DEFAULT_FILE_MODE);
708 if (fd == -1) {
709 if (errno == ENOENT) {
710 char *parent = dirname(path);
711 if (parent == NULL)
712 return got_error_from_errno();
713 err = add_dir_on_disk(worktree, parent);
714 if (err)
715 return err;
716 fd = open(ondisk_path,
717 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
718 GOT_DEFAULT_FILE_MODE);
719 if (fd == -1)
720 return got_error_from_errno();
721 } else if (errno == EEXIST) {
722 struct stat sb;
723 if (lstat(ondisk_path, &sb) == -1) {
724 err = got_error_from_errno();
725 goto done;
726 } else if (!S_ISREG(sb.st_mode)) {
727 /* TODO file is obstructed; do something */
728 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
729 goto done;
730 } else {
731 err = got_opentemp_named_fd(&tmppath, &fd,
732 ondisk_path);
733 if (err)
734 goto done;
735 update = 1;
737 } else
738 return got_error_from_errno();
741 (*progress_cb)(progress_arg,
742 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
744 hdrlen = got_object_blob_get_hdrlen(blob);
745 do {
746 const uint8_t *buf = got_object_blob_get_read_buf(blob);
747 err = got_object_blob_read_block(&len, blob);
748 if (err)
749 break;
750 if (len > 0) {
751 /* Skip blob object header first time around. */
752 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
753 if (outlen == -1) {
754 err = got_error_from_errno();
755 goto done;
756 } else if (outlen != len - hdrlen) {
757 err = got_error(GOT_ERR_IO);
758 goto done;
760 hdrlen = 0;
762 } while (len != 0);
764 fsync(fd);
766 if (update) {
767 if (rename(tmppath, ondisk_path) != 0) {
768 err = got_error_from_errno();
769 goto done;
773 if (entry == NULL)
774 entry = got_fileindex_entry_get(fileindex, path);
775 if (entry)
776 err = got_fileindex_entry_update(entry, ondisk_path,
777 blob->id.sha1, worktree->base_commit_id->sha1);
778 else {
779 err = got_fileindex_entry_alloc(&entry, ondisk_path,
780 path, blob->id.sha1, worktree->base_commit_id->sha1);
781 if (err)
782 goto done;
783 err = got_fileindex_entry_add(fileindex, entry);
785 done:
786 if (fd != -1)
787 close(fd);
788 free(tmppath);
789 return err;
792 static const struct got_error *
793 get_file_status(unsigned char *status, struct got_fileindex_entry *ie,
794 const char *abspath, struct got_repository *repo)
796 const struct got_error *err = NULL;
797 struct got_object_id id;
798 size_t hdrlen;
799 FILE *f = NULL;
800 uint8_t fbuf[8192];
801 struct got_blob_object *blob = NULL;
802 size_t flen, blen;
803 struct stat sb;
805 *status = GOT_STATUS_NO_CHANGE;
807 if (lstat(abspath, &sb) == -1)
808 return got_error_from_errno();
810 if (!S_ISREG(sb.st_mode)) {
811 *status = GOT_STATUS_OBSTRUCTED;
812 return NULL;
815 if (ie->ctime_sec == sb.st_ctime &&
816 ie->ctime_nsec == sb.st_ctimensec &&
817 ie->mtime_sec == sb.st_mtime &&
818 ie->mtime_sec == sb.st_mtime &&
819 ie->mtime_nsec == sb.st_mtimensec &&
820 ie->size == (sb.st_size & 0xffffffff))
821 return NULL;
823 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
824 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
825 if (err)
826 return err;
828 f = fopen(abspath, "r");
829 if (f == NULL) {
830 err = got_error_from_errno();
831 goto done;
833 hdrlen = got_object_blob_get_hdrlen(blob);
834 while (1) {
835 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
836 err = got_object_blob_read_block(&blen, blob);
837 if (err)
838 break;
839 flen = fread(fbuf, 1, sizeof(fbuf), f);
840 if (blen == 0) {
841 if (flen != 0)
842 *status = GOT_STATUS_MODIFIY;
843 break;
844 } else if (flen == 0) {
845 if (blen != 0)
846 *status = GOT_STATUS_MODIFIY;
847 break;
848 } else if (blen - hdrlen == flen) {
849 /* Skip blob object header first time around. */
850 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
851 *status = GOT_STATUS_MODIFIY;
852 break;
854 } else {
855 *status = GOT_STATUS_MODIFIY;
856 break;
858 hdrlen = 0;
860 done:
861 if (blob)
862 got_object_blob_close(blob);
863 if (f)
864 fclose(f);
865 return err;
868 static const struct got_error *
869 update_blob(struct got_worktree *worktree,
870 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
871 struct got_tree_entry *te, const char *path,
872 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
873 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
875 const struct got_error *err = NULL;
876 struct got_blob_object *blob = NULL;
877 char *ondisk_path;
878 unsigned char status = GOT_STATUS_NO_CHANGE;
880 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
881 return got_error_from_errno();
883 if (ie) {
884 if (memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
885 SHA1_DIGEST_LENGTH) == 0) {
886 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
887 path);
888 goto done;
890 if (memcmp(ie->blob_sha1,
891 te->id->sha1, SHA1_DIGEST_LENGTH) == 0)
892 goto done;
894 err = get_file_status(&status, ie, ondisk_path, repo);
895 if (err)
896 goto done;
898 if (status == GOT_STATUS_OBSTRUCTED) {
899 (*progress_cb)(progress_arg, status, path);
900 goto done;
904 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
905 if (err)
906 goto done;
908 if (status == GOT_STATUS_MODIFIY)
909 err = merge_blob(worktree, fileindex, ie, ondisk_path, path,
910 blob, repo, progress_cb, progress_arg);
911 else
912 err = install_blob(worktree, fileindex, ie, ondisk_path, path,
913 blob, repo, progress_cb, progress_arg);
915 got_object_blob_close(blob);
916 done:
917 free(ondisk_path);
918 return err;
921 static const struct got_error *
922 remove_ondisk_file(const char *root_path, const char *path)
924 const struct got_error *err = NULL;
925 char *ondisk_path = NULL;
927 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
928 return got_error_from_errno();
930 if (unlink(ondisk_path) == -1) {
931 if (errno != ENOENT)
932 err = got_error_from_errno();
933 } else {
934 char *parent = dirname(ondisk_path);
935 while (parent && strcmp(parent, root_path) != 0) {
936 if (rmdir(parent) == -1) {
937 if (errno != ENOTEMPTY)
938 err = got_error_from_errno();
939 break;
941 parent = dirname(parent);
944 free(ondisk_path);
945 return err;
948 struct diff_cb_arg {
949 struct got_fileindex *fileindex;
950 struct got_worktree *worktree;
951 struct got_repository *repo;
952 got_worktree_checkout_cb progress_cb;
953 void *progress_arg;
954 got_worktree_cancel_cb cancel_cb;
955 void *cancel_arg;
956 };
958 static const struct got_error *
959 diff_old_new(void *arg, struct got_fileindex_entry *ie,
960 struct got_tree_entry *te, const char *parent_path)
962 struct diff_cb_arg *a = arg;
964 return update_blob(a->worktree, a->fileindex, ie, te,
965 ie->path, a->repo, a->progress_cb, a->progress_arg,
966 a->cancel_cb, a->cancel_arg);
969 static const struct got_error *
970 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
972 const struct got_error *err;
973 struct diff_cb_arg *a = arg;
975 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE, ie->path);
977 err = remove_ondisk_file(a->worktree->root_path, ie->path);
978 if (err)
979 return err;
980 got_fileindex_entry_remove(a->fileindex, ie);
981 return NULL;
984 static const struct got_error *
985 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
987 struct diff_cb_arg *a = arg;
988 const struct got_error *err;
989 char *path;
991 if (asprintf(&path, "%s%s%s", parent_path,
992 parent_path[0] ? "/" : "", te->name)
993 == -1)
994 return got_error_from_errno();
996 if (S_ISDIR(te->mode))
997 err = add_dir_on_disk(a->worktree, path);
998 else
999 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1000 a->repo, a->progress_cb, a->progress_arg,
1001 a->cancel_cb, a->cancel_arg);
1003 free(path);
1004 return err;
1007 const struct got_error *
1008 got_worktree_checkout_files(struct got_worktree *worktree,
1009 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1010 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1012 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1013 struct got_commit_object *commit = NULL;
1014 struct got_object_id *tree_id = NULL;
1015 struct got_tree_object *tree = NULL;
1016 char *fileindex_path = NULL, *new_fileindex_path = NULL;
1017 struct got_fileindex *fileindex = NULL;
1018 FILE *index = NULL, *new_index = NULL;
1019 struct got_fileindex_diff_tree_cb diff_cb;
1020 struct diff_cb_arg arg;
1022 err = lock_worktree(worktree, LOCK_EX);
1023 if (err)
1024 return err;
1026 fileindex = got_fileindex_alloc();
1027 if (fileindex == NULL) {
1028 err = got_error_from_errno();
1029 goto done;
1032 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1033 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1034 err = got_error_from_errno();
1035 fileindex_path = NULL;
1036 goto done;
1040 * Read the file index.
1041 * Checking out files is supposed to be an idempotent operation.
1042 * If the on-disk file index is incomplete we will try to complete it.
1044 index = fopen(fileindex_path, "rb");
1045 if (index == NULL) {
1046 if (errno != ENOENT) {
1047 err = got_error_from_errno();
1048 goto done;
1050 } else {
1051 err = got_fileindex_read(fileindex, index);
1052 fclose(index);
1053 if (err)
1054 goto done;
1057 err = got_opentemp_named(&new_fileindex_path, &new_index,
1058 fileindex_path);
1059 if (err)
1060 goto done;
1062 err = got_object_open_as_commit(&commit, repo,
1063 worktree->base_commit_id);
1064 if (err)
1065 goto done;
1067 err = got_object_id_by_path(&tree_id, repo,
1068 worktree->base_commit_id, worktree->path_prefix);
1069 if (err)
1070 goto done;
1072 err = got_object_open_as_tree(&tree, repo, tree_id);
1073 if (err)
1074 goto done;
1076 diff_cb.diff_old_new = diff_old_new;
1077 diff_cb.diff_old = diff_old;
1078 diff_cb.diff_new = diff_new;
1079 arg.fileindex = fileindex;
1080 arg.worktree = worktree;
1081 arg.repo = repo;
1082 arg.progress_cb = progress_cb;
1083 arg.progress_arg = progress_arg;
1084 arg.cancel_cb = cancel_cb;
1085 arg.cancel_arg = cancel_arg;
1086 checkout_err = got_fileindex_diff_tree(fileindex, tree, repo,
1087 &diff_cb, &arg);
1089 /* Try to sync the fileindex back to disk in any case. */
1090 err = got_fileindex_write(fileindex, new_index);
1091 if (err)
1092 goto done;
1094 if (rename(new_fileindex_path, fileindex_path) != 0) {
1095 err = got_error_from_errno();
1096 goto done;
1099 free(new_fileindex_path);
1100 new_fileindex_path = NULL;
1102 done:
1103 if (tree)
1104 got_object_tree_close(tree);
1105 if (commit)
1106 got_object_commit_close(commit);
1107 if (new_fileindex_path)
1108 unlink(new_fileindex_path);
1109 if (new_index)
1110 fclose(new_index);
1111 free(new_fileindex_path);
1112 free(fileindex_path);
1113 got_fileindex_free(fileindex);
1114 if (checkout_err)
1115 err = checkout_err;
1116 unlockerr = lock_worktree(worktree, LOCK_SH);
1117 if (unlockerr && err == NULL)
1118 err = unlockerr;
1119 return err;
1122 struct diff_dir_cb_arg {
1123 struct got_fileindex *fileindex;
1124 struct got_worktree *worktree;
1125 struct got_repository *repo;
1126 got_worktree_status_cb status_cb;
1127 void *status_arg;
1128 got_worktree_cancel_cb cancel_cb;
1129 void *cancel_arg;
1132 static const struct got_error *
1133 status_old_new(void *arg, struct got_fileindex_entry *ie,
1134 struct dirent *de, const char *parent_path)
1136 const struct got_error *err = NULL;
1137 struct diff_dir_cb_arg *a = arg;
1138 char *abspath;
1139 unsigned char status = GOT_STATUS_NO_CHANGE;
1141 if (parent_path[0]) {
1142 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1143 parent_path, de->d_name) == -1)
1144 return got_error_from_errno();
1145 } else {
1146 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1147 de->d_name) == -1)
1148 return got_error_from_errno();
1151 err = get_file_status(&status, ie, abspath, a->repo);
1152 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1153 struct got_object_id id;
1154 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1155 err = (*a->status_cb)(a->status_arg, status, ie->path, &id);
1157 free(abspath);
1158 return err;
1161 static const struct got_error *
1162 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1164 struct diff_dir_cb_arg *a = arg;
1165 struct got_object_id id;
1166 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1167 return (*a->status_cb)(a->status_arg, GOT_STATUS_MISSING, ie->path,
1168 &id);
1171 static const struct got_error *
1172 status_new(void *arg, struct dirent *de, const char *parent_path)
1174 const struct got_error *err = NULL;
1175 struct diff_dir_cb_arg *a = arg;
1176 char *path = NULL;
1178 if (de->d_type == DT_DIR)
1179 return NULL;
1181 if (parent_path[0]) {
1182 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1183 return got_error_from_errno();
1184 } else {
1185 path = de->d_name;
1188 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1189 NULL);
1190 if (parent_path[0])
1191 free(path);
1192 return err;
1195 const struct got_error *
1196 got_worktree_status(struct got_worktree *worktree,
1197 struct got_repository *repo, got_worktree_status_cb status_cb,
1198 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1200 const struct got_error *err = NULL;
1201 DIR *workdir = NULL;
1202 char *fileindex_path = NULL;
1203 struct got_fileindex *fileindex = NULL;
1204 FILE *index = NULL;
1205 struct got_fileindex_diff_dir_cb fdiff_cb;
1206 struct diff_dir_cb_arg arg;
1208 fileindex = got_fileindex_alloc();
1209 if (fileindex == NULL) {
1210 err = got_error_from_errno();
1211 goto done;
1214 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1215 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1216 err = got_error_from_errno();
1217 fileindex_path = NULL;
1218 goto done;
1221 index = fopen(fileindex_path, "rb");
1222 if (index == NULL) {
1223 if (errno != ENOENT) {
1224 err = got_error_from_errno();
1225 goto done;
1227 } else {
1228 err = got_fileindex_read(fileindex, index);
1229 fclose(index);
1230 if (err)
1231 goto done;
1234 workdir = opendir(worktree->root_path);
1235 if (workdir == NULL) {
1236 err = got_error_from_errno();
1237 goto done;
1239 fdiff_cb.diff_old_new = status_old_new;
1240 fdiff_cb.diff_old = status_old;
1241 fdiff_cb.diff_new = status_new;
1242 arg.fileindex = fileindex;
1243 arg.worktree = worktree;
1244 arg.repo = repo;
1245 arg.status_cb = status_cb;
1246 arg.status_arg = status_arg;
1247 arg.cancel_cb = cancel_cb;
1248 arg.cancel_arg = cancel_arg;
1249 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1250 repo, &fdiff_cb, &arg);
1251 done:
1252 if (workdir)
1253 closedir(workdir);
1254 free(fileindex_path);
1255 got_fileindex_free(fileindex);
1256 return err;