fix some fd leaks in error paths and avoid some double close
[got-portable.git] / lib / worktree_open.c
blob5ca8baf35beb64bc05c5d0a9686bfc5324e0258f
1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include "got_compat.h"
19 #include <sys/stat.h>
20 #include <sys/queue.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <limits.h>
25 #include <stddef.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
31 #include "got_cancel.h"
32 #include "got_error.h"
33 #include "got_reference.h"
34 #include "got_path.h"
35 #include "got_worktree.h"
36 #include "got_repository.h"
37 #include "got_gotconfig.h"
38 #include "got_object.h"
40 #include "got_lib_worktree.h"
41 #include "got_lib_gotconfig.h"
43 #ifndef nitems
44 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
45 #endif
47 static const struct got_error *
48 read_meta_file(char **content, const char *path_got, const char *name)
50 const struct got_error *err = NULL;
51 char *path;
52 int fd = -1;
53 ssize_t n;
54 struct stat sb;
56 *content = NULL;
58 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
59 err = got_error_from_errno("asprintf");
60 path = NULL;
61 goto done;
64 fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
65 if (fd == -1) {
66 if (errno == ENOENT)
67 err = got_error_path(path, GOT_ERR_WORKTREE_META);
68 else
69 err = got_error_from_errno2("open", path);
70 goto done;
72 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
73 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
74 : got_error_from_errno2("flock", path));
75 goto done;
78 if (fstat(fd, &sb) != 0) {
79 err = got_error_from_errno2("fstat", path);
80 goto done;
82 *content = calloc(1, sb.st_size);
83 if (*content == NULL) {
84 err = got_error_from_errno("calloc");
85 goto done;
88 n = read(fd, *content, sb.st_size);
89 if (n != sb.st_size) {
90 err = (n == -1 ? got_error_from_errno2("read", path) :
91 got_error_path(path, GOT_ERR_WORKTREE_META));
92 goto done;
94 if ((*content)[sb.st_size - 1] != '\n') {
95 err = got_error_path(path, GOT_ERR_WORKTREE_META);
96 goto done;
98 (*content)[sb.st_size - 1] = '\0';
100 done:
101 if (fd != -1 && close(fd) == -1 && err == NULL)
102 err = got_error_from_errno2("close", path_got);
103 free(path);
104 if (err) {
105 free(*content);
106 *content = NULL;
108 return err;
111 static const struct got_error *
112 open_worktree(struct got_worktree **worktree, const char *path,
113 const char *meta_dir)
115 const struct got_error *err = NULL;
116 char *path_meta;
117 char *formatstr = NULL;
118 char *uuidstr = NULL;
119 char *path_lock = NULL;
120 char *base_commit_id_str = NULL;
121 int version, fd = -1;
122 const char *errstr;
123 struct got_repository *repo = NULL;
124 int *pack_fds = NULL;
125 uint32_t uuid_status;
127 *worktree = NULL;
129 if (asprintf(&path_meta, "%s/%s", path, meta_dir) == -1) {
130 err = got_error_from_errno("asprintf");
131 path_meta = NULL;
132 goto done;
135 if (asprintf(&path_lock, "%s/%s", path_meta, GOT_WORKTREE_LOCK) == -1) {
136 err = got_error_from_errno("asprintf");
137 path_lock = NULL;
138 goto done;
141 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK | O_CLOEXEC);
142 if (fd == -1) {
143 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
144 : got_error_from_errno2("open", path_lock));
145 goto done;
148 err = read_meta_file(&formatstr, path_meta, GOT_WORKTREE_FORMAT);
149 if (err)
150 goto done;
152 version = strtonum(formatstr, 1, INT_MAX, &errstr);
153 if (errstr) {
154 err = got_error_msg(GOT_ERR_WORKTREE_META,
155 "could not parse work tree format version number");
156 goto done;
158 if (version != GOT_WORKTREE_FORMAT_VERSION) {
159 err = got_error(GOT_ERR_WORKTREE_VERS);
160 goto done;
163 *worktree = calloc(1, sizeof(**worktree));
164 if (*worktree == NULL) {
165 err = got_error_from_errno("calloc");
166 goto done;
168 (*worktree)->lockfd = -1;
170 (*worktree)->root_path = realpath(path, NULL);
171 if ((*worktree)->root_path == NULL) {
172 err = got_error_from_errno2("realpath", path);
173 goto done;
175 (*worktree)->meta_dir = meta_dir;
176 err = read_meta_file(&(*worktree)->repo_path, path_meta,
177 GOT_WORKTREE_REPOSITORY);
178 if (err)
179 goto done;
181 err = read_meta_file(&(*worktree)->path_prefix, path_meta,
182 GOT_WORKTREE_PATH_PREFIX);
183 if (err)
184 goto done;
186 err = read_meta_file(&base_commit_id_str, path_meta,
187 GOT_WORKTREE_BASE_COMMIT);
188 if (err)
189 goto done;
191 err = read_meta_file(&uuidstr, path_meta, GOT_WORKTREE_UUID);
192 if (err)
193 goto done;
194 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
195 if (uuid_status != uuid_s_ok) {
196 err = got_error_uuid(uuid_status, "uuid_from_string");
197 goto done;
200 err = got_repo_pack_fds_open(&pack_fds);
201 if (err)
202 goto done;
204 err = got_repo_open(&repo, (*worktree)->repo_path, NULL, pack_fds);
205 if (err)
206 goto done;
208 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
209 base_commit_id_str);
210 if (err)
211 goto done;
213 err = read_meta_file(&(*worktree)->head_ref_name, path_meta,
214 GOT_WORKTREE_HEAD_REF);
215 if (err)
216 goto done;
218 if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
219 (*worktree)->root_path, (*worktree)->meta_dir,
220 GOT_GOTCONFIG_FILENAME) == -1) {
221 err = got_error_from_errno("asprintf");
222 goto done;
225 err = got_gotconfig_read(&(*worktree)->gotconfig,
226 (*worktree)->gotconfig_path);
227 if (err)
228 goto done;
230 (*worktree)->root_fd = open((*worktree)->root_path,
231 O_DIRECTORY | O_CLOEXEC);
232 if ((*worktree)->root_fd == -1) {
233 err = got_error_from_errno2("open", (*worktree)->root_path);
234 goto done;
236 done:
237 if (repo) {
238 const struct got_error *close_err = got_repo_close(repo);
239 if (err == NULL)
240 err = close_err;
242 if (pack_fds) {
243 const struct got_error *pack_err =
244 got_repo_pack_fds_close(pack_fds);
245 if (err == NULL)
246 err = pack_err;
248 free(path_meta);
249 free(path_lock);
250 free(base_commit_id_str);
251 free(uuidstr);
252 free(formatstr);
253 if (err) {
254 if (fd != -1)
255 close(fd);
256 if (*worktree != NULL)
257 got_worktree_close(*worktree);
258 *worktree = NULL;
259 } else
260 (*worktree)->lockfd = fd;
262 return err;
265 const struct got_error *
266 got_worktree_open(struct got_worktree **worktree, const char *path,
267 const char *meta_dir)
269 const struct got_error *err = NULL;
270 char *worktree_path;
271 const char *meta_dirs[] = {
272 GOT_WORKTREE_GOT_DIR,
273 GOT_WORKTREE_CVG_DIR
275 int i;
277 worktree_path = strdup(path);
278 if (worktree_path == NULL)
279 return got_error_from_errno("strdup");
281 for (;;) {
282 char *parent_path;
284 if (meta_dir == NULL) {
285 for (i = 0; i < nitems(meta_dirs); i++) {
286 err = open_worktree(worktree, worktree_path,
287 meta_dirs[i]);
288 if (err == NULL ||
289 err->code == GOT_ERR_WORKTREE_BUSY)
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;