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 #ifndef nitems
45 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
46 #endif
48 static const struct got_error *
49 read_meta_file(char **content, const char *path_got, const char *name)
50 {
51 const struct got_error *err = NULL;
52 char *path;
53 int fd = -1;
54 ssize_t n;
55 struct stat sb;
57 *content = NULL;
59 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
60 err = got_error_from_errno("asprintf");
61 path = NULL;
62 goto done;
63 }
65 fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
66 if (fd == -1) {
67 if (errno == ENOENT)
68 err = got_error_path(path, GOT_ERR_WORKTREE_META);
69 else
70 err = got_error_from_errno2("open", path);
71 goto done;
72 }
73 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
74 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
75 : got_error_from_errno2("flock", path));
76 goto done;
77 }
79 if (fstat(fd, &sb) != 0) {
80 err = got_error_from_errno2("fstat", path);
81 goto done;
82 }
83 if (sb.st_size == 0) {
84 err = got_error_path(path, GOT_ERR_WORKTREE_META);
85 goto done;
86 }
87 *content = calloc(1, sb.st_size);
88 if (*content == NULL) {
89 err = got_error_from_errno("calloc");
90 goto done;
91 }
93 n = read(fd, *content, sb.st_size);
94 if (n != sb.st_size) {
95 err = (n == -1 ? got_error_from_errno2("read", path) :
96 got_error_path(path, GOT_ERR_WORKTREE_META));
97 goto done;
98 }
99 if ((*content)[sb.st_size - 1] != '\n') {
100 err = got_error_path(path, GOT_ERR_WORKTREE_META);
101 goto done;
103 (*content)[sb.st_size - 1] = '\0';
105 done:
106 if (fd != -1 && close(fd) == -1 && err == NULL)
107 err = got_error_from_errno2("close", path_got);
108 free(path);
109 if (err) {
110 free(*content);
111 *content = NULL;
113 return err;
116 static const struct got_error *
117 open_worktree(struct got_worktree **worktree, const char *path,
118 const char *meta_dir)
120 const struct got_error *err = NULL;
121 char *path_meta;
122 char *formatstr = NULL;
123 char *uuidstr = NULL;
124 char *path_lock = NULL;
125 char *base_commit_id_str = NULL;
126 int version, fd = -1;
127 const char *errstr;
128 struct got_repository *repo = NULL;
129 int *pack_fds = NULL;
130 uint32_t uuid_status;
132 *worktree = NULL;
134 if (asprintf(&path_meta, "%s/%s", path, meta_dir) == -1) {
135 err = got_error_from_errno("asprintf");
136 path_meta = NULL;
137 goto done;
140 if (asprintf(&path_lock, "%s/%s", path_meta, GOT_WORKTREE_LOCK) == -1) {
141 err = got_error_from_errno("asprintf");
142 path_lock = NULL;
143 goto done;
146 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK | O_CLOEXEC);
147 if (fd == -1) {
148 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
149 : got_error_from_errno2("open", path_lock));
150 goto done;
153 err = read_meta_file(&formatstr, path_meta, GOT_WORKTREE_FORMAT);
154 if (err)
155 goto done;
157 version = strtonum(formatstr, 1, INT_MAX, &errstr);
158 if (errstr) {
159 err = got_error_msg(GOT_ERR_WORKTREE_META,
160 "could not parse work tree format version number");
161 goto done;
163 if (version != GOT_WORKTREE_FORMAT_VERSION) {
164 err = got_error(GOT_ERR_WORKTREE_VERS);
165 goto done;
168 *worktree = calloc(1, sizeof(**worktree));
169 if (*worktree == NULL) {
170 err = got_error_from_errno("calloc");
171 goto done;
173 (*worktree)->lockfd = -1;
175 (*worktree)->root_path = realpath(path, NULL);
176 if ((*worktree)->root_path == NULL) {
177 err = got_error_from_errno2("realpath", path);
178 goto done;
180 (*worktree)->meta_dir = meta_dir;
181 err = read_meta_file(&(*worktree)->repo_path, path_meta,
182 GOT_WORKTREE_REPOSITORY);
183 if (err)
184 goto done;
186 err = read_meta_file(&(*worktree)->path_prefix, path_meta,
187 GOT_WORKTREE_PATH_PREFIX);
188 if (err)
189 goto done;
191 err = read_meta_file(&base_commit_id_str, path_meta,
192 GOT_WORKTREE_BASE_COMMIT);
193 if (err)
194 goto done;
196 err = read_meta_file(&uuidstr, path_meta, GOT_WORKTREE_UUID);
197 if (err)
198 goto done;
199 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
200 if (uuid_status != uuid_s_ok) {
201 err = got_error_uuid(uuid_status, "uuid_from_string");
202 goto done;
205 err = got_repo_pack_fds_open(&pack_fds);
206 if (err)
207 goto done;
209 err = got_repo_open(&repo, (*worktree)->repo_path, NULL, pack_fds);
210 if (err)
211 goto done;
213 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
214 base_commit_id_str);
215 if (err)
216 goto done;
218 err = read_meta_file(&(*worktree)->head_ref_name, path_meta,
219 GOT_WORKTREE_HEAD_REF);
220 if (err)
221 goto done;
223 if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
224 (*worktree)->root_path, (*worktree)->meta_dir,
225 GOT_GOTCONFIG_FILENAME) == -1) {
226 err = got_error_from_errno("asprintf");
227 goto done;
230 err = got_gotconfig_read(&(*worktree)->gotconfig,
231 (*worktree)->gotconfig_path);
232 if (err)
233 goto done;
235 (*worktree)->root_fd = open((*worktree)->root_path,
236 O_DIRECTORY | O_CLOEXEC);
237 if ((*worktree)->root_fd == -1) {
238 err = got_error_from_errno2("open", (*worktree)->root_path);
239 goto done;
241 done:
242 if (repo) {
243 const struct got_error *close_err = got_repo_close(repo);
244 if (err == NULL)
245 err = close_err;
247 if (pack_fds) {
248 const struct got_error *pack_err =
249 got_repo_pack_fds_close(pack_fds);
250 if (err == NULL)
251 err = pack_err;
253 free(path_meta);
254 free(path_lock);
255 free(base_commit_id_str);
256 free(uuidstr);
257 free(formatstr);
258 if (err) {
259 if (fd != -1)
260 close(fd);
261 if (*worktree != NULL)
262 got_worktree_close(*worktree);
263 *worktree = NULL;
264 } else
265 (*worktree)->lockfd = fd;
267 return err;
270 const struct got_error *
271 got_worktree_open(struct got_worktree **worktree, const char *path,
272 const char *meta_dir)
274 const struct got_error *err = NULL;
275 char *worktree_path;
276 const char *meta_dirs[] = {
277 GOT_WORKTREE_GOT_DIR,
278 GOT_WORKTREE_CVG_DIR
279 };
280 int i;
282 worktree_path = strdup(path);
283 if (worktree_path == NULL)
284 return got_error_from_errno("strdup");
286 for (;;) {
287 char *parent_path;
289 if (meta_dir == NULL) {
290 for (i = 0; i < nitems(meta_dirs); i++) {
291 err = open_worktree(worktree, worktree_path,
292 meta_dirs[i]);
293 if (err == NULL ||
294 err->code == GOT_ERR_WORKTREE_BUSY)
295 break;
297 } else
298 err = open_worktree(worktree, worktree_path, meta_dir);
299 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
300 free(worktree_path);
301 return err;
303 if (*worktree) {
304 free(worktree_path);
305 return NULL;
307 if (worktree_path[0] == '/' && worktree_path[1] == '\0')
308 break;
309 err = got_path_dirname(&parent_path, worktree_path);
310 if (err) {
311 if (err->code != GOT_ERR_BAD_PATH) {
312 free(worktree_path);
313 return err;
315 break;
317 free(worktree_path);
318 worktree_path = parent_path;
321 free(worktree_path);
322 return got_error(GOT_ERR_NOT_WORKTREE);
325 const struct got_error *
326 got_worktree_close(struct got_worktree *worktree)
328 const struct got_error *err = NULL;
330 if (worktree->lockfd != -1) {
331 if (close(worktree->lockfd) == -1)
332 err = got_error_from_errno2("close",
333 got_worktree_get_root_path(worktree));
335 if (close(worktree->root_fd) == -1 && err == NULL)
336 err = got_error_from_errno2("close",
337 got_worktree_get_root_path(worktree));
338 free(worktree->repo_path);
339 free(worktree->path_prefix);
340 free(worktree->base_commit_id);
341 free(worktree->head_ref_name);
342 free(worktree->root_path);
343 free(worktree->gotconfig_path);
344 got_gotconfig_free(worktree->gotconfig);
345 free(worktree);
346 return err;
349 const char *
350 got_worktree_get_root_path(struct got_worktree *worktree)
352 return worktree->root_path;
355 const char *
356 got_worktree_get_repo_path(struct got_worktree *worktree)
358 return worktree->repo_path;
361 const char *
362 got_worktree_get_path_prefix(struct got_worktree *worktree)
364 return worktree->path_prefix;