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 *content = calloc(1, sb.st_size);
84 if (*content == NULL) {
85 err = got_error_from_errno("calloc");
86 goto done;
87 }
89 n = read(fd, *content, sb.st_size);
90 if (n != sb.st_size) {
91 err = (n == -1 ? got_error_from_errno2("read", path) :
92 got_error_path(path, GOT_ERR_WORKTREE_META));
93 goto done;
94 }
95 if ((*content)[sb.st_size - 1] != '\n') {
96 err = got_error_path(path, GOT_ERR_WORKTREE_META);
97 goto done;
98 }
99 (*content)[sb.st_size - 1] = '\0';
101 done:
102 if (fd != -1 && close(fd) == -1 && err == NULL)
103 err = got_error_from_errno2("close", path_got);
104 free(path);
105 if (err) {
106 free(*content);
107 *content = NULL;
109 return err;
112 static const struct got_error *
113 open_worktree(struct got_worktree **worktree, const char *path,
114 const char *meta_dir)
116 const struct got_error *err = NULL;
117 char *path_meta;
118 char *formatstr = NULL;
119 char *uuidstr = NULL;
120 char *path_lock = NULL;
121 char *base_commit_id_str = NULL;
122 int version, fd = -1;
123 const char *errstr;
124 struct got_repository *repo = NULL;
125 int *pack_fds = NULL;
126 uint32_t uuid_status;
128 *worktree = NULL;
130 if (asprintf(&path_meta, "%s/%s", path, meta_dir) == -1) {
131 err = got_error_from_errno("asprintf");
132 path_meta = NULL;
133 goto done;
136 if (asprintf(&path_lock, "%s/%s", path_meta, GOT_WORKTREE_LOCK) == -1) {
137 err = got_error_from_errno("asprintf");
138 path_lock = NULL;
139 goto done;
142 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK | O_CLOEXEC);
143 if (fd == -1) {
144 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
145 : got_error_from_errno2("open", path_lock));
146 goto done;
149 err = read_meta_file(&formatstr, path_meta, GOT_WORKTREE_FORMAT);
150 if (err)
151 goto done;
153 version = strtonum(formatstr, 1, INT_MAX, &errstr);
154 if (errstr) {
155 err = got_error_msg(GOT_ERR_WORKTREE_META,
156 "could not parse work tree format version number");
157 goto done;
159 if (version != GOT_WORKTREE_FORMAT_VERSION) {
160 err = got_error(GOT_ERR_WORKTREE_VERS);
161 goto done;
164 *worktree = calloc(1, sizeof(**worktree));
165 if (*worktree == NULL) {
166 err = got_error_from_errno("calloc");
167 goto done;
169 (*worktree)->lockfd = -1;
171 (*worktree)->root_path = realpath(path, NULL);
172 if ((*worktree)->root_path == NULL) {
173 err = got_error_from_errno2("realpath", path);
174 goto done;
176 (*worktree)->meta_dir = meta_dir;
177 err = read_meta_file(&(*worktree)->repo_path, path_meta,
178 GOT_WORKTREE_REPOSITORY);
179 if (err)
180 goto done;
182 err = read_meta_file(&(*worktree)->path_prefix, path_meta,
183 GOT_WORKTREE_PATH_PREFIX);
184 if (err)
185 goto done;
187 err = read_meta_file(&base_commit_id_str, path_meta,
188 GOT_WORKTREE_BASE_COMMIT);
189 if (err)
190 goto done;
192 err = read_meta_file(&uuidstr, path_meta, GOT_WORKTREE_UUID);
193 if (err)
194 goto done;
195 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
196 if (uuid_status != uuid_s_ok) {
197 err = got_error_uuid(uuid_status, "uuid_from_string");
198 goto done;
201 err = got_repo_pack_fds_open(&pack_fds);
202 if (err)
203 goto done;
205 err = got_repo_open(&repo, (*worktree)->repo_path, NULL, pack_fds);
206 if (err)
207 goto done;
209 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
210 base_commit_id_str);
211 if (err)
212 goto done;
214 err = read_meta_file(&(*worktree)->head_ref_name, path_meta,
215 GOT_WORKTREE_HEAD_REF);
216 if (err)
217 goto done;
219 if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
220 (*worktree)->root_path, (*worktree)->meta_dir,
221 GOT_GOTCONFIG_FILENAME) == -1) {
222 err = got_error_from_errno("asprintf");
223 goto done;
226 err = got_gotconfig_read(&(*worktree)->gotconfig,
227 (*worktree)->gotconfig_path);
228 if (err)
229 goto done;
231 (*worktree)->root_fd = open((*worktree)->root_path,
232 O_DIRECTORY | O_CLOEXEC);
233 if ((*worktree)->root_fd == -1) {
234 err = got_error_from_errno2("open", (*worktree)->root_path);
235 goto done;
237 done:
238 if (repo) {
239 const struct got_error *close_err = got_repo_close(repo);
240 if (err == NULL)
241 err = close_err;
243 if (pack_fds) {
244 const struct got_error *pack_err =
245 got_repo_pack_fds_close(pack_fds);
246 if (err == NULL)
247 err = pack_err;
249 free(path_meta);
250 free(path_lock);
251 free(base_commit_id_str);
252 free(uuidstr);
253 free(formatstr);
254 if (err) {
255 if (fd != -1)
256 close(fd);
257 if (*worktree != NULL)
258 got_worktree_close(*worktree);
259 *worktree = NULL;
260 } else
261 (*worktree)->lockfd = fd;
263 return err;
266 const struct got_error *
267 got_worktree_open(struct got_worktree **worktree, const char *path,
268 const char *meta_dir)
270 const struct got_error *err = NULL;
271 char *worktree_path;
272 const char *meta_dirs[] = {
273 GOT_WORKTREE_GOT_DIR,
274 GOT_WORKTREE_CVG_DIR
275 };
276 int i;
278 worktree_path = strdup(path);
279 if (worktree_path == NULL)
280 return got_error_from_errno("strdup");
282 for (;;) {
283 char *parent_path;
285 if (meta_dir == NULL) {
286 for (i = 0; i < nitems(meta_dirs); i++) {
287 err = open_worktree(worktree, worktree_path,
288 meta_dirs[i]);
289 if (err == NULL)
290 break;
292 } else
293 err = open_worktree(worktree, worktree_path, meta_dir);
294 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
295 free(worktree_path);
296 return err;
298 if (*worktree) {
299 free(worktree_path);
300 return NULL;
302 if (worktree_path[0] == '/' && worktree_path[1] == '\0')
303 break;
304 err = got_path_dirname(&parent_path, worktree_path);
305 if (err) {
306 if (err->code != GOT_ERR_BAD_PATH) {
307 free(worktree_path);
308 return err;
310 break;
312 free(worktree_path);
313 worktree_path = parent_path;
316 free(worktree_path);
317 return got_error(GOT_ERR_NOT_WORKTREE);
320 const struct got_error *
321 got_worktree_close(struct got_worktree *worktree)
323 const struct got_error *err = NULL;
325 if (worktree->lockfd != -1) {
326 if (close(worktree->lockfd) == -1)
327 err = got_error_from_errno2("close",
328 got_worktree_get_root_path(worktree));
330 if (close(worktree->root_fd) == -1 && err == NULL)
331 err = got_error_from_errno2("close",
332 got_worktree_get_root_path(worktree));
333 free(worktree->repo_path);
334 free(worktree->path_prefix);
335 free(worktree->base_commit_id);
336 free(worktree->head_ref_name);
337 free(worktree->root_path);
338 free(worktree->gotconfig_path);
339 got_gotconfig_free(worktree->gotconfig);
340 free(worktree);
341 return err;
344 const char *
345 got_worktree_get_root_path(struct got_worktree *worktree)
347 return worktree->root_path;
350 const char *
351 got_worktree_get_repo_path(struct got_worktree *worktree)
353 return worktree->repo_path;
356 const char *
357 got_worktree_get_path_prefix(struct got_worktree *worktree)
359 return worktree->path_prefix;