2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
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.
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.
18 #include <sys/limits.h>
19 #include <sys/queue.h>
31 #include "got_error.h"
32 #include "got_repository.h"
33 #include "got_reference.h"
34 #include "got_object.h"
35 #include "got_worktree.h"
36 #include "got_opentemp.h"
38 #include "got_lib_worktree.h"
39 #include "got_lib_path.h"
40 #include "got_lib_sha1.h"
41 #include "got_lib_fileindex.h"
42 #include "got_lib_inflate.h"
43 #include "got_lib_delta.h"
44 #include "got_lib_object.h"
47 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
50 static const struct got_error *
51 create_meta_file(const char *path_got, const char *name, const char *content)
53 const struct got_error *err = NULL;
59 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
60 err = got_error_from_errno();
65 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
66 GOT_DEFAULT_FILE_MODE);
68 err = got_error_from_errno();
72 /* The file should be empty. */
73 n = read(fd, buf, sizeof(buf));
75 err = (n == -1 ? got_error_from_errno() :
76 got_error(GOT_ERR_WORKTREE_EXISTS));
81 int len = dprintf(fd, "%s\n", content);
82 if (len != strlen(content) + 1) {
83 err = got_error_from_errno();
89 if (fd != -1 && close(fd) == -1 && err == NULL)
90 err = got_error_from_errno();
95 static const struct got_error *
96 read_meta_file(char **content, const char *path_got, const char *name)
98 const struct got_error *err = NULL;
106 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
107 err = got_error_from_errno();
112 fd = open(path, O_RDONLY | O_NOFOLLOW);
114 err = got_error_from_errno();
117 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
118 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
119 : got_error_from_errno());
124 *content = calloc(1, sb.st_size);
125 if (*content == NULL) {
126 err = got_error_from_errno();
130 n = read(fd, *content, sb.st_size);
131 if (n != sb.st_size) {
132 err = (n == -1 ? got_error_from_errno() :
133 got_error(GOT_ERR_WORKTREE_META));
136 if ((*content)[sb.st_size - 1] != '\n') {
137 err = got_error(GOT_ERR_WORKTREE_META);
140 (*content)[sb.st_size - 1] = '\0';
143 if (fd != -1 && close(fd) == -1 && err == NULL)
144 err = got_error_from_errno();
153 const struct got_error *
154 got_worktree_init(const char *path, struct got_reference *head_ref,
155 const char *prefix, struct got_repository *repo)
157 const struct got_error *err = NULL;
158 struct got_object_id *commit_id = NULL;
160 char *path_got = NULL;
162 char *repo_path = NULL;
163 char *formatstr = NULL;
164 char *absprefix = NULL;
165 char *basestr = NULL;
167 err = got_ref_resolve(&commit_id, repo, head_ref);
170 err = got_object_get_type(&obj_type, repo, commit_id);
173 if (obj_type != GOT_OBJ_TYPE_COMMIT)
174 return got_error(GOT_ERR_OBJ_TYPE);
176 if (!got_path_is_absolute(prefix)) {
177 if (asprintf(&absprefix, "/%s", prefix) == -1)
178 return got_error_from_errno();
181 /* Create top-level directory (may already exist). */
182 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
183 err = got_error_from_errno();
187 /* Create .got directory (may already exist). */
188 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
189 err = got_error_from_errno();
192 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
193 err = got_error_from_errno();
197 /* Create an empty lock file. */
198 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
202 /* Create an empty file index. */
203 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
207 /* Write the HEAD reference. */
208 refstr = got_ref_to_str(head_ref);
209 if (refstr == NULL) {
210 err = got_error_from_errno();
213 err = create_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
217 /* Record our base commit. */
218 err = got_object_id_str(&basestr, commit_id);
221 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
225 /* Store path to repository. */
226 repo_path = got_repo_get_path(repo);
227 if (repo_path == NULL) {
228 err = got_error_from_errno();
231 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY, repo_path);
235 /* Store in-repository path prefix. */
236 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
237 absprefix ? absprefix : prefix);
241 /* Stamp work tree with format file. */
242 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
243 err = got_error_from_errno();
246 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
261 const struct got_error *
262 got_worktree_open(struct got_worktree **worktree, const char *path)
264 const struct got_error *err = NULL;
266 char *formatstr = NULL;
267 char *path_lock = NULL;
268 char *base_commit_id_str = NULL;
269 char *head_ref_str = NULL;
270 int version, fd = -1;
272 struct got_repository *repo = NULL;
276 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
277 err = got_error_from_errno();
282 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
283 err = got_error_from_errno();
288 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
290 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
291 : got_error_from_errno());
295 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
299 version = strtonum(formatstr, 1, INT_MAX, &errstr);
301 err = got_error(GOT_ERR_WORKTREE_META);
304 if (version != GOT_WORKTREE_FORMAT_VERSION) {
305 err = got_error(GOT_ERR_WORKTREE_VERS);
309 *worktree = calloc(1, sizeof(**worktree));
310 if (*worktree == NULL) {
311 err = got_error_from_errno();
314 (*worktree)->lockfd = -1;
316 (*worktree)->root_path = strdup(path);
317 if ((*worktree)->root_path == NULL) {
318 err = got_error_from_errno();
321 err = read_meta_file(&(*worktree)->repo_path, path_got,
322 GOT_WORKTREE_REPOSITORY);
326 err = read_meta_file(&(*worktree)->path_prefix, path_got,
327 GOT_WORKTREE_PATH_PREFIX);
331 err = read_meta_file(&base_commit_id_str, path_got,
332 GOT_WORKTREE_BASE_COMMIT);
336 err = got_repo_open(&repo, (*worktree)->repo_path);
340 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
345 err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
349 err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
352 got_repo_close(repo);
356 free(base_commit_id_str);
360 if (*worktree != NULL)
361 got_worktree_close(*worktree);
364 (*worktree)->lockfd = fd;
370 got_worktree_close(struct got_worktree *worktree)
372 free(worktree->root_path);
373 free(worktree->repo_path);
374 free(worktree->path_prefix);
375 free(worktree->base_commit_id);
376 if (worktree->head_ref)
377 got_ref_close(worktree->head_ref);
378 if (worktree->lockfd != -1)
379 close(worktree->lockfd);
384 got_worktree_get_repo_path(struct got_worktree *worktree)
386 return strdup(worktree->repo_path);
390 got_worktree_get_head_ref_name(struct got_worktree *worktree)
392 return got_ref_to_str(worktree->head_ref);
395 static const struct got_error *
396 lock_worktree(struct got_worktree *worktree, int operation)
398 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
399 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
400 : got_error_from_errno());
405 apply_path_prefix(struct got_worktree *worktree, const char *path)
407 const char *p = path;
408 p += strlen(worktree->path_prefix);
414 static const struct got_error *
415 add_file_on_disk(struct got_worktree *worktree, struct got_fileindex *fileindex,
416 const char *path, struct got_blob_object *blob, struct got_repository *repo)
418 const struct got_error *err = NULL;
422 struct got_fileindex_entry *entry;
424 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
425 apply_path_prefix(worktree, path)) == -1)
426 return got_error_from_errno();
428 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
429 GOT_DEFAULT_FILE_MODE);
431 err = got_error_from_errno();
432 if (errno == EEXIST) {
434 if (lstat(ondisk_path, &sb) == -1) {
435 err = got_error_from_errno();
436 } else if (!S_ISREG(sb.st_mode)) {
437 /* TODO file is obstructed; do something */
438 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
444 hdrlen = got_object_blob_get_hdrlen(blob);
446 const uint8_t *buf = got_object_blob_get_read_buf(blob);
447 err = got_object_blob_read_block(&len, blob);
451 /* Skip blob object header first time around. */
452 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
454 err = got_error_from_errno();
456 } else if (outlen != len - hdrlen) {
457 err = got_error(GOT_ERR_IO);
466 err = got_fileindex_entry_alloc(&entry, ondisk_path,
467 apply_path_prefix(worktree, path), blob->id.sha1,
468 worktree->base_commit_id->sha1);
472 err = got_fileindex_entry_add(fileindex, entry);
481 static const struct got_error *
482 add_dir_on_disk(struct got_worktree *worktree, const char *path)
484 const struct got_error *err = NULL;
487 if (asprintf(&abspath, "%s/%s", worktree->root_path,
488 apply_path_prefix(worktree, path)) == -1)
489 return got_error_from_errno();
491 /* XXX queue work rather than editing disk directly? */
492 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
495 if (errno != EEXIST) {
496 err = got_error_from_errno();
500 if (lstat(abspath, &sb) == -1) {
501 err = got_error_from_errno();
505 if (!S_ISDIR(sb.st_mode)) {
506 /* TODO directory is obstructed; do something */
507 return got_error(GOT_ERR_FILE_OBSTRUCTED);
516 static const struct got_error *
517 tree_checkout(struct got_worktree *, struct got_fileindex *,
518 struct got_tree_object *, const char *, struct got_repository *,
519 got_worktree_checkout_cb progress_cb, void *progress_arg,
520 got_worktree_cancel_cb cancel_cb, void *cancel_arg);
522 static const struct got_error *
523 tree_checkout_entry(struct got_worktree *worktree,
524 struct got_fileindex *fileindex, struct got_tree_entry *te,
525 const char *parent, struct got_repository *repo,
526 got_worktree_checkout_cb progress_cb, void *progress_arg,
527 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
529 const struct got_error *err = NULL;
530 struct got_object *obj = NULL;
531 struct got_blob_object *blob = NULL;
532 struct got_tree_object *tree = NULL;
534 char *progress_path = NULL;
537 if (parent[0] == '/' && parent[1] == '\0')
539 if (asprintf(&path, "%s/%s", parent, te->name) == -1)
540 return got_error_from_errno();
542 /* Skip this entry if it is outside of our path prefix. */
543 len = MIN(strlen(worktree->path_prefix), strlen(path));
544 if (strncmp(path, worktree->path_prefix, len) != 0) {
549 err = got_object_open(&obj, repo, te->id);
553 progress_path = path;
554 if (strncmp(progress_path, worktree->path_prefix, len) == 0)
555 progress_path += len;
556 (*progress_cb)(progress_arg, progress_path);
559 case GOT_OBJ_TYPE_BLOB:
560 if (strlen(worktree->path_prefix) >= strlen(path))
562 err = got_object_blob_open(&blob, repo, obj, 8192);
565 err = add_file_on_disk(worktree, fileindex, path, blob, repo);
567 case GOT_OBJ_TYPE_TREE:
568 err = got_object_tree_open(&tree, repo, obj);
571 if (strlen(worktree->path_prefix) < strlen(path)) {
572 err = add_dir_on_disk(worktree, path);
576 /* XXX infinite recursion possible */
577 err = tree_checkout(worktree, fileindex, tree, path, repo,
578 progress_cb, progress_arg, cancel_cb, cancel_arg);
586 got_object_blob_close(blob);
588 got_object_tree_close(tree);
590 got_object_close(obj);
595 static const struct got_error *
596 tree_checkout(struct got_worktree *worktree,
597 struct got_fileindex *fileindex, struct got_tree_object *tree,
598 const char *path, struct got_repository *repo,
599 got_worktree_checkout_cb progress_cb, void *progress_arg,
600 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
602 const struct got_error *err = NULL;
603 const struct got_tree_entries *entries;
604 struct got_tree_entry *te;
607 /* Skip this tree if it is outside of our path prefix. */
608 len = MIN(strlen(worktree->path_prefix), strlen(path));
609 if (strncmp(path, worktree->path_prefix, len) != 0)
612 entries = got_object_tree_get_entries(tree);
613 SIMPLEQ_FOREACH(te, &entries->head, entry) {
615 err = (*cancel_cb)(cancel_arg);
619 err = tree_checkout_entry(worktree, fileindex, te, path, repo,
620 progress_cb, progress_arg, cancel_cb, cancel_arg);
628 const struct got_error *
629 got_worktree_checkout_files(struct got_worktree *worktree,
630 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
631 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
633 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
634 struct got_commit_object *commit = NULL;
635 struct got_tree_object *tree = NULL;
636 char *fileindex_path = NULL, *new_fileindex_path = NULL;
637 struct got_fileindex *fileindex = NULL;
638 FILE *new_index = NULL;
640 err = lock_worktree(worktree, LOCK_EX);
644 fileindex = got_fileindex_alloc();
645 if (fileindex == NULL) {
646 err = got_error_from_errno();
650 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
651 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
652 err = got_error_from_errno();
653 fileindex_path = NULL;
657 err = got_opentemp_named(&new_fileindex_path, &new_index,
662 err = got_object_open_as_commit(&commit, repo,
663 worktree->base_commit_id);
667 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
671 checkout_err = tree_checkout(worktree, fileindex, tree, "/", repo,
672 progress_cb, progress_arg, cancel_cb, cancel_arg);
674 /* Try to sync the fileindex back to disk in any case. */
675 err = got_fileindex_write(fileindex, new_index);
679 if (rename(new_fileindex_path, fileindex_path) != 0) {
680 err = got_error_from_errno();
684 free(new_fileindex_path);
685 new_fileindex_path = NULL;
689 got_object_tree_close(tree);
691 got_object_commit_close(commit);
692 if (new_fileindex_path)
693 unlink(new_fileindex_path);
696 free(new_fileindex_path);
697 free(fileindex_path);
698 got_fileindex_free(fileindex);
701 unlockerr = lock_worktree(worktree, LOCK_SH);
702 if (unlockerr && err == NULL)