2 8e359fa0 2022-10-13 stsp * Copyright (c) 2018, 2019, 2022 Stefan Sperling <stsp@openbsd.org>
4 8e359fa0 2022-10-13 stsp * Permission to use, copy, modify, and distribute this software for any
5 8e359fa0 2022-10-13 stsp * purpose with or without fee is hereby granted, provided that the above
6 8e359fa0 2022-10-13 stsp * copyright notice and this permission notice appear in all copies.
8 8e359fa0 2022-10-13 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 8e359fa0 2022-10-13 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 8e359fa0 2022-10-13 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 8e359fa0 2022-10-13 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 8e359fa0 2022-10-13 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 8e359fa0 2022-10-13 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 8e359fa0 2022-10-13 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 8e359fa0 2022-10-13 stsp #include <sys/mman.h>
18 8e359fa0 2022-10-13 stsp #include <sys/queue.h>
19 8e359fa0 2022-10-13 stsp #include <sys/types.h>
20 8e359fa0 2022-10-13 stsp #include <sys/tree.h>
21 8e359fa0 2022-10-13 stsp #include <sys/stat.h>
22 8e359fa0 2022-10-13 stsp #include <sys/socket.h>
23 8e359fa0 2022-10-13 stsp #include <sys/uio.h>
25 8e359fa0 2022-10-13 stsp #include <errno.h>
26 8e359fa0 2022-10-13 stsp #include <imsg.h>
27 8e359fa0 2022-10-13 stsp #include <stdio.h>
28 8e359fa0 2022-10-13 stsp #include <stdint.h>
29 8e359fa0 2022-10-13 stsp #include <stdlib.h>
30 8e359fa0 2022-10-13 stsp #include <string.h>
31 8e359fa0 2022-10-13 stsp #include <sha1.h>
32 8e359fa0 2022-10-13 stsp #include <limits.h>
33 8e359fa0 2022-10-13 stsp #include <unistd.h>
35 8e359fa0 2022-10-13 stsp #include "got_error.h"
36 8e359fa0 2022-10-13 stsp #include "got_object.h"
37 8e359fa0 2022-10-13 stsp #include "got_repository.h"
38 8e359fa0 2022-10-13 stsp #include "got_opentemp.h"
39 8e359fa0 2022-10-13 stsp #include "got_path.h"
41 8e359fa0 2022-10-13 stsp #include "got_lib_delta.h"
42 8e359fa0 2022-10-13 stsp #include "got_lib_object.h"
43 8e359fa0 2022-10-13 stsp #include "got_lib_privsep.h"
44 8e359fa0 2022-10-13 stsp #include "got_lib_object_cache.h"
45 8e359fa0 2022-10-13 stsp #include "got_lib_pack.h"
46 8e359fa0 2022-10-13 stsp #include "got_lib_repository.h"
48 8e359fa0 2022-10-13 stsp static const struct got_error *
49 8e359fa0 2022-10-13 stsp request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
50 8e359fa0 2022-10-13 stsp struct got_object_id *id)
52 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
53 8e359fa0 2022-10-13 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
55 8e359fa0 2022-10-13 stsp err = got_privsep_send_packed_obj_req(ibuf, idx, id);
59 8e359fa0 2022-10-13 stsp err = got_privsep_recv_obj(obj, ibuf);
63 8e359fa0 2022-10-13 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
65 8e359fa0 2022-10-13 stsp return NULL;
68 8e359fa0 2022-10-13 stsp /* Create temporary files used during delta application. */
69 8e359fa0 2022-10-13 stsp static const struct got_error *
70 8e359fa0 2022-10-13 stsp pack_child_send_tempfiles(struct imsgbuf *ibuf, struct got_pack *pack)
72 8e359fa0 2022-10-13 stsp const struct got_error *err;
73 8e359fa0 2022-10-13 stsp int basefd = -1, accumfd = -1;
76 8e359fa0 2022-10-13 stsp * For performance reasons, the child will keep reusing the
77 8e359fa0 2022-10-13 stsp * same temporary files during every object request.
78 8e359fa0 2022-10-13 stsp * Opening and closing new files for every object request is
79 8e359fa0 2022-10-13 stsp * too expensive during operations such as 'gotadmin pack'.
81 8e359fa0 2022-10-13 stsp if (pack->child_has_tempfiles)
82 8e359fa0 2022-10-13 stsp return NULL;
84 8e359fa0 2022-10-13 stsp basefd = dup(pack->basefd);
85 8e359fa0 2022-10-13 stsp if (basefd == -1)
86 8e359fa0 2022-10-13 stsp return got_error_from_errno("dup");
88 8e359fa0 2022-10-13 stsp accumfd = dup(pack->accumfd);
89 8e359fa0 2022-10-13 stsp if (accumfd == -1) {
90 8e359fa0 2022-10-13 stsp err = got_error_from_errno("dup");
94 8e359fa0 2022-10-13 stsp err = got_privsep_send_tmpfd(ibuf, basefd);
98 8e359fa0 2022-10-13 stsp err = got_privsep_send_tmpfd(ibuf, accumfd);
101 8e359fa0 2022-10-13 stsp if (basefd != -1)
102 8e359fa0 2022-10-13 stsp close(basefd);
103 8e359fa0 2022-10-13 stsp if (accumfd != -1)
104 8e359fa0 2022-10-13 stsp close(accumfd);
106 8e359fa0 2022-10-13 stsp pack->child_has_tempfiles = 1;
107 8e359fa0 2022-10-13 stsp return NULL;
110 8e359fa0 2022-10-13 stsp static const struct got_error *
111 8e359fa0 2022-10-13 stsp request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
112 8e359fa0 2022-10-13 stsp int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
114 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
115 8e359fa0 2022-10-13 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
116 8e359fa0 2022-10-13 stsp int outfd_child;
118 8e359fa0 2022-10-13 stsp err = pack_child_send_tempfiles(ibuf, pack);
120 8e359fa0 2022-10-13 stsp return err;
122 8e359fa0 2022-10-13 stsp outfd_child = dup(outfd);
123 8e359fa0 2022-10-13 stsp if (outfd_child == -1)
124 8e359fa0 2022-10-13 stsp return got_error_from_errno("dup");
126 8e359fa0 2022-10-13 stsp err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
128 8e359fa0 2022-10-13 stsp close(outfd_child);
129 8e359fa0 2022-10-13 stsp return err;
132 8e359fa0 2022-10-13 stsp err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
134 8e359fa0 2022-10-13 stsp return err;
136 8e359fa0 2022-10-13 stsp err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
138 8e359fa0 2022-10-13 stsp return err;
140 8e359fa0 2022-10-13 stsp return NULL;
143 8e359fa0 2022-10-13 stsp static const struct got_error *
144 8e359fa0 2022-10-13 stsp read_packed_object_privsep(struct got_object **obj,
145 8e359fa0 2022-10-13 stsp struct got_repository *repo, struct got_pack *pack,
146 8e359fa0 2022-10-13 stsp struct got_packidx *packidx, int idx, struct got_object_id *id)
148 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
150 8e359fa0 2022-10-13 stsp if (pack->privsep_child == NULL) {
151 8e359fa0 2022-10-13 stsp err = got_pack_start_privsep_child(pack, packidx);
153 8e359fa0 2022-10-13 stsp return err;
156 8e359fa0 2022-10-13 stsp return request_packed_object(obj, pack, idx, id);
159 8e359fa0 2022-10-13 stsp static const struct got_error *
160 8e359fa0 2022-10-13 stsp read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
161 8e359fa0 2022-10-13 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
162 8e359fa0 2022-10-13 stsp struct got_object_id *id)
164 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
166 8e359fa0 2022-10-13 stsp if (pack->privsep_child == NULL) {
167 8e359fa0 2022-10-13 stsp err = got_pack_start_privsep_child(pack, packidx);
169 8e359fa0 2022-10-13 stsp return err;
172 8e359fa0 2022-10-13 stsp return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
176 8e359fa0 2022-10-13 stsp const struct got_error *
177 8e359fa0 2022-10-13 stsp got_object_open_packed(struct got_object **obj, struct got_object_id *id,
178 8e359fa0 2022-10-13 stsp struct got_repository *repo)
180 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
181 8e359fa0 2022-10-13 stsp struct got_pack *pack = NULL;
182 8e359fa0 2022-10-13 stsp struct got_packidx *packidx = NULL;
184 8e359fa0 2022-10-13 stsp char *path_packfile;
186 8e359fa0 2022-10-13 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
188 8e359fa0 2022-10-13 stsp return err;
190 8e359fa0 2022-10-13 stsp err = got_packidx_get_packfile_path(&path_packfile,
191 8e359fa0 2022-10-13 stsp packidx->path_packidx);
193 8e359fa0 2022-10-13 stsp return err;
195 8e359fa0 2022-10-13 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
196 8e359fa0 2022-10-13 stsp if (pack == NULL) {
197 8e359fa0 2022-10-13 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
202 8e359fa0 2022-10-13 stsp err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
206 8e359fa0 2022-10-13 stsp free(path_packfile);
207 8e359fa0 2022-10-13 stsp return err;
210 8e359fa0 2022-10-13 stsp const struct got_error *
211 8e359fa0 2022-10-13 stsp got_object_open_from_packfile(struct got_object **obj, struct got_object_id *id,
212 8e359fa0 2022-10-13 stsp struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
213 8e359fa0 2022-10-13 stsp struct got_repository *repo)
215 8e359fa0 2022-10-13 stsp return read_packed_object_privsep(obj, repo, pack, packidx,
216 8e359fa0 2022-10-13 stsp obj_idx, id);
219 8e359fa0 2022-10-13 stsp const struct got_error *
220 8e359fa0 2022-10-13 stsp got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
221 8e359fa0 2022-10-13 stsp off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
222 8e359fa0 2022-10-13 stsp off_t *delta_out_offset, struct got_object_id **base_id, int delta_cache_fd,
223 8e359fa0 2022-10-13 stsp struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
224 8e359fa0 2022-10-13 stsp struct got_repository *repo)
226 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
227 8e359fa0 2022-10-13 stsp struct got_pack *pack = NULL;
228 8e359fa0 2022-10-13 stsp char *path_packfile;
230 8e359fa0 2022-10-13 stsp *base_size = 0;
231 8e359fa0 2022-10-13 stsp *result_size = 0;
232 8e359fa0 2022-10-13 stsp *delta_size = 0;
233 8e359fa0 2022-10-13 stsp *delta_compressed_size = 0;
234 8e359fa0 2022-10-13 stsp *delta_offset = 0;
235 8e359fa0 2022-10-13 stsp *delta_out_offset = 0;
237 8e359fa0 2022-10-13 stsp err = got_packidx_get_packfile_path(&path_packfile,
238 8e359fa0 2022-10-13 stsp packidx->path_packidx);
240 8e359fa0 2022-10-13 stsp return err;
242 8e359fa0 2022-10-13 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
243 8e359fa0 2022-10-13 stsp if (pack == NULL) {
244 8e359fa0 2022-10-13 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
246 8e359fa0 2022-10-13 stsp return err;
249 8e359fa0 2022-10-13 stsp if (pack->privsep_child == NULL) {
250 8e359fa0 2022-10-13 stsp err = got_pack_start_privsep_child(pack, packidx);
252 8e359fa0 2022-10-13 stsp return err;
255 8e359fa0 2022-10-13 stsp if (!pack->child_has_delta_outfd) {
256 8e359fa0 2022-10-13 stsp int outfd_child;
257 8e359fa0 2022-10-13 stsp outfd_child = dup(delta_cache_fd);
258 8e359fa0 2022-10-13 stsp if (outfd_child == -1)
259 8e359fa0 2022-10-13 stsp return got_error_from_errno("dup");
260 8e359fa0 2022-10-13 stsp err = got_privsep_send_raw_delta_outfd(
261 8e359fa0 2022-10-13 stsp pack->privsep_child->ibuf, outfd_child);
263 8e359fa0 2022-10-13 stsp return err;
264 8e359fa0 2022-10-13 stsp pack->child_has_delta_outfd = 1;
267 8e359fa0 2022-10-13 stsp err = got_privsep_send_raw_delta_req(pack->privsep_child->ibuf,
268 8e359fa0 2022-10-13 stsp obj_idx, id);
270 8e359fa0 2022-10-13 stsp return err;
272 8e359fa0 2022-10-13 stsp return got_privsep_recv_raw_delta(base_size, result_size, delta_size,
273 8e359fa0 2022-10-13 stsp delta_compressed_size, delta_offset, delta_out_offset, base_id,
274 8e359fa0 2022-10-13 stsp pack->privsep_child->ibuf);
277 8e359fa0 2022-10-13 stsp static const struct got_error *
278 8e359fa0 2022-10-13 stsp request_object(struct got_object **obj, struct got_object_id *id,
279 8e359fa0 2022-10-13 stsp struct got_repository *repo, int fd)
281 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
282 8e359fa0 2022-10-13 stsp struct imsgbuf *ibuf;
284 8e359fa0 2022-10-13 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
286 8e359fa0 2022-10-13 stsp err = got_privsep_send_obj_req(ibuf, fd, id);
288 8e359fa0 2022-10-13 stsp return err;
290 8e359fa0 2022-10-13 stsp return got_privsep_recv_obj(obj, ibuf);
293 8e359fa0 2022-10-13 stsp static const struct got_error *
294 8e359fa0 2022-10-13 stsp request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
295 8e359fa0 2022-10-13 stsp struct got_object_id *id, struct got_repository *repo, int infd)
297 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
298 8e359fa0 2022-10-13 stsp struct imsgbuf *ibuf;
299 8e359fa0 2022-10-13 stsp int outfd_child;
301 8e359fa0 2022-10-13 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
303 8e359fa0 2022-10-13 stsp outfd_child = dup(outfd);
304 8e359fa0 2022-10-13 stsp if (outfd_child == -1)
305 8e359fa0 2022-10-13 stsp return got_error_from_errno("dup");
307 8e359fa0 2022-10-13 stsp err = got_privsep_send_raw_obj_req(ibuf, infd, id);
309 8e359fa0 2022-10-13 stsp return err;
311 8e359fa0 2022-10-13 stsp err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
313 8e359fa0 2022-10-13 stsp return err;
315 8e359fa0 2022-10-13 stsp return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
318 8e359fa0 2022-10-13 stsp static const struct got_error *
319 1d9e43b0 2022-10-13 stsp start_child(struct got_repository *repo, int type)
321 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
322 8e359fa0 2022-10-13 stsp int imsg_fds[2];
324 8e359fa0 2022-10-13 stsp struct imsgbuf *ibuf;
325 1d9e43b0 2022-10-13 stsp const char *prog_path;
327 1d9e43b0 2022-10-13 stsp switch (type) {
328 1d9e43b0 2022-10-13 stsp case GOT_REPO_PRIVSEP_CHILD_OBJECT:
329 1d9e43b0 2022-10-13 stsp prog_path = GOT_PATH_PROG_READ_OBJECT;
331 1d9e43b0 2022-10-13 stsp case GOT_REPO_PRIVSEP_CHILD_TREE:
332 1d9e43b0 2022-10-13 stsp prog_path = GOT_PATH_PROG_READ_TREE;
334 1d9e43b0 2022-10-13 stsp case GOT_REPO_PRIVSEP_CHILD_COMMIT:
335 1d9e43b0 2022-10-13 stsp prog_path = GOT_PATH_PROG_READ_COMMIT;
337 1d9e43b0 2022-10-13 stsp case GOT_REPO_PRIVSEP_CHILD_BLOB:
338 1d9e43b0 2022-10-13 stsp prog_path = GOT_PATH_PROG_READ_BLOB;
340 1d9e43b0 2022-10-13 stsp case GOT_REPO_PRIVSEP_CHILD_TAG:
341 1d9e43b0 2022-10-13 stsp prog_path = GOT_PATH_PROG_READ_TAG;
344 1d9e43b0 2022-10-13 stsp return got_error(GOT_ERR_OBJ_TYPE);
347 8e359fa0 2022-10-13 stsp ibuf = calloc(1, sizeof(*ibuf));
348 8e359fa0 2022-10-13 stsp if (ibuf == NULL)
349 8e359fa0 2022-10-13 stsp return got_error_from_errno("calloc");
351 8e359fa0 2022-10-13 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
352 8e359fa0 2022-10-13 stsp err = got_error_from_errno("socketpair");
353 8e359fa0 2022-10-13 stsp free(ibuf);
354 8e359fa0 2022-10-13 stsp return err;
357 8e359fa0 2022-10-13 stsp pid = fork();
358 8e359fa0 2022-10-13 stsp if (pid == -1) {
359 8e359fa0 2022-10-13 stsp err = got_error_from_errno("fork");
360 8e359fa0 2022-10-13 stsp free(ibuf);
361 8e359fa0 2022-10-13 stsp return err;
363 8e359fa0 2022-10-13 stsp else if (pid == 0) {
364 1d9e43b0 2022-10-13 stsp got_privsep_exec_child(imsg_fds, prog_path, repo->path);
365 8e359fa0 2022-10-13 stsp /* not reached */
368 8e359fa0 2022-10-13 stsp if (close(imsg_fds[1]) == -1) {
369 8e359fa0 2022-10-13 stsp err = got_error_from_errno("close");
370 8e359fa0 2022-10-13 stsp free(ibuf);
371 8e359fa0 2022-10-13 stsp return err;
374 1d9e43b0 2022-10-13 stsp repo->privsep_children[type].imsg_fd = imsg_fds[0];
375 1d9e43b0 2022-10-13 stsp repo->privsep_children[type].pid = pid;
376 8e359fa0 2022-10-13 stsp imsg_init(ibuf, imsg_fds[0]);
377 1d9e43b0 2022-10-13 stsp repo->privsep_children[type].ibuf = ibuf;
379 8e359fa0 2022-10-13 stsp return NULL;
382 8e359fa0 2022-10-13 stsp const struct got_error *
383 8e359fa0 2022-10-13 stsp got_object_read_header_privsep(struct got_object **obj,
384 8e359fa0 2022-10-13 stsp struct got_object_id *id, struct got_repository *repo, int obj_fd)
386 8e359fa0 2022-10-13 stsp const struct got_error *err;
388 8e359fa0 2022-10-13 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
389 8e359fa0 2022-10-13 stsp return request_object(obj, id, repo, obj_fd);
391 1d9e43b0 2022-10-13 stsp err = start_child(repo, GOT_REPO_PRIVSEP_CHILD_OBJECT);
393 8e359fa0 2022-10-13 stsp return err;
395 8e359fa0 2022-10-13 stsp return request_object(obj, id, repo, obj_fd);
398 8e359fa0 2022-10-13 stsp static const struct got_error *
399 8e359fa0 2022-10-13 stsp read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
400 8e359fa0 2022-10-13 stsp int outfd, struct got_object_id *id, struct got_repository *repo,
401 8e359fa0 2022-10-13 stsp int obj_fd)
403 8e359fa0 2022-10-13 stsp const struct got_error *err;
405 8e359fa0 2022-10-13 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
406 8e359fa0 2022-10-13 stsp return request_raw_object(outbuf, size, hdrlen, outfd, id,
407 8e359fa0 2022-10-13 stsp repo, obj_fd);
409 1d9e43b0 2022-10-13 stsp err = start_child(repo, GOT_REPO_PRIVSEP_CHILD_OBJECT);
411 8e359fa0 2022-10-13 stsp return err;
413 8e359fa0 2022-10-13 stsp return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
417 8e359fa0 2022-10-13 stsp const struct got_error *
418 8e359fa0 2022-10-13 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
419 8e359fa0 2022-10-13 stsp struct got_object_id *id)
421 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
424 8e359fa0 2022-10-13 stsp *obj = got_repo_get_cached_object(repo, id);
425 8e359fa0 2022-10-13 stsp if (*obj != NULL) {
426 8e359fa0 2022-10-13 stsp (*obj)->refcnt++;
427 8e359fa0 2022-10-13 stsp return NULL;
430 8e359fa0 2022-10-13 stsp err = got_object_open_packed(obj, id, repo);
431 8e359fa0 2022-10-13 stsp if (err && err->code != GOT_ERR_NO_OBJ)
432 8e359fa0 2022-10-13 stsp return err;
433 8e359fa0 2022-10-13 stsp if (*obj) {
434 8e359fa0 2022-10-13 stsp (*obj)->refcnt++;
435 8e359fa0 2022-10-13 stsp return got_repo_cache_object(repo, id, *obj);
438 8e359fa0 2022-10-13 stsp err = got_object_open_loose_fd(&fd, id, repo);
440 8e359fa0 2022-10-13 stsp if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
441 8e359fa0 2022-10-13 stsp err = got_error_no_obj(id);
442 8e359fa0 2022-10-13 stsp return err;
445 8e359fa0 2022-10-13 stsp err = got_object_read_header_privsep(obj, id, repo, fd);
447 8e359fa0 2022-10-13 stsp return err;
449 8e359fa0 2022-10-13 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
451 8e359fa0 2022-10-13 stsp (*obj)->refcnt++;
452 8e359fa0 2022-10-13 stsp return got_repo_cache_object(repo, id, *obj);
455 8e359fa0 2022-10-13 stsp /* *outfd must be initialized to -1 by caller */
456 8e359fa0 2022-10-13 stsp const struct got_error *
457 8e359fa0 2022-10-13 stsp got_object_raw_open(struct got_raw_object **obj, int *outfd,
458 8e359fa0 2022-10-13 stsp struct got_repository *repo, struct got_object_id *id)
460 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
461 8e359fa0 2022-10-13 stsp struct got_packidx *packidx = NULL;
463 8e359fa0 2022-10-13 stsp uint8_t *outbuf = NULL;
464 8e359fa0 2022-10-13 stsp off_t size = 0;
465 8e359fa0 2022-10-13 stsp size_t hdrlen = 0;
466 8e359fa0 2022-10-13 stsp char *path_packfile = NULL;
468 8e359fa0 2022-10-13 stsp *obj = got_repo_get_cached_raw_object(repo, id);
469 8e359fa0 2022-10-13 stsp if (*obj != NULL) {
470 8e359fa0 2022-10-13 stsp (*obj)->refcnt++;
471 8e359fa0 2022-10-13 stsp return NULL;
474 8e359fa0 2022-10-13 stsp if (*outfd == -1) {
475 8e359fa0 2022-10-13 stsp *outfd = got_opentempfd();
476 8e359fa0 2022-10-13 stsp if (*outfd == -1)
477 8e359fa0 2022-10-13 stsp return got_error_from_errno("got_opentempfd");
480 8e359fa0 2022-10-13 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
481 8e359fa0 2022-10-13 stsp if (err == NULL) {
482 8e359fa0 2022-10-13 stsp struct got_pack *pack = NULL;
484 8e359fa0 2022-10-13 stsp err = got_packidx_get_packfile_path(&path_packfile,
485 8e359fa0 2022-10-13 stsp packidx->path_packidx);
489 8e359fa0 2022-10-13 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
490 8e359fa0 2022-10-13 stsp if (pack == NULL) {
491 8e359fa0 2022-10-13 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
496 8e359fa0 2022-10-13 stsp err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
497 8e359fa0 2022-10-13 stsp *outfd, pack, packidx, idx, id);
500 8e359fa0 2022-10-13 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
503 8e359fa0 2022-10-13 stsp err = got_object_open_loose_fd(&fd, id, repo);
506 8e359fa0 2022-10-13 stsp err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
507 8e359fa0 2022-10-13 stsp id, repo, fd);
512 8e359fa0 2022-10-13 stsp *obj = calloc(1, sizeof(**obj));
513 8e359fa0 2022-10-13 stsp if (*obj == NULL) {
514 8e359fa0 2022-10-13 stsp err = got_error_from_errno("calloc");
517 8e359fa0 2022-10-13 stsp (*obj)->fd = -1;
519 8e359fa0 2022-10-13 stsp if (outbuf) {
520 8e359fa0 2022-10-13 stsp (*obj)->data = outbuf;
522 8e359fa0 2022-10-13 stsp struct stat sb;
523 8e359fa0 2022-10-13 stsp if (fstat(*outfd, &sb) == -1) {
524 8e359fa0 2022-10-13 stsp err = got_error_from_errno("fstat");
528 8e359fa0 2022-10-13 stsp if (sb.st_size != hdrlen + size) {
529 8e359fa0 2022-10-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
532 8e359fa0 2022-10-13 stsp #ifndef GOT_PACK_NO_MMAP
533 8e359fa0 2022-10-13 stsp if (hdrlen + size > 0) {
534 8e359fa0 2022-10-13 stsp (*obj)->data = mmap(NULL, hdrlen + size, PROT_READ,
535 8e359fa0 2022-10-13 stsp MAP_PRIVATE, *outfd, 0);
536 8e359fa0 2022-10-13 stsp if ((*obj)->data == MAP_FAILED) {
537 8e359fa0 2022-10-13 stsp if (errno != ENOMEM) {
538 8e359fa0 2022-10-13 stsp err = got_error_from_errno("mmap");
541 8e359fa0 2022-10-13 stsp (*obj)->data = NULL;
543 8e359fa0 2022-10-13 stsp (*obj)->fd = *outfd;
544 8e359fa0 2022-10-13 stsp *outfd = -1;
548 8e359fa0 2022-10-13 stsp if (*outfd != -1) {
549 8e359fa0 2022-10-13 stsp (*obj)->f = fdopen(*outfd, "r");
550 8e359fa0 2022-10-13 stsp if ((*obj)->f == NULL) {
551 8e359fa0 2022-10-13 stsp err = got_error_from_errno("fdopen");
554 8e359fa0 2022-10-13 stsp *outfd = -1;
557 8e359fa0 2022-10-13 stsp (*obj)->hdrlen = hdrlen;
558 8e359fa0 2022-10-13 stsp (*obj)->size = size;
559 8e359fa0 2022-10-13 stsp err = got_repo_cache_raw_object(repo, id, *obj);
561 8e359fa0 2022-10-13 stsp free(path_packfile);
563 8e359fa0 2022-10-13 stsp if (*obj) {
564 8e359fa0 2022-10-13 stsp got_object_raw_close(*obj);
565 8e359fa0 2022-10-13 stsp *obj = NULL;
567 8e359fa0 2022-10-13 stsp free(outbuf);
569 8e359fa0 2022-10-13 stsp (*obj)->refcnt++;
570 8e359fa0 2022-10-13 stsp return err;
573 8e359fa0 2022-10-13 stsp static const struct got_error *
574 8e359fa0 2022-10-13 stsp request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
575 8e359fa0 2022-10-13 stsp int pack_idx, struct got_object_id *id)
577 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
579 8e359fa0 2022-10-13 stsp err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
582 8e359fa0 2022-10-13 stsp return err;
584 8e359fa0 2022-10-13 stsp err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
586 8e359fa0 2022-10-13 stsp return err;
588 8e359fa0 2022-10-13 stsp (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
589 8e359fa0 2022-10-13 stsp return NULL;
592 8e359fa0 2022-10-13 stsp static const struct got_error *
593 8e359fa0 2022-10-13 stsp read_packed_commit_privsep(struct got_commit_object **commit,
594 8e359fa0 2022-10-13 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
595 8e359fa0 2022-10-13 stsp struct got_object_id *id)
597 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
599 8e359fa0 2022-10-13 stsp if (pack->privsep_child)
600 8e359fa0 2022-10-13 stsp return request_packed_commit(commit, pack, idx, id);
602 8e359fa0 2022-10-13 stsp err = got_pack_start_privsep_child(pack, packidx);
604 8e359fa0 2022-10-13 stsp return err;
606 8e359fa0 2022-10-13 stsp return request_packed_commit(commit, pack, idx, id);
609 8e359fa0 2022-10-13 stsp static const struct got_error *
610 8e359fa0 2022-10-13 stsp request_commit(struct got_commit_object **commit, struct got_repository *repo,
611 8e359fa0 2022-10-13 stsp int fd, struct got_object_id *id)
613 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
614 8e359fa0 2022-10-13 stsp struct imsgbuf *ibuf;
616 8e359fa0 2022-10-13 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
618 8e359fa0 2022-10-13 stsp err = got_privsep_send_commit_req(ibuf, fd, id, -1);
620 8e359fa0 2022-10-13 stsp return err;
622 8e359fa0 2022-10-13 stsp return got_privsep_recv_commit(commit, ibuf);
625 8e359fa0 2022-10-13 stsp static const struct got_error *
626 8e359fa0 2022-10-13 stsp read_commit_privsep(struct got_commit_object **commit, int obj_fd,
627 8e359fa0 2022-10-13 stsp struct got_object_id *id, struct got_repository *repo)
629 8e359fa0 2022-10-13 stsp const struct got_error *err;
631 8e359fa0 2022-10-13 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
632 8e359fa0 2022-10-13 stsp return request_commit(commit, repo, obj_fd, id);
634 1d9e43b0 2022-10-13 stsp err = start_child(repo, GOT_REPO_PRIVSEP_CHILD_COMMIT);
636 8e359fa0 2022-10-13 stsp return err;
638 8e359fa0 2022-10-13 stsp return request_commit(commit, repo, obj_fd, id);
641 8e359fa0 2022-10-13 stsp static const struct got_error *
642 8e359fa0 2022-10-13 stsp open_commit(struct got_commit_object **commit,
643 8e359fa0 2022-10-13 stsp struct got_repository *repo, struct got_object_id *id, int check_cache)
645 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
646 8e359fa0 2022-10-13 stsp struct got_packidx *packidx = NULL;
648 8e359fa0 2022-10-13 stsp char *path_packfile = NULL;
650 8e359fa0 2022-10-13 stsp if (check_cache) {
651 8e359fa0 2022-10-13 stsp *commit = got_repo_get_cached_commit(repo, id);
652 8e359fa0 2022-10-13 stsp if (*commit != NULL) {
653 8e359fa0 2022-10-13 stsp (*commit)->refcnt++;
654 8e359fa0 2022-10-13 stsp return NULL;
657 8e359fa0 2022-10-13 stsp *commit = NULL;
659 8e359fa0 2022-10-13 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
660 8e359fa0 2022-10-13 stsp if (err == NULL) {
661 8e359fa0 2022-10-13 stsp struct got_pack *pack = NULL;
663 8e359fa0 2022-10-13 stsp err = got_packidx_get_packfile_path(&path_packfile,
664 8e359fa0 2022-10-13 stsp packidx->path_packidx);
666 8e359fa0 2022-10-13 stsp return err;
668 8e359fa0 2022-10-13 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
669 8e359fa0 2022-10-13 stsp if (pack == NULL) {
670 8e359fa0 2022-10-13 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
675 8e359fa0 2022-10-13 stsp err = read_packed_commit_privsep(commit, pack,
676 8e359fa0 2022-10-13 stsp packidx, idx, id);
677 8e359fa0 2022-10-13 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
680 8e359fa0 2022-10-13 stsp err = got_object_open_loose_fd(&fd, id, repo);
682 8e359fa0 2022-10-13 stsp return err;
683 8e359fa0 2022-10-13 stsp err = read_commit_privsep(commit, fd, id, repo);
686 8e359fa0 2022-10-13 stsp if (err == NULL) {
687 8e359fa0 2022-10-13 stsp (*commit)->refcnt++;
688 8e359fa0 2022-10-13 stsp err = got_repo_cache_commit(repo, id, *commit);
691 8e359fa0 2022-10-13 stsp free(path_packfile);
692 8e359fa0 2022-10-13 stsp return err;
695 8e359fa0 2022-10-13 stsp const struct got_error *
696 8e359fa0 2022-10-13 stsp got_object_open_as_commit(struct got_commit_object **commit,
697 8e359fa0 2022-10-13 stsp struct got_repository *repo, struct got_object_id *id)
699 8e359fa0 2022-10-13 stsp *commit = got_repo_get_cached_commit(repo, id);
700 8e359fa0 2022-10-13 stsp if (*commit != NULL) {
701 8e359fa0 2022-10-13 stsp (*commit)->refcnt++;
702 8e359fa0 2022-10-13 stsp return NULL;
705 8e359fa0 2022-10-13 stsp return open_commit(commit, repo, id, 0);
708 8e359fa0 2022-10-13 stsp const struct got_error *
709 8e359fa0 2022-10-13 stsp got_object_commit_open(struct got_commit_object **commit,
710 8e359fa0 2022-10-13 stsp struct got_repository *repo, struct got_object *obj)
712 8e359fa0 2022-10-13 stsp return open_commit(commit, repo, got_object_get_id(obj), 1);
715 8e359fa0 2022-10-13 stsp static const struct got_error *
716 8e359fa0 2022-10-13 stsp request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
717 8e359fa0 2022-10-13 stsp int pack_idx, struct got_object_id *id)
719 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
721 8e359fa0 2022-10-13 stsp err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
724 8e359fa0 2022-10-13 stsp return err;
726 8e359fa0 2022-10-13 stsp return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
729 8e359fa0 2022-10-13 stsp static const struct got_error *
730 8e359fa0 2022-10-13 stsp read_packed_tree_privsep(struct got_tree_object **tree,
731 8e359fa0 2022-10-13 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
732 8e359fa0 2022-10-13 stsp struct got_object_id *id)
734 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
736 8e359fa0 2022-10-13 stsp if (pack->privsep_child)
737 8e359fa0 2022-10-13 stsp return request_packed_tree(tree, pack, idx, id);
739 8e359fa0 2022-10-13 stsp err = got_pack_start_privsep_child(pack, packidx);
741 8e359fa0 2022-10-13 stsp return err;
743 8e359fa0 2022-10-13 stsp return request_packed_tree(tree, pack, idx, id);
746 8e359fa0 2022-10-13 stsp static const struct got_error *
747 8e359fa0 2022-10-13 stsp request_tree(struct got_tree_object **tree, struct got_repository *repo,
748 8e359fa0 2022-10-13 stsp int fd, struct got_object_id *id)
750 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
751 8e359fa0 2022-10-13 stsp struct imsgbuf *ibuf;
753 8e359fa0 2022-10-13 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
755 8e359fa0 2022-10-13 stsp err = got_privsep_send_tree_req(ibuf, fd, id, -1);
757 8e359fa0 2022-10-13 stsp return err;
759 8e359fa0 2022-10-13 stsp return got_privsep_recv_tree(tree, ibuf);
762 8e359fa0 2022-10-13 stsp static const struct got_error *
763 8e359fa0 2022-10-13 stsp read_tree_privsep(struct got_tree_object **tree, int obj_fd,
764 8e359fa0 2022-10-13 stsp struct got_object_id *id, struct got_repository *repo)
766 8e359fa0 2022-10-13 stsp const struct got_error *err;
768 8e359fa0 2022-10-13 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
769 8e359fa0 2022-10-13 stsp return request_tree(tree, repo, obj_fd, id);
771 1d9e43b0 2022-10-13 stsp err = start_child(repo, GOT_REPO_PRIVSEP_CHILD_TREE);
773 8e359fa0 2022-10-13 stsp return err;
775 8e359fa0 2022-10-13 stsp return request_tree(tree, repo, obj_fd, id);
778 8e359fa0 2022-10-13 stsp static const struct got_error *
779 8e359fa0 2022-10-13 stsp open_tree(struct got_tree_object **tree, struct got_repository *repo,
780 8e359fa0 2022-10-13 stsp struct got_object_id *id, int check_cache)
782 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
783 8e359fa0 2022-10-13 stsp struct got_packidx *packidx = NULL;
785 8e359fa0 2022-10-13 stsp char *path_packfile = NULL;
787 8e359fa0 2022-10-13 stsp if (check_cache) {
788 8e359fa0 2022-10-13 stsp *tree = got_repo_get_cached_tree(repo, id);
789 8e359fa0 2022-10-13 stsp if (*tree != NULL) {
790 8e359fa0 2022-10-13 stsp (*tree)->refcnt++;
791 8e359fa0 2022-10-13 stsp return NULL;
794 8e359fa0 2022-10-13 stsp *tree = NULL;
796 8e359fa0 2022-10-13 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
797 8e359fa0 2022-10-13 stsp if (err == NULL) {
798 8e359fa0 2022-10-13 stsp struct got_pack *pack = NULL;
800 8e359fa0 2022-10-13 stsp err = got_packidx_get_packfile_path(&path_packfile,
801 8e359fa0 2022-10-13 stsp packidx->path_packidx);
803 8e359fa0 2022-10-13 stsp return err;
805 8e359fa0 2022-10-13 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
806 8e359fa0 2022-10-13 stsp if (pack == NULL) {
807 8e359fa0 2022-10-13 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
812 8e359fa0 2022-10-13 stsp err = read_packed_tree_privsep(tree, pack,
813 8e359fa0 2022-10-13 stsp packidx, idx, id);
814 8e359fa0 2022-10-13 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
817 8e359fa0 2022-10-13 stsp err = got_object_open_loose_fd(&fd, id, repo);
819 8e359fa0 2022-10-13 stsp return err;
820 8e359fa0 2022-10-13 stsp err = read_tree_privsep(tree, fd, id, repo);
823 8e359fa0 2022-10-13 stsp if (err == NULL) {
824 8e359fa0 2022-10-13 stsp (*tree)->refcnt++;
825 8e359fa0 2022-10-13 stsp err = got_repo_cache_tree(repo, id, *tree);
828 8e359fa0 2022-10-13 stsp free(path_packfile);
829 8e359fa0 2022-10-13 stsp return err;
832 8e359fa0 2022-10-13 stsp const struct got_error *
833 8e359fa0 2022-10-13 stsp got_object_open_as_tree(struct got_tree_object **tree,
834 8e359fa0 2022-10-13 stsp struct got_repository *repo, struct got_object_id *id)
836 8e359fa0 2022-10-13 stsp *tree = got_repo_get_cached_tree(repo, id);
837 8e359fa0 2022-10-13 stsp if (*tree != NULL) {
838 8e359fa0 2022-10-13 stsp (*tree)->refcnt++;
839 8e359fa0 2022-10-13 stsp return NULL;
842 8e359fa0 2022-10-13 stsp return open_tree(tree, repo, id, 0);
845 8e359fa0 2022-10-13 stsp const struct got_error *
846 8e359fa0 2022-10-13 stsp got_object_tree_open(struct got_tree_object **tree,
847 8e359fa0 2022-10-13 stsp struct got_repository *repo, struct got_object *obj)
849 8e359fa0 2022-10-13 stsp return open_tree(tree, repo, got_object_get_id(obj), 1);
852 8e359fa0 2022-10-13 stsp static const struct got_error *
853 8e359fa0 2022-10-13 stsp request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
854 8e359fa0 2022-10-13 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
855 8e359fa0 2022-10-13 stsp struct got_object_id *id)
857 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
858 8e359fa0 2022-10-13 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
859 8e359fa0 2022-10-13 stsp int outfd_child;
861 8e359fa0 2022-10-13 stsp err = pack_child_send_tempfiles(ibuf, pack);
863 8e359fa0 2022-10-13 stsp return err;
865 8e359fa0 2022-10-13 stsp outfd_child = dup(outfd);
866 8e359fa0 2022-10-13 stsp if (outfd_child == -1)
867 8e359fa0 2022-10-13 stsp return got_error_from_errno("dup");
869 8e359fa0 2022-10-13 stsp err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
871 8e359fa0 2022-10-13 stsp return err;
873 8e359fa0 2022-10-13 stsp err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
874 8e359fa0 2022-10-13 stsp outfd_child);
876 8e359fa0 2022-10-13 stsp return err;
879 8e359fa0 2022-10-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen,
880 8e359fa0 2022-10-13 stsp pack->privsep_child->ibuf);
882 8e359fa0 2022-10-13 stsp return err;
884 8e359fa0 2022-10-13 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
885 8e359fa0 2022-10-13 stsp err = got_error_from_errno("lseek");
887 8e359fa0 2022-10-13 stsp return err;
890 8e359fa0 2022-10-13 stsp static const struct got_error *
891 8e359fa0 2022-10-13 stsp read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
892 8e359fa0 2022-10-13 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
893 8e359fa0 2022-10-13 stsp struct got_object_id *id)
895 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
897 8e359fa0 2022-10-13 stsp if (pack->privsep_child == NULL) {
898 8e359fa0 2022-10-13 stsp err = got_pack_start_privsep_child(pack, packidx);
900 8e359fa0 2022-10-13 stsp return err;
903 8e359fa0 2022-10-13 stsp return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
907 8e359fa0 2022-10-13 stsp static const struct got_error *
908 8e359fa0 2022-10-13 stsp request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
909 8e359fa0 2022-10-13 stsp int infd, struct got_object_id *id, struct imsgbuf *ibuf)
911 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
912 8e359fa0 2022-10-13 stsp int outfd_child;
914 8e359fa0 2022-10-13 stsp outfd_child = dup(outfd);
915 8e359fa0 2022-10-13 stsp if (outfd_child == -1)
916 8e359fa0 2022-10-13 stsp return got_error_from_errno("dup");
918 8e359fa0 2022-10-13 stsp err = got_privsep_send_blob_req(ibuf, infd, id, -1);
920 8e359fa0 2022-10-13 stsp return err;
922 8e359fa0 2022-10-13 stsp err = got_privsep_send_blob_outfd(ibuf, outfd_child);
924 8e359fa0 2022-10-13 stsp return err;
926 8e359fa0 2022-10-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
928 8e359fa0 2022-10-13 stsp return err;
930 8e359fa0 2022-10-13 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
931 8e359fa0 2022-10-13 stsp return got_error_from_errno("lseek");
933 8e359fa0 2022-10-13 stsp return err;
936 8e359fa0 2022-10-13 stsp static const struct got_error *
937 8e359fa0 2022-10-13 stsp read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
938 8e359fa0 2022-10-13 stsp int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
940 8e359fa0 2022-10-13 stsp const struct got_error *err;
941 8e359fa0 2022-10-13 stsp struct imsgbuf *ibuf;
943 8e359fa0 2022-10-13 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
944 8e359fa0 2022-10-13 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
945 8e359fa0 2022-10-13 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, id,
949 1d9e43b0 2022-10-13 stsp err = start_child(repo, GOT_REPO_PRIVSEP_CHILD_BLOB);
951 8e359fa0 2022-10-13 stsp return err;
953 1d9e43b0 2022-10-13 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
954 8e359fa0 2022-10-13 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
957 8e359fa0 2022-10-13 stsp static const struct got_error *
958 8e359fa0 2022-10-13 stsp open_blob(struct got_blob_object **blob, struct got_repository *repo,
959 8e359fa0 2022-10-13 stsp struct got_object_id *id, size_t blocksize, int outfd)
961 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
962 8e359fa0 2022-10-13 stsp struct got_packidx *packidx = NULL;
963 8e359fa0 2022-10-13 stsp int idx, dfd = -1;
964 8e359fa0 2022-10-13 stsp char *path_packfile = NULL;
965 8e359fa0 2022-10-13 stsp uint8_t *outbuf;
966 8e359fa0 2022-10-13 stsp size_t size, hdrlen;
967 8e359fa0 2022-10-13 stsp struct stat sb;
969 8e359fa0 2022-10-13 stsp *blob = calloc(1, sizeof(**blob));
970 8e359fa0 2022-10-13 stsp if (*blob == NULL)
971 8e359fa0 2022-10-13 stsp return got_error_from_errno("calloc");
973 8e359fa0 2022-10-13 stsp (*blob)->read_buf = malloc(blocksize);
974 8e359fa0 2022-10-13 stsp if ((*blob)->read_buf == NULL) {
975 8e359fa0 2022-10-13 stsp err = got_error_from_errno("malloc");
979 8e359fa0 2022-10-13 stsp if (ftruncate(outfd, 0L) == -1) {
980 8e359fa0 2022-10-13 stsp err = got_error_from_errno("ftruncate");
983 8e359fa0 2022-10-13 stsp if (lseek(outfd, SEEK_SET, 0) == -1) {
984 8e359fa0 2022-10-13 stsp err = got_error_from_errno("lseek");
988 8e359fa0 2022-10-13 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
989 8e359fa0 2022-10-13 stsp if (err == NULL) {
990 8e359fa0 2022-10-13 stsp struct got_pack *pack = NULL;
992 8e359fa0 2022-10-13 stsp err = got_packidx_get_packfile_path(&path_packfile,
993 8e359fa0 2022-10-13 stsp packidx->path_packidx);
997 8e359fa0 2022-10-13 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
998 8e359fa0 2022-10-13 stsp if (pack == NULL) {
999 8e359fa0 2022-10-13 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1002 8e359fa0 2022-10-13 stsp goto done;
1004 8e359fa0 2022-10-13 stsp err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1005 8e359fa0 2022-10-13 stsp pack, packidx, idx, id);
1006 8e359fa0 2022-10-13 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1009 8e359fa0 2022-10-13 stsp err = got_object_open_loose_fd(&infd, id, repo);
1011 8e359fa0 2022-10-13 stsp goto done;
1012 8e359fa0 2022-10-13 stsp err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1013 8e359fa0 2022-10-13 stsp id, repo);
1016 8e359fa0 2022-10-13 stsp goto done;
1018 8e359fa0 2022-10-13 stsp if (hdrlen > size) {
1019 8e359fa0 2022-10-13 stsp err = got_error(GOT_ERR_BAD_OBJ_HDR);
1020 8e359fa0 2022-10-13 stsp goto done;
1023 8e359fa0 2022-10-13 stsp if (outbuf) {
1024 8e359fa0 2022-10-13 stsp (*blob)->f = fmemopen(outbuf, size, "rb");
1025 8e359fa0 2022-10-13 stsp if ((*blob)->f == NULL) {
1026 8e359fa0 2022-10-13 stsp err = got_error_from_errno("fmemopen");
1027 8e359fa0 2022-10-13 stsp free(outbuf);
1028 8e359fa0 2022-10-13 stsp goto done;
1030 8e359fa0 2022-10-13 stsp (*blob)->data = outbuf;
1032 8e359fa0 2022-10-13 stsp if (fstat(outfd, &sb) == -1) {
1033 8e359fa0 2022-10-13 stsp err = got_error_from_errno("fstat");
1034 8e359fa0 2022-10-13 stsp goto done;
1037 8e359fa0 2022-10-13 stsp if (sb.st_size != size) {
1038 8e359fa0 2022-10-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1039 8e359fa0 2022-10-13 stsp goto done;
1042 8e359fa0 2022-10-13 stsp dfd = dup(outfd);
1043 8e359fa0 2022-10-13 stsp if (dfd == -1) {
1044 8e359fa0 2022-10-13 stsp err = got_error_from_errno("dup");
1045 8e359fa0 2022-10-13 stsp goto done;
1048 8e359fa0 2022-10-13 stsp (*blob)->f = fdopen(dfd, "rb");
1049 8e359fa0 2022-10-13 stsp if ((*blob)->f == NULL) {
1050 8e359fa0 2022-10-13 stsp err = got_error_from_errno("fdopen");
1051 8e359fa0 2022-10-13 stsp close(dfd);
1053 8e359fa0 2022-10-13 stsp goto done;
1057 8e359fa0 2022-10-13 stsp (*blob)->hdrlen = hdrlen;
1058 8e359fa0 2022-10-13 stsp (*blob)->blocksize = blocksize;
1059 8e359fa0 2022-10-13 stsp memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1062 8e359fa0 2022-10-13 stsp free(path_packfile);
1063 8e359fa0 2022-10-13 stsp if (err) {
1064 8e359fa0 2022-10-13 stsp if (*blob) {
1065 8e359fa0 2022-10-13 stsp got_object_blob_close(*blob);
1066 8e359fa0 2022-10-13 stsp *blob = NULL;
1069 8e359fa0 2022-10-13 stsp return err;
1072 8e359fa0 2022-10-13 stsp const struct got_error *
1073 8e359fa0 2022-10-13 stsp got_object_open_as_blob(struct got_blob_object **blob,
1074 8e359fa0 2022-10-13 stsp struct got_repository *repo, struct got_object_id *id, size_t blocksize,
1075 8e359fa0 2022-10-13 stsp int outfd)
1077 8e359fa0 2022-10-13 stsp return open_blob(blob, repo, id, blocksize, outfd);
1080 8e359fa0 2022-10-13 stsp const struct got_error *
1081 8e359fa0 2022-10-13 stsp got_object_blob_open(struct got_blob_object **blob,
1082 8e359fa0 2022-10-13 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize,
1083 8e359fa0 2022-10-13 stsp int outfd)
1085 8e359fa0 2022-10-13 stsp return open_blob(blob, repo, got_object_get_id(obj), blocksize, outfd);
1088 8e359fa0 2022-10-13 stsp static const struct got_error *
1089 8e359fa0 2022-10-13 stsp request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1090 8e359fa0 2022-10-13 stsp int pack_idx, struct got_object_id *id)
1092 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
1094 8e359fa0 2022-10-13 stsp err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1095 8e359fa0 2022-10-13 stsp pack_idx);
1097 8e359fa0 2022-10-13 stsp return err;
1099 8e359fa0 2022-10-13 stsp return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1102 8e359fa0 2022-10-13 stsp static const struct got_error *
1103 8e359fa0 2022-10-13 stsp read_packed_tag_privsep(struct got_tag_object **tag,
1104 8e359fa0 2022-10-13 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1105 8e359fa0 2022-10-13 stsp struct got_object_id *id)
1107 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
1109 8e359fa0 2022-10-13 stsp if (pack->privsep_child)
1110 8e359fa0 2022-10-13 stsp return request_packed_tag(tag, pack, idx, id);
1112 8e359fa0 2022-10-13 stsp err = got_pack_start_privsep_child(pack, packidx);
1114 8e359fa0 2022-10-13 stsp return err;
1116 8e359fa0 2022-10-13 stsp return request_packed_tag(tag, pack, idx, id);
1119 8e359fa0 2022-10-13 stsp static const struct got_error *
1120 8e359fa0 2022-10-13 stsp request_tag(struct got_tag_object **tag, struct got_repository *repo,
1121 8e359fa0 2022-10-13 stsp int fd, struct got_object_id *id)
1123 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
1124 8e359fa0 2022-10-13 stsp struct imsgbuf *ibuf;
1126 8e359fa0 2022-10-13 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1128 8e359fa0 2022-10-13 stsp err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1130 8e359fa0 2022-10-13 stsp return err;
1132 8e359fa0 2022-10-13 stsp return got_privsep_recv_tag(tag, ibuf);
1135 8e359fa0 2022-10-13 stsp static const struct got_error *
1136 8e359fa0 2022-10-13 stsp read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1137 8e359fa0 2022-10-13 stsp struct got_object_id *id, struct got_repository *repo)
1139 8e359fa0 2022-10-13 stsp const struct got_error *err;
1141 8e359fa0 2022-10-13 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1142 8e359fa0 2022-10-13 stsp return request_tag(tag, repo, obj_fd, id);
1144 1d9e43b0 2022-10-13 stsp err = start_child(repo, GOT_REPO_PRIVSEP_CHILD_TAG);
1146 8e359fa0 2022-10-13 stsp return err;
1148 8e359fa0 2022-10-13 stsp return request_tag(tag, repo, obj_fd, id);
1151 8e359fa0 2022-10-13 stsp static const struct got_error *
1152 8e359fa0 2022-10-13 stsp open_tag(struct got_tag_object **tag, struct got_repository *repo,
1153 8e359fa0 2022-10-13 stsp struct got_object_id *id, int check_cache)
1155 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
1156 8e359fa0 2022-10-13 stsp struct got_packidx *packidx = NULL;
1158 8e359fa0 2022-10-13 stsp char *path_packfile = NULL;
1159 8e359fa0 2022-10-13 stsp struct got_object *obj = NULL;
1160 8e359fa0 2022-10-13 stsp int obj_type = GOT_OBJ_TYPE_ANY;
1162 8e359fa0 2022-10-13 stsp if (check_cache) {
1163 8e359fa0 2022-10-13 stsp *tag = got_repo_get_cached_tag(repo, id);
1164 8e359fa0 2022-10-13 stsp if (*tag != NULL) {
1165 8e359fa0 2022-10-13 stsp (*tag)->refcnt++;
1166 8e359fa0 2022-10-13 stsp return NULL;
1169 8e359fa0 2022-10-13 stsp *tag = NULL;
1171 8e359fa0 2022-10-13 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1172 8e359fa0 2022-10-13 stsp if (err == NULL) {
1173 8e359fa0 2022-10-13 stsp struct got_pack *pack = NULL;
1175 8e359fa0 2022-10-13 stsp err = got_packidx_get_packfile_path(&path_packfile,
1176 8e359fa0 2022-10-13 stsp packidx->path_packidx);
1178 8e359fa0 2022-10-13 stsp return err;
1180 8e359fa0 2022-10-13 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1181 8e359fa0 2022-10-13 stsp if (pack == NULL) {
1182 8e359fa0 2022-10-13 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1185 8e359fa0 2022-10-13 stsp goto done;
1188 8e359fa0 2022-10-13 stsp /* Beware of "lightweight" tags: Check object type first. */
1189 8e359fa0 2022-10-13 stsp err = read_packed_object_privsep(&obj, repo, pack, packidx,
1192 8e359fa0 2022-10-13 stsp goto done;
1193 8e359fa0 2022-10-13 stsp obj_type = obj->type;
1194 8e359fa0 2022-10-13 stsp got_object_close(obj);
1195 8e359fa0 2022-10-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG) {
1196 8e359fa0 2022-10-13 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1197 8e359fa0 2022-10-13 stsp goto done;
1199 8e359fa0 2022-10-13 stsp err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1200 8e359fa0 2022-10-13 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1203 8e359fa0 2022-10-13 stsp err = got_object_open_loose_fd(&fd, id, repo);
1205 8e359fa0 2022-10-13 stsp return err;
1206 8e359fa0 2022-10-13 stsp err = got_object_read_header_privsep(&obj, id, repo, fd);
1208 8e359fa0 2022-10-13 stsp return err;
1209 8e359fa0 2022-10-13 stsp obj_type = obj->type;
1210 8e359fa0 2022-10-13 stsp got_object_close(obj);
1211 8e359fa0 2022-10-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
1212 8e359fa0 2022-10-13 stsp return got_error(GOT_ERR_OBJ_TYPE);
1214 8e359fa0 2022-10-13 stsp err = got_object_open_loose_fd(&fd, id, repo);
1216 8e359fa0 2022-10-13 stsp return err;
1217 8e359fa0 2022-10-13 stsp err = read_tag_privsep(tag, fd, id, repo);
1220 8e359fa0 2022-10-13 stsp if (err == NULL) {
1221 8e359fa0 2022-10-13 stsp (*tag)->refcnt++;
1222 8e359fa0 2022-10-13 stsp err = got_repo_cache_tag(repo, id, *tag);
1225 8e359fa0 2022-10-13 stsp free(path_packfile);
1226 8e359fa0 2022-10-13 stsp return err;
1229 8e359fa0 2022-10-13 stsp const struct got_error *
1230 8e359fa0 2022-10-13 stsp got_object_open_as_tag(struct got_tag_object **tag,
1231 8e359fa0 2022-10-13 stsp struct got_repository *repo, struct got_object_id *id)
1233 8e359fa0 2022-10-13 stsp *tag = got_repo_get_cached_tag(repo, id);
1234 8e359fa0 2022-10-13 stsp if (*tag != NULL) {
1235 8e359fa0 2022-10-13 stsp (*tag)->refcnt++;
1236 8e359fa0 2022-10-13 stsp return NULL;
1239 8e359fa0 2022-10-13 stsp return open_tag(tag, repo, id, 0);
1242 8e359fa0 2022-10-13 stsp const struct got_error *
1243 8e359fa0 2022-10-13 stsp got_object_tag_open(struct got_tag_object **tag,
1244 8e359fa0 2022-10-13 stsp struct got_repository *repo, struct got_object *obj)
1246 8e359fa0 2022-10-13 stsp return open_tag(tag, repo, got_object_get_id(obj), 1);
1249 8e359fa0 2022-10-13 stsp const struct got_error *
1250 8e359fa0 2022-10-13 stsp got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
1251 8e359fa0 2022-10-13 stsp struct got_object_id *commit_id, const char *path,
1252 8e359fa0 2022-10-13 stsp struct got_repository *repo)
1254 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
1255 8e359fa0 2022-10-13 stsp struct got_pack *pack = NULL;
1256 8e359fa0 2022-10-13 stsp struct got_packidx *packidx = NULL;
1257 8e359fa0 2022-10-13 stsp char *path_packfile = NULL;
1258 8e359fa0 2022-10-13 stsp struct got_commit_object *changed_commit = NULL;
1259 8e359fa0 2022-10-13 stsp struct got_object_id *changed_commit_id = NULL;
1262 8e359fa0 2022-10-13 stsp err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
1263 8e359fa0 2022-10-13 stsp if (err) {
1264 8e359fa0 2022-10-13 stsp if (err->code != GOT_ERR_NO_OBJ)
1265 8e359fa0 2022-10-13 stsp return err;
1266 8e359fa0 2022-10-13 stsp return NULL;
1269 8e359fa0 2022-10-13 stsp err = got_packidx_get_packfile_path(&path_packfile,
1270 8e359fa0 2022-10-13 stsp packidx->path_packidx);
1272 8e359fa0 2022-10-13 stsp return err;
1274 8e359fa0 2022-10-13 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1275 8e359fa0 2022-10-13 stsp if (pack == NULL) {
1276 8e359fa0 2022-10-13 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
1278 8e359fa0 2022-10-13 stsp goto done;
1281 8e359fa0 2022-10-13 stsp if (pack->privsep_child == NULL) {
1282 8e359fa0 2022-10-13 stsp err = got_pack_start_privsep_child(pack, packidx);
1284 8e359fa0 2022-10-13 stsp goto done;
1287 8e359fa0 2022-10-13 stsp err = got_privsep_send_commit_traversal_request(
1288 8e359fa0 2022-10-13 stsp pack->privsep_child->ibuf, commit_id, idx, path);
1290 8e359fa0 2022-10-13 stsp goto done;
1292 8e359fa0 2022-10-13 stsp err = got_privsep_recv_traversed_commits(&changed_commit,
1293 8e359fa0 2022-10-13 stsp &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
1295 8e359fa0 2022-10-13 stsp goto done;
1297 8e359fa0 2022-10-13 stsp if (changed_commit) {
1299 8e359fa0 2022-10-13 stsp * Cache the commit in which the path was changed.
1300 8e359fa0 2022-10-13 stsp * This commit might be opened again soon.
1302 8e359fa0 2022-10-13 stsp changed_commit->refcnt++;
1303 8e359fa0 2022-10-13 stsp err = got_repo_cache_commit(repo, changed_commit_id,
1304 8e359fa0 2022-10-13 stsp changed_commit);
1305 8e359fa0 2022-10-13 stsp got_object_commit_close(changed_commit);
1308 8e359fa0 2022-10-13 stsp free(path_packfile);
1309 8e359fa0 2022-10-13 stsp free(changed_commit_id);
1310 8e359fa0 2022-10-13 stsp return err;
1313 8e359fa0 2022-10-13 stsp const struct got_error *
1314 8e359fa0 2022-10-13 stsp got_object_enumerate(int *found_all_objects,
1315 8e359fa0 2022-10-13 stsp got_object_enumerate_commit_cb cb_commit,
1316 8e359fa0 2022-10-13 stsp got_object_enumerate_tree_cb cb_tree, void *cb_arg,
1317 8e359fa0 2022-10-13 stsp struct got_object_id **ours, int nours,
1318 8e359fa0 2022-10-13 stsp struct got_object_id **theirs, int ntheirs,
1319 8e359fa0 2022-10-13 stsp struct got_packidx *packidx, struct got_repository *repo)
1321 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
1322 8e359fa0 2022-10-13 stsp struct got_pack *pack;
1323 8e359fa0 2022-10-13 stsp char *path_packfile = NULL;
1325 8e359fa0 2022-10-13 stsp err = got_packidx_get_packfile_path(&path_packfile,
1326 8e359fa0 2022-10-13 stsp packidx->path_packidx);
1328 8e359fa0 2022-10-13 stsp return err;
1330 8e359fa0 2022-10-13 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1331 8e359fa0 2022-10-13 stsp if (pack == NULL) {
1332 8e359fa0 2022-10-13 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
1334 8e359fa0 2022-10-13 stsp goto done;
1337 8e359fa0 2022-10-13 stsp if (pack->privsep_child == NULL) {
1338 8e359fa0 2022-10-13 stsp err = got_pack_start_privsep_child(pack, packidx);
1340 8e359fa0 2022-10-13 stsp goto done;
1343 8e359fa0 2022-10-13 stsp err = got_privsep_send_object_enumeration_request(
1344 8e359fa0 2022-10-13 stsp pack->privsep_child->ibuf);
1346 8e359fa0 2022-10-13 stsp goto done;
1348 8e359fa0 2022-10-13 stsp err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
1349 8e359fa0 2022-10-13 stsp ours, nours);
1351 8e359fa0 2022-10-13 stsp goto done;
1352 8e359fa0 2022-10-13 stsp err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
1354 8e359fa0 2022-10-13 stsp goto done;
1356 8e359fa0 2022-10-13 stsp err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
1357 8e359fa0 2022-10-13 stsp theirs, ntheirs);
1359 8e359fa0 2022-10-13 stsp goto done;
1360 8e359fa0 2022-10-13 stsp err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
1362 8e359fa0 2022-10-13 stsp goto done;
1364 8e359fa0 2022-10-13 stsp err = got_privsep_recv_enumerated_objects(found_all_objects,
1365 8e359fa0 2022-10-13 stsp pack->privsep_child->ibuf, cb_commit, cb_tree, cb_arg, repo);
1367 8e359fa0 2022-10-13 stsp free(path_packfile);
1368 8e359fa0 2022-10-13 stsp return err;