Blame


1 7d69d862 2021-11-15 stsp /*
2 7d69d862 2021-11-15 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 7d69d862 2021-11-15 stsp *
4 7d69d862 2021-11-15 stsp * Permission to use, copy, modify, and distribute this software for any
5 7d69d862 2021-11-15 stsp * purpose with or without fee is hereby granted, provided that the above
6 7d69d862 2021-11-15 stsp * copyright notice and this permission notice appear in all copies.
7 7d69d862 2021-11-15 stsp *
8 7d69d862 2021-11-15 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 7d69d862 2021-11-15 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 7d69d862 2021-11-15 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 7d69d862 2021-11-15 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 7d69d862 2021-11-15 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 7d69d862 2021-11-15 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 7d69d862 2021-11-15 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 7d69d862 2021-11-15 stsp */
16 7d69d862 2021-11-15 stsp
17 7d69d862 2021-11-15 stsp #include <sys/stat.h>
18 7d69d862 2021-11-15 stsp #include <sys/queue.h>
19 7d69d862 2021-11-15 stsp
20 7d69d862 2021-11-15 stsp #include <errno.h>
21 7d69d862 2021-11-15 stsp #include <fcntl.h>
22 7d69d862 2021-11-15 stsp #include <limits.h>
23 7d69d862 2021-11-15 stsp #include <stddef.h>
24 7d69d862 2021-11-15 stsp #include <stdio.h>
25 7d69d862 2021-11-15 stsp #include <stdlib.h>
26 7d69d862 2021-11-15 stsp #include <string.h>
27 7d69d862 2021-11-15 stsp #include <unistd.h>
28 7d69d862 2021-11-15 stsp #include <uuid.h>
29 7d69d862 2021-11-15 stsp
30 7d69d862 2021-11-15 stsp #include "got_cancel.h"
31 7d69d862 2021-11-15 stsp #include "got_error.h"
32 7d69d862 2021-11-15 stsp #include "got_reference.h"
33 7d69d862 2021-11-15 stsp #include "got_path.h"
34 7d69d862 2021-11-15 stsp #include "got_worktree.h"
35 7d69d862 2021-11-15 stsp #include "got_repository.h"
36 7d69d862 2021-11-15 stsp #include "got_gotconfig.h"
37 7d69d862 2021-11-15 stsp #include "got_object.h"
38 7d69d862 2021-11-15 stsp
39 7d69d862 2021-11-15 stsp #include "got_lib_worktree.h"
40 7d69d862 2021-11-15 stsp #include "got_lib_gotconfig.h"
41 7d69d862 2021-11-15 stsp
42 7d69d862 2021-11-15 stsp static const struct got_error *
43 7d69d862 2021-11-15 stsp read_meta_file(char **content, const char *path_got, const char *name)
44 7d69d862 2021-11-15 stsp {
45 7d69d862 2021-11-15 stsp const struct got_error *err = NULL;
46 7d69d862 2021-11-15 stsp char *path;
47 7d69d862 2021-11-15 stsp int fd = -1;
48 7d69d862 2021-11-15 stsp ssize_t n;
49 7d69d862 2021-11-15 stsp struct stat sb;
50 7d69d862 2021-11-15 stsp
51 7d69d862 2021-11-15 stsp *content = NULL;
52 7d69d862 2021-11-15 stsp
53 7d69d862 2021-11-15 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
54 7d69d862 2021-11-15 stsp err = got_error_from_errno("asprintf");
55 7d69d862 2021-11-15 stsp path = NULL;
56 7d69d862 2021-11-15 stsp goto done;
57 7d69d862 2021-11-15 stsp }
58 7d69d862 2021-11-15 stsp
59 8bd0cdad 2021-12-31 stsp fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
60 7d69d862 2021-11-15 stsp if (fd == -1) {
61 7d69d862 2021-11-15 stsp if (errno == ENOENT)
62 7d69d862 2021-11-15 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
63 7d69d862 2021-11-15 stsp else
64 7d69d862 2021-11-15 stsp err = got_error_from_errno2("open", path);
65 7d69d862 2021-11-15 stsp goto done;
66 7d69d862 2021-11-15 stsp }
67 7d69d862 2021-11-15 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
68 7d69d862 2021-11-15 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
69 7d69d862 2021-11-15 stsp : got_error_from_errno2("flock", path));
70 7d69d862 2021-11-15 stsp goto done;
71 7d69d862 2021-11-15 stsp }
72 7d69d862 2021-11-15 stsp
73 7d69d862 2021-11-15 stsp if (fstat(fd, &sb) != 0) {
74 7d69d862 2021-11-15 stsp err = got_error_from_errno2("fstat", path);
75 7d69d862 2021-11-15 stsp goto done;
76 7d69d862 2021-11-15 stsp }
77 7d69d862 2021-11-15 stsp *content = calloc(1, sb.st_size);
78 7d69d862 2021-11-15 stsp if (*content == NULL) {
79 7d69d862 2021-11-15 stsp err = got_error_from_errno("calloc");
80 7d69d862 2021-11-15 stsp goto done;
81 7d69d862 2021-11-15 stsp }
82 7d69d862 2021-11-15 stsp
83 7d69d862 2021-11-15 stsp n = read(fd, *content, sb.st_size);
84 7d69d862 2021-11-15 stsp if (n != sb.st_size) {
85 7d69d862 2021-11-15 stsp err = (n == -1 ? got_error_from_errno2("read", path) :
86 7d69d862 2021-11-15 stsp got_error_path(path, GOT_ERR_WORKTREE_META));
87 7d69d862 2021-11-15 stsp goto done;
88 7d69d862 2021-11-15 stsp }
89 7d69d862 2021-11-15 stsp if ((*content)[sb.st_size - 1] != '\n') {
90 7d69d862 2021-11-15 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
91 7d69d862 2021-11-15 stsp goto done;
92 7d69d862 2021-11-15 stsp }
93 7d69d862 2021-11-15 stsp (*content)[sb.st_size - 1] = '\0';
94 7d69d862 2021-11-15 stsp
95 7d69d862 2021-11-15 stsp done:
96 7d69d862 2021-11-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
97 7d69d862 2021-11-15 stsp err = got_error_from_errno2("close", path_got);
98 7d69d862 2021-11-15 stsp free(path);
99 7d69d862 2021-11-15 stsp if (err) {
100 7d69d862 2021-11-15 stsp free(*content);
101 7d69d862 2021-11-15 stsp *content = NULL;
102 7d69d862 2021-11-15 stsp }
103 7d69d862 2021-11-15 stsp return err;
104 7d69d862 2021-11-15 stsp }
105 7d69d862 2021-11-15 stsp
106 7d69d862 2021-11-15 stsp static const struct got_error *
107 7d69d862 2021-11-15 stsp open_worktree(struct got_worktree **worktree, const char *path)
108 7d69d862 2021-11-15 stsp {
109 7d69d862 2021-11-15 stsp const struct got_error *err = NULL;
110 7d69d862 2021-11-15 stsp char *path_got;
111 7d69d862 2021-11-15 stsp char *formatstr = NULL;
112 7d69d862 2021-11-15 stsp char *uuidstr = NULL;
113 7d69d862 2021-11-15 stsp char *path_lock = NULL;
114 7d69d862 2021-11-15 stsp char *base_commit_id_str = NULL;
115 7d69d862 2021-11-15 stsp int version, fd = -1;
116 7d69d862 2021-11-15 stsp const char *errstr;
117 7d69d862 2021-11-15 stsp struct got_repository *repo = NULL;
118 7d69d862 2021-11-15 stsp uint32_t uuid_status;
119 7d69d862 2021-11-15 stsp
120 7d69d862 2021-11-15 stsp *worktree = NULL;
121 7d69d862 2021-11-15 stsp
122 7d69d862 2021-11-15 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
123 7d69d862 2021-11-15 stsp err = got_error_from_errno("asprintf");
124 7d69d862 2021-11-15 stsp path_got = NULL;
125 7d69d862 2021-11-15 stsp goto done;
126 7d69d862 2021-11-15 stsp }
127 7d69d862 2021-11-15 stsp
128 7d69d862 2021-11-15 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
129 7d69d862 2021-11-15 stsp err = got_error_from_errno("asprintf");
130 7d69d862 2021-11-15 stsp path_lock = NULL;
131 7d69d862 2021-11-15 stsp goto done;
132 7d69d862 2021-11-15 stsp }
133 7d69d862 2021-11-15 stsp
134 8bd0cdad 2021-12-31 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK | O_CLOEXEC);
135 7d69d862 2021-11-15 stsp if (fd == -1) {
136 7d69d862 2021-11-15 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
137 7d69d862 2021-11-15 stsp : got_error_from_errno2("open", path_lock));
138 7d69d862 2021-11-15 stsp goto done;
139 7d69d862 2021-11-15 stsp }
140 7d69d862 2021-11-15 stsp
141 7d69d862 2021-11-15 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
142 7d69d862 2021-11-15 stsp if (err)
143 7d69d862 2021-11-15 stsp goto done;
144 7d69d862 2021-11-15 stsp
145 7d69d862 2021-11-15 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
146 7d69d862 2021-11-15 stsp if (errstr) {
147 7d69d862 2021-11-15 stsp err = got_error_msg(GOT_ERR_WORKTREE_META,
148 7d69d862 2021-11-15 stsp "could not parse work tree format version number");
149 7d69d862 2021-11-15 stsp goto done;
150 7d69d862 2021-11-15 stsp }
151 7d69d862 2021-11-15 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
152 7d69d862 2021-11-15 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
153 7d69d862 2021-11-15 stsp goto done;
154 7d69d862 2021-11-15 stsp }
155 7d69d862 2021-11-15 stsp
156 7d69d862 2021-11-15 stsp *worktree = calloc(1, sizeof(**worktree));
157 7d69d862 2021-11-15 stsp if (*worktree == NULL) {
158 7d69d862 2021-11-15 stsp err = got_error_from_errno("calloc");
159 7d69d862 2021-11-15 stsp goto done;
160 7d69d862 2021-11-15 stsp }
161 7d69d862 2021-11-15 stsp (*worktree)->lockfd = -1;
162 7d69d862 2021-11-15 stsp
163 7d69d862 2021-11-15 stsp (*worktree)->root_path = realpath(path, NULL);
164 7d69d862 2021-11-15 stsp if ((*worktree)->root_path == NULL) {
165 7d69d862 2021-11-15 stsp err = got_error_from_errno2("realpath", path);
166 7d69d862 2021-11-15 stsp goto done;
167 7d69d862 2021-11-15 stsp }
168 7d69d862 2021-11-15 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
169 7d69d862 2021-11-15 stsp GOT_WORKTREE_REPOSITORY);
170 7d69d862 2021-11-15 stsp if (err)
171 7d69d862 2021-11-15 stsp goto done;
172 7d69d862 2021-11-15 stsp
173 7d69d862 2021-11-15 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
174 7d69d862 2021-11-15 stsp GOT_WORKTREE_PATH_PREFIX);
175 7d69d862 2021-11-15 stsp if (err)
176 7d69d862 2021-11-15 stsp goto done;
177 7d69d862 2021-11-15 stsp
178 7d69d862 2021-11-15 stsp err = read_meta_file(&base_commit_id_str, path_got,
179 7d69d862 2021-11-15 stsp GOT_WORKTREE_BASE_COMMIT);
180 7d69d862 2021-11-15 stsp if (err)
181 7d69d862 2021-11-15 stsp goto done;
182 7d69d862 2021-11-15 stsp
183 7d69d862 2021-11-15 stsp err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
184 7d69d862 2021-11-15 stsp if (err)
185 7d69d862 2021-11-15 stsp goto done;
186 7d69d862 2021-11-15 stsp uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
187 7d69d862 2021-11-15 stsp if (uuid_status != uuid_s_ok) {
188 7d69d862 2021-11-15 stsp err = got_error_uuid(uuid_status, "uuid_from_string");
189 7d69d862 2021-11-15 stsp goto done;
190 7d69d862 2021-11-15 stsp }
191 7d69d862 2021-11-15 stsp
192 7d69d862 2021-11-15 stsp err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
193 7d69d862 2021-11-15 stsp if (err)
194 7d69d862 2021-11-15 stsp goto done;
195 7d69d862 2021-11-15 stsp
196 7d69d862 2021-11-15 stsp err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
197 7d69d862 2021-11-15 stsp base_commit_id_str);
198 7d69d862 2021-11-15 stsp if (err)
199 7d69d862 2021-11-15 stsp goto done;
200 7d69d862 2021-11-15 stsp
201 7d69d862 2021-11-15 stsp err = read_meta_file(&(*worktree)->head_ref_name, path_got,
202 7d69d862 2021-11-15 stsp GOT_WORKTREE_HEAD_REF);
203 7d69d862 2021-11-15 stsp if (err)
204 7d69d862 2021-11-15 stsp goto done;
205 7d69d862 2021-11-15 stsp
206 7d69d862 2021-11-15 stsp if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
207 7d69d862 2021-11-15 stsp (*worktree)->root_path,
208 7d69d862 2021-11-15 stsp GOT_WORKTREE_GOT_DIR, GOT_GOTCONFIG_FILENAME) == -1) {
209 7d69d862 2021-11-15 stsp err = got_error_from_errno("asprintf");
210 7d69d862 2021-11-15 stsp goto done;
211 7d69d862 2021-11-15 stsp }
212 7d69d862 2021-11-15 stsp
213 7d69d862 2021-11-15 stsp err = got_gotconfig_read(&(*worktree)->gotconfig,
214 7d69d862 2021-11-15 stsp (*worktree)->gotconfig_path);
215 7d69d862 2021-11-15 stsp
216 8bd0cdad 2021-12-31 stsp (*worktree)->root_fd = open((*worktree)->root_path,
217 8bd0cdad 2021-12-31 stsp O_DIRECTORY | O_CLOEXEC);
218 7d69d862 2021-11-15 stsp if ((*worktree)->root_fd == -1) {
219 7d69d862 2021-11-15 stsp err = got_error_from_errno2("open", (*worktree)->root_path);
220 7d69d862 2021-11-15 stsp goto done;
221 7d69d862 2021-11-15 stsp }
222 7d69d862 2021-11-15 stsp done:
223 7d69d862 2021-11-15 stsp if (repo) {
224 7d69d862 2021-11-15 stsp const struct got_error *close_err = got_repo_close(repo);
225 7d69d862 2021-11-15 stsp if (err == NULL)
226 7d69d862 2021-11-15 stsp err = close_err;
227 7d69d862 2021-11-15 stsp }
228 7d69d862 2021-11-15 stsp free(path_got);
229 7d69d862 2021-11-15 stsp free(path_lock);
230 7d69d862 2021-11-15 stsp free(base_commit_id_str);
231 7d69d862 2021-11-15 stsp free(uuidstr);
232 7d69d862 2021-11-15 stsp free(formatstr);
233 7d69d862 2021-11-15 stsp if (err) {
234 7d69d862 2021-11-15 stsp if (fd != -1)
235 7d69d862 2021-11-15 stsp close(fd);
236 7d69d862 2021-11-15 stsp if (*worktree != NULL)
237 7d69d862 2021-11-15 stsp got_worktree_close(*worktree);
238 7d69d862 2021-11-15 stsp *worktree = NULL;
239 7d69d862 2021-11-15 stsp } else
240 7d69d862 2021-11-15 stsp (*worktree)->lockfd = fd;
241 7d69d862 2021-11-15 stsp
242 7d69d862 2021-11-15 stsp return err;
243 7d69d862 2021-11-15 stsp }
244 7d69d862 2021-11-15 stsp
245 7d69d862 2021-11-15 stsp const struct got_error *
246 7d69d862 2021-11-15 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
247 7d69d862 2021-11-15 stsp {
248 7d69d862 2021-11-15 stsp const struct got_error *err = NULL;
249 7d69d862 2021-11-15 stsp char *worktree_path;
250 7d69d862 2021-11-15 stsp
251 7d69d862 2021-11-15 stsp worktree_path = strdup(path);
252 7d69d862 2021-11-15 stsp if (worktree_path == NULL)
253 7d69d862 2021-11-15 stsp return got_error_from_errno("strdup");
254 7d69d862 2021-11-15 stsp
255 7d69d862 2021-11-15 stsp for (;;) {
256 7d69d862 2021-11-15 stsp char *parent_path;
257 7d69d862 2021-11-15 stsp
258 7d69d862 2021-11-15 stsp err = open_worktree(worktree, worktree_path);
259 7d69d862 2021-11-15 stsp if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
260 7d69d862 2021-11-15 stsp free(worktree_path);
261 7d69d862 2021-11-15 stsp return err;
262 7d69d862 2021-11-15 stsp }
263 7d69d862 2021-11-15 stsp if (*worktree) {
264 7d69d862 2021-11-15 stsp free(worktree_path);
265 7d69d862 2021-11-15 stsp return NULL;
266 7d69d862 2021-11-15 stsp }
267 7d69d862 2021-11-15 stsp if (worktree_path[0] == '/' && worktree_path[1] == '\0')
268 7d69d862 2021-11-15 stsp break;
269 7d69d862 2021-11-15 stsp err = got_path_dirname(&parent_path, worktree_path);
270 7d69d862 2021-11-15 stsp if (err) {
271 7d69d862 2021-11-15 stsp if (err->code != GOT_ERR_BAD_PATH) {
272 7d69d862 2021-11-15 stsp free(worktree_path);
273 7d69d862 2021-11-15 stsp return err;
274 7d69d862 2021-11-15 stsp }
275 7d69d862 2021-11-15 stsp break;
276 7d69d862 2021-11-15 stsp }
277 7d69d862 2021-11-15 stsp free(worktree_path);
278 7d69d862 2021-11-15 stsp worktree_path = parent_path;
279 7d69d862 2021-11-15 stsp }
280 7d69d862 2021-11-15 stsp
281 7d69d862 2021-11-15 stsp free(worktree_path);
282 7d69d862 2021-11-15 stsp return got_error(GOT_ERR_NOT_WORKTREE);
283 7d69d862 2021-11-15 stsp }
284 7d69d862 2021-11-15 stsp
285 7d69d862 2021-11-15 stsp const struct got_error *
286 7d69d862 2021-11-15 stsp got_worktree_close(struct got_worktree *worktree)
287 7d69d862 2021-11-15 stsp {
288 7d69d862 2021-11-15 stsp const struct got_error *err = NULL;
289 7d69d862 2021-11-15 stsp
290 7d69d862 2021-11-15 stsp if (worktree->lockfd != -1) {
291 7d69d862 2021-11-15 stsp if (close(worktree->lockfd) == -1)
292 7d69d862 2021-11-15 stsp err = got_error_from_errno2("close",
293 7d69d862 2021-11-15 stsp got_worktree_get_root_path(worktree));
294 7d69d862 2021-11-15 stsp }
295 7d69d862 2021-11-15 stsp if (close(worktree->root_fd) == -1 && err == NULL)
296 7d69d862 2021-11-15 stsp err = got_error_from_errno2("close",
297 7d69d862 2021-11-15 stsp got_worktree_get_root_path(worktree));
298 7d69d862 2021-11-15 stsp free(worktree->repo_path);
299 7d69d862 2021-11-15 stsp free(worktree->path_prefix);
300 7d69d862 2021-11-15 stsp free(worktree->base_commit_id);
301 7d69d862 2021-11-15 stsp free(worktree->head_ref_name);
302 7d69d862 2021-11-15 stsp free(worktree->root_path);
303 7d69d862 2021-11-15 stsp free(worktree->gotconfig_path);
304 7d69d862 2021-11-15 stsp got_gotconfig_free(worktree->gotconfig);
305 7d69d862 2021-11-15 stsp free(worktree);
306 7d69d862 2021-11-15 stsp return err;
307 7d69d862 2021-11-15 stsp }
308 7d69d862 2021-11-15 stsp
309 7d69d862 2021-11-15 stsp const char *
310 7d69d862 2021-11-15 stsp got_worktree_get_root_path(struct got_worktree *worktree)
311 7d69d862 2021-11-15 stsp {
312 7d69d862 2021-11-15 stsp return worktree->root_path;
313 7d69d862 2021-11-15 stsp }
314 7d69d862 2021-11-15 stsp
315 7d69d862 2021-11-15 stsp const char *
316 7d69d862 2021-11-15 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
317 7d69d862 2021-11-15 stsp {
318 7d69d862 2021-11-15 stsp return worktree->repo_path;
319 7d69d862 2021-11-15 stsp }
320 7d69d862 2021-11-15 stsp
321 7d69d862 2021-11-15 stsp const char *
322 7d69d862 2021-11-15 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
323 7d69d862 2021-11-15 stsp {
324 7d69d862 2021-11-15 stsp return worktree->path_prefix;
325 7d69d862 2021-11-15 stsp }