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 db696021 2022-01-04 stsp /* Create temporary files used during delta application. */
170 59d1e4a0 2021-03-10 stsp static const struct got_error *
171 db696021 2022-01-04 stsp pack_child_send_tempfiles(struct imsgbuf *ibuf, struct got_pack *pack)
173 db696021 2022-01-04 stsp const struct got_error *err;
174 db696021 2022-01-04 stsp int basefd, accumfd;
177 db696021 2022-01-04 stsp * For performance reasons, the child will keep reusing the
178 db696021 2022-01-04 stsp * same temporary files during every object request.
179 db696021 2022-01-04 stsp * Opening and closing new files for every object request is
180 db696021 2022-01-04 stsp * too expensive during operations such as 'gotadmin pack'.
182 db696021 2022-01-04 stsp if (pack->child_has_tempfiles)
183 db696021 2022-01-04 stsp return NULL;
185 59d1e4a0 2021-03-10 stsp basefd = got_opentempfd();
186 59d1e4a0 2021-03-10 stsp if (basefd == -1)
187 59d1e4a0 2021-03-10 stsp return got_error_from_errno("got_opentempfd");
189 db696021 2022-01-04 stsp err = got_privsep_send_tmpfd(ibuf, basefd);
191 db696021 2022-01-04 stsp return err;
193 59d1e4a0 2021-03-10 stsp accumfd = got_opentempfd();
194 db696021 2022-01-04 stsp if (accumfd == -1)
195 59d1e4a0 2021-03-10 stsp return got_error_from_errno("got_opentempfd");
197 db696021 2022-01-04 stsp err = got_privsep_send_tmpfd(ibuf, accumfd);
199 59d1e4a0 2021-03-10 stsp return err;
201 db696021 2022-01-04 stsp pack->child_has_tempfiles = 1;
202 db696021 2022-01-04 stsp return NULL;
205 db696021 2022-01-04 stsp static const struct got_error *
206 db696021 2022-01-04 stsp request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
207 db696021 2022-01-04 stsp int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
209 db696021 2022-01-04 stsp const struct got_error *err = NULL;
210 db696021 2022-01-04 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
211 db696021 2022-01-04 stsp int outfd_child;
213 db696021 2022-01-04 stsp err = pack_child_send_tempfiles(ibuf, pack);
215 db696021 2022-01-04 stsp return err;
217 db696021 2022-01-04 stsp outfd_child = dup(outfd);
218 db696021 2022-01-04 stsp if (outfd_child == -1)
219 db696021 2022-01-04 stsp return got_error_from_errno("dup");
221 59d1e4a0 2021-03-10 stsp err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
223 59d1e4a0 2021-03-10 stsp close(outfd_child);
224 59d1e4a0 2021-03-10 stsp return err;
227 59d1e4a0 2021-03-10 stsp err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
229 59d1e4a0 2021-03-10 stsp return err;
231 59d1e4a0 2021-03-10 stsp err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
233 59d1e4a0 2021-03-10 stsp return err;
235 59d1e4a0 2021-03-10 stsp return NULL;
238 da506691 2019-05-22 stsp static void
239 b48e2ddb 2019-05-22 stsp set_max_datasize(void)
241 b48e2ddb 2019-05-22 stsp struct rlimit rl;
243 b48e2ddb 2019-05-22 stsp if (getrlimit(RLIMIT_DATA, &rl) != 0)
246 b48e2ddb 2019-05-22 stsp rl.rlim_cur = rl.rlim_max;
247 b48e2ddb 2019-05-22 stsp setrlimit(RLIMIT_DATA, &rl);
250 a158c901 2018-12-23 stsp static const struct got_error *
251 711fb6e8 2018-12-23 stsp start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
253 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
254 a158c901 2018-12-23 stsp int imsg_fds[2];
256 a158c901 2018-12-23 stsp struct imsgbuf *ibuf;
258 a158c901 2018-12-23 stsp ibuf = calloc(1, sizeof(*ibuf));
259 a158c901 2018-12-23 stsp if (ibuf == NULL)
260 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
262 a158c901 2018-12-23 stsp pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
263 a158c901 2018-12-23 stsp if (pack->privsep_child == NULL) {
264 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
265 a158c901 2018-12-23 stsp free(ibuf);
266 a158c901 2018-12-23 stsp return err;
268 db696021 2022-01-04 stsp pack->child_has_tempfiles = 0;
270 a158c901 2018-12-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
271 638f9024 2019-05-13 stsp err = got_error_from_errno("socketpair");
275 a158c901 2018-12-23 stsp pid = fork();
276 a158c901 2018-12-23 stsp if (pid == -1) {
277 638f9024 2019-05-13 stsp err = got_error_from_errno("fork");
279 a158c901 2018-12-23 stsp } else if (pid == 0) {
280 b48e2ddb 2019-05-22 stsp set_max_datasize();
281 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
282 a158c901 2018-12-23 stsp pack->path_packfile);
283 a158c901 2018-12-23 stsp /* not reached */
286 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1)
287 638f9024 2019-05-13 stsp return got_error_from_errno("close");
288 a158c901 2018-12-23 stsp pack->privsep_child->imsg_fd = imsg_fds[0];
289 a158c901 2018-12-23 stsp pack->privsep_child->pid = pid;
290 a158c901 2018-12-23 stsp imsg_init(ibuf, imsg_fds[0]);
291 a158c901 2018-12-23 stsp pack->privsep_child->ibuf = ibuf;
293 a158c901 2018-12-23 stsp err = got_privsep_init_pack_child(ibuf, pack, packidx);
295 a158c901 2018-12-23 stsp const struct got_error *child_err;
296 a158c901 2018-12-23 stsp err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
297 a158c901 2018-12-23 stsp child_err = got_privsep_wait_for_child(
298 a158c901 2018-12-23 stsp pack->privsep_child->pid);
299 a158c901 2018-12-23 stsp if (child_err && err == NULL)
300 a158c901 2018-12-23 stsp err = child_err;
304 a158c901 2018-12-23 stsp free(ibuf);
305 a158c901 2018-12-23 stsp free(pack->privsep_child);
306 a158c901 2018-12-23 stsp pack->privsep_child = NULL;
308 a158c901 2018-12-23 stsp return err;
311 711fb6e8 2018-12-23 stsp static const struct got_error *
312 711fb6e8 2018-12-23 stsp read_packed_object_privsep(struct got_object **obj,
313 711fb6e8 2018-12-23 stsp struct got_repository *repo, struct got_pack *pack,
314 711fb6e8 2018-12-23 stsp struct got_packidx *packidx, int idx, struct got_object_id *id)
316 711fb6e8 2018-12-23 stsp const struct got_error *err = NULL;
318 59d1e4a0 2021-03-10 stsp if (pack->privsep_child == NULL) {
319 59d1e4a0 2021-03-10 stsp err = start_pack_privsep_child(pack, packidx);
321 59d1e4a0 2021-03-10 stsp return err;
324 711fb6e8 2018-12-23 stsp return request_packed_object(obj, pack, idx, id);
327 59d1e4a0 2021-03-10 stsp static const struct got_error *
328 59d1e4a0 2021-03-10 stsp read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
329 59d1e4a0 2021-03-10 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
330 59d1e4a0 2021-03-10 stsp struct got_object_id *id)
332 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
334 59d1e4a0 2021-03-10 stsp if (pack->privsep_child == NULL) {
335 59d1e4a0 2021-03-10 stsp err = start_pack_privsep_child(pack, packidx);
337 59d1e4a0 2021-03-10 stsp return err;
340 59d1e4a0 2021-03-10 stsp return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
344 b3d68e7f 2021-07-03 stsp const struct got_error *
345 b3d68e7f 2021-07-03 stsp got_object_open_packed(struct got_object **obj, struct got_object_id *id,
346 2090a03d 2018-09-09 stsp struct got_repository *repo)
348 2090a03d 2018-09-09 stsp const struct got_error *err = NULL;
349 2090a03d 2018-09-09 stsp struct got_pack *pack = NULL;
350 2090a03d 2018-09-09 stsp struct got_packidx *packidx = NULL;
352 2090a03d 2018-09-09 stsp char *path_packfile;
354 2090a03d 2018-09-09 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
356 2090a03d 2018-09-09 stsp return err;
358 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
359 aea75d87 2021-07-06 stsp packidx->path_packidx);
361 2090a03d 2018-09-09 stsp return err;
363 2090a03d 2018-09-09 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
364 2090a03d 2018-09-09 stsp if (pack == NULL) {
365 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
370 a158c901 2018-12-23 stsp err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
374 2090a03d 2018-09-09 stsp free(path_packfile);
375 4558fcd4 2018-01-14 stsp return err;
378 9f2369b0 2018-12-24 stsp static const struct got_error *
379 d5c81d44 2021-07-08 stsp request_object(struct got_object **obj, struct got_object_id *id,
380 d5c81d44 2021-07-08 stsp struct got_repository *repo, int fd)
382 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
383 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
385 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
387 d5c81d44 2021-07-08 stsp err = got_privsep_send_obj_req(ibuf, fd, id);
389 9f2369b0 2018-12-24 stsp return err;
391 9f2369b0 2018-12-24 stsp return got_privsep_recv_obj(obj, ibuf);
394 9f2369b0 2018-12-24 stsp static const struct got_error *
395 59d1e4a0 2021-03-10 stsp request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
396 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo, int infd)
398 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
399 59d1e4a0 2021-03-10 stsp struct imsgbuf *ibuf;
400 59d1e4a0 2021-03-10 stsp int outfd_child;
402 59d1e4a0 2021-03-10 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
404 59d1e4a0 2021-03-10 stsp outfd_child = dup(outfd);
405 59d1e4a0 2021-03-10 stsp if (outfd_child == -1)
406 59d1e4a0 2021-03-10 stsp return got_error_from_errno("dup");
408 d5c81d44 2021-07-08 stsp err = got_privsep_send_raw_obj_req(ibuf, infd, id);
410 59d1e4a0 2021-03-10 stsp return err;
412 59d1e4a0 2021-03-10 stsp err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
414 59d1e4a0 2021-03-10 stsp return err;
416 59d1e4a0 2021-03-10 stsp return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
419 59d1e4a0 2021-03-10 stsp static const struct got_error *
420 59d1e4a0 2021-03-10 stsp start_read_object_child(struct got_repository *repo)
422 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
423 9f2369b0 2018-12-24 stsp int imsg_fds[2];
425 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
427 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
428 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
429 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
431 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
432 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
433 ddc7b220 2019-09-08 stsp free(ibuf);
434 ddc7b220 2019-09-08 stsp return err;
437 9f2369b0 2018-12-24 stsp pid = fork();
438 ddc7b220 2019-09-08 stsp if (pid == -1) {
439 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
440 ddc7b220 2019-09-08 stsp free(ibuf);
441 ddc7b220 2019-09-08 stsp return err;
443 9f2369b0 2018-12-24 stsp else if (pid == 0) {
444 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
445 9f2369b0 2018-12-24 stsp repo->path);
446 9f2369b0 2018-12-24 stsp /* not reached */
449 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
450 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
451 ddc7b220 2019-09-08 stsp free(ibuf);
452 ddc7b220 2019-09-08 stsp return err;
455 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
456 9f2369b0 2018-12-24 stsp imsg_fds[0];
457 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
458 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
459 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
461 59d1e4a0 2021-03-10 stsp return NULL;
464 b3d68e7f 2021-07-03 stsp const struct got_error *
465 b3d68e7f 2021-07-03 stsp got_object_read_header_privsep(struct got_object **obj,
466 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo, int obj_fd)
468 59d1e4a0 2021-03-10 stsp const struct got_error *err;
470 59d1e4a0 2021-03-10 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
471 d5c81d44 2021-07-08 stsp return request_object(obj, id, repo, obj_fd);
473 59d1e4a0 2021-03-10 stsp err = start_read_object_child(repo);
475 7495ec13 2021-04-04 stsp close(obj_fd);
476 59d1e4a0 2021-03-10 stsp return err;
479 d5c81d44 2021-07-08 stsp return request_object(obj, id, repo, obj_fd);
482 59d1e4a0 2021-03-10 stsp static const struct got_error *
483 59d1e4a0 2021-03-10 stsp read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
484 d5c81d44 2021-07-08 stsp int outfd, struct got_object_id *id, struct got_repository *repo,
485 d5c81d44 2021-07-08 stsp int obj_fd)
487 59d1e4a0 2021-03-10 stsp const struct got_error *err;
489 59d1e4a0 2021-03-10 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
490 d5c81d44 2021-07-08 stsp return request_raw_object(outbuf, size, hdrlen, outfd, id,
491 d5c81d44 2021-07-08 stsp repo, obj_fd);
493 59d1e4a0 2021-03-10 stsp err = start_read_object_child(repo);
495 59d1e4a0 2021-03-10 stsp return err;
497 d5c81d44 2021-07-08 stsp return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
501 4558fcd4 2018-01-14 stsp const struct got_error *
502 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
503 4558fcd4 2018-01-14 stsp struct got_object_id *id)
505 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
508 7bb0daa1 2018-06-21 stsp *obj = got_repo_get_cached_object(repo, id);
509 7bb0daa1 2018-06-21 stsp if (*obj != NULL) {
510 7bb0daa1 2018-06-21 stsp (*obj)->refcnt++;
511 7bb0daa1 2018-06-21 stsp return NULL;
514 b3d68e7f 2021-07-03 stsp err = got_object_open_packed(obj, id, repo);
515 e82b1d81 2019-07-27 stsp if (err && err->code != GOT_ERR_NO_OBJ)
516 e82b1d81 2019-07-27 stsp return err;
517 e82b1d81 2019-07-27 stsp if (*obj) {
518 e82b1d81 2019-07-27 stsp (*obj)->refcnt++;
519 e82b1d81 2019-07-27 stsp return got_repo_cache_object(repo, id, *obj);
522 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
524 762d73f4 2021-04-10 stsp if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
525 762d73f4 2021-04-10 stsp err = got_error_no_obj(id);
526 762d73f4 2021-04-10 stsp return err;
529 d5c81d44 2021-07-08 stsp err = got_object_read_header_privsep(obj, id, repo, fd);
531 4558fcd4 2018-01-14 stsp return err;
533 762d73f4 2021-04-10 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
535 59790a32 2018-09-15 stsp (*obj)->refcnt++;
536 762d73f4 2021-04-10 stsp return got_repo_cache_object(repo, id, *obj);
539 d3c116bf 2021-10-15 stsp /* *outfd must be initialized to -1 by caller */
540 59d1e4a0 2021-03-10 stsp const struct got_error *
541 d3c116bf 2021-10-15 stsp got_object_raw_open(struct got_raw_object **obj, int *outfd,
542 94dac27c 2021-10-15 stsp struct got_repository *repo, struct got_object_id *id)
544 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
545 59d1e4a0 2021-03-10 stsp struct got_packidx *packidx = NULL;
547 59d1e4a0 2021-03-10 stsp uint8_t *outbuf = NULL;
548 59d1e4a0 2021-03-10 stsp off_t size = 0;
549 59d1e4a0 2021-03-10 stsp size_t hdrlen = 0;
550 59d1e4a0 2021-03-10 stsp char *path_packfile = NULL;
552 d3c116bf 2021-10-15 stsp *obj = got_repo_get_cached_raw_object(repo, id);
553 d3c116bf 2021-10-15 stsp if (*obj != NULL) {
554 d3c116bf 2021-10-15 stsp (*obj)->refcnt++;
555 d3c116bf 2021-10-15 stsp return NULL;
558 d3c116bf 2021-10-15 stsp if (*outfd == -1) {
559 d3c116bf 2021-10-15 stsp *outfd = got_opentempfd();
560 d3c116bf 2021-10-15 stsp if (*outfd == -1)
561 d3c116bf 2021-10-15 stsp return got_error_from_errno("got_opentempfd");
564 59d1e4a0 2021-03-10 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
565 59d1e4a0 2021-03-10 stsp if (err == NULL) {
566 59d1e4a0 2021-03-10 stsp struct got_pack *pack = NULL;
568 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
569 aea75d87 2021-07-06 stsp packidx->path_packidx);
573 59d1e4a0 2021-03-10 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
574 59d1e4a0 2021-03-10 stsp if (pack == NULL) {
575 59d1e4a0 2021-03-10 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
580 59d1e4a0 2021-03-10 stsp err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
581 d3c116bf 2021-10-15 stsp *outfd, pack, packidx, idx, id);
584 59d1e4a0 2021-03-10 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
587 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
590 d3c116bf 2021-10-15 stsp err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
591 d5c81d44 2021-07-08 stsp id, repo, fd);
596 59d1e4a0 2021-03-10 stsp *obj = calloc(1, sizeof(**obj));
597 59d1e4a0 2021-03-10 stsp if (*obj == NULL) {
598 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("calloc");
602 59d1e4a0 2021-03-10 stsp if (outbuf) {
603 59d1e4a0 2021-03-10 stsp (*obj)->f = fmemopen(outbuf, hdrlen + size, "r");
604 59d1e4a0 2021-03-10 stsp if ((*obj)->f == NULL) {
605 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fdopen");
608 59d1e4a0 2021-03-10 stsp (*obj)->data = outbuf;
610 59d1e4a0 2021-03-10 stsp struct stat sb;
611 d3c116bf 2021-10-15 stsp if (fstat(*outfd, &sb) == -1) {
612 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fstat");
616 a8591711 2021-06-18 stsp if (sb.st_size != hdrlen + size) {
617 59d1e4a0 2021-03-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
621 d3c116bf 2021-10-15 stsp (*obj)->f = fdopen(*outfd, "r");
622 59d1e4a0 2021-03-10 stsp if ((*obj)->f == NULL) {
623 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fdopen");
626 59d1e4a0 2021-03-10 stsp (*obj)->data = NULL;
627 d3c116bf 2021-10-15 stsp *outfd = -1;
629 59d1e4a0 2021-03-10 stsp (*obj)->hdrlen = hdrlen;
630 59d1e4a0 2021-03-10 stsp (*obj)->size = size;
631 d3c116bf 2021-10-15 stsp err = got_repo_cache_raw_object(repo, id, *obj);
633 59d1e4a0 2021-03-10 stsp free(path_packfile);
635 59d1e4a0 2021-03-10 stsp if (*obj) {
636 59d1e4a0 2021-03-10 stsp got_object_raw_close(*obj);
637 59d1e4a0 2021-03-10 stsp *obj = NULL;
639 59d1e4a0 2021-03-10 stsp free(outbuf);
641 d3c116bf 2021-10-15 stsp (*obj)->refcnt++;
642 59d1e4a0 2021-03-10 stsp return err;
645 59d1e4a0 2021-03-10 stsp const struct got_error *
646 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
647 6dfa2fd3 2018-02-12 stsp const char *id_str)
649 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
651 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
652 6dd1ece6 2019-11-10 stsp return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
654 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
657 15a94983 2018-12-23 stsp const struct got_error *
658 15a94983 2018-12-23 stsp got_object_resolve_id_str(struct got_object_id **id,
659 15a94983 2018-12-23 stsp struct got_repository *repo, const char *id_str)
661 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
662 15a94983 2018-12-23 stsp struct got_object *obj;
664 15a94983 2018-12-23 stsp err = got_object_open_by_id_str(&obj, repo, id_str);
666 15a94983 2018-12-23 stsp return err;
668 15a94983 2018-12-23 stsp *id = got_object_id_dup(got_object_get_id(obj));
669 15a94983 2018-12-23 stsp got_object_close(obj);
670 15a94983 2018-12-23 stsp if (*id == NULL)
671 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
673 15a94983 2018-12-23 stsp return NULL;
676 434025f3 2018-09-16 stsp static const struct got_error *
677 a158c901 2018-12-23 stsp request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
678 a158c901 2018-12-23 stsp int pack_idx, struct got_object_id *id)
680 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
682 a158c901 2018-12-23 stsp err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
685 a158c901 2018-12-23 stsp return err;
687 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
689 ca6e02ac 2020-01-07 stsp return err;
691 ca6e02ac 2020-01-07 stsp (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
692 ca6e02ac 2020-01-07 stsp return NULL;
695 a158c901 2018-12-23 stsp static const struct got_error *
696 a158c901 2018-12-23 stsp read_packed_commit_privsep(struct got_commit_object **commit,
697 a158c901 2018-12-23 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
698 a158c901 2018-12-23 stsp struct got_object_id *id)
700 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
702 a158c901 2018-12-23 stsp if (pack->privsep_child)
703 a158c901 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
705 711fb6e8 2018-12-23 stsp err = start_pack_privsep_child(pack, packidx);
707 a158c901 2018-12-23 stsp return err;
709 711fb6e8 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
712 9f2369b0 2018-12-24 stsp static const struct got_error *
713 9f2369b0 2018-12-24 stsp request_commit(struct got_commit_object **commit, struct got_repository *repo,
714 d5c81d44 2021-07-08 stsp int fd, struct got_object_id *id)
716 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
717 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
719 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
721 d5c81d44 2021-07-08 stsp err = got_privsep_send_commit_req(ibuf, fd, id, -1);
723 9f2369b0 2018-12-24 stsp return err;
725 9f2369b0 2018-12-24 stsp return got_privsep_recv_commit(commit, ibuf);
728 9f2369b0 2018-12-24 stsp static const struct got_error *
729 9f2369b0 2018-12-24 stsp read_commit_privsep(struct got_commit_object **commit, int obj_fd,
730 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo)
732 ddc7b220 2019-09-08 stsp const struct got_error *err;
733 9f2369b0 2018-12-24 stsp int imsg_fds[2];
735 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
737 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
738 d5c81d44 2021-07-08 stsp return request_commit(commit, repo, obj_fd, id);
740 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
741 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
742 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
744 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
745 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
746 ddc7b220 2019-09-08 stsp free(ibuf);
747 ddc7b220 2019-09-08 stsp return err;
750 9f2369b0 2018-12-24 stsp pid = fork();
751 ddc7b220 2019-09-08 stsp if (pid == -1) {
752 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
753 ddc7b220 2019-09-08 stsp free(ibuf);
754 ddc7b220 2019-09-08 stsp return err;
756 9f2369b0 2018-12-24 stsp else if (pid == 0) {
757 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
758 9f2369b0 2018-12-24 stsp repo->path);
759 9f2369b0 2018-12-24 stsp /* not reached */
762 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
763 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
764 ddc7b220 2019-09-08 stsp free(ibuf);
765 ddc7b220 2019-09-08 stsp return err;
767 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
768 9f2369b0 2018-12-24 stsp imsg_fds[0];
769 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
770 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
771 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
773 d5c81d44 2021-07-08 stsp return request_commit(commit, repo, obj_fd, id);
777 a158c901 2018-12-23 stsp static const struct got_error *
778 434025f3 2018-09-16 stsp open_commit(struct got_commit_object **commit,
779 1785f84a 2018-12-23 stsp struct got_repository *repo, struct got_object_id *id, int check_cache)
781 434025f3 2018-09-16 stsp const struct got_error *err = NULL;
782 1785f84a 2018-12-23 stsp struct got_packidx *packidx = NULL;
784 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
786 434025f3 2018-09-16 stsp if (check_cache) {
787 1785f84a 2018-12-23 stsp *commit = got_repo_get_cached_commit(repo, id);
788 434025f3 2018-09-16 stsp if (*commit != NULL) {
789 434025f3 2018-09-16 stsp (*commit)->refcnt++;
790 434025f3 2018-09-16 stsp return NULL;
793 434025f3 2018-09-16 stsp *commit = NULL;
795 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
796 e82b1d81 2019-07-27 stsp if (err == NULL) {
797 1785f84a 2018-12-23 stsp struct got_pack *pack = NULL;
799 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
800 aea75d87 2021-07-06 stsp packidx->path_packidx);
802 1785f84a 2018-12-23 stsp return err;
804 1785f84a 2018-12-23 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
805 434025f3 2018-09-16 stsp if (pack == NULL) {
806 1785f84a 2018-12-23 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
811 a158c901 2018-12-23 stsp err = read_packed_commit_privsep(commit, pack,
812 1785f84a 2018-12-23 stsp packidx, idx, id);
813 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
816 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
818 e82b1d81 2019-07-27 stsp return err;
819 d5c81d44 2021-07-08 stsp err = read_commit_privsep(commit, fd, id, repo);
822 434025f3 2018-09-16 stsp if (err == NULL) {
823 434025f3 2018-09-16 stsp (*commit)->refcnt++;
824 1785f84a 2018-12-23 stsp err = got_repo_cache_commit(repo, id, *commit);
827 8d2c5ea3 2019-08-13 stsp free(path_packfile);
828 e32baab7 2018-11-05 stsp return err;
831 e32baab7 2018-11-05 stsp const struct got_error *
832 e32baab7 2018-11-05 stsp got_object_open_as_commit(struct got_commit_object **commit,
833 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object_id *id)
835 e32baab7 2018-11-05 stsp *commit = got_repo_get_cached_commit(repo, id);
836 e32baab7 2018-11-05 stsp if (*commit != NULL) {
837 e32baab7 2018-11-05 stsp (*commit)->refcnt++;
838 e32baab7 2018-11-05 stsp return NULL;
841 1785f84a 2018-12-23 stsp return open_commit(commit, repo, id, 0);
844 e32baab7 2018-11-05 stsp const struct got_error *
845 e32baab7 2018-11-05 stsp got_object_commit_open(struct got_commit_object **commit,
846 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object *obj)
848 1785f84a 2018-12-23 stsp return open_commit(commit, repo, got_object_get_id(obj), 1);
851 434025f3 2018-09-16 stsp const struct got_error *
852 dbc6a6b6 2018-07-12 stsp got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
854 dbc6a6b6 2018-07-12 stsp const struct got_error *err = NULL;
856 dbc6a6b6 2018-07-12 stsp *qid = calloc(1, sizeof(**qid));
857 dbc6a6b6 2018-07-12 stsp if (*qid == NULL)
858 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
860 dbc6a6b6 2018-07-12 stsp (*qid)->id = got_object_id_dup(id);
861 dbc6a6b6 2018-07-12 stsp if ((*qid)->id == NULL) {
862 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
863 fa2f6902 2018-07-23 stsp got_object_qid_free(*qid);
864 dbc6a6b6 2018-07-12 stsp *qid = NULL;
865 dbc6a6b6 2018-07-12 stsp return err;
868 9ca9aafb 2021-06-18 stsp return NULL;
871 9ca9aafb 2021-06-18 stsp const struct got_error *
872 9ca9aafb 2021-06-18 stsp got_object_id_queue_copy(const struct got_object_id_queue *src,
873 9ca9aafb 2021-06-18 stsp struct got_object_id_queue *dest)
875 9ca9aafb 2021-06-18 stsp const struct got_error *err;
876 9ca9aafb 2021-06-18 stsp struct got_object_qid *qid;
878 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, src, entry) {
879 9ca9aafb 2021-06-18 stsp struct got_object_qid *new;
881 9ca9aafb 2021-06-18 stsp * Deep-copy the object ID only. Let the caller deal
882 9ca9aafb 2021-06-18 stsp * with setting up the new->data pointer if needed.
884 9ca9aafb 2021-06-18 stsp err = got_object_qid_alloc(&new, qid->id);
886 9ca9aafb 2021-06-18 stsp got_object_id_queue_free(dest);
887 9ca9aafb 2021-06-18 stsp return err;
889 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(dest, new, entry);
892 dbc6a6b6 2018-07-12 stsp return NULL;
895 a158c901 2018-12-23 stsp static const struct got_error *
896 13c729f7 2018-12-24 stsp request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
897 13c729f7 2018-12-24 stsp int pack_idx, struct got_object_id *id)
899 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
901 13c729f7 2018-12-24 stsp err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
904 a158c901 2018-12-23 stsp return err;
906 a158c901 2018-12-23 stsp return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
909 71eb0e7f 2018-09-16 stsp static const struct got_error *
910 13c729f7 2018-12-24 stsp read_packed_tree_privsep(struct got_tree_object **tree,
911 13c729f7 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
912 13c729f7 2018-12-24 stsp struct got_object_id *id)
914 13c729f7 2018-12-24 stsp const struct got_error *err = NULL;
916 13c729f7 2018-12-24 stsp if (pack->privsep_child)
917 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
919 13c729f7 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
921 13c729f7 2018-12-24 stsp return err;
923 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
926 13c729f7 2018-12-24 stsp static const struct got_error *
927 9f2369b0 2018-12-24 stsp request_tree(struct got_tree_object **tree, struct got_repository *repo,
928 d5c81d44 2021-07-08 stsp int fd, struct got_object_id *id)
930 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
931 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
933 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
935 d5c81d44 2021-07-08 stsp err = got_privsep_send_tree_req(ibuf, fd, id, -1);
937 9f2369b0 2018-12-24 stsp return err;
939 9f2369b0 2018-12-24 stsp return got_privsep_recv_tree(tree, ibuf);
942 9f2369b0 2018-12-24 stsp const struct got_error *
943 9f2369b0 2018-12-24 stsp read_tree_privsep(struct got_tree_object **tree, int obj_fd,
944 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo)
946 ddc7b220 2019-09-08 stsp const struct got_error *err;
947 9f2369b0 2018-12-24 stsp int imsg_fds[2];
949 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
951 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
952 d5c81d44 2021-07-08 stsp return request_tree(tree, repo, obj_fd, id);
954 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
955 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
956 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
958 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
959 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
960 ddc7b220 2019-09-08 stsp free(ibuf);
961 ddc7b220 2019-09-08 stsp return err;
964 9f2369b0 2018-12-24 stsp pid = fork();
965 ddc7b220 2019-09-08 stsp if (pid == -1) {
966 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
967 ddc7b220 2019-09-08 stsp free(ibuf);
968 ddc7b220 2019-09-08 stsp return err;
970 9f2369b0 2018-12-24 stsp else if (pid == 0) {
971 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
972 9f2369b0 2018-12-24 stsp repo->path);
973 9f2369b0 2018-12-24 stsp /* not reached */
976 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
977 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
978 ddc7b220 2019-09-08 stsp free(ibuf);
979 ddc7b220 2019-09-08 stsp return err;
981 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
982 9f2369b0 2018-12-24 stsp imsg_fds[0];
983 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
984 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
985 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
988 d5c81d44 2021-07-08 stsp return request_tree(tree, repo, obj_fd, id);
991 9f2369b0 2018-12-24 stsp static const struct got_error *
992 13c729f7 2018-12-24 stsp open_tree(struct got_tree_object **tree, struct got_repository *repo,
993 13c729f7 2018-12-24 stsp struct got_object_id *id, int check_cache)
995 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
996 13c729f7 2018-12-24 stsp struct got_packidx *packidx = NULL;
998 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1000 71eb0e7f 2018-09-16 stsp if (check_cache) {
1001 13c729f7 2018-12-24 stsp *tree = got_repo_get_cached_tree(repo, id);
1002 71eb0e7f 2018-09-16 stsp if (*tree != NULL) {
1003 71eb0e7f 2018-09-16 stsp (*tree)->refcnt++;
1004 71eb0e7f 2018-09-16 stsp return NULL;
1007 71eb0e7f 2018-09-16 stsp *tree = NULL;
1009 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1010 e82b1d81 2019-07-27 stsp if (err == NULL) {
1011 13c729f7 2018-12-24 stsp struct got_pack *pack = NULL;
1013 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
1014 aea75d87 2021-07-06 stsp packidx->path_packidx);
1016 13c729f7 2018-12-24 stsp return err;
1018 13c729f7 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1019 e7885405 2018-09-10 stsp if (pack == NULL) {
1020 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1023 8d2c5ea3 2019-08-13 stsp goto done;
1025 13c729f7 2018-12-24 stsp err = read_packed_tree_privsep(tree, pack,
1026 13c729f7 2018-12-24 stsp packidx, idx, id);
1027 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1030 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1032 e82b1d81 2019-07-27 stsp return err;
1033 d5c81d44 2021-07-08 stsp err = read_tree_privsep(tree, fd, id, repo);
1036 f6be5c30 2018-06-22 stsp if (err == NULL) {
1037 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
1038 13c729f7 2018-12-24 stsp err = got_repo_cache_tree(repo, id, *tree);
1041 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1042 776d4d29 2018-06-17 stsp return err;
1045 776d4d29 2018-06-17 stsp const struct got_error *
1046 776d4d29 2018-06-17 stsp got_object_open_as_tree(struct got_tree_object **tree,
1047 776d4d29 2018-06-17 stsp struct got_repository *repo, struct got_object_id *id)
1049 e8eb494a 2018-09-16 stsp *tree = got_repo_get_cached_tree(repo, id);
1050 e8eb494a 2018-09-16 stsp if (*tree != NULL) {
1051 e8eb494a 2018-09-16 stsp (*tree)->refcnt++;
1052 e8eb494a 2018-09-16 stsp return NULL;
1055 13c729f7 2018-12-24 stsp return open_tree(tree, repo, id, 0);
1058 71eb0e7f 2018-09-16 stsp const struct got_error *
1059 71eb0e7f 2018-09-16 stsp got_object_tree_open(struct got_tree_object **tree,
1060 71eb0e7f 2018-09-16 stsp struct got_repository *repo, struct got_object *obj)
1062 13c729f7 2018-12-24 stsp return open_tree(tree, repo, got_object_get_id(obj), 1);
1066 56e0773d 2019-11-28 stsp got_object_tree_get_nentries(struct got_tree_object *tree)
1068 56e0773d 2019-11-28 stsp return tree->nentries;
1071 56e0773d 2019-11-28 stsp struct got_tree_entry *
1072 56e0773d 2019-11-28 stsp got_object_tree_get_first_entry(struct got_tree_object *tree)
1074 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, 0);
1077 56e0773d 2019-11-28 stsp struct got_tree_entry *
1078 56e0773d 2019-11-28 stsp got_object_tree_get_last_entry(struct got_tree_object *tree)
1080 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, tree->nentries - 1);
1083 56e0773d 2019-11-28 stsp struct got_tree_entry *
1084 56e0773d 2019-11-28 stsp got_object_tree_get_entry(struct got_tree_object *tree, int i)
1086 56e0773d 2019-11-28 stsp if (i < 0 || i >= tree->nentries)
1087 56e0773d 2019-11-28 stsp return NULL;
1088 56e0773d 2019-11-28 stsp return &tree->entries[i];
1092 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(struct got_tree_entry *te)
1094 56e0773d 2019-11-28 stsp return te->mode;
1097 56e0773d 2019-11-28 stsp const char *
1098 56e0773d 2019-11-28 stsp got_tree_entry_get_name(struct got_tree_entry *te)
1100 56e0773d 2019-11-28 stsp return &te->name[0];
1103 56e0773d 2019-11-28 stsp struct got_object_id *
1104 56e0773d 2019-11-28 stsp got_tree_entry_get_id(struct got_tree_entry *te)
1106 56e0773d 2019-11-28 stsp return &te->id;
1109 0d6c6ee3 2020-05-20 stsp const struct got_error *
1110 af57b12a 2020-07-23 stsp got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1112 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
1113 32596e16 2020-07-23 stsp size_t len, totlen, hdrlen, offset;
1115 aa092692 2020-07-23 stsp *s = NULL;
1117 659dc16e 2020-07-23 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1118 659dc16e 2020-07-23 stsp totlen = 0;
1119 32596e16 2020-07-23 stsp offset = 0;
1123 659dc16e 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1125 af57b12a 2020-07-23 stsp return err;
1127 659dc16e 2020-07-23 stsp if (len == 0)
1130 659dc16e 2020-07-23 stsp totlen += len - hdrlen;
1131 af57b12a 2020-07-23 stsp p = realloc(*s, totlen + 1);
1132 659dc16e 2020-07-23 stsp if (p == NULL) {
1133 659dc16e 2020-07-23 stsp err = got_error_from_errno("realloc");
1135 af57b12a 2020-07-23 stsp *s = NULL;
1136 af57b12a 2020-07-23 stsp return err;
1139 659dc16e 2020-07-23 stsp /* Skip blob object header first time around. */
1140 af57b12a 2020-07-23 stsp memcpy(*s + offset,
1141 f8f7c882 2020-07-23 stsp got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1142 659dc16e 2020-07-23 stsp hdrlen = 0;
1143 32596e16 2020-07-23 stsp offset = totlen;
1144 659dc16e 2020-07-23 stsp } while (len > 0);
1146 af57b12a 2020-07-23 stsp (*s)[totlen] = '\0';
1147 af57b12a 2020-07-23 stsp return NULL;
1150 af57b12a 2020-07-23 stsp const struct got_error *
1151 af57b12a 2020-07-23 stsp got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1152 af57b12a 2020-07-23 stsp struct got_repository *repo)
1154 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
1155 af57b12a 2020-07-23 stsp struct got_blob_object *blob = NULL;
1157 af57b12a 2020-07-23 stsp *link_target = NULL;
1159 af57b12a 2020-07-23 stsp if (!got_object_tree_entry_is_symlink(te))
1160 af57b12a 2020-07-23 stsp return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1162 af57b12a 2020-07-23 stsp err = got_object_open_as_blob(&blob, repo,
1163 af57b12a 2020-07-23 stsp got_tree_entry_get_id(te), PATH_MAX);
1165 af57b12a 2020-07-23 stsp return err;
1167 af57b12a 2020-07-23 stsp err = got_object_blob_read_to_str(link_target, blob);
1168 af57b12a 2020-07-23 stsp got_object_blob_close(blob);
1169 659dc16e 2020-07-23 stsp if (err) {
1170 659dc16e 2020-07-23 stsp free(*link_target);
1171 659dc16e 2020-07-23 stsp *link_target = NULL;
1173 0d6c6ee3 2020-05-20 stsp return err;
1177 56e0773d 2019-11-28 stsp got_tree_entry_get_index(struct got_tree_entry *te)
1179 56e0773d 2019-11-28 stsp return te->idx;
1182 56e0773d 2019-11-28 stsp struct got_tree_entry *
1183 56e0773d 2019-11-28 stsp got_tree_entry_get_next(struct got_tree_object *tree,
1184 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
1186 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx + 1);
1189 56e0773d 2019-11-28 stsp struct got_tree_entry *
1190 56e0773d 2019-11-28 stsp got_tree_entry_get_prev(struct got_tree_object *tree,
1191 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
1193 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx - 1);
1196 3840f4c9 2018-09-12 stsp static const struct got_error *
1197 ac544f8c 2019-01-13 stsp request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1198 ebc55e2d 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1199 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
1201 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
1202 db696021 2022-01-04 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1203 db696021 2022-01-04 stsp int outfd_child;
1205 db696021 2022-01-04 stsp err = pack_child_send_tempfiles(ibuf, pack);
1207 db696021 2022-01-04 stsp return err;
1209 3840f4c9 2018-09-12 stsp outfd_child = dup(outfd);
1210 3840f4c9 2018-09-12 stsp if (outfd_child == -1)
1211 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1213 ebc55e2d 2018-12-24 stsp err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1215 3840f4c9 2018-09-12 stsp return err;
1217 3840f4c9 2018-09-12 stsp err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1218 3840f4c9 2018-09-12 stsp outfd_child);
1219 3840f4c9 2018-09-12 stsp if (err) {
1220 3840f4c9 2018-09-12 stsp return err;
1223 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen,
1224 ebc55e2d 2018-12-24 stsp pack->privsep_child->ibuf);
1226 3840f4c9 2018-09-12 stsp return err;
1228 3840f4c9 2018-09-12 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1229 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1231 3840f4c9 2018-09-12 stsp return err;
1234 ebc55e2d 2018-12-24 stsp static const struct got_error *
1235 ac544f8c 2019-01-13 stsp read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1236 ac544f8c 2019-01-13 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1237 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
1239 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
1241 ebc55e2d 2018-12-24 stsp if (pack->privsep_child == NULL) {
1242 ebc55e2d 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
1244 ebc55e2d 2018-12-24 stsp return err;
1247 ac544f8c 2019-01-13 stsp return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1251 9f2369b0 2018-12-24 stsp static const struct got_error *
1252 ac544f8c 2019-01-13 stsp request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1253 d5c81d44 2021-07-08 stsp int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1255 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1256 9f2369b0 2018-12-24 stsp int outfd_child;
1258 9f2369b0 2018-12-24 stsp outfd_child = dup(outfd);
1259 9f2369b0 2018-12-24 stsp if (outfd_child == -1)
1260 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1262 d5c81d44 2021-07-08 stsp err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1264 9f2369b0 2018-12-24 stsp return err;
1266 9f2369b0 2018-12-24 stsp err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1268 9f2369b0 2018-12-24 stsp return err;
1270 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1272 9f2369b0 2018-12-24 stsp return err;
1274 9f2369b0 2018-12-24 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1275 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
1277 9f2369b0 2018-12-24 stsp return err;
1280 9f2369b0 2018-12-24 stsp static const struct got_error *
1281 ac544f8c 2019-01-13 stsp read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1282 d5c81d44 2021-07-08 stsp int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1284 ddc7b220 2019-09-08 stsp const struct got_error *err;
1285 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1286 9f2369b0 2018-12-24 stsp pid_t pid;
1287 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1289 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1290 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1291 d5c81d44 2021-07-08 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1295 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1296 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1297 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1299 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1300 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1301 ddc7b220 2019-09-08 stsp free(ibuf);
1302 ddc7b220 2019-09-08 stsp return err;
1305 9f2369b0 2018-12-24 stsp pid = fork();
1306 ddc7b220 2019-09-08 stsp if (pid == -1) {
1307 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1308 ddc7b220 2019-09-08 stsp free(ibuf);
1309 ddc7b220 2019-09-08 stsp return err;
1311 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1312 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1313 9f2369b0 2018-12-24 stsp repo->path);
1314 9f2369b0 2018-12-24 stsp /* not reached */
1317 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
1318 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1319 ddc7b220 2019-09-08 stsp free(ibuf);
1320 ddc7b220 2019-09-08 stsp return err;
1322 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1323 9f2369b0 2018-12-24 stsp imsg_fds[0];
1324 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1325 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1326 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1328 d5c81d44 2021-07-08 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1331 ebc55e2d 2018-12-24 stsp static const struct got_error *
1332 ebc55e2d 2018-12-24 stsp open_blob(struct got_blob_object **blob, struct got_repository *repo,
1333 ebc55e2d 2018-12-24 stsp struct got_object_id *id, size_t blocksize)
1335 ebc55e2d 2018-12-24 stsp const struct got_error *err = NULL;
1336 ebc55e2d 2018-12-24 stsp struct got_packidx *packidx = NULL;
1338 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1339 ac544f8c 2019-01-13 stsp uint8_t *outbuf;
1340 e82b1d81 2019-07-27 stsp int outfd;
1341 ebc55e2d 2018-12-24 stsp size_t size, hdrlen;
1342 ebc55e2d 2018-12-24 stsp struct stat sb;
1344 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
1345 4558fcd4 2018-01-14 stsp if (*blob == NULL)
1346 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1348 55da3778 2018-09-10 stsp outfd = got_opentempfd();
1349 55da3778 2018-09-10 stsp if (outfd == -1)
1350 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
1352 062ebb78 2018-07-12 stsp (*blob)->read_buf = malloc(blocksize);
1353 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
1354 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1355 c7254d79 2018-04-24 stsp goto done;
1358 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1359 e82b1d81 2019-07-27 stsp if (err == NULL) {
1360 ebc55e2d 2018-12-24 stsp struct got_pack *pack = NULL;
1362 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
1363 aea75d87 2021-07-06 stsp packidx->path_packidx);
1365 ebc55e2d 2018-12-24 stsp goto done;
1367 ebc55e2d 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1368 55da3778 2018-09-10 stsp if (pack == NULL) {
1369 ebc55e2d 2018-12-24 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1372 55da3778 2018-09-10 stsp goto done;
1374 ac544f8c 2019-01-13 stsp err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1375 ac544f8c 2019-01-13 stsp pack, packidx, idx, id);
1376 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1379 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&infd, id, repo);
1381 e82b1d81 2019-07-27 stsp goto done;
1382 ac544f8c 2019-01-13 stsp err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1383 d5c81d44 2021-07-08 stsp id, repo);
1386 55da3778 2018-09-10 stsp goto done;
1388 de060dff 2018-12-24 stsp if (hdrlen > size) {
1389 ebc55e2d 2018-12-24 stsp err = got_error(GOT_ERR_BAD_OBJ_HDR);
1390 ebc55e2d 2018-12-24 stsp goto done;
1393 ac544f8c 2019-01-13 stsp if (outbuf) {
1394 08578a35 2021-01-22 stsp if (close(outfd) == -1 && err == NULL)
1395 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1396 ac544f8c 2019-01-13 stsp outfd = -1;
1397 ac544f8c 2019-01-13 stsp (*blob)->f = fmemopen(outbuf, size, "rb");
1398 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1399 638f9024 2019-05-13 stsp err = got_error_from_errno("fmemopen");
1400 ac544f8c 2019-01-13 stsp free(outbuf);
1401 ac544f8c 2019-01-13 stsp goto done;
1403 ac544f8c 2019-01-13 stsp (*blob)->data = outbuf;
1405 ac544f8c 2019-01-13 stsp if (fstat(outfd, &sb) == -1) {
1406 638f9024 2019-05-13 stsp err = got_error_from_errno("fstat");
1407 ac544f8c 2019-01-13 stsp goto done;
1410 ac544f8c 2019-01-13 stsp if (sb.st_size != size) {
1411 ac544f8c 2019-01-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1412 ac544f8c 2019-01-13 stsp goto done;
1415 ac544f8c 2019-01-13 stsp (*blob)->f = fdopen(outfd, "rb");
1416 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1417 638f9024 2019-05-13 stsp err = got_error_from_errno("fdopen");
1418 ac544f8c 2019-01-13 stsp close(outfd);
1419 ac544f8c 2019-01-13 stsp outfd = -1;
1420 ac544f8c 2019-01-13 stsp goto done;
1424 ebc55e2d 2018-12-24 stsp (*blob)->hdrlen = hdrlen;
1425 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
1426 ebc55e2d 2018-12-24 stsp memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1429 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1430 55da3778 2018-09-10 stsp if (err) {
1431 55da3778 2018-09-10 stsp if (*blob) {
1432 7baf5860 2019-03-19 stsp got_object_blob_close(*blob);
1433 55da3778 2018-09-10 stsp *blob = NULL;
1434 55da3778 2018-09-10 stsp } else if (outfd != -1)
1435 55da3778 2018-09-10 stsp close(outfd);
1437 a19581a2 2018-06-21 stsp return err;
1440 a19581a2 2018-06-21 stsp const struct got_error *
1441 a19581a2 2018-06-21 stsp got_object_open_as_blob(struct got_blob_object **blob,
1442 a19581a2 2018-06-21 stsp struct got_repository *repo, struct got_object_id *id,
1443 a19581a2 2018-06-21 stsp size_t blocksize)
1445 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, id, blocksize);
1448 ebc55e2d 2018-12-24 stsp const struct got_error *
1449 ebc55e2d 2018-12-24 stsp got_object_blob_open(struct got_blob_object **blob,
1450 ebc55e2d 2018-12-24 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
1452 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1455 fb43ecf1 2019-02-11 stsp const struct got_error *
1456 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
1458 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL;
1459 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
1460 56b63ca4 2021-01-22 stsp if (blob->f && fclose(blob->f) == EOF)
1461 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1462 ac544f8c 2019-01-13 stsp free(blob->data);
1463 68482ea3 2017-11-27 stsp free(blob);
1464 fb43ecf1 2019-02-11 stsp return err;
1468 8ba819a3 2020-07-23 stsp got_object_blob_rewind(struct got_blob_object *blob)
1470 8ba819a3 2020-07-23 stsp if (blob->f)
1471 8ba819a3 2020-07-23 stsp rewind(blob->f);
1475 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1477 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1481 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
1483 f934cf2c 2018-02-12 stsp return blob->hdrlen;
1486 f934cf2c 2018-02-12 stsp const uint8_t *
1487 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
1489 f934cf2c 2018-02-12 stsp return blob->read_buf;
1492 68482ea3 2017-11-27 stsp const struct got_error *
1493 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1497 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1498 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
1499 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
1500 eb651edf 2018-02-11 stsp *outlenp = n;
1501 35e9ba5d 2018-06-21 stsp return NULL;
1504 35e9ba5d 2018-06-21 stsp const struct got_error *
1505 be659d10 2020-11-18 stsp got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1506 6c4c42e0 2019-06-24 stsp off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1508 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
1509 b6752625 2018-12-24 stsp size_t n, len, hdrlen;
1510 84451b3e 2018-07-10 stsp const uint8_t *buf;
1512 c33ebc60 2020-11-18 stsp const int alloc_chunksz = 512;
1513 c33ebc60 2020-11-18 stsp size_t nalloc = 0;
1514 f595d9bd 2019-08-14 stsp off_t off = 0, total_len = 0;
1516 6c4c42e0 2019-06-24 stsp if (line_offsets)
1517 6c4c42e0 2019-06-24 stsp *line_offsets = NULL;
1518 f595d9bd 2019-08-14 stsp if (filesize)
1519 f595d9bd 2019-08-14 stsp *filesize = 0;
1520 84451b3e 2018-07-10 stsp if (nlines)
1521 84451b3e 2018-07-10 stsp *nlines = 0;
1523 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1525 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
1527 35e9ba5d 2018-06-21 stsp return err;
1528 35e9ba5d 2018-06-21 stsp if (len == 0)
1530 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
1531 b02560ec 2019-08-19 stsp i = hdrlen;
1532 f1cbc3bc 2020-11-18 stsp if (nlines) {
1533 f1cbc3bc 2020-11-18 stsp if (line_offsets && *line_offsets == NULL) {
1534 78695fb7 2019-08-12 stsp /* Have some data but perhaps no '\n'. */
1535 78695fb7 2019-08-12 stsp *nlines = 1;
1536 c33ebc60 2020-11-18 stsp nalloc = alloc_chunksz;
1537 c33ebc60 2020-11-18 stsp *line_offsets = calloc(nalloc,
1538 c33ebc60 2020-11-18 stsp sizeof(**line_offsets));
1539 78695fb7 2019-08-12 stsp if (*line_offsets == NULL)
1540 845785d4 2020-02-02 tracey return got_error_from_errno("calloc");
1542 b02560ec 2019-08-19 stsp /* Skip forward over end of first line. */
1543 b02560ec 2019-08-19 stsp while (i < len) {
1544 b02560ec 2019-08-19 stsp if (buf[i] == '\n')
1549 b02560ec 2019-08-19 stsp /* Scan '\n' offsets in remaining chunk of data. */
1550 b02560ec 2019-08-19 stsp while (i < len) {
1551 b02560ec 2019-08-19 stsp if (buf[i] != '\n') {
1555 f595d9bd 2019-08-14 stsp (*nlines)++;
1556 c33ebc60 2020-11-18 stsp if (line_offsets && nalloc < *nlines) {
1557 c33ebc60 2020-11-18 stsp size_t n = *nlines + alloc_chunksz;
1558 78695fb7 2019-08-12 stsp off_t *o = recallocarray(*line_offsets,
1559 c33ebc60 2020-11-18 stsp nalloc, n, sizeof(**line_offsets));
1560 78695fb7 2019-08-12 stsp if (o == NULL) {
1561 78695fb7 2019-08-12 stsp free(*line_offsets);
1562 78695fb7 2019-08-12 stsp *line_offsets = NULL;
1563 78695fb7 2019-08-12 stsp return got_error_from_errno(
1564 78695fb7 2019-08-12 stsp "recallocarray");
1566 78695fb7 2019-08-12 stsp *line_offsets = o;
1567 c33ebc60 2020-11-18 stsp nalloc = n;
1569 f1cbc3bc 2020-11-18 stsp if (line_offsets) {
1570 f1cbc3bc 2020-11-18 stsp off = total_len + i - hdrlen + 1;
1571 f1cbc3bc 2020-11-18 stsp (*line_offsets)[*nlines - 1] = off;
1576 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
1577 454a6b59 2018-12-24 stsp n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1578 b6752625 2018-12-24 stsp if (n != len - hdrlen)
1579 b6752625 2018-12-24 stsp return got_ferror(outfile, GOT_ERR_IO);
1580 f595d9bd 2019-08-14 stsp total_len += len - hdrlen;
1581 35e9ba5d 2018-06-21 stsp hdrlen = 0;
1582 35e9ba5d 2018-06-21 stsp } while (len != 0);
1584 cbe7f848 2019-02-11 stsp if (fflush(outfile) != 0)
1585 638f9024 2019-05-13 stsp return got_error_from_errno("fflush");
1586 35e9ba5d 2018-06-21 stsp rewind(outfile);
1588 f595d9bd 2019-08-14 stsp if (filesize)
1589 f595d9bd 2019-08-14 stsp *filesize = total_len;
1591 776d4d29 2018-06-17 stsp return NULL;
1594 f4a881ce 2018-11-17 stsp static const struct got_error *
1595 268f7291 2018-12-24 stsp request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1596 268f7291 2018-12-24 stsp int pack_idx, struct got_object_id *id)
1598 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
1600 268f7291 2018-12-24 stsp err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1601 268f7291 2018-12-24 stsp pack_idx);
1603 a158c901 2018-12-23 stsp return err;
1605 a158c901 2018-12-23 stsp return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1608 268f7291 2018-12-24 stsp static const struct got_error *
1609 268f7291 2018-12-24 stsp read_packed_tag_privsep(struct got_tag_object **tag,
1610 268f7291 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1611 268f7291 2018-12-24 stsp struct got_object_id *id)
1613 268f7291 2018-12-24 stsp const struct got_error *err = NULL;
1615 268f7291 2018-12-24 stsp if (pack->privsep_child)
1616 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1618 268f7291 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
1620 268f7291 2018-12-24 stsp return err;
1622 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1625 9f2369b0 2018-12-24 stsp static const struct got_error *
1626 9f2369b0 2018-12-24 stsp request_tag(struct got_tag_object **tag, struct got_repository *repo,
1627 d5c81d44 2021-07-08 stsp int fd, struct got_object_id *id)
1629 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1630 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1632 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1634 d5c81d44 2021-07-08 stsp err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1636 9f2369b0 2018-12-24 stsp return err;
1638 9f2369b0 2018-12-24 stsp return got_privsep_recv_tag(tag, ibuf);
1641 9f2369b0 2018-12-24 stsp static const struct got_error *
1642 9f2369b0 2018-12-24 stsp read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1643 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo)
1645 ddc7b220 2019-09-08 stsp const struct got_error *err;
1646 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1647 9f2369b0 2018-12-24 stsp pid_t pid;
1648 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1650 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1651 d5c81d44 2021-07-08 stsp return request_tag(tag, repo, obj_fd, id);
1653 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1654 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1655 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1657 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1658 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1659 ddc7b220 2019-09-08 stsp free(ibuf);
1660 ddc7b220 2019-09-08 stsp return err;
1663 9f2369b0 2018-12-24 stsp pid = fork();
1664 ddc7b220 2019-09-08 stsp if (pid == -1) {
1665 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1666 ddc7b220 2019-09-08 stsp free(ibuf);
1667 ddc7b220 2019-09-08 stsp return err;
1669 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1670 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1671 9f2369b0 2018-12-24 stsp repo->path);
1672 9f2369b0 2018-12-24 stsp /* not reached */
1675 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
1676 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1677 ddc7b220 2019-09-08 stsp free(ibuf);
1678 ddc7b220 2019-09-08 stsp return err;
1680 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1681 9f2369b0 2018-12-24 stsp imsg_fds[0];
1682 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1683 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1684 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1686 d5c81d44 2021-07-08 stsp return request_tag(tag, repo, obj_fd, id);
1689 a158c901 2018-12-23 stsp static const struct got_error *
1690 268f7291 2018-12-24 stsp open_tag(struct got_tag_object **tag, struct got_repository *repo,
1691 268f7291 2018-12-24 stsp struct got_object_id *id, int check_cache)
1693 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1694 268f7291 2018-12-24 stsp struct got_packidx *packidx = NULL;
1696 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1697 5d844a1e 2019-08-13 stsp struct got_object *obj = NULL;
1698 5d844a1e 2019-08-13 stsp int obj_type = GOT_OBJ_TYPE_ANY;
1700 f4a881ce 2018-11-17 stsp if (check_cache) {
1701 268f7291 2018-12-24 stsp *tag = got_repo_get_cached_tag(repo, id);
1702 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1703 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1704 f4a881ce 2018-11-17 stsp return NULL;
1707 f4a881ce 2018-11-17 stsp *tag = NULL;
1709 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1710 e82b1d81 2019-07-27 stsp if (err == NULL) {
1711 268f7291 2018-12-24 stsp struct got_pack *pack = NULL;
1713 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
1714 aea75d87 2021-07-06 stsp packidx->path_packidx);
1716 268f7291 2018-12-24 stsp return err;
1718 268f7291 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1719 f4a881ce 2018-11-17 stsp if (pack == NULL) {
1720 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1723 8d2c5ea3 2019-08-13 stsp goto done;
1726 992eb9d8 2020-02-07 tracey /* Beware of "lightweight" tags: Check object type first. */
1727 5d844a1e 2019-08-13 stsp err = read_packed_object_privsep(&obj, repo, pack, packidx,
1730 5d844a1e 2019-08-13 stsp goto done;
1731 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1732 5d844a1e 2019-08-13 stsp got_object_close(obj);
1733 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG) {
1734 5d844a1e 2019-08-13 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1735 5d844a1e 2019-08-13 stsp goto done;
1737 5d844a1e 2019-08-13 stsp err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1738 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1741 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1743 e82b1d81 2019-07-27 stsp return err;
1744 d5c81d44 2021-07-08 stsp err = got_object_read_header_privsep(&obj, id, repo, fd);
1746 5d844a1e 2019-08-13 stsp return err;
1747 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1748 5d844a1e 2019-08-13 stsp got_object_close(obj);
1749 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
1750 5d844a1e 2019-08-13 stsp return got_error(GOT_ERR_OBJ_TYPE);
1752 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1754 5d844a1e 2019-08-13 stsp return err;
1755 d5c81d44 2021-07-08 stsp err = read_tag_privsep(tag, fd, id, repo);
1758 f4a881ce 2018-11-17 stsp if (err == NULL) {
1759 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1760 268f7291 2018-12-24 stsp err = got_repo_cache_tag(repo, id, *tag);
1763 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1764 f4a881ce 2018-11-17 stsp return err;
1767 f4a881ce 2018-11-17 stsp const struct got_error *
1768 f4a881ce 2018-11-17 stsp got_object_open_as_tag(struct got_tag_object **tag,
1769 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object_id *id)
1771 f4a881ce 2018-11-17 stsp *tag = got_repo_get_cached_tag(repo, id);
1772 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1773 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1774 f4a881ce 2018-11-17 stsp return NULL;
1777 268f7291 2018-12-24 stsp return open_tag(tag, repo, id, 0);
1780 f4a881ce 2018-11-17 stsp const struct got_error *
1781 f4a881ce 2018-11-17 stsp got_object_tag_open(struct got_tag_object **tag,
1782 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object *obj)
1784 268f7291 2018-12-24 stsp return open_tag(tag, repo, got_object_get_id(obj), 1);
1787 d24820bf 2019-08-11 stsp const char *
1788 d24820bf 2019-08-11 stsp got_object_tag_get_name(struct got_tag_object *tag)
1790 d24820bf 2019-08-11 stsp return tag->tag;
1794 0bd18d37 2019-02-01 stsp got_object_tag_get_object_type(struct got_tag_object *tag)
1796 0bd18d37 2019-02-01 stsp return tag->obj_type;
1799 0bd18d37 2019-02-01 stsp struct got_object_id *
1800 0bd18d37 2019-02-01 stsp got_object_tag_get_object_id(struct got_tag_object *tag)
1802 0bd18d37 2019-02-01 stsp return &tag->id;
1806 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_time(struct got_tag_object *tag)
1808 01073a5d 2019-08-22 stsp return tag->tagger_time;
1812 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1814 01073a5d 2019-08-22 stsp return tag->tagger_gmtoff;
1817 01073a5d 2019-08-22 stsp const char *
1818 01073a5d 2019-08-22 stsp got_object_tag_get_tagger(struct got_tag_object *tag)
1820 01073a5d 2019-08-22 stsp return tag->tagger;
1823 01073a5d 2019-08-22 stsp const char *
1824 01073a5d 2019-08-22 stsp got_object_tag_get_message(struct got_tag_object *tag)
1826 01073a5d 2019-08-22 stsp return tag->tagmsg;
1829 776d4d29 2018-06-17 stsp static struct got_tree_entry *
1830 65a9bbe9 2018-09-15 stsp find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1834 63da309a 2018-11-07 stsp /* Note that tree entries are sorted in strncmp() order. */
1835 56e0773d 2019-11-28 stsp for (i = 0; i < tree->nentries; i++) {
1836 56e0773d 2019-11-28 stsp struct got_tree_entry *te = &tree->entries[i];
1837 63da309a 2018-11-07 stsp int cmp = strncmp(te->name, name, len);
1838 63da309a 2018-11-07 stsp if (cmp < 0)
1840 63da309a 2018-11-07 stsp if (cmp > 0)
1842 63da309a 2018-11-07 stsp if (te->name[len] == '\0')
1843 776d4d29 2018-06-17 stsp return te;
1845 eb651edf 2018-02-11 stsp return NULL;
1848 56e0773d 2019-11-28 stsp struct got_tree_entry *
1849 a129376b 2019-03-28 stsp got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1851 a129376b 2019-03-28 stsp return find_entry_by_name(tree, name, strlen(name));
1854 776d4d29 2018-06-17 stsp const struct got_error *
1855 67b631c9 2021-10-10 stsp got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1856 67b631c9 2021-10-10 stsp struct got_repository *repo, struct got_tree_object *tree,
1857 67b631c9 2021-10-10 stsp const char *path)
1859 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
1860 67b631c9 2021-10-10 stsp struct got_tree_object *subtree = NULL;
1861 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
1862 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1863 b7cd37e5 2018-11-18 stsp size_t seglen;
1865 27d434c2 2018-09-15 stsp *id = NULL;
1868 5e54fb30 2019-05-31 stsp while (s[0] == '/')
1871 65a9bbe9 2018-09-15 stsp seglen = 0;
1872 67b631c9 2021-10-10 stsp subtree = tree;
1873 b7cd37e5 2018-11-18 stsp while (*s) {
1874 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
1876 776d4d29 2018-06-17 stsp if (*s != '/') {
1883 67b631c9 2021-10-10 stsp te = find_entry_by_name(subtree, seg, seglen);
1884 db37e2c0 2018-06-21 stsp if (te == NULL) {
1885 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1886 776d4d29 2018-06-17 stsp goto done;
1889 b7cd37e5 2018-11-18 stsp if (*s == '\0')
1892 776d4d29 2018-06-17 stsp seg = s + 1;
1893 65a9bbe9 2018-09-15 stsp seglen = 0;
1896 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
1898 db37e2c0 2018-06-21 stsp te = NULL;
1900 776d4d29 2018-06-17 stsp goto done;
1901 67b631c9 2021-10-10 stsp if (subtree != tree)
1902 67b631c9 2021-10-10 stsp got_object_tree_close(subtree);
1903 67b631c9 2021-10-10 stsp subtree = next_tree;
1908 56e0773d 2019-11-28 stsp *id = got_object_id_dup(&te->id);
1909 27d434c2 2018-09-15 stsp if (*id == NULL)
1910 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
1912 67b631c9 2021-10-10 stsp *mode = te->mode;
1914 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1916 67b631c9 2021-10-10 stsp if (subtree && subtree != tree)
1917 67b631c9 2021-10-10 stsp got_object_tree_close(subtree);
1918 67b631c9 2021-10-10 stsp return err;
1920 67b631c9 2021-10-10 stsp const struct got_error *
1921 67b631c9 2021-10-10 stsp got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1922 67b631c9 2021-10-10 stsp struct got_object_id *commit_id, const char *path)
1924 67b631c9 2021-10-10 stsp const struct got_error *err = NULL;
1925 67b631c9 2021-10-10 stsp struct got_commit_object *commit = NULL;
1926 67b631c9 2021-10-10 stsp struct got_tree_object *tree = NULL;
1928 67b631c9 2021-10-10 stsp *id = NULL;
1930 67b631c9 2021-10-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
1932 67b631c9 2021-10-10 stsp goto done;
1934 67b631c9 2021-10-10 stsp /* Handle opening of root of commit's tree. */
1935 67b631c9 2021-10-10 stsp if (got_path_is_root_dir(path)) {
1936 67b631c9 2021-10-10 stsp *id = got_object_id_dup(commit->tree_id);
1937 67b631c9 2021-10-10 stsp if (*id == NULL)
1938 67b631c9 2021-10-10 stsp err = got_error_from_errno("got_object_id_dup");
1940 67b631c9 2021-10-10 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1942 67b631c9 2021-10-10 stsp goto done;
1943 67b631c9 2021-10-10 stsp err = got_object_tree_find_path(id, NULL, repo, tree, path);
1946 776d4d29 2018-06-17 stsp if (commit)
1947 776d4d29 2018-06-17 stsp got_object_commit_close(commit);
1949 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
1950 776d4d29 2018-06-17 stsp return err;
1954 ac5f2b26 2020-05-05 stsp * Normalize file mode bits to avoid false positive tree entry differences
1955 ac5f2b26 2020-05-05 stsp * in case tree entries have unexpected mode bits set.
1957 ac5f2b26 2020-05-05 stsp static mode_t
1958 ac5f2b26 2020-05-05 stsp normalize_mode_for_comparison(mode_t mode)
1961 ac5f2b26 2020-05-05 stsp * For directories, the only relevant bit is the IFDIR bit.
1962 ac5f2b26 2020-05-05 stsp * This allows us to detect paths changing from a directory
1963 ac5f2b26 2020-05-05 stsp * to a file and vice versa.
1965 ac5f2b26 2020-05-05 stsp if (S_ISDIR(mode))
1966 ac5f2b26 2020-05-05 stsp return mode & S_IFDIR;
1969 40dde666 2020-07-23 stsp * For symlinks, the only relevant bit is the IFLNK bit.
1970 40dde666 2020-07-23 stsp * This allows us to detect paths changing from a symlinks
1971 40dde666 2020-07-23 stsp * to a file or directory and vice versa.
1973 40dde666 2020-07-23 stsp if (S_ISLNK(mode))
1974 40dde666 2020-07-23 stsp return mode & S_IFLNK;
1976 ac5f2b26 2020-05-05 stsp /* For files, the only change we care about is the executable bit. */
1977 ac5f2b26 2020-05-05 stsp return mode & S_IXUSR;
1980 07862c20 2018-09-15 stsp const struct got_error *
1981 07862c20 2018-09-15 stsp got_object_tree_path_changed(int *changed,
1982 07862c20 2018-09-15 stsp struct got_tree_object *tree01, struct got_tree_object *tree02,
1983 07862c20 2018-09-15 stsp const char *path, struct got_repository *repo)
1985 07862c20 2018-09-15 stsp const struct got_error *err = NULL;
1986 07862c20 2018-09-15 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1987 07862c20 2018-09-15 stsp struct got_tree_entry *te1 = NULL, *te2 = NULL;
1988 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1989 3b7f9878 2018-11-18 stsp size_t seglen;
1991 07862c20 2018-09-15 stsp *changed = 0;
1993 07862c20 2018-09-15 stsp /* We not do support comparing the root path. */
1994 61a7d79f 2020-02-29 stsp if (got_path_is_root_dir(path))
1995 63f810e6 2020-02-29 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
1997 07862c20 2018-09-15 stsp tree1 = tree01;
1998 07862c20 2018-09-15 stsp tree2 = tree02;
2000 61a7d79f 2020-02-29 stsp while (*s == '/')
2003 65a9bbe9 2018-09-15 stsp seglen = 0;
2004 3b7f9878 2018-11-18 stsp while (*s) {
2005 07862c20 2018-09-15 stsp struct got_tree_object *next_tree1, *next_tree2;
2006 ac5f2b26 2020-05-05 stsp mode_t mode1, mode2;
2008 07862c20 2018-09-15 stsp if (*s != '/') {
2015 65a9bbe9 2018-09-15 stsp te1 = find_entry_by_name(tree1, seg, seglen);
2016 07862c20 2018-09-15 stsp if (te1 == NULL) {
2017 07862c20 2018-09-15 stsp err = got_error(GOT_ERR_NO_OBJ);
2018 07862c20 2018-09-15 stsp goto done;
2021 e8bfb8f3 2020-12-18 stsp if (tree2)
2022 e8bfb8f3 2020-12-18 stsp te2 = find_entry_by_name(tree2, seg, seglen);
2024 e8bfb8f3 2020-12-18 stsp if (te2) {
2025 e8bfb8f3 2020-12-18 stsp mode1 = normalize_mode_for_comparison(te1->mode);
2026 e8bfb8f3 2020-12-18 stsp mode2 = normalize_mode_for_comparison(te2->mode);
2027 e8bfb8f3 2020-12-18 stsp if (mode1 != mode2) {
2028 e8bfb8f3 2020-12-18 stsp *changed = 1;
2029 e8bfb8f3 2020-12-18 stsp goto done;
2032 e8bfb8f3 2020-12-18 stsp if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2033 e8bfb8f3 2020-12-18 stsp *changed = 0;
2034 e8bfb8f3 2020-12-18 stsp goto done;
2038 3b7f9878 2018-11-18 stsp if (*s == '\0') { /* final path element */
2039 07862c20 2018-09-15 stsp *changed = 1;
2040 07862c20 2018-09-15 stsp goto done;
2043 07862c20 2018-09-15 stsp seg = s + 1;
2045 65a9bbe9 2018-09-15 stsp seglen = 0;
2047 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree1, repo,
2048 56e0773d 2019-11-28 stsp &te1->id);
2049 07862c20 2018-09-15 stsp te1 = NULL;
2051 07862c20 2018-09-15 stsp goto done;
2052 a31cea73 2018-09-15 stsp if (tree1 != tree01)
2053 a31cea73 2018-09-15 stsp got_object_tree_close(tree1);
2054 07862c20 2018-09-15 stsp tree1 = next_tree1;
2056 e8bfb8f3 2020-12-18 stsp if (te2) {
2057 e8bfb8f3 2020-12-18 stsp err = got_object_open_as_tree(&next_tree2, repo,
2058 e8bfb8f3 2020-12-18 stsp &te2->id);
2059 e8bfb8f3 2020-12-18 stsp te2 = NULL;
2061 e8bfb8f3 2020-12-18 stsp goto done;
2062 e8bfb8f3 2020-12-18 stsp if (tree2 != tree02)
2063 e8bfb8f3 2020-12-18 stsp got_object_tree_close(tree2);
2064 e8bfb8f3 2020-12-18 stsp tree2 = next_tree2;
2065 e8bfb8f3 2020-12-18 stsp } else if (tree2) {
2066 e8bfb8f3 2020-12-18 stsp if (tree2 != tree02)
2067 e8bfb8f3 2020-12-18 stsp got_object_tree_close(tree2);
2068 e8bfb8f3 2020-12-18 stsp tree2 = NULL;
2073 a31cea73 2018-09-15 stsp if (tree1 && tree1 != tree01)
2074 07862c20 2018-09-15 stsp got_object_tree_close(tree1);
2075 a31cea73 2018-09-15 stsp if (tree2 && tree2 != tree02)
2076 07862c20 2018-09-15 stsp got_object_tree_close(tree2);
2077 77880158 2018-11-04 stsp return err;
2080 ed175427 2019-05-09 stsp const struct got_error *
2081 ed175427 2019-05-09 stsp got_object_tree_entry_dup(struct got_tree_entry **new_te,
2082 ed175427 2019-05-09 stsp struct got_tree_entry *te)
2084 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
2086 ed175427 2019-05-09 stsp *new_te = calloc(1, sizeof(**new_te));
2087 ed175427 2019-05-09 stsp if (*new_te == NULL)
2088 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
2090 ed175427 2019-05-09 stsp (*new_te)->mode = te->mode;
2091 56e0773d 2019-11-28 stsp memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2092 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2093 8c4eabf2 2019-05-10 stsp return err;
2097 56e0773d 2019-11-28 stsp got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2099 63c5ca5d 2019-08-24 stsp return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2103 e40622f4 2020-07-23 stsp got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2105 e40622f4 2020-07-23 stsp /* S_IFDIR check avoids confusing symlinks with submodules. */
2106 e40622f4 2020-07-23 stsp return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2109 e40622f4 2020-07-23 stsp static const struct got_error *
2110 e40622f4 2020-07-23 stsp resolve_symlink(char **link_target, const char *path,
2111 e40622f4 2020-07-23 stsp struct got_object_id *commit_id, struct got_repository *repo)
2113 e40622f4 2020-07-23 stsp const struct got_error *err = NULL;
2114 dbdd6209 2020-10-19 stsp char buf[PATH_MAX];
2115 e40622f4 2020-07-23 stsp char *name, *parent_path = NULL;
2116 e40622f4 2020-07-23 stsp struct got_object_id *tree_obj_id = NULL;
2117 e40622f4 2020-07-23 stsp struct got_tree_object *tree = NULL;
2118 e40622f4 2020-07-23 stsp struct got_tree_entry *te = NULL;
2120 e40622f4 2020-07-23 stsp *link_target = NULL;
2122 dbdd6209 2020-10-19 stsp if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2123 dbdd6209 2020-10-19 stsp return got_error(GOT_ERR_NO_SPACE);
2125 dbdd6209 2020-10-19 stsp name = basename(buf);
2126 e40622f4 2020-07-23 stsp if (name == NULL)
2127 e40622f4 2020-07-23 stsp return got_error_from_errno2("basename", path);
2129 e40622f4 2020-07-23 stsp err = got_path_dirname(&parent_path, path);
2131 e40622f4 2020-07-23 stsp return err;
2133 e40622f4 2020-07-23 stsp err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
2134 e40622f4 2020-07-23 stsp parent_path);
2135 e40622f4 2020-07-23 stsp if (err) {
2136 e40622f4 2020-07-23 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2137 e40622f4 2020-07-23 stsp /* Display the complete path in error message. */
2138 e40622f4 2020-07-23 stsp err = got_error_path(path, err->code);
2140 e40622f4 2020-07-23 stsp goto done;
2143 e40622f4 2020-07-23 stsp err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2145 e40622f4 2020-07-23 stsp goto done;
2147 e40622f4 2020-07-23 stsp te = got_object_tree_find_entry(tree, name);
2148 e40622f4 2020-07-23 stsp if (te == NULL) {
2149 e40622f4 2020-07-23 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2150 e40622f4 2020-07-23 stsp goto done;
2153 e40622f4 2020-07-23 stsp if (got_object_tree_entry_is_symlink(te)) {
2154 e40622f4 2020-07-23 stsp err = got_tree_entry_get_symlink_target(link_target, te, repo);
2156 e40622f4 2020-07-23 stsp goto done;
2157 e40622f4 2020-07-23 stsp if (!got_path_is_absolute(*link_target)) {
2158 e40622f4 2020-07-23 stsp char *abspath;
2159 e40622f4 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", parent_path,
2160 e40622f4 2020-07-23 stsp *link_target) == -1) {
2161 e40622f4 2020-07-23 stsp err = got_error_from_errno("asprintf");
2162 e40622f4 2020-07-23 stsp goto done;
2164 e40622f4 2020-07-23 stsp free(*link_target);
2165 e40622f4 2020-07-23 stsp *link_target = malloc(PATH_MAX);
2166 e40622f4 2020-07-23 stsp if (*link_target == NULL) {
2167 e40622f4 2020-07-23 stsp err = got_error_from_errno("malloc");
2168 e40622f4 2020-07-23 stsp goto done;
2170 e40622f4 2020-07-23 stsp err = got_canonpath(abspath, *link_target, PATH_MAX);
2171 e40622f4 2020-07-23 stsp free(abspath);
2173 e40622f4 2020-07-23 stsp goto done;
2177 e40622f4 2020-07-23 stsp free(tree_obj_id);
2179 e40622f4 2020-07-23 stsp got_object_tree_close(tree);
2180 e40622f4 2020-07-23 stsp if (err) {
2181 e40622f4 2020-07-23 stsp free(*link_target);
2182 e40622f4 2020-07-23 stsp *link_target = NULL;
2184 e40622f4 2020-07-23 stsp return err;
2187 ca6e02ac 2020-01-07 stsp const struct got_error *
2188 e40622f4 2020-07-23 stsp got_object_resolve_symlinks(char **link_target, const char *path,
2189 e40622f4 2020-07-23 stsp struct got_object_id *commit_id, struct got_repository *repo)
2191 e40622f4 2020-07-23 stsp const struct got_error *err = NULL;
2192 e40622f4 2020-07-23 stsp char *next_target = NULL;
2193 e40622f4 2020-07-23 stsp int max_recursion = 40; /* matches Git */
2195 e40622f4 2020-07-23 stsp *link_target = NULL;
2198 e40622f4 2020-07-23 stsp err = resolve_symlink(&next_target,
2199 e40622f4 2020-07-23 stsp *link_target ? *link_target : path, commit_id, repo);
2202 e40622f4 2020-07-23 stsp if (next_target) {
2203 e40622f4 2020-07-23 stsp free(*link_target);
2204 e40622f4 2020-07-23 stsp if (--max_recursion == 0) {
2205 e40622f4 2020-07-23 stsp err = got_error_path(path, GOT_ERR_RECURSION);
2206 e40622f4 2020-07-23 stsp *link_target = NULL;
2209 e40622f4 2020-07-23 stsp *link_target = next_target;
2211 e40622f4 2020-07-23 stsp } while (next_target);
2213 e40622f4 2020-07-23 stsp return err;
2216 e40622f4 2020-07-23 stsp const struct got_error *
2217 ca6e02ac 2020-01-07 stsp got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2218 ca6e02ac 2020-01-07 stsp struct got_object_id *commit_id, const char *path,
2219 ca6e02ac 2020-01-07 stsp struct got_repository *repo)
2221 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
2222 ca6e02ac 2020-01-07 stsp struct got_pack *pack = NULL;
2223 ca6e02ac 2020-01-07 stsp struct got_packidx *packidx = NULL;
2224 ca6e02ac 2020-01-07 stsp char *path_packfile = NULL;
2225 ca6e02ac 2020-01-07 stsp struct got_commit_object *changed_commit = NULL;
2226 ca6e02ac 2020-01-07 stsp struct got_object_id *changed_commit_id = NULL;
2229 ca6e02ac 2020-01-07 stsp err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2230 ca6e02ac 2020-01-07 stsp if (err) {
2231 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
2232 ca6e02ac 2020-01-07 stsp return err;
2233 ca6e02ac 2020-01-07 stsp return NULL;
2236 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
2237 aea75d87 2021-07-06 stsp packidx->path_packidx);
2239 ca6e02ac 2020-01-07 stsp return err;
2241 ca6e02ac 2020-01-07 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
2242 ca6e02ac 2020-01-07 stsp if (pack == NULL) {
2243 ca6e02ac 2020-01-07 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2245 ca6e02ac 2020-01-07 stsp goto done;
2248 ca6e02ac 2020-01-07 stsp if (pack->privsep_child == NULL) {
2249 ca6e02ac 2020-01-07 stsp err = start_pack_privsep_child(pack, packidx);
2251 ca6e02ac 2020-01-07 stsp goto done;
2254 ca6e02ac 2020-01-07 stsp err = got_privsep_send_commit_traversal_request(
2255 ca6e02ac 2020-01-07 stsp pack->privsep_child->ibuf, commit_id, idx, path);
2257 ca6e02ac 2020-01-07 stsp goto done;
2259 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_traversed_commits(&changed_commit,
2260 ca6e02ac 2020-01-07 stsp &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2262 ca6e02ac 2020-01-07 stsp goto done;
2264 ca6e02ac 2020-01-07 stsp if (changed_commit) {
2266 ca6e02ac 2020-01-07 stsp * Cache the commit in which the path was changed.
2267 ca6e02ac 2020-01-07 stsp * This commit might be opened again soon.
2269 ca6e02ac 2020-01-07 stsp changed_commit->refcnt++;
2270 ca6e02ac 2020-01-07 stsp err = got_repo_cache_commit(repo, changed_commit_id,
2271 ca6e02ac 2020-01-07 stsp changed_commit);
2272 ca6e02ac 2020-01-07 stsp got_object_commit_close(changed_commit);
2275 ca6e02ac 2020-01-07 stsp free(path_packfile);
2276 ca6e02ac 2020-01-07 stsp free(changed_commit_id);
2277 ca6e02ac 2020-01-07 stsp return err;