2 * Copyright (c) 2018, 2019, 2020 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.
28 #include "got_cancel.h"
29 #include "got_error.h"
30 #include "got_reference.h"
32 #include "got_worktree.h"
33 #include "got_repository.h"
34 #include "got_gotconfig.h"
35 #include "got_object.h"
37 #include "got_lib_worktree.h"
38 #include "got_lib_gotconfig.h"
40 static const struct got_error *
41 read_meta_file(char **content, const char *path_got, const char *name)
43 const struct got_error *err = NULL;
51 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
52 err = got_error_from_errno("asprintf");
57 fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
60 err = got_error_path(path, GOT_ERR_WORKTREE_META);
62 err = got_error_from_errno2("open", path);
65 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
66 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
67 : got_error_from_errno2("flock", path));
71 if (fstat(fd, &sb) != 0) {
72 err = got_error_from_errno2("fstat", path);
75 *content = calloc(1, sb.st_size);
76 if (*content == NULL) {
77 err = got_error_from_errno("calloc");
81 n = read(fd, *content, sb.st_size);
82 if (n != sb.st_size) {
83 err = (n == -1 ? got_error_from_errno2("read", path) :
84 got_error_path(path, GOT_ERR_WORKTREE_META));
87 if ((*content)[sb.st_size - 1] != '\n') {
88 err = got_error_path(path, GOT_ERR_WORKTREE_META);
91 (*content)[sb.st_size - 1] = '\0';
94 if (fd != -1 && close(fd) == -1 && err == NULL)
95 err = got_error_from_errno2("close", path_got);
104 static const struct got_error *
105 open_worktree(struct got_worktree **worktree, const char *path)
107 const struct got_error *err = NULL;
109 char *formatstr = NULL;
110 char *uuidstr = NULL;
111 char *path_lock = NULL;
112 char *base_commit_id_str = NULL;
113 int version, fd = -1;
115 struct got_repository *repo = NULL;
116 int *pack_fds = NULL;
117 uint32_t uuid_status;
121 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
122 err = got_error_from_errno("asprintf");
127 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
128 err = got_error_from_errno("asprintf");
133 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK | O_CLOEXEC);
135 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
136 : got_error_from_errno2("open", path_lock));
140 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
144 version = strtonum(formatstr, 1, INT_MAX, &errstr);
146 err = got_error_msg(GOT_ERR_WORKTREE_META,
147 "could not parse work tree format version number");
150 if (version != GOT_WORKTREE_FORMAT_VERSION) {
151 err = got_error(GOT_ERR_WORKTREE_VERS);
155 *worktree = calloc(1, sizeof(**worktree));
156 if (*worktree == NULL) {
157 err = got_error_from_errno("calloc");
160 (*worktree)->lockfd = -1;
162 (*worktree)->root_path = realpath(path, NULL);
163 if ((*worktree)->root_path == NULL) {
164 err = got_error_from_errno2("realpath", path);
167 err = read_meta_file(&(*worktree)->repo_path, path_got,
168 GOT_WORKTREE_REPOSITORY);
172 err = read_meta_file(&(*worktree)->path_prefix, path_got,
173 GOT_WORKTREE_PATH_PREFIX);
177 err = read_meta_file(&base_commit_id_str, path_got,
178 GOT_WORKTREE_BASE_COMMIT);
182 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
185 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
186 if (uuid_status != uuid_s_ok) {
187 err = got_error_uuid(uuid_status, "uuid_from_string");
191 err = got_repo_pack_fds_open(&pack_fds);
195 err = got_repo_open(&repo, (*worktree)->repo_path, NULL, pack_fds);
199 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
204 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
205 GOT_WORKTREE_HEAD_REF);
209 if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
210 (*worktree)->root_path,
211 GOT_WORKTREE_GOT_DIR, GOT_GOTCONFIG_FILENAME) == -1) {
212 err = got_error_from_errno("asprintf");
216 err = got_gotconfig_read(&(*worktree)->gotconfig,
217 (*worktree)->gotconfig_path);
219 (*worktree)->root_fd = open((*worktree)->root_path,
220 O_DIRECTORY | O_CLOEXEC);
221 if ((*worktree)->root_fd == -1) {
222 err = got_error_from_errno2("open", (*worktree)->root_path);
227 const struct got_error *close_err = got_repo_close(repo);
232 const struct got_error *pack_err =
233 got_repo_pack_fds_close(pack_fds);
239 free(base_commit_id_str);
245 if (*worktree != NULL)
246 got_worktree_close(*worktree);
249 (*worktree)->lockfd = fd;
254 const struct got_error *
255 got_worktree_open(struct got_worktree **worktree, const char *path)
257 const struct got_error *err = NULL;
260 worktree_path = strdup(path);
261 if (worktree_path == NULL)
262 return got_error_from_errno("strdup");
267 err = open_worktree(worktree, worktree_path);
268 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
276 if (worktree_path[0] == '/' && worktree_path[1] == '\0')
278 err = got_path_dirname(&parent_path, worktree_path);
280 if (err->code != GOT_ERR_BAD_PATH) {
287 worktree_path = parent_path;
291 return got_error(GOT_ERR_NOT_WORKTREE);
294 const struct got_error *
295 got_worktree_close(struct got_worktree *worktree)
297 const struct got_error *err = NULL;
299 if (worktree->lockfd != -1) {
300 if (close(worktree->lockfd) == -1)
301 err = got_error_from_errno2("close",
302 got_worktree_get_root_path(worktree));
304 if (close(worktree->root_fd) == -1 && err == NULL)
305 err = got_error_from_errno2("close",
306 got_worktree_get_root_path(worktree));
307 free(worktree->repo_path);
308 free(worktree->path_prefix);
309 free(worktree->base_commit_id);
310 free(worktree->head_ref_name);
311 free(worktree->root_path);
312 free(worktree->gotconfig_path);
313 got_gotconfig_free(worktree->gotconfig);
319 got_worktree_get_root_path(struct got_worktree *worktree)
321 return worktree->root_path;
325 got_worktree_get_repo_path(struct got_worktree *worktree)
327 return worktree->repo_path;
331 got_worktree_get_path_prefix(struct got_worktree *worktree)
333 return worktree->path_prefix;