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;
57 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
58 err = got_error_from_errno();
63 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
64 GOT_DEFAULT_FILE_MODE);
66 err = got_error_from_errno();
71 int len = dprintf(fd, "%s\n", content);
72 if (len != strlen(content) + 1) {
73 err = got_error_from_errno();
79 if (fd != -1 && close(fd) == -1 && err == NULL)
80 err = got_error_from_errno();
85 static const struct got_error *
86 read_meta_file(char **content, const char *path_got, const char *name)
88 const struct got_error *err = NULL;
96 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
97 err = got_error_from_errno();
102 fd = open(path, O_RDONLY | O_NOFOLLOW);
104 err = got_error_from_errno();
107 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
108 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
109 : got_error_from_errno());
114 *content = calloc(1, sb.st_size);
115 if (*content == NULL) {
116 err = got_error_from_errno();
120 n = read(fd, *content, sb.st_size);
121 if (n != sb.st_size) {
122 err = (n == -1 ? got_error_from_errno() :
123 got_error(GOT_ERR_WORKTREE_META));
126 if ((*content)[sb.st_size - 1] != '\n') {
127 err = got_error(GOT_ERR_WORKTREE_META);
130 (*content)[sb.st_size - 1] = '\0';
133 if (fd != -1 && close(fd) == -1 && err == NULL)
134 err = got_error_from_errno();
143 const struct got_error *
144 got_worktree_init(const char *path, struct got_reference *head_ref,
145 const char *prefix, struct got_repository *repo)
147 const struct got_error *err = NULL;
148 struct got_object_id *commit_id = NULL;
150 char *path_got = NULL;
152 char *repo_path = NULL;
153 char *formatstr = NULL;
154 char *absprefix = NULL;
155 char *basestr = NULL;
157 err = got_ref_resolve(&commit_id, repo, head_ref);
160 err = got_object_get_type(&obj_type, repo, commit_id);
163 if (obj_type != GOT_OBJ_TYPE_COMMIT)
164 return got_error(GOT_ERR_OBJ_TYPE);
166 if (!got_path_is_absolute(prefix)) {
167 if (asprintf(&absprefix, "/%s", prefix) == -1)
168 return got_error_from_errno();
171 /* Create top-level directory (may already exist). */
172 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
173 err = got_error_from_errno();
177 /* Create .got directory (may already exist). */
178 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
179 err = got_error_from_errno();
182 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
183 err = got_error_from_errno();
187 /* Create an empty lock file. */
188 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
192 /* Create an empty file index. */
193 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
197 /* Write the HEAD reference. */
198 refstr = got_ref_to_str(head_ref);
199 if (refstr == NULL) {
200 err = got_error_from_errno();
203 err = create_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
207 /* Record our base commit. */
208 err = got_object_id_str(&basestr, commit_id);
211 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
215 /* Store path to repository. */
216 repo_path = got_repo_get_path(repo);
217 if (repo_path == NULL) {
218 err = got_error_from_errno();
221 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY, repo_path);
225 /* Store in-repository path prefix. */
226 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
227 absprefix ? absprefix : prefix);
231 /* Stamp work tree with format file. */
232 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
233 err = got_error_from_errno();
236 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
251 const struct got_error *
252 got_worktree_open(struct got_worktree **worktree, const char *path)
254 const struct got_error *err = NULL;
256 char *formatstr = NULL;
257 char *path_lock = NULL;
258 char *base_commit_id_str = NULL;
259 char *head_ref_str = NULL;
260 int version, fd = -1;
262 struct got_repository *repo = NULL;
266 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
267 err = got_error_from_errno();
272 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
273 err = got_error_from_errno();
278 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
280 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
281 : got_error_from_errno());
285 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
289 version = strtonum(formatstr, 1, INT_MAX, &errstr);
291 err = got_error(GOT_ERR_WORKTREE_META);
294 if (version != GOT_WORKTREE_FORMAT_VERSION) {
295 err = got_error(GOT_ERR_WORKTREE_VERS);
299 *worktree = calloc(1, sizeof(**worktree));
300 if (*worktree == NULL) {
301 err = got_error_from_errno();
304 (*worktree)->lockfd = -1;
306 (*worktree)->root_path = strdup(path);
307 if ((*worktree)->root_path == NULL) {
308 err = got_error_from_errno();
311 err = read_meta_file(&(*worktree)->repo_path, path_got,
312 GOT_WORKTREE_REPOSITORY);
316 err = read_meta_file(&(*worktree)->path_prefix, path_got,
317 GOT_WORKTREE_PATH_PREFIX);
321 err = read_meta_file(&base_commit_id_str, path_got,
322 GOT_WORKTREE_BASE_COMMIT);
326 err = got_repo_open(&repo, (*worktree)->repo_path);
330 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
335 err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
339 err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
342 got_repo_close(repo);
346 free(base_commit_id_str);
350 if (*worktree != NULL)
351 got_worktree_close(*worktree);
354 (*worktree)->lockfd = fd;
360 got_worktree_close(struct got_worktree *worktree)
362 free(worktree->root_path);
363 free(worktree->repo_path);
364 free(worktree->path_prefix);
365 free(worktree->base_commit_id);
366 if (worktree->head_ref)
367 got_ref_close(worktree->head_ref);
368 if (worktree->lockfd != -1)
369 close(worktree->lockfd);
374 got_worktree_get_repo_path(struct got_worktree *worktree)
376 return strdup(worktree->repo_path);
380 got_worktree_get_head_ref_name(struct got_worktree *worktree)
382 return got_ref_to_str(worktree->head_ref);
385 static const struct got_error *
386 lock_worktree(struct got_worktree *worktree, int operation)
388 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
389 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
390 : got_error_from_errno());
395 apply_path_prefix(struct got_worktree *worktree, const char *path)
397 const char *p = path;
398 p += strlen(worktree->path_prefix);
404 static const struct got_error *
405 blob_checkout(struct got_worktree *worktree, struct got_fileindex *fileindex,
406 struct got_fileindex_entry *entry, const char *path,
407 struct got_blob_object *blob, struct got_repository *repo,
408 got_worktree_checkout_cb progress_cb, void *progress_arg,
409 const char *progress_path)
411 const struct got_error *err = NULL;
416 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
417 apply_path_prefix(worktree, path)) == -1)
418 return got_error_from_errno();
420 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
421 GOT_DEFAULT_FILE_MODE);
423 err = got_error_from_errno();
424 if (errno == EEXIST) {
426 if (lstat(ondisk_path, &sb) == -1) {
427 err = got_error_from_errno();
428 } else if (!S_ISREG(sb.st_mode)) {
429 /* TODO file is obstructed; do something */
430 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
432 /* TODO: Merge the file! */
433 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
441 (*progress_cb)(progress_arg, GOT_STATUS_ADD, progress_path);
443 hdrlen = got_object_blob_get_hdrlen(blob);
445 const uint8_t *buf = got_object_blob_get_read_buf(blob);
446 err = got_object_blob_read_block(&len, blob);
450 /* Skip blob object header first time around. */
451 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
453 err = got_error_from_errno();
455 } else if (outlen != len - hdrlen) {
456 err = got_error(GOT_ERR_IO);
466 err = got_fileindex_entry_update(entry, ondisk_path,
467 blob->id.sha1, worktree->base_commit_id->sha1);
469 err = got_fileindex_entry_alloc(&entry, ondisk_path,
470 apply_path_prefix(worktree, path), blob->id.sha1,
471 worktree->base_commit_id->sha1);
474 err = got_fileindex_entry_add(fileindex, entry);
484 static const struct got_error *
485 add_dir_on_disk(struct got_worktree *worktree, const char *path)
487 const struct got_error *err = NULL;
490 if (asprintf(&abspath, "%s/%s", worktree->root_path,
491 apply_path_prefix(worktree, path)) == -1)
492 return got_error_from_errno();
494 /* XXX queue work rather than editing disk directly? */
495 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
498 if (errno != EEXIST) {
499 err = got_error_from_errno();
503 if (lstat(abspath, &sb) == -1) {
504 err = got_error_from_errno();
508 if (!S_ISDIR(sb.st_mode)) {
509 /* TODO directory is obstructed; do something */
510 return got_error(GOT_ERR_FILE_OBSTRUCTED);
519 static const struct got_error *
520 tree_checkout(struct got_worktree *, struct got_fileindex *,
521 struct got_tree_object *, const char *, struct got_repository *,
522 got_worktree_checkout_cb progress_cb, void *progress_arg,
523 got_worktree_cancel_cb cancel_cb, void *cancel_arg);
525 static const struct got_error *
526 tree_checkout_entry(struct got_worktree *worktree,
527 struct got_fileindex *fileindex, struct got_tree_entry *te,
528 const char *parent, struct got_repository *repo,
529 got_worktree_checkout_cb progress_cb, void *progress_arg,
530 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
532 const struct got_error *err = NULL;
533 struct got_object *obj = NULL;
534 struct got_blob_object *blob = NULL;
535 struct got_fileindex_entry *entry = NULL;
536 struct got_tree_object *tree = NULL;
538 char *progress_path = NULL;
541 if (parent[0] == '/' && parent[1] == '\0')
543 if (asprintf(&path, "%s/%s", parent, te->name) == -1)
544 return got_error_from_errno();
546 /* Skip this entry if it is outside of our path prefix. */
547 len = MIN(strlen(worktree->path_prefix), strlen(path));
548 if (strncmp(path, worktree->path_prefix, len) != 0) {
553 err = got_object_open(&obj, repo, te->id);
557 progress_path = path;
558 if (strncmp(progress_path, worktree->path_prefix, len) == 0)
559 progress_path += len;
562 case GOT_OBJ_TYPE_BLOB:
563 if (strlen(worktree->path_prefix) >= strlen(path))
565 entry = got_fileindex_entry_get(fileindex,
566 apply_path_prefix(worktree, path));
568 memcmp(entry->commit_sha1, worktree->base_commit_id->sha1,
569 SHA1_DIGEST_LENGTH) == 0)
570 break; /* file already checked out */
571 err = got_object_blob_open(&blob, repo, obj, 8192);
574 err = blob_checkout(worktree, fileindex, entry, path, blob,
575 repo, progress_cb, progress_arg, progress_path);
577 case GOT_OBJ_TYPE_TREE:
578 if (strlen(worktree->path_prefix) < strlen(path)) {
579 err = add_dir_on_disk(worktree, path);
583 err = got_object_tree_open(&tree, repo, obj);
586 /* XXX infinite recursion possible */
587 err = tree_checkout(worktree, fileindex, tree, path, repo,
588 progress_cb, progress_arg, cancel_cb, cancel_arg);
596 got_object_blob_close(blob);
598 got_object_tree_close(tree);
600 got_object_close(obj);
605 static const struct got_error *
606 tree_checkout(struct got_worktree *worktree,
607 struct got_fileindex *fileindex, struct got_tree_object *tree,
608 const char *path, struct got_repository *repo,
609 got_worktree_checkout_cb progress_cb, void *progress_arg,
610 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
612 const struct got_error *err = NULL;
613 const struct got_tree_entries *entries;
614 struct got_tree_entry *te;
617 /* Skip this tree if it is outside of our path prefix. */
618 len = MIN(strlen(worktree->path_prefix), strlen(path));
619 if (strncmp(path, worktree->path_prefix, len) != 0)
622 entries = got_object_tree_get_entries(tree);
623 SIMPLEQ_FOREACH(te, &entries->head, entry) {
625 err = (*cancel_cb)(cancel_arg);
629 err = tree_checkout_entry(worktree, fileindex, te, path, repo,
630 progress_cb, progress_arg, cancel_cb, cancel_arg);
638 const struct got_error *
639 got_worktree_checkout_files(struct got_worktree *worktree,
640 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
641 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
643 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
644 struct got_commit_object *commit = NULL;
645 struct got_tree_object *tree = NULL;
646 char *fileindex_path = NULL, *new_fileindex_path = NULL;
647 struct got_fileindex *fileindex = NULL;
648 FILE *index = NULL, *new_index = NULL;
650 err = lock_worktree(worktree, LOCK_EX);
654 fileindex = got_fileindex_alloc();
655 if (fileindex == NULL) {
656 err = got_error_from_errno();
660 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
661 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
662 err = got_error_from_errno();
663 fileindex_path = NULL;
668 * Read the file index.
669 * Checking out files is supposed to be an idempotent operation.
670 * If the on-disk file index is incomplete we will try to complete it.
672 index = fopen(fileindex_path, "rb");
674 err = got_error_from_errno();
677 err = got_fileindex_read(fileindex, index);
682 err = got_opentemp_named(&new_fileindex_path, &new_index,
687 err = got_object_open_as_commit(&commit, repo,
688 worktree->base_commit_id);
692 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
696 checkout_err = tree_checkout(worktree, fileindex, tree, "/", repo,
697 progress_cb, progress_arg, cancel_cb, cancel_arg);
699 /* Try to sync the fileindex back to disk in any case. */
700 err = got_fileindex_write(fileindex, new_index);
704 if (rename(new_fileindex_path, fileindex_path) != 0) {
705 err = got_error_from_errno();
709 free(new_fileindex_path);
710 new_fileindex_path = NULL;
714 got_object_tree_close(tree);
716 got_object_commit_close(commit);
717 if (new_fileindex_path)
718 unlink(new_fileindex_path);
721 free(new_fileindex_path);
722 free(fileindex_path);
723 got_fileindex_free(fileindex);
726 unlockerr = lock_worktree(worktree, LOCK_SH);
727 if (unlockerr && err == NULL)