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>
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <limits.h>
22 #include <stddef.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <uuid.h>
29 #include "got_cancel.h"
30 #include "got_error.h"
31 #include "got_reference.h"
32 #include "got_path.h"
33 #include "got_worktree.h"
34 #include "got_repository.h"
35 #include "got_gotconfig.h"
36 #include "got_object.h"
38 #include "got_lib_worktree.h"
39 #include "got_lib_gotconfig.h"
41 static const struct got_error *
42 read_meta_file(char **content, const char *path_got, const char *name)
43 {
44 const struct got_error *err = NULL;
45 char *path;
46 int fd = -1;
47 ssize_t n;
48 struct stat sb;
50 *content = NULL;
52 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
53 err = got_error_from_errno("asprintf");
54 path = NULL;
55 goto done;
56 }
58 fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
59 if (fd == -1) {
60 if (errno == ENOENT)
61 err = got_error_path(path, GOT_ERR_WORKTREE_META);
62 else
63 err = got_error_from_errno2("open", path);
64 goto done;
65 }
66 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
67 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
68 : got_error_from_errno2("flock", path));
69 goto done;
70 }
72 if (fstat(fd, &sb) != 0) {
73 err = got_error_from_errno2("fstat", path);
74 goto done;
75 }
76 *content = calloc(1, sb.st_size);
77 if (*content == NULL) {
78 err = got_error_from_errno("calloc");
79 goto done;
80 }
82 n = read(fd, *content, sb.st_size);
83 if (n != sb.st_size) {
84 err = (n == -1 ? got_error_from_errno2("read", path) :
85 got_error_path(path, GOT_ERR_WORKTREE_META));
86 goto done;
87 }
88 if ((*content)[sb.st_size - 1] != '\n') {
89 err = got_error_path(path, GOT_ERR_WORKTREE_META);
90 goto done;
91 }
92 (*content)[sb.st_size - 1] = '\0';
94 done:
95 if (fd != -1 && close(fd) == -1 && err == NULL)
96 err = got_error_from_errno2("close", path_got);
97 free(path);
98 if (err) {
99 free(*content);
100 *content = NULL;
102 return err;
105 static const struct got_error *
106 open_worktree(struct got_worktree **worktree, const char *path)
108 const struct got_error *err = NULL;
109 char *path_got;
110 char *formatstr = NULL;
111 char *uuidstr = NULL;
112 char *path_lock = NULL;
113 char *base_commit_id_str = NULL;
114 int version, fd = -1;
115 const char *errstr;
116 struct got_repository *repo = NULL;
117 uint32_t uuid_status;
119 *worktree = NULL;
121 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
122 err = got_error_from_errno("asprintf");
123 path_got = NULL;
124 goto done;
127 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
128 err = got_error_from_errno("asprintf");
129 path_lock = NULL;
130 goto done;
133 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK | O_CLOEXEC);
134 if (fd == -1) {
135 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
136 : got_error_from_errno2("open", path_lock));
137 goto done;
140 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
141 if (err)
142 goto done;
144 version = strtonum(formatstr, 1, INT_MAX, &errstr);
145 if (errstr) {
146 err = got_error_msg(GOT_ERR_WORKTREE_META,
147 "could not parse work tree format version number");
148 goto done;
150 if (version != GOT_WORKTREE_FORMAT_VERSION) {
151 err = got_error(GOT_ERR_WORKTREE_VERS);
152 goto done;
155 *worktree = calloc(1, sizeof(**worktree));
156 if (*worktree == NULL) {
157 err = got_error_from_errno("calloc");
158 goto done;
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);
165 goto done;
167 err = read_meta_file(&(*worktree)->repo_path, path_got,
168 GOT_WORKTREE_REPOSITORY);
169 if (err)
170 goto done;
172 err = read_meta_file(&(*worktree)->path_prefix, path_got,
173 GOT_WORKTREE_PATH_PREFIX);
174 if (err)
175 goto done;
177 err = read_meta_file(&base_commit_id_str, path_got,
178 GOT_WORKTREE_BASE_COMMIT);
179 if (err)
180 goto done;
182 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
183 if (err)
184 goto done;
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");
188 goto done;
191 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
192 if (err)
193 goto done;
195 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
196 base_commit_id_str);
197 if (err)
198 goto done;
200 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
201 GOT_WORKTREE_HEAD_REF);
202 if (err)
203 goto done;
205 if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
206 (*worktree)->root_path,
207 GOT_WORKTREE_GOT_DIR, GOT_GOTCONFIG_FILENAME) == -1) {
208 err = got_error_from_errno("asprintf");
209 goto done;
212 err = got_gotconfig_read(&(*worktree)->gotconfig,
213 (*worktree)->gotconfig_path);
215 (*worktree)->root_fd = open((*worktree)->root_path,
216 O_DIRECTORY | O_CLOEXEC);
217 if ((*worktree)->root_fd == -1) {
218 err = got_error_from_errno2("open", (*worktree)->root_path);
219 goto done;
221 done:
222 if (repo) {
223 const struct got_error *close_err = got_repo_close(repo);
224 if (err == NULL)
225 err = close_err;
227 free(path_got);
228 free(path_lock);
229 free(base_commit_id_str);
230 free(uuidstr);
231 free(formatstr);
232 if (err) {
233 if (fd != -1)
234 close(fd);
235 if (*worktree != NULL)
236 got_worktree_close(*worktree);
237 *worktree = NULL;
238 } else
239 (*worktree)->lockfd = fd;
241 return err;
244 const struct got_error *
245 got_worktree_open(struct got_worktree **worktree, const char *path)
247 const struct got_error *err = NULL;
248 char *worktree_path;
250 worktree_path = strdup(path);
251 if (worktree_path == NULL)
252 return got_error_from_errno("strdup");
254 for (;;) {
255 char *parent_path;
257 err = open_worktree(worktree, worktree_path);
258 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
259 free(worktree_path);
260 return err;
262 if (*worktree) {
263 free(worktree_path);
264 return NULL;
266 if (worktree_path[0] == '/' && worktree_path[1] == '\0')
267 break;
268 err = got_path_dirname(&parent_path, worktree_path);
269 if (err) {
270 if (err->code != GOT_ERR_BAD_PATH) {
271 free(worktree_path);
272 return err;
274 break;
276 free(worktree_path);
277 worktree_path = parent_path;
280 free(worktree_path);
281 return got_error(GOT_ERR_NOT_WORKTREE);
284 const struct got_error *
285 got_worktree_close(struct got_worktree *worktree)
287 const struct got_error *err = NULL;
289 if (worktree->lockfd != -1) {
290 if (close(worktree->lockfd) == -1)
291 err = got_error_from_errno2("close",
292 got_worktree_get_root_path(worktree));
294 if (close(worktree->root_fd) == -1 && err == NULL)
295 err = got_error_from_errno2("close",
296 got_worktree_get_root_path(worktree));
297 free(worktree->repo_path);
298 free(worktree->path_prefix);
299 free(worktree->base_commit_id);
300 free(worktree->head_ref_name);
301 free(worktree->root_path);
302 free(worktree->gotconfig_path);
303 got_gotconfig_free(worktree->gotconfig);
304 free(worktree);
305 return err;
308 const char *
309 got_worktree_get_root_path(struct got_worktree *worktree)
311 return worktree->root_path;
314 const char *
315 got_worktree_get_repo_path(struct got_worktree *worktree)
317 return worktree->repo_path;
320 const char *
321 got_worktree_get_path_prefix(struct got_worktree *worktree)
323 return worktree->path_prefix;