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 <sha2.h>
25 #include <stddef.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <uuid.h>
32 #include "got_cancel.h"
33 #include "got_error.h"
34 #include "got_reference.h"
35 #include "got_path.h"
36 #include "got_worktree.h"
37 #include "got_repository.h"
38 #include "got_gotconfig.h"
39 #include "got_object.h"
41 #include "got_lib_worktree.h"
42 #include "got_lib_gotconfig.h"
44 static const struct got_error *
45 read_meta_file(char **content, const char *path_got, const char *name)
46 {
47 const struct got_error *err = NULL;
48 char *path;
49 int fd = -1;
50 ssize_t n;
51 struct stat sb;
53 *content = NULL;
55 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
56 err = got_error_from_errno("asprintf");
57 path = NULL;
58 goto done;
59 }
61 fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
62 if (fd == -1) {
63 if (errno == ENOENT)
64 err = got_error_path(path, GOT_ERR_WORKTREE_META);
65 else
66 err = got_error_from_errno2("open", path);
67 goto done;
68 }
69 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
70 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
71 : got_error_from_errno2("flock", path));
72 goto done;
73 }
75 if (fstat(fd, &sb) != 0) {
76 err = got_error_from_errno2("fstat", path);
77 goto done;
78 }
79 *content = calloc(1, sb.st_size);
80 if (*content == NULL) {
81 err = got_error_from_errno("calloc");
82 goto done;
83 }
85 n = read(fd, *content, sb.st_size);
86 if (n != sb.st_size) {
87 err = (n == -1 ? got_error_from_errno2("read", path) :
88 got_error_path(path, GOT_ERR_WORKTREE_META));
89 goto done;
90 }
91 if ((*content)[sb.st_size - 1] != '\n') {
92 err = got_error_path(path, GOT_ERR_WORKTREE_META);
93 goto done;
94 }
95 (*content)[sb.st_size - 1] = '\0';
97 done:
98 if (fd != -1 && close(fd) == -1 && err == NULL)
99 err = got_error_from_errno2("close", path_got);
100 free(path);
101 if (err) {
102 free(*content);
103 *content = NULL;
105 return err;
108 static const struct got_error *
109 open_worktree(struct got_worktree **worktree, const char *path)
111 const struct got_error *err = NULL;
112 char *path_got;
113 char *formatstr = NULL;
114 char *uuidstr = NULL;
115 char *path_lock = NULL;
116 char *base_commit_id_str = NULL;
117 int version, fd = -1;
118 const char *errstr;
119 struct got_repository *repo = NULL;
120 int *pack_fds = NULL;
121 uint32_t uuid_status;
123 *worktree = NULL;
125 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
126 err = got_error_from_errno("asprintf");
127 path_got = NULL;
128 goto done;
131 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
132 err = got_error_from_errno("asprintf");
133 path_lock = NULL;
134 goto done;
137 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK | O_CLOEXEC);
138 if (fd == -1) {
139 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
140 : got_error_from_errno2("open", path_lock));
141 goto done;
144 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
145 if (err)
146 goto done;
148 version = strtonum(formatstr, 1, INT_MAX, &errstr);
149 if (errstr) {
150 err = got_error_msg(GOT_ERR_WORKTREE_META,
151 "could not parse work tree format version number");
152 goto done;
154 if (version != GOT_WORKTREE_FORMAT_VERSION) {
155 err = got_error(GOT_ERR_WORKTREE_VERS);
156 goto done;
159 *worktree = calloc(1, sizeof(**worktree));
160 if (*worktree == NULL) {
161 err = got_error_from_errno("calloc");
162 goto done;
164 (*worktree)->lockfd = -1;
166 (*worktree)->root_path = realpath(path, NULL);
167 if ((*worktree)->root_path == NULL) {
168 err = got_error_from_errno2("realpath", path);
169 goto done;
171 err = read_meta_file(&(*worktree)->repo_path, path_got,
172 GOT_WORKTREE_REPOSITORY);
173 if (err)
174 goto done;
176 err = read_meta_file(&(*worktree)->path_prefix, path_got,
177 GOT_WORKTREE_PATH_PREFIX);
178 if (err)
179 goto done;
181 err = read_meta_file(&base_commit_id_str, path_got,
182 GOT_WORKTREE_BASE_COMMIT);
183 if (err)
184 goto done;
186 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
187 if (err)
188 goto done;
189 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
190 if (uuid_status != uuid_s_ok) {
191 err = got_error_uuid(uuid_status, "uuid_from_string");
192 goto done;
195 err = got_repo_pack_fds_open(&pack_fds);
196 if (err)
197 goto done;
199 err = got_repo_open(&repo, (*worktree)->repo_path, NULL, pack_fds);
200 if (err)
201 goto done;
203 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
204 base_commit_id_str);
205 if (err)
206 goto done;
208 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
209 GOT_WORKTREE_HEAD_REF);
210 if (err)
211 goto done;
213 if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
214 (*worktree)->root_path,
215 GOT_WORKTREE_GOT_DIR, GOT_GOTCONFIG_FILENAME) == -1) {
216 err = got_error_from_errno("asprintf");
217 goto done;
220 err = got_gotconfig_read(&(*worktree)->gotconfig,
221 (*worktree)->gotconfig_path);
222 if (err)
223 goto done;
225 (*worktree)->root_fd = open((*worktree)->root_path,
226 O_DIRECTORY | O_CLOEXEC);
227 if ((*worktree)->root_fd == -1) {
228 err = got_error_from_errno2("open", (*worktree)->root_path);
229 goto done;
231 done:
232 if (repo) {
233 const struct got_error *close_err = got_repo_close(repo);
234 if (err == NULL)
235 err = close_err;
237 if (pack_fds) {
238 const struct got_error *pack_err =
239 got_repo_pack_fds_close(pack_fds);
240 if (err == NULL)
241 err = pack_err;
243 free(path_got);
244 free(path_lock);
245 free(base_commit_id_str);
246 free(uuidstr);
247 free(formatstr);
248 if (err) {
249 if (fd != -1)
250 close(fd);
251 if (*worktree != NULL)
252 got_worktree_close(*worktree);
253 *worktree = NULL;
254 } else
255 (*worktree)->lockfd = fd;
257 return err;
260 const struct got_error *
261 got_worktree_open(struct got_worktree **worktree, const char *path)
263 const struct got_error *err = NULL;
264 char *worktree_path;
266 worktree_path = strdup(path);
267 if (worktree_path == NULL)
268 return got_error_from_errno("strdup");
270 for (;;) {
271 char *parent_path;
273 err = open_worktree(worktree, worktree_path);
274 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
275 free(worktree_path);
276 return err;
278 if (*worktree) {
279 free(worktree_path);
280 return NULL;
282 if (worktree_path[0] == '/' && worktree_path[1] == '\0')
283 break;
284 err = got_path_dirname(&parent_path, worktree_path);
285 if (err) {
286 if (err->code != GOT_ERR_BAD_PATH) {
287 free(worktree_path);
288 return err;
290 break;
292 free(worktree_path);
293 worktree_path = parent_path;
296 free(worktree_path);
297 return got_error(GOT_ERR_NOT_WORKTREE);
300 const struct got_error *
301 got_worktree_close(struct got_worktree *worktree)
303 const struct got_error *err = NULL;
305 if (worktree->lockfd != -1) {
306 if (close(worktree->lockfd) == -1)
307 err = got_error_from_errno2("close",
308 got_worktree_get_root_path(worktree));
310 if (close(worktree->root_fd) == -1 && err == NULL)
311 err = got_error_from_errno2("close",
312 got_worktree_get_root_path(worktree));
313 free(worktree->repo_path);
314 free(worktree->path_prefix);
315 free(worktree->base_commit_id);
316 free(worktree->head_ref_name);
317 free(worktree->root_path);
318 free(worktree->gotconfig_path);
319 got_gotconfig_free(worktree->gotconfig);
320 free(worktree);
321 return err;
324 const char *
325 got_worktree_get_root_path(struct got_worktree *worktree)
327 return worktree->root_path;
330 const char *
331 got_worktree_get_repo_path(struct got_worktree *worktree)
333 return worktree->repo_path;
336 const char *
337 got_worktree_get_path_prefix(struct got_worktree *worktree)
339 return worktree->path_prefix;