2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 d71d75ad 2017-11-05 stsp * Permission to use, copy, modify, and distribute this software for any
5 d71d75ad 2017-11-05 stsp * purpose with or without fee is hereby granted, provided that the above
6 d71d75ad 2017-11-05 stsp * copyright notice and this permission notice appear in all copies.
8 d71d75ad 2017-11-05 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 d71d75ad 2017-11-05 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 d71d75ad 2017-11-05 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 d71d75ad 2017-11-05 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 d71d75ad 2017-11-05 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 d71d75ad 2017-11-05 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 d71d75ad 2017-11-05 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 2178c42e 2018-04-22 stsp #include <sys/types.h>
18 0ffeb3c2 2017-11-26 stsp #include <sys/stat.h>
19 2178c42e 2018-04-22 stsp #include <sys/uio.h>
20 2178c42e 2018-04-22 stsp #include <sys/socket.h>
21 2178c42e 2018-04-22 stsp #include <sys/wait.h>
22 2b0ae357 2022-01-10 thomas #include <sys/mman.h>
24 a1fd68d8 2018-01-12 stsp #include <errno.h>
25 2178c42e 2018-04-22 stsp #include <fcntl.h>
26 d71d75ad 2017-11-05 stsp #include <stdio.h>
27 ab9a70b2 2017-11-06 stsp #include <stdlib.h>
28 ab9a70b2 2017-11-06 stsp #include <string.h>
29 2178c42e 2018-04-22 stsp #include <stdint.h>
30 81a12da5 2020-09-09 naddy #include <unistd.h>
31 ab9a70b2 2017-11-06 stsp #include <zlib.h>
32 ab9a70b2 2017-11-06 stsp #include <ctype.h>
33 e40622f4 2020-07-23 stsp #include <libgen.h>
34 ab9a70b2 2017-11-06 stsp #include <limits.h>
35 788c352e 2018-06-16 stsp #include <time.h>
37 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
39 ab9a70b2 2017-11-06 stsp #include "got_error.h"
40 d71d75ad 2017-11-05 stsp #include "got_object.h"
41 ab9a70b2 2017-11-06 stsp #include "got_repository.h"
42 511a516b 2018-05-19 stsp #include "got_opentemp.h"
43 324d37e7 2019-05-11 stsp #include "got_path.h"
45 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
46 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
47 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
48 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
49 2178c42e 2018-04-22 stsp #include "got_lib_privsep.h"
50 6bef87be 2018-09-11 stsp #include "got_lib_object_idcache.h"
51 6bef87be 2018-09-11 stsp #include "got_lib_object_cache.h"
52 ad242220 2018-09-08 stsp #include "got_lib_object_parse.h"
53 15a94983 2018-12-23 stsp #include "got_lib_pack.h"
54 7bb0daa1 2018-06-21 stsp #include "got_lib_repository.h"
57 ab9a70b2 2017-11-06 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 63915ee5 2022-06-23 thomas #ifndef nitems
61 63915ee5 2022-06-23 thomas #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
64 3235492e 2018-04-01 stsp struct got_object_id *
65 3235492e 2018-04-01 stsp got_object_get_id(struct got_object *obj)
67 6402fb3c 2018-09-15 stsp return &obj->id;
70 bacc9935 2018-05-20 stsp const struct got_error *
71 bacc9935 2018-05-20 stsp got_object_get_id_str(char **outbuf, struct got_object *obj)
73 bacc9935 2018-05-20 stsp return got_object_id_str(outbuf, &obj->id);
76 15a94983 2018-12-23 stsp const struct got_error *
77 15a94983 2018-12-23 stsp got_object_get_type(int *type, struct got_repository *repo,
78 15a94983 2018-12-23 stsp struct got_object_id *id)
80 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
81 15a94983 2018-12-23 stsp struct got_object *obj;
83 15a94983 2018-12-23 stsp err = got_object_open(&obj, repo, id);
87 b107e67f 2018-01-19 stsp switch (obj->type) {
88 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_COMMIT:
89 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_TREE:
90 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_BLOB:
91 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
92 15a94983 2018-12-23 stsp *type = obj->type;
95 15a94983 2018-12-23 stsp err = got_error(GOT_ERR_OBJ_TYPE);
99 15a94983 2018-12-23 stsp got_object_close(obj);
100 15a94983 2018-12-23 stsp return err;
103 90bdb554 2019-04-11 stsp const struct got_error *
104 90bdb554 2019-04-11 stsp got_object_get_path(char **path, struct got_object_id *id,
105 90bdb554 2019-04-11 stsp struct got_repository *repo)
107 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
108 7a132809 2018-07-23 stsp char *hex = NULL;
109 41d2888b 2019-08-11 stsp char *path_objects;
111 e6b1056e 2018-04-22 stsp *path = NULL;
113 41d2888b 2019-08-11 stsp path_objects = got_repo_get_path_objects(repo);
114 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
115 638f9024 2019-05-13 stsp return got_error_from_errno("got_repo_get_path_objects");
117 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
121 d1cda826 2017-11-06 stsp if (asprintf(path, "%s/%.2x/%s", path_objects,
122 d1cda826 2017-11-06 stsp id->sha1[0], hex + 2) == -1)
123 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
127 d1cda826 2017-11-06 stsp free(path_objects);
128 d1cda826 2017-11-06 stsp return err;
131 762d73f4 2021-04-10 stsp const struct got_error *
132 762d73f4 2021-04-10 stsp got_object_open_loose_fd(int *fd, struct got_object_id *id,
133 4796fb13 2018-12-23 stsp struct got_repository *repo)
135 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
136 a1fd68d8 2018-01-12 stsp char *path;
138 90bdb554 2019-04-11 stsp err = got_object_get_path(&path, id, repo);
140 d1cda826 2017-11-06 stsp return err;
141 06340621 2021-12-31 thomas *fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
142 d5003b79 2018-04-22 stsp if (*fd == -1) {
143 e82b1d81 2019-07-27 stsp err = got_error_from_errno2("open", path);
147 4558fcd4 2018-01-14 stsp free(path);
148 2090a03d 2018-09-09 stsp return err;
151 2090a03d 2018-09-09 stsp static const struct got_error *
152 a158c901 2018-12-23 stsp request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
153 a158c901 2018-12-23 stsp struct got_object_id *id)
155 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
156 a158c901 2018-12-23 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
158 a158c901 2018-12-23 stsp err = got_privsep_send_packed_obj_req(ibuf, idx, id);
160 a158c901 2018-12-23 stsp return err;
162 a158c901 2018-12-23 stsp err = got_privsep_recv_obj(obj, ibuf);
164 a158c901 2018-12-23 stsp return err;
166 a158c901 2018-12-23 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
168 a158c901 2018-12-23 stsp return NULL;
171 bc1f382f 2022-01-05 thomas /* Create temporary files used during delta application. */
172 59d1e4a0 2021-03-10 stsp static const struct got_error *
173 bc1f382f 2022-01-05 thomas pack_child_send_tempfiles(struct imsgbuf *ibuf, struct got_pack *pack)
175 bc1f382f 2022-01-05 thomas const struct got_error *err;
176 bfb5ee0b 2022-05-31 thomas int basefd = -1, accumfd = -1;
179 bc1f382f 2022-01-05 thomas * For performance reasons, the child will keep reusing the
180 bc1f382f 2022-01-05 thomas * same temporary files during every object request.
181 bc1f382f 2022-01-05 thomas * Opening and closing new files for every object request is
182 bc1f382f 2022-01-05 thomas * too expensive during operations such as 'gotadmin pack'.
184 bc1f382f 2022-01-05 thomas if (pack->child_has_tempfiles)
185 bc1f382f 2022-01-05 thomas return NULL;
187 bfb5ee0b 2022-05-31 thomas basefd = dup(pack->basefd);
188 59d1e4a0 2021-03-10 stsp if (basefd == -1)
189 bfb5ee0b 2022-05-31 thomas return got_error_from_errno("dup");
191 bfb5ee0b 2022-05-31 thomas accumfd = dup(pack->accumfd);
192 bfb5ee0b 2022-05-31 thomas if (accumfd == -1) {
193 bfb5ee0b 2022-05-31 thomas err = got_error_from_errno("dup");
194 bfb5ee0b 2022-05-31 thomas goto done;
197 bc1f382f 2022-01-05 thomas err = got_privsep_send_tmpfd(ibuf, basefd);
199 bfb5ee0b 2022-05-31 thomas goto done;
201 bc1f382f 2022-01-05 thomas err = got_privsep_send_tmpfd(ibuf, accumfd);
203 bfb5ee0b 2022-05-31 thomas if (err) {
204 bfb5ee0b 2022-05-31 thomas if (basefd != -1)
205 bfb5ee0b 2022-05-31 thomas close(basefd);
206 bfb5ee0b 2022-05-31 thomas if (accumfd != -1)
207 bfb5ee0b 2022-05-31 thomas close(accumfd);
209 bfb5ee0b 2022-05-31 thomas pack->child_has_tempfiles = 1;
210 bc1f382f 2022-01-05 thomas return NULL;
213 bc1f382f 2022-01-05 thomas static const struct got_error *
214 bc1f382f 2022-01-05 thomas request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
215 bc1f382f 2022-01-05 thomas int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
217 bc1f382f 2022-01-05 thomas const struct got_error *err = NULL;
218 bc1f382f 2022-01-05 thomas struct imsgbuf *ibuf = pack->privsep_child->ibuf;
219 bc1f382f 2022-01-05 thomas int outfd_child;
221 bc1f382f 2022-01-05 thomas err = pack_child_send_tempfiles(ibuf, pack);
223 bc1f382f 2022-01-05 thomas return err;
225 bc1f382f 2022-01-05 thomas outfd_child = dup(outfd);
226 bc1f382f 2022-01-05 thomas if (outfd_child == -1)
227 bc1f382f 2022-01-05 thomas return got_error_from_errno("dup");
229 59d1e4a0 2021-03-10 stsp err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
231 59d1e4a0 2021-03-10 stsp close(outfd_child);
232 59d1e4a0 2021-03-10 stsp return err;
235 59d1e4a0 2021-03-10 stsp err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
237 59d1e4a0 2021-03-10 stsp return err;
239 59d1e4a0 2021-03-10 stsp err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
241 59d1e4a0 2021-03-10 stsp return err;
243 59d1e4a0 2021-03-10 stsp return NULL;
246 711fb6e8 2018-12-23 stsp static const struct got_error *
247 711fb6e8 2018-12-23 stsp read_packed_object_privsep(struct got_object **obj,
248 711fb6e8 2018-12-23 stsp struct got_repository *repo, struct got_pack *pack,
249 711fb6e8 2018-12-23 stsp struct got_packidx *packidx, int idx, struct got_object_id *id)
251 711fb6e8 2018-12-23 stsp const struct got_error *err = NULL;
253 59d1e4a0 2021-03-10 stsp if (pack->privsep_child == NULL) {
254 68036464 2022-06-26 thomas err = got_pack_start_privsep_child(pack, packidx);
256 59d1e4a0 2021-03-10 stsp return err;
259 711fb6e8 2018-12-23 stsp return request_packed_object(obj, pack, idx, id);
262 59d1e4a0 2021-03-10 stsp static const struct got_error *
263 59d1e4a0 2021-03-10 stsp read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
264 59d1e4a0 2021-03-10 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
265 59d1e4a0 2021-03-10 stsp struct got_object_id *id)
267 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
269 59d1e4a0 2021-03-10 stsp if (pack->privsep_child == NULL) {
270 68036464 2022-06-26 thomas err = got_pack_start_privsep_child(pack, packidx);
272 59d1e4a0 2021-03-10 stsp return err;
275 59d1e4a0 2021-03-10 stsp return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
279 b3d68e7f 2021-07-03 stsp const struct got_error *
280 b3d68e7f 2021-07-03 stsp got_object_open_packed(struct got_object **obj, struct got_object_id *id,
281 2090a03d 2018-09-09 stsp struct got_repository *repo)
283 2090a03d 2018-09-09 stsp const struct got_error *err = NULL;
284 2090a03d 2018-09-09 stsp struct got_pack *pack = NULL;
285 2090a03d 2018-09-09 stsp struct got_packidx *packidx = NULL;
287 2090a03d 2018-09-09 stsp char *path_packfile;
289 2090a03d 2018-09-09 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
291 2090a03d 2018-09-09 stsp return err;
293 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
294 aea75d87 2021-07-06 stsp packidx->path_packidx);
296 2090a03d 2018-09-09 stsp return err;
298 2090a03d 2018-09-09 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
299 2090a03d 2018-09-09 stsp if (pack == NULL) {
300 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
305 a158c901 2018-12-23 stsp err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
309 2090a03d 2018-09-09 stsp free(path_packfile);
310 4558fcd4 2018-01-14 stsp return err;
313 f9c2e8e5 2022-02-13 thomas const struct got_error *
314 f9c2e8e5 2022-02-13 thomas got_object_open_from_packfile(struct got_object **obj, struct got_object_id *id,
315 f9c2e8e5 2022-02-13 thomas struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
316 f9c2e8e5 2022-02-13 thomas struct got_repository *repo)
318 f9c2e8e5 2022-02-13 thomas return read_packed_object_privsep(obj, repo, pack, packidx,
319 f9c2e8e5 2022-02-13 thomas obj_idx, id);
322 f9c2e8e5 2022-02-13 thomas const struct got_error *
323 f9c2e8e5 2022-02-13 thomas got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
324 9249e7e3 2022-05-12 thomas off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
325 9249e7e3 2022-05-12 thomas off_t *delta_out_offset, struct got_object_id **base_id, int delta_cache_fd,
326 f9c2e8e5 2022-02-13 thomas struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
327 f9c2e8e5 2022-02-13 thomas struct got_repository *repo)
329 f9c2e8e5 2022-02-13 thomas const struct got_error *err = NULL;
330 f9c2e8e5 2022-02-13 thomas struct got_pack *pack = NULL;
331 f9c2e8e5 2022-02-13 thomas char *path_packfile;
333 f9c2e8e5 2022-02-13 thomas *base_size = 0;
334 f9c2e8e5 2022-02-13 thomas *result_size = 0;
335 f9c2e8e5 2022-02-13 thomas *delta_size = 0;
336 9249e7e3 2022-05-12 thomas *delta_compressed_size = 0;
337 f9c2e8e5 2022-02-13 thomas *delta_offset = 0;
338 f9c2e8e5 2022-02-13 thomas *delta_out_offset = 0;
340 f9c2e8e5 2022-02-13 thomas err = got_packidx_get_packfile_path(&path_packfile,
341 f9c2e8e5 2022-02-13 thomas packidx->path_packidx);
343 f9c2e8e5 2022-02-13 thomas return err;
345 f9c2e8e5 2022-02-13 thomas pack = got_repo_get_cached_pack(repo, path_packfile);
346 f9c2e8e5 2022-02-13 thomas if (pack == NULL) {
347 f9c2e8e5 2022-02-13 thomas err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
349 f9c2e8e5 2022-02-13 thomas return err;
352 f9c2e8e5 2022-02-13 thomas if (pack->privsep_child == NULL) {
353 68036464 2022-06-26 thomas err = got_pack_start_privsep_child(pack, packidx);
355 f9c2e8e5 2022-02-13 thomas return err;
358 f9c2e8e5 2022-02-13 thomas if (!pack->child_has_delta_outfd) {
359 f9c2e8e5 2022-02-13 thomas int outfd_child;
360 f9c2e8e5 2022-02-13 thomas outfd_child = dup(delta_cache_fd);
361 f9c2e8e5 2022-02-13 thomas if (outfd_child == -1)
362 f9c2e8e5 2022-02-13 thomas return got_error_from_errno("dup");
363 f9c2e8e5 2022-02-13 thomas err = got_privsep_send_raw_delta_outfd(
364 f9c2e8e5 2022-02-13 thomas pack->privsep_child->ibuf, outfd_child);
366 f9c2e8e5 2022-02-13 thomas return err;
367 f9c2e8e5 2022-02-13 thomas pack->child_has_delta_outfd = 1;
370 f9c2e8e5 2022-02-13 thomas err = got_privsep_send_raw_delta_req(pack->privsep_child->ibuf,
371 f9c2e8e5 2022-02-13 thomas obj_idx, id);
373 f9c2e8e5 2022-02-13 thomas return err;
375 f9c2e8e5 2022-02-13 thomas return got_privsep_recv_raw_delta(base_size, result_size, delta_size,
376 9249e7e3 2022-05-12 thomas delta_compressed_size, delta_offset, delta_out_offset, base_id,
377 9249e7e3 2022-05-12 thomas pack->privsep_child->ibuf);
380 9f2369b0 2018-12-24 stsp static const struct got_error *
381 d5c81d44 2021-07-08 stsp request_object(struct got_object **obj, struct got_object_id *id,
382 d5c81d44 2021-07-08 stsp struct got_repository *repo, int fd)
384 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
385 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
387 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
389 d5c81d44 2021-07-08 stsp err = got_privsep_send_obj_req(ibuf, fd, id);
391 9f2369b0 2018-12-24 stsp return err;
393 9f2369b0 2018-12-24 stsp return got_privsep_recv_obj(obj, ibuf);
396 9f2369b0 2018-12-24 stsp static const struct got_error *
397 59d1e4a0 2021-03-10 stsp request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
398 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo, int infd)
400 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
401 59d1e4a0 2021-03-10 stsp struct imsgbuf *ibuf;
402 59d1e4a0 2021-03-10 stsp int outfd_child;
404 59d1e4a0 2021-03-10 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
406 59d1e4a0 2021-03-10 stsp outfd_child = dup(outfd);
407 59d1e4a0 2021-03-10 stsp if (outfd_child == -1)
408 59d1e4a0 2021-03-10 stsp return got_error_from_errno("dup");
410 d5c81d44 2021-07-08 stsp err = got_privsep_send_raw_obj_req(ibuf, infd, id);
412 59d1e4a0 2021-03-10 stsp return err;
414 59d1e4a0 2021-03-10 stsp err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
416 59d1e4a0 2021-03-10 stsp return err;
418 59d1e4a0 2021-03-10 stsp return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
421 59d1e4a0 2021-03-10 stsp static const struct got_error *
422 59d1e4a0 2021-03-10 stsp start_read_object_child(struct got_repository *repo)
424 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
425 9f2369b0 2018-12-24 stsp int imsg_fds[2];
427 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
429 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
430 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
431 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
433 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
434 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
435 ddc7b220 2019-09-08 stsp free(ibuf);
436 ddc7b220 2019-09-08 stsp return err;
439 9f2369b0 2018-12-24 stsp pid = fork();
440 ddc7b220 2019-09-08 stsp if (pid == -1) {
441 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
442 ddc7b220 2019-09-08 stsp free(ibuf);
443 ddc7b220 2019-09-08 stsp return err;
445 9f2369b0 2018-12-24 stsp else if (pid == 0) {
446 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
447 9f2369b0 2018-12-24 stsp repo->path);
448 9f2369b0 2018-12-24 stsp /* not reached */
451 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
452 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
453 ddc7b220 2019-09-08 stsp free(ibuf);
454 ddc7b220 2019-09-08 stsp return err;
457 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
458 9f2369b0 2018-12-24 stsp imsg_fds[0];
459 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
460 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
461 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
463 59d1e4a0 2021-03-10 stsp return NULL;
466 b3d68e7f 2021-07-03 stsp const struct got_error *
467 b3d68e7f 2021-07-03 stsp got_object_read_header_privsep(struct got_object **obj,
468 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo, int obj_fd)
470 59d1e4a0 2021-03-10 stsp const struct got_error *err;
472 59d1e4a0 2021-03-10 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
473 d5c81d44 2021-07-08 stsp return request_object(obj, id, repo, obj_fd);
475 59d1e4a0 2021-03-10 stsp err = start_read_object_child(repo);
477 7495ec13 2021-04-04 stsp close(obj_fd);
478 59d1e4a0 2021-03-10 stsp return err;
481 d5c81d44 2021-07-08 stsp return request_object(obj, id, repo, obj_fd);
484 59d1e4a0 2021-03-10 stsp static const struct got_error *
485 59d1e4a0 2021-03-10 stsp read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
486 d5c81d44 2021-07-08 stsp int outfd, struct got_object_id *id, struct got_repository *repo,
487 d5c81d44 2021-07-08 stsp int obj_fd)
489 59d1e4a0 2021-03-10 stsp const struct got_error *err;
491 59d1e4a0 2021-03-10 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
492 d5c81d44 2021-07-08 stsp return request_raw_object(outbuf, size, hdrlen, outfd, id,
493 d5c81d44 2021-07-08 stsp repo, obj_fd);
495 59d1e4a0 2021-03-10 stsp err = start_read_object_child(repo);
497 59d1e4a0 2021-03-10 stsp return err;
499 d5c81d44 2021-07-08 stsp return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
503 4558fcd4 2018-01-14 stsp const struct got_error *
504 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
505 4558fcd4 2018-01-14 stsp struct got_object_id *id)
507 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
510 7bb0daa1 2018-06-21 stsp *obj = got_repo_get_cached_object(repo, id);
511 7bb0daa1 2018-06-21 stsp if (*obj != NULL) {
512 7bb0daa1 2018-06-21 stsp (*obj)->refcnt++;
513 7bb0daa1 2018-06-21 stsp return NULL;
516 b3d68e7f 2021-07-03 stsp err = got_object_open_packed(obj, id, repo);
517 e82b1d81 2019-07-27 stsp if (err && err->code != GOT_ERR_NO_OBJ)
518 e82b1d81 2019-07-27 stsp return err;
519 e82b1d81 2019-07-27 stsp if (*obj) {
520 e82b1d81 2019-07-27 stsp (*obj)->refcnt++;
521 e82b1d81 2019-07-27 stsp return got_repo_cache_object(repo, id, *obj);
524 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
526 762d73f4 2021-04-10 stsp if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
527 762d73f4 2021-04-10 stsp err = got_error_no_obj(id);
528 762d73f4 2021-04-10 stsp return err;
531 d5c81d44 2021-07-08 stsp err = got_object_read_header_privsep(obj, id, repo, fd);
533 4558fcd4 2018-01-14 stsp return err;
535 762d73f4 2021-04-10 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
537 59790a32 2018-09-15 stsp (*obj)->refcnt++;
538 762d73f4 2021-04-10 stsp return got_repo_cache_object(repo, id, *obj);
541 8ab9215c 2021-10-15 thomas /* *outfd must be initialized to -1 by caller */
542 59d1e4a0 2021-03-10 stsp const struct got_error *
543 8ab9215c 2021-10-15 thomas got_object_raw_open(struct got_raw_object **obj, int *outfd,
544 f8bb1d3e 2021-10-17 thomas struct got_repository *repo, struct got_object_id *id)
546 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
547 59d1e4a0 2021-03-10 stsp struct got_packidx *packidx = NULL;
549 59d1e4a0 2021-03-10 stsp uint8_t *outbuf = NULL;
550 59d1e4a0 2021-03-10 stsp off_t size = 0;
551 59d1e4a0 2021-03-10 stsp size_t hdrlen = 0;
552 59d1e4a0 2021-03-10 stsp char *path_packfile = NULL;
554 8ab9215c 2021-10-15 thomas *obj = got_repo_get_cached_raw_object(repo, id);
555 8ab9215c 2021-10-15 thomas if (*obj != NULL) {
556 8ab9215c 2021-10-15 thomas (*obj)->refcnt++;
557 8ab9215c 2021-10-15 thomas return NULL;
560 8ab9215c 2021-10-15 thomas if (*outfd == -1) {
561 8ab9215c 2021-10-15 thomas *outfd = got_opentempfd();
562 8ab9215c 2021-10-15 thomas if (*outfd == -1)
563 8ab9215c 2021-10-15 thomas return got_error_from_errno("got_opentempfd");
566 59d1e4a0 2021-03-10 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
567 59d1e4a0 2021-03-10 stsp if (err == NULL) {
568 59d1e4a0 2021-03-10 stsp struct got_pack *pack = NULL;
570 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
571 aea75d87 2021-07-06 stsp packidx->path_packidx);
575 59d1e4a0 2021-03-10 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
576 59d1e4a0 2021-03-10 stsp if (pack == NULL) {
577 59d1e4a0 2021-03-10 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
582 59d1e4a0 2021-03-10 stsp err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
583 8ab9215c 2021-10-15 thomas *outfd, pack, packidx, idx, id);
585 dd3af45a 2021-10-15 thomas goto done;
586 59d1e4a0 2021-03-10 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
589 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
592 8ab9215c 2021-10-15 thomas err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
593 d5c81d44 2021-07-08 stsp id, repo, fd);
595 dd3af45a 2021-10-15 thomas goto done;
598 59d1e4a0 2021-03-10 stsp *obj = calloc(1, sizeof(**obj));
599 59d1e4a0 2021-03-10 stsp if (*obj == NULL) {
600 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("calloc");
603 2b0ae357 2022-01-10 thomas (*obj)->fd = -1;
605 59d1e4a0 2021-03-10 stsp if (outbuf) {
606 59d1e4a0 2021-03-10 stsp (*obj)->data = outbuf;
608 59d1e4a0 2021-03-10 stsp struct stat sb;
609 8ab9215c 2021-10-15 thomas if (fstat(*outfd, &sb) == -1) {
610 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fstat");
614 a8591711 2021-06-18 stsp if (sb.st_size != hdrlen + size) {
615 59d1e4a0 2021-03-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
618 2b0ae357 2022-01-10 thomas #ifndef GOT_PACK_NO_MMAP
619 2b0ae357 2022-01-10 thomas if (hdrlen + size > 0) {
620 2b0ae357 2022-01-10 thomas (*obj)->data = mmap(NULL, hdrlen + size, PROT_READ,
621 2b0ae357 2022-01-10 thomas MAP_PRIVATE, *outfd, 0);
622 2b0ae357 2022-01-10 thomas if ((*obj)->data == MAP_FAILED) {
623 2b0ae357 2022-01-10 thomas if (errno != ENOMEM) {
624 2b0ae357 2022-01-10 thomas err = got_error_from_errno("mmap");
625 2b0ae357 2022-01-10 thomas goto done;
627 2b0ae357 2022-01-10 thomas (*obj)->data = NULL;
629 2b0ae357 2022-01-10 thomas (*obj)->fd = *outfd;
630 2b0ae357 2022-01-10 thomas *outfd = -1;
634 2b0ae357 2022-01-10 thomas if (*outfd != -1) {
635 2b0ae357 2022-01-10 thomas (*obj)->f = fdopen(*outfd, "r");
636 2b0ae357 2022-01-10 thomas if ((*obj)->f == NULL) {
637 2b0ae357 2022-01-10 thomas err = got_error_from_errno("fdopen");
638 2b0ae357 2022-01-10 thomas goto done;
640 2b0ae357 2022-01-10 thomas *outfd = -1;
643 59d1e4a0 2021-03-10 stsp (*obj)->hdrlen = hdrlen;
644 59d1e4a0 2021-03-10 stsp (*obj)->size = size;
645 8ab9215c 2021-10-15 thomas err = got_repo_cache_raw_object(repo, id, *obj);
647 59d1e4a0 2021-03-10 stsp free(path_packfile);
649 59d1e4a0 2021-03-10 stsp if (*obj) {
650 59d1e4a0 2021-03-10 stsp got_object_raw_close(*obj);
651 59d1e4a0 2021-03-10 stsp *obj = NULL;
653 59d1e4a0 2021-03-10 stsp free(outbuf);
655 8ab9215c 2021-10-15 thomas (*obj)->refcnt++;
656 59d1e4a0 2021-03-10 stsp return err;
659 59d1e4a0 2021-03-10 stsp const struct got_error *
660 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
661 6dfa2fd3 2018-02-12 stsp const char *id_str)
663 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
665 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
666 6dd1ece6 2019-11-10 stsp return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
668 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
671 15a94983 2018-12-23 stsp const struct got_error *
672 15a94983 2018-12-23 stsp got_object_resolve_id_str(struct got_object_id **id,
673 15a94983 2018-12-23 stsp struct got_repository *repo, const char *id_str)
675 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
676 15a94983 2018-12-23 stsp struct got_object *obj;
678 15a94983 2018-12-23 stsp err = got_object_open_by_id_str(&obj, repo, id_str);
680 15a94983 2018-12-23 stsp return err;
682 15a94983 2018-12-23 stsp *id = got_object_id_dup(got_object_get_id(obj));
683 15a94983 2018-12-23 stsp got_object_close(obj);
684 15a94983 2018-12-23 stsp if (*id == NULL)
685 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
687 15a94983 2018-12-23 stsp return NULL;
690 434025f3 2018-09-16 stsp static const struct got_error *
691 a158c901 2018-12-23 stsp request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
692 a158c901 2018-12-23 stsp int pack_idx, struct got_object_id *id)
694 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
696 a158c901 2018-12-23 stsp err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
699 a158c901 2018-12-23 stsp return err;
701 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
703 ca6e02ac 2020-01-07 stsp return err;
705 ca6e02ac 2020-01-07 stsp (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
706 ca6e02ac 2020-01-07 stsp return NULL;
709 a158c901 2018-12-23 stsp static const struct got_error *
710 a158c901 2018-12-23 stsp read_packed_commit_privsep(struct got_commit_object **commit,
711 a158c901 2018-12-23 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
712 a158c901 2018-12-23 stsp struct got_object_id *id)
714 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
716 a158c901 2018-12-23 stsp if (pack->privsep_child)
717 a158c901 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
719 68036464 2022-06-26 thomas err = got_pack_start_privsep_child(pack, packidx);
721 a158c901 2018-12-23 stsp return err;
723 711fb6e8 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
726 9f2369b0 2018-12-24 stsp static const struct got_error *
727 9f2369b0 2018-12-24 stsp request_commit(struct got_commit_object **commit, struct got_repository *repo,
728 d5c81d44 2021-07-08 stsp int fd, struct got_object_id *id)
730 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
731 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
733 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
735 d5c81d44 2021-07-08 stsp err = got_privsep_send_commit_req(ibuf, fd, id, -1);
737 9f2369b0 2018-12-24 stsp return err;
739 9f2369b0 2018-12-24 stsp return got_privsep_recv_commit(commit, ibuf);
742 9f2369b0 2018-12-24 stsp static const struct got_error *
743 9f2369b0 2018-12-24 stsp read_commit_privsep(struct got_commit_object **commit, int obj_fd,
744 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo)
746 ddc7b220 2019-09-08 stsp const struct got_error *err;
747 9f2369b0 2018-12-24 stsp int imsg_fds[2];
749 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
751 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
752 d5c81d44 2021-07-08 stsp return request_commit(commit, repo, obj_fd, id);
754 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
755 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
756 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
758 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
759 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
760 ddc7b220 2019-09-08 stsp free(ibuf);
761 ddc7b220 2019-09-08 stsp return err;
764 9f2369b0 2018-12-24 stsp pid = fork();
765 ddc7b220 2019-09-08 stsp if (pid == -1) {
766 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
767 ddc7b220 2019-09-08 stsp free(ibuf);
768 ddc7b220 2019-09-08 stsp return err;
770 9f2369b0 2018-12-24 stsp else if (pid == 0) {
771 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
772 9f2369b0 2018-12-24 stsp repo->path);
773 9f2369b0 2018-12-24 stsp /* not reached */
776 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
777 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
778 ddc7b220 2019-09-08 stsp free(ibuf);
779 ddc7b220 2019-09-08 stsp return err;
781 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
782 9f2369b0 2018-12-24 stsp imsg_fds[0];
783 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
784 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
785 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
787 d5c81d44 2021-07-08 stsp return request_commit(commit, repo, obj_fd, id);
791 a158c901 2018-12-23 stsp static const struct got_error *
792 434025f3 2018-09-16 stsp open_commit(struct got_commit_object **commit,
793 1785f84a 2018-12-23 stsp struct got_repository *repo, struct got_object_id *id, int check_cache)
795 434025f3 2018-09-16 stsp const struct got_error *err = NULL;
796 1785f84a 2018-12-23 stsp struct got_packidx *packidx = NULL;
798 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
800 434025f3 2018-09-16 stsp if (check_cache) {
801 1785f84a 2018-12-23 stsp *commit = got_repo_get_cached_commit(repo, id);
802 434025f3 2018-09-16 stsp if (*commit != NULL) {
803 434025f3 2018-09-16 stsp (*commit)->refcnt++;
804 434025f3 2018-09-16 stsp return NULL;
807 434025f3 2018-09-16 stsp *commit = NULL;
809 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
810 e82b1d81 2019-07-27 stsp if (err == NULL) {
811 1785f84a 2018-12-23 stsp struct got_pack *pack = NULL;
813 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
814 aea75d87 2021-07-06 stsp packidx->path_packidx);
816 1785f84a 2018-12-23 stsp return err;
818 1785f84a 2018-12-23 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
819 434025f3 2018-09-16 stsp if (pack == NULL) {
820 1785f84a 2018-12-23 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
825 a158c901 2018-12-23 stsp err = read_packed_commit_privsep(commit, pack,
826 1785f84a 2018-12-23 stsp packidx, idx, id);
827 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
830 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
832 e82b1d81 2019-07-27 stsp return err;
833 d5c81d44 2021-07-08 stsp err = read_commit_privsep(commit, fd, id, repo);
836 434025f3 2018-09-16 stsp if (err == NULL) {
837 434025f3 2018-09-16 stsp (*commit)->refcnt++;
838 1785f84a 2018-12-23 stsp err = got_repo_cache_commit(repo, id, *commit);
841 8d2c5ea3 2019-08-13 stsp free(path_packfile);
842 e32baab7 2018-11-05 stsp return err;
845 e32baab7 2018-11-05 stsp const struct got_error *
846 e32baab7 2018-11-05 stsp got_object_open_as_commit(struct got_commit_object **commit,
847 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object_id *id)
849 e32baab7 2018-11-05 stsp *commit = got_repo_get_cached_commit(repo, id);
850 e32baab7 2018-11-05 stsp if (*commit != NULL) {
851 e32baab7 2018-11-05 stsp (*commit)->refcnt++;
852 e32baab7 2018-11-05 stsp return NULL;
855 1785f84a 2018-12-23 stsp return open_commit(commit, repo, id, 0);
858 e32baab7 2018-11-05 stsp const struct got_error *
859 e32baab7 2018-11-05 stsp got_object_commit_open(struct got_commit_object **commit,
860 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object *obj)
862 1785f84a 2018-12-23 stsp return open_commit(commit, repo, got_object_get_id(obj), 1);
865 434025f3 2018-09-16 stsp const struct got_error *
866 dbc6a6b6 2018-07-12 stsp got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
868 dbc6a6b6 2018-07-12 stsp *qid = calloc(1, sizeof(**qid));
869 dbc6a6b6 2018-07-12 stsp if (*qid == NULL)
870 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
872 ec242592 2022-04-22 thomas memcpy(&(*qid)->id, id, sizeof((*qid)->id));
873 9ca9aafb 2021-06-18 stsp return NULL;
876 9ca9aafb 2021-06-18 stsp const struct got_error *
877 9ca9aafb 2021-06-18 stsp got_object_id_queue_copy(const struct got_object_id_queue *src,
878 9ca9aafb 2021-06-18 stsp struct got_object_id_queue *dest)
880 9ca9aafb 2021-06-18 stsp const struct got_error *err;
881 9ca9aafb 2021-06-18 stsp struct got_object_qid *qid;
883 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, src, entry) {
884 9ca9aafb 2021-06-18 stsp struct got_object_qid *new;
886 9ca9aafb 2021-06-18 stsp * Deep-copy the object ID only. Let the caller deal
887 9ca9aafb 2021-06-18 stsp * with setting up the new->data pointer if needed.
889 ec242592 2022-04-22 thomas err = got_object_qid_alloc(&new, &qid->id);
891 9ca9aafb 2021-06-18 stsp got_object_id_queue_free(dest);
892 9ca9aafb 2021-06-18 stsp return err;
894 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(dest, new, entry);
897 dbc6a6b6 2018-07-12 stsp return NULL;
900 a158c901 2018-12-23 stsp static const struct got_error *
901 13c729f7 2018-12-24 stsp request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
902 13c729f7 2018-12-24 stsp int pack_idx, struct got_object_id *id)
904 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
906 13c729f7 2018-12-24 stsp err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
909 a158c901 2018-12-23 stsp return err;
911 a158c901 2018-12-23 stsp return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
914 71eb0e7f 2018-09-16 stsp static const struct got_error *
915 13c729f7 2018-12-24 stsp read_packed_tree_privsep(struct got_tree_object **tree,
916 13c729f7 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
917 13c729f7 2018-12-24 stsp struct got_object_id *id)
919 13c729f7 2018-12-24 stsp const struct got_error *err = NULL;
921 13c729f7 2018-12-24 stsp if (pack->privsep_child)
922 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
924 68036464 2022-06-26 thomas err = got_pack_start_privsep_child(pack, packidx);
926 13c729f7 2018-12-24 stsp return err;
928 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
931 13c729f7 2018-12-24 stsp static const struct got_error *
932 9f2369b0 2018-12-24 stsp request_tree(struct got_tree_object **tree, struct got_repository *repo,
933 d5c81d44 2021-07-08 stsp int fd, struct got_object_id *id)
935 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
936 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
938 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
940 d5c81d44 2021-07-08 stsp err = got_privsep_send_tree_req(ibuf, fd, id, -1);
942 9f2369b0 2018-12-24 stsp return err;
944 9f2369b0 2018-12-24 stsp return got_privsep_recv_tree(tree, ibuf);
947 ef20f542 2022-06-26 thomas static const struct got_error *
948 9f2369b0 2018-12-24 stsp read_tree_privsep(struct got_tree_object **tree, int obj_fd,
949 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo)
951 ddc7b220 2019-09-08 stsp const struct got_error *err;
952 9f2369b0 2018-12-24 stsp int imsg_fds[2];
954 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
956 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
957 d5c81d44 2021-07-08 stsp return request_tree(tree, repo, obj_fd, id);
959 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
960 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
961 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
963 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
964 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
965 ddc7b220 2019-09-08 stsp free(ibuf);
966 ddc7b220 2019-09-08 stsp return err;
969 9f2369b0 2018-12-24 stsp pid = fork();
970 ddc7b220 2019-09-08 stsp if (pid == -1) {
971 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
972 ddc7b220 2019-09-08 stsp free(ibuf);
973 ddc7b220 2019-09-08 stsp return err;
975 9f2369b0 2018-12-24 stsp else if (pid == 0) {
976 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
977 9f2369b0 2018-12-24 stsp repo->path);
978 9f2369b0 2018-12-24 stsp /* not reached */
981 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
982 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
983 ddc7b220 2019-09-08 stsp free(ibuf);
984 ddc7b220 2019-09-08 stsp return err;
986 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
987 9f2369b0 2018-12-24 stsp imsg_fds[0];
988 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
989 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
990 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
993 d5c81d44 2021-07-08 stsp return request_tree(tree, repo, obj_fd, id);
996 9f2369b0 2018-12-24 stsp static const struct got_error *
997 13c729f7 2018-12-24 stsp open_tree(struct got_tree_object **tree, struct got_repository *repo,
998 13c729f7 2018-12-24 stsp struct got_object_id *id, int check_cache)
1000 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
1001 13c729f7 2018-12-24 stsp struct got_packidx *packidx = NULL;
1003 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1005 71eb0e7f 2018-09-16 stsp if (check_cache) {
1006 13c729f7 2018-12-24 stsp *tree = got_repo_get_cached_tree(repo, id);
1007 71eb0e7f 2018-09-16 stsp if (*tree != NULL) {
1008 71eb0e7f 2018-09-16 stsp (*tree)->refcnt++;
1009 71eb0e7f 2018-09-16 stsp return NULL;
1012 71eb0e7f 2018-09-16 stsp *tree = NULL;
1014 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1015 e82b1d81 2019-07-27 stsp if (err == NULL) {
1016 13c729f7 2018-12-24 stsp struct got_pack *pack = NULL;
1018 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
1019 aea75d87 2021-07-06 stsp packidx->path_packidx);
1021 13c729f7 2018-12-24 stsp return err;
1023 13c729f7 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1024 e7885405 2018-09-10 stsp if (pack == NULL) {
1025 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1028 8d2c5ea3 2019-08-13 stsp goto done;
1030 13c729f7 2018-12-24 stsp err = read_packed_tree_privsep(tree, pack,
1031 13c729f7 2018-12-24 stsp packidx, idx, id);
1032 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1035 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1037 e82b1d81 2019-07-27 stsp return err;
1038 d5c81d44 2021-07-08 stsp err = read_tree_privsep(tree, fd, id, repo);
1041 f6be5c30 2018-06-22 stsp if (err == NULL) {
1042 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
1043 13c729f7 2018-12-24 stsp err = got_repo_cache_tree(repo, id, *tree);
1046 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1047 776d4d29 2018-06-17 stsp return err;
1050 776d4d29 2018-06-17 stsp const struct got_error *
1051 776d4d29 2018-06-17 stsp got_object_open_as_tree(struct got_tree_object **tree,
1052 776d4d29 2018-06-17 stsp struct got_repository *repo, struct got_object_id *id)
1054 e8eb494a 2018-09-16 stsp *tree = got_repo_get_cached_tree(repo, id);
1055 e8eb494a 2018-09-16 stsp if (*tree != NULL) {
1056 e8eb494a 2018-09-16 stsp (*tree)->refcnt++;
1057 e8eb494a 2018-09-16 stsp return NULL;
1060 13c729f7 2018-12-24 stsp return open_tree(tree, repo, id, 0);
1063 71eb0e7f 2018-09-16 stsp const struct got_error *
1064 71eb0e7f 2018-09-16 stsp got_object_tree_open(struct got_tree_object **tree,
1065 71eb0e7f 2018-09-16 stsp struct got_repository *repo, struct got_object *obj)
1067 13c729f7 2018-12-24 stsp return open_tree(tree, repo, got_object_get_id(obj), 1);
1071 56e0773d 2019-11-28 stsp got_object_tree_get_nentries(struct got_tree_object *tree)
1073 56e0773d 2019-11-28 stsp return tree->nentries;
1076 56e0773d 2019-11-28 stsp struct got_tree_entry *
1077 56e0773d 2019-11-28 stsp got_object_tree_get_first_entry(struct got_tree_object *tree)
1079 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, 0);
1082 56e0773d 2019-11-28 stsp struct got_tree_entry *
1083 56e0773d 2019-11-28 stsp got_object_tree_get_last_entry(struct got_tree_object *tree)
1085 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, tree->nentries - 1);
1088 56e0773d 2019-11-28 stsp struct got_tree_entry *
1089 56e0773d 2019-11-28 stsp got_object_tree_get_entry(struct got_tree_object *tree, int i)
1091 56e0773d 2019-11-28 stsp if (i < 0 || i >= tree->nentries)
1092 56e0773d 2019-11-28 stsp return NULL;
1093 56e0773d 2019-11-28 stsp return &tree->entries[i];
1097 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(struct got_tree_entry *te)
1099 56e0773d 2019-11-28 stsp return te->mode;
1102 56e0773d 2019-11-28 stsp const char *
1103 56e0773d 2019-11-28 stsp got_tree_entry_get_name(struct got_tree_entry *te)
1105 56e0773d 2019-11-28 stsp return &te->name[0];
1108 56e0773d 2019-11-28 stsp struct got_object_id *
1109 56e0773d 2019-11-28 stsp got_tree_entry_get_id(struct got_tree_entry *te)
1111 56e0773d 2019-11-28 stsp return &te->id;
1114 0d6c6ee3 2020-05-20 stsp const struct got_error *
1115 af57b12a 2020-07-23 stsp got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1117 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
1118 32596e16 2020-07-23 stsp size_t len, totlen, hdrlen, offset;
1120 aa092692 2020-07-23 stsp *s = NULL;
1122 659dc16e 2020-07-23 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1123 659dc16e 2020-07-23 stsp totlen = 0;
1124 32596e16 2020-07-23 stsp offset = 0;
1128 659dc16e 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1130 af57b12a 2020-07-23 stsp return err;
1132 659dc16e 2020-07-23 stsp if (len == 0)
1135 659dc16e 2020-07-23 stsp totlen += len - hdrlen;
1136 af57b12a 2020-07-23 stsp p = realloc(*s, totlen + 1);
1137 659dc16e 2020-07-23 stsp if (p == NULL) {
1138 659dc16e 2020-07-23 stsp err = got_error_from_errno("realloc");
1140 af57b12a 2020-07-23 stsp *s = NULL;
1141 af57b12a 2020-07-23 stsp return err;
1144 659dc16e 2020-07-23 stsp /* Skip blob object header first time around. */
1145 af57b12a 2020-07-23 stsp memcpy(*s + offset,
1146 f8f7c882 2020-07-23 stsp got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1147 659dc16e 2020-07-23 stsp hdrlen = 0;
1148 32596e16 2020-07-23 stsp offset = totlen;
1149 659dc16e 2020-07-23 stsp } while (len > 0);
1151 af57b12a 2020-07-23 stsp (*s)[totlen] = '\0';
1152 af57b12a 2020-07-23 stsp return NULL;
1155 af57b12a 2020-07-23 stsp const struct got_error *
1156 af57b12a 2020-07-23 stsp got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1157 af57b12a 2020-07-23 stsp struct got_repository *repo)
1159 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
1160 af57b12a 2020-07-23 stsp struct got_blob_object *blob = NULL;
1161 f4ae6ddb 2022-07-01 thomas int fd = -1;
1163 af57b12a 2020-07-23 stsp *link_target = NULL;
1165 af57b12a 2020-07-23 stsp if (!got_object_tree_entry_is_symlink(te))
1166 af57b12a 2020-07-23 stsp return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1168 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
1169 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
1170 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1171 f4ae6ddb 2022-07-01 thomas goto done;
1174 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo,
1175 f4ae6ddb 2022-07-01 thomas got_tree_entry_get_id(te), PATH_MAX, fd);
1177 f4ae6ddb 2022-07-01 thomas goto done;
1179 af57b12a 2020-07-23 stsp err = got_object_blob_read_to_str(link_target, blob);
1181 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
1182 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
1183 f4ae6ddb 2022-07-01 thomas if (blob)
1184 f4ae6ddb 2022-07-01 thomas got_object_blob_close(blob);
1185 659dc16e 2020-07-23 stsp if (err) {
1186 659dc16e 2020-07-23 stsp free(*link_target);
1187 659dc16e 2020-07-23 stsp *link_target = NULL;
1189 0d6c6ee3 2020-05-20 stsp return err;
1193 56e0773d 2019-11-28 stsp got_tree_entry_get_index(struct got_tree_entry *te)
1195 56e0773d 2019-11-28 stsp return te->idx;
1198 56e0773d 2019-11-28 stsp struct got_tree_entry *
1199 56e0773d 2019-11-28 stsp got_tree_entry_get_next(struct got_tree_object *tree,
1200 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
1202 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx + 1);
1205 56e0773d 2019-11-28 stsp struct got_tree_entry *
1206 56e0773d 2019-11-28 stsp got_tree_entry_get_prev(struct got_tree_object *tree,
1207 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
1209 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx - 1);
1212 3840f4c9 2018-09-12 stsp static const struct got_error *
1213 ac544f8c 2019-01-13 stsp request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1214 ebc55e2d 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1215 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
1217 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
1218 bc1f382f 2022-01-05 thomas struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1219 bc1f382f 2022-01-05 thomas int outfd_child;
1221 bc1f382f 2022-01-05 thomas err = pack_child_send_tempfiles(ibuf, pack);
1222 bc1f382f 2022-01-05 thomas if (err)
1223 bc1f382f 2022-01-05 thomas return err;
1225 3840f4c9 2018-09-12 stsp outfd_child = dup(outfd);
1226 3840f4c9 2018-09-12 stsp if (outfd_child == -1)
1227 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1229 ebc55e2d 2018-12-24 stsp err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1231 3840f4c9 2018-09-12 stsp return err;
1233 3840f4c9 2018-09-12 stsp err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1234 3840f4c9 2018-09-12 stsp outfd_child);
1235 3840f4c9 2018-09-12 stsp if (err) {
1236 3840f4c9 2018-09-12 stsp return err;
1239 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen,
1240 ebc55e2d 2018-12-24 stsp pack->privsep_child->ibuf);
1242 3840f4c9 2018-09-12 stsp return err;
1244 3840f4c9 2018-09-12 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1245 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1247 3840f4c9 2018-09-12 stsp return err;
1250 ebc55e2d 2018-12-24 stsp static const struct got_error *
1251 ac544f8c 2019-01-13 stsp read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1252 ac544f8c 2019-01-13 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1253 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
1255 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
1257 ebc55e2d 2018-12-24 stsp if (pack->privsep_child == NULL) {
1258 68036464 2022-06-26 thomas err = got_pack_start_privsep_child(pack, packidx);
1260 ebc55e2d 2018-12-24 stsp return err;
1263 ac544f8c 2019-01-13 stsp return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1267 9f2369b0 2018-12-24 stsp static const struct got_error *
1268 ac544f8c 2019-01-13 stsp request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1269 d5c81d44 2021-07-08 stsp int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1271 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1272 9f2369b0 2018-12-24 stsp int outfd_child;
1274 9f2369b0 2018-12-24 stsp outfd_child = dup(outfd);
1275 9f2369b0 2018-12-24 stsp if (outfd_child == -1)
1276 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1278 d5c81d44 2021-07-08 stsp err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1280 9f2369b0 2018-12-24 stsp return err;
1282 9f2369b0 2018-12-24 stsp err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1284 9f2369b0 2018-12-24 stsp return err;
1286 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1288 9f2369b0 2018-12-24 stsp return err;
1290 9f2369b0 2018-12-24 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1291 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
1293 9f2369b0 2018-12-24 stsp return err;
1296 9f2369b0 2018-12-24 stsp static const struct got_error *
1297 ac544f8c 2019-01-13 stsp read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1298 d5c81d44 2021-07-08 stsp int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1300 ddc7b220 2019-09-08 stsp const struct got_error *err;
1301 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1302 9f2369b0 2018-12-24 stsp pid_t pid;
1303 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1305 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1306 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1307 d5c81d44 2021-07-08 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1311 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1312 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1313 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1315 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1316 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1317 ddc7b220 2019-09-08 stsp free(ibuf);
1318 ddc7b220 2019-09-08 stsp return err;
1321 9f2369b0 2018-12-24 stsp pid = fork();
1322 ddc7b220 2019-09-08 stsp if (pid == -1) {
1323 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1324 ddc7b220 2019-09-08 stsp free(ibuf);
1325 ddc7b220 2019-09-08 stsp return err;
1327 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1328 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1329 9f2369b0 2018-12-24 stsp repo->path);
1330 9f2369b0 2018-12-24 stsp /* not reached */
1333 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
1334 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1335 ddc7b220 2019-09-08 stsp free(ibuf);
1336 ddc7b220 2019-09-08 stsp return err;
1338 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1339 9f2369b0 2018-12-24 stsp imsg_fds[0];
1340 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1341 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1342 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1344 d5c81d44 2021-07-08 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1347 ebc55e2d 2018-12-24 stsp static const struct got_error *
1348 ebc55e2d 2018-12-24 stsp open_blob(struct got_blob_object **blob, struct got_repository *repo,
1349 f4ae6ddb 2022-07-01 thomas struct got_object_id *id, size_t blocksize, int outfd)
1351 ebc55e2d 2018-12-24 stsp const struct got_error *err = NULL;
1352 ebc55e2d 2018-12-24 stsp struct got_packidx *packidx = NULL;
1353 f9bba04a 2022-07-01 thomas int idx, dfd = -1;
1354 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1355 ac544f8c 2019-01-13 stsp uint8_t *outbuf;
1356 ebc55e2d 2018-12-24 stsp size_t size, hdrlen;
1357 ebc55e2d 2018-12-24 stsp struct stat sb;
1359 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
1360 4558fcd4 2018-01-14 stsp if (*blob == NULL)
1361 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1363 062ebb78 2018-07-12 stsp (*blob)->read_buf = malloc(blocksize);
1364 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
1365 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1366 c7254d79 2018-04-24 stsp goto done;
1369 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1370 e82b1d81 2019-07-27 stsp if (err == NULL) {
1371 ebc55e2d 2018-12-24 stsp struct got_pack *pack = NULL;
1373 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
1374 aea75d87 2021-07-06 stsp packidx->path_packidx);
1376 ebc55e2d 2018-12-24 stsp goto done;
1378 ebc55e2d 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1379 55da3778 2018-09-10 stsp if (pack == NULL) {
1380 ebc55e2d 2018-12-24 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1383 55da3778 2018-09-10 stsp goto done;
1385 ac544f8c 2019-01-13 stsp err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1386 ac544f8c 2019-01-13 stsp pack, packidx, idx, id);
1387 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1390 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&infd, id, repo);
1392 e82b1d81 2019-07-27 stsp goto done;
1393 ac544f8c 2019-01-13 stsp err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1394 d5c81d44 2021-07-08 stsp id, repo);
1397 55da3778 2018-09-10 stsp goto done;
1399 de060dff 2018-12-24 stsp if (hdrlen > size) {
1400 ebc55e2d 2018-12-24 stsp err = got_error(GOT_ERR_BAD_OBJ_HDR);
1401 ebc55e2d 2018-12-24 stsp goto done;
1404 ac544f8c 2019-01-13 stsp if (outbuf) {
1405 ac544f8c 2019-01-13 stsp (*blob)->f = fmemopen(outbuf, size, "rb");
1406 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1407 638f9024 2019-05-13 stsp err = got_error_from_errno("fmemopen");
1408 ac544f8c 2019-01-13 stsp free(outbuf);
1409 ac544f8c 2019-01-13 stsp goto done;
1411 ac544f8c 2019-01-13 stsp (*blob)->data = outbuf;
1413 ac544f8c 2019-01-13 stsp if (fstat(outfd, &sb) == -1) {
1414 638f9024 2019-05-13 stsp err = got_error_from_errno("fstat");
1415 ac544f8c 2019-01-13 stsp goto done;
1418 ac544f8c 2019-01-13 stsp if (sb.st_size != size) {
1419 ac544f8c 2019-01-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1420 f9bba04a 2022-07-01 thomas goto done;
1423 f9bba04a 2022-07-01 thomas dfd = dup(outfd);
1424 f9bba04a 2022-07-01 thomas if (dfd == -1) {
1425 f9bba04a 2022-07-01 thomas err = got_error_from_errno("dup");
1426 ac544f8c 2019-01-13 stsp goto done;
1429 acb9e3ea 2022-07-01 thomas (*blob)->f = fdopen(dfd, "rb");
1430 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1431 638f9024 2019-05-13 stsp err = got_error_from_errno("fdopen");
1432 f9bba04a 2022-07-01 thomas close(dfd);
1433 f9bba04a 2022-07-01 thomas dfd = -1;
1434 ac544f8c 2019-01-13 stsp goto done;
1438 ebc55e2d 2018-12-24 stsp (*blob)->hdrlen = hdrlen;
1439 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
1440 ebc55e2d 2018-12-24 stsp memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1443 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1444 55da3778 2018-09-10 stsp if (err) {
1445 55da3778 2018-09-10 stsp if (*blob) {
1446 7baf5860 2019-03-19 stsp got_object_blob_close(*blob);
1447 55da3778 2018-09-10 stsp *blob = NULL;
1450 a19581a2 2018-06-21 stsp return err;
1453 a19581a2 2018-06-21 stsp const struct got_error *
1454 a19581a2 2018-06-21 stsp got_object_open_as_blob(struct got_blob_object **blob,
1455 f4ae6ddb 2022-07-01 thomas struct got_repository *repo, struct got_object_id *id, size_t blocksize,
1456 f4ae6ddb 2022-07-01 thomas int outfd)
1458 f4ae6ddb 2022-07-01 thomas return open_blob(blob, repo, id, blocksize, outfd);
1461 ebc55e2d 2018-12-24 stsp const struct got_error *
1462 ebc55e2d 2018-12-24 stsp got_object_blob_open(struct got_blob_object **blob,
1463 f4ae6ddb 2022-07-01 thomas struct got_repository *repo, struct got_object *obj, size_t blocksize,
1464 f4ae6ddb 2022-07-01 thomas int outfd)
1466 f4ae6ddb 2022-07-01 thomas return open_blob(blob, repo, got_object_get_id(obj), blocksize, outfd);
1469 fb43ecf1 2019-02-11 stsp const struct got_error *
1470 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
1472 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL;
1473 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
1474 56b63ca4 2021-01-22 stsp if (blob->f && fclose(blob->f) == EOF)
1475 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1476 ac544f8c 2019-01-13 stsp free(blob->data);
1477 68482ea3 2017-11-27 stsp free(blob);
1478 fb43ecf1 2019-02-11 stsp return err;
1482 8ba819a3 2020-07-23 stsp got_object_blob_rewind(struct got_blob_object *blob)
1484 8ba819a3 2020-07-23 stsp if (blob->f)
1485 8ba819a3 2020-07-23 stsp rewind(blob->f);
1489 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1491 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1495 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
1497 f934cf2c 2018-02-12 stsp return blob->hdrlen;
1500 f934cf2c 2018-02-12 stsp const uint8_t *
1501 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
1503 f934cf2c 2018-02-12 stsp return blob->read_buf;
1506 68482ea3 2017-11-27 stsp const struct got_error *
1507 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1511 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1512 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
1513 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
1514 eb651edf 2018-02-11 stsp *outlenp = n;
1515 35e9ba5d 2018-06-21 stsp return NULL;
1518 35e9ba5d 2018-06-21 stsp const struct got_error *
1519 be659d10 2020-11-18 stsp got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1520 6c4c42e0 2019-06-24 stsp off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1522 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
1523 b6752625 2018-12-24 stsp size_t n, len, hdrlen;
1524 84451b3e 2018-07-10 stsp const uint8_t *buf;
1526 c33ebc60 2020-11-18 stsp const int alloc_chunksz = 512;
1527 c33ebc60 2020-11-18 stsp size_t nalloc = 0;
1528 f595d9bd 2019-08-14 stsp off_t off = 0, total_len = 0;
1530 6c4c42e0 2019-06-24 stsp if (line_offsets)
1531 6c4c42e0 2019-06-24 stsp *line_offsets = NULL;
1532 f595d9bd 2019-08-14 stsp if (filesize)
1533 f595d9bd 2019-08-14 stsp *filesize = 0;
1534 84451b3e 2018-07-10 stsp if (nlines)
1535 84451b3e 2018-07-10 stsp *nlines = 0;
1537 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1539 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
1541 35e9ba5d 2018-06-21 stsp return err;
1542 35e9ba5d 2018-06-21 stsp if (len == 0)
1544 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
1545 b02560ec 2019-08-19 stsp i = hdrlen;
1546 f1cbc3bc 2020-11-18 stsp if (nlines) {
1547 f1cbc3bc 2020-11-18 stsp if (line_offsets && *line_offsets == NULL) {
1548 78695fb7 2019-08-12 stsp /* Have some data but perhaps no '\n'. */
1549 78695fb7 2019-08-12 stsp *nlines = 1;
1550 c33ebc60 2020-11-18 stsp nalloc = alloc_chunksz;
1551 c33ebc60 2020-11-18 stsp *line_offsets = calloc(nalloc,
1552 c33ebc60 2020-11-18 stsp sizeof(**line_offsets));
1553 78695fb7 2019-08-12 stsp if (*line_offsets == NULL)
1554 845785d4 2020-02-02 tracey return got_error_from_errno("calloc");
1556 b02560ec 2019-08-19 stsp /* Skip forward over end of first line. */
1557 b02560ec 2019-08-19 stsp while (i < len) {
1558 b02560ec 2019-08-19 stsp if (buf[i] == '\n')
1563 b02560ec 2019-08-19 stsp /* Scan '\n' offsets in remaining chunk of data. */
1564 b02560ec 2019-08-19 stsp while (i < len) {
1565 b02560ec 2019-08-19 stsp if (buf[i] != '\n') {
1569 f595d9bd 2019-08-14 stsp (*nlines)++;
1570 c33ebc60 2020-11-18 stsp if (line_offsets && nalloc < *nlines) {
1571 c33ebc60 2020-11-18 stsp size_t n = *nlines + alloc_chunksz;
1572 78695fb7 2019-08-12 stsp off_t *o = recallocarray(*line_offsets,
1573 c33ebc60 2020-11-18 stsp nalloc, n, sizeof(**line_offsets));
1574 78695fb7 2019-08-12 stsp if (o == NULL) {
1575 78695fb7 2019-08-12 stsp free(*line_offsets);
1576 78695fb7 2019-08-12 stsp *line_offsets = NULL;
1577 78695fb7 2019-08-12 stsp return got_error_from_errno(
1578 78695fb7 2019-08-12 stsp "recallocarray");
1580 78695fb7 2019-08-12 stsp *line_offsets = o;
1581 c33ebc60 2020-11-18 stsp nalloc = n;
1583 f1cbc3bc 2020-11-18 stsp if (line_offsets) {
1584 f1cbc3bc 2020-11-18 stsp off = total_len + i - hdrlen + 1;
1585 f1cbc3bc 2020-11-18 stsp (*line_offsets)[*nlines - 1] = off;
1590 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
1591 454a6b59 2018-12-24 stsp n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1592 b6752625 2018-12-24 stsp if (n != len - hdrlen)
1593 b6752625 2018-12-24 stsp return got_ferror(outfile, GOT_ERR_IO);
1594 f595d9bd 2019-08-14 stsp total_len += len - hdrlen;
1595 35e9ba5d 2018-06-21 stsp hdrlen = 0;
1596 35e9ba5d 2018-06-21 stsp } while (len != 0);
1598 cbe7f848 2019-02-11 stsp if (fflush(outfile) != 0)
1599 638f9024 2019-05-13 stsp return got_error_from_errno("fflush");
1600 35e9ba5d 2018-06-21 stsp rewind(outfile);
1602 f595d9bd 2019-08-14 stsp if (filesize)
1603 f595d9bd 2019-08-14 stsp *filesize = total_len;
1605 776d4d29 2018-06-17 stsp return NULL;
1608 f4a881ce 2018-11-17 stsp static const struct got_error *
1609 268f7291 2018-12-24 stsp request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1610 268f7291 2018-12-24 stsp int pack_idx, struct got_object_id *id)
1612 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
1614 268f7291 2018-12-24 stsp err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1615 268f7291 2018-12-24 stsp pack_idx);
1617 a158c901 2018-12-23 stsp return err;
1619 a158c901 2018-12-23 stsp return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1622 268f7291 2018-12-24 stsp static const struct got_error *
1623 268f7291 2018-12-24 stsp read_packed_tag_privsep(struct got_tag_object **tag,
1624 268f7291 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1625 268f7291 2018-12-24 stsp struct got_object_id *id)
1627 268f7291 2018-12-24 stsp const struct got_error *err = NULL;
1629 268f7291 2018-12-24 stsp if (pack->privsep_child)
1630 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1632 68036464 2022-06-26 thomas err = got_pack_start_privsep_child(pack, packidx);
1634 268f7291 2018-12-24 stsp return err;
1636 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1639 9f2369b0 2018-12-24 stsp static const struct got_error *
1640 9f2369b0 2018-12-24 stsp request_tag(struct got_tag_object **tag, struct got_repository *repo,
1641 d5c81d44 2021-07-08 stsp int fd, struct got_object_id *id)
1643 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1644 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1646 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1648 d5c81d44 2021-07-08 stsp err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1650 9f2369b0 2018-12-24 stsp return err;
1652 9f2369b0 2018-12-24 stsp return got_privsep_recv_tag(tag, ibuf);
1655 9f2369b0 2018-12-24 stsp static const struct got_error *
1656 9f2369b0 2018-12-24 stsp read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1657 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo)
1659 ddc7b220 2019-09-08 stsp const struct got_error *err;
1660 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1661 9f2369b0 2018-12-24 stsp pid_t pid;
1662 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1664 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1665 d5c81d44 2021-07-08 stsp return request_tag(tag, repo, obj_fd, id);
1667 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1668 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1669 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1671 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1672 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1673 ddc7b220 2019-09-08 stsp free(ibuf);
1674 ddc7b220 2019-09-08 stsp return err;
1677 9f2369b0 2018-12-24 stsp pid = fork();
1678 ddc7b220 2019-09-08 stsp if (pid == -1) {
1679 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1680 ddc7b220 2019-09-08 stsp free(ibuf);
1681 ddc7b220 2019-09-08 stsp return err;
1683 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1684 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1685 9f2369b0 2018-12-24 stsp repo->path);
1686 9f2369b0 2018-12-24 stsp /* not reached */
1689 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
1690 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1691 ddc7b220 2019-09-08 stsp free(ibuf);
1692 ddc7b220 2019-09-08 stsp return err;
1694 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1695 9f2369b0 2018-12-24 stsp imsg_fds[0];
1696 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1697 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1698 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1700 d5c81d44 2021-07-08 stsp return request_tag(tag, repo, obj_fd, id);
1703 a158c901 2018-12-23 stsp static const struct got_error *
1704 268f7291 2018-12-24 stsp open_tag(struct got_tag_object **tag, struct got_repository *repo,
1705 268f7291 2018-12-24 stsp struct got_object_id *id, int check_cache)
1707 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1708 268f7291 2018-12-24 stsp struct got_packidx *packidx = NULL;
1710 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1711 5d844a1e 2019-08-13 stsp struct got_object *obj = NULL;
1712 5d844a1e 2019-08-13 stsp int obj_type = GOT_OBJ_TYPE_ANY;
1714 f4a881ce 2018-11-17 stsp if (check_cache) {
1715 268f7291 2018-12-24 stsp *tag = got_repo_get_cached_tag(repo, id);
1716 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1717 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1718 f4a881ce 2018-11-17 stsp return NULL;
1721 f4a881ce 2018-11-17 stsp *tag = NULL;
1723 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1724 e82b1d81 2019-07-27 stsp if (err == NULL) {
1725 268f7291 2018-12-24 stsp struct got_pack *pack = NULL;
1727 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
1728 aea75d87 2021-07-06 stsp packidx->path_packidx);
1730 268f7291 2018-12-24 stsp return err;
1732 268f7291 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1733 f4a881ce 2018-11-17 stsp if (pack == NULL) {
1734 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1737 8d2c5ea3 2019-08-13 stsp goto done;
1740 992eb9d8 2020-02-07 tracey /* Beware of "lightweight" tags: Check object type first. */
1741 5d844a1e 2019-08-13 stsp err = read_packed_object_privsep(&obj, repo, pack, packidx,
1744 5d844a1e 2019-08-13 stsp goto done;
1745 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1746 5d844a1e 2019-08-13 stsp got_object_close(obj);
1747 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG) {
1748 5d844a1e 2019-08-13 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1749 5d844a1e 2019-08-13 stsp goto done;
1751 5d844a1e 2019-08-13 stsp err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1752 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1755 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1757 e82b1d81 2019-07-27 stsp return err;
1758 d5c81d44 2021-07-08 stsp err = got_object_read_header_privsep(&obj, id, repo, fd);
1760 5d844a1e 2019-08-13 stsp return err;
1761 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1762 5d844a1e 2019-08-13 stsp got_object_close(obj);
1763 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
1764 5d844a1e 2019-08-13 stsp return got_error(GOT_ERR_OBJ_TYPE);
1766 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1768 5d844a1e 2019-08-13 stsp return err;
1769 d5c81d44 2021-07-08 stsp err = read_tag_privsep(tag, fd, id, repo);
1772 f4a881ce 2018-11-17 stsp if (err == NULL) {
1773 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1774 268f7291 2018-12-24 stsp err = got_repo_cache_tag(repo, id, *tag);
1777 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1778 f4a881ce 2018-11-17 stsp return err;
1781 f4a881ce 2018-11-17 stsp const struct got_error *
1782 f4a881ce 2018-11-17 stsp got_object_open_as_tag(struct got_tag_object **tag,
1783 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object_id *id)
1785 f4a881ce 2018-11-17 stsp *tag = got_repo_get_cached_tag(repo, id);
1786 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1787 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1788 f4a881ce 2018-11-17 stsp return NULL;
1791 268f7291 2018-12-24 stsp return open_tag(tag, repo, id, 0);
1794 f4a881ce 2018-11-17 stsp const struct got_error *
1795 f4a881ce 2018-11-17 stsp got_object_tag_open(struct got_tag_object **tag,
1796 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object *obj)
1798 268f7291 2018-12-24 stsp return open_tag(tag, repo, got_object_get_id(obj), 1);
1801 d24820bf 2019-08-11 stsp const char *
1802 d24820bf 2019-08-11 stsp got_object_tag_get_name(struct got_tag_object *tag)
1804 d24820bf 2019-08-11 stsp return tag->tag;
1808 0bd18d37 2019-02-01 stsp got_object_tag_get_object_type(struct got_tag_object *tag)
1810 0bd18d37 2019-02-01 stsp return tag->obj_type;
1813 0bd18d37 2019-02-01 stsp struct got_object_id *
1814 0bd18d37 2019-02-01 stsp got_object_tag_get_object_id(struct got_tag_object *tag)
1816 0bd18d37 2019-02-01 stsp return &tag->id;
1820 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_time(struct got_tag_object *tag)
1822 01073a5d 2019-08-22 stsp return tag->tagger_time;
1826 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1828 01073a5d 2019-08-22 stsp return tag->tagger_gmtoff;
1831 01073a5d 2019-08-22 stsp const char *
1832 01073a5d 2019-08-22 stsp got_object_tag_get_tagger(struct got_tag_object *tag)
1834 01073a5d 2019-08-22 stsp return tag->tagger;
1837 01073a5d 2019-08-22 stsp const char *
1838 01073a5d 2019-08-22 stsp got_object_tag_get_message(struct got_tag_object *tag)
1840 01073a5d 2019-08-22 stsp return tag->tagmsg;
1843 776d4d29 2018-06-17 stsp static struct got_tree_entry *
1844 65a9bbe9 2018-09-15 stsp find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1848 63da309a 2018-11-07 stsp /* Note that tree entries are sorted in strncmp() order. */
1849 56e0773d 2019-11-28 stsp for (i = 0; i < tree->nentries; i++) {
1850 56e0773d 2019-11-28 stsp struct got_tree_entry *te = &tree->entries[i];
1851 63da309a 2018-11-07 stsp int cmp = strncmp(te->name, name, len);
1852 63da309a 2018-11-07 stsp if (cmp < 0)
1854 63da309a 2018-11-07 stsp if (cmp > 0)
1856 63da309a 2018-11-07 stsp if (te->name[len] == '\0')
1857 776d4d29 2018-06-17 stsp return te;
1859 eb651edf 2018-02-11 stsp return NULL;
1862 56e0773d 2019-11-28 stsp struct got_tree_entry *
1863 a129376b 2019-03-28 stsp got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1865 a129376b 2019-03-28 stsp return find_entry_by_name(tree, name, strlen(name));
1868 776d4d29 2018-06-17 stsp const struct got_error *
1869 cc8021af 2021-10-12 thomas got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1870 cc8021af 2021-10-12 thomas struct got_repository *repo, struct got_tree_object *tree,
1871 cc8021af 2021-10-12 thomas const char *path)
1873 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
1874 cc8021af 2021-10-12 thomas struct got_tree_object *subtree = NULL;
1875 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
1876 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1877 b7cd37e5 2018-11-18 stsp size_t seglen;
1879 27d434c2 2018-09-15 stsp *id = NULL;
1882 5e54fb30 2019-05-31 stsp while (s[0] == '/')
1885 65a9bbe9 2018-09-15 stsp seglen = 0;
1886 cc8021af 2021-10-12 thomas subtree = tree;
1887 b7cd37e5 2018-11-18 stsp while (*s) {
1888 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
1890 776d4d29 2018-06-17 stsp if (*s != '/') {
1897 cc8021af 2021-10-12 thomas te = find_entry_by_name(subtree, seg, seglen);
1898 db37e2c0 2018-06-21 stsp if (te == NULL) {
1899 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1900 776d4d29 2018-06-17 stsp goto done;
1903 b7cd37e5 2018-11-18 stsp if (*s == '\0')
1906 776d4d29 2018-06-17 stsp seg = s + 1;
1907 65a9bbe9 2018-09-15 stsp seglen = 0;
1910 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
1912 db37e2c0 2018-06-21 stsp te = NULL;
1914 776d4d29 2018-06-17 stsp goto done;
1915 cc8021af 2021-10-12 thomas if (subtree != tree)
1916 cc8021af 2021-10-12 thomas got_object_tree_close(subtree);
1917 cc8021af 2021-10-12 thomas subtree = next_tree;
1922 56e0773d 2019-11-28 stsp *id = got_object_id_dup(&te->id);
1923 27d434c2 2018-09-15 stsp if (*id == NULL)
1924 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
1925 cc8021af 2021-10-12 thomas if (mode)
1926 cc8021af 2021-10-12 thomas *mode = te->mode;
1928 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1930 cc8021af 2021-10-12 thomas if (subtree && subtree != tree)
1931 cc8021af 2021-10-12 thomas got_object_tree_close(subtree);
1932 cc8021af 2021-10-12 thomas return err;
1934 cc8021af 2021-10-12 thomas const struct got_error *
1935 cc8021af 2021-10-12 thomas got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1936 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
1938 cc8021af 2021-10-12 thomas const struct got_error *err = NULL;
1939 cc8021af 2021-10-12 thomas struct got_tree_object *tree = NULL;
1941 cc8021af 2021-10-12 thomas *id = NULL;
1943 cc8021af 2021-10-12 thomas /* Handle opening of root of commit's tree. */
1944 cc8021af 2021-10-12 thomas if (got_path_is_root_dir(path)) {
1945 cc8021af 2021-10-12 thomas *id = got_object_id_dup(commit->tree_id);
1946 cc8021af 2021-10-12 thomas if (*id == NULL)
1947 cc8021af 2021-10-12 thomas err = got_error_from_errno("got_object_id_dup");
1948 cc8021af 2021-10-12 thomas } else {
1949 cc8021af 2021-10-12 thomas err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1950 cc8021af 2021-10-12 thomas if (err)
1951 cc8021af 2021-10-12 thomas goto done;
1952 cc8021af 2021-10-12 thomas err = got_object_tree_find_path(id, NULL, repo, tree, path);
1956 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
1957 776d4d29 2018-06-17 stsp return err;
1961 ac5f2b26 2020-05-05 stsp * Normalize file mode bits to avoid false positive tree entry differences
1962 ac5f2b26 2020-05-05 stsp * in case tree entries have unexpected mode bits set.
1964 ac5f2b26 2020-05-05 stsp static mode_t
1965 ac5f2b26 2020-05-05 stsp normalize_mode_for_comparison(mode_t mode)
1968 ac5f2b26 2020-05-05 stsp * For directories, the only relevant bit is the IFDIR bit.
1969 ac5f2b26 2020-05-05 stsp * This allows us to detect paths changing from a directory
1970 ac5f2b26 2020-05-05 stsp * to a file and vice versa.
1972 ac5f2b26 2020-05-05 stsp if (S_ISDIR(mode))
1973 ac5f2b26 2020-05-05 stsp return mode & S_IFDIR;
1976 40dde666 2020-07-23 stsp * For symlinks, the only relevant bit is the IFLNK bit.
1977 40dde666 2020-07-23 stsp * This allows us to detect paths changing from a symlinks
1978 40dde666 2020-07-23 stsp * to a file or directory and vice versa.
1980 40dde666 2020-07-23 stsp if (S_ISLNK(mode))
1981 40dde666 2020-07-23 stsp return mode & S_IFLNK;
1983 ac5f2b26 2020-05-05 stsp /* For files, the only change we care about is the executable bit. */
1984 ac5f2b26 2020-05-05 stsp return mode & S_IXUSR;
1987 07862c20 2018-09-15 stsp const struct got_error *
1988 07862c20 2018-09-15 stsp got_object_tree_path_changed(int *changed,
1989 07862c20 2018-09-15 stsp struct got_tree_object *tree01, struct got_tree_object *tree02,
1990 07862c20 2018-09-15 stsp const char *path, struct got_repository *repo)
1992 07862c20 2018-09-15 stsp const struct got_error *err = NULL;
1993 07862c20 2018-09-15 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1994 07862c20 2018-09-15 stsp struct got_tree_entry *te1 = NULL, *te2 = NULL;
1995 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1996 3b7f9878 2018-11-18 stsp size_t seglen;
1998 07862c20 2018-09-15 stsp *changed = 0;
2000 07862c20 2018-09-15 stsp /* We not do support comparing the root path. */
2001 61a7d79f 2020-02-29 stsp if (got_path_is_root_dir(path))
2002 63f810e6 2020-02-29 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
2004 07862c20 2018-09-15 stsp tree1 = tree01;
2005 07862c20 2018-09-15 stsp tree2 = tree02;
2007 61a7d79f 2020-02-29 stsp while (*s == '/')
2010 65a9bbe9 2018-09-15 stsp seglen = 0;
2011 3b7f9878 2018-11-18 stsp while (*s) {
2012 07862c20 2018-09-15 stsp struct got_tree_object *next_tree1, *next_tree2;
2013 ac5f2b26 2020-05-05 stsp mode_t mode1, mode2;
2015 07862c20 2018-09-15 stsp if (*s != '/') {
2022 65a9bbe9 2018-09-15 stsp te1 = find_entry_by_name(tree1, seg, seglen);
2023 07862c20 2018-09-15 stsp if (te1 == NULL) {
2024 07862c20 2018-09-15 stsp err = got_error(GOT_ERR_NO_OBJ);
2025 07862c20 2018-09-15 stsp goto done;
2028 e8bfb8f3 2020-12-18 stsp if (tree2)
2029 e8bfb8f3 2020-12-18 stsp te2 = find_entry_by_name(tree2, seg, seglen);
2031 e8bfb8f3 2020-12-18 stsp if (te2) {
2032 e8bfb8f3 2020-12-18 stsp mode1 = normalize_mode_for_comparison(te1->mode);
2033 e8bfb8f3 2020-12-18 stsp mode2 = normalize_mode_for_comparison(te2->mode);
2034 e8bfb8f3 2020-12-18 stsp if (mode1 != mode2) {
2035 e8bfb8f3 2020-12-18 stsp *changed = 1;
2036 e8bfb8f3 2020-12-18 stsp goto done;
2039 e8bfb8f3 2020-12-18 stsp if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2040 e8bfb8f3 2020-12-18 stsp *changed = 0;
2041 e8bfb8f3 2020-12-18 stsp goto done;
2045 3b7f9878 2018-11-18 stsp if (*s == '\0') { /* final path element */
2046 07862c20 2018-09-15 stsp *changed = 1;
2047 07862c20 2018-09-15 stsp goto done;
2050 07862c20 2018-09-15 stsp seg = s + 1;
2052 65a9bbe9 2018-09-15 stsp seglen = 0;
2054 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree1, repo,
2055 56e0773d 2019-11-28 stsp &te1->id);
2056 07862c20 2018-09-15 stsp te1 = NULL;
2058 07862c20 2018-09-15 stsp goto done;
2059 a31cea73 2018-09-15 stsp if (tree1 != tree01)
2060 a31cea73 2018-09-15 stsp got_object_tree_close(tree1);
2061 07862c20 2018-09-15 stsp tree1 = next_tree1;
2063 e8bfb8f3 2020-12-18 stsp if (te2) {
2064 e8bfb8f3 2020-12-18 stsp err = got_object_open_as_tree(&next_tree2, repo,
2065 e8bfb8f3 2020-12-18 stsp &te2->id);
2066 e8bfb8f3 2020-12-18 stsp te2 = NULL;
2068 e8bfb8f3 2020-12-18 stsp goto done;
2069 e8bfb8f3 2020-12-18 stsp if (tree2 != tree02)
2070 e8bfb8f3 2020-12-18 stsp got_object_tree_close(tree2);
2071 e8bfb8f3 2020-12-18 stsp tree2 = next_tree2;
2072 e8bfb8f3 2020-12-18 stsp } else if (tree2) {
2073 e8bfb8f3 2020-12-18 stsp if (tree2 != tree02)
2074 e8bfb8f3 2020-12-18 stsp got_object_tree_close(tree2);
2075 e8bfb8f3 2020-12-18 stsp tree2 = NULL;
2080 a31cea73 2018-09-15 stsp if (tree1 && tree1 != tree01)
2081 07862c20 2018-09-15 stsp got_object_tree_close(tree1);
2082 a31cea73 2018-09-15 stsp if (tree2 && tree2 != tree02)
2083 07862c20 2018-09-15 stsp got_object_tree_close(tree2);
2084 77880158 2018-11-04 stsp return err;
2087 ed175427 2019-05-09 stsp const struct got_error *
2088 ed175427 2019-05-09 stsp got_object_tree_entry_dup(struct got_tree_entry **new_te,
2089 ed175427 2019-05-09 stsp struct got_tree_entry *te)
2091 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
2093 ed175427 2019-05-09 stsp *new_te = calloc(1, sizeof(**new_te));
2094 ed175427 2019-05-09 stsp if (*new_te == NULL)
2095 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
2097 ed175427 2019-05-09 stsp (*new_te)->mode = te->mode;
2098 56e0773d 2019-11-28 stsp memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2099 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2100 8c4eabf2 2019-05-10 stsp return err;
2104 56e0773d 2019-11-28 stsp got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2106 63c5ca5d 2019-08-24 stsp return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2110 e40622f4 2020-07-23 stsp got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2112 e40622f4 2020-07-23 stsp /* S_IFDIR check avoids confusing symlinks with submodules. */
2113 e40622f4 2020-07-23 stsp return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2116 e40622f4 2020-07-23 stsp static const struct got_error *
2117 e40622f4 2020-07-23 stsp resolve_symlink(char **link_target, const char *path,
2118 945f9229 2022-04-16 thomas struct got_commit_object *commit, struct got_repository *repo)
2120 e40622f4 2020-07-23 stsp const struct got_error *err = NULL;
2121 dbdd6209 2020-10-19 stsp char buf[PATH_MAX];
2122 e40622f4 2020-07-23 stsp char *name, *parent_path = NULL;
2123 e40622f4 2020-07-23 stsp struct got_object_id *tree_obj_id = NULL;
2124 e40622f4 2020-07-23 stsp struct got_tree_object *tree = NULL;
2125 e40622f4 2020-07-23 stsp struct got_tree_entry *te = NULL;
2127 e40622f4 2020-07-23 stsp *link_target = NULL;
2129 dbdd6209 2020-10-19 stsp if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2130 dbdd6209 2020-10-19 stsp return got_error(GOT_ERR_NO_SPACE);
2132 dbdd6209 2020-10-19 stsp name = basename(buf);
2133 e40622f4 2020-07-23 stsp if (name == NULL)
2134 e40622f4 2020-07-23 stsp return got_error_from_errno2("basename", path);
2136 e40622f4 2020-07-23 stsp err = got_path_dirname(&parent_path, path);
2138 e40622f4 2020-07-23 stsp return err;
2140 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_obj_id, repo, commit,
2141 e40622f4 2020-07-23 stsp parent_path);
2142 e40622f4 2020-07-23 stsp if (err) {
2143 e40622f4 2020-07-23 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2144 e40622f4 2020-07-23 stsp /* Display the complete path in error message. */
2145 e40622f4 2020-07-23 stsp err = got_error_path(path, err->code);
2147 e40622f4 2020-07-23 stsp goto done;
2150 e40622f4 2020-07-23 stsp err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2152 e40622f4 2020-07-23 stsp goto done;
2154 e40622f4 2020-07-23 stsp te = got_object_tree_find_entry(tree, name);
2155 e40622f4 2020-07-23 stsp if (te == NULL) {
2156 e40622f4 2020-07-23 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2157 e40622f4 2020-07-23 stsp goto done;
2160 e40622f4 2020-07-23 stsp if (got_object_tree_entry_is_symlink(te)) {
2161 e40622f4 2020-07-23 stsp err = got_tree_entry_get_symlink_target(link_target, te, repo);
2163 e40622f4 2020-07-23 stsp goto done;
2164 e40622f4 2020-07-23 stsp if (!got_path_is_absolute(*link_target)) {
2165 e40622f4 2020-07-23 stsp char *abspath;
2166 e40622f4 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", parent_path,
2167 e40622f4 2020-07-23 stsp *link_target) == -1) {
2168 e40622f4 2020-07-23 stsp err = got_error_from_errno("asprintf");
2169 e40622f4 2020-07-23 stsp goto done;
2171 e40622f4 2020-07-23 stsp free(*link_target);
2172 e40622f4 2020-07-23 stsp *link_target = malloc(PATH_MAX);
2173 e40622f4 2020-07-23 stsp if (*link_target == NULL) {
2174 e40622f4 2020-07-23 stsp err = got_error_from_errno("malloc");
2175 e40622f4 2020-07-23 stsp goto done;
2177 e40622f4 2020-07-23 stsp err = got_canonpath(abspath, *link_target, PATH_MAX);
2178 e40622f4 2020-07-23 stsp free(abspath);
2180 e40622f4 2020-07-23 stsp goto done;
2184 e40622f4 2020-07-23 stsp free(tree_obj_id);
2186 e40622f4 2020-07-23 stsp got_object_tree_close(tree);
2187 e40622f4 2020-07-23 stsp if (err) {
2188 e40622f4 2020-07-23 stsp free(*link_target);
2189 e40622f4 2020-07-23 stsp *link_target = NULL;
2191 e40622f4 2020-07-23 stsp return err;
2194 ca6e02ac 2020-01-07 stsp const struct got_error *
2195 e40622f4 2020-07-23 stsp got_object_resolve_symlinks(char **link_target, const char *path,
2196 945f9229 2022-04-16 thomas struct got_commit_object *commit, struct got_repository *repo)
2198 e40622f4 2020-07-23 stsp const struct got_error *err = NULL;
2199 e40622f4 2020-07-23 stsp char *next_target = NULL;
2200 e40622f4 2020-07-23 stsp int max_recursion = 40; /* matches Git */
2202 e40622f4 2020-07-23 stsp *link_target = NULL;
2205 e40622f4 2020-07-23 stsp err = resolve_symlink(&next_target,
2206 945f9229 2022-04-16 thomas *link_target ? *link_target : path, commit, repo);
2209 e40622f4 2020-07-23 stsp if (next_target) {
2210 e40622f4 2020-07-23 stsp free(*link_target);
2211 e40622f4 2020-07-23 stsp if (--max_recursion == 0) {
2212 e40622f4 2020-07-23 stsp err = got_error_path(path, GOT_ERR_RECURSION);
2213 e40622f4 2020-07-23 stsp *link_target = NULL;
2216 e40622f4 2020-07-23 stsp *link_target = next_target;
2218 e40622f4 2020-07-23 stsp } while (next_target);
2220 e40622f4 2020-07-23 stsp return err;
2223 e40622f4 2020-07-23 stsp const struct got_error *
2224 ca6e02ac 2020-01-07 stsp got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2225 ca6e02ac 2020-01-07 stsp struct got_object_id *commit_id, const char *path,
2226 ca6e02ac 2020-01-07 stsp struct got_repository *repo)
2228 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
2229 ca6e02ac 2020-01-07 stsp struct got_pack *pack = NULL;
2230 ca6e02ac 2020-01-07 stsp struct got_packidx *packidx = NULL;
2231 ca6e02ac 2020-01-07 stsp char *path_packfile = NULL;
2232 ca6e02ac 2020-01-07 stsp struct got_commit_object *changed_commit = NULL;
2233 ca6e02ac 2020-01-07 stsp struct got_object_id *changed_commit_id = NULL;
2236 ca6e02ac 2020-01-07 stsp err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2237 ca6e02ac 2020-01-07 stsp if (err) {
2238 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
2239 ca6e02ac 2020-01-07 stsp return err;
2240 ca6e02ac 2020-01-07 stsp return NULL;
2243 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
2244 aea75d87 2021-07-06 stsp packidx->path_packidx);
2246 ca6e02ac 2020-01-07 stsp return err;
2248 ca6e02ac 2020-01-07 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
2249 ca6e02ac 2020-01-07 stsp if (pack == NULL) {
2250 ca6e02ac 2020-01-07 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2252 ca6e02ac 2020-01-07 stsp goto done;
2255 ca6e02ac 2020-01-07 stsp if (pack->privsep_child == NULL) {
2256 68036464 2022-06-26 thomas err = got_pack_start_privsep_child(pack, packidx);
2258 ca6e02ac 2020-01-07 stsp goto done;
2261 ca6e02ac 2020-01-07 stsp err = got_privsep_send_commit_traversal_request(
2262 ca6e02ac 2020-01-07 stsp pack->privsep_child->ibuf, commit_id, idx, path);
2264 ca6e02ac 2020-01-07 stsp goto done;
2266 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_traversed_commits(&changed_commit,
2267 ca6e02ac 2020-01-07 stsp &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2269 ca6e02ac 2020-01-07 stsp goto done;
2271 ca6e02ac 2020-01-07 stsp if (changed_commit) {
2273 ca6e02ac 2020-01-07 stsp * Cache the commit in which the path was changed.
2274 ca6e02ac 2020-01-07 stsp * This commit might be opened again soon.
2276 ca6e02ac 2020-01-07 stsp changed_commit->refcnt++;
2277 ca6e02ac 2020-01-07 stsp err = got_repo_cache_commit(repo, changed_commit_id,
2278 ca6e02ac 2020-01-07 stsp changed_commit);
2279 ca6e02ac 2020-01-07 stsp got_object_commit_close(changed_commit);
2282 ca6e02ac 2020-01-07 stsp free(path_packfile);
2283 ca6e02ac 2020-01-07 stsp free(changed_commit_id);
2284 ca6e02ac 2020-01-07 stsp return err;
2287 63915ee5 2022-06-23 thomas const struct got_error *
2288 e71f1e62 2022-06-23 thomas got_object_enumerate(int *found_all_objects,
2289 e71f1e62 2022-06-23 thomas got_object_enumerate_commit_cb cb_commit,
2290 63915ee5 2022-06-23 thomas got_object_enumerate_tree_cb cb_tree, void *cb_arg,
2291 63915ee5 2022-06-23 thomas struct got_object_id **ours, int nours,
2292 63915ee5 2022-06-23 thomas struct got_object_id **theirs, int ntheirs,
2293 63915ee5 2022-06-23 thomas struct got_packidx *packidx, struct got_repository *repo)
2295 63915ee5 2022-06-23 thomas const struct got_error *err = NULL;
2296 63915ee5 2022-06-23 thomas struct got_pack *pack;
2297 63915ee5 2022-06-23 thomas char *path_packfile = NULL;
2299 63915ee5 2022-06-23 thomas err = got_packidx_get_packfile_path(&path_packfile,
2300 63915ee5 2022-06-23 thomas packidx->path_packidx);
2301 63915ee5 2022-06-23 thomas if (err)
2302 63915ee5 2022-06-23 thomas return err;
2304 63915ee5 2022-06-23 thomas pack = got_repo_get_cached_pack(repo, path_packfile);
2305 63915ee5 2022-06-23 thomas if (pack == NULL) {
2306 63915ee5 2022-06-23 thomas err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2307 63915ee5 2022-06-23 thomas if (err)
2308 63915ee5 2022-06-23 thomas goto done;
2311 63915ee5 2022-06-23 thomas if (pack->privsep_child == NULL) {
2312 68036464 2022-06-26 thomas err = got_pack_start_privsep_child(pack, packidx);
2313 63915ee5 2022-06-23 thomas if (err)
2314 63915ee5 2022-06-23 thomas goto done;
2317 63915ee5 2022-06-23 thomas err = got_privsep_send_object_enumeration_request(
2318 63915ee5 2022-06-23 thomas pack->privsep_child->ibuf);
2319 63915ee5 2022-06-23 thomas if (err)
2320 63915ee5 2022-06-23 thomas goto done;
2322 63915ee5 2022-06-23 thomas err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
2323 63915ee5 2022-06-23 thomas ours, nours);
2324 63915ee5 2022-06-23 thomas if (err)
2325 63915ee5 2022-06-23 thomas goto done;
2326 63915ee5 2022-06-23 thomas err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
2327 63915ee5 2022-06-23 thomas if (err)
2328 63915ee5 2022-06-23 thomas goto done;
2330 63915ee5 2022-06-23 thomas err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
2331 63915ee5 2022-06-23 thomas theirs, ntheirs);
2332 63915ee5 2022-06-23 thomas if (err)
2333 63915ee5 2022-06-23 thomas goto done;
2334 63915ee5 2022-06-23 thomas err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
2335 63915ee5 2022-06-23 thomas if (err)
2336 63915ee5 2022-06-23 thomas goto done;
2338 e71f1e62 2022-06-23 thomas err = got_privsep_recv_enumerated_objects(found_all_objects,
2339 e71f1e62 2022-06-23 thomas pack->privsep_child->ibuf, cb_commit, cb_tree, cb_arg, repo);
2341 63915ee5 2022-06-23 thomas free(path_packfile);
2342 63915ee5 2022-06-23 thomas return err;