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 uint16_t mode, 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 /*
669 * Do not update timestamps of already modified files. Otherwise,
670 * the status walk would treat them as unmodified files again.
671 */
672 err = got_fileindex_entry_update(ie, ondisk_path,
673 blob1->id.sha1, worktree->base_commit_id->sha1, 0);
674 done:
675 if (merged_fd != -1)
676 close(merged_fd);
677 if (f1)
678 fclose(f1);
679 if (f2)
680 fclose(f2);
681 if (blob2)
682 got_object_blob_close(blob2);
683 free(merged_path);
684 if (blob1_path) {
685 unlink(blob1_path);
686 free(blob1_path);
688 if (blob2_path) {
689 unlink(blob2_path);
690 free(blob2_path);
692 free(id_str);
693 free(label1);
694 return err;
697 static const struct got_error *
698 install_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
699 struct got_fileindex_entry *entry, const char *ondisk_path, const char *path,
700 uint16_t mode, struct got_blob_object *blob,
701 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
702 void *progress_arg)
704 const struct got_error *err = NULL;
705 int fd = -1;
706 size_t len, hdrlen;
707 int update = 0;
708 char *tmppath = NULL;
709 struct stat sb;
711 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
712 GOT_DEFAULT_FILE_MODE);
713 if (fd == -1) {
714 if (errno == ENOENT) {
715 char *parent = dirname(path);
716 if (parent == NULL)
717 return got_error_from_errno();
718 err = add_dir_on_disk(worktree, parent);
719 if (err)
720 return err;
721 fd = open(ondisk_path,
722 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
723 GOT_DEFAULT_FILE_MODE);
724 if (fd == -1)
725 return got_error_from_errno();
726 } else if (errno == EEXIST) {
727 if (lstat(ondisk_path, &sb) == -1) {
728 err = got_error_from_errno();
729 goto done;
730 } else if (!S_ISREG(sb.st_mode)) {
731 /* TODO file is obstructed; do something */
732 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
733 goto done;
734 } else {
735 err = got_opentemp_named_fd(&tmppath, &fd,
736 ondisk_path);
737 if (err)
738 goto done;
739 update = 1;
741 } else
742 return got_error_from_errno();
745 (*progress_cb)(progress_arg,
746 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
748 hdrlen = got_object_blob_get_hdrlen(blob);
749 do {
750 const uint8_t *buf = got_object_blob_get_read_buf(blob);
751 err = got_object_blob_read_block(&len, blob);
752 if (err)
753 break;
754 if (len > 0) {
755 /* Skip blob object header first time around. */
756 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
757 if (outlen == -1) {
758 err = got_error_from_errno();
759 goto done;
760 } else if (outlen != len - hdrlen) {
761 err = got_error(GOT_ERR_IO);
762 goto done;
764 hdrlen = 0;
766 } while (len != 0);
768 fsync(fd);
770 if (update) {
771 if (rename(tmppath, ondisk_path) != 0) {
772 err = got_error_from_errno();
773 goto done;
775 } else {
776 /* In case of an update stat buf has been loaded above. */
777 if (lstat(ondisk_path, &sb) == -1) {
778 err = got_error_from_errno();
779 goto done;
783 if (mode & S_IXUSR) {
784 if (chmod(ondisk_path, sb.st_mode | S_IXUSR) == -1) {
785 err = got_error_from_errno();
786 goto done;
788 } else {
789 if (chmod(ondisk_path, sb.st_mode & ~S_IXUSR) == -1) {
790 err = got_error_from_errno();
791 goto done;
795 if (entry == NULL)
796 entry = got_fileindex_entry_get(fileindex, path);
797 if (entry)
798 err = got_fileindex_entry_update(entry, ondisk_path,
799 blob->id.sha1, worktree->base_commit_id->sha1, 1);
800 else {
801 err = got_fileindex_entry_alloc(&entry, ondisk_path,
802 path, blob->id.sha1, worktree->base_commit_id->sha1);
803 if (err)
804 goto done;
805 err = got_fileindex_entry_add(fileindex, entry);
807 done:
808 if (fd != -1)
809 close(fd);
810 free(tmppath);
811 return err;
814 static const struct got_error *
815 get_file_status(unsigned char *status, struct got_fileindex_entry *ie,
816 const char *abspath, struct got_repository *repo)
818 const struct got_error *err = NULL;
819 struct got_object_id id;
820 size_t hdrlen;
821 FILE *f = NULL;
822 uint8_t fbuf[8192];
823 struct got_blob_object *blob = NULL;
824 size_t flen, blen;
825 struct stat sb;
827 *status = GOT_STATUS_NO_CHANGE;
829 if (lstat(abspath, &sb) == -1)
830 return got_error_from_errno();
832 if (!S_ISREG(sb.st_mode)) {
833 *status = GOT_STATUS_OBSTRUCTED;
834 return NULL;
837 if (ie->ctime_sec == sb.st_ctime &&
838 ie->ctime_nsec == sb.st_ctimensec &&
839 ie->mtime_sec == sb.st_mtime &&
840 ie->mtime_sec == sb.st_mtime &&
841 ie->mtime_nsec == sb.st_mtimensec &&
842 ie->size == (sb.st_size & 0xffffffff))
843 return NULL;
845 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
846 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
847 if (err)
848 return err;
850 f = fopen(abspath, "r");
851 if (f == NULL) {
852 err = got_error_from_errno();
853 goto done;
855 hdrlen = got_object_blob_get_hdrlen(blob);
856 while (1) {
857 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
858 err = got_object_blob_read_block(&blen, blob);
859 if (err)
860 break;
861 flen = fread(fbuf, 1, sizeof(fbuf), f);
862 if (blen == 0) {
863 if (flen != 0)
864 *status = GOT_STATUS_MODIFY;
865 break;
866 } else if (flen == 0) {
867 if (blen != 0)
868 *status = GOT_STATUS_MODIFY;
869 break;
870 } else if (blen - hdrlen == flen) {
871 /* Skip blob object header first time around. */
872 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
873 *status = GOT_STATUS_MODIFY;
874 break;
876 } else {
877 *status = GOT_STATUS_MODIFY;
878 break;
880 hdrlen = 0;
882 done:
883 if (blob)
884 got_object_blob_close(blob);
885 if (f)
886 fclose(f);
887 return err;
890 static const struct got_error *
891 update_blob(struct got_worktree *worktree,
892 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
893 struct got_tree_entry *te, const char *path,
894 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
895 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
897 const struct got_error *err = NULL;
898 struct got_blob_object *blob = NULL;
899 char *ondisk_path;
900 unsigned char status = GOT_STATUS_NO_CHANGE;
902 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
903 return got_error_from_errno();
905 if (ie) {
906 if (memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
907 SHA1_DIGEST_LENGTH) == 0) {
908 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
909 path);
910 goto done;
912 if (memcmp(ie->blob_sha1,
913 te->id->sha1, SHA1_DIGEST_LENGTH) == 0)
914 goto done;
916 err = get_file_status(&status, ie, ondisk_path, repo);
917 if (err)
918 goto done;
920 if (status == GOT_STATUS_OBSTRUCTED) {
921 (*progress_cb)(progress_arg, status, path);
922 goto done;
926 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
927 if (err)
928 goto done;
930 if (status == GOT_STATUS_MODIFY)
931 err = merge_blob(worktree, fileindex, ie, ondisk_path, path,
932 te->mode, blob, repo, progress_cb, progress_arg);
933 else
934 err = install_blob(worktree, fileindex, ie, ondisk_path, path,
935 te->mode, blob, repo, progress_cb, progress_arg);
937 got_object_blob_close(blob);
938 done:
939 free(ondisk_path);
940 return err;
943 static const struct got_error *
944 remove_ondisk_file(const char *root_path, const char *path)
946 const struct got_error *err = NULL;
947 char *ondisk_path = NULL;
949 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
950 return got_error_from_errno();
952 if (unlink(ondisk_path) == -1) {
953 if (errno != ENOENT)
954 err = got_error_from_errno();
955 } else {
956 char *parent = dirname(ondisk_path);
957 while (parent && strcmp(parent, root_path) != 0) {
958 if (rmdir(parent) == -1) {
959 if (errno != ENOTEMPTY)
960 err = got_error_from_errno();
961 break;
963 parent = dirname(parent);
966 free(ondisk_path);
967 return err;
970 struct diff_cb_arg {
971 struct got_fileindex *fileindex;
972 struct got_worktree *worktree;
973 struct got_repository *repo;
974 got_worktree_checkout_cb progress_cb;
975 void *progress_arg;
976 got_worktree_cancel_cb cancel_cb;
977 void *cancel_arg;
978 };
980 static const struct got_error *
981 diff_old_new(void *arg, struct got_fileindex_entry *ie,
982 struct got_tree_entry *te, const char *parent_path)
984 struct diff_cb_arg *a = arg;
986 return update_blob(a->worktree, a->fileindex, ie, te,
987 ie->path, a->repo, a->progress_cb, a->progress_arg,
988 a->cancel_cb, a->cancel_arg);
991 static const struct got_error *
992 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
994 const struct got_error *err;
995 struct diff_cb_arg *a = arg;
997 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE, ie->path);
999 err = remove_ondisk_file(a->worktree->root_path, ie->path);
1000 if (err)
1001 return err;
1002 got_fileindex_entry_remove(a->fileindex, ie);
1003 return NULL;
1006 static const struct got_error *
1007 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1009 struct diff_cb_arg *a = arg;
1010 const struct got_error *err;
1011 char *path;
1013 if (asprintf(&path, "%s%s%s", parent_path,
1014 parent_path[0] ? "/" : "", te->name)
1015 == -1)
1016 return got_error_from_errno();
1018 if (S_ISDIR(te->mode))
1019 err = add_dir_on_disk(a->worktree, path);
1020 else
1021 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1022 a->repo, a->progress_cb, a->progress_arg,
1023 a->cancel_cb, a->cancel_arg);
1025 free(path);
1026 return err;
1029 const struct got_error *
1030 got_worktree_checkout_files(struct got_worktree *worktree,
1031 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1032 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1034 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1035 struct got_commit_object *commit = NULL;
1036 struct got_object_id *tree_id = NULL;
1037 struct got_tree_object *tree = NULL;
1038 char *fileindex_path = NULL, *new_fileindex_path = NULL;
1039 struct got_fileindex *fileindex = NULL;
1040 FILE *index = NULL, *new_index = NULL;
1041 struct got_fileindex_diff_tree_cb diff_cb;
1042 struct diff_cb_arg arg;
1044 err = lock_worktree(worktree, LOCK_EX);
1045 if (err)
1046 return err;
1048 fileindex = got_fileindex_alloc();
1049 if (fileindex == NULL) {
1050 err = got_error_from_errno();
1051 goto done;
1054 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1055 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1056 err = got_error_from_errno();
1057 fileindex_path = NULL;
1058 goto done;
1062 * Read the file index.
1063 * Checking out files is supposed to be an idempotent operation.
1064 * If the on-disk file index is incomplete we will try to complete it.
1066 index = fopen(fileindex_path, "rb");
1067 if (index == NULL) {
1068 if (errno != ENOENT) {
1069 err = got_error_from_errno();
1070 goto done;
1072 } else {
1073 err = got_fileindex_read(fileindex, index);
1074 fclose(index);
1075 if (err)
1076 goto done;
1079 err = got_opentemp_named(&new_fileindex_path, &new_index,
1080 fileindex_path);
1081 if (err)
1082 goto done;
1084 err = got_object_open_as_commit(&commit, repo,
1085 worktree->base_commit_id);
1086 if (err)
1087 goto done;
1089 err = got_object_id_by_path(&tree_id, repo,
1090 worktree->base_commit_id, worktree->path_prefix);
1091 if (err)
1092 goto done;
1094 err = got_object_open_as_tree(&tree, repo, tree_id);
1095 if (err)
1096 goto done;
1098 diff_cb.diff_old_new = diff_old_new;
1099 diff_cb.diff_old = diff_old;
1100 diff_cb.diff_new = diff_new;
1101 arg.fileindex = fileindex;
1102 arg.worktree = worktree;
1103 arg.repo = repo;
1104 arg.progress_cb = progress_cb;
1105 arg.progress_arg = progress_arg;
1106 arg.cancel_cb = cancel_cb;
1107 arg.cancel_arg = cancel_arg;
1108 checkout_err = got_fileindex_diff_tree(fileindex, tree, repo,
1109 &diff_cb, &arg);
1111 /* Try to sync the fileindex back to disk in any case. */
1112 err = got_fileindex_write(fileindex, new_index);
1113 if (err)
1114 goto done;
1116 if (rename(new_fileindex_path, fileindex_path) != 0) {
1117 err = got_error_from_errno();
1118 goto done;
1121 free(new_fileindex_path);
1122 new_fileindex_path = NULL;
1124 done:
1125 if (tree)
1126 got_object_tree_close(tree);
1127 if (commit)
1128 got_object_commit_close(commit);
1129 if (new_fileindex_path)
1130 unlink(new_fileindex_path);
1131 if (new_index)
1132 fclose(new_index);
1133 free(new_fileindex_path);
1134 free(fileindex_path);
1135 got_fileindex_free(fileindex);
1136 if (checkout_err)
1137 err = checkout_err;
1138 unlockerr = lock_worktree(worktree, LOCK_SH);
1139 if (unlockerr && err == NULL)
1140 err = unlockerr;
1141 return err;
1144 struct diff_dir_cb_arg {
1145 struct got_fileindex *fileindex;
1146 struct got_worktree *worktree;
1147 struct got_repository *repo;
1148 got_worktree_status_cb status_cb;
1149 void *status_arg;
1150 got_worktree_cancel_cb cancel_cb;
1151 void *cancel_arg;
1154 static const struct got_error *
1155 status_old_new(void *arg, struct got_fileindex_entry *ie,
1156 struct dirent *de, const char *parent_path)
1158 const struct got_error *err = NULL;
1159 struct diff_dir_cb_arg *a = arg;
1160 char *abspath;
1161 unsigned char status = GOT_STATUS_NO_CHANGE;
1163 if (parent_path[0]) {
1164 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1165 parent_path, de->d_name) == -1)
1166 return got_error_from_errno();
1167 } else {
1168 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1169 de->d_name) == -1)
1170 return got_error_from_errno();
1173 err = get_file_status(&status, ie, abspath, a->repo);
1174 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1175 struct got_object_id id;
1176 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1177 err = (*a->status_cb)(a->status_arg, status, ie->path, &id);
1179 free(abspath);
1180 return err;
1183 static const struct got_error *
1184 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1186 struct diff_dir_cb_arg *a = arg;
1187 struct got_object_id id;
1188 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1189 return (*a->status_cb)(a->status_arg, GOT_STATUS_MISSING, ie->path,
1190 &id);
1193 static const struct got_error *
1194 status_new(void *arg, struct dirent *de, const char *parent_path)
1196 const struct got_error *err = NULL;
1197 struct diff_dir_cb_arg *a = arg;
1198 char *path = NULL;
1200 if (de->d_type == DT_DIR)
1201 return NULL;
1203 if (parent_path[0]) {
1204 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1205 return got_error_from_errno();
1206 } else {
1207 path = de->d_name;
1210 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1211 NULL);
1212 if (parent_path[0])
1213 free(path);
1214 return err;
1217 const struct got_error *
1218 got_worktree_status(struct got_worktree *worktree,
1219 struct got_repository *repo, got_worktree_status_cb status_cb,
1220 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1222 const struct got_error *err = NULL;
1223 DIR *workdir = NULL;
1224 char *fileindex_path = NULL;
1225 struct got_fileindex *fileindex = NULL;
1226 FILE *index = NULL;
1227 struct got_fileindex_diff_dir_cb fdiff_cb;
1228 struct diff_dir_cb_arg arg;
1230 fileindex = got_fileindex_alloc();
1231 if (fileindex == NULL) {
1232 err = got_error_from_errno();
1233 goto done;
1236 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1237 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1238 err = got_error_from_errno();
1239 fileindex_path = NULL;
1240 goto done;
1243 index = fopen(fileindex_path, "rb");
1244 if (index == NULL) {
1245 if (errno != ENOENT) {
1246 err = got_error_from_errno();
1247 goto done;
1249 } else {
1250 err = got_fileindex_read(fileindex, index);
1251 fclose(index);
1252 if (err)
1253 goto done;
1256 workdir = opendir(worktree->root_path);
1257 if (workdir == NULL) {
1258 err = got_error_from_errno();
1259 goto done;
1261 fdiff_cb.diff_old_new = status_old_new;
1262 fdiff_cb.diff_old = status_old;
1263 fdiff_cb.diff_new = status_new;
1264 arg.fileindex = fileindex;
1265 arg.worktree = worktree;
1266 arg.repo = repo;
1267 arg.status_cb = status_cb;
1268 arg.status_arg = status_arg;
1269 arg.cancel_cb = cancel_cb;
1270 arg.cancel_arg = cancel_arg;
1271 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1272 repo, &fdiff_cb, &arg);
1273 done:
1274 if (workdir)
1275 closedir(workdir);
1276 free(fileindex_path);
1277 got_fileindex_free(fileindex);
1278 return err;