Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include "got_compat.h"
19 #include <sys/stat.h>
20 #include <sys/queue.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <limits.h>
25 #include <stddef.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
31 #include "got_cancel.h"
32 #include "got_error.h"
33 #include "got_reference.h"
34 #include "got_path.h"
35 #include "got_worktree.h"
36 #include "got_repository.h"
37 #include "got_gotconfig.h"
38 #include "got_object.h"
40 #include "got_lib_worktree.h"
41 #include "got_lib_gotconfig.h"
43 #ifndef nitems
44 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
45 #endif
47 static const struct got_error *
48 read_meta_file(char **content, const char *path_got, const char *name)
49 {
50 const struct got_error *err = NULL;
51 char *path;
52 int fd = -1;
53 ssize_t n;
54 struct stat sb;
56 *content = NULL;
58 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
59 err = got_error_from_errno("asprintf");
60 path = NULL;
61 goto done;
62 }
64 fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
65 if (fd == -1) {
66 if (errno == ENOENT)
67 err = got_error_path(path, GOT_ERR_WORKTREE_META);
68 else
69 err = got_error_from_errno2("open", path);
70 goto done;
71 }
72 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
73 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
74 : got_error_from_errno2("flock", path));
75 goto done;
76 }
78 if (fstat(fd, &sb) != 0) {
79 err = got_error_from_errno2("fstat", path);
80 goto done;
81 }
82 if (sb.st_size == 0) {
83 err = got_error_path(path, GOT_ERR_WORKTREE_META);
84 goto done;
85 }
86 *content = calloc(1, sb.st_size);
87 if (*content == NULL) {
88 err = got_error_from_errno("calloc");
89 goto done;
90 }
92 n = read(fd, *content, sb.st_size);
93 if (n != sb.st_size) {
94 err = (n == -1 ? got_error_from_errno2("read", path) :
95 got_error_path(path, GOT_ERR_WORKTREE_META));
96 goto done;
97 }
98 if ((*content)[sb.st_size - 1] != '\n') {
99 err = got_error_path(path, GOT_ERR_WORKTREE_META);
100 goto done;
102 (*content)[sb.st_size - 1] = '\0';
104 done:
105 if (fd != -1 && close(fd) == -1 && err == NULL)
106 err = got_error_from_errno2("close", path_got);
107 free(path);
108 if (err) {
109 free(*content);
110 *content = NULL;
112 return err;
115 static const struct got_error *
116 open_worktree(struct got_worktree **worktree, const char *path,
117 const char *meta_dir)
119 const struct got_error *err = NULL;
120 char *path_meta;
121 char *formatstr = NULL;
122 char *uuidstr = NULL;
123 char *path_lock = NULL;
124 char *base_commit_id_str = NULL;
125 int version, fd = -1;
126 const char *errstr;
127 struct got_repository *repo = NULL;
128 int *pack_fds = NULL;
129 uint32_t uuid_status;
131 *worktree = NULL;
133 if (asprintf(&path_meta, "%s/%s", path, meta_dir) == -1) {
134 err = got_error_from_errno("asprintf");
135 path_meta = NULL;
136 goto done;
139 if (asprintf(&path_lock, "%s/%s", path_meta, GOT_WORKTREE_LOCK) == -1) {
140 err = got_error_from_errno("asprintf");
141 path_lock = NULL;
142 goto done;
145 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK | O_CLOEXEC);
146 if (fd == -1) {
147 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
148 : got_error_from_errno2("open", path_lock));
149 goto done;
152 err = read_meta_file(&formatstr, path_meta, GOT_WORKTREE_FORMAT);
153 if (err)
154 goto done;
156 version = strtonum(formatstr, 1, INT_MAX, &errstr);
157 if (errstr) {
158 err = got_error_msg(GOT_ERR_WORKTREE_META,
159 "could not parse work tree format version number");
160 goto done;
162 if (version != GOT_WORKTREE_FORMAT_VERSION) {
163 err = got_error(GOT_ERR_WORKTREE_VERS);
164 goto done;
167 *worktree = calloc(1, sizeof(**worktree));
168 if (*worktree == NULL) {
169 err = got_error_from_errno("calloc");
170 goto done;
172 (*worktree)->lockfd = -1;
174 (*worktree)->root_path = realpath(path, NULL);
175 if ((*worktree)->root_path == NULL) {
176 err = got_error_from_errno2("realpath", path);
177 goto done;
179 (*worktree)->meta_dir = meta_dir;
180 err = read_meta_file(&(*worktree)->repo_path, path_meta,
181 GOT_WORKTREE_REPOSITORY);
182 if (err)
183 goto done;
185 err = read_meta_file(&(*worktree)->path_prefix, path_meta,
186 GOT_WORKTREE_PATH_PREFIX);
187 if (err)
188 goto done;
190 err = read_meta_file(&base_commit_id_str, path_meta,
191 GOT_WORKTREE_BASE_COMMIT);
192 if (err)
193 goto done;
195 err = read_meta_file(&uuidstr, path_meta, GOT_WORKTREE_UUID);
196 if (err)
197 goto done;
198 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
199 if (uuid_status != uuid_s_ok) {
200 err = got_error_uuid(uuid_status, "uuid_from_string");
201 goto done;
204 err = got_repo_pack_fds_open(&pack_fds);
205 if (err)
206 goto done;
208 err = got_repo_open(&repo, (*worktree)->repo_path, NULL, pack_fds);
209 if (err)
210 goto done;
212 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
213 base_commit_id_str);
214 if (err)
215 goto done;
217 err = read_meta_file(&(*worktree)->head_ref_name, path_meta,
218 GOT_WORKTREE_HEAD_REF);
219 if (err)
220 goto done;
222 if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
223 (*worktree)->root_path, (*worktree)->meta_dir,
224 GOT_GOTCONFIG_FILENAME) == -1) {
225 err = got_error_from_errno("asprintf");
226 goto done;
229 err = got_gotconfig_read(&(*worktree)->gotconfig,
230 (*worktree)->gotconfig_path);
231 if (err)
232 goto done;
234 (*worktree)->root_fd = open((*worktree)->root_path,
235 O_DIRECTORY | O_CLOEXEC);
236 if ((*worktree)->root_fd == -1) {
237 err = got_error_from_errno2("open", (*worktree)->root_path);
238 goto done;
240 done:
241 if (repo) {
242 const struct got_error *close_err = got_repo_close(repo);
243 if (err == NULL)
244 err = close_err;
246 if (pack_fds) {
247 const struct got_error *pack_err =
248 got_repo_pack_fds_close(pack_fds);
249 if (err == NULL)
250 err = pack_err;
252 free(path_meta);
253 free(path_lock);
254 free(base_commit_id_str);
255 free(uuidstr);
256 free(formatstr);
257 if (err) {
258 if (fd != -1)
259 close(fd);
260 if (*worktree != NULL)
261 got_worktree_close(*worktree);
262 *worktree = NULL;
263 } else
264 (*worktree)->lockfd = fd;
266 return err;
269 const struct got_error *
270 got_worktree_open(struct got_worktree **worktree, const char *path,
271 const char *meta_dir)
273 const struct got_error *err = NULL;
274 char *worktree_path;
275 const char *meta_dirs[] = {
276 GOT_WORKTREE_GOT_DIR,
277 GOT_WORKTREE_CVG_DIR
278 };
279 int i;
281 worktree_path = strdup(path);
282 if (worktree_path == NULL)
283 return got_error_from_errno("strdup");
285 for (;;) {
286 char *parent_path;
288 if (meta_dir == NULL) {
289 for (i = 0; i < nitems(meta_dirs); i++) {
290 err = open_worktree(worktree, worktree_path,
291 meta_dirs[i]);
292 if (err == NULL ||
293 err->code == GOT_ERR_WORKTREE_BUSY)
294 break;
296 } else
297 err = open_worktree(worktree, worktree_path, meta_dir);
298 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
299 free(worktree_path);
300 return err;
302 if (*worktree) {
303 free(worktree_path);
304 return NULL;
306 if (worktree_path[0] == '/' && worktree_path[1] == '\0')
307 break;
308 err = got_path_dirname(&parent_path, worktree_path);
309 if (err) {
310 if (err->code != GOT_ERR_BAD_PATH) {
311 free(worktree_path);
312 return err;
314 break;
316 free(worktree_path);
317 worktree_path = parent_path;
320 free(worktree_path);
321 return got_error(GOT_ERR_NOT_WORKTREE);
324 const struct got_error *
325 got_worktree_close(struct got_worktree *worktree)
327 const struct got_error *err = NULL;
329 if (worktree->lockfd != -1) {
330 if (close(worktree->lockfd) == -1)
331 err = got_error_from_errno2("close",
332 got_worktree_get_root_path(worktree));
334 if (close(worktree->root_fd) == -1 && err == NULL)
335 err = got_error_from_errno2("close",
336 got_worktree_get_root_path(worktree));
337 free(worktree->repo_path);
338 free(worktree->path_prefix);
339 free(worktree->base_commit_id);
340 free(worktree->head_ref_name);
341 free(worktree->root_path);
342 free(worktree->gotconfig_path);
343 got_gotconfig_free(worktree->gotconfig);
344 free(worktree);
345 return err;
348 const char *
349 got_worktree_get_root_path(struct got_worktree *worktree)
351 return worktree->root_path;
354 const char *
355 got_worktree_get_repo_path(struct got_worktree *worktree)
357 return worktree->repo_path;
360 const char *
361 got_worktree_get_path_prefix(struct got_worktree *worktree)
363 return worktree->path_prefix;