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 8bd0cdad 2021-12-31 stsp *fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
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 d3c116bf 2021-10-15 stsp /* *outfd must be initialized to -1 by caller */
532 59d1e4a0 2021-03-10 stsp const struct got_error *
533 d3c116bf 2021-10-15 stsp got_object_raw_open(struct got_raw_object **obj, int *outfd,
534 94dac27c 2021-10-15 stsp struct got_repository *repo, struct got_object_id *id)
536 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
537 59d1e4a0 2021-03-10 stsp struct got_packidx *packidx = NULL;
539 59d1e4a0 2021-03-10 stsp uint8_t *outbuf = NULL;
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 d3c116bf 2021-10-15 stsp *obj = got_repo_get_cached_raw_object(repo, id);
545 d3c116bf 2021-10-15 stsp if (*obj != NULL) {
546 d3c116bf 2021-10-15 stsp (*obj)->refcnt++;
547 d3c116bf 2021-10-15 stsp return NULL;
550 d3c116bf 2021-10-15 stsp if (*outfd == -1) {
551 d3c116bf 2021-10-15 stsp *outfd = got_opentempfd();
552 d3c116bf 2021-10-15 stsp if (*outfd == -1)
553 d3c116bf 2021-10-15 stsp return got_error_from_errno("got_opentempfd");
556 59d1e4a0 2021-03-10 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
557 59d1e4a0 2021-03-10 stsp if (err == NULL) {
558 59d1e4a0 2021-03-10 stsp struct got_pack *pack = NULL;
560 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
561 aea75d87 2021-07-06 stsp packidx->path_packidx);
565 59d1e4a0 2021-03-10 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
566 59d1e4a0 2021-03-10 stsp if (pack == NULL) {
567 59d1e4a0 2021-03-10 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
572 59d1e4a0 2021-03-10 stsp err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
573 d3c116bf 2021-10-15 stsp *outfd, pack, packidx, idx, id);
576 59d1e4a0 2021-03-10 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
579 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
582 d3c116bf 2021-10-15 stsp err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
583 d5c81d44 2021-07-08 stsp id, repo, fd);
588 59d1e4a0 2021-03-10 stsp *obj = calloc(1, sizeof(**obj));
589 59d1e4a0 2021-03-10 stsp if (*obj == NULL) {
590 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("calloc");
594 59d1e4a0 2021-03-10 stsp if (outbuf) {
595 59d1e4a0 2021-03-10 stsp (*obj)->f = fmemopen(outbuf, hdrlen + size, "r");
596 59d1e4a0 2021-03-10 stsp if ((*obj)->f == NULL) {
597 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fdopen");
600 59d1e4a0 2021-03-10 stsp (*obj)->data = outbuf;
602 59d1e4a0 2021-03-10 stsp struct stat sb;
603 d3c116bf 2021-10-15 stsp if (fstat(*outfd, &sb) == -1) {
604 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fstat");
608 a8591711 2021-06-18 stsp if (sb.st_size != hdrlen + size) {
609 59d1e4a0 2021-03-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
613 d3c116bf 2021-10-15 stsp (*obj)->f = fdopen(*outfd, "r");
614 59d1e4a0 2021-03-10 stsp if ((*obj)->f == NULL) {
615 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fdopen");
618 59d1e4a0 2021-03-10 stsp (*obj)->data = NULL;
619 d3c116bf 2021-10-15 stsp *outfd = -1;
621 59d1e4a0 2021-03-10 stsp (*obj)->hdrlen = hdrlen;
622 59d1e4a0 2021-03-10 stsp (*obj)->size = size;
623 d3c116bf 2021-10-15 stsp err = got_repo_cache_raw_object(repo, id, *obj);
625 59d1e4a0 2021-03-10 stsp free(path_packfile);
627 59d1e4a0 2021-03-10 stsp if (*obj) {
628 59d1e4a0 2021-03-10 stsp got_object_raw_close(*obj);
629 59d1e4a0 2021-03-10 stsp *obj = NULL;
631 59d1e4a0 2021-03-10 stsp free(outbuf);
633 d3c116bf 2021-10-15 stsp (*obj)->refcnt++;
634 59d1e4a0 2021-03-10 stsp return err;
637 59d1e4a0 2021-03-10 stsp const struct got_error *
638 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
639 6dfa2fd3 2018-02-12 stsp const char *id_str)
641 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
643 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
644 6dd1ece6 2019-11-10 stsp return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
646 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
649 15a94983 2018-12-23 stsp const struct got_error *
650 15a94983 2018-12-23 stsp got_object_resolve_id_str(struct got_object_id **id,
651 15a94983 2018-12-23 stsp struct got_repository *repo, const char *id_str)
653 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
654 15a94983 2018-12-23 stsp struct got_object *obj;
656 15a94983 2018-12-23 stsp err = got_object_open_by_id_str(&obj, repo, id_str);
658 15a94983 2018-12-23 stsp return err;
660 15a94983 2018-12-23 stsp *id = got_object_id_dup(got_object_get_id(obj));
661 15a94983 2018-12-23 stsp got_object_close(obj);
662 15a94983 2018-12-23 stsp if (*id == NULL)
663 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
665 15a94983 2018-12-23 stsp return NULL;
668 434025f3 2018-09-16 stsp static const struct got_error *
669 a158c901 2018-12-23 stsp request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
670 a158c901 2018-12-23 stsp int pack_idx, struct got_object_id *id)
672 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
674 a158c901 2018-12-23 stsp err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
677 a158c901 2018-12-23 stsp return err;
679 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
681 ca6e02ac 2020-01-07 stsp return err;
683 ca6e02ac 2020-01-07 stsp (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
684 ca6e02ac 2020-01-07 stsp return NULL;
687 a158c901 2018-12-23 stsp static const struct got_error *
688 a158c901 2018-12-23 stsp read_packed_commit_privsep(struct got_commit_object **commit,
689 a158c901 2018-12-23 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
690 a158c901 2018-12-23 stsp struct got_object_id *id)
692 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
694 a158c901 2018-12-23 stsp if (pack->privsep_child)
695 a158c901 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
697 711fb6e8 2018-12-23 stsp err = start_pack_privsep_child(pack, packidx);
699 a158c901 2018-12-23 stsp return err;
701 711fb6e8 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
704 9f2369b0 2018-12-24 stsp static const struct got_error *
705 9f2369b0 2018-12-24 stsp request_commit(struct got_commit_object **commit, struct got_repository *repo,
706 d5c81d44 2021-07-08 stsp int fd, struct got_object_id *id)
708 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
709 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
711 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
713 d5c81d44 2021-07-08 stsp err = got_privsep_send_commit_req(ibuf, fd, id, -1);
715 9f2369b0 2018-12-24 stsp return err;
717 9f2369b0 2018-12-24 stsp return got_privsep_recv_commit(commit, ibuf);
720 9f2369b0 2018-12-24 stsp static const struct got_error *
721 9f2369b0 2018-12-24 stsp read_commit_privsep(struct got_commit_object **commit, int obj_fd,
722 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo)
724 ddc7b220 2019-09-08 stsp const struct got_error *err;
725 9f2369b0 2018-12-24 stsp int imsg_fds[2];
727 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
729 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
730 d5c81d44 2021-07-08 stsp return request_commit(commit, repo, obj_fd, id);
732 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
733 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
734 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
736 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
737 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
738 ddc7b220 2019-09-08 stsp free(ibuf);
739 ddc7b220 2019-09-08 stsp return err;
742 9f2369b0 2018-12-24 stsp pid = fork();
743 ddc7b220 2019-09-08 stsp if (pid == -1) {
744 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
745 ddc7b220 2019-09-08 stsp free(ibuf);
746 ddc7b220 2019-09-08 stsp return err;
748 9f2369b0 2018-12-24 stsp else if (pid == 0) {
749 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
750 9f2369b0 2018-12-24 stsp repo->path);
751 9f2369b0 2018-12-24 stsp /* not reached */
754 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
755 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
756 ddc7b220 2019-09-08 stsp free(ibuf);
757 ddc7b220 2019-09-08 stsp return err;
759 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
760 9f2369b0 2018-12-24 stsp imsg_fds[0];
761 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
762 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
763 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
765 d5c81d44 2021-07-08 stsp return request_commit(commit, repo, obj_fd, id);
769 a158c901 2018-12-23 stsp static const struct got_error *
770 434025f3 2018-09-16 stsp open_commit(struct got_commit_object **commit,
771 1785f84a 2018-12-23 stsp struct got_repository *repo, struct got_object_id *id, int check_cache)
773 434025f3 2018-09-16 stsp const struct got_error *err = NULL;
774 1785f84a 2018-12-23 stsp struct got_packidx *packidx = NULL;
776 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
778 434025f3 2018-09-16 stsp if (check_cache) {
779 1785f84a 2018-12-23 stsp *commit = got_repo_get_cached_commit(repo, id);
780 434025f3 2018-09-16 stsp if (*commit != NULL) {
781 434025f3 2018-09-16 stsp (*commit)->refcnt++;
782 434025f3 2018-09-16 stsp return NULL;
785 434025f3 2018-09-16 stsp *commit = NULL;
787 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
788 e82b1d81 2019-07-27 stsp if (err == NULL) {
789 1785f84a 2018-12-23 stsp struct got_pack *pack = NULL;
791 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
792 aea75d87 2021-07-06 stsp packidx->path_packidx);
794 1785f84a 2018-12-23 stsp return err;
796 1785f84a 2018-12-23 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
797 434025f3 2018-09-16 stsp if (pack == NULL) {
798 1785f84a 2018-12-23 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
803 a158c901 2018-12-23 stsp err = read_packed_commit_privsep(commit, pack,
804 1785f84a 2018-12-23 stsp packidx, idx, id);
805 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
808 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
810 e82b1d81 2019-07-27 stsp return err;
811 d5c81d44 2021-07-08 stsp err = read_commit_privsep(commit, fd, id, repo);
814 434025f3 2018-09-16 stsp if (err == NULL) {
815 434025f3 2018-09-16 stsp (*commit)->refcnt++;
816 1785f84a 2018-12-23 stsp err = got_repo_cache_commit(repo, id, *commit);
819 8d2c5ea3 2019-08-13 stsp free(path_packfile);
820 e32baab7 2018-11-05 stsp return err;
823 e32baab7 2018-11-05 stsp const struct got_error *
824 e32baab7 2018-11-05 stsp got_object_open_as_commit(struct got_commit_object **commit,
825 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object_id *id)
827 e32baab7 2018-11-05 stsp *commit = got_repo_get_cached_commit(repo, id);
828 e32baab7 2018-11-05 stsp if (*commit != NULL) {
829 e32baab7 2018-11-05 stsp (*commit)->refcnt++;
830 e32baab7 2018-11-05 stsp return NULL;
833 1785f84a 2018-12-23 stsp return open_commit(commit, repo, id, 0);
836 e32baab7 2018-11-05 stsp const struct got_error *
837 e32baab7 2018-11-05 stsp got_object_commit_open(struct got_commit_object **commit,
838 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object *obj)
840 1785f84a 2018-12-23 stsp return open_commit(commit, repo, got_object_get_id(obj), 1);
843 434025f3 2018-09-16 stsp const struct got_error *
844 dbc6a6b6 2018-07-12 stsp got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
846 dbc6a6b6 2018-07-12 stsp const struct got_error *err = NULL;
848 dbc6a6b6 2018-07-12 stsp *qid = calloc(1, sizeof(**qid));
849 dbc6a6b6 2018-07-12 stsp if (*qid == NULL)
850 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
852 dbc6a6b6 2018-07-12 stsp (*qid)->id = got_object_id_dup(id);
853 dbc6a6b6 2018-07-12 stsp if ((*qid)->id == NULL) {
854 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
855 fa2f6902 2018-07-23 stsp got_object_qid_free(*qid);
856 dbc6a6b6 2018-07-12 stsp *qid = NULL;
857 dbc6a6b6 2018-07-12 stsp return err;
860 9ca9aafb 2021-06-18 stsp return NULL;
863 9ca9aafb 2021-06-18 stsp const struct got_error *
864 9ca9aafb 2021-06-18 stsp got_object_id_queue_copy(const struct got_object_id_queue *src,
865 9ca9aafb 2021-06-18 stsp struct got_object_id_queue *dest)
867 9ca9aafb 2021-06-18 stsp const struct got_error *err;
868 9ca9aafb 2021-06-18 stsp struct got_object_qid *qid;
870 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, src, entry) {
871 9ca9aafb 2021-06-18 stsp struct got_object_qid *new;
873 9ca9aafb 2021-06-18 stsp * Deep-copy the object ID only. Let the caller deal
874 9ca9aafb 2021-06-18 stsp * with setting up the new->data pointer if needed.
876 9ca9aafb 2021-06-18 stsp err = got_object_qid_alloc(&new, qid->id);
878 9ca9aafb 2021-06-18 stsp got_object_id_queue_free(dest);
879 9ca9aafb 2021-06-18 stsp return err;
881 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(dest, new, entry);
884 dbc6a6b6 2018-07-12 stsp return NULL;
887 a158c901 2018-12-23 stsp static const struct got_error *
888 13c729f7 2018-12-24 stsp request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
889 13c729f7 2018-12-24 stsp int pack_idx, struct got_object_id *id)
891 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
893 13c729f7 2018-12-24 stsp err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
896 a158c901 2018-12-23 stsp return err;
898 a158c901 2018-12-23 stsp return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
901 71eb0e7f 2018-09-16 stsp static const struct got_error *
902 13c729f7 2018-12-24 stsp read_packed_tree_privsep(struct got_tree_object **tree,
903 13c729f7 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
904 13c729f7 2018-12-24 stsp struct got_object_id *id)
906 13c729f7 2018-12-24 stsp const struct got_error *err = NULL;
908 13c729f7 2018-12-24 stsp if (pack->privsep_child)
909 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
911 13c729f7 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
913 13c729f7 2018-12-24 stsp return err;
915 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
918 13c729f7 2018-12-24 stsp static const struct got_error *
919 9f2369b0 2018-12-24 stsp request_tree(struct got_tree_object **tree, struct got_repository *repo,
920 d5c81d44 2021-07-08 stsp int fd, struct got_object_id *id)
922 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
923 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
925 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
927 d5c81d44 2021-07-08 stsp err = got_privsep_send_tree_req(ibuf, fd, id, -1);
929 9f2369b0 2018-12-24 stsp return err;
931 9f2369b0 2018-12-24 stsp return got_privsep_recv_tree(tree, ibuf);
934 9f2369b0 2018-12-24 stsp const struct got_error *
935 9f2369b0 2018-12-24 stsp read_tree_privsep(struct got_tree_object **tree, int obj_fd,
936 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo)
938 ddc7b220 2019-09-08 stsp const struct got_error *err;
939 9f2369b0 2018-12-24 stsp int imsg_fds[2];
941 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
943 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
944 d5c81d44 2021-07-08 stsp return request_tree(tree, repo, obj_fd, id);
946 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
947 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
948 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
950 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
951 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
952 ddc7b220 2019-09-08 stsp free(ibuf);
953 ddc7b220 2019-09-08 stsp return err;
956 9f2369b0 2018-12-24 stsp pid = fork();
957 ddc7b220 2019-09-08 stsp if (pid == -1) {
958 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
959 ddc7b220 2019-09-08 stsp free(ibuf);
960 ddc7b220 2019-09-08 stsp return err;
962 9f2369b0 2018-12-24 stsp else if (pid == 0) {
963 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
964 9f2369b0 2018-12-24 stsp repo->path);
965 9f2369b0 2018-12-24 stsp /* not reached */
968 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
969 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
970 ddc7b220 2019-09-08 stsp free(ibuf);
971 ddc7b220 2019-09-08 stsp return err;
973 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
974 9f2369b0 2018-12-24 stsp imsg_fds[0];
975 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
976 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
977 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
980 d5c81d44 2021-07-08 stsp return request_tree(tree, repo, obj_fd, id);
983 9f2369b0 2018-12-24 stsp static const struct got_error *
984 13c729f7 2018-12-24 stsp open_tree(struct got_tree_object **tree, struct got_repository *repo,
985 13c729f7 2018-12-24 stsp struct got_object_id *id, int check_cache)
987 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
988 13c729f7 2018-12-24 stsp struct got_packidx *packidx = NULL;
990 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
992 71eb0e7f 2018-09-16 stsp if (check_cache) {
993 13c729f7 2018-12-24 stsp *tree = got_repo_get_cached_tree(repo, id);
994 71eb0e7f 2018-09-16 stsp if (*tree != NULL) {
995 71eb0e7f 2018-09-16 stsp (*tree)->refcnt++;
996 71eb0e7f 2018-09-16 stsp return NULL;
999 71eb0e7f 2018-09-16 stsp *tree = NULL;
1001 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1002 e82b1d81 2019-07-27 stsp if (err == NULL) {
1003 13c729f7 2018-12-24 stsp struct got_pack *pack = NULL;
1005 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
1006 aea75d87 2021-07-06 stsp packidx->path_packidx);
1008 13c729f7 2018-12-24 stsp return err;
1010 13c729f7 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1011 e7885405 2018-09-10 stsp if (pack == NULL) {
1012 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1015 8d2c5ea3 2019-08-13 stsp goto done;
1017 13c729f7 2018-12-24 stsp err = read_packed_tree_privsep(tree, pack,
1018 13c729f7 2018-12-24 stsp packidx, idx, id);
1019 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1022 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1024 e82b1d81 2019-07-27 stsp return err;
1025 d5c81d44 2021-07-08 stsp err = read_tree_privsep(tree, fd, id, repo);
1028 f6be5c30 2018-06-22 stsp if (err == NULL) {
1029 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
1030 13c729f7 2018-12-24 stsp err = got_repo_cache_tree(repo, id, *tree);
1033 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1034 776d4d29 2018-06-17 stsp return err;
1037 776d4d29 2018-06-17 stsp const struct got_error *
1038 776d4d29 2018-06-17 stsp got_object_open_as_tree(struct got_tree_object **tree,
1039 776d4d29 2018-06-17 stsp struct got_repository *repo, struct got_object_id *id)
1041 e8eb494a 2018-09-16 stsp *tree = got_repo_get_cached_tree(repo, id);
1042 e8eb494a 2018-09-16 stsp if (*tree != NULL) {
1043 e8eb494a 2018-09-16 stsp (*tree)->refcnt++;
1044 e8eb494a 2018-09-16 stsp return NULL;
1047 13c729f7 2018-12-24 stsp return open_tree(tree, repo, id, 0);
1050 71eb0e7f 2018-09-16 stsp const struct got_error *
1051 71eb0e7f 2018-09-16 stsp got_object_tree_open(struct got_tree_object **tree,
1052 71eb0e7f 2018-09-16 stsp struct got_repository *repo, struct got_object *obj)
1054 13c729f7 2018-12-24 stsp return open_tree(tree, repo, got_object_get_id(obj), 1);
1058 56e0773d 2019-11-28 stsp got_object_tree_get_nentries(struct got_tree_object *tree)
1060 56e0773d 2019-11-28 stsp return tree->nentries;
1063 56e0773d 2019-11-28 stsp struct got_tree_entry *
1064 56e0773d 2019-11-28 stsp got_object_tree_get_first_entry(struct got_tree_object *tree)
1066 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, 0);
1069 56e0773d 2019-11-28 stsp struct got_tree_entry *
1070 56e0773d 2019-11-28 stsp got_object_tree_get_last_entry(struct got_tree_object *tree)
1072 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, tree->nentries - 1);
1075 56e0773d 2019-11-28 stsp struct got_tree_entry *
1076 56e0773d 2019-11-28 stsp got_object_tree_get_entry(struct got_tree_object *tree, int i)
1078 56e0773d 2019-11-28 stsp if (i < 0 || i >= tree->nentries)
1079 56e0773d 2019-11-28 stsp return NULL;
1080 56e0773d 2019-11-28 stsp return &tree->entries[i];
1084 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(struct got_tree_entry *te)
1086 56e0773d 2019-11-28 stsp return te->mode;
1089 56e0773d 2019-11-28 stsp const char *
1090 56e0773d 2019-11-28 stsp got_tree_entry_get_name(struct got_tree_entry *te)
1092 56e0773d 2019-11-28 stsp return &te->name[0];
1095 56e0773d 2019-11-28 stsp struct got_object_id *
1096 56e0773d 2019-11-28 stsp got_tree_entry_get_id(struct got_tree_entry *te)
1098 56e0773d 2019-11-28 stsp return &te->id;
1101 0d6c6ee3 2020-05-20 stsp const struct got_error *
1102 af57b12a 2020-07-23 stsp got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1104 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
1105 32596e16 2020-07-23 stsp size_t len, totlen, hdrlen, offset;
1107 aa092692 2020-07-23 stsp *s = NULL;
1109 659dc16e 2020-07-23 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1110 659dc16e 2020-07-23 stsp totlen = 0;
1111 32596e16 2020-07-23 stsp offset = 0;
1115 659dc16e 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1117 af57b12a 2020-07-23 stsp return err;
1119 659dc16e 2020-07-23 stsp if (len == 0)
1122 659dc16e 2020-07-23 stsp totlen += len - hdrlen;
1123 af57b12a 2020-07-23 stsp p = realloc(*s, totlen + 1);
1124 659dc16e 2020-07-23 stsp if (p == NULL) {
1125 659dc16e 2020-07-23 stsp err = got_error_from_errno("realloc");
1127 af57b12a 2020-07-23 stsp *s = NULL;
1128 af57b12a 2020-07-23 stsp return err;
1131 659dc16e 2020-07-23 stsp /* Skip blob object header first time around. */
1132 af57b12a 2020-07-23 stsp memcpy(*s + offset,
1133 f8f7c882 2020-07-23 stsp got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1134 659dc16e 2020-07-23 stsp hdrlen = 0;
1135 32596e16 2020-07-23 stsp offset = totlen;
1136 659dc16e 2020-07-23 stsp } while (len > 0);
1138 af57b12a 2020-07-23 stsp (*s)[totlen] = '\0';
1139 af57b12a 2020-07-23 stsp return NULL;
1142 af57b12a 2020-07-23 stsp const struct got_error *
1143 af57b12a 2020-07-23 stsp got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1144 af57b12a 2020-07-23 stsp struct got_repository *repo)
1146 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
1147 af57b12a 2020-07-23 stsp struct got_blob_object *blob = NULL;
1149 af57b12a 2020-07-23 stsp *link_target = NULL;
1151 af57b12a 2020-07-23 stsp if (!got_object_tree_entry_is_symlink(te))
1152 af57b12a 2020-07-23 stsp return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1154 af57b12a 2020-07-23 stsp err = got_object_open_as_blob(&blob, repo,
1155 af57b12a 2020-07-23 stsp got_tree_entry_get_id(te), PATH_MAX);
1157 af57b12a 2020-07-23 stsp return err;
1159 af57b12a 2020-07-23 stsp err = got_object_blob_read_to_str(link_target, blob);
1160 af57b12a 2020-07-23 stsp got_object_blob_close(blob);
1161 659dc16e 2020-07-23 stsp if (err) {
1162 659dc16e 2020-07-23 stsp free(*link_target);
1163 659dc16e 2020-07-23 stsp *link_target = NULL;
1165 0d6c6ee3 2020-05-20 stsp return err;
1169 56e0773d 2019-11-28 stsp got_tree_entry_get_index(struct got_tree_entry *te)
1171 56e0773d 2019-11-28 stsp return te->idx;
1174 56e0773d 2019-11-28 stsp struct got_tree_entry *
1175 56e0773d 2019-11-28 stsp got_tree_entry_get_next(struct got_tree_object *tree,
1176 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
1178 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx + 1);
1181 56e0773d 2019-11-28 stsp struct got_tree_entry *
1182 56e0773d 2019-11-28 stsp got_tree_entry_get_prev(struct got_tree_object *tree,
1183 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
1185 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx - 1);
1188 3840f4c9 2018-09-12 stsp static const struct got_error *
1189 ac544f8c 2019-01-13 stsp request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1190 ebc55e2d 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1191 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
1193 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
1194 3840f4c9 2018-09-12 stsp int outfd_child;
1195 3840f4c9 2018-09-12 stsp int basefd, accumfd; /* temporary files for delta application */
1197 3840f4c9 2018-09-12 stsp basefd = got_opentempfd();
1198 3840f4c9 2018-09-12 stsp if (basefd == -1)
1199 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
1200 3840f4c9 2018-09-12 stsp accumfd = got_opentempfd();
1201 3840f4c9 2018-09-12 stsp if (accumfd == -1)
1202 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
1204 3840f4c9 2018-09-12 stsp outfd_child = dup(outfd);
1205 3840f4c9 2018-09-12 stsp if (outfd_child == -1)
1206 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1208 ebc55e2d 2018-12-24 stsp err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1210 3840f4c9 2018-09-12 stsp return err;
1212 3840f4c9 2018-09-12 stsp err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1213 3840f4c9 2018-09-12 stsp outfd_child);
1214 3840f4c9 2018-09-12 stsp if (err) {
1215 41496140 2019-02-21 stsp close(basefd);
1216 41496140 2019-02-21 stsp close(accumfd);
1217 3840f4c9 2018-09-12 stsp return err;
1220 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1222 3840f4c9 2018-09-12 stsp if (err) {
1223 3840f4c9 2018-09-12 stsp close(accumfd);
1224 3840f4c9 2018-09-12 stsp return err;
1227 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1230 3840f4c9 2018-09-12 stsp return err;
1232 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen,
1233 ebc55e2d 2018-12-24 stsp pack->privsep_child->ibuf);
1235 3840f4c9 2018-09-12 stsp return err;
1237 3840f4c9 2018-09-12 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1238 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1240 3840f4c9 2018-09-12 stsp return err;
1243 ebc55e2d 2018-12-24 stsp static const struct got_error *
1244 ac544f8c 2019-01-13 stsp read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1245 ac544f8c 2019-01-13 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1246 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
1248 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
1250 ebc55e2d 2018-12-24 stsp if (pack->privsep_child == NULL) {
1251 ebc55e2d 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
1253 ebc55e2d 2018-12-24 stsp return err;
1256 ac544f8c 2019-01-13 stsp return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1260 9f2369b0 2018-12-24 stsp static const struct got_error *
1261 ac544f8c 2019-01-13 stsp request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1262 d5c81d44 2021-07-08 stsp int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1264 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1265 9f2369b0 2018-12-24 stsp int outfd_child;
1267 9f2369b0 2018-12-24 stsp outfd_child = dup(outfd);
1268 9f2369b0 2018-12-24 stsp if (outfd_child == -1)
1269 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1271 d5c81d44 2021-07-08 stsp err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1273 9f2369b0 2018-12-24 stsp return err;
1275 9f2369b0 2018-12-24 stsp err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1277 9f2369b0 2018-12-24 stsp return err;
1279 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1281 9f2369b0 2018-12-24 stsp return err;
1283 9f2369b0 2018-12-24 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1284 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
1286 9f2369b0 2018-12-24 stsp return err;
1289 9f2369b0 2018-12-24 stsp static const struct got_error *
1290 ac544f8c 2019-01-13 stsp read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1291 d5c81d44 2021-07-08 stsp int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1293 ddc7b220 2019-09-08 stsp const struct got_error *err;
1294 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1295 9f2369b0 2018-12-24 stsp pid_t pid;
1296 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1298 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1299 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1300 d5c81d44 2021-07-08 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1304 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1305 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1306 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1308 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1309 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1310 ddc7b220 2019-09-08 stsp free(ibuf);
1311 ddc7b220 2019-09-08 stsp return err;
1314 9f2369b0 2018-12-24 stsp pid = fork();
1315 ddc7b220 2019-09-08 stsp if (pid == -1) {
1316 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1317 ddc7b220 2019-09-08 stsp free(ibuf);
1318 ddc7b220 2019-09-08 stsp return err;
1320 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1321 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1322 9f2369b0 2018-12-24 stsp repo->path);
1323 9f2369b0 2018-12-24 stsp /* not reached */
1326 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
1327 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1328 ddc7b220 2019-09-08 stsp free(ibuf);
1329 ddc7b220 2019-09-08 stsp return err;
1331 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1332 9f2369b0 2018-12-24 stsp imsg_fds[0];
1333 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1334 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1335 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1337 d5c81d44 2021-07-08 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1340 ebc55e2d 2018-12-24 stsp static const struct got_error *
1341 ebc55e2d 2018-12-24 stsp open_blob(struct got_blob_object **blob, struct got_repository *repo,
1342 ebc55e2d 2018-12-24 stsp struct got_object_id *id, size_t blocksize)
1344 ebc55e2d 2018-12-24 stsp const struct got_error *err = NULL;
1345 ebc55e2d 2018-12-24 stsp struct got_packidx *packidx = NULL;
1347 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1348 ac544f8c 2019-01-13 stsp uint8_t *outbuf;
1349 e82b1d81 2019-07-27 stsp int outfd;
1350 ebc55e2d 2018-12-24 stsp size_t size, hdrlen;
1351 ebc55e2d 2018-12-24 stsp struct stat sb;
1353 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
1354 4558fcd4 2018-01-14 stsp if (*blob == NULL)
1355 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1357 55da3778 2018-09-10 stsp outfd = got_opentempfd();
1358 55da3778 2018-09-10 stsp if (outfd == -1)
1359 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
1361 062ebb78 2018-07-12 stsp (*blob)->read_buf = malloc(blocksize);
1362 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
1363 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1364 c7254d79 2018-04-24 stsp goto done;
1367 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1368 e82b1d81 2019-07-27 stsp if (err == NULL) {
1369 ebc55e2d 2018-12-24 stsp struct got_pack *pack = NULL;
1371 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
1372 aea75d87 2021-07-06 stsp packidx->path_packidx);
1374 ebc55e2d 2018-12-24 stsp goto done;
1376 ebc55e2d 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1377 55da3778 2018-09-10 stsp if (pack == NULL) {
1378 ebc55e2d 2018-12-24 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1381 55da3778 2018-09-10 stsp goto done;
1383 ac544f8c 2019-01-13 stsp err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1384 ac544f8c 2019-01-13 stsp pack, packidx, idx, id);
1385 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1388 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&infd, id, repo);
1390 e82b1d81 2019-07-27 stsp goto done;
1391 ac544f8c 2019-01-13 stsp err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1392 d5c81d44 2021-07-08 stsp id, repo);
1395 55da3778 2018-09-10 stsp goto done;
1397 de060dff 2018-12-24 stsp if (hdrlen > size) {
1398 ebc55e2d 2018-12-24 stsp err = got_error(GOT_ERR_BAD_OBJ_HDR);
1399 ebc55e2d 2018-12-24 stsp goto done;
1402 ac544f8c 2019-01-13 stsp if (outbuf) {
1403 08578a35 2021-01-22 stsp if (close(outfd) == -1 && err == NULL)
1404 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1405 ac544f8c 2019-01-13 stsp outfd = -1;
1406 ac544f8c 2019-01-13 stsp (*blob)->f = fmemopen(outbuf, size, "rb");
1407 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1408 638f9024 2019-05-13 stsp err = got_error_from_errno("fmemopen");
1409 ac544f8c 2019-01-13 stsp free(outbuf);
1410 ac544f8c 2019-01-13 stsp goto done;
1412 ac544f8c 2019-01-13 stsp (*blob)->data = outbuf;
1414 ac544f8c 2019-01-13 stsp if (fstat(outfd, &sb) == -1) {
1415 638f9024 2019-05-13 stsp err = got_error_from_errno("fstat");
1416 ac544f8c 2019-01-13 stsp goto done;
1419 ac544f8c 2019-01-13 stsp if (sb.st_size != size) {
1420 ac544f8c 2019-01-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1421 ac544f8c 2019-01-13 stsp goto done;
1424 ac544f8c 2019-01-13 stsp (*blob)->f = fdopen(outfd, "rb");
1425 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1426 638f9024 2019-05-13 stsp err = got_error_from_errno("fdopen");
1427 ac544f8c 2019-01-13 stsp close(outfd);
1428 ac544f8c 2019-01-13 stsp outfd = -1;
1429 ac544f8c 2019-01-13 stsp goto done;
1433 ebc55e2d 2018-12-24 stsp (*blob)->hdrlen = hdrlen;
1434 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
1435 ebc55e2d 2018-12-24 stsp memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1438 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1439 55da3778 2018-09-10 stsp if (err) {
1440 55da3778 2018-09-10 stsp if (*blob) {
1441 7baf5860 2019-03-19 stsp got_object_blob_close(*blob);
1442 55da3778 2018-09-10 stsp *blob = NULL;
1443 55da3778 2018-09-10 stsp } else if (outfd != -1)
1444 55da3778 2018-09-10 stsp close(outfd);
1446 a19581a2 2018-06-21 stsp return err;
1449 a19581a2 2018-06-21 stsp const struct got_error *
1450 a19581a2 2018-06-21 stsp got_object_open_as_blob(struct got_blob_object **blob,
1451 a19581a2 2018-06-21 stsp struct got_repository *repo, struct got_object_id *id,
1452 a19581a2 2018-06-21 stsp size_t blocksize)
1454 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, id, blocksize);
1457 ebc55e2d 2018-12-24 stsp const struct got_error *
1458 ebc55e2d 2018-12-24 stsp got_object_blob_open(struct got_blob_object **blob,
1459 ebc55e2d 2018-12-24 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
1461 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1464 fb43ecf1 2019-02-11 stsp const struct got_error *
1465 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
1467 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL;
1468 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
1469 56b63ca4 2021-01-22 stsp if (blob->f && fclose(blob->f) == EOF)
1470 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1471 ac544f8c 2019-01-13 stsp free(blob->data);
1472 68482ea3 2017-11-27 stsp free(blob);
1473 fb43ecf1 2019-02-11 stsp return err;
1477 8ba819a3 2020-07-23 stsp got_object_blob_rewind(struct got_blob_object *blob)
1479 8ba819a3 2020-07-23 stsp if (blob->f)
1480 8ba819a3 2020-07-23 stsp rewind(blob->f);
1484 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1486 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1490 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
1492 f934cf2c 2018-02-12 stsp return blob->hdrlen;
1495 f934cf2c 2018-02-12 stsp const uint8_t *
1496 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
1498 f934cf2c 2018-02-12 stsp return blob->read_buf;
1501 68482ea3 2017-11-27 stsp const struct got_error *
1502 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1506 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1507 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
1508 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
1509 eb651edf 2018-02-11 stsp *outlenp = n;
1510 35e9ba5d 2018-06-21 stsp return NULL;
1513 35e9ba5d 2018-06-21 stsp const struct got_error *
1514 be659d10 2020-11-18 stsp got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1515 6c4c42e0 2019-06-24 stsp off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1517 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
1518 b6752625 2018-12-24 stsp size_t n, len, hdrlen;
1519 84451b3e 2018-07-10 stsp const uint8_t *buf;
1521 c33ebc60 2020-11-18 stsp const int alloc_chunksz = 512;
1522 c33ebc60 2020-11-18 stsp size_t nalloc = 0;
1523 f595d9bd 2019-08-14 stsp off_t off = 0, total_len = 0;
1525 6c4c42e0 2019-06-24 stsp if (line_offsets)
1526 6c4c42e0 2019-06-24 stsp *line_offsets = NULL;
1527 f595d9bd 2019-08-14 stsp if (filesize)
1528 f595d9bd 2019-08-14 stsp *filesize = 0;
1529 84451b3e 2018-07-10 stsp if (nlines)
1530 84451b3e 2018-07-10 stsp *nlines = 0;
1532 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1534 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
1536 35e9ba5d 2018-06-21 stsp return err;
1537 35e9ba5d 2018-06-21 stsp if (len == 0)
1539 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
1540 b02560ec 2019-08-19 stsp i = hdrlen;
1541 f1cbc3bc 2020-11-18 stsp if (nlines) {
1542 f1cbc3bc 2020-11-18 stsp if (line_offsets && *line_offsets == NULL) {
1543 78695fb7 2019-08-12 stsp /* Have some data but perhaps no '\n'. */
1544 78695fb7 2019-08-12 stsp *nlines = 1;
1545 c33ebc60 2020-11-18 stsp nalloc = alloc_chunksz;
1546 c33ebc60 2020-11-18 stsp *line_offsets = calloc(nalloc,
1547 c33ebc60 2020-11-18 stsp sizeof(**line_offsets));
1548 78695fb7 2019-08-12 stsp if (*line_offsets == NULL)
1549 845785d4 2020-02-02 tracey return got_error_from_errno("calloc");
1551 b02560ec 2019-08-19 stsp /* Skip forward over end of first line. */
1552 b02560ec 2019-08-19 stsp while (i < len) {
1553 b02560ec 2019-08-19 stsp if (buf[i] == '\n')
1558 b02560ec 2019-08-19 stsp /* Scan '\n' offsets in remaining chunk of data. */
1559 b02560ec 2019-08-19 stsp while (i < len) {
1560 b02560ec 2019-08-19 stsp if (buf[i] != '\n') {
1564 f595d9bd 2019-08-14 stsp (*nlines)++;
1565 c33ebc60 2020-11-18 stsp if (line_offsets && nalloc < *nlines) {
1566 c33ebc60 2020-11-18 stsp size_t n = *nlines + alloc_chunksz;
1567 78695fb7 2019-08-12 stsp off_t *o = recallocarray(*line_offsets,
1568 c33ebc60 2020-11-18 stsp nalloc, n, sizeof(**line_offsets));
1569 78695fb7 2019-08-12 stsp if (o == NULL) {
1570 78695fb7 2019-08-12 stsp free(*line_offsets);
1571 78695fb7 2019-08-12 stsp *line_offsets = NULL;
1572 78695fb7 2019-08-12 stsp return got_error_from_errno(
1573 78695fb7 2019-08-12 stsp "recallocarray");
1575 78695fb7 2019-08-12 stsp *line_offsets = o;
1576 c33ebc60 2020-11-18 stsp nalloc = n;
1578 f1cbc3bc 2020-11-18 stsp if (line_offsets) {
1579 f1cbc3bc 2020-11-18 stsp off = total_len + i - hdrlen + 1;
1580 f1cbc3bc 2020-11-18 stsp (*line_offsets)[*nlines - 1] = off;
1585 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
1586 454a6b59 2018-12-24 stsp n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1587 b6752625 2018-12-24 stsp if (n != len - hdrlen)
1588 b6752625 2018-12-24 stsp return got_ferror(outfile, GOT_ERR_IO);
1589 f595d9bd 2019-08-14 stsp total_len += len - hdrlen;
1590 35e9ba5d 2018-06-21 stsp hdrlen = 0;
1591 35e9ba5d 2018-06-21 stsp } while (len != 0);
1593 cbe7f848 2019-02-11 stsp if (fflush(outfile) != 0)
1594 638f9024 2019-05-13 stsp return got_error_from_errno("fflush");
1595 35e9ba5d 2018-06-21 stsp rewind(outfile);
1597 f595d9bd 2019-08-14 stsp if (filesize)
1598 f595d9bd 2019-08-14 stsp *filesize = total_len;
1600 776d4d29 2018-06-17 stsp return NULL;
1603 f4a881ce 2018-11-17 stsp static const struct got_error *
1604 268f7291 2018-12-24 stsp request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1605 268f7291 2018-12-24 stsp int pack_idx, struct got_object_id *id)
1607 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
1609 268f7291 2018-12-24 stsp err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1610 268f7291 2018-12-24 stsp pack_idx);
1612 a158c901 2018-12-23 stsp return err;
1614 a158c901 2018-12-23 stsp return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1617 268f7291 2018-12-24 stsp static const struct got_error *
1618 268f7291 2018-12-24 stsp read_packed_tag_privsep(struct got_tag_object **tag,
1619 268f7291 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1620 268f7291 2018-12-24 stsp struct got_object_id *id)
1622 268f7291 2018-12-24 stsp const struct got_error *err = NULL;
1624 268f7291 2018-12-24 stsp if (pack->privsep_child)
1625 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1627 268f7291 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
1629 268f7291 2018-12-24 stsp return err;
1631 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1634 9f2369b0 2018-12-24 stsp static const struct got_error *
1635 9f2369b0 2018-12-24 stsp request_tag(struct got_tag_object **tag, struct got_repository *repo,
1636 d5c81d44 2021-07-08 stsp int fd, struct got_object_id *id)
1638 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1639 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1641 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1643 d5c81d44 2021-07-08 stsp err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1645 9f2369b0 2018-12-24 stsp return err;
1647 9f2369b0 2018-12-24 stsp return got_privsep_recv_tag(tag, ibuf);
1650 9f2369b0 2018-12-24 stsp static const struct got_error *
1651 9f2369b0 2018-12-24 stsp read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1652 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo)
1654 ddc7b220 2019-09-08 stsp const struct got_error *err;
1655 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1656 9f2369b0 2018-12-24 stsp pid_t pid;
1657 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1659 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1660 d5c81d44 2021-07-08 stsp return request_tag(tag, repo, obj_fd, id);
1662 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1663 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1664 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1666 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1667 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1668 ddc7b220 2019-09-08 stsp free(ibuf);
1669 ddc7b220 2019-09-08 stsp return err;
1672 9f2369b0 2018-12-24 stsp pid = fork();
1673 ddc7b220 2019-09-08 stsp if (pid == -1) {
1674 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1675 ddc7b220 2019-09-08 stsp free(ibuf);
1676 ddc7b220 2019-09-08 stsp return err;
1678 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1679 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1680 9f2369b0 2018-12-24 stsp repo->path);
1681 9f2369b0 2018-12-24 stsp /* not reached */
1684 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
1685 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1686 ddc7b220 2019-09-08 stsp free(ibuf);
1687 ddc7b220 2019-09-08 stsp return err;
1689 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1690 9f2369b0 2018-12-24 stsp imsg_fds[0];
1691 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1692 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1693 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1695 d5c81d44 2021-07-08 stsp return request_tag(tag, repo, obj_fd, id);
1698 a158c901 2018-12-23 stsp static const struct got_error *
1699 268f7291 2018-12-24 stsp open_tag(struct got_tag_object **tag, struct got_repository *repo,
1700 268f7291 2018-12-24 stsp struct got_object_id *id, int check_cache)
1702 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1703 268f7291 2018-12-24 stsp struct got_packidx *packidx = NULL;
1705 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1706 5d844a1e 2019-08-13 stsp struct got_object *obj = NULL;
1707 5d844a1e 2019-08-13 stsp int obj_type = GOT_OBJ_TYPE_ANY;
1709 f4a881ce 2018-11-17 stsp if (check_cache) {
1710 268f7291 2018-12-24 stsp *tag = got_repo_get_cached_tag(repo, id);
1711 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1712 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1713 f4a881ce 2018-11-17 stsp return NULL;
1716 f4a881ce 2018-11-17 stsp *tag = NULL;
1718 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1719 e82b1d81 2019-07-27 stsp if (err == NULL) {
1720 268f7291 2018-12-24 stsp struct got_pack *pack = NULL;
1722 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
1723 aea75d87 2021-07-06 stsp packidx->path_packidx);
1725 268f7291 2018-12-24 stsp return err;
1727 268f7291 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1728 f4a881ce 2018-11-17 stsp if (pack == NULL) {
1729 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1732 8d2c5ea3 2019-08-13 stsp goto done;
1735 992eb9d8 2020-02-07 tracey /* Beware of "lightweight" tags: Check object type first. */
1736 5d844a1e 2019-08-13 stsp err = read_packed_object_privsep(&obj, repo, pack, packidx,
1739 5d844a1e 2019-08-13 stsp goto done;
1740 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1741 5d844a1e 2019-08-13 stsp got_object_close(obj);
1742 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG) {
1743 5d844a1e 2019-08-13 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1744 5d844a1e 2019-08-13 stsp goto done;
1746 5d844a1e 2019-08-13 stsp err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1747 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1750 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1752 e82b1d81 2019-07-27 stsp return err;
1753 d5c81d44 2021-07-08 stsp err = got_object_read_header_privsep(&obj, id, repo, fd);
1755 5d844a1e 2019-08-13 stsp return err;
1756 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1757 5d844a1e 2019-08-13 stsp got_object_close(obj);
1758 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
1759 5d844a1e 2019-08-13 stsp return got_error(GOT_ERR_OBJ_TYPE);
1761 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1763 5d844a1e 2019-08-13 stsp return err;
1764 d5c81d44 2021-07-08 stsp err = read_tag_privsep(tag, fd, id, repo);
1767 f4a881ce 2018-11-17 stsp if (err == NULL) {
1768 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1769 268f7291 2018-12-24 stsp err = got_repo_cache_tag(repo, id, *tag);
1772 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1773 f4a881ce 2018-11-17 stsp return err;
1776 f4a881ce 2018-11-17 stsp const struct got_error *
1777 f4a881ce 2018-11-17 stsp got_object_open_as_tag(struct got_tag_object **tag,
1778 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object_id *id)
1780 f4a881ce 2018-11-17 stsp *tag = got_repo_get_cached_tag(repo, id);
1781 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1782 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1783 f4a881ce 2018-11-17 stsp return NULL;
1786 268f7291 2018-12-24 stsp return open_tag(tag, repo, id, 0);
1789 f4a881ce 2018-11-17 stsp const struct got_error *
1790 f4a881ce 2018-11-17 stsp got_object_tag_open(struct got_tag_object **tag,
1791 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object *obj)
1793 268f7291 2018-12-24 stsp return open_tag(tag, repo, got_object_get_id(obj), 1);
1796 d24820bf 2019-08-11 stsp const char *
1797 d24820bf 2019-08-11 stsp got_object_tag_get_name(struct got_tag_object *tag)
1799 d24820bf 2019-08-11 stsp return tag->tag;
1803 0bd18d37 2019-02-01 stsp got_object_tag_get_object_type(struct got_tag_object *tag)
1805 0bd18d37 2019-02-01 stsp return tag->obj_type;
1808 0bd18d37 2019-02-01 stsp struct got_object_id *
1809 0bd18d37 2019-02-01 stsp got_object_tag_get_object_id(struct got_tag_object *tag)
1811 0bd18d37 2019-02-01 stsp return &tag->id;
1815 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_time(struct got_tag_object *tag)
1817 01073a5d 2019-08-22 stsp return tag->tagger_time;
1821 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1823 01073a5d 2019-08-22 stsp return tag->tagger_gmtoff;
1826 01073a5d 2019-08-22 stsp const char *
1827 01073a5d 2019-08-22 stsp got_object_tag_get_tagger(struct got_tag_object *tag)
1829 01073a5d 2019-08-22 stsp return tag->tagger;
1832 01073a5d 2019-08-22 stsp const char *
1833 01073a5d 2019-08-22 stsp got_object_tag_get_message(struct got_tag_object *tag)
1835 01073a5d 2019-08-22 stsp return tag->tagmsg;
1838 776d4d29 2018-06-17 stsp static struct got_tree_entry *
1839 65a9bbe9 2018-09-15 stsp find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1843 63da309a 2018-11-07 stsp /* Note that tree entries are sorted in strncmp() order. */
1844 56e0773d 2019-11-28 stsp for (i = 0; i < tree->nentries; i++) {
1845 56e0773d 2019-11-28 stsp struct got_tree_entry *te = &tree->entries[i];
1846 63da309a 2018-11-07 stsp int cmp = strncmp(te->name, name, len);
1847 63da309a 2018-11-07 stsp if (cmp < 0)
1849 63da309a 2018-11-07 stsp if (cmp > 0)
1851 63da309a 2018-11-07 stsp if (te->name[len] == '\0')
1852 776d4d29 2018-06-17 stsp return te;
1854 eb651edf 2018-02-11 stsp return NULL;
1857 56e0773d 2019-11-28 stsp struct got_tree_entry *
1858 a129376b 2019-03-28 stsp got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1860 a129376b 2019-03-28 stsp return find_entry_by_name(tree, name, strlen(name));
1863 776d4d29 2018-06-17 stsp const struct got_error *
1864 67b631c9 2021-10-10 stsp got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1865 67b631c9 2021-10-10 stsp struct got_repository *repo, struct got_tree_object *tree,
1866 67b631c9 2021-10-10 stsp const char *path)
1868 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
1869 67b631c9 2021-10-10 stsp struct got_tree_object *subtree = NULL;
1870 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
1871 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1872 b7cd37e5 2018-11-18 stsp size_t seglen;
1874 27d434c2 2018-09-15 stsp *id = NULL;
1877 5e54fb30 2019-05-31 stsp while (s[0] == '/')
1880 65a9bbe9 2018-09-15 stsp seglen = 0;
1881 67b631c9 2021-10-10 stsp subtree = tree;
1882 b7cd37e5 2018-11-18 stsp while (*s) {
1883 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
1885 776d4d29 2018-06-17 stsp if (*s != '/') {
1892 67b631c9 2021-10-10 stsp te = find_entry_by_name(subtree, seg, seglen);
1893 db37e2c0 2018-06-21 stsp if (te == NULL) {
1894 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1895 776d4d29 2018-06-17 stsp goto done;
1898 b7cd37e5 2018-11-18 stsp if (*s == '\0')
1901 776d4d29 2018-06-17 stsp seg = s + 1;
1902 65a9bbe9 2018-09-15 stsp seglen = 0;
1905 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
1907 db37e2c0 2018-06-21 stsp te = NULL;
1909 776d4d29 2018-06-17 stsp goto done;
1910 67b631c9 2021-10-10 stsp if (subtree != tree)
1911 67b631c9 2021-10-10 stsp got_object_tree_close(subtree);
1912 67b631c9 2021-10-10 stsp subtree = next_tree;
1917 56e0773d 2019-11-28 stsp *id = got_object_id_dup(&te->id);
1918 27d434c2 2018-09-15 stsp if (*id == NULL)
1919 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
1921 67b631c9 2021-10-10 stsp *mode = te->mode;
1923 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1925 67b631c9 2021-10-10 stsp if (subtree && subtree != tree)
1926 67b631c9 2021-10-10 stsp got_object_tree_close(subtree);
1927 67b631c9 2021-10-10 stsp return err;
1929 67b631c9 2021-10-10 stsp const struct got_error *
1930 67b631c9 2021-10-10 stsp got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1931 67b631c9 2021-10-10 stsp struct got_object_id *commit_id, const char *path)
1933 67b631c9 2021-10-10 stsp const struct got_error *err = NULL;
1934 67b631c9 2021-10-10 stsp struct got_commit_object *commit = NULL;
1935 67b631c9 2021-10-10 stsp struct got_tree_object *tree = NULL;
1937 67b631c9 2021-10-10 stsp *id = NULL;
1939 67b631c9 2021-10-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
1941 67b631c9 2021-10-10 stsp goto done;
1943 67b631c9 2021-10-10 stsp /* Handle opening of root of commit's tree. */
1944 67b631c9 2021-10-10 stsp if (got_path_is_root_dir(path)) {
1945 67b631c9 2021-10-10 stsp *id = got_object_id_dup(commit->tree_id);
1946 67b631c9 2021-10-10 stsp if (*id == NULL)
1947 67b631c9 2021-10-10 stsp err = got_error_from_errno("got_object_id_dup");
1949 67b631c9 2021-10-10 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1951 67b631c9 2021-10-10 stsp goto done;
1952 67b631c9 2021-10-10 stsp err = got_object_tree_find_path(id, NULL, repo, tree, path);
1955 776d4d29 2018-06-17 stsp if (commit)
1956 776d4d29 2018-06-17 stsp got_object_commit_close(commit);
1958 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
1959 776d4d29 2018-06-17 stsp return err;
1963 ac5f2b26 2020-05-05 stsp * Normalize file mode bits to avoid false positive tree entry differences
1964 ac5f2b26 2020-05-05 stsp * in case tree entries have unexpected mode bits set.
1966 ac5f2b26 2020-05-05 stsp static mode_t
1967 ac5f2b26 2020-05-05 stsp normalize_mode_for_comparison(mode_t mode)
1970 ac5f2b26 2020-05-05 stsp * For directories, the only relevant bit is the IFDIR bit.
1971 ac5f2b26 2020-05-05 stsp * This allows us to detect paths changing from a directory
1972 ac5f2b26 2020-05-05 stsp * to a file and vice versa.
1974 ac5f2b26 2020-05-05 stsp if (S_ISDIR(mode))
1975 ac5f2b26 2020-05-05 stsp return mode & S_IFDIR;
1978 40dde666 2020-07-23 stsp * For symlinks, the only relevant bit is the IFLNK bit.
1979 40dde666 2020-07-23 stsp * This allows us to detect paths changing from a symlinks
1980 40dde666 2020-07-23 stsp * to a file or directory and vice versa.
1982 40dde666 2020-07-23 stsp if (S_ISLNK(mode))
1983 40dde666 2020-07-23 stsp return mode & S_IFLNK;
1985 ac5f2b26 2020-05-05 stsp /* For files, the only change we care about is the executable bit. */
1986 ac5f2b26 2020-05-05 stsp return mode & S_IXUSR;
1989 07862c20 2018-09-15 stsp const struct got_error *
1990 07862c20 2018-09-15 stsp got_object_tree_path_changed(int *changed,
1991 07862c20 2018-09-15 stsp struct got_tree_object *tree01, struct got_tree_object *tree02,
1992 07862c20 2018-09-15 stsp const char *path, struct got_repository *repo)
1994 07862c20 2018-09-15 stsp const struct got_error *err = NULL;
1995 07862c20 2018-09-15 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1996 07862c20 2018-09-15 stsp struct got_tree_entry *te1 = NULL, *te2 = NULL;
1997 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1998 3b7f9878 2018-11-18 stsp size_t seglen;
2000 07862c20 2018-09-15 stsp *changed = 0;
2002 07862c20 2018-09-15 stsp /* We not do support comparing the root path. */
2003 61a7d79f 2020-02-29 stsp if (got_path_is_root_dir(path))
2004 63f810e6 2020-02-29 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
2006 07862c20 2018-09-15 stsp tree1 = tree01;
2007 07862c20 2018-09-15 stsp tree2 = tree02;
2009 61a7d79f 2020-02-29 stsp while (*s == '/')
2012 65a9bbe9 2018-09-15 stsp seglen = 0;
2013 3b7f9878 2018-11-18 stsp while (*s) {
2014 07862c20 2018-09-15 stsp struct got_tree_object *next_tree1, *next_tree2;
2015 ac5f2b26 2020-05-05 stsp mode_t mode1, mode2;
2017 07862c20 2018-09-15 stsp if (*s != '/') {
2024 65a9bbe9 2018-09-15 stsp te1 = find_entry_by_name(tree1, seg, seglen);
2025 07862c20 2018-09-15 stsp if (te1 == NULL) {
2026 07862c20 2018-09-15 stsp err = got_error(GOT_ERR_NO_OBJ);
2027 07862c20 2018-09-15 stsp goto done;
2030 e8bfb8f3 2020-12-18 stsp if (tree2)
2031 e8bfb8f3 2020-12-18 stsp te2 = find_entry_by_name(tree2, seg, seglen);
2033 e8bfb8f3 2020-12-18 stsp if (te2) {
2034 e8bfb8f3 2020-12-18 stsp mode1 = normalize_mode_for_comparison(te1->mode);
2035 e8bfb8f3 2020-12-18 stsp mode2 = normalize_mode_for_comparison(te2->mode);
2036 e8bfb8f3 2020-12-18 stsp if (mode1 != mode2) {
2037 e8bfb8f3 2020-12-18 stsp *changed = 1;
2038 e8bfb8f3 2020-12-18 stsp goto done;
2041 e8bfb8f3 2020-12-18 stsp if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2042 e8bfb8f3 2020-12-18 stsp *changed = 0;
2043 e8bfb8f3 2020-12-18 stsp goto done;
2047 3b7f9878 2018-11-18 stsp if (*s == '\0') { /* final path element */
2048 07862c20 2018-09-15 stsp *changed = 1;
2049 07862c20 2018-09-15 stsp goto done;
2052 07862c20 2018-09-15 stsp seg = s + 1;
2054 65a9bbe9 2018-09-15 stsp seglen = 0;
2056 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree1, repo,
2057 56e0773d 2019-11-28 stsp &te1->id);
2058 07862c20 2018-09-15 stsp te1 = NULL;
2060 07862c20 2018-09-15 stsp goto done;
2061 a31cea73 2018-09-15 stsp if (tree1 != tree01)
2062 a31cea73 2018-09-15 stsp got_object_tree_close(tree1);
2063 07862c20 2018-09-15 stsp tree1 = next_tree1;
2065 e8bfb8f3 2020-12-18 stsp if (te2) {
2066 e8bfb8f3 2020-12-18 stsp err = got_object_open_as_tree(&next_tree2, repo,
2067 e8bfb8f3 2020-12-18 stsp &te2->id);
2068 e8bfb8f3 2020-12-18 stsp te2 = NULL;
2070 e8bfb8f3 2020-12-18 stsp goto done;
2071 e8bfb8f3 2020-12-18 stsp if (tree2 != tree02)
2072 e8bfb8f3 2020-12-18 stsp got_object_tree_close(tree2);
2073 e8bfb8f3 2020-12-18 stsp tree2 = next_tree2;
2074 e8bfb8f3 2020-12-18 stsp } else if (tree2) {
2075 e8bfb8f3 2020-12-18 stsp if (tree2 != tree02)
2076 e8bfb8f3 2020-12-18 stsp got_object_tree_close(tree2);
2077 e8bfb8f3 2020-12-18 stsp tree2 = NULL;
2082 a31cea73 2018-09-15 stsp if (tree1 && tree1 != tree01)
2083 07862c20 2018-09-15 stsp got_object_tree_close(tree1);
2084 a31cea73 2018-09-15 stsp if (tree2 && tree2 != tree02)
2085 07862c20 2018-09-15 stsp got_object_tree_close(tree2);
2086 77880158 2018-11-04 stsp return err;
2089 ed175427 2019-05-09 stsp const struct got_error *
2090 ed175427 2019-05-09 stsp got_object_tree_entry_dup(struct got_tree_entry **new_te,
2091 ed175427 2019-05-09 stsp struct got_tree_entry *te)
2093 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
2095 ed175427 2019-05-09 stsp *new_te = calloc(1, sizeof(**new_te));
2096 ed175427 2019-05-09 stsp if (*new_te == NULL)
2097 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
2099 ed175427 2019-05-09 stsp (*new_te)->mode = te->mode;
2100 56e0773d 2019-11-28 stsp memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2101 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2102 8c4eabf2 2019-05-10 stsp return err;
2106 56e0773d 2019-11-28 stsp got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2108 63c5ca5d 2019-08-24 stsp return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2112 e40622f4 2020-07-23 stsp got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2114 e40622f4 2020-07-23 stsp /* S_IFDIR check avoids confusing symlinks with submodules. */
2115 e40622f4 2020-07-23 stsp return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2118 e40622f4 2020-07-23 stsp static const struct got_error *
2119 e40622f4 2020-07-23 stsp resolve_symlink(char **link_target, const char *path,
2120 e40622f4 2020-07-23 stsp struct got_object_id *commit_id, struct got_repository *repo)
2122 e40622f4 2020-07-23 stsp const struct got_error *err = NULL;
2123 dbdd6209 2020-10-19 stsp char buf[PATH_MAX];
2124 e40622f4 2020-07-23 stsp char *name, *parent_path = NULL;
2125 e40622f4 2020-07-23 stsp struct got_object_id *tree_obj_id = NULL;
2126 e40622f4 2020-07-23 stsp struct got_tree_object *tree = NULL;
2127 e40622f4 2020-07-23 stsp struct got_tree_entry *te = NULL;
2129 e40622f4 2020-07-23 stsp *link_target = NULL;
2131 dbdd6209 2020-10-19 stsp if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2132 dbdd6209 2020-10-19 stsp return got_error(GOT_ERR_NO_SPACE);
2134 dbdd6209 2020-10-19 stsp name = basename(buf);
2135 e40622f4 2020-07-23 stsp if (name == NULL)
2136 e40622f4 2020-07-23 stsp return got_error_from_errno2("basename", path);
2138 e40622f4 2020-07-23 stsp err = got_path_dirname(&parent_path, path);
2140 e40622f4 2020-07-23 stsp return err;
2142 e40622f4 2020-07-23 stsp err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
2143 e40622f4 2020-07-23 stsp parent_path);
2144 e40622f4 2020-07-23 stsp if (err) {
2145 e40622f4 2020-07-23 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2146 e40622f4 2020-07-23 stsp /* Display the complete path in error message. */
2147 e40622f4 2020-07-23 stsp err = got_error_path(path, err->code);
2149 e40622f4 2020-07-23 stsp goto done;
2152 e40622f4 2020-07-23 stsp err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2154 e40622f4 2020-07-23 stsp goto done;
2156 e40622f4 2020-07-23 stsp te = got_object_tree_find_entry(tree, name);
2157 e40622f4 2020-07-23 stsp if (te == NULL) {
2158 e40622f4 2020-07-23 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2159 e40622f4 2020-07-23 stsp goto done;
2162 e40622f4 2020-07-23 stsp if (got_object_tree_entry_is_symlink(te)) {
2163 e40622f4 2020-07-23 stsp err = got_tree_entry_get_symlink_target(link_target, te, repo);
2165 e40622f4 2020-07-23 stsp goto done;
2166 e40622f4 2020-07-23 stsp if (!got_path_is_absolute(*link_target)) {
2167 e40622f4 2020-07-23 stsp char *abspath;
2168 e40622f4 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", parent_path,
2169 e40622f4 2020-07-23 stsp *link_target) == -1) {
2170 e40622f4 2020-07-23 stsp err = got_error_from_errno("asprintf");
2171 e40622f4 2020-07-23 stsp goto done;
2173 e40622f4 2020-07-23 stsp free(*link_target);
2174 e40622f4 2020-07-23 stsp *link_target = malloc(PATH_MAX);
2175 e40622f4 2020-07-23 stsp if (*link_target == NULL) {
2176 e40622f4 2020-07-23 stsp err = got_error_from_errno("malloc");
2177 e40622f4 2020-07-23 stsp goto done;
2179 e40622f4 2020-07-23 stsp err = got_canonpath(abspath, *link_target, PATH_MAX);
2180 e40622f4 2020-07-23 stsp free(abspath);
2182 e40622f4 2020-07-23 stsp goto done;
2186 e40622f4 2020-07-23 stsp free(tree_obj_id);
2188 e40622f4 2020-07-23 stsp got_object_tree_close(tree);
2189 e40622f4 2020-07-23 stsp if (err) {
2190 e40622f4 2020-07-23 stsp free(*link_target);
2191 e40622f4 2020-07-23 stsp *link_target = NULL;
2193 e40622f4 2020-07-23 stsp return err;
2196 ca6e02ac 2020-01-07 stsp const struct got_error *
2197 e40622f4 2020-07-23 stsp got_object_resolve_symlinks(char **link_target, const char *path,
2198 e40622f4 2020-07-23 stsp struct got_object_id *commit_id, struct got_repository *repo)
2200 e40622f4 2020-07-23 stsp const struct got_error *err = NULL;
2201 e40622f4 2020-07-23 stsp char *next_target = NULL;
2202 e40622f4 2020-07-23 stsp int max_recursion = 40; /* matches Git */
2204 e40622f4 2020-07-23 stsp *link_target = NULL;
2207 e40622f4 2020-07-23 stsp err = resolve_symlink(&next_target,
2208 e40622f4 2020-07-23 stsp *link_target ? *link_target : path, commit_id, repo);
2211 e40622f4 2020-07-23 stsp if (next_target) {
2212 e40622f4 2020-07-23 stsp free(*link_target);
2213 e40622f4 2020-07-23 stsp if (--max_recursion == 0) {
2214 e40622f4 2020-07-23 stsp err = got_error_path(path, GOT_ERR_RECURSION);
2215 e40622f4 2020-07-23 stsp *link_target = NULL;
2218 e40622f4 2020-07-23 stsp *link_target = next_target;
2220 e40622f4 2020-07-23 stsp } while (next_target);
2222 e40622f4 2020-07-23 stsp return err;
2225 e40622f4 2020-07-23 stsp const struct got_error *
2226 ca6e02ac 2020-01-07 stsp got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2227 ca6e02ac 2020-01-07 stsp struct got_object_id *commit_id, const char *path,
2228 ca6e02ac 2020-01-07 stsp struct got_repository *repo)
2230 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
2231 ca6e02ac 2020-01-07 stsp struct got_pack *pack = NULL;
2232 ca6e02ac 2020-01-07 stsp struct got_packidx *packidx = NULL;
2233 ca6e02ac 2020-01-07 stsp char *path_packfile = NULL;
2234 ca6e02ac 2020-01-07 stsp struct got_commit_object *changed_commit = NULL;
2235 ca6e02ac 2020-01-07 stsp struct got_object_id *changed_commit_id = NULL;
2238 ca6e02ac 2020-01-07 stsp err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2239 ca6e02ac 2020-01-07 stsp if (err) {
2240 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
2241 ca6e02ac 2020-01-07 stsp return err;
2242 ca6e02ac 2020-01-07 stsp return NULL;
2245 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
2246 aea75d87 2021-07-06 stsp packidx->path_packidx);
2248 ca6e02ac 2020-01-07 stsp return err;
2250 ca6e02ac 2020-01-07 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
2251 ca6e02ac 2020-01-07 stsp if (pack == NULL) {
2252 ca6e02ac 2020-01-07 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2254 ca6e02ac 2020-01-07 stsp goto done;
2257 ca6e02ac 2020-01-07 stsp if (pack->privsep_child == NULL) {
2258 ca6e02ac 2020-01-07 stsp err = start_pack_privsep_child(pack, packidx);
2260 ca6e02ac 2020-01-07 stsp goto done;
2263 ca6e02ac 2020-01-07 stsp err = got_privsep_send_commit_traversal_request(
2264 ca6e02ac 2020-01-07 stsp pack->privsep_child->ibuf, commit_id, idx, path);
2266 ca6e02ac 2020-01-07 stsp goto done;
2268 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_traversed_commits(&changed_commit,
2269 ca6e02ac 2020-01-07 stsp &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2271 ca6e02ac 2020-01-07 stsp goto done;
2273 ca6e02ac 2020-01-07 stsp if (changed_commit) {
2275 ca6e02ac 2020-01-07 stsp * Cache the commit in which the path was changed.
2276 ca6e02ac 2020-01-07 stsp * This commit might be opened again soon.
2278 ca6e02ac 2020-01-07 stsp changed_commit->refcnt++;
2279 ca6e02ac 2020-01-07 stsp err = got_repo_cache_commit(repo, changed_commit_id,
2280 ca6e02ac 2020-01-07 stsp changed_commit);
2281 ca6e02ac 2020-01-07 stsp got_object_commit_close(changed_commit);
2284 ca6e02ac 2020-01-07 stsp free(path_packfile);
2285 ca6e02ac 2020-01-07 stsp free(changed_commit_id);
2286 ca6e02ac 2020-01-07 stsp return err;