2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 d71d75ad 2017-11-05 stsp * Permission to use, copy, modify, and distribute this software for any
5 d71d75ad 2017-11-05 stsp * purpose with or without fee is hereby granted, provided that the above
6 d71d75ad 2017-11-05 stsp * copyright notice and this permission notice appear in all copies.
8 d71d75ad 2017-11-05 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 d71d75ad 2017-11-05 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 d71d75ad 2017-11-05 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 d71d75ad 2017-11-05 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 d71d75ad 2017-11-05 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 d71d75ad 2017-11-05 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 d71d75ad 2017-11-05 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 2178c42e 2018-04-22 stsp #include <sys/types.h>
18 0ffeb3c2 2017-11-26 stsp #include <sys/stat.h>
19 2178c42e 2018-04-22 stsp #include <sys/uio.h>
20 2178c42e 2018-04-22 stsp #include <sys/socket.h>
21 2178c42e 2018-04-22 stsp #include <sys/wait.h>
22 b48e2ddb 2019-05-22 stsp #include <sys/resource.h>
23 2b0ae357 2022-01-10 thomas #include <sys/mman.h>
25 a1fd68d8 2018-01-12 stsp #include <errno.h>
26 2178c42e 2018-04-22 stsp #include <fcntl.h>
27 d71d75ad 2017-11-05 stsp #include <stdio.h>
28 ab9a70b2 2017-11-06 stsp #include <stdlib.h>
29 ab9a70b2 2017-11-06 stsp #include <string.h>
30 2178c42e 2018-04-22 stsp #include <stdint.h>
31 81a12da5 2020-09-09 naddy #include <unistd.h>
32 ab9a70b2 2017-11-06 stsp #include <zlib.h>
33 ab9a70b2 2017-11-06 stsp #include <ctype.h>
34 e40622f4 2020-07-23 stsp #include <libgen.h>
35 ab9a70b2 2017-11-06 stsp #include <limits.h>
36 788c352e 2018-06-16 stsp #include <time.h>
38 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
40 ab9a70b2 2017-11-06 stsp #include "got_error.h"
41 d71d75ad 2017-11-05 stsp #include "got_object.h"
42 ab9a70b2 2017-11-06 stsp #include "got_repository.h"
43 511a516b 2018-05-19 stsp #include "got_opentemp.h"
44 324d37e7 2019-05-11 stsp #include "got_path.h"
46 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
47 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
48 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
49 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
50 2178c42e 2018-04-22 stsp #include "got_lib_privsep.h"
51 6bef87be 2018-09-11 stsp #include "got_lib_object_idcache.h"
52 6bef87be 2018-09-11 stsp #include "got_lib_object_cache.h"
53 ad242220 2018-09-08 stsp #include "got_lib_object_parse.h"
54 15a94983 2018-12-23 stsp #include "got_lib_pack.h"
55 7bb0daa1 2018-06-21 stsp #include "got_lib_repository.h"
58 ab9a70b2 2017-11-06 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 3235492e 2018-04-01 stsp struct got_object_id *
62 3235492e 2018-04-01 stsp got_object_get_id(struct got_object *obj)
64 6402fb3c 2018-09-15 stsp return &obj->id;
67 bacc9935 2018-05-20 stsp const struct got_error *
68 bacc9935 2018-05-20 stsp got_object_get_id_str(char **outbuf, struct got_object *obj)
70 bacc9935 2018-05-20 stsp return got_object_id_str(outbuf, &obj->id);
73 15a94983 2018-12-23 stsp const struct got_error *
74 15a94983 2018-12-23 stsp got_object_get_type(int *type, struct got_repository *repo,
75 15a94983 2018-12-23 stsp struct got_object_id *id)
77 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
78 15a94983 2018-12-23 stsp struct got_object *obj;
80 15a94983 2018-12-23 stsp err = got_object_open(&obj, repo, id);
84 b107e67f 2018-01-19 stsp switch (obj->type) {
85 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_COMMIT:
86 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_TREE:
87 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_BLOB:
88 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
89 15a94983 2018-12-23 stsp *type = obj->type;
92 15a94983 2018-12-23 stsp err = got_error(GOT_ERR_OBJ_TYPE);
96 15a94983 2018-12-23 stsp got_object_close(obj);
100 90bdb554 2019-04-11 stsp const struct got_error *
101 90bdb554 2019-04-11 stsp got_object_get_path(char **path, struct got_object_id *id,
102 90bdb554 2019-04-11 stsp struct got_repository *repo)
104 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
105 7a132809 2018-07-23 stsp char *hex = NULL;
106 41d2888b 2019-08-11 stsp char *path_objects;
108 e6b1056e 2018-04-22 stsp *path = NULL;
110 41d2888b 2019-08-11 stsp path_objects = got_repo_get_path_objects(repo);
111 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
112 638f9024 2019-05-13 stsp return got_error_from_errno("got_repo_get_path_objects");
114 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
118 d1cda826 2017-11-06 stsp if (asprintf(path, "%s/%.2x/%s", path_objects,
119 d1cda826 2017-11-06 stsp id->sha1[0], hex + 2) == -1)
120 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
124 d1cda826 2017-11-06 stsp free(path_objects);
125 d1cda826 2017-11-06 stsp return err;
128 762d73f4 2021-04-10 stsp const struct got_error *
129 762d73f4 2021-04-10 stsp got_object_open_loose_fd(int *fd, struct got_object_id *id,
130 4796fb13 2018-12-23 stsp struct got_repository *repo)
132 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
133 a1fd68d8 2018-01-12 stsp char *path;
135 90bdb554 2019-04-11 stsp err = got_object_get_path(&path, id, repo);
137 d1cda826 2017-11-06 stsp return err;
138 06340621 2021-12-31 thomas *fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
139 d5003b79 2018-04-22 stsp if (*fd == -1) {
140 e82b1d81 2019-07-27 stsp err = got_error_from_errno2("open", path);
144 4558fcd4 2018-01-14 stsp free(path);
145 2090a03d 2018-09-09 stsp return err;
148 2090a03d 2018-09-09 stsp static const struct got_error *
149 a158c901 2018-12-23 stsp request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
150 a158c901 2018-12-23 stsp struct got_object_id *id)
152 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
153 a158c901 2018-12-23 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
155 a158c901 2018-12-23 stsp err = got_privsep_send_packed_obj_req(ibuf, idx, id);
157 a158c901 2018-12-23 stsp return err;
159 a158c901 2018-12-23 stsp err = got_privsep_recv_obj(obj, ibuf);
161 a158c901 2018-12-23 stsp return err;
163 a158c901 2018-12-23 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
165 a158c901 2018-12-23 stsp return NULL;
168 bc1f382f 2022-01-05 thomas /* Create temporary files used during delta application. */
169 59d1e4a0 2021-03-10 stsp static const struct got_error *
170 bc1f382f 2022-01-05 thomas pack_child_send_tempfiles(struct imsgbuf *ibuf, struct got_pack *pack)
172 bc1f382f 2022-01-05 thomas const struct got_error *err;
173 bc1f382f 2022-01-05 thomas int basefd, accumfd;
176 bc1f382f 2022-01-05 thomas * For performance reasons, the child will keep reusing the
177 bc1f382f 2022-01-05 thomas * same temporary files during every object request.
178 bc1f382f 2022-01-05 thomas * Opening and closing new files for every object request is
179 bc1f382f 2022-01-05 thomas * too expensive during operations such as 'gotadmin pack'.
181 bc1f382f 2022-01-05 thomas if (pack->child_has_tempfiles)
182 bc1f382f 2022-01-05 thomas return NULL;
184 59d1e4a0 2021-03-10 stsp basefd = got_opentempfd();
185 59d1e4a0 2021-03-10 stsp if (basefd == -1)
186 59d1e4a0 2021-03-10 stsp return got_error_from_errno("got_opentempfd");
188 bc1f382f 2022-01-05 thomas err = got_privsep_send_tmpfd(ibuf, basefd);
190 bc1f382f 2022-01-05 thomas return err;
192 59d1e4a0 2021-03-10 stsp accumfd = got_opentempfd();
193 bc1f382f 2022-01-05 thomas if (accumfd == -1)
194 59d1e4a0 2021-03-10 stsp return got_error_from_errno("got_opentempfd");
196 bc1f382f 2022-01-05 thomas err = got_privsep_send_tmpfd(ibuf, accumfd);
198 59d1e4a0 2021-03-10 stsp return err;
200 bc1f382f 2022-01-05 thomas pack->child_has_tempfiles = 1;
201 bc1f382f 2022-01-05 thomas return NULL;
204 bc1f382f 2022-01-05 thomas static const struct got_error *
205 bc1f382f 2022-01-05 thomas request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
206 bc1f382f 2022-01-05 thomas int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
208 bc1f382f 2022-01-05 thomas const struct got_error *err = NULL;
209 bc1f382f 2022-01-05 thomas struct imsgbuf *ibuf = pack->privsep_child->ibuf;
210 bc1f382f 2022-01-05 thomas int outfd_child;
212 bc1f382f 2022-01-05 thomas err = pack_child_send_tempfiles(ibuf, pack);
214 bc1f382f 2022-01-05 thomas return err;
216 bc1f382f 2022-01-05 thomas outfd_child = dup(outfd);
217 bc1f382f 2022-01-05 thomas if (outfd_child == -1)
218 bc1f382f 2022-01-05 thomas return got_error_from_errno("dup");
220 59d1e4a0 2021-03-10 stsp err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
222 59d1e4a0 2021-03-10 stsp close(outfd_child);
223 59d1e4a0 2021-03-10 stsp return err;
226 59d1e4a0 2021-03-10 stsp err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
228 59d1e4a0 2021-03-10 stsp return err;
230 59d1e4a0 2021-03-10 stsp err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
232 59d1e4a0 2021-03-10 stsp return err;
234 59d1e4a0 2021-03-10 stsp return NULL;
237 da506691 2019-05-22 stsp static void
238 b48e2ddb 2019-05-22 stsp set_max_datasize(void)
240 b48e2ddb 2019-05-22 stsp struct rlimit rl;
242 b48e2ddb 2019-05-22 stsp if (getrlimit(RLIMIT_DATA, &rl) != 0)
245 b48e2ddb 2019-05-22 stsp rl.rlim_cur = rl.rlim_max;
246 b48e2ddb 2019-05-22 stsp setrlimit(RLIMIT_DATA, &rl);
249 a158c901 2018-12-23 stsp static const struct got_error *
250 711fb6e8 2018-12-23 stsp start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
252 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
253 a158c901 2018-12-23 stsp int imsg_fds[2];
255 a158c901 2018-12-23 stsp struct imsgbuf *ibuf;
257 a158c901 2018-12-23 stsp ibuf = calloc(1, sizeof(*ibuf));
258 a158c901 2018-12-23 stsp if (ibuf == NULL)
259 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
261 a158c901 2018-12-23 stsp pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
262 a158c901 2018-12-23 stsp if (pack->privsep_child == NULL) {
263 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
264 a158c901 2018-12-23 stsp free(ibuf);
265 a158c901 2018-12-23 stsp return err;
267 bc1f382f 2022-01-05 thomas pack->child_has_tempfiles = 0;
269 a158c901 2018-12-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
270 638f9024 2019-05-13 stsp err = got_error_from_errno("socketpair");
274 a158c901 2018-12-23 stsp pid = fork();
275 a158c901 2018-12-23 stsp if (pid == -1) {
276 638f9024 2019-05-13 stsp err = got_error_from_errno("fork");
278 a158c901 2018-12-23 stsp } else if (pid == 0) {
279 b48e2ddb 2019-05-22 stsp set_max_datasize();
280 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
281 a158c901 2018-12-23 stsp pack->path_packfile);
282 a158c901 2018-12-23 stsp /* not reached */
285 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1)
286 638f9024 2019-05-13 stsp return got_error_from_errno("close");
287 a158c901 2018-12-23 stsp pack->privsep_child->imsg_fd = imsg_fds[0];
288 a158c901 2018-12-23 stsp pack->privsep_child->pid = pid;
289 a158c901 2018-12-23 stsp imsg_init(ibuf, imsg_fds[0]);
290 a158c901 2018-12-23 stsp pack->privsep_child->ibuf = ibuf;
292 a158c901 2018-12-23 stsp err = got_privsep_init_pack_child(ibuf, pack, packidx);
294 a158c901 2018-12-23 stsp const struct got_error *child_err;
295 a158c901 2018-12-23 stsp err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
296 a158c901 2018-12-23 stsp child_err = got_privsep_wait_for_child(
297 a158c901 2018-12-23 stsp pack->privsep_child->pid);
298 a158c901 2018-12-23 stsp if (child_err && err == NULL)
299 a158c901 2018-12-23 stsp err = child_err;
303 a158c901 2018-12-23 stsp free(ibuf);
304 a158c901 2018-12-23 stsp free(pack->privsep_child);
305 a158c901 2018-12-23 stsp pack->privsep_child = NULL;
307 a158c901 2018-12-23 stsp return err;
310 711fb6e8 2018-12-23 stsp static const struct got_error *
311 711fb6e8 2018-12-23 stsp read_packed_object_privsep(struct got_object **obj,
312 711fb6e8 2018-12-23 stsp struct got_repository *repo, struct got_pack *pack,
313 711fb6e8 2018-12-23 stsp struct got_packidx *packidx, int idx, struct got_object_id *id)
315 711fb6e8 2018-12-23 stsp const struct got_error *err = NULL;
317 59d1e4a0 2021-03-10 stsp if (pack->privsep_child == NULL) {
318 59d1e4a0 2021-03-10 stsp err = start_pack_privsep_child(pack, packidx);
320 59d1e4a0 2021-03-10 stsp return err;
323 711fb6e8 2018-12-23 stsp return request_packed_object(obj, pack, idx, id);
326 59d1e4a0 2021-03-10 stsp static const struct got_error *
327 59d1e4a0 2021-03-10 stsp read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
328 59d1e4a0 2021-03-10 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
329 59d1e4a0 2021-03-10 stsp struct got_object_id *id)
331 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
333 59d1e4a0 2021-03-10 stsp if (pack->privsep_child == NULL) {
334 59d1e4a0 2021-03-10 stsp err = start_pack_privsep_child(pack, packidx);
336 59d1e4a0 2021-03-10 stsp return err;
339 59d1e4a0 2021-03-10 stsp return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
343 b3d68e7f 2021-07-03 stsp const struct got_error *
344 b3d68e7f 2021-07-03 stsp got_object_open_packed(struct got_object **obj, struct got_object_id *id,
345 2090a03d 2018-09-09 stsp struct got_repository *repo)
347 2090a03d 2018-09-09 stsp const struct got_error *err = NULL;
348 2090a03d 2018-09-09 stsp struct got_pack *pack = NULL;
349 2090a03d 2018-09-09 stsp struct got_packidx *packidx = NULL;
351 2090a03d 2018-09-09 stsp char *path_packfile;
353 2090a03d 2018-09-09 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
355 2090a03d 2018-09-09 stsp return err;
357 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
358 aea75d87 2021-07-06 stsp packidx->path_packidx);
360 2090a03d 2018-09-09 stsp return err;
362 2090a03d 2018-09-09 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
363 2090a03d 2018-09-09 stsp if (pack == NULL) {
364 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
369 a158c901 2018-12-23 stsp err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
373 2090a03d 2018-09-09 stsp free(path_packfile);
374 4558fcd4 2018-01-14 stsp return err;
377 9f2369b0 2018-12-24 stsp static const struct got_error *
378 d5c81d44 2021-07-08 stsp request_object(struct got_object **obj, struct got_object_id *id,
379 d5c81d44 2021-07-08 stsp struct got_repository *repo, int fd)
381 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
382 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
384 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
386 d5c81d44 2021-07-08 stsp err = got_privsep_send_obj_req(ibuf, fd, id);
388 9f2369b0 2018-12-24 stsp return err;
390 9f2369b0 2018-12-24 stsp return got_privsep_recv_obj(obj, ibuf);
393 9f2369b0 2018-12-24 stsp static const struct got_error *
394 59d1e4a0 2021-03-10 stsp request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
395 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo, int infd)
397 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
398 59d1e4a0 2021-03-10 stsp struct imsgbuf *ibuf;
399 59d1e4a0 2021-03-10 stsp int outfd_child;
401 59d1e4a0 2021-03-10 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
403 59d1e4a0 2021-03-10 stsp outfd_child = dup(outfd);
404 59d1e4a0 2021-03-10 stsp if (outfd_child == -1)
405 59d1e4a0 2021-03-10 stsp return got_error_from_errno("dup");
407 d5c81d44 2021-07-08 stsp err = got_privsep_send_raw_obj_req(ibuf, infd, id);
409 59d1e4a0 2021-03-10 stsp return err;
411 59d1e4a0 2021-03-10 stsp err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
413 59d1e4a0 2021-03-10 stsp return err;
415 59d1e4a0 2021-03-10 stsp return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
418 59d1e4a0 2021-03-10 stsp static const struct got_error *
419 59d1e4a0 2021-03-10 stsp start_read_object_child(struct got_repository *repo)
421 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
422 9f2369b0 2018-12-24 stsp int imsg_fds[2];
424 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
426 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
427 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
428 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
430 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
431 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
432 ddc7b220 2019-09-08 stsp free(ibuf);
433 ddc7b220 2019-09-08 stsp return err;
436 9f2369b0 2018-12-24 stsp pid = fork();
437 ddc7b220 2019-09-08 stsp if (pid == -1) {
438 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
439 ddc7b220 2019-09-08 stsp free(ibuf);
440 ddc7b220 2019-09-08 stsp return err;
442 9f2369b0 2018-12-24 stsp else if (pid == 0) {
443 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
444 9f2369b0 2018-12-24 stsp repo->path);
445 9f2369b0 2018-12-24 stsp /* not reached */
448 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
449 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
450 ddc7b220 2019-09-08 stsp free(ibuf);
451 ddc7b220 2019-09-08 stsp return err;
454 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
455 9f2369b0 2018-12-24 stsp imsg_fds[0];
456 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
457 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
458 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
460 59d1e4a0 2021-03-10 stsp return NULL;
463 b3d68e7f 2021-07-03 stsp const struct got_error *
464 b3d68e7f 2021-07-03 stsp got_object_read_header_privsep(struct got_object **obj,
465 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo, int obj_fd)
467 59d1e4a0 2021-03-10 stsp const struct got_error *err;
469 59d1e4a0 2021-03-10 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
470 d5c81d44 2021-07-08 stsp return request_object(obj, id, repo, obj_fd);
472 59d1e4a0 2021-03-10 stsp err = start_read_object_child(repo);
474 7495ec13 2021-04-04 stsp close(obj_fd);
475 59d1e4a0 2021-03-10 stsp return err;
478 d5c81d44 2021-07-08 stsp return request_object(obj, id, repo, obj_fd);
481 59d1e4a0 2021-03-10 stsp static const struct got_error *
482 59d1e4a0 2021-03-10 stsp read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
483 d5c81d44 2021-07-08 stsp int outfd, struct got_object_id *id, struct got_repository *repo,
484 d5c81d44 2021-07-08 stsp int obj_fd)
486 59d1e4a0 2021-03-10 stsp const struct got_error *err;
488 59d1e4a0 2021-03-10 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
489 d5c81d44 2021-07-08 stsp return request_raw_object(outbuf, size, hdrlen, outfd, id,
490 d5c81d44 2021-07-08 stsp repo, obj_fd);
492 59d1e4a0 2021-03-10 stsp err = start_read_object_child(repo);
494 59d1e4a0 2021-03-10 stsp return err;
496 d5c81d44 2021-07-08 stsp return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
500 4558fcd4 2018-01-14 stsp const struct got_error *
501 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
502 4558fcd4 2018-01-14 stsp struct got_object_id *id)
504 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
507 7bb0daa1 2018-06-21 stsp *obj = got_repo_get_cached_object(repo, id);
508 7bb0daa1 2018-06-21 stsp if (*obj != NULL) {
509 7bb0daa1 2018-06-21 stsp (*obj)->refcnt++;
510 7bb0daa1 2018-06-21 stsp return NULL;
513 b3d68e7f 2021-07-03 stsp err = got_object_open_packed(obj, id, repo);
514 e82b1d81 2019-07-27 stsp if (err && err->code != GOT_ERR_NO_OBJ)
515 e82b1d81 2019-07-27 stsp return err;
516 e82b1d81 2019-07-27 stsp if (*obj) {
517 e82b1d81 2019-07-27 stsp (*obj)->refcnt++;
518 e82b1d81 2019-07-27 stsp return got_repo_cache_object(repo, id, *obj);
521 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
523 762d73f4 2021-04-10 stsp if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
524 762d73f4 2021-04-10 stsp err = got_error_no_obj(id);
525 762d73f4 2021-04-10 stsp return err;
528 d5c81d44 2021-07-08 stsp err = got_object_read_header_privsep(obj, id, repo, fd);
530 4558fcd4 2018-01-14 stsp return err;
532 762d73f4 2021-04-10 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
534 59790a32 2018-09-15 stsp (*obj)->refcnt++;
535 762d73f4 2021-04-10 stsp return got_repo_cache_object(repo, id, *obj);
538 8ab9215c 2021-10-15 thomas /* *outfd must be initialized to -1 by caller */
539 59d1e4a0 2021-03-10 stsp const struct got_error *
540 8ab9215c 2021-10-15 thomas got_object_raw_open(struct got_raw_object **obj, int *outfd,
541 f8bb1d3e 2021-10-17 thomas struct got_repository *repo, struct got_object_id *id)
543 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
544 59d1e4a0 2021-03-10 stsp struct got_packidx *packidx = NULL;
546 59d1e4a0 2021-03-10 stsp uint8_t *outbuf = NULL;
547 59d1e4a0 2021-03-10 stsp off_t size = 0;
548 59d1e4a0 2021-03-10 stsp size_t hdrlen = 0;
549 59d1e4a0 2021-03-10 stsp char *path_packfile = NULL;
551 8ab9215c 2021-10-15 thomas *obj = got_repo_get_cached_raw_object(repo, id);
552 8ab9215c 2021-10-15 thomas if (*obj != NULL) {
553 8ab9215c 2021-10-15 thomas (*obj)->refcnt++;
554 8ab9215c 2021-10-15 thomas return NULL;
557 8ab9215c 2021-10-15 thomas if (*outfd == -1) {
558 8ab9215c 2021-10-15 thomas *outfd = got_opentempfd();
559 8ab9215c 2021-10-15 thomas if (*outfd == -1)
560 8ab9215c 2021-10-15 thomas return got_error_from_errno("got_opentempfd");
563 59d1e4a0 2021-03-10 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
564 59d1e4a0 2021-03-10 stsp if (err == NULL) {
565 59d1e4a0 2021-03-10 stsp struct got_pack *pack = NULL;
567 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
568 aea75d87 2021-07-06 stsp packidx->path_packidx);
572 59d1e4a0 2021-03-10 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
573 59d1e4a0 2021-03-10 stsp if (pack == NULL) {
574 59d1e4a0 2021-03-10 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
579 59d1e4a0 2021-03-10 stsp err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
580 8ab9215c 2021-10-15 thomas *outfd, pack, packidx, idx, id);
582 dd3af45a 2021-10-15 thomas goto done;
583 59d1e4a0 2021-03-10 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
586 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
589 8ab9215c 2021-10-15 thomas err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
590 d5c81d44 2021-07-08 stsp id, repo, fd);
592 dd3af45a 2021-10-15 thomas goto done;
595 59d1e4a0 2021-03-10 stsp *obj = calloc(1, sizeof(**obj));
596 59d1e4a0 2021-03-10 stsp if (*obj == NULL) {
597 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("calloc");
600 2b0ae357 2022-01-10 thomas (*obj)->fd = -1;
602 59d1e4a0 2021-03-10 stsp if (outbuf) {
603 59d1e4a0 2021-03-10 stsp (*obj)->data = outbuf;
605 59d1e4a0 2021-03-10 stsp struct stat sb;
606 8ab9215c 2021-10-15 thomas if (fstat(*outfd, &sb) == -1) {
607 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fstat");
611 a8591711 2021-06-18 stsp if (sb.st_size != hdrlen + size) {
612 59d1e4a0 2021-03-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
615 2b0ae357 2022-01-10 thomas #ifndef GOT_PACK_NO_MMAP
616 2b0ae357 2022-01-10 thomas if (hdrlen + size > 0) {
617 2b0ae357 2022-01-10 thomas (*obj)->data = mmap(NULL, hdrlen + size, PROT_READ,
618 2b0ae357 2022-01-10 thomas MAP_PRIVATE, *outfd, 0);
619 2b0ae357 2022-01-10 thomas if ((*obj)->data == MAP_FAILED) {
620 2b0ae357 2022-01-10 thomas if (errno != ENOMEM) {
621 2b0ae357 2022-01-10 thomas err = got_error_from_errno("mmap");
622 2b0ae357 2022-01-10 thomas goto done;
624 2b0ae357 2022-01-10 thomas (*obj)->data = NULL;
626 2b0ae357 2022-01-10 thomas (*obj)->fd = *outfd;
627 2b0ae357 2022-01-10 thomas *outfd = -1;
631 2b0ae357 2022-01-10 thomas if (*outfd != -1) {
632 2b0ae357 2022-01-10 thomas (*obj)->f = fdopen(*outfd, "r");
633 2b0ae357 2022-01-10 thomas if ((*obj)->f == NULL) {
634 2b0ae357 2022-01-10 thomas err = got_error_from_errno("fdopen");
635 2b0ae357 2022-01-10 thomas goto done;
637 2b0ae357 2022-01-10 thomas *outfd = -1;
640 59d1e4a0 2021-03-10 stsp (*obj)->hdrlen = hdrlen;
641 59d1e4a0 2021-03-10 stsp (*obj)->size = size;
642 8ab9215c 2021-10-15 thomas err = got_repo_cache_raw_object(repo, id, *obj);
644 59d1e4a0 2021-03-10 stsp free(path_packfile);
646 59d1e4a0 2021-03-10 stsp if (*obj) {
647 59d1e4a0 2021-03-10 stsp got_object_raw_close(*obj);
648 59d1e4a0 2021-03-10 stsp *obj = NULL;
650 59d1e4a0 2021-03-10 stsp free(outbuf);
652 8ab9215c 2021-10-15 thomas (*obj)->refcnt++;
653 59d1e4a0 2021-03-10 stsp return err;
656 59d1e4a0 2021-03-10 stsp const struct got_error *
657 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
658 6dfa2fd3 2018-02-12 stsp const char *id_str)
660 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
662 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
663 6dd1ece6 2019-11-10 stsp return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
665 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
668 15a94983 2018-12-23 stsp const struct got_error *
669 15a94983 2018-12-23 stsp got_object_resolve_id_str(struct got_object_id **id,
670 15a94983 2018-12-23 stsp struct got_repository *repo, const char *id_str)
672 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
673 15a94983 2018-12-23 stsp struct got_object *obj;
675 15a94983 2018-12-23 stsp err = got_object_open_by_id_str(&obj, repo, id_str);
677 15a94983 2018-12-23 stsp return err;
679 15a94983 2018-12-23 stsp *id = got_object_id_dup(got_object_get_id(obj));
680 15a94983 2018-12-23 stsp got_object_close(obj);
681 15a94983 2018-12-23 stsp if (*id == NULL)
682 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
684 15a94983 2018-12-23 stsp return NULL;
687 434025f3 2018-09-16 stsp static const struct got_error *
688 a158c901 2018-12-23 stsp request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
689 a158c901 2018-12-23 stsp int pack_idx, struct got_object_id *id)
691 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
693 a158c901 2018-12-23 stsp err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
696 a158c901 2018-12-23 stsp return err;
698 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
700 ca6e02ac 2020-01-07 stsp return err;
702 ca6e02ac 2020-01-07 stsp (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
703 ca6e02ac 2020-01-07 stsp return NULL;
706 a158c901 2018-12-23 stsp static const struct got_error *
707 a158c901 2018-12-23 stsp read_packed_commit_privsep(struct got_commit_object **commit,
708 a158c901 2018-12-23 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
709 a158c901 2018-12-23 stsp struct got_object_id *id)
711 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
713 a158c901 2018-12-23 stsp if (pack->privsep_child)
714 a158c901 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
716 711fb6e8 2018-12-23 stsp err = start_pack_privsep_child(pack, packidx);
718 a158c901 2018-12-23 stsp return err;
720 711fb6e8 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
723 9f2369b0 2018-12-24 stsp static const struct got_error *
724 9f2369b0 2018-12-24 stsp request_commit(struct got_commit_object **commit, struct got_repository *repo,
725 d5c81d44 2021-07-08 stsp int fd, struct got_object_id *id)
727 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
728 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
730 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
732 d5c81d44 2021-07-08 stsp err = got_privsep_send_commit_req(ibuf, fd, id, -1);
734 9f2369b0 2018-12-24 stsp return err;
736 9f2369b0 2018-12-24 stsp return got_privsep_recv_commit(commit, ibuf);
739 9f2369b0 2018-12-24 stsp static const struct got_error *
740 9f2369b0 2018-12-24 stsp read_commit_privsep(struct got_commit_object **commit, int obj_fd,
741 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo)
743 ddc7b220 2019-09-08 stsp const struct got_error *err;
744 9f2369b0 2018-12-24 stsp int imsg_fds[2];
746 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
748 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
749 d5c81d44 2021-07-08 stsp return request_commit(commit, repo, obj_fd, id);
751 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
752 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
753 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
755 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
756 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
757 ddc7b220 2019-09-08 stsp free(ibuf);
758 ddc7b220 2019-09-08 stsp return err;
761 9f2369b0 2018-12-24 stsp pid = fork();
762 ddc7b220 2019-09-08 stsp if (pid == -1) {
763 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
764 ddc7b220 2019-09-08 stsp free(ibuf);
765 ddc7b220 2019-09-08 stsp return err;
767 9f2369b0 2018-12-24 stsp else if (pid == 0) {
768 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
769 9f2369b0 2018-12-24 stsp repo->path);
770 9f2369b0 2018-12-24 stsp /* not reached */
773 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
774 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
775 ddc7b220 2019-09-08 stsp free(ibuf);
776 ddc7b220 2019-09-08 stsp return err;
778 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
779 9f2369b0 2018-12-24 stsp imsg_fds[0];
780 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
781 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
782 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
784 d5c81d44 2021-07-08 stsp return request_commit(commit, repo, obj_fd, id);
788 a158c901 2018-12-23 stsp static const struct got_error *
789 434025f3 2018-09-16 stsp open_commit(struct got_commit_object **commit,
790 1785f84a 2018-12-23 stsp struct got_repository *repo, struct got_object_id *id, int check_cache)
792 434025f3 2018-09-16 stsp const struct got_error *err = NULL;
793 1785f84a 2018-12-23 stsp struct got_packidx *packidx = NULL;
795 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
797 434025f3 2018-09-16 stsp if (check_cache) {
798 1785f84a 2018-12-23 stsp *commit = got_repo_get_cached_commit(repo, id);
799 434025f3 2018-09-16 stsp if (*commit != NULL) {
800 434025f3 2018-09-16 stsp (*commit)->refcnt++;
801 434025f3 2018-09-16 stsp return NULL;
804 434025f3 2018-09-16 stsp *commit = NULL;
806 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
807 e82b1d81 2019-07-27 stsp if (err == NULL) {
808 1785f84a 2018-12-23 stsp struct got_pack *pack = NULL;
810 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
811 aea75d87 2021-07-06 stsp packidx->path_packidx);
813 1785f84a 2018-12-23 stsp return err;
815 1785f84a 2018-12-23 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
816 434025f3 2018-09-16 stsp if (pack == NULL) {
817 1785f84a 2018-12-23 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
822 a158c901 2018-12-23 stsp err = read_packed_commit_privsep(commit, pack,
823 1785f84a 2018-12-23 stsp packidx, idx, id);
824 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
827 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
829 e82b1d81 2019-07-27 stsp return err;
830 d5c81d44 2021-07-08 stsp err = read_commit_privsep(commit, fd, id, repo);
833 434025f3 2018-09-16 stsp if (err == NULL) {
834 434025f3 2018-09-16 stsp (*commit)->refcnt++;
835 1785f84a 2018-12-23 stsp err = got_repo_cache_commit(repo, id, *commit);
838 8d2c5ea3 2019-08-13 stsp free(path_packfile);
839 e32baab7 2018-11-05 stsp return err;
842 e32baab7 2018-11-05 stsp const struct got_error *
843 e32baab7 2018-11-05 stsp got_object_open_as_commit(struct got_commit_object **commit,
844 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object_id *id)
846 e32baab7 2018-11-05 stsp *commit = got_repo_get_cached_commit(repo, id);
847 e32baab7 2018-11-05 stsp if (*commit != NULL) {
848 e32baab7 2018-11-05 stsp (*commit)->refcnt++;
849 e32baab7 2018-11-05 stsp return NULL;
852 1785f84a 2018-12-23 stsp return open_commit(commit, repo, id, 0);
855 e32baab7 2018-11-05 stsp const struct got_error *
856 e32baab7 2018-11-05 stsp got_object_commit_open(struct got_commit_object **commit,
857 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object *obj)
859 1785f84a 2018-12-23 stsp return open_commit(commit, repo, got_object_get_id(obj), 1);
862 434025f3 2018-09-16 stsp const struct got_error *
863 dbc6a6b6 2018-07-12 stsp got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
865 dbc6a6b6 2018-07-12 stsp const struct got_error *err = NULL;
867 dbc6a6b6 2018-07-12 stsp *qid = calloc(1, sizeof(**qid));
868 dbc6a6b6 2018-07-12 stsp if (*qid == NULL)
869 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
871 dbc6a6b6 2018-07-12 stsp (*qid)->id = got_object_id_dup(id);
872 dbc6a6b6 2018-07-12 stsp if ((*qid)->id == NULL) {
873 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
874 fa2f6902 2018-07-23 stsp got_object_qid_free(*qid);
875 dbc6a6b6 2018-07-12 stsp *qid = NULL;
876 dbc6a6b6 2018-07-12 stsp return err;
879 9ca9aafb 2021-06-18 stsp return NULL;
882 9ca9aafb 2021-06-18 stsp const struct got_error *
883 9ca9aafb 2021-06-18 stsp got_object_id_queue_copy(const struct got_object_id_queue *src,
884 9ca9aafb 2021-06-18 stsp struct got_object_id_queue *dest)
886 9ca9aafb 2021-06-18 stsp const struct got_error *err;
887 9ca9aafb 2021-06-18 stsp struct got_object_qid *qid;
889 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, src, entry) {
890 9ca9aafb 2021-06-18 stsp struct got_object_qid *new;
892 9ca9aafb 2021-06-18 stsp * Deep-copy the object ID only. Let the caller deal
893 9ca9aafb 2021-06-18 stsp * with setting up the new->data pointer if needed.
895 9ca9aafb 2021-06-18 stsp err = got_object_qid_alloc(&new, qid->id);
897 9ca9aafb 2021-06-18 stsp got_object_id_queue_free(dest);
898 9ca9aafb 2021-06-18 stsp return err;
900 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(dest, new, entry);
903 dbc6a6b6 2018-07-12 stsp return NULL;
906 a158c901 2018-12-23 stsp static const struct got_error *
907 13c729f7 2018-12-24 stsp request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
908 13c729f7 2018-12-24 stsp int pack_idx, struct got_object_id *id)
910 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
912 13c729f7 2018-12-24 stsp err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
915 a158c901 2018-12-23 stsp return err;
917 a158c901 2018-12-23 stsp return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
920 71eb0e7f 2018-09-16 stsp static const struct got_error *
921 13c729f7 2018-12-24 stsp read_packed_tree_privsep(struct got_tree_object **tree,
922 13c729f7 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
923 13c729f7 2018-12-24 stsp struct got_object_id *id)
925 13c729f7 2018-12-24 stsp const struct got_error *err = NULL;
927 13c729f7 2018-12-24 stsp if (pack->privsep_child)
928 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
930 13c729f7 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
932 13c729f7 2018-12-24 stsp return err;
934 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
937 13c729f7 2018-12-24 stsp static const struct got_error *
938 9f2369b0 2018-12-24 stsp request_tree(struct got_tree_object **tree, struct got_repository *repo,
939 d5c81d44 2021-07-08 stsp int fd, struct got_object_id *id)
941 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
942 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
944 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
946 d5c81d44 2021-07-08 stsp err = got_privsep_send_tree_req(ibuf, fd, id, -1);
948 9f2369b0 2018-12-24 stsp return err;
950 9f2369b0 2018-12-24 stsp return got_privsep_recv_tree(tree, ibuf);
953 9f2369b0 2018-12-24 stsp const struct got_error *
954 9f2369b0 2018-12-24 stsp read_tree_privsep(struct got_tree_object **tree, int obj_fd,
955 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo)
957 ddc7b220 2019-09-08 stsp const struct got_error *err;
958 9f2369b0 2018-12-24 stsp int imsg_fds[2];
960 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
962 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
963 d5c81d44 2021-07-08 stsp return request_tree(tree, repo, obj_fd, id);
965 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
966 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
967 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
969 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
970 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
971 ddc7b220 2019-09-08 stsp free(ibuf);
972 ddc7b220 2019-09-08 stsp return err;
975 9f2369b0 2018-12-24 stsp pid = fork();
976 ddc7b220 2019-09-08 stsp if (pid == -1) {
977 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
978 ddc7b220 2019-09-08 stsp free(ibuf);
979 ddc7b220 2019-09-08 stsp return err;
981 9f2369b0 2018-12-24 stsp else if (pid == 0) {
982 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
983 9f2369b0 2018-12-24 stsp repo->path);
984 9f2369b0 2018-12-24 stsp /* not reached */
987 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
988 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
989 ddc7b220 2019-09-08 stsp free(ibuf);
990 ddc7b220 2019-09-08 stsp return err;
992 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
993 9f2369b0 2018-12-24 stsp imsg_fds[0];
994 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
995 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
996 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
999 d5c81d44 2021-07-08 stsp return request_tree(tree, repo, obj_fd, id);
1002 9f2369b0 2018-12-24 stsp static const struct got_error *
1003 13c729f7 2018-12-24 stsp open_tree(struct got_tree_object **tree, struct got_repository *repo,
1004 13c729f7 2018-12-24 stsp struct got_object_id *id, int check_cache)
1006 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
1007 13c729f7 2018-12-24 stsp struct got_packidx *packidx = NULL;
1009 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1011 71eb0e7f 2018-09-16 stsp if (check_cache) {
1012 13c729f7 2018-12-24 stsp *tree = got_repo_get_cached_tree(repo, id);
1013 71eb0e7f 2018-09-16 stsp if (*tree != NULL) {
1014 71eb0e7f 2018-09-16 stsp (*tree)->refcnt++;
1015 71eb0e7f 2018-09-16 stsp return NULL;
1018 71eb0e7f 2018-09-16 stsp *tree = NULL;
1020 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1021 e82b1d81 2019-07-27 stsp if (err == NULL) {
1022 13c729f7 2018-12-24 stsp struct got_pack *pack = NULL;
1024 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
1025 aea75d87 2021-07-06 stsp packidx->path_packidx);
1027 13c729f7 2018-12-24 stsp return err;
1029 13c729f7 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1030 e7885405 2018-09-10 stsp if (pack == NULL) {
1031 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1034 8d2c5ea3 2019-08-13 stsp goto done;
1036 13c729f7 2018-12-24 stsp err = read_packed_tree_privsep(tree, pack,
1037 13c729f7 2018-12-24 stsp packidx, idx, id);
1038 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1041 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1043 e82b1d81 2019-07-27 stsp return err;
1044 d5c81d44 2021-07-08 stsp err = read_tree_privsep(tree, fd, id, repo);
1047 f6be5c30 2018-06-22 stsp if (err == NULL) {
1048 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
1049 13c729f7 2018-12-24 stsp err = got_repo_cache_tree(repo, id, *tree);
1052 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1053 776d4d29 2018-06-17 stsp return err;
1056 776d4d29 2018-06-17 stsp const struct got_error *
1057 776d4d29 2018-06-17 stsp got_object_open_as_tree(struct got_tree_object **tree,
1058 776d4d29 2018-06-17 stsp struct got_repository *repo, struct got_object_id *id)
1060 e8eb494a 2018-09-16 stsp *tree = got_repo_get_cached_tree(repo, id);
1061 e8eb494a 2018-09-16 stsp if (*tree != NULL) {
1062 e8eb494a 2018-09-16 stsp (*tree)->refcnt++;
1063 e8eb494a 2018-09-16 stsp return NULL;
1066 13c729f7 2018-12-24 stsp return open_tree(tree, repo, id, 0);
1069 71eb0e7f 2018-09-16 stsp const struct got_error *
1070 71eb0e7f 2018-09-16 stsp got_object_tree_open(struct got_tree_object **tree,
1071 71eb0e7f 2018-09-16 stsp struct got_repository *repo, struct got_object *obj)
1073 13c729f7 2018-12-24 stsp return open_tree(tree, repo, got_object_get_id(obj), 1);
1077 56e0773d 2019-11-28 stsp got_object_tree_get_nentries(struct got_tree_object *tree)
1079 56e0773d 2019-11-28 stsp return tree->nentries;
1082 56e0773d 2019-11-28 stsp struct got_tree_entry *
1083 56e0773d 2019-11-28 stsp got_object_tree_get_first_entry(struct got_tree_object *tree)
1085 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, 0);
1088 56e0773d 2019-11-28 stsp struct got_tree_entry *
1089 56e0773d 2019-11-28 stsp got_object_tree_get_last_entry(struct got_tree_object *tree)
1091 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, tree->nentries - 1);
1094 56e0773d 2019-11-28 stsp struct got_tree_entry *
1095 56e0773d 2019-11-28 stsp got_object_tree_get_entry(struct got_tree_object *tree, int i)
1097 56e0773d 2019-11-28 stsp if (i < 0 || i >= tree->nentries)
1098 56e0773d 2019-11-28 stsp return NULL;
1099 56e0773d 2019-11-28 stsp return &tree->entries[i];
1103 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(struct got_tree_entry *te)
1105 56e0773d 2019-11-28 stsp return te->mode;
1108 56e0773d 2019-11-28 stsp const char *
1109 56e0773d 2019-11-28 stsp got_tree_entry_get_name(struct got_tree_entry *te)
1111 56e0773d 2019-11-28 stsp return &te->name[0];
1114 56e0773d 2019-11-28 stsp struct got_object_id *
1115 56e0773d 2019-11-28 stsp got_tree_entry_get_id(struct got_tree_entry *te)
1117 56e0773d 2019-11-28 stsp return &te->id;
1120 0d6c6ee3 2020-05-20 stsp const struct got_error *
1121 af57b12a 2020-07-23 stsp got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1123 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
1124 32596e16 2020-07-23 stsp size_t len, totlen, hdrlen, offset;
1126 aa092692 2020-07-23 stsp *s = NULL;
1128 659dc16e 2020-07-23 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1129 659dc16e 2020-07-23 stsp totlen = 0;
1130 32596e16 2020-07-23 stsp offset = 0;
1134 659dc16e 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1136 af57b12a 2020-07-23 stsp return err;
1138 659dc16e 2020-07-23 stsp if (len == 0)
1141 659dc16e 2020-07-23 stsp totlen += len - hdrlen;
1142 af57b12a 2020-07-23 stsp p = realloc(*s, totlen + 1);
1143 659dc16e 2020-07-23 stsp if (p == NULL) {
1144 659dc16e 2020-07-23 stsp err = got_error_from_errno("realloc");
1146 af57b12a 2020-07-23 stsp *s = NULL;
1147 af57b12a 2020-07-23 stsp return err;
1150 659dc16e 2020-07-23 stsp /* Skip blob object header first time around. */
1151 af57b12a 2020-07-23 stsp memcpy(*s + offset,
1152 f8f7c882 2020-07-23 stsp got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1153 659dc16e 2020-07-23 stsp hdrlen = 0;
1154 32596e16 2020-07-23 stsp offset = totlen;
1155 659dc16e 2020-07-23 stsp } while (len > 0);
1157 af57b12a 2020-07-23 stsp (*s)[totlen] = '\0';
1158 af57b12a 2020-07-23 stsp return NULL;
1161 af57b12a 2020-07-23 stsp const struct got_error *
1162 af57b12a 2020-07-23 stsp got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1163 af57b12a 2020-07-23 stsp struct got_repository *repo)
1165 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
1166 af57b12a 2020-07-23 stsp struct got_blob_object *blob = NULL;
1168 af57b12a 2020-07-23 stsp *link_target = NULL;
1170 af57b12a 2020-07-23 stsp if (!got_object_tree_entry_is_symlink(te))
1171 af57b12a 2020-07-23 stsp return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1173 af57b12a 2020-07-23 stsp err = got_object_open_as_blob(&blob, repo,
1174 af57b12a 2020-07-23 stsp got_tree_entry_get_id(te), PATH_MAX);
1176 af57b12a 2020-07-23 stsp return err;
1178 af57b12a 2020-07-23 stsp err = got_object_blob_read_to_str(link_target, blob);
1179 af57b12a 2020-07-23 stsp got_object_blob_close(blob);
1180 659dc16e 2020-07-23 stsp if (err) {
1181 659dc16e 2020-07-23 stsp free(*link_target);
1182 659dc16e 2020-07-23 stsp *link_target = NULL;
1184 0d6c6ee3 2020-05-20 stsp return err;
1188 56e0773d 2019-11-28 stsp got_tree_entry_get_index(struct got_tree_entry *te)
1190 56e0773d 2019-11-28 stsp return te->idx;
1193 56e0773d 2019-11-28 stsp struct got_tree_entry *
1194 56e0773d 2019-11-28 stsp got_tree_entry_get_next(struct got_tree_object *tree,
1195 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
1197 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx + 1);
1200 56e0773d 2019-11-28 stsp struct got_tree_entry *
1201 56e0773d 2019-11-28 stsp got_tree_entry_get_prev(struct got_tree_object *tree,
1202 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
1204 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx - 1);
1207 3840f4c9 2018-09-12 stsp static const struct got_error *
1208 ac544f8c 2019-01-13 stsp request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1209 ebc55e2d 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1210 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
1212 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
1213 bc1f382f 2022-01-05 thomas struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1214 bc1f382f 2022-01-05 thomas int outfd_child;
1216 bc1f382f 2022-01-05 thomas err = pack_child_send_tempfiles(ibuf, pack);
1217 bc1f382f 2022-01-05 thomas if (err)
1218 bc1f382f 2022-01-05 thomas return err;
1220 3840f4c9 2018-09-12 stsp outfd_child = dup(outfd);
1221 3840f4c9 2018-09-12 stsp if (outfd_child == -1)
1222 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1224 ebc55e2d 2018-12-24 stsp err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1226 3840f4c9 2018-09-12 stsp return err;
1228 3840f4c9 2018-09-12 stsp err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1229 3840f4c9 2018-09-12 stsp outfd_child);
1230 3840f4c9 2018-09-12 stsp if (err) {
1231 3840f4c9 2018-09-12 stsp return err;
1234 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen,
1235 ebc55e2d 2018-12-24 stsp pack->privsep_child->ibuf);
1237 3840f4c9 2018-09-12 stsp return err;
1239 3840f4c9 2018-09-12 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1240 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1242 3840f4c9 2018-09-12 stsp return err;
1245 ebc55e2d 2018-12-24 stsp static const struct got_error *
1246 ac544f8c 2019-01-13 stsp read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1247 ac544f8c 2019-01-13 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1248 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
1250 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
1252 ebc55e2d 2018-12-24 stsp if (pack->privsep_child == NULL) {
1253 ebc55e2d 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
1255 ebc55e2d 2018-12-24 stsp return err;
1258 ac544f8c 2019-01-13 stsp return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1262 9f2369b0 2018-12-24 stsp static const struct got_error *
1263 ac544f8c 2019-01-13 stsp request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1264 d5c81d44 2021-07-08 stsp int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1266 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1267 9f2369b0 2018-12-24 stsp int outfd_child;
1269 9f2369b0 2018-12-24 stsp outfd_child = dup(outfd);
1270 9f2369b0 2018-12-24 stsp if (outfd_child == -1)
1271 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1273 d5c81d44 2021-07-08 stsp err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1275 9f2369b0 2018-12-24 stsp return err;
1277 9f2369b0 2018-12-24 stsp err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1279 9f2369b0 2018-12-24 stsp return err;
1281 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1283 9f2369b0 2018-12-24 stsp return err;
1285 9f2369b0 2018-12-24 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1286 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
1288 9f2369b0 2018-12-24 stsp return err;
1291 9f2369b0 2018-12-24 stsp static const struct got_error *
1292 ac544f8c 2019-01-13 stsp read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1293 d5c81d44 2021-07-08 stsp int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1295 ddc7b220 2019-09-08 stsp const struct got_error *err;
1296 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1297 9f2369b0 2018-12-24 stsp pid_t pid;
1298 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1300 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1301 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1302 d5c81d44 2021-07-08 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1306 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1307 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1308 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1310 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1311 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1312 ddc7b220 2019-09-08 stsp free(ibuf);
1313 ddc7b220 2019-09-08 stsp return err;
1316 9f2369b0 2018-12-24 stsp pid = fork();
1317 ddc7b220 2019-09-08 stsp if (pid == -1) {
1318 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1319 ddc7b220 2019-09-08 stsp free(ibuf);
1320 ddc7b220 2019-09-08 stsp return err;
1322 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1323 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1324 9f2369b0 2018-12-24 stsp repo->path);
1325 9f2369b0 2018-12-24 stsp /* not reached */
1328 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
1329 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1330 ddc7b220 2019-09-08 stsp free(ibuf);
1331 ddc7b220 2019-09-08 stsp return err;
1333 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1334 9f2369b0 2018-12-24 stsp imsg_fds[0];
1335 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1336 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1337 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1339 d5c81d44 2021-07-08 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1342 ebc55e2d 2018-12-24 stsp static const struct got_error *
1343 ebc55e2d 2018-12-24 stsp open_blob(struct got_blob_object **blob, struct got_repository *repo,
1344 ebc55e2d 2018-12-24 stsp struct got_object_id *id, size_t blocksize)
1346 ebc55e2d 2018-12-24 stsp const struct got_error *err = NULL;
1347 ebc55e2d 2018-12-24 stsp struct got_packidx *packidx = NULL;
1349 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1350 ac544f8c 2019-01-13 stsp uint8_t *outbuf;
1351 e82b1d81 2019-07-27 stsp int outfd;
1352 ebc55e2d 2018-12-24 stsp size_t size, hdrlen;
1353 ebc55e2d 2018-12-24 stsp struct stat sb;
1355 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
1356 4558fcd4 2018-01-14 stsp if (*blob == NULL)
1357 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1359 55da3778 2018-09-10 stsp outfd = got_opentempfd();
1360 55da3778 2018-09-10 stsp if (outfd == -1)
1361 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
1363 062ebb78 2018-07-12 stsp (*blob)->read_buf = malloc(blocksize);
1364 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
1365 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1366 c7254d79 2018-04-24 stsp goto done;
1369 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1370 e82b1d81 2019-07-27 stsp if (err == NULL) {
1371 ebc55e2d 2018-12-24 stsp struct got_pack *pack = NULL;
1373 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
1374 aea75d87 2021-07-06 stsp packidx->path_packidx);
1376 ebc55e2d 2018-12-24 stsp goto done;
1378 ebc55e2d 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1379 55da3778 2018-09-10 stsp if (pack == NULL) {
1380 ebc55e2d 2018-12-24 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1383 55da3778 2018-09-10 stsp goto done;
1385 ac544f8c 2019-01-13 stsp err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1386 ac544f8c 2019-01-13 stsp pack, packidx, idx, id);
1387 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1390 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&infd, id, repo);
1392 e82b1d81 2019-07-27 stsp goto done;
1393 ac544f8c 2019-01-13 stsp err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1394 d5c81d44 2021-07-08 stsp id, repo);
1397 55da3778 2018-09-10 stsp goto done;
1399 de060dff 2018-12-24 stsp if (hdrlen > size) {
1400 ebc55e2d 2018-12-24 stsp err = got_error(GOT_ERR_BAD_OBJ_HDR);
1401 ebc55e2d 2018-12-24 stsp goto done;
1404 ac544f8c 2019-01-13 stsp if (outbuf) {
1405 08578a35 2021-01-22 stsp if (close(outfd) == -1 && err == NULL)
1406 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1407 ac544f8c 2019-01-13 stsp outfd = -1;
1408 ac544f8c 2019-01-13 stsp (*blob)->f = fmemopen(outbuf, size, "rb");
1409 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1410 638f9024 2019-05-13 stsp err = got_error_from_errno("fmemopen");
1411 ac544f8c 2019-01-13 stsp free(outbuf);
1412 ac544f8c 2019-01-13 stsp goto done;
1414 ac544f8c 2019-01-13 stsp (*blob)->data = outbuf;
1416 ac544f8c 2019-01-13 stsp if (fstat(outfd, &sb) == -1) {
1417 638f9024 2019-05-13 stsp err = got_error_from_errno("fstat");
1418 ac544f8c 2019-01-13 stsp goto done;
1421 ac544f8c 2019-01-13 stsp if (sb.st_size != size) {
1422 ac544f8c 2019-01-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1423 ac544f8c 2019-01-13 stsp goto done;
1426 ac544f8c 2019-01-13 stsp (*blob)->f = fdopen(outfd, "rb");
1427 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1428 638f9024 2019-05-13 stsp err = got_error_from_errno("fdopen");
1429 ac544f8c 2019-01-13 stsp close(outfd);
1430 ac544f8c 2019-01-13 stsp outfd = -1;
1431 ac544f8c 2019-01-13 stsp goto done;
1435 ebc55e2d 2018-12-24 stsp (*blob)->hdrlen = hdrlen;
1436 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
1437 ebc55e2d 2018-12-24 stsp memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1440 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1441 55da3778 2018-09-10 stsp if (err) {
1442 55da3778 2018-09-10 stsp if (*blob) {
1443 7baf5860 2019-03-19 stsp got_object_blob_close(*blob);
1444 55da3778 2018-09-10 stsp *blob = NULL;
1445 55da3778 2018-09-10 stsp } else if (outfd != -1)
1446 55da3778 2018-09-10 stsp close(outfd);
1448 a19581a2 2018-06-21 stsp return err;
1451 a19581a2 2018-06-21 stsp const struct got_error *
1452 a19581a2 2018-06-21 stsp got_object_open_as_blob(struct got_blob_object **blob,
1453 a19581a2 2018-06-21 stsp struct got_repository *repo, struct got_object_id *id,
1454 a19581a2 2018-06-21 stsp size_t blocksize)
1456 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, id, blocksize);
1459 ebc55e2d 2018-12-24 stsp const struct got_error *
1460 ebc55e2d 2018-12-24 stsp got_object_blob_open(struct got_blob_object **blob,
1461 ebc55e2d 2018-12-24 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
1463 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1466 fb43ecf1 2019-02-11 stsp const struct got_error *
1467 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
1469 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL;
1470 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
1471 56b63ca4 2021-01-22 stsp if (blob->f && fclose(blob->f) == EOF)
1472 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1473 ac544f8c 2019-01-13 stsp free(blob->data);
1474 68482ea3 2017-11-27 stsp free(blob);
1475 fb43ecf1 2019-02-11 stsp return err;
1479 8ba819a3 2020-07-23 stsp got_object_blob_rewind(struct got_blob_object *blob)
1481 8ba819a3 2020-07-23 stsp if (blob->f)
1482 8ba819a3 2020-07-23 stsp rewind(blob->f);
1486 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1488 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1492 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
1494 f934cf2c 2018-02-12 stsp return blob->hdrlen;
1497 f934cf2c 2018-02-12 stsp const uint8_t *
1498 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
1500 f934cf2c 2018-02-12 stsp return blob->read_buf;
1503 68482ea3 2017-11-27 stsp const struct got_error *
1504 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1508 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1509 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
1510 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
1511 eb651edf 2018-02-11 stsp *outlenp = n;
1512 35e9ba5d 2018-06-21 stsp return NULL;
1515 35e9ba5d 2018-06-21 stsp const struct got_error *
1516 be659d10 2020-11-18 stsp got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1517 6c4c42e0 2019-06-24 stsp off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1519 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
1520 b6752625 2018-12-24 stsp size_t n, len, hdrlen;
1521 84451b3e 2018-07-10 stsp const uint8_t *buf;
1523 c33ebc60 2020-11-18 stsp const int alloc_chunksz = 512;
1524 c33ebc60 2020-11-18 stsp size_t nalloc = 0;
1525 f595d9bd 2019-08-14 stsp off_t off = 0, total_len = 0;
1527 6c4c42e0 2019-06-24 stsp if (line_offsets)
1528 6c4c42e0 2019-06-24 stsp *line_offsets = NULL;
1529 f595d9bd 2019-08-14 stsp if (filesize)
1530 f595d9bd 2019-08-14 stsp *filesize = 0;
1531 84451b3e 2018-07-10 stsp if (nlines)
1532 84451b3e 2018-07-10 stsp *nlines = 0;
1534 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1536 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
1538 35e9ba5d 2018-06-21 stsp return err;
1539 35e9ba5d 2018-06-21 stsp if (len == 0)
1541 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
1542 b02560ec 2019-08-19 stsp i = hdrlen;
1543 f1cbc3bc 2020-11-18 stsp if (nlines) {
1544 f1cbc3bc 2020-11-18 stsp if (line_offsets && *line_offsets == NULL) {
1545 78695fb7 2019-08-12 stsp /* Have some data but perhaps no '\n'. */
1546 78695fb7 2019-08-12 stsp *nlines = 1;
1547 c33ebc60 2020-11-18 stsp nalloc = alloc_chunksz;
1548 c33ebc60 2020-11-18 stsp *line_offsets = calloc(nalloc,
1549 c33ebc60 2020-11-18 stsp sizeof(**line_offsets));
1550 78695fb7 2019-08-12 stsp if (*line_offsets == NULL)
1551 845785d4 2020-02-02 tracey return got_error_from_errno("calloc");
1553 b02560ec 2019-08-19 stsp /* Skip forward over end of first line. */
1554 b02560ec 2019-08-19 stsp while (i < len) {
1555 b02560ec 2019-08-19 stsp if (buf[i] == '\n')
1560 b02560ec 2019-08-19 stsp /* Scan '\n' offsets in remaining chunk of data. */
1561 b02560ec 2019-08-19 stsp while (i < len) {
1562 b02560ec 2019-08-19 stsp if (buf[i] != '\n') {
1566 f595d9bd 2019-08-14 stsp (*nlines)++;
1567 c33ebc60 2020-11-18 stsp if (line_offsets && nalloc < *nlines) {
1568 c33ebc60 2020-11-18 stsp size_t n = *nlines + alloc_chunksz;
1569 78695fb7 2019-08-12 stsp off_t *o = recallocarray(*line_offsets,
1570 c33ebc60 2020-11-18 stsp nalloc, n, sizeof(**line_offsets));
1571 78695fb7 2019-08-12 stsp if (o == NULL) {
1572 78695fb7 2019-08-12 stsp free(*line_offsets);
1573 78695fb7 2019-08-12 stsp *line_offsets = NULL;
1574 78695fb7 2019-08-12 stsp return got_error_from_errno(
1575 78695fb7 2019-08-12 stsp "recallocarray");
1577 78695fb7 2019-08-12 stsp *line_offsets = o;
1578 c33ebc60 2020-11-18 stsp nalloc = n;
1580 f1cbc3bc 2020-11-18 stsp if (line_offsets) {
1581 f1cbc3bc 2020-11-18 stsp off = total_len + i - hdrlen + 1;
1582 f1cbc3bc 2020-11-18 stsp (*line_offsets)[*nlines - 1] = off;
1587 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
1588 454a6b59 2018-12-24 stsp n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1589 b6752625 2018-12-24 stsp if (n != len - hdrlen)
1590 b6752625 2018-12-24 stsp return got_ferror(outfile, GOT_ERR_IO);
1591 f595d9bd 2019-08-14 stsp total_len += len - hdrlen;
1592 35e9ba5d 2018-06-21 stsp hdrlen = 0;
1593 35e9ba5d 2018-06-21 stsp } while (len != 0);
1595 cbe7f848 2019-02-11 stsp if (fflush(outfile) != 0)
1596 638f9024 2019-05-13 stsp return got_error_from_errno("fflush");
1597 35e9ba5d 2018-06-21 stsp rewind(outfile);
1599 f595d9bd 2019-08-14 stsp if (filesize)
1600 f595d9bd 2019-08-14 stsp *filesize = total_len;
1602 776d4d29 2018-06-17 stsp return NULL;
1605 f4a881ce 2018-11-17 stsp static const struct got_error *
1606 268f7291 2018-12-24 stsp request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1607 268f7291 2018-12-24 stsp int pack_idx, struct got_object_id *id)
1609 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
1611 268f7291 2018-12-24 stsp err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1612 268f7291 2018-12-24 stsp pack_idx);
1614 a158c901 2018-12-23 stsp return err;
1616 a158c901 2018-12-23 stsp return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1619 268f7291 2018-12-24 stsp static const struct got_error *
1620 268f7291 2018-12-24 stsp read_packed_tag_privsep(struct got_tag_object **tag,
1621 268f7291 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1622 268f7291 2018-12-24 stsp struct got_object_id *id)
1624 268f7291 2018-12-24 stsp const struct got_error *err = NULL;
1626 268f7291 2018-12-24 stsp if (pack->privsep_child)
1627 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1629 268f7291 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
1631 268f7291 2018-12-24 stsp return err;
1633 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1636 9f2369b0 2018-12-24 stsp static const struct got_error *
1637 9f2369b0 2018-12-24 stsp request_tag(struct got_tag_object **tag, struct got_repository *repo,
1638 d5c81d44 2021-07-08 stsp int fd, struct got_object_id *id)
1640 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1641 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1643 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1645 d5c81d44 2021-07-08 stsp err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1647 9f2369b0 2018-12-24 stsp return err;
1649 9f2369b0 2018-12-24 stsp return got_privsep_recv_tag(tag, ibuf);
1652 9f2369b0 2018-12-24 stsp static const struct got_error *
1653 9f2369b0 2018-12-24 stsp read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1654 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo)
1656 ddc7b220 2019-09-08 stsp const struct got_error *err;
1657 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1658 9f2369b0 2018-12-24 stsp pid_t pid;
1659 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1661 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1662 d5c81d44 2021-07-08 stsp return request_tag(tag, repo, obj_fd, id);
1664 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1665 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1666 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1668 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1669 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1670 ddc7b220 2019-09-08 stsp free(ibuf);
1671 ddc7b220 2019-09-08 stsp return err;
1674 9f2369b0 2018-12-24 stsp pid = fork();
1675 ddc7b220 2019-09-08 stsp if (pid == -1) {
1676 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1677 ddc7b220 2019-09-08 stsp free(ibuf);
1678 ddc7b220 2019-09-08 stsp return err;
1680 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1681 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1682 9f2369b0 2018-12-24 stsp repo->path);
1683 9f2369b0 2018-12-24 stsp /* not reached */
1686 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
1687 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1688 ddc7b220 2019-09-08 stsp free(ibuf);
1689 ddc7b220 2019-09-08 stsp return err;
1691 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1692 9f2369b0 2018-12-24 stsp imsg_fds[0];
1693 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1694 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1695 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1697 d5c81d44 2021-07-08 stsp return request_tag(tag, repo, obj_fd, id);
1700 a158c901 2018-12-23 stsp static const struct got_error *
1701 268f7291 2018-12-24 stsp open_tag(struct got_tag_object **tag, struct got_repository *repo,
1702 268f7291 2018-12-24 stsp struct got_object_id *id, int check_cache)
1704 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1705 268f7291 2018-12-24 stsp struct got_packidx *packidx = NULL;
1707 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1708 5d844a1e 2019-08-13 stsp struct got_object *obj = NULL;
1709 5d844a1e 2019-08-13 stsp int obj_type = GOT_OBJ_TYPE_ANY;
1711 f4a881ce 2018-11-17 stsp if (check_cache) {
1712 268f7291 2018-12-24 stsp *tag = got_repo_get_cached_tag(repo, id);
1713 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1714 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1715 f4a881ce 2018-11-17 stsp return NULL;
1718 f4a881ce 2018-11-17 stsp *tag = NULL;
1720 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1721 e82b1d81 2019-07-27 stsp if (err == NULL) {
1722 268f7291 2018-12-24 stsp struct got_pack *pack = NULL;
1724 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
1725 aea75d87 2021-07-06 stsp packidx->path_packidx);
1727 268f7291 2018-12-24 stsp return err;
1729 268f7291 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1730 f4a881ce 2018-11-17 stsp if (pack == NULL) {
1731 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1734 8d2c5ea3 2019-08-13 stsp goto done;
1737 992eb9d8 2020-02-07 tracey /* Beware of "lightweight" tags: Check object type first. */
1738 5d844a1e 2019-08-13 stsp err = read_packed_object_privsep(&obj, repo, pack, packidx,
1741 5d844a1e 2019-08-13 stsp goto done;
1742 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1743 5d844a1e 2019-08-13 stsp got_object_close(obj);
1744 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG) {
1745 5d844a1e 2019-08-13 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1746 5d844a1e 2019-08-13 stsp goto done;
1748 5d844a1e 2019-08-13 stsp err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1749 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1752 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1754 e82b1d81 2019-07-27 stsp return err;
1755 d5c81d44 2021-07-08 stsp err = got_object_read_header_privsep(&obj, id, repo, fd);
1757 5d844a1e 2019-08-13 stsp return err;
1758 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1759 5d844a1e 2019-08-13 stsp got_object_close(obj);
1760 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
1761 5d844a1e 2019-08-13 stsp return got_error(GOT_ERR_OBJ_TYPE);
1763 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1765 5d844a1e 2019-08-13 stsp return err;
1766 d5c81d44 2021-07-08 stsp err = read_tag_privsep(tag, fd, id, repo);
1769 f4a881ce 2018-11-17 stsp if (err == NULL) {
1770 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1771 268f7291 2018-12-24 stsp err = got_repo_cache_tag(repo, id, *tag);
1774 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1775 f4a881ce 2018-11-17 stsp return err;
1778 f4a881ce 2018-11-17 stsp const struct got_error *
1779 f4a881ce 2018-11-17 stsp got_object_open_as_tag(struct got_tag_object **tag,
1780 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object_id *id)
1782 f4a881ce 2018-11-17 stsp *tag = got_repo_get_cached_tag(repo, id);
1783 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1784 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1785 f4a881ce 2018-11-17 stsp return NULL;
1788 268f7291 2018-12-24 stsp return open_tag(tag, repo, id, 0);
1791 f4a881ce 2018-11-17 stsp const struct got_error *
1792 f4a881ce 2018-11-17 stsp got_object_tag_open(struct got_tag_object **tag,
1793 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object *obj)
1795 268f7291 2018-12-24 stsp return open_tag(tag, repo, got_object_get_id(obj), 1);
1798 d24820bf 2019-08-11 stsp const char *
1799 d24820bf 2019-08-11 stsp got_object_tag_get_name(struct got_tag_object *tag)
1801 d24820bf 2019-08-11 stsp return tag->tag;
1805 0bd18d37 2019-02-01 stsp got_object_tag_get_object_type(struct got_tag_object *tag)
1807 0bd18d37 2019-02-01 stsp return tag->obj_type;
1810 0bd18d37 2019-02-01 stsp struct got_object_id *
1811 0bd18d37 2019-02-01 stsp got_object_tag_get_object_id(struct got_tag_object *tag)
1813 0bd18d37 2019-02-01 stsp return &tag->id;
1817 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_time(struct got_tag_object *tag)
1819 01073a5d 2019-08-22 stsp return tag->tagger_time;
1823 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1825 01073a5d 2019-08-22 stsp return tag->tagger_gmtoff;
1828 01073a5d 2019-08-22 stsp const char *
1829 01073a5d 2019-08-22 stsp got_object_tag_get_tagger(struct got_tag_object *tag)
1831 01073a5d 2019-08-22 stsp return tag->tagger;
1834 01073a5d 2019-08-22 stsp const char *
1835 01073a5d 2019-08-22 stsp got_object_tag_get_message(struct got_tag_object *tag)
1837 01073a5d 2019-08-22 stsp return tag->tagmsg;
1840 776d4d29 2018-06-17 stsp static struct got_tree_entry *
1841 65a9bbe9 2018-09-15 stsp find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1845 63da309a 2018-11-07 stsp /* Note that tree entries are sorted in strncmp() order. */
1846 56e0773d 2019-11-28 stsp for (i = 0; i < tree->nentries; i++) {
1847 56e0773d 2019-11-28 stsp struct got_tree_entry *te = &tree->entries[i];
1848 63da309a 2018-11-07 stsp int cmp = strncmp(te->name, name, len);
1849 63da309a 2018-11-07 stsp if (cmp < 0)
1851 63da309a 2018-11-07 stsp if (cmp > 0)
1853 63da309a 2018-11-07 stsp if (te->name[len] == '\0')
1854 776d4d29 2018-06-17 stsp return te;
1856 eb651edf 2018-02-11 stsp return NULL;
1859 56e0773d 2019-11-28 stsp struct got_tree_entry *
1860 a129376b 2019-03-28 stsp got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1862 a129376b 2019-03-28 stsp return find_entry_by_name(tree, name, strlen(name));
1865 776d4d29 2018-06-17 stsp const struct got_error *
1866 cc8021af 2021-10-12 thomas got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1867 cc8021af 2021-10-12 thomas struct got_repository *repo, struct got_tree_object *tree,
1868 cc8021af 2021-10-12 thomas const char *path)
1870 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
1871 cc8021af 2021-10-12 thomas struct got_tree_object *subtree = NULL;
1872 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
1873 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1874 b7cd37e5 2018-11-18 stsp size_t seglen;
1876 27d434c2 2018-09-15 stsp *id = NULL;
1879 5e54fb30 2019-05-31 stsp while (s[0] == '/')
1882 65a9bbe9 2018-09-15 stsp seglen = 0;
1883 cc8021af 2021-10-12 thomas subtree = tree;
1884 b7cd37e5 2018-11-18 stsp while (*s) {
1885 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
1887 776d4d29 2018-06-17 stsp if (*s != '/') {
1894 cc8021af 2021-10-12 thomas te = find_entry_by_name(subtree, seg, seglen);
1895 db37e2c0 2018-06-21 stsp if (te == NULL) {
1896 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1897 776d4d29 2018-06-17 stsp goto done;
1900 b7cd37e5 2018-11-18 stsp if (*s == '\0')
1903 776d4d29 2018-06-17 stsp seg = s + 1;
1904 65a9bbe9 2018-09-15 stsp seglen = 0;
1907 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
1909 db37e2c0 2018-06-21 stsp te = NULL;
1911 776d4d29 2018-06-17 stsp goto done;
1912 cc8021af 2021-10-12 thomas if (subtree != tree)
1913 cc8021af 2021-10-12 thomas got_object_tree_close(subtree);
1914 cc8021af 2021-10-12 thomas subtree = next_tree;
1919 56e0773d 2019-11-28 stsp *id = got_object_id_dup(&te->id);
1920 27d434c2 2018-09-15 stsp if (*id == NULL)
1921 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
1922 cc8021af 2021-10-12 thomas if (mode)
1923 cc8021af 2021-10-12 thomas *mode = te->mode;
1925 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1927 cc8021af 2021-10-12 thomas if (subtree && subtree != tree)
1928 cc8021af 2021-10-12 thomas got_object_tree_close(subtree);
1929 cc8021af 2021-10-12 thomas return err;
1931 cc8021af 2021-10-12 thomas const struct got_error *
1932 cc8021af 2021-10-12 thomas got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1933 cc8021af 2021-10-12 thomas struct got_object_id *commit_id, const char *path)
1935 cc8021af 2021-10-12 thomas const struct got_error *err = NULL;
1936 cc8021af 2021-10-12 thomas struct got_commit_object *commit = NULL;
1937 cc8021af 2021-10-12 thomas struct got_tree_object *tree = NULL;
1939 cc8021af 2021-10-12 thomas *id = NULL;
1941 cc8021af 2021-10-12 thomas err = got_object_open_as_commit(&commit, repo, commit_id);
1942 cc8021af 2021-10-12 thomas if (err)
1943 cc8021af 2021-10-12 thomas goto done;
1945 cc8021af 2021-10-12 thomas /* Handle opening of root of commit's tree. */
1946 cc8021af 2021-10-12 thomas if (got_path_is_root_dir(path)) {
1947 cc8021af 2021-10-12 thomas *id = got_object_id_dup(commit->tree_id);
1948 cc8021af 2021-10-12 thomas if (*id == NULL)
1949 cc8021af 2021-10-12 thomas err = got_error_from_errno("got_object_id_dup");
1950 cc8021af 2021-10-12 thomas } else {
1951 cc8021af 2021-10-12 thomas err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1952 cc8021af 2021-10-12 thomas if (err)
1953 cc8021af 2021-10-12 thomas goto done;
1954 cc8021af 2021-10-12 thomas err = got_object_tree_find_path(id, NULL, repo, tree, path);
1957 776d4d29 2018-06-17 stsp if (commit)
1958 776d4d29 2018-06-17 stsp got_object_commit_close(commit);
1960 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
1961 776d4d29 2018-06-17 stsp return err;
1965 ac5f2b26 2020-05-05 stsp * Normalize file mode bits to avoid false positive tree entry differences
1966 ac5f2b26 2020-05-05 stsp * in case tree entries have unexpected mode bits set.
1968 ac5f2b26 2020-05-05 stsp static mode_t
1969 ac5f2b26 2020-05-05 stsp normalize_mode_for_comparison(mode_t mode)
1972 ac5f2b26 2020-05-05 stsp * For directories, the only relevant bit is the IFDIR bit.
1973 ac5f2b26 2020-05-05 stsp * This allows us to detect paths changing from a directory
1974 ac5f2b26 2020-05-05 stsp * to a file and vice versa.
1976 ac5f2b26 2020-05-05 stsp if (S_ISDIR(mode))
1977 ac5f2b26 2020-05-05 stsp return mode & S_IFDIR;
1980 40dde666 2020-07-23 stsp * For symlinks, the only relevant bit is the IFLNK bit.
1981 40dde666 2020-07-23 stsp * This allows us to detect paths changing from a symlinks
1982 40dde666 2020-07-23 stsp * to a file or directory and vice versa.
1984 40dde666 2020-07-23 stsp if (S_ISLNK(mode))
1985 40dde666 2020-07-23 stsp return mode & S_IFLNK;
1987 ac5f2b26 2020-05-05 stsp /* For files, the only change we care about is the executable bit. */
1988 ac5f2b26 2020-05-05 stsp return mode & S_IXUSR;
1991 07862c20 2018-09-15 stsp const struct got_error *
1992 07862c20 2018-09-15 stsp got_object_tree_path_changed(int *changed,
1993 07862c20 2018-09-15 stsp struct got_tree_object *tree01, struct got_tree_object *tree02,
1994 07862c20 2018-09-15 stsp const char *path, struct got_repository *repo)
1996 07862c20 2018-09-15 stsp const struct got_error *err = NULL;
1997 07862c20 2018-09-15 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1998 07862c20 2018-09-15 stsp struct got_tree_entry *te1 = NULL, *te2 = NULL;
1999 65a9bbe9 2018-09-15 stsp const char *seg, *s;
2000 3b7f9878 2018-11-18 stsp size_t seglen;
2002 07862c20 2018-09-15 stsp *changed = 0;
2004 07862c20 2018-09-15 stsp /* We not do support comparing the root path. */
2005 61a7d79f 2020-02-29 stsp if (got_path_is_root_dir(path))
2006 63f810e6 2020-02-29 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
2008 07862c20 2018-09-15 stsp tree1 = tree01;
2009 07862c20 2018-09-15 stsp tree2 = tree02;
2011 61a7d79f 2020-02-29 stsp while (*s == '/')
2014 65a9bbe9 2018-09-15 stsp seglen = 0;
2015 3b7f9878 2018-11-18 stsp while (*s) {
2016 07862c20 2018-09-15 stsp struct got_tree_object *next_tree1, *next_tree2;
2017 ac5f2b26 2020-05-05 stsp mode_t mode1, mode2;
2019 07862c20 2018-09-15 stsp if (*s != '/') {
2026 65a9bbe9 2018-09-15 stsp te1 = find_entry_by_name(tree1, seg, seglen);
2027 07862c20 2018-09-15 stsp if (te1 == NULL) {
2028 07862c20 2018-09-15 stsp err = got_error(GOT_ERR_NO_OBJ);
2029 07862c20 2018-09-15 stsp goto done;
2032 e8bfb8f3 2020-12-18 stsp if (tree2)
2033 e8bfb8f3 2020-12-18 stsp te2 = find_entry_by_name(tree2, seg, seglen);
2035 e8bfb8f3 2020-12-18 stsp if (te2) {
2036 e8bfb8f3 2020-12-18 stsp mode1 = normalize_mode_for_comparison(te1->mode);
2037 e8bfb8f3 2020-12-18 stsp mode2 = normalize_mode_for_comparison(te2->mode);
2038 e8bfb8f3 2020-12-18 stsp if (mode1 != mode2) {
2039 e8bfb8f3 2020-12-18 stsp *changed = 1;
2040 e8bfb8f3 2020-12-18 stsp goto done;
2043 e8bfb8f3 2020-12-18 stsp if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2044 e8bfb8f3 2020-12-18 stsp *changed = 0;
2045 e8bfb8f3 2020-12-18 stsp goto done;
2049 3b7f9878 2018-11-18 stsp if (*s == '\0') { /* final path element */
2050 07862c20 2018-09-15 stsp *changed = 1;
2051 07862c20 2018-09-15 stsp goto done;
2054 07862c20 2018-09-15 stsp seg = s + 1;
2056 65a9bbe9 2018-09-15 stsp seglen = 0;
2058 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree1, repo,
2059 56e0773d 2019-11-28 stsp &te1->id);
2060 07862c20 2018-09-15 stsp te1 = NULL;
2062 07862c20 2018-09-15 stsp goto done;
2063 a31cea73 2018-09-15 stsp if (tree1 != tree01)
2064 a31cea73 2018-09-15 stsp got_object_tree_close(tree1);
2065 07862c20 2018-09-15 stsp tree1 = next_tree1;
2067 e8bfb8f3 2020-12-18 stsp if (te2) {
2068 e8bfb8f3 2020-12-18 stsp err = got_object_open_as_tree(&next_tree2, repo,
2069 e8bfb8f3 2020-12-18 stsp &te2->id);
2070 e8bfb8f3 2020-12-18 stsp te2 = NULL;
2072 e8bfb8f3 2020-12-18 stsp goto done;
2073 e8bfb8f3 2020-12-18 stsp if (tree2 != tree02)
2074 e8bfb8f3 2020-12-18 stsp got_object_tree_close(tree2);
2075 e8bfb8f3 2020-12-18 stsp tree2 = next_tree2;
2076 e8bfb8f3 2020-12-18 stsp } else if (tree2) {
2077 e8bfb8f3 2020-12-18 stsp if (tree2 != tree02)
2078 e8bfb8f3 2020-12-18 stsp got_object_tree_close(tree2);
2079 e8bfb8f3 2020-12-18 stsp tree2 = NULL;
2084 a31cea73 2018-09-15 stsp if (tree1 && tree1 != tree01)
2085 07862c20 2018-09-15 stsp got_object_tree_close(tree1);
2086 a31cea73 2018-09-15 stsp if (tree2 && tree2 != tree02)
2087 07862c20 2018-09-15 stsp got_object_tree_close(tree2);
2088 77880158 2018-11-04 stsp return err;
2091 ed175427 2019-05-09 stsp const struct got_error *
2092 ed175427 2019-05-09 stsp got_object_tree_entry_dup(struct got_tree_entry **new_te,
2093 ed175427 2019-05-09 stsp struct got_tree_entry *te)
2095 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
2097 ed175427 2019-05-09 stsp *new_te = calloc(1, sizeof(**new_te));
2098 ed175427 2019-05-09 stsp if (*new_te == NULL)
2099 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
2101 ed175427 2019-05-09 stsp (*new_te)->mode = te->mode;
2102 56e0773d 2019-11-28 stsp memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2103 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2104 8c4eabf2 2019-05-10 stsp return err;
2108 56e0773d 2019-11-28 stsp got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2110 63c5ca5d 2019-08-24 stsp return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2114 e40622f4 2020-07-23 stsp got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2116 e40622f4 2020-07-23 stsp /* S_IFDIR check avoids confusing symlinks with submodules. */
2117 e40622f4 2020-07-23 stsp return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2120 e40622f4 2020-07-23 stsp static const struct got_error *
2121 e40622f4 2020-07-23 stsp resolve_symlink(char **link_target, const char *path,
2122 e40622f4 2020-07-23 stsp struct got_object_id *commit_id, struct got_repository *repo)
2124 e40622f4 2020-07-23 stsp const struct got_error *err = NULL;
2125 dbdd6209 2020-10-19 stsp char buf[PATH_MAX];
2126 e40622f4 2020-07-23 stsp char *name, *parent_path = NULL;
2127 e40622f4 2020-07-23 stsp struct got_object_id *tree_obj_id = NULL;
2128 e40622f4 2020-07-23 stsp struct got_tree_object *tree = NULL;
2129 e40622f4 2020-07-23 stsp struct got_tree_entry *te = NULL;
2131 e40622f4 2020-07-23 stsp *link_target = NULL;
2133 dbdd6209 2020-10-19 stsp if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2134 dbdd6209 2020-10-19 stsp return got_error(GOT_ERR_NO_SPACE);
2136 dbdd6209 2020-10-19 stsp name = basename(buf);
2137 e40622f4 2020-07-23 stsp if (name == NULL)
2138 e40622f4 2020-07-23 stsp return got_error_from_errno2("basename", path);
2140 e40622f4 2020-07-23 stsp err = got_path_dirname(&parent_path, path);
2142 e40622f4 2020-07-23 stsp return err;
2144 e40622f4 2020-07-23 stsp err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
2145 e40622f4 2020-07-23 stsp parent_path);
2146 e40622f4 2020-07-23 stsp if (err) {
2147 e40622f4 2020-07-23 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2148 e40622f4 2020-07-23 stsp /* Display the complete path in error message. */
2149 e40622f4 2020-07-23 stsp err = got_error_path(path, err->code);
2151 e40622f4 2020-07-23 stsp goto done;
2154 e40622f4 2020-07-23 stsp err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2156 e40622f4 2020-07-23 stsp goto done;
2158 e40622f4 2020-07-23 stsp te = got_object_tree_find_entry(tree, name);
2159 e40622f4 2020-07-23 stsp if (te == NULL) {
2160 e40622f4 2020-07-23 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2161 e40622f4 2020-07-23 stsp goto done;
2164 e40622f4 2020-07-23 stsp if (got_object_tree_entry_is_symlink(te)) {
2165 e40622f4 2020-07-23 stsp err = got_tree_entry_get_symlink_target(link_target, te, repo);
2167 e40622f4 2020-07-23 stsp goto done;
2168 e40622f4 2020-07-23 stsp if (!got_path_is_absolute(*link_target)) {
2169 e40622f4 2020-07-23 stsp char *abspath;
2170 e40622f4 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", parent_path,
2171 e40622f4 2020-07-23 stsp *link_target) == -1) {
2172 e40622f4 2020-07-23 stsp err = got_error_from_errno("asprintf");
2173 e40622f4 2020-07-23 stsp goto done;
2175 e40622f4 2020-07-23 stsp free(*link_target);
2176 e40622f4 2020-07-23 stsp *link_target = malloc(PATH_MAX);
2177 e40622f4 2020-07-23 stsp if (*link_target == NULL) {
2178 e40622f4 2020-07-23 stsp err = got_error_from_errno("malloc");
2179 e40622f4 2020-07-23 stsp goto done;
2181 e40622f4 2020-07-23 stsp err = got_canonpath(abspath, *link_target, PATH_MAX);
2182 e40622f4 2020-07-23 stsp free(abspath);
2184 e40622f4 2020-07-23 stsp goto done;
2188 e40622f4 2020-07-23 stsp free(tree_obj_id);
2190 e40622f4 2020-07-23 stsp got_object_tree_close(tree);
2191 e40622f4 2020-07-23 stsp if (err) {
2192 e40622f4 2020-07-23 stsp free(*link_target);
2193 e40622f4 2020-07-23 stsp *link_target = NULL;
2195 e40622f4 2020-07-23 stsp return err;
2198 ca6e02ac 2020-01-07 stsp const struct got_error *
2199 e40622f4 2020-07-23 stsp got_object_resolve_symlinks(char **link_target, const char *path,
2200 e40622f4 2020-07-23 stsp struct got_object_id *commit_id, struct got_repository *repo)
2202 e40622f4 2020-07-23 stsp const struct got_error *err = NULL;
2203 e40622f4 2020-07-23 stsp char *next_target = NULL;
2204 e40622f4 2020-07-23 stsp int max_recursion = 40; /* matches Git */
2206 e40622f4 2020-07-23 stsp *link_target = NULL;
2209 e40622f4 2020-07-23 stsp err = resolve_symlink(&next_target,
2210 e40622f4 2020-07-23 stsp *link_target ? *link_target : path, commit_id, repo);
2213 e40622f4 2020-07-23 stsp if (next_target) {
2214 e40622f4 2020-07-23 stsp free(*link_target);
2215 e40622f4 2020-07-23 stsp if (--max_recursion == 0) {
2216 e40622f4 2020-07-23 stsp err = got_error_path(path, GOT_ERR_RECURSION);
2217 e40622f4 2020-07-23 stsp *link_target = NULL;
2220 e40622f4 2020-07-23 stsp *link_target = next_target;
2222 e40622f4 2020-07-23 stsp } while (next_target);
2224 e40622f4 2020-07-23 stsp return err;
2227 e40622f4 2020-07-23 stsp const struct got_error *
2228 ca6e02ac 2020-01-07 stsp got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2229 ca6e02ac 2020-01-07 stsp struct got_object_id *commit_id, const char *path,
2230 ca6e02ac 2020-01-07 stsp struct got_repository *repo)
2232 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
2233 ca6e02ac 2020-01-07 stsp struct got_pack *pack = NULL;
2234 ca6e02ac 2020-01-07 stsp struct got_packidx *packidx = NULL;
2235 ca6e02ac 2020-01-07 stsp char *path_packfile = NULL;
2236 ca6e02ac 2020-01-07 stsp struct got_commit_object *changed_commit = NULL;
2237 ca6e02ac 2020-01-07 stsp struct got_object_id *changed_commit_id = NULL;
2240 ca6e02ac 2020-01-07 stsp err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2241 ca6e02ac 2020-01-07 stsp if (err) {
2242 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
2243 ca6e02ac 2020-01-07 stsp return err;
2244 ca6e02ac 2020-01-07 stsp return NULL;
2247 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
2248 aea75d87 2021-07-06 stsp packidx->path_packidx);
2250 ca6e02ac 2020-01-07 stsp return err;
2252 ca6e02ac 2020-01-07 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
2253 ca6e02ac 2020-01-07 stsp if (pack == NULL) {
2254 ca6e02ac 2020-01-07 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2256 ca6e02ac 2020-01-07 stsp goto done;
2259 ca6e02ac 2020-01-07 stsp if (pack->privsep_child == NULL) {
2260 ca6e02ac 2020-01-07 stsp err = start_pack_privsep_child(pack, packidx);
2262 ca6e02ac 2020-01-07 stsp goto done;
2265 ca6e02ac 2020-01-07 stsp err = got_privsep_send_commit_traversal_request(
2266 ca6e02ac 2020-01-07 stsp pack->privsep_child->ibuf, commit_id, idx, path);
2268 ca6e02ac 2020-01-07 stsp goto done;
2270 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_traversed_commits(&changed_commit,
2271 ca6e02ac 2020-01-07 stsp &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2273 ca6e02ac 2020-01-07 stsp goto done;
2275 ca6e02ac 2020-01-07 stsp if (changed_commit) {
2277 ca6e02ac 2020-01-07 stsp * Cache the commit in which the path was changed.
2278 ca6e02ac 2020-01-07 stsp * This commit might be opened again soon.
2280 ca6e02ac 2020-01-07 stsp changed_commit->refcnt++;
2281 ca6e02ac 2020-01-07 stsp err = got_repo_cache_commit(repo, changed_commit_id,
2282 ca6e02ac 2020-01-07 stsp changed_commit);
2283 ca6e02ac 2020-01-07 stsp got_object_commit_close(changed_commit);
2286 ca6e02ac 2020-01-07 stsp free(path_packfile);
2287 ca6e02ac 2020-01-07 stsp free(changed_commit_id);
2288 ca6e02ac 2020-01-07 stsp return err;