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 5822e79e 2023-02-23 op #include <sha2.h>
33 8e359fa0 2022-10-13 stsp #include <limits.h>
34 8e359fa0 2022-10-13 stsp #include <unistd.h>
36 8e359fa0 2022-10-13 stsp #include "got_error.h"
37 8e359fa0 2022-10-13 stsp #include "got_object.h"
38 8e359fa0 2022-10-13 stsp #include "got_repository.h"
39 8e359fa0 2022-10-13 stsp #include "got_opentemp.h"
40 8e359fa0 2022-10-13 stsp #include "got_path.h"
42 8e359fa0 2022-10-13 stsp #include "got_lib_delta.h"
43 8e359fa0 2022-10-13 stsp #include "got_lib_object.h"
44 8e359fa0 2022-10-13 stsp #include "got_lib_privsep.h"
45 8e359fa0 2022-10-13 stsp #include "got_lib_object_cache.h"
46 8e359fa0 2022-10-13 stsp #include "got_lib_pack.h"
47 8e359fa0 2022-10-13 stsp #include "got_lib_repository.h"
49 8e359fa0 2022-10-13 stsp static const struct got_error *
50 8e359fa0 2022-10-13 stsp request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
51 8e359fa0 2022-10-13 stsp struct got_object_id *id)
53 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
54 8e359fa0 2022-10-13 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
56 8e359fa0 2022-10-13 stsp err = got_privsep_send_packed_obj_req(ibuf, idx, id);
60 8e359fa0 2022-10-13 stsp err = got_privsep_recv_obj(obj, ibuf);
64 8e359fa0 2022-10-13 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
66 8e359fa0 2022-10-13 stsp return NULL;
69 8e359fa0 2022-10-13 stsp /* Create temporary files used during delta application. */
70 8e359fa0 2022-10-13 stsp static const struct got_error *
71 8e359fa0 2022-10-13 stsp pack_child_send_tempfiles(struct imsgbuf *ibuf, struct got_pack *pack)
73 8e359fa0 2022-10-13 stsp const struct got_error *err;
74 8e359fa0 2022-10-13 stsp int basefd = -1, accumfd = -1;
77 8e359fa0 2022-10-13 stsp * For performance reasons, the child will keep reusing the
78 8e359fa0 2022-10-13 stsp * same temporary files during every object request.
79 8e359fa0 2022-10-13 stsp * Opening and closing new files for every object request is
80 8e359fa0 2022-10-13 stsp * too expensive during operations such as 'gotadmin pack'.
82 8e359fa0 2022-10-13 stsp if (pack->child_has_tempfiles)
83 8e359fa0 2022-10-13 stsp return NULL;
85 8e359fa0 2022-10-13 stsp basefd = dup(pack->basefd);
86 8e359fa0 2022-10-13 stsp if (basefd == -1)
87 8e359fa0 2022-10-13 stsp return got_error_from_errno("dup");
89 8e359fa0 2022-10-13 stsp accumfd = dup(pack->accumfd);
90 8e359fa0 2022-10-13 stsp if (accumfd == -1) {
91 8e359fa0 2022-10-13 stsp err = got_error_from_errno("dup");
95 8e359fa0 2022-10-13 stsp err = got_privsep_send_tmpfd(ibuf, basefd);
99 8e359fa0 2022-10-13 stsp err = got_privsep_send_tmpfd(ibuf, accumfd);
102 8e359fa0 2022-10-13 stsp if (basefd != -1)
103 8e359fa0 2022-10-13 stsp close(basefd);
104 8e359fa0 2022-10-13 stsp if (accumfd != -1)
105 8e359fa0 2022-10-13 stsp close(accumfd);
107 8e359fa0 2022-10-13 stsp pack->child_has_tempfiles = 1;
108 c33c3763 2023-02-03 mark return err;
111 8e359fa0 2022-10-13 stsp static const struct got_error *
112 8e359fa0 2022-10-13 stsp request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
113 8e359fa0 2022-10-13 stsp int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
115 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
116 8e359fa0 2022-10-13 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
117 8e359fa0 2022-10-13 stsp int outfd_child;
119 06bd8ee4 2024-02-13 op err = pack_child_send_tempfiles(ibuf, pack);
121 8e359fa0 2022-10-13 stsp return err;
123 8e359fa0 2022-10-13 stsp outfd_child = dup(outfd);
124 8e359fa0 2022-10-13 stsp if (outfd_child == -1)
125 8e359fa0 2022-10-13 stsp return got_error_from_errno("dup");
127 8e359fa0 2022-10-13 stsp err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
129 8e359fa0 2022-10-13 stsp close(outfd_child);
130 8e359fa0 2022-10-13 stsp return err;
133 8e359fa0 2022-10-13 stsp err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
135 8e359fa0 2022-10-13 stsp return err;
137 8e359fa0 2022-10-13 stsp err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
139 8e359fa0 2022-10-13 stsp return err;
141 8e359fa0 2022-10-13 stsp return NULL;
144 8e359fa0 2022-10-13 stsp static const struct got_error *
145 8e359fa0 2022-10-13 stsp read_packed_object_privsep(struct got_object **obj,
146 8e359fa0 2022-10-13 stsp struct got_repository *repo, struct got_pack *pack,
147 8e359fa0 2022-10-13 stsp struct got_packidx *packidx, int idx, struct got_object_id *id)
149 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
151 8e359fa0 2022-10-13 stsp if (pack->privsep_child == NULL) {
152 8e359fa0 2022-10-13 stsp err = got_pack_start_privsep_child(pack, packidx);
154 8e359fa0 2022-10-13 stsp return err;
157 8e359fa0 2022-10-13 stsp return request_packed_object(obj, pack, idx, id);
160 8e359fa0 2022-10-13 stsp static const struct got_error *
161 8e359fa0 2022-10-13 stsp read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
162 8e359fa0 2022-10-13 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
163 8e359fa0 2022-10-13 stsp struct got_object_id *id)
165 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
167 8e359fa0 2022-10-13 stsp if (pack->privsep_child == NULL) {
168 8e359fa0 2022-10-13 stsp err = got_pack_start_privsep_child(pack, packidx);
170 8e359fa0 2022-10-13 stsp return err;
173 8e359fa0 2022-10-13 stsp return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
177 8e359fa0 2022-10-13 stsp const struct got_error *
178 8e359fa0 2022-10-13 stsp got_object_open_packed(struct got_object **obj, struct got_object_id *id,
179 8e359fa0 2022-10-13 stsp struct got_repository *repo)
181 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
182 8e359fa0 2022-10-13 stsp struct got_pack *pack = NULL;
183 8e359fa0 2022-10-13 stsp struct got_packidx *packidx = NULL;
185 8e359fa0 2022-10-13 stsp char *path_packfile;
187 8e359fa0 2022-10-13 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
189 8e359fa0 2022-10-13 stsp return err;
191 8e359fa0 2022-10-13 stsp err = got_packidx_get_packfile_path(&path_packfile,
192 8e359fa0 2022-10-13 stsp packidx->path_packidx);
194 8e359fa0 2022-10-13 stsp return err;
196 8e359fa0 2022-10-13 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
197 8e359fa0 2022-10-13 stsp if (pack == NULL) {
198 8e359fa0 2022-10-13 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
203 8e359fa0 2022-10-13 stsp err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
207 8e359fa0 2022-10-13 stsp free(path_packfile);
208 8e359fa0 2022-10-13 stsp return err;
211 8e359fa0 2022-10-13 stsp const struct got_error *
212 8e359fa0 2022-10-13 stsp got_object_open_from_packfile(struct got_object **obj, struct got_object_id *id,
213 8e359fa0 2022-10-13 stsp struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
214 8e359fa0 2022-10-13 stsp struct got_repository *repo)
216 8e359fa0 2022-10-13 stsp return read_packed_object_privsep(obj, repo, pack, packidx,
217 8e359fa0 2022-10-13 stsp obj_idx, id);
220 8e359fa0 2022-10-13 stsp const struct got_error *
221 8e359fa0 2022-10-13 stsp got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
222 8e359fa0 2022-10-13 stsp off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
223 8e359fa0 2022-10-13 stsp off_t *delta_out_offset, struct got_object_id **base_id, int delta_cache_fd,
224 8e359fa0 2022-10-13 stsp struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
225 8e359fa0 2022-10-13 stsp struct got_repository *repo)
227 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
228 8e359fa0 2022-10-13 stsp struct got_pack *pack = NULL;
229 8e359fa0 2022-10-13 stsp char *path_packfile;
231 8e359fa0 2022-10-13 stsp *base_size = 0;
232 8e359fa0 2022-10-13 stsp *result_size = 0;
233 8e359fa0 2022-10-13 stsp *delta_size = 0;
234 8e359fa0 2022-10-13 stsp *delta_compressed_size = 0;
235 8e359fa0 2022-10-13 stsp *delta_offset = 0;
236 8e359fa0 2022-10-13 stsp *delta_out_offset = 0;
238 8e359fa0 2022-10-13 stsp err = got_packidx_get_packfile_path(&path_packfile,
239 8e359fa0 2022-10-13 stsp packidx->path_packidx);
241 8e359fa0 2022-10-13 stsp return err;
243 8e359fa0 2022-10-13 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
244 8e359fa0 2022-10-13 stsp if (pack == NULL) {
245 8e359fa0 2022-10-13 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
247 8e359fa0 2022-10-13 stsp return err;
250 8e359fa0 2022-10-13 stsp if (pack->privsep_child == NULL) {
251 8e359fa0 2022-10-13 stsp err = got_pack_start_privsep_child(pack, packidx);
253 8e359fa0 2022-10-13 stsp return err;
256 8e359fa0 2022-10-13 stsp if (!pack->child_has_delta_outfd) {
257 8e359fa0 2022-10-13 stsp int outfd_child;
258 8e359fa0 2022-10-13 stsp outfd_child = dup(delta_cache_fd);
259 8e359fa0 2022-10-13 stsp if (outfd_child == -1)
260 8e359fa0 2022-10-13 stsp return got_error_from_errno("dup");
261 8e359fa0 2022-10-13 stsp err = got_privsep_send_raw_delta_outfd(
262 8e359fa0 2022-10-13 stsp pack->privsep_child->ibuf, outfd_child);
264 8e359fa0 2022-10-13 stsp return err;
265 8e359fa0 2022-10-13 stsp pack->child_has_delta_outfd = 1;
268 8e359fa0 2022-10-13 stsp err = got_privsep_send_raw_delta_req(pack->privsep_child->ibuf,
269 8e359fa0 2022-10-13 stsp obj_idx, id);
271 8e359fa0 2022-10-13 stsp return err;
273 8e359fa0 2022-10-13 stsp return got_privsep_recv_raw_delta(base_size, result_size, delta_size,
274 8e359fa0 2022-10-13 stsp delta_compressed_size, delta_offset, delta_out_offset, base_id,
275 8e359fa0 2022-10-13 stsp pack->privsep_child->ibuf);
278 8e359fa0 2022-10-13 stsp static const struct got_error *
279 8e359fa0 2022-10-13 stsp request_object(struct got_object **obj, struct got_object_id *id,
280 8e359fa0 2022-10-13 stsp struct got_repository *repo, int fd)
282 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
283 8e359fa0 2022-10-13 stsp struct imsgbuf *ibuf;
285 8e359fa0 2022-10-13 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
287 8e359fa0 2022-10-13 stsp err = got_privsep_send_obj_req(ibuf, fd, id);
289 8e359fa0 2022-10-13 stsp return err;
291 8e359fa0 2022-10-13 stsp return got_privsep_recv_obj(obj, ibuf);
294 8e359fa0 2022-10-13 stsp static const struct got_error *
295 8e359fa0 2022-10-13 stsp request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
296 8e359fa0 2022-10-13 stsp struct got_object_id *id, struct got_repository *repo, int infd)
298 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
299 8e359fa0 2022-10-13 stsp struct imsgbuf *ibuf;
300 8e359fa0 2022-10-13 stsp int outfd_child;
302 8e359fa0 2022-10-13 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
304 8e359fa0 2022-10-13 stsp outfd_child = dup(outfd);
305 8e359fa0 2022-10-13 stsp if (outfd_child == -1)
306 8e359fa0 2022-10-13 stsp return got_error_from_errno("dup");
308 8e359fa0 2022-10-13 stsp err = got_privsep_send_raw_obj_req(ibuf, infd, id);
310 8e359fa0 2022-10-13 stsp return err;
312 8e359fa0 2022-10-13 stsp err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
314 8e359fa0 2022-10-13 stsp return err;
316 8e359fa0 2022-10-13 stsp return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
319 8e359fa0 2022-10-13 stsp static const struct got_error *
320 1d9e43b0 2022-10-13 stsp start_child(struct got_repository *repo, int type)
322 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
323 8e359fa0 2022-10-13 stsp int imsg_fds[2];
325 8e359fa0 2022-10-13 stsp struct imsgbuf *ibuf;
326 1d9e43b0 2022-10-13 stsp const char *prog_path;
328 1d9e43b0 2022-10-13 stsp switch (type) {
329 1d9e43b0 2022-10-13 stsp case GOT_REPO_PRIVSEP_CHILD_OBJECT:
330 1d9e43b0 2022-10-13 stsp prog_path = GOT_PATH_PROG_READ_OBJECT;
332 1d9e43b0 2022-10-13 stsp case GOT_REPO_PRIVSEP_CHILD_TREE:
333 1d9e43b0 2022-10-13 stsp prog_path = GOT_PATH_PROG_READ_TREE;
335 1d9e43b0 2022-10-13 stsp case GOT_REPO_PRIVSEP_CHILD_COMMIT:
336 1d9e43b0 2022-10-13 stsp prog_path = GOT_PATH_PROG_READ_COMMIT;
338 1d9e43b0 2022-10-13 stsp case GOT_REPO_PRIVSEP_CHILD_BLOB:
339 1d9e43b0 2022-10-13 stsp prog_path = GOT_PATH_PROG_READ_BLOB;
341 1d9e43b0 2022-10-13 stsp case GOT_REPO_PRIVSEP_CHILD_TAG:
342 1d9e43b0 2022-10-13 stsp prog_path = GOT_PATH_PROG_READ_TAG;
345 1d9e43b0 2022-10-13 stsp return got_error(GOT_ERR_OBJ_TYPE);
348 8e359fa0 2022-10-13 stsp ibuf = calloc(1, sizeof(*ibuf));
349 8e359fa0 2022-10-13 stsp if (ibuf == NULL)
350 8e359fa0 2022-10-13 stsp return got_error_from_errno("calloc");
352 8e359fa0 2022-10-13 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
353 8e359fa0 2022-10-13 stsp err = got_error_from_errno("socketpair");
354 8e359fa0 2022-10-13 stsp free(ibuf);
355 8e359fa0 2022-10-13 stsp return err;
358 8e359fa0 2022-10-13 stsp pid = fork();
359 8e359fa0 2022-10-13 stsp if (pid == -1) {
360 8e359fa0 2022-10-13 stsp err = got_error_from_errno("fork");
361 8e359fa0 2022-10-13 stsp free(ibuf);
362 8e359fa0 2022-10-13 stsp return err;
364 8e359fa0 2022-10-13 stsp else if (pid == 0) {
365 1d9e43b0 2022-10-13 stsp got_privsep_exec_child(imsg_fds, prog_path, repo->path);
366 8e359fa0 2022-10-13 stsp /* not reached */
369 8e359fa0 2022-10-13 stsp if (close(imsg_fds[1]) == -1) {
370 8e359fa0 2022-10-13 stsp err = got_error_from_errno("close");
371 8e359fa0 2022-10-13 stsp free(ibuf);
372 8e359fa0 2022-10-13 stsp return err;
375 1d9e43b0 2022-10-13 stsp repo->privsep_children[type].imsg_fd = imsg_fds[0];
376 1d9e43b0 2022-10-13 stsp repo->privsep_children[type].pid = pid;
377 8e359fa0 2022-10-13 stsp imsg_init(ibuf, imsg_fds[0]);
378 1d9e43b0 2022-10-13 stsp repo->privsep_children[type].ibuf = ibuf;
380 8e359fa0 2022-10-13 stsp return NULL;
383 8e359fa0 2022-10-13 stsp const struct got_error *
384 8e359fa0 2022-10-13 stsp got_object_read_header_privsep(struct got_object **obj,
385 8e359fa0 2022-10-13 stsp struct got_object_id *id, struct got_repository *repo, int obj_fd)
387 8e359fa0 2022-10-13 stsp const struct got_error *err;
389 8e359fa0 2022-10-13 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
390 8e359fa0 2022-10-13 stsp return request_object(obj, id, repo, obj_fd);
392 1d9e43b0 2022-10-13 stsp err = start_child(repo, GOT_REPO_PRIVSEP_CHILD_OBJECT);
394 8e359fa0 2022-10-13 stsp return err;
396 8e359fa0 2022-10-13 stsp return request_object(obj, id, repo, obj_fd);
399 8e359fa0 2022-10-13 stsp static const struct got_error *
400 8e359fa0 2022-10-13 stsp read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
401 8e359fa0 2022-10-13 stsp int outfd, struct got_object_id *id, struct got_repository *repo,
402 8e359fa0 2022-10-13 stsp int obj_fd)
404 8e359fa0 2022-10-13 stsp const struct got_error *err;
406 8e359fa0 2022-10-13 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
407 8e359fa0 2022-10-13 stsp return request_raw_object(outbuf, size, hdrlen, outfd, id,
408 8e359fa0 2022-10-13 stsp repo, obj_fd);
410 1d9e43b0 2022-10-13 stsp err = start_child(repo, GOT_REPO_PRIVSEP_CHILD_OBJECT);
412 8e359fa0 2022-10-13 stsp return err;
414 8e359fa0 2022-10-13 stsp return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
418 8e359fa0 2022-10-13 stsp const struct got_error *
419 8e359fa0 2022-10-13 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
420 8e359fa0 2022-10-13 stsp struct got_object_id *id)
422 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
425 8e359fa0 2022-10-13 stsp *obj = got_repo_get_cached_object(repo, id);
426 8e359fa0 2022-10-13 stsp if (*obj != NULL) {
427 8e359fa0 2022-10-13 stsp (*obj)->refcnt++;
428 8e359fa0 2022-10-13 stsp return NULL;
431 8e359fa0 2022-10-13 stsp err = got_object_open_packed(obj, id, repo);
432 8e359fa0 2022-10-13 stsp if (err && err->code != GOT_ERR_NO_OBJ)
433 8e359fa0 2022-10-13 stsp return err;
434 8e359fa0 2022-10-13 stsp if (*obj) {
435 8e359fa0 2022-10-13 stsp (*obj)->refcnt++;
436 8e359fa0 2022-10-13 stsp return got_repo_cache_object(repo, id, *obj);
439 8e359fa0 2022-10-13 stsp err = got_object_open_loose_fd(&fd, id, repo);
441 8e359fa0 2022-10-13 stsp if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
442 8e359fa0 2022-10-13 stsp err = got_error_no_obj(id);
443 8e359fa0 2022-10-13 stsp return err;
446 8e359fa0 2022-10-13 stsp err = got_object_read_header_privsep(obj, id, repo, fd);
448 8e359fa0 2022-10-13 stsp return err;
450 3e0381db 2023-02-05 op memcpy(&(*obj)->id, id, sizeof(*id));
452 8e359fa0 2022-10-13 stsp (*obj)->refcnt++;
453 8e359fa0 2022-10-13 stsp return got_repo_cache_object(repo, id, *obj);
456 8e359fa0 2022-10-13 stsp /* *outfd must be initialized to -1 by caller */
457 8e359fa0 2022-10-13 stsp const struct got_error *
458 8e359fa0 2022-10-13 stsp got_object_raw_open(struct got_raw_object **obj, int *outfd,
459 8e359fa0 2022-10-13 stsp struct got_repository *repo, struct got_object_id *id)
461 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
462 8e359fa0 2022-10-13 stsp struct got_packidx *packidx = NULL;
464 8e359fa0 2022-10-13 stsp uint8_t *outbuf = NULL;
465 8e359fa0 2022-10-13 stsp off_t size = 0;
466 8e359fa0 2022-10-13 stsp size_t hdrlen = 0;
467 8e359fa0 2022-10-13 stsp char *path_packfile = NULL;
469 8e359fa0 2022-10-13 stsp *obj = got_repo_get_cached_raw_object(repo, id);
470 8e359fa0 2022-10-13 stsp if (*obj != NULL) {
471 8e359fa0 2022-10-13 stsp (*obj)->refcnt++;
472 8e359fa0 2022-10-13 stsp return NULL;
475 8e359fa0 2022-10-13 stsp if (*outfd == -1) {
476 8e359fa0 2022-10-13 stsp *outfd = got_opentempfd();
477 8e359fa0 2022-10-13 stsp if (*outfd == -1)
478 8e359fa0 2022-10-13 stsp return got_error_from_errno("got_opentempfd");
481 8e359fa0 2022-10-13 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
482 8e359fa0 2022-10-13 stsp if (err == NULL) {
483 8e359fa0 2022-10-13 stsp struct got_pack *pack = NULL;
485 8e359fa0 2022-10-13 stsp err = got_packidx_get_packfile_path(&path_packfile,
486 8e359fa0 2022-10-13 stsp packidx->path_packidx);
490 8e359fa0 2022-10-13 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
491 8e359fa0 2022-10-13 stsp if (pack == NULL) {
492 8e359fa0 2022-10-13 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
497 8e359fa0 2022-10-13 stsp err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
498 8e359fa0 2022-10-13 stsp *outfd, pack, packidx, idx, id);
501 8e359fa0 2022-10-13 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
504 8e359fa0 2022-10-13 stsp err = got_object_open_loose_fd(&fd, id, repo);
507 8e359fa0 2022-10-13 stsp err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
508 8e359fa0 2022-10-13 stsp id, repo, fd);
513 60c140ae 2023-01-09 stsp err = got_object_raw_alloc(obj, outbuf, outfd,
514 60c140ae 2023-01-09 stsp GOT_DELTA_RESULT_SIZE_CACHED_MAX, hdrlen, size);
518 8e359fa0 2022-10-13 stsp err = got_repo_cache_raw_object(repo, id, *obj);
520 8e359fa0 2022-10-13 stsp free(path_packfile);
522 8e359fa0 2022-10-13 stsp if (*obj) {
523 8e359fa0 2022-10-13 stsp got_object_raw_close(*obj);
524 8e359fa0 2022-10-13 stsp *obj = NULL;
526 8e359fa0 2022-10-13 stsp free(outbuf);
528 8e359fa0 2022-10-13 stsp return err;
531 8e359fa0 2022-10-13 stsp static const struct got_error *
532 8e359fa0 2022-10-13 stsp request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
533 8e359fa0 2022-10-13 stsp int pack_idx, struct got_object_id *id)
535 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
537 8e359fa0 2022-10-13 stsp err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
540 8e359fa0 2022-10-13 stsp return err;
542 8e359fa0 2022-10-13 stsp err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
544 8e359fa0 2022-10-13 stsp return err;
546 8e359fa0 2022-10-13 stsp (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
547 8e359fa0 2022-10-13 stsp return NULL;
550 8e359fa0 2022-10-13 stsp static const struct got_error *
551 8e359fa0 2022-10-13 stsp read_packed_commit_privsep(struct got_commit_object **commit,
552 8e359fa0 2022-10-13 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
553 8e359fa0 2022-10-13 stsp struct got_object_id *id)
555 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
557 8e359fa0 2022-10-13 stsp if (pack->privsep_child)
558 8e359fa0 2022-10-13 stsp return request_packed_commit(commit, pack, idx, id);
560 8e359fa0 2022-10-13 stsp err = got_pack_start_privsep_child(pack, packidx);
562 8e359fa0 2022-10-13 stsp return err;
564 8e359fa0 2022-10-13 stsp return request_packed_commit(commit, pack, idx, id);
567 8e359fa0 2022-10-13 stsp static const struct got_error *
568 8e359fa0 2022-10-13 stsp request_commit(struct got_commit_object **commit, struct got_repository *repo,
569 8e359fa0 2022-10-13 stsp int fd, struct got_object_id *id)
571 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
572 8e359fa0 2022-10-13 stsp struct imsgbuf *ibuf;
574 8e359fa0 2022-10-13 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
576 8e359fa0 2022-10-13 stsp err = got_privsep_send_commit_req(ibuf, fd, id, -1);
578 8e359fa0 2022-10-13 stsp return err;
580 8e359fa0 2022-10-13 stsp return got_privsep_recv_commit(commit, ibuf);
583 8e359fa0 2022-10-13 stsp static const struct got_error *
584 8e359fa0 2022-10-13 stsp read_commit_privsep(struct got_commit_object **commit, int obj_fd,
585 8e359fa0 2022-10-13 stsp struct got_object_id *id, struct got_repository *repo)
587 8e359fa0 2022-10-13 stsp const struct got_error *err;
589 8e359fa0 2022-10-13 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
590 8e359fa0 2022-10-13 stsp return request_commit(commit, repo, obj_fd, id);
592 1d9e43b0 2022-10-13 stsp err = start_child(repo, GOT_REPO_PRIVSEP_CHILD_COMMIT);
594 8e359fa0 2022-10-13 stsp return err;
596 8e359fa0 2022-10-13 stsp return request_commit(commit, repo, obj_fd, id);
599 8e359fa0 2022-10-13 stsp static const struct got_error *
600 8e359fa0 2022-10-13 stsp open_commit(struct got_commit_object **commit,
601 8e359fa0 2022-10-13 stsp struct got_repository *repo, struct got_object_id *id, int check_cache)
603 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
604 8e359fa0 2022-10-13 stsp struct got_packidx *packidx = NULL;
606 8e359fa0 2022-10-13 stsp char *path_packfile = NULL;
608 8e359fa0 2022-10-13 stsp if (check_cache) {
609 8e359fa0 2022-10-13 stsp *commit = got_repo_get_cached_commit(repo, id);
610 8e359fa0 2022-10-13 stsp if (*commit != NULL) {
611 8e359fa0 2022-10-13 stsp (*commit)->refcnt++;
612 8e359fa0 2022-10-13 stsp return NULL;
615 8e359fa0 2022-10-13 stsp *commit = NULL;
617 8e359fa0 2022-10-13 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
618 8e359fa0 2022-10-13 stsp if (err == NULL) {
619 8e359fa0 2022-10-13 stsp struct got_pack *pack = NULL;
621 8e359fa0 2022-10-13 stsp err = got_packidx_get_packfile_path(&path_packfile,
622 8e359fa0 2022-10-13 stsp packidx->path_packidx);
624 8e359fa0 2022-10-13 stsp return err;
626 8e359fa0 2022-10-13 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
627 8e359fa0 2022-10-13 stsp if (pack == NULL) {
628 8e359fa0 2022-10-13 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
633 8e359fa0 2022-10-13 stsp err = read_packed_commit_privsep(commit, pack,
634 8e359fa0 2022-10-13 stsp packidx, idx, id);
635 8e359fa0 2022-10-13 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
638 8e359fa0 2022-10-13 stsp err = got_object_open_loose_fd(&fd, id, repo);
640 8e359fa0 2022-10-13 stsp return err;
641 8e359fa0 2022-10-13 stsp err = read_commit_privsep(commit, fd, id, repo);
644 8e359fa0 2022-10-13 stsp if (err == NULL) {
645 8e359fa0 2022-10-13 stsp (*commit)->refcnt++;
646 8e359fa0 2022-10-13 stsp err = got_repo_cache_commit(repo, id, *commit);
649 8e359fa0 2022-10-13 stsp free(path_packfile);
650 8e359fa0 2022-10-13 stsp return err;
653 8e359fa0 2022-10-13 stsp const struct got_error *
654 8e359fa0 2022-10-13 stsp got_object_open_as_commit(struct got_commit_object **commit,
655 8e359fa0 2022-10-13 stsp struct got_repository *repo, struct got_object_id *id)
657 8e359fa0 2022-10-13 stsp *commit = got_repo_get_cached_commit(repo, id);
658 8e359fa0 2022-10-13 stsp if (*commit != NULL) {
659 8e359fa0 2022-10-13 stsp (*commit)->refcnt++;
660 8e359fa0 2022-10-13 stsp return NULL;
663 8e359fa0 2022-10-13 stsp return open_commit(commit, repo, id, 0);
666 8e359fa0 2022-10-13 stsp const struct got_error *
667 8e359fa0 2022-10-13 stsp got_object_commit_open(struct got_commit_object **commit,
668 8e359fa0 2022-10-13 stsp struct got_repository *repo, struct got_object *obj)
670 8e359fa0 2022-10-13 stsp return open_commit(commit, repo, got_object_get_id(obj), 1);
673 8e359fa0 2022-10-13 stsp static const struct got_error *
674 8e359fa0 2022-10-13 stsp request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
675 8e359fa0 2022-10-13 stsp int pack_idx, struct got_object_id *id)
677 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
679 8e359fa0 2022-10-13 stsp err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
682 8e359fa0 2022-10-13 stsp return err;
684 8e359fa0 2022-10-13 stsp return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
687 8e359fa0 2022-10-13 stsp static const struct got_error *
688 8e359fa0 2022-10-13 stsp read_packed_tree_privsep(struct got_tree_object **tree,
689 8e359fa0 2022-10-13 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
690 8e359fa0 2022-10-13 stsp struct got_object_id *id)
692 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
694 8e359fa0 2022-10-13 stsp if (pack->privsep_child)
695 8e359fa0 2022-10-13 stsp return request_packed_tree(tree, pack, idx, id);
697 8e359fa0 2022-10-13 stsp err = got_pack_start_privsep_child(pack, packidx);
699 8e359fa0 2022-10-13 stsp return err;
701 8e359fa0 2022-10-13 stsp return request_packed_tree(tree, pack, idx, id);
704 8e359fa0 2022-10-13 stsp static const struct got_error *
705 8e359fa0 2022-10-13 stsp request_tree(struct got_tree_object **tree, struct got_repository *repo,
706 8e359fa0 2022-10-13 stsp int fd, struct got_object_id *id)
708 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
709 8e359fa0 2022-10-13 stsp struct imsgbuf *ibuf;
711 8e359fa0 2022-10-13 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
713 8e359fa0 2022-10-13 stsp err = got_privsep_send_tree_req(ibuf, fd, id, -1);
715 8e359fa0 2022-10-13 stsp return err;
717 8e359fa0 2022-10-13 stsp return got_privsep_recv_tree(tree, ibuf);
720 8e359fa0 2022-10-13 stsp static const struct got_error *
721 8e359fa0 2022-10-13 stsp read_tree_privsep(struct got_tree_object **tree, int obj_fd,
722 8e359fa0 2022-10-13 stsp struct got_object_id *id, struct got_repository *repo)
724 8e359fa0 2022-10-13 stsp const struct got_error *err;
726 8e359fa0 2022-10-13 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
727 8e359fa0 2022-10-13 stsp return request_tree(tree, repo, obj_fd, id);
729 1d9e43b0 2022-10-13 stsp err = start_child(repo, GOT_REPO_PRIVSEP_CHILD_TREE);
731 8e359fa0 2022-10-13 stsp return err;
733 8e359fa0 2022-10-13 stsp return request_tree(tree, repo, obj_fd, id);
736 8e359fa0 2022-10-13 stsp static const struct got_error *
737 8e359fa0 2022-10-13 stsp open_tree(struct got_tree_object **tree, struct got_repository *repo,
738 8e359fa0 2022-10-13 stsp struct got_object_id *id, int check_cache)
740 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
741 8e359fa0 2022-10-13 stsp struct got_packidx *packidx = NULL;
743 8e359fa0 2022-10-13 stsp char *path_packfile = NULL;
745 8e359fa0 2022-10-13 stsp if (check_cache) {
746 8e359fa0 2022-10-13 stsp *tree = got_repo_get_cached_tree(repo, id);
747 8e359fa0 2022-10-13 stsp if (*tree != NULL) {
748 8e359fa0 2022-10-13 stsp (*tree)->refcnt++;
749 8e359fa0 2022-10-13 stsp return NULL;
752 8e359fa0 2022-10-13 stsp *tree = NULL;
754 8e359fa0 2022-10-13 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
755 8e359fa0 2022-10-13 stsp if (err == NULL) {
756 8e359fa0 2022-10-13 stsp struct got_pack *pack = NULL;
758 8e359fa0 2022-10-13 stsp err = got_packidx_get_packfile_path(&path_packfile,
759 8e359fa0 2022-10-13 stsp packidx->path_packidx);
761 8e359fa0 2022-10-13 stsp return err;
763 8e359fa0 2022-10-13 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
764 8e359fa0 2022-10-13 stsp if (pack == NULL) {
765 8e359fa0 2022-10-13 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
770 8e359fa0 2022-10-13 stsp err = read_packed_tree_privsep(tree, pack,
771 8e359fa0 2022-10-13 stsp packidx, idx, id);
772 8e359fa0 2022-10-13 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
775 8e359fa0 2022-10-13 stsp err = got_object_open_loose_fd(&fd, id, repo);
777 8e359fa0 2022-10-13 stsp return err;
778 8e359fa0 2022-10-13 stsp err = read_tree_privsep(tree, fd, id, repo);
781 8e359fa0 2022-10-13 stsp if (err == NULL) {
782 8e359fa0 2022-10-13 stsp (*tree)->refcnt++;
783 8e359fa0 2022-10-13 stsp err = got_repo_cache_tree(repo, id, *tree);
786 8e359fa0 2022-10-13 stsp free(path_packfile);
787 8e359fa0 2022-10-13 stsp return err;
790 8e359fa0 2022-10-13 stsp const struct got_error *
791 8e359fa0 2022-10-13 stsp got_object_open_as_tree(struct got_tree_object **tree,
792 8e359fa0 2022-10-13 stsp struct got_repository *repo, struct got_object_id *id)
794 8e359fa0 2022-10-13 stsp *tree = got_repo_get_cached_tree(repo, id);
795 8e359fa0 2022-10-13 stsp if (*tree != NULL) {
796 8e359fa0 2022-10-13 stsp (*tree)->refcnt++;
797 8e359fa0 2022-10-13 stsp return NULL;
800 8e359fa0 2022-10-13 stsp return open_tree(tree, repo, id, 0);
803 8e359fa0 2022-10-13 stsp const struct got_error *
804 8e359fa0 2022-10-13 stsp got_object_tree_open(struct got_tree_object **tree,
805 8e359fa0 2022-10-13 stsp struct got_repository *repo, struct got_object *obj)
807 8e359fa0 2022-10-13 stsp return open_tree(tree, repo, got_object_get_id(obj), 1);
810 8e359fa0 2022-10-13 stsp static const struct got_error *
811 8e359fa0 2022-10-13 stsp request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
812 8e359fa0 2022-10-13 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
813 8e359fa0 2022-10-13 stsp struct got_object_id *id)
815 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
816 8e359fa0 2022-10-13 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
817 8e359fa0 2022-10-13 stsp int outfd_child;
819 06bd8ee4 2024-02-13 op err = pack_child_send_tempfiles(ibuf, pack);
821 8e359fa0 2022-10-13 stsp return err;
823 8e359fa0 2022-10-13 stsp outfd_child = dup(outfd);
824 8e359fa0 2022-10-13 stsp if (outfd_child == -1)
825 8e359fa0 2022-10-13 stsp return got_error_from_errno("dup");
827 8e359fa0 2022-10-13 stsp err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
829 8e359fa0 2022-10-13 stsp return err;
831 8e359fa0 2022-10-13 stsp err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
832 8e359fa0 2022-10-13 stsp outfd_child);
834 8e359fa0 2022-10-13 stsp return err;
837 8e359fa0 2022-10-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen,
838 8e359fa0 2022-10-13 stsp pack->privsep_child->ibuf);
840 8e359fa0 2022-10-13 stsp return err;
842 8e359fa0 2022-10-13 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
843 8e359fa0 2022-10-13 stsp err = got_error_from_errno("lseek");
845 8e359fa0 2022-10-13 stsp return err;
848 8e359fa0 2022-10-13 stsp static const struct got_error *
849 8e359fa0 2022-10-13 stsp read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
850 8e359fa0 2022-10-13 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
851 8e359fa0 2022-10-13 stsp struct got_object_id *id)
853 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
855 8e359fa0 2022-10-13 stsp if (pack->privsep_child == NULL) {
856 8e359fa0 2022-10-13 stsp err = got_pack_start_privsep_child(pack, packidx);
858 8e359fa0 2022-10-13 stsp return err;
861 8e359fa0 2022-10-13 stsp return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
865 8e359fa0 2022-10-13 stsp static const struct got_error *
866 8e359fa0 2022-10-13 stsp request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
867 8e359fa0 2022-10-13 stsp int infd, struct got_object_id *id, struct imsgbuf *ibuf)
869 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
870 8e359fa0 2022-10-13 stsp int outfd_child;
872 8e359fa0 2022-10-13 stsp outfd_child = dup(outfd);
873 8e359fa0 2022-10-13 stsp if (outfd_child == -1)
874 8e359fa0 2022-10-13 stsp return got_error_from_errno("dup");
876 8e359fa0 2022-10-13 stsp err = got_privsep_send_blob_req(ibuf, infd, id, -1);
878 8e359fa0 2022-10-13 stsp return err;
880 8e359fa0 2022-10-13 stsp err = got_privsep_send_blob_outfd(ibuf, outfd_child);
882 8e359fa0 2022-10-13 stsp return err;
884 8e359fa0 2022-10-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
886 8e359fa0 2022-10-13 stsp return err;
888 8e359fa0 2022-10-13 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
889 8e359fa0 2022-10-13 stsp return got_error_from_errno("lseek");
891 8e359fa0 2022-10-13 stsp return err;
894 8e359fa0 2022-10-13 stsp static const struct got_error *
895 8e359fa0 2022-10-13 stsp read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
896 8e359fa0 2022-10-13 stsp int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
898 8e359fa0 2022-10-13 stsp const struct got_error *err;
899 8e359fa0 2022-10-13 stsp struct imsgbuf *ibuf;
901 8e359fa0 2022-10-13 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
902 8e359fa0 2022-10-13 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
903 8e359fa0 2022-10-13 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, id,
907 1d9e43b0 2022-10-13 stsp err = start_child(repo, GOT_REPO_PRIVSEP_CHILD_BLOB);
909 8e359fa0 2022-10-13 stsp return err;
911 1d9e43b0 2022-10-13 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
912 8e359fa0 2022-10-13 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
915 8e359fa0 2022-10-13 stsp static const struct got_error *
916 8e359fa0 2022-10-13 stsp open_blob(struct got_blob_object **blob, struct got_repository *repo,
917 8e359fa0 2022-10-13 stsp struct got_object_id *id, size_t blocksize, int outfd)
919 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
920 8e359fa0 2022-10-13 stsp struct got_packidx *packidx = NULL;
921 8e359fa0 2022-10-13 stsp int idx, dfd = -1;
922 8e359fa0 2022-10-13 stsp char *path_packfile = NULL;
923 8e359fa0 2022-10-13 stsp uint8_t *outbuf;
924 8e359fa0 2022-10-13 stsp size_t size, hdrlen;
925 8e359fa0 2022-10-13 stsp struct stat sb;
927 8e359fa0 2022-10-13 stsp *blob = calloc(1, sizeof(**blob));
928 8e359fa0 2022-10-13 stsp if (*blob == NULL)
929 8e359fa0 2022-10-13 stsp return got_error_from_errno("calloc");
931 8e359fa0 2022-10-13 stsp (*blob)->read_buf = malloc(blocksize);
932 8e359fa0 2022-10-13 stsp if ((*blob)->read_buf == NULL) {
933 8e359fa0 2022-10-13 stsp err = got_error_from_errno("malloc");
937 8e359fa0 2022-10-13 stsp if (ftruncate(outfd, 0L) == -1) {
938 8e359fa0 2022-10-13 stsp err = got_error_from_errno("ftruncate");
941 8e359fa0 2022-10-13 stsp if (lseek(outfd, SEEK_SET, 0) == -1) {
942 8e359fa0 2022-10-13 stsp err = got_error_from_errno("lseek");
946 8e359fa0 2022-10-13 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
947 8e359fa0 2022-10-13 stsp if (err == NULL) {
948 8e359fa0 2022-10-13 stsp struct got_pack *pack = NULL;
950 8e359fa0 2022-10-13 stsp err = got_packidx_get_packfile_path(&path_packfile,
951 8e359fa0 2022-10-13 stsp packidx->path_packidx);
955 8e359fa0 2022-10-13 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
956 8e359fa0 2022-10-13 stsp if (pack == NULL) {
957 8e359fa0 2022-10-13 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
962 8e359fa0 2022-10-13 stsp err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
963 8e359fa0 2022-10-13 stsp pack, packidx, idx, id);
964 8e359fa0 2022-10-13 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
967 8e359fa0 2022-10-13 stsp err = got_object_open_loose_fd(&infd, id, repo);
970 8e359fa0 2022-10-13 stsp err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
976 8e359fa0 2022-10-13 stsp if (hdrlen > size) {
977 8e359fa0 2022-10-13 stsp err = got_error(GOT_ERR_BAD_OBJ_HDR);
981 8e359fa0 2022-10-13 stsp if (outbuf) {
982 8e359fa0 2022-10-13 stsp (*blob)->f = fmemopen(outbuf, size, "rb");
983 8e359fa0 2022-10-13 stsp if ((*blob)->f == NULL) {
984 8e359fa0 2022-10-13 stsp err = got_error_from_errno("fmemopen");
985 8e359fa0 2022-10-13 stsp free(outbuf);
988 8e359fa0 2022-10-13 stsp (*blob)->data = outbuf;
990 8e359fa0 2022-10-13 stsp if (fstat(outfd, &sb) == -1) {
991 8e359fa0 2022-10-13 stsp err = got_error_from_errno("fstat");
995 8e359fa0 2022-10-13 stsp if (sb.st_size != size) {
996 8e359fa0 2022-10-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1000 8e359fa0 2022-10-13 stsp dfd = dup(outfd);
1001 8e359fa0 2022-10-13 stsp if (dfd == -1) {
1002 8e359fa0 2022-10-13 stsp err = got_error_from_errno("dup");
1003 8e359fa0 2022-10-13 stsp goto done;
1006 8e359fa0 2022-10-13 stsp (*blob)->f = fdopen(dfd, "rb");
1007 8e359fa0 2022-10-13 stsp if ((*blob)->f == NULL) {
1008 8e359fa0 2022-10-13 stsp err = got_error_from_errno("fdopen");
1009 8e359fa0 2022-10-13 stsp close(dfd);
1011 8e359fa0 2022-10-13 stsp goto done;
1015 8e359fa0 2022-10-13 stsp (*blob)->hdrlen = hdrlen;
1016 8e359fa0 2022-10-13 stsp (*blob)->blocksize = blocksize;
1017 08175cbb 2023-02-10 op memcpy(&(*blob)->id, id, sizeof(*id));
1020 8e359fa0 2022-10-13 stsp free(path_packfile);
1021 8e359fa0 2022-10-13 stsp if (err) {
1022 8e359fa0 2022-10-13 stsp if (*blob) {
1023 8e359fa0 2022-10-13 stsp got_object_blob_close(*blob);
1024 8e359fa0 2022-10-13 stsp *blob = NULL;
1027 8e359fa0 2022-10-13 stsp return err;
1030 8e359fa0 2022-10-13 stsp const struct got_error *
1031 8e359fa0 2022-10-13 stsp got_object_open_as_blob(struct got_blob_object **blob,
1032 8e359fa0 2022-10-13 stsp struct got_repository *repo, struct got_object_id *id, size_t blocksize,
1033 8e359fa0 2022-10-13 stsp int outfd)
1035 8e359fa0 2022-10-13 stsp return open_blob(blob, repo, id, blocksize, outfd);
1038 8e359fa0 2022-10-13 stsp const struct got_error *
1039 8e359fa0 2022-10-13 stsp got_object_blob_open(struct got_blob_object **blob,
1040 8e359fa0 2022-10-13 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize,
1041 8e359fa0 2022-10-13 stsp int outfd)
1043 8e359fa0 2022-10-13 stsp return open_blob(blob, repo, got_object_get_id(obj), blocksize, outfd);
1046 8e359fa0 2022-10-13 stsp static const struct got_error *
1047 8e359fa0 2022-10-13 stsp request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1048 8e359fa0 2022-10-13 stsp int pack_idx, struct got_object_id *id)
1050 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
1052 8e359fa0 2022-10-13 stsp err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1053 8e359fa0 2022-10-13 stsp pack_idx);
1055 8e359fa0 2022-10-13 stsp return err;
1057 8e359fa0 2022-10-13 stsp return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1060 8e359fa0 2022-10-13 stsp static const struct got_error *
1061 8e359fa0 2022-10-13 stsp read_packed_tag_privsep(struct got_tag_object **tag,
1062 8e359fa0 2022-10-13 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1063 8e359fa0 2022-10-13 stsp struct got_object_id *id)
1065 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
1067 8e359fa0 2022-10-13 stsp if (pack->privsep_child)
1068 8e359fa0 2022-10-13 stsp return request_packed_tag(tag, pack, idx, id);
1070 8e359fa0 2022-10-13 stsp err = got_pack_start_privsep_child(pack, packidx);
1072 8e359fa0 2022-10-13 stsp return err;
1074 8e359fa0 2022-10-13 stsp return request_packed_tag(tag, pack, idx, id);
1077 8e359fa0 2022-10-13 stsp static const struct got_error *
1078 8e359fa0 2022-10-13 stsp request_tag(struct got_tag_object **tag, struct got_repository *repo,
1079 8e359fa0 2022-10-13 stsp int fd, struct got_object_id *id)
1081 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
1082 8e359fa0 2022-10-13 stsp struct imsgbuf *ibuf;
1084 8e359fa0 2022-10-13 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1086 8e359fa0 2022-10-13 stsp err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1088 8e359fa0 2022-10-13 stsp return err;
1090 8e359fa0 2022-10-13 stsp return got_privsep_recv_tag(tag, ibuf);
1093 8e359fa0 2022-10-13 stsp static const struct got_error *
1094 8e359fa0 2022-10-13 stsp read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1095 8e359fa0 2022-10-13 stsp struct got_object_id *id, struct got_repository *repo)
1097 8e359fa0 2022-10-13 stsp const struct got_error *err;
1099 8e359fa0 2022-10-13 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1100 8e359fa0 2022-10-13 stsp return request_tag(tag, repo, obj_fd, id);
1102 1d9e43b0 2022-10-13 stsp err = start_child(repo, GOT_REPO_PRIVSEP_CHILD_TAG);
1104 8e359fa0 2022-10-13 stsp return err;
1106 8e359fa0 2022-10-13 stsp return request_tag(tag, repo, obj_fd, id);
1109 8e359fa0 2022-10-13 stsp static const struct got_error *
1110 8e359fa0 2022-10-13 stsp open_tag(struct got_tag_object **tag, struct got_repository *repo,
1111 8e359fa0 2022-10-13 stsp struct got_object_id *id, int check_cache)
1113 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
1114 8e359fa0 2022-10-13 stsp struct got_packidx *packidx = NULL;
1116 8e359fa0 2022-10-13 stsp char *path_packfile = NULL;
1117 8e359fa0 2022-10-13 stsp struct got_object *obj = NULL;
1118 8e359fa0 2022-10-13 stsp int obj_type = GOT_OBJ_TYPE_ANY;
1120 8e359fa0 2022-10-13 stsp if (check_cache) {
1121 8e359fa0 2022-10-13 stsp *tag = got_repo_get_cached_tag(repo, id);
1122 8e359fa0 2022-10-13 stsp if (*tag != NULL) {
1123 8e359fa0 2022-10-13 stsp (*tag)->refcnt++;
1124 8e359fa0 2022-10-13 stsp return NULL;
1127 8e359fa0 2022-10-13 stsp *tag = NULL;
1129 8e359fa0 2022-10-13 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1130 8e359fa0 2022-10-13 stsp if (err == NULL) {
1131 8e359fa0 2022-10-13 stsp struct got_pack *pack = NULL;
1133 8e359fa0 2022-10-13 stsp err = got_packidx_get_packfile_path(&path_packfile,
1134 8e359fa0 2022-10-13 stsp packidx->path_packidx);
1136 8e359fa0 2022-10-13 stsp return err;
1138 8e359fa0 2022-10-13 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1139 8e359fa0 2022-10-13 stsp if (pack == NULL) {
1140 8e359fa0 2022-10-13 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1143 8e359fa0 2022-10-13 stsp goto done;
1146 8e359fa0 2022-10-13 stsp /* Beware of "lightweight" tags: Check object type first. */
1147 8e359fa0 2022-10-13 stsp err = read_packed_object_privsep(&obj, repo, pack, packidx,
1150 8e359fa0 2022-10-13 stsp goto done;
1151 8e359fa0 2022-10-13 stsp obj_type = obj->type;
1152 8e359fa0 2022-10-13 stsp got_object_close(obj);
1153 8e359fa0 2022-10-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG) {
1154 8e359fa0 2022-10-13 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1155 8e359fa0 2022-10-13 stsp goto done;
1157 8e359fa0 2022-10-13 stsp err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1158 8e359fa0 2022-10-13 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1161 8e359fa0 2022-10-13 stsp err = got_object_open_loose_fd(&fd, id, repo);
1163 8e359fa0 2022-10-13 stsp return err;
1164 8e359fa0 2022-10-13 stsp err = got_object_read_header_privsep(&obj, id, repo, fd);
1166 8e359fa0 2022-10-13 stsp return err;
1167 8e359fa0 2022-10-13 stsp obj_type = obj->type;
1168 8e359fa0 2022-10-13 stsp got_object_close(obj);
1169 8e359fa0 2022-10-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
1170 8e359fa0 2022-10-13 stsp return got_error(GOT_ERR_OBJ_TYPE);
1172 8e359fa0 2022-10-13 stsp err = got_object_open_loose_fd(&fd, id, repo);
1174 8e359fa0 2022-10-13 stsp return err;
1175 8e359fa0 2022-10-13 stsp err = read_tag_privsep(tag, fd, id, repo);
1178 8e359fa0 2022-10-13 stsp if (err == NULL) {
1179 8e359fa0 2022-10-13 stsp (*tag)->refcnt++;
1180 8e359fa0 2022-10-13 stsp err = got_repo_cache_tag(repo, id, *tag);
1183 8e359fa0 2022-10-13 stsp free(path_packfile);
1184 8e359fa0 2022-10-13 stsp return err;
1187 8e359fa0 2022-10-13 stsp const struct got_error *
1188 8e359fa0 2022-10-13 stsp got_object_open_as_tag(struct got_tag_object **tag,
1189 8e359fa0 2022-10-13 stsp struct got_repository *repo, struct got_object_id *id)
1191 8e359fa0 2022-10-13 stsp *tag = got_repo_get_cached_tag(repo, id);
1192 8e359fa0 2022-10-13 stsp if (*tag != NULL) {
1193 8e359fa0 2022-10-13 stsp (*tag)->refcnt++;
1194 8e359fa0 2022-10-13 stsp return NULL;
1197 8e359fa0 2022-10-13 stsp return open_tag(tag, repo, id, 0);
1200 8e359fa0 2022-10-13 stsp const struct got_error *
1201 8e359fa0 2022-10-13 stsp got_object_tag_open(struct got_tag_object **tag,
1202 8e359fa0 2022-10-13 stsp struct got_repository *repo, struct got_object *obj)
1204 8e359fa0 2022-10-13 stsp return open_tag(tag, repo, got_object_get_id(obj), 1);
1207 8e359fa0 2022-10-13 stsp const struct got_error *
1208 8e359fa0 2022-10-13 stsp got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
1209 8e359fa0 2022-10-13 stsp struct got_object_id *commit_id, const char *path,
1210 8e359fa0 2022-10-13 stsp struct got_repository *repo)
1212 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
1213 8e359fa0 2022-10-13 stsp struct got_pack *pack = NULL;
1214 8e359fa0 2022-10-13 stsp struct got_packidx *packidx = NULL;
1215 8e359fa0 2022-10-13 stsp char *path_packfile = NULL;
1216 8e359fa0 2022-10-13 stsp struct got_commit_object *changed_commit = NULL;
1217 8e359fa0 2022-10-13 stsp struct got_object_id *changed_commit_id = NULL;
1220 8e359fa0 2022-10-13 stsp err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
1221 8e359fa0 2022-10-13 stsp if (err) {
1222 8e359fa0 2022-10-13 stsp if (err->code != GOT_ERR_NO_OBJ)
1223 8e359fa0 2022-10-13 stsp return err;
1224 8e359fa0 2022-10-13 stsp return NULL;
1227 8e359fa0 2022-10-13 stsp err = got_packidx_get_packfile_path(&path_packfile,
1228 8e359fa0 2022-10-13 stsp packidx->path_packidx);
1230 8e359fa0 2022-10-13 stsp return err;
1232 8e359fa0 2022-10-13 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1233 8e359fa0 2022-10-13 stsp if (pack == NULL) {
1234 8e359fa0 2022-10-13 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
1236 8e359fa0 2022-10-13 stsp goto done;
1239 8e359fa0 2022-10-13 stsp if (pack->privsep_child == NULL) {
1240 8e359fa0 2022-10-13 stsp err = got_pack_start_privsep_child(pack, packidx);
1242 8e359fa0 2022-10-13 stsp goto done;
1245 8e359fa0 2022-10-13 stsp err = got_privsep_send_commit_traversal_request(
1246 8e359fa0 2022-10-13 stsp pack->privsep_child->ibuf, commit_id, idx, path);
1248 8e359fa0 2022-10-13 stsp goto done;
1250 8e359fa0 2022-10-13 stsp err = got_privsep_recv_traversed_commits(&changed_commit,
1251 8e359fa0 2022-10-13 stsp &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
1253 8e359fa0 2022-10-13 stsp goto done;
1255 8e359fa0 2022-10-13 stsp if (changed_commit) {
1257 8e359fa0 2022-10-13 stsp * Cache the commit in which the path was changed.
1258 8e359fa0 2022-10-13 stsp * This commit might be opened again soon.
1260 8e359fa0 2022-10-13 stsp changed_commit->refcnt++;
1261 8e359fa0 2022-10-13 stsp err = got_repo_cache_commit(repo, changed_commit_id,
1262 8e359fa0 2022-10-13 stsp changed_commit);
1263 8e359fa0 2022-10-13 stsp got_object_commit_close(changed_commit);
1266 8e359fa0 2022-10-13 stsp free(path_packfile);
1267 8e359fa0 2022-10-13 stsp free(changed_commit_id);
1268 8e359fa0 2022-10-13 stsp return err;
1271 8e359fa0 2022-10-13 stsp const struct got_error *
1272 8e359fa0 2022-10-13 stsp got_object_enumerate(int *found_all_objects,
1273 8e359fa0 2022-10-13 stsp got_object_enumerate_commit_cb cb_commit,
1274 8e359fa0 2022-10-13 stsp got_object_enumerate_tree_cb cb_tree, void *cb_arg,
1275 8e359fa0 2022-10-13 stsp struct got_object_id **ours, int nours,
1276 8e359fa0 2022-10-13 stsp struct got_object_id **theirs, int ntheirs,
1277 8e359fa0 2022-10-13 stsp struct got_packidx *packidx, struct got_repository *repo)
1279 8e359fa0 2022-10-13 stsp const struct got_error *err = NULL;
1280 8e359fa0 2022-10-13 stsp struct got_pack *pack;
1281 8e359fa0 2022-10-13 stsp char *path_packfile = NULL;
1283 8e359fa0 2022-10-13 stsp err = got_packidx_get_packfile_path(&path_packfile,
1284 8e359fa0 2022-10-13 stsp packidx->path_packidx);
1286 8e359fa0 2022-10-13 stsp return err;
1288 8e359fa0 2022-10-13 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1289 8e359fa0 2022-10-13 stsp if (pack == NULL) {
1290 8e359fa0 2022-10-13 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
1292 8e359fa0 2022-10-13 stsp goto done;
1295 8e359fa0 2022-10-13 stsp if (pack->privsep_child == NULL) {
1296 8e359fa0 2022-10-13 stsp err = got_pack_start_privsep_child(pack, packidx);
1298 8e359fa0 2022-10-13 stsp goto done;
1301 8e359fa0 2022-10-13 stsp err = got_privsep_send_object_enumeration_request(
1302 8e359fa0 2022-10-13 stsp pack->privsep_child->ibuf);
1304 8e359fa0 2022-10-13 stsp goto done;
1306 8e359fa0 2022-10-13 stsp err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
1307 8e359fa0 2022-10-13 stsp ours, nours);
1309 8e359fa0 2022-10-13 stsp goto done;
1310 8e359fa0 2022-10-13 stsp err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
1312 8e359fa0 2022-10-13 stsp goto done;
1314 8e359fa0 2022-10-13 stsp err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
1315 8e359fa0 2022-10-13 stsp theirs, ntheirs);
1317 8e359fa0 2022-10-13 stsp goto done;
1318 8e359fa0 2022-10-13 stsp err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
1320 8e359fa0 2022-10-13 stsp goto done;
1322 8e359fa0 2022-10-13 stsp err = got_privsep_recv_enumerated_objects(found_all_objects,
1323 8e359fa0 2022-10-13 stsp pack->privsep_child->ibuf, cb_commit, cb_tree, cb_arg, repo);
1325 8e359fa0 2022-10-13 stsp free(path_packfile);
1326 8e359fa0 2022-10-13 stsp return err;