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>
35 #include <util.h>
37 #include "got_error.h"
38 #include "got_repository.h"
39 #include "got_reference.h"
40 #include "got_object.h"
41 #include "got_worktree.h"
42 #include "got_opentemp.h"
44 #include "got_lib_worktree.h"
45 #include "got_lib_path.h"
46 #include "got_lib_sha1.h"
47 #include "got_lib_fileindex.h"
48 #include "got_lib_inflate.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_object.h"
51 #include "got_lib_diff.h"
53 #ifndef MIN
54 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
55 #endif
57 static const struct got_error *
58 create_meta_file(const char *path_got, const char *name, const char *content)
59 {
60 const struct got_error *err = NULL;
61 char *path;
62 int fd = -1;
64 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
65 err = got_error_from_errno();
66 path = NULL;
67 goto done;
68 }
70 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
71 GOT_DEFAULT_FILE_MODE);
72 if (fd == -1) {
73 err = got_error_from_errno();
74 goto done;
75 }
77 if (content) {
78 int len = dprintf(fd, "%s\n", content);
79 if (len != strlen(content) + 1) {
80 err = got_error_from_errno();
81 goto done;
82 }
83 }
85 done:
86 if (fd != -1 && close(fd) == -1 && err == NULL)
87 err = got_error_from_errno();
88 free(path);
89 return err;
90 }
92 static const struct got_error *
93 update_meta_file(const char *path_got, const char *name, const char *content)
94 {
95 const struct got_error *err = NULL;
96 FILE *tmpfile = NULL;
97 char *tmppath = NULL;
98 char *path = NULL;
100 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
101 err = got_error_from_errno();
102 path = NULL;
103 goto done;
106 err = got_opentemp_named(&tmppath, &tmpfile, path);
107 if (err)
108 goto done;
110 if (content) {
111 int len = fprintf(tmpfile, "%s\n", content);
112 if (len != strlen(content) + 1) {
113 err = got_error_from_errno();
114 goto done;
118 if (rename(tmppath, path) != 0) {
119 err = got_error_from_errno();
120 unlink(tmppath);
121 goto done;
124 done:
125 free(tmppath);
126 if (fclose(tmpfile) != 0 && err == NULL)
127 err = got_error_from_errno();
128 return err;
131 static const struct got_error *
132 read_meta_file(char **content, const char *path_got, const char *name)
134 const struct got_error *err = NULL;
135 char *path;
136 int fd = -1;
137 ssize_t n;
138 struct stat sb;
140 *content = NULL;
142 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
143 err = got_error_from_errno();
144 path = NULL;
145 goto done;
148 fd = open(path, O_RDONLY | O_NOFOLLOW);
149 if (fd == -1) {
150 if (errno == ENOENT)
151 err = got_error(GOT_ERR_WORKTREE_META);
152 else
153 err = got_error_from_errno();
154 goto done;
156 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
157 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
158 : got_error_from_errno());
159 goto done;
162 if (lstat(path, &sb) != 0) {
163 err = got_error_from_errno();
164 goto done;
166 *content = calloc(1, sb.st_size);
167 if (*content == NULL) {
168 err = got_error_from_errno();
169 goto done;
172 n = read(fd, *content, sb.st_size);
173 if (n != sb.st_size) {
174 err = (n == -1 ? got_error_from_errno() :
175 got_error(GOT_ERR_WORKTREE_META));
176 goto done;
178 if ((*content)[sb.st_size - 1] != '\n') {
179 err = got_error(GOT_ERR_WORKTREE_META);
180 goto done;
182 (*content)[sb.st_size - 1] = '\0';
184 done:
185 if (fd != -1 && close(fd) == -1 && err == NULL)
186 err = got_error_from_errno();
187 free(path);
188 if (err) {
189 free(*content);
190 *content = NULL;
192 return err;
195 const struct got_error *
196 got_worktree_init(const char *path, struct got_reference *head_ref,
197 const char *prefix, struct got_repository *repo)
199 const struct got_error *err = NULL;
200 struct got_object_id *commit_id = NULL;
201 uuid_t uuid;
202 uint32_t uuid_status;
203 int obj_type;
204 char *path_got = NULL;
205 char *refstr = NULL;
206 char *formatstr = NULL;
207 char *absprefix = NULL;
208 char *basestr = NULL;
209 char *uuidstr = NULL;
211 if (strcmp(path, got_repo_get_path(repo)) == 0) {
212 err = got_error(GOT_ERR_WORKTREE_REPO);
213 goto done;
216 err = got_ref_resolve(&commit_id, repo, head_ref);
217 if (err)
218 return err;
219 err = got_object_get_type(&obj_type, repo, commit_id);
220 if (err)
221 return err;
222 if (obj_type != GOT_OBJ_TYPE_COMMIT)
223 return got_error(GOT_ERR_OBJ_TYPE);
225 if (!got_path_is_absolute(prefix)) {
226 if (asprintf(&absprefix, "/%s", prefix) == -1)
227 return got_error_from_errno();
230 /* Create top-level directory (may already exist). */
231 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
232 err = got_error_from_errno();
233 goto done;
236 /* Create .got directory (may already exist). */
237 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
238 err = got_error_from_errno();
239 goto done;
241 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
242 err = got_error_from_errno();
243 goto done;
246 /* Create an empty lock file. */
247 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
248 if (err)
249 goto done;
251 /* Create an empty file index. */
252 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
253 if (err)
254 goto done;
256 /* Write the HEAD reference. */
257 refstr = got_ref_to_str(head_ref);
258 if (refstr == NULL) {
259 err = got_error_from_errno();
260 goto done;
262 err = create_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
263 if (err)
264 goto done;
266 /* Record our base commit. */
267 err = got_object_id_str(&basestr, commit_id);
268 if (err)
269 goto done;
270 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
271 if (err)
272 goto done;
274 /* Store path to repository. */
275 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
276 got_repo_get_path(repo));
277 if (err)
278 goto done;
280 /* Store in-repository path prefix. */
281 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
282 absprefix ? absprefix : prefix);
283 if (err)
284 goto done;
286 /* Generate UUID. */
287 uuid_create(&uuid, &uuid_status);
288 if (uuid_status != uuid_s_ok) {
289 err = got_error_uuid(uuid_status);
290 goto done;
292 uuid_to_string(&uuid, &uuidstr, &uuid_status);
293 if (uuid_status != uuid_s_ok) {
294 err = got_error_uuid(uuid_status);
295 goto done;
297 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
298 if (err)
299 goto done;
301 /* Stamp work tree with format file. */
302 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
303 err = got_error_from_errno();
304 goto done;
306 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
307 if (err)
308 goto done;
310 done:
311 free(commit_id);
312 free(path_got);
313 free(formatstr);
314 free(refstr);
315 free(absprefix);
316 free(basestr);
317 free(uuidstr);
318 return err;
321 static const struct got_error *
322 open_worktree(struct got_worktree **worktree, const char *path)
324 const struct got_error *err = NULL;
325 char *path_got;
326 char *formatstr = NULL;
327 char *uuidstr = NULL;
328 char *path_lock = NULL;
329 char *base_commit_id_str = NULL;
330 char *head_ref_str = NULL;
331 int version, fd = -1;
332 const char *errstr;
333 struct got_repository *repo = NULL;
334 uint32_t uuid_status;
336 *worktree = NULL;
338 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
339 err = got_error_from_errno();
340 path_got = NULL;
341 goto done;
344 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
345 err = got_error_from_errno();
346 path_lock = NULL;
347 goto done;
350 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
351 if (fd == -1) {
352 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
353 : got_error_from_errno());
354 goto done;
357 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
358 if (err)
359 goto done;
361 version = strtonum(formatstr, 1, INT_MAX, &errstr);
362 if (errstr) {
363 err = got_error(GOT_ERR_WORKTREE_META);
364 goto done;
366 if (version != GOT_WORKTREE_FORMAT_VERSION) {
367 err = got_error(GOT_ERR_WORKTREE_VERS);
368 goto done;
371 *worktree = calloc(1, sizeof(**worktree));
372 if (*worktree == NULL) {
373 err = got_error_from_errno();
374 goto done;
376 (*worktree)->lockfd = -1;
378 (*worktree)->root_path = strdup(path);
379 if ((*worktree)->root_path == NULL) {
380 err = got_error_from_errno();
381 goto done;
383 err = read_meta_file(&(*worktree)->repo_path, path_got,
384 GOT_WORKTREE_REPOSITORY);
385 if (err)
386 goto done;
388 err = read_meta_file(&(*worktree)->path_prefix, path_got,
389 GOT_WORKTREE_PATH_PREFIX);
390 if (err)
391 goto done;
393 err = read_meta_file(&base_commit_id_str, path_got,
394 GOT_WORKTREE_BASE_COMMIT);
395 if (err)
396 goto done;
398 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
399 if (err)
400 goto done;
401 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
402 if (uuid_status != uuid_s_ok) {
403 err = got_error_uuid(uuid_status);
404 goto done;
407 err = got_repo_open(&repo, (*worktree)->repo_path);
408 if (err)
409 goto done;
411 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
412 base_commit_id_str);
413 if (err)
414 goto done;
416 err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
417 if (err)
418 goto done;
420 err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
421 done:
422 if (repo)
423 got_repo_close(repo);
424 free(path_got);
425 free(path_lock);
426 free(head_ref_str);
427 free(base_commit_id_str);
428 free(uuidstr);
429 free(formatstr);
430 if (err) {
431 if (fd != -1)
432 close(fd);
433 if (*worktree != NULL)
434 got_worktree_close(*worktree);
435 *worktree = NULL;
436 } else
437 (*worktree)->lockfd = fd;
439 return err;
442 const struct got_error *
443 got_worktree_open(struct got_worktree **worktree, const char *path)
445 const struct got_error *err = NULL;
447 do {
448 err = open_worktree(worktree, path);
449 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
450 return err;
451 if (*worktree)
452 return NULL;
453 path = dirname(path);
454 if (path == NULL)
455 return got_error_from_errno();
456 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
458 return got_error(GOT_ERR_NOT_WORKTREE);
461 const struct got_error *
462 got_worktree_close(struct got_worktree *worktree)
464 const struct got_error *err = NULL;
465 free(worktree->root_path);
466 free(worktree->repo_path);
467 free(worktree->path_prefix);
468 free(worktree->base_commit_id);
469 if (worktree->head_ref)
470 got_ref_close(worktree->head_ref);
471 if (worktree->lockfd != -1)
472 if (close(worktree->lockfd) != 0)
473 err = got_error_from_errno();
474 free(worktree);
475 return err;
478 const char *
479 got_worktree_get_root_path(struct got_worktree *worktree)
481 return worktree->root_path;
484 const char *
485 got_worktree_get_repo_path(struct got_worktree *worktree)
487 return worktree->repo_path;
490 const char *
491 got_worktree_get_path_prefix(struct got_worktree *worktree)
493 return worktree->path_prefix;
496 const struct got_error *
497 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
498 const char *path_prefix)
500 char *absprefix = NULL;
502 if (!got_path_is_absolute(path_prefix)) {
503 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
504 return got_error_from_errno();
506 *match = (strcmp(absprefix ? absprefix : path_prefix,
507 worktree->path_prefix) == 0);
508 free(absprefix);
509 return NULL;
512 char *
513 got_worktree_get_head_ref_name(struct got_worktree *worktree)
515 return got_ref_to_str(worktree->head_ref);
518 struct got_reference *
519 got_worktree_get_head_ref(struct got_worktree *worktree)
521 return got_ref_dup(worktree->head_ref);
524 struct got_object_id *
525 got_worktree_get_base_commit_id(struct got_worktree *worktree)
527 return worktree->base_commit_id;
530 const struct got_error *
531 got_worktree_set_base_commit_id(struct got_worktree *worktree,
532 struct got_repository *repo, struct got_object_id *commit_id)
534 const struct got_error *err;
535 struct got_object *obj = NULL;
536 char *id_str = NULL;
537 char *path_got = NULL;
539 if (asprintf(&path_got, "%s/%s", worktree->root_path,
540 GOT_WORKTREE_GOT_DIR) == -1) {
541 err = got_error_from_errno();
542 path_got = NULL;
543 goto done;
546 err = got_object_open(&obj, repo, commit_id);
547 if (err)
548 return err;
550 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
551 err = got_error(GOT_ERR_OBJ_TYPE);
552 goto done;
555 /* Record our base commit. */
556 err = got_object_id_str(&id_str, commit_id);
557 if (err)
558 goto done;
559 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
560 if (err)
561 goto done;
563 free(worktree->base_commit_id);
564 worktree->base_commit_id = got_object_id_dup(commit_id);
565 if (worktree->base_commit_id == NULL) {
566 err = got_error_from_errno();
567 goto done;
569 done:
570 if (obj)
571 got_object_close(obj);
572 free(id_str);
573 free(path_got);
574 return err;
577 static const struct got_error *
578 lock_worktree(struct got_worktree *worktree, int operation)
580 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
581 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
582 : got_error_from_errno());
583 return NULL;
586 static const struct got_error *
587 add_dir_on_disk(struct got_worktree *worktree, const char *path)
589 const struct got_error *err = NULL;
590 char *abspath;
592 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
593 return got_error_from_errno();
595 err = got_path_mkdir(abspath);
596 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
597 struct stat sb;
598 err = NULL;
599 if (lstat(abspath, &sb) == -1) {
600 err = got_error_from_errno();
601 } else if (!S_ISDIR(sb.st_mode)) {
602 /* TODO directory is obstructed; do something */
603 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
606 free(abspath);
607 return err;
610 static const struct got_error *
611 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
613 const struct got_error *err = NULL;
614 uint8_t fbuf1[8192];
615 uint8_t fbuf2[8192];
616 size_t flen1 = 0, flen2 = 0;
618 *same = 1;
620 while (1) {
621 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
622 if (flen1 == 0 && ferror(f1)) {
623 err = got_error_from_errno();
624 break;
626 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
627 if (flen2 == 0 && ferror(f2)) {
628 err = got_error_from_errno();
629 break;
631 if (flen1 == 0) {
632 if (flen2 != 0)
633 *same = 0;
634 break;
635 } else if (flen2 == 0) {
636 if (flen1 != 0)
637 *same = 0;
638 break;
639 } else if (flen1 == flen2) {
640 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
641 *same = 0;
642 break;
644 } else {
645 *same = 0;
646 break;
650 return err;
653 static const struct got_error *
654 check_files_equal(int *same, const char *f1_path, const char *f2_path)
656 const struct got_error *err = NULL;
657 struct stat sb;
658 size_t size1, size2;
659 FILE *f1 = NULL, *f2 = NULL;
661 *same = 1;
663 if (lstat(f1_path, &sb) != 0) {
664 err = got_error_from_errno();
665 goto done;
667 size1 = sb.st_size;
669 if (lstat(f2_path, &sb) != 0) {
670 err = got_error_from_errno();
671 goto done;
673 size2 = sb.st_size;
675 if (size1 != size2) {
676 *same = 0;
677 return NULL;
680 f1 = fopen(f1_path, "r");
681 if (f1 == NULL)
682 return got_error_from_errno();
684 f2 = fopen(f2_path, "r");
685 if (f2 == NULL) {
686 err = got_error_from_errno();
687 goto done;
690 err = check_file_contents_equal(same, f1, f2);
691 done:
692 if (f1 && fclose(f1) != 0 && err == NULL)
693 err = got_error_from_errno();
694 if (f2 && fclose(f2) != 0 && err == NULL)
695 err = got_error_from_errno();
697 return err;
700 /*
701 * Perform a 3-way merge where the file's version in the file index (blob2)
702 * acts as the common ancestor, the incoming blob (blob1) acts as the first
703 * derived version, and the file on disk acts as the second derived version.
704 */
705 static const struct got_error *
706 merge_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
707 struct got_fileindex_entry *ie, const char *ondisk_path, const char *path,
708 uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob1,
709 struct got_repository *repo,
710 got_worktree_checkout_cb progress_cb, void *progress_arg)
712 const struct got_error *err = NULL;
713 int merged_fd = -1;
714 struct got_blob_object *blob2 = NULL;
715 FILE *f1 = NULL, *f2 = NULL;
716 char *blob1_path = NULL, *blob2_path = NULL;
717 char *merged_path = NULL, *base_path = NULL;
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;
758 if (got_fileindex_entry_has_blob(ie)) {
759 struct got_object_id id2;
760 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
761 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
762 if (err)
763 goto done;
764 err = got_object_blob_dump_to_file(NULL, NULL, f2, blob2);
765 if (err)
766 goto done;
767 } else {
768 /*
769 * If the file has no blob, this is an "add vs add" conflict,
770 * and we simply use an empty ancestor file to make both files
771 * appear in the merged result in their entirety.
772 */
775 err = got_object_id_str(&id_str, worktree->base_commit_id);
776 if (err)
777 goto done;
778 if (asprintf(&label1, "commit %s", id_str) == -1) {
779 err = got_error_from_errno();
780 goto done;
783 err = got_merge_diff3(&overlapcnt, merged_fd, blob1_path,
784 blob2_path, ondisk_path, label1, path);
785 if (err)
786 goto done;
788 (*progress_cb)(progress_arg,
789 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
792 if (fsync(merged_fd) != 0) {
793 err = got_error_from_errno();
794 goto done;
797 /* Check if a clean merge has subsumed all local changes. */
798 if (overlapcnt == 0) {
799 err = check_files_equal(&update_timestamps, blob1_path,
800 merged_path);
801 if (err)
802 goto done;
805 if (chmod(merged_path, st_mode) != 0) {
806 err = got_error_from_errno();
807 goto done;
810 if (rename(merged_path, ondisk_path) != 0) {
811 err = got_error_from_errno();
812 unlink(merged_path);
813 goto done;
816 /*
817 * Do not update timestamps of already modified files. Otherwise,
818 * a future status walk would treat them as unmodified files again.
819 */
820 err = got_fileindex_entry_update(ie, ondisk_path,
821 blob1->id.sha1, worktree->base_commit_id->sha1, update_timestamps);
822 done:
823 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
824 err = got_error_from_errno();
825 if (f1 && fclose(f1) != 0 && err == NULL)
826 err = got_error_from_errno();
827 if (f2 && fclose(f2) != 0 && err == NULL)
828 err = got_error_from_errno();
829 if (blob2)
830 got_object_blob_close(blob2);
831 free(merged_path);
832 free(base_path);
833 if (blob1_path) {
834 unlink(blob1_path);
835 free(blob1_path);
837 if (blob2_path) {
838 unlink(blob2_path);
839 free(blob2_path);
841 free(id_str);
842 free(label1);
843 return err;
846 static const struct got_error *
847 install_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
848 struct got_fileindex_entry *entry, const char *ondisk_path, const char *path,
849 uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob,
850 int restoring_missing_file, struct got_repository *repo,
851 got_worktree_checkout_cb progress_cb, void *progress_arg)
853 const struct got_error *err = NULL;
854 int fd = -1;
855 size_t len, hdrlen;
856 int update = 0;
857 char *tmppath = NULL;
859 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
860 GOT_DEFAULT_FILE_MODE);
861 if (fd == -1) {
862 if (errno == ENOENT) {
863 char *parent = dirname(path);
864 if (parent == NULL)
865 return got_error_from_errno();
866 err = add_dir_on_disk(worktree, parent);
867 if (err)
868 return err;
869 fd = open(ondisk_path,
870 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
871 GOT_DEFAULT_FILE_MODE);
872 if (fd == -1)
873 return got_error_from_errno();
874 } else if (errno == EEXIST) {
875 if (!S_ISREG(st_mode)) {
876 /* TODO file is obstructed; do something */
877 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
878 goto done;
879 } else {
880 err = got_opentemp_named_fd(&tmppath, &fd,
881 ondisk_path);
882 if (err)
883 goto done;
884 update = 1;
886 } else
887 return got_error_from_errno();
890 if (restoring_missing_file)
891 (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
892 else
893 (*progress_cb)(progress_arg,
894 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
896 hdrlen = got_object_blob_get_hdrlen(blob);
897 do {
898 const uint8_t *buf = got_object_blob_get_read_buf(blob);
899 err = got_object_blob_read_block(&len, blob);
900 if (err)
901 break;
902 if (len > 0) {
903 /* Skip blob object header first time around. */
904 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
905 if (outlen == -1) {
906 err = got_error_from_errno();
907 goto done;
908 } else if (outlen != len - hdrlen) {
909 err = got_error(GOT_ERR_IO);
910 goto done;
912 hdrlen = 0;
914 } while (len != 0);
916 if (fsync(fd) != 0) {
917 err = got_error_from_errno();
918 goto done;
921 if (update) {
922 if (rename(tmppath, ondisk_path) != 0) {
923 err = got_error_from_errno();
924 unlink(tmppath);
925 goto done;
929 if (te_mode & S_IXUSR) {
930 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
931 err = got_error_from_errno();
932 goto done;
934 } else {
935 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
936 err = got_error_from_errno();
937 goto done;
941 if (entry == NULL)
942 entry = got_fileindex_entry_get(fileindex, path);
943 if (entry)
944 err = got_fileindex_entry_update(entry, ondisk_path,
945 blob->id.sha1, worktree->base_commit_id->sha1, 1);
946 else {
947 err = got_fileindex_entry_alloc(&entry, ondisk_path,
948 path, blob->id.sha1, worktree->base_commit_id->sha1);
949 if (err)
950 goto done;
951 err = got_fileindex_entry_add(fileindex, entry);
953 done:
954 if (fd != -1 && close(fd) != 0 && err == NULL)
955 err = got_error_from_errno();
956 free(tmppath);
957 return err;
960 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
961 static const struct got_error *
962 get_modified_file_content_status(unsigned char *status, FILE *f)
964 const struct got_error *err = NULL;
965 const char *markers[3] = {
966 GOT_DIFF_CONFLICT_MARKER_BEGIN,
967 GOT_DIFF_CONFLICT_MARKER_SEP,
968 GOT_DIFF_CONFLICT_MARKER_END
969 };
970 int i = 0;
971 char *line;
972 size_t len;
973 const char delim[3] = {'\0', '\0', '\0'};
975 while (*status == GOT_STATUS_MODIFY) {
976 line = fparseln(f, &len, NULL, delim, 0);
977 if (line == NULL) {
978 if (feof(f))
979 break;
980 err = got_ferror(f, GOT_ERR_IO);
981 break;
984 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
985 if (markers[i] == GOT_DIFF_CONFLICT_MARKER_END)
986 *status = GOT_STATUS_CONFLICT;
987 else
988 i++;
992 return err;
995 static const struct got_error *
996 get_file_status(unsigned char *status, struct stat *sb,
997 struct got_fileindex_entry *ie, const char *abspath,
998 struct got_repository *repo)
1000 const struct got_error *err = NULL;
1001 struct got_object_id id;
1002 size_t hdrlen;
1003 FILE *f = NULL;
1004 uint8_t fbuf[8192];
1005 struct got_blob_object *blob = NULL;
1006 size_t flen, blen;
1008 *status = GOT_STATUS_NO_CHANGE;
1010 if (lstat(abspath, sb) == -1) {
1011 if (errno == ENOENT) {
1012 if (ie) {
1013 if (got_fileindex_entry_has_file_on_disk(ie))
1014 *status = GOT_STATUS_MISSING;
1015 else
1016 *status = GOT_STATUS_DELETE;
1017 sb->st_mode =
1018 ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
1019 & (S_IRWXU | S_IRWXG | S_IRWXO));
1020 } else
1021 sb->st_mode = GOT_DEFAULT_FILE_MODE;
1022 return NULL;
1024 return got_error_from_errno();
1027 if (!S_ISREG(sb->st_mode)) {
1028 *status = GOT_STATUS_OBSTRUCTED;
1029 return NULL;
1032 if (ie == NULL)
1033 return NULL;
1035 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1036 *status = GOT_STATUS_DELETE;
1037 return NULL;
1038 } else if (!got_fileindex_entry_has_blob(ie)) {
1039 *status = GOT_STATUS_ADD;
1040 return NULL;
1043 if (ie->ctime_sec == sb->st_ctime &&
1044 ie->ctime_nsec == sb->st_ctimensec &&
1045 ie->mtime_sec == sb->st_mtime &&
1046 ie->mtime_sec == sb->st_mtime &&
1047 ie->mtime_nsec == sb->st_mtimensec &&
1048 ie->size == (sb->st_size & 0xffffffff))
1049 return NULL;
1051 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1052 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1053 if (err)
1054 return err;
1056 f = fopen(abspath, "r");
1057 if (f == NULL) {
1058 err = got_error_from_errno();
1059 goto done;
1061 hdrlen = got_object_blob_get_hdrlen(blob);
1062 while (1) {
1063 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1064 err = got_object_blob_read_block(&blen, blob);
1065 if (err)
1066 goto done;
1067 /* Skip length of blob object header first time around. */
1068 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1069 if (flen == 0 && ferror(f)) {
1070 err = got_error_from_errno();
1071 goto done;
1073 if (blen == 0) {
1074 if (flen != 0)
1075 *status = GOT_STATUS_MODIFY;
1076 break;
1077 } else if (flen == 0) {
1078 if (blen != 0)
1079 *status = GOT_STATUS_MODIFY;
1080 break;
1081 } else if (blen - hdrlen == flen) {
1082 /* Skip blob object header first time around. */
1083 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1084 *status = GOT_STATUS_MODIFY;
1085 break;
1087 } else {
1088 *status = GOT_STATUS_MODIFY;
1089 break;
1091 hdrlen = 0;
1094 if (*status == GOT_STATUS_MODIFY) {
1095 rewind(f);
1096 err = get_modified_file_content_status(status, f);
1098 done:
1099 if (blob)
1100 got_object_blob_close(blob);
1101 if (f)
1102 fclose(f);
1103 return err;
1106 static const struct got_error *
1107 update_blob(struct got_worktree *worktree,
1108 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1109 struct got_tree_entry *te, const char *path,
1110 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1111 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1113 const struct got_error *err = NULL;
1114 struct got_blob_object *blob = NULL;
1115 char *ondisk_path;
1116 unsigned char status = GOT_STATUS_NO_CHANGE;
1117 struct stat sb;
1119 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1120 return got_error_from_errno();
1122 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1123 if (err)
1124 goto done;
1126 if (status == GOT_STATUS_OBSTRUCTED) {
1127 (*progress_cb)(progress_arg, status, path);
1128 goto done;
1131 if (ie && status != GOT_STATUS_MISSING) {
1132 if (got_fileindex_entry_has_commit(ie) &&
1133 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1134 SHA1_DIGEST_LENGTH) == 0) {
1135 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1136 path);
1137 goto done;
1139 if (got_fileindex_entry_has_blob(ie) &&
1140 memcmp(ie->blob_sha1, te->id->sha1,
1141 SHA1_DIGEST_LENGTH) == 0)
1142 goto done;
1145 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1146 if (err)
1147 goto done;
1149 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD)
1150 err = merge_blob(worktree, fileindex, ie, ondisk_path, path,
1151 te->mode, sb.st_mode, blob, repo, progress_cb,
1152 progress_arg);
1153 else
1154 err = install_blob(worktree, fileindex, ie, ondisk_path, path,
1155 te->mode, sb.st_mode, blob, status == GOT_STATUS_MISSING,
1156 repo, progress_cb, progress_arg);
1158 got_object_blob_close(blob);
1159 done:
1160 free(ondisk_path);
1161 return err;
1164 static const struct got_error *
1165 remove_ondisk_file(const char *root_path, const char *path)
1167 const struct got_error *err = NULL;
1168 char *ondisk_path = NULL;
1170 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1171 return got_error_from_errno();
1173 if (unlink(ondisk_path) == -1) {
1174 if (errno != ENOENT)
1175 err = got_error_from_errno();
1176 } else {
1177 char *parent = dirname(ondisk_path);
1178 while (parent && strcmp(parent, root_path) != 0) {
1179 if (rmdir(parent) == -1) {
1180 if (errno != ENOTEMPTY)
1181 err = got_error_from_errno();
1182 break;
1184 parent = dirname(parent);
1187 free(ondisk_path);
1188 return err;
1191 struct diff_cb_arg {
1192 struct got_fileindex *fileindex;
1193 struct got_worktree *worktree;
1194 struct got_repository *repo;
1195 got_worktree_checkout_cb progress_cb;
1196 void *progress_arg;
1197 got_worktree_cancel_cb cancel_cb;
1198 void *cancel_arg;
1201 static const struct got_error *
1202 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1203 struct got_tree_entry *te, const char *parent_path)
1205 struct diff_cb_arg *a = arg;
1207 return update_blob(a->worktree, a->fileindex, ie, te,
1208 ie->path, a->repo, a->progress_cb, a->progress_arg,
1209 a->cancel_cb, a->cancel_arg);
1212 static const struct got_error *
1213 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1215 const struct got_error *err;
1216 struct diff_cb_arg *a = arg;
1218 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE, ie->path);
1220 err = remove_ondisk_file(a->worktree->root_path, ie->path);
1221 if (err)
1222 return err;
1223 got_fileindex_entry_remove(a->fileindex, ie);
1224 return NULL;
1227 static const struct got_error *
1228 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1230 struct diff_cb_arg *a = arg;
1231 const struct got_error *err;
1232 char *path;
1234 if (asprintf(&path, "%s%s%s", parent_path,
1235 parent_path[0] ? "/" : "", te->name)
1236 == -1)
1237 return got_error_from_errno();
1239 if (S_ISDIR(te->mode))
1240 err = add_dir_on_disk(a->worktree, path);
1241 else
1242 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1243 a->repo, a->progress_cb, a->progress_arg,
1244 a->cancel_cb, a->cancel_arg);
1246 free(path);
1247 return err;
1250 const struct got_error *
1251 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1253 const struct got_error *err = NULL;
1254 char *uuidstr = NULL;
1255 uint32_t uuid_status;
1257 *refname = NULL;
1259 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1260 if (uuid_status != uuid_s_ok)
1261 return got_error_uuid(uuid_status);
1263 if (asprintf(refname, "%s-%s", GOT_WORKTREE_BASE_REF_PREFIX, uuidstr)
1264 == -1) {
1265 err = got_error_from_errno();
1266 *refname = NULL;
1268 free(uuidstr);
1269 return err;
1273 * Prevent Git's garbage collector from deleting our base commit by
1274 * setting a reference to our base commit's ID.
1276 static const struct got_error *
1277 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1279 const struct got_error *err = NULL;
1280 struct got_reference *ref = NULL;
1281 char *refname;
1283 err = got_worktree_get_base_ref_name(&refname, worktree);
1284 if (err)
1285 return err;
1287 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1288 if (err)
1289 goto done;
1291 err = got_ref_write(ref, repo);
1292 done:
1293 free(refname);
1294 if (ref)
1295 got_ref_close(ref);
1296 return err;
1300 const struct got_error *
1301 got_worktree_checkout_files(struct got_worktree *worktree,
1302 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1303 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1305 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1306 struct got_commit_object *commit = NULL;
1307 struct got_object_id *tree_id = NULL;
1308 struct got_tree_object *tree = NULL;
1309 char *fileindex_path = NULL, *new_fileindex_path = NULL;
1310 struct got_fileindex *fileindex = NULL;
1311 FILE *index = NULL, *new_index = NULL;
1312 struct got_fileindex_diff_tree_cb diff_cb;
1313 struct diff_cb_arg arg;
1315 err = lock_worktree(worktree, LOCK_EX);
1316 if (err)
1317 return err;
1319 fileindex = got_fileindex_alloc();
1320 if (fileindex == NULL) {
1321 err = got_error_from_errno();
1322 goto done;
1325 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1326 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1327 err = got_error_from_errno();
1328 fileindex_path = NULL;
1329 goto done;
1333 * Read the file index.
1334 * Checking out files is supposed to be an idempotent operation.
1335 * If the on-disk file index is incomplete we will try to complete it.
1337 index = fopen(fileindex_path, "rb");
1338 if (index == NULL) {
1339 if (errno != ENOENT) {
1340 err = got_error_from_errno();
1341 goto done;
1343 } else {
1344 err = got_fileindex_read(fileindex, index);
1345 fclose(index);
1346 if (err)
1347 goto done;
1350 err = got_opentemp_named(&new_fileindex_path, &new_index,
1351 fileindex_path);
1352 if (err)
1353 goto done;
1355 err = ref_base_commit(worktree, repo);
1356 if (err)
1357 goto done;
1359 err = got_object_open_as_commit(&commit, repo,
1360 worktree->base_commit_id);
1361 if (err)
1362 goto done;
1364 err = got_object_id_by_path(&tree_id, repo,
1365 worktree->base_commit_id, worktree->path_prefix);
1366 if (err)
1367 goto done;
1369 err = got_object_open_as_tree(&tree, repo, tree_id);
1370 if (err)
1371 goto done;
1373 diff_cb.diff_old_new = diff_old_new;
1374 diff_cb.diff_old = diff_old;
1375 diff_cb.diff_new = diff_new;
1376 arg.fileindex = fileindex;
1377 arg.worktree = worktree;
1378 arg.repo = repo;
1379 arg.progress_cb = progress_cb;
1380 arg.progress_arg = progress_arg;
1381 arg.cancel_cb = cancel_cb;
1382 arg.cancel_arg = cancel_arg;
1383 checkout_err = got_fileindex_diff_tree(fileindex, tree, repo,
1384 &diff_cb, &arg);
1386 /* Try to sync the fileindex back to disk in any case. */
1387 err = got_fileindex_write(fileindex, new_index);
1388 if (err)
1389 goto done;
1391 if (rename(new_fileindex_path, fileindex_path) != 0) {
1392 err = got_error_from_errno();
1393 unlink(new_fileindex_path);
1394 goto done;
1397 free(new_fileindex_path);
1398 new_fileindex_path = NULL;
1400 done:
1401 if (tree)
1402 got_object_tree_close(tree);
1403 if (commit)
1404 got_object_commit_close(commit);
1405 if (new_fileindex_path)
1406 unlink(new_fileindex_path);
1407 if (new_index)
1408 fclose(new_index);
1409 free(new_fileindex_path);
1410 free(fileindex_path);
1411 got_fileindex_free(fileindex);
1412 if (checkout_err)
1413 err = checkout_err;
1414 unlockerr = lock_worktree(worktree, LOCK_SH);
1415 if (unlockerr && err == NULL)
1416 err = unlockerr;
1417 return err;
1420 struct diff_dir_cb_arg {
1421 struct got_fileindex *fileindex;
1422 struct got_worktree *worktree;
1423 const char *status_path;
1424 size_t status_path_len;
1425 struct got_repository *repo;
1426 got_worktree_status_cb status_cb;
1427 void *status_arg;
1428 got_worktree_cancel_cb cancel_cb;
1429 void *cancel_arg;
1432 static const struct got_error *
1433 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1434 got_worktree_status_cb status_cb, void *status_arg,
1435 struct got_repository *repo)
1437 const struct got_error *err = NULL;
1438 unsigned char status = GOT_STATUS_NO_CHANGE;
1439 struct stat sb;
1440 struct got_object_id id;
1442 err = get_file_status(&status, &sb, ie, abspath, repo);
1443 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1444 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1445 err = (*status_cb)(status_arg, status, ie->path, &id);
1447 return err;
1450 static const struct got_error *
1451 status_old_new(void *arg, struct got_fileindex_entry *ie,
1452 struct dirent *de, const char *parent_path)
1454 const struct got_error *err = NULL;
1455 struct diff_dir_cb_arg *a = arg;
1456 char *abspath;
1458 if (got_path_cmp(parent_path, a->status_path) != 0 &&
1459 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1460 return NULL;
1462 if (parent_path[0]) {
1463 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1464 parent_path, de->d_name) == -1)
1465 return got_error_from_errno();
1466 } else {
1467 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1468 de->d_name) == -1)
1469 return got_error_from_errno();
1472 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1473 a->repo);
1474 free(abspath);
1475 return err;
1478 static const struct got_error *
1479 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1481 struct diff_dir_cb_arg *a = arg;
1482 struct got_object_id id;
1483 unsigned char status;
1485 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1486 return NULL;
1488 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1489 if (got_fileindex_entry_has_file_on_disk(ie))
1490 status = GOT_STATUS_MISSING;
1491 else
1492 status = GOT_STATUS_DELETE;
1493 return (*a->status_cb)(a->status_arg, status, ie->path, &id);
1496 static const struct got_error *
1497 status_new(void *arg, struct dirent *de, const char *parent_path)
1499 const struct got_error *err = NULL;
1500 struct diff_dir_cb_arg *a = arg;
1501 char *path = NULL;
1503 if (de->d_type == DT_DIR)
1504 return NULL;
1506 /* XXX ignore symlinks for now */
1507 if (de->d_type == DT_LNK)
1508 return NULL;
1510 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1511 return NULL;
1513 if (parent_path[0]) {
1514 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1515 return got_error_from_errno();
1516 } else {
1517 path = de->d_name;
1520 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1521 NULL);
1522 if (parent_path[0])
1523 free(path);
1524 return err;
1527 const struct got_error *
1528 got_worktree_status(struct got_worktree *worktree, const char *path,
1529 struct got_repository *repo, got_worktree_status_cb status_cb,
1530 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1532 const struct got_error *err = NULL;
1533 DIR *workdir = NULL;
1534 char *fileindex_path = NULL;
1535 struct got_fileindex *fileindex = NULL;
1536 FILE *index = NULL;
1537 struct got_fileindex_diff_dir_cb fdiff_cb;
1538 struct diff_dir_cb_arg arg;
1539 char *ondisk_path = NULL;
1541 fileindex = got_fileindex_alloc();
1542 if (fileindex == NULL) {
1543 err = got_error_from_errno();
1544 goto done;
1547 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1548 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1549 err = got_error_from_errno();
1550 fileindex_path = NULL;
1551 goto done;
1554 index = fopen(fileindex_path, "rb");
1555 if (index == NULL) {
1556 if (errno != ENOENT) {
1557 err = got_error_from_errno();
1558 goto done;
1560 } else {
1561 err = got_fileindex_read(fileindex, index);
1562 fclose(index);
1563 if (err)
1564 goto done;
1567 if (asprintf(&ondisk_path, "%s%s%s",
1568 worktree->root_path, path[0] ? "/" : "", path) == -1) {
1569 err = got_error_from_errno();
1570 goto done;
1572 workdir = opendir(ondisk_path);
1573 if (workdir == NULL) {
1574 if (errno == ENOTDIR || errno == ENOENT) {
1575 struct got_fileindex_entry *ie;
1576 ie = got_fileindex_entry_get(fileindex, path);
1577 if (ie == NULL) {
1578 err = got_error(GOT_ERR_BAD_PATH);
1579 goto done;
1581 err = report_file_status(ie, ondisk_path,
1582 status_cb, status_arg, repo);
1583 goto done;
1584 } else {
1585 err = got_error_from_errno();
1586 goto done;
1589 fdiff_cb.diff_old_new = status_old_new;
1590 fdiff_cb.diff_old = status_old;
1591 fdiff_cb.diff_new = status_new;
1592 arg.fileindex = fileindex;
1593 arg.worktree = worktree;
1594 arg.status_path = path;
1595 arg.status_path_len = strlen(path);
1596 arg.repo = repo;
1597 arg.status_cb = status_cb;
1598 arg.status_arg = status_arg;
1599 arg.cancel_cb = cancel_cb;
1600 arg.cancel_arg = cancel_arg;
1601 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1602 path, repo, &fdiff_cb, &arg);
1603 done:
1604 if (workdir)
1605 closedir(workdir);
1606 free(ondisk_path);
1607 free(fileindex_path);
1608 got_fileindex_free(fileindex);
1609 return err;
1612 const struct got_error *
1613 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
1614 const char *arg)
1616 const struct got_error *err = NULL;
1617 char *resolved, *path = NULL;
1618 size_t len;
1620 *wt_path = NULL;
1622 resolved = realpath(arg, NULL);
1623 if (resolved == NULL)
1624 return got_error_from_errno();
1626 if (strncmp(got_worktree_get_root_path(worktree), resolved,
1627 strlen(got_worktree_get_root_path(worktree)))) {
1628 err = got_error(GOT_ERR_BAD_PATH);
1629 goto done;
1632 path = strdup(resolved + strlen(got_worktree_get_root_path(worktree)));
1633 if (path == NULL) {
1634 err = got_error_from_errno();
1635 goto done;
1638 /* XXX status walk can't deal with trailing slash! */
1639 len = strlen(path);
1640 while (path[len - 1] == '/') {
1641 path[len - 1] = '\0';
1642 len--;
1644 done:
1645 free(resolved);
1646 if (err == NULL)
1647 *wt_path = path;
1648 else
1649 free(path);
1650 return err;
1653 const struct got_error *
1654 got_worktree_schedule_add(struct got_worktree *worktree,
1655 const char *ondisk_path, got_worktree_status_cb status_cb, void *status_arg,
1656 struct got_repository *repo)
1658 struct got_fileindex *fileindex = NULL;
1659 struct got_fileindex_entry *ie = NULL;
1660 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
1661 FILE *index = NULL, *new_index = NULL;
1662 const struct got_error *err = NULL, *unlockerr = NULL;
1663 int ie_added = 0;
1665 err = lock_worktree(worktree, LOCK_EX);
1666 if (err)
1667 return err;
1669 err = got_path_skip_common_ancestor(&relpath,
1670 got_worktree_get_root_path(worktree), ondisk_path);
1671 if (err)
1672 goto done;
1674 fileindex = got_fileindex_alloc();
1675 if (fileindex == NULL) {
1676 err = got_error_from_errno();
1677 goto done;
1680 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1681 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1682 err = got_error_from_errno();
1683 fileindex_path = NULL;
1684 goto done;
1687 index = fopen(fileindex_path, "rb");
1688 if (index == NULL) {
1689 err = got_error_from_errno();
1690 goto done;
1693 err = got_fileindex_read(fileindex, index);
1694 if (err)
1695 goto done;
1697 if (got_fileindex_entry_get(fileindex, relpath) != NULL) {
1698 err = got_error_set_errno(EEXIST);
1699 goto done;
1702 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
1703 if (err)
1704 goto done;
1706 err = got_fileindex_entry_add(fileindex, ie);
1707 if (err)
1708 goto done;
1709 ie_added = 1; /* now owned by fileindex; don't free separately */
1711 err = got_opentemp_named(&new_fileindex_path, &new_index,
1712 fileindex_path);
1713 if (err)
1714 goto done;
1716 err = got_fileindex_write(fileindex, new_index);
1717 if (err)
1718 goto done;
1720 if (rename(new_fileindex_path, fileindex_path) != 0) {
1721 err = got_error_from_errno();
1722 goto done;
1725 free(new_fileindex_path);
1726 new_fileindex_path = NULL;
1728 err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
1729 done:
1730 if (index) {
1731 if (fclose(index) != 0 && err == NULL)
1732 err = got_error_from_errno();
1734 if (new_fileindex_path) {
1735 if (unlink(new_fileindex_path) != 0 && err == NULL)
1736 err = got_error_from_errno();
1737 free(new_fileindex_path);
1739 if (ie && !ie_added)
1740 got_fileindex_entry_free(ie);
1741 if (fileindex)
1742 got_fileindex_free(fileindex);
1743 unlockerr = lock_worktree(worktree, LOCK_SH);
1744 if (unlockerr && err == NULL)
1745 err = unlockerr;
1746 free(relpath);
1747 return err;
1750 const struct got_error *
1751 got_worktree_schedule_delete(struct got_worktree *worktree,
1752 const char *ondisk_path, int delete_local_mods,
1753 got_worktree_status_cb status_cb, void *status_arg,
1754 struct got_repository *repo)
1756 struct got_fileindex *fileindex = NULL;
1757 struct got_fileindex_entry *ie = NULL;
1758 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
1759 FILE *index = NULL, *new_index = NULL;
1760 const struct got_error *err = NULL, *unlockerr = NULL;
1761 unsigned char status;
1762 struct stat sb;
1764 err = lock_worktree(worktree, LOCK_EX);
1765 if (err)
1766 return err;
1768 err = got_path_skip_common_ancestor(&relpath,
1769 got_worktree_get_root_path(worktree), ondisk_path);
1770 if (err)
1771 goto done;
1773 fileindex = got_fileindex_alloc();
1774 if (fileindex == NULL) {
1775 err = got_error_from_errno();
1776 goto done;
1779 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1780 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1781 err = got_error_from_errno();
1782 fileindex_path = NULL;
1783 goto done;
1786 index = fopen(fileindex_path, "rb");
1787 if (index == NULL) {
1788 err = got_error_from_errno();
1789 goto done;
1792 err = got_fileindex_read(fileindex, index);
1793 if (err)
1794 goto done;
1796 ie = got_fileindex_entry_get(fileindex, relpath);
1797 if (ie == NULL) {
1798 err = got_error(GOT_ERR_BAD_PATH);
1799 goto done;
1802 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1803 if (err)
1804 goto done;
1806 if (status != GOT_STATUS_NO_CHANGE) {
1807 if (status == GOT_STATUS_DELETE) {
1808 err = got_error_set_errno(ENOENT);
1809 goto done;
1811 if (status != GOT_STATUS_MODIFY) {
1812 err = got_error(GOT_ERR_FILE_STATUS);
1813 goto done;
1815 if (!delete_local_mods) {
1816 err = got_error(GOT_ERR_FILE_MODIFIED);
1817 goto done;
1821 if (unlink(ondisk_path) != 0) {
1822 err = got_error_from_errno();
1823 goto done;
1826 got_fileindex_entry_mark_deleted_from_disk(ie);
1828 err = got_opentemp_named(&new_fileindex_path, &new_index,
1829 fileindex_path);
1830 if (err)
1831 goto done;
1833 err = got_fileindex_write(fileindex, new_index);
1834 if (err)
1835 goto done;
1837 if (rename(new_fileindex_path, fileindex_path) != 0) {
1838 err = got_error_from_errno();
1839 goto done;
1842 free(new_fileindex_path);
1843 new_fileindex_path = NULL;
1845 err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
1846 done:
1847 free(relpath);
1848 if (index) {
1849 if (fclose(index) != 0 && err == NULL)
1850 err = got_error_from_errno();
1852 if (new_fileindex_path) {
1853 if (unlink(new_fileindex_path) != 0 && err == NULL)
1854 err = got_error_from_errno();
1855 free(new_fileindex_path);
1857 if (fileindex)
1858 got_fileindex_free(fileindex);
1859 unlockerr = lock_worktree(worktree, LOCK_SH);
1860 if (unlockerr && err == NULL)
1861 err = unlockerr;
1862 return err;