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>
34 #include <uuid.h>
36 #include "got_error.h"
37 #include "got_repository.h"
38 #include "got_reference.h"
39 #include "got_object.h"
40 #include "got_worktree.h"
41 #include "got_opentemp.h"
43 #include "got_lib_worktree.h"
44 #include "got_lib_path.h"
45 #include "got_lib_sha1.h"
46 #include "got_lib_fileindex.h"
47 #include "got_lib_inflate.h"
48 #include "got_lib_delta.h"
49 #include "got_lib_object.h"
50 #include "got_lib_diff.h"
52 #ifndef MIN
53 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
54 #endif
56 static const struct got_error *
57 create_meta_file(const char *path_got, const char *name, const char *content)
58 {
59 const struct got_error *err = NULL;
60 char *path;
61 int fd = -1;
63 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
64 err = got_error_from_errno();
65 path = NULL;
66 goto done;
67 }
69 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
70 GOT_DEFAULT_FILE_MODE);
71 if (fd == -1) {
72 err = got_error_from_errno();
73 goto done;
74 }
76 if (content) {
77 int len = dprintf(fd, "%s\n", content);
78 if (len != strlen(content) + 1) {
79 err = got_error_from_errno();
80 goto done;
81 }
82 }
84 done:
85 if (fd != -1 && close(fd) == -1 && err == NULL)
86 err = got_error_from_errno();
87 free(path);
88 return err;
89 }
91 static const struct got_error *
92 update_meta_file(const char *path_got, const char *name, const char *content)
93 {
94 const struct got_error *err = NULL;
95 FILE *tmpfile = NULL;
96 char *tmppath = NULL;
97 char *path = NULL;
99 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
100 err = got_error_from_errno();
101 path = NULL;
102 goto done;
105 err = got_opentemp_named(&tmppath, &tmpfile, path);
106 if (err)
107 goto done;
109 if (content) {
110 int len = fprintf(tmpfile, "%s\n", content);
111 if (len != strlen(content) + 1) {
112 err = got_error_from_errno();
113 goto done;
117 if (rename(tmppath, path) != 0) {
118 err = got_error_from_errno();
119 unlink(tmppath);
120 goto done;
123 done:
124 free(tmppath);
125 if (fclose(tmpfile) != 0 && err == NULL)
126 err = got_error_from_errno();
127 return err;
130 static const struct got_error *
131 read_meta_file(char **content, const char *path_got, const char *name)
133 const struct got_error *err = NULL;
134 char *path;
135 int fd = -1;
136 ssize_t n;
137 struct stat sb;
139 *content = NULL;
141 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
142 err = got_error_from_errno();
143 path = NULL;
144 goto done;
147 fd = open(path, O_RDONLY | O_NOFOLLOW);
148 if (fd == -1) {
149 if (errno == ENOENT)
150 err = got_error(GOT_ERR_WORKTREE_META);
151 else
152 err = got_error_from_errno();
153 goto done;
155 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
156 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
157 : got_error_from_errno());
158 goto done;
161 if (lstat(path, &sb) != 0) {
162 err = got_error_from_errno();
163 goto done;
165 *content = calloc(1, sb.st_size);
166 if (*content == NULL) {
167 err = got_error_from_errno();
168 goto done;
171 n = read(fd, *content, sb.st_size);
172 if (n != sb.st_size) {
173 err = (n == -1 ? got_error_from_errno() :
174 got_error(GOT_ERR_WORKTREE_META));
175 goto done;
177 if ((*content)[sb.st_size - 1] != '\n') {
178 err = got_error(GOT_ERR_WORKTREE_META);
179 goto done;
181 (*content)[sb.st_size - 1] = '\0';
183 done:
184 if (fd != -1 && close(fd) == -1 && err == NULL)
185 err = got_error_from_errno();
186 free(path);
187 if (err) {
188 free(*content);
189 *content = NULL;
191 return err;
194 const struct got_error *
195 got_worktree_init(const char *path, struct got_reference *head_ref,
196 const char *prefix, struct got_repository *repo)
198 const struct got_error *err = NULL;
199 struct got_object_id *commit_id = NULL;
200 uuid_t uuid;
201 uint32_t uuid_status;
202 int obj_type;
203 char *path_got = NULL;
204 char *refstr = NULL;
205 char *formatstr = NULL;
206 char *absprefix = NULL;
207 char *basestr = NULL;
208 char *uuidstr = NULL;
210 if (strcmp(path, got_repo_get_path(repo)) == 0) {
211 err = got_error(GOT_ERR_WORKTREE_REPO);
212 goto done;
215 err = got_ref_resolve(&commit_id, repo, head_ref);
216 if (err)
217 return err;
218 err = got_object_get_type(&obj_type, repo, commit_id);
219 if (err)
220 return err;
221 if (obj_type != GOT_OBJ_TYPE_COMMIT)
222 return got_error(GOT_ERR_OBJ_TYPE);
224 if (!got_path_is_absolute(prefix)) {
225 if (asprintf(&absprefix, "/%s", prefix) == -1)
226 return got_error_from_errno();
229 /* Create top-level directory (may already exist). */
230 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
231 err = got_error_from_errno();
232 goto done;
235 /* Create .got directory (may already exist). */
236 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
237 err = got_error_from_errno();
238 goto done;
240 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
241 err = got_error_from_errno();
242 goto done;
245 /* Create an empty lock file. */
246 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
247 if (err)
248 goto done;
250 /* Create an empty file index. */
251 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
252 if (err)
253 goto done;
255 /* Write the HEAD reference. */
256 refstr = got_ref_to_str(head_ref);
257 if (refstr == NULL) {
258 err = got_error_from_errno();
259 goto done;
261 err = create_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
262 if (err)
263 goto done;
265 /* Record our base commit. */
266 err = got_object_id_str(&basestr, commit_id);
267 if (err)
268 goto done;
269 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
270 if (err)
271 goto done;
273 /* Store path to repository. */
274 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
275 got_repo_get_path(repo));
276 if (err)
277 goto done;
279 /* Store in-repository path prefix. */
280 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
281 absprefix ? absprefix : prefix);
282 if (err)
283 goto done;
285 /* Generate UUID. */
286 uuid_create(&uuid, &uuid_status);
287 if (uuid_status != uuid_s_ok) {
288 err = got_error_uuid(uuid_status);
289 goto done;
291 uuid_to_string(&uuid, &uuidstr, &uuid_status);
292 if (uuid_status != uuid_s_ok) {
293 err = got_error_uuid(uuid_status);
294 goto done;
296 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
297 if (err)
298 goto done;
300 /* Stamp work tree with format file. */
301 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
302 err = got_error_from_errno();
303 goto done;
305 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
306 if (err)
307 goto done;
309 done:
310 free(commit_id);
311 free(path_got);
312 free(formatstr);
313 free(refstr);
314 free(absprefix);
315 free(basestr);
316 free(uuidstr);
317 return err;
320 static const struct got_error *
321 open_worktree(struct got_worktree **worktree, const char *path)
323 const struct got_error *err = NULL;
324 char *path_got;
325 char *formatstr = NULL;
326 char *uuidstr = NULL;
327 char *path_lock = NULL;
328 char *base_commit_id_str = NULL;
329 char *head_ref_str = NULL;
330 int version, fd = -1;
331 const char *errstr;
332 struct got_repository *repo = NULL;
333 uint32_t uuid_status;
335 *worktree = NULL;
337 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
338 err = got_error_from_errno();
339 path_got = NULL;
340 goto done;
343 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
344 err = got_error_from_errno();
345 path_lock = NULL;
346 goto done;
349 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
350 if (fd == -1) {
351 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
352 : got_error_from_errno());
353 goto done;
356 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
357 if (err)
358 goto done;
360 version = strtonum(formatstr, 1, INT_MAX, &errstr);
361 if (errstr) {
362 err = got_error(GOT_ERR_WORKTREE_META);
363 goto done;
365 if (version != GOT_WORKTREE_FORMAT_VERSION) {
366 err = got_error(GOT_ERR_WORKTREE_VERS);
367 goto done;
370 *worktree = calloc(1, sizeof(**worktree));
371 if (*worktree == NULL) {
372 err = got_error_from_errno();
373 goto done;
375 (*worktree)->lockfd = -1;
377 (*worktree)->root_path = strdup(path);
378 if ((*worktree)->root_path == NULL) {
379 err = got_error_from_errno();
380 goto done;
382 err = read_meta_file(&(*worktree)->repo_path, path_got,
383 GOT_WORKTREE_REPOSITORY);
384 if (err)
385 goto done;
387 err = read_meta_file(&(*worktree)->path_prefix, path_got,
388 GOT_WORKTREE_PATH_PREFIX);
389 if (err)
390 goto done;
392 err = read_meta_file(&base_commit_id_str, path_got,
393 GOT_WORKTREE_BASE_COMMIT);
394 if (err)
395 goto done;
397 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
398 if (err)
399 goto done;
400 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
401 if (uuid_status != uuid_s_ok) {
402 err = got_error_uuid(uuid_status);
403 goto done;
406 err = got_repo_open(&repo, (*worktree)->repo_path);
407 if (err)
408 goto done;
410 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
411 base_commit_id_str);
412 if (err)
413 goto done;
415 err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
416 if (err)
417 goto done;
419 err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
420 done:
421 if (repo)
422 got_repo_close(repo);
423 free(path_got);
424 free(path_lock);
425 free(head_ref_str);
426 free(base_commit_id_str);
427 free(uuidstr);
428 free(formatstr);
429 if (err) {
430 if (fd != -1)
431 close(fd);
432 if (*worktree != NULL)
433 got_worktree_close(*worktree);
434 *worktree = NULL;
435 } else
436 (*worktree)->lockfd = fd;
438 return err;
441 const struct got_error *
442 got_worktree_open(struct got_worktree **worktree, const char *path)
444 const struct got_error *err = NULL;
446 do {
447 err = open_worktree(worktree, path);
448 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
449 return err;
450 if (*worktree)
451 return NULL;
452 path = dirname(path);
453 if (path == NULL)
454 return got_error_from_errno();
455 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
457 return got_error(GOT_ERR_NOT_WORKTREE);
460 const struct got_error *
461 got_worktree_close(struct got_worktree *worktree)
463 const struct got_error *err = NULL;
464 free(worktree->root_path);
465 free(worktree->repo_path);
466 free(worktree->path_prefix);
467 free(worktree->base_commit_id);
468 if (worktree->head_ref)
469 got_ref_close(worktree->head_ref);
470 if (worktree->lockfd != -1)
471 if (close(worktree->lockfd) != 0)
472 err = got_error_from_errno();
473 free(worktree);
474 return err;
477 const char *
478 got_worktree_get_root_path(struct got_worktree *worktree)
480 return worktree->root_path;
483 const char *
484 got_worktree_get_repo_path(struct got_worktree *worktree)
486 return worktree->repo_path;
489 const char *
490 got_worktree_get_path_prefix(struct got_worktree *worktree)
492 return worktree->path_prefix;
495 const struct got_error *
496 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
497 const char *path_prefix)
499 char *absprefix = NULL;
501 if (!got_path_is_absolute(path_prefix)) {
502 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
503 return got_error_from_errno();
505 *match = (strcmp(absprefix ? absprefix : path_prefix,
506 worktree->path_prefix) == 0);
507 free(absprefix);
508 return NULL;
511 char *
512 got_worktree_get_head_ref_name(struct got_worktree *worktree)
514 return got_ref_to_str(worktree->head_ref);
517 struct got_reference *
518 got_worktree_get_head_ref(struct got_worktree *worktree)
520 return got_ref_dup(worktree->head_ref);
523 struct got_object_id *
524 got_worktree_get_base_commit_id(struct got_worktree *worktree)
526 return worktree->base_commit_id;
529 const struct got_error *
530 got_worktree_set_base_commit_id(struct got_worktree *worktree,
531 struct got_repository *repo, struct got_object_id *commit_id)
533 const struct got_error *err;
534 struct got_object *obj = NULL;
535 char *id_str = NULL;
536 char *path_got = NULL;
538 if (asprintf(&path_got, "%s/%s", worktree->root_path,
539 GOT_WORKTREE_GOT_DIR) == -1) {
540 err = got_error_from_errno();
541 path_got = NULL;
542 goto done;
545 err = got_object_open(&obj, repo, commit_id);
546 if (err)
547 return err;
549 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
550 err = got_error(GOT_ERR_OBJ_TYPE);
551 goto done;
554 /* Record our base commit. */
555 err = got_object_id_str(&id_str, commit_id);
556 if (err)
557 goto done;
558 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
559 if (err)
560 goto done;
562 free(worktree->base_commit_id);
563 worktree->base_commit_id = got_object_id_dup(commit_id);
564 if (worktree->base_commit_id == NULL) {
565 err = got_error_from_errno();
566 goto done;
568 done:
569 if (obj)
570 got_object_close(obj);
571 free(id_str);
572 free(path_got);
573 return err;
576 static const struct got_error *
577 lock_worktree(struct got_worktree *worktree, int operation)
579 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
580 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
581 : got_error_from_errno());
582 return NULL;
585 static const struct got_error *
586 add_dir_on_disk(struct got_worktree *worktree, const char *path)
588 const struct got_error *err = NULL;
589 char *abspath;
591 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
592 return got_error_from_errno();
594 err = got_path_mkdir(abspath);
595 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
596 struct stat sb;
597 err = NULL;
598 if (lstat(abspath, &sb) == -1) {
599 err = got_error_from_errno();
600 } else if (!S_ISDIR(sb.st_mode)) {
601 /* TODO directory is obstructed; do something */
602 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
605 free(abspath);
606 return err;
609 static const struct got_error *
610 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
612 const struct got_error *err = NULL;
613 uint8_t fbuf1[8192];
614 uint8_t fbuf2[8192];
615 size_t flen1 = 0, flen2 = 0;
617 *same = 1;
619 while (1) {
620 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
621 if (flen1 == 0 && ferror(f1)) {
622 err = got_error_from_errno();
623 break;
625 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
626 if (flen2 == 0 && ferror(f2)) {
627 err = got_error_from_errno();
628 break;
630 if (flen1 == 0) {
631 if (flen2 != 0)
632 *same = 0;
633 break;
634 } else if (flen2 == 0) {
635 if (flen1 != 0)
636 *same = 0;
637 break;
638 } else if (flen1 == flen2) {
639 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
640 *same = 0;
641 break;
643 } else {
644 *same = 0;
645 break;
649 return err;
652 static const struct got_error *
653 check_files_equal(int *same, const char *f1_path, const char *f2_path)
655 const struct got_error *err = NULL;
656 struct stat sb;
657 size_t size1, size2;
658 FILE *f1 = NULL, *f2 = NULL;
660 *same = 1;
662 if (lstat(f1_path, &sb) != 0) {
663 err = got_error_from_errno();
664 goto done;
666 size1 = sb.st_size;
668 if (lstat(f2_path, &sb) != 0) {
669 err = got_error_from_errno();
670 goto done;
672 size2 = sb.st_size;
674 if (size1 != size2) {
675 *same = 0;
676 return NULL;
679 f1 = fopen(f1_path, "r");
680 if (f1 == NULL)
681 return got_error_from_errno();
683 f2 = fopen(f2_path, "r");
684 if (f2 == NULL) {
685 err = got_error_from_errno();
686 goto done;
689 err = check_file_contents_equal(same, f1, f2);
690 done:
691 if (f1 && fclose(f1) != 0 && err == NULL)
692 err = got_error_from_errno();
693 if (f2 && fclose(f2) != 0 && err == NULL)
694 err = got_error_from_errno();
696 return err;
699 /*
700 * Perform a 3-way merge where the file's version in the file index (blob2)
701 * acts as the common ancestor, the incoming blob (blob1) acts as the first
702 * derived version, and the file on disk acts as the second derived version.
703 */
704 static const struct got_error *
705 merge_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
706 struct got_fileindex_entry *ie, const char *ondisk_path, const char *path,
707 uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob1,
708 struct got_repository *repo,
709 got_worktree_checkout_cb progress_cb, void *progress_arg)
711 const struct got_error *err = NULL;
712 int merged_fd = -1;
713 struct got_blob_object *blob2 = NULL;
714 FILE *f1 = NULL, *f2 = NULL;
715 char *blob1_path = NULL, *blob2_path = NULL;
716 char *merged_path = NULL, *base_path = NULL;
717 struct got_object_id id2;
718 char *id_str = NULL;
719 char *label1 = NULL;
720 int overlapcnt = 0, update_timestamps = 0;
721 char *parent;
723 parent = dirname(ondisk_path);
724 if (parent == NULL)
725 return got_error_from_errno();
727 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
728 return got_error_from_errno();
730 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
731 if (err)
732 goto done;
734 free(base_path);
735 if (asprintf(&base_path, "%s/got-merge-blob1", parent) == -1) {
736 err = got_error_from_errno();
737 base_path = NULL;
738 goto done;
741 err = got_opentemp_named(&blob1_path, &f1, base_path);
742 if (err)
743 goto done;
744 err = got_object_blob_dump_to_file(NULL, NULL, f1, blob1);
745 if (err)
746 goto done;
748 free(base_path);
749 if (asprintf(&base_path, "%s/got-merge-blob2", parent) == -1) {
750 err = got_error_from_errno();
751 base_path = NULL;
752 goto done;
755 err = got_opentemp_named(&blob2_path, &f2, base_path);
756 if (err)
757 goto done;
759 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
760 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
761 if (err)
762 goto done;
763 err = got_object_blob_dump_to_file(NULL, NULL, f2, blob2);
764 if (err)
765 goto done;
767 err = got_object_id_str(&id_str, worktree->base_commit_id);
768 if (err)
769 goto done;
770 if (asprintf(&label1, "commit %s", id_str) == -1) {
771 err = got_error_from_errno();
772 goto done;
775 err = got_merge_diff3(&overlapcnt, merged_fd, blob1_path,
776 blob2_path, ondisk_path, label1, path);
777 if (err)
778 goto done;
780 (*progress_cb)(progress_arg,
781 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
784 if (fsync(merged_fd) != 0) {
785 err = got_error_from_errno();
786 goto done;
789 /* Check if a clean merge has subsumed all local changes. */
790 if (overlapcnt == 0) {
791 err = check_files_equal(&update_timestamps, blob1_path,
792 merged_path);
793 if (err)
794 goto done;
797 if (chmod(merged_path, st_mode) != 0) {
798 err = got_error_from_errno();
799 goto done;
802 if (rename(merged_path, ondisk_path) != 0) {
803 err = got_error_from_errno();
804 unlink(merged_path);
805 goto done;
808 /*
809 * Do not update timestamps of already modified files. Otherwise,
810 * a future status walk would treat them as unmodified files again.
811 */
812 err = got_fileindex_entry_update(ie, ondisk_path,
813 blob1->id.sha1, worktree->base_commit_id->sha1, update_timestamps);
814 done:
815 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
816 err = got_error_from_errno();
817 if (f1 && fclose(f1) != 0 && err == NULL)
818 err = got_error_from_errno();
819 if (f2 && fclose(f2) != 0 && err == NULL)
820 err = got_error_from_errno();
821 if (blob2)
822 got_object_blob_close(blob2);
823 free(merged_path);
824 free(base_path);
825 if (blob1_path) {
826 unlink(blob1_path);
827 free(blob1_path);
829 if (blob2_path) {
830 unlink(blob2_path);
831 free(blob2_path);
833 free(id_str);
834 free(label1);
835 return err;
838 static const struct got_error *
839 install_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
840 struct got_fileindex_entry *entry, const char *ondisk_path, const char *path,
841 uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob,
842 int restoring_missing_file, struct got_repository *repo,
843 got_worktree_checkout_cb progress_cb, void *progress_arg)
845 const struct got_error *err = NULL;
846 int fd = -1;
847 size_t len, hdrlen;
848 int update = 0;
849 char *tmppath = NULL;
851 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
852 GOT_DEFAULT_FILE_MODE);
853 if (fd == -1) {
854 if (errno == ENOENT) {
855 char *parent = dirname(path);
856 if (parent == NULL)
857 return got_error_from_errno();
858 err = add_dir_on_disk(worktree, parent);
859 if (err)
860 return err;
861 fd = open(ondisk_path,
862 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
863 GOT_DEFAULT_FILE_MODE);
864 if (fd == -1)
865 return got_error_from_errno();
866 } else if (errno == EEXIST) {
867 if (!S_ISREG(st_mode)) {
868 /* TODO file is obstructed; do something */
869 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
870 goto done;
871 } else {
872 err = got_opentemp_named_fd(&tmppath, &fd,
873 ondisk_path);
874 if (err)
875 goto done;
876 update = 1;
878 } else
879 return got_error_from_errno();
882 if (restoring_missing_file)
883 (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
884 else
885 (*progress_cb)(progress_arg,
886 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
888 hdrlen = got_object_blob_get_hdrlen(blob);
889 do {
890 const uint8_t *buf = got_object_blob_get_read_buf(blob);
891 err = got_object_blob_read_block(&len, blob);
892 if (err)
893 break;
894 if (len > 0) {
895 /* Skip blob object header first time around. */
896 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
897 if (outlen == -1) {
898 err = got_error_from_errno();
899 goto done;
900 } else if (outlen != len - hdrlen) {
901 err = got_error(GOT_ERR_IO);
902 goto done;
904 hdrlen = 0;
906 } while (len != 0);
908 if (fsync(fd) != 0) {
909 err = got_error_from_errno();
910 goto done;
913 if (update) {
914 if (rename(tmppath, ondisk_path) != 0) {
915 err = got_error_from_errno();
916 unlink(tmppath);
917 goto done;
921 if (te_mode & S_IXUSR) {
922 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
923 err = got_error_from_errno();
924 goto done;
926 } else {
927 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
928 err = got_error_from_errno();
929 goto done;
933 if (entry == NULL)
934 entry = got_fileindex_entry_get(fileindex, path);
935 if (entry)
936 err = got_fileindex_entry_update(entry, ondisk_path,
937 blob->id.sha1, worktree->base_commit_id->sha1, 1);
938 else {
939 err = got_fileindex_entry_alloc(&entry, ondisk_path,
940 path, blob->id.sha1, worktree->base_commit_id->sha1);
941 if (err)
942 goto done;
943 err = got_fileindex_entry_add(fileindex, entry);
945 done:
946 if (fd != -1 && close(fd) != 0 && err == NULL)
947 err = got_error_from_errno();
948 free(tmppath);
949 return err;
952 static const struct got_error *
953 get_file_status(unsigned char *status, struct stat *sb,
954 struct got_fileindex_entry *ie, const char *abspath,
955 struct got_repository *repo)
957 const struct got_error *err = NULL;
958 struct got_object_id id;
959 size_t hdrlen;
960 FILE *f = NULL;
961 uint8_t fbuf[8192];
962 struct got_blob_object *blob = NULL;
963 size_t flen, blen;
965 *status = GOT_STATUS_NO_CHANGE;
967 if (lstat(abspath, sb) == -1) {
968 if (errno == ENOENT) {
969 if (ie) {
970 *status = GOT_STATUS_MISSING;
971 sb->st_mode =
972 ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
973 & (S_IRWXU | S_IRWXG | S_IRWXO));
974 } else
975 sb->st_mode = GOT_DEFAULT_FILE_MODE;
976 return NULL;
978 return got_error_from_errno();
981 if (!S_ISREG(sb->st_mode)) {
982 *status = GOT_STATUS_OBSTRUCTED;
983 return NULL;
986 if (ie == NULL)
987 return NULL;
989 if (!got_fileindex_entry_has_blob(ie)) {
990 *status = GOT_STATUS_ADD;
991 return NULL;
994 if (ie->ctime_sec == sb->st_ctime &&
995 ie->ctime_nsec == sb->st_ctimensec &&
996 ie->mtime_sec == sb->st_mtime &&
997 ie->mtime_sec == sb->st_mtime &&
998 ie->mtime_nsec == sb->st_mtimensec &&
999 ie->size == (sb->st_size & 0xffffffff))
1000 return NULL;
1002 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1003 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1004 if (err)
1005 return err;
1007 f = fopen(abspath, "r");
1008 if (f == NULL) {
1009 err = got_error_from_errno();
1010 goto done;
1012 hdrlen = got_object_blob_get_hdrlen(blob);
1013 while (1) {
1014 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1015 err = got_object_blob_read_block(&blen, blob);
1016 if (err)
1017 break;
1018 /* Skip length of blob object header first time around. */
1019 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1020 if (flen == 0 && ferror(f)) {
1021 err = got_error_from_errno();
1022 break;
1024 if (blen == 0) {
1025 if (flen != 0)
1026 *status = GOT_STATUS_MODIFY;
1027 break;
1028 } else if (flen == 0) {
1029 if (blen != 0)
1030 *status = GOT_STATUS_MODIFY;
1031 break;
1032 } else if (blen - hdrlen == flen) {
1033 /* Skip blob object header first time around. */
1034 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1035 *status = GOT_STATUS_MODIFY;
1036 break;
1038 } else {
1039 *status = GOT_STATUS_MODIFY;
1040 break;
1042 hdrlen = 0;
1044 done:
1045 if (blob)
1046 got_object_blob_close(blob);
1047 if (f)
1048 fclose(f);
1049 return err;
1052 static const struct got_error *
1053 update_blob(struct got_worktree *worktree,
1054 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1055 struct got_tree_entry *te, const char *path,
1056 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1057 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1059 const struct got_error *err = NULL;
1060 struct got_blob_object *blob = NULL;
1061 char *ondisk_path;
1062 unsigned char status = GOT_STATUS_NO_CHANGE;
1063 struct stat sb;
1065 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1066 return got_error_from_errno();
1068 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1069 if (err)
1070 goto done;
1072 if (status == GOT_STATUS_OBSTRUCTED) {
1073 (*progress_cb)(progress_arg, status, path);
1074 goto done;
1077 if (ie && status != GOT_STATUS_MISSING) {
1078 if (memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1079 SHA1_DIGEST_LENGTH) == 0) {
1080 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1081 path);
1082 goto done;
1084 if (memcmp(ie->blob_sha1,
1085 te->id->sha1, SHA1_DIGEST_LENGTH) == 0)
1086 goto done;
1089 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1090 if (err)
1091 goto done;
1093 if (status == GOT_STATUS_MODIFY)
1094 err = merge_blob(worktree, fileindex, ie, ondisk_path, path,
1095 te->mode, sb.st_mode, blob, repo, progress_cb,
1096 progress_arg);
1097 else
1098 err = install_blob(worktree, fileindex, ie, ondisk_path, path,
1099 te->mode, sb.st_mode, blob, status == GOT_STATUS_MISSING,
1100 repo, progress_cb, progress_arg);
1102 got_object_blob_close(blob);
1103 done:
1104 free(ondisk_path);
1105 return err;
1108 static const struct got_error *
1109 remove_ondisk_file(const char *root_path, const char *path)
1111 const struct got_error *err = NULL;
1112 char *ondisk_path = NULL;
1114 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1115 return got_error_from_errno();
1117 if (unlink(ondisk_path) == -1) {
1118 if (errno != ENOENT)
1119 err = got_error_from_errno();
1120 } else {
1121 char *parent = dirname(ondisk_path);
1122 while (parent && strcmp(parent, root_path) != 0) {
1123 if (rmdir(parent) == -1) {
1124 if (errno != ENOTEMPTY)
1125 err = got_error_from_errno();
1126 break;
1128 parent = dirname(parent);
1131 free(ondisk_path);
1132 return err;
1135 struct diff_cb_arg {
1136 struct got_fileindex *fileindex;
1137 struct got_worktree *worktree;
1138 struct got_repository *repo;
1139 got_worktree_checkout_cb progress_cb;
1140 void *progress_arg;
1141 got_worktree_cancel_cb cancel_cb;
1142 void *cancel_arg;
1145 static const struct got_error *
1146 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1147 struct got_tree_entry *te, const char *parent_path)
1149 struct diff_cb_arg *a = arg;
1151 return update_blob(a->worktree, a->fileindex, ie, te,
1152 ie->path, a->repo, a->progress_cb, a->progress_arg,
1153 a->cancel_cb, a->cancel_arg);
1156 static const struct got_error *
1157 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1159 const struct got_error *err;
1160 struct diff_cb_arg *a = arg;
1162 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE, ie->path);
1164 err = remove_ondisk_file(a->worktree->root_path, ie->path);
1165 if (err)
1166 return err;
1167 got_fileindex_entry_remove(a->fileindex, ie);
1168 return NULL;
1171 static const struct got_error *
1172 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1174 struct diff_cb_arg *a = arg;
1175 const struct got_error *err;
1176 char *path;
1178 if (asprintf(&path, "%s%s%s", parent_path,
1179 parent_path[0] ? "/" : "", te->name)
1180 == -1)
1181 return got_error_from_errno();
1183 if (S_ISDIR(te->mode))
1184 err = add_dir_on_disk(a->worktree, path);
1185 else
1186 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1187 a->repo, a->progress_cb, a->progress_arg,
1188 a->cancel_cb, a->cancel_arg);
1190 free(path);
1191 return err;
1194 const struct got_error *
1195 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1197 const struct got_error *err = NULL;
1198 char *uuidstr = NULL;
1199 uint32_t uuid_status;
1201 *refname = NULL;
1203 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1204 if (uuid_status != uuid_s_ok)
1205 return got_error_uuid(uuid_status);
1207 if (asprintf(refname, "%s-%s", GOT_WORKTREE_BASE_REF_PREFIX, uuidstr)
1208 == -1) {
1209 err = got_error_from_errno();
1210 *refname = NULL;
1212 free(uuidstr);
1213 return err;
1217 * Prevent Git's garbage collector from deleting our base commit by
1218 * setting a reference to our base commit's ID.
1220 static const struct got_error *
1221 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1223 const struct got_error *err = NULL;
1224 struct got_reference *ref = NULL;
1225 char *refname;
1227 err = got_worktree_get_base_ref_name(&refname, worktree);
1228 if (err)
1229 return err;
1231 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1232 if (err)
1233 goto done;
1235 err = got_ref_write(ref, repo);
1236 done:
1237 free(refname);
1238 if (ref)
1239 got_ref_close(ref);
1240 return err;
1244 const struct got_error *
1245 got_worktree_checkout_files(struct got_worktree *worktree,
1246 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1247 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1249 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1250 struct got_commit_object *commit = NULL;
1251 struct got_object_id *tree_id = NULL;
1252 struct got_tree_object *tree = NULL;
1253 char *fileindex_path = NULL, *new_fileindex_path = NULL;
1254 struct got_fileindex *fileindex = NULL;
1255 FILE *index = NULL, *new_index = NULL;
1256 struct got_fileindex_diff_tree_cb diff_cb;
1257 struct diff_cb_arg arg;
1259 err = lock_worktree(worktree, LOCK_EX);
1260 if (err)
1261 return err;
1263 fileindex = got_fileindex_alloc();
1264 if (fileindex == NULL) {
1265 err = got_error_from_errno();
1266 goto done;
1269 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1270 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1271 err = got_error_from_errno();
1272 fileindex_path = NULL;
1273 goto done;
1277 * Read the file index.
1278 * Checking out files is supposed to be an idempotent operation.
1279 * If the on-disk file index is incomplete we will try to complete it.
1281 index = fopen(fileindex_path, "rb");
1282 if (index == NULL) {
1283 if (errno != ENOENT) {
1284 err = got_error_from_errno();
1285 goto done;
1287 } else {
1288 err = got_fileindex_read(fileindex, index);
1289 fclose(index);
1290 if (err)
1291 goto done;
1294 err = got_opentemp_named(&new_fileindex_path, &new_index,
1295 fileindex_path);
1296 if (err)
1297 goto done;
1299 err = ref_base_commit(worktree, repo);
1300 if (err)
1301 goto done;
1303 err = got_object_open_as_commit(&commit, repo,
1304 worktree->base_commit_id);
1305 if (err)
1306 goto done;
1308 err = got_object_id_by_path(&tree_id, repo,
1309 worktree->base_commit_id, worktree->path_prefix);
1310 if (err)
1311 goto done;
1313 err = got_object_open_as_tree(&tree, repo, tree_id);
1314 if (err)
1315 goto done;
1317 diff_cb.diff_old_new = diff_old_new;
1318 diff_cb.diff_old = diff_old;
1319 diff_cb.diff_new = diff_new;
1320 arg.fileindex = fileindex;
1321 arg.worktree = worktree;
1322 arg.repo = repo;
1323 arg.progress_cb = progress_cb;
1324 arg.progress_arg = progress_arg;
1325 arg.cancel_cb = cancel_cb;
1326 arg.cancel_arg = cancel_arg;
1327 checkout_err = got_fileindex_diff_tree(fileindex, tree, repo,
1328 &diff_cb, &arg);
1330 /* Try to sync the fileindex back to disk in any case. */
1331 err = got_fileindex_write(fileindex, new_index);
1332 if (err)
1333 goto done;
1335 if (rename(new_fileindex_path, fileindex_path) != 0) {
1336 err = got_error_from_errno();
1337 unlink(new_fileindex_path);
1338 goto done;
1341 free(new_fileindex_path);
1342 new_fileindex_path = NULL;
1344 done:
1345 if (tree)
1346 got_object_tree_close(tree);
1347 if (commit)
1348 got_object_commit_close(commit);
1349 if (new_fileindex_path)
1350 unlink(new_fileindex_path);
1351 if (new_index)
1352 fclose(new_index);
1353 free(new_fileindex_path);
1354 free(fileindex_path);
1355 got_fileindex_free(fileindex);
1356 if (checkout_err)
1357 err = checkout_err;
1358 unlockerr = lock_worktree(worktree, LOCK_SH);
1359 if (unlockerr && err == NULL)
1360 err = unlockerr;
1361 return err;
1364 struct diff_dir_cb_arg {
1365 struct got_fileindex *fileindex;
1366 struct got_worktree *worktree;
1367 const char *status_path;
1368 size_t status_path_len;
1369 struct got_repository *repo;
1370 got_worktree_status_cb status_cb;
1371 void *status_arg;
1372 got_worktree_cancel_cb cancel_cb;
1373 void *cancel_arg;
1376 static const struct got_error *
1377 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1378 got_worktree_status_cb status_cb, void *status_arg,
1379 struct got_repository *repo)
1381 const struct got_error *err = NULL;
1382 unsigned char status = GOT_STATUS_NO_CHANGE;
1383 struct stat sb;
1384 struct got_object_id id;
1386 err = get_file_status(&status, &sb, ie, abspath, repo);
1387 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1388 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1389 err = (*status_cb)(status_arg, status, ie->path, &id);
1391 return err;
1394 static const struct got_error *
1395 status_old_new(void *arg, struct got_fileindex_entry *ie,
1396 struct dirent *de, const char *parent_path)
1398 const struct got_error *err = NULL;
1399 struct diff_dir_cb_arg *a = arg;
1400 char *abspath;
1402 if (got_path_cmp(parent_path, a->status_path) != 0 &&
1403 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1404 return NULL;
1406 if (parent_path[0]) {
1407 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1408 parent_path, de->d_name) == -1)
1409 return got_error_from_errno();
1410 } else {
1411 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1412 de->d_name) == -1)
1413 return got_error_from_errno();
1416 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1417 a->repo);
1418 free(abspath);
1419 return err;
1422 static const struct got_error *
1423 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1425 struct diff_dir_cb_arg *a = arg;
1426 struct got_object_id id;
1428 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1429 return NULL;
1431 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1432 return (*a->status_cb)(a->status_arg, GOT_STATUS_MISSING, ie->path,
1433 &id);
1436 static const struct got_error *
1437 status_new(void *arg, struct dirent *de, const char *parent_path)
1439 const struct got_error *err = NULL;
1440 struct diff_dir_cb_arg *a = arg;
1441 char *path = NULL;
1443 if (de->d_type == DT_DIR)
1444 return NULL;
1446 /* XXX ignore symlinks for now */
1447 if (de->d_type == DT_LNK)
1448 return NULL;
1450 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1451 return NULL;
1453 if (parent_path[0]) {
1454 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1455 return got_error_from_errno();
1456 } else {
1457 path = de->d_name;
1460 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1461 NULL);
1462 if (parent_path[0])
1463 free(path);
1464 return err;
1467 const struct got_error *
1468 got_worktree_status(struct got_worktree *worktree, const char *path,
1469 struct got_repository *repo, got_worktree_status_cb status_cb,
1470 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1472 const struct got_error *err = NULL;
1473 DIR *workdir = NULL;
1474 char *fileindex_path = NULL;
1475 struct got_fileindex *fileindex = NULL;
1476 FILE *index = NULL;
1477 struct got_fileindex_diff_dir_cb fdiff_cb;
1478 struct diff_dir_cb_arg arg;
1479 char *ondisk_path = NULL;
1481 fileindex = got_fileindex_alloc();
1482 if (fileindex == NULL) {
1483 err = got_error_from_errno();
1484 goto done;
1487 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1488 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1489 err = got_error_from_errno();
1490 fileindex_path = NULL;
1491 goto done;
1494 index = fopen(fileindex_path, "rb");
1495 if (index == NULL) {
1496 if (errno != ENOENT) {
1497 err = got_error_from_errno();
1498 goto done;
1500 } else {
1501 err = got_fileindex_read(fileindex, index);
1502 fclose(index);
1503 if (err)
1504 goto done;
1507 if (asprintf(&ondisk_path, "%s%s%s",
1508 worktree->root_path, path[0] ? "/" : "", path) == -1) {
1509 err = got_error_from_errno();
1510 goto done;
1512 workdir = opendir(ondisk_path);
1513 if (workdir == NULL) {
1514 if (errno == ENOTDIR) {
1515 struct got_fileindex_entry *ie;
1516 ie = got_fileindex_entry_get(fileindex, path);
1517 if (ie == NULL) {
1518 err = got_error(GOT_ERR_BAD_PATH);
1519 goto done;
1521 err = report_file_status(ie, ondisk_path,
1522 status_cb, status_arg, repo);
1523 goto done;
1524 } else {
1525 err = got_error_from_errno();
1526 goto done;
1529 fdiff_cb.diff_old_new = status_old_new;
1530 fdiff_cb.diff_old = status_old;
1531 fdiff_cb.diff_new = status_new;
1532 arg.fileindex = fileindex;
1533 arg.worktree = worktree;
1534 arg.status_path = path;
1535 arg.status_path_len = strlen(path);
1536 arg.repo = repo;
1537 arg.status_cb = status_cb;
1538 arg.status_arg = status_arg;
1539 arg.cancel_cb = cancel_cb;
1540 arg.cancel_arg = cancel_arg;
1541 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1542 path, repo, &fdiff_cb, &arg);
1543 done:
1544 if (workdir)
1545 closedir(workdir);
1546 free(ondisk_path);
1547 free(fileindex_path);
1548 got_fileindex_free(fileindex);
1549 return err;
1552 const struct got_error *
1553 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
1554 const char *arg)
1556 const struct got_error *err = NULL;
1557 char *resolved, *path = NULL;
1558 size_t len;
1560 *wt_path = NULL;
1562 resolved = realpath(arg, NULL);
1563 if (resolved == NULL)
1564 return got_error_from_errno();
1566 if (strncmp(got_worktree_get_root_path(worktree), resolved,
1567 strlen(got_worktree_get_root_path(worktree)))) {
1568 err = got_error(GOT_ERR_BAD_PATH);
1569 goto done;
1572 path = strdup(resolved + strlen(got_worktree_get_root_path(worktree)));
1573 if (path == NULL) {
1574 err = got_error_from_errno();
1575 goto done;
1578 /* XXX status walk can't deal with trailing slash! */
1579 len = strlen(path);
1580 while (path[len - 1] == '/') {
1581 path[len - 1] = '\0';
1582 len--;
1584 done:
1585 free(resolved);
1586 if (err == NULL)
1587 *wt_path = path;
1588 else
1589 free(path);
1590 return err;
1593 const struct got_error *
1594 got_worktree_schedule_add(char **relpath, struct got_worktree *worktree,
1595 const char *ondisk_path)
1597 struct got_fileindex *fileindex = NULL;
1598 struct got_fileindex_entry *ie = NULL;
1599 char *fileindex_path = NULL, *new_fileindex_path = NULL;
1600 FILE *index = NULL, *new_index = NULL;
1601 const struct got_error *err = NULL, *unlockerr = NULL;
1603 *relpath = NULL;
1605 err = lock_worktree(worktree, LOCK_EX);
1606 if (err)
1607 return err;
1609 err = got_path_skip_common_ancestor(relpath,
1610 got_worktree_get_root_path(worktree), ondisk_path);
1611 if (err)
1612 goto done;
1614 err = got_fileindex_entry_alloc(&ie, ondisk_path, *relpath, NULL, NULL);
1615 if (err)
1616 goto done;
1618 fileindex = got_fileindex_alloc();
1619 if (fileindex == NULL) {
1620 err = got_error_from_errno();
1621 goto done;
1624 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1625 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1626 err = got_error_from_errno();
1627 fileindex_path = NULL;
1628 goto done;
1631 index = fopen(fileindex_path, "rb");
1632 if (index == NULL) {
1633 err = got_error_from_errno();
1634 goto done;
1637 err = got_fileindex_read(fileindex, index);
1638 if (err)
1639 goto done;
1641 err = got_fileindex_entry_add(fileindex, ie);
1642 if (err)
1643 goto done;
1644 ie = NULL; /* now owned by fileindex; don't free separately */
1646 err = got_opentemp_named(&new_fileindex_path, &new_index,
1647 fileindex_path);
1648 if (err)
1649 goto done;
1651 err = got_fileindex_write(fileindex, new_index);
1652 if (err)
1653 goto done;
1655 if (rename(new_fileindex_path, fileindex_path) != 0) {
1656 err = got_error_from_errno();
1657 goto done;
1660 free(new_fileindex_path);
1661 new_fileindex_path = NULL;
1662 done:
1663 if (index) {
1664 if (fclose(index) != 0 && err == NULL)
1665 err = got_error_from_errno();
1667 if (new_fileindex_path) {
1668 if (unlink(new_fileindex_path) != 0 && err == NULL)
1669 err = got_error_from_errno();
1670 free(new_fileindex_path);
1672 if (ie)
1673 got_fileindex_entry_free(ie);
1674 if (fileindex)
1675 got_fileindex_free(fileindex);
1676 unlockerr = lock_worktree(worktree, LOCK_SH);
1677 if (unlockerr && err == NULL)
1678 err = unlockerr;
1679 if (err) {
1680 free(*relpath);
1681 *relpath = NULL;
1683 return err;