2 1ea7ccc6 2021-11-15 thomas * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 1ea7ccc6 2021-11-15 thomas * Permission to use, copy, modify, and distribute this software for any
5 1ea7ccc6 2021-11-15 thomas * purpose with or without fee is hereby granted, provided that the above
6 1ea7ccc6 2021-11-15 thomas * copyright notice and this permission notice appear in all copies.
8 1ea7ccc6 2021-11-15 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 1ea7ccc6 2021-11-15 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 1ea7ccc6 2021-11-15 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 1ea7ccc6 2021-11-15 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 1ea7ccc6 2021-11-15 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 1ea7ccc6 2021-11-15 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 1ea7ccc6 2021-11-15 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
19 1ea7ccc6 2021-11-15 thomas #include <sys/stat.h>
20 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
22 1ea7ccc6 2021-11-15 thomas #include <errno.h>
23 1ea7ccc6 2021-11-15 thomas #include <fcntl.h>
24 1ea7ccc6 2021-11-15 thomas #include <limits.h>
25 1ea7ccc6 2021-11-15 thomas #include <stddef.h>
26 1ea7ccc6 2021-11-15 thomas #include <stdio.h>
27 1ea7ccc6 2021-11-15 thomas #include <stdlib.h>
28 1ea7ccc6 2021-11-15 thomas #include <string.h>
29 1ea7ccc6 2021-11-15 thomas #include <unistd.h>
31 1ea7ccc6 2021-11-15 thomas #include "got_cancel.h"
32 1ea7ccc6 2021-11-15 thomas #include "got_error.h"
33 1ea7ccc6 2021-11-15 thomas #include "got_reference.h"
34 1ea7ccc6 2021-11-15 thomas #include "got_path.h"
35 1ea7ccc6 2021-11-15 thomas #include "got_worktree.h"
36 1ea7ccc6 2021-11-15 thomas #include "got_repository.h"
37 1ea7ccc6 2021-11-15 thomas #include "got_gotconfig.h"
38 1ea7ccc6 2021-11-15 thomas #include "got_object.h"
40 1ea7ccc6 2021-11-15 thomas #include "got_lib_worktree.h"
41 1ea7ccc6 2021-11-15 thomas #include "got_lib_gotconfig.h"
43 ad10f64e 2023-07-19 thomas #ifndef nitems
44 ad10f64e 2023-07-19 thomas #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
47 1ea7ccc6 2021-11-15 thomas static const struct got_error *
48 1ea7ccc6 2021-11-15 thomas read_meta_file(char **content, const char *path_got, const char *name)
50 1ea7ccc6 2021-11-15 thomas const struct got_error *err = NULL;
51 1ea7ccc6 2021-11-15 thomas char *path;
52 1ea7ccc6 2021-11-15 thomas int fd = -1;
53 1ea7ccc6 2021-11-15 thomas ssize_t n;
54 1ea7ccc6 2021-11-15 thomas struct stat sb;
56 1ea7ccc6 2021-11-15 thomas *content = NULL;
58 1ea7ccc6 2021-11-15 thomas if (asprintf(&path, "%s/%s", path_got, name) == -1) {
59 1ea7ccc6 2021-11-15 thomas err = got_error_from_errno("asprintf");
60 1ea7ccc6 2021-11-15 thomas path = NULL;
61 1ea7ccc6 2021-11-15 thomas goto done;
64 06340621 2021-12-31 thomas fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
65 1ea7ccc6 2021-11-15 thomas if (fd == -1) {
66 1ea7ccc6 2021-11-15 thomas if (errno == ENOENT)
67 1ea7ccc6 2021-11-15 thomas err = got_error_path(path, GOT_ERR_WORKTREE_META);
69 1ea7ccc6 2021-11-15 thomas err = got_error_from_errno2("open", path);
70 1ea7ccc6 2021-11-15 thomas goto done;
72 1ea7ccc6 2021-11-15 thomas if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
73 1ea7ccc6 2021-11-15 thomas err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
74 1ea7ccc6 2021-11-15 thomas : got_error_from_errno2("flock", path));
75 1ea7ccc6 2021-11-15 thomas goto done;
78 1ea7ccc6 2021-11-15 thomas if (fstat(fd, &sb) != 0) {
79 1ea7ccc6 2021-11-15 thomas err = got_error_from_errno2("fstat", path);
80 a7cf3076 2023-12-30 thomas goto done;
82 a7cf3076 2023-12-30 thomas if (sb.st_size == 0) {
83 a7cf3076 2023-12-30 thomas err = got_error_path(path, GOT_ERR_WORKTREE_META);
84 1ea7ccc6 2021-11-15 thomas goto done;
86 1ea7ccc6 2021-11-15 thomas *content = calloc(1, sb.st_size);
87 1ea7ccc6 2021-11-15 thomas if (*content == NULL) {
88 1ea7ccc6 2021-11-15 thomas err = got_error_from_errno("calloc");
89 1ea7ccc6 2021-11-15 thomas goto done;
92 1ea7ccc6 2021-11-15 thomas n = read(fd, *content, sb.st_size);
93 1ea7ccc6 2021-11-15 thomas if (n != sb.st_size) {
94 1ea7ccc6 2021-11-15 thomas err = (n == -1 ? got_error_from_errno2("read", path) :
95 1ea7ccc6 2021-11-15 thomas got_error_path(path, GOT_ERR_WORKTREE_META));
96 1ea7ccc6 2021-11-15 thomas goto done;
98 1ea7ccc6 2021-11-15 thomas if ((*content)[sb.st_size - 1] != '\n') {
99 1ea7ccc6 2021-11-15 thomas err = got_error_path(path, GOT_ERR_WORKTREE_META);
100 1ea7ccc6 2021-11-15 thomas goto done;
102 1ea7ccc6 2021-11-15 thomas (*content)[sb.st_size - 1] = '\0';
105 1ea7ccc6 2021-11-15 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
106 1ea7ccc6 2021-11-15 thomas err = got_error_from_errno2("close", path_got);
107 1ea7ccc6 2021-11-15 thomas free(path);
108 1ea7ccc6 2021-11-15 thomas if (err) {
109 1ea7ccc6 2021-11-15 thomas free(*content);
110 1ea7ccc6 2021-11-15 thomas *content = NULL;
112 1ea7ccc6 2021-11-15 thomas return err;
115 1ea7ccc6 2021-11-15 thomas static const struct got_error *
116 ad10f64e 2023-07-19 thomas open_worktree(struct got_worktree **worktree, const char *path,
117 ad10f64e 2023-07-19 thomas const char *meta_dir)
119 1ea7ccc6 2021-11-15 thomas const struct got_error *err = NULL;
120 ad10f64e 2023-07-19 thomas char *path_meta;
121 1ea7ccc6 2021-11-15 thomas char *formatstr = NULL;
122 1ea7ccc6 2021-11-15 thomas char *uuidstr = NULL;
123 1ea7ccc6 2021-11-15 thomas char *path_lock = NULL;
124 1ea7ccc6 2021-11-15 thomas char *base_commit_id_str = NULL;
125 1ea7ccc6 2021-11-15 thomas int version, fd = -1;
126 1ea7ccc6 2021-11-15 thomas const char *errstr;
127 1ea7ccc6 2021-11-15 thomas struct got_repository *repo = NULL;
128 b6652669 2022-06-23 thomas int *pack_fds = NULL;
129 1ea7ccc6 2021-11-15 thomas uint32_t uuid_status;
131 1ea7ccc6 2021-11-15 thomas *worktree = NULL;
133 ad10f64e 2023-07-19 thomas if (asprintf(&path_meta, "%s/%s", path, meta_dir) == -1) {
134 1ea7ccc6 2021-11-15 thomas err = got_error_from_errno("asprintf");
135 ad10f64e 2023-07-19 thomas path_meta = NULL;
136 1ea7ccc6 2021-11-15 thomas goto done;
139 ad10f64e 2023-07-19 thomas if (asprintf(&path_lock, "%s/%s", path_meta, GOT_WORKTREE_LOCK) == -1) {
140 1ea7ccc6 2021-11-15 thomas err = got_error_from_errno("asprintf");
141 1ea7ccc6 2021-11-15 thomas path_lock = NULL;
142 1ea7ccc6 2021-11-15 thomas goto done;
145 06340621 2021-12-31 thomas fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK | O_CLOEXEC);
146 1ea7ccc6 2021-11-15 thomas if (fd == -1) {
147 1ea7ccc6 2021-11-15 thomas err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
148 1ea7ccc6 2021-11-15 thomas : got_error_from_errno2("open", path_lock));
149 1ea7ccc6 2021-11-15 thomas goto done;
152 ad10f64e 2023-07-19 thomas err = read_meta_file(&formatstr, path_meta, GOT_WORKTREE_FORMAT);
154 1ea7ccc6 2021-11-15 thomas goto done;
156 1ea7ccc6 2021-11-15 thomas version = strtonum(formatstr, 1, INT_MAX, &errstr);
157 1ea7ccc6 2021-11-15 thomas if (errstr) {
158 1ea7ccc6 2021-11-15 thomas err = got_error_msg(GOT_ERR_WORKTREE_META,
159 1ea7ccc6 2021-11-15 thomas "could not parse work tree format version number");
160 1ea7ccc6 2021-11-15 thomas goto done;
162 1ea7ccc6 2021-11-15 thomas if (version != GOT_WORKTREE_FORMAT_VERSION) {
163 1ea7ccc6 2021-11-15 thomas err = got_error(GOT_ERR_WORKTREE_VERS);
164 1ea7ccc6 2021-11-15 thomas goto done;
167 1ea7ccc6 2021-11-15 thomas *worktree = calloc(1, sizeof(**worktree));
168 1ea7ccc6 2021-11-15 thomas if (*worktree == NULL) {
169 1ea7ccc6 2021-11-15 thomas err = got_error_from_errno("calloc");
170 1ea7ccc6 2021-11-15 thomas goto done;
172 1ea7ccc6 2021-11-15 thomas (*worktree)->lockfd = -1;
174 1ea7ccc6 2021-11-15 thomas (*worktree)->root_path = realpath(path, NULL);
175 1ea7ccc6 2021-11-15 thomas if ((*worktree)->root_path == NULL) {
176 1ea7ccc6 2021-11-15 thomas err = got_error_from_errno2("realpath", path);
177 1ea7ccc6 2021-11-15 thomas goto done;
179 ad10f64e 2023-07-19 thomas (*worktree)->meta_dir = meta_dir;
180 ad10f64e 2023-07-19 thomas err = read_meta_file(&(*worktree)->repo_path, path_meta,
181 1ea7ccc6 2021-11-15 thomas GOT_WORKTREE_REPOSITORY);
183 1ea7ccc6 2021-11-15 thomas goto done;
185 ad10f64e 2023-07-19 thomas err = read_meta_file(&(*worktree)->path_prefix, path_meta,
186 1ea7ccc6 2021-11-15 thomas GOT_WORKTREE_PATH_PREFIX);
188 1ea7ccc6 2021-11-15 thomas goto done;
190 ad10f64e 2023-07-19 thomas err = read_meta_file(&base_commit_id_str, path_meta,
191 1ea7ccc6 2021-11-15 thomas GOT_WORKTREE_BASE_COMMIT);
193 1ea7ccc6 2021-11-15 thomas goto done;
195 ad10f64e 2023-07-19 thomas err = read_meta_file(&uuidstr, path_meta, GOT_WORKTREE_UUID);
197 1ea7ccc6 2021-11-15 thomas goto done;
198 1ea7ccc6 2021-11-15 thomas uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
199 1ea7ccc6 2021-11-15 thomas if (uuid_status != uuid_s_ok) {
200 1ea7ccc6 2021-11-15 thomas err = got_error_uuid(uuid_status, "uuid_from_string");
201 1ea7ccc6 2021-11-15 thomas goto done;
204 b6652669 2022-06-23 thomas err = got_repo_pack_fds_open(&pack_fds);
206 1ea7ccc6 2021-11-15 thomas goto done;
208 b6652669 2022-06-23 thomas err = got_repo_open(&repo, (*worktree)->repo_path, NULL, pack_fds);
210 7cd52833 2022-06-23 thomas goto done;
212 1ea7ccc6 2021-11-15 thomas err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
213 1ea7ccc6 2021-11-15 thomas base_commit_id_str);
215 1ea7ccc6 2021-11-15 thomas goto done;
217 ad10f64e 2023-07-19 thomas err = read_meta_file(&(*worktree)->head_ref_name, path_meta,
218 1ea7ccc6 2021-11-15 thomas GOT_WORKTREE_HEAD_REF);
220 1ea7ccc6 2021-11-15 thomas goto done;
222 1ea7ccc6 2021-11-15 thomas if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
223 ad10f64e 2023-07-19 thomas (*worktree)->root_path, (*worktree)->meta_dir,
224 ad10f64e 2023-07-19 thomas GOT_GOTCONFIG_FILENAME) == -1) {
225 1ea7ccc6 2021-11-15 thomas err = got_error_from_errno("asprintf");
226 1ea7ccc6 2021-11-15 thomas goto done;
229 1ea7ccc6 2021-11-15 thomas err = got_gotconfig_read(&(*worktree)->gotconfig,
230 1ea7ccc6 2021-11-15 thomas (*worktree)->gotconfig_path);
232 1b2f32fb 2022-10-27 thomas goto done;
234 06340621 2021-12-31 thomas (*worktree)->root_fd = open((*worktree)->root_path,
235 06340621 2021-12-31 thomas O_DIRECTORY | O_CLOEXEC);
236 1ea7ccc6 2021-11-15 thomas if ((*worktree)->root_fd == -1) {
237 1ea7ccc6 2021-11-15 thomas err = got_error_from_errno2("open", (*worktree)->root_path);
238 1ea7ccc6 2021-11-15 thomas goto done;
241 1ea7ccc6 2021-11-15 thomas if (repo) {
242 1ea7ccc6 2021-11-15 thomas const struct got_error *close_err = got_repo_close(repo);
243 1ea7ccc6 2021-11-15 thomas if (err == NULL)
244 1ea7ccc6 2021-11-15 thomas err = close_err;
246 b6652669 2022-06-23 thomas if (pack_fds) {
247 b6652669 2022-06-23 thomas const struct got_error *pack_err =
248 b6652669 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
249 b6652669 2022-06-23 thomas if (err == NULL)
250 b6652669 2022-06-23 thomas err = pack_err;
252 ad10f64e 2023-07-19 thomas free(path_meta);
253 1ea7ccc6 2021-11-15 thomas free(path_lock);
254 1ea7ccc6 2021-11-15 thomas free(base_commit_id_str);
255 1ea7ccc6 2021-11-15 thomas free(uuidstr);
256 1ea7ccc6 2021-11-15 thomas free(formatstr);
257 1ea7ccc6 2021-11-15 thomas if (err) {
258 1ea7ccc6 2021-11-15 thomas if (fd != -1)
259 1ea7ccc6 2021-11-15 thomas close(fd);
260 1ea7ccc6 2021-11-15 thomas if (*worktree != NULL)
261 1ea7ccc6 2021-11-15 thomas got_worktree_close(*worktree);
262 1ea7ccc6 2021-11-15 thomas *worktree = NULL;
264 1ea7ccc6 2021-11-15 thomas (*worktree)->lockfd = fd;
266 1ea7ccc6 2021-11-15 thomas return err;
269 1ea7ccc6 2021-11-15 thomas const struct got_error *
270 ad10f64e 2023-07-19 thomas got_worktree_open(struct got_worktree **worktree, const char *path,
271 ad10f64e 2023-07-19 thomas const char *meta_dir)
273 1ea7ccc6 2021-11-15 thomas const struct got_error *err = NULL;
274 1ea7ccc6 2021-11-15 thomas char *worktree_path;
275 ad10f64e 2023-07-19 thomas const char *meta_dirs[] = {
276 ad10f64e 2023-07-19 thomas GOT_WORKTREE_GOT_DIR,
277 ad10f64e 2023-07-19 thomas GOT_WORKTREE_CVG_DIR
281 1ea7ccc6 2021-11-15 thomas worktree_path = strdup(path);
282 1ea7ccc6 2021-11-15 thomas if (worktree_path == NULL)
283 1ea7ccc6 2021-11-15 thomas return got_error_from_errno("strdup");
285 1ea7ccc6 2021-11-15 thomas for (;;) {
286 1ea7ccc6 2021-11-15 thomas char *parent_path;
288 ad10f64e 2023-07-19 thomas if (meta_dir == NULL) {
289 ad10f64e 2023-07-19 thomas for (i = 0; i < nitems(meta_dirs); i++) {
290 ad10f64e 2023-07-19 thomas err = open_worktree(worktree, worktree_path,
291 ad10f64e 2023-07-19 thomas meta_dirs[i]);
292 012b711c 2023-09-05 thomas if (err == NULL ||
293 012b711c 2023-09-05 thomas err->code == GOT_ERR_WORKTREE_BUSY)
297 ad10f64e 2023-07-19 thomas err = open_worktree(worktree, worktree_path, meta_dir);
298 1ea7ccc6 2021-11-15 thomas if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
299 1ea7ccc6 2021-11-15 thomas free(worktree_path);
300 1ea7ccc6 2021-11-15 thomas return err;
302 1ea7ccc6 2021-11-15 thomas if (*worktree) {
303 1ea7ccc6 2021-11-15 thomas free(worktree_path);
304 1ea7ccc6 2021-11-15 thomas return NULL;
306 1ea7ccc6 2021-11-15 thomas if (worktree_path[0] == '/' && worktree_path[1] == '\0')
308 1ea7ccc6 2021-11-15 thomas err = got_path_dirname(&parent_path, worktree_path);
309 1ea7ccc6 2021-11-15 thomas if (err) {
310 1ea7ccc6 2021-11-15 thomas if (err->code != GOT_ERR_BAD_PATH) {
311 1ea7ccc6 2021-11-15 thomas free(worktree_path);
312 1ea7ccc6 2021-11-15 thomas return err;
316 1ea7ccc6 2021-11-15 thomas free(worktree_path);
317 1ea7ccc6 2021-11-15 thomas worktree_path = parent_path;
320 1ea7ccc6 2021-11-15 thomas free(worktree_path);
321 1ea7ccc6 2021-11-15 thomas return got_error(GOT_ERR_NOT_WORKTREE);
324 1ea7ccc6 2021-11-15 thomas const struct got_error *
325 1ea7ccc6 2021-11-15 thomas got_worktree_close(struct got_worktree *worktree)
327 1ea7ccc6 2021-11-15 thomas const struct got_error *err = NULL;
329 1ea7ccc6 2021-11-15 thomas if (worktree->lockfd != -1) {
330 1ea7ccc6 2021-11-15 thomas if (close(worktree->lockfd) == -1)
331 1ea7ccc6 2021-11-15 thomas err = got_error_from_errno2("close",
332 1ea7ccc6 2021-11-15 thomas got_worktree_get_root_path(worktree));
334 1ea7ccc6 2021-11-15 thomas if (close(worktree->root_fd) == -1 && err == NULL)
335 1ea7ccc6 2021-11-15 thomas err = got_error_from_errno2("close",
336 1ea7ccc6 2021-11-15 thomas got_worktree_get_root_path(worktree));
337 1ea7ccc6 2021-11-15 thomas free(worktree->repo_path);
338 1ea7ccc6 2021-11-15 thomas free(worktree->path_prefix);
339 1ea7ccc6 2021-11-15 thomas free(worktree->base_commit_id);
340 1ea7ccc6 2021-11-15 thomas free(worktree->head_ref_name);
341 1ea7ccc6 2021-11-15 thomas free(worktree->root_path);
342 1ea7ccc6 2021-11-15 thomas free(worktree->gotconfig_path);
343 1ea7ccc6 2021-11-15 thomas got_gotconfig_free(worktree->gotconfig);
344 1ea7ccc6 2021-11-15 thomas free(worktree);
345 1ea7ccc6 2021-11-15 thomas return err;
348 1ea7ccc6 2021-11-15 thomas const char *
349 1ea7ccc6 2021-11-15 thomas got_worktree_get_root_path(struct got_worktree *worktree)
351 1ea7ccc6 2021-11-15 thomas return worktree->root_path;
354 1ea7ccc6 2021-11-15 thomas const char *
355 1ea7ccc6 2021-11-15 thomas got_worktree_get_repo_path(struct got_worktree *worktree)
357 1ea7ccc6 2021-11-15 thomas return worktree->repo_path;
360 1ea7ccc6 2021-11-15 thomas const char *
361 1ea7ccc6 2021-11-15 thomas got_worktree_get_path_prefix(struct got_worktree *worktree)
363 1ea7ccc6 2021-11-15 thomas return worktree->path_prefix;