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 <stddef.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <uuid.h>
30 #include "got_cancel.h"
31 #include "got_error.h"
32 #include "got_reference.h"
33 #include "got_path.h"
34 #include "got_worktree.h"
35 #include "got_repository.h"
36 #include "got_gotconfig.h"
37 #include "got_object.h"
39 #include "got_lib_worktree.h"
40 #include "got_lib_gotconfig.h"
42 static const struct got_error *
43 read_meta_file(char **content, const char *path_got, const char *name)
44 {
45 const struct got_error *err = NULL;
46 char *path;
47 int fd = -1;
48 ssize_t n;
49 struct stat sb;
51 *content = NULL;
53 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
54 err = got_error_from_errno("asprintf");
55 path = NULL;
56 goto done;
57 }
59 fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
60 if (fd == -1) {
61 if (errno == ENOENT)
62 err = got_error_path(path, GOT_ERR_WORKTREE_META);
63 else
64 err = got_error_from_errno2("open", path);
65 goto done;
66 }
67 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
68 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
69 : got_error_from_errno2("flock", path));
70 goto done;
71 }
73 if (fstat(fd, &sb) != 0) {
74 err = got_error_from_errno2("fstat", path);
75 goto done;
76 }
77 *content = calloc(1, sb.st_size);
78 if (*content == NULL) {
79 err = got_error_from_errno("calloc");
80 goto done;
81 }
83 n = read(fd, *content, sb.st_size);
84 if (n != sb.st_size) {
85 err = (n == -1 ? got_error_from_errno2("read", path) :
86 got_error_path(path, GOT_ERR_WORKTREE_META));
87 goto done;
88 }
89 if ((*content)[sb.st_size - 1] != '\n') {
90 err = got_error_path(path, GOT_ERR_WORKTREE_META);
91 goto done;
92 }
93 (*content)[sb.st_size - 1] = '\0';
95 done:
96 if (fd != -1 && close(fd) == -1 && err == NULL)
97 err = got_error_from_errno2("close", path_got);
98 free(path);
99 if (err) {
100 free(*content);
101 *content = NULL;
103 return err;
106 static const struct got_error *
107 open_worktree(struct got_worktree **worktree, const char *path)
109 const struct got_error *err = NULL;
110 char *path_got;
111 char *formatstr = NULL;
112 char *uuidstr = NULL;
113 char *path_lock = NULL;
114 char *base_commit_id_str = NULL;
115 int version, fd = -1;
116 const char *errstr;
117 struct got_repository *repo = NULL;
118 uint32_t uuid_status;
120 *worktree = NULL;
122 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
123 err = got_error_from_errno("asprintf");
124 path_got = NULL;
125 goto done;
128 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
129 err = got_error_from_errno("asprintf");
130 path_lock = NULL;
131 goto done;
134 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK | O_CLOEXEC);
135 if (fd == -1) {
136 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
137 : got_error_from_errno2("open", path_lock));
138 goto done;
141 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
142 if (err)
143 goto done;
145 version = strtonum(formatstr, 1, INT_MAX, &errstr);
146 if (errstr) {
147 err = got_error_msg(GOT_ERR_WORKTREE_META,
148 "could not parse work tree format version number");
149 goto done;
151 if (version != GOT_WORKTREE_FORMAT_VERSION) {
152 err = got_error(GOT_ERR_WORKTREE_VERS);
153 goto done;
156 *worktree = calloc(1, sizeof(**worktree));
157 if (*worktree == NULL) {
158 err = got_error_from_errno("calloc");
159 goto done;
161 (*worktree)->lockfd = -1;
163 (*worktree)->root_path = realpath(path, NULL);
164 if ((*worktree)->root_path == NULL) {
165 err = got_error_from_errno2("realpath", path);
166 goto done;
168 err = read_meta_file(&(*worktree)->repo_path, path_got,
169 GOT_WORKTREE_REPOSITORY);
170 if (err)
171 goto done;
173 err = read_meta_file(&(*worktree)->path_prefix, path_got,
174 GOT_WORKTREE_PATH_PREFIX);
175 if (err)
176 goto done;
178 err = read_meta_file(&base_commit_id_str, path_got,
179 GOT_WORKTREE_BASE_COMMIT);
180 if (err)
181 goto done;
183 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
184 if (err)
185 goto done;
186 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
187 if (uuid_status != uuid_s_ok) {
188 err = got_error_uuid(uuid_status, "uuid_from_string");
189 goto done;
192 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
193 if (err)
194 goto done;
196 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
197 base_commit_id_str);
198 if (err)
199 goto done;
201 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
202 GOT_WORKTREE_HEAD_REF);
203 if (err)
204 goto done;
206 if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
207 (*worktree)->root_path,
208 GOT_WORKTREE_GOT_DIR, GOT_GOTCONFIG_FILENAME) == -1) {
209 err = got_error_from_errno("asprintf");
210 goto done;
213 err = got_gotconfig_read(&(*worktree)->gotconfig,
214 (*worktree)->gotconfig_path);
216 (*worktree)->root_fd = open((*worktree)->root_path,
217 O_DIRECTORY | O_CLOEXEC);
218 if ((*worktree)->root_fd == -1) {
219 err = got_error_from_errno2("open", (*worktree)->root_path);
220 goto done;
222 done:
223 if (repo) {
224 const struct got_error *close_err = got_repo_close(repo);
225 if (err == NULL)
226 err = close_err;
228 free(path_got);
229 free(path_lock);
230 free(base_commit_id_str);
231 free(uuidstr);
232 free(formatstr);
233 if (err) {
234 if (fd != -1)
235 close(fd);
236 if (*worktree != NULL)
237 got_worktree_close(*worktree);
238 *worktree = NULL;
239 } else
240 (*worktree)->lockfd = fd;
242 return err;
245 const struct got_error *
246 got_worktree_open(struct got_worktree **worktree, const char *path)
248 const struct got_error *err = NULL;
249 char *worktree_path;
251 worktree_path = strdup(path);
252 if (worktree_path == NULL)
253 return got_error_from_errno("strdup");
255 for (;;) {
256 char *parent_path;
258 err = open_worktree(worktree, worktree_path);
259 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
260 free(worktree_path);
261 return err;
263 if (*worktree) {
264 free(worktree_path);
265 return NULL;
267 if (worktree_path[0] == '/' && worktree_path[1] == '\0')
268 break;
269 err = got_path_dirname(&parent_path, worktree_path);
270 if (err) {
271 if (err->code != GOT_ERR_BAD_PATH) {
272 free(worktree_path);
273 return err;
275 break;
277 free(worktree_path);
278 worktree_path = parent_path;
281 free(worktree_path);
282 return got_error(GOT_ERR_NOT_WORKTREE);
285 const struct got_error *
286 got_worktree_close(struct got_worktree *worktree)
288 const struct got_error *err = NULL;
290 if (worktree->lockfd != -1) {
291 if (close(worktree->lockfd) == -1)
292 err = got_error_from_errno2("close",
293 got_worktree_get_root_path(worktree));
295 if (close(worktree->root_fd) == -1 && err == NULL)
296 err = got_error_from_errno2("close",
297 got_worktree_get_root_path(worktree));
298 free(worktree->repo_path);
299 free(worktree->path_prefix);
300 free(worktree->base_commit_id);
301 free(worktree->head_ref_name);
302 free(worktree->root_path);
303 free(worktree->gotconfig_path);
304 got_gotconfig_free(worktree->gotconfig);
305 free(worktree);
306 return err;
309 const char *
310 got_worktree_get_root_path(struct got_worktree *worktree)
312 return worktree->root_path;
315 const char *
316 got_worktree_get_repo_path(struct got_worktree *worktree)
318 return worktree->repo_path;
321 const char *
322 got_worktree_get_path_prefix(struct got_worktree *worktree)
324 return worktree->path_prefix;