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 3235492e 2018-04-01 stsp struct got_object_id *
64 3235492e 2018-04-01 stsp got_object_get_id(struct got_object *obj)
66 6402fb3c 2018-09-15 stsp return &obj->id;
69 bacc9935 2018-05-20 stsp const struct got_error *
70 bacc9935 2018-05-20 stsp got_object_get_id_str(char **outbuf, struct got_object *obj)
72 bacc9935 2018-05-20 stsp return got_object_id_str(outbuf, &obj->id);
75 15a94983 2018-12-23 stsp const struct got_error *
76 15a94983 2018-12-23 stsp got_object_get_type(int *type, struct got_repository *repo,
77 15a94983 2018-12-23 stsp struct got_object_id *id)
79 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
80 15a94983 2018-12-23 stsp struct got_object *obj;
82 15a94983 2018-12-23 stsp err = got_object_open(&obj, repo, id);
86 b107e67f 2018-01-19 stsp switch (obj->type) {
87 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_COMMIT:
88 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_TREE:
89 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_BLOB:
90 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
91 15a94983 2018-12-23 stsp *type = obj->type;
94 15a94983 2018-12-23 stsp err = got_error(GOT_ERR_OBJ_TYPE);
98 15a94983 2018-12-23 stsp got_object_close(obj);
102 90bdb554 2019-04-11 stsp const struct got_error *
103 90bdb554 2019-04-11 stsp got_object_get_path(char **path, struct got_object_id *id,
104 90bdb554 2019-04-11 stsp struct got_repository *repo)
106 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
107 7a132809 2018-07-23 stsp char *hex = NULL;
108 41d2888b 2019-08-11 stsp char *path_objects;
110 e6b1056e 2018-04-22 stsp *path = NULL;
112 41d2888b 2019-08-11 stsp path_objects = got_repo_get_path_objects(repo);
113 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
114 638f9024 2019-05-13 stsp return got_error_from_errno("got_repo_get_path_objects");
116 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
120 d1cda826 2017-11-06 stsp if (asprintf(path, "%s/%.2x/%s", path_objects,
121 d1cda826 2017-11-06 stsp id->sha1[0], hex + 2) == -1)
122 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
126 d1cda826 2017-11-06 stsp free(path_objects);
127 d1cda826 2017-11-06 stsp return err;
130 762d73f4 2021-04-10 stsp const struct got_error *
131 762d73f4 2021-04-10 stsp got_object_open_loose_fd(int *fd, struct got_object_id *id,
132 4796fb13 2018-12-23 stsp struct got_repository *repo)
134 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
135 a1fd68d8 2018-01-12 stsp char *path;
137 90bdb554 2019-04-11 stsp err = got_object_get_path(&path, id, repo);
139 d1cda826 2017-11-06 stsp return err;
140 8bd0cdad 2021-12-31 stsp *fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
141 d5003b79 2018-04-22 stsp if (*fd == -1) {
142 e82b1d81 2019-07-27 stsp err = got_error_from_errno2("open", path);
146 4558fcd4 2018-01-14 stsp free(path);
147 2090a03d 2018-09-09 stsp return err;
150 2090a03d 2018-09-09 stsp static const struct got_error *
151 a158c901 2018-12-23 stsp request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
152 a158c901 2018-12-23 stsp struct got_object_id *id)
154 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
155 a158c901 2018-12-23 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
157 a158c901 2018-12-23 stsp err = got_privsep_send_packed_obj_req(ibuf, idx, id);
159 a158c901 2018-12-23 stsp return err;
161 a158c901 2018-12-23 stsp err = got_privsep_recv_obj(obj, ibuf);
163 a158c901 2018-12-23 stsp return err;
165 a158c901 2018-12-23 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
167 a158c901 2018-12-23 stsp return NULL;
170 db696021 2022-01-04 stsp /* Create temporary files used during delta application. */
171 59d1e4a0 2021-03-10 stsp static const struct got_error *
172 db696021 2022-01-04 stsp pack_child_send_tempfiles(struct imsgbuf *ibuf, struct got_pack *pack)
174 db696021 2022-01-04 stsp const struct got_error *err;
175 57160834 2022-05-31 stsp int basefd = -1, accumfd = -1;
178 db696021 2022-01-04 stsp * For performance reasons, the child will keep reusing the
179 db696021 2022-01-04 stsp * same temporary files during every object request.
180 db696021 2022-01-04 stsp * Opening and closing new files for every object request is
181 db696021 2022-01-04 stsp * too expensive during operations such as 'gotadmin pack'.
183 db696021 2022-01-04 stsp if (pack->child_has_tempfiles)
184 db696021 2022-01-04 stsp return NULL;
186 57160834 2022-05-31 stsp basefd = dup(pack->basefd);
187 59d1e4a0 2021-03-10 stsp if (basefd == -1)
188 57160834 2022-05-31 stsp return got_error_from_errno("dup");
190 57160834 2022-05-31 stsp accumfd = dup(pack->accumfd);
191 57160834 2022-05-31 stsp if (accumfd == -1) {
192 57160834 2022-05-31 stsp err = got_error_from_errno("dup");
196 db696021 2022-01-04 stsp err = got_privsep_send_tmpfd(ibuf, basefd);
200 db696021 2022-01-04 stsp err = got_privsep_send_tmpfd(ibuf, accumfd);
203 57160834 2022-05-31 stsp if (basefd != -1)
204 57160834 2022-05-31 stsp close(basefd);
205 57160834 2022-05-31 stsp if (accumfd != -1)
206 57160834 2022-05-31 stsp close(accumfd);
208 57160834 2022-05-31 stsp pack->child_has_tempfiles = 1;
209 db696021 2022-01-04 stsp return NULL;
212 db696021 2022-01-04 stsp static const struct got_error *
213 db696021 2022-01-04 stsp request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
214 db696021 2022-01-04 stsp int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
216 db696021 2022-01-04 stsp const struct got_error *err = NULL;
217 db696021 2022-01-04 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
218 db696021 2022-01-04 stsp int outfd_child;
220 db696021 2022-01-04 stsp err = pack_child_send_tempfiles(ibuf, pack);
222 db696021 2022-01-04 stsp return err;
224 db696021 2022-01-04 stsp outfd_child = dup(outfd);
225 db696021 2022-01-04 stsp if (outfd_child == -1)
226 db696021 2022-01-04 stsp return got_error_from_errno("dup");
228 59d1e4a0 2021-03-10 stsp err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
230 59d1e4a0 2021-03-10 stsp close(outfd_child);
231 59d1e4a0 2021-03-10 stsp return err;
234 59d1e4a0 2021-03-10 stsp err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
236 59d1e4a0 2021-03-10 stsp return err;
238 59d1e4a0 2021-03-10 stsp err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
240 59d1e4a0 2021-03-10 stsp return err;
242 59d1e4a0 2021-03-10 stsp return NULL;
245 da506691 2019-05-22 stsp static void
246 b48e2ddb 2019-05-22 stsp set_max_datasize(void)
248 b48e2ddb 2019-05-22 stsp struct rlimit rl;
250 b48e2ddb 2019-05-22 stsp if (getrlimit(RLIMIT_DATA, &rl) != 0)
253 b48e2ddb 2019-05-22 stsp rl.rlim_cur = rl.rlim_max;
254 b48e2ddb 2019-05-22 stsp setrlimit(RLIMIT_DATA, &rl);
257 a158c901 2018-12-23 stsp static const struct got_error *
258 711fb6e8 2018-12-23 stsp start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
260 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
261 a158c901 2018-12-23 stsp int imsg_fds[2];
263 a158c901 2018-12-23 stsp struct imsgbuf *ibuf;
265 a158c901 2018-12-23 stsp ibuf = calloc(1, sizeof(*ibuf));
266 a158c901 2018-12-23 stsp if (ibuf == NULL)
267 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
269 a158c901 2018-12-23 stsp pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
270 a158c901 2018-12-23 stsp if (pack->privsep_child == NULL) {
271 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
272 a158c901 2018-12-23 stsp free(ibuf);
273 a158c901 2018-12-23 stsp return err;
275 db696021 2022-01-04 stsp pack->child_has_tempfiles = 0;
276 67fd6849 2022-02-13 stsp pack->child_has_delta_outfd = 0;
278 a158c901 2018-12-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
279 638f9024 2019-05-13 stsp err = got_error_from_errno("socketpair");
283 a158c901 2018-12-23 stsp pid = fork();
284 a158c901 2018-12-23 stsp if (pid == -1) {
285 638f9024 2019-05-13 stsp err = got_error_from_errno("fork");
287 a158c901 2018-12-23 stsp } else if (pid == 0) {
288 b48e2ddb 2019-05-22 stsp set_max_datasize();
289 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
290 a158c901 2018-12-23 stsp pack->path_packfile);
291 a158c901 2018-12-23 stsp /* not reached */
294 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1)
295 638f9024 2019-05-13 stsp return got_error_from_errno("close");
296 a158c901 2018-12-23 stsp pack->privsep_child->imsg_fd = imsg_fds[0];
297 a158c901 2018-12-23 stsp pack->privsep_child->pid = pid;
298 a158c901 2018-12-23 stsp imsg_init(ibuf, imsg_fds[0]);
299 a158c901 2018-12-23 stsp pack->privsep_child->ibuf = ibuf;
301 a158c901 2018-12-23 stsp err = got_privsep_init_pack_child(ibuf, pack, packidx);
303 a158c901 2018-12-23 stsp const struct got_error *child_err;
304 a158c901 2018-12-23 stsp err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
305 a158c901 2018-12-23 stsp child_err = got_privsep_wait_for_child(
306 a158c901 2018-12-23 stsp pack->privsep_child->pid);
307 a158c901 2018-12-23 stsp if (child_err && err == NULL)
308 a158c901 2018-12-23 stsp err = child_err;
312 a158c901 2018-12-23 stsp free(ibuf);
313 a158c901 2018-12-23 stsp free(pack->privsep_child);
314 a158c901 2018-12-23 stsp pack->privsep_child = NULL;
316 a158c901 2018-12-23 stsp return err;
319 711fb6e8 2018-12-23 stsp static const struct got_error *
320 711fb6e8 2018-12-23 stsp read_packed_object_privsep(struct got_object **obj,
321 711fb6e8 2018-12-23 stsp struct got_repository *repo, struct got_pack *pack,
322 711fb6e8 2018-12-23 stsp struct got_packidx *packidx, int idx, struct got_object_id *id)
324 711fb6e8 2018-12-23 stsp const struct got_error *err = NULL;
326 59d1e4a0 2021-03-10 stsp if (pack->privsep_child == NULL) {
327 59d1e4a0 2021-03-10 stsp err = start_pack_privsep_child(pack, packidx);
329 59d1e4a0 2021-03-10 stsp return err;
332 711fb6e8 2018-12-23 stsp return request_packed_object(obj, pack, idx, id);
335 59d1e4a0 2021-03-10 stsp static const struct got_error *
336 59d1e4a0 2021-03-10 stsp read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
337 59d1e4a0 2021-03-10 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
338 59d1e4a0 2021-03-10 stsp struct got_object_id *id)
340 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
342 59d1e4a0 2021-03-10 stsp if (pack->privsep_child == NULL) {
343 59d1e4a0 2021-03-10 stsp err = start_pack_privsep_child(pack, packidx);
345 59d1e4a0 2021-03-10 stsp return err;
348 59d1e4a0 2021-03-10 stsp return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
352 b3d68e7f 2021-07-03 stsp const struct got_error *
353 b3d68e7f 2021-07-03 stsp got_object_open_packed(struct got_object **obj, struct got_object_id *id,
354 2090a03d 2018-09-09 stsp struct got_repository *repo)
356 2090a03d 2018-09-09 stsp const struct got_error *err = NULL;
357 2090a03d 2018-09-09 stsp struct got_pack *pack = NULL;
358 2090a03d 2018-09-09 stsp struct got_packidx *packidx = NULL;
360 2090a03d 2018-09-09 stsp char *path_packfile;
362 2090a03d 2018-09-09 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
364 2090a03d 2018-09-09 stsp return err;
366 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
367 aea75d87 2021-07-06 stsp packidx->path_packidx);
369 2090a03d 2018-09-09 stsp return err;
371 2090a03d 2018-09-09 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
372 2090a03d 2018-09-09 stsp if (pack == NULL) {
373 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
378 a158c901 2018-12-23 stsp err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
382 2090a03d 2018-09-09 stsp free(path_packfile);
383 4558fcd4 2018-01-14 stsp return err;
386 67fd6849 2022-02-13 stsp const struct got_error *
387 67fd6849 2022-02-13 stsp got_object_open_from_packfile(struct got_object **obj, struct got_object_id *id,
388 67fd6849 2022-02-13 stsp struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
389 67fd6849 2022-02-13 stsp struct got_repository *repo)
391 67fd6849 2022-02-13 stsp return read_packed_object_privsep(obj, repo, pack, packidx,
392 67fd6849 2022-02-13 stsp obj_idx, id);
395 67fd6849 2022-02-13 stsp const struct got_error *
396 67fd6849 2022-02-13 stsp got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
397 2d9e6abf 2022-05-04 stsp off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
398 2d9e6abf 2022-05-04 stsp off_t *delta_out_offset, struct got_object_id **base_id, int delta_cache_fd,
399 67fd6849 2022-02-13 stsp struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
400 67fd6849 2022-02-13 stsp struct got_repository *repo)
402 67fd6849 2022-02-13 stsp const struct got_error *err = NULL;
403 67fd6849 2022-02-13 stsp struct got_pack *pack = NULL;
404 67fd6849 2022-02-13 stsp char *path_packfile;
406 67fd6849 2022-02-13 stsp *base_size = 0;
407 67fd6849 2022-02-13 stsp *result_size = 0;
408 67fd6849 2022-02-13 stsp *delta_size = 0;
409 2d9e6abf 2022-05-04 stsp *delta_compressed_size = 0;
410 67fd6849 2022-02-13 stsp *delta_offset = 0;
411 67fd6849 2022-02-13 stsp *delta_out_offset = 0;
413 67fd6849 2022-02-13 stsp err = got_packidx_get_packfile_path(&path_packfile,
414 67fd6849 2022-02-13 stsp packidx->path_packidx);
416 67fd6849 2022-02-13 stsp return err;
418 67fd6849 2022-02-13 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
419 67fd6849 2022-02-13 stsp if (pack == NULL) {
420 67fd6849 2022-02-13 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
422 67fd6849 2022-02-13 stsp return err;
425 67fd6849 2022-02-13 stsp if (pack->privsep_child == NULL) {
426 67fd6849 2022-02-13 stsp err = start_pack_privsep_child(pack, packidx);
428 67fd6849 2022-02-13 stsp return err;
431 67fd6849 2022-02-13 stsp if (!pack->child_has_delta_outfd) {
432 67fd6849 2022-02-13 stsp int outfd_child;
433 67fd6849 2022-02-13 stsp outfd_child = dup(delta_cache_fd);
434 67fd6849 2022-02-13 stsp if (outfd_child == -1)
435 67fd6849 2022-02-13 stsp return got_error_from_errno("dup");
436 67fd6849 2022-02-13 stsp err = got_privsep_send_raw_delta_outfd(
437 67fd6849 2022-02-13 stsp pack->privsep_child->ibuf, outfd_child);
439 67fd6849 2022-02-13 stsp return err;
440 67fd6849 2022-02-13 stsp pack->child_has_delta_outfd = 1;
443 67fd6849 2022-02-13 stsp err = got_privsep_send_raw_delta_req(pack->privsep_child->ibuf,
444 67fd6849 2022-02-13 stsp obj_idx, id);
446 67fd6849 2022-02-13 stsp return err;
448 67fd6849 2022-02-13 stsp return got_privsep_recv_raw_delta(base_size, result_size, delta_size,
449 2d9e6abf 2022-05-04 stsp delta_compressed_size, delta_offset, delta_out_offset, base_id,
450 2d9e6abf 2022-05-04 stsp pack->privsep_child->ibuf);
454 fae7e038 2022-05-07 stsp * XXX This function does not really belong in object.c. It is only here
455 fae7e038 2022-05-07 stsp * because it needs start_pack_privsep_child(); relevant code should
456 fae7e038 2022-05-07 stsp * probably be moved to pack.c/pack_create.c.
458 fae7e038 2022-05-07 stsp const struct got_error *
459 fae7e038 2022-05-07 stsp got_object_prepare_delta_reuse(struct got_pack **pack,
460 fae7e038 2022-05-07 stsp struct got_packidx *packidx, int delta_outfd, struct got_repository *repo)
462 fae7e038 2022-05-07 stsp const struct got_error *err = NULL;
463 fae7e038 2022-05-07 stsp char *path_packfile = NULL;
465 fae7e038 2022-05-07 stsp err = got_packidx_get_packfile_path(&path_packfile,
466 fae7e038 2022-05-07 stsp packidx->path_packidx);
468 fae7e038 2022-05-07 stsp return err;
470 fae7e038 2022-05-07 stsp *pack = got_repo_get_cached_pack(repo, path_packfile);
471 fae7e038 2022-05-07 stsp if (*pack == NULL) {
472 fae7e038 2022-05-07 stsp err = got_repo_cache_pack(pack, repo, path_packfile, packidx);
476 fae7e038 2022-05-07 stsp if ((*pack)->privsep_child == NULL) {
477 fae7e038 2022-05-07 stsp err = start_pack_privsep_child(*pack, packidx);
482 fae7e038 2022-05-07 stsp if (!(*pack)->child_has_delta_outfd) {
483 fae7e038 2022-05-07 stsp int outfd_child;
484 fae7e038 2022-05-07 stsp outfd_child = dup(delta_outfd);
485 fae7e038 2022-05-07 stsp if (outfd_child == -1) {
486 fae7e038 2022-05-07 stsp err = got_error_from_errno("dup");
489 fae7e038 2022-05-07 stsp err = got_privsep_send_raw_delta_outfd(
490 fae7e038 2022-05-07 stsp (*pack)->privsep_child->ibuf, outfd_child);
493 fae7e038 2022-05-07 stsp (*pack)->child_has_delta_outfd = 1;
496 fae7e038 2022-05-07 stsp err = got_privsep_send_delta_reuse_req((*pack)->privsep_child->ibuf);
498 fae7e038 2022-05-07 stsp free(path_packfile);
499 fae7e038 2022-05-07 stsp return err;
502 9f2369b0 2018-12-24 stsp static const struct got_error *
503 d5c81d44 2021-07-08 stsp request_object(struct got_object **obj, struct got_object_id *id,
504 d5c81d44 2021-07-08 stsp struct got_repository *repo, int fd)
506 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
507 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
509 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
511 d5c81d44 2021-07-08 stsp err = got_privsep_send_obj_req(ibuf, fd, id);
513 9f2369b0 2018-12-24 stsp return err;
515 9f2369b0 2018-12-24 stsp return got_privsep_recv_obj(obj, ibuf);
518 9f2369b0 2018-12-24 stsp static const struct got_error *
519 59d1e4a0 2021-03-10 stsp request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
520 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo, int infd)
522 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
523 59d1e4a0 2021-03-10 stsp struct imsgbuf *ibuf;
524 59d1e4a0 2021-03-10 stsp int outfd_child;
526 59d1e4a0 2021-03-10 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
528 59d1e4a0 2021-03-10 stsp outfd_child = dup(outfd);
529 59d1e4a0 2021-03-10 stsp if (outfd_child == -1)
530 59d1e4a0 2021-03-10 stsp return got_error_from_errno("dup");
532 d5c81d44 2021-07-08 stsp err = got_privsep_send_raw_obj_req(ibuf, infd, id);
534 59d1e4a0 2021-03-10 stsp return err;
536 59d1e4a0 2021-03-10 stsp err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
538 59d1e4a0 2021-03-10 stsp return err;
540 59d1e4a0 2021-03-10 stsp return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
543 59d1e4a0 2021-03-10 stsp static const struct got_error *
544 59d1e4a0 2021-03-10 stsp start_read_object_child(struct got_repository *repo)
546 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
547 9f2369b0 2018-12-24 stsp int imsg_fds[2];
549 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
551 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
552 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
553 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
555 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
556 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
557 ddc7b220 2019-09-08 stsp free(ibuf);
558 ddc7b220 2019-09-08 stsp return err;
561 9f2369b0 2018-12-24 stsp pid = fork();
562 ddc7b220 2019-09-08 stsp if (pid == -1) {
563 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
564 ddc7b220 2019-09-08 stsp free(ibuf);
565 ddc7b220 2019-09-08 stsp return err;
567 9f2369b0 2018-12-24 stsp else if (pid == 0) {
568 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
569 9f2369b0 2018-12-24 stsp repo->path);
570 9f2369b0 2018-12-24 stsp /* not reached */
573 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
574 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
575 ddc7b220 2019-09-08 stsp free(ibuf);
576 ddc7b220 2019-09-08 stsp return err;
579 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
580 9f2369b0 2018-12-24 stsp imsg_fds[0];
581 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
582 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
583 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
585 59d1e4a0 2021-03-10 stsp return NULL;
588 b3d68e7f 2021-07-03 stsp const struct got_error *
589 b3d68e7f 2021-07-03 stsp got_object_read_header_privsep(struct got_object **obj,
590 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo, int obj_fd)
592 59d1e4a0 2021-03-10 stsp const struct got_error *err;
594 59d1e4a0 2021-03-10 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
595 d5c81d44 2021-07-08 stsp return request_object(obj, id, repo, obj_fd);
597 59d1e4a0 2021-03-10 stsp err = start_read_object_child(repo);
599 7495ec13 2021-04-04 stsp close(obj_fd);
600 59d1e4a0 2021-03-10 stsp return err;
603 d5c81d44 2021-07-08 stsp return request_object(obj, id, repo, obj_fd);
606 59d1e4a0 2021-03-10 stsp static const struct got_error *
607 59d1e4a0 2021-03-10 stsp read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
608 d5c81d44 2021-07-08 stsp int outfd, struct got_object_id *id, struct got_repository *repo,
609 d5c81d44 2021-07-08 stsp int obj_fd)
611 59d1e4a0 2021-03-10 stsp const struct got_error *err;
613 59d1e4a0 2021-03-10 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
614 d5c81d44 2021-07-08 stsp return request_raw_object(outbuf, size, hdrlen, outfd, id,
615 d5c81d44 2021-07-08 stsp repo, obj_fd);
617 59d1e4a0 2021-03-10 stsp err = start_read_object_child(repo);
619 59d1e4a0 2021-03-10 stsp return err;
621 d5c81d44 2021-07-08 stsp return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
625 4558fcd4 2018-01-14 stsp const struct got_error *
626 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
627 4558fcd4 2018-01-14 stsp struct got_object_id *id)
629 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
632 7bb0daa1 2018-06-21 stsp *obj = got_repo_get_cached_object(repo, id);
633 7bb0daa1 2018-06-21 stsp if (*obj != NULL) {
634 7bb0daa1 2018-06-21 stsp (*obj)->refcnt++;
635 7bb0daa1 2018-06-21 stsp return NULL;
638 b3d68e7f 2021-07-03 stsp err = got_object_open_packed(obj, id, repo);
639 e82b1d81 2019-07-27 stsp if (err && err->code != GOT_ERR_NO_OBJ)
640 e82b1d81 2019-07-27 stsp return err;
641 e82b1d81 2019-07-27 stsp if (*obj) {
642 e82b1d81 2019-07-27 stsp (*obj)->refcnt++;
643 e82b1d81 2019-07-27 stsp return got_repo_cache_object(repo, id, *obj);
646 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
648 762d73f4 2021-04-10 stsp if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
649 762d73f4 2021-04-10 stsp err = got_error_no_obj(id);
650 762d73f4 2021-04-10 stsp return err;
653 d5c81d44 2021-07-08 stsp err = got_object_read_header_privsep(obj, id, repo, fd);
655 4558fcd4 2018-01-14 stsp return err;
657 762d73f4 2021-04-10 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
659 59790a32 2018-09-15 stsp (*obj)->refcnt++;
660 762d73f4 2021-04-10 stsp return got_repo_cache_object(repo, id, *obj);
663 d3c116bf 2021-10-15 stsp /* *outfd must be initialized to -1 by caller */
664 59d1e4a0 2021-03-10 stsp const struct got_error *
665 d3c116bf 2021-10-15 stsp got_object_raw_open(struct got_raw_object **obj, int *outfd,
666 94dac27c 2021-10-15 stsp struct got_repository *repo, struct got_object_id *id)
668 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
669 59d1e4a0 2021-03-10 stsp struct got_packidx *packidx = NULL;
671 59d1e4a0 2021-03-10 stsp uint8_t *outbuf = NULL;
672 59d1e4a0 2021-03-10 stsp off_t size = 0;
673 59d1e4a0 2021-03-10 stsp size_t hdrlen = 0;
674 59d1e4a0 2021-03-10 stsp char *path_packfile = NULL;
676 d3c116bf 2021-10-15 stsp *obj = got_repo_get_cached_raw_object(repo, id);
677 d3c116bf 2021-10-15 stsp if (*obj != NULL) {
678 d3c116bf 2021-10-15 stsp (*obj)->refcnt++;
679 d3c116bf 2021-10-15 stsp return NULL;
682 d3c116bf 2021-10-15 stsp if (*outfd == -1) {
683 d3c116bf 2021-10-15 stsp *outfd = got_opentempfd();
684 d3c116bf 2021-10-15 stsp if (*outfd == -1)
685 d3c116bf 2021-10-15 stsp return got_error_from_errno("got_opentempfd");
688 59d1e4a0 2021-03-10 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
689 59d1e4a0 2021-03-10 stsp if (err == NULL) {
690 59d1e4a0 2021-03-10 stsp struct got_pack *pack = NULL;
692 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
693 aea75d87 2021-07-06 stsp packidx->path_packidx);
697 59d1e4a0 2021-03-10 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
698 59d1e4a0 2021-03-10 stsp if (pack == NULL) {
699 59d1e4a0 2021-03-10 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
704 59d1e4a0 2021-03-10 stsp err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
705 d3c116bf 2021-10-15 stsp *outfd, pack, packidx, idx, id);
708 59d1e4a0 2021-03-10 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
711 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
714 d3c116bf 2021-10-15 stsp err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
715 d5c81d44 2021-07-08 stsp id, repo, fd);
720 59d1e4a0 2021-03-10 stsp *obj = calloc(1, sizeof(**obj));
721 59d1e4a0 2021-03-10 stsp if (*obj == NULL) {
722 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("calloc");
725 64a8571e 2022-01-07 stsp (*obj)->fd = -1;
727 59d1e4a0 2021-03-10 stsp if (outbuf) {
728 59d1e4a0 2021-03-10 stsp (*obj)->data = outbuf;
730 59d1e4a0 2021-03-10 stsp struct stat sb;
731 d3c116bf 2021-10-15 stsp if (fstat(*outfd, &sb) == -1) {
732 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fstat");
736 a8591711 2021-06-18 stsp if (sb.st_size != hdrlen + size) {
737 59d1e4a0 2021-03-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
740 64a8571e 2022-01-07 stsp #ifndef GOT_PACK_NO_MMAP
741 64a8571e 2022-01-07 stsp if (hdrlen + size > 0) {
742 64a8571e 2022-01-07 stsp (*obj)->data = mmap(NULL, hdrlen + size, PROT_READ,
743 64a8571e 2022-01-07 stsp MAP_PRIVATE, *outfd, 0);
744 64a8571e 2022-01-07 stsp if ((*obj)->data == MAP_FAILED) {
745 64a8571e 2022-01-07 stsp if (errno != ENOMEM) {
746 64a8571e 2022-01-07 stsp err = got_error_from_errno("mmap");
749 64a8571e 2022-01-07 stsp (*obj)->data = NULL;
751 64a8571e 2022-01-07 stsp (*obj)->fd = *outfd;
752 64a8571e 2022-01-07 stsp *outfd = -1;
756 64a8571e 2022-01-07 stsp if (*outfd != -1) {
757 64a8571e 2022-01-07 stsp (*obj)->f = fdopen(*outfd, "r");
758 64a8571e 2022-01-07 stsp if ((*obj)->f == NULL) {
759 64a8571e 2022-01-07 stsp err = got_error_from_errno("fdopen");
762 64a8571e 2022-01-07 stsp *outfd = -1;
765 59d1e4a0 2021-03-10 stsp (*obj)->hdrlen = hdrlen;
766 59d1e4a0 2021-03-10 stsp (*obj)->size = size;
767 d3c116bf 2021-10-15 stsp err = got_repo_cache_raw_object(repo, id, *obj);
769 59d1e4a0 2021-03-10 stsp free(path_packfile);
771 59d1e4a0 2021-03-10 stsp if (*obj) {
772 59d1e4a0 2021-03-10 stsp got_object_raw_close(*obj);
773 59d1e4a0 2021-03-10 stsp *obj = NULL;
775 59d1e4a0 2021-03-10 stsp free(outbuf);
777 d3c116bf 2021-10-15 stsp (*obj)->refcnt++;
778 59d1e4a0 2021-03-10 stsp return err;
781 59d1e4a0 2021-03-10 stsp const struct got_error *
782 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
783 6dfa2fd3 2018-02-12 stsp const char *id_str)
785 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
787 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
788 6dd1ece6 2019-11-10 stsp return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
790 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
793 15a94983 2018-12-23 stsp const struct got_error *
794 15a94983 2018-12-23 stsp got_object_resolve_id_str(struct got_object_id **id,
795 15a94983 2018-12-23 stsp struct got_repository *repo, const char *id_str)
797 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
798 15a94983 2018-12-23 stsp struct got_object *obj;
800 15a94983 2018-12-23 stsp err = got_object_open_by_id_str(&obj, repo, id_str);
802 15a94983 2018-12-23 stsp return err;
804 15a94983 2018-12-23 stsp *id = got_object_id_dup(got_object_get_id(obj));
805 15a94983 2018-12-23 stsp got_object_close(obj);
806 15a94983 2018-12-23 stsp if (*id == NULL)
807 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
809 15a94983 2018-12-23 stsp return NULL;
812 434025f3 2018-09-16 stsp static const struct got_error *
813 a158c901 2018-12-23 stsp request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
814 a158c901 2018-12-23 stsp int pack_idx, struct got_object_id *id)
816 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
818 a158c901 2018-12-23 stsp err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
821 a158c901 2018-12-23 stsp return err;
823 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
825 ca6e02ac 2020-01-07 stsp return err;
827 ca6e02ac 2020-01-07 stsp (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
828 ca6e02ac 2020-01-07 stsp return NULL;
831 a158c901 2018-12-23 stsp static const struct got_error *
832 a158c901 2018-12-23 stsp read_packed_commit_privsep(struct got_commit_object **commit,
833 a158c901 2018-12-23 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
834 a158c901 2018-12-23 stsp struct got_object_id *id)
836 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
838 a158c901 2018-12-23 stsp if (pack->privsep_child)
839 a158c901 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
841 711fb6e8 2018-12-23 stsp err = start_pack_privsep_child(pack, packidx);
843 a158c901 2018-12-23 stsp return err;
845 711fb6e8 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
848 9f2369b0 2018-12-24 stsp static const struct got_error *
849 9f2369b0 2018-12-24 stsp request_commit(struct got_commit_object **commit, struct got_repository *repo,
850 d5c81d44 2021-07-08 stsp int fd, struct got_object_id *id)
852 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
853 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
855 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
857 d5c81d44 2021-07-08 stsp err = got_privsep_send_commit_req(ibuf, fd, id, -1);
859 9f2369b0 2018-12-24 stsp return err;
861 9f2369b0 2018-12-24 stsp return got_privsep_recv_commit(commit, ibuf);
864 9f2369b0 2018-12-24 stsp static const struct got_error *
865 9f2369b0 2018-12-24 stsp read_commit_privsep(struct got_commit_object **commit, int obj_fd,
866 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo)
868 ddc7b220 2019-09-08 stsp const struct got_error *err;
869 9f2369b0 2018-12-24 stsp int imsg_fds[2];
871 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
873 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
874 d5c81d44 2021-07-08 stsp return request_commit(commit, repo, obj_fd, id);
876 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
877 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
878 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
880 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
881 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
882 ddc7b220 2019-09-08 stsp free(ibuf);
883 ddc7b220 2019-09-08 stsp return err;
886 9f2369b0 2018-12-24 stsp pid = fork();
887 ddc7b220 2019-09-08 stsp if (pid == -1) {
888 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
889 ddc7b220 2019-09-08 stsp free(ibuf);
890 ddc7b220 2019-09-08 stsp return err;
892 9f2369b0 2018-12-24 stsp else if (pid == 0) {
893 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
894 9f2369b0 2018-12-24 stsp repo->path);
895 9f2369b0 2018-12-24 stsp /* not reached */
898 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
899 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
900 ddc7b220 2019-09-08 stsp free(ibuf);
901 ddc7b220 2019-09-08 stsp return err;
903 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
904 9f2369b0 2018-12-24 stsp imsg_fds[0];
905 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
906 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
907 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
909 d5c81d44 2021-07-08 stsp return request_commit(commit, repo, obj_fd, id);
913 a158c901 2018-12-23 stsp static const struct got_error *
914 434025f3 2018-09-16 stsp open_commit(struct got_commit_object **commit,
915 1785f84a 2018-12-23 stsp struct got_repository *repo, struct got_object_id *id, int check_cache)
917 434025f3 2018-09-16 stsp const struct got_error *err = NULL;
918 1785f84a 2018-12-23 stsp struct got_packidx *packidx = NULL;
920 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
922 434025f3 2018-09-16 stsp if (check_cache) {
923 1785f84a 2018-12-23 stsp *commit = got_repo_get_cached_commit(repo, id);
924 434025f3 2018-09-16 stsp if (*commit != NULL) {
925 434025f3 2018-09-16 stsp (*commit)->refcnt++;
926 434025f3 2018-09-16 stsp return NULL;
929 434025f3 2018-09-16 stsp *commit = NULL;
931 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
932 e82b1d81 2019-07-27 stsp if (err == NULL) {
933 1785f84a 2018-12-23 stsp struct got_pack *pack = NULL;
935 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
936 aea75d87 2021-07-06 stsp packidx->path_packidx);
938 1785f84a 2018-12-23 stsp return err;
940 1785f84a 2018-12-23 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
941 434025f3 2018-09-16 stsp if (pack == NULL) {
942 1785f84a 2018-12-23 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
947 a158c901 2018-12-23 stsp err = read_packed_commit_privsep(commit, pack,
948 1785f84a 2018-12-23 stsp packidx, idx, id);
949 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
952 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
954 e82b1d81 2019-07-27 stsp return err;
955 d5c81d44 2021-07-08 stsp err = read_commit_privsep(commit, fd, id, repo);
958 434025f3 2018-09-16 stsp if (err == NULL) {
959 434025f3 2018-09-16 stsp (*commit)->refcnt++;
960 1785f84a 2018-12-23 stsp err = got_repo_cache_commit(repo, id, *commit);
963 8d2c5ea3 2019-08-13 stsp free(path_packfile);
964 e32baab7 2018-11-05 stsp return err;
967 e32baab7 2018-11-05 stsp const struct got_error *
968 e32baab7 2018-11-05 stsp got_object_open_as_commit(struct got_commit_object **commit,
969 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object_id *id)
971 e32baab7 2018-11-05 stsp *commit = got_repo_get_cached_commit(repo, id);
972 e32baab7 2018-11-05 stsp if (*commit != NULL) {
973 e32baab7 2018-11-05 stsp (*commit)->refcnt++;
974 e32baab7 2018-11-05 stsp return NULL;
977 1785f84a 2018-12-23 stsp return open_commit(commit, repo, id, 0);
980 e32baab7 2018-11-05 stsp const struct got_error *
981 e32baab7 2018-11-05 stsp got_object_commit_open(struct got_commit_object **commit,
982 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object *obj)
984 1785f84a 2018-12-23 stsp return open_commit(commit, repo, got_object_get_id(obj), 1);
987 434025f3 2018-09-16 stsp const struct got_error *
988 dbc6a6b6 2018-07-12 stsp got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
990 dbc6a6b6 2018-07-12 stsp *qid = calloc(1, sizeof(**qid));
991 dbc6a6b6 2018-07-12 stsp if (*qid == NULL)
992 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
994 d7b5a0e8 2022-04-20 stsp memcpy(&(*qid)->id, id, sizeof((*qid)->id));
995 9ca9aafb 2021-06-18 stsp return NULL;
998 9ca9aafb 2021-06-18 stsp const struct got_error *
999 9ca9aafb 2021-06-18 stsp got_object_id_queue_copy(const struct got_object_id_queue *src,
1000 9ca9aafb 2021-06-18 stsp struct got_object_id_queue *dest)
1002 9ca9aafb 2021-06-18 stsp const struct got_error *err;
1003 9ca9aafb 2021-06-18 stsp struct got_object_qid *qid;
1005 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, src, entry) {
1006 9ca9aafb 2021-06-18 stsp struct got_object_qid *new;
1008 9ca9aafb 2021-06-18 stsp * Deep-copy the object ID only. Let the caller deal
1009 9ca9aafb 2021-06-18 stsp * with setting up the new->data pointer if needed.
1011 d7b5a0e8 2022-04-20 stsp err = got_object_qid_alloc(&new, &qid->id);
1012 9ca9aafb 2021-06-18 stsp if (err) {
1013 9ca9aafb 2021-06-18 stsp got_object_id_queue_free(dest);
1014 9ca9aafb 2021-06-18 stsp return err;
1016 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(dest, new, entry);
1019 dbc6a6b6 2018-07-12 stsp return NULL;
1022 a158c901 2018-12-23 stsp static const struct got_error *
1023 13c729f7 2018-12-24 stsp request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
1024 13c729f7 2018-12-24 stsp int pack_idx, struct got_object_id *id)
1026 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
1028 13c729f7 2018-12-24 stsp err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
1029 13c729f7 2018-12-24 stsp pack_idx);
1031 a158c901 2018-12-23 stsp return err;
1033 a158c901 2018-12-23 stsp return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
1036 71eb0e7f 2018-09-16 stsp static const struct got_error *
1037 13c729f7 2018-12-24 stsp read_packed_tree_privsep(struct got_tree_object **tree,
1038 13c729f7 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1039 13c729f7 2018-12-24 stsp struct got_object_id *id)
1041 13c729f7 2018-12-24 stsp const struct got_error *err = NULL;
1043 13c729f7 2018-12-24 stsp if (pack->privsep_child)
1044 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
1046 13c729f7 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
1048 13c729f7 2018-12-24 stsp return err;
1050 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
1053 13c729f7 2018-12-24 stsp static const struct got_error *
1054 9f2369b0 2018-12-24 stsp request_tree(struct got_tree_object **tree, struct got_repository *repo,
1055 d5c81d44 2021-07-08 stsp int fd, struct got_object_id *id)
1057 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1058 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1060 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
1062 d5c81d44 2021-07-08 stsp err = got_privsep_send_tree_req(ibuf, fd, id, -1);
1064 9f2369b0 2018-12-24 stsp return err;
1066 9f2369b0 2018-12-24 stsp return got_privsep_recv_tree(tree, ibuf);
1069 9f2369b0 2018-12-24 stsp const struct got_error *
1070 9f2369b0 2018-12-24 stsp read_tree_privsep(struct got_tree_object **tree, int obj_fd,
1071 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo)
1073 ddc7b220 2019-09-08 stsp const struct got_error *err;
1074 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1075 9f2369b0 2018-12-24 stsp pid_t pid;
1076 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1078 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
1079 d5c81d44 2021-07-08 stsp return request_tree(tree, repo, obj_fd, id);
1081 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1082 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1083 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1085 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1086 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1087 ddc7b220 2019-09-08 stsp free(ibuf);
1088 ddc7b220 2019-09-08 stsp return err;
1091 9f2369b0 2018-12-24 stsp pid = fork();
1092 ddc7b220 2019-09-08 stsp if (pid == -1) {
1093 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1094 ddc7b220 2019-09-08 stsp free(ibuf);
1095 ddc7b220 2019-09-08 stsp return err;
1097 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1098 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1099 9f2369b0 2018-12-24 stsp repo->path);
1100 9f2369b0 2018-12-24 stsp /* not reached */
1103 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
1104 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1105 ddc7b220 2019-09-08 stsp free(ibuf);
1106 ddc7b220 2019-09-08 stsp return err;
1108 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1109 9f2369b0 2018-12-24 stsp imsg_fds[0];
1110 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1111 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1112 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1115 d5c81d44 2021-07-08 stsp return request_tree(tree, repo, obj_fd, id);
1118 9f2369b0 2018-12-24 stsp static const struct got_error *
1119 13c729f7 2018-12-24 stsp open_tree(struct got_tree_object **tree, struct got_repository *repo,
1120 13c729f7 2018-12-24 stsp struct got_object_id *id, int check_cache)
1122 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
1123 13c729f7 2018-12-24 stsp struct got_packidx *packidx = NULL;
1125 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1127 71eb0e7f 2018-09-16 stsp if (check_cache) {
1128 13c729f7 2018-12-24 stsp *tree = got_repo_get_cached_tree(repo, id);
1129 71eb0e7f 2018-09-16 stsp if (*tree != NULL) {
1130 71eb0e7f 2018-09-16 stsp (*tree)->refcnt++;
1131 71eb0e7f 2018-09-16 stsp return NULL;
1134 71eb0e7f 2018-09-16 stsp *tree = NULL;
1136 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1137 e82b1d81 2019-07-27 stsp if (err == NULL) {
1138 13c729f7 2018-12-24 stsp struct got_pack *pack = NULL;
1140 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
1141 aea75d87 2021-07-06 stsp packidx->path_packidx);
1143 13c729f7 2018-12-24 stsp return err;
1145 13c729f7 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1146 e7885405 2018-09-10 stsp if (pack == NULL) {
1147 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1150 8d2c5ea3 2019-08-13 stsp goto done;
1152 13c729f7 2018-12-24 stsp err = read_packed_tree_privsep(tree, pack,
1153 13c729f7 2018-12-24 stsp packidx, idx, id);
1154 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1157 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1159 e82b1d81 2019-07-27 stsp return err;
1160 d5c81d44 2021-07-08 stsp err = read_tree_privsep(tree, fd, id, repo);
1163 f6be5c30 2018-06-22 stsp if (err == NULL) {
1164 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
1165 13c729f7 2018-12-24 stsp err = got_repo_cache_tree(repo, id, *tree);
1168 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1169 776d4d29 2018-06-17 stsp return err;
1172 776d4d29 2018-06-17 stsp const struct got_error *
1173 776d4d29 2018-06-17 stsp got_object_open_as_tree(struct got_tree_object **tree,
1174 776d4d29 2018-06-17 stsp struct got_repository *repo, struct got_object_id *id)
1176 e8eb494a 2018-09-16 stsp *tree = got_repo_get_cached_tree(repo, id);
1177 e8eb494a 2018-09-16 stsp if (*tree != NULL) {
1178 e8eb494a 2018-09-16 stsp (*tree)->refcnt++;
1179 e8eb494a 2018-09-16 stsp return NULL;
1182 13c729f7 2018-12-24 stsp return open_tree(tree, repo, id, 0);
1185 71eb0e7f 2018-09-16 stsp const struct got_error *
1186 71eb0e7f 2018-09-16 stsp got_object_tree_open(struct got_tree_object **tree,
1187 71eb0e7f 2018-09-16 stsp struct got_repository *repo, struct got_object *obj)
1189 13c729f7 2018-12-24 stsp return open_tree(tree, repo, got_object_get_id(obj), 1);
1193 56e0773d 2019-11-28 stsp got_object_tree_get_nentries(struct got_tree_object *tree)
1195 56e0773d 2019-11-28 stsp return tree->nentries;
1198 56e0773d 2019-11-28 stsp struct got_tree_entry *
1199 56e0773d 2019-11-28 stsp got_object_tree_get_first_entry(struct got_tree_object *tree)
1201 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, 0);
1204 56e0773d 2019-11-28 stsp struct got_tree_entry *
1205 56e0773d 2019-11-28 stsp got_object_tree_get_last_entry(struct got_tree_object *tree)
1207 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, tree->nentries - 1);
1210 56e0773d 2019-11-28 stsp struct got_tree_entry *
1211 56e0773d 2019-11-28 stsp got_object_tree_get_entry(struct got_tree_object *tree, int i)
1213 56e0773d 2019-11-28 stsp if (i < 0 || i >= tree->nentries)
1214 56e0773d 2019-11-28 stsp return NULL;
1215 56e0773d 2019-11-28 stsp return &tree->entries[i];
1219 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(struct got_tree_entry *te)
1221 56e0773d 2019-11-28 stsp return te->mode;
1224 56e0773d 2019-11-28 stsp const char *
1225 56e0773d 2019-11-28 stsp got_tree_entry_get_name(struct got_tree_entry *te)
1227 56e0773d 2019-11-28 stsp return &te->name[0];
1230 56e0773d 2019-11-28 stsp struct got_object_id *
1231 56e0773d 2019-11-28 stsp got_tree_entry_get_id(struct got_tree_entry *te)
1233 56e0773d 2019-11-28 stsp return &te->id;
1236 0d6c6ee3 2020-05-20 stsp const struct got_error *
1237 af57b12a 2020-07-23 stsp got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1239 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
1240 32596e16 2020-07-23 stsp size_t len, totlen, hdrlen, offset;
1242 aa092692 2020-07-23 stsp *s = NULL;
1244 659dc16e 2020-07-23 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1245 659dc16e 2020-07-23 stsp totlen = 0;
1246 32596e16 2020-07-23 stsp offset = 0;
1250 659dc16e 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1252 af57b12a 2020-07-23 stsp return err;
1254 659dc16e 2020-07-23 stsp if (len == 0)
1257 659dc16e 2020-07-23 stsp totlen += len - hdrlen;
1258 af57b12a 2020-07-23 stsp p = realloc(*s, totlen + 1);
1259 659dc16e 2020-07-23 stsp if (p == NULL) {
1260 659dc16e 2020-07-23 stsp err = got_error_from_errno("realloc");
1262 af57b12a 2020-07-23 stsp *s = NULL;
1263 af57b12a 2020-07-23 stsp return err;
1266 659dc16e 2020-07-23 stsp /* Skip blob object header first time around. */
1267 af57b12a 2020-07-23 stsp memcpy(*s + offset,
1268 f8f7c882 2020-07-23 stsp got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1269 659dc16e 2020-07-23 stsp hdrlen = 0;
1270 32596e16 2020-07-23 stsp offset = totlen;
1271 659dc16e 2020-07-23 stsp } while (len > 0);
1273 af57b12a 2020-07-23 stsp (*s)[totlen] = '\0';
1274 af57b12a 2020-07-23 stsp return NULL;
1277 af57b12a 2020-07-23 stsp const struct got_error *
1278 af57b12a 2020-07-23 stsp got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1279 af57b12a 2020-07-23 stsp struct got_repository *repo)
1281 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
1282 af57b12a 2020-07-23 stsp struct got_blob_object *blob = NULL;
1284 af57b12a 2020-07-23 stsp *link_target = NULL;
1286 af57b12a 2020-07-23 stsp if (!got_object_tree_entry_is_symlink(te))
1287 af57b12a 2020-07-23 stsp return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1289 af57b12a 2020-07-23 stsp err = got_object_open_as_blob(&blob, repo,
1290 af57b12a 2020-07-23 stsp got_tree_entry_get_id(te), PATH_MAX);
1292 af57b12a 2020-07-23 stsp return err;
1294 af57b12a 2020-07-23 stsp err = got_object_blob_read_to_str(link_target, blob);
1295 af57b12a 2020-07-23 stsp got_object_blob_close(blob);
1296 659dc16e 2020-07-23 stsp if (err) {
1297 659dc16e 2020-07-23 stsp free(*link_target);
1298 659dc16e 2020-07-23 stsp *link_target = NULL;
1300 0d6c6ee3 2020-05-20 stsp return err;
1304 56e0773d 2019-11-28 stsp got_tree_entry_get_index(struct got_tree_entry *te)
1306 56e0773d 2019-11-28 stsp return te->idx;
1309 56e0773d 2019-11-28 stsp struct got_tree_entry *
1310 56e0773d 2019-11-28 stsp got_tree_entry_get_next(struct got_tree_object *tree,
1311 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
1313 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx + 1);
1316 56e0773d 2019-11-28 stsp struct got_tree_entry *
1317 56e0773d 2019-11-28 stsp got_tree_entry_get_prev(struct got_tree_object *tree,
1318 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
1320 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx - 1);
1323 3840f4c9 2018-09-12 stsp static const struct got_error *
1324 ac544f8c 2019-01-13 stsp request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1325 ebc55e2d 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1326 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
1328 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
1329 db696021 2022-01-04 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1330 db696021 2022-01-04 stsp int outfd_child;
1332 db696021 2022-01-04 stsp err = pack_child_send_tempfiles(ibuf, pack);
1334 db696021 2022-01-04 stsp return err;
1336 3840f4c9 2018-09-12 stsp outfd_child = dup(outfd);
1337 3840f4c9 2018-09-12 stsp if (outfd_child == -1)
1338 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1340 ebc55e2d 2018-12-24 stsp err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1342 3840f4c9 2018-09-12 stsp return err;
1344 3840f4c9 2018-09-12 stsp err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1345 3840f4c9 2018-09-12 stsp outfd_child);
1346 3840f4c9 2018-09-12 stsp if (err) {
1347 3840f4c9 2018-09-12 stsp return err;
1350 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen,
1351 ebc55e2d 2018-12-24 stsp pack->privsep_child->ibuf);
1353 3840f4c9 2018-09-12 stsp return err;
1355 3840f4c9 2018-09-12 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1356 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1358 3840f4c9 2018-09-12 stsp return err;
1361 ebc55e2d 2018-12-24 stsp static const struct got_error *
1362 ac544f8c 2019-01-13 stsp read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1363 ac544f8c 2019-01-13 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1364 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
1366 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
1368 ebc55e2d 2018-12-24 stsp if (pack->privsep_child == NULL) {
1369 ebc55e2d 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
1371 ebc55e2d 2018-12-24 stsp return err;
1374 ac544f8c 2019-01-13 stsp return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1378 9f2369b0 2018-12-24 stsp static const struct got_error *
1379 ac544f8c 2019-01-13 stsp request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1380 d5c81d44 2021-07-08 stsp int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1382 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1383 9f2369b0 2018-12-24 stsp int outfd_child;
1385 9f2369b0 2018-12-24 stsp outfd_child = dup(outfd);
1386 9f2369b0 2018-12-24 stsp if (outfd_child == -1)
1387 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1389 d5c81d44 2021-07-08 stsp err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1391 9f2369b0 2018-12-24 stsp return err;
1393 9f2369b0 2018-12-24 stsp err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1395 9f2369b0 2018-12-24 stsp return err;
1397 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1399 9f2369b0 2018-12-24 stsp return err;
1401 9f2369b0 2018-12-24 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1402 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
1404 9f2369b0 2018-12-24 stsp return err;
1407 9f2369b0 2018-12-24 stsp static const struct got_error *
1408 ac544f8c 2019-01-13 stsp read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1409 d5c81d44 2021-07-08 stsp int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1411 ddc7b220 2019-09-08 stsp const struct got_error *err;
1412 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1413 9f2369b0 2018-12-24 stsp pid_t pid;
1414 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1416 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1417 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1418 d5c81d44 2021-07-08 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1422 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1423 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1424 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1426 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1427 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1428 ddc7b220 2019-09-08 stsp free(ibuf);
1429 ddc7b220 2019-09-08 stsp return err;
1432 9f2369b0 2018-12-24 stsp pid = fork();
1433 ddc7b220 2019-09-08 stsp if (pid == -1) {
1434 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1435 ddc7b220 2019-09-08 stsp free(ibuf);
1436 ddc7b220 2019-09-08 stsp return err;
1438 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1439 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1440 9f2369b0 2018-12-24 stsp repo->path);
1441 9f2369b0 2018-12-24 stsp /* not reached */
1444 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
1445 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1446 ddc7b220 2019-09-08 stsp free(ibuf);
1447 ddc7b220 2019-09-08 stsp return err;
1449 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1450 9f2369b0 2018-12-24 stsp imsg_fds[0];
1451 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1452 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1453 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1455 d5c81d44 2021-07-08 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1458 ebc55e2d 2018-12-24 stsp static const struct got_error *
1459 ebc55e2d 2018-12-24 stsp open_blob(struct got_blob_object **blob, struct got_repository *repo,
1460 ebc55e2d 2018-12-24 stsp struct got_object_id *id, size_t blocksize)
1462 ebc55e2d 2018-12-24 stsp const struct got_error *err = NULL;
1463 ebc55e2d 2018-12-24 stsp struct got_packidx *packidx = NULL;
1465 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1466 ac544f8c 2019-01-13 stsp uint8_t *outbuf;
1467 e82b1d81 2019-07-27 stsp int outfd;
1468 ebc55e2d 2018-12-24 stsp size_t size, hdrlen;
1469 ebc55e2d 2018-12-24 stsp struct stat sb;
1471 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
1472 4558fcd4 2018-01-14 stsp if (*blob == NULL)
1473 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1475 55da3778 2018-09-10 stsp outfd = got_opentempfd();
1476 55da3778 2018-09-10 stsp if (outfd == -1)
1477 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
1479 062ebb78 2018-07-12 stsp (*blob)->read_buf = malloc(blocksize);
1480 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
1481 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1482 c7254d79 2018-04-24 stsp goto done;
1485 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1486 e82b1d81 2019-07-27 stsp if (err == NULL) {
1487 ebc55e2d 2018-12-24 stsp struct got_pack *pack = NULL;
1489 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
1490 aea75d87 2021-07-06 stsp packidx->path_packidx);
1492 ebc55e2d 2018-12-24 stsp goto done;
1494 ebc55e2d 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1495 55da3778 2018-09-10 stsp if (pack == NULL) {
1496 ebc55e2d 2018-12-24 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1499 55da3778 2018-09-10 stsp goto done;
1501 ac544f8c 2019-01-13 stsp err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1502 ac544f8c 2019-01-13 stsp pack, packidx, idx, id);
1503 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1506 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&infd, id, repo);
1508 e82b1d81 2019-07-27 stsp goto done;
1509 ac544f8c 2019-01-13 stsp err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1510 d5c81d44 2021-07-08 stsp id, repo);
1513 55da3778 2018-09-10 stsp goto done;
1515 de060dff 2018-12-24 stsp if (hdrlen > size) {
1516 ebc55e2d 2018-12-24 stsp err = got_error(GOT_ERR_BAD_OBJ_HDR);
1517 ebc55e2d 2018-12-24 stsp goto done;
1520 ac544f8c 2019-01-13 stsp if (outbuf) {
1521 08578a35 2021-01-22 stsp if (close(outfd) == -1 && err == NULL)
1522 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1523 ac544f8c 2019-01-13 stsp outfd = -1;
1524 ac544f8c 2019-01-13 stsp (*blob)->f = fmemopen(outbuf, size, "rb");
1525 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1526 638f9024 2019-05-13 stsp err = got_error_from_errno("fmemopen");
1527 ac544f8c 2019-01-13 stsp free(outbuf);
1528 ac544f8c 2019-01-13 stsp goto done;
1530 ac544f8c 2019-01-13 stsp (*blob)->data = outbuf;
1532 ac544f8c 2019-01-13 stsp if (fstat(outfd, &sb) == -1) {
1533 638f9024 2019-05-13 stsp err = got_error_from_errno("fstat");
1534 ac544f8c 2019-01-13 stsp goto done;
1537 ac544f8c 2019-01-13 stsp if (sb.st_size != size) {
1538 ac544f8c 2019-01-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1539 ac544f8c 2019-01-13 stsp goto done;
1542 ac544f8c 2019-01-13 stsp (*blob)->f = fdopen(outfd, "rb");
1543 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1544 638f9024 2019-05-13 stsp err = got_error_from_errno("fdopen");
1545 ac544f8c 2019-01-13 stsp close(outfd);
1546 ac544f8c 2019-01-13 stsp outfd = -1;
1547 ac544f8c 2019-01-13 stsp goto done;
1551 ebc55e2d 2018-12-24 stsp (*blob)->hdrlen = hdrlen;
1552 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
1553 ebc55e2d 2018-12-24 stsp memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1556 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1557 55da3778 2018-09-10 stsp if (err) {
1558 55da3778 2018-09-10 stsp if (*blob) {
1559 7baf5860 2019-03-19 stsp got_object_blob_close(*blob);
1560 55da3778 2018-09-10 stsp *blob = NULL;
1561 55da3778 2018-09-10 stsp } else if (outfd != -1)
1562 55da3778 2018-09-10 stsp close(outfd);
1564 a19581a2 2018-06-21 stsp return err;
1567 a19581a2 2018-06-21 stsp const struct got_error *
1568 a19581a2 2018-06-21 stsp got_object_open_as_blob(struct got_blob_object **blob,
1569 a19581a2 2018-06-21 stsp struct got_repository *repo, struct got_object_id *id,
1570 a19581a2 2018-06-21 stsp size_t blocksize)
1572 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, id, blocksize);
1575 ebc55e2d 2018-12-24 stsp const struct got_error *
1576 ebc55e2d 2018-12-24 stsp got_object_blob_open(struct got_blob_object **blob,
1577 ebc55e2d 2018-12-24 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
1579 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1582 fb43ecf1 2019-02-11 stsp const struct got_error *
1583 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
1585 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL;
1586 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
1587 56b63ca4 2021-01-22 stsp if (blob->f && fclose(blob->f) == EOF)
1588 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1589 ac544f8c 2019-01-13 stsp free(blob->data);
1590 68482ea3 2017-11-27 stsp free(blob);
1591 fb43ecf1 2019-02-11 stsp return err;
1595 8ba819a3 2020-07-23 stsp got_object_blob_rewind(struct got_blob_object *blob)
1597 8ba819a3 2020-07-23 stsp if (blob->f)
1598 8ba819a3 2020-07-23 stsp rewind(blob->f);
1602 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1604 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1608 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
1610 f934cf2c 2018-02-12 stsp return blob->hdrlen;
1613 f934cf2c 2018-02-12 stsp const uint8_t *
1614 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
1616 f934cf2c 2018-02-12 stsp return blob->read_buf;
1619 68482ea3 2017-11-27 stsp const struct got_error *
1620 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1624 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1625 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
1626 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
1627 eb651edf 2018-02-11 stsp *outlenp = n;
1628 35e9ba5d 2018-06-21 stsp return NULL;
1631 35e9ba5d 2018-06-21 stsp const struct got_error *
1632 be659d10 2020-11-18 stsp got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1633 6c4c42e0 2019-06-24 stsp off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1635 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
1636 b6752625 2018-12-24 stsp size_t n, len, hdrlen;
1637 84451b3e 2018-07-10 stsp const uint8_t *buf;
1639 c33ebc60 2020-11-18 stsp const int alloc_chunksz = 512;
1640 c33ebc60 2020-11-18 stsp size_t nalloc = 0;
1641 f595d9bd 2019-08-14 stsp off_t off = 0, total_len = 0;
1643 6c4c42e0 2019-06-24 stsp if (line_offsets)
1644 6c4c42e0 2019-06-24 stsp *line_offsets = NULL;
1645 f595d9bd 2019-08-14 stsp if (filesize)
1646 f595d9bd 2019-08-14 stsp *filesize = 0;
1647 84451b3e 2018-07-10 stsp if (nlines)
1648 84451b3e 2018-07-10 stsp *nlines = 0;
1650 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1652 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
1654 35e9ba5d 2018-06-21 stsp return err;
1655 35e9ba5d 2018-06-21 stsp if (len == 0)
1657 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
1658 b02560ec 2019-08-19 stsp i = hdrlen;
1659 f1cbc3bc 2020-11-18 stsp if (nlines) {
1660 f1cbc3bc 2020-11-18 stsp if (line_offsets && *line_offsets == NULL) {
1661 78695fb7 2019-08-12 stsp /* Have some data but perhaps no '\n'. */
1662 78695fb7 2019-08-12 stsp *nlines = 1;
1663 c33ebc60 2020-11-18 stsp nalloc = alloc_chunksz;
1664 c33ebc60 2020-11-18 stsp *line_offsets = calloc(nalloc,
1665 c33ebc60 2020-11-18 stsp sizeof(**line_offsets));
1666 78695fb7 2019-08-12 stsp if (*line_offsets == NULL)
1667 845785d4 2020-02-02 tracey return got_error_from_errno("calloc");
1669 b02560ec 2019-08-19 stsp /* Skip forward over end of first line. */
1670 b02560ec 2019-08-19 stsp while (i < len) {
1671 b02560ec 2019-08-19 stsp if (buf[i] == '\n')
1676 b02560ec 2019-08-19 stsp /* Scan '\n' offsets in remaining chunk of data. */
1677 b02560ec 2019-08-19 stsp while (i < len) {
1678 b02560ec 2019-08-19 stsp if (buf[i] != '\n') {
1682 f595d9bd 2019-08-14 stsp (*nlines)++;
1683 c33ebc60 2020-11-18 stsp if (line_offsets && nalloc < *nlines) {
1684 c33ebc60 2020-11-18 stsp size_t n = *nlines + alloc_chunksz;
1685 78695fb7 2019-08-12 stsp off_t *o = recallocarray(*line_offsets,
1686 c33ebc60 2020-11-18 stsp nalloc, n, sizeof(**line_offsets));
1687 78695fb7 2019-08-12 stsp if (o == NULL) {
1688 78695fb7 2019-08-12 stsp free(*line_offsets);
1689 78695fb7 2019-08-12 stsp *line_offsets = NULL;
1690 78695fb7 2019-08-12 stsp return got_error_from_errno(
1691 78695fb7 2019-08-12 stsp "recallocarray");
1693 78695fb7 2019-08-12 stsp *line_offsets = o;
1694 c33ebc60 2020-11-18 stsp nalloc = n;
1696 f1cbc3bc 2020-11-18 stsp if (line_offsets) {
1697 f1cbc3bc 2020-11-18 stsp off = total_len + i - hdrlen + 1;
1698 f1cbc3bc 2020-11-18 stsp (*line_offsets)[*nlines - 1] = off;
1703 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
1704 454a6b59 2018-12-24 stsp n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1705 b6752625 2018-12-24 stsp if (n != len - hdrlen)
1706 b6752625 2018-12-24 stsp return got_ferror(outfile, GOT_ERR_IO);
1707 f595d9bd 2019-08-14 stsp total_len += len - hdrlen;
1708 35e9ba5d 2018-06-21 stsp hdrlen = 0;
1709 35e9ba5d 2018-06-21 stsp } while (len != 0);
1711 cbe7f848 2019-02-11 stsp if (fflush(outfile) != 0)
1712 638f9024 2019-05-13 stsp return got_error_from_errno("fflush");
1713 35e9ba5d 2018-06-21 stsp rewind(outfile);
1715 f595d9bd 2019-08-14 stsp if (filesize)
1716 f595d9bd 2019-08-14 stsp *filesize = total_len;
1718 776d4d29 2018-06-17 stsp return NULL;
1721 f4a881ce 2018-11-17 stsp static const struct got_error *
1722 268f7291 2018-12-24 stsp request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1723 268f7291 2018-12-24 stsp int pack_idx, struct got_object_id *id)
1725 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
1727 268f7291 2018-12-24 stsp err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1728 268f7291 2018-12-24 stsp pack_idx);
1730 a158c901 2018-12-23 stsp return err;
1732 a158c901 2018-12-23 stsp return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1735 268f7291 2018-12-24 stsp static const struct got_error *
1736 268f7291 2018-12-24 stsp read_packed_tag_privsep(struct got_tag_object **tag,
1737 268f7291 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1738 268f7291 2018-12-24 stsp struct got_object_id *id)
1740 268f7291 2018-12-24 stsp const struct got_error *err = NULL;
1742 268f7291 2018-12-24 stsp if (pack->privsep_child)
1743 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1745 268f7291 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
1747 268f7291 2018-12-24 stsp return err;
1749 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1752 9f2369b0 2018-12-24 stsp static const struct got_error *
1753 9f2369b0 2018-12-24 stsp request_tag(struct got_tag_object **tag, struct got_repository *repo,
1754 d5c81d44 2021-07-08 stsp int fd, struct got_object_id *id)
1756 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1757 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1759 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1761 d5c81d44 2021-07-08 stsp err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1763 9f2369b0 2018-12-24 stsp return err;
1765 9f2369b0 2018-12-24 stsp return got_privsep_recv_tag(tag, ibuf);
1768 9f2369b0 2018-12-24 stsp static const struct got_error *
1769 9f2369b0 2018-12-24 stsp read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1770 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo)
1772 ddc7b220 2019-09-08 stsp const struct got_error *err;
1773 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1774 9f2369b0 2018-12-24 stsp pid_t pid;
1775 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1777 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1778 d5c81d44 2021-07-08 stsp return request_tag(tag, repo, obj_fd, id);
1780 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1781 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1782 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1784 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1785 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1786 ddc7b220 2019-09-08 stsp free(ibuf);
1787 ddc7b220 2019-09-08 stsp return err;
1790 9f2369b0 2018-12-24 stsp pid = fork();
1791 ddc7b220 2019-09-08 stsp if (pid == -1) {
1792 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1793 ddc7b220 2019-09-08 stsp free(ibuf);
1794 ddc7b220 2019-09-08 stsp return err;
1796 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1797 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1798 9f2369b0 2018-12-24 stsp repo->path);
1799 9f2369b0 2018-12-24 stsp /* not reached */
1802 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
1803 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1804 ddc7b220 2019-09-08 stsp free(ibuf);
1805 ddc7b220 2019-09-08 stsp return err;
1807 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1808 9f2369b0 2018-12-24 stsp imsg_fds[0];
1809 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1810 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1811 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1813 d5c81d44 2021-07-08 stsp return request_tag(tag, repo, obj_fd, id);
1816 a158c901 2018-12-23 stsp static const struct got_error *
1817 268f7291 2018-12-24 stsp open_tag(struct got_tag_object **tag, struct got_repository *repo,
1818 268f7291 2018-12-24 stsp struct got_object_id *id, int check_cache)
1820 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1821 268f7291 2018-12-24 stsp struct got_packidx *packidx = NULL;
1823 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1824 5d844a1e 2019-08-13 stsp struct got_object *obj = NULL;
1825 5d844a1e 2019-08-13 stsp int obj_type = GOT_OBJ_TYPE_ANY;
1827 f4a881ce 2018-11-17 stsp if (check_cache) {
1828 268f7291 2018-12-24 stsp *tag = got_repo_get_cached_tag(repo, id);
1829 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1830 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1831 f4a881ce 2018-11-17 stsp return NULL;
1834 f4a881ce 2018-11-17 stsp *tag = NULL;
1836 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1837 e82b1d81 2019-07-27 stsp if (err == NULL) {
1838 268f7291 2018-12-24 stsp struct got_pack *pack = NULL;
1840 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
1841 aea75d87 2021-07-06 stsp packidx->path_packidx);
1843 268f7291 2018-12-24 stsp return err;
1845 268f7291 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1846 f4a881ce 2018-11-17 stsp if (pack == NULL) {
1847 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1850 8d2c5ea3 2019-08-13 stsp goto done;
1853 992eb9d8 2020-02-07 tracey /* Beware of "lightweight" tags: Check object type first. */
1854 5d844a1e 2019-08-13 stsp err = read_packed_object_privsep(&obj, repo, pack, packidx,
1857 5d844a1e 2019-08-13 stsp goto done;
1858 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1859 5d844a1e 2019-08-13 stsp got_object_close(obj);
1860 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG) {
1861 5d844a1e 2019-08-13 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1862 5d844a1e 2019-08-13 stsp goto done;
1864 5d844a1e 2019-08-13 stsp err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1865 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1868 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1870 e82b1d81 2019-07-27 stsp return err;
1871 d5c81d44 2021-07-08 stsp err = got_object_read_header_privsep(&obj, id, repo, fd);
1873 5d844a1e 2019-08-13 stsp return err;
1874 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1875 5d844a1e 2019-08-13 stsp got_object_close(obj);
1876 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
1877 5d844a1e 2019-08-13 stsp return got_error(GOT_ERR_OBJ_TYPE);
1879 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1881 5d844a1e 2019-08-13 stsp return err;
1882 d5c81d44 2021-07-08 stsp err = read_tag_privsep(tag, fd, id, repo);
1885 f4a881ce 2018-11-17 stsp if (err == NULL) {
1886 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1887 268f7291 2018-12-24 stsp err = got_repo_cache_tag(repo, id, *tag);
1890 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1891 f4a881ce 2018-11-17 stsp return err;
1894 f4a881ce 2018-11-17 stsp const struct got_error *
1895 f4a881ce 2018-11-17 stsp got_object_open_as_tag(struct got_tag_object **tag,
1896 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object_id *id)
1898 f4a881ce 2018-11-17 stsp *tag = got_repo_get_cached_tag(repo, id);
1899 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1900 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1901 f4a881ce 2018-11-17 stsp return NULL;
1904 268f7291 2018-12-24 stsp return open_tag(tag, repo, id, 0);
1907 f4a881ce 2018-11-17 stsp const struct got_error *
1908 f4a881ce 2018-11-17 stsp got_object_tag_open(struct got_tag_object **tag,
1909 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object *obj)
1911 268f7291 2018-12-24 stsp return open_tag(tag, repo, got_object_get_id(obj), 1);
1914 d24820bf 2019-08-11 stsp const char *
1915 d24820bf 2019-08-11 stsp got_object_tag_get_name(struct got_tag_object *tag)
1917 d24820bf 2019-08-11 stsp return tag->tag;
1921 0bd18d37 2019-02-01 stsp got_object_tag_get_object_type(struct got_tag_object *tag)
1923 0bd18d37 2019-02-01 stsp return tag->obj_type;
1926 0bd18d37 2019-02-01 stsp struct got_object_id *
1927 0bd18d37 2019-02-01 stsp got_object_tag_get_object_id(struct got_tag_object *tag)
1929 0bd18d37 2019-02-01 stsp return &tag->id;
1933 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_time(struct got_tag_object *tag)
1935 01073a5d 2019-08-22 stsp return tag->tagger_time;
1939 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1941 01073a5d 2019-08-22 stsp return tag->tagger_gmtoff;
1944 01073a5d 2019-08-22 stsp const char *
1945 01073a5d 2019-08-22 stsp got_object_tag_get_tagger(struct got_tag_object *tag)
1947 01073a5d 2019-08-22 stsp return tag->tagger;
1950 01073a5d 2019-08-22 stsp const char *
1951 01073a5d 2019-08-22 stsp got_object_tag_get_message(struct got_tag_object *tag)
1953 01073a5d 2019-08-22 stsp return tag->tagmsg;
1956 776d4d29 2018-06-17 stsp static struct got_tree_entry *
1957 65a9bbe9 2018-09-15 stsp find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1961 63da309a 2018-11-07 stsp /* Note that tree entries are sorted in strncmp() order. */
1962 56e0773d 2019-11-28 stsp for (i = 0; i < tree->nentries; i++) {
1963 56e0773d 2019-11-28 stsp struct got_tree_entry *te = &tree->entries[i];
1964 63da309a 2018-11-07 stsp int cmp = strncmp(te->name, name, len);
1965 63da309a 2018-11-07 stsp if (cmp < 0)
1967 63da309a 2018-11-07 stsp if (cmp > 0)
1969 63da309a 2018-11-07 stsp if (te->name[len] == '\0')
1970 776d4d29 2018-06-17 stsp return te;
1972 eb651edf 2018-02-11 stsp return NULL;
1975 56e0773d 2019-11-28 stsp struct got_tree_entry *
1976 a129376b 2019-03-28 stsp got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1978 a129376b 2019-03-28 stsp return find_entry_by_name(tree, name, strlen(name));
1981 776d4d29 2018-06-17 stsp const struct got_error *
1982 67b631c9 2021-10-10 stsp got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1983 67b631c9 2021-10-10 stsp struct got_repository *repo, struct got_tree_object *tree,
1984 67b631c9 2021-10-10 stsp const char *path)
1986 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
1987 67b631c9 2021-10-10 stsp struct got_tree_object *subtree = NULL;
1988 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
1989 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1990 b7cd37e5 2018-11-18 stsp size_t seglen;
1992 27d434c2 2018-09-15 stsp *id = NULL;
1995 5e54fb30 2019-05-31 stsp while (s[0] == '/')
1998 65a9bbe9 2018-09-15 stsp seglen = 0;
1999 67b631c9 2021-10-10 stsp subtree = tree;
2000 b7cd37e5 2018-11-18 stsp while (*s) {
2001 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
2003 776d4d29 2018-06-17 stsp if (*s != '/') {
2010 67b631c9 2021-10-10 stsp te = find_entry_by_name(subtree, seg, seglen);
2011 db37e2c0 2018-06-21 stsp if (te == NULL) {
2012 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2013 776d4d29 2018-06-17 stsp goto done;
2016 b7cd37e5 2018-11-18 stsp if (*s == '\0')
2019 776d4d29 2018-06-17 stsp seg = s + 1;
2020 65a9bbe9 2018-09-15 stsp seglen = 0;
2023 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
2025 db37e2c0 2018-06-21 stsp te = NULL;
2027 776d4d29 2018-06-17 stsp goto done;
2028 67b631c9 2021-10-10 stsp if (subtree != tree)
2029 67b631c9 2021-10-10 stsp got_object_tree_close(subtree);
2030 67b631c9 2021-10-10 stsp subtree = next_tree;
2035 56e0773d 2019-11-28 stsp *id = got_object_id_dup(&te->id);
2036 27d434c2 2018-09-15 stsp if (*id == NULL)
2037 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2039 67b631c9 2021-10-10 stsp *mode = te->mode;
2041 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2043 67b631c9 2021-10-10 stsp if (subtree && subtree != tree)
2044 67b631c9 2021-10-10 stsp got_object_tree_close(subtree);
2045 67b631c9 2021-10-10 stsp return err;
2047 67b631c9 2021-10-10 stsp const struct got_error *
2048 67b631c9 2021-10-10 stsp got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
2049 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
2051 67b631c9 2021-10-10 stsp const struct got_error *err = NULL;
2052 67b631c9 2021-10-10 stsp struct got_tree_object *tree = NULL;
2054 67b631c9 2021-10-10 stsp *id = NULL;
2056 67b631c9 2021-10-10 stsp /* Handle opening of root of commit's tree. */
2057 67b631c9 2021-10-10 stsp if (got_path_is_root_dir(path)) {
2058 67b631c9 2021-10-10 stsp *id = got_object_id_dup(commit->tree_id);
2059 67b631c9 2021-10-10 stsp if (*id == NULL)
2060 67b631c9 2021-10-10 stsp err = got_error_from_errno("got_object_id_dup");
2062 67b631c9 2021-10-10 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
2064 67b631c9 2021-10-10 stsp goto done;
2065 67b631c9 2021-10-10 stsp err = got_object_tree_find_path(id, NULL, repo, tree, path);
2069 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
2070 776d4d29 2018-06-17 stsp return err;
2074 ac5f2b26 2020-05-05 stsp * Normalize file mode bits to avoid false positive tree entry differences
2075 ac5f2b26 2020-05-05 stsp * in case tree entries have unexpected mode bits set.
2077 ac5f2b26 2020-05-05 stsp static mode_t
2078 ac5f2b26 2020-05-05 stsp normalize_mode_for_comparison(mode_t mode)
2081 ac5f2b26 2020-05-05 stsp * For directories, the only relevant bit is the IFDIR bit.
2082 ac5f2b26 2020-05-05 stsp * This allows us to detect paths changing from a directory
2083 ac5f2b26 2020-05-05 stsp * to a file and vice versa.
2085 ac5f2b26 2020-05-05 stsp if (S_ISDIR(mode))
2086 ac5f2b26 2020-05-05 stsp return mode & S_IFDIR;
2089 40dde666 2020-07-23 stsp * For symlinks, the only relevant bit is the IFLNK bit.
2090 40dde666 2020-07-23 stsp * This allows us to detect paths changing from a symlinks
2091 40dde666 2020-07-23 stsp * to a file or directory and vice versa.
2093 40dde666 2020-07-23 stsp if (S_ISLNK(mode))
2094 40dde666 2020-07-23 stsp return mode & S_IFLNK;
2096 ac5f2b26 2020-05-05 stsp /* For files, the only change we care about is the executable bit. */
2097 ac5f2b26 2020-05-05 stsp return mode & S_IXUSR;
2100 07862c20 2018-09-15 stsp const struct got_error *
2101 07862c20 2018-09-15 stsp got_object_tree_path_changed(int *changed,
2102 07862c20 2018-09-15 stsp struct got_tree_object *tree01, struct got_tree_object *tree02,
2103 07862c20 2018-09-15 stsp const char *path, struct got_repository *repo)
2105 07862c20 2018-09-15 stsp const struct got_error *err = NULL;
2106 07862c20 2018-09-15 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2107 07862c20 2018-09-15 stsp struct got_tree_entry *te1 = NULL, *te2 = NULL;
2108 65a9bbe9 2018-09-15 stsp const char *seg, *s;
2109 3b7f9878 2018-11-18 stsp size_t seglen;
2111 07862c20 2018-09-15 stsp *changed = 0;
2113 07862c20 2018-09-15 stsp /* We not do support comparing the root path. */
2114 61a7d79f 2020-02-29 stsp if (got_path_is_root_dir(path))
2115 63f810e6 2020-02-29 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
2117 07862c20 2018-09-15 stsp tree1 = tree01;
2118 07862c20 2018-09-15 stsp tree2 = tree02;
2120 61a7d79f 2020-02-29 stsp while (*s == '/')
2123 65a9bbe9 2018-09-15 stsp seglen = 0;
2124 3b7f9878 2018-11-18 stsp while (*s) {
2125 07862c20 2018-09-15 stsp struct got_tree_object *next_tree1, *next_tree2;
2126 ac5f2b26 2020-05-05 stsp mode_t mode1, mode2;
2128 07862c20 2018-09-15 stsp if (*s != '/') {
2135 65a9bbe9 2018-09-15 stsp te1 = find_entry_by_name(tree1, seg, seglen);
2136 07862c20 2018-09-15 stsp if (te1 == NULL) {
2137 07862c20 2018-09-15 stsp err = got_error(GOT_ERR_NO_OBJ);
2138 07862c20 2018-09-15 stsp goto done;
2141 e8bfb8f3 2020-12-18 stsp if (tree2)
2142 e8bfb8f3 2020-12-18 stsp te2 = find_entry_by_name(tree2, seg, seglen);
2144 e8bfb8f3 2020-12-18 stsp if (te2) {
2145 e8bfb8f3 2020-12-18 stsp mode1 = normalize_mode_for_comparison(te1->mode);
2146 e8bfb8f3 2020-12-18 stsp mode2 = normalize_mode_for_comparison(te2->mode);
2147 e8bfb8f3 2020-12-18 stsp if (mode1 != mode2) {
2148 e8bfb8f3 2020-12-18 stsp *changed = 1;
2149 e8bfb8f3 2020-12-18 stsp goto done;
2152 e8bfb8f3 2020-12-18 stsp if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2153 e8bfb8f3 2020-12-18 stsp *changed = 0;
2154 e8bfb8f3 2020-12-18 stsp goto done;
2158 3b7f9878 2018-11-18 stsp if (*s == '\0') { /* final path element */
2159 07862c20 2018-09-15 stsp *changed = 1;
2160 07862c20 2018-09-15 stsp goto done;
2163 07862c20 2018-09-15 stsp seg = s + 1;
2165 65a9bbe9 2018-09-15 stsp seglen = 0;
2167 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree1, repo,
2168 56e0773d 2019-11-28 stsp &te1->id);
2169 07862c20 2018-09-15 stsp te1 = NULL;
2171 07862c20 2018-09-15 stsp goto done;
2172 a31cea73 2018-09-15 stsp if (tree1 != tree01)
2173 a31cea73 2018-09-15 stsp got_object_tree_close(tree1);
2174 07862c20 2018-09-15 stsp tree1 = next_tree1;
2176 e8bfb8f3 2020-12-18 stsp if (te2) {
2177 e8bfb8f3 2020-12-18 stsp err = got_object_open_as_tree(&next_tree2, repo,
2178 e8bfb8f3 2020-12-18 stsp &te2->id);
2179 e8bfb8f3 2020-12-18 stsp te2 = NULL;
2181 e8bfb8f3 2020-12-18 stsp goto done;
2182 e8bfb8f3 2020-12-18 stsp if (tree2 != tree02)
2183 e8bfb8f3 2020-12-18 stsp got_object_tree_close(tree2);
2184 e8bfb8f3 2020-12-18 stsp tree2 = next_tree2;
2185 e8bfb8f3 2020-12-18 stsp } else if (tree2) {
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 = NULL;
2193 a31cea73 2018-09-15 stsp if (tree1 && tree1 != tree01)
2194 07862c20 2018-09-15 stsp got_object_tree_close(tree1);
2195 a31cea73 2018-09-15 stsp if (tree2 && tree2 != tree02)
2196 07862c20 2018-09-15 stsp got_object_tree_close(tree2);
2197 77880158 2018-11-04 stsp return err;
2200 ed175427 2019-05-09 stsp const struct got_error *
2201 ed175427 2019-05-09 stsp got_object_tree_entry_dup(struct got_tree_entry **new_te,
2202 ed175427 2019-05-09 stsp struct got_tree_entry *te)
2204 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
2206 ed175427 2019-05-09 stsp *new_te = calloc(1, sizeof(**new_te));
2207 ed175427 2019-05-09 stsp if (*new_te == NULL)
2208 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
2210 ed175427 2019-05-09 stsp (*new_te)->mode = te->mode;
2211 56e0773d 2019-11-28 stsp memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2212 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2213 8c4eabf2 2019-05-10 stsp return err;
2217 56e0773d 2019-11-28 stsp got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2219 63c5ca5d 2019-08-24 stsp return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2223 e40622f4 2020-07-23 stsp got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2225 e40622f4 2020-07-23 stsp /* S_IFDIR check avoids confusing symlinks with submodules. */
2226 e40622f4 2020-07-23 stsp return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2229 e40622f4 2020-07-23 stsp static const struct got_error *
2230 e40622f4 2020-07-23 stsp resolve_symlink(char **link_target, const char *path,
2231 a44927cc 2022-04-07 stsp struct got_commit_object *commit, struct got_repository *repo)
2233 e40622f4 2020-07-23 stsp const struct got_error *err = NULL;
2234 dbdd6209 2020-10-19 stsp char buf[PATH_MAX];
2235 e40622f4 2020-07-23 stsp char *name, *parent_path = NULL;
2236 e40622f4 2020-07-23 stsp struct got_object_id *tree_obj_id = NULL;
2237 e40622f4 2020-07-23 stsp struct got_tree_object *tree = NULL;
2238 e40622f4 2020-07-23 stsp struct got_tree_entry *te = NULL;
2240 e40622f4 2020-07-23 stsp *link_target = NULL;
2242 dbdd6209 2020-10-19 stsp if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2243 dbdd6209 2020-10-19 stsp return got_error(GOT_ERR_NO_SPACE);
2245 dbdd6209 2020-10-19 stsp name = basename(buf);
2246 e40622f4 2020-07-23 stsp if (name == NULL)
2247 e40622f4 2020-07-23 stsp return got_error_from_errno2("basename", path);
2249 e40622f4 2020-07-23 stsp err = got_path_dirname(&parent_path, path);
2251 e40622f4 2020-07-23 stsp return err;
2253 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_obj_id, repo, commit,
2254 e40622f4 2020-07-23 stsp parent_path);
2255 e40622f4 2020-07-23 stsp if (err) {
2256 e40622f4 2020-07-23 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2257 e40622f4 2020-07-23 stsp /* Display the complete path in error message. */
2258 e40622f4 2020-07-23 stsp err = got_error_path(path, err->code);
2260 e40622f4 2020-07-23 stsp goto done;
2263 e40622f4 2020-07-23 stsp err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2265 e40622f4 2020-07-23 stsp goto done;
2267 e40622f4 2020-07-23 stsp te = got_object_tree_find_entry(tree, name);
2268 e40622f4 2020-07-23 stsp if (te == NULL) {
2269 e40622f4 2020-07-23 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2270 e40622f4 2020-07-23 stsp goto done;
2273 e40622f4 2020-07-23 stsp if (got_object_tree_entry_is_symlink(te)) {
2274 e40622f4 2020-07-23 stsp err = got_tree_entry_get_symlink_target(link_target, te, repo);
2276 e40622f4 2020-07-23 stsp goto done;
2277 e40622f4 2020-07-23 stsp if (!got_path_is_absolute(*link_target)) {
2278 e40622f4 2020-07-23 stsp char *abspath;
2279 e40622f4 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", parent_path,
2280 e40622f4 2020-07-23 stsp *link_target) == -1) {
2281 e40622f4 2020-07-23 stsp err = got_error_from_errno("asprintf");
2282 e40622f4 2020-07-23 stsp goto done;
2284 e40622f4 2020-07-23 stsp free(*link_target);
2285 e40622f4 2020-07-23 stsp *link_target = malloc(PATH_MAX);
2286 e40622f4 2020-07-23 stsp if (*link_target == NULL) {
2287 e40622f4 2020-07-23 stsp err = got_error_from_errno("malloc");
2288 e40622f4 2020-07-23 stsp goto done;
2290 e40622f4 2020-07-23 stsp err = got_canonpath(abspath, *link_target, PATH_MAX);
2291 e40622f4 2020-07-23 stsp free(abspath);
2293 e40622f4 2020-07-23 stsp goto done;
2297 e40622f4 2020-07-23 stsp free(tree_obj_id);
2299 e40622f4 2020-07-23 stsp got_object_tree_close(tree);
2300 e40622f4 2020-07-23 stsp if (err) {
2301 e40622f4 2020-07-23 stsp free(*link_target);
2302 e40622f4 2020-07-23 stsp *link_target = NULL;
2304 e40622f4 2020-07-23 stsp return err;
2307 ca6e02ac 2020-01-07 stsp const struct got_error *
2308 e40622f4 2020-07-23 stsp got_object_resolve_symlinks(char **link_target, const char *path,
2309 a44927cc 2022-04-07 stsp struct got_commit_object *commit, struct got_repository *repo)
2311 e40622f4 2020-07-23 stsp const struct got_error *err = NULL;
2312 e40622f4 2020-07-23 stsp char *next_target = NULL;
2313 e40622f4 2020-07-23 stsp int max_recursion = 40; /* matches Git */
2315 e40622f4 2020-07-23 stsp *link_target = NULL;
2318 e40622f4 2020-07-23 stsp err = resolve_symlink(&next_target,
2319 a44927cc 2022-04-07 stsp *link_target ? *link_target : path, commit, repo);
2322 e40622f4 2020-07-23 stsp if (next_target) {
2323 e40622f4 2020-07-23 stsp free(*link_target);
2324 e40622f4 2020-07-23 stsp if (--max_recursion == 0) {
2325 e40622f4 2020-07-23 stsp err = got_error_path(path, GOT_ERR_RECURSION);
2326 e40622f4 2020-07-23 stsp *link_target = NULL;
2329 e40622f4 2020-07-23 stsp *link_target = next_target;
2331 e40622f4 2020-07-23 stsp } while (next_target);
2333 e40622f4 2020-07-23 stsp return err;
2336 e40622f4 2020-07-23 stsp const struct got_error *
2337 ca6e02ac 2020-01-07 stsp got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2338 ca6e02ac 2020-01-07 stsp struct got_object_id *commit_id, const char *path,
2339 ca6e02ac 2020-01-07 stsp struct got_repository *repo)
2341 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
2342 ca6e02ac 2020-01-07 stsp struct got_pack *pack = NULL;
2343 ca6e02ac 2020-01-07 stsp struct got_packidx *packidx = NULL;
2344 ca6e02ac 2020-01-07 stsp char *path_packfile = NULL;
2345 ca6e02ac 2020-01-07 stsp struct got_commit_object *changed_commit = NULL;
2346 ca6e02ac 2020-01-07 stsp struct got_object_id *changed_commit_id = NULL;
2349 ca6e02ac 2020-01-07 stsp err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2350 ca6e02ac 2020-01-07 stsp if (err) {
2351 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
2352 ca6e02ac 2020-01-07 stsp return err;
2353 ca6e02ac 2020-01-07 stsp return NULL;
2356 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
2357 aea75d87 2021-07-06 stsp packidx->path_packidx);
2359 ca6e02ac 2020-01-07 stsp return err;
2361 ca6e02ac 2020-01-07 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
2362 ca6e02ac 2020-01-07 stsp if (pack == NULL) {
2363 ca6e02ac 2020-01-07 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2365 ca6e02ac 2020-01-07 stsp goto done;
2368 ca6e02ac 2020-01-07 stsp if (pack->privsep_child == NULL) {
2369 ca6e02ac 2020-01-07 stsp err = start_pack_privsep_child(pack, packidx);
2371 ca6e02ac 2020-01-07 stsp goto done;
2374 ca6e02ac 2020-01-07 stsp err = got_privsep_send_commit_traversal_request(
2375 ca6e02ac 2020-01-07 stsp pack->privsep_child->ibuf, commit_id, idx, path);
2377 ca6e02ac 2020-01-07 stsp goto done;
2379 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_traversed_commits(&changed_commit,
2380 ca6e02ac 2020-01-07 stsp &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2382 ca6e02ac 2020-01-07 stsp goto done;
2384 ca6e02ac 2020-01-07 stsp if (changed_commit) {
2386 ca6e02ac 2020-01-07 stsp * Cache the commit in which the path was changed.
2387 ca6e02ac 2020-01-07 stsp * This commit might be opened again soon.
2389 ca6e02ac 2020-01-07 stsp changed_commit->refcnt++;
2390 ca6e02ac 2020-01-07 stsp err = got_repo_cache_commit(repo, changed_commit_id,
2391 ca6e02ac 2020-01-07 stsp changed_commit);
2392 ca6e02ac 2020-01-07 stsp got_object_commit_close(changed_commit);
2395 ca6e02ac 2020-01-07 stsp free(path_packfile);
2396 ca6e02ac 2020-01-07 stsp free(changed_commit_id);
2397 ca6e02ac 2020-01-07 stsp return err;