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>
28 #include "got_cancel.h"
29 #include "got_error.h"
30 #include "got_reference.h"
31 #include "got_path.h"
32 #include "got_worktree.h"
33 #include "got_repository.h"
34 #include "got_gotconfig.h"
35 #include "got_object.h"
37 #include "got_lib_worktree.h"
38 #include "got_lib_gotconfig.h"
40 static const struct got_error *
41 read_meta_file(char **content, const char *path_got, const char *name)
42 {
43 const struct got_error *err = NULL;
44 char *path;
45 int fd = -1;
46 ssize_t n;
47 struct stat sb;
49 *content = NULL;
51 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
52 err = got_error_from_errno("asprintf");
53 path = NULL;
54 goto done;
55 }
57 fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
58 if (fd == -1) {
59 if (errno == ENOENT)
60 err = got_error_path(path, GOT_ERR_WORKTREE_META);
61 else
62 err = got_error_from_errno2("open", path);
63 goto done;
64 }
65 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
66 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
67 : got_error_from_errno2("flock", path));
68 goto done;
69 }
71 if (fstat(fd, &sb) != 0) {
72 err = got_error_from_errno2("fstat", path);
73 goto done;
74 }
75 *content = calloc(1, sb.st_size);
76 if (*content == NULL) {
77 err = got_error_from_errno("calloc");
78 goto done;
79 }
81 n = read(fd, *content, sb.st_size);
82 if (n != sb.st_size) {
83 err = (n == -1 ? got_error_from_errno2("read", path) :
84 got_error_path(path, GOT_ERR_WORKTREE_META));
85 goto done;
86 }
87 if ((*content)[sb.st_size - 1] != '\n') {
88 err = got_error_path(path, GOT_ERR_WORKTREE_META);
89 goto done;
90 }
91 (*content)[sb.st_size - 1] = '\0';
93 done:
94 if (fd != -1 && close(fd) == -1 && err == NULL)
95 err = got_error_from_errno2("close", path_got);
96 free(path);
97 if (err) {
98 free(*content);
99 *content = NULL;
101 return err;
104 static const struct got_error *
105 open_worktree(struct got_worktree **worktree, const char *path)
107 const struct got_error *err = NULL;
108 char *path_got;
109 char *formatstr = NULL;
110 char *uuidstr = NULL;
111 char *path_lock = NULL;
112 char *base_commit_id_str = NULL;
113 int version, fd = -1;
114 const char *errstr;
115 struct got_repository *repo = NULL;
116 uint32_t uuid_status;
118 *worktree = NULL;
120 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
121 err = got_error_from_errno("asprintf");
122 path_got = NULL;
123 goto done;
126 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
127 err = got_error_from_errno("asprintf");
128 path_lock = NULL;
129 goto done;
132 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK | O_CLOEXEC);
133 if (fd == -1) {
134 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
135 : got_error_from_errno2("open", path_lock));
136 goto done;
139 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
140 if (err)
141 goto done;
143 version = strtonum(formatstr, 1, INT_MAX, &errstr);
144 if (errstr) {
145 err = got_error_msg(GOT_ERR_WORKTREE_META,
146 "could not parse work tree format version number");
147 goto done;
149 if (version != GOT_WORKTREE_FORMAT_VERSION) {
150 err = got_error(GOT_ERR_WORKTREE_VERS);
151 goto done;
154 *worktree = calloc(1, sizeof(**worktree));
155 if (*worktree == NULL) {
156 err = got_error_from_errno("calloc");
157 goto done;
159 (*worktree)->lockfd = -1;
161 (*worktree)->root_path = realpath(path, NULL);
162 if ((*worktree)->root_path == NULL) {
163 err = got_error_from_errno2("realpath", path);
164 goto done;
166 err = read_meta_file(&(*worktree)->repo_path, path_got,
167 GOT_WORKTREE_REPOSITORY);
168 if (err)
169 goto done;
171 err = read_meta_file(&(*worktree)->path_prefix, path_got,
172 GOT_WORKTREE_PATH_PREFIX);
173 if (err)
174 goto done;
176 err = read_meta_file(&base_commit_id_str, path_got,
177 GOT_WORKTREE_BASE_COMMIT);
178 if (err)
179 goto done;
181 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
182 if (err)
183 goto done;
184 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
185 if (uuid_status != uuid_s_ok) {
186 err = got_error_uuid(uuid_status, "uuid_from_string");
187 goto done;
190 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
191 if (err)
192 goto done;
194 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
195 base_commit_id_str);
196 if (err)
197 goto done;
199 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
200 GOT_WORKTREE_HEAD_REF);
201 if (err)
202 goto done;
204 if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
205 (*worktree)->root_path,
206 GOT_WORKTREE_GOT_DIR, GOT_GOTCONFIG_FILENAME) == -1) {
207 err = got_error_from_errno("asprintf");
208 goto done;
211 err = got_gotconfig_read(&(*worktree)->gotconfig,
212 (*worktree)->gotconfig_path);
214 (*worktree)->root_fd = open((*worktree)->root_path,
215 O_DIRECTORY | O_CLOEXEC);
216 if ((*worktree)->root_fd == -1) {
217 err = got_error_from_errno2("open", (*worktree)->root_path);
218 goto done;
220 done:
221 if (repo) {
222 const struct got_error *close_err = got_repo_close(repo);
223 if (err == NULL)
224 err = close_err;
226 free(path_got);
227 free(path_lock);
228 free(base_commit_id_str);
229 free(uuidstr);
230 free(formatstr);
231 if (err) {
232 if (fd != -1)
233 close(fd);
234 if (*worktree != NULL)
235 got_worktree_close(*worktree);
236 *worktree = NULL;
237 } else
238 (*worktree)->lockfd = fd;
240 return err;
243 const struct got_error *
244 got_worktree_open(struct got_worktree **worktree, const char *path)
246 const struct got_error *err = NULL;
247 char *worktree_path;
249 worktree_path = strdup(path);
250 if (worktree_path == NULL)
251 return got_error_from_errno("strdup");
253 for (;;) {
254 char *parent_path;
256 err = open_worktree(worktree, worktree_path);
257 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
258 free(worktree_path);
259 return err;
261 if (*worktree) {
262 free(worktree_path);
263 return NULL;
265 if (worktree_path[0] == '/' && worktree_path[1] == '\0')
266 break;
267 err = got_path_dirname(&parent_path, worktree_path);
268 if (err) {
269 if (err->code != GOT_ERR_BAD_PATH) {
270 free(worktree_path);
271 return err;
273 break;
275 free(worktree_path);
276 worktree_path = parent_path;
279 free(worktree_path);
280 return got_error(GOT_ERR_NOT_WORKTREE);
283 const struct got_error *
284 got_worktree_close(struct got_worktree *worktree)
286 const struct got_error *err = NULL;
288 if (worktree->lockfd != -1) {
289 if (close(worktree->lockfd) == -1)
290 err = got_error_from_errno2("close",
291 got_worktree_get_root_path(worktree));
293 if (close(worktree->root_fd) == -1 && err == NULL)
294 err = got_error_from_errno2("close",
295 got_worktree_get_root_path(worktree));
296 free(worktree->repo_path);
297 free(worktree->path_prefix);
298 free(worktree->base_commit_id);
299 free(worktree->head_ref_name);
300 free(worktree->root_path);
301 free(worktree->gotconfig_path);
302 got_gotconfig_free(worktree->gotconfig);
303 free(worktree);
304 return err;
307 const char *
308 got_worktree_get_root_path(struct got_worktree *worktree)
310 return worktree->root_path;
313 const char *
314 got_worktree_get_repo_path(struct got_worktree *worktree)
316 return worktree->repo_path;
319 const char *
320 got_worktree_get_path_prefix(struct got_worktree *worktree)
322 return worktree->path_prefix;