2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 d71d75ad 2017-11-05 stsp * Permission to use, copy, modify, and distribute this software for any
5 d71d75ad 2017-11-05 stsp * purpose with or without fee is hereby granted, provided that the above
6 d71d75ad 2017-11-05 stsp * copyright notice and this permission notice appear in all copies.
8 d71d75ad 2017-11-05 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 d71d75ad 2017-11-05 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 d71d75ad 2017-11-05 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 d71d75ad 2017-11-05 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 d71d75ad 2017-11-05 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 d71d75ad 2017-11-05 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 d71d75ad 2017-11-05 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 2178c42e 2018-04-22 stsp #include <sys/types.h>
18 0ffeb3c2 2017-11-26 stsp #include <sys/stat.h>
19 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
20 2178c42e 2018-04-22 stsp #include <sys/uio.h>
21 2178c42e 2018-04-22 stsp #include <sys/socket.h>
22 2178c42e 2018-04-22 stsp #include <sys/wait.h>
23 2b0ae357 2022-01-10 thomas #include <sys/mman.h>
25 a1fd68d8 2018-01-12 stsp #include <errno.h>
26 2178c42e 2018-04-22 stsp #include <fcntl.h>
27 d71d75ad 2017-11-05 stsp #include <stdio.h>
28 ab9a70b2 2017-11-06 stsp #include <stdlib.h>
29 ab9a70b2 2017-11-06 stsp #include <string.h>
30 2178c42e 2018-04-22 stsp #include <stdint.h>
31 81a12da5 2020-09-09 naddy #include <unistd.h>
32 ab9a70b2 2017-11-06 stsp #include <zlib.h>
33 ab9a70b2 2017-11-06 stsp #include <ctype.h>
34 e40622f4 2020-07-23 stsp #include <libgen.h>
35 ab9a70b2 2017-11-06 stsp #include <limits.h>
36 788c352e 2018-06-16 stsp #include <time.h>
38 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
40 ab9a70b2 2017-11-06 stsp #include "got_error.h"
41 d71d75ad 2017-11-05 stsp #include "got_object.h"
42 ab9a70b2 2017-11-06 stsp #include "got_repository.h"
43 511a516b 2018-05-19 stsp #include "got_opentemp.h"
44 324d37e7 2019-05-11 stsp #include "got_path.h"
46 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
47 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
48 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
49 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
50 2178c42e 2018-04-22 stsp #include "got_lib_privsep.h"
51 6bef87be 2018-09-11 stsp #include "got_lib_object_idcache.h"
52 6bef87be 2018-09-11 stsp #include "got_lib_object_cache.h"
53 ad242220 2018-09-08 stsp #include "got_lib_object_parse.h"
54 15a94983 2018-12-23 stsp #include "got_lib_pack.h"
55 7bb0daa1 2018-06-21 stsp #include "got_lib_repository.h"
58 ab9a70b2 2017-11-06 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 63915ee5 2022-06-23 thomas #ifndef nitems
62 63915ee5 2022-06-23 thomas #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
65 3235492e 2018-04-01 stsp struct got_object_id *
66 3235492e 2018-04-01 stsp got_object_get_id(struct got_object *obj)
68 6402fb3c 2018-09-15 stsp return &obj->id;
71 bacc9935 2018-05-20 stsp const struct got_error *
72 bacc9935 2018-05-20 stsp got_object_get_id_str(char **outbuf, struct got_object *obj)
74 bacc9935 2018-05-20 stsp return got_object_id_str(outbuf, &obj->id);
77 15a94983 2018-12-23 stsp const struct got_error *
78 15a94983 2018-12-23 stsp got_object_get_type(int *type, struct got_repository *repo,
79 15a94983 2018-12-23 stsp struct got_object_id *id)
81 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
82 15a94983 2018-12-23 stsp struct got_object *obj;
84 15a94983 2018-12-23 stsp err = got_object_open(&obj, repo, id);
88 b107e67f 2018-01-19 stsp switch (obj->type) {
89 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_COMMIT:
90 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_TREE:
91 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_BLOB:
92 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
93 15a94983 2018-12-23 stsp *type = obj->type;
96 15a94983 2018-12-23 stsp err = got_error(GOT_ERR_OBJ_TYPE);
100 15a94983 2018-12-23 stsp got_object_close(obj);
101 15a94983 2018-12-23 stsp return err;
104 90bdb554 2019-04-11 stsp const struct got_error *
105 90bdb554 2019-04-11 stsp got_object_get_path(char **path, struct got_object_id *id,
106 90bdb554 2019-04-11 stsp struct got_repository *repo)
108 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
109 7a132809 2018-07-23 stsp char *hex = NULL;
110 41d2888b 2019-08-11 stsp char *path_objects;
112 e6b1056e 2018-04-22 stsp *path = NULL;
114 41d2888b 2019-08-11 stsp path_objects = got_repo_get_path_objects(repo);
115 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
116 638f9024 2019-05-13 stsp return got_error_from_errno("got_repo_get_path_objects");
118 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
122 d1cda826 2017-11-06 stsp if (asprintf(path, "%s/%.2x/%s", path_objects,
123 d1cda826 2017-11-06 stsp id->sha1[0], hex + 2) == -1)
124 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
128 d1cda826 2017-11-06 stsp free(path_objects);
129 d1cda826 2017-11-06 stsp return err;
132 762d73f4 2021-04-10 stsp const struct got_error *
133 762d73f4 2021-04-10 stsp got_object_open_loose_fd(int *fd, struct got_object_id *id,
134 4796fb13 2018-12-23 stsp struct got_repository *repo)
136 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
137 a1fd68d8 2018-01-12 stsp char *path;
139 90bdb554 2019-04-11 stsp err = got_object_get_path(&path, id, repo);
141 d1cda826 2017-11-06 stsp return err;
142 06340621 2021-12-31 thomas *fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
143 d5003b79 2018-04-22 stsp if (*fd == -1) {
144 e82b1d81 2019-07-27 stsp err = got_error_from_errno2("open", path);
148 4558fcd4 2018-01-14 stsp free(path);
149 2090a03d 2018-09-09 stsp return err;
152 2090a03d 2018-09-09 stsp static const struct got_error *
153 a158c901 2018-12-23 stsp request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
154 a158c901 2018-12-23 stsp struct got_object_id *id)
156 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
157 a158c901 2018-12-23 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
159 a158c901 2018-12-23 stsp err = got_privsep_send_packed_obj_req(ibuf, idx, id);
161 a158c901 2018-12-23 stsp return err;
163 a158c901 2018-12-23 stsp err = got_privsep_recv_obj(obj, ibuf);
165 a158c901 2018-12-23 stsp return err;
167 a158c901 2018-12-23 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
169 a158c901 2018-12-23 stsp return NULL;
172 bc1f382f 2022-01-05 thomas /* Create temporary files used during delta application. */
173 59d1e4a0 2021-03-10 stsp static const struct got_error *
174 bc1f382f 2022-01-05 thomas pack_child_send_tempfiles(struct imsgbuf *ibuf, struct got_pack *pack)
176 bc1f382f 2022-01-05 thomas const struct got_error *err;
177 bfb5ee0b 2022-05-31 thomas int basefd = -1, accumfd = -1;
180 bc1f382f 2022-01-05 thomas * For performance reasons, the child will keep reusing the
181 bc1f382f 2022-01-05 thomas * same temporary files during every object request.
182 bc1f382f 2022-01-05 thomas * Opening and closing new files for every object request is
183 bc1f382f 2022-01-05 thomas * too expensive during operations such as 'gotadmin pack'.
185 bc1f382f 2022-01-05 thomas if (pack->child_has_tempfiles)
186 bc1f382f 2022-01-05 thomas return NULL;
188 bfb5ee0b 2022-05-31 thomas basefd = dup(pack->basefd);
189 59d1e4a0 2021-03-10 stsp if (basefd == -1)
190 bfb5ee0b 2022-05-31 thomas return got_error_from_errno("dup");
192 bfb5ee0b 2022-05-31 thomas accumfd = dup(pack->accumfd);
193 bfb5ee0b 2022-05-31 thomas if (accumfd == -1) {
194 bfb5ee0b 2022-05-31 thomas err = got_error_from_errno("dup");
195 bfb5ee0b 2022-05-31 thomas goto done;
198 bc1f382f 2022-01-05 thomas err = got_privsep_send_tmpfd(ibuf, basefd);
200 bfb5ee0b 2022-05-31 thomas goto done;
202 bc1f382f 2022-01-05 thomas err = got_privsep_send_tmpfd(ibuf, accumfd);
204 bfb5ee0b 2022-05-31 thomas if (err) {
205 bfb5ee0b 2022-05-31 thomas if (basefd != -1)
206 bfb5ee0b 2022-05-31 thomas close(basefd);
207 bfb5ee0b 2022-05-31 thomas if (accumfd != -1)
208 bfb5ee0b 2022-05-31 thomas close(accumfd);
210 bfb5ee0b 2022-05-31 thomas pack->child_has_tempfiles = 1;
211 bc1f382f 2022-01-05 thomas return NULL;
214 bc1f382f 2022-01-05 thomas static const struct got_error *
215 bc1f382f 2022-01-05 thomas request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
216 bc1f382f 2022-01-05 thomas int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
218 bc1f382f 2022-01-05 thomas const struct got_error *err = NULL;
219 bc1f382f 2022-01-05 thomas struct imsgbuf *ibuf = pack->privsep_child->ibuf;
220 bc1f382f 2022-01-05 thomas int outfd_child;
222 bc1f382f 2022-01-05 thomas err = pack_child_send_tempfiles(ibuf, pack);
224 bc1f382f 2022-01-05 thomas return err;
226 bc1f382f 2022-01-05 thomas outfd_child = dup(outfd);
227 bc1f382f 2022-01-05 thomas if (outfd_child == -1)
228 bc1f382f 2022-01-05 thomas return got_error_from_errno("dup");
230 59d1e4a0 2021-03-10 stsp err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
232 59d1e4a0 2021-03-10 stsp close(outfd_child);
233 59d1e4a0 2021-03-10 stsp return err;
236 59d1e4a0 2021-03-10 stsp err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
238 59d1e4a0 2021-03-10 stsp return err;
240 59d1e4a0 2021-03-10 stsp err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
242 59d1e4a0 2021-03-10 stsp return err;
244 59d1e4a0 2021-03-10 stsp return NULL;
247 711fb6e8 2018-12-23 stsp static const struct got_error *
248 711fb6e8 2018-12-23 stsp read_packed_object_privsep(struct got_object **obj,
249 711fb6e8 2018-12-23 stsp struct got_repository *repo, struct got_pack *pack,
250 711fb6e8 2018-12-23 stsp struct got_packidx *packidx, int idx, struct got_object_id *id)
252 711fb6e8 2018-12-23 stsp const struct got_error *err = NULL;
254 59d1e4a0 2021-03-10 stsp if (pack->privsep_child == NULL) {
255 68036464 2022-06-26 thomas err = got_pack_start_privsep_child(pack, packidx);
257 59d1e4a0 2021-03-10 stsp return err;
260 711fb6e8 2018-12-23 stsp return request_packed_object(obj, pack, idx, id);
263 59d1e4a0 2021-03-10 stsp static const struct got_error *
264 59d1e4a0 2021-03-10 stsp read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
265 59d1e4a0 2021-03-10 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
266 59d1e4a0 2021-03-10 stsp struct got_object_id *id)
268 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
270 59d1e4a0 2021-03-10 stsp if (pack->privsep_child == NULL) {
271 68036464 2022-06-26 thomas err = got_pack_start_privsep_child(pack, packidx);
273 59d1e4a0 2021-03-10 stsp return err;
276 59d1e4a0 2021-03-10 stsp return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
280 b3d68e7f 2021-07-03 stsp const struct got_error *
281 b3d68e7f 2021-07-03 stsp got_object_open_packed(struct got_object **obj, struct got_object_id *id,
282 2090a03d 2018-09-09 stsp struct got_repository *repo)
284 2090a03d 2018-09-09 stsp const struct got_error *err = NULL;
285 2090a03d 2018-09-09 stsp struct got_pack *pack = NULL;
286 2090a03d 2018-09-09 stsp struct got_packidx *packidx = NULL;
288 2090a03d 2018-09-09 stsp char *path_packfile;
290 2090a03d 2018-09-09 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
292 2090a03d 2018-09-09 stsp return err;
294 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
295 aea75d87 2021-07-06 stsp packidx->path_packidx);
297 2090a03d 2018-09-09 stsp return err;
299 2090a03d 2018-09-09 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
300 2090a03d 2018-09-09 stsp if (pack == NULL) {
301 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
306 a158c901 2018-12-23 stsp err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
310 2090a03d 2018-09-09 stsp free(path_packfile);
311 4558fcd4 2018-01-14 stsp return err;
314 f9c2e8e5 2022-02-13 thomas const struct got_error *
315 f9c2e8e5 2022-02-13 thomas got_object_open_from_packfile(struct got_object **obj, struct got_object_id *id,
316 f9c2e8e5 2022-02-13 thomas struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
317 f9c2e8e5 2022-02-13 thomas struct got_repository *repo)
319 f9c2e8e5 2022-02-13 thomas return read_packed_object_privsep(obj, repo, pack, packidx,
320 f9c2e8e5 2022-02-13 thomas obj_idx, id);
323 f9c2e8e5 2022-02-13 thomas const struct got_error *
324 f9c2e8e5 2022-02-13 thomas got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
325 9249e7e3 2022-05-12 thomas off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
326 9249e7e3 2022-05-12 thomas off_t *delta_out_offset, struct got_object_id **base_id, int delta_cache_fd,
327 f9c2e8e5 2022-02-13 thomas struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
328 f9c2e8e5 2022-02-13 thomas struct got_repository *repo)
330 f9c2e8e5 2022-02-13 thomas const struct got_error *err = NULL;
331 f9c2e8e5 2022-02-13 thomas struct got_pack *pack = NULL;
332 f9c2e8e5 2022-02-13 thomas char *path_packfile;
334 f9c2e8e5 2022-02-13 thomas *base_size = 0;
335 f9c2e8e5 2022-02-13 thomas *result_size = 0;
336 f9c2e8e5 2022-02-13 thomas *delta_size = 0;
337 9249e7e3 2022-05-12 thomas *delta_compressed_size = 0;
338 f9c2e8e5 2022-02-13 thomas *delta_offset = 0;
339 f9c2e8e5 2022-02-13 thomas *delta_out_offset = 0;
341 f9c2e8e5 2022-02-13 thomas err = got_packidx_get_packfile_path(&path_packfile,
342 f9c2e8e5 2022-02-13 thomas packidx->path_packidx);
344 f9c2e8e5 2022-02-13 thomas return err;
346 f9c2e8e5 2022-02-13 thomas pack = got_repo_get_cached_pack(repo, path_packfile);
347 f9c2e8e5 2022-02-13 thomas if (pack == NULL) {
348 f9c2e8e5 2022-02-13 thomas err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
350 f9c2e8e5 2022-02-13 thomas return err;
353 f9c2e8e5 2022-02-13 thomas if (pack->privsep_child == NULL) {
354 68036464 2022-06-26 thomas err = got_pack_start_privsep_child(pack, packidx);
356 f9c2e8e5 2022-02-13 thomas return err;
359 f9c2e8e5 2022-02-13 thomas if (!pack->child_has_delta_outfd) {
360 f9c2e8e5 2022-02-13 thomas int outfd_child;
361 f9c2e8e5 2022-02-13 thomas outfd_child = dup(delta_cache_fd);
362 f9c2e8e5 2022-02-13 thomas if (outfd_child == -1)
363 f9c2e8e5 2022-02-13 thomas return got_error_from_errno("dup");
364 f9c2e8e5 2022-02-13 thomas err = got_privsep_send_raw_delta_outfd(
365 f9c2e8e5 2022-02-13 thomas pack->privsep_child->ibuf, outfd_child);
367 f9c2e8e5 2022-02-13 thomas return err;
368 f9c2e8e5 2022-02-13 thomas pack->child_has_delta_outfd = 1;
371 f9c2e8e5 2022-02-13 thomas err = got_privsep_send_raw_delta_req(pack->privsep_child->ibuf,
372 f9c2e8e5 2022-02-13 thomas obj_idx, id);
374 f9c2e8e5 2022-02-13 thomas return err;
376 f9c2e8e5 2022-02-13 thomas return got_privsep_recv_raw_delta(base_size, result_size, delta_size,
377 9249e7e3 2022-05-12 thomas delta_compressed_size, delta_offset, delta_out_offset, base_id,
378 9249e7e3 2022-05-12 thomas pack->privsep_child->ibuf);
381 9f2369b0 2018-12-24 stsp static const struct got_error *
382 d5c81d44 2021-07-08 stsp request_object(struct got_object **obj, struct got_object_id *id,
383 d5c81d44 2021-07-08 stsp struct got_repository *repo, int fd)
385 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
386 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
388 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
390 d5c81d44 2021-07-08 stsp err = got_privsep_send_obj_req(ibuf, fd, id);
392 9f2369b0 2018-12-24 stsp return err;
394 9f2369b0 2018-12-24 stsp return got_privsep_recv_obj(obj, ibuf);
397 9f2369b0 2018-12-24 stsp static const struct got_error *
398 59d1e4a0 2021-03-10 stsp request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
399 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo, int infd)
401 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
402 59d1e4a0 2021-03-10 stsp struct imsgbuf *ibuf;
403 59d1e4a0 2021-03-10 stsp int outfd_child;
405 59d1e4a0 2021-03-10 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
407 59d1e4a0 2021-03-10 stsp outfd_child = dup(outfd);
408 59d1e4a0 2021-03-10 stsp if (outfd_child == -1)
409 59d1e4a0 2021-03-10 stsp return got_error_from_errno("dup");
411 d5c81d44 2021-07-08 stsp err = got_privsep_send_raw_obj_req(ibuf, infd, id);
413 59d1e4a0 2021-03-10 stsp return err;
415 59d1e4a0 2021-03-10 stsp err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
417 59d1e4a0 2021-03-10 stsp return err;
419 59d1e4a0 2021-03-10 stsp return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
422 59d1e4a0 2021-03-10 stsp static const struct got_error *
423 59d1e4a0 2021-03-10 stsp start_read_object_child(struct got_repository *repo)
425 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
426 9f2369b0 2018-12-24 stsp int imsg_fds[2];
428 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
430 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
431 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
432 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
434 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
435 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
436 ddc7b220 2019-09-08 stsp free(ibuf);
437 ddc7b220 2019-09-08 stsp return err;
440 9f2369b0 2018-12-24 stsp pid = fork();
441 ddc7b220 2019-09-08 stsp if (pid == -1) {
442 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
443 ddc7b220 2019-09-08 stsp free(ibuf);
444 ddc7b220 2019-09-08 stsp return err;
446 9f2369b0 2018-12-24 stsp else if (pid == 0) {
447 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
448 9f2369b0 2018-12-24 stsp repo->path);
449 9f2369b0 2018-12-24 stsp /* not reached */
452 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
453 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
454 ddc7b220 2019-09-08 stsp free(ibuf);
455 ddc7b220 2019-09-08 stsp return err;
458 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
459 9f2369b0 2018-12-24 stsp imsg_fds[0];
460 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
461 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
462 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
464 59d1e4a0 2021-03-10 stsp return NULL;
467 b3d68e7f 2021-07-03 stsp const struct got_error *
468 b3d68e7f 2021-07-03 stsp got_object_read_header_privsep(struct got_object **obj,
469 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo, int obj_fd)
471 59d1e4a0 2021-03-10 stsp const struct got_error *err;
473 59d1e4a0 2021-03-10 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
474 d5c81d44 2021-07-08 stsp return request_object(obj, id, repo, obj_fd);
476 59d1e4a0 2021-03-10 stsp err = start_read_object_child(repo);
478 7495ec13 2021-04-04 stsp close(obj_fd);
479 59d1e4a0 2021-03-10 stsp return err;
482 d5c81d44 2021-07-08 stsp return request_object(obj, id, repo, obj_fd);
485 59d1e4a0 2021-03-10 stsp static const struct got_error *
486 59d1e4a0 2021-03-10 stsp read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
487 d5c81d44 2021-07-08 stsp int outfd, struct got_object_id *id, struct got_repository *repo,
488 d5c81d44 2021-07-08 stsp int obj_fd)
490 59d1e4a0 2021-03-10 stsp const struct got_error *err;
492 59d1e4a0 2021-03-10 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
493 d5c81d44 2021-07-08 stsp return request_raw_object(outbuf, size, hdrlen, outfd, id,
494 d5c81d44 2021-07-08 stsp repo, obj_fd);
496 59d1e4a0 2021-03-10 stsp err = start_read_object_child(repo);
498 59d1e4a0 2021-03-10 stsp return err;
500 d5c81d44 2021-07-08 stsp return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
504 4558fcd4 2018-01-14 stsp const struct got_error *
505 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
506 4558fcd4 2018-01-14 stsp struct got_object_id *id)
508 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
511 7bb0daa1 2018-06-21 stsp *obj = got_repo_get_cached_object(repo, id);
512 7bb0daa1 2018-06-21 stsp if (*obj != NULL) {
513 7bb0daa1 2018-06-21 stsp (*obj)->refcnt++;
514 7bb0daa1 2018-06-21 stsp return NULL;
517 b3d68e7f 2021-07-03 stsp err = got_object_open_packed(obj, id, repo);
518 e82b1d81 2019-07-27 stsp if (err && err->code != GOT_ERR_NO_OBJ)
519 e82b1d81 2019-07-27 stsp return err;
520 e82b1d81 2019-07-27 stsp if (*obj) {
521 e82b1d81 2019-07-27 stsp (*obj)->refcnt++;
522 e82b1d81 2019-07-27 stsp return got_repo_cache_object(repo, id, *obj);
525 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
527 762d73f4 2021-04-10 stsp if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
528 762d73f4 2021-04-10 stsp err = got_error_no_obj(id);
529 762d73f4 2021-04-10 stsp return err;
532 d5c81d44 2021-07-08 stsp err = got_object_read_header_privsep(obj, id, repo, fd);
534 4558fcd4 2018-01-14 stsp return err;
536 762d73f4 2021-04-10 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
538 59790a32 2018-09-15 stsp (*obj)->refcnt++;
539 762d73f4 2021-04-10 stsp return got_repo_cache_object(repo, id, *obj);
542 8ab9215c 2021-10-15 thomas /* *outfd must be initialized to -1 by caller */
543 59d1e4a0 2021-03-10 stsp const struct got_error *
544 8ab9215c 2021-10-15 thomas got_object_raw_open(struct got_raw_object **obj, int *outfd,
545 f8bb1d3e 2021-10-17 thomas struct got_repository *repo, struct got_object_id *id)
547 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
548 59d1e4a0 2021-03-10 stsp struct got_packidx *packidx = NULL;
550 59d1e4a0 2021-03-10 stsp uint8_t *outbuf = NULL;
551 59d1e4a0 2021-03-10 stsp off_t size = 0;
552 59d1e4a0 2021-03-10 stsp size_t hdrlen = 0;
553 59d1e4a0 2021-03-10 stsp char *path_packfile = NULL;
555 8ab9215c 2021-10-15 thomas *obj = got_repo_get_cached_raw_object(repo, id);
556 8ab9215c 2021-10-15 thomas if (*obj != NULL) {
557 8ab9215c 2021-10-15 thomas (*obj)->refcnt++;
558 8ab9215c 2021-10-15 thomas return NULL;
561 8ab9215c 2021-10-15 thomas if (*outfd == -1) {
562 8ab9215c 2021-10-15 thomas *outfd = got_opentempfd();
563 8ab9215c 2021-10-15 thomas if (*outfd == -1)
564 8ab9215c 2021-10-15 thomas return got_error_from_errno("got_opentempfd");
567 59d1e4a0 2021-03-10 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
568 59d1e4a0 2021-03-10 stsp if (err == NULL) {
569 59d1e4a0 2021-03-10 stsp struct got_pack *pack = NULL;
571 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
572 aea75d87 2021-07-06 stsp packidx->path_packidx);
576 59d1e4a0 2021-03-10 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
577 59d1e4a0 2021-03-10 stsp if (pack == NULL) {
578 59d1e4a0 2021-03-10 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
583 59d1e4a0 2021-03-10 stsp err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
584 8ab9215c 2021-10-15 thomas *outfd, pack, packidx, idx, id);
586 dd3af45a 2021-10-15 thomas goto done;
587 59d1e4a0 2021-03-10 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
590 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
593 8ab9215c 2021-10-15 thomas err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
594 d5c81d44 2021-07-08 stsp id, repo, fd);
596 dd3af45a 2021-10-15 thomas goto done;
599 59d1e4a0 2021-03-10 stsp *obj = calloc(1, sizeof(**obj));
600 59d1e4a0 2021-03-10 stsp if (*obj == NULL) {
601 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("calloc");
604 2b0ae357 2022-01-10 thomas (*obj)->fd = -1;
606 59d1e4a0 2021-03-10 stsp if (outbuf) {
607 59d1e4a0 2021-03-10 stsp (*obj)->data = outbuf;
609 59d1e4a0 2021-03-10 stsp struct stat sb;
610 8ab9215c 2021-10-15 thomas if (fstat(*outfd, &sb) == -1) {
611 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fstat");
615 a8591711 2021-06-18 stsp if (sb.st_size != hdrlen + size) {
616 59d1e4a0 2021-03-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
619 2b0ae357 2022-01-10 thomas #ifndef GOT_PACK_NO_MMAP
620 2b0ae357 2022-01-10 thomas if (hdrlen + size > 0) {
621 2b0ae357 2022-01-10 thomas (*obj)->data = mmap(NULL, hdrlen + size, PROT_READ,
622 2b0ae357 2022-01-10 thomas MAP_PRIVATE, *outfd, 0);
623 2b0ae357 2022-01-10 thomas if ((*obj)->data == MAP_FAILED) {
624 2b0ae357 2022-01-10 thomas if (errno != ENOMEM) {
625 2b0ae357 2022-01-10 thomas err = got_error_from_errno("mmap");
626 2b0ae357 2022-01-10 thomas goto done;
628 2b0ae357 2022-01-10 thomas (*obj)->data = NULL;
630 2b0ae357 2022-01-10 thomas (*obj)->fd = *outfd;
631 2b0ae357 2022-01-10 thomas *outfd = -1;
635 2b0ae357 2022-01-10 thomas if (*outfd != -1) {
636 2b0ae357 2022-01-10 thomas (*obj)->f = fdopen(*outfd, "r");
637 2b0ae357 2022-01-10 thomas if ((*obj)->f == NULL) {
638 2b0ae357 2022-01-10 thomas err = got_error_from_errno("fdopen");
639 2b0ae357 2022-01-10 thomas goto done;
641 2b0ae357 2022-01-10 thomas *outfd = -1;
644 59d1e4a0 2021-03-10 stsp (*obj)->hdrlen = hdrlen;
645 59d1e4a0 2021-03-10 stsp (*obj)->size = size;
646 8ab9215c 2021-10-15 thomas err = got_repo_cache_raw_object(repo, id, *obj);
648 59d1e4a0 2021-03-10 stsp free(path_packfile);
650 59d1e4a0 2021-03-10 stsp if (*obj) {
651 59d1e4a0 2021-03-10 stsp got_object_raw_close(*obj);
652 59d1e4a0 2021-03-10 stsp *obj = NULL;
654 59d1e4a0 2021-03-10 stsp free(outbuf);
656 8ab9215c 2021-10-15 thomas (*obj)->refcnt++;
657 59d1e4a0 2021-03-10 stsp return err;
660 59d1e4a0 2021-03-10 stsp const struct got_error *
661 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
662 6dfa2fd3 2018-02-12 stsp const char *id_str)
664 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
666 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
667 6dd1ece6 2019-11-10 stsp return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
669 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
672 15a94983 2018-12-23 stsp const struct got_error *
673 15a94983 2018-12-23 stsp got_object_resolve_id_str(struct got_object_id **id,
674 15a94983 2018-12-23 stsp struct got_repository *repo, const char *id_str)
676 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
677 15a94983 2018-12-23 stsp struct got_object *obj;
679 15a94983 2018-12-23 stsp err = got_object_open_by_id_str(&obj, repo, id_str);
681 15a94983 2018-12-23 stsp return err;
683 15a94983 2018-12-23 stsp *id = got_object_id_dup(got_object_get_id(obj));
684 15a94983 2018-12-23 stsp got_object_close(obj);
685 15a94983 2018-12-23 stsp if (*id == NULL)
686 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
688 15a94983 2018-12-23 stsp return NULL;
691 434025f3 2018-09-16 stsp static const struct got_error *
692 a158c901 2018-12-23 stsp request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
693 a158c901 2018-12-23 stsp int pack_idx, struct got_object_id *id)
695 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
697 a158c901 2018-12-23 stsp err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
700 a158c901 2018-12-23 stsp return err;
702 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
704 ca6e02ac 2020-01-07 stsp return err;
706 ca6e02ac 2020-01-07 stsp (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
707 ca6e02ac 2020-01-07 stsp return NULL;
710 a158c901 2018-12-23 stsp static const struct got_error *
711 a158c901 2018-12-23 stsp read_packed_commit_privsep(struct got_commit_object **commit,
712 a158c901 2018-12-23 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
713 a158c901 2018-12-23 stsp struct got_object_id *id)
715 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
717 a158c901 2018-12-23 stsp if (pack->privsep_child)
718 a158c901 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
720 68036464 2022-06-26 thomas err = got_pack_start_privsep_child(pack, packidx);
722 a158c901 2018-12-23 stsp return err;
724 711fb6e8 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
727 9f2369b0 2018-12-24 stsp static const struct got_error *
728 9f2369b0 2018-12-24 stsp request_commit(struct got_commit_object **commit, struct got_repository *repo,
729 d5c81d44 2021-07-08 stsp int fd, struct got_object_id *id)
731 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
732 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
734 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
736 d5c81d44 2021-07-08 stsp err = got_privsep_send_commit_req(ibuf, fd, id, -1);
738 9f2369b0 2018-12-24 stsp return err;
740 9f2369b0 2018-12-24 stsp return got_privsep_recv_commit(commit, ibuf);
743 9f2369b0 2018-12-24 stsp static const struct got_error *
744 9f2369b0 2018-12-24 stsp read_commit_privsep(struct got_commit_object **commit, int obj_fd,
745 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo)
747 ddc7b220 2019-09-08 stsp const struct got_error *err;
748 9f2369b0 2018-12-24 stsp int imsg_fds[2];
750 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
752 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
753 d5c81d44 2021-07-08 stsp return request_commit(commit, repo, obj_fd, id);
755 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
756 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
757 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
759 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
760 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
761 ddc7b220 2019-09-08 stsp free(ibuf);
762 ddc7b220 2019-09-08 stsp return err;
765 9f2369b0 2018-12-24 stsp pid = fork();
766 ddc7b220 2019-09-08 stsp if (pid == -1) {
767 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
768 ddc7b220 2019-09-08 stsp free(ibuf);
769 ddc7b220 2019-09-08 stsp return err;
771 9f2369b0 2018-12-24 stsp else if (pid == 0) {
772 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
773 9f2369b0 2018-12-24 stsp repo->path);
774 9f2369b0 2018-12-24 stsp /* not reached */
777 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
778 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
779 ddc7b220 2019-09-08 stsp free(ibuf);
780 ddc7b220 2019-09-08 stsp return err;
782 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
783 9f2369b0 2018-12-24 stsp imsg_fds[0];
784 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
785 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
786 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
788 d5c81d44 2021-07-08 stsp return request_commit(commit, repo, obj_fd, id);
792 a158c901 2018-12-23 stsp static const struct got_error *
793 434025f3 2018-09-16 stsp open_commit(struct got_commit_object **commit,
794 1785f84a 2018-12-23 stsp struct got_repository *repo, struct got_object_id *id, int check_cache)
796 434025f3 2018-09-16 stsp const struct got_error *err = NULL;
797 1785f84a 2018-12-23 stsp struct got_packidx *packidx = NULL;
799 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
801 434025f3 2018-09-16 stsp if (check_cache) {
802 1785f84a 2018-12-23 stsp *commit = got_repo_get_cached_commit(repo, id);
803 434025f3 2018-09-16 stsp if (*commit != NULL) {
804 434025f3 2018-09-16 stsp (*commit)->refcnt++;
805 434025f3 2018-09-16 stsp return NULL;
808 434025f3 2018-09-16 stsp *commit = NULL;
810 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
811 e82b1d81 2019-07-27 stsp if (err == NULL) {
812 1785f84a 2018-12-23 stsp struct got_pack *pack = NULL;
814 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
815 aea75d87 2021-07-06 stsp packidx->path_packidx);
817 1785f84a 2018-12-23 stsp return err;
819 1785f84a 2018-12-23 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
820 434025f3 2018-09-16 stsp if (pack == NULL) {
821 1785f84a 2018-12-23 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
826 a158c901 2018-12-23 stsp err = read_packed_commit_privsep(commit, pack,
827 1785f84a 2018-12-23 stsp packidx, idx, id);
828 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
831 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
833 e82b1d81 2019-07-27 stsp return err;
834 d5c81d44 2021-07-08 stsp err = read_commit_privsep(commit, fd, id, repo);
837 434025f3 2018-09-16 stsp if (err == NULL) {
838 434025f3 2018-09-16 stsp (*commit)->refcnt++;
839 1785f84a 2018-12-23 stsp err = got_repo_cache_commit(repo, id, *commit);
842 8d2c5ea3 2019-08-13 stsp free(path_packfile);
843 e32baab7 2018-11-05 stsp return err;
846 e32baab7 2018-11-05 stsp const struct got_error *
847 e32baab7 2018-11-05 stsp got_object_open_as_commit(struct got_commit_object **commit,
848 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object_id *id)
850 e32baab7 2018-11-05 stsp *commit = got_repo_get_cached_commit(repo, id);
851 e32baab7 2018-11-05 stsp if (*commit != NULL) {
852 e32baab7 2018-11-05 stsp (*commit)->refcnt++;
853 e32baab7 2018-11-05 stsp return NULL;
856 1785f84a 2018-12-23 stsp return open_commit(commit, repo, id, 0);
859 e32baab7 2018-11-05 stsp const struct got_error *
860 e32baab7 2018-11-05 stsp got_object_commit_open(struct got_commit_object **commit,
861 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object *obj)
863 1785f84a 2018-12-23 stsp return open_commit(commit, repo, got_object_get_id(obj), 1);
866 434025f3 2018-09-16 stsp const struct got_error *
867 dbc6a6b6 2018-07-12 stsp got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
869 dbc6a6b6 2018-07-12 stsp *qid = calloc(1, sizeof(**qid));
870 dbc6a6b6 2018-07-12 stsp if (*qid == NULL)
871 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
873 ec242592 2022-04-22 thomas memcpy(&(*qid)->id, id, sizeof((*qid)->id));
874 9ca9aafb 2021-06-18 stsp return NULL;
877 9ca9aafb 2021-06-18 stsp const struct got_error *
878 9ca9aafb 2021-06-18 stsp got_object_id_queue_copy(const struct got_object_id_queue *src,
879 9ca9aafb 2021-06-18 stsp struct got_object_id_queue *dest)
881 9ca9aafb 2021-06-18 stsp const struct got_error *err;
882 9ca9aafb 2021-06-18 stsp struct got_object_qid *qid;
884 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, src, entry) {
885 9ca9aafb 2021-06-18 stsp struct got_object_qid *new;
887 9ca9aafb 2021-06-18 stsp * Deep-copy the object ID only. Let the caller deal
888 9ca9aafb 2021-06-18 stsp * with setting up the new->data pointer if needed.
890 b6b86fd1 2022-08-30 thomas err = got_object_qid_alloc(&new, &qid->id);
892 9ca9aafb 2021-06-18 stsp got_object_id_queue_free(dest);
893 9ca9aafb 2021-06-18 stsp return err;
895 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(dest, new, entry);
898 dbc6a6b6 2018-07-12 stsp return NULL;
901 a158c901 2018-12-23 stsp static const struct got_error *
902 13c729f7 2018-12-24 stsp request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
903 13c729f7 2018-12-24 stsp int pack_idx, struct got_object_id *id)
905 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
907 13c729f7 2018-12-24 stsp err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
910 a158c901 2018-12-23 stsp return err;
912 a158c901 2018-12-23 stsp return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
915 71eb0e7f 2018-09-16 stsp static const struct got_error *
916 13c729f7 2018-12-24 stsp read_packed_tree_privsep(struct got_tree_object **tree,
917 13c729f7 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
918 13c729f7 2018-12-24 stsp struct got_object_id *id)
920 13c729f7 2018-12-24 stsp const struct got_error *err = NULL;
922 13c729f7 2018-12-24 stsp if (pack->privsep_child)
923 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
925 68036464 2022-06-26 thomas err = got_pack_start_privsep_child(pack, packidx);
927 13c729f7 2018-12-24 stsp return err;
929 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
932 13c729f7 2018-12-24 stsp static const struct got_error *
933 9f2369b0 2018-12-24 stsp request_tree(struct got_tree_object **tree, struct got_repository *repo,
934 d5c81d44 2021-07-08 stsp int fd, struct got_object_id *id)
936 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
937 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
939 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
941 d5c81d44 2021-07-08 stsp err = got_privsep_send_tree_req(ibuf, fd, id, -1);
943 9f2369b0 2018-12-24 stsp return err;
945 9f2369b0 2018-12-24 stsp return got_privsep_recv_tree(tree, ibuf);
948 ef20f542 2022-06-26 thomas static const struct got_error *
949 9f2369b0 2018-12-24 stsp read_tree_privsep(struct got_tree_object **tree, int obj_fd,
950 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo)
952 ddc7b220 2019-09-08 stsp const struct got_error *err;
953 9f2369b0 2018-12-24 stsp int imsg_fds[2];
955 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
957 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
958 d5c81d44 2021-07-08 stsp return request_tree(tree, repo, obj_fd, id);
960 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
961 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
962 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
964 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
965 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
966 ddc7b220 2019-09-08 stsp free(ibuf);
967 ddc7b220 2019-09-08 stsp return err;
970 9f2369b0 2018-12-24 stsp pid = fork();
971 ddc7b220 2019-09-08 stsp if (pid == -1) {
972 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
973 ddc7b220 2019-09-08 stsp free(ibuf);
974 ddc7b220 2019-09-08 stsp return err;
976 9f2369b0 2018-12-24 stsp else if (pid == 0) {
977 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
978 9f2369b0 2018-12-24 stsp repo->path);
979 9f2369b0 2018-12-24 stsp /* not reached */
982 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
983 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
984 ddc7b220 2019-09-08 stsp free(ibuf);
985 ddc7b220 2019-09-08 stsp return err;
987 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
988 9f2369b0 2018-12-24 stsp imsg_fds[0];
989 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
990 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
991 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
994 d5c81d44 2021-07-08 stsp return request_tree(tree, repo, obj_fd, id);
997 9f2369b0 2018-12-24 stsp static const struct got_error *
998 13c729f7 2018-12-24 stsp open_tree(struct got_tree_object **tree, struct got_repository *repo,
999 13c729f7 2018-12-24 stsp struct got_object_id *id, int check_cache)
1001 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
1002 13c729f7 2018-12-24 stsp struct got_packidx *packidx = NULL;
1004 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1006 71eb0e7f 2018-09-16 stsp if (check_cache) {
1007 13c729f7 2018-12-24 stsp *tree = got_repo_get_cached_tree(repo, id);
1008 71eb0e7f 2018-09-16 stsp if (*tree != NULL) {
1009 71eb0e7f 2018-09-16 stsp (*tree)->refcnt++;
1010 71eb0e7f 2018-09-16 stsp return NULL;
1013 71eb0e7f 2018-09-16 stsp *tree = NULL;
1015 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1016 e82b1d81 2019-07-27 stsp if (err == NULL) {
1017 13c729f7 2018-12-24 stsp struct got_pack *pack = NULL;
1019 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
1020 aea75d87 2021-07-06 stsp packidx->path_packidx);
1022 13c729f7 2018-12-24 stsp return err;
1024 13c729f7 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1025 e7885405 2018-09-10 stsp if (pack == NULL) {
1026 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1029 8d2c5ea3 2019-08-13 stsp goto done;
1031 13c729f7 2018-12-24 stsp err = read_packed_tree_privsep(tree, pack,
1032 13c729f7 2018-12-24 stsp packidx, idx, id);
1033 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1036 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1038 e82b1d81 2019-07-27 stsp return err;
1039 d5c81d44 2021-07-08 stsp err = read_tree_privsep(tree, fd, id, repo);
1042 f6be5c30 2018-06-22 stsp if (err == NULL) {
1043 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
1044 13c729f7 2018-12-24 stsp err = got_repo_cache_tree(repo, id, *tree);
1047 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1048 776d4d29 2018-06-17 stsp return err;
1051 776d4d29 2018-06-17 stsp const struct got_error *
1052 776d4d29 2018-06-17 stsp got_object_open_as_tree(struct got_tree_object **tree,
1053 776d4d29 2018-06-17 stsp struct got_repository *repo, struct got_object_id *id)
1055 e8eb494a 2018-09-16 stsp *tree = got_repo_get_cached_tree(repo, id);
1056 e8eb494a 2018-09-16 stsp if (*tree != NULL) {
1057 e8eb494a 2018-09-16 stsp (*tree)->refcnt++;
1058 e8eb494a 2018-09-16 stsp return NULL;
1061 13c729f7 2018-12-24 stsp return open_tree(tree, repo, id, 0);
1064 71eb0e7f 2018-09-16 stsp const struct got_error *
1065 71eb0e7f 2018-09-16 stsp got_object_tree_open(struct got_tree_object **tree,
1066 71eb0e7f 2018-09-16 stsp struct got_repository *repo, struct got_object *obj)
1068 13c729f7 2018-12-24 stsp return open_tree(tree, repo, got_object_get_id(obj), 1);
1072 56e0773d 2019-11-28 stsp got_object_tree_get_nentries(struct got_tree_object *tree)
1074 56e0773d 2019-11-28 stsp return tree->nentries;
1077 56e0773d 2019-11-28 stsp struct got_tree_entry *
1078 56e0773d 2019-11-28 stsp got_object_tree_get_first_entry(struct got_tree_object *tree)
1080 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, 0);
1083 56e0773d 2019-11-28 stsp struct got_tree_entry *
1084 56e0773d 2019-11-28 stsp got_object_tree_get_last_entry(struct got_tree_object *tree)
1086 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, tree->nentries - 1);
1089 56e0773d 2019-11-28 stsp struct got_tree_entry *
1090 56e0773d 2019-11-28 stsp got_object_tree_get_entry(struct got_tree_object *tree, int i)
1092 56e0773d 2019-11-28 stsp if (i < 0 || i >= tree->nentries)
1093 56e0773d 2019-11-28 stsp return NULL;
1094 56e0773d 2019-11-28 stsp return &tree->entries[i];
1098 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(struct got_tree_entry *te)
1100 56e0773d 2019-11-28 stsp return te->mode;
1103 56e0773d 2019-11-28 stsp const char *
1104 56e0773d 2019-11-28 stsp got_tree_entry_get_name(struct got_tree_entry *te)
1106 56e0773d 2019-11-28 stsp return &te->name[0];
1109 56e0773d 2019-11-28 stsp struct got_object_id *
1110 56e0773d 2019-11-28 stsp got_tree_entry_get_id(struct got_tree_entry *te)
1112 56e0773d 2019-11-28 stsp return &te->id;
1115 0d6c6ee3 2020-05-20 stsp const struct got_error *
1116 af57b12a 2020-07-23 stsp got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1118 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
1119 32596e16 2020-07-23 stsp size_t len, totlen, hdrlen, offset;
1121 aa092692 2020-07-23 stsp *s = NULL;
1123 659dc16e 2020-07-23 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1124 659dc16e 2020-07-23 stsp totlen = 0;
1125 32596e16 2020-07-23 stsp offset = 0;
1129 659dc16e 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1131 af57b12a 2020-07-23 stsp return err;
1133 659dc16e 2020-07-23 stsp if (len == 0)
1136 659dc16e 2020-07-23 stsp totlen += len - hdrlen;
1137 af57b12a 2020-07-23 stsp p = realloc(*s, totlen + 1);
1138 659dc16e 2020-07-23 stsp if (p == NULL) {
1139 659dc16e 2020-07-23 stsp err = got_error_from_errno("realloc");
1141 af57b12a 2020-07-23 stsp *s = NULL;
1142 af57b12a 2020-07-23 stsp return err;
1145 659dc16e 2020-07-23 stsp /* Skip blob object header first time around. */
1146 af57b12a 2020-07-23 stsp memcpy(*s + offset,
1147 f8f7c882 2020-07-23 stsp got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1148 659dc16e 2020-07-23 stsp hdrlen = 0;
1149 32596e16 2020-07-23 stsp offset = totlen;
1150 659dc16e 2020-07-23 stsp } while (len > 0);
1152 af57b12a 2020-07-23 stsp (*s)[totlen] = '\0';
1153 af57b12a 2020-07-23 stsp return NULL;
1156 af57b12a 2020-07-23 stsp const struct got_error *
1157 af57b12a 2020-07-23 stsp got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1158 af57b12a 2020-07-23 stsp struct got_repository *repo)
1160 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
1161 af57b12a 2020-07-23 stsp struct got_blob_object *blob = NULL;
1162 f4ae6ddb 2022-07-01 thomas int fd = -1;
1164 af57b12a 2020-07-23 stsp *link_target = NULL;
1166 af57b12a 2020-07-23 stsp if (!got_object_tree_entry_is_symlink(te))
1167 af57b12a 2020-07-23 stsp return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1169 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
1170 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
1171 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1172 f4ae6ddb 2022-07-01 thomas goto done;
1175 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo,
1176 f4ae6ddb 2022-07-01 thomas got_tree_entry_get_id(te), PATH_MAX, fd);
1178 f4ae6ddb 2022-07-01 thomas goto done;
1180 af57b12a 2020-07-23 stsp err = got_object_blob_read_to_str(link_target, blob);
1182 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
1183 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
1184 f4ae6ddb 2022-07-01 thomas if (blob)
1185 f4ae6ddb 2022-07-01 thomas got_object_blob_close(blob);
1186 659dc16e 2020-07-23 stsp if (err) {
1187 659dc16e 2020-07-23 stsp free(*link_target);
1188 659dc16e 2020-07-23 stsp *link_target = NULL;
1190 0d6c6ee3 2020-05-20 stsp return err;
1194 56e0773d 2019-11-28 stsp got_tree_entry_get_index(struct got_tree_entry *te)
1196 56e0773d 2019-11-28 stsp return te->idx;
1199 56e0773d 2019-11-28 stsp struct got_tree_entry *
1200 56e0773d 2019-11-28 stsp got_tree_entry_get_next(struct got_tree_object *tree,
1201 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
1203 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx + 1);
1206 56e0773d 2019-11-28 stsp struct got_tree_entry *
1207 56e0773d 2019-11-28 stsp got_tree_entry_get_prev(struct got_tree_object *tree,
1208 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
1210 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx - 1);
1213 3840f4c9 2018-09-12 stsp static const struct got_error *
1214 ac544f8c 2019-01-13 stsp request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1215 ebc55e2d 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1216 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
1218 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
1219 bc1f382f 2022-01-05 thomas struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1220 bc1f382f 2022-01-05 thomas int outfd_child;
1222 bc1f382f 2022-01-05 thomas err = pack_child_send_tempfiles(ibuf, pack);
1223 bc1f382f 2022-01-05 thomas if (err)
1224 bc1f382f 2022-01-05 thomas return err;
1226 3840f4c9 2018-09-12 stsp outfd_child = dup(outfd);
1227 3840f4c9 2018-09-12 stsp if (outfd_child == -1)
1228 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1230 ebc55e2d 2018-12-24 stsp err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1232 3840f4c9 2018-09-12 stsp return err;
1234 3840f4c9 2018-09-12 stsp err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1235 3840f4c9 2018-09-12 stsp outfd_child);
1236 3840f4c9 2018-09-12 stsp if (err) {
1237 3840f4c9 2018-09-12 stsp return err;
1240 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen,
1241 ebc55e2d 2018-12-24 stsp pack->privsep_child->ibuf);
1243 3840f4c9 2018-09-12 stsp return err;
1245 3840f4c9 2018-09-12 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1246 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1248 3840f4c9 2018-09-12 stsp return err;
1251 ebc55e2d 2018-12-24 stsp static const struct got_error *
1252 ac544f8c 2019-01-13 stsp read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1253 ac544f8c 2019-01-13 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1254 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
1256 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
1258 ebc55e2d 2018-12-24 stsp if (pack->privsep_child == NULL) {
1259 68036464 2022-06-26 thomas err = got_pack_start_privsep_child(pack, packidx);
1261 ebc55e2d 2018-12-24 stsp return err;
1264 ac544f8c 2019-01-13 stsp return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1268 9f2369b0 2018-12-24 stsp static const struct got_error *
1269 ac544f8c 2019-01-13 stsp request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1270 d5c81d44 2021-07-08 stsp int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1272 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1273 9f2369b0 2018-12-24 stsp int outfd_child;
1275 9f2369b0 2018-12-24 stsp outfd_child = dup(outfd);
1276 9f2369b0 2018-12-24 stsp if (outfd_child == -1)
1277 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1279 d5c81d44 2021-07-08 stsp err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1281 9f2369b0 2018-12-24 stsp return err;
1283 9f2369b0 2018-12-24 stsp err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1285 9f2369b0 2018-12-24 stsp return err;
1287 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1289 9f2369b0 2018-12-24 stsp return err;
1291 9f2369b0 2018-12-24 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1292 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
1294 9f2369b0 2018-12-24 stsp return err;
1297 9f2369b0 2018-12-24 stsp static const struct got_error *
1298 ac544f8c 2019-01-13 stsp read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1299 d5c81d44 2021-07-08 stsp int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1301 ddc7b220 2019-09-08 stsp const struct got_error *err;
1302 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1303 9f2369b0 2018-12-24 stsp pid_t pid;
1304 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1306 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1307 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1308 d5c81d44 2021-07-08 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1312 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1313 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1314 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1316 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1317 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1318 ddc7b220 2019-09-08 stsp free(ibuf);
1319 ddc7b220 2019-09-08 stsp return err;
1322 9f2369b0 2018-12-24 stsp pid = fork();
1323 ddc7b220 2019-09-08 stsp if (pid == -1) {
1324 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1325 ddc7b220 2019-09-08 stsp free(ibuf);
1326 ddc7b220 2019-09-08 stsp return err;
1328 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1329 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1330 9f2369b0 2018-12-24 stsp repo->path);
1331 9f2369b0 2018-12-24 stsp /* not reached */
1334 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
1335 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1336 ddc7b220 2019-09-08 stsp free(ibuf);
1337 ddc7b220 2019-09-08 stsp return err;
1339 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1340 9f2369b0 2018-12-24 stsp imsg_fds[0];
1341 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1342 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1343 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1345 d5c81d44 2021-07-08 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1348 ebc55e2d 2018-12-24 stsp static const struct got_error *
1349 ebc55e2d 2018-12-24 stsp open_blob(struct got_blob_object **blob, struct got_repository *repo,
1350 f4ae6ddb 2022-07-01 thomas struct got_object_id *id, size_t blocksize, int outfd)
1352 ebc55e2d 2018-12-24 stsp const struct got_error *err = NULL;
1353 ebc55e2d 2018-12-24 stsp struct got_packidx *packidx = NULL;
1354 f9bba04a 2022-07-01 thomas int idx, dfd = -1;
1355 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1356 ac544f8c 2019-01-13 stsp uint8_t *outbuf;
1357 ebc55e2d 2018-12-24 stsp size_t size, hdrlen;
1358 ebc55e2d 2018-12-24 stsp struct stat sb;
1360 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
1361 4558fcd4 2018-01-14 stsp if (*blob == NULL)
1362 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1364 062ebb78 2018-07-12 stsp (*blob)->read_buf = malloc(blocksize);
1365 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
1366 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1367 c7254d79 2018-04-24 stsp goto done;
1370 19a6a6b5 2022-07-01 thomas if (ftruncate(outfd, 0L) == -1) {
1371 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("ftruncate");
1372 19a6a6b5 2022-07-01 thomas goto done;
1374 19a6a6b5 2022-07-01 thomas if (lseek(outfd, SEEK_SET, 0) == -1) {
1375 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("lseek");
1376 19a6a6b5 2022-07-01 thomas goto done;
1379 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1380 e82b1d81 2019-07-27 stsp if (err == NULL) {
1381 ebc55e2d 2018-12-24 stsp struct got_pack *pack = NULL;
1383 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
1384 aea75d87 2021-07-06 stsp packidx->path_packidx);
1386 ebc55e2d 2018-12-24 stsp goto done;
1388 ebc55e2d 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1389 55da3778 2018-09-10 stsp if (pack == NULL) {
1390 ebc55e2d 2018-12-24 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1393 55da3778 2018-09-10 stsp goto done;
1395 ac544f8c 2019-01-13 stsp err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1396 ac544f8c 2019-01-13 stsp pack, packidx, idx, id);
1397 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1400 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&infd, id, repo);
1402 e82b1d81 2019-07-27 stsp goto done;
1403 ac544f8c 2019-01-13 stsp err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1404 d5c81d44 2021-07-08 stsp id, repo);
1407 55da3778 2018-09-10 stsp goto done;
1409 de060dff 2018-12-24 stsp if (hdrlen > size) {
1410 ebc55e2d 2018-12-24 stsp err = got_error(GOT_ERR_BAD_OBJ_HDR);
1411 ebc55e2d 2018-12-24 stsp goto done;
1414 ac544f8c 2019-01-13 stsp if (outbuf) {
1415 ac544f8c 2019-01-13 stsp (*blob)->f = fmemopen(outbuf, size, "rb");
1416 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1417 638f9024 2019-05-13 stsp err = got_error_from_errno("fmemopen");
1418 ac544f8c 2019-01-13 stsp free(outbuf);
1419 ac544f8c 2019-01-13 stsp goto done;
1421 ac544f8c 2019-01-13 stsp (*blob)->data = outbuf;
1423 ac544f8c 2019-01-13 stsp if (fstat(outfd, &sb) == -1) {
1424 638f9024 2019-05-13 stsp err = got_error_from_errno("fstat");
1425 ac544f8c 2019-01-13 stsp goto done;
1428 ac544f8c 2019-01-13 stsp if (sb.st_size != size) {
1429 ac544f8c 2019-01-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1430 f9bba04a 2022-07-01 thomas goto done;
1433 f9bba04a 2022-07-01 thomas dfd = dup(outfd);
1434 f9bba04a 2022-07-01 thomas if (dfd == -1) {
1435 f9bba04a 2022-07-01 thomas err = got_error_from_errno("dup");
1436 ac544f8c 2019-01-13 stsp goto done;
1439 acb9e3ea 2022-07-01 thomas (*blob)->f = fdopen(dfd, "rb");
1440 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1441 638f9024 2019-05-13 stsp err = got_error_from_errno("fdopen");
1442 f9bba04a 2022-07-01 thomas close(dfd);
1443 f9bba04a 2022-07-01 thomas dfd = -1;
1444 ac544f8c 2019-01-13 stsp goto done;
1448 ebc55e2d 2018-12-24 stsp (*blob)->hdrlen = hdrlen;
1449 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
1450 ebc55e2d 2018-12-24 stsp memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1453 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1454 55da3778 2018-09-10 stsp if (err) {
1455 55da3778 2018-09-10 stsp if (*blob) {
1456 7baf5860 2019-03-19 stsp got_object_blob_close(*blob);
1457 55da3778 2018-09-10 stsp *blob = NULL;
1460 a19581a2 2018-06-21 stsp return err;
1463 a19581a2 2018-06-21 stsp const struct got_error *
1464 a19581a2 2018-06-21 stsp got_object_open_as_blob(struct got_blob_object **blob,
1465 f4ae6ddb 2022-07-01 thomas struct got_repository *repo, struct got_object_id *id, size_t blocksize,
1466 f4ae6ddb 2022-07-01 thomas int outfd)
1468 f4ae6ddb 2022-07-01 thomas return open_blob(blob, repo, id, blocksize, outfd);
1471 ebc55e2d 2018-12-24 stsp const struct got_error *
1472 ebc55e2d 2018-12-24 stsp got_object_blob_open(struct got_blob_object **blob,
1473 f4ae6ddb 2022-07-01 thomas struct got_repository *repo, struct got_object *obj, size_t blocksize,
1474 f4ae6ddb 2022-07-01 thomas int outfd)
1476 f4ae6ddb 2022-07-01 thomas return open_blob(blob, repo, got_object_get_id(obj), blocksize, outfd);
1479 fb43ecf1 2019-02-11 stsp const struct got_error *
1480 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
1482 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL;
1483 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
1484 56b63ca4 2021-01-22 stsp if (blob->f && fclose(blob->f) == EOF)
1485 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1486 ac544f8c 2019-01-13 stsp free(blob->data);
1487 68482ea3 2017-11-27 stsp free(blob);
1488 fb43ecf1 2019-02-11 stsp return err;
1492 8ba819a3 2020-07-23 stsp got_object_blob_rewind(struct got_blob_object *blob)
1494 8ba819a3 2020-07-23 stsp if (blob->f)
1495 8ba819a3 2020-07-23 stsp rewind(blob->f);
1499 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1501 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1505 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
1507 f934cf2c 2018-02-12 stsp return blob->hdrlen;
1510 f934cf2c 2018-02-12 stsp const uint8_t *
1511 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
1513 f934cf2c 2018-02-12 stsp return blob->read_buf;
1516 68482ea3 2017-11-27 stsp const struct got_error *
1517 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1521 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1522 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
1523 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
1524 eb651edf 2018-02-11 stsp *outlenp = n;
1525 35e9ba5d 2018-06-21 stsp return NULL;
1528 35e9ba5d 2018-06-21 stsp const struct got_error *
1529 be659d10 2020-11-18 stsp got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1530 6c4c42e0 2019-06-24 stsp off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1532 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
1533 b6752625 2018-12-24 stsp size_t n, len, hdrlen;
1534 84451b3e 2018-07-10 stsp const uint8_t *buf;
1536 c33ebc60 2020-11-18 stsp const int alloc_chunksz = 512;
1537 c33ebc60 2020-11-18 stsp size_t nalloc = 0;
1538 f595d9bd 2019-08-14 stsp off_t off = 0, total_len = 0;
1540 6c4c42e0 2019-06-24 stsp if (line_offsets)
1541 6c4c42e0 2019-06-24 stsp *line_offsets = NULL;
1542 f595d9bd 2019-08-14 stsp if (filesize)
1543 f595d9bd 2019-08-14 stsp *filesize = 0;
1544 84451b3e 2018-07-10 stsp if (nlines)
1545 84451b3e 2018-07-10 stsp *nlines = 0;
1547 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1549 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
1551 35e9ba5d 2018-06-21 stsp return err;
1552 35e9ba5d 2018-06-21 stsp if (len == 0)
1554 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
1555 b02560ec 2019-08-19 stsp i = hdrlen;
1556 f1cbc3bc 2020-11-18 stsp if (nlines) {
1557 f1cbc3bc 2020-11-18 stsp if (line_offsets && *line_offsets == NULL) {
1558 78695fb7 2019-08-12 stsp /* Have some data but perhaps no '\n'. */
1559 78695fb7 2019-08-12 stsp *nlines = 1;
1560 c33ebc60 2020-11-18 stsp nalloc = alloc_chunksz;
1561 c33ebc60 2020-11-18 stsp *line_offsets = calloc(nalloc,
1562 c33ebc60 2020-11-18 stsp sizeof(**line_offsets));
1563 78695fb7 2019-08-12 stsp if (*line_offsets == NULL)
1564 845785d4 2020-02-02 tracey return got_error_from_errno("calloc");
1566 b02560ec 2019-08-19 stsp /* Skip forward over end of first line. */
1567 b02560ec 2019-08-19 stsp while (i < len) {
1568 b02560ec 2019-08-19 stsp if (buf[i] == '\n')
1573 b02560ec 2019-08-19 stsp /* Scan '\n' offsets in remaining chunk of data. */
1574 b02560ec 2019-08-19 stsp while (i < len) {
1575 b02560ec 2019-08-19 stsp if (buf[i] != '\n') {
1579 f595d9bd 2019-08-14 stsp (*nlines)++;
1580 c33ebc60 2020-11-18 stsp if (line_offsets && nalloc < *nlines) {
1581 c33ebc60 2020-11-18 stsp size_t n = *nlines + alloc_chunksz;
1582 78695fb7 2019-08-12 stsp off_t *o = recallocarray(*line_offsets,
1583 c33ebc60 2020-11-18 stsp nalloc, n, sizeof(**line_offsets));
1584 78695fb7 2019-08-12 stsp if (o == NULL) {
1585 78695fb7 2019-08-12 stsp free(*line_offsets);
1586 78695fb7 2019-08-12 stsp *line_offsets = NULL;
1587 78695fb7 2019-08-12 stsp return got_error_from_errno(
1588 78695fb7 2019-08-12 stsp "recallocarray");
1590 78695fb7 2019-08-12 stsp *line_offsets = o;
1591 c33ebc60 2020-11-18 stsp nalloc = n;
1593 f1cbc3bc 2020-11-18 stsp if (line_offsets) {
1594 f1cbc3bc 2020-11-18 stsp off = total_len + i - hdrlen + 1;
1595 f1cbc3bc 2020-11-18 stsp (*line_offsets)[*nlines - 1] = off;
1600 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
1601 454a6b59 2018-12-24 stsp n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1602 b6752625 2018-12-24 stsp if (n != len - hdrlen)
1603 b6752625 2018-12-24 stsp return got_ferror(outfile, GOT_ERR_IO);
1604 f595d9bd 2019-08-14 stsp total_len += len - hdrlen;
1605 35e9ba5d 2018-06-21 stsp hdrlen = 0;
1606 35e9ba5d 2018-06-21 stsp } while (len != 0);
1608 cbe7f848 2019-02-11 stsp if (fflush(outfile) != 0)
1609 638f9024 2019-05-13 stsp return got_error_from_errno("fflush");
1610 35e9ba5d 2018-06-21 stsp rewind(outfile);
1612 f595d9bd 2019-08-14 stsp if (filesize)
1613 f595d9bd 2019-08-14 stsp *filesize = total_len;
1615 776d4d29 2018-06-17 stsp return NULL;
1618 f4a881ce 2018-11-17 stsp static const struct got_error *
1619 268f7291 2018-12-24 stsp request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1620 268f7291 2018-12-24 stsp int pack_idx, struct got_object_id *id)
1622 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
1624 268f7291 2018-12-24 stsp err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1625 268f7291 2018-12-24 stsp pack_idx);
1627 a158c901 2018-12-23 stsp return err;
1629 a158c901 2018-12-23 stsp return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1632 268f7291 2018-12-24 stsp static const struct got_error *
1633 268f7291 2018-12-24 stsp read_packed_tag_privsep(struct got_tag_object **tag,
1634 268f7291 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1635 268f7291 2018-12-24 stsp struct got_object_id *id)
1637 268f7291 2018-12-24 stsp const struct got_error *err = NULL;
1639 268f7291 2018-12-24 stsp if (pack->privsep_child)
1640 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1642 68036464 2022-06-26 thomas err = got_pack_start_privsep_child(pack, packidx);
1644 268f7291 2018-12-24 stsp return err;
1646 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1649 9f2369b0 2018-12-24 stsp static const struct got_error *
1650 9f2369b0 2018-12-24 stsp request_tag(struct got_tag_object **tag, struct got_repository *repo,
1651 d5c81d44 2021-07-08 stsp int fd, struct got_object_id *id)
1653 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1654 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1656 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1658 d5c81d44 2021-07-08 stsp err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1660 9f2369b0 2018-12-24 stsp return err;
1662 9f2369b0 2018-12-24 stsp return got_privsep_recv_tag(tag, ibuf);
1665 9f2369b0 2018-12-24 stsp static const struct got_error *
1666 9f2369b0 2018-12-24 stsp read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1667 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo)
1669 ddc7b220 2019-09-08 stsp const struct got_error *err;
1670 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1671 9f2369b0 2018-12-24 stsp pid_t pid;
1672 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1674 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1675 d5c81d44 2021-07-08 stsp return request_tag(tag, repo, obj_fd, id);
1677 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1678 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1679 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1681 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1682 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1683 ddc7b220 2019-09-08 stsp free(ibuf);
1684 ddc7b220 2019-09-08 stsp return err;
1687 9f2369b0 2018-12-24 stsp pid = fork();
1688 ddc7b220 2019-09-08 stsp if (pid == -1) {
1689 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1690 ddc7b220 2019-09-08 stsp free(ibuf);
1691 ddc7b220 2019-09-08 stsp return err;
1693 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1694 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1695 9f2369b0 2018-12-24 stsp repo->path);
1696 9f2369b0 2018-12-24 stsp /* not reached */
1699 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
1700 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1701 ddc7b220 2019-09-08 stsp free(ibuf);
1702 ddc7b220 2019-09-08 stsp return err;
1704 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1705 9f2369b0 2018-12-24 stsp imsg_fds[0];
1706 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1707 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1708 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1710 d5c81d44 2021-07-08 stsp return request_tag(tag, repo, obj_fd, id);
1713 a158c901 2018-12-23 stsp static const struct got_error *
1714 268f7291 2018-12-24 stsp open_tag(struct got_tag_object **tag, struct got_repository *repo,
1715 268f7291 2018-12-24 stsp struct got_object_id *id, int check_cache)
1717 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1718 268f7291 2018-12-24 stsp struct got_packidx *packidx = NULL;
1720 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1721 5d844a1e 2019-08-13 stsp struct got_object *obj = NULL;
1722 5d844a1e 2019-08-13 stsp int obj_type = GOT_OBJ_TYPE_ANY;
1724 f4a881ce 2018-11-17 stsp if (check_cache) {
1725 268f7291 2018-12-24 stsp *tag = got_repo_get_cached_tag(repo, id);
1726 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1727 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1728 f4a881ce 2018-11-17 stsp return NULL;
1731 f4a881ce 2018-11-17 stsp *tag = NULL;
1733 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1734 e82b1d81 2019-07-27 stsp if (err == NULL) {
1735 268f7291 2018-12-24 stsp struct got_pack *pack = NULL;
1737 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
1738 aea75d87 2021-07-06 stsp packidx->path_packidx);
1740 268f7291 2018-12-24 stsp return err;
1742 268f7291 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1743 f4a881ce 2018-11-17 stsp if (pack == NULL) {
1744 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1747 8d2c5ea3 2019-08-13 stsp goto done;
1750 992eb9d8 2020-02-07 tracey /* Beware of "lightweight" tags: Check object type first. */
1751 5d844a1e 2019-08-13 stsp err = read_packed_object_privsep(&obj, repo, pack, packidx,
1754 5d844a1e 2019-08-13 stsp goto done;
1755 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1756 5d844a1e 2019-08-13 stsp got_object_close(obj);
1757 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG) {
1758 5d844a1e 2019-08-13 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1759 5d844a1e 2019-08-13 stsp goto done;
1761 5d844a1e 2019-08-13 stsp err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1762 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1765 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1767 e82b1d81 2019-07-27 stsp return err;
1768 d5c81d44 2021-07-08 stsp err = got_object_read_header_privsep(&obj, id, repo, fd);
1770 5d844a1e 2019-08-13 stsp return err;
1771 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1772 5d844a1e 2019-08-13 stsp got_object_close(obj);
1773 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
1774 5d844a1e 2019-08-13 stsp return got_error(GOT_ERR_OBJ_TYPE);
1776 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1778 5d844a1e 2019-08-13 stsp return err;
1779 d5c81d44 2021-07-08 stsp err = read_tag_privsep(tag, fd, id, repo);
1782 f4a881ce 2018-11-17 stsp if (err == NULL) {
1783 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1784 268f7291 2018-12-24 stsp err = got_repo_cache_tag(repo, id, *tag);
1787 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1788 f4a881ce 2018-11-17 stsp return err;
1791 f4a881ce 2018-11-17 stsp const struct got_error *
1792 f4a881ce 2018-11-17 stsp got_object_open_as_tag(struct got_tag_object **tag,
1793 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object_id *id)
1795 f4a881ce 2018-11-17 stsp *tag = got_repo_get_cached_tag(repo, id);
1796 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1797 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1798 f4a881ce 2018-11-17 stsp return NULL;
1801 268f7291 2018-12-24 stsp return open_tag(tag, repo, id, 0);
1804 f4a881ce 2018-11-17 stsp const struct got_error *
1805 f4a881ce 2018-11-17 stsp got_object_tag_open(struct got_tag_object **tag,
1806 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object *obj)
1808 268f7291 2018-12-24 stsp return open_tag(tag, repo, got_object_get_id(obj), 1);
1811 d24820bf 2019-08-11 stsp const char *
1812 d24820bf 2019-08-11 stsp got_object_tag_get_name(struct got_tag_object *tag)
1814 d24820bf 2019-08-11 stsp return tag->tag;
1818 0bd18d37 2019-02-01 stsp got_object_tag_get_object_type(struct got_tag_object *tag)
1820 0bd18d37 2019-02-01 stsp return tag->obj_type;
1823 0bd18d37 2019-02-01 stsp struct got_object_id *
1824 0bd18d37 2019-02-01 stsp got_object_tag_get_object_id(struct got_tag_object *tag)
1826 0bd18d37 2019-02-01 stsp return &tag->id;
1830 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_time(struct got_tag_object *tag)
1832 01073a5d 2019-08-22 stsp return tag->tagger_time;
1836 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1838 01073a5d 2019-08-22 stsp return tag->tagger_gmtoff;
1841 01073a5d 2019-08-22 stsp const char *
1842 01073a5d 2019-08-22 stsp got_object_tag_get_tagger(struct got_tag_object *tag)
1844 01073a5d 2019-08-22 stsp return tag->tagger;
1847 01073a5d 2019-08-22 stsp const char *
1848 01073a5d 2019-08-22 stsp got_object_tag_get_message(struct got_tag_object *tag)
1850 01073a5d 2019-08-22 stsp return tag->tagmsg;
1853 776d4d29 2018-06-17 stsp static struct got_tree_entry *
1854 65a9bbe9 2018-09-15 stsp find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1858 63da309a 2018-11-07 stsp /* Note that tree entries are sorted in strncmp() order. */
1859 56e0773d 2019-11-28 stsp for (i = 0; i < tree->nentries; i++) {
1860 56e0773d 2019-11-28 stsp struct got_tree_entry *te = &tree->entries[i];
1861 63da309a 2018-11-07 stsp int cmp = strncmp(te->name, name, len);
1862 63da309a 2018-11-07 stsp if (cmp < 0)
1864 63da309a 2018-11-07 stsp if (cmp > 0)
1866 63da309a 2018-11-07 stsp if (te->name[len] == '\0')
1867 776d4d29 2018-06-17 stsp return te;
1869 eb651edf 2018-02-11 stsp return NULL;
1872 56e0773d 2019-11-28 stsp struct got_tree_entry *
1873 a129376b 2019-03-28 stsp got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1875 a129376b 2019-03-28 stsp return find_entry_by_name(tree, name, strlen(name));
1878 776d4d29 2018-06-17 stsp const struct got_error *
1879 cc8021af 2021-10-12 thomas got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1880 cc8021af 2021-10-12 thomas struct got_repository *repo, struct got_tree_object *tree,
1881 cc8021af 2021-10-12 thomas const char *path)
1883 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
1884 cc8021af 2021-10-12 thomas struct got_tree_object *subtree = NULL;
1885 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
1886 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1887 b7cd37e5 2018-11-18 stsp size_t seglen;
1889 27d434c2 2018-09-15 stsp *id = NULL;
1892 5e54fb30 2019-05-31 stsp while (s[0] == '/')
1895 65a9bbe9 2018-09-15 stsp seglen = 0;
1896 cc8021af 2021-10-12 thomas subtree = tree;
1897 b7cd37e5 2018-11-18 stsp while (*s) {
1898 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
1900 776d4d29 2018-06-17 stsp if (*s != '/') {
1907 cc8021af 2021-10-12 thomas te = find_entry_by_name(subtree, seg, seglen);
1908 db37e2c0 2018-06-21 stsp if (te == NULL) {
1909 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1910 776d4d29 2018-06-17 stsp goto done;
1913 b7cd37e5 2018-11-18 stsp if (*s == '\0')
1916 776d4d29 2018-06-17 stsp seg = s + 1;
1917 65a9bbe9 2018-09-15 stsp seglen = 0;
1920 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
1922 db37e2c0 2018-06-21 stsp te = NULL;
1924 776d4d29 2018-06-17 stsp goto done;
1925 cc8021af 2021-10-12 thomas if (subtree != tree)
1926 cc8021af 2021-10-12 thomas got_object_tree_close(subtree);
1927 cc8021af 2021-10-12 thomas subtree = next_tree;
1932 56e0773d 2019-11-28 stsp *id = got_object_id_dup(&te->id);
1933 27d434c2 2018-09-15 stsp if (*id == NULL)
1934 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
1935 cc8021af 2021-10-12 thomas if (mode)
1936 cc8021af 2021-10-12 thomas *mode = te->mode;
1938 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1940 cc8021af 2021-10-12 thomas if (subtree && subtree != tree)
1941 cc8021af 2021-10-12 thomas got_object_tree_close(subtree);
1942 cc8021af 2021-10-12 thomas return err;
1944 cc8021af 2021-10-12 thomas const struct got_error *
1945 cc8021af 2021-10-12 thomas got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1946 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
1948 cc8021af 2021-10-12 thomas const struct got_error *err = NULL;
1949 cc8021af 2021-10-12 thomas struct got_tree_object *tree = NULL;
1951 cc8021af 2021-10-12 thomas *id = NULL;
1953 cc8021af 2021-10-12 thomas /* Handle opening of root of commit's tree. */
1954 cc8021af 2021-10-12 thomas if (got_path_is_root_dir(path)) {
1955 cc8021af 2021-10-12 thomas *id = got_object_id_dup(commit->tree_id);
1956 cc8021af 2021-10-12 thomas if (*id == NULL)
1957 cc8021af 2021-10-12 thomas err = got_error_from_errno("got_object_id_dup");
1958 cc8021af 2021-10-12 thomas } else {
1959 cc8021af 2021-10-12 thomas err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1960 cc8021af 2021-10-12 thomas if (err)
1961 cc8021af 2021-10-12 thomas goto done;
1962 cc8021af 2021-10-12 thomas err = got_object_tree_find_path(id, NULL, repo, tree, path);
1966 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
1967 776d4d29 2018-06-17 stsp return err;
1971 ac5f2b26 2020-05-05 stsp * Normalize file mode bits to avoid false positive tree entry differences
1972 ac5f2b26 2020-05-05 stsp * in case tree entries have unexpected mode bits set.
1974 ac5f2b26 2020-05-05 stsp static mode_t
1975 ac5f2b26 2020-05-05 stsp normalize_mode_for_comparison(mode_t mode)
1978 ac5f2b26 2020-05-05 stsp * For directories, the only relevant bit is the IFDIR bit.
1979 ac5f2b26 2020-05-05 stsp * This allows us to detect paths changing from a directory
1980 ac5f2b26 2020-05-05 stsp * to a file and vice versa.
1982 ac5f2b26 2020-05-05 stsp if (S_ISDIR(mode))
1983 ac5f2b26 2020-05-05 stsp return mode & S_IFDIR;
1986 40dde666 2020-07-23 stsp * For symlinks, the only relevant bit is the IFLNK bit.
1987 40dde666 2020-07-23 stsp * This allows us to detect paths changing from a symlinks
1988 40dde666 2020-07-23 stsp * to a file or directory and vice versa.
1990 40dde666 2020-07-23 stsp if (S_ISLNK(mode))
1991 40dde666 2020-07-23 stsp return mode & S_IFLNK;
1993 ac5f2b26 2020-05-05 stsp /* For files, the only change we care about is the executable bit. */
1994 ac5f2b26 2020-05-05 stsp return mode & S_IXUSR;
1997 07862c20 2018-09-15 stsp const struct got_error *
1998 07862c20 2018-09-15 stsp got_object_tree_path_changed(int *changed,
1999 07862c20 2018-09-15 stsp struct got_tree_object *tree01, struct got_tree_object *tree02,
2000 07862c20 2018-09-15 stsp const char *path, struct got_repository *repo)
2002 07862c20 2018-09-15 stsp const struct got_error *err = NULL;
2003 07862c20 2018-09-15 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2004 07862c20 2018-09-15 stsp struct got_tree_entry *te1 = NULL, *te2 = NULL;
2005 65a9bbe9 2018-09-15 stsp const char *seg, *s;
2006 3b7f9878 2018-11-18 stsp size_t seglen;
2008 07862c20 2018-09-15 stsp *changed = 0;
2010 07862c20 2018-09-15 stsp /* We not do support comparing the root path. */
2011 61a7d79f 2020-02-29 stsp if (got_path_is_root_dir(path))
2012 63f810e6 2020-02-29 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
2014 07862c20 2018-09-15 stsp tree1 = tree01;
2015 07862c20 2018-09-15 stsp tree2 = tree02;
2017 61a7d79f 2020-02-29 stsp while (*s == '/')
2020 65a9bbe9 2018-09-15 stsp seglen = 0;
2021 3b7f9878 2018-11-18 stsp while (*s) {
2022 07862c20 2018-09-15 stsp struct got_tree_object *next_tree1, *next_tree2;
2023 ac5f2b26 2020-05-05 stsp mode_t mode1, mode2;
2025 07862c20 2018-09-15 stsp if (*s != '/') {
2032 65a9bbe9 2018-09-15 stsp te1 = find_entry_by_name(tree1, seg, seglen);
2033 07862c20 2018-09-15 stsp if (te1 == NULL) {
2034 07862c20 2018-09-15 stsp err = got_error(GOT_ERR_NO_OBJ);
2035 07862c20 2018-09-15 stsp goto done;
2038 e8bfb8f3 2020-12-18 stsp if (tree2)
2039 e8bfb8f3 2020-12-18 stsp te2 = find_entry_by_name(tree2, seg, seglen);
2041 e8bfb8f3 2020-12-18 stsp if (te2) {
2042 e8bfb8f3 2020-12-18 stsp mode1 = normalize_mode_for_comparison(te1->mode);
2043 e8bfb8f3 2020-12-18 stsp mode2 = normalize_mode_for_comparison(te2->mode);
2044 e8bfb8f3 2020-12-18 stsp if (mode1 != mode2) {
2045 e8bfb8f3 2020-12-18 stsp *changed = 1;
2046 e8bfb8f3 2020-12-18 stsp goto done;
2049 e8bfb8f3 2020-12-18 stsp if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2050 e8bfb8f3 2020-12-18 stsp *changed = 0;
2051 e8bfb8f3 2020-12-18 stsp goto done;
2055 3b7f9878 2018-11-18 stsp if (*s == '\0') { /* final path element */
2056 07862c20 2018-09-15 stsp *changed = 1;
2057 07862c20 2018-09-15 stsp goto done;
2060 07862c20 2018-09-15 stsp seg = s + 1;
2062 65a9bbe9 2018-09-15 stsp seglen = 0;
2064 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree1, repo,
2065 56e0773d 2019-11-28 stsp &te1->id);
2066 07862c20 2018-09-15 stsp te1 = NULL;
2068 07862c20 2018-09-15 stsp goto done;
2069 a31cea73 2018-09-15 stsp if (tree1 != tree01)
2070 a31cea73 2018-09-15 stsp got_object_tree_close(tree1);
2071 07862c20 2018-09-15 stsp tree1 = next_tree1;
2073 e8bfb8f3 2020-12-18 stsp if (te2) {
2074 e8bfb8f3 2020-12-18 stsp err = got_object_open_as_tree(&next_tree2, repo,
2075 e8bfb8f3 2020-12-18 stsp &te2->id);
2076 e8bfb8f3 2020-12-18 stsp te2 = NULL;
2078 e8bfb8f3 2020-12-18 stsp goto done;
2079 e8bfb8f3 2020-12-18 stsp if (tree2 != tree02)
2080 e8bfb8f3 2020-12-18 stsp got_object_tree_close(tree2);
2081 e8bfb8f3 2020-12-18 stsp tree2 = next_tree2;
2082 e8bfb8f3 2020-12-18 stsp } else if (tree2) {
2083 e8bfb8f3 2020-12-18 stsp if (tree2 != tree02)
2084 e8bfb8f3 2020-12-18 stsp got_object_tree_close(tree2);
2085 e8bfb8f3 2020-12-18 stsp tree2 = NULL;
2090 a31cea73 2018-09-15 stsp if (tree1 && tree1 != tree01)
2091 07862c20 2018-09-15 stsp got_object_tree_close(tree1);
2092 a31cea73 2018-09-15 stsp if (tree2 && tree2 != tree02)
2093 07862c20 2018-09-15 stsp got_object_tree_close(tree2);
2094 77880158 2018-11-04 stsp return err;
2097 ed175427 2019-05-09 stsp const struct got_error *
2098 ed175427 2019-05-09 stsp got_object_tree_entry_dup(struct got_tree_entry **new_te,
2099 ed175427 2019-05-09 stsp struct got_tree_entry *te)
2101 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
2103 ed175427 2019-05-09 stsp *new_te = calloc(1, sizeof(**new_te));
2104 ed175427 2019-05-09 stsp if (*new_te == NULL)
2105 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
2107 ed175427 2019-05-09 stsp (*new_te)->mode = te->mode;
2108 56e0773d 2019-11-28 stsp memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2109 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2110 8c4eabf2 2019-05-10 stsp return err;
2114 56e0773d 2019-11-28 stsp got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2116 63c5ca5d 2019-08-24 stsp return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2120 e40622f4 2020-07-23 stsp got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2122 e40622f4 2020-07-23 stsp /* S_IFDIR check avoids confusing symlinks with submodules. */
2123 e40622f4 2020-07-23 stsp return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2126 e40622f4 2020-07-23 stsp static const struct got_error *
2127 e40622f4 2020-07-23 stsp resolve_symlink(char **link_target, const char *path,
2128 945f9229 2022-04-16 thomas struct got_commit_object *commit, struct got_repository *repo)
2130 e40622f4 2020-07-23 stsp const struct got_error *err = NULL;
2131 dbdd6209 2020-10-19 stsp char buf[PATH_MAX];
2132 e40622f4 2020-07-23 stsp char *name, *parent_path = NULL;
2133 e40622f4 2020-07-23 stsp struct got_object_id *tree_obj_id = NULL;
2134 e40622f4 2020-07-23 stsp struct got_tree_object *tree = NULL;
2135 e40622f4 2020-07-23 stsp struct got_tree_entry *te = NULL;
2137 e40622f4 2020-07-23 stsp *link_target = NULL;
2139 dbdd6209 2020-10-19 stsp if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2140 dbdd6209 2020-10-19 stsp return got_error(GOT_ERR_NO_SPACE);
2142 dbdd6209 2020-10-19 stsp name = basename(buf);
2143 e40622f4 2020-07-23 stsp if (name == NULL)
2144 e40622f4 2020-07-23 stsp return got_error_from_errno2("basename", path);
2146 e40622f4 2020-07-23 stsp err = got_path_dirname(&parent_path, path);
2148 e40622f4 2020-07-23 stsp return err;
2150 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_obj_id, repo, commit,
2151 e40622f4 2020-07-23 stsp parent_path);
2152 e40622f4 2020-07-23 stsp if (err) {
2153 e40622f4 2020-07-23 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2154 e40622f4 2020-07-23 stsp /* Display the complete path in error message. */
2155 e40622f4 2020-07-23 stsp err = got_error_path(path, err->code);
2157 e40622f4 2020-07-23 stsp goto done;
2160 e40622f4 2020-07-23 stsp err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2162 e40622f4 2020-07-23 stsp goto done;
2164 e40622f4 2020-07-23 stsp te = got_object_tree_find_entry(tree, name);
2165 e40622f4 2020-07-23 stsp if (te == NULL) {
2166 e40622f4 2020-07-23 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2167 e40622f4 2020-07-23 stsp goto done;
2170 e40622f4 2020-07-23 stsp if (got_object_tree_entry_is_symlink(te)) {
2171 e40622f4 2020-07-23 stsp err = got_tree_entry_get_symlink_target(link_target, te, repo);
2173 e40622f4 2020-07-23 stsp goto done;
2174 e40622f4 2020-07-23 stsp if (!got_path_is_absolute(*link_target)) {
2175 e40622f4 2020-07-23 stsp char *abspath;
2176 e40622f4 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", parent_path,
2177 e40622f4 2020-07-23 stsp *link_target) == -1) {
2178 e40622f4 2020-07-23 stsp err = got_error_from_errno("asprintf");
2179 e40622f4 2020-07-23 stsp goto done;
2181 e40622f4 2020-07-23 stsp free(*link_target);
2182 e40622f4 2020-07-23 stsp *link_target = malloc(PATH_MAX);
2183 e40622f4 2020-07-23 stsp if (*link_target == NULL) {
2184 e40622f4 2020-07-23 stsp err = got_error_from_errno("malloc");
2185 e40622f4 2020-07-23 stsp goto done;
2187 e40622f4 2020-07-23 stsp err = got_canonpath(abspath, *link_target, PATH_MAX);
2188 e40622f4 2020-07-23 stsp free(abspath);
2190 e40622f4 2020-07-23 stsp goto done;
2194 e40622f4 2020-07-23 stsp free(tree_obj_id);
2196 e40622f4 2020-07-23 stsp got_object_tree_close(tree);
2197 e40622f4 2020-07-23 stsp if (err) {
2198 e40622f4 2020-07-23 stsp free(*link_target);
2199 e40622f4 2020-07-23 stsp *link_target = NULL;
2201 e40622f4 2020-07-23 stsp return err;
2204 ca6e02ac 2020-01-07 stsp const struct got_error *
2205 e40622f4 2020-07-23 stsp got_object_resolve_symlinks(char **link_target, const char *path,
2206 945f9229 2022-04-16 thomas struct got_commit_object *commit, struct got_repository *repo)
2208 e40622f4 2020-07-23 stsp const struct got_error *err = NULL;
2209 e40622f4 2020-07-23 stsp char *next_target = NULL;
2210 e40622f4 2020-07-23 stsp int max_recursion = 40; /* matches Git */
2212 e40622f4 2020-07-23 stsp *link_target = NULL;
2215 e40622f4 2020-07-23 stsp err = resolve_symlink(&next_target,
2216 945f9229 2022-04-16 thomas *link_target ? *link_target : path, commit, repo);
2219 e40622f4 2020-07-23 stsp if (next_target) {
2220 e40622f4 2020-07-23 stsp free(*link_target);
2221 e40622f4 2020-07-23 stsp if (--max_recursion == 0) {
2222 e40622f4 2020-07-23 stsp err = got_error_path(path, GOT_ERR_RECURSION);
2223 e40622f4 2020-07-23 stsp *link_target = NULL;
2226 e40622f4 2020-07-23 stsp *link_target = next_target;
2228 e40622f4 2020-07-23 stsp } while (next_target);
2230 e40622f4 2020-07-23 stsp return err;
2233 e40622f4 2020-07-23 stsp const struct got_error *
2234 ca6e02ac 2020-01-07 stsp got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2235 ca6e02ac 2020-01-07 stsp struct got_object_id *commit_id, const char *path,
2236 ca6e02ac 2020-01-07 stsp struct got_repository *repo)
2238 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
2239 ca6e02ac 2020-01-07 stsp struct got_pack *pack = NULL;
2240 ca6e02ac 2020-01-07 stsp struct got_packidx *packidx = NULL;
2241 ca6e02ac 2020-01-07 stsp char *path_packfile = NULL;
2242 ca6e02ac 2020-01-07 stsp struct got_commit_object *changed_commit = NULL;
2243 ca6e02ac 2020-01-07 stsp struct got_object_id *changed_commit_id = NULL;
2246 ca6e02ac 2020-01-07 stsp err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2247 ca6e02ac 2020-01-07 stsp if (err) {
2248 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
2249 ca6e02ac 2020-01-07 stsp return err;
2250 ca6e02ac 2020-01-07 stsp return NULL;
2253 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
2254 aea75d87 2021-07-06 stsp packidx->path_packidx);
2256 ca6e02ac 2020-01-07 stsp return err;
2258 ca6e02ac 2020-01-07 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
2259 ca6e02ac 2020-01-07 stsp if (pack == NULL) {
2260 ca6e02ac 2020-01-07 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2262 ca6e02ac 2020-01-07 stsp goto done;
2265 ca6e02ac 2020-01-07 stsp if (pack->privsep_child == NULL) {
2266 68036464 2022-06-26 thomas err = got_pack_start_privsep_child(pack, packidx);
2268 ca6e02ac 2020-01-07 stsp goto done;
2271 ca6e02ac 2020-01-07 stsp err = got_privsep_send_commit_traversal_request(
2272 ca6e02ac 2020-01-07 stsp pack->privsep_child->ibuf, commit_id, idx, path);
2274 ca6e02ac 2020-01-07 stsp goto done;
2276 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_traversed_commits(&changed_commit,
2277 ca6e02ac 2020-01-07 stsp &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2279 ca6e02ac 2020-01-07 stsp goto done;
2281 ca6e02ac 2020-01-07 stsp if (changed_commit) {
2283 ca6e02ac 2020-01-07 stsp * Cache the commit in which the path was changed.
2284 ca6e02ac 2020-01-07 stsp * This commit might be opened again soon.
2286 ca6e02ac 2020-01-07 stsp changed_commit->refcnt++;
2287 ca6e02ac 2020-01-07 stsp err = got_repo_cache_commit(repo, changed_commit_id,
2288 ca6e02ac 2020-01-07 stsp changed_commit);
2289 ca6e02ac 2020-01-07 stsp got_object_commit_close(changed_commit);
2292 ca6e02ac 2020-01-07 stsp free(path_packfile);
2293 ca6e02ac 2020-01-07 stsp free(changed_commit_id);
2294 ca6e02ac 2020-01-07 stsp return err;
2297 63915ee5 2022-06-23 thomas const struct got_error *
2298 e71f1e62 2022-06-23 thomas got_object_enumerate(int *found_all_objects,
2299 e71f1e62 2022-06-23 thomas got_object_enumerate_commit_cb cb_commit,
2300 63915ee5 2022-06-23 thomas got_object_enumerate_tree_cb cb_tree, void *cb_arg,
2301 63915ee5 2022-06-23 thomas struct got_object_id **ours, int nours,
2302 63915ee5 2022-06-23 thomas struct got_object_id **theirs, int ntheirs,
2303 63915ee5 2022-06-23 thomas struct got_packidx *packidx, struct got_repository *repo)
2305 63915ee5 2022-06-23 thomas const struct got_error *err = NULL;
2306 63915ee5 2022-06-23 thomas struct got_pack *pack;
2307 63915ee5 2022-06-23 thomas char *path_packfile = NULL;
2309 63915ee5 2022-06-23 thomas err = got_packidx_get_packfile_path(&path_packfile,
2310 63915ee5 2022-06-23 thomas packidx->path_packidx);
2311 63915ee5 2022-06-23 thomas if (err)
2312 63915ee5 2022-06-23 thomas return err;
2314 63915ee5 2022-06-23 thomas pack = got_repo_get_cached_pack(repo, path_packfile);
2315 63915ee5 2022-06-23 thomas if (pack == NULL) {
2316 63915ee5 2022-06-23 thomas err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2317 63915ee5 2022-06-23 thomas if (err)
2318 63915ee5 2022-06-23 thomas goto done;
2321 63915ee5 2022-06-23 thomas if (pack->privsep_child == NULL) {
2322 68036464 2022-06-26 thomas err = got_pack_start_privsep_child(pack, packidx);
2323 63915ee5 2022-06-23 thomas if (err)
2324 63915ee5 2022-06-23 thomas goto done;
2327 63915ee5 2022-06-23 thomas err = got_privsep_send_object_enumeration_request(
2328 63915ee5 2022-06-23 thomas pack->privsep_child->ibuf);
2329 63915ee5 2022-06-23 thomas if (err)
2330 63915ee5 2022-06-23 thomas goto done;
2332 63915ee5 2022-06-23 thomas err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
2333 63915ee5 2022-06-23 thomas ours, nours);
2334 63915ee5 2022-06-23 thomas if (err)
2335 63915ee5 2022-06-23 thomas goto done;
2336 63915ee5 2022-06-23 thomas err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
2337 63915ee5 2022-06-23 thomas if (err)
2338 63915ee5 2022-06-23 thomas goto done;
2340 63915ee5 2022-06-23 thomas err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
2341 63915ee5 2022-06-23 thomas theirs, ntheirs);
2342 63915ee5 2022-06-23 thomas if (err)
2343 63915ee5 2022-06-23 thomas goto done;
2344 63915ee5 2022-06-23 thomas err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
2345 63915ee5 2022-06-23 thomas if (err)
2346 63915ee5 2022-06-23 thomas goto done;
2348 e71f1e62 2022-06-23 thomas err = got_privsep_recv_enumerated_objects(found_all_objects,
2349 e71f1e62 2022-06-23 thomas pack->privsep_child->ibuf, cb_commit, cb_tree, cb_arg, repo);
2351 63915ee5 2022-06-23 thomas free(path_packfile);
2352 63915ee5 2022-06-23 thomas return err;