2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 d71d75ad 2017-11-05 stsp * Permission to use, copy, modify, and distribute this software for any
5 d71d75ad 2017-11-05 stsp * purpose with or without fee is hereby granted, provided that the above
6 d71d75ad 2017-11-05 stsp * copyright notice and this permission notice appear in all copies.
8 d71d75ad 2017-11-05 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 d71d75ad 2017-11-05 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 d71d75ad 2017-11-05 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 d71d75ad 2017-11-05 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 d71d75ad 2017-11-05 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 d71d75ad 2017-11-05 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 d71d75ad 2017-11-05 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 2178c42e 2018-04-22 stsp #include <sys/types.h>
18 0ffeb3c2 2017-11-26 stsp #include <sys/stat.h>
19 d1cda826 2017-11-06 stsp #include <sys/queue.h>
20 f8b19efd 2021-10-13 stsp #include <sys/tree.h>
21 2178c42e 2018-04-22 stsp #include <sys/uio.h>
22 2178c42e 2018-04-22 stsp #include <sys/socket.h>
23 2178c42e 2018-04-22 stsp #include <sys/wait.h>
24 b48e2ddb 2019-05-22 stsp #include <sys/resource.h>
26 a1fd68d8 2018-01-12 stsp #include <errno.h>
27 2178c42e 2018-04-22 stsp #include <fcntl.h>
28 d71d75ad 2017-11-05 stsp #include <stdio.h>
29 ab9a70b2 2017-11-06 stsp #include <stdlib.h>
30 ab9a70b2 2017-11-06 stsp #include <string.h>
31 2178c42e 2018-04-22 stsp #include <stdint.h>
32 d71d75ad 2017-11-05 stsp #include <sha1.h>
33 81a12da5 2020-09-09 naddy #include <unistd.h>
34 ab9a70b2 2017-11-06 stsp #include <zlib.h>
35 ab9a70b2 2017-11-06 stsp #include <ctype.h>
36 e40622f4 2020-07-23 stsp #include <libgen.h>
37 ab9a70b2 2017-11-06 stsp #include <limits.h>
38 2178c42e 2018-04-22 stsp #include <imsg.h>
39 788c352e 2018-06-16 stsp #include <time.h>
41 ab9a70b2 2017-11-06 stsp #include "got_error.h"
42 d71d75ad 2017-11-05 stsp #include "got_object.h"
43 ab9a70b2 2017-11-06 stsp #include "got_repository.h"
44 511a516b 2018-05-19 stsp #include "got_opentemp.h"
45 324d37e7 2019-05-11 stsp #include "got_path.h"
47 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
48 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
49 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
51 2178c42e 2018-04-22 stsp #include "got_lib_privsep.h"
52 6bef87be 2018-09-11 stsp #include "got_lib_object_idcache.h"
53 6bef87be 2018-09-11 stsp #include "got_lib_object_cache.h"
54 ad242220 2018-09-08 stsp #include "got_lib_object_parse.h"
55 15a94983 2018-12-23 stsp #include "got_lib_pack.h"
56 7bb0daa1 2018-06-21 stsp #include "got_lib_repository.h"
59 ab9a70b2 2017-11-06 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 3235492e 2018-04-01 stsp struct got_object_id *
63 3235492e 2018-04-01 stsp got_object_get_id(struct got_object *obj)
65 6402fb3c 2018-09-15 stsp return &obj->id;
68 bacc9935 2018-05-20 stsp const struct got_error *
69 bacc9935 2018-05-20 stsp got_object_get_id_str(char **outbuf, struct got_object *obj)
71 bacc9935 2018-05-20 stsp return got_object_id_str(outbuf, &obj->id);
74 15a94983 2018-12-23 stsp const struct got_error *
75 15a94983 2018-12-23 stsp got_object_get_type(int *type, struct got_repository *repo,
76 15a94983 2018-12-23 stsp struct got_object_id *id)
78 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
79 15a94983 2018-12-23 stsp struct got_object *obj;
81 15a94983 2018-12-23 stsp err = got_object_open(&obj, repo, id);
85 b107e67f 2018-01-19 stsp switch (obj->type) {
86 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_COMMIT:
87 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_TREE:
88 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_BLOB:
89 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
90 15a94983 2018-12-23 stsp *type = obj->type;
93 15a94983 2018-12-23 stsp err = got_error(GOT_ERR_OBJ_TYPE);
97 15a94983 2018-12-23 stsp got_object_close(obj);
101 90bdb554 2019-04-11 stsp const struct got_error *
102 90bdb554 2019-04-11 stsp got_object_get_path(char **path, struct got_object_id *id,
103 90bdb554 2019-04-11 stsp struct got_repository *repo)
105 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
106 7a132809 2018-07-23 stsp char *hex = NULL;
107 41d2888b 2019-08-11 stsp char *path_objects;
109 e6b1056e 2018-04-22 stsp *path = NULL;
111 41d2888b 2019-08-11 stsp path_objects = got_repo_get_path_objects(repo);
112 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
113 638f9024 2019-05-13 stsp return got_error_from_errno("got_repo_get_path_objects");
115 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
119 d1cda826 2017-11-06 stsp if (asprintf(path, "%s/%.2x/%s", path_objects,
120 d1cda826 2017-11-06 stsp id->sha1[0], hex + 2) == -1)
121 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
125 d1cda826 2017-11-06 stsp free(path_objects);
126 d1cda826 2017-11-06 stsp return err;
129 762d73f4 2021-04-10 stsp const struct got_error *
130 762d73f4 2021-04-10 stsp got_object_open_loose_fd(int *fd, struct got_object_id *id,
131 4796fb13 2018-12-23 stsp struct got_repository *repo)
133 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
134 a1fd68d8 2018-01-12 stsp char *path;
136 90bdb554 2019-04-11 stsp err = got_object_get_path(&path, id, repo);
138 d1cda826 2017-11-06 stsp return err;
139 a5b57ccf 2019-04-11 stsp *fd = open(path, O_RDONLY | O_NOFOLLOW);
140 d5003b79 2018-04-22 stsp if (*fd == -1) {
141 e82b1d81 2019-07-27 stsp err = got_error_from_errno2("open", path);
145 4558fcd4 2018-01-14 stsp free(path);
146 2090a03d 2018-09-09 stsp return err;
149 2090a03d 2018-09-09 stsp static const struct got_error *
150 a158c901 2018-12-23 stsp request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
151 a158c901 2018-12-23 stsp struct got_object_id *id)
153 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
154 a158c901 2018-12-23 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
156 a158c901 2018-12-23 stsp err = got_privsep_send_packed_obj_req(ibuf, idx, id);
158 a158c901 2018-12-23 stsp return err;
160 a158c901 2018-12-23 stsp err = got_privsep_recv_obj(obj, ibuf);
162 a158c901 2018-12-23 stsp return err;
164 a158c901 2018-12-23 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
166 a158c901 2018-12-23 stsp return NULL;
169 59d1e4a0 2021-03-10 stsp static const struct got_error *
170 59d1e4a0 2021-03-10 stsp request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
171 59d1e4a0 2021-03-10 stsp int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
173 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
174 59d1e4a0 2021-03-10 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
175 59d1e4a0 2021-03-10 stsp int outfd_child;
176 59d1e4a0 2021-03-10 stsp int basefd, accumfd; /* temporary files for delta application */
178 59d1e4a0 2021-03-10 stsp basefd = got_opentempfd();
179 59d1e4a0 2021-03-10 stsp if (basefd == -1)
180 59d1e4a0 2021-03-10 stsp return got_error_from_errno("got_opentempfd");
182 59d1e4a0 2021-03-10 stsp accumfd = got_opentempfd();
183 59d1e4a0 2021-03-10 stsp if (accumfd == -1) {
184 59d1e4a0 2021-03-10 stsp close(basefd);
185 59d1e4a0 2021-03-10 stsp return got_error_from_errno("got_opentempfd");
188 59d1e4a0 2021-03-10 stsp outfd_child = dup(outfd);
189 59d1e4a0 2021-03-10 stsp if (outfd_child == -1) {
190 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("dup");
191 59d1e4a0 2021-03-10 stsp close(basefd);
192 59d1e4a0 2021-03-10 stsp close(accumfd);
193 59d1e4a0 2021-03-10 stsp return err;
196 59d1e4a0 2021-03-10 stsp err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
198 59d1e4a0 2021-03-10 stsp close(basefd);
199 59d1e4a0 2021-03-10 stsp close(accumfd);
200 59d1e4a0 2021-03-10 stsp close(outfd_child);
201 59d1e4a0 2021-03-10 stsp return err;
204 59d1e4a0 2021-03-10 stsp err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
206 59d1e4a0 2021-03-10 stsp close(basefd);
207 59d1e4a0 2021-03-10 stsp close(accumfd);
208 59d1e4a0 2021-03-10 stsp return err;
212 59d1e4a0 2021-03-10 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
215 59d1e4a0 2021-03-10 stsp close(accumfd);
216 59d1e4a0 2021-03-10 stsp return err;
219 59d1e4a0 2021-03-10 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
222 59d1e4a0 2021-03-10 stsp return err;
224 59d1e4a0 2021-03-10 stsp err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
226 59d1e4a0 2021-03-10 stsp return err;
228 59d1e4a0 2021-03-10 stsp return NULL;
231 da506691 2019-05-22 stsp static void
232 b48e2ddb 2019-05-22 stsp set_max_datasize(void)
234 b48e2ddb 2019-05-22 stsp struct rlimit rl;
236 b48e2ddb 2019-05-22 stsp if (getrlimit(RLIMIT_DATA, &rl) != 0)
239 b48e2ddb 2019-05-22 stsp rl.rlim_cur = rl.rlim_max;
240 b48e2ddb 2019-05-22 stsp setrlimit(RLIMIT_DATA, &rl);
243 a158c901 2018-12-23 stsp static const struct got_error *
244 711fb6e8 2018-12-23 stsp start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
246 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
247 a158c901 2018-12-23 stsp int imsg_fds[2];
249 a158c901 2018-12-23 stsp struct imsgbuf *ibuf;
251 a158c901 2018-12-23 stsp ibuf = calloc(1, sizeof(*ibuf));
252 a158c901 2018-12-23 stsp if (ibuf == NULL)
253 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
255 a158c901 2018-12-23 stsp pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
256 a158c901 2018-12-23 stsp if (pack->privsep_child == NULL) {
257 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
258 a158c901 2018-12-23 stsp free(ibuf);
259 a158c901 2018-12-23 stsp return err;
262 a158c901 2018-12-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
263 638f9024 2019-05-13 stsp err = got_error_from_errno("socketpair");
267 a158c901 2018-12-23 stsp pid = fork();
268 a158c901 2018-12-23 stsp if (pid == -1) {
269 638f9024 2019-05-13 stsp err = got_error_from_errno("fork");
271 a158c901 2018-12-23 stsp } else if (pid == 0) {
272 b48e2ddb 2019-05-22 stsp set_max_datasize();
273 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
274 a158c901 2018-12-23 stsp pack->path_packfile);
275 a158c901 2018-12-23 stsp /* not reached */
278 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1)
279 638f9024 2019-05-13 stsp return got_error_from_errno("close");
280 a158c901 2018-12-23 stsp pack->privsep_child->imsg_fd = imsg_fds[0];
281 a158c901 2018-12-23 stsp pack->privsep_child->pid = pid;
282 a158c901 2018-12-23 stsp imsg_init(ibuf, imsg_fds[0]);
283 a158c901 2018-12-23 stsp pack->privsep_child->ibuf = ibuf;
285 a158c901 2018-12-23 stsp err = got_privsep_init_pack_child(ibuf, pack, packidx);
287 a158c901 2018-12-23 stsp const struct got_error *child_err;
288 a158c901 2018-12-23 stsp err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
289 a158c901 2018-12-23 stsp child_err = got_privsep_wait_for_child(
290 a158c901 2018-12-23 stsp pack->privsep_child->pid);
291 a158c901 2018-12-23 stsp if (child_err && err == NULL)
292 a158c901 2018-12-23 stsp err = child_err;
296 a158c901 2018-12-23 stsp free(ibuf);
297 a158c901 2018-12-23 stsp free(pack->privsep_child);
298 a158c901 2018-12-23 stsp pack->privsep_child = NULL;
300 a158c901 2018-12-23 stsp return err;
303 711fb6e8 2018-12-23 stsp static const struct got_error *
304 711fb6e8 2018-12-23 stsp read_packed_object_privsep(struct got_object **obj,
305 711fb6e8 2018-12-23 stsp struct got_repository *repo, struct got_pack *pack,
306 711fb6e8 2018-12-23 stsp struct got_packidx *packidx, int idx, struct got_object_id *id)
308 711fb6e8 2018-12-23 stsp const struct got_error *err = NULL;
310 59d1e4a0 2021-03-10 stsp if (pack->privsep_child == NULL) {
311 59d1e4a0 2021-03-10 stsp err = start_pack_privsep_child(pack, packidx);
313 59d1e4a0 2021-03-10 stsp return err;
316 711fb6e8 2018-12-23 stsp return request_packed_object(obj, pack, idx, id);
319 59d1e4a0 2021-03-10 stsp static const struct got_error *
320 59d1e4a0 2021-03-10 stsp read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
321 59d1e4a0 2021-03-10 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
322 59d1e4a0 2021-03-10 stsp struct got_object_id *id)
324 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
326 59d1e4a0 2021-03-10 stsp if (pack->privsep_child == NULL) {
327 59d1e4a0 2021-03-10 stsp err = start_pack_privsep_child(pack, packidx);
329 59d1e4a0 2021-03-10 stsp return err;
332 59d1e4a0 2021-03-10 stsp return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
336 b3d68e7f 2021-07-03 stsp const struct got_error *
337 b3d68e7f 2021-07-03 stsp got_object_open_packed(struct got_object **obj, struct got_object_id *id,
338 2090a03d 2018-09-09 stsp struct got_repository *repo)
340 2090a03d 2018-09-09 stsp const struct got_error *err = NULL;
341 2090a03d 2018-09-09 stsp struct got_pack *pack = NULL;
342 2090a03d 2018-09-09 stsp struct got_packidx *packidx = NULL;
344 2090a03d 2018-09-09 stsp char *path_packfile;
346 2090a03d 2018-09-09 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
348 2090a03d 2018-09-09 stsp return err;
350 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
351 aea75d87 2021-07-06 stsp packidx->path_packidx);
353 2090a03d 2018-09-09 stsp return err;
355 2090a03d 2018-09-09 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
356 2090a03d 2018-09-09 stsp if (pack == NULL) {
357 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
362 a158c901 2018-12-23 stsp err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
366 2090a03d 2018-09-09 stsp free(path_packfile);
367 4558fcd4 2018-01-14 stsp return err;
370 9f2369b0 2018-12-24 stsp static const struct got_error *
371 d5c81d44 2021-07-08 stsp request_object(struct got_object **obj, struct got_object_id *id,
372 d5c81d44 2021-07-08 stsp struct got_repository *repo, int fd)
374 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
375 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
377 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
379 d5c81d44 2021-07-08 stsp err = got_privsep_send_obj_req(ibuf, fd, id);
381 9f2369b0 2018-12-24 stsp return err;
383 9f2369b0 2018-12-24 stsp return got_privsep_recv_obj(obj, ibuf);
386 9f2369b0 2018-12-24 stsp static const struct got_error *
387 59d1e4a0 2021-03-10 stsp request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
388 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo, int infd)
390 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
391 59d1e4a0 2021-03-10 stsp struct imsgbuf *ibuf;
392 59d1e4a0 2021-03-10 stsp int outfd_child;
394 59d1e4a0 2021-03-10 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
396 59d1e4a0 2021-03-10 stsp outfd_child = dup(outfd);
397 59d1e4a0 2021-03-10 stsp if (outfd_child == -1)
398 59d1e4a0 2021-03-10 stsp return got_error_from_errno("dup");
400 d5c81d44 2021-07-08 stsp err = got_privsep_send_raw_obj_req(ibuf, infd, id);
402 59d1e4a0 2021-03-10 stsp return err;
404 59d1e4a0 2021-03-10 stsp err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
406 59d1e4a0 2021-03-10 stsp return err;
408 59d1e4a0 2021-03-10 stsp return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
411 59d1e4a0 2021-03-10 stsp static const struct got_error *
412 59d1e4a0 2021-03-10 stsp start_read_object_child(struct got_repository *repo)
414 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
415 9f2369b0 2018-12-24 stsp int imsg_fds[2];
417 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
419 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
420 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
421 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
423 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
424 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
425 ddc7b220 2019-09-08 stsp free(ibuf);
426 ddc7b220 2019-09-08 stsp return err;
429 9f2369b0 2018-12-24 stsp pid = fork();
430 ddc7b220 2019-09-08 stsp if (pid == -1) {
431 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
432 ddc7b220 2019-09-08 stsp free(ibuf);
433 ddc7b220 2019-09-08 stsp return err;
435 9f2369b0 2018-12-24 stsp else if (pid == 0) {
436 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
437 9f2369b0 2018-12-24 stsp repo->path);
438 9f2369b0 2018-12-24 stsp /* not reached */
441 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
442 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
443 ddc7b220 2019-09-08 stsp free(ibuf);
444 ddc7b220 2019-09-08 stsp return err;
447 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
448 9f2369b0 2018-12-24 stsp imsg_fds[0];
449 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
450 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
451 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
453 59d1e4a0 2021-03-10 stsp return NULL;
456 b3d68e7f 2021-07-03 stsp const struct got_error *
457 b3d68e7f 2021-07-03 stsp got_object_read_header_privsep(struct got_object **obj,
458 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo, int obj_fd)
460 59d1e4a0 2021-03-10 stsp const struct got_error *err;
462 59d1e4a0 2021-03-10 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
463 d5c81d44 2021-07-08 stsp return request_object(obj, id, repo, obj_fd);
465 59d1e4a0 2021-03-10 stsp err = start_read_object_child(repo);
467 7495ec13 2021-04-04 stsp close(obj_fd);
468 59d1e4a0 2021-03-10 stsp return err;
471 d5c81d44 2021-07-08 stsp return request_object(obj, id, repo, obj_fd);
474 59d1e4a0 2021-03-10 stsp static const struct got_error *
475 59d1e4a0 2021-03-10 stsp read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
476 d5c81d44 2021-07-08 stsp int outfd, struct got_object_id *id, struct got_repository *repo,
477 d5c81d44 2021-07-08 stsp int obj_fd)
479 59d1e4a0 2021-03-10 stsp const struct got_error *err;
481 59d1e4a0 2021-03-10 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
482 d5c81d44 2021-07-08 stsp return request_raw_object(outbuf, size, hdrlen, outfd, id,
483 d5c81d44 2021-07-08 stsp repo, obj_fd);
485 59d1e4a0 2021-03-10 stsp err = start_read_object_child(repo);
487 59d1e4a0 2021-03-10 stsp return err;
489 d5c81d44 2021-07-08 stsp return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
493 4558fcd4 2018-01-14 stsp const struct got_error *
494 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
495 4558fcd4 2018-01-14 stsp struct got_object_id *id)
497 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
500 7bb0daa1 2018-06-21 stsp *obj = got_repo_get_cached_object(repo, id);
501 7bb0daa1 2018-06-21 stsp if (*obj != NULL) {
502 7bb0daa1 2018-06-21 stsp (*obj)->refcnt++;
503 7bb0daa1 2018-06-21 stsp return NULL;
506 b3d68e7f 2021-07-03 stsp err = got_object_open_packed(obj, id, repo);
507 e82b1d81 2019-07-27 stsp if (err && err->code != GOT_ERR_NO_OBJ)
508 e82b1d81 2019-07-27 stsp return err;
509 e82b1d81 2019-07-27 stsp if (*obj) {
510 e82b1d81 2019-07-27 stsp (*obj)->refcnt++;
511 e82b1d81 2019-07-27 stsp return got_repo_cache_object(repo, id, *obj);
514 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
516 762d73f4 2021-04-10 stsp if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
517 762d73f4 2021-04-10 stsp err = got_error_no_obj(id);
518 762d73f4 2021-04-10 stsp return err;
521 d5c81d44 2021-07-08 stsp err = got_object_read_header_privsep(obj, id, repo, fd);
523 4558fcd4 2018-01-14 stsp return err;
525 762d73f4 2021-04-10 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
527 59790a32 2018-09-15 stsp (*obj)->refcnt++;
528 762d73f4 2021-04-10 stsp return got_repo_cache_object(repo, id, *obj);
531 59d1e4a0 2021-03-10 stsp const struct got_error *
532 59d1e4a0 2021-03-10 stsp got_object_raw_open(struct got_raw_object **obj, struct got_repository *repo,
533 59d1e4a0 2021-03-10 stsp struct got_object_id *id, size_t blocksize)
535 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
536 59d1e4a0 2021-03-10 stsp struct got_packidx *packidx = NULL;
538 59d1e4a0 2021-03-10 stsp uint8_t *outbuf = NULL;
539 59d1e4a0 2021-03-10 stsp int outfd = -1;
540 59d1e4a0 2021-03-10 stsp off_t size = 0;
541 59d1e4a0 2021-03-10 stsp size_t hdrlen = 0;
542 59d1e4a0 2021-03-10 stsp char *path_packfile = NULL;
544 59d1e4a0 2021-03-10 stsp *obj = NULL;
546 59d1e4a0 2021-03-10 stsp outfd = got_opentempfd();
547 59d1e4a0 2021-03-10 stsp if (outfd == -1)
548 59d1e4a0 2021-03-10 stsp return got_error_from_errno("got_opentempfd");
550 59d1e4a0 2021-03-10 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
551 59d1e4a0 2021-03-10 stsp if (err == NULL) {
552 59d1e4a0 2021-03-10 stsp struct got_pack *pack = NULL;
554 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
555 aea75d87 2021-07-06 stsp packidx->path_packidx);
559 59d1e4a0 2021-03-10 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
560 59d1e4a0 2021-03-10 stsp if (pack == NULL) {
561 59d1e4a0 2021-03-10 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
566 59d1e4a0 2021-03-10 stsp err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
567 59d1e4a0 2021-03-10 stsp outfd, pack, packidx, idx, id);
570 59d1e4a0 2021-03-10 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
573 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
576 59d1e4a0 2021-03-10 stsp err = read_object_raw_privsep(&outbuf, &size, &hdrlen, outfd,
577 d5c81d44 2021-07-08 stsp id, repo, fd);
582 59d1e4a0 2021-03-10 stsp *obj = calloc(1, sizeof(**obj));
583 59d1e4a0 2021-03-10 stsp if (*obj == NULL) {
584 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("calloc");
588 59d1e4a0 2021-03-10 stsp (*obj)->read_buf = malloc(blocksize);
589 59d1e4a0 2021-03-10 stsp if ((*obj)->read_buf == NULL) {
590 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("malloc");
594 59d1e4a0 2021-03-10 stsp if (outbuf) {
595 59d1e4a0 2021-03-10 stsp if (close(outfd) == -1) {
596 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("close");
599 59d1e4a0 2021-03-10 stsp outfd = -1;
600 59d1e4a0 2021-03-10 stsp (*obj)->f = fmemopen(outbuf, hdrlen + size, "r");
601 59d1e4a0 2021-03-10 stsp if ((*obj)->f == NULL) {
602 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fdopen");
605 59d1e4a0 2021-03-10 stsp (*obj)->data = outbuf;
607 59d1e4a0 2021-03-10 stsp struct stat sb;
608 59d1e4a0 2021-03-10 stsp if (fstat(outfd, &sb) == -1) {
609 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fstat");
613 a8591711 2021-06-18 stsp if (sb.st_size != hdrlen + size) {
614 59d1e4a0 2021-03-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
618 59d1e4a0 2021-03-10 stsp (*obj)->f = fdopen(outfd, "r");
619 59d1e4a0 2021-03-10 stsp if ((*obj)->f == NULL) {
620 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fdopen");
623 59d1e4a0 2021-03-10 stsp outfd = -1;
624 59d1e4a0 2021-03-10 stsp (*obj)->data = NULL;
626 59d1e4a0 2021-03-10 stsp (*obj)->hdrlen = hdrlen;
627 59d1e4a0 2021-03-10 stsp (*obj)->size = size;
628 59d1e4a0 2021-03-10 stsp (*obj)->blocksize = blocksize;
630 59d1e4a0 2021-03-10 stsp free(path_packfile);
632 59d1e4a0 2021-03-10 stsp if (*obj) {
633 59d1e4a0 2021-03-10 stsp got_object_raw_close(*obj);
634 59d1e4a0 2021-03-10 stsp *obj = NULL;
636 59d1e4a0 2021-03-10 stsp if (outfd != -1)
637 59d1e4a0 2021-03-10 stsp close(outfd);
638 59d1e4a0 2021-03-10 stsp free(outbuf);
640 59d1e4a0 2021-03-10 stsp return err;
644 59d1e4a0 2021-03-10 stsp got_object_raw_rewind(struct got_raw_object *obj)
646 59d1e4a0 2021-03-10 stsp if (obj->f)
647 59d1e4a0 2021-03-10 stsp rewind(obj->f);
651 59d1e4a0 2021-03-10 stsp got_object_raw_get_hdrlen(struct got_raw_object *obj)
653 59d1e4a0 2021-03-10 stsp return obj->hdrlen;
656 59d1e4a0 2021-03-10 stsp const uint8_t *
657 59d1e4a0 2021-03-10 stsp got_object_raw_get_read_buf(struct got_raw_object *obj)
659 59d1e4a0 2021-03-10 stsp return obj->read_buf;
662 59d1e4a0 2021-03-10 stsp const struct got_error *
663 59d1e4a0 2021-03-10 stsp got_object_raw_read_block(size_t *outlenp, struct got_raw_object *obj)
667 59d1e4a0 2021-03-10 stsp n = fread(obj->read_buf, 1, obj->blocksize, obj->f);
668 59d1e4a0 2021-03-10 stsp if (n == 0 && ferror(obj->f))
669 59d1e4a0 2021-03-10 stsp return got_ferror(obj->f, GOT_ERR_IO);
670 59d1e4a0 2021-03-10 stsp *outlenp = n;
671 59d1e4a0 2021-03-10 stsp return NULL;
674 59d1e4a0 2021-03-10 stsp const struct got_error *
675 59d1e4a0 2021-03-10 stsp got_object_raw_close(struct got_raw_object *obj)
677 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
679 59d1e4a0 2021-03-10 stsp free(obj->read_buf);
680 59d1e4a0 2021-03-10 stsp if (obj->f != NULL && fclose(obj->f) == EOF && err == NULL)
681 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fclose");
682 59d1e4a0 2021-03-10 stsp free(obj->data);
684 59d1e4a0 2021-03-10 stsp return err;
687 6dfa2fd3 2018-02-12 stsp const struct got_error *
688 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
689 6dfa2fd3 2018-02-12 stsp const char *id_str)
691 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
693 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
694 6dd1ece6 2019-11-10 stsp return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
696 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
699 15a94983 2018-12-23 stsp const struct got_error *
700 15a94983 2018-12-23 stsp got_object_resolve_id_str(struct got_object_id **id,
701 15a94983 2018-12-23 stsp struct got_repository *repo, const char *id_str)
703 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
704 15a94983 2018-12-23 stsp struct got_object *obj;
706 15a94983 2018-12-23 stsp err = got_object_open_by_id_str(&obj, repo, id_str);
708 15a94983 2018-12-23 stsp return err;
710 15a94983 2018-12-23 stsp *id = got_object_id_dup(got_object_get_id(obj));
711 15a94983 2018-12-23 stsp got_object_close(obj);
712 15a94983 2018-12-23 stsp if (*id == NULL)
713 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
715 15a94983 2018-12-23 stsp return NULL;
718 434025f3 2018-09-16 stsp static const struct got_error *
719 a158c901 2018-12-23 stsp request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
720 a158c901 2018-12-23 stsp int pack_idx, struct got_object_id *id)
722 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
724 a158c901 2018-12-23 stsp err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
727 a158c901 2018-12-23 stsp return err;
729 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
731 ca6e02ac 2020-01-07 stsp return err;
733 ca6e02ac 2020-01-07 stsp (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
734 ca6e02ac 2020-01-07 stsp return NULL;
737 a158c901 2018-12-23 stsp static const struct got_error *
738 a158c901 2018-12-23 stsp read_packed_commit_privsep(struct got_commit_object **commit,
739 a158c901 2018-12-23 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
740 a158c901 2018-12-23 stsp struct got_object_id *id)
742 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
744 a158c901 2018-12-23 stsp if (pack->privsep_child)
745 a158c901 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
747 711fb6e8 2018-12-23 stsp err = start_pack_privsep_child(pack, packidx);
749 a158c901 2018-12-23 stsp return err;
751 711fb6e8 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
754 9f2369b0 2018-12-24 stsp static const struct got_error *
755 9f2369b0 2018-12-24 stsp request_commit(struct got_commit_object **commit, struct got_repository *repo,
756 d5c81d44 2021-07-08 stsp int fd, struct got_object_id *id)
758 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
759 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
761 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
763 d5c81d44 2021-07-08 stsp err = got_privsep_send_commit_req(ibuf, fd, id, -1);
765 9f2369b0 2018-12-24 stsp return err;
767 9f2369b0 2018-12-24 stsp return got_privsep_recv_commit(commit, ibuf);
770 9f2369b0 2018-12-24 stsp static const struct got_error *
771 9f2369b0 2018-12-24 stsp read_commit_privsep(struct got_commit_object **commit, int obj_fd,
772 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo)
774 ddc7b220 2019-09-08 stsp const struct got_error *err;
775 9f2369b0 2018-12-24 stsp int imsg_fds[2];
777 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
779 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
780 d5c81d44 2021-07-08 stsp return request_commit(commit, repo, obj_fd, id);
782 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
783 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
784 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
786 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
787 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
788 ddc7b220 2019-09-08 stsp free(ibuf);
789 ddc7b220 2019-09-08 stsp return err;
792 9f2369b0 2018-12-24 stsp pid = fork();
793 ddc7b220 2019-09-08 stsp if (pid == -1) {
794 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
795 ddc7b220 2019-09-08 stsp free(ibuf);
796 ddc7b220 2019-09-08 stsp return err;
798 9f2369b0 2018-12-24 stsp else if (pid == 0) {
799 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
800 9f2369b0 2018-12-24 stsp repo->path);
801 9f2369b0 2018-12-24 stsp /* not reached */
804 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
805 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
806 ddc7b220 2019-09-08 stsp free(ibuf);
807 ddc7b220 2019-09-08 stsp return err;
809 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
810 9f2369b0 2018-12-24 stsp imsg_fds[0];
811 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
812 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
813 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
815 d5c81d44 2021-07-08 stsp return request_commit(commit, repo, obj_fd, id);
819 a158c901 2018-12-23 stsp static const struct got_error *
820 434025f3 2018-09-16 stsp open_commit(struct got_commit_object **commit,
821 1785f84a 2018-12-23 stsp struct got_repository *repo, struct got_object_id *id, int check_cache)
823 434025f3 2018-09-16 stsp const struct got_error *err = NULL;
824 1785f84a 2018-12-23 stsp struct got_packidx *packidx = NULL;
826 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
828 434025f3 2018-09-16 stsp if (check_cache) {
829 1785f84a 2018-12-23 stsp *commit = got_repo_get_cached_commit(repo, id);
830 434025f3 2018-09-16 stsp if (*commit != NULL) {
831 434025f3 2018-09-16 stsp (*commit)->refcnt++;
832 434025f3 2018-09-16 stsp return NULL;
835 434025f3 2018-09-16 stsp *commit = NULL;
837 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
838 e82b1d81 2019-07-27 stsp if (err == NULL) {
839 1785f84a 2018-12-23 stsp struct got_pack *pack = NULL;
841 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
842 aea75d87 2021-07-06 stsp packidx->path_packidx);
844 1785f84a 2018-12-23 stsp return err;
846 1785f84a 2018-12-23 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
847 434025f3 2018-09-16 stsp if (pack == NULL) {
848 1785f84a 2018-12-23 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
853 a158c901 2018-12-23 stsp err = read_packed_commit_privsep(commit, pack,
854 1785f84a 2018-12-23 stsp packidx, idx, id);
855 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
858 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
860 e82b1d81 2019-07-27 stsp return err;
861 d5c81d44 2021-07-08 stsp err = read_commit_privsep(commit, fd, id, repo);
864 434025f3 2018-09-16 stsp if (err == NULL) {
865 434025f3 2018-09-16 stsp (*commit)->refcnt++;
866 1785f84a 2018-12-23 stsp err = got_repo_cache_commit(repo, id, *commit);
869 8d2c5ea3 2019-08-13 stsp free(path_packfile);
870 e32baab7 2018-11-05 stsp return err;
873 e32baab7 2018-11-05 stsp const struct got_error *
874 e32baab7 2018-11-05 stsp got_object_open_as_commit(struct got_commit_object **commit,
875 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object_id *id)
877 e32baab7 2018-11-05 stsp *commit = got_repo_get_cached_commit(repo, id);
878 e32baab7 2018-11-05 stsp if (*commit != NULL) {
879 e32baab7 2018-11-05 stsp (*commit)->refcnt++;
880 e32baab7 2018-11-05 stsp return NULL;
883 1785f84a 2018-12-23 stsp return open_commit(commit, repo, id, 0);
886 e32baab7 2018-11-05 stsp const struct got_error *
887 e32baab7 2018-11-05 stsp got_object_commit_open(struct got_commit_object **commit,
888 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object *obj)
890 1785f84a 2018-12-23 stsp return open_commit(commit, repo, got_object_get_id(obj), 1);
893 434025f3 2018-09-16 stsp const struct got_error *
894 dbc6a6b6 2018-07-12 stsp got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
896 dbc6a6b6 2018-07-12 stsp const struct got_error *err = NULL;
898 dbc6a6b6 2018-07-12 stsp *qid = calloc(1, sizeof(**qid));
899 dbc6a6b6 2018-07-12 stsp if (*qid == NULL)
900 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
902 dbc6a6b6 2018-07-12 stsp (*qid)->id = got_object_id_dup(id);
903 dbc6a6b6 2018-07-12 stsp if ((*qid)->id == NULL) {
904 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
905 fa2f6902 2018-07-23 stsp got_object_qid_free(*qid);
906 dbc6a6b6 2018-07-12 stsp *qid = NULL;
907 dbc6a6b6 2018-07-12 stsp return err;
910 9ca9aafb 2021-06-18 stsp return NULL;
913 9ca9aafb 2021-06-18 stsp const struct got_error *
914 9ca9aafb 2021-06-18 stsp got_object_id_queue_copy(const struct got_object_id_queue *src,
915 9ca9aafb 2021-06-18 stsp struct got_object_id_queue *dest)
917 9ca9aafb 2021-06-18 stsp const struct got_error *err;
918 9ca9aafb 2021-06-18 stsp struct got_object_qid *qid;
920 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, src, entry) {
921 9ca9aafb 2021-06-18 stsp struct got_object_qid *new;
923 9ca9aafb 2021-06-18 stsp * Deep-copy the object ID only. Let the caller deal
924 9ca9aafb 2021-06-18 stsp * with setting up the new->data pointer if needed.
926 9ca9aafb 2021-06-18 stsp err = got_object_qid_alloc(&new, qid->id);
928 9ca9aafb 2021-06-18 stsp got_object_id_queue_free(dest);
929 9ca9aafb 2021-06-18 stsp return err;
931 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(dest, new, entry);
934 dbc6a6b6 2018-07-12 stsp return NULL;
937 a158c901 2018-12-23 stsp static const struct got_error *
938 13c729f7 2018-12-24 stsp request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
939 13c729f7 2018-12-24 stsp int pack_idx, struct got_object_id *id)
941 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
943 13c729f7 2018-12-24 stsp err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
946 a158c901 2018-12-23 stsp return err;
948 a158c901 2018-12-23 stsp return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
951 71eb0e7f 2018-09-16 stsp static const struct got_error *
952 13c729f7 2018-12-24 stsp read_packed_tree_privsep(struct got_tree_object **tree,
953 13c729f7 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
954 13c729f7 2018-12-24 stsp struct got_object_id *id)
956 13c729f7 2018-12-24 stsp const struct got_error *err = NULL;
958 13c729f7 2018-12-24 stsp if (pack->privsep_child)
959 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
961 13c729f7 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
963 13c729f7 2018-12-24 stsp return err;
965 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
968 13c729f7 2018-12-24 stsp static const struct got_error *
969 9f2369b0 2018-12-24 stsp request_tree(struct got_tree_object **tree, struct got_repository *repo,
970 d5c81d44 2021-07-08 stsp int fd, struct got_object_id *id)
972 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
973 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
975 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
977 d5c81d44 2021-07-08 stsp err = got_privsep_send_tree_req(ibuf, fd, id, -1);
979 9f2369b0 2018-12-24 stsp return err;
981 9f2369b0 2018-12-24 stsp return got_privsep_recv_tree(tree, ibuf);
984 9f2369b0 2018-12-24 stsp const struct got_error *
985 9f2369b0 2018-12-24 stsp read_tree_privsep(struct got_tree_object **tree, int obj_fd,
986 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo)
988 ddc7b220 2019-09-08 stsp const struct got_error *err;
989 9f2369b0 2018-12-24 stsp int imsg_fds[2];
991 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
993 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
994 d5c81d44 2021-07-08 stsp return request_tree(tree, repo, obj_fd, id);
996 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
997 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
998 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1000 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1001 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1002 ddc7b220 2019-09-08 stsp free(ibuf);
1003 ddc7b220 2019-09-08 stsp return err;
1006 9f2369b0 2018-12-24 stsp pid = fork();
1007 ddc7b220 2019-09-08 stsp if (pid == -1) {
1008 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1009 ddc7b220 2019-09-08 stsp free(ibuf);
1010 ddc7b220 2019-09-08 stsp return err;
1012 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1013 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1014 9f2369b0 2018-12-24 stsp repo->path);
1015 9f2369b0 2018-12-24 stsp /* not reached */
1018 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
1019 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1020 ddc7b220 2019-09-08 stsp free(ibuf);
1021 ddc7b220 2019-09-08 stsp return err;
1023 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1024 9f2369b0 2018-12-24 stsp imsg_fds[0];
1025 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1026 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1027 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1030 d5c81d44 2021-07-08 stsp return request_tree(tree, repo, obj_fd, id);
1033 9f2369b0 2018-12-24 stsp static const struct got_error *
1034 13c729f7 2018-12-24 stsp open_tree(struct got_tree_object **tree, struct got_repository *repo,
1035 13c729f7 2018-12-24 stsp struct got_object_id *id, int check_cache)
1037 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
1038 13c729f7 2018-12-24 stsp struct got_packidx *packidx = NULL;
1040 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1042 71eb0e7f 2018-09-16 stsp if (check_cache) {
1043 13c729f7 2018-12-24 stsp *tree = got_repo_get_cached_tree(repo, id);
1044 71eb0e7f 2018-09-16 stsp if (*tree != NULL) {
1045 71eb0e7f 2018-09-16 stsp (*tree)->refcnt++;
1046 71eb0e7f 2018-09-16 stsp return NULL;
1049 71eb0e7f 2018-09-16 stsp *tree = NULL;
1051 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1052 e82b1d81 2019-07-27 stsp if (err == NULL) {
1053 13c729f7 2018-12-24 stsp struct got_pack *pack = NULL;
1055 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
1056 aea75d87 2021-07-06 stsp packidx->path_packidx);
1058 13c729f7 2018-12-24 stsp return err;
1060 13c729f7 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1061 e7885405 2018-09-10 stsp if (pack == NULL) {
1062 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1065 8d2c5ea3 2019-08-13 stsp goto done;
1067 13c729f7 2018-12-24 stsp err = read_packed_tree_privsep(tree, pack,
1068 13c729f7 2018-12-24 stsp packidx, idx, id);
1069 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1072 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1074 e82b1d81 2019-07-27 stsp return err;
1075 d5c81d44 2021-07-08 stsp err = read_tree_privsep(tree, fd, id, repo);
1078 f6be5c30 2018-06-22 stsp if (err == NULL) {
1079 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
1080 13c729f7 2018-12-24 stsp err = got_repo_cache_tree(repo, id, *tree);
1083 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1084 776d4d29 2018-06-17 stsp return err;
1087 776d4d29 2018-06-17 stsp const struct got_error *
1088 776d4d29 2018-06-17 stsp got_object_open_as_tree(struct got_tree_object **tree,
1089 776d4d29 2018-06-17 stsp struct got_repository *repo, struct got_object_id *id)
1091 e8eb494a 2018-09-16 stsp *tree = got_repo_get_cached_tree(repo, id);
1092 e8eb494a 2018-09-16 stsp if (*tree != NULL) {
1093 e8eb494a 2018-09-16 stsp (*tree)->refcnt++;
1094 e8eb494a 2018-09-16 stsp return NULL;
1097 13c729f7 2018-12-24 stsp return open_tree(tree, repo, id, 0);
1100 71eb0e7f 2018-09-16 stsp const struct got_error *
1101 71eb0e7f 2018-09-16 stsp got_object_tree_open(struct got_tree_object **tree,
1102 71eb0e7f 2018-09-16 stsp struct got_repository *repo, struct got_object *obj)
1104 13c729f7 2018-12-24 stsp return open_tree(tree, repo, got_object_get_id(obj), 1);
1108 56e0773d 2019-11-28 stsp got_object_tree_get_nentries(struct got_tree_object *tree)
1110 56e0773d 2019-11-28 stsp return tree->nentries;
1113 56e0773d 2019-11-28 stsp struct got_tree_entry *
1114 56e0773d 2019-11-28 stsp got_object_tree_get_first_entry(struct got_tree_object *tree)
1116 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, 0);
1119 56e0773d 2019-11-28 stsp struct got_tree_entry *
1120 56e0773d 2019-11-28 stsp got_object_tree_get_last_entry(struct got_tree_object *tree)
1122 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, tree->nentries - 1);
1125 56e0773d 2019-11-28 stsp struct got_tree_entry *
1126 56e0773d 2019-11-28 stsp got_object_tree_get_entry(struct got_tree_object *tree, int i)
1128 56e0773d 2019-11-28 stsp if (i < 0 || i >= tree->nentries)
1129 56e0773d 2019-11-28 stsp return NULL;
1130 56e0773d 2019-11-28 stsp return &tree->entries[i];
1134 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(struct got_tree_entry *te)
1136 56e0773d 2019-11-28 stsp return te->mode;
1139 56e0773d 2019-11-28 stsp const char *
1140 56e0773d 2019-11-28 stsp got_tree_entry_get_name(struct got_tree_entry *te)
1142 56e0773d 2019-11-28 stsp return &te->name[0];
1145 56e0773d 2019-11-28 stsp struct got_object_id *
1146 56e0773d 2019-11-28 stsp got_tree_entry_get_id(struct got_tree_entry *te)
1148 56e0773d 2019-11-28 stsp return &te->id;
1151 0d6c6ee3 2020-05-20 stsp const struct got_error *
1152 af57b12a 2020-07-23 stsp got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1154 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
1155 32596e16 2020-07-23 stsp size_t len, totlen, hdrlen, offset;
1157 aa092692 2020-07-23 stsp *s = NULL;
1159 659dc16e 2020-07-23 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1160 659dc16e 2020-07-23 stsp totlen = 0;
1161 32596e16 2020-07-23 stsp offset = 0;
1165 659dc16e 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1167 af57b12a 2020-07-23 stsp return err;
1169 659dc16e 2020-07-23 stsp if (len == 0)
1172 659dc16e 2020-07-23 stsp totlen += len - hdrlen;
1173 af57b12a 2020-07-23 stsp p = realloc(*s, totlen + 1);
1174 659dc16e 2020-07-23 stsp if (p == NULL) {
1175 659dc16e 2020-07-23 stsp err = got_error_from_errno("realloc");
1177 af57b12a 2020-07-23 stsp *s = NULL;
1178 af57b12a 2020-07-23 stsp return err;
1181 659dc16e 2020-07-23 stsp /* Skip blob object header first time around. */
1182 af57b12a 2020-07-23 stsp memcpy(*s + offset,
1183 f8f7c882 2020-07-23 stsp got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1184 659dc16e 2020-07-23 stsp hdrlen = 0;
1185 32596e16 2020-07-23 stsp offset = totlen;
1186 659dc16e 2020-07-23 stsp } while (len > 0);
1188 af57b12a 2020-07-23 stsp (*s)[totlen] = '\0';
1189 af57b12a 2020-07-23 stsp return NULL;
1192 af57b12a 2020-07-23 stsp const struct got_error *
1193 af57b12a 2020-07-23 stsp got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1194 af57b12a 2020-07-23 stsp struct got_repository *repo)
1196 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
1197 af57b12a 2020-07-23 stsp struct got_blob_object *blob = NULL;
1199 af57b12a 2020-07-23 stsp *link_target = NULL;
1201 af57b12a 2020-07-23 stsp if (!got_object_tree_entry_is_symlink(te))
1202 af57b12a 2020-07-23 stsp return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1204 af57b12a 2020-07-23 stsp err = got_object_open_as_blob(&blob, repo,
1205 af57b12a 2020-07-23 stsp got_tree_entry_get_id(te), PATH_MAX);
1207 af57b12a 2020-07-23 stsp return err;
1209 af57b12a 2020-07-23 stsp err = got_object_blob_read_to_str(link_target, blob);
1210 af57b12a 2020-07-23 stsp got_object_blob_close(blob);
1211 659dc16e 2020-07-23 stsp if (err) {
1212 659dc16e 2020-07-23 stsp free(*link_target);
1213 659dc16e 2020-07-23 stsp *link_target = NULL;
1215 0d6c6ee3 2020-05-20 stsp return err;
1219 56e0773d 2019-11-28 stsp got_tree_entry_get_index(struct got_tree_entry *te)
1221 56e0773d 2019-11-28 stsp return te->idx;
1224 56e0773d 2019-11-28 stsp struct got_tree_entry *
1225 56e0773d 2019-11-28 stsp got_tree_entry_get_next(struct got_tree_object *tree,
1226 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
1228 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx + 1);
1231 56e0773d 2019-11-28 stsp struct got_tree_entry *
1232 56e0773d 2019-11-28 stsp got_tree_entry_get_prev(struct got_tree_object *tree,
1233 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
1235 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx - 1);
1238 3840f4c9 2018-09-12 stsp static const struct got_error *
1239 ac544f8c 2019-01-13 stsp request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1240 ebc55e2d 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1241 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
1243 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
1244 3840f4c9 2018-09-12 stsp int outfd_child;
1245 3840f4c9 2018-09-12 stsp int basefd, accumfd; /* temporary files for delta application */
1247 3840f4c9 2018-09-12 stsp basefd = got_opentempfd();
1248 3840f4c9 2018-09-12 stsp if (basefd == -1)
1249 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
1250 3840f4c9 2018-09-12 stsp accumfd = got_opentempfd();
1251 3840f4c9 2018-09-12 stsp if (accumfd == -1)
1252 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
1254 3840f4c9 2018-09-12 stsp outfd_child = dup(outfd);
1255 3840f4c9 2018-09-12 stsp if (outfd_child == -1)
1256 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1258 ebc55e2d 2018-12-24 stsp err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1260 3840f4c9 2018-09-12 stsp return err;
1262 3840f4c9 2018-09-12 stsp err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1263 3840f4c9 2018-09-12 stsp outfd_child);
1264 3840f4c9 2018-09-12 stsp if (err) {
1265 41496140 2019-02-21 stsp close(basefd);
1266 41496140 2019-02-21 stsp close(accumfd);
1267 3840f4c9 2018-09-12 stsp return err;
1270 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1272 3840f4c9 2018-09-12 stsp if (err) {
1273 3840f4c9 2018-09-12 stsp close(accumfd);
1274 3840f4c9 2018-09-12 stsp return err;
1277 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1280 3840f4c9 2018-09-12 stsp return err;
1282 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen,
1283 ebc55e2d 2018-12-24 stsp pack->privsep_child->ibuf);
1285 3840f4c9 2018-09-12 stsp return err;
1287 3840f4c9 2018-09-12 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1288 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1290 3840f4c9 2018-09-12 stsp return err;
1293 ebc55e2d 2018-12-24 stsp static const struct got_error *
1294 ac544f8c 2019-01-13 stsp read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1295 ac544f8c 2019-01-13 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1296 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
1298 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
1300 ebc55e2d 2018-12-24 stsp if (pack->privsep_child == NULL) {
1301 ebc55e2d 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
1303 ebc55e2d 2018-12-24 stsp return err;
1306 ac544f8c 2019-01-13 stsp return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1310 9f2369b0 2018-12-24 stsp static const struct got_error *
1311 ac544f8c 2019-01-13 stsp request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1312 d5c81d44 2021-07-08 stsp int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1314 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1315 9f2369b0 2018-12-24 stsp int outfd_child;
1317 9f2369b0 2018-12-24 stsp outfd_child = dup(outfd);
1318 9f2369b0 2018-12-24 stsp if (outfd_child == -1)
1319 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1321 d5c81d44 2021-07-08 stsp err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1323 9f2369b0 2018-12-24 stsp return err;
1325 9f2369b0 2018-12-24 stsp err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1327 9f2369b0 2018-12-24 stsp return err;
1329 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1331 9f2369b0 2018-12-24 stsp return err;
1333 9f2369b0 2018-12-24 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1334 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
1336 9f2369b0 2018-12-24 stsp return err;
1339 9f2369b0 2018-12-24 stsp static const struct got_error *
1340 ac544f8c 2019-01-13 stsp read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1341 d5c81d44 2021-07-08 stsp int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1343 ddc7b220 2019-09-08 stsp const struct got_error *err;
1344 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1345 9f2369b0 2018-12-24 stsp pid_t pid;
1346 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1348 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1349 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1350 d5c81d44 2021-07-08 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1354 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1355 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1356 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1358 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1359 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1360 ddc7b220 2019-09-08 stsp free(ibuf);
1361 ddc7b220 2019-09-08 stsp return err;
1364 9f2369b0 2018-12-24 stsp pid = fork();
1365 ddc7b220 2019-09-08 stsp if (pid == -1) {
1366 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1367 ddc7b220 2019-09-08 stsp free(ibuf);
1368 ddc7b220 2019-09-08 stsp return err;
1370 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1371 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1372 9f2369b0 2018-12-24 stsp repo->path);
1373 9f2369b0 2018-12-24 stsp /* not reached */
1376 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
1377 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1378 ddc7b220 2019-09-08 stsp free(ibuf);
1379 ddc7b220 2019-09-08 stsp return err;
1381 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1382 9f2369b0 2018-12-24 stsp imsg_fds[0];
1383 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1384 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1385 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1387 d5c81d44 2021-07-08 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1390 ebc55e2d 2018-12-24 stsp static const struct got_error *
1391 ebc55e2d 2018-12-24 stsp open_blob(struct got_blob_object **blob, struct got_repository *repo,
1392 ebc55e2d 2018-12-24 stsp struct got_object_id *id, size_t blocksize)
1394 ebc55e2d 2018-12-24 stsp const struct got_error *err = NULL;
1395 ebc55e2d 2018-12-24 stsp struct got_packidx *packidx = NULL;
1397 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1398 ac544f8c 2019-01-13 stsp uint8_t *outbuf;
1399 e82b1d81 2019-07-27 stsp int outfd;
1400 ebc55e2d 2018-12-24 stsp size_t size, hdrlen;
1401 ebc55e2d 2018-12-24 stsp struct stat sb;
1403 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
1404 4558fcd4 2018-01-14 stsp if (*blob == NULL)
1405 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1407 55da3778 2018-09-10 stsp outfd = got_opentempfd();
1408 55da3778 2018-09-10 stsp if (outfd == -1)
1409 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
1411 062ebb78 2018-07-12 stsp (*blob)->read_buf = malloc(blocksize);
1412 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
1413 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1414 c7254d79 2018-04-24 stsp goto done;
1417 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1418 e82b1d81 2019-07-27 stsp if (err == NULL) {
1419 ebc55e2d 2018-12-24 stsp struct got_pack *pack = NULL;
1421 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
1422 aea75d87 2021-07-06 stsp packidx->path_packidx);
1424 ebc55e2d 2018-12-24 stsp goto done;
1426 ebc55e2d 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1427 55da3778 2018-09-10 stsp if (pack == NULL) {
1428 ebc55e2d 2018-12-24 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1431 55da3778 2018-09-10 stsp goto done;
1433 ac544f8c 2019-01-13 stsp err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1434 ac544f8c 2019-01-13 stsp pack, packidx, idx, id);
1435 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1438 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&infd, id, repo);
1440 e82b1d81 2019-07-27 stsp goto done;
1441 ac544f8c 2019-01-13 stsp err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1442 d5c81d44 2021-07-08 stsp id, repo);
1445 55da3778 2018-09-10 stsp goto done;
1447 de060dff 2018-12-24 stsp if (hdrlen > size) {
1448 ebc55e2d 2018-12-24 stsp err = got_error(GOT_ERR_BAD_OBJ_HDR);
1449 ebc55e2d 2018-12-24 stsp goto done;
1452 ac544f8c 2019-01-13 stsp if (outbuf) {
1453 08578a35 2021-01-22 stsp if (close(outfd) == -1 && err == NULL)
1454 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1455 ac544f8c 2019-01-13 stsp outfd = -1;
1456 ac544f8c 2019-01-13 stsp (*blob)->f = fmemopen(outbuf, size, "rb");
1457 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1458 638f9024 2019-05-13 stsp err = got_error_from_errno("fmemopen");
1459 ac544f8c 2019-01-13 stsp free(outbuf);
1460 ac544f8c 2019-01-13 stsp goto done;
1462 ac544f8c 2019-01-13 stsp (*blob)->data = outbuf;
1464 ac544f8c 2019-01-13 stsp if (fstat(outfd, &sb) == -1) {
1465 638f9024 2019-05-13 stsp err = got_error_from_errno("fstat");
1466 ac544f8c 2019-01-13 stsp goto done;
1469 ac544f8c 2019-01-13 stsp if (sb.st_size != size) {
1470 ac544f8c 2019-01-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1471 ac544f8c 2019-01-13 stsp goto done;
1474 ac544f8c 2019-01-13 stsp (*blob)->f = fdopen(outfd, "rb");
1475 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1476 638f9024 2019-05-13 stsp err = got_error_from_errno("fdopen");
1477 ac544f8c 2019-01-13 stsp close(outfd);
1478 ac544f8c 2019-01-13 stsp outfd = -1;
1479 ac544f8c 2019-01-13 stsp goto done;
1483 ebc55e2d 2018-12-24 stsp (*blob)->hdrlen = hdrlen;
1484 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
1485 ebc55e2d 2018-12-24 stsp memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1488 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1489 55da3778 2018-09-10 stsp if (err) {
1490 55da3778 2018-09-10 stsp if (*blob) {
1491 7baf5860 2019-03-19 stsp got_object_blob_close(*blob);
1492 55da3778 2018-09-10 stsp *blob = NULL;
1493 55da3778 2018-09-10 stsp } else if (outfd != -1)
1494 55da3778 2018-09-10 stsp close(outfd);
1496 a19581a2 2018-06-21 stsp return err;
1499 a19581a2 2018-06-21 stsp const struct got_error *
1500 a19581a2 2018-06-21 stsp got_object_open_as_blob(struct got_blob_object **blob,
1501 a19581a2 2018-06-21 stsp struct got_repository *repo, struct got_object_id *id,
1502 a19581a2 2018-06-21 stsp size_t blocksize)
1504 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, id, blocksize);
1507 ebc55e2d 2018-12-24 stsp const struct got_error *
1508 ebc55e2d 2018-12-24 stsp got_object_blob_open(struct got_blob_object **blob,
1509 ebc55e2d 2018-12-24 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
1511 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1514 fb43ecf1 2019-02-11 stsp const struct got_error *
1515 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
1517 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL;
1518 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
1519 56b63ca4 2021-01-22 stsp if (blob->f && fclose(blob->f) == EOF)
1520 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1521 ac544f8c 2019-01-13 stsp free(blob->data);
1522 68482ea3 2017-11-27 stsp free(blob);
1523 fb43ecf1 2019-02-11 stsp return err;
1527 8ba819a3 2020-07-23 stsp got_object_blob_rewind(struct got_blob_object *blob)
1529 8ba819a3 2020-07-23 stsp if (blob->f)
1530 8ba819a3 2020-07-23 stsp rewind(blob->f);
1534 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1536 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1540 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
1542 f934cf2c 2018-02-12 stsp return blob->hdrlen;
1545 f934cf2c 2018-02-12 stsp const uint8_t *
1546 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
1548 f934cf2c 2018-02-12 stsp return blob->read_buf;
1551 68482ea3 2017-11-27 stsp const struct got_error *
1552 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1556 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1557 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
1558 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
1559 eb651edf 2018-02-11 stsp *outlenp = n;
1560 35e9ba5d 2018-06-21 stsp return NULL;
1563 35e9ba5d 2018-06-21 stsp const struct got_error *
1564 be659d10 2020-11-18 stsp got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1565 6c4c42e0 2019-06-24 stsp off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1567 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
1568 b6752625 2018-12-24 stsp size_t n, len, hdrlen;
1569 84451b3e 2018-07-10 stsp const uint8_t *buf;
1571 c33ebc60 2020-11-18 stsp const int alloc_chunksz = 512;
1572 c33ebc60 2020-11-18 stsp size_t nalloc = 0;
1573 f595d9bd 2019-08-14 stsp off_t off = 0, total_len = 0;
1575 6c4c42e0 2019-06-24 stsp if (line_offsets)
1576 6c4c42e0 2019-06-24 stsp *line_offsets = NULL;
1577 f595d9bd 2019-08-14 stsp if (filesize)
1578 f595d9bd 2019-08-14 stsp *filesize = 0;
1579 84451b3e 2018-07-10 stsp if (nlines)
1580 84451b3e 2018-07-10 stsp *nlines = 0;
1582 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1584 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
1586 35e9ba5d 2018-06-21 stsp return err;
1587 35e9ba5d 2018-06-21 stsp if (len == 0)
1589 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
1590 b02560ec 2019-08-19 stsp i = hdrlen;
1591 f1cbc3bc 2020-11-18 stsp if (nlines) {
1592 f1cbc3bc 2020-11-18 stsp if (line_offsets && *line_offsets == NULL) {
1593 78695fb7 2019-08-12 stsp /* Have some data but perhaps no '\n'. */
1594 78695fb7 2019-08-12 stsp *nlines = 1;
1595 c33ebc60 2020-11-18 stsp nalloc = alloc_chunksz;
1596 c33ebc60 2020-11-18 stsp *line_offsets = calloc(nalloc,
1597 c33ebc60 2020-11-18 stsp sizeof(**line_offsets));
1598 78695fb7 2019-08-12 stsp if (*line_offsets == NULL)
1599 845785d4 2020-02-02 tracey return got_error_from_errno("calloc");
1601 b02560ec 2019-08-19 stsp /* Skip forward over end of first line. */
1602 b02560ec 2019-08-19 stsp while (i < len) {
1603 b02560ec 2019-08-19 stsp if (buf[i] == '\n')
1608 b02560ec 2019-08-19 stsp /* Scan '\n' offsets in remaining chunk of data. */
1609 b02560ec 2019-08-19 stsp while (i < len) {
1610 b02560ec 2019-08-19 stsp if (buf[i] != '\n') {
1614 f595d9bd 2019-08-14 stsp (*nlines)++;
1615 c33ebc60 2020-11-18 stsp if (line_offsets && nalloc < *nlines) {
1616 c33ebc60 2020-11-18 stsp size_t n = *nlines + alloc_chunksz;
1617 78695fb7 2019-08-12 stsp off_t *o = recallocarray(*line_offsets,
1618 c33ebc60 2020-11-18 stsp nalloc, n, sizeof(**line_offsets));
1619 78695fb7 2019-08-12 stsp if (o == NULL) {
1620 78695fb7 2019-08-12 stsp free(*line_offsets);
1621 78695fb7 2019-08-12 stsp *line_offsets = NULL;
1622 78695fb7 2019-08-12 stsp return got_error_from_errno(
1623 78695fb7 2019-08-12 stsp "recallocarray");
1625 78695fb7 2019-08-12 stsp *line_offsets = o;
1626 c33ebc60 2020-11-18 stsp nalloc = n;
1628 f1cbc3bc 2020-11-18 stsp if (line_offsets) {
1629 f1cbc3bc 2020-11-18 stsp off = total_len + i - hdrlen + 1;
1630 f1cbc3bc 2020-11-18 stsp (*line_offsets)[*nlines - 1] = off;
1635 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
1636 454a6b59 2018-12-24 stsp n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1637 b6752625 2018-12-24 stsp if (n != len - hdrlen)
1638 b6752625 2018-12-24 stsp return got_ferror(outfile, GOT_ERR_IO);
1639 f595d9bd 2019-08-14 stsp total_len += len - hdrlen;
1640 35e9ba5d 2018-06-21 stsp hdrlen = 0;
1641 35e9ba5d 2018-06-21 stsp } while (len != 0);
1643 cbe7f848 2019-02-11 stsp if (fflush(outfile) != 0)
1644 638f9024 2019-05-13 stsp return got_error_from_errno("fflush");
1645 35e9ba5d 2018-06-21 stsp rewind(outfile);
1647 f595d9bd 2019-08-14 stsp if (filesize)
1648 f595d9bd 2019-08-14 stsp *filesize = total_len;
1650 776d4d29 2018-06-17 stsp return NULL;
1653 f4a881ce 2018-11-17 stsp static const struct got_error *
1654 268f7291 2018-12-24 stsp request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1655 268f7291 2018-12-24 stsp int pack_idx, struct got_object_id *id)
1657 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
1659 268f7291 2018-12-24 stsp err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1660 268f7291 2018-12-24 stsp pack_idx);
1662 a158c901 2018-12-23 stsp return err;
1664 a158c901 2018-12-23 stsp return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1667 268f7291 2018-12-24 stsp static const struct got_error *
1668 268f7291 2018-12-24 stsp read_packed_tag_privsep(struct got_tag_object **tag,
1669 268f7291 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1670 268f7291 2018-12-24 stsp struct got_object_id *id)
1672 268f7291 2018-12-24 stsp const struct got_error *err = NULL;
1674 268f7291 2018-12-24 stsp if (pack->privsep_child)
1675 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1677 268f7291 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
1679 268f7291 2018-12-24 stsp return err;
1681 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1684 9f2369b0 2018-12-24 stsp static const struct got_error *
1685 9f2369b0 2018-12-24 stsp request_tag(struct got_tag_object **tag, struct got_repository *repo,
1686 d5c81d44 2021-07-08 stsp int fd, struct got_object_id *id)
1688 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1689 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1691 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1693 d5c81d44 2021-07-08 stsp err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1695 9f2369b0 2018-12-24 stsp return err;
1697 9f2369b0 2018-12-24 stsp return got_privsep_recv_tag(tag, ibuf);
1700 9f2369b0 2018-12-24 stsp static const struct got_error *
1701 9f2369b0 2018-12-24 stsp read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1702 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo)
1704 ddc7b220 2019-09-08 stsp const struct got_error *err;
1705 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1706 9f2369b0 2018-12-24 stsp pid_t pid;
1707 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1709 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1710 d5c81d44 2021-07-08 stsp return request_tag(tag, repo, obj_fd, id);
1712 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1713 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1714 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1716 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1717 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1718 ddc7b220 2019-09-08 stsp free(ibuf);
1719 ddc7b220 2019-09-08 stsp return err;
1722 9f2369b0 2018-12-24 stsp pid = fork();
1723 ddc7b220 2019-09-08 stsp if (pid == -1) {
1724 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1725 ddc7b220 2019-09-08 stsp free(ibuf);
1726 ddc7b220 2019-09-08 stsp return err;
1728 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1729 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1730 9f2369b0 2018-12-24 stsp repo->path);
1731 9f2369b0 2018-12-24 stsp /* not reached */
1734 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
1735 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1736 ddc7b220 2019-09-08 stsp free(ibuf);
1737 ddc7b220 2019-09-08 stsp return err;
1739 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1740 9f2369b0 2018-12-24 stsp imsg_fds[0];
1741 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1742 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1743 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1745 d5c81d44 2021-07-08 stsp return request_tag(tag, repo, obj_fd, id);
1748 a158c901 2018-12-23 stsp static const struct got_error *
1749 268f7291 2018-12-24 stsp open_tag(struct got_tag_object **tag, struct got_repository *repo,
1750 268f7291 2018-12-24 stsp struct got_object_id *id, int check_cache)
1752 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1753 268f7291 2018-12-24 stsp struct got_packidx *packidx = NULL;
1755 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1756 5d844a1e 2019-08-13 stsp struct got_object *obj = NULL;
1757 5d844a1e 2019-08-13 stsp int obj_type = GOT_OBJ_TYPE_ANY;
1759 f4a881ce 2018-11-17 stsp if (check_cache) {
1760 268f7291 2018-12-24 stsp *tag = got_repo_get_cached_tag(repo, id);
1761 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1762 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1763 f4a881ce 2018-11-17 stsp return NULL;
1766 f4a881ce 2018-11-17 stsp *tag = NULL;
1768 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1769 e82b1d81 2019-07-27 stsp if (err == NULL) {
1770 268f7291 2018-12-24 stsp struct got_pack *pack = NULL;
1772 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
1773 aea75d87 2021-07-06 stsp packidx->path_packidx);
1775 268f7291 2018-12-24 stsp return err;
1777 268f7291 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1778 f4a881ce 2018-11-17 stsp if (pack == NULL) {
1779 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1782 8d2c5ea3 2019-08-13 stsp goto done;
1785 992eb9d8 2020-02-07 tracey /* Beware of "lightweight" tags: Check object type first. */
1786 5d844a1e 2019-08-13 stsp err = read_packed_object_privsep(&obj, repo, pack, packidx,
1789 5d844a1e 2019-08-13 stsp goto done;
1790 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1791 5d844a1e 2019-08-13 stsp got_object_close(obj);
1792 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG) {
1793 5d844a1e 2019-08-13 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1794 5d844a1e 2019-08-13 stsp goto done;
1796 5d844a1e 2019-08-13 stsp err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1797 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1800 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1802 e82b1d81 2019-07-27 stsp return err;
1803 d5c81d44 2021-07-08 stsp err = got_object_read_header_privsep(&obj, id, repo, fd);
1805 5d844a1e 2019-08-13 stsp return err;
1806 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1807 5d844a1e 2019-08-13 stsp got_object_close(obj);
1808 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
1809 5d844a1e 2019-08-13 stsp return got_error(GOT_ERR_OBJ_TYPE);
1811 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1813 5d844a1e 2019-08-13 stsp return err;
1814 d5c81d44 2021-07-08 stsp err = read_tag_privsep(tag, fd, id, repo);
1817 f4a881ce 2018-11-17 stsp if (err == NULL) {
1818 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1819 268f7291 2018-12-24 stsp err = got_repo_cache_tag(repo, id, *tag);
1822 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1823 f4a881ce 2018-11-17 stsp return err;
1826 f4a881ce 2018-11-17 stsp const struct got_error *
1827 f4a881ce 2018-11-17 stsp got_object_open_as_tag(struct got_tag_object **tag,
1828 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object_id *id)
1830 f4a881ce 2018-11-17 stsp *tag = got_repo_get_cached_tag(repo, id);
1831 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1832 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1833 f4a881ce 2018-11-17 stsp return NULL;
1836 268f7291 2018-12-24 stsp return open_tag(tag, repo, id, 0);
1839 f4a881ce 2018-11-17 stsp const struct got_error *
1840 f4a881ce 2018-11-17 stsp got_object_tag_open(struct got_tag_object **tag,
1841 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object *obj)
1843 268f7291 2018-12-24 stsp return open_tag(tag, repo, got_object_get_id(obj), 1);
1846 d24820bf 2019-08-11 stsp const char *
1847 d24820bf 2019-08-11 stsp got_object_tag_get_name(struct got_tag_object *tag)
1849 d24820bf 2019-08-11 stsp return tag->tag;
1853 0bd18d37 2019-02-01 stsp got_object_tag_get_object_type(struct got_tag_object *tag)
1855 0bd18d37 2019-02-01 stsp return tag->obj_type;
1858 0bd18d37 2019-02-01 stsp struct got_object_id *
1859 0bd18d37 2019-02-01 stsp got_object_tag_get_object_id(struct got_tag_object *tag)
1861 0bd18d37 2019-02-01 stsp return &tag->id;
1865 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_time(struct got_tag_object *tag)
1867 01073a5d 2019-08-22 stsp return tag->tagger_time;
1871 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1873 01073a5d 2019-08-22 stsp return tag->tagger_gmtoff;
1876 01073a5d 2019-08-22 stsp const char *
1877 01073a5d 2019-08-22 stsp got_object_tag_get_tagger(struct got_tag_object *tag)
1879 01073a5d 2019-08-22 stsp return tag->tagger;
1882 01073a5d 2019-08-22 stsp const char *
1883 01073a5d 2019-08-22 stsp got_object_tag_get_message(struct got_tag_object *tag)
1885 01073a5d 2019-08-22 stsp return tag->tagmsg;
1888 776d4d29 2018-06-17 stsp static struct got_tree_entry *
1889 65a9bbe9 2018-09-15 stsp find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1893 63da309a 2018-11-07 stsp /* Note that tree entries are sorted in strncmp() order. */
1894 56e0773d 2019-11-28 stsp for (i = 0; i < tree->nentries; i++) {
1895 56e0773d 2019-11-28 stsp struct got_tree_entry *te = &tree->entries[i];
1896 63da309a 2018-11-07 stsp int cmp = strncmp(te->name, name, len);
1897 63da309a 2018-11-07 stsp if (cmp < 0)
1899 63da309a 2018-11-07 stsp if (cmp > 0)
1901 63da309a 2018-11-07 stsp if (te->name[len] == '\0')
1902 776d4d29 2018-06-17 stsp return te;
1904 eb651edf 2018-02-11 stsp return NULL;
1907 56e0773d 2019-11-28 stsp struct got_tree_entry *
1908 a129376b 2019-03-28 stsp got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1910 a129376b 2019-03-28 stsp return find_entry_by_name(tree, name, strlen(name));
1913 776d4d29 2018-06-17 stsp const struct got_error *
1914 67b631c9 2021-10-10 stsp got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1915 67b631c9 2021-10-10 stsp struct got_repository *repo, struct got_tree_object *tree,
1916 67b631c9 2021-10-10 stsp const char *path)
1918 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
1919 67b631c9 2021-10-10 stsp struct got_tree_object *subtree = NULL;
1920 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
1921 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1922 b7cd37e5 2018-11-18 stsp size_t seglen;
1924 27d434c2 2018-09-15 stsp *id = NULL;
1927 5e54fb30 2019-05-31 stsp while (s[0] == '/')
1930 65a9bbe9 2018-09-15 stsp seglen = 0;
1931 67b631c9 2021-10-10 stsp subtree = tree;
1932 b7cd37e5 2018-11-18 stsp while (*s) {
1933 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
1935 776d4d29 2018-06-17 stsp if (*s != '/') {
1942 67b631c9 2021-10-10 stsp te = find_entry_by_name(subtree, seg, seglen);
1943 db37e2c0 2018-06-21 stsp if (te == NULL) {
1944 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1945 776d4d29 2018-06-17 stsp goto done;
1948 b7cd37e5 2018-11-18 stsp if (*s == '\0')
1951 776d4d29 2018-06-17 stsp seg = s + 1;
1952 65a9bbe9 2018-09-15 stsp seglen = 0;
1955 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
1957 db37e2c0 2018-06-21 stsp te = NULL;
1959 776d4d29 2018-06-17 stsp goto done;
1960 67b631c9 2021-10-10 stsp if (subtree != tree)
1961 67b631c9 2021-10-10 stsp got_object_tree_close(subtree);
1962 67b631c9 2021-10-10 stsp subtree = next_tree;
1967 56e0773d 2019-11-28 stsp *id = got_object_id_dup(&te->id);
1968 27d434c2 2018-09-15 stsp if (*id == NULL)
1969 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
1971 67b631c9 2021-10-10 stsp *mode = te->mode;
1973 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1975 67b631c9 2021-10-10 stsp if (subtree && subtree != tree)
1976 67b631c9 2021-10-10 stsp got_object_tree_close(subtree);
1977 67b631c9 2021-10-10 stsp return err;
1979 67b631c9 2021-10-10 stsp const struct got_error *
1980 67b631c9 2021-10-10 stsp got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1981 67b631c9 2021-10-10 stsp struct got_object_id *commit_id, const char *path)
1983 67b631c9 2021-10-10 stsp const struct got_error *err = NULL;
1984 67b631c9 2021-10-10 stsp struct got_commit_object *commit = NULL;
1985 67b631c9 2021-10-10 stsp struct got_tree_object *tree = NULL;
1987 67b631c9 2021-10-10 stsp *id = NULL;
1989 67b631c9 2021-10-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
1991 67b631c9 2021-10-10 stsp goto done;
1993 67b631c9 2021-10-10 stsp /* Handle opening of root of commit's tree. */
1994 67b631c9 2021-10-10 stsp if (got_path_is_root_dir(path)) {
1995 67b631c9 2021-10-10 stsp *id = got_object_id_dup(commit->tree_id);
1996 67b631c9 2021-10-10 stsp if (*id == NULL)
1997 67b631c9 2021-10-10 stsp err = got_error_from_errno("got_object_id_dup");
1999 67b631c9 2021-10-10 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
2001 67b631c9 2021-10-10 stsp goto done;
2002 67b631c9 2021-10-10 stsp err = got_object_tree_find_path(id, NULL, repo, tree, path);
2005 776d4d29 2018-06-17 stsp if (commit)
2006 776d4d29 2018-06-17 stsp got_object_commit_close(commit);
2008 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
2009 776d4d29 2018-06-17 stsp return err;
2013 ac5f2b26 2020-05-05 stsp * Normalize file mode bits to avoid false positive tree entry differences
2014 ac5f2b26 2020-05-05 stsp * in case tree entries have unexpected mode bits set.
2016 ac5f2b26 2020-05-05 stsp static mode_t
2017 ac5f2b26 2020-05-05 stsp normalize_mode_for_comparison(mode_t mode)
2020 ac5f2b26 2020-05-05 stsp * For directories, the only relevant bit is the IFDIR bit.
2021 ac5f2b26 2020-05-05 stsp * This allows us to detect paths changing from a directory
2022 ac5f2b26 2020-05-05 stsp * to a file and vice versa.
2024 ac5f2b26 2020-05-05 stsp if (S_ISDIR(mode))
2025 ac5f2b26 2020-05-05 stsp return mode & S_IFDIR;
2028 40dde666 2020-07-23 stsp * For symlinks, the only relevant bit is the IFLNK bit.
2029 40dde666 2020-07-23 stsp * This allows us to detect paths changing from a symlinks
2030 40dde666 2020-07-23 stsp * to a file or directory and vice versa.
2032 40dde666 2020-07-23 stsp if (S_ISLNK(mode))
2033 40dde666 2020-07-23 stsp return mode & S_IFLNK;
2035 ac5f2b26 2020-05-05 stsp /* For files, the only change we care about is the executable bit. */
2036 ac5f2b26 2020-05-05 stsp return mode & S_IXUSR;
2039 07862c20 2018-09-15 stsp const struct got_error *
2040 07862c20 2018-09-15 stsp got_object_tree_path_changed(int *changed,
2041 07862c20 2018-09-15 stsp struct got_tree_object *tree01, struct got_tree_object *tree02,
2042 07862c20 2018-09-15 stsp const char *path, struct got_repository *repo)
2044 07862c20 2018-09-15 stsp const struct got_error *err = NULL;
2045 07862c20 2018-09-15 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2046 07862c20 2018-09-15 stsp struct got_tree_entry *te1 = NULL, *te2 = NULL;
2047 65a9bbe9 2018-09-15 stsp const char *seg, *s;
2048 3b7f9878 2018-11-18 stsp size_t seglen;
2050 07862c20 2018-09-15 stsp *changed = 0;
2052 07862c20 2018-09-15 stsp /* We not do support comparing the root path. */
2053 61a7d79f 2020-02-29 stsp if (got_path_is_root_dir(path))
2054 63f810e6 2020-02-29 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
2056 07862c20 2018-09-15 stsp tree1 = tree01;
2057 07862c20 2018-09-15 stsp tree2 = tree02;
2059 61a7d79f 2020-02-29 stsp while (*s == '/')
2062 65a9bbe9 2018-09-15 stsp seglen = 0;
2063 3b7f9878 2018-11-18 stsp while (*s) {
2064 07862c20 2018-09-15 stsp struct got_tree_object *next_tree1, *next_tree2;
2065 ac5f2b26 2020-05-05 stsp mode_t mode1, mode2;
2067 07862c20 2018-09-15 stsp if (*s != '/') {
2074 65a9bbe9 2018-09-15 stsp te1 = find_entry_by_name(tree1, seg, seglen);
2075 07862c20 2018-09-15 stsp if (te1 == NULL) {
2076 07862c20 2018-09-15 stsp err = got_error(GOT_ERR_NO_OBJ);
2077 07862c20 2018-09-15 stsp goto done;
2080 e8bfb8f3 2020-12-18 stsp if (tree2)
2081 e8bfb8f3 2020-12-18 stsp te2 = find_entry_by_name(tree2, seg, seglen);
2083 e8bfb8f3 2020-12-18 stsp if (te2) {
2084 e8bfb8f3 2020-12-18 stsp mode1 = normalize_mode_for_comparison(te1->mode);
2085 e8bfb8f3 2020-12-18 stsp mode2 = normalize_mode_for_comparison(te2->mode);
2086 e8bfb8f3 2020-12-18 stsp if (mode1 != mode2) {
2087 e8bfb8f3 2020-12-18 stsp *changed = 1;
2088 e8bfb8f3 2020-12-18 stsp goto done;
2091 e8bfb8f3 2020-12-18 stsp if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2092 e8bfb8f3 2020-12-18 stsp *changed = 0;
2093 e8bfb8f3 2020-12-18 stsp goto done;
2097 3b7f9878 2018-11-18 stsp if (*s == '\0') { /* final path element */
2098 07862c20 2018-09-15 stsp *changed = 1;
2099 07862c20 2018-09-15 stsp goto done;
2102 07862c20 2018-09-15 stsp seg = s + 1;
2104 65a9bbe9 2018-09-15 stsp seglen = 0;
2106 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree1, repo,
2107 56e0773d 2019-11-28 stsp &te1->id);
2108 07862c20 2018-09-15 stsp te1 = NULL;
2110 07862c20 2018-09-15 stsp goto done;
2111 a31cea73 2018-09-15 stsp if (tree1 != tree01)
2112 a31cea73 2018-09-15 stsp got_object_tree_close(tree1);
2113 07862c20 2018-09-15 stsp tree1 = next_tree1;
2115 e8bfb8f3 2020-12-18 stsp if (te2) {
2116 e8bfb8f3 2020-12-18 stsp err = got_object_open_as_tree(&next_tree2, repo,
2117 e8bfb8f3 2020-12-18 stsp &te2->id);
2118 e8bfb8f3 2020-12-18 stsp te2 = NULL;
2120 e8bfb8f3 2020-12-18 stsp goto done;
2121 e8bfb8f3 2020-12-18 stsp if (tree2 != tree02)
2122 e8bfb8f3 2020-12-18 stsp got_object_tree_close(tree2);
2123 e8bfb8f3 2020-12-18 stsp tree2 = next_tree2;
2124 e8bfb8f3 2020-12-18 stsp } else if (tree2) {
2125 e8bfb8f3 2020-12-18 stsp if (tree2 != tree02)
2126 e8bfb8f3 2020-12-18 stsp got_object_tree_close(tree2);
2127 e8bfb8f3 2020-12-18 stsp tree2 = NULL;
2132 a31cea73 2018-09-15 stsp if (tree1 && tree1 != tree01)
2133 07862c20 2018-09-15 stsp got_object_tree_close(tree1);
2134 a31cea73 2018-09-15 stsp if (tree2 && tree2 != tree02)
2135 07862c20 2018-09-15 stsp got_object_tree_close(tree2);
2136 77880158 2018-11-04 stsp return err;
2139 ed175427 2019-05-09 stsp const struct got_error *
2140 ed175427 2019-05-09 stsp got_object_tree_entry_dup(struct got_tree_entry **new_te,
2141 ed175427 2019-05-09 stsp struct got_tree_entry *te)
2143 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
2145 ed175427 2019-05-09 stsp *new_te = calloc(1, sizeof(**new_te));
2146 ed175427 2019-05-09 stsp if (*new_te == NULL)
2147 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
2149 ed175427 2019-05-09 stsp (*new_te)->mode = te->mode;
2150 56e0773d 2019-11-28 stsp memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2151 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2152 8c4eabf2 2019-05-10 stsp return err;
2156 56e0773d 2019-11-28 stsp got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2158 63c5ca5d 2019-08-24 stsp return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2162 e40622f4 2020-07-23 stsp got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2164 e40622f4 2020-07-23 stsp /* S_IFDIR check avoids confusing symlinks with submodules. */
2165 e40622f4 2020-07-23 stsp return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2168 e40622f4 2020-07-23 stsp static const struct got_error *
2169 e40622f4 2020-07-23 stsp resolve_symlink(char **link_target, const char *path,
2170 e40622f4 2020-07-23 stsp struct got_object_id *commit_id, struct got_repository *repo)
2172 e40622f4 2020-07-23 stsp const struct got_error *err = NULL;
2173 dbdd6209 2020-10-19 stsp char buf[PATH_MAX];
2174 e40622f4 2020-07-23 stsp char *name, *parent_path = NULL;
2175 e40622f4 2020-07-23 stsp struct got_object_id *tree_obj_id = NULL;
2176 e40622f4 2020-07-23 stsp struct got_tree_object *tree = NULL;
2177 e40622f4 2020-07-23 stsp struct got_tree_entry *te = NULL;
2179 e40622f4 2020-07-23 stsp *link_target = NULL;
2181 dbdd6209 2020-10-19 stsp if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2182 dbdd6209 2020-10-19 stsp return got_error(GOT_ERR_NO_SPACE);
2184 dbdd6209 2020-10-19 stsp name = basename(buf);
2185 e40622f4 2020-07-23 stsp if (name == NULL)
2186 e40622f4 2020-07-23 stsp return got_error_from_errno2("basename", path);
2188 e40622f4 2020-07-23 stsp err = got_path_dirname(&parent_path, path);
2190 e40622f4 2020-07-23 stsp return err;
2192 e40622f4 2020-07-23 stsp err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
2193 e40622f4 2020-07-23 stsp parent_path);
2194 e40622f4 2020-07-23 stsp if (err) {
2195 e40622f4 2020-07-23 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2196 e40622f4 2020-07-23 stsp /* Display the complete path in error message. */
2197 e40622f4 2020-07-23 stsp err = got_error_path(path, err->code);
2199 e40622f4 2020-07-23 stsp goto done;
2202 e40622f4 2020-07-23 stsp err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2204 e40622f4 2020-07-23 stsp goto done;
2206 e40622f4 2020-07-23 stsp te = got_object_tree_find_entry(tree, name);
2207 e40622f4 2020-07-23 stsp if (te == NULL) {
2208 e40622f4 2020-07-23 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2209 e40622f4 2020-07-23 stsp goto done;
2212 e40622f4 2020-07-23 stsp if (got_object_tree_entry_is_symlink(te)) {
2213 e40622f4 2020-07-23 stsp err = got_tree_entry_get_symlink_target(link_target, te, repo);
2215 e40622f4 2020-07-23 stsp goto done;
2216 e40622f4 2020-07-23 stsp if (!got_path_is_absolute(*link_target)) {
2217 e40622f4 2020-07-23 stsp char *abspath;
2218 e40622f4 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", parent_path,
2219 e40622f4 2020-07-23 stsp *link_target) == -1) {
2220 e40622f4 2020-07-23 stsp err = got_error_from_errno("asprintf");
2221 e40622f4 2020-07-23 stsp goto done;
2223 e40622f4 2020-07-23 stsp free(*link_target);
2224 e40622f4 2020-07-23 stsp *link_target = malloc(PATH_MAX);
2225 e40622f4 2020-07-23 stsp if (*link_target == NULL) {
2226 e40622f4 2020-07-23 stsp err = got_error_from_errno("malloc");
2227 e40622f4 2020-07-23 stsp goto done;
2229 e40622f4 2020-07-23 stsp err = got_canonpath(abspath, *link_target, PATH_MAX);
2230 e40622f4 2020-07-23 stsp free(abspath);
2232 e40622f4 2020-07-23 stsp goto done;
2236 e40622f4 2020-07-23 stsp free(tree_obj_id);
2238 e40622f4 2020-07-23 stsp got_object_tree_close(tree);
2239 e40622f4 2020-07-23 stsp if (err) {
2240 e40622f4 2020-07-23 stsp free(*link_target);
2241 e40622f4 2020-07-23 stsp *link_target = NULL;
2243 e40622f4 2020-07-23 stsp return err;
2246 ca6e02ac 2020-01-07 stsp const struct got_error *
2247 e40622f4 2020-07-23 stsp got_object_resolve_symlinks(char **link_target, const char *path,
2248 e40622f4 2020-07-23 stsp struct got_object_id *commit_id, struct got_repository *repo)
2250 e40622f4 2020-07-23 stsp const struct got_error *err = NULL;
2251 e40622f4 2020-07-23 stsp char *next_target = NULL;
2252 e40622f4 2020-07-23 stsp int max_recursion = 40; /* matches Git */
2254 e40622f4 2020-07-23 stsp *link_target = NULL;
2257 e40622f4 2020-07-23 stsp err = resolve_symlink(&next_target,
2258 e40622f4 2020-07-23 stsp *link_target ? *link_target : path, commit_id, repo);
2261 e40622f4 2020-07-23 stsp if (next_target) {
2262 e40622f4 2020-07-23 stsp free(*link_target);
2263 e40622f4 2020-07-23 stsp if (--max_recursion == 0) {
2264 e40622f4 2020-07-23 stsp err = got_error_path(path, GOT_ERR_RECURSION);
2265 e40622f4 2020-07-23 stsp *link_target = NULL;
2268 e40622f4 2020-07-23 stsp *link_target = next_target;
2270 e40622f4 2020-07-23 stsp } while (next_target);
2272 e40622f4 2020-07-23 stsp return err;
2275 e40622f4 2020-07-23 stsp const struct got_error *
2276 ca6e02ac 2020-01-07 stsp got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2277 ca6e02ac 2020-01-07 stsp struct got_object_id *commit_id, const char *path,
2278 ca6e02ac 2020-01-07 stsp struct got_repository *repo)
2280 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
2281 ca6e02ac 2020-01-07 stsp struct got_pack *pack = NULL;
2282 ca6e02ac 2020-01-07 stsp struct got_packidx *packidx = NULL;
2283 ca6e02ac 2020-01-07 stsp char *path_packfile = NULL;
2284 ca6e02ac 2020-01-07 stsp struct got_commit_object *changed_commit = NULL;
2285 ca6e02ac 2020-01-07 stsp struct got_object_id *changed_commit_id = NULL;
2288 ca6e02ac 2020-01-07 stsp err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2289 ca6e02ac 2020-01-07 stsp if (err) {
2290 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
2291 ca6e02ac 2020-01-07 stsp return err;
2292 ca6e02ac 2020-01-07 stsp return NULL;
2295 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
2296 aea75d87 2021-07-06 stsp packidx->path_packidx);
2298 ca6e02ac 2020-01-07 stsp return err;
2300 ca6e02ac 2020-01-07 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
2301 ca6e02ac 2020-01-07 stsp if (pack == NULL) {
2302 ca6e02ac 2020-01-07 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2304 ca6e02ac 2020-01-07 stsp goto done;
2307 ca6e02ac 2020-01-07 stsp if (pack->privsep_child == NULL) {
2308 ca6e02ac 2020-01-07 stsp err = start_pack_privsep_child(pack, packidx);
2310 ca6e02ac 2020-01-07 stsp goto done;
2313 ca6e02ac 2020-01-07 stsp err = got_privsep_send_commit_traversal_request(
2314 ca6e02ac 2020-01-07 stsp pack->privsep_child->ibuf, commit_id, idx, path);
2316 ca6e02ac 2020-01-07 stsp goto done;
2318 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_traversed_commits(&changed_commit,
2319 ca6e02ac 2020-01-07 stsp &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2321 ca6e02ac 2020-01-07 stsp goto done;
2323 ca6e02ac 2020-01-07 stsp if (changed_commit) {
2325 ca6e02ac 2020-01-07 stsp * Cache the commit in which the path was changed.
2326 ca6e02ac 2020-01-07 stsp * This commit might be opened again soon.
2328 ca6e02ac 2020-01-07 stsp changed_commit->refcnt++;
2329 ca6e02ac 2020-01-07 stsp err = got_repo_cache_commit(repo, changed_commit_id,
2330 ca6e02ac 2020-01-07 stsp changed_commit);
2331 ca6e02ac 2020-01-07 stsp got_object_commit_close(changed_commit);
2334 ca6e02ac 2020-01-07 stsp free(path_packfile);
2335 ca6e02ac 2020-01-07 stsp free(changed_commit_id);
2336 ca6e02ac 2020-01-07 stsp return err;