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 <sys/stat.h>
18 #include <sys/queue.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <limits.h>
23 #include <sha1.h>
24 #include <stddef.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <uuid.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 static const struct got_error *
44 read_meta_file(char **content, const char *path_got, const char *name)
45 {
46 const struct got_error *err = NULL;
47 char *path;
48 int fd = -1;
49 ssize_t n;
50 struct stat sb;
52 *content = NULL;
54 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
55 err = got_error_from_errno("asprintf");
56 path = NULL;
57 goto done;
58 }
60 fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
61 if (fd == -1) {
62 if (errno == ENOENT)
63 err = got_error_path(path, GOT_ERR_WORKTREE_META);
64 else
65 err = got_error_from_errno2("open", path);
66 goto done;
67 }
68 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
69 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
70 : got_error_from_errno2("flock", path));
71 goto done;
72 }
74 if (fstat(fd, &sb) != 0) {
75 err = got_error_from_errno2("fstat", path);
76 goto done;
77 }
78 *content = calloc(1, sb.st_size);
79 if (*content == NULL) {
80 err = got_error_from_errno("calloc");
81 goto done;
82 }
84 n = read(fd, *content, sb.st_size);
85 if (n != sb.st_size) {
86 err = (n == -1 ? got_error_from_errno2("read", path) :
87 got_error_path(path, GOT_ERR_WORKTREE_META));
88 goto done;
89 }
90 if ((*content)[sb.st_size - 1] != '\n') {
91 err = got_error_path(path, GOT_ERR_WORKTREE_META);
92 goto done;
93 }
94 (*content)[sb.st_size - 1] = '\0';
96 done:
97 if (fd != -1 && close(fd) == -1 && err == NULL)
98 err = got_error_from_errno2("close", path_got);
99 free(path);
100 if (err) {
101 free(*content);
102 *content = NULL;
104 return err;
107 static const struct got_error *
108 open_worktree(struct got_worktree **worktree, const char *path)
110 const struct got_error *err = NULL;
111 char *path_got;
112 char *formatstr = NULL;
113 char *uuidstr = NULL;
114 char *path_lock = NULL;
115 char *base_commit_id_str = NULL;
116 int version, fd = -1;
117 const char *errstr;
118 struct got_repository *repo = NULL;
119 int *pack_fds = NULL;
120 uint32_t uuid_status;
122 *worktree = NULL;
124 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
125 err = got_error_from_errno("asprintf");
126 path_got = NULL;
127 goto done;
130 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
131 err = got_error_from_errno("asprintf");
132 path_lock = NULL;
133 goto done;
136 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK | O_CLOEXEC);
137 if (fd == -1) {
138 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
139 : got_error_from_errno2("open", path_lock));
140 goto done;
143 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
144 if (err)
145 goto done;
147 version = strtonum(formatstr, 1, INT_MAX, &errstr);
148 if (errstr) {
149 err = got_error_msg(GOT_ERR_WORKTREE_META,
150 "could not parse work tree format version number");
151 goto done;
153 if (version != GOT_WORKTREE_FORMAT_VERSION) {
154 err = got_error(GOT_ERR_WORKTREE_VERS);
155 goto done;
158 *worktree = calloc(1, sizeof(**worktree));
159 if (*worktree == NULL) {
160 err = got_error_from_errno("calloc");
161 goto done;
163 (*worktree)->lockfd = -1;
165 (*worktree)->root_path = realpath(path, NULL);
166 if ((*worktree)->root_path == NULL) {
167 err = got_error_from_errno2("realpath", path);
168 goto done;
170 err = read_meta_file(&(*worktree)->repo_path, path_got,
171 GOT_WORKTREE_REPOSITORY);
172 if (err)
173 goto done;
175 err = read_meta_file(&(*worktree)->path_prefix, path_got,
176 GOT_WORKTREE_PATH_PREFIX);
177 if (err)
178 goto done;
180 err = read_meta_file(&base_commit_id_str, path_got,
181 GOT_WORKTREE_BASE_COMMIT);
182 if (err)
183 goto done;
185 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
186 if (err)
187 goto done;
188 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
189 if (uuid_status != uuid_s_ok) {
190 err = got_error_uuid(uuid_status, "uuid_from_string");
191 goto done;
194 err = got_repo_pack_fds_open(&pack_fds);
195 if (err)
196 goto done;
198 err = got_repo_open(&repo, (*worktree)->repo_path, NULL, pack_fds);
199 if (err)
200 goto done;
202 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
203 base_commit_id_str);
204 if (err)
205 goto done;
207 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
208 GOT_WORKTREE_HEAD_REF);
209 if (err)
210 goto done;
212 if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
213 (*worktree)->root_path,
214 GOT_WORKTREE_GOT_DIR, GOT_GOTCONFIG_FILENAME) == -1) {
215 err = got_error_from_errno("asprintf");
216 goto done;
219 err = got_gotconfig_read(&(*worktree)->gotconfig,
220 (*worktree)->gotconfig_path);
221 if (err)
222 goto done;
224 (*worktree)->root_fd = open((*worktree)->root_path,
225 O_DIRECTORY | O_CLOEXEC);
226 if ((*worktree)->root_fd == -1) {
227 err = got_error_from_errno2("open", (*worktree)->root_path);
228 goto done;
230 done:
231 if (repo) {
232 const struct got_error *close_err = got_repo_close(repo);
233 if (err == NULL)
234 err = close_err;
236 if (pack_fds) {
237 const struct got_error *pack_err =
238 got_repo_pack_fds_close(pack_fds);
239 if (err == NULL)
240 err = pack_err;
242 free(path_got);
243 free(path_lock);
244 free(base_commit_id_str);
245 free(uuidstr);
246 free(formatstr);
247 if (err) {
248 if (fd != -1)
249 close(fd);
250 if (*worktree != NULL)
251 got_worktree_close(*worktree);
252 *worktree = NULL;
253 } else
254 (*worktree)->lockfd = fd;
256 return err;
259 const struct got_error *
260 got_worktree_open(struct got_worktree **worktree, const char *path)
262 const struct got_error *err = NULL;
263 char *worktree_path;
265 worktree_path = strdup(path);
266 if (worktree_path == NULL)
267 return got_error_from_errno("strdup");
269 for (;;) {
270 char *parent_path;
272 err = open_worktree(worktree, worktree_path);
273 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
274 free(worktree_path);
275 return err;
277 if (*worktree) {
278 free(worktree_path);
279 return NULL;
281 if (worktree_path[0] == '/' && worktree_path[1] == '\0')
282 break;
283 err = got_path_dirname(&parent_path, worktree_path);
284 if (err) {
285 if (err->code != GOT_ERR_BAD_PATH) {
286 free(worktree_path);
287 return err;
289 break;
291 free(worktree_path);
292 worktree_path = parent_path;
295 free(worktree_path);
296 return got_error(GOT_ERR_NOT_WORKTREE);
299 const struct got_error *
300 got_worktree_close(struct got_worktree *worktree)
302 const struct got_error *err = NULL;
304 if (worktree->lockfd != -1) {
305 if (close(worktree->lockfd) == -1)
306 err = got_error_from_errno2("close",
307 got_worktree_get_root_path(worktree));
309 if (close(worktree->root_fd) == -1 && err == NULL)
310 err = got_error_from_errno2("close",
311 got_worktree_get_root_path(worktree));
312 free(worktree->repo_path);
313 free(worktree->path_prefix);
314 free(worktree->base_commit_id);
315 free(worktree->head_ref_name);
316 free(worktree->root_path);
317 free(worktree->gotconfig_path);
318 got_gotconfig_free(worktree->gotconfig);
319 free(worktree);
320 return err;
323 const char *
324 got_worktree_get_root_path(struct got_worktree *worktree)
326 return worktree->root_path;
329 const char *
330 got_worktree_get_repo_path(struct got_worktree *worktree)
332 return worktree->repo_path;
335 const char *
336 got_worktree_get_path_prefix(struct got_worktree *worktree)
338 return worktree->path_prefix;