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 d1cda826 2017-11-06 stsp #include <sys/queue.h>
20 f8b19efd 2021-10-13 stsp #include <sys/tree.h>
21 2178c42e 2018-04-22 stsp #include <sys/uio.h>
22 2178c42e 2018-04-22 stsp #include <sys/socket.h>
23 2178c42e 2018-04-22 stsp #include <sys/wait.h>
24 b48e2ddb 2019-05-22 stsp #include <sys/resource.h>
25 64a8571e 2022-01-07 stsp #include <sys/mman.h>
27 a1fd68d8 2018-01-12 stsp #include <errno.h>
28 2178c42e 2018-04-22 stsp #include <fcntl.h>
29 d71d75ad 2017-11-05 stsp #include <stdio.h>
30 ab9a70b2 2017-11-06 stsp #include <stdlib.h>
31 ab9a70b2 2017-11-06 stsp #include <string.h>
32 2178c42e 2018-04-22 stsp #include <stdint.h>
33 d71d75ad 2017-11-05 stsp #include <sha1.h>
34 81a12da5 2020-09-09 naddy #include <unistd.h>
35 ab9a70b2 2017-11-06 stsp #include <zlib.h>
36 ab9a70b2 2017-11-06 stsp #include <ctype.h>
37 e40622f4 2020-07-23 stsp #include <libgen.h>
38 ab9a70b2 2017-11-06 stsp #include <limits.h>
39 2178c42e 2018-04-22 stsp #include <imsg.h>
40 788c352e 2018-06-16 stsp #include <time.h>
42 ab9a70b2 2017-11-06 stsp #include "got_error.h"
43 d71d75ad 2017-11-05 stsp #include "got_object.h"
44 ab9a70b2 2017-11-06 stsp #include "got_repository.h"
45 511a516b 2018-05-19 stsp #include "got_opentemp.h"
46 324d37e7 2019-05-11 stsp #include "got_path.h"
48 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
49 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
50 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
51 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
52 2178c42e 2018-04-22 stsp #include "got_lib_privsep.h"
53 6bef87be 2018-09-11 stsp #include "got_lib_object_idcache.h"
54 6bef87be 2018-09-11 stsp #include "got_lib_object_cache.h"
55 ad242220 2018-09-08 stsp #include "got_lib_object_parse.h"
56 15a94983 2018-12-23 stsp #include "got_lib_pack.h"
57 7bb0daa1 2018-06-21 stsp #include "got_lib_repository.h"
60 ab9a70b2 2017-11-06 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 0ab4c957 2022-06-13 stsp #ifndef nitems
64 0ab4c957 2022-06-13 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
67 3235492e 2018-04-01 stsp struct got_object_id *
68 3235492e 2018-04-01 stsp got_object_get_id(struct got_object *obj)
70 6402fb3c 2018-09-15 stsp return &obj->id;
73 bacc9935 2018-05-20 stsp const struct got_error *
74 bacc9935 2018-05-20 stsp got_object_get_id_str(char **outbuf, struct got_object *obj)
76 bacc9935 2018-05-20 stsp return got_object_id_str(outbuf, &obj->id);
79 15a94983 2018-12-23 stsp const struct got_error *
80 15a94983 2018-12-23 stsp got_object_get_type(int *type, struct got_repository *repo,
81 15a94983 2018-12-23 stsp struct got_object_id *id)
83 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
84 15a94983 2018-12-23 stsp struct got_object *obj;
86 15a94983 2018-12-23 stsp err = got_object_open(&obj, repo, id);
90 b107e67f 2018-01-19 stsp switch (obj->type) {
91 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_COMMIT:
92 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_TREE:
93 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_BLOB:
94 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
95 15a94983 2018-12-23 stsp *type = obj->type;
98 15a94983 2018-12-23 stsp err = got_error(GOT_ERR_OBJ_TYPE);
102 15a94983 2018-12-23 stsp got_object_close(obj);
103 15a94983 2018-12-23 stsp return err;
106 90bdb554 2019-04-11 stsp const struct got_error *
107 90bdb554 2019-04-11 stsp got_object_get_path(char **path, struct got_object_id *id,
108 90bdb554 2019-04-11 stsp struct got_repository *repo)
110 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
111 7a132809 2018-07-23 stsp char *hex = NULL;
112 41d2888b 2019-08-11 stsp char *path_objects;
114 e6b1056e 2018-04-22 stsp *path = NULL;
116 41d2888b 2019-08-11 stsp path_objects = got_repo_get_path_objects(repo);
117 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
118 638f9024 2019-05-13 stsp return got_error_from_errno("got_repo_get_path_objects");
120 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
124 d1cda826 2017-11-06 stsp if (asprintf(path, "%s/%.2x/%s", path_objects,
125 d1cda826 2017-11-06 stsp id->sha1[0], hex + 2) == -1)
126 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
130 d1cda826 2017-11-06 stsp free(path_objects);
131 d1cda826 2017-11-06 stsp return err;
134 762d73f4 2021-04-10 stsp const struct got_error *
135 762d73f4 2021-04-10 stsp got_object_open_loose_fd(int *fd, struct got_object_id *id,
136 4796fb13 2018-12-23 stsp struct got_repository *repo)
138 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
139 a1fd68d8 2018-01-12 stsp char *path;
141 90bdb554 2019-04-11 stsp err = got_object_get_path(&path, id, repo);
143 d1cda826 2017-11-06 stsp return err;
144 8bd0cdad 2021-12-31 stsp *fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
145 d5003b79 2018-04-22 stsp if (*fd == -1) {
146 e82b1d81 2019-07-27 stsp err = got_error_from_errno2("open", path);
150 4558fcd4 2018-01-14 stsp free(path);
151 2090a03d 2018-09-09 stsp return err;
154 2090a03d 2018-09-09 stsp static const struct got_error *
155 a158c901 2018-12-23 stsp request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
156 a158c901 2018-12-23 stsp struct got_object_id *id)
158 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
159 a158c901 2018-12-23 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
161 a158c901 2018-12-23 stsp err = got_privsep_send_packed_obj_req(ibuf, idx, id);
163 a158c901 2018-12-23 stsp return err;
165 a158c901 2018-12-23 stsp err = got_privsep_recv_obj(obj, ibuf);
167 a158c901 2018-12-23 stsp return err;
169 a158c901 2018-12-23 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
171 a158c901 2018-12-23 stsp return NULL;
174 db696021 2022-01-04 stsp /* Create temporary files used during delta application. */
175 59d1e4a0 2021-03-10 stsp static const struct got_error *
176 db696021 2022-01-04 stsp pack_child_send_tempfiles(struct imsgbuf *ibuf, struct got_pack *pack)
178 db696021 2022-01-04 stsp const struct got_error *err;
179 57160834 2022-05-31 stsp int basefd = -1, accumfd = -1;
182 db696021 2022-01-04 stsp * For performance reasons, the child will keep reusing the
183 db696021 2022-01-04 stsp * same temporary files during every object request.
184 db696021 2022-01-04 stsp * Opening and closing new files for every object request is
185 db696021 2022-01-04 stsp * too expensive during operations such as 'gotadmin pack'.
187 db696021 2022-01-04 stsp if (pack->child_has_tempfiles)
188 db696021 2022-01-04 stsp return NULL;
190 57160834 2022-05-31 stsp basefd = dup(pack->basefd);
191 59d1e4a0 2021-03-10 stsp if (basefd == -1)
192 57160834 2022-05-31 stsp return got_error_from_errno("dup");
194 57160834 2022-05-31 stsp accumfd = dup(pack->accumfd);
195 57160834 2022-05-31 stsp if (accumfd == -1) {
196 57160834 2022-05-31 stsp err = got_error_from_errno("dup");
200 db696021 2022-01-04 stsp err = got_privsep_send_tmpfd(ibuf, basefd);
204 db696021 2022-01-04 stsp err = got_privsep_send_tmpfd(ibuf, accumfd);
207 57160834 2022-05-31 stsp if (basefd != -1)
208 57160834 2022-05-31 stsp close(basefd);
209 57160834 2022-05-31 stsp if (accumfd != -1)
210 57160834 2022-05-31 stsp close(accumfd);
212 57160834 2022-05-31 stsp pack->child_has_tempfiles = 1;
213 db696021 2022-01-04 stsp return NULL;
216 db696021 2022-01-04 stsp static const struct got_error *
217 db696021 2022-01-04 stsp request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
218 db696021 2022-01-04 stsp int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
220 db696021 2022-01-04 stsp const struct got_error *err = NULL;
221 db696021 2022-01-04 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
222 db696021 2022-01-04 stsp int outfd_child;
224 db696021 2022-01-04 stsp err = pack_child_send_tempfiles(ibuf, pack);
226 db696021 2022-01-04 stsp return err;
228 db696021 2022-01-04 stsp outfd_child = dup(outfd);
229 db696021 2022-01-04 stsp if (outfd_child == -1)
230 db696021 2022-01-04 stsp return got_error_from_errno("dup");
232 59d1e4a0 2021-03-10 stsp err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
234 59d1e4a0 2021-03-10 stsp close(outfd_child);
235 59d1e4a0 2021-03-10 stsp return err;
238 59d1e4a0 2021-03-10 stsp err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
240 59d1e4a0 2021-03-10 stsp return err;
242 59d1e4a0 2021-03-10 stsp err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
244 59d1e4a0 2021-03-10 stsp return err;
246 59d1e4a0 2021-03-10 stsp return NULL;
249 da506691 2019-05-22 stsp static void
250 b48e2ddb 2019-05-22 stsp set_max_datasize(void)
252 b48e2ddb 2019-05-22 stsp struct rlimit rl;
254 b48e2ddb 2019-05-22 stsp if (getrlimit(RLIMIT_DATA, &rl) != 0)
257 b48e2ddb 2019-05-22 stsp rl.rlim_cur = rl.rlim_max;
258 b48e2ddb 2019-05-22 stsp setrlimit(RLIMIT_DATA, &rl);
261 a158c901 2018-12-23 stsp static const struct got_error *
262 711fb6e8 2018-12-23 stsp start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
264 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
265 a158c901 2018-12-23 stsp int imsg_fds[2];
267 a158c901 2018-12-23 stsp struct imsgbuf *ibuf;
269 a158c901 2018-12-23 stsp ibuf = calloc(1, sizeof(*ibuf));
270 a158c901 2018-12-23 stsp if (ibuf == NULL)
271 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
273 a158c901 2018-12-23 stsp pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
274 a158c901 2018-12-23 stsp if (pack->privsep_child == NULL) {
275 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
276 a158c901 2018-12-23 stsp free(ibuf);
277 a158c901 2018-12-23 stsp return err;
279 db696021 2022-01-04 stsp pack->child_has_tempfiles = 0;
280 67fd6849 2022-02-13 stsp pack->child_has_delta_outfd = 0;
282 a158c901 2018-12-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
283 638f9024 2019-05-13 stsp err = got_error_from_errno("socketpair");
287 a158c901 2018-12-23 stsp pid = fork();
288 a158c901 2018-12-23 stsp if (pid == -1) {
289 638f9024 2019-05-13 stsp err = got_error_from_errno("fork");
291 a158c901 2018-12-23 stsp } else if (pid == 0) {
292 b48e2ddb 2019-05-22 stsp set_max_datasize();
293 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
294 a158c901 2018-12-23 stsp pack->path_packfile);
295 a158c901 2018-12-23 stsp /* not reached */
298 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1)
299 638f9024 2019-05-13 stsp return got_error_from_errno("close");
300 a158c901 2018-12-23 stsp pack->privsep_child->imsg_fd = imsg_fds[0];
301 a158c901 2018-12-23 stsp pack->privsep_child->pid = pid;
302 a158c901 2018-12-23 stsp imsg_init(ibuf, imsg_fds[0]);
303 a158c901 2018-12-23 stsp pack->privsep_child->ibuf = ibuf;
305 a158c901 2018-12-23 stsp err = got_privsep_init_pack_child(ibuf, pack, packidx);
307 a158c901 2018-12-23 stsp const struct got_error *child_err;
308 a158c901 2018-12-23 stsp err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
309 a158c901 2018-12-23 stsp child_err = got_privsep_wait_for_child(
310 a158c901 2018-12-23 stsp pack->privsep_child->pid);
311 a158c901 2018-12-23 stsp if (child_err && err == NULL)
312 a158c901 2018-12-23 stsp err = child_err;
316 a158c901 2018-12-23 stsp free(ibuf);
317 a158c901 2018-12-23 stsp free(pack->privsep_child);
318 a158c901 2018-12-23 stsp pack->privsep_child = NULL;
320 a158c901 2018-12-23 stsp return err;
323 711fb6e8 2018-12-23 stsp static const struct got_error *
324 711fb6e8 2018-12-23 stsp read_packed_object_privsep(struct got_object **obj,
325 711fb6e8 2018-12-23 stsp struct got_repository *repo, struct got_pack *pack,
326 711fb6e8 2018-12-23 stsp struct got_packidx *packidx, int idx, struct got_object_id *id)
328 711fb6e8 2018-12-23 stsp const struct got_error *err = NULL;
330 59d1e4a0 2021-03-10 stsp if (pack->privsep_child == NULL) {
331 59d1e4a0 2021-03-10 stsp err = start_pack_privsep_child(pack, packidx);
333 59d1e4a0 2021-03-10 stsp return err;
336 711fb6e8 2018-12-23 stsp return request_packed_object(obj, pack, idx, id);
339 59d1e4a0 2021-03-10 stsp static const struct got_error *
340 59d1e4a0 2021-03-10 stsp read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
341 59d1e4a0 2021-03-10 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
342 59d1e4a0 2021-03-10 stsp struct got_object_id *id)
344 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
346 59d1e4a0 2021-03-10 stsp if (pack->privsep_child == NULL) {
347 59d1e4a0 2021-03-10 stsp err = start_pack_privsep_child(pack, packidx);
349 59d1e4a0 2021-03-10 stsp return err;
352 59d1e4a0 2021-03-10 stsp return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
356 b3d68e7f 2021-07-03 stsp const struct got_error *
357 b3d68e7f 2021-07-03 stsp got_object_open_packed(struct got_object **obj, struct got_object_id *id,
358 2090a03d 2018-09-09 stsp struct got_repository *repo)
360 2090a03d 2018-09-09 stsp const struct got_error *err = NULL;
361 2090a03d 2018-09-09 stsp struct got_pack *pack = NULL;
362 2090a03d 2018-09-09 stsp struct got_packidx *packidx = NULL;
364 2090a03d 2018-09-09 stsp char *path_packfile;
366 2090a03d 2018-09-09 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
368 2090a03d 2018-09-09 stsp return err;
370 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
371 aea75d87 2021-07-06 stsp packidx->path_packidx);
373 2090a03d 2018-09-09 stsp return err;
375 2090a03d 2018-09-09 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
376 2090a03d 2018-09-09 stsp if (pack == NULL) {
377 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
382 a158c901 2018-12-23 stsp err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
386 2090a03d 2018-09-09 stsp free(path_packfile);
387 4558fcd4 2018-01-14 stsp return err;
390 67fd6849 2022-02-13 stsp const struct got_error *
391 67fd6849 2022-02-13 stsp got_object_open_from_packfile(struct got_object **obj, struct got_object_id *id,
392 67fd6849 2022-02-13 stsp struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
393 67fd6849 2022-02-13 stsp struct got_repository *repo)
395 67fd6849 2022-02-13 stsp return read_packed_object_privsep(obj, repo, pack, packidx,
396 67fd6849 2022-02-13 stsp obj_idx, id);
399 67fd6849 2022-02-13 stsp const struct got_error *
400 67fd6849 2022-02-13 stsp got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
401 2d9e6abf 2022-05-04 stsp off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
402 2d9e6abf 2022-05-04 stsp off_t *delta_out_offset, struct got_object_id **base_id, int delta_cache_fd,
403 67fd6849 2022-02-13 stsp struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
404 67fd6849 2022-02-13 stsp struct got_repository *repo)
406 67fd6849 2022-02-13 stsp const struct got_error *err = NULL;
407 67fd6849 2022-02-13 stsp struct got_pack *pack = NULL;
408 67fd6849 2022-02-13 stsp char *path_packfile;
410 67fd6849 2022-02-13 stsp *base_size = 0;
411 67fd6849 2022-02-13 stsp *result_size = 0;
412 67fd6849 2022-02-13 stsp *delta_size = 0;
413 2d9e6abf 2022-05-04 stsp *delta_compressed_size = 0;
414 67fd6849 2022-02-13 stsp *delta_offset = 0;
415 67fd6849 2022-02-13 stsp *delta_out_offset = 0;
417 67fd6849 2022-02-13 stsp err = got_packidx_get_packfile_path(&path_packfile,
418 67fd6849 2022-02-13 stsp packidx->path_packidx);
420 67fd6849 2022-02-13 stsp return err;
422 67fd6849 2022-02-13 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
423 67fd6849 2022-02-13 stsp if (pack == NULL) {
424 67fd6849 2022-02-13 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
426 67fd6849 2022-02-13 stsp return err;
429 67fd6849 2022-02-13 stsp if (pack->privsep_child == NULL) {
430 67fd6849 2022-02-13 stsp err = start_pack_privsep_child(pack, packidx);
432 67fd6849 2022-02-13 stsp return err;
435 67fd6849 2022-02-13 stsp if (!pack->child_has_delta_outfd) {
436 67fd6849 2022-02-13 stsp int outfd_child;
437 67fd6849 2022-02-13 stsp outfd_child = dup(delta_cache_fd);
438 67fd6849 2022-02-13 stsp if (outfd_child == -1)
439 67fd6849 2022-02-13 stsp return got_error_from_errno("dup");
440 67fd6849 2022-02-13 stsp err = got_privsep_send_raw_delta_outfd(
441 67fd6849 2022-02-13 stsp pack->privsep_child->ibuf, outfd_child);
443 67fd6849 2022-02-13 stsp return err;
444 67fd6849 2022-02-13 stsp pack->child_has_delta_outfd = 1;
447 67fd6849 2022-02-13 stsp err = got_privsep_send_raw_delta_req(pack->privsep_child->ibuf,
448 67fd6849 2022-02-13 stsp obj_idx, id);
450 67fd6849 2022-02-13 stsp return err;
452 67fd6849 2022-02-13 stsp return got_privsep_recv_raw_delta(base_size, result_size, delta_size,
453 2d9e6abf 2022-05-04 stsp delta_compressed_size, delta_offset, delta_out_offset, base_id,
454 2d9e6abf 2022-05-04 stsp pack->privsep_child->ibuf);
458 fae7e038 2022-05-07 stsp * XXX This function does not really belong in object.c. It is only here
459 fae7e038 2022-05-07 stsp * because it needs start_pack_privsep_child(); relevant code should
460 fae7e038 2022-05-07 stsp * probably be moved to pack.c/pack_create.c.
462 fae7e038 2022-05-07 stsp const struct got_error *
463 fae7e038 2022-05-07 stsp got_object_prepare_delta_reuse(struct got_pack **pack,
464 fae7e038 2022-05-07 stsp struct got_packidx *packidx, int delta_outfd, struct got_repository *repo)
466 fae7e038 2022-05-07 stsp const struct got_error *err = NULL;
467 fae7e038 2022-05-07 stsp char *path_packfile = NULL;
469 fae7e038 2022-05-07 stsp err = got_packidx_get_packfile_path(&path_packfile,
470 fae7e038 2022-05-07 stsp packidx->path_packidx);
472 fae7e038 2022-05-07 stsp return err;
474 fae7e038 2022-05-07 stsp *pack = got_repo_get_cached_pack(repo, path_packfile);
475 fae7e038 2022-05-07 stsp if (*pack == NULL) {
476 fae7e038 2022-05-07 stsp err = got_repo_cache_pack(pack, repo, path_packfile, packidx);
480 fae7e038 2022-05-07 stsp if ((*pack)->privsep_child == NULL) {
481 fae7e038 2022-05-07 stsp err = start_pack_privsep_child(*pack, packidx);
486 fae7e038 2022-05-07 stsp if (!(*pack)->child_has_delta_outfd) {
487 fae7e038 2022-05-07 stsp int outfd_child;
488 fae7e038 2022-05-07 stsp outfd_child = dup(delta_outfd);
489 fae7e038 2022-05-07 stsp if (outfd_child == -1) {
490 fae7e038 2022-05-07 stsp err = got_error_from_errno("dup");
493 fae7e038 2022-05-07 stsp err = got_privsep_send_raw_delta_outfd(
494 fae7e038 2022-05-07 stsp (*pack)->privsep_child->ibuf, outfd_child);
497 fae7e038 2022-05-07 stsp (*pack)->child_has_delta_outfd = 1;
500 fae7e038 2022-05-07 stsp err = got_privsep_send_delta_reuse_req((*pack)->privsep_child->ibuf);
502 fae7e038 2022-05-07 stsp free(path_packfile);
503 fae7e038 2022-05-07 stsp return err;
506 9f2369b0 2018-12-24 stsp static const struct got_error *
507 d5c81d44 2021-07-08 stsp request_object(struct got_object **obj, struct got_object_id *id,
508 d5c81d44 2021-07-08 stsp struct got_repository *repo, int fd)
510 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
511 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
513 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
515 d5c81d44 2021-07-08 stsp err = got_privsep_send_obj_req(ibuf, fd, id);
517 9f2369b0 2018-12-24 stsp return err;
519 9f2369b0 2018-12-24 stsp return got_privsep_recv_obj(obj, ibuf);
522 9f2369b0 2018-12-24 stsp static const struct got_error *
523 59d1e4a0 2021-03-10 stsp request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
524 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo, int infd)
526 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
527 59d1e4a0 2021-03-10 stsp struct imsgbuf *ibuf;
528 59d1e4a0 2021-03-10 stsp int outfd_child;
530 59d1e4a0 2021-03-10 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
532 59d1e4a0 2021-03-10 stsp outfd_child = dup(outfd);
533 59d1e4a0 2021-03-10 stsp if (outfd_child == -1)
534 59d1e4a0 2021-03-10 stsp return got_error_from_errno("dup");
536 d5c81d44 2021-07-08 stsp err = got_privsep_send_raw_obj_req(ibuf, infd, id);
538 59d1e4a0 2021-03-10 stsp return err;
540 59d1e4a0 2021-03-10 stsp err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
542 59d1e4a0 2021-03-10 stsp return err;
544 59d1e4a0 2021-03-10 stsp return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
547 59d1e4a0 2021-03-10 stsp static const struct got_error *
548 59d1e4a0 2021-03-10 stsp start_read_object_child(struct got_repository *repo)
550 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
551 9f2369b0 2018-12-24 stsp int imsg_fds[2];
553 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
555 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
556 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
557 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
559 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
560 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
561 ddc7b220 2019-09-08 stsp free(ibuf);
562 ddc7b220 2019-09-08 stsp return err;
565 9f2369b0 2018-12-24 stsp pid = fork();
566 ddc7b220 2019-09-08 stsp if (pid == -1) {
567 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
568 ddc7b220 2019-09-08 stsp free(ibuf);
569 ddc7b220 2019-09-08 stsp return err;
571 9f2369b0 2018-12-24 stsp else if (pid == 0) {
572 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
573 9f2369b0 2018-12-24 stsp repo->path);
574 9f2369b0 2018-12-24 stsp /* not reached */
577 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
578 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
579 ddc7b220 2019-09-08 stsp free(ibuf);
580 ddc7b220 2019-09-08 stsp return err;
583 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
584 9f2369b0 2018-12-24 stsp imsg_fds[0];
585 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
586 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
587 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
589 59d1e4a0 2021-03-10 stsp return NULL;
592 b3d68e7f 2021-07-03 stsp const struct got_error *
593 b3d68e7f 2021-07-03 stsp got_object_read_header_privsep(struct got_object **obj,
594 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo, int obj_fd)
596 59d1e4a0 2021-03-10 stsp const struct got_error *err;
598 59d1e4a0 2021-03-10 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
599 d5c81d44 2021-07-08 stsp return request_object(obj, id, repo, obj_fd);
601 59d1e4a0 2021-03-10 stsp err = start_read_object_child(repo);
603 7495ec13 2021-04-04 stsp close(obj_fd);
604 59d1e4a0 2021-03-10 stsp return err;
607 d5c81d44 2021-07-08 stsp return request_object(obj, id, repo, obj_fd);
610 59d1e4a0 2021-03-10 stsp static const struct got_error *
611 59d1e4a0 2021-03-10 stsp read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
612 d5c81d44 2021-07-08 stsp int outfd, struct got_object_id *id, struct got_repository *repo,
613 d5c81d44 2021-07-08 stsp int obj_fd)
615 59d1e4a0 2021-03-10 stsp const struct got_error *err;
617 59d1e4a0 2021-03-10 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
618 d5c81d44 2021-07-08 stsp return request_raw_object(outbuf, size, hdrlen, outfd, id,
619 d5c81d44 2021-07-08 stsp repo, obj_fd);
621 59d1e4a0 2021-03-10 stsp err = start_read_object_child(repo);
623 59d1e4a0 2021-03-10 stsp return err;
625 d5c81d44 2021-07-08 stsp return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
629 4558fcd4 2018-01-14 stsp const struct got_error *
630 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
631 4558fcd4 2018-01-14 stsp struct got_object_id *id)
633 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
636 7bb0daa1 2018-06-21 stsp *obj = got_repo_get_cached_object(repo, id);
637 7bb0daa1 2018-06-21 stsp if (*obj != NULL) {
638 7bb0daa1 2018-06-21 stsp (*obj)->refcnt++;
639 7bb0daa1 2018-06-21 stsp return NULL;
642 b3d68e7f 2021-07-03 stsp err = got_object_open_packed(obj, id, repo);
643 e82b1d81 2019-07-27 stsp if (err && err->code != GOT_ERR_NO_OBJ)
644 e82b1d81 2019-07-27 stsp return err;
645 e82b1d81 2019-07-27 stsp if (*obj) {
646 e82b1d81 2019-07-27 stsp (*obj)->refcnt++;
647 e82b1d81 2019-07-27 stsp return got_repo_cache_object(repo, id, *obj);
650 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
652 762d73f4 2021-04-10 stsp if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
653 762d73f4 2021-04-10 stsp err = got_error_no_obj(id);
654 762d73f4 2021-04-10 stsp return err;
657 d5c81d44 2021-07-08 stsp err = got_object_read_header_privsep(obj, id, repo, fd);
659 4558fcd4 2018-01-14 stsp return err;
661 762d73f4 2021-04-10 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
663 59790a32 2018-09-15 stsp (*obj)->refcnt++;
664 762d73f4 2021-04-10 stsp return got_repo_cache_object(repo, id, *obj);
667 d3c116bf 2021-10-15 stsp /* *outfd must be initialized to -1 by caller */
668 59d1e4a0 2021-03-10 stsp const struct got_error *
669 d3c116bf 2021-10-15 stsp got_object_raw_open(struct got_raw_object **obj, int *outfd,
670 94dac27c 2021-10-15 stsp struct got_repository *repo, struct got_object_id *id)
672 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
673 59d1e4a0 2021-03-10 stsp struct got_packidx *packidx = NULL;
675 59d1e4a0 2021-03-10 stsp uint8_t *outbuf = NULL;
676 59d1e4a0 2021-03-10 stsp off_t size = 0;
677 59d1e4a0 2021-03-10 stsp size_t hdrlen = 0;
678 59d1e4a0 2021-03-10 stsp char *path_packfile = NULL;
680 d3c116bf 2021-10-15 stsp *obj = got_repo_get_cached_raw_object(repo, id);
681 d3c116bf 2021-10-15 stsp if (*obj != NULL) {
682 d3c116bf 2021-10-15 stsp (*obj)->refcnt++;
683 d3c116bf 2021-10-15 stsp return NULL;
686 d3c116bf 2021-10-15 stsp if (*outfd == -1) {
687 d3c116bf 2021-10-15 stsp *outfd = got_opentempfd();
688 d3c116bf 2021-10-15 stsp if (*outfd == -1)
689 d3c116bf 2021-10-15 stsp return got_error_from_errno("got_opentempfd");
692 59d1e4a0 2021-03-10 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
693 59d1e4a0 2021-03-10 stsp if (err == NULL) {
694 59d1e4a0 2021-03-10 stsp struct got_pack *pack = NULL;
696 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
697 aea75d87 2021-07-06 stsp packidx->path_packidx);
701 59d1e4a0 2021-03-10 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
702 59d1e4a0 2021-03-10 stsp if (pack == NULL) {
703 59d1e4a0 2021-03-10 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
708 59d1e4a0 2021-03-10 stsp err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
709 d3c116bf 2021-10-15 stsp *outfd, pack, packidx, idx, id);
712 59d1e4a0 2021-03-10 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
715 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
718 d3c116bf 2021-10-15 stsp err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
719 d5c81d44 2021-07-08 stsp id, repo, fd);
724 59d1e4a0 2021-03-10 stsp *obj = calloc(1, sizeof(**obj));
725 59d1e4a0 2021-03-10 stsp if (*obj == NULL) {
726 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("calloc");
729 64a8571e 2022-01-07 stsp (*obj)->fd = -1;
731 59d1e4a0 2021-03-10 stsp if (outbuf) {
732 59d1e4a0 2021-03-10 stsp (*obj)->data = outbuf;
734 59d1e4a0 2021-03-10 stsp struct stat sb;
735 d3c116bf 2021-10-15 stsp if (fstat(*outfd, &sb) == -1) {
736 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fstat");
740 a8591711 2021-06-18 stsp if (sb.st_size != hdrlen + size) {
741 59d1e4a0 2021-03-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
744 64a8571e 2022-01-07 stsp #ifndef GOT_PACK_NO_MMAP
745 64a8571e 2022-01-07 stsp if (hdrlen + size > 0) {
746 64a8571e 2022-01-07 stsp (*obj)->data = mmap(NULL, hdrlen + size, PROT_READ,
747 64a8571e 2022-01-07 stsp MAP_PRIVATE, *outfd, 0);
748 64a8571e 2022-01-07 stsp if ((*obj)->data == MAP_FAILED) {
749 64a8571e 2022-01-07 stsp if (errno != ENOMEM) {
750 64a8571e 2022-01-07 stsp err = got_error_from_errno("mmap");
753 64a8571e 2022-01-07 stsp (*obj)->data = NULL;
755 64a8571e 2022-01-07 stsp (*obj)->fd = *outfd;
756 64a8571e 2022-01-07 stsp *outfd = -1;
760 64a8571e 2022-01-07 stsp if (*outfd != -1) {
761 64a8571e 2022-01-07 stsp (*obj)->f = fdopen(*outfd, "r");
762 64a8571e 2022-01-07 stsp if ((*obj)->f == NULL) {
763 64a8571e 2022-01-07 stsp err = got_error_from_errno("fdopen");
766 64a8571e 2022-01-07 stsp *outfd = -1;
769 59d1e4a0 2021-03-10 stsp (*obj)->hdrlen = hdrlen;
770 59d1e4a0 2021-03-10 stsp (*obj)->size = size;
771 d3c116bf 2021-10-15 stsp err = got_repo_cache_raw_object(repo, id, *obj);
773 59d1e4a0 2021-03-10 stsp free(path_packfile);
775 59d1e4a0 2021-03-10 stsp if (*obj) {
776 59d1e4a0 2021-03-10 stsp got_object_raw_close(*obj);
777 59d1e4a0 2021-03-10 stsp *obj = NULL;
779 59d1e4a0 2021-03-10 stsp free(outbuf);
781 d3c116bf 2021-10-15 stsp (*obj)->refcnt++;
782 59d1e4a0 2021-03-10 stsp return err;
785 59d1e4a0 2021-03-10 stsp const struct got_error *
786 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
787 6dfa2fd3 2018-02-12 stsp const char *id_str)
789 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
791 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
792 6dd1ece6 2019-11-10 stsp return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
794 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
797 15a94983 2018-12-23 stsp const struct got_error *
798 15a94983 2018-12-23 stsp got_object_resolve_id_str(struct got_object_id **id,
799 15a94983 2018-12-23 stsp struct got_repository *repo, const char *id_str)
801 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
802 15a94983 2018-12-23 stsp struct got_object *obj;
804 15a94983 2018-12-23 stsp err = got_object_open_by_id_str(&obj, repo, id_str);
806 15a94983 2018-12-23 stsp return err;
808 15a94983 2018-12-23 stsp *id = got_object_id_dup(got_object_get_id(obj));
809 15a94983 2018-12-23 stsp got_object_close(obj);
810 15a94983 2018-12-23 stsp if (*id == NULL)
811 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
813 15a94983 2018-12-23 stsp return NULL;
816 434025f3 2018-09-16 stsp static const struct got_error *
817 a158c901 2018-12-23 stsp request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
818 a158c901 2018-12-23 stsp int pack_idx, struct got_object_id *id)
820 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
822 a158c901 2018-12-23 stsp err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
825 a158c901 2018-12-23 stsp return err;
827 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
829 ca6e02ac 2020-01-07 stsp return err;
831 ca6e02ac 2020-01-07 stsp (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
832 ca6e02ac 2020-01-07 stsp return NULL;
835 a158c901 2018-12-23 stsp static const struct got_error *
836 a158c901 2018-12-23 stsp read_packed_commit_privsep(struct got_commit_object **commit,
837 a158c901 2018-12-23 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
838 a158c901 2018-12-23 stsp struct got_object_id *id)
840 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
842 a158c901 2018-12-23 stsp if (pack->privsep_child)
843 a158c901 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
845 711fb6e8 2018-12-23 stsp err = start_pack_privsep_child(pack, packidx);
847 a158c901 2018-12-23 stsp return err;
849 711fb6e8 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
852 9f2369b0 2018-12-24 stsp static const struct got_error *
853 9f2369b0 2018-12-24 stsp request_commit(struct got_commit_object **commit, struct got_repository *repo,
854 d5c81d44 2021-07-08 stsp int fd, struct got_object_id *id)
856 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
857 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
859 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
861 d5c81d44 2021-07-08 stsp err = got_privsep_send_commit_req(ibuf, fd, id, -1);
863 9f2369b0 2018-12-24 stsp return err;
865 9f2369b0 2018-12-24 stsp return got_privsep_recv_commit(commit, ibuf);
868 9f2369b0 2018-12-24 stsp static const struct got_error *
869 9f2369b0 2018-12-24 stsp read_commit_privsep(struct got_commit_object **commit, int obj_fd,
870 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo)
872 ddc7b220 2019-09-08 stsp const struct got_error *err;
873 9f2369b0 2018-12-24 stsp int imsg_fds[2];
875 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
877 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
878 d5c81d44 2021-07-08 stsp return request_commit(commit, repo, obj_fd, id);
880 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
881 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
882 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
884 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
885 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
886 ddc7b220 2019-09-08 stsp free(ibuf);
887 ddc7b220 2019-09-08 stsp return err;
890 9f2369b0 2018-12-24 stsp pid = fork();
891 ddc7b220 2019-09-08 stsp if (pid == -1) {
892 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
893 ddc7b220 2019-09-08 stsp free(ibuf);
894 ddc7b220 2019-09-08 stsp return err;
896 9f2369b0 2018-12-24 stsp else if (pid == 0) {
897 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
898 9f2369b0 2018-12-24 stsp repo->path);
899 9f2369b0 2018-12-24 stsp /* not reached */
902 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
903 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
904 ddc7b220 2019-09-08 stsp free(ibuf);
905 ddc7b220 2019-09-08 stsp return err;
907 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
908 9f2369b0 2018-12-24 stsp imsg_fds[0];
909 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
910 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
911 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
913 d5c81d44 2021-07-08 stsp return request_commit(commit, repo, obj_fd, id);
917 a158c901 2018-12-23 stsp static const struct got_error *
918 434025f3 2018-09-16 stsp open_commit(struct got_commit_object **commit,
919 1785f84a 2018-12-23 stsp struct got_repository *repo, struct got_object_id *id, int check_cache)
921 434025f3 2018-09-16 stsp const struct got_error *err = NULL;
922 1785f84a 2018-12-23 stsp struct got_packidx *packidx = NULL;
924 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
926 434025f3 2018-09-16 stsp if (check_cache) {
927 1785f84a 2018-12-23 stsp *commit = got_repo_get_cached_commit(repo, id);
928 434025f3 2018-09-16 stsp if (*commit != NULL) {
929 434025f3 2018-09-16 stsp (*commit)->refcnt++;
930 434025f3 2018-09-16 stsp return NULL;
933 434025f3 2018-09-16 stsp *commit = NULL;
935 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
936 e82b1d81 2019-07-27 stsp if (err == NULL) {
937 1785f84a 2018-12-23 stsp struct got_pack *pack = NULL;
939 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
940 aea75d87 2021-07-06 stsp packidx->path_packidx);
942 1785f84a 2018-12-23 stsp return err;
944 1785f84a 2018-12-23 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
945 434025f3 2018-09-16 stsp if (pack == NULL) {
946 1785f84a 2018-12-23 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
951 a158c901 2018-12-23 stsp err = read_packed_commit_privsep(commit, pack,
952 1785f84a 2018-12-23 stsp packidx, idx, id);
953 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
956 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
958 e82b1d81 2019-07-27 stsp return err;
959 d5c81d44 2021-07-08 stsp err = read_commit_privsep(commit, fd, id, repo);
962 434025f3 2018-09-16 stsp if (err == NULL) {
963 434025f3 2018-09-16 stsp (*commit)->refcnt++;
964 1785f84a 2018-12-23 stsp err = got_repo_cache_commit(repo, id, *commit);
967 8d2c5ea3 2019-08-13 stsp free(path_packfile);
968 e32baab7 2018-11-05 stsp return err;
971 e32baab7 2018-11-05 stsp const struct got_error *
972 e32baab7 2018-11-05 stsp got_object_open_as_commit(struct got_commit_object **commit,
973 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object_id *id)
975 e32baab7 2018-11-05 stsp *commit = got_repo_get_cached_commit(repo, id);
976 e32baab7 2018-11-05 stsp if (*commit != NULL) {
977 e32baab7 2018-11-05 stsp (*commit)->refcnt++;
978 e32baab7 2018-11-05 stsp return NULL;
981 1785f84a 2018-12-23 stsp return open_commit(commit, repo, id, 0);
984 e32baab7 2018-11-05 stsp const struct got_error *
985 e32baab7 2018-11-05 stsp got_object_commit_open(struct got_commit_object **commit,
986 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object *obj)
988 1785f84a 2018-12-23 stsp return open_commit(commit, repo, got_object_get_id(obj), 1);
991 434025f3 2018-09-16 stsp const struct got_error *
992 dbc6a6b6 2018-07-12 stsp got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
994 dbc6a6b6 2018-07-12 stsp *qid = calloc(1, sizeof(**qid));
995 dbc6a6b6 2018-07-12 stsp if (*qid == NULL)
996 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
998 d7b5a0e8 2022-04-20 stsp memcpy(&(*qid)->id, id, sizeof((*qid)->id));
999 9ca9aafb 2021-06-18 stsp return NULL;
1002 9ca9aafb 2021-06-18 stsp const struct got_error *
1003 9ca9aafb 2021-06-18 stsp got_object_id_queue_copy(const struct got_object_id_queue *src,
1004 9ca9aafb 2021-06-18 stsp struct got_object_id_queue *dest)
1006 9ca9aafb 2021-06-18 stsp const struct got_error *err;
1007 9ca9aafb 2021-06-18 stsp struct got_object_qid *qid;
1009 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, src, entry) {
1010 9ca9aafb 2021-06-18 stsp struct got_object_qid *new;
1012 9ca9aafb 2021-06-18 stsp * Deep-copy the object ID only. Let the caller deal
1013 9ca9aafb 2021-06-18 stsp * with setting up the new->data pointer if needed.
1015 d7b5a0e8 2022-04-20 stsp err = got_object_qid_alloc(&new, &qid->id);
1016 9ca9aafb 2021-06-18 stsp if (err) {
1017 9ca9aafb 2021-06-18 stsp got_object_id_queue_free(dest);
1018 9ca9aafb 2021-06-18 stsp return err;
1020 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(dest, new, entry);
1023 dbc6a6b6 2018-07-12 stsp return NULL;
1026 a158c901 2018-12-23 stsp static const struct got_error *
1027 13c729f7 2018-12-24 stsp request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
1028 13c729f7 2018-12-24 stsp int pack_idx, struct got_object_id *id)
1030 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
1032 13c729f7 2018-12-24 stsp err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
1033 13c729f7 2018-12-24 stsp pack_idx);
1035 a158c901 2018-12-23 stsp return err;
1037 a158c901 2018-12-23 stsp return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
1040 71eb0e7f 2018-09-16 stsp static const struct got_error *
1041 13c729f7 2018-12-24 stsp read_packed_tree_privsep(struct got_tree_object **tree,
1042 13c729f7 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1043 13c729f7 2018-12-24 stsp struct got_object_id *id)
1045 13c729f7 2018-12-24 stsp const struct got_error *err = NULL;
1047 13c729f7 2018-12-24 stsp if (pack->privsep_child)
1048 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
1050 13c729f7 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
1052 13c729f7 2018-12-24 stsp return err;
1054 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
1057 13c729f7 2018-12-24 stsp static const struct got_error *
1058 9f2369b0 2018-12-24 stsp request_tree(struct got_tree_object **tree, struct got_repository *repo,
1059 d5c81d44 2021-07-08 stsp int fd, struct got_object_id *id)
1061 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1062 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1064 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
1066 d5c81d44 2021-07-08 stsp err = got_privsep_send_tree_req(ibuf, fd, id, -1);
1068 9f2369b0 2018-12-24 stsp return err;
1070 9f2369b0 2018-12-24 stsp return got_privsep_recv_tree(tree, ibuf);
1073 9f2369b0 2018-12-24 stsp const struct got_error *
1074 9f2369b0 2018-12-24 stsp read_tree_privsep(struct got_tree_object **tree, int obj_fd,
1075 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo)
1077 ddc7b220 2019-09-08 stsp const struct got_error *err;
1078 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1079 9f2369b0 2018-12-24 stsp pid_t pid;
1080 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1082 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
1083 d5c81d44 2021-07-08 stsp return request_tree(tree, repo, obj_fd, id);
1085 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1086 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1087 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1089 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1090 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1091 ddc7b220 2019-09-08 stsp free(ibuf);
1092 ddc7b220 2019-09-08 stsp return err;
1095 9f2369b0 2018-12-24 stsp pid = fork();
1096 ddc7b220 2019-09-08 stsp if (pid == -1) {
1097 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1098 ddc7b220 2019-09-08 stsp free(ibuf);
1099 ddc7b220 2019-09-08 stsp return err;
1101 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1102 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1103 9f2369b0 2018-12-24 stsp repo->path);
1104 9f2369b0 2018-12-24 stsp /* not reached */
1107 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
1108 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1109 ddc7b220 2019-09-08 stsp free(ibuf);
1110 ddc7b220 2019-09-08 stsp return err;
1112 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1113 9f2369b0 2018-12-24 stsp imsg_fds[0];
1114 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1115 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1116 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1119 d5c81d44 2021-07-08 stsp return request_tree(tree, repo, obj_fd, id);
1122 9f2369b0 2018-12-24 stsp static const struct got_error *
1123 13c729f7 2018-12-24 stsp open_tree(struct got_tree_object **tree, struct got_repository *repo,
1124 13c729f7 2018-12-24 stsp struct got_object_id *id, int check_cache)
1126 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
1127 13c729f7 2018-12-24 stsp struct got_packidx *packidx = NULL;
1129 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1131 71eb0e7f 2018-09-16 stsp if (check_cache) {
1132 13c729f7 2018-12-24 stsp *tree = got_repo_get_cached_tree(repo, id);
1133 71eb0e7f 2018-09-16 stsp if (*tree != NULL) {
1134 71eb0e7f 2018-09-16 stsp (*tree)->refcnt++;
1135 71eb0e7f 2018-09-16 stsp return NULL;
1138 71eb0e7f 2018-09-16 stsp *tree = NULL;
1140 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1141 e82b1d81 2019-07-27 stsp if (err == NULL) {
1142 13c729f7 2018-12-24 stsp struct got_pack *pack = NULL;
1144 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
1145 aea75d87 2021-07-06 stsp packidx->path_packidx);
1147 13c729f7 2018-12-24 stsp return err;
1149 13c729f7 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1150 e7885405 2018-09-10 stsp if (pack == NULL) {
1151 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1154 8d2c5ea3 2019-08-13 stsp goto done;
1156 13c729f7 2018-12-24 stsp err = read_packed_tree_privsep(tree, pack,
1157 13c729f7 2018-12-24 stsp packidx, idx, id);
1158 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1161 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1163 e82b1d81 2019-07-27 stsp return err;
1164 d5c81d44 2021-07-08 stsp err = read_tree_privsep(tree, fd, id, repo);
1167 f6be5c30 2018-06-22 stsp if (err == NULL) {
1168 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
1169 13c729f7 2018-12-24 stsp err = got_repo_cache_tree(repo, id, *tree);
1172 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1173 776d4d29 2018-06-17 stsp return err;
1176 776d4d29 2018-06-17 stsp const struct got_error *
1177 776d4d29 2018-06-17 stsp got_object_open_as_tree(struct got_tree_object **tree,
1178 776d4d29 2018-06-17 stsp struct got_repository *repo, struct got_object_id *id)
1180 e8eb494a 2018-09-16 stsp *tree = got_repo_get_cached_tree(repo, id);
1181 e8eb494a 2018-09-16 stsp if (*tree != NULL) {
1182 e8eb494a 2018-09-16 stsp (*tree)->refcnt++;
1183 e8eb494a 2018-09-16 stsp return NULL;
1186 13c729f7 2018-12-24 stsp return open_tree(tree, repo, id, 0);
1189 71eb0e7f 2018-09-16 stsp const struct got_error *
1190 71eb0e7f 2018-09-16 stsp got_object_tree_open(struct got_tree_object **tree,
1191 71eb0e7f 2018-09-16 stsp struct got_repository *repo, struct got_object *obj)
1193 13c729f7 2018-12-24 stsp return open_tree(tree, repo, got_object_get_id(obj), 1);
1197 56e0773d 2019-11-28 stsp got_object_tree_get_nentries(struct got_tree_object *tree)
1199 56e0773d 2019-11-28 stsp return tree->nentries;
1202 56e0773d 2019-11-28 stsp struct got_tree_entry *
1203 56e0773d 2019-11-28 stsp got_object_tree_get_first_entry(struct got_tree_object *tree)
1205 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, 0);
1208 56e0773d 2019-11-28 stsp struct got_tree_entry *
1209 56e0773d 2019-11-28 stsp got_object_tree_get_last_entry(struct got_tree_object *tree)
1211 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, tree->nentries - 1);
1214 56e0773d 2019-11-28 stsp struct got_tree_entry *
1215 56e0773d 2019-11-28 stsp got_object_tree_get_entry(struct got_tree_object *tree, int i)
1217 56e0773d 2019-11-28 stsp if (i < 0 || i >= tree->nentries)
1218 56e0773d 2019-11-28 stsp return NULL;
1219 56e0773d 2019-11-28 stsp return &tree->entries[i];
1223 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(struct got_tree_entry *te)
1225 56e0773d 2019-11-28 stsp return te->mode;
1228 56e0773d 2019-11-28 stsp const char *
1229 56e0773d 2019-11-28 stsp got_tree_entry_get_name(struct got_tree_entry *te)
1231 56e0773d 2019-11-28 stsp return &te->name[0];
1234 56e0773d 2019-11-28 stsp struct got_object_id *
1235 56e0773d 2019-11-28 stsp got_tree_entry_get_id(struct got_tree_entry *te)
1237 56e0773d 2019-11-28 stsp return &te->id;
1240 0d6c6ee3 2020-05-20 stsp const struct got_error *
1241 af57b12a 2020-07-23 stsp got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1243 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
1244 32596e16 2020-07-23 stsp size_t len, totlen, hdrlen, offset;
1246 aa092692 2020-07-23 stsp *s = NULL;
1248 659dc16e 2020-07-23 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1249 659dc16e 2020-07-23 stsp totlen = 0;
1250 32596e16 2020-07-23 stsp offset = 0;
1254 659dc16e 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1256 af57b12a 2020-07-23 stsp return err;
1258 659dc16e 2020-07-23 stsp if (len == 0)
1261 659dc16e 2020-07-23 stsp totlen += len - hdrlen;
1262 af57b12a 2020-07-23 stsp p = realloc(*s, totlen + 1);
1263 659dc16e 2020-07-23 stsp if (p == NULL) {
1264 659dc16e 2020-07-23 stsp err = got_error_from_errno("realloc");
1266 af57b12a 2020-07-23 stsp *s = NULL;
1267 af57b12a 2020-07-23 stsp return err;
1270 659dc16e 2020-07-23 stsp /* Skip blob object header first time around. */
1271 af57b12a 2020-07-23 stsp memcpy(*s + offset,
1272 f8f7c882 2020-07-23 stsp got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1273 659dc16e 2020-07-23 stsp hdrlen = 0;
1274 32596e16 2020-07-23 stsp offset = totlen;
1275 659dc16e 2020-07-23 stsp } while (len > 0);
1277 af57b12a 2020-07-23 stsp (*s)[totlen] = '\0';
1278 af57b12a 2020-07-23 stsp return NULL;
1281 af57b12a 2020-07-23 stsp const struct got_error *
1282 af57b12a 2020-07-23 stsp got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1283 af57b12a 2020-07-23 stsp struct got_repository *repo)
1285 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
1286 af57b12a 2020-07-23 stsp struct got_blob_object *blob = NULL;
1288 af57b12a 2020-07-23 stsp *link_target = NULL;
1290 af57b12a 2020-07-23 stsp if (!got_object_tree_entry_is_symlink(te))
1291 af57b12a 2020-07-23 stsp return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1293 af57b12a 2020-07-23 stsp err = got_object_open_as_blob(&blob, repo,
1294 af57b12a 2020-07-23 stsp got_tree_entry_get_id(te), PATH_MAX);
1296 af57b12a 2020-07-23 stsp return err;
1298 af57b12a 2020-07-23 stsp err = got_object_blob_read_to_str(link_target, blob);
1299 af57b12a 2020-07-23 stsp got_object_blob_close(blob);
1300 659dc16e 2020-07-23 stsp if (err) {
1301 659dc16e 2020-07-23 stsp free(*link_target);
1302 659dc16e 2020-07-23 stsp *link_target = NULL;
1304 0d6c6ee3 2020-05-20 stsp return err;
1308 56e0773d 2019-11-28 stsp got_tree_entry_get_index(struct got_tree_entry *te)
1310 56e0773d 2019-11-28 stsp return te->idx;
1313 56e0773d 2019-11-28 stsp struct got_tree_entry *
1314 56e0773d 2019-11-28 stsp got_tree_entry_get_next(struct got_tree_object *tree,
1315 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
1317 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx + 1);
1320 56e0773d 2019-11-28 stsp struct got_tree_entry *
1321 56e0773d 2019-11-28 stsp got_tree_entry_get_prev(struct got_tree_object *tree,
1322 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
1324 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx - 1);
1327 3840f4c9 2018-09-12 stsp static const struct got_error *
1328 ac544f8c 2019-01-13 stsp request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1329 ebc55e2d 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1330 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
1332 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
1333 db696021 2022-01-04 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1334 db696021 2022-01-04 stsp int outfd_child;
1336 db696021 2022-01-04 stsp err = pack_child_send_tempfiles(ibuf, pack);
1338 db696021 2022-01-04 stsp return err;
1340 3840f4c9 2018-09-12 stsp outfd_child = dup(outfd);
1341 3840f4c9 2018-09-12 stsp if (outfd_child == -1)
1342 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1344 ebc55e2d 2018-12-24 stsp err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1346 3840f4c9 2018-09-12 stsp return err;
1348 3840f4c9 2018-09-12 stsp err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1349 3840f4c9 2018-09-12 stsp outfd_child);
1350 3840f4c9 2018-09-12 stsp if (err) {
1351 3840f4c9 2018-09-12 stsp return err;
1354 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen,
1355 ebc55e2d 2018-12-24 stsp pack->privsep_child->ibuf);
1357 3840f4c9 2018-09-12 stsp return err;
1359 3840f4c9 2018-09-12 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1360 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1362 3840f4c9 2018-09-12 stsp return err;
1365 ebc55e2d 2018-12-24 stsp static const struct got_error *
1366 ac544f8c 2019-01-13 stsp read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1367 ac544f8c 2019-01-13 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1368 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
1370 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
1372 ebc55e2d 2018-12-24 stsp if (pack->privsep_child == NULL) {
1373 ebc55e2d 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
1375 ebc55e2d 2018-12-24 stsp return err;
1378 ac544f8c 2019-01-13 stsp return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1382 9f2369b0 2018-12-24 stsp static const struct got_error *
1383 ac544f8c 2019-01-13 stsp request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1384 d5c81d44 2021-07-08 stsp int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1386 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1387 9f2369b0 2018-12-24 stsp int outfd_child;
1389 9f2369b0 2018-12-24 stsp outfd_child = dup(outfd);
1390 9f2369b0 2018-12-24 stsp if (outfd_child == -1)
1391 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1393 d5c81d44 2021-07-08 stsp err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1395 9f2369b0 2018-12-24 stsp return err;
1397 9f2369b0 2018-12-24 stsp err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1399 9f2369b0 2018-12-24 stsp return err;
1401 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1403 9f2369b0 2018-12-24 stsp return err;
1405 9f2369b0 2018-12-24 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1406 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
1408 9f2369b0 2018-12-24 stsp return err;
1411 9f2369b0 2018-12-24 stsp static const struct got_error *
1412 ac544f8c 2019-01-13 stsp read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1413 d5c81d44 2021-07-08 stsp int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1415 ddc7b220 2019-09-08 stsp const struct got_error *err;
1416 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1417 9f2369b0 2018-12-24 stsp pid_t pid;
1418 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1420 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1421 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1422 d5c81d44 2021-07-08 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1426 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1427 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1428 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1430 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1431 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1432 ddc7b220 2019-09-08 stsp free(ibuf);
1433 ddc7b220 2019-09-08 stsp return err;
1436 9f2369b0 2018-12-24 stsp pid = fork();
1437 ddc7b220 2019-09-08 stsp if (pid == -1) {
1438 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1439 ddc7b220 2019-09-08 stsp free(ibuf);
1440 ddc7b220 2019-09-08 stsp return err;
1442 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1443 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1444 9f2369b0 2018-12-24 stsp repo->path);
1445 9f2369b0 2018-12-24 stsp /* not reached */
1448 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
1449 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1450 ddc7b220 2019-09-08 stsp free(ibuf);
1451 ddc7b220 2019-09-08 stsp return err;
1453 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1454 9f2369b0 2018-12-24 stsp imsg_fds[0];
1455 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1456 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1457 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1459 d5c81d44 2021-07-08 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1462 ebc55e2d 2018-12-24 stsp static const struct got_error *
1463 ebc55e2d 2018-12-24 stsp open_blob(struct got_blob_object **blob, struct got_repository *repo,
1464 ebc55e2d 2018-12-24 stsp struct got_object_id *id, size_t blocksize)
1466 ebc55e2d 2018-12-24 stsp const struct got_error *err = NULL;
1467 ebc55e2d 2018-12-24 stsp struct got_packidx *packidx = NULL;
1469 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1470 ac544f8c 2019-01-13 stsp uint8_t *outbuf;
1471 e82b1d81 2019-07-27 stsp int outfd;
1472 ebc55e2d 2018-12-24 stsp size_t size, hdrlen;
1473 ebc55e2d 2018-12-24 stsp struct stat sb;
1475 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
1476 4558fcd4 2018-01-14 stsp if (*blob == NULL)
1477 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1479 55da3778 2018-09-10 stsp outfd = got_opentempfd();
1480 55da3778 2018-09-10 stsp if (outfd == -1)
1481 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
1483 062ebb78 2018-07-12 stsp (*blob)->read_buf = malloc(blocksize);
1484 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
1485 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1486 c7254d79 2018-04-24 stsp goto done;
1489 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1490 e82b1d81 2019-07-27 stsp if (err == NULL) {
1491 ebc55e2d 2018-12-24 stsp struct got_pack *pack = NULL;
1493 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
1494 aea75d87 2021-07-06 stsp packidx->path_packidx);
1496 ebc55e2d 2018-12-24 stsp goto done;
1498 ebc55e2d 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1499 55da3778 2018-09-10 stsp if (pack == NULL) {
1500 ebc55e2d 2018-12-24 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1503 55da3778 2018-09-10 stsp goto done;
1505 ac544f8c 2019-01-13 stsp err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1506 ac544f8c 2019-01-13 stsp pack, packidx, idx, id);
1507 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1510 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&infd, id, repo);
1512 e82b1d81 2019-07-27 stsp goto done;
1513 ac544f8c 2019-01-13 stsp err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1514 d5c81d44 2021-07-08 stsp id, repo);
1517 55da3778 2018-09-10 stsp goto done;
1519 de060dff 2018-12-24 stsp if (hdrlen > size) {
1520 ebc55e2d 2018-12-24 stsp err = got_error(GOT_ERR_BAD_OBJ_HDR);
1521 ebc55e2d 2018-12-24 stsp goto done;
1524 ac544f8c 2019-01-13 stsp if (outbuf) {
1525 08578a35 2021-01-22 stsp if (close(outfd) == -1 && err == NULL)
1526 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1527 ac544f8c 2019-01-13 stsp outfd = -1;
1528 ac544f8c 2019-01-13 stsp (*blob)->f = fmemopen(outbuf, size, "rb");
1529 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1530 638f9024 2019-05-13 stsp err = got_error_from_errno("fmemopen");
1531 ac544f8c 2019-01-13 stsp free(outbuf);
1532 ac544f8c 2019-01-13 stsp goto done;
1534 ac544f8c 2019-01-13 stsp (*blob)->data = outbuf;
1536 ac544f8c 2019-01-13 stsp if (fstat(outfd, &sb) == -1) {
1537 638f9024 2019-05-13 stsp err = got_error_from_errno("fstat");
1538 ac544f8c 2019-01-13 stsp goto done;
1541 ac544f8c 2019-01-13 stsp if (sb.st_size != size) {
1542 ac544f8c 2019-01-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1543 ac544f8c 2019-01-13 stsp goto done;
1546 ac544f8c 2019-01-13 stsp (*blob)->f = fdopen(outfd, "rb");
1547 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1548 638f9024 2019-05-13 stsp err = got_error_from_errno("fdopen");
1549 ac544f8c 2019-01-13 stsp close(outfd);
1550 ac544f8c 2019-01-13 stsp outfd = -1;
1551 ac544f8c 2019-01-13 stsp goto done;
1555 ebc55e2d 2018-12-24 stsp (*blob)->hdrlen = hdrlen;
1556 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
1557 ebc55e2d 2018-12-24 stsp memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1560 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1561 55da3778 2018-09-10 stsp if (err) {
1562 55da3778 2018-09-10 stsp if (*blob) {
1563 7baf5860 2019-03-19 stsp got_object_blob_close(*blob);
1564 55da3778 2018-09-10 stsp *blob = NULL;
1565 55da3778 2018-09-10 stsp } else if (outfd != -1)
1566 55da3778 2018-09-10 stsp close(outfd);
1568 a19581a2 2018-06-21 stsp return err;
1571 a19581a2 2018-06-21 stsp const struct got_error *
1572 a19581a2 2018-06-21 stsp got_object_open_as_blob(struct got_blob_object **blob,
1573 a19581a2 2018-06-21 stsp struct got_repository *repo, struct got_object_id *id,
1574 a19581a2 2018-06-21 stsp size_t blocksize)
1576 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, id, blocksize);
1579 ebc55e2d 2018-12-24 stsp const struct got_error *
1580 ebc55e2d 2018-12-24 stsp got_object_blob_open(struct got_blob_object **blob,
1581 ebc55e2d 2018-12-24 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
1583 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1586 fb43ecf1 2019-02-11 stsp const struct got_error *
1587 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
1589 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL;
1590 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
1591 56b63ca4 2021-01-22 stsp if (blob->f && fclose(blob->f) == EOF)
1592 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1593 ac544f8c 2019-01-13 stsp free(blob->data);
1594 68482ea3 2017-11-27 stsp free(blob);
1595 fb43ecf1 2019-02-11 stsp return err;
1599 8ba819a3 2020-07-23 stsp got_object_blob_rewind(struct got_blob_object *blob)
1601 8ba819a3 2020-07-23 stsp if (blob->f)
1602 8ba819a3 2020-07-23 stsp rewind(blob->f);
1606 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1608 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1612 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
1614 f934cf2c 2018-02-12 stsp return blob->hdrlen;
1617 f934cf2c 2018-02-12 stsp const uint8_t *
1618 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
1620 f934cf2c 2018-02-12 stsp return blob->read_buf;
1623 68482ea3 2017-11-27 stsp const struct got_error *
1624 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1628 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1629 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
1630 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
1631 eb651edf 2018-02-11 stsp *outlenp = n;
1632 35e9ba5d 2018-06-21 stsp return NULL;
1635 35e9ba5d 2018-06-21 stsp const struct got_error *
1636 be659d10 2020-11-18 stsp got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1637 6c4c42e0 2019-06-24 stsp off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1639 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
1640 b6752625 2018-12-24 stsp size_t n, len, hdrlen;
1641 84451b3e 2018-07-10 stsp const uint8_t *buf;
1643 c33ebc60 2020-11-18 stsp const int alloc_chunksz = 512;
1644 c33ebc60 2020-11-18 stsp size_t nalloc = 0;
1645 f595d9bd 2019-08-14 stsp off_t off = 0, total_len = 0;
1647 6c4c42e0 2019-06-24 stsp if (line_offsets)
1648 6c4c42e0 2019-06-24 stsp *line_offsets = NULL;
1649 f595d9bd 2019-08-14 stsp if (filesize)
1650 f595d9bd 2019-08-14 stsp *filesize = 0;
1651 84451b3e 2018-07-10 stsp if (nlines)
1652 84451b3e 2018-07-10 stsp *nlines = 0;
1654 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1656 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
1658 35e9ba5d 2018-06-21 stsp return err;
1659 35e9ba5d 2018-06-21 stsp if (len == 0)
1661 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
1662 b02560ec 2019-08-19 stsp i = hdrlen;
1663 f1cbc3bc 2020-11-18 stsp if (nlines) {
1664 f1cbc3bc 2020-11-18 stsp if (line_offsets && *line_offsets == NULL) {
1665 78695fb7 2019-08-12 stsp /* Have some data but perhaps no '\n'. */
1666 78695fb7 2019-08-12 stsp *nlines = 1;
1667 c33ebc60 2020-11-18 stsp nalloc = alloc_chunksz;
1668 c33ebc60 2020-11-18 stsp *line_offsets = calloc(nalloc,
1669 c33ebc60 2020-11-18 stsp sizeof(**line_offsets));
1670 78695fb7 2019-08-12 stsp if (*line_offsets == NULL)
1671 845785d4 2020-02-02 tracey return got_error_from_errno("calloc");
1673 b02560ec 2019-08-19 stsp /* Skip forward over end of first line. */
1674 b02560ec 2019-08-19 stsp while (i < len) {
1675 b02560ec 2019-08-19 stsp if (buf[i] == '\n')
1680 b02560ec 2019-08-19 stsp /* Scan '\n' offsets in remaining chunk of data. */
1681 b02560ec 2019-08-19 stsp while (i < len) {
1682 b02560ec 2019-08-19 stsp if (buf[i] != '\n') {
1686 f595d9bd 2019-08-14 stsp (*nlines)++;
1687 c33ebc60 2020-11-18 stsp if (line_offsets && nalloc < *nlines) {
1688 c33ebc60 2020-11-18 stsp size_t n = *nlines + alloc_chunksz;
1689 78695fb7 2019-08-12 stsp off_t *o = recallocarray(*line_offsets,
1690 c33ebc60 2020-11-18 stsp nalloc, n, sizeof(**line_offsets));
1691 78695fb7 2019-08-12 stsp if (o == NULL) {
1692 78695fb7 2019-08-12 stsp free(*line_offsets);
1693 78695fb7 2019-08-12 stsp *line_offsets = NULL;
1694 78695fb7 2019-08-12 stsp return got_error_from_errno(
1695 78695fb7 2019-08-12 stsp "recallocarray");
1697 78695fb7 2019-08-12 stsp *line_offsets = o;
1698 c33ebc60 2020-11-18 stsp nalloc = n;
1700 f1cbc3bc 2020-11-18 stsp if (line_offsets) {
1701 f1cbc3bc 2020-11-18 stsp off = total_len + i - hdrlen + 1;
1702 f1cbc3bc 2020-11-18 stsp (*line_offsets)[*nlines - 1] = off;
1707 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
1708 454a6b59 2018-12-24 stsp n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1709 b6752625 2018-12-24 stsp if (n != len - hdrlen)
1710 b6752625 2018-12-24 stsp return got_ferror(outfile, GOT_ERR_IO);
1711 f595d9bd 2019-08-14 stsp total_len += len - hdrlen;
1712 35e9ba5d 2018-06-21 stsp hdrlen = 0;
1713 35e9ba5d 2018-06-21 stsp } while (len != 0);
1715 cbe7f848 2019-02-11 stsp if (fflush(outfile) != 0)
1716 638f9024 2019-05-13 stsp return got_error_from_errno("fflush");
1717 35e9ba5d 2018-06-21 stsp rewind(outfile);
1719 f595d9bd 2019-08-14 stsp if (filesize)
1720 f595d9bd 2019-08-14 stsp *filesize = total_len;
1722 776d4d29 2018-06-17 stsp return NULL;
1725 f4a881ce 2018-11-17 stsp static const struct got_error *
1726 268f7291 2018-12-24 stsp request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1727 268f7291 2018-12-24 stsp int pack_idx, struct got_object_id *id)
1729 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
1731 268f7291 2018-12-24 stsp err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1732 268f7291 2018-12-24 stsp pack_idx);
1734 a158c901 2018-12-23 stsp return err;
1736 a158c901 2018-12-23 stsp return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1739 268f7291 2018-12-24 stsp static const struct got_error *
1740 268f7291 2018-12-24 stsp read_packed_tag_privsep(struct got_tag_object **tag,
1741 268f7291 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1742 268f7291 2018-12-24 stsp struct got_object_id *id)
1744 268f7291 2018-12-24 stsp const struct got_error *err = NULL;
1746 268f7291 2018-12-24 stsp if (pack->privsep_child)
1747 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1749 268f7291 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
1751 268f7291 2018-12-24 stsp return err;
1753 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1756 9f2369b0 2018-12-24 stsp static const struct got_error *
1757 9f2369b0 2018-12-24 stsp request_tag(struct got_tag_object **tag, struct got_repository *repo,
1758 d5c81d44 2021-07-08 stsp int fd, struct got_object_id *id)
1760 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1761 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1763 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1765 d5c81d44 2021-07-08 stsp err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1767 9f2369b0 2018-12-24 stsp return err;
1769 9f2369b0 2018-12-24 stsp return got_privsep_recv_tag(tag, ibuf);
1772 9f2369b0 2018-12-24 stsp static const struct got_error *
1773 9f2369b0 2018-12-24 stsp read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1774 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo)
1776 ddc7b220 2019-09-08 stsp const struct got_error *err;
1777 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1778 9f2369b0 2018-12-24 stsp pid_t pid;
1779 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1781 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1782 d5c81d44 2021-07-08 stsp return request_tag(tag, repo, obj_fd, id);
1784 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1785 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1786 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1788 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1789 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1790 ddc7b220 2019-09-08 stsp free(ibuf);
1791 ddc7b220 2019-09-08 stsp return err;
1794 9f2369b0 2018-12-24 stsp pid = fork();
1795 ddc7b220 2019-09-08 stsp if (pid == -1) {
1796 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1797 ddc7b220 2019-09-08 stsp free(ibuf);
1798 ddc7b220 2019-09-08 stsp return err;
1800 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1801 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1802 9f2369b0 2018-12-24 stsp repo->path);
1803 9f2369b0 2018-12-24 stsp /* not reached */
1806 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
1807 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1808 ddc7b220 2019-09-08 stsp free(ibuf);
1809 ddc7b220 2019-09-08 stsp return err;
1811 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1812 9f2369b0 2018-12-24 stsp imsg_fds[0];
1813 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1814 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1815 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1817 d5c81d44 2021-07-08 stsp return request_tag(tag, repo, obj_fd, id);
1820 a158c901 2018-12-23 stsp static const struct got_error *
1821 268f7291 2018-12-24 stsp open_tag(struct got_tag_object **tag, struct got_repository *repo,
1822 268f7291 2018-12-24 stsp struct got_object_id *id, int check_cache)
1824 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1825 268f7291 2018-12-24 stsp struct got_packidx *packidx = NULL;
1827 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1828 5d844a1e 2019-08-13 stsp struct got_object *obj = NULL;
1829 5d844a1e 2019-08-13 stsp int obj_type = GOT_OBJ_TYPE_ANY;
1831 f4a881ce 2018-11-17 stsp if (check_cache) {
1832 268f7291 2018-12-24 stsp *tag = got_repo_get_cached_tag(repo, id);
1833 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1834 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1835 f4a881ce 2018-11-17 stsp return NULL;
1838 f4a881ce 2018-11-17 stsp *tag = NULL;
1840 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1841 e82b1d81 2019-07-27 stsp if (err == NULL) {
1842 268f7291 2018-12-24 stsp struct got_pack *pack = NULL;
1844 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
1845 aea75d87 2021-07-06 stsp packidx->path_packidx);
1847 268f7291 2018-12-24 stsp return err;
1849 268f7291 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1850 f4a881ce 2018-11-17 stsp if (pack == NULL) {
1851 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1854 8d2c5ea3 2019-08-13 stsp goto done;
1857 992eb9d8 2020-02-07 tracey /* Beware of "lightweight" tags: Check object type first. */
1858 5d844a1e 2019-08-13 stsp err = read_packed_object_privsep(&obj, repo, pack, packidx,
1861 5d844a1e 2019-08-13 stsp goto done;
1862 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1863 5d844a1e 2019-08-13 stsp got_object_close(obj);
1864 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG) {
1865 5d844a1e 2019-08-13 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1866 5d844a1e 2019-08-13 stsp goto done;
1868 5d844a1e 2019-08-13 stsp err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1869 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1872 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1874 e82b1d81 2019-07-27 stsp return err;
1875 d5c81d44 2021-07-08 stsp err = got_object_read_header_privsep(&obj, id, repo, fd);
1877 5d844a1e 2019-08-13 stsp return err;
1878 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1879 5d844a1e 2019-08-13 stsp got_object_close(obj);
1880 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
1881 5d844a1e 2019-08-13 stsp return got_error(GOT_ERR_OBJ_TYPE);
1883 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1885 5d844a1e 2019-08-13 stsp return err;
1886 d5c81d44 2021-07-08 stsp err = read_tag_privsep(tag, fd, id, repo);
1889 f4a881ce 2018-11-17 stsp if (err == NULL) {
1890 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1891 268f7291 2018-12-24 stsp err = got_repo_cache_tag(repo, id, *tag);
1894 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1895 f4a881ce 2018-11-17 stsp return err;
1898 f4a881ce 2018-11-17 stsp const struct got_error *
1899 f4a881ce 2018-11-17 stsp got_object_open_as_tag(struct got_tag_object **tag,
1900 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object_id *id)
1902 f4a881ce 2018-11-17 stsp *tag = got_repo_get_cached_tag(repo, id);
1903 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1904 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1905 f4a881ce 2018-11-17 stsp return NULL;
1908 268f7291 2018-12-24 stsp return open_tag(tag, repo, id, 0);
1911 f4a881ce 2018-11-17 stsp const struct got_error *
1912 f4a881ce 2018-11-17 stsp got_object_tag_open(struct got_tag_object **tag,
1913 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object *obj)
1915 268f7291 2018-12-24 stsp return open_tag(tag, repo, got_object_get_id(obj), 1);
1918 d24820bf 2019-08-11 stsp const char *
1919 d24820bf 2019-08-11 stsp got_object_tag_get_name(struct got_tag_object *tag)
1921 d24820bf 2019-08-11 stsp return tag->tag;
1925 0bd18d37 2019-02-01 stsp got_object_tag_get_object_type(struct got_tag_object *tag)
1927 0bd18d37 2019-02-01 stsp return tag->obj_type;
1930 0bd18d37 2019-02-01 stsp struct got_object_id *
1931 0bd18d37 2019-02-01 stsp got_object_tag_get_object_id(struct got_tag_object *tag)
1933 0bd18d37 2019-02-01 stsp return &tag->id;
1937 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_time(struct got_tag_object *tag)
1939 01073a5d 2019-08-22 stsp return tag->tagger_time;
1943 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1945 01073a5d 2019-08-22 stsp return tag->tagger_gmtoff;
1948 01073a5d 2019-08-22 stsp const char *
1949 01073a5d 2019-08-22 stsp got_object_tag_get_tagger(struct got_tag_object *tag)
1951 01073a5d 2019-08-22 stsp return tag->tagger;
1954 01073a5d 2019-08-22 stsp const char *
1955 01073a5d 2019-08-22 stsp got_object_tag_get_message(struct got_tag_object *tag)
1957 01073a5d 2019-08-22 stsp return tag->tagmsg;
1960 776d4d29 2018-06-17 stsp static struct got_tree_entry *
1961 65a9bbe9 2018-09-15 stsp find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1965 63da309a 2018-11-07 stsp /* Note that tree entries are sorted in strncmp() order. */
1966 56e0773d 2019-11-28 stsp for (i = 0; i < tree->nentries; i++) {
1967 56e0773d 2019-11-28 stsp struct got_tree_entry *te = &tree->entries[i];
1968 63da309a 2018-11-07 stsp int cmp = strncmp(te->name, name, len);
1969 63da309a 2018-11-07 stsp if (cmp < 0)
1971 63da309a 2018-11-07 stsp if (cmp > 0)
1973 63da309a 2018-11-07 stsp if (te->name[len] == '\0')
1974 776d4d29 2018-06-17 stsp return te;
1976 eb651edf 2018-02-11 stsp return NULL;
1979 56e0773d 2019-11-28 stsp struct got_tree_entry *
1980 a129376b 2019-03-28 stsp got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1982 a129376b 2019-03-28 stsp return find_entry_by_name(tree, name, strlen(name));
1985 776d4d29 2018-06-17 stsp const struct got_error *
1986 67b631c9 2021-10-10 stsp got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1987 67b631c9 2021-10-10 stsp struct got_repository *repo, struct got_tree_object *tree,
1988 67b631c9 2021-10-10 stsp const char *path)
1990 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
1991 67b631c9 2021-10-10 stsp struct got_tree_object *subtree = NULL;
1992 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
1993 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1994 b7cd37e5 2018-11-18 stsp size_t seglen;
1996 27d434c2 2018-09-15 stsp *id = NULL;
1999 5e54fb30 2019-05-31 stsp while (s[0] == '/')
2002 65a9bbe9 2018-09-15 stsp seglen = 0;
2003 67b631c9 2021-10-10 stsp subtree = tree;
2004 b7cd37e5 2018-11-18 stsp while (*s) {
2005 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
2007 776d4d29 2018-06-17 stsp if (*s != '/') {
2014 67b631c9 2021-10-10 stsp te = find_entry_by_name(subtree, seg, seglen);
2015 db37e2c0 2018-06-21 stsp if (te == NULL) {
2016 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2017 776d4d29 2018-06-17 stsp goto done;
2020 b7cd37e5 2018-11-18 stsp if (*s == '\0')
2023 776d4d29 2018-06-17 stsp seg = s + 1;
2024 65a9bbe9 2018-09-15 stsp seglen = 0;
2027 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
2029 db37e2c0 2018-06-21 stsp te = NULL;
2031 776d4d29 2018-06-17 stsp goto done;
2032 67b631c9 2021-10-10 stsp if (subtree != tree)
2033 67b631c9 2021-10-10 stsp got_object_tree_close(subtree);
2034 67b631c9 2021-10-10 stsp subtree = next_tree;
2039 56e0773d 2019-11-28 stsp *id = got_object_id_dup(&te->id);
2040 27d434c2 2018-09-15 stsp if (*id == NULL)
2041 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2043 67b631c9 2021-10-10 stsp *mode = te->mode;
2045 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2047 67b631c9 2021-10-10 stsp if (subtree && subtree != tree)
2048 67b631c9 2021-10-10 stsp got_object_tree_close(subtree);
2049 67b631c9 2021-10-10 stsp return err;
2051 67b631c9 2021-10-10 stsp const struct got_error *
2052 67b631c9 2021-10-10 stsp got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
2053 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
2055 67b631c9 2021-10-10 stsp const struct got_error *err = NULL;
2056 67b631c9 2021-10-10 stsp struct got_tree_object *tree = NULL;
2058 67b631c9 2021-10-10 stsp *id = NULL;
2060 67b631c9 2021-10-10 stsp /* Handle opening of root of commit's tree. */
2061 67b631c9 2021-10-10 stsp if (got_path_is_root_dir(path)) {
2062 67b631c9 2021-10-10 stsp *id = got_object_id_dup(commit->tree_id);
2063 67b631c9 2021-10-10 stsp if (*id == NULL)
2064 67b631c9 2021-10-10 stsp err = got_error_from_errno("got_object_id_dup");
2066 67b631c9 2021-10-10 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
2068 67b631c9 2021-10-10 stsp goto done;
2069 67b631c9 2021-10-10 stsp err = got_object_tree_find_path(id, NULL, repo, tree, path);
2073 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
2074 776d4d29 2018-06-17 stsp return err;
2078 ac5f2b26 2020-05-05 stsp * Normalize file mode bits to avoid false positive tree entry differences
2079 ac5f2b26 2020-05-05 stsp * in case tree entries have unexpected mode bits set.
2081 ac5f2b26 2020-05-05 stsp static mode_t
2082 ac5f2b26 2020-05-05 stsp normalize_mode_for_comparison(mode_t mode)
2085 ac5f2b26 2020-05-05 stsp * For directories, the only relevant bit is the IFDIR bit.
2086 ac5f2b26 2020-05-05 stsp * This allows us to detect paths changing from a directory
2087 ac5f2b26 2020-05-05 stsp * to a file and vice versa.
2089 ac5f2b26 2020-05-05 stsp if (S_ISDIR(mode))
2090 ac5f2b26 2020-05-05 stsp return mode & S_IFDIR;
2093 40dde666 2020-07-23 stsp * For symlinks, the only relevant bit is the IFLNK bit.
2094 40dde666 2020-07-23 stsp * This allows us to detect paths changing from a symlinks
2095 40dde666 2020-07-23 stsp * to a file or directory and vice versa.
2097 40dde666 2020-07-23 stsp if (S_ISLNK(mode))
2098 40dde666 2020-07-23 stsp return mode & S_IFLNK;
2100 ac5f2b26 2020-05-05 stsp /* For files, the only change we care about is the executable bit. */
2101 ac5f2b26 2020-05-05 stsp return mode & S_IXUSR;
2104 07862c20 2018-09-15 stsp const struct got_error *
2105 07862c20 2018-09-15 stsp got_object_tree_path_changed(int *changed,
2106 07862c20 2018-09-15 stsp struct got_tree_object *tree01, struct got_tree_object *tree02,
2107 07862c20 2018-09-15 stsp const char *path, struct got_repository *repo)
2109 07862c20 2018-09-15 stsp const struct got_error *err = NULL;
2110 07862c20 2018-09-15 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2111 07862c20 2018-09-15 stsp struct got_tree_entry *te1 = NULL, *te2 = NULL;
2112 65a9bbe9 2018-09-15 stsp const char *seg, *s;
2113 3b7f9878 2018-11-18 stsp size_t seglen;
2115 07862c20 2018-09-15 stsp *changed = 0;
2117 07862c20 2018-09-15 stsp /* We not do support comparing the root path. */
2118 61a7d79f 2020-02-29 stsp if (got_path_is_root_dir(path))
2119 63f810e6 2020-02-29 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
2121 07862c20 2018-09-15 stsp tree1 = tree01;
2122 07862c20 2018-09-15 stsp tree2 = tree02;
2124 61a7d79f 2020-02-29 stsp while (*s == '/')
2127 65a9bbe9 2018-09-15 stsp seglen = 0;
2128 3b7f9878 2018-11-18 stsp while (*s) {
2129 07862c20 2018-09-15 stsp struct got_tree_object *next_tree1, *next_tree2;
2130 ac5f2b26 2020-05-05 stsp mode_t mode1, mode2;
2132 07862c20 2018-09-15 stsp if (*s != '/') {
2139 65a9bbe9 2018-09-15 stsp te1 = find_entry_by_name(tree1, seg, seglen);
2140 07862c20 2018-09-15 stsp if (te1 == NULL) {
2141 07862c20 2018-09-15 stsp err = got_error(GOT_ERR_NO_OBJ);
2142 07862c20 2018-09-15 stsp goto done;
2145 e8bfb8f3 2020-12-18 stsp if (tree2)
2146 e8bfb8f3 2020-12-18 stsp te2 = find_entry_by_name(tree2, seg, seglen);
2148 e8bfb8f3 2020-12-18 stsp if (te2) {
2149 e8bfb8f3 2020-12-18 stsp mode1 = normalize_mode_for_comparison(te1->mode);
2150 e8bfb8f3 2020-12-18 stsp mode2 = normalize_mode_for_comparison(te2->mode);
2151 e8bfb8f3 2020-12-18 stsp if (mode1 != mode2) {
2152 e8bfb8f3 2020-12-18 stsp *changed = 1;
2153 e8bfb8f3 2020-12-18 stsp goto done;
2156 e8bfb8f3 2020-12-18 stsp if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2157 e8bfb8f3 2020-12-18 stsp *changed = 0;
2158 e8bfb8f3 2020-12-18 stsp goto done;
2162 3b7f9878 2018-11-18 stsp if (*s == '\0') { /* final path element */
2163 07862c20 2018-09-15 stsp *changed = 1;
2164 07862c20 2018-09-15 stsp goto done;
2167 07862c20 2018-09-15 stsp seg = s + 1;
2169 65a9bbe9 2018-09-15 stsp seglen = 0;
2171 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree1, repo,
2172 56e0773d 2019-11-28 stsp &te1->id);
2173 07862c20 2018-09-15 stsp te1 = NULL;
2175 07862c20 2018-09-15 stsp goto done;
2176 a31cea73 2018-09-15 stsp if (tree1 != tree01)
2177 a31cea73 2018-09-15 stsp got_object_tree_close(tree1);
2178 07862c20 2018-09-15 stsp tree1 = next_tree1;
2180 e8bfb8f3 2020-12-18 stsp if (te2) {
2181 e8bfb8f3 2020-12-18 stsp err = got_object_open_as_tree(&next_tree2, repo,
2182 e8bfb8f3 2020-12-18 stsp &te2->id);
2183 e8bfb8f3 2020-12-18 stsp te2 = NULL;
2185 e8bfb8f3 2020-12-18 stsp goto done;
2186 e8bfb8f3 2020-12-18 stsp if (tree2 != tree02)
2187 e8bfb8f3 2020-12-18 stsp got_object_tree_close(tree2);
2188 e8bfb8f3 2020-12-18 stsp tree2 = next_tree2;
2189 e8bfb8f3 2020-12-18 stsp } else if (tree2) {
2190 e8bfb8f3 2020-12-18 stsp if (tree2 != tree02)
2191 e8bfb8f3 2020-12-18 stsp got_object_tree_close(tree2);
2192 e8bfb8f3 2020-12-18 stsp tree2 = NULL;
2197 a31cea73 2018-09-15 stsp if (tree1 && tree1 != tree01)
2198 07862c20 2018-09-15 stsp got_object_tree_close(tree1);
2199 a31cea73 2018-09-15 stsp if (tree2 && tree2 != tree02)
2200 07862c20 2018-09-15 stsp got_object_tree_close(tree2);
2201 77880158 2018-11-04 stsp return err;
2204 ed175427 2019-05-09 stsp const struct got_error *
2205 ed175427 2019-05-09 stsp got_object_tree_entry_dup(struct got_tree_entry **new_te,
2206 ed175427 2019-05-09 stsp struct got_tree_entry *te)
2208 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
2210 ed175427 2019-05-09 stsp *new_te = calloc(1, sizeof(**new_te));
2211 ed175427 2019-05-09 stsp if (*new_te == NULL)
2212 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
2214 ed175427 2019-05-09 stsp (*new_te)->mode = te->mode;
2215 56e0773d 2019-11-28 stsp memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2216 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2217 8c4eabf2 2019-05-10 stsp return err;
2221 56e0773d 2019-11-28 stsp got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2223 63c5ca5d 2019-08-24 stsp return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2227 e40622f4 2020-07-23 stsp got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2229 e40622f4 2020-07-23 stsp /* S_IFDIR check avoids confusing symlinks with submodules. */
2230 e40622f4 2020-07-23 stsp return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2233 e40622f4 2020-07-23 stsp static const struct got_error *
2234 e40622f4 2020-07-23 stsp resolve_symlink(char **link_target, const char *path,
2235 a44927cc 2022-04-07 stsp struct got_commit_object *commit, struct got_repository *repo)
2237 e40622f4 2020-07-23 stsp const struct got_error *err = NULL;
2238 dbdd6209 2020-10-19 stsp char buf[PATH_MAX];
2239 e40622f4 2020-07-23 stsp char *name, *parent_path = NULL;
2240 e40622f4 2020-07-23 stsp struct got_object_id *tree_obj_id = NULL;
2241 e40622f4 2020-07-23 stsp struct got_tree_object *tree = NULL;
2242 e40622f4 2020-07-23 stsp struct got_tree_entry *te = NULL;
2244 e40622f4 2020-07-23 stsp *link_target = NULL;
2246 dbdd6209 2020-10-19 stsp if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2247 dbdd6209 2020-10-19 stsp return got_error(GOT_ERR_NO_SPACE);
2249 dbdd6209 2020-10-19 stsp name = basename(buf);
2250 e40622f4 2020-07-23 stsp if (name == NULL)
2251 e40622f4 2020-07-23 stsp return got_error_from_errno2("basename", path);
2253 e40622f4 2020-07-23 stsp err = got_path_dirname(&parent_path, path);
2255 e40622f4 2020-07-23 stsp return err;
2257 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_obj_id, repo, commit,
2258 e40622f4 2020-07-23 stsp parent_path);
2259 e40622f4 2020-07-23 stsp if (err) {
2260 e40622f4 2020-07-23 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2261 e40622f4 2020-07-23 stsp /* Display the complete path in error message. */
2262 e40622f4 2020-07-23 stsp err = got_error_path(path, err->code);
2264 e40622f4 2020-07-23 stsp goto done;
2267 e40622f4 2020-07-23 stsp err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2269 e40622f4 2020-07-23 stsp goto done;
2271 e40622f4 2020-07-23 stsp te = got_object_tree_find_entry(tree, name);
2272 e40622f4 2020-07-23 stsp if (te == NULL) {
2273 e40622f4 2020-07-23 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2274 e40622f4 2020-07-23 stsp goto done;
2277 e40622f4 2020-07-23 stsp if (got_object_tree_entry_is_symlink(te)) {
2278 e40622f4 2020-07-23 stsp err = got_tree_entry_get_symlink_target(link_target, te, repo);
2280 e40622f4 2020-07-23 stsp goto done;
2281 e40622f4 2020-07-23 stsp if (!got_path_is_absolute(*link_target)) {
2282 e40622f4 2020-07-23 stsp char *abspath;
2283 e40622f4 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", parent_path,
2284 e40622f4 2020-07-23 stsp *link_target) == -1) {
2285 e40622f4 2020-07-23 stsp err = got_error_from_errno("asprintf");
2286 e40622f4 2020-07-23 stsp goto done;
2288 e40622f4 2020-07-23 stsp free(*link_target);
2289 e40622f4 2020-07-23 stsp *link_target = malloc(PATH_MAX);
2290 e40622f4 2020-07-23 stsp if (*link_target == NULL) {
2291 e40622f4 2020-07-23 stsp err = got_error_from_errno("malloc");
2292 e40622f4 2020-07-23 stsp goto done;
2294 e40622f4 2020-07-23 stsp err = got_canonpath(abspath, *link_target, PATH_MAX);
2295 e40622f4 2020-07-23 stsp free(abspath);
2297 e40622f4 2020-07-23 stsp goto done;
2301 e40622f4 2020-07-23 stsp free(tree_obj_id);
2303 e40622f4 2020-07-23 stsp got_object_tree_close(tree);
2304 e40622f4 2020-07-23 stsp if (err) {
2305 e40622f4 2020-07-23 stsp free(*link_target);
2306 e40622f4 2020-07-23 stsp *link_target = NULL;
2308 e40622f4 2020-07-23 stsp return err;
2311 ca6e02ac 2020-01-07 stsp const struct got_error *
2312 e40622f4 2020-07-23 stsp got_object_resolve_symlinks(char **link_target, const char *path,
2313 a44927cc 2022-04-07 stsp struct got_commit_object *commit, struct got_repository *repo)
2315 e40622f4 2020-07-23 stsp const struct got_error *err = NULL;
2316 e40622f4 2020-07-23 stsp char *next_target = NULL;
2317 e40622f4 2020-07-23 stsp int max_recursion = 40; /* matches Git */
2319 e40622f4 2020-07-23 stsp *link_target = NULL;
2322 e40622f4 2020-07-23 stsp err = resolve_symlink(&next_target,
2323 a44927cc 2022-04-07 stsp *link_target ? *link_target : path, commit, repo);
2326 e40622f4 2020-07-23 stsp if (next_target) {
2327 e40622f4 2020-07-23 stsp free(*link_target);
2328 e40622f4 2020-07-23 stsp if (--max_recursion == 0) {
2329 e40622f4 2020-07-23 stsp err = got_error_path(path, GOT_ERR_RECURSION);
2330 e40622f4 2020-07-23 stsp *link_target = NULL;
2333 e40622f4 2020-07-23 stsp *link_target = next_target;
2335 e40622f4 2020-07-23 stsp } while (next_target);
2337 e40622f4 2020-07-23 stsp return err;
2340 e40622f4 2020-07-23 stsp const struct got_error *
2341 ca6e02ac 2020-01-07 stsp got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2342 ca6e02ac 2020-01-07 stsp struct got_object_id *commit_id, const char *path,
2343 ca6e02ac 2020-01-07 stsp struct got_repository *repo)
2345 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
2346 ca6e02ac 2020-01-07 stsp struct got_pack *pack = NULL;
2347 ca6e02ac 2020-01-07 stsp struct got_packidx *packidx = NULL;
2348 ca6e02ac 2020-01-07 stsp char *path_packfile = NULL;
2349 ca6e02ac 2020-01-07 stsp struct got_commit_object *changed_commit = NULL;
2350 ca6e02ac 2020-01-07 stsp struct got_object_id *changed_commit_id = NULL;
2353 ca6e02ac 2020-01-07 stsp err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2354 ca6e02ac 2020-01-07 stsp if (err) {
2355 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
2356 ca6e02ac 2020-01-07 stsp return err;
2357 ca6e02ac 2020-01-07 stsp return NULL;
2360 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
2361 aea75d87 2021-07-06 stsp packidx->path_packidx);
2363 ca6e02ac 2020-01-07 stsp return err;
2365 ca6e02ac 2020-01-07 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
2366 ca6e02ac 2020-01-07 stsp if (pack == NULL) {
2367 ca6e02ac 2020-01-07 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2369 ca6e02ac 2020-01-07 stsp goto done;
2372 ca6e02ac 2020-01-07 stsp if (pack->privsep_child == NULL) {
2373 ca6e02ac 2020-01-07 stsp err = start_pack_privsep_child(pack, packidx);
2375 ca6e02ac 2020-01-07 stsp goto done;
2378 ca6e02ac 2020-01-07 stsp err = got_privsep_send_commit_traversal_request(
2379 ca6e02ac 2020-01-07 stsp pack->privsep_child->ibuf, commit_id, idx, path);
2381 ca6e02ac 2020-01-07 stsp goto done;
2383 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_traversed_commits(&changed_commit,
2384 ca6e02ac 2020-01-07 stsp &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2386 ca6e02ac 2020-01-07 stsp goto done;
2388 ca6e02ac 2020-01-07 stsp if (changed_commit) {
2390 ca6e02ac 2020-01-07 stsp * Cache the commit in which the path was changed.
2391 ca6e02ac 2020-01-07 stsp * This commit might be opened again soon.
2393 ca6e02ac 2020-01-07 stsp changed_commit->refcnt++;
2394 ca6e02ac 2020-01-07 stsp err = got_repo_cache_commit(repo, changed_commit_id,
2395 ca6e02ac 2020-01-07 stsp changed_commit);
2396 ca6e02ac 2020-01-07 stsp got_object_commit_close(changed_commit);
2399 ca6e02ac 2020-01-07 stsp free(path_packfile);
2400 ca6e02ac 2020-01-07 stsp free(changed_commit_id);
2401 ca6e02ac 2020-01-07 stsp return err;
2404 0ab4c957 2022-06-13 stsp const struct got_error *
2405 db9b9b1c 2022-06-14 stsp got_object_enumerate(int *found_all_objects,
2406 db9b9b1c 2022-06-14 stsp got_object_enumerate_commit_cb cb_commit,
2407 0ab4c957 2022-06-13 stsp got_object_enumerate_tree_cb cb_tree, void *cb_arg,
2408 0ab4c957 2022-06-13 stsp struct got_object_id **ours, int nours,
2409 0ab4c957 2022-06-13 stsp struct got_object_id **theirs, int ntheirs,
2410 0ab4c957 2022-06-13 stsp struct got_packidx *packidx, struct got_repository *repo)
2412 0ab4c957 2022-06-13 stsp const struct got_error *err = NULL;
2413 0ab4c957 2022-06-13 stsp struct got_pack *pack;
2414 0ab4c957 2022-06-13 stsp char *path_packfile = NULL;
2416 0ab4c957 2022-06-13 stsp err = got_packidx_get_packfile_path(&path_packfile,
2417 0ab4c957 2022-06-13 stsp packidx->path_packidx);
2419 0ab4c957 2022-06-13 stsp return err;
2421 0ab4c957 2022-06-13 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
2422 0ab4c957 2022-06-13 stsp if (pack == NULL) {
2423 0ab4c957 2022-06-13 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2425 0ab4c957 2022-06-13 stsp goto done;
2428 0ab4c957 2022-06-13 stsp if (pack->privsep_child == NULL) {
2429 0ab4c957 2022-06-13 stsp err = start_pack_privsep_child(pack, packidx);
2431 0ab4c957 2022-06-13 stsp goto done;
2434 0ab4c957 2022-06-13 stsp err = got_privsep_send_object_enumeration_request(
2435 0ab4c957 2022-06-13 stsp pack->privsep_child->ibuf);
2437 0ab4c957 2022-06-13 stsp goto done;
2439 0ab4c957 2022-06-13 stsp err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
2440 0ab4c957 2022-06-13 stsp ours, nours);
2442 0ab4c957 2022-06-13 stsp goto done;
2443 0ab4c957 2022-06-13 stsp err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
2445 0ab4c957 2022-06-13 stsp goto done;
2447 0ab4c957 2022-06-13 stsp err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
2448 0ab4c957 2022-06-13 stsp theirs, ntheirs);
2450 0ab4c957 2022-06-13 stsp goto done;
2451 0ab4c957 2022-06-13 stsp err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
2453 0ab4c957 2022-06-13 stsp goto done;
2455 db9b9b1c 2022-06-14 stsp err = got_privsep_recv_enumerated_objects(found_all_objects,
2456 db9b9b1c 2022-06-14 stsp pack->privsep_child->ibuf, cb_commit, cb_tree, cb_arg, repo);
2458 0ab4c957 2022-06-13 stsp free(path_packfile);
2459 0ab4c957 2022-06-13 stsp return err;