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 (ie->ctime_sec == sb->st_ctime &&
990 ie->ctime_nsec == sb->st_ctimensec &&
991 ie->mtime_sec == sb->st_mtime &&
992 ie->mtime_sec == sb->st_mtime &&
993 ie->mtime_nsec == sb->st_mtimensec &&
994 ie->size == (sb->st_size & 0xffffffff))
995 return NULL;
997 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
998 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
999 if (err)
1000 return err;
1002 f = fopen(abspath, "r");
1003 if (f == NULL) {
1004 err = got_error_from_errno();
1005 goto done;
1007 hdrlen = got_object_blob_get_hdrlen(blob);
1008 while (1) {
1009 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1010 err = got_object_blob_read_block(&blen, blob);
1011 if (err)
1012 break;
1013 /* Skip length of blob object header first time around. */
1014 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1015 if (flen == 0 && ferror(f)) {
1016 err = got_error_from_errno();
1017 break;
1019 if (blen == 0) {
1020 if (flen != 0)
1021 *status = GOT_STATUS_MODIFY;
1022 break;
1023 } else if (flen == 0) {
1024 if (blen != 0)
1025 *status = GOT_STATUS_MODIFY;
1026 break;
1027 } else if (blen - hdrlen == flen) {
1028 /* Skip blob object header first time around. */
1029 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1030 *status = GOT_STATUS_MODIFY;
1031 break;
1033 } else {
1034 *status = GOT_STATUS_MODIFY;
1035 break;
1037 hdrlen = 0;
1039 done:
1040 if (blob)
1041 got_object_blob_close(blob);
1042 if (f)
1043 fclose(f);
1044 return err;
1047 static const struct got_error *
1048 update_blob(struct got_worktree *worktree,
1049 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1050 struct got_tree_entry *te, const char *path,
1051 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1052 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1054 const struct got_error *err = NULL;
1055 struct got_blob_object *blob = NULL;
1056 char *ondisk_path;
1057 unsigned char status = GOT_STATUS_NO_CHANGE;
1058 struct stat sb;
1060 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1061 return got_error_from_errno();
1063 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1064 if (err)
1065 goto done;
1067 if (status == GOT_STATUS_OBSTRUCTED) {
1068 (*progress_cb)(progress_arg, status, path);
1069 goto done;
1072 if (ie && status != GOT_STATUS_MISSING) {
1073 if (memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1074 SHA1_DIGEST_LENGTH) == 0) {
1075 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1076 path);
1077 goto done;
1079 if (memcmp(ie->blob_sha1,
1080 te->id->sha1, SHA1_DIGEST_LENGTH) == 0)
1081 goto done;
1084 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1085 if (err)
1086 goto done;
1088 if (status == GOT_STATUS_MODIFY)
1089 err = merge_blob(worktree, fileindex, ie, ondisk_path, path,
1090 te->mode, sb.st_mode, blob, repo, progress_cb,
1091 progress_arg);
1092 else
1093 err = install_blob(worktree, fileindex, ie, ondisk_path, path,
1094 te->mode, sb.st_mode, blob, status == GOT_STATUS_MISSING,
1095 repo, progress_cb, progress_arg);
1097 got_object_blob_close(blob);
1098 done:
1099 free(ondisk_path);
1100 return err;
1103 static const struct got_error *
1104 remove_ondisk_file(const char *root_path, const char *path)
1106 const struct got_error *err = NULL;
1107 char *ondisk_path = NULL;
1109 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1110 return got_error_from_errno();
1112 if (unlink(ondisk_path) == -1) {
1113 if (errno != ENOENT)
1114 err = got_error_from_errno();
1115 } else {
1116 char *parent = dirname(ondisk_path);
1117 while (parent && strcmp(parent, root_path) != 0) {
1118 if (rmdir(parent) == -1) {
1119 if (errno != ENOTEMPTY)
1120 err = got_error_from_errno();
1121 break;
1123 parent = dirname(parent);
1126 free(ondisk_path);
1127 return err;
1130 struct diff_cb_arg {
1131 struct got_fileindex *fileindex;
1132 struct got_worktree *worktree;
1133 struct got_repository *repo;
1134 got_worktree_checkout_cb progress_cb;
1135 void *progress_arg;
1136 got_worktree_cancel_cb cancel_cb;
1137 void *cancel_arg;
1140 static const struct got_error *
1141 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1142 struct got_tree_entry *te, const char *parent_path)
1144 struct diff_cb_arg *a = arg;
1146 return update_blob(a->worktree, a->fileindex, ie, te,
1147 ie->path, a->repo, a->progress_cb, a->progress_arg,
1148 a->cancel_cb, a->cancel_arg);
1151 static const struct got_error *
1152 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1154 const struct got_error *err;
1155 struct diff_cb_arg *a = arg;
1157 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE, ie->path);
1159 err = remove_ondisk_file(a->worktree->root_path, ie->path);
1160 if (err)
1161 return err;
1162 got_fileindex_entry_remove(a->fileindex, ie);
1163 return NULL;
1166 static const struct got_error *
1167 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1169 struct diff_cb_arg *a = arg;
1170 const struct got_error *err;
1171 char *path;
1173 if (asprintf(&path, "%s%s%s", parent_path,
1174 parent_path[0] ? "/" : "", te->name)
1175 == -1)
1176 return got_error_from_errno();
1178 if (S_ISDIR(te->mode))
1179 err = add_dir_on_disk(a->worktree, path);
1180 else
1181 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1182 a->repo, a->progress_cb, a->progress_arg,
1183 a->cancel_cb, a->cancel_arg);
1185 free(path);
1186 return err;
1189 const struct got_error *
1190 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1192 const struct got_error *err = NULL;
1193 char *uuidstr = NULL;
1194 uint32_t uuid_status;
1196 *refname = NULL;
1198 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1199 if (uuid_status != uuid_s_ok)
1200 return got_error_uuid(uuid_status);
1202 if (asprintf(refname, "%s-%s", GOT_WORKTREE_BASE_REF_PREFIX, uuidstr)
1203 == -1) {
1204 err = got_error_from_errno();
1205 *refname = NULL;
1207 free(uuidstr);
1208 return err;
1212 * Prevent Git's garbage collector from deleting our base commit by
1213 * setting a reference to our base commit's ID.
1215 static const struct got_error *
1216 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1218 const struct got_error *err = NULL;
1219 struct got_reference *ref = NULL;
1220 char *refname;
1222 err = got_worktree_get_base_ref_name(&refname, worktree);
1223 if (err)
1224 return err;
1226 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1227 if (err)
1228 goto done;
1230 err = got_ref_write(ref, repo);
1231 done:
1232 free(refname);
1233 if (ref)
1234 got_ref_close(ref);
1235 return err;
1239 const struct got_error *
1240 got_worktree_checkout_files(struct got_worktree *worktree,
1241 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1242 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1244 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1245 struct got_commit_object *commit = NULL;
1246 struct got_object_id *tree_id = NULL;
1247 struct got_tree_object *tree = NULL;
1248 char *fileindex_path = NULL, *new_fileindex_path = NULL;
1249 struct got_fileindex *fileindex = NULL;
1250 FILE *index = NULL, *new_index = NULL;
1251 struct got_fileindex_diff_tree_cb diff_cb;
1252 struct diff_cb_arg arg;
1254 err = lock_worktree(worktree, LOCK_EX);
1255 if (err)
1256 return err;
1258 fileindex = got_fileindex_alloc();
1259 if (fileindex == NULL) {
1260 err = got_error_from_errno();
1261 goto done;
1264 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1265 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1266 err = got_error_from_errno();
1267 fileindex_path = NULL;
1268 goto done;
1272 * Read the file index.
1273 * Checking out files is supposed to be an idempotent operation.
1274 * If the on-disk file index is incomplete we will try to complete it.
1276 index = fopen(fileindex_path, "rb");
1277 if (index == NULL) {
1278 if (errno != ENOENT) {
1279 err = got_error_from_errno();
1280 goto done;
1282 } else {
1283 err = got_fileindex_read(fileindex, index);
1284 fclose(index);
1285 if (err)
1286 goto done;
1289 err = got_opentemp_named(&new_fileindex_path, &new_index,
1290 fileindex_path);
1291 if (err)
1292 goto done;
1294 err = ref_base_commit(worktree, repo);
1295 if (err)
1296 goto done;
1298 err = got_object_open_as_commit(&commit, repo,
1299 worktree->base_commit_id);
1300 if (err)
1301 goto done;
1303 err = got_object_id_by_path(&tree_id, repo,
1304 worktree->base_commit_id, worktree->path_prefix);
1305 if (err)
1306 goto done;
1308 err = got_object_open_as_tree(&tree, repo, tree_id);
1309 if (err)
1310 goto done;
1312 diff_cb.diff_old_new = diff_old_new;
1313 diff_cb.diff_old = diff_old;
1314 diff_cb.diff_new = diff_new;
1315 arg.fileindex = fileindex;
1316 arg.worktree = worktree;
1317 arg.repo = repo;
1318 arg.progress_cb = progress_cb;
1319 arg.progress_arg = progress_arg;
1320 arg.cancel_cb = cancel_cb;
1321 arg.cancel_arg = cancel_arg;
1322 checkout_err = got_fileindex_diff_tree(fileindex, tree, repo,
1323 &diff_cb, &arg);
1325 /* Try to sync the fileindex back to disk in any case. */
1326 err = got_fileindex_write(fileindex, new_index);
1327 if (err)
1328 goto done;
1330 if (rename(new_fileindex_path, fileindex_path) != 0) {
1331 err = got_error_from_errno();
1332 unlink(new_fileindex_path);
1333 goto done;
1336 free(new_fileindex_path);
1337 new_fileindex_path = NULL;
1339 done:
1340 if (tree)
1341 got_object_tree_close(tree);
1342 if (commit)
1343 got_object_commit_close(commit);
1344 if (new_fileindex_path)
1345 unlink(new_fileindex_path);
1346 if (new_index)
1347 fclose(new_index);
1348 free(new_fileindex_path);
1349 free(fileindex_path);
1350 got_fileindex_free(fileindex);
1351 if (checkout_err)
1352 err = checkout_err;
1353 unlockerr = lock_worktree(worktree, LOCK_SH);
1354 if (unlockerr && err == NULL)
1355 err = unlockerr;
1356 return err;
1359 struct diff_dir_cb_arg {
1360 struct got_fileindex *fileindex;
1361 struct got_worktree *worktree;
1362 const char *status_path;
1363 size_t status_path_len;
1364 struct got_repository *repo;
1365 got_worktree_status_cb status_cb;
1366 void *status_arg;
1367 got_worktree_cancel_cb cancel_cb;
1368 void *cancel_arg;
1371 static const struct got_error *
1372 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1373 got_worktree_status_cb status_cb, void *status_arg,
1374 struct got_repository *repo)
1376 const struct got_error *err = NULL;
1377 unsigned char status = GOT_STATUS_NO_CHANGE;
1378 struct stat sb;
1379 struct got_object_id id;
1381 err = get_file_status(&status, &sb, ie, abspath, repo);
1382 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1383 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1384 err = (*status_cb)(status_arg, status, ie->path, &id);
1386 return err;
1389 static const struct got_error *
1390 status_old_new(void *arg, struct got_fileindex_entry *ie,
1391 struct dirent *de, const char *parent_path)
1393 const struct got_error *err = NULL;
1394 struct diff_dir_cb_arg *a = arg;
1395 char *abspath;
1397 if (got_path_cmp(parent_path, a->status_path) != 0 &&
1398 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1399 return NULL;
1401 if (parent_path[0]) {
1402 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1403 parent_path, de->d_name) == -1)
1404 return got_error_from_errno();
1405 } else {
1406 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1407 de->d_name) == -1)
1408 return got_error_from_errno();
1411 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1412 a->repo);
1413 free(abspath);
1414 return err;
1417 static const struct got_error *
1418 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1420 struct diff_dir_cb_arg *a = arg;
1421 struct got_object_id id;
1423 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1424 return NULL;
1426 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1427 return (*a->status_cb)(a->status_arg, GOT_STATUS_MISSING, ie->path,
1428 &id);
1431 static const struct got_error *
1432 status_new(void *arg, struct dirent *de, const char *parent_path)
1434 const struct got_error *err = NULL;
1435 struct diff_dir_cb_arg *a = arg;
1436 char *path = NULL;
1438 if (de->d_type == DT_DIR)
1439 return NULL;
1441 /* XXX ignore symlinks for now */
1442 if (de->d_type == DT_LNK)
1443 return NULL;
1445 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1446 return NULL;
1448 if (parent_path[0]) {
1449 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1450 return got_error_from_errno();
1451 } else {
1452 path = de->d_name;
1455 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1456 NULL);
1457 if (parent_path[0])
1458 free(path);
1459 return err;
1462 const struct got_error *
1463 got_worktree_status(struct got_worktree *worktree, const char *path,
1464 struct got_repository *repo, got_worktree_status_cb status_cb,
1465 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1467 const struct got_error *err = NULL;
1468 DIR *workdir = NULL;
1469 char *fileindex_path = NULL;
1470 struct got_fileindex *fileindex = NULL;
1471 FILE *index = NULL;
1472 struct got_fileindex_diff_dir_cb fdiff_cb;
1473 struct diff_dir_cb_arg arg;
1474 char *ondisk_path = NULL;
1476 fileindex = got_fileindex_alloc();
1477 if (fileindex == NULL) {
1478 err = got_error_from_errno();
1479 goto done;
1482 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1483 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1484 err = got_error_from_errno();
1485 fileindex_path = NULL;
1486 goto done;
1489 index = fopen(fileindex_path, "rb");
1490 if (index == NULL) {
1491 if (errno != ENOENT) {
1492 err = got_error_from_errno();
1493 goto done;
1495 } else {
1496 err = got_fileindex_read(fileindex, index);
1497 fclose(index);
1498 if (err)
1499 goto done;
1502 if (asprintf(&ondisk_path, "%s%s%s",
1503 worktree->root_path, path[0] ? "/" : "", path) == -1) {
1504 err = got_error_from_errno();
1505 goto done;
1507 workdir = opendir(ondisk_path);
1508 if (workdir == NULL) {
1509 if (errno == ENOTDIR) {
1510 struct got_fileindex_entry *ie;
1511 ie = got_fileindex_entry_get(fileindex, path);
1512 if (ie == NULL) {
1513 err = got_error(GOT_ERR_BAD_PATH);
1514 goto done;
1516 err = report_file_status(ie, ondisk_path,
1517 status_cb, status_arg, repo);
1518 goto done;
1519 } else {
1520 err = got_error_from_errno();
1521 goto done;
1524 fdiff_cb.diff_old_new = status_old_new;
1525 fdiff_cb.diff_old = status_old;
1526 fdiff_cb.diff_new = status_new;
1527 arg.fileindex = fileindex;
1528 arg.worktree = worktree;
1529 arg.status_path = path;
1530 arg.status_path_len = strlen(path);
1531 arg.repo = repo;
1532 arg.status_cb = status_cb;
1533 arg.status_arg = status_arg;
1534 arg.cancel_cb = cancel_cb;
1535 arg.cancel_arg = cancel_arg;
1536 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1537 path, repo, &fdiff_cb, &arg);
1538 done:
1539 if (workdir)
1540 closedir(workdir);
1541 free(ondisk_path);
1542 free(fileindex_path);
1543 got_fileindex_free(fileindex);
1544 return err;
1547 const struct got_error *
1548 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
1549 const char *arg)
1551 const struct got_error *err = NULL;
1552 char *resolved, *path = NULL;
1553 size_t len;
1555 *wt_path = NULL;
1557 resolved = realpath(arg, NULL);
1558 if (resolved == NULL)
1559 return got_error_from_errno();
1561 if (strncmp(got_worktree_get_root_path(worktree), resolved,
1562 strlen(got_worktree_get_root_path(worktree)))) {
1563 err = got_error(GOT_ERR_BAD_PATH);
1564 goto done;
1567 path = strdup(resolved + strlen(got_worktree_get_root_path(worktree)));
1568 if (path == NULL) {
1569 err = got_error_from_errno();
1570 goto done;
1573 /* XXX status walk can't deal with trailing slash! */
1574 len = strlen(path);
1575 while (path[len - 1] == '/') {
1576 path[len - 1] = '\0';
1577 len--;
1579 done:
1580 free(resolved);
1581 if (err == NULL)
1582 *wt_path = path;
1583 else
1584 free(path);
1585 return err;