2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 d71d75ad 2017-11-05 stsp * Permission to use, copy, modify, and distribute this software for any
5 d71d75ad 2017-11-05 stsp * purpose with or without fee is hereby granted, provided that the above
6 d71d75ad 2017-11-05 stsp * copyright notice and this permission notice appear in all copies.
8 d71d75ad 2017-11-05 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 d71d75ad 2017-11-05 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 d71d75ad 2017-11-05 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 d71d75ad 2017-11-05 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 d71d75ad 2017-11-05 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 d71d75ad 2017-11-05 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 d71d75ad 2017-11-05 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 2178c42e 2018-04-22 stsp #include <sys/types.h>
18 0ffeb3c2 2017-11-26 stsp #include <sys/stat.h>
19 2178c42e 2018-04-22 stsp #include <sys/uio.h>
20 2178c42e 2018-04-22 stsp #include <sys/socket.h>
21 2178c42e 2018-04-22 stsp #include <sys/wait.h>
22 b48e2ddb 2019-05-22 stsp #include <sys/resource.h>
23 2b0ae357 2022-01-10 thomas #include <sys/mman.h>
25 a1fd68d8 2018-01-12 stsp #include <errno.h>
26 2178c42e 2018-04-22 stsp #include <fcntl.h>
27 d71d75ad 2017-11-05 stsp #include <stdio.h>
28 ab9a70b2 2017-11-06 stsp #include <stdlib.h>
29 ab9a70b2 2017-11-06 stsp #include <string.h>
30 2178c42e 2018-04-22 stsp #include <stdint.h>
31 81a12da5 2020-09-09 naddy #include <unistd.h>
32 ab9a70b2 2017-11-06 stsp #include <zlib.h>
33 ab9a70b2 2017-11-06 stsp #include <ctype.h>
34 e40622f4 2020-07-23 stsp #include <libgen.h>
35 ab9a70b2 2017-11-06 stsp #include <limits.h>
36 788c352e 2018-06-16 stsp #include <time.h>
38 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
40 ab9a70b2 2017-11-06 stsp #include "got_error.h"
41 d71d75ad 2017-11-05 stsp #include "got_object.h"
42 ab9a70b2 2017-11-06 stsp #include "got_repository.h"
43 511a516b 2018-05-19 stsp #include "got_opentemp.h"
44 324d37e7 2019-05-11 stsp #include "got_path.h"
46 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
47 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
48 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
49 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
50 2178c42e 2018-04-22 stsp #include "got_lib_privsep.h"
51 6bef87be 2018-09-11 stsp #include "got_lib_object_idcache.h"
52 6bef87be 2018-09-11 stsp #include "got_lib_object_cache.h"
53 ad242220 2018-09-08 stsp #include "got_lib_object_parse.h"
54 15a94983 2018-12-23 stsp #include "got_lib_pack.h"
55 7bb0daa1 2018-06-21 stsp #include "got_lib_repository.h"
58 ab9a70b2 2017-11-06 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 3235492e 2018-04-01 stsp struct got_object_id *
62 3235492e 2018-04-01 stsp got_object_get_id(struct got_object *obj)
64 6402fb3c 2018-09-15 stsp return &obj->id;
67 bacc9935 2018-05-20 stsp const struct got_error *
68 bacc9935 2018-05-20 stsp got_object_get_id_str(char **outbuf, struct got_object *obj)
70 bacc9935 2018-05-20 stsp return got_object_id_str(outbuf, &obj->id);
73 15a94983 2018-12-23 stsp const struct got_error *
74 15a94983 2018-12-23 stsp got_object_get_type(int *type, struct got_repository *repo,
75 15a94983 2018-12-23 stsp struct got_object_id *id)
77 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
78 15a94983 2018-12-23 stsp struct got_object *obj;
80 15a94983 2018-12-23 stsp err = got_object_open(&obj, repo, id);
84 b107e67f 2018-01-19 stsp switch (obj->type) {
85 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_COMMIT:
86 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_TREE:
87 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_BLOB:
88 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
89 15a94983 2018-12-23 stsp *type = obj->type;
92 15a94983 2018-12-23 stsp err = got_error(GOT_ERR_OBJ_TYPE);
96 15a94983 2018-12-23 stsp got_object_close(obj);
100 90bdb554 2019-04-11 stsp const struct got_error *
101 90bdb554 2019-04-11 stsp got_object_get_path(char **path, struct got_object_id *id,
102 90bdb554 2019-04-11 stsp struct got_repository *repo)
104 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
105 7a132809 2018-07-23 stsp char *hex = NULL;
106 41d2888b 2019-08-11 stsp char *path_objects;
108 e6b1056e 2018-04-22 stsp *path = NULL;
110 41d2888b 2019-08-11 stsp path_objects = got_repo_get_path_objects(repo);
111 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
112 638f9024 2019-05-13 stsp return got_error_from_errno("got_repo_get_path_objects");
114 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
118 d1cda826 2017-11-06 stsp if (asprintf(path, "%s/%.2x/%s", path_objects,
119 d1cda826 2017-11-06 stsp id->sha1[0], hex + 2) == -1)
120 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
124 d1cda826 2017-11-06 stsp free(path_objects);
125 d1cda826 2017-11-06 stsp return err;
128 762d73f4 2021-04-10 stsp const struct got_error *
129 762d73f4 2021-04-10 stsp got_object_open_loose_fd(int *fd, struct got_object_id *id,
130 4796fb13 2018-12-23 stsp struct got_repository *repo)
132 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
133 a1fd68d8 2018-01-12 stsp char *path;
135 90bdb554 2019-04-11 stsp err = got_object_get_path(&path, id, repo);
137 d1cda826 2017-11-06 stsp return err;
138 06340621 2021-12-31 thomas *fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
139 d5003b79 2018-04-22 stsp if (*fd == -1) {
140 e82b1d81 2019-07-27 stsp err = got_error_from_errno2("open", path);
144 4558fcd4 2018-01-14 stsp free(path);
145 2090a03d 2018-09-09 stsp return err;
148 2090a03d 2018-09-09 stsp static const struct got_error *
149 a158c901 2018-12-23 stsp request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
150 a158c901 2018-12-23 stsp struct got_object_id *id)
152 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
153 a158c901 2018-12-23 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
155 a158c901 2018-12-23 stsp err = got_privsep_send_packed_obj_req(ibuf, idx, id);
157 a158c901 2018-12-23 stsp return err;
159 a158c901 2018-12-23 stsp err = got_privsep_recv_obj(obj, ibuf);
161 a158c901 2018-12-23 stsp return err;
163 a158c901 2018-12-23 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
165 a158c901 2018-12-23 stsp return NULL;
168 bc1f382f 2022-01-05 thomas /* Create temporary files used during delta application. */
169 59d1e4a0 2021-03-10 stsp static const struct got_error *
170 bc1f382f 2022-01-05 thomas pack_child_send_tempfiles(struct imsgbuf *ibuf, struct got_pack *pack)
172 bc1f382f 2022-01-05 thomas const struct got_error *err;
173 bfb5ee0b 2022-05-31 thomas int basefd = -1, accumfd = -1;
176 bc1f382f 2022-01-05 thomas * For performance reasons, the child will keep reusing the
177 bc1f382f 2022-01-05 thomas * same temporary files during every object request.
178 bc1f382f 2022-01-05 thomas * Opening and closing new files for every object request is
179 bc1f382f 2022-01-05 thomas * too expensive during operations such as 'gotadmin pack'.
181 bc1f382f 2022-01-05 thomas if (pack->child_has_tempfiles)
182 bc1f382f 2022-01-05 thomas return NULL;
184 bfb5ee0b 2022-05-31 thomas basefd = dup(pack->basefd);
185 59d1e4a0 2021-03-10 stsp if (basefd == -1)
186 bfb5ee0b 2022-05-31 thomas return got_error_from_errno("dup");
188 bfb5ee0b 2022-05-31 thomas accumfd = dup(pack->accumfd);
189 bfb5ee0b 2022-05-31 thomas if (accumfd == -1) {
190 bfb5ee0b 2022-05-31 thomas err = got_error_from_errno("dup");
191 bfb5ee0b 2022-05-31 thomas goto done;
194 bc1f382f 2022-01-05 thomas err = got_privsep_send_tmpfd(ibuf, basefd);
196 bfb5ee0b 2022-05-31 thomas goto done;
198 bc1f382f 2022-01-05 thomas err = got_privsep_send_tmpfd(ibuf, accumfd);
200 bfb5ee0b 2022-05-31 thomas if (err) {
201 bfb5ee0b 2022-05-31 thomas if (basefd != -1)
202 bfb5ee0b 2022-05-31 thomas close(basefd);
203 bfb5ee0b 2022-05-31 thomas if (accumfd != -1)
204 bfb5ee0b 2022-05-31 thomas close(accumfd);
206 bfb5ee0b 2022-05-31 thomas pack->child_has_tempfiles = 1;
207 bc1f382f 2022-01-05 thomas return NULL;
210 bc1f382f 2022-01-05 thomas static const struct got_error *
211 bc1f382f 2022-01-05 thomas request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
212 bc1f382f 2022-01-05 thomas int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
214 bc1f382f 2022-01-05 thomas const struct got_error *err = NULL;
215 bc1f382f 2022-01-05 thomas struct imsgbuf *ibuf = pack->privsep_child->ibuf;
216 bc1f382f 2022-01-05 thomas int outfd_child;
218 bc1f382f 2022-01-05 thomas err = pack_child_send_tempfiles(ibuf, pack);
220 bc1f382f 2022-01-05 thomas return err;
222 bc1f382f 2022-01-05 thomas outfd_child = dup(outfd);
223 bc1f382f 2022-01-05 thomas if (outfd_child == -1)
224 bc1f382f 2022-01-05 thomas return got_error_from_errno("dup");
226 59d1e4a0 2021-03-10 stsp err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
228 59d1e4a0 2021-03-10 stsp close(outfd_child);
229 59d1e4a0 2021-03-10 stsp return err;
232 59d1e4a0 2021-03-10 stsp err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
234 59d1e4a0 2021-03-10 stsp return err;
236 59d1e4a0 2021-03-10 stsp err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
238 59d1e4a0 2021-03-10 stsp return err;
240 59d1e4a0 2021-03-10 stsp return NULL;
243 da506691 2019-05-22 stsp static void
244 b48e2ddb 2019-05-22 stsp set_max_datasize(void)
246 b48e2ddb 2019-05-22 stsp struct rlimit rl;
248 b48e2ddb 2019-05-22 stsp if (getrlimit(RLIMIT_DATA, &rl) != 0)
251 b48e2ddb 2019-05-22 stsp rl.rlim_cur = rl.rlim_max;
252 b48e2ddb 2019-05-22 stsp setrlimit(RLIMIT_DATA, &rl);
255 a158c901 2018-12-23 stsp static const struct got_error *
256 711fb6e8 2018-12-23 stsp start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
258 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
259 a158c901 2018-12-23 stsp int imsg_fds[2];
261 a158c901 2018-12-23 stsp struct imsgbuf *ibuf;
263 a158c901 2018-12-23 stsp ibuf = calloc(1, sizeof(*ibuf));
264 a158c901 2018-12-23 stsp if (ibuf == NULL)
265 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
267 a158c901 2018-12-23 stsp pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
268 a158c901 2018-12-23 stsp if (pack->privsep_child == NULL) {
269 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
270 a158c901 2018-12-23 stsp free(ibuf);
271 a158c901 2018-12-23 stsp return err;
273 bc1f382f 2022-01-05 thomas pack->child_has_tempfiles = 0;
274 f9c2e8e5 2022-02-13 thomas pack->child_has_delta_outfd = 0;
276 a158c901 2018-12-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
277 638f9024 2019-05-13 stsp err = got_error_from_errno("socketpair");
281 a158c901 2018-12-23 stsp pid = fork();
282 a158c901 2018-12-23 stsp if (pid == -1) {
283 638f9024 2019-05-13 stsp err = got_error_from_errno("fork");
285 a158c901 2018-12-23 stsp } else if (pid == 0) {
286 b48e2ddb 2019-05-22 stsp set_max_datasize();
287 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
288 a158c901 2018-12-23 stsp pack->path_packfile);
289 a158c901 2018-12-23 stsp /* not reached */
292 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1)
293 638f9024 2019-05-13 stsp return got_error_from_errno("close");
294 a158c901 2018-12-23 stsp pack->privsep_child->imsg_fd = imsg_fds[0];
295 a158c901 2018-12-23 stsp pack->privsep_child->pid = pid;
296 a158c901 2018-12-23 stsp imsg_init(ibuf, imsg_fds[0]);
297 a158c901 2018-12-23 stsp pack->privsep_child->ibuf = ibuf;
299 a158c901 2018-12-23 stsp err = got_privsep_init_pack_child(ibuf, pack, packidx);
301 a158c901 2018-12-23 stsp const struct got_error *child_err;
302 a158c901 2018-12-23 stsp err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
303 a158c901 2018-12-23 stsp child_err = got_privsep_wait_for_child(
304 a158c901 2018-12-23 stsp pack->privsep_child->pid);
305 a158c901 2018-12-23 stsp if (child_err && err == NULL)
306 a158c901 2018-12-23 stsp err = child_err;
310 a158c901 2018-12-23 stsp free(ibuf);
311 a158c901 2018-12-23 stsp free(pack->privsep_child);
312 a158c901 2018-12-23 stsp pack->privsep_child = NULL;
314 a158c901 2018-12-23 stsp return err;
317 711fb6e8 2018-12-23 stsp static const struct got_error *
318 711fb6e8 2018-12-23 stsp read_packed_object_privsep(struct got_object **obj,
319 711fb6e8 2018-12-23 stsp struct got_repository *repo, struct got_pack *pack,
320 711fb6e8 2018-12-23 stsp struct got_packidx *packidx, int idx, struct got_object_id *id)
322 711fb6e8 2018-12-23 stsp const struct got_error *err = NULL;
324 59d1e4a0 2021-03-10 stsp if (pack->privsep_child == NULL) {
325 59d1e4a0 2021-03-10 stsp err = start_pack_privsep_child(pack, packidx);
327 59d1e4a0 2021-03-10 stsp return err;
330 711fb6e8 2018-12-23 stsp return request_packed_object(obj, pack, idx, id);
333 59d1e4a0 2021-03-10 stsp static const struct got_error *
334 59d1e4a0 2021-03-10 stsp read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
335 59d1e4a0 2021-03-10 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
336 59d1e4a0 2021-03-10 stsp struct got_object_id *id)
338 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
340 59d1e4a0 2021-03-10 stsp if (pack->privsep_child == NULL) {
341 59d1e4a0 2021-03-10 stsp err = start_pack_privsep_child(pack, packidx);
343 59d1e4a0 2021-03-10 stsp return err;
346 59d1e4a0 2021-03-10 stsp return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
350 b3d68e7f 2021-07-03 stsp const struct got_error *
351 b3d68e7f 2021-07-03 stsp got_object_open_packed(struct got_object **obj, struct got_object_id *id,
352 2090a03d 2018-09-09 stsp struct got_repository *repo)
354 2090a03d 2018-09-09 stsp const struct got_error *err = NULL;
355 2090a03d 2018-09-09 stsp struct got_pack *pack = NULL;
356 2090a03d 2018-09-09 stsp struct got_packidx *packidx = NULL;
358 2090a03d 2018-09-09 stsp char *path_packfile;
360 2090a03d 2018-09-09 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
362 2090a03d 2018-09-09 stsp return err;
364 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
365 aea75d87 2021-07-06 stsp packidx->path_packidx);
367 2090a03d 2018-09-09 stsp return err;
369 2090a03d 2018-09-09 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
370 2090a03d 2018-09-09 stsp if (pack == NULL) {
371 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
376 a158c901 2018-12-23 stsp err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
380 2090a03d 2018-09-09 stsp free(path_packfile);
381 4558fcd4 2018-01-14 stsp return err;
384 f9c2e8e5 2022-02-13 thomas const struct got_error *
385 f9c2e8e5 2022-02-13 thomas got_object_open_from_packfile(struct got_object **obj, struct got_object_id *id,
386 f9c2e8e5 2022-02-13 thomas struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
387 f9c2e8e5 2022-02-13 thomas struct got_repository *repo)
389 f9c2e8e5 2022-02-13 thomas return read_packed_object_privsep(obj, repo, pack, packidx,
390 f9c2e8e5 2022-02-13 thomas obj_idx, id);
393 f9c2e8e5 2022-02-13 thomas const struct got_error *
394 f9c2e8e5 2022-02-13 thomas got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
395 9249e7e3 2022-05-12 thomas off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
396 9249e7e3 2022-05-12 thomas off_t *delta_out_offset, struct got_object_id **base_id, int delta_cache_fd,
397 f9c2e8e5 2022-02-13 thomas struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
398 f9c2e8e5 2022-02-13 thomas struct got_repository *repo)
400 f9c2e8e5 2022-02-13 thomas const struct got_error *err = NULL;
401 f9c2e8e5 2022-02-13 thomas struct got_pack *pack = NULL;
402 f9c2e8e5 2022-02-13 thomas char *path_packfile;
404 f9c2e8e5 2022-02-13 thomas *base_size = 0;
405 f9c2e8e5 2022-02-13 thomas *result_size = 0;
406 f9c2e8e5 2022-02-13 thomas *delta_size = 0;
407 9249e7e3 2022-05-12 thomas *delta_compressed_size = 0;
408 f9c2e8e5 2022-02-13 thomas *delta_offset = 0;
409 f9c2e8e5 2022-02-13 thomas *delta_out_offset = 0;
411 f9c2e8e5 2022-02-13 thomas err = got_packidx_get_packfile_path(&path_packfile,
412 f9c2e8e5 2022-02-13 thomas packidx->path_packidx);
414 f9c2e8e5 2022-02-13 thomas return err;
416 f9c2e8e5 2022-02-13 thomas pack = got_repo_get_cached_pack(repo, path_packfile);
417 f9c2e8e5 2022-02-13 thomas if (pack == NULL) {
418 f9c2e8e5 2022-02-13 thomas err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
420 f9c2e8e5 2022-02-13 thomas return err;
423 f9c2e8e5 2022-02-13 thomas if (pack->privsep_child == NULL) {
424 f9c2e8e5 2022-02-13 thomas err = start_pack_privsep_child(pack, packidx);
426 f9c2e8e5 2022-02-13 thomas return err;
429 f9c2e8e5 2022-02-13 thomas if (!pack->child_has_delta_outfd) {
430 f9c2e8e5 2022-02-13 thomas int outfd_child;
431 f9c2e8e5 2022-02-13 thomas outfd_child = dup(delta_cache_fd);
432 f9c2e8e5 2022-02-13 thomas if (outfd_child == -1)
433 f9c2e8e5 2022-02-13 thomas return got_error_from_errno("dup");
434 f9c2e8e5 2022-02-13 thomas err = got_privsep_send_raw_delta_outfd(
435 f9c2e8e5 2022-02-13 thomas pack->privsep_child->ibuf, outfd_child);
437 f9c2e8e5 2022-02-13 thomas return err;
438 f9c2e8e5 2022-02-13 thomas pack->child_has_delta_outfd = 1;
441 f9c2e8e5 2022-02-13 thomas err = got_privsep_send_raw_delta_req(pack->privsep_child->ibuf,
442 f9c2e8e5 2022-02-13 thomas obj_idx, id);
444 f9c2e8e5 2022-02-13 thomas return err;
446 f9c2e8e5 2022-02-13 thomas return got_privsep_recv_raw_delta(base_size, result_size, delta_size,
447 9249e7e3 2022-05-12 thomas delta_compressed_size, delta_offset, delta_out_offset, base_id,
448 9249e7e3 2022-05-12 thomas pack->privsep_child->ibuf);
452 7d0d4920 2022-05-12 thomas * XXX This function does not really belong in object.c. It is only here
453 7d0d4920 2022-05-12 thomas * because it needs start_pack_privsep_child(); relevant code should
454 7d0d4920 2022-05-12 thomas * probably be moved to pack.c/pack_create.c.
456 7d0d4920 2022-05-12 thomas const struct got_error *
457 7d0d4920 2022-05-12 thomas got_object_prepare_delta_reuse(struct got_pack **pack,
458 7d0d4920 2022-05-12 thomas struct got_packidx *packidx, int delta_outfd, struct got_repository *repo)
460 7d0d4920 2022-05-12 thomas const struct got_error *err = NULL;
461 7d0d4920 2022-05-12 thomas char *path_packfile = NULL;
463 7d0d4920 2022-05-12 thomas err = got_packidx_get_packfile_path(&path_packfile,
464 7d0d4920 2022-05-12 thomas packidx->path_packidx);
466 7d0d4920 2022-05-12 thomas return err;
468 7d0d4920 2022-05-12 thomas *pack = got_repo_get_cached_pack(repo, path_packfile);
469 7d0d4920 2022-05-12 thomas if (*pack == NULL) {
470 7d0d4920 2022-05-12 thomas err = got_repo_cache_pack(pack, repo, path_packfile, packidx);
472 7d0d4920 2022-05-12 thomas goto done;
474 7d0d4920 2022-05-12 thomas if ((*pack)->privsep_child == NULL) {
475 7d0d4920 2022-05-12 thomas err = start_pack_privsep_child(*pack, packidx);
477 7d0d4920 2022-05-12 thomas goto done;
480 7d0d4920 2022-05-12 thomas if (!(*pack)->child_has_delta_outfd) {
481 7d0d4920 2022-05-12 thomas int outfd_child;
482 7d0d4920 2022-05-12 thomas outfd_child = dup(delta_outfd);
483 7d0d4920 2022-05-12 thomas if (outfd_child == -1) {
484 7d0d4920 2022-05-12 thomas err = got_error_from_errno("dup");
485 7d0d4920 2022-05-12 thomas goto done;
487 7d0d4920 2022-05-12 thomas err = got_privsep_send_raw_delta_outfd(
488 7d0d4920 2022-05-12 thomas (*pack)->privsep_child->ibuf, outfd_child);
490 7d0d4920 2022-05-12 thomas goto done;
491 7d0d4920 2022-05-12 thomas (*pack)->child_has_delta_outfd = 1;
494 7d0d4920 2022-05-12 thomas err = got_privsep_send_delta_reuse_req((*pack)->privsep_child->ibuf);
496 7d0d4920 2022-05-12 thomas free(path_packfile);
497 7d0d4920 2022-05-12 thomas return err;
500 9f2369b0 2018-12-24 stsp static const struct got_error *
501 d5c81d44 2021-07-08 stsp request_object(struct got_object **obj, struct got_object_id *id,
502 d5c81d44 2021-07-08 stsp struct got_repository *repo, int fd)
504 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
505 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
507 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
509 d5c81d44 2021-07-08 stsp err = got_privsep_send_obj_req(ibuf, fd, id);
511 9f2369b0 2018-12-24 stsp return err;
513 9f2369b0 2018-12-24 stsp return got_privsep_recv_obj(obj, ibuf);
516 9f2369b0 2018-12-24 stsp static const struct got_error *
517 59d1e4a0 2021-03-10 stsp request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
518 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo, int infd)
520 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
521 59d1e4a0 2021-03-10 stsp struct imsgbuf *ibuf;
522 59d1e4a0 2021-03-10 stsp int outfd_child;
524 59d1e4a0 2021-03-10 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
526 59d1e4a0 2021-03-10 stsp outfd_child = dup(outfd);
527 59d1e4a0 2021-03-10 stsp if (outfd_child == -1)
528 59d1e4a0 2021-03-10 stsp return got_error_from_errno("dup");
530 d5c81d44 2021-07-08 stsp err = got_privsep_send_raw_obj_req(ibuf, infd, id);
532 59d1e4a0 2021-03-10 stsp return err;
534 59d1e4a0 2021-03-10 stsp err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
536 59d1e4a0 2021-03-10 stsp return err;
538 59d1e4a0 2021-03-10 stsp return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
541 59d1e4a0 2021-03-10 stsp static const struct got_error *
542 59d1e4a0 2021-03-10 stsp start_read_object_child(struct got_repository *repo)
544 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
545 9f2369b0 2018-12-24 stsp int imsg_fds[2];
547 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
549 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
550 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
551 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
553 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
554 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
555 ddc7b220 2019-09-08 stsp free(ibuf);
556 ddc7b220 2019-09-08 stsp return err;
559 9f2369b0 2018-12-24 stsp pid = fork();
560 ddc7b220 2019-09-08 stsp if (pid == -1) {
561 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
562 ddc7b220 2019-09-08 stsp free(ibuf);
563 ddc7b220 2019-09-08 stsp return err;
565 9f2369b0 2018-12-24 stsp else if (pid == 0) {
566 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
567 9f2369b0 2018-12-24 stsp repo->path);
568 9f2369b0 2018-12-24 stsp /* not reached */
571 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
572 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
573 ddc7b220 2019-09-08 stsp free(ibuf);
574 ddc7b220 2019-09-08 stsp return err;
577 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
578 9f2369b0 2018-12-24 stsp imsg_fds[0];
579 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
580 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
581 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
583 59d1e4a0 2021-03-10 stsp return NULL;
586 b3d68e7f 2021-07-03 stsp const struct got_error *
587 b3d68e7f 2021-07-03 stsp got_object_read_header_privsep(struct got_object **obj,
588 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo, int obj_fd)
590 59d1e4a0 2021-03-10 stsp const struct got_error *err;
592 59d1e4a0 2021-03-10 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
593 d5c81d44 2021-07-08 stsp return request_object(obj, id, repo, obj_fd);
595 59d1e4a0 2021-03-10 stsp err = start_read_object_child(repo);
597 7495ec13 2021-04-04 stsp close(obj_fd);
598 59d1e4a0 2021-03-10 stsp return err;
601 d5c81d44 2021-07-08 stsp return request_object(obj, id, repo, obj_fd);
604 59d1e4a0 2021-03-10 stsp static const struct got_error *
605 59d1e4a0 2021-03-10 stsp read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
606 d5c81d44 2021-07-08 stsp int outfd, struct got_object_id *id, struct got_repository *repo,
607 d5c81d44 2021-07-08 stsp int obj_fd)
609 59d1e4a0 2021-03-10 stsp const struct got_error *err;
611 59d1e4a0 2021-03-10 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
612 d5c81d44 2021-07-08 stsp return request_raw_object(outbuf, size, hdrlen, outfd, id,
613 d5c81d44 2021-07-08 stsp repo, obj_fd);
615 59d1e4a0 2021-03-10 stsp err = start_read_object_child(repo);
617 59d1e4a0 2021-03-10 stsp return err;
619 d5c81d44 2021-07-08 stsp return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
623 4558fcd4 2018-01-14 stsp const struct got_error *
624 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
625 4558fcd4 2018-01-14 stsp struct got_object_id *id)
627 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
630 7bb0daa1 2018-06-21 stsp *obj = got_repo_get_cached_object(repo, id);
631 7bb0daa1 2018-06-21 stsp if (*obj != NULL) {
632 7bb0daa1 2018-06-21 stsp (*obj)->refcnt++;
633 7bb0daa1 2018-06-21 stsp return NULL;
636 b3d68e7f 2021-07-03 stsp err = got_object_open_packed(obj, id, repo);
637 e82b1d81 2019-07-27 stsp if (err && err->code != GOT_ERR_NO_OBJ)
638 e82b1d81 2019-07-27 stsp return err;
639 e82b1d81 2019-07-27 stsp if (*obj) {
640 e82b1d81 2019-07-27 stsp (*obj)->refcnt++;
641 e82b1d81 2019-07-27 stsp return got_repo_cache_object(repo, id, *obj);
644 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
646 762d73f4 2021-04-10 stsp if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
647 762d73f4 2021-04-10 stsp err = got_error_no_obj(id);
648 762d73f4 2021-04-10 stsp return err;
651 d5c81d44 2021-07-08 stsp err = got_object_read_header_privsep(obj, id, repo, fd);
653 4558fcd4 2018-01-14 stsp return err;
655 762d73f4 2021-04-10 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
657 59790a32 2018-09-15 stsp (*obj)->refcnt++;
658 762d73f4 2021-04-10 stsp return got_repo_cache_object(repo, id, *obj);
661 8ab9215c 2021-10-15 thomas /* *outfd must be initialized to -1 by caller */
662 59d1e4a0 2021-03-10 stsp const struct got_error *
663 8ab9215c 2021-10-15 thomas got_object_raw_open(struct got_raw_object **obj, int *outfd,
664 f8bb1d3e 2021-10-17 thomas struct got_repository *repo, struct got_object_id *id)
666 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
667 59d1e4a0 2021-03-10 stsp struct got_packidx *packidx = NULL;
669 59d1e4a0 2021-03-10 stsp uint8_t *outbuf = NULL;
670 59d1e4a0 2021-03-10 stsp off_t size = 0;
671 59d1e4a0 2021-03-10 stsp size_t hdrlen = 0;
672 59d1e4a0 2021-03-10 stsp char *path_packfile = NULL;
674 8ab9215c 2021-10-15 thomas *obj = got_repo_get_cached_raw_object(repo, id);
675 8ab9215c 2021-10-15 thomas if (*obj != NULL) {
676 8ab9215c 2021-10-15 thomas (*obj)->refcnt++;
677 8ab9215c 2021-10-15 thomas return NULL;
680 8ab9215c 2021-10-15 thomas if (*outfd == -1) {
681 8ab9215c 2021-10-15 thomas *outfd = got_opentempfd();
682 8ab9215c 2021-10-15 thomas if (*outfd == -1)
683 8ab9215c 2021-10-15 thomas return got_error_from_errno("got_opentempfd");
686 59d1e4a0 2021-03-10 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
687 59d1e4a0 2021-03-10 stsp if (err == NULL) {
688 59d1e4a0 2021-03-10 stsp struct got_pack *pack = NULL;
690 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
691 aea75d87 2021-07-06 stsp packidx->path_packidx);
695 59d1e4a0 2021-03-10 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
696 59d1e4a0 2021-03-10 stsp if (pack == NULL) {
697 59d1e4a0 2021-03-10 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
702 59d1e4a0 2021-03-10 stsp err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
703 8ab9215c 2021-10-15 thomas *outfd, pack, packidx, idx, id);
705 dd3af45a 2021-10-15 thomas goto done;
706 59d1e4a0 2021-03-10 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
709 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
712 8ab9215c 2021-10-15 thomas err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
713 d5c81d44 2021-07-08 stsp id, repo, fd);
715 dd3af45a 2021-10-15 thomas goto done;
718 59d1e4a0 2021-03-10 stsp *obj = calloc(1, sizeof(**obj));
719 59d1e4a0 2021-03-10 stsp if (*obj == NULL) {
720 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("calloc");
723 2b0ae357 2022-01-10 thomas (*obj)->fd = -1;
725 59d1e4a0 2021-03-10 stsp if (outbuf) {
726 59d1e4a0 2021-03-10 stsp (*obj)->data = outbuf;
728 59d1e4a0 2021-03-10 stsp struct stat sb;
729 8ab9215c 2021-10-15 thomas if (fstat(*outfd, &sb) == -1) {
730 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fstat");
734 a8591711 2021-06-18 stsp if (sb.st_size != hdrlen + size) {
735 59d1e4a0 2021-03-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
738 2b0ae357 2022-01-10 thomas #ifndef GOT_PACK_NO_MMAP
739 2b0ae357 2022-01-10 thomas if (hdrlen + size > 0) {
740 2b0ae357 2022-01-10 thomas (*obj)->data = mmap(NULL, hdrlen + size, PROT_READ,
741 2b0ae357 2022-01-10 thomas MAP_PRIVATE, *outfd, 0);
742 2b0ae357 2022-01-10 thomas if ((*obj)->data == MAP_FAILED) {
743 2b0ae357 2022-01-10 thomas if (errno != ENOMEM) {
744 2b0ae357 2022-01-10 thomas err = got_error_from_errno("mmap");
745 2b0ae357 2022-01-10 thomas goto done;
747 2b0ae357 2022-01-10 thomas (*obj)->data = NULL;
749 2b0ae357 2022-01-10 thomas (*obj)->fd = *outfd;
750 2b0ae357 2022-01-10 thomas *outfd = -1;
754 2b0ae357 2022-01-10 thomas if (*outfd != -1) {
755 2b0ae357 2022-01-10 thomas (*obj)->f = fdopen(*outfd, "r");
756 2b0ae357 2022-01-10 thomas if ((*obj)->f == NULL) {
757 2b0ae357 2022-01-10 thomas err = got_error_from_errno("fdopen");
758 2b0ae357 2022-01-10 thomas goto done;
760 2b0ae357 2022-01-10 thomas *outfd = -1;
763 59d1e4a0 2021-03-10 stsp (*obj)->hdrlen = hdrlen;
764 59d1e4a0 2021-03-10 stsp (*obj)->size = size;
765 8ab9215c 2021-10-15 thomas err = got_repo_cache_raw_object(repo, id, *obj);
767 59d1e4a0 2021-03-10 stsp free(path_packfile);
769 59d1e4a0 2021-03-10 stsp if (*obj) {
770 59d1e4a0 2021-03-10 stsp got_object_raw_close(*obj);
771 59d1e4a0 2021-03-10 stsp *obj = NULL;
773 59d1e4a0 2021-03-10 stsp free(outbuf);
775 8ab9215c 2021-10-15 thomas (*obj)->refcnt++;
776 59d1e4a0 2021-03-10 stsp return err;
779 59d1e4a0 2021-03-10 stsp const struct got_error *
780 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
781 6dfa2fd3 2018-02-12 stsp const char *id_str)
783 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
785 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
786 6dd1ece6 2019-11-10 stsp return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
788 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
791 15a94983 2018-12-23 stsp const struct got_error *
792 15a94983 2018-12-23 stsp got_object_resolve_id_str(struct got_object_id **id,
793 15a94983 2018-12-23 stsp struct got_repository *repo, const char *id_str)
795 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
796 15a94983 2018-12-23 stsp struct got_object *obj;
798 15a94983 2018-12-23 stsp err = got_object_open_by_id_str(&obj, repo, id_str);
800 15a94983 2018-12-23 stsp return err;
802 15a94983 2018-12-23 stsp *id = got_object_id_dup(got_object_get_id(obj));
803 15a94983 2018-12-23 stsp got_object_close(obj);
804 15a94983 2018-12-23 stsp if (*id == NULL)
805 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
807 15a94983 2018-12-23 stsp return NULL;
810 434025f3 2018-09-16 stsp static const struct got_error *
811 a158c901 2018-12-23 stsp request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
812 a158c901 2018-12-23 stsp int pack_idx, struct got_object_id *id)
814 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
816 a158c901 2018-12-23 stsp err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
819 a158c901 2018-12-23 stsp return err;
821 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
823 ca6e02ac 2020-01-07 stsp return err;
825 ca6e02ac 2020-01-07 stsp (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
826 ca6e02ac 2020-01-07 stsp return NULL;
829 a158c901 2018-12-23 stsp static const struct got_error *
830 a158c901 2018-12-23 stsp read_packed_commit_privsep(struct got_commit_object **commit,
831 a158c901 2018-12-23 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
832 a158c901 2018-12-23 stsp struct got_object_id *id)
834 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
836 a158c901 2018-12-23 stsp if (pack->privsep_child)
837 a158c901 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
839 711fb6e8 2018-12-23 stsp err = start_pack_privsep_child(pack, packidx);
841 a158c901 2018-12-23 stsp return err;
843 711fb6e8 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
846 9f2369b0 2018-12-24 stsp static const struct got_error *
847 9f2369b0 2018-12-24 stsp request_commit(struct got_commit_object **commit, struct got_repository *repo,
848 d5c81d44 2021-07-08 stsp int fd, struct got_object_id *id)
850 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
851 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
853 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
855 d5c81d44 2021-07-08 stsp err = got_privsep_send_commit_req(ibuf, fd, id, -1);
857 9f2369b0 2018-12-24 stsp return err;
859 9f2369b0 2018-12-24 stsp return got_privsep_recv_commit(commit, ibuf);
862 9f2369b0 2018-12-24 stsp static const struct got_error *
863 9f2369b0 2018-12-24 stsp read_commit_privsep(struct got_commit_object **commit, int obj_fd,
864 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo)
866 ddc7b220 2019-09-08 stsp const struct got_error *err;
867 9f2369b0 2018-12-24 stsp int imsg_fds[2];
869 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
871 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
872 d5c81d44 2021-07-08 stsp return request_commit(commit, repo, obj_fd, id);
874 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
875 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
876 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
878 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
879 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
880 ddc7b220 2019-09-08 stsp free(ibuf);
881 ddc7b220 2019-09-08 stsp return err;
884 9f2369b0 2018-12-24 stsp pid = fork();
885 ddc7b220 2019-09-08 stsp if (pid == -1) {
886 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
887 ddc7b220 2019-09-08 stsp free(ibuf);
888 ddc7b220 2019-09-08 stsp return err;
890 9f2369b0 2018-12-24 stsp else if (pid == 0) {
891 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
892 9f2369b0 2018-12-24 stsp repo->path);
893 9f2369b0 2018-12-24 stsp /* not reached */
896 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
897 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
898 ddc7b220 2019-09-08 stsp free(ibuf);
899 ddc7b220 2019-09-08 stsp return err;
901 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
902 9f2369b0 2018-12-24 stsp imsg_fds[0];
903 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
904 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
905 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
907 d5c81d44 2021-07-08 stsp return request_commit(commit, repo, obj_fd, id);
911 a158c901 2018-12-23 stsp static const struct got_error *
912 434025f3 2018-09-16 stsp open_commit(struct got_commit_object **commit,
913 1785f84a 2018-12-23 stsp struct got_repository *repo, struct got_object_id *id, int check_cache)
915 434025f3 2018-09-16 stsp const struct got_error *err = NULL;
916 1785f84a 2018-12-23 stsp struct got_packidx *packidx = NULL;
918 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
920 434025f3 2018-09-16 stsp if (check_cache) {
921 1785f84a 2018-12-23 stsp *commit = got_repo_get_cached_commit(repo, id);
922 434025f3 2018-09-16 stsp if (*commit != NULL) {
923 434025f3 2018-09-16 stsp (*commit)->refcnt++;
924 434025f3 2018-09-16 stsp return NULL;
927 434025f3 2018-09-16 stsp *commit = NULL;
929 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
930 e82b1d81 2019-07-27 stsp if (err == NULL) {
931 1785f84a 2018-12-23 stsp struct got_pack *pack = NULL;
933 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
934 aea75d87 2021-07-06 stsp packidx->path_packidx);
936 1785f84a 2018-12-23 stsp return err;
938 1785f84a 2018-12-23 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
939 434025f3 2018-09-16 stsp if (pack == NULL) {
940 1785f84a 2018-12-23 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
945 a158c901 2018-12-23 stsp err = read_packed_commit_privsep(commit, pack,
946 1785f84a 2018-12-23 stsp packidx, idx, id);
947 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
950 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
952 e82b1d81 2019-07-27 stsp return err;
953 d5c81d44 2021-07-08 stsp err = read_commit_privsep(commit, fd, id, repo);
956 434025f3 2018-09-16 stsp if (err == NULL) {
957 434025f3 2018-09-16 stsp (*commit)->refcnt++;
958 1785f84a 2018-12-23 stsp err = got_repo_cache_commit(repo, id, *commit);
961 8d2c5ea3 2019-08-13 stsp free(path_packfile);
962 e32baab7 2018-11-05 stsp return err;
965 e32baab7 2018-11-05 stsp const struct got_error *
966 e32baab7 2018-11-05 stsp got_object_open_as_commit(struct got_commit_object **commit,
967 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object_id *id)
969 e32baab7 2018-11-05 stsp *commit = got_repo_get_cached_commit(repo, id);
970 e32baab7 2018-11-05 stsp if (*commit != NULL) {
971 e32baab7 2018-11-05 stsp (*commit)->refcnt++;
972 e32baab7 2018-11-05 stsp return NULL;
975 1785f84a 2018-12-23 stsp return open_commit(commit, repo, id, 0);
978 e32baab7 2018-11-05 stsp const struct got_error *
979 e32baab7 2018-11-05 stsp got_object_commit_open(struct got_commit_object **commit,
980 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object *obj)
982 1785f84a 2018-12-23 stsp return open_commit(commit, repo, got_object_get_id(obj), 1);
985 434025f3 2018-09-16 stsp const struct got_error *
986 dbc6a6b6 2018-07-12 stsp got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
988 dbc6a6b6 2018-07-12 stsp *qid = calloc(1, sizeof(**qid));
989 dbc6a6b6 2018-07-12 stsp if (*qid == NULL)
990 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
992 ec242592 2022-04-22 thomas memcpy(&(*qid)->id, id, sizeof((*qid)->id));
993 9ca9aafb 2021-06-18 stsp return NULL;
996 9ca9aafb 2021-06-18 stsp const struct got_error *
997 9ca9aafb 2021-06-18 stsp got_object_id_queue_copy(const struct got_object_id_queue *src,
998 9ca9aafb 2021-06-18 stsp struct got_object_id_queue *dest)
1000 9ca9aafb 2021-06-18 stsp const struct got_error *err;
1001 9ca9aafb 2021-06-18 stsp struct got_object_qid *qid;
1003 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, src, entry) {
1004 9ca9aafb 2021-06-18 stsp struct got_object_qid *new;
1006 9ca9aafb 2021-06-18 stsp * Deep-copy the object ID only. Let the caller deal
1007 9ca9aafb 2021-06-18 stsp * with setting up the new->data pointer if needed.
1009 ec242592 2022-04-22 thomas err = got_object_qid_alloc(&new, &qid->id);
1010 9ca9aafb 2021-06-18 stsp if (err) {
1011 9ca9aafb 2021-06-18 stsp got_object_id_queue_free(dest);
1012 9ca9aafb 2021-06-18 stsp return err;
1014 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(dest, new, entry);
1017 dbc6a6b6 2018-07-12 stsp return NULL;
1020 a158c901 2018-12-23 stsp static const struct got_error *
1021 13c729f7 2018-12-24 stsp request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
1022 13c729f7 2018-12-24 stsp int pack_idx, struct got_object_id *id)
1024 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
1026 13c729f7 2018-12-24 stsp err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
1027 13c729f7 2018-12-24 stsp pack_idx);
1029 a158c901 2018-12-23 stsp return err;
1031 a158c901 2018-12-23 stsp return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
1034 71eb0e7f 2018-09-16 stsp static const struct got_error *
1035 13c729f7 2018-12-24 stsp read_packed_tree_privsep(struct got_tree_object **tree,
1036 13c729f7 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1037 13c729f7 2018-12-24 stsp struct got_object_id *id)
1039 13c729f7 2018-12-24 stsp const struct got_error *err = NULL;
1041 13c729f7 2018-12-24 stsp if (pack->privsep_child)
1042 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
1044 13c729f7 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
1046 13c729f7 2018-12-24 stsp return err;
1048 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
1051 13c729f7 2018-12-24 stsp static const struct got_error *
1052 9f2369b0 2018-12-24 stsp request_tree(struct got_tree_object **tree, struct got_repository *repo,
1053 d5c81d44 2021-07-08 stsp int fd, struct got_object_id *id)
1055 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1056 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1058 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
1060 d5c81d44 2021-07-08 stsp err = got_privsep_send_tree_req(ibuf, fd, id, -1);
1062 9f2369b0 2018-12-24 stsp return err;
1064 9f2369b0 2018-12-24 stsp return got_privsep_recv_tree(tree, ibuf);
1067 9f2369b0 2018-12-24 stsp const struct got_error *
1068 9f2369b0 2018-12-24 stsp read_tree_privsep(struct got_tree_object **tree, int obj_fd,
1069 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo)
1071 ddc7b220 2019-09-08 stsp const struct got_error *err;
1072 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1073 9f2369b0 2018-12-24 stsp pid_t pid;
1074 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1076 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
1077 d5c81d44 2021-07-08 stsp return request_tree(tree, repo, obj_fd, id);
1079 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1080 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1081 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1083 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1084 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1085 ddc7b220 2019-09-08 stsp free(ibuf);
1086 ddc7b220 2019-09-08 stsp return err;
1089 9f2369b0 2018-12-24 stsp pid = fork();
1090 ddc7b220 2019-09-08 stsp if (pid == -1) {
1091 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1092 ddc7b220 2019-09-08 stsp free(ibuf);
1093 ddc7b220 2019-09-08 stsp return err;
1095 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1096 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1097 9f2369b0 2018-12-24 stsp repo->path);
1098 9f2369b0 2018-12-24 stsp /* not reached */
1101 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
1102 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1103 ddc7b220 2019-09-08 stsp free(ibuf);
1104 ddc7b220 2019-09-08 stsp return err;
1106 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1107 9f2369b0 2018-12-24 stsp imsg_fds[0];
1108 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1109 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1110 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1113 d5c81d44 2021-07-08 stsp return request_tree(tree, repo, obj_fd, id);
1116 9f2369b0 2018-12-24 stsp static const struct got_error *
1117 13c729f7 2018-12-24 stsp open_tree(struct got_tree_object **tree, struct got_repository *repo,
1118 13c729f7 2018-12-24 stsp struct got_object_id *id, int check_cache)
1120 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
1121 13c729f7 2018-12-24 stsp struct got_packidx *packidx = NULL;
1123 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1125 71eb0e7f 2018-09-16 stsp if (check_cache) {
1126 13c729f7 2018-12-24 stsp *tree = got_repo_get_cached_tree(repo, id);
1127 71eb0e7f 2018-09-16 stsp if (*tree != NULL) {
1128 71eb0e7f 2018-09-16 stsp (*tree)->refcnt++;
1129 71eb0e7f 2018-09-16 stsp return NULL;
1132 71eb0e7f 2018-09-16 stsp *tree = NULL;
1134 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1135 e82b1d81 2019-07-27 stsp if (err == NULL) {
1136 13c729f7 2018-12-24 stsp struct got_pack *pack = NULL;
1138 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
1139 aea75d87 2021-07-06 stsp packidx->path_packidx);
1141 13c729f7 2018-12-24 stsp return err;
1143 13c729f7 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1144 e7885405 2018-09-10 stsp if (pack == NULL) {
1145 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1148 8d2c5ea3 2019-08-13 stsp goto done;
1150 13c729f7 2018-12-24 stsp err = read_packed_tree_privsep(tree, pack,
1151 13c729f7 2018-12-24 stsp packidx, idx, id);
1152 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1155 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1157 e82b1d81 2019-07-27 stsp return err;
1158 d5c81d44 2021-07-08 stsp err = read_tree_privsep(tree, fd, id, repo);
1161 f6be5c30 2018-06-22 stsp if (err == NULL) {
1162 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
1163 13c729f7 2018-12-24 stsp err = got_repo_cache_tree(repo, id, *tree);
1166 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1167 776d4d29 2018-06-17 stsp return err;
1170 776d4d29 2018-06-17 stsp const struct got_error *
1171 776d4d29 2018-06-17 stsp got_object_open_as_tree(struct got_tree_object **tree,
1172 776d4d29 2018-06-17 stsp struct got_repository *repo, struct got_object_id *id)
1174 e8eb494a 2018-09-16 stsp *tree = got_repo_get_cached_tree(repo, id);
1175 e8eb494a 2018-09-16 stsp if (*tree != NULL) {
1176 e8eb494a 2018-09-16 stsp (*tree)->refcnt++;
1177 e8eb494a 2018-09-16 stsp return NULL;
1180 13c729f7 2018-12-24 stsp return open_tree(tree, repo, id, 0);
1183 71eb0e7f 2018-09-16 stsp const struct got_error *
1184 71eb0e7f 2018-09-16 stsp got_object_tree_open(struct got_tree_object **tree,
1185 71eb0e7f 2018-09-16 stsp struct got_repository *repo, struct got_object *obj)
1187 13c729f7 2018-12-24 stsp return open_tree(tree, repo, got_object_get_id(obj), 1);
1191 56e0773d 2019-11-28 stsp got_object_tree_get_nentries(struct got_tree_object *tree)
1193 56e0773d 2019-11-28 stsp return tree->nentries;
1196 56e0773d 2019-11-28 stsp struct got_tree_entry *
1197 56e0773d 2019-11-28 stsp got_object_tree_get_first_entry(struct got_tree_object *tree)
1199 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, 0);
1202 56e0773d 2019-11-28 stsp struct got_tree_entry *
1203 56e0773d 2019-11-28 stsp got_object_tree_get_last_entry(struct got_tree_object *tree)
1205 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, tree->nentries - 1);
1208 56e0773d 2019-11-28 stsp struct got_tree_entry *
1209 56e0773d 2019-11-28 stsp got_object_tree_get_entry(struct got_tree_object *tree, int i)
1211 56e0773d 2019-11-28 stsp if (i < 0 || i >= tree->nentries)
1212 56e0773d 2019-11-28 stsp return NULL;
1213 56e0773d 2019-11-28 stsp return &tree->entries[i];
1217 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(struct got_tree_entry *te)
1219 56e0773d 2019-11-28 stsp return te->mode;
1222 56e0773d 2019-11-28 stsp const char *
1223 56e0773d 2019-11-28 stsp got_tree_entry_get_name(struct got_tree_entry *te)
1225 56e0773d 2019-11-28 stsp return &te->name[0];
1228 56e0773d 2019-11-28 stsp struct got_object_id *
1229 56e0773d 2019-11-28 stsp got_tree_entry_get_id(struct got_tree_entry *te)
1231 56e0773d 2019-11-28 stsp return &te->id;
1234 0d6c6ee3 2020-05-20 stsp const struct got_error *
1235 af57b12a 2020-07-23 stsp got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1237 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
1238 32596e16 2020-07-23 stsp size_t len, totlen, hdrlen, offset;
1240 aa092692 2020-07-23 stsp *s = NULL;
1242 659dc16e 2020-07-23 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1243 659dc16e 2020-07-23 stsp totlen = 0;
1244 32596e16 2020-07-23 stsp offset = 0;
1248 659dc16e 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1250 af57b12a 2020-07-23 stsp return err;
1252 659dc16e 2020-07-23 stsp if (len == 0)
1255 659dc16e 2020-07-23 stsp totlen += len - hdrlen;
1256 af57b12a 2020-07-23 stsp p = realloc(*s, totlen + 1);
1257 659dc16e 2020-07-23 stsp if (p == NULL) {
1258 659dc16e 2020-07-23 stsp err = got_error_from_errno("realloc");
1260 af57b12a 2020-07-23 stsp *s = NULL;
1261 af57b12a 2020-07-23 stsp return err;
1264 659dc16e 2020-07-23 stsp /* Skip blob object header first time around. */
1265 af57b12a 2020-07-23 stsp memcpy(*s + offset,
1266 f8f7c882 2020-07-23 stsp got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1267 659dc16e 2020-07-23 stsp hdrlen = 0;
1268 32596e16 2020-07-23 stsp offset = totlen;
1269 659dc16e 2020-07-23 stsp } while (len > 0);
1271 af57b12a 2020-07-23 stsp (*s)[totlen] = '\0';
1272 af57b12a 2020-07-23 stsp return NULL;
1275 af57b12a 2020-07-23 stsp const struct got_error *
1276 af57b12a 2020-07-23 stsp got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1277 af57b12a 2020-07-23 stsp struct got_repository *repo)
1279 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
1280 af57b12a 2020-07-23 stsp struct got_blob_object *blob = NULL;
1282 af57b12a 2020-07-23 stsp *link_target = NULL;
1284 af57b12a 2020-07-23 stsp if (!got_object_tree_entry_is_symlink(te))
1285 af57b12a 2020-07-23 stsp return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1287 af57b12a 2020-07-23 stsp err = got_object_open_as_blob(&blob, repo,
1288 af57b12a 2020-07-23 stsp got_tree_entry_get_id(te), PATH_MAX);
1290 af57b12a 2020-07-23 stsp return err;
1292 af57b12a 2020-07-23 stsp err = got_object_blob_read_to_str(link_target, blob);
1293 af57b12a 2020-07-23 stsp got_object_blob_close(blob);
1294 659dc16e 2020-07-23 stsp if (err) {
1295 659dc16e 2020-07-23 stsp free(*link_target);
1296 659dc16e 2020-07-23 stsp *link_target = NULL;
1298 0d6c6ee3 2020-05-20 stsp return err;
1302 56e0773d 2019-11-28 stsp got_tree_entry_get_index(struct got_tree_entry *te)
1304 56e0773d 2019-11-28 stsp return te->idx;
1307 56e0773d 2019-11-28 stsp struct got_tree_entry *
1308 56e0773d 2019-11-28 stsp got_tree_entry_get_next(struct got_tree_object *tree,
1309 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
1311 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx + 1);
1314 56e0773d 2019-11-28 stsp struct got_tree_entry *
1315 56e0773d 2019-11-28 stsp got_tree_entry_get_prev(struct got_tree_object *tree,
1316 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
1318 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx - 1);
1321 3840f4c9 2018-09-12 stsp static const struct got_error *
1322 ac544f8c 2019-01-13 stsp request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1323 ebc55e2d 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1324 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
1326 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
1327 bc1f382f 2022-01-05 thomas struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1328 bc1f382f 2022-01-05 thomas int outfd_child;
1330 bc1f382f 2022-01-05 thomas err = pack_child_send_tempfiles(ibuf, pack);
1331 bc1f382f 2022-01-05 thomas if (err)
1332 bc1f382f 2022-01-05 thomas return err;
1334 3840f4c9 2018-09-12 stsp outfd_child = dup(outfd);
1335 3840f4c9 2018-09-12 stsp if (outfd_child == -1)
1336 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1338 ebc55e2d 2018-12-24 stsp err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1340 3840f4c9 2018-09-12 stsp return err;
1342 3840f4c9 2018-09-12 stsp err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1343 3840f4c9 2018-09-12 stsp outfd_child);
1344 3840f4c9 2018-09-12 stsp if (err) {
1345 3840f4c9 2018-09-12 stsp return err;
1348 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen,
1349 ebc55e2d 2018-12-24 stsp pack->privsep_child->ibuf);
1351 3840f4c9 2018-09-12 stsp return err;
1353 3840f4c9 2018-09-12 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1354 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1356 3840f4c9 2018-09-12 stsp return err;
1359 ebc55e2d 2018-12-24 stsp static const struct got_error *
1360 ac544f8c 2019-01-13 stsp read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1361 ac544f8c 2019-01-13 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1362 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
1364 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
1366 ebc55e2d 2018-12-24 stsp if (pack->privsep_child == NULL) {
1367 ebc55e2d 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
1369 ebc55e2d 2018-12-24 stsp return err;
1372 ac544f8c 2019-01-13 stsp return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1376 9f2369b0 2018-12-24 stsp static const struct got_error *
1377 ac544f8c 2019-01-13 stsp request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1378 d5c81d44 2021-07-08 stsp int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1380 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1381 9f2369b0 2018-12-24 stsp int outfd_child;
1383 9f2369b0 2018-12-24 stsp outfd_child = dup(outfd);
1384 9f2369b0 2018-12-24 stsp if (outfd_child == -1)
1385 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1387 d5c81d44 2021-07-08 stsp err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1389 9f2369b0 2018-12-24 stsp return err;
1391 9f2369b0 2018-12-24 stsp err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1393 9f2369b0 2018-12-24 stsp return err;
1395 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1397 9f2369b0 2018-12-24 stsp return err;
1399 9f2369b0 2018-12-24 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1400 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
1402 9f2369b0 2018-12-24 stsp return err;
1405 9f2369b0 2018-12-24 stsp static const struct got_error *
1406 ac544f8c 2019-01-13 stsp read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1407 d5c81d44 2021-07-08 stsp int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1409 ddc7b220 2019-09-08 stsp const struct got_error *err;
1410 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1411 9f2369b0 2018-12-24 stsp pid_t pid;
1412 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1414 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1415 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1416 d5c81d44 2021-07-08 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1420 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1421 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1422 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1424 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1425 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1426 ddc7b220 2019-09-08 stsp free(ibuf);
1427 ddc7b220 2019-09-08 stsp return err;
1430 9f2369b0 2018-12-24 stsp pid = fork();
1431 ddc7b220 2019-09-08 stsp if (pid == -1) {
1432 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1433 ddc7b220 2019-09-08 stsp free(ibuf);
1434 ddc7b220 2019-09-08 stsp return err;
1436 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1437 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1438 9f2369b0 2018-12-24 stsp repo->path);
1439 9f2369b0 2018-12-24 stsp /* not reached */
1442 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
1443 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1444 ddc7b220 2019-09-08 stsp free(ibuf);
1445 ddc7b220 2019-09-08 stsp return err;
1447 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1448 9f2369b0 2018-12-24 stsp imsg_fds[0];
1449 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1450 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1451 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1453 d5c81d44 2021-07-08 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1456 ebc55e2d 2018-12-24 stsp static const struct got_error *
1457 ebc55e2d 2018-12-24 stsp open_blob(struct got_blob_object **blob, struct got_repository *repo,
1458 ebc55e2d 2018-12-24 stsp struct got_object_id *id, size_t blocksize)
1460 ebc55e2d 2018-12-24 stsp const struct got_error *err = NULL;
1461 ebc55e2d 2018-12-24 stsp struct got_packidx *packidx = NULL;
1463 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1464 ac544f8c 2019-01-13 stsp uint8_t *outbuf;
1465 e82b1d81 2019-07-27 stsp int outfd;
1466 ebc55e2d 2018-12-24 stsp size_t size, hdrlen;
1467 ebc55e2d 2018-12-24 stsp struct stat sb;
1469 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
1470 4558fcd4 2018-01-14 stsp if (*blob == NULL)
1471 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1473 55da3778 2018-09-10 stsp outfd = got_opentempfd();
1474 55da3778 2018-09-10 stsp if (outfd == -1)
1475 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
1477 062ebb78 2018-07-12 stsp (*blob)->read_buf = malloc(blocksize);
1478 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
1479 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1480 c7254d79 2018-04-24 stsp goto done;
1483 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1484 e82b1d81 2019-07-27 stsp if (err == NULL) {
1485 ebc55e2d 2018-12-24 stsp struct got_pack *pack = NULL;
1487 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
1488 aea75d87 2021-07-06 stsp packidx->path_packidx);
1490 ebc55e2d 2018-12-24 stsp goto done;
1492 ebc55e2d 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1493 55da3778 2018-09-10 stsp if (pack == NULL) {
1494 ebc55e2d 2018-12-24 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1497 55da3778 2018-09-10 stsp goto done;
1499 ac544f8c 2019-01-13 stsp err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1500 ac544f8c 2019-01-13 stsp pack, packidx, idx, id);
1501 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1504 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&infd, id, repo);
1506 e82b1d81 2019-07-27 stsp goto done;
1507 ac544f8c 2019-01-13 stsp err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1508 d5c81d44 2021-07-08 stsp id, repo);
1511 55da3778 2018-09-10 stsp goto done;
1513 de060dff 2018-12-24 stsp if (hdrlen > size) {
1514 ebc55e2d 2018-12-24 stsp err = got_error(GOT_ERR_BAD_OBJ_HDR);
1515 ebc55e2d 2018-12-24 stsp goto done;
1518 ac544f8c 2019-01-13 stsp if (outbuf) {
1519 08578a35 2021-01-22 stsp if (close(outfd) == -1 && err == NULL)
1520 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1521 ac544f8c 2019-01-13 stsp outfd = -1;
1522 ac544f8c 2019-01-13 stsp (*blob)->f = fmemopen(outbuf, size, "rb");
1523 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1524 638f9024 2019-05-13 stsp err = got_error_from_errno("fmemopen");
1525 ac544f8c 2019-01-13 stsp free(outbuf);
1526 ac544f8c 2019-01-13 stsp goto done;
1528 ac544f8c 2019-01-13 stsp (*blob)->data = outbuf;
1530 ac544f8c 2019-01-13 stsp if (fstat(outfd, &sb) == -1) {
1531 638f9024 2019-05-13 stsp err = got_error_from_errno("fstat");
1532 ac544f8c 2019-01-13 stsp goto done;
1535 ac544f8c 2019-01-13 stsp if (sb.st_size != size) {
1536 ac544f8c 2019-01-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1537 ac544f8c 2019-01-13 stsp goto done;
1540 ac544f8c 2019-01-13 stsp (*blob)->f = fdopen(outfd, "rb");
1541 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1542 638f9024 2019-05-13 stsp err = got_error_from_errno("fdopen");
1543 ac544f8c 2019-01-13 stsp close(outfd);
1544 ac544f8c 2019-01-13 stsp outfd = -1;
1545 ac544f8c 2019-01-13 stsp goto done;
1549 ebc55e2d 2018-12-24 stsp (*blob)->hdrlen = hdrlen;
1550 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
1551 ebc55e2d 2018-12-24 stsp memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1554 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1555 55da3778 2018-09-10 stsp if (err) {
1556 55da3778 2018-09-10 stsp if (*blob) {
1557 7baf5860 2019-03-19 stsp got_object_blob_close(*blob);
1558 55da3778 2018-09-10 stsp *blob = NULL;
1559 55da3778 2018-09-10 stsp } else if (outfd != -1)
1560 55da3778 2018-09-10 stsp close(outfd);
1562 a19581a2 2018-06-21 stsp return err;
1565 a19581a2 2018-06-21 stsp const struct got_error *
1566 a19581a2 2018-06-21 stsp got_object_open_as_blob(struct got_blob_object **blob,
1567 a19581a2 2018-06-21 stsp struct got_repository *repo, struct got_object_id *id,
1568 a19581a2 2018-06-21 stsp size_t blocksize)
1570 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, id, blocksize);
1573 ebc55e2d 2018-12-24 stsp const struct got_error *
1574 ebc55e2d 2018-12-24 stsp got_object_blob_open(struct got_blob_object **blob,
1575 ebc55e2d 2018-12-24 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
1577 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1580 fb43ecf1 2019-02-11 stsp const struct got_error *
1581 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
1583 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL;
1584 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
1585 56b63ca4 2021-01-22 stsp if (blob->f && fclose(blob->f) == EOF)
1586 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1587 ac544f8c 2019-01-13 stsp free(blob->data);
1588 68482ea3 2017-11-27 stsp free(blob);
1589 fb43ecf1 2019-02-11 stsp return err;
1593 8ba819a3 2020-07-23 stsp got_object_blob_rewind(struct got_blob_object *blob)
1595 8ba819a3 2020-07-23 stsp if (blob->f)
1596 8ba819a3 2020-07-23 stsp rewind(blob->f);
1600 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1602 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1606 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
1608 f934cf2c 2018-02-12 stsp return blob->hdrlen;
1611 f934cf2c 2018-02-12 stsp const uint8_t *
1612 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
1614 f934cf2c 2018-02-12 stsp return blob->read_buf;
1617 68482ea3 2017-11-27 stsp const struct got_error *
1618 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1622 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1623 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
1624 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
1625 eb651edf 2018-02-11 stsp *outlenp = n;
1626 35e9ba5d 2018-06-21 stsp return NULL;
1629 35e9ba5d 2018-06-21 stsp const struct got_error *
1630 be659d10 2020-11-18 stsp got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1631 6c4c42e0 2019-06-24 stsp off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1633 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
1634 b6752625 2018-12-24 stsp size_t n, len, hdrlen;
1635 84451b3e 2018-07-10 stsp const uint8_t *buf;
1637 c33ebc60 2020-11-18 stsp const int alloc_chunksz = 512;
1638 c33ebc60 2020-11-18 stsp size_t nalloc = 0;
1639 f595d9bd 2019-08-14 stsp off_t off = 0, total_len = 0;
1641 6c4c42e0 2019-06-24 stsp if (line_offsets)
1642 6c4c42e0 2019-06-24 stsp *line_offsets = NULL;
1643 f595d9bd 2019-08-14 stsp if (filesize)
1644 f595d9bd 2019-08-14 stsp *filesize = 0;
1645 84451b3e 2018-07-10 stsp if (nlines)
1646 84451b3e 2018-07-10 stsp *nlines = 0;
1648 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1650 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
1652 35e9ba5d 2018-06-21 stsp return err;
1653 35e9ba5d 2018-06-21 stsp if (len == 0)
1655 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
1656 b02560ec 2019-08-19 stsp i = hdrlen;
1657 f1cbc3bc 2020-11-18 stsp if (nlines) {
1658 f1cbc3bc 2020-11-18 stsp if (line_offsets && *line_offsets == NULL) {
1659 78695fb7 2019-08-12 stsp /* Have some data but perhaps no '\n'. */
1660 78695fb7 2019-08-12 stsp *nlines = 1;
1661 c33ebc60 2020-11-18 stsp nalloc = alloc_chunksz;
1662 c33ebc60 2020-11-18 stsp *line_offsets = calloc(nalloc,
1663 c33ebc60 2020-11-18 stsp sizeof(**line_offsets));
1664 78695fb7 2019-08-12 stsp if (*line_offsets == NULL)
1665 845785d4 2020-02-02 tracey return got_error_from_errno("calloc");
1667 b02560ec 2019-08-19 stsp /* Skip forward over end of first line. */
1668 b02560ec 2019-08-19 stsp while (i < len) {
1669 b02560ec 2019-08-19 stsp if (buf[i] == '\n')
1674 b02560ec 2019-08-19 stsp /* Scan '\n' offsets in remaining chunk of data. */
1675 b02560ec 2019-08-19 stsp while (i < len) {
1676 b02560ec 2019-08-19 stsp if (buf[i] != '\n') {
1680 f595d9bd 2019-08-14 stsp (*nlines)++;
1681 c33ebc60 2020-11-18 stsp if (line_offsets && nalloc < *nlines) {
1682 c33ebc60 2020-11-18 stsp size_t n = *nlines + alloc_chunksz;
1683 78695fb7 2019-08-12 stsp off_t *o = recallocarray(*line_offsets,
1684 c33ebc60 2020-11-18 stsp nalloc, n, sizeof(**line_offsets));
1685 78695fb7 2019-08-12 stsp if (o == NULL) {
1686 78695fb7 2019-08-12 stsp free(*line_offsets);
1687 78695fb7 2019-08-12 stsp *line_offsets = NULL;
1688 78695fb7 2019-08-12 stsp return got_error_from_errno(
1689 78695fb7 2019-08-12 stsp "recallocarray");
1691 78695fb7 2019-08-12 stsp *line_offsets = o;
1692 c33ebc60 2020-11-18 stsp nalloc = n;
1694 f1cbc3bc 2020-11-18 stsp if (line_offsets) {
1695 f1cbc3bc 2020-11-18 stsp off = total_len + i - hdrlen + 1;
1696 f1cbc3bc 2020-11-18 stsp (*line_offsets)[*nlines - 1] = off;
1701 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
1702 454a6b59 2018-12-24 stsp n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1703 b6752625 2018-12-24 stsp if (n != len - hdrlen)
1704 b6752625 2018-12-24 stsp return got_ferror(outfile, GOT_ERR_IO);
1705 f595d9bd 2019-08-14 stsp total_len += len - hdrlen;
1706 35e9ba5d 2018-06-21 stsp hdrlen = 0;
1707 35e9ba5d 2018-06-21 stsp } while (len != 0);
1709 cbe7f848 2019-02-11 stsp if (fflush(outfile) != 0)
1710 638f9024 2019-05-13 stsp return got_error_from_errno("fflush");
1711 35e9ba5d 2018-06-21 stsp rewind(outfile);
1713 f595d9bd 2019-08-14 stsp if (filesize)
1714 f595d9bd 2019-08-14 stsp *filesize = total_len;
1716 776d4d29 2018-06-17 stsp return NULL;
1719 f4a881ce 2018-11-17 stsp static const struct got_error *
1720 268f7291 2018-12-24 stsp request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1721 268f7291 2018-12-24 stsp int pack_idx, struct got_object_id *id)
1723 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
1725 268f7291 2018-12-24 stsp err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1726 268f7291 2018-12-24 stsp pack_idx);
1728 a158c901 2018-12-23 stsp return err;
1730 a158c901 2018-12-23 stsp return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1733 268f7291 2018-12-24 stsp static const struct got_error *
1734 268f7291 2018-12-24 stsp read_packed_tag_privsep(struct got_tag_object **tag,
1735 268f7291 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1736 268f7291 2018-12-24 stsp struct got_object_id *id)
1738 268f7291 2018-12-24 stsp const struct got_error *err = NULL;
1740 268f7291 2018-12-24 stsp if (pack->privsep_child)
1741 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1743 268f7291 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
1745 268f7291 2018-12-24 stsp return err;
1747 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1750 9f2369b0 2018-12-24 stsp static const struct got_error *
1751 9f2369b0 2018-12-24 stsp request_tag(struct got_tag_object **tag, struct got_repository *repo,
1752 d5c81d44 2021-07-08 stsp int fd, struct got_object_id *id)
1754 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1755 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1757 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1759 d5c81d44 2021-07-08 stsp err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1761 9f2369b0 2018-12-24 stsp return err;
1763 9f2369b0 2018-12-24 stsp return got_privsep_recv_tag(tag, ibuf);
1766 9f2369b0 2018-12-24 stsp static const struct got_error *
1767 9f2369b0 2018-12-24 stsp read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1768 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo)
1770 ddc7b220 2019-09-08 stsp const struct got_error *err;
1771 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1772 9f2369b0 2018-12-24 stsp pid_t pid;
1773 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1775 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1776 d5c81d44 2021-07-08 stsp return request_tag(tag, repo, obj_fd, id);
1778 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1779 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1780 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1782 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1783 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1784 ddc7b220 2019-09-08 stsp free(ibuf);
1785 ddc7b220 2019-09-08 stsp return err;
1788 9f2369b0 2018-12-24 stsp pid = fork();
1789 ddc7b220 2019-09-08 stsp if (pid == -1) {
1790 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1791 ddc7b220 2019-09-08 stsp free(ibuf);
1792 ddc7b220 2019-09-08 stsp return err;
1794 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1795 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1796 9f2369b0 2018-12-24 stsp repo->path);
1797 9f2369b0 2018-12-24 stsp /* not reached */
1800 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
1801 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1802 ddc7b220 2019-09-08 stsp free(ibuf);
1803 ddc7b220 2019-09-08 stsp return err;
1805 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1806 9f2369b0 2018-12-24 stsp imsg_fds[0];
1807 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1808 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1809 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1811 d5c81d44 2021-07-08 stsp return request_tag(tag, repo, obj_fd, id);
1814 a158c901 2018-12-23 stsp static const struct got_error *
1815 268f7291 2018-12-24 stsp open_tag(struct got_tag_object **tag, struct got_repository *repo,
1816 268f7291 2018-12-24 stsp struct got_object_id *id, int check_cache)
1818 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1819 268f7291 2018-12-24 stsp struct got_packidx *packidx = NULL;
1821 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1822 5d844a1e 2019-08-13 stsp struct got_object *obj = NULL;
1823 5d844a1e 2019-08-13 stsp int obj_type = GOT_OBJ_TYPE_ANY;
1825 f4a881ce 2018-11-17 stsp if (check_cache) {
1826 268f7291 2018-12-24 stsp *tag = got_repo_get_cached_tag(repo, id);
1827 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1828 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1829 f4a881ce 2018-11-17 stsp return NULL;
1832 f4a881ce 2018-11-17 stsp *tag = NULL;
1834 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1835 e82b1d81 2019-07-27 stsp if (err == NULL) {
1836 268f7291 2018-12-24 stsp struct got_pack *pack = NULL;
1838 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
1839 aea75d87 2021-07-06 stsp packidx->path_packidx);
1841 268f7291 2018-12-24 stsp return err;
1843 268f7291 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1844 f4a881ce 2018-11-17 stsp if (pack == NULL) {
1845 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1848 8d2c5ea3 2019-08-13 stsp goto done;
1851 992eb9d8 2020-02-07 tracey /* Beware of "lightweight" tags: Check object type first. */
1852 5d844a1e 2019-08-13 stsp err = read_packed_object_privsep(&obj, repo, pack, packidx,
1855 5d844a1e 2019-08-13 stsp goto done;
1856 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1857 5d844a1e 2019-08-13 stsp got_object_close(obj);
1858 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG) {
1859 5d844a1e 2019-08-13 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1860 5d844a1e 2019-08-13 stsp goto done;
1862 5d844a1e 2019-08-13 stsp err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1863 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1866 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1868 e82b1d81 2019-07-27 stsp return err;
1869 d5c81d44 2021-07-08 stsp err = got_object_read_header_privsep(&obj, id, repo, fd);
1871 5d844a1e 2019-08-13 stsp return err;
1872 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1873 5d844a1e 2019-08-13 stsp got_object_close(obj);
1874 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
1875 5d844a1e 2019-08-13 stsp return got_error(GOT_ERR_OBJ_TYPE);
1877 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1879 5d844a1e 2019-08-13 stsp return err;
1880 d5c81d44 2021-07-08 stsp err = read_tag_privsep(tag, fd, id, repo);
1883 f4a881ce 2018-11-17 stsp if (err == NULL) {
1884 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1885 268f7291 2018-12-24 stsp err = got_repo_cache_tag(repo, id, *tag);
1888 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1889 f4a881ce 2018-11-17 stsp return err;
1892 f4a881ce 2018-11-17 stsp const struct got_error *
1893 f4a881ce 2018-11-17 stsp got_object_open_as_tag(struct got_tag_object **tag,
1894 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object_id *id)
1896 f4a881ce 2018-11-17 stsp *tag = got_repo_get_cached_tag(repo, id);
1897 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1898 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1899 f4a881ce 2018-11-17 stsp return NULL;
1902 268f7291 2018-12-24 stsp return open_tag(tag, repo, id, 0);
1905 f4a881ce 2018-11-17 stsp const struct got_error *
1906 f4a881ce 2018-11-17 stsp got_object_tag_open(struct got_tag_object **tag,
1907 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object *obj)
1909 268f7291 2018-12-24 stsp return open_tag(tag, repo, got_object_get_id(obj), 1);
1912 d24820bf 2019-08-11 stsp const char *
1913 d24820bf 2019-08-11 stsp got_object_tag_get_name(struct got_tag_object *tag)
1915 d24820bf 2019-08-11 stsp return tag->tag;
1919 0bd18d37 2019-02-01 stsp got_object_tag_get_object_type(struct got_tag_object *tag)
1921 0bd18d37 2019-02-01 stsp return tag->obj_type;
1924 0bd18d37 2019-02-01 stsp struct got_object_id *
1925 0bd18d37 2019-02-01 stsp got_object_tag_get_object_id(struct got_tag_object *tag)
1927 0bd18d37 2019-02-01 stsp return &tag->id;
1931 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_time(struct got_tag_object *tag)
1933 01073a5d 2019-08-22 stsp return tag->tagger_time;
1937 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1939 01073a5d 2019-08-22 stsp return tag->tagger_gmtoff;
1942 01073a5d 2019-08-22 stsp const char *
1943 01073a5d 2019-08-22 stsp got_object_tag_get_tagger(struct got_tag_object *tag)
1945 01073a5d 2019-08-22 stsp return tag->tagger;
1948 01073a5d 2019-08-22 stsp const char *
1949 01073a5d 2019-08-22 stsp got_object_tag_get_message(struct got_tag_object *tag)
1951 01073a5d 2019-08-22 stsp return tag->tagmsg;
1954 776d4d29 2018-06-17 stsp static struct got_tree_entry *
1955 65a9bbe9 2018-09-15 stsp find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1959 63da309a 2018-11-07 stsp /* Note that tree entries are sorted in strncmp() order. */
1960 56e0773d 2019-11-28 stsp for (i = 0; i < tree->nentries; i++) {
1961 56e0773d 2019-11-28 stsp struct got_tree_entry *te = &tree->entries[i];
1962 63da309a 2018-11-07 stsp int cmp = strncmp(te->name, name, len);
1963 63da309a 2018-11-07 stsp if (cmp < 0)
1965 63da309a 2018-11-07 stsp if (cmp > 0)
1967 63da309a 2018-11-07 stsp if (te->name[len] == '\0')
1968 776d4d29 2018-06-17 stsp return te;
1970 eb651edf 2018-02-11 stsp return NULL;
1973 56e0773d 2019-11-28 stsp struct got_tree_entry *
1974 a129376b 2019-03-28 stsp got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1976 a129376b 2019-03-28 stsp return find_entry_by_name(tree, name, strlen(name));
1979 776d4d29 2018-06-17 stsp const struct got_error *
1980 cc8021af 2021-10-12 thomas got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1981 cc8021af 2021-10-12 thomas struct got_repository *repo, struct got_tree_object *tree,
1982 cc8021af 2021-10-12 thomas const char *path)
1984 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
1985 cc8021af 2021-10-12 thomas struct got_tree_object *subtree = NULL;
1986 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
1987 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1988 b7cd37e5 2018-11-18 stsp size_t seglen;
1990 27d434c2 2018-09-15 stsp *id = NULL;
1993 5e54fb30 2019-05-31 stsp while (s[0] == '/')
1996 65a9bbe9 2018-09-15 stsp seglen = 0;
1997 cc8021af 2021-10-12 thomas subtree = tree;
1998 b7cd37e5 2018-11-18 stsp while (*s) {
1999 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
2001 776d4d29 2018-06-17 stsp if (*s != '/') {
2008 cc8021af 2021-10-12 thomas te = find_entry_by_name(subtree, seg, seglen);
2009 db37e2c0 2018-06-21 stsp if (te == NULL) {
2010 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2011 776d4d29 2018-06-17 stsp goto done;
2014 b7cd37e5 2018-11-18 stsp if (*s == '\0')
2017 776d4d29 2018-06-17 stsp seg = s + 1;
2018 65a9bbe9 2018-09-15 stsp seglen = 0;
2021 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
2023 db37e2c0 2018-06-21 stsp te = NULL;
2025 776d4d29 2018-06-17 stsp goto done;
2026 cc8021af 2021-10-12 thomas if (subtree != tree)
2027 cc8021af 2021-10-12 thomas got_object_tree_close(subtree);
2028 cc8021af 2021-10-12 thomas subtree = next_tree;
2033 56e0773d 2019-11-28 stsp *id = got_object_id_dup(&te->id);
2034 27d434c2 2018-09-15 stsp if (*id == NULL)
2035 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2036 cc8021af 2021-10-12 thomas if (mode)
2037 cc8021af 2021-10-12 thomas *mode = te->mode;
2039 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2041 cc8021af 2021-10-12 thomas if (subtree && subtree != tree)
2042 cc8021af 2021-10-12 thomas got_object_tree_close(subtree);
2043 cc8021af 2021-10-12 thomas return err;
2045 cc8021af 2021-10-12 thomas const struct got_error *
2046 cc8021af 2021-10-12 thomas got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
2047 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
2049 cc8021af 2021-10-12 thomas const struct got_error *err = NULL;
2050 cc8021af 2021-10-12 thomas struct got_tree_object *tree = NULL;
2052 cc8021af 2021-10-12 thomas *id = NULL;
2054 cc8021af 2021-10-12 thomas /* Handle opening of root of commit's tree. */
2055 cc8021af 2021-10-12 thomas if (got_path_is_root_dir(path)) {
2056 cc8021af 2021-10-12 thomas *id = got_object_id_dup(commit->tree_id);
2057 cc8021af 2021-10-12 thomas if (*id == NULL)
2058 cc8021af 2021-10-12 thomas err = got_error_from_errno("got_object_id_dup");
2059 cc8021af 2021-10-12 thomas } else {
2060 cc8021af 2021-10-12 thomas err = got_object_open_as_tree(&tree, repo, commit->tree_id);
2061 cc8021af 2021-10-12 thomas if (err)
2062 cc8021af 2021-10-12 thomas goto done;
2063 cc8021af 2021-10-12 thomas err = got_object_tree_find_path(id, NULL, repo, tree, path);
2067 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
2068 776d4d29 2018-06-17 stsp return err;
2072 ac5f2b26 2020-05-05 stsp * Normalize file mode bits to avoid false positive tree entry differences
2073 ac5f2b26 2020-05-05 stsp * in case tree entries have unexpected mode bits set.
2075 ac5f2b26 2020-05-05 stsp static mode_t
2076 ac5f2b26 2020-05-05 stsp normalize_mode_for_comparison(mode_t mode)
2079 ac5f2b26 2020-05-05 stsp * For directories, the only relevant bit is the IFDIR bit.
2080 ac5f2b26 2020-05-05 stsp * This allows us to detect paths changing from a directory
2081 ac5f2b26 2020-05-05 stsp * to a file and vice versa.
2083 ac5f2b26 2020-05-05 stsp if (S_ISDIR(mode))
2084 ac5f2b26 2020-05-05 stsp return mode & S_IFDIR;
2087 40dde666 2020-07-23 stsp * For symlinks, the only relevant bit is the IFLNK bit.
2088 40dde666 2020-07-23 stsp * This allows us to detect paths changing from a symlinks
2089 40dde666 2020-07-23 stsp * to a file or directory and vice versa.
2091 40dde666 2020-07-23 stsp if (S_ISLNK(mode))
2092 40dde666 2020-07-23 stsp return mode & S_IFLNK;
2094 ac5f2b26 2020-05-05 stsp /* For files, the only change we care about is the executable bit. */
2095 ac5f2b26 2020-05-05 stsp return mode & S_IXUSR;
2098 07862c20 2018-09-15 stsp const struct got_error *
2099 07862c20 2018-09-15 stsp got_object_tree_path_changed(int *changed,
2100 07862c20 2018-09-15 stsp struct got_tree_object *tree01, struct got_tree_object *tree02,
2101 07862c20 2018-09-15 stsp const char *path, struct got_repository *repo)
2103 07862c20 2018-09-15 stsp const struct got_error *err = NULL;
2104 07862c20 2018-09-15 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2105 07862c20 2018-09-15 stsp struct got_tree_entry *te1 = NULL, *te2 = NULL;
2106 65a9bbe9 2018-09-15 stsp const char *seg, *s;
2107 3b7f9878 2018-11-18 stsp size_t seglen;
2109 07862c20 2018-09-15 stsp *changed = 0;
2111 07862c20 2018-09-15 stsp /* We not do support comparing the root path. */
2112 61a7d79f 2020-02-29 stsp if (got_path_is_root_dir(path))
2113 63f810e6 2020-02-29 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
2115 07862c20 2018-09-15 stsp tree1 = tree01;
2116 07862c20 2018-09-15 stsp tree2 = tree02;
2118 61a7d79f 2020-02-29 stsp while (*s == '/')
2121 65a9bbe9 2018-09-15 stsp seglen = 0;
2122 3b7f9878 2018-11-18 stsp while (*s) {
2123 07862c20 2018-09-15 stsp struct got_tree_object *next_tree1, *next_tree2;
2124 ac5f2b26 2020-05-05 stsp mode_t mode1, mode2;
2126 07862c20 2018-09-15 stsp if (*s != '/') {
2133 65a9bbe9 2018-09-15 stsp te1 = find_entry_by_name(tree1, seg, seglen);
2134 07862c20 2018-09-15 stsp if (te1 == NULL) {
2135 07862c20 2018-09-15 stsp err = got_error(GOT_ERR_NO_OBJ);
2136 07862c20 2018-09-15 stsp goto done;
2139 e8bfb8f3 2020-12-18 stsp if (tree2)
2140 e8bfb8f3 2020-12-18 stsp te2 = find_entry_by_name(tree2, seg, seglen);
2142 e8bfb8f3 2020-12-18 stsp if (te2) {
2143 e8bfb8f3 2020-12-18 stsp mode1 = normalize_mode_for_comparison(te1->mode);
2144 e8bfb8f3 2020-12-18 stsp mode2 = normalize_mode_for_comparison(te2->mode);
2145 e8bfb8f3 2020-12-18 stsp if (mode1 != mode2) {
2146 e8bfb8f3 2020-12-18 stsp *changed = 1;
2147 e8bfb8f3 2020-12-18 stsp goto done;
2150 e8bfb8f3 2020-12-18 stsp if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2151 e8bfb8f3 2020-12-18 stsp *changed = 0;
2152 e8bfb8f3 2020-12-18 stsp goto done;
2156 3b7f9878 2018-11-18 stsp if (*s == '\0') { /* final path element */
2157 07862c20 2018-09-15 stsp *changed = 1;
2158 07862c20 2018-09-15 stsp goto done;
2161 07862c20 2018-09-15 stsp seg = s + 1;
2163 65a9bbe9 2018-09-15 stsp seglen = 0;
2165 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree1, repo,
2166 56e0773d 2019-11-28 stsp &te1->id);
2167 07862c20 2018-09-15 stsp te1 = NULL;
2169 07862c20 2018-09-15 stsp goto done;
2170 a31cea73 2018-09-15 stsp if (tree1 != tree01)
2171 a31cea73 2018-09-15 stsp got_object_tree_close(tree1);
2172 07862c20 2018-09-15 stsp tree1 = next_tree1;
2174 e8bfb8f3 2020-12-18 stsp if (te2) {
2175 e8bfb8f3 2020-12-18 stsp err = got_object_open_as_tree(&next_tree2, repo,
2176 e8bfb8f3 2020-12-18 stsp &te2->id);
2177 e8bfb8f3 2020-12-18 stsp te2 = NULL;
2179 e8bfb8f3 2020-12-18 stsp goto done;
2180 e8bfb8f3 2020-12-18 stsp if (tree2 != tree02)
2181 e8bfb8f3 2020-12-18 stsp got_object_tree_close(tree2);
2182 e8bfb8f3 2020-12-18 stsp tree2 = next_tree2;
2183 e8bfb8f3 2020-12-18 stsp } else if (tree2) {
2184 e8bfb8f3 2020-12-18 stsp if (tree2 != tree02)
2185 e8bfb8f3 2020-12-18 stsp got_object_tree_close(tree2);
2186 e8bfb8f3 2020-12-18 stsp tree2 = NULL;
2191 a31cea73 2018-09-15 stsp if (tree1 && tree1 != tree01)
2192 07862c20 2018-09-15 stsp got_object_tree_close(tree1);
2193 a31cea73 2018-09-15 stsp if (tree2 && tree2 != tree02)
2194 07862c20 2018-09-15 stsp got_object_tree_close(tree2);
2195 77880158 2018-11-04 stsp return err;
2198 ed175427 2019-05-09 stsp const struct got_error *
2199 ed175427 2019-05-09 stsp got_object_tree_entry_dup(struct got_tree_entry **new_te,
2200 ed175427 2019-05-09 stsp struct got_tree_entry *te)
2202 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
2204 ed175427 2019-05-09 stsp *new_te = calloc(1, sizeof(**new_te));
2205 ed175427 2019-05-09 stsp if (*new_te == NULL)
2206 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
2208 ed175427 2019-05-09 stsp (*new_te)->mode = te->mode;
2209 56e0773d 2019-11-28 stsp memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2210 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2211 8c4eabf2 2019-05-10 stsp return err;
2215 56e0773d 2019-11-28 stsp got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2217 63c5ca5d 2019-08-24 stsp return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2221 e40622f4 2020-07-23 stsp got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2223 e40622f4 2020-07-23 stsp /* S_IFDIR check avoids confusing symlinks with submodules. */
2224 e40622f4 2020-07-23 stsp return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2227 e40622f4 2020-07-23 stsp static const struct got_error *
2228 e40622f4 2020-07-23 stsp resolve_symlink(char **link_target, const char *path,
2229 945f9229 2022-04-16 thomas struct got_commit_object *commit, struct got_repository *repo)
2231 e40622f4 2020-07-23 stsp const struct got_error *err = NULL;
2232 dbdd6209 2020-10-19 stsp char buf[PATH_MAX];
2233 e40622f4 2020-07-23 stsp char *name, *parent_path = NULL;
2234 e40622f4 2020-07-23 stsp struct got_object_id *tree_obj_id = NULL;
2235 e40622f4 2020-07-23 stsp struct got_tree_object *tree = NULL;
2236 e40622f4 2020-07-23 stsp struct got_tree_entry *te = NULL;
2238 e40622f4 2020-07-23 stsp *link_target = NULL;
2240 dbdd6209 2020-10-19 stsp if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2241 dbdd6209 2020-10-19 stsp return got_error(GOT_ERR_NO_SPACE);
2243 dbdd6209 2020-10-19 stsp name = basename(buf);
2244 e40622f4 2020-07-23 stsp if (name == NULL)
2245 e40622f4 2020-07-23 stsp return got_error_from_errno2("basename", path);
2247 e40622f4 2020-07-23 stsp err = got_path_dirname(&parent_path, path);
2249 e40622f4 2020-07-23 stsp return err;
2251 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_obj_id, repo, commit,
2252 e40622f4 2020-07-23 stsp parent_path);
2253 e40622f4 2020-07-23 stsp if (err) {
2254 e40622f4 2020-07-23 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2255 e40622f4 2020-07-23 stsp /* Display the complete path in error message. */
2256 e40622f4 2020-07-23 stsp err = got_error_path(path, err->code);
2258 e40622f4 2020-07-23 stsp goto done;
2261 e40622f4 2020-07-23 stsp err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2263 e40622f4 2020-07-23 stsp goto done;
2265 e40622f4 2020-07-23 stsp te = got_object_tree_find_entry(tree, name);
2266 e40622f4 2020-07-23 stsp if (te == NULL) {
2267 e40622f4 2020-07-23 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2268 e40622f4 2020-07-23 stsp goto done;
2271 e40622f4 2020-07-23 stsp if (got_object_tree_entry_is_symlink(te)) {
2272 e40622f4 2020-07-23 stsp err = got_tree_entry_get_symlink_target(link_target, te, repo);
2274 e40622f4 2020-07-23 stsp goto done;
2275 e40622f4 2020-07-23 stsp if (!got_path_is_absolute(*link_target)) {
2276 e40622f4 2020-07-23 stsp char *abspath;
2277 e40622f4 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", parent_path,
2278 e40622f4 2020-07-23 stsp *link_target) == -1) {
2279 e40622f4 2020-07-23 stsp err = got_error_from_errno("asprintf");
2280 e40622f4 2020-07-23 stsp goto done;
2282 e40622f4 2020-07-23 stsp free(*link_target);
2283 e40622f4 2020-07-23 stsp *link_target = malloc(PATH_MAX);
2284 e40622f4 2020-07-23 stsp if (*link_target == NULL) {
2285 e40622f4 2020-07-23 stsp err = got_error_from_errno("malloc");
2286 e40622f4 2020-07-23 stsp goto done;
2288 e40622f4 2020-07-23 stsp err = got_canonpath(abspath, *link_target, PATH_MAX);
2289 e40622f4 2020-07-23 stsp free(abspath);
2291 e40622f4 2020-07-23 stsp goto done;
2295 e40622f4 2020-07-23 stsp free(tree_obj_id);
2297 e40622f4 2020-07-23 stsp got_object_tree_close(tree);
2298 e40622f4 2020-07-23 stsp if (err) {
2299 e40622f4 2020-07-23 stsp free(*link_target);
2300 e40622f4 2020-07-23 stsp *link_target = NULL;
2302 e40622f4 2020-07-23 stsp return err;
2305 ca6e02ac 2020-01-07 stsp const struct got_error *
2306 e40622f4 2020-07-23 stsp got_object_resolve_symlinks(char **link_target, const char *path,
2307 945f9229 2022-04-16 thomas struct got_commit_object *commit, struct got_repository *repo)
2309 e40622f4 2020-07-23 stsp const struct got_error *err = NULL;
2310 e40622f4 2020-07-23 stsp char *next_target = NULL;
2311 e40622f4 2020-07-23 stsp int max_recursion = 40; /* matches Git */
2313 e40622f4 2020-07-23 stsp *link_target = NULL;
2316 e40622f4 2020-07-23 stsp err = resolve_symlink(&next_target,
2317 945f9229 2022-04-16 thomas *link_target ? *link_target : path, commit, repo);
2320 e40622f4 2020-07-23 stsp if (next_target) {
2321 e40622f4 2020-07-23 stsp free(*link_target);
2322 e40622f4 2020-07-23 stsp if (--max_recursion == 0) {
2323 e40622f4 2020-07-23 stsp err = got_error_path(path, GOT_ERR_RECURSION);
2324 e40622f4 2020-07-23 stsp *link_target = NULL;
2327 e40622f4 2020-07-23 stsp *link_target = next_target;
2329 e40622f4 2020-07-23 stsp } while (next_target);
2331 e40622f4 2020-07-23 stsp return err;
2334 e40622f4 2020-07-23 stsp const struct got_error *
2335 ca6e02ac 2020-01-07 stsp got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2336 ca6e02ac 2020-01-07 stsp struct got_object_id *commit_id, const char *path,
2337 ca6e02ac 2020-01-07 stsp struct got_repository *repo)
2339 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
2340 ca6e02ac 2020-01-07 stsp struct got_pack *pack = NULL;
2341 ca6e02ac 2020-01-07 stsp struct got_packidx *packidx = NULL;
2342 ca6e02ac 2020-01-07 stsp char *path_packfile = NULL;
2343 ca6e02ac 2020-01-07 stsp struct got_commit_object *changed_commit = NULL;
2344 ca6e02ac 2020-01-07 stsp struct got_object_id *changed_commit_id = NULL;
2347 ca6e02ac 2020-01-07 stsp err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2348 ca6e02ac 2020-01-07 stsp if (err) {
2349 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
2350 ca6e02ac 2020-01-07 stsp return err;
2351 ca6e02ac 2020-01-07 stsp return NULL;
2354 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
2355 aea75d87 2021-07-06 stsp packidx->path_packidx);
2357 ca6e02ac 2020-01-07 stsp return err;
2359 ca6e02ac 2020-01-07 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
2360 ca6e02ac 2020-01-07 stsp if (pack == NULL) {
2361 ca6e02ac 2020-01-07 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2363 ca6e02ac 2020-01-07 stsp goto done;
2366 ca6e02ac 2020-01-07 stsp if (pack->privsep_child == NULL) {
2367 ca6e02ac 2020-01-07 stsp err = start_pack_privsep_child(pack, packidx);
2369 ca6e02ac 2020-01-07 stsp goto done;
2372 ca6e02ac 2020-01-07 stsp err = got_privsep_send_commit_traversal_request(
2373 ca6e02ac 2020-01-07 stsp pack->privsep_child->ibuf, commit_id, idx, path);
2375 ca6e02ac 2020-01-07 stsp goto done;
2377 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_traversed_commits(&changed_commit,
2378 ca6e02ac 2020-01-07 stsp &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2380 ca6e02ac 2020-01-07 stsp goto done;
2382 ca6e02ac 2020-01-07 stsp if (changed_commit) {
2384 ca6e02ac 2020-01-07 stsp * Cache the commit in which the path was changed.
2385 ca6e02ac 2020-01-07 stsp * This commit might be opened again soon.
2387 ca6e02ac 2020-01-07 stsp changed_commit->refcnt++;
2388 ca6e02ac 2020-01-07 stsp err = got_repo_cache_commit(repo, changed_commit_id,
2389 ca6e02ac 2020-01-07 stsp changed_commit);
2390 ca6e02ac 2020-01-07 stsp got_object_commit_close(changed_commit);
2393 ca6e02ac 2020-01-07 stsp free(path_packfile);
2394 ca6e02ac 2020-01-07 stsp free(changed_commit_id);
2395 ca6e02ac 2020-01-07 stsp return err;