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 if (got_fileindex_entry_has_file_on_disk(ie))
971 *status = GOT_STATUS_MISSING;
972 else
973 *status = GOT_STATUS_DELETE;
974 sb->st_mode =
975 ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
976 & (S_IRWXU | S_IRWXG | S_IRWXO));
977 } else
978 sb->st_mode = GOT_DEFAULT_FILE_MODE;
979 return NULL;
981 return got_error_from_errno();
984 if (!S_ISREG(sb->st_mode)) {
985 *status = GOT_STATUS_OBSTRUCTED;
986 return NULL;
989 if (ie == NULL)
990 return NULL;
992 if (!got_fileindex_entry_has_file_on_disk(ie)) {
993 *status = GOT_STATUS_DELETE;
994 return NULL;
995 } else if (!got_fileindex_entry_has_blob(ie)) {
996 *status = GOT_STATUS_ADD;
997 return NULL;
1000 if (ie->ctime_sec == sb->st_ctime &&
1001 ie->ctime_nsec == sb->st_ctimensec &&
1002 ie->mtime_sec == sb->st_mtime &&
1003 ie->mtime_sec == sb->st_mtime &&
1004 ie->mtime_nsec == sb->st_mtimensec &&
1005 ie->size == (sb->st_size & 0xffffffff))
1006 return NULL;
1008 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1009 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1010 if (err)
1011 return err;
1013 f = fopen(abspath, "r");
1014 if (f == NULL) {
1015 err = got_error_from_errno();
1016 goto done;
1018 hdrlen = got_object_blob_get_hdrlen(blob);
1019 while (1) {
1020 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1021 err = got_object_blob_read_block(&blen, blob);
1022 if (err)
1023 break;
1024 /* Skip length of blob object header first time around. */
1025 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1026 if (flen == 0 && ferror(f)) {
1027 err = got_error_from_errno();
1028 break;
1030 if (blen == 0) {
1031 if (flen != 0)
1032 *status = GOT_STATUS_MODIFY;
1033 break;
1034 } else if (flen == 0) {
1035 if (blen != 0)
1036 *status = GOT_STATUS_MODIFY;
1037 break;
1038 } else if (blen - hdrlen == flen) {
1039 /* Skip blob object header first time around. */
1040 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1041 *status = GOT_STATUS_MODIFY;
1042 break;
1044 } else {
1045 *status = GOT_STATUS_MODIFY;
1046 break;
1048 hdrlen = 0;
1050 done:
1051 if (blob)
1052 got_object_blob_close(blob);
1053 if (f)
1054 fclose(f);
1055 return err;
1058 static const struct got_error *
1059 update_blob(struct got_worktree *worktree,
1060 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1061 struct got_tree_entry *te, const char *path,
1062 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1063 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1065 const struct got_error *err = NULL;
1066 struct got_blob_object *blob = NULL;
1067 char *ondisk_path;
1068 unsigned char status = GOT_STATUS_NO_CHANGE;
1069 struct stat sb;
1071 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1072 return got_error_from_errno();
1074 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1075 if (err)
1076 goto done;
1078 if (status == GOT_STATUS_OBSTRUCTED) {
1079 (*progress_cb)(progress_arg, status, path);
1080 goto done;
1083 if (ie && status != GOT_STATUS_MISSING) {
1084 if (memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1085 SHA1_DIGEST_LENGTH) == 0) {
1086 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1087 path);
1088 goto done;
1090 if (memcmp(ie->blob_sha1,
1091 te->id->sha1, SHA1_DIGEST_LENGTH) == 0)
1092 goto done;
1095 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1096 if (err)
1097 goto done;
1099 if (status == GOT_STATUS_MODIFY)
1100 err = merge_blob(worktree, fileindex, ie, ondisk_path, path,
1101 te->mode, sb.st_mode, blob, repo, progress_cb,
1102 progress_arg);
1103 else
1104 err = install_blob(worktree, fileindex, ie, ondisk_path, path,
1105 te->mode, sb.st_mode, blob, status == GOT_STATUS_MISSING,
1106 repo, progress_cb, progress_arg);
1108 got_object_blob_close(blob);
1109 done:
1110 free(ondisk_path);
1111 return err;
1114 static const struct got_error *
1115 remove_ondisk_file(const char *root_path, const char *path)
1117 const struct got_error *err = NULL;
1118 char *ondisk_path = NULL;
1120 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1121 return got_error_from_errno();
1123 if (unlink(ondisk_path) == -1) {
1124 if (errno != ENOENT)
1125 err = got_error_from_errno();
1126 } else {
1127 char *parent = dirname(ondisk_path);
1128 while (parent && strcmp(parent, root_path) != 0) {
1129 if (rmdir(parent) == -1) {
1130 if (errno != ENOTEMPTY)
1131 err = got_error_from_errno();
1132 break;
1134 parent = dirname(parent);
1137 free(ondisk_path);
1138 return err;
1141 struct diff_cb_arg {
1142 struct got_fileindex *fileindex;
1143 struct got_worktree *worktree;
1144 struct got_repository *repo;
1145 got_worktree_checkout_cb progress_cb;
1146 void *progress_arg;
1147 got_worktree_cancel_cb cancel_cb;
1148 void *cancel_arg;
1151 static const struct got_error *
1152 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1153 struct got_tree_entry *te, const char *parent_path)
1155 struct diff_cb_arg *a = arg;
1157 return update_blob(a->worktree, a->fileindex, ie, te,
1158 ie->path, a->repo, a->progress_cb, a->progress_arg,
1159 a->cancel_cb, a->cancel_arg);
1162 static const struct got_error *
1163 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1165 const struct got_error *err;
1166 struct diff_cb_arg *a = arg;
1168 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE, ie->path);
1170 err = remove_ondisk_file(a->worktree->root_path, ie->path);
1171 if (err)
1172 return err;
1173 got_fileindex_entry_remove(a->fileindex, ie);
1174 return NULL;
1177 static const struct got_error *
1178 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1180 struct diff_cb_arg *a = arg;
1181 const struct got_error *err;
1182 char *path;
1184 if (asprintf(&path, "%s%s%s", parent_path,
1185 parent_path[0] ? "/" : "", te->name)
1186 == -1)
1187 return got_error_from_errno();
1189 if (S_ISDIR(te->mode))
1190 err = add_dir_on_disk(a->worktree, path);
1191 else
1192 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1193 a->repo, a->progress_cb, a->progress_arg,
1194 a->cancel_cb, a->cancel_arg);
1196 free(path);
1197 return err;
1200 const struct got_error *
1201 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1203 const struct got_error *err = NULL;
1204 char *uuidstr = NULL;
1205 uint32_t uuid_status;
1207 *refname = NULL;
1209 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1210 if (uuid_status != uuid_s_ok)
1211 return got_error_uuid(uuid_status);
1213 if (asprintf(refname, "%s-%s", GOT_WORKTREE_BASE_REF_PREFIX, uuidstr)
1214 == -1) {
1215 err = got_error_from_errno();
1216 *refname = NULL;
1218 free(uuidstr);
1219 return err;
1223 * Prevent Git's garbage collector from deleting our base commit by
1224 * setting a reference to our base commit's ID.
1226 static const struct got_error *
1227 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1229 const struct got_error *err = NULL;
1230 struct got_reference *ref = NULL;
1231 char *refname;
1233 err = got_worktree_get_base_ref_name(&refname, worktree);
1234 if (err)
1235 return err;
1237 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1238 if (err)
1239 goto done;
1241 err = got_ref_write(ref, repo);
1242 done:
1243 free(refname);
1244 if (ref)
1245 got_ref_close(ref);
1246 return err;
1250 const struct got_error *
1251 got_worktree_checkout_files(struct got_worktree *worktree,
1252 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1253 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1255 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1256 struct got_commit_object *commit = NULL;
1257 struct got_object_id *tree_id = NULL;
1258 struct got_tree_object *tree = NULL;
1259 char *fileindex_path = NULL, *new_fileindex_path = NULL;
1260 struct got_fileindex *fileindex = NULL;
1261 FILE *index = NULL, *new_index = NULL;
1262 struct got_fileindex_diff_tree_cb diff_cb;
1263 struct diff_cb_arg arg;
1265 err = lock_worktree(worktree, LOCK_EX);
1266 if (err)
1267 return err;
1269 fileindex = got_fileindex_alloc();
1270 if (fileindex == NULL) {
1271 err = got_error_from_errno();
1272 goto done;
1275 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1276 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1277 err = got_error_from_errno();
1278 fileindex_path = NULL;
1279 goto done;
1283 * Read the file index.
1284 * Checking out files is supposed to be an idempotent operation.
1285 * If the on-disk file index is incomplete we will try to complete it.
1287 index = fopen(fileindex_path, "rb");
1288 if (index == NULL) {
1289 if (errno != ENOENT) {
1290 err = got_error_from_errno();
1291 goto done;
1293 } else {
1294 err = got_fileindex_read(fileindex, index);
1295 fclose(index);
1296 if (err)
1297 goto done;
1300 err = got_opentemp_named(&new_fileindex_path, &new_index,
1301 fileindex_path);
1302 if (err)
1303 goto done;
1305 err = ref_base_commit(worktree, repo);
1306 if (err)
1307 goto done;
1309 err = got_object_open_as_commit(&commit, repo,
1310 worktree->base_commit_id);
1311 if (err)
1312 goto done;
1314 err = got_object_id_by_path(&tree_id, repo,
1315 worktree->base_commit_id, worktree->path_prefix);
1316 if (err)
1317 goto done;
1319 err = got_object_open_as_tree(&tree, repo, tree_id);
1320 if (err)
1321 goto done;
1323 diff_cb.diff_old_new = diff_old_new;
1324 diff_cb.diff_old = diff_old;
1325 diff_cb.diff_new = diff_new;
1326 arg.fileindex = fileindex;
1327 arg.worktree = worktree;
1328 arg.repo = repo;
1329 arg.progress_cb = progress_cb;
1330 arg.progress_arg = progress_arg;
1331 arg.cancel_cb = cancel_cb;
1332 arg.cancel_arg = cancel_arg;
1333 checkout_err = got_fileindex_diff_tree(fileindex, tree, repo,
1334 &diff_cb, &arg);
1336 /* Try to sync the fileindex back to disk in any case. */
1337 err = got_fileindex_write(fileindex, new_index);
1338 if (err)
1339 goto done;
1341 if (rename(new_fileindex_path, fileindex_path) != 0) {
1342 err = got_error_from_errno();
1343 unlink(new_fileindex_path);
1344 goto done;
1347 free(new_fileindex_path);
1348 new_fileindex_path = NULL;
1350 done:
1351 if (tree)
1352 got_object_tree_close(tree);
1353 if (commit)
1354 got_object_commit_close(commit);
1355 if (new_fileindex_path)
1356 unlink(new_fileindex_path);
1357 if (new_index)
1358 fclose(new_index);
1359 free(new_fileindex_path);
1360 free(fileindex_path);
1361 got_fileindex_free(fileindex);
1362 if (checkout_err)
1363 err = checkout_err;
1364 unlockerr = lock_worktree(worktree, LOCK_SH);
1365 if (unlockerr && err == NULL)
1366 err = unlockerr;
1367 return err;
1370 struct diff_dir_cb_arg {
1371 struct got_fileindex *fileindex;
1372 struct got_worktree *worktree;
1373 const char *status_path;
1374 size_t status_path_len;
1375 struct got_repository *repo;
1376 got_worktree_status_cb status_cb;
1377 void *status_arg;
1378 got_worktree_cancel_cb cancel_cb;
1379 void *cancel_arg;
1382 static const struct got_error *
1383 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1384 got_worktree_status_cb status_cb, void *status_arg,
1385 struct got_repository *repo)
1387 const struct got_error *err = NULL;
1388 unsigned char status = GOT_STATUS_NO_CHANGE;
1389 struct stat sb;
1390 struct got_object_id id;
1392 err = get_file_status(&status, &sb, ie, abspath, repo);
1393 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1394 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1395 err = (*status_cb)(status_arg, status, ie->path, &id);
1397 return err;
1400 static const struct got_error *
1401 status_old_new(void *arg, struct got_fileindex_entry *ie,
1402 struct dirent *de, const char *parent_path)
1404 const struct got_error *err = NULL;
1405 struct diff_dir_cb_arg *a = arg;
1406 char *abspath;
1408 if (got_path_cmp(parent_path, a->status_path) != 0 &&
1409 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1410 return NULL;
1412 if (parent_path[0]) {
1413 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1414 parent_path, de->d_name) == -1)
1415 return got_error_from_errno();
1416 } else {
1417 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1418 de->d_name) == -1)
1419 return got_error_from_errno();
1422 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1423 a->repo);
1424 free(abspath);
1425 return err;
1428 static const struct got_error *
1429 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1431 struct diff_dir_cb_arg *a = arg;
1432 struct got_object_id id;
1433 unsigned char status;
1435 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1436 return NULL;
1438 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1439 if (got_fileindex_entry_has_file_on_disk(ie))
1440 status = GOT_STATUS_MISSING;
1441 else
1442 status = GOT_STATUS_DELETE;
1443 return (*a->status_cb)(a->status_arg, status, ie->path, &id);
1446 static const struct got_error *
1447 status_new(void *arg, struct dirent *de, const char *parent_path)
1449 const struct got_error *err = NULL;
1450 struct diff_dir_cb_arg *a = arg;
1451 char *path = NULL;
1453 if (de->d_type == DT_DIR)
1454 return NULL;
1456 /* XXX ignore symlinks for now */
1457 if (de->d_type == DT_LNK)
1458 return NULL;
1460 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1461 return NULL;
1463 if (parent_path[0]) {
1464 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1465 return got_error_from_errno();
1466 } else {
1467 path = de->d_name;
1470 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1471 NULL);
1472 if (parent_path[0])
1473 free(path);
1474 return err;
1477 const struct got_error *
1478 got_worktree_status(struct got_worktree *worktree, const char *path,
1479 struct got_repository *repo, got_worktree_status_cb status_cb,
1480 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1482 const struct got_error *err = NULL;
1483 DIR *workdir = NULL;
1484 char *fileindex_path = NULL;
1485 struct got_fileindex *fileindex = NULL;
1486 FILE *index = NULL;
1487 struct got_fileindex_diff_dir_cb fdiff_cb;
1488 struct diff_dir_cb_arg arg;
1489 char *ondisk_path = NULL;
1491 fileindex = got_fileindex_alloc();
1492 if (fileindex == NULL) {
1493 err = got_error_from_errno();
1494 goto done;
1497 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1498 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1499 err = got_error_from_errno();
1500 fileindex_path = NULL;
1501 goto done;
1504 index = fopen(fileindex_path, "rb");
1505 if (index == NULL) {
1506 if (errno != ENOENT) {
1507 err = got_error_from_errno();
1508 goto done;
1510 } else {
1511 err = got_fileindex_read(fileindex, index);
1512 fclose(index);
1513 if (err)
1514 goto done;
1517 if (asprintf(&ondisk_path, "%s%s%s",
1518 worktree->root_path, path[0] ? "/" : "", path) == -1) {
1519 err = got_error_from_errno();
1520 goto done;
1522 workdir = opendir(ondisk_path);
1523 if (workdir == NULL) {
1524 if (errno == ENOTDIR || errno == ENOENT) {
1525 struct got_fileindex_entry *ie;
1526 ie = got_fileindex_entry_get(fileindex, path);
1527 if (ie == NULL) {
1528 err = got_error(GOT_ERR_BAD_PATH);
1529 goto done;
1531 err = report_file_status(ie, ondisk_path,
1532 status_cb, status_arg, repo);
1533 goto done;
1534 } else {
1535 err = got_error_from_errno();
1536 goto done;
1539 fdiff_cb.diff_old_new = status_old_new;
1540 fdiff_cb.diff_old = status_old;
1541 fdiff_cb.diff_new = status_new;
1542 arg.fileindex = fileindex;
1543 arg.worktree = worktree;
1544 arg.status_path = path;
1545 arg.status_path_len = strlen(path);
1546 arg.repo = repo;
1547 arg.status_cb = status_cb;
1548 arg.status_arg = status_arg;
1549 arg.cancel_cb = cancel_cb;
1550 arg.cancel_arg = cancel_arg;
1551 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1552 path, repo, &fdiff_cb, &arg);
1553 done:
1554 if (workdir)
1555 closedir(workdir);
1556 free(ondisk_path);
1557 free(fileindex_path);
1558 got_fileindex_free(fileindex);
1559 return err;
1562 const struct got_error *
1563 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
1564 const char *arg)
1566 const struct got_error *err = NULL;
1567 char *resolved, *path = NULL;
1568 size_t len;
1570 *wt_path = NULL;
1572 resolved = realpath(arg, NULL);
1573 if (resolved == NULL)
1574 return got_error_from_errno();
1576 if (strncmp(got_worktree_get_root_path(worktree), resolved,
1577 strlen(got_worktree_get_root_path(worktree)))) {
1578 err = got_error(GOT_ERR_BAD_PATH);
1579 goto done;
1582 path = strdup(resolved + strlen(got_worktree_get_root_path(worktree)));
1583 if (path == NULL) {
1584 err = got_error_from_errno();
1585 goto done;
1588 /* XXX status walk can't deal with trailing slash! */
1589 len = strlen(path);
1590 while (path[len - 1] == '/') {
1591 path[len - 1] = '\0';
1592 len--;
1594 done:
1595 free(resolved);
1596 if (err == NULL)
1597 *wt_path = path;
1598 else
1599 free(path);
1600 return err;
1603 const struct got_error *
1604 got_worktree_schedule_add(struct got_worktree *worktree,
1605 const char *ondisk_path, got_worktree_status_cb status_cb, void *status_arg,
1606 struct got_repository *repo)
1608 struct got_fileindex *fileindex = NULL;
1609 struct got_fileindex_entry *ie = NULL;
1610 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
1611 FILE *index = NULL, *new_index = NULL;
1612 const struct got_error *err = NULL, *unlockerr = NULL;
1613 int ie_added = 0;
1615 err = lock_worktree(worktree, LOCK_EX);
1616 if (err)
1617 return err;
1619 err = got_path_skip_common_ancestor(&relpath,
1620 got_worktree_get_root_path(worktree), ondisk_path);
1621 if (err)
1622 goto done;
1624 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
1625 if (err)
1626 goto done;
1628 fileindex = got_fileindex_alloc();
1629 if (fileindex == NULL) {
1630 err = got_error_from_errno();
1631 goto done;
1634 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1635 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1636 err = got_error_from_errno();
1637 fileindex_path = NULL;
1638 goto done;
1641 index = fopen(fileindex_path, "rb");
1642 if (index == NULL) {
1643 err = got_error_from_errno();
1644 goto done;
1647 err = got_fileindex_read(fileindex, index);
1648 if (err)
1649 goto done;
1651 err = got_fileindex_entry_add(fileindex, ie);
1652 if (err)
1653 goto done;
1654 ie_added = 1; /* now owned by fileindex; don't free separately */
1656 err = got_opentemp_named(&new_fileindex_path, &new_index,
1657 fileindex_path);
1658 if (err)
1659 goto done;
1661 err = got_fileindex_write(fileindex, new_index);
1662 if (err)
1663 goto done;
1665 if (rename(new_fileindex_path, fileindex_path) != 0) {
1666 err = got_error_from_errno();
1667 goto done;
1670 free(new_fileindex_path);
1671 new_fileindex_path = NULL;
1673 err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
1674 done:
1675 if (index) {
1676 if (fclose(index) != 0 && err == NULL)
1677 err = got_error_from_errno();
1679 if (new_fileindex_path) {
1680 if (unlink(new_fileindex_path) != 0 && err == NULL)
1681 err = got_error_from_errno();
1682 free(new_fileindex_path);
1684 if (!ie_added)
1685 got_fileindex_entry_free(ie);
1686 if (fileindex)
1687 got_fileindex_free(fileindex);
1688 unlockerr = lock_worktree(worktree, LOCK_SH);
1689 if (unlockerr && err == NULL)
1690 err = unlockerr;
1691 free(relpath);
1692 return err;
1695 const struct got_error *
1696 got_worktree_schedule_delete(struct got_worktree *worktree,
1697 const char *ondisk_path, int delete_local_mods,
1698 got_worktree_status_cb status_cb, void *status_arg,
1699 struct got_repository *repo)
1701 struct got_fileindex *fileindex = NULL;
1702 struct got_fileindex_entry *ie = NULL;
1703 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
1704 FILE *index = NULL, *new_index = NULL;
1705 const struct got_error *err = NULL, *unlockerr = NULL;
1706 unsigned char status;
1707 struct stat sb;
1709 err = lock_worktree(worktree, LOCK_EX);
1710 if (err)
1711 return err;
1713 err = got_path_skip_common_ancestor(&relpath,
1714 got_worktree_get_root_path(worktree), ondisk_path);
1715 if (err)
1716 goto done;
1718 fileindex = got_fileindex_alloc();
1719 if (fileindex == NULL) {
1720 err = got_error_from_errno();
1721 goto done;
1724 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1725 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1726 err = got_error_from_errno();
1727 fileindex_path = NULL;
1728 goto done;
1731 index = fopen(fileindex_path, "rb");
1732 if (index == NULL) {
1733 err = got_error_from_errno();
1734 goto done;
1737 err = got_fileindex_read(fileindex, index);
1738 if (err)
1739 goto done;
1741 ie = got_fileindex_entry_get(fileindex, relpath);
1742 if (ie == NULL) {
1743 err = got_error(GOT_ERR_BAD_PATH);
1744 goto done;
1747 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1748 if (err)
1749 goto done;
1751 if (status != GOT_STATUS_NO_CHANGE) {
1752 if (status != GOT_STATUS_MODIFY) {
1753 err = got_error(GOT_ERR_FILE_STATUS);
1754 goto done;
1756 if (!delete_local_mods) {
1757 err = got_error(GOT_ERR_FILE_MODIFIED);
1758 goto done;
1762 if (unlink(ondisk_path) != 0) {
1763 err = got_error_from_errno();
1764 goto done;
1767 got_fileindex_entry_mark_deleted_from_disk(ie);
1769 err = got_opentemp_named(&new_fileindex_path, &new_index,
1770 fileindex_path);
1771 if (err)
1772 goto done;
1774 err = got_fileindex_write(fileindex, new_index);
1775 if (err)
1776 goto done;
1778 if (rename(new_fileindex_path, fileindex_path) != 0) {
1779 err = got_error_from_errno();
1780 goto done;
1783 free(new_fileindex_path);
1784 new_fileindex_path = NULL;
1786 err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
1787 done:
1788 free(relpath);
1789 if (index) {
1790 if (fclose(index) != 0 && err == NULL)
1791 err = got_error_from_errno();
1793 if (new_fileindex_path) {
1794 if (unlink(new_fileindex_path) != 0 && err == NULL)
1795 err = got_error_from_errno();
1796 free(new_fileindex_path);
1798 if (fileindex)
1799 got_fileindex_free(fileindex);
1800 unlockerr = lock_worktree(worktree, LOCK_SH);
1801 if (unlockerr && err == NULL)
1802 err = unlockerr;
1803 return err;