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, 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, 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 int version, fd = -1;
273 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
274 err = got_error_from_errno();
279 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
280 err = got_error_from_errno();
285 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
287 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
288 : got_error_from_errno());
292 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
296 version = strtonum(formatstr, 1, INT_MAX, &errstr);
298 err = got_error(GOT_ERR_WORKTREE_META);
301 if (version != GOT_WORKTREE_FORMAT_VERSION) {
302 err = got_error(GOT_ERR_WORKTREE_VERS);
306 *worktree = calloc(1, sizeof(**worktree));
307 if (*worktree == NULL) {
308 err = got_error_from_errno();
311 (*worktree)->lockfd = -1;
313 (*worktree)->root_path = strdup(path);
314 if ((*worktree)->root_path == NULL) {
315 err = got_error_from_errno();
318 err = read_meta_file(&(*worktree)->repo_path, path_got,
319 GOT_WORKTREE_REPOSITORY);
322 err = read_meta_file(&(*worktree)->path_prefix, path_got,
323 GOT_WORKTREE_PATH_PREFIX);
327 err = read_meta_file(&(*worktree)->base, path_got, GOT_WORKTREE_BASE);
331 err = read_meta_file(&(*worktree)->head_ref, path_got,
342 if (*worktree != NULL)
343 got_worktree_close(*worktree);
346 (*worktree)->lockfd = fd;
352 got_worktree_close(struct got_worktree *worktree)
354 free(worktree->root_path);
355 free(worktree->repo_path);
356 free(worktree->path_prefix);
357 free(worktree->base);
358 free(worktree->head_ref);
359 if (worktree->lockfd != -1)
360 close(worktree->lockfd);
365 got_worktree_get_repo_path(struct got_worktree *worktree)
367 return strdup(worktree->repo_path);
371 got_worktree_get_head_ref_name(struct got_worktree *worktree)
373 return strdup(worktree->head_ref);
376 static const struct got_error *
377 lock_worktree(struct got_worktree *worktree, int operation)
379 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
380 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
381 : got_error_from_errno());
386 apply_path_prefix(struct got_worktree *worktree, const char *path)
388 const char *p = path;
389 p += strlen(worktree->path_prefix);
395 static const struct got_error *
396 add_file_on_disk(struct got_worktree *worktree, struct got_fileindex *fileindex,
397 const char *path, struct got_blob_object *blob, struct got_repository *repo)
399 const struct got_error *err = NULL;
403 struct got_fileindex_entry *entry;
405 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
406 apply_path_prefix(worktree, path)) == -1)
407 return got_error_from_errno();
409 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
410 GOT_DEFAULT_FILE_MODE);
412 err = got_error_from_errno();
413 if (errno == EEXIST) {
415 if (lstat(ondisk_path, &sb) == -1) {
416 err = got_error_from_errno();
417 } else if (!S_ISREG(sb.st_mode)) {
418 /* TODO file is obstructed; do something */
419 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
425 hdrlen = got_object_blob_get_hdrlen(blob);
427 const uint8_t *buf = got_object_blob_get_read_buf(blob);
428 err = got_object_blob_read_block(&len, blob);
432 /* Skip blob object header first time around. */
433 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
435 err = got_error_from_errno();
437 } else if (outlen != len - hdrlen) {
438 err = got_error(GOT_ERR_IO);
447 err = got_fileindex_entry_alloc(&entry, ondisk_path,
448 apply_path_prefix(worktree, path), blob->id.sha1);
452 err = got_fileindex_entry_add(fileindex, entry);
461 static const struct got_error *
462 add_dir_on_disk(struct got_worktree *worktree, const char *path)
464 const struct got_error *err = NULL;
467 if (asprintf(&abspath, "%s/%s", worktree->root_path,
468 apply_path_prefix(worktree, path)) == -1)
469 return got_error_from_errno();
471 /* XXX queue work rather than editing disk directly? */
472 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
475 if (errno != EEXIST) {
476 err = got_error_from_errno();
480 if (lstat(abspath, &sb) == -1) {
481 err = got_error_from_errno();
485 if (!S_ISDIR(sb.st_mode)) {
486 /* TODO directory is obstructed; do something */
487 return got_error(GOT_ERR_FILE_OBSTRUCTED);
496 static const struct got_error *
497 tree_checkout(struct got_worktree *, struct got_fileindex *,
498 struct got_tree_object *, const char *, struct got_repository *,
499 got_worktree_checkout_cb progress_cb, void *progress_arg,
500 got_worktree_cancel_cb cancel_cb, void *cancel_arg);
502 static const struct got_error *
503 tree_checkout_entry(struct got_worktree *worktree,
504 struct got_fileindex *fileindex, struct got_tree_entry *te,
505 const char *parent, struct got_repository *repo,
506 got_worktree_checkout_cb progress_cb, void *progress_arg,
507 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
509 const struct got_error *err = NULL;
510 struct got_object *obj = NULL;
511 struct got_blob_object *blob = NULL;
512 struct got_tree_object *tree = NULL;
514 char *progress_path = NULL;
517 if (parent[0] == '/' && parent[1] == '\0')
519 if (asprintf(&path, "%s/%s", parent, te->name) == -1)
520 return got_error_from_errno();
522 /* Skip this entry if it is outside of our path prefix. */
523 len = MIN(strlen(worktree->path_prefix), strlen(path));
524 if (strncmp(path, worktree->path_prefix, len) != 0) {
529 err = got_object_open(&obj, repo, te->id);
533 progress_path = path;
534 if (strncmp(progress_path, worktree->path_prefix, len) == 0)
535 progress_path += len;
536 (*progress_cb)(progress_arg, progress_path);
539 case GOT_OBJ_TYPE_BLOB:
540 if (strlen(worktree->path_prefix) >= strlen(path))
542 err = got_object_blob_open(&blob, repo, obj, 8192);
545 err = add_file_on_disk(worktree, fileindex, path, blob, repo);
547 case GOT_OBJ_TYPE_TREE:
548 err = got_object_tree_open(&tree, repo, obj);
551 if (strlen(worktree->path_prefix) < strlen(path)) {
552 err = add_dir_on_disk(worktree, path);
556 /* XXX infinite recursion possible */
557 err = tree_checkout(worktree, fileindex, tree, path, repo,
558 progress_cb, progress_arg, cancel_cb, cancel_arg);
566 got_object_blob_close(blob);
568 got_object_tree_close(tree);
570 got_object_close(obj);
575 static const struct got_error *
576 tree_checkout(struct got_worktree *worktree,
577 struct got_fileindex *fileindex, struct got_tree_object *tree,
578 const char *path, struct got_repository *repo,
579 got_worktree_checkout_cb progress_cb, void *progress_arg,
580 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
582 const struct got_error *err = NULL;
583 const struct got_tree_entries *entries;
584 struct got_tree_entry *te;
587 /* Skip this tree if it is outside of our path prefix. */
588 len = MIN(strlen(worktree->path_prefix), strlen(path));
589 if (strncmp(path, worktree->path_prefix, len) != 0)
592 entries = got_object_tree_get_entries(tree);
593 SIMPLEQ_FOREACH(te, &entries->head, entry) {
595 err = (*cancel_cb)(cancel_arg);
599 err = tree_checkout_entry(worktree, fileindex, te, path, repo,
600 progress_cb, progress_arg, cancel_cb, cancel_arg);
608 const struct got_error *
609 got_worktree_checkout_files(struct got_worktree *worktree,
610 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
611 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
613 const struct got_error *err = NULL, *unlockerr;
614 struct got_object_id *commit_id = NULL;
615 struct got_commit_object *commit = NULL;
616 struct got_tree_object *tree = NULL;
617 char *fileindex_path = NULL, *new_fileindex_path = NULL;
618 struct got_fileindex *fileindex = NULL;
619 FILE *new_index = NULL;
621 err = lock_worktree(worktree, LOCK_EX);
625 err = got_object_resolve_id_str(&commit_id, repo, worktree->base);
629 fileindex = got_fileindex_alloc();
630 if (fileindex == NULL) {
631 err = got_error_from_errno();
635 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
636 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
637 err = got_error_from_errno();
638 fileindex_path = NULL;
642 err = got_opentemp_named(&new_fileindex_path, &new_index,
647 err = got_object_open_as_commit(&commit, repo, commit_id);
651 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
655 err = tree_checkout(worktree, fileindex, tree, "/", repo,
656 progress_cb, progress_arg, cancel_cb, cancel_arg);
660 err = got_fileindex_write(fileindex, new_index);
664 if (rename(new_fileindex_path, fileindex_path) != 0) {
665 err = got_error_from_errno();
669 free(new_fileindex_path);
670 new_fileindex_path = NULL;
674 got_object_tree_close(tree);
676 got_object_commit_close(commit);
678 if (new_fileindex_path)
679 unlink(new_fileindex_path);
682 free(new_fileindex_path);
683 free(fileindex_path);
684 got_fileindex_free(fileindex);
685 unlockerr = lock_worktree(worktree, LOCK_SH);
686 if (unlockerr && err == NULL)