2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 d71d75ad 2017-11-05 stsp * Permission to use, copy, modify, and distribute this software for any
5 d71d75ad 2017-11-05 stsp * purpose with or without fee is hereby granted, provided that the above
6 d71d75ad 2017-11-05 stsp * copyright notice and this permission notice appear in all copies.
8 d71d75ad 2017-11-05 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 d71d75ad 2017-11-05 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 d71d75ad 2017-11-05 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 d71d75ad 2017-11-05 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 d71d75ad 2017-11-05 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 d71d75ad 2017-11-05 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 d71d75ad 2017-11-05 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 2178c42e 2018-04-22 stsp #include <sys/types.h>
18 0ffeb3c2 2017-11-26 stsp #include <sys/stat.h>
19 2178c42e 2018-04-22 stsp #include <sys/uio.h>
20 2178c42e 2018-04-22 stsp #include <sys/socket.h>
21 2178c42e 2018-04-22 stsp #include <sys/wait.h>
22 b48e2ddb 2019-05-22 stsp #include <sys/resource.h>
24 a1fd68d8 2018-01-12 stsp #include <errno.h>
25 2178c42e 2018-04-22 stsp #include <fcntl.h>
26 d71d75ad 2017-11-05 stsp #include <stdio.h>
27 ab9a70b2 2017-11-06 stsp #include <stdlib.h>
28 ab9a70b2 2017-11-06 stsp #include <string.h>
29 2178c42e 2018-04-22 stsp #include <stdint.h>
30 81a12da5 2020-09-09 naddy #include <unistd.h>
31 ab9a70b2 2017-11-06 stsp #include <zlib.h>
32 ab9a70b2 2017-11-06 stsp #include <ctype.h>
33 e40622f4 2020-07-23 stsp #include <libgen.h>
34 ab9a70b2 2017-11-06 stsp #include <limits.h>
35 788c352e 2018-06-16 stsp #include <time.h>
37 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
39 ab9a70b2 2017-11-06 stsp #include "got_error.h"
40 d71d75ad 2017-11-05 stsp #include "got_object.h"
41 ab9a70b2 2017-11-06 stsp #include "got_repository.h"
42 511a516b 2018-05-19 stsp #include "got_opentemp.h"
43 324d37e7 2019-05-11 stsp #include "got_path.h"
45 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
46 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
47 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
48 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
49 2178c42e 2018-04-22 stsp #include "got_lib_privsep.h"
50 6bef87be 2018-09-11 stsp #include "got_lib_object_idcache.h"
51 6bef87be 2018-09-11 stsp #include "got_lib_object_cache.h"
52 ad242220 2018-09-08 stsp #include "got_lib_object_parse.h"
53 15a94983 2018-12-23 stsp #include "got_lib_pack.h"
54 7bb0daa1 2018-06-21 stsp #include "got_lib_repository.h"
57 ab9a70b2 2017-11-06 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 3235492e 2018-04-01 stsp struct got_object_id *
61 3235492e 2018-04-01 stsp got_object_get_id(struct got_object *obj)
63 6402fb3c 2018-09-15 stsp return &obj->id;
66 bacc9935 2018-05-20 stsp const struct got_error *
67 bacc9935 2018-05-20 stsp got_object_get_id_str(char **outbuf, struct got_object *obj)
69 bacc9935 2018-05-20 stsp return got_object_id_str(outbuf, &obj->id);
72 15a94983 2018-12-23 stsp const struct got_error *
73 15a94983 2018-12-23 stsp got_object_get_type(int *type, struct got_repository *repo,
74 15a94983 2018-12-23 stsp struct got_object_id *id)
76 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
77 15a94983 2018-12-23 stsp struct got_object *obj;
79 15a94983 2018-12-23 stsp err = got_object_open(&obj, repo, id);
83 b107e67f 2018-01-19 stsp switch (obj->type) {
84 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_COMMIT:
85 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_TREE:
86 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_BLOB:
87 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
88 15a94983 2018-12-23 stsp *type = obj->type;
91 15a94983 2018-12-23 stsp err = got_error(GOT_ERR_OBJ_TYPE);
95 15a94983 2018-12-23 stsp got_object_close(obj);
99 90bdb554 2019-04-11 stsp const struct got_error *
100 90bdb554 2019-04-11 stsp got_object_get_path(char **path, struct got_object_id *id,
101 90bdb554 2019-04-11 stsp struct got_repository *repo)
103 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
104 7a132809 2018-07-23 stsp char *hex = NULL;
105 41d2888b 2019-08-11 stsp char *path_objects;
107 e6b1056e 2018-04-22 stsp *path = NULL;
109 41d2888b 2019-08-11 stsp path_objects = got_repo_get_path_objects(repo);
110 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
111 638f9024 2019-05-13 stsp return got_error_from_errno("got_repo_get_path_objects");
113 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
117 d1cda826 2017-11-06 stsp if (asprintf(path, "%s/%.2x/%s", path_objects,
118 d1cda826 2017-11-06 stsp id->sha1[0], hex + 2) == -1)
119 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
123 d1cda826 2017-11-06 stsp free(path_objects);
124 d1cda826 2017-11-06 stsp return err;
127 762d73f4 2021-04-10 stsp const struct got_error *
128 762d73f4 2021-04-10 stsp got_object_open_loose_fd(int *fd, struct got_object_id *id,
129 4796fb13 2018-12-23 stsp struct got_repository *repo)
131 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
132 a1fd68d8 2018-01-12 stsp char *path;
134 90bdb554 2019-04-11 stsp err = got_object_get_path(&path, id, repo);
136 d1cda826 2017-11-06 stsp return err;
137 a5b57ccf 2019-04-11 stsp *fd = open(path, O_RDONLY | O_NOFOLLOW);
138 d5003b79 2018-04-22 stsp if (*fd == -1) {
139 e82b1d81 2019-07-27 stsp err = got_error_from_errno2("open", path);
143 4558fcd4 2018-01-14 stsp free(path);
144 2090a03d 2018-09-09 stsp return err;
147 2090a03d 2018-09-09 stsp static const struct got_error *
148 a158c901 2018-12-23 stsp request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
149 a158c901 2018-12-23 stsp struct got_object_id *id)
151 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
152 a158c901 2018-12-23 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
154 a158c901 2018-12-23 stsp err = got_privsep_send_packed_obj_req(ibuf, idx, id);
156 a158c901 2018-12-23 stsp return err;
158 a158c901 2018-12-23 stsp err = got_privsep_recv_obj(obj, ibuf);
160 a158c901 2018-12-23 stsp return err;
162 a158c901 2018-12-23 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
164 a158c901 2018-12-23 stsp return NULL;
167 59d1e4a0 2021-03-10 stsp static const struct got_error *
168 59d1e4a0 2021-03-10 stsp request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
169 59d1e4a0 2021-03-10 stsp int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
171 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
172 59d1e4a0 2021-03-10 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
173 59d1e4a0 2021-03-10 stsp int outfd_child;
174 59d1e4a0 2021-03-10 stsp int basefd, accumfd; /* temporary files for delta application */
176 59d1e4a0 2021-03-10 stsp basefd = got_opentempfd();
177 59d1e4a0 2021-03-10 stsp if (basefd == -1)
178 59d1e4a0 2021-03-10 stsp return got_error_from_errno("got_opentempfd");
180 59d1e4a0 2021-03-10 stsp accumfd = got_opentempfd();
181 59d1e4a0 2021-03-10 stsp if (accumfd == -1) {
182 59d1e4a0 2021-03-10 stsp close(basefd);
183 59d1e4a0 2021-03-10 stsp return got_error_from_errno("got_opentempfd");
186 59d1e4a0 2021-03-10 stsp outfd_child = dup(outfd);
187 59d1e4a0 2021-03-10 stsp if (outfd_child == -1) {
188 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("dup");
189 59d1e4a0 2021-03-10 stsp close(basefd);
190 59d1e4a0 2021-03-10 stsp close(accumfd);
191 59d1e4a0 2021-03-10 stsp return err;
194 59d1e4a0 2021-03-10 stsp err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
196 59d1e4a0 2021-03-10 stsp close(basefd);
197 59d1e4a0 2021-03-10 stsp close(accumfd);
198 59d1e4a0 2021-03-10 stsp close(outfd_child);
199 59d1e4a0 2021-03-10 stsp return err;
202 59d1e4a0 2021-03-10 stsp err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
204 59d1e4a0 2021-03-10 stsp close(basefd);
205 59d1e4a0 2021-03-10 stsp close(accumfd);
206 59d1e4a0 2021-03-10 stsp return err;
210 59d1e4a0 2021-03-10 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
213 59d1e4a0 2021-03-10 stsp close(accumfd);
214 59d1e4a0 2021-03-10 stsp return err;
217 59d1e4a0 2021-03-10 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
220 59d1e4a0 2021-03-10 stsp return err;
222 59d1e4a0 2021-03-10 stsp err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
224 59d1e4a0 2021-03-10 stsp return err;
226 59d1e4a0 2021-03-10 stsp return NULL;
229 da506691 2019-05-22 stsp static void
230 b48e2ddb 2019-05-22 stsp set_max_datasize(void)
232 b48e2ddb 2019-05-22 stsp struct rlimit rl;
234 b48e2ddb 2019-05-22 stsp if (getrlimit(RLIMIT_DATA, &rl) != 0)
237 b48e2ddb 2019-05-22 stsp rl.rlim_cur = rl.rlim_max;
238 b48e2ddb 2019-05-22 stsp setrlimit(RLIMIT_DATA, &rl);
241 a158c901 2018-12-23 stsp static const struct got_error *
242 711fb6e8 2018-12-23 stsp start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
244 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
245 a158c901 2018-12-23 stsp int imsg_fds[2];
247 a158c901 2018-12-23 stsp struct imsgbuf *ibuf;
249 a158c901 2018-12-23 stsp ibuf = calloc(1, sizeof(*ibuf));
250 a158c901 2018-12-23 stsp if (ibuf == NULL)
251 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
253 a158c901 2018-12-23 stsp pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
254 a158c901 2018-12-23 stsp if (pack->privsep_child == NULL) {
255 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
256 a158c901 2018-12-23 stsp free(ibuf);
257 a158c901 2018-12-23 stsp return err;
260 a158c901 2018-12-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
261 638f9024 2019-05-13 stsp err = got_error_from_errno("socketpair");
265 a158c901 2018-12-23 stsp pid = fork();
266 a158c901 2018-12-23 stsp if (pid == -1) {
267 638f9024 2019-05-13 stsp err = got_error_from_errno("fork");
269 a158c901 2018-12-23 stsp } else if (pid == 0) {
270 b48e2ddb 2019-05-22 stsp set_max_datasize();
271 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
272 a158c901 2018-12-23 stsp pack->path_packfile);
273 a158c901 2018-12-23 stsp /* not reached */
276 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1)
277 638f9024 2019-05-13 stsp return got_error_from_errno("close");
278 a158c901 2018-12-23 stsp pack->privsep_child->imsg_fd = imsg_fds[0];
279 a158c901 2018-12-23 stsp pack->privsep_child->pid = pid;
280 a158c901 2018-12-23 stsp imsg_init(ibuf, imsg_fds[0]);
281 a158c901 2018-12-23 stsp pack->privsep_child->ibuf = ibuf;
283 a158c901 2018-12-23 stsp err = got_privsep_init_pack_child(ibuf, pack, packidx);
285 a158c901 2018-12-23 stsp const struct got_error *child_err;
286 a158c901 2018-12-23 stsp err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
287 a158c901 2018-12-23 stsp child_err = got_privsep_wait_for_child(
288 a158c901 2018-12-23 stsp pack->privsep_child->pid);
289 a158c901 2018-12-23 stsp if (child_err && err == NULL)
290 a158c901 2018-12-23 stsp err = child_err;
294 a158c901 2018-12-23 stsp free(ibuf);
295 a158c901 2018-12-23 stsp free(pack->privsep_child);
296 a158c901 2018-12-23 stsp pack->privsep_child = NULL;
298 a158c901 2018-12-23 stsp return err;
301 711fb6e8 2018-12-23 stsp static const struct got_error *
302 711fb6e8 2018-12-23 stsp read_packed_object_privsep(struct got_object **obj,
303 711fb6e8 2018-12-23 stsp struct got_repository *repo, struct got_pack *pack,
304 711fb6e8 2018-12-23 stsp struct got_packidx *packidx, int idx, struct got_object_id *id)
306 711fb6e8 2018-12-23 stsp const struct got_error *err = NULL;
308 59d1e4a0 2021-03-10 stsp if (pack->privsep_child == NULL) {
309 59d1e4a0 2021-03-10 stsp err = start_pack_privsep_child(pack, packidx);
311 59d1e4a0 2021-03-10 stsp return err;
314 711fb6e8 2018-12-23 stsp return request_packed_object(obj, pack, idx, id);
317 59d1e4a0 2021-03-10 stsp static const struct got_error *
318 59d1e4a0 2021-03-10 stsp read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
319 59d1e4a0 2021-03-10 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
320 59d1e4a0 2021-03-10 stsp struct got_object_id *id)
322 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
324 59d1e4a0 2021-03-10 stsp if (pack->privsep_child == NULL) {
325 59d1e4a0 2021-03-10 stsp err = start_pack_privsep_child(pack, packidx);
327 59d1e4a0 2021-03-10 stsp return err;
330 59d1e4a0 2021-03-10 stsp return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
334 b3d68e7f 2021-07-03 stsp const struct got_error *
335 b3d68e7f 2021-07-03 stsp got_object_open_packed(struct got_object **obj, struct got_object_id *id,
336 2090a03d 2018-09-09 stsp struct got_repository *repo)
338 2090a03d 2018-09-09 stsp const struct got_error *err = NULL;
339 2090a03d 2018-09-09 stsp struct got_pack *pack = NULL;
340 2090a03d 2018-09-09 stsp struct got_packidx *packidx = NULL;
342 2090a03d 2018-09-09 stsp char *path_packfile;
344 2090a03d 2018-09-09 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
346 2090a03d 2018-09-09 stsp return err;
348 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
349 aea75d87 2021-07-06 stsp packidx->path_packidx);
351 2090a03d 2018-09-09 stsp return err;
353 2090a03d 2018-09-09 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
354 2090a03d 2018-09-09 stsp if (pack == NULL) {
355 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
360 a158c901 2018-12-23 stsp err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
364 2090a03d 2018-09-09 stsp free(path_packfile);
365 4558fcd4 2018-01-14 stsp return err;
368 9f2369b0 2018-12-24 stsp static const struct got_error *
369 d5c81d44 2021-07-08 stsp request_object(struct got_object **obj, struct got_object_id *id,
370 d5c81d44 2021-07-08 stsp struct got_repository *repo, int fd)
372 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
373 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
375 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
377 d5c81d44 2021-07-08 stsp err = got_privsep_send_obj_req(ibuf, fd, id);
379 9f2369b0 2018-12-24 stsp return err;
381 9f2369b0 2018-12-24 stsp return got_privsep_recv_obj(obj, ibuf);
384 9f2369b0 2018-12-24 stsp static const struct got_error *
385 59d1e4a0 2021-03-10 stsp request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
386 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo, int infd)
388 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
389 59d1e4a0 2021-03-10 stsp struct imsgbuf *ibuf;
390 59d1e4a0 2021-03-10 stsp int outfd_child;
392 59d1e4a0 2021-03-10 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
394 59d1e4a0 2021-03-10 stsp outfd_child = dup(outfd);
395 59d1e4a0 2021-03-10 stsp if (outfd_child == -1)
396 59d1e4a0 2021-03-10 stsp return got_error_from_errno("dup");
398 d5c81d44 2021-07-08 stsp err = got_privsep_send_raw_obj_req(ibuf, infd, id);
400 59d1e4a0 2021-03-10 stsp return err;
402 59d1e4a0 2021-03-10 stsp err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
404 59d1e4a0 2021-03-10 stsp return err;
406 59d1e4a0 2021-03-10 stsp return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
409 59d1e4a0 2021-03-10 stsp static const struct got_error *
410 59d1e4a0 2021-03-10 stsp start_read_object_child(struct got_repository *repo)
412 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
413 9f2369b0 2018-12-24 stsp int imsg_fds[2];
415 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
417 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
418 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
419 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
421 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
422 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
423 ddc7b220 2019-09-08 stsp free(ibuf);
424 ddc7b220 2019-09-08 stsp return err;
427 9f2369b0 2018-12-24 stsp pid = fork();
428 ddc7b220 2019-09-08 stsp if (pid == -1) {
429 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
430 ddc7b220 2019-09-08 stsp free(ibuf);
431 ddc7b220 2019-09-08 stsp return err;
433 9f2369b0 2018-12-24 stsp else if (pid == 0) {
434 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
435 9f2369b0 2018-12-24 stsp repo->path);
436 9f2369b0 2018-12-24 stsp /* not reached */
439 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
440 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
441 ddc7b220 2019-09-08 stsp free(ibuf);
442 ddc7b220 2019-09-08 stsp return err;
445 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
446 9f2369b0 2018-12-24 stsp imsg_fds[0];
447 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
448 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
449 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
451 59d1e4a0 2021-03-10 stsp return NULL;
454 b3d68e7f 2021-07-03 stsp const struct got_error *
455 b3d68e7f 2021-07-03 stsp got_object_read_header_privsep(struct got_object **obj,
456 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo, int obj_fd)
458 59d1e4a0 2021-03-10 stsp const struct got_error *err;
460 59d1e4a0 2021-03-10 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
461 d5c81d44 2021-07-08 stsp return request_object(obj, id, repo, obj_fd);
463 59d1e4a0 2021-03-10 stsp err = start_read_object_child(repo);
465 7495ec13 2021-04-04 stsp close(obj_fd);
466 59d1e4a0 2021-03-10 stsp return err;
469 d5c81d44 2021-07-08 stsp return request_object(obj, id, repo, obj_fd);
472 59d1e4a0 2021-03-10 stsp static const struct got_error *
473 59d1e4a0 2021-03-10 stsp read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
474 d5c81d44 2021-07-08 stsp int outfd, struct got_object_id *id, struct got_repository *repo,
475 d5c81d44 2021-07-08 stsp int obj_fd)
477 59d1e4a0 2021-03-10 stsp const struct got_error *err;
479 59d1e4a0 2021-03-10 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
480 d5c81d44 2021-07-08 stsp return request_raw_object(outbuf, size, hdrlen, outfd, id,
481 d5c81d44 2021-07-08 stsp repo, obj_fd);
483 59d1e4a0 2021-03-10 stsp err = start_read_object_child(repo);
485 59d1e4a0 2021-03-10 stsp return err;
487 d5c81d44 2021-07-08 stsp return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
491 4558fcd4 2018-01-14 stsp const struct got_error *
492 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
493 4558fcd4 2018-01-14 stsp struct got_object_id *id)
495 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
498 7bb0daa1 2018-06-21 stsp *obj = got_repo_get_cached_object(repo, id);
499 7bb0daa1 2018-06-21 stsp if (*obj != NULL) {
500 7bb0daa1 2018-06-21 stsp (*obj)->refcnt++;
501 7bb0daa1 2018-06-21 stsp return NULL;
504 b3d68e7f 2021-07-03 stsp err = got_object_open_packed(obj, id, repo);
505 e82b1d81 2019-07-27 stsp if (err && err->code != GOT_ERR_NO_OBJ)
506 e82b1d81 2019-07-27 stsp return err;
507 e82b1d81 2019-07-27 stsp if (*obj) {
508 e82b1d81 2019-07-27 stsp (*obj)->refcnt++;
509 e82b1d81 2019-07-27 stsp return got_repo_cache_object(repo, id, *obj);
512 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
514 762d73f4 2021-04-10 stsp if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
515 762d73f4 2021-04-10 stsp err = got_error_no_obj(id);
516 762d73f4 2021-04-10 stsp return err;
519 d5c81d44 2021-07-08 stsp err = got_object_read_header_privsep(obj, id, repo, fd);
521 4558fcd4 2018-01-14 stsp return err;
523 762d73f4 2021-04-10 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
525 59790a32 2018-09-15 stsp (*obj)->refcnt++;
526 762d73f4 2021-04-10 stsp return got_repo_cache_object(repo, id, *obj);
529 8ab9215c 2021-10-15 thomas /* *outfd must be initialized to -1 by caller */
530 59d1e4a0 2021-03-10 stsp const struct got_error *
531 8ab9215c 2021-10-15 thomas got_object_raw_open(struct got_raw_object **obj, int *outfd,
532 f8bb1d3e 2021-10-17 thomas struct got_repository *repo, struct got_object_id *id)
534 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
535 59d1e4a0 2021-03-10 stsp struct got_packidx *packidx = NULL;
537 59d1e4a0 2021-03-10 stsp uint8_t *outbuf = NULL;
538 59d1e4a0 2021-03-10 stsp off_t size = 0;
539 59d1e4a0 2021-03-10 stsp size_t hdrlen = 0;
540 59d1e4a0 2021-03-10 stsp char *path_packfile = NULL;
542 8ab9215c 2021-10-15 thomas *obj = got_repo_get_cached_raw_object(repo, id);
543 8ab9215c 2021-10-15 thomas if (*obj != NULL) {
544 8ab9215c 2021-10-15 thomas (*obj)->refcnt++;
545 8ab9215c 2021-10-15 thomas return NULL;
548 8ab9215c 2021-10-15 thomas if (*outfd == -1) {
549 8ab9215c 2021-10-15 thomas *outfd = got_opentempfd();
550 8ab9215c 2021-10-15 thomas if (*outfd == -1)
551 8ab9215c 2021-10-15 thomas return got_error_from_errno("got_opentempfd");
554 59d1e4a0 2021-03-10 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
555 59d1e4a0 2021-03-10 stsp if (err == NULL) {
556 59d1e4a0 2021-03-10 stsp struct got_pack *pack = NULL;
558 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
559 aea75d87 2021-07-06 stsp packidx->path_packidx);
563 59d1e4a0 2021-03-10 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
564 59d1e4a0 2021-03-10 stsp if (pack == NULL) {
565 59d1e4a0 2021-03-10 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
570 59d1e4a0 2021-03-10 stsp err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
571 8ab9215c 2021-10-15 thomas *outfd, pack, packidx, idx, id);
573 dd3af45a 2021-10-15 thomas goto done;
574 59d1e4a0 2021-03-10 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
577 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
580 8ab9215c 2021-10-15 thomas err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
581 d5c81d44 2021-07-08 stsp id, repo, fd);
583 dd3af45a 2021-10-15 thomas goto done;
586 59d1e4a0 2021-03-10 stsp *obj = calloc(1, sizeof(**obj));
587 59d1e4a0 2021-03-10 stsp if (*obj == NULL) {
588 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("calloc");
592 59d1e4a0 2021-03-10 stsp if (outbuf) {
593 59d1e4a0 2021-03-10 stsp (*obj)->f = fmemopen(outbuf, hdrlen + size, "r");
594 59d1e4a0 2021-03-10 stsp if ((*obj)->f == NULL) {
595 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fdopen");
598 59d1e4a0 2021-03-10 stsp (*obj)->data = outbuf;
600 59d1e4a0 2021-03-10 stsp struct stat sb;
601 8ab9215c 2021-10-15 thomas if (fstat(*outfd, &sb) == -1) {
602 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fstat");
606 a8591711 2021-06-18 stsp if (sb.st_size != hdrlen + size) {
607 59d1e4a0 2021-03-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
611 8ab9215c 2021-10-15 thomas (*obj)->f = fdopen(*outfd, "r");
612 59d1e4a0 2021-03-10 stsp if ((*obj)->f == NULL) {
613 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fdopen");
616 59d1e4a0 2021-03-10 stsp (*obj)->data = NULL;
617 8ab9215c 2021-10-15 thomas *outfd = -1;
619 59d1e4a0 2021-03-10 stsp (*obj)->hdrlen = hdrlen;
620 59d1e4a0 2021-03-10 stsp (*obj)->size = size;
621 8ab9215c 2021-10-15 thomas err = got_repo_cache_raw_object(repo, id, *obj);
623 59d1e4a0 2021-03-10 stsp free(path_packfile);
625 59d1e4a0 2021-03-10 stsp if (*obj) {
626 59d1e4a0 2021-03-10 stsp got_object_raw_close(*obj);
627 59d1e4a0 2021-03-10 stsp *obj = NULL;
629 59d1e4a0 2021-03-10 stsp free(outbuf);
631 8ab9215c 2021-10-15 thomas (*obj)->refcnt++;
632 59d1e4a0 2021-03-10 stsp return err;
635 59d1e4a0 2021-03-10 stsp const struct got_error *
636 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
637 6dfa2fd3 2018-02-12 stsp const char *id_str)
639 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
641 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
642 6dd1ece6 2019-11-10 stsp return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
644 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
647 15a94983 2018-12-23 stsp const struct got_error *
648 15a94983 2018-12-23 stsp got_object_resolve_id_str(struct got_object_id **id,
649 15a94983 2018-12-23 stsp struct got_repository *repo, const char *id_str)
651 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
652 15a94983 2018-12-23 stsp struct got_object *obj;
654 15a94983 2018-12-23 stsp err = got_object_open_by_id_str(&obj, repo, id_str);
656 15a94983 2018-12-23 stsp return err;
658 15a94983 2018-12-23 stsp *id = got_object_id_dup(got_object_get_id(obj));
659 15a94983 2018-12-23 stsp got_object_close(obj);
660 15a94983 2018-12-23 stsp if (*id == NULL)
661 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
663 15a94983 2018-12-23 stsp return NULL;
666 434025f3 2018-09-16 stsp static const struct got_error *
667 a158c901 2018-12-23 stsp request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
668 a158c901 2018-12-23 stsp int pack_idx, struct got_object_id *id)
670 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
672 a158c901 2018-12-23 stsp err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
675 a158c901 2018-12-23 stsp return err;
677 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
679 ca6e02ac 2020-01-07 stsp return err;
681 ca6e02ac 2020-01-07 stsp (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
682 ca6e02ac 2020-01-07 stsp return NULL;
685 a158c901 2018-12-23 stsp static const struct got_error *
686 a158c901 2018-12-23 stsp read_packed_commit_privsep(struct got_commit_object **commit,
687 a158c901 2018-12-23 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
688 a158c901 2018-12-23 stsp struct got_object_id *id)
690 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
692 a158c901 2018-12-23 stsp if (pack->privsep_child)
693 a158c901 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
695 711fb6e8 2018-12-23 stsp err = start_pack_privsep_child(pack, packidx);
697 a158c901 2018-12-23 stsp return err;
699 711fb6e8 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
702 9f2369b0 2018-12-24 stsp static const struct got_error *
703 9f2369b0 2018-12-24 stsp request_commit(struct got_commit_object **commit, struct got_repository *repo,
704 d5c81d44 2021-07-08 stsp int fd, struct got_object_id *id)
706 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
707 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
709 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
711 d5c81d44 2021-07-08 stsp err = got_privsep_send_commit_req(ibuf, fd, id, -1);
713 9f2369b0 2018-12-24 stsp return err;
715 9f2369b0 2018-12-24 stsp return got_privsep_recv_commit(commit, ibuf);
718 9f2369b0 2018-12-24 stsp static const struct got_error *
719 9f2369b0 2018-12-24 stsp read_commit_privsep(struct got_commit_object **commit, int obj_fd,
720 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo)
722 ddc7b220 2019-09-08 stsp const struct got_error *err;
723 9f2369b0 2018-12-24 stsp int imsg_fds[2];
725 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
727 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
728 d5c81d44 2021-07-08 stsp return request_commit(commit, repo, obj_fd, id);
730 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
731 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
732 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
734 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
735 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
736 ddc7b220 2019-09-08 stsp free(ibuf);
737 ddc7b220 2019-09-08 stsp return err;
740 9f2369b0 2018-12-24 stsp pid = fork();
741 ddc7b220 2019-09-08 stsp if (pid == -1) {
742 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
743 ddc7b220 2019-09-08 stsp free(ibuf);
744 ddc7b220 2019-09-08 stsp return err;
746 9f2369b0 2018-12-24 stsp else if (pid == 0) {
747 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
748 9f2369b0 2018-12-24 stsp repo->path);
749 9f2369b0 2018-12-24 stsp /* not reached */
752 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
753 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
754 ddc7b220 2019-09-08 stsp free(ibuf);
755 ddc7b220 2019-09-08 stsp return err;
757 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
758 9f2369b0 2018-12-24 stsp imsg_fds[0];
759 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
760 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
761 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
763 d5c81d44 2021-07-08 stsp return request_commit(commit, repo, obj_fd, id);
767 a158c901 2018-12-23 stsp static const struct got_error *
768 434025f3 2018-09-16 stsp open_commit(struct got_commit_object **commit,
769 1785f84a 2018-12-23 stsp struct got_repository *repo, struct got_object_id *id, int check_cache)
771 434025f3 2018-09-16 stsp const struct got_error *err = NULL;
772 1785f84a 2018-12-23 stsp struct got_packidx *packidx = NULL;
774 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
776 434025f3 2018-09-16 stsp if (check_cache) {
777 1785f84a 2018-12-23 stsp *commit = got_repo_get_cached_commit(repo, id);
778 434025f3 2018-09-16 stsp if (*commit != NULL) {
779 434025f3 2018-09-16 stsp (*commit)->refcnt++;
780 434025f3 2018-09-16 stsp return NULL;
783 434025f3 2018-09-16 stsp *commit = NULL;
785 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
786 e82b1d81 2019-07-27 stsp if (err == NULL) {
787 1785f84a 2018-12-23 stsp struct got_pack *pack = NULL;
789 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
790 aea75d87 2021-07-06 stsp packidx->path_packidx);
792 1785f84a 2018-12-23 stsp return err;
794 1785f84a 2018-12-23 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
795 434025f3 2018-09-16 stsp if (pack == NULL) {
796 1785f84a 2018-12-23 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
801 a158c901 2018-12-23 stsp err = read_packed_commit_privsep(commit, pack,
802 1785f84a 2018-12-23 stsp packidx, idx, id);
803 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
806 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
808 e82b1d81 2019-07-27 stsp return err;
809 d5c81d44 2021-07-08 stsp err = read_commit_privsep(commit, fd, id, repo);
812 434025f3 2018-09-16 stsp if (err == NULL) {
813 434025f3 2018-09-16 stsp (*commit)->refcnt++;
814 1785f84a 2018-12-23 stsp err = got_repo_cache_commit(repo, id, *commit);
817 8d2c5ea3 2019-08-13 stsp free(path_packfile);
818 e32baab7 2018-11-05 stsp return err;
821 e32baab7 2018-11-05 stsp const struct got_error *
822 e32baab7 2018-11-05 stsp got_object_open_as_commit(struct got_commit_object **commit,
823 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object_id *id)
825 e32baab7 2018-11-05 stsp *commit = got_repo_get_cached_commit(repo, id);
826 e32baab7 2018-11-05 stsp if (*commit != NULL) {
827 e32baab7 2018-11-05 stsp (*commit)->refcnt++;
828 e32baab7 2018-11-05 stsp return NULL;
831 1785f84a 2018-12-23 stsp return open_commit(commit, repo, id, 0);
834 e32baab7 2018-11-05 stsp const struct got_error *
835 e32baab7 2018-11-05 stsp got_object_commit_open(struct got_commit_object **commit,
836 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object *obj)
838 1785f84a 2018-12-23 stsp return open_commit(commit, repo, got_object_get_id(obj), 1);
841 434025f3 2018-09-16 stsp const struct got_error *
842 dbc6a6b6 2018-07-12 stsp got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
844 dbc6a6b6 2018-07-12 stsp const struct got_error *err = NULL;
846 dbc6a6b6 2018-07-12 stsp *qid = calloc(1, sizeof(**qid));
847 dbc6a6b6 2018-07-12 stsp if (*qid == NULL)
848 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
850 dbc6a6b6 2018-07-12 stsp (*qid)->id = got_object_id_dup(id);
851 dbc6a6b6 2018-07-12 stsp if ((*qid)->id == NULL) {
852 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
853 fa2f6902 2018-07-23 stsp got_object_qid_free(*qid);
854 dbc6a6b6 2018-07-12 stsp *qid = NULL;
855 dbc6a6b6 2018-07-12 stsp return err;
858 9ca9aafb 2021-06-18 stsp return NULL;
861 9ca9aafb 2021-06-18 stsp const struct got_error *
862 9ca9aafb 2021-06-18 stsp got_object_id_queue_copy(const struct got_object_id_queue *src,
863 9ca9aafb 2021-06-18 stsp struct got_object_id_queue *dest)
865 9ca9aafb 2021-06-18 stsp const struct got_error *err;
866 9ca9aafb 2021-06-18 stsp struct got_object_qid *qid;
868 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, src, entry) {
869 9ca9aafb 2021-06-18 stsp struct got_object_qid *new;
871 9ca9aafb 2021-06-18 stsp * Deep-copy the object ID only. Let the caller deal
872 9ca9aafb 2021-06-18 stsp * with setting up the new->data pointer if needed.
874 9ca9aafb 2021-06-18 stsp err = got_object_qid_alloc(&new, qid->id);
876 9ca9aafb 2021-06-18 stsp got_object_id_queue_free(dest);
877 9ca9aafb 2021-06-18 stsp return err;
879 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(dest, new, entry);
882 dbc6a6b6 2018-07-12 stsp return NULL;
885 a158c901 2018-12-23 stsp static const struct got_error *
886 13c729f7 2018-12-24 stsp request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
887 13c729f7 2018-12-24 stsp int pack_idx, struct got_object_id *id)
889 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
891 13c729f7 2018-12-24 stsp err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
894 a158c901 2018-12-23 stsp return err;
896 a158c901 2018-12-23 stsp return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
899 71eb0e7f 2018-09-16 stsp static const struct got_error *
900 13c729f7 2018-12-24 stsp read_packed_tree_privsep(struct got_tree_object **tree,
901 13c729f7 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
902 13c729f7 2018-12-24 stsp struct got_object_id *id)
904 13c729f7 2018-12-24 stsp const struct got_error *err = NULL;
906 13c729f7 2018-12-24 stsp if (pack->privsep_child)
907 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
909 13c729f7 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
911 13c729f7 2018-12-24 stsp return err;
913 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
916 13c729f7 2018-12-24 stsp static const struct got_error *
917 9f2369b0 2018-12-24 stsp request_tree(struct got_tree_object **tree, struct got_repository *repo,
918 d5c81d44 2021-07-08 stsp int fd, struct got_object_id *id)
920 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
921 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
923 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
925 d5c81d44 2021-07-08 stsp err = got_privsep_send_tree_req(ibuf, fd, id, -1);
927 9f2369b0 2018-12-24 stsp return err;
929 9f2369b0 2018-12-24 stsp return got_privsep_recv_tree(tree, ibuf);
932 9f2369b0 2018-12-24 stsp const struct got_error *
933 9f2369b0 2018-12-24 stsp read_tree_privsep(struct got_tree_object **tree, int obj_fd,
934 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo)
936 ddc7b220 2019-09-08 stsp const struct got_error *err;
937 9f2369b0 2018-12-24 stsp int imsg_fds[2];
939 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
941 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
942 d5c81d44 2021-07-08 stsp return request_tree(tree, repo, obj_fd, id);
944 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
945 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
946 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
948 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
949 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
950 ddc7b220 2019-09-08 stsp free(ibuf);
951 ddc7b220 2019-09-08 stsp return err;
954 9f2369b0 2018-12-24 stsp pid = fork();
955 ddc7b220 2019-09-08 stsp if (pid == -1) {
956 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
957 ddc7b220 2019-09-08 stsp free(ibuf);
958 ddc7b220 2019-09-08 stsp return err;
960 9f2369b0 2018-12-24 stsp else if (pid == 0) {
961 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
962 9f2369b0 2018-12-24 stsp repo->path);
963 9f2369b0 2018-12-24 stsp /* not reached */
966 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
967 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
968 ddc7b220 2019-09-08 stsp free(ibuf);
969 ddc7b220 2019-09-08 stsp return err;
971 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
972 9f2369b0 2018-12-24 stsp imsg_fds[0];
973 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
974 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
975 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
978 d5c81d44 2021-07-08 stsp return request_tree(tree, repo, obj_fd, id);
981 9f2369b0 2018-12-24 stsp static const struct got_error *
982 13c729f7 2018-12-24 stsp open_tree(struct got_tree_object **tree, struct got_repository *repo,
983 13c729f7 2018-12-24 stsp struct got_object_id *id, int check_cache)
985 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
986 13c729f7 2018-12-24 stsp struct got_packidx *packidx = NULL;
988 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
990 71eb0e7f 2018-09-16 stsp if (check_cache) {
991 13c729f7 2018-12-24 stsp *tree = got_repo_get_cached_tree(repo, id);
992 71eb0e7f 2018-09-16 stsp if (*tree != NULL) {
993 71eb0e7f 2018-09-16 stsp (*tree)->refcnt++;
994 71eb0e7f 2018-09-16 stsp return NULL;
997 71eb0e7f 2018-09-16 stsp *tree = NULL;
999 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1000 e82b1d81 2019-07-27 stsp if (err == NULL) {
1001 13c729f7 2018-12-24 stsp struct got_pack *pack = NULL;
1003 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
1004 aea75d87 2021-07-06 stsp packidx->path_packidx);
1006 13c729f7 2018-12-24 stsp return err;
1008 13c729f7 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1009 e7885405 2018-09-10 stsp if (pack == NULL) {
1010 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1013 8d2c5ea3 2019-08-13 stsp goto done;
1015 13c729f7 2018-12-24 stsp err = read_packed_tree_privsep(tree, pack,
1016 13c729f7 2018-12-24 stsp packidx, idx, id);
1017 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1020 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1022 e82b1d81 2019-07-27 stsp return err;
1023 d5c81d44 2021-07-08 stsp err = read_tree_privsep(tree, fd, id, repo);
1026 f6be5c30 2018-06-22 stsp if (err == NULL) {
1027 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
1028 13c729f7 2018-12-24 stsp err = got_repo_cache_tree(repo, id, *tree);
1031 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1032 776d4d29 2018-06-17 stsp return err;
1035 776d4d29 2018-06-17 stsp const struct got_error *
1036 776d4d29 2018-06-17 stsp got_object_open_as_tree(struct got_tree_object **tree,
1037 776d4d29 2018-06-17 stsp struct got_repository *repo, struct got_object_id *id)
1039 e8eb494a 2018-09-16 stsp *tree = got_repo_get_cached_tree(repo, id);
1040 e8eb494a 2018-09-16 stsp if (*tree != NULL) {
1041 e8eb494a 2018-09-16 stsp (*tree)->refcnt++;
1042 e8eb494a 2018-09-16 stsp return NULL;
1045 13c729f7 2018-12-24 stsp return open_tree(tree, repo, id, 0);
1048 71eb0e7f 2018-09-16 stsp const struct got_error *
1049 71eb0e7f 2018-09-16 stsp got_object_tree_open(struct got_tree_object **tree,
1050 71eb0e7f 2018-09-16 stsp struct got_repository *repo, struct got_object *obj)
1052 13c729f7 2018-12-24 stsp return open_tree(tree, repo, got_object_get_id(obj), 1);
1056 56e0773d 2019-11-28 stsp got_object_tree_get_nentries(struct got_tree_object *tree)
1058 56e0773d 2019-11-28 stsp return tree->nentries;
1061 56e0773d 2019-11-28 stsp struct got_tree_entry *
1062 56e0773d 2019-11-28 stsp got_object_tree_get_first_entry(struct got_tree_object *tree)
1064 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, 0);
1067 56e0773d 2019-11-28 stsp struct got_tree_entry *
1068 56e0773d 2019-11-28 stsp got_object_tree_get_last_entry(struct got_tree_object *tree)
1070 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, tree->nentries - 1);
1073 56e0773d 2019-11-28 stsp struct got_tree_entry *
1074 56e0773d 2019-11-28 stsp got_object_tree_get_entry(struct got_tree_object *tree, int i)
1076 56e0773d 2019-11-28 stsp if (i < 0 || i >= tree->nentries)
1077 56e0773d 2019-11-28 stsp return NULL;
1078 56e0773d 2019-11-28 stsp return &tree->entries[i];
1082 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(struct got_tree_entry *te)
1084 56e0773d 2019-11-28 stsp return te->mode;
1087 56e0773d 2019-11-28 stsp const char *
1088 56e0773d 2019-11-28 stsp got_tree_entry_get_name(struct got_tree_entry *te)
1090 56e0773d 2019-11-28 stsp return &te->name[0];
1093 56e0773d 2019-11-28 stsp struct got_object_id *
1094 56e0773d 2019-11-28 stsp got_tree_entry_get_id(struct got_tree_entry *te)
1096 56e0773d 2019-11-28 stsp return &te->id;
1099 0d6c6ee3 2020-05-20 stsp const struct got_error *
1100 af57b12a 2020-07-23 stsp got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1102 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
1103 32596e16 2020-07-23 stsp size_t len, totlen, hdrlen, offset;
1105 aa092692 2020-07-23 stsp *s = NULL;
1107 659dc16e 2020-07-23 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1108 659dc16e 2020-07-23 stsp totlen = 0;
1109 32596e16 2020-07-23 stsp offset = 0;
1113 659dc16e 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1115 af57b12a 2020-07-23 stsp return err;
1117 659dc16e 2020-07-23 stsp if (len == 0)
1120 659dc16e 2020-07-23 stsp totlen += len - hdrlen;
1121 af57b12a 2020-07-23 stsp p = realloc(*s, totlen + 1);
1122 659dc16e 2020-07-23 stsp if (p == NULL) {
1123 659dc16e 2020-07-23 stsp err = got_error_from_errno("realloc");
1125 af57b12a 2020-07-23 stsp *s = NULL;
1126 af57b12a 2020-07-23 stsp return err;
1129 659dc16e 2020-07-23 stsp /* Skip blob object header first time around. */
1130 af57b12a 2020-07-23 stsp memcpy(*s + offset,
1131 f8f7c882 2020-07-23 stsp got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1132 659dc16e 2020-07-23 stsp hdrlen = 0;
1133 32596e16 2020-07-23 stsp offset = totlen;
1134 659dc16e 2020-07-23 stsp } while (len > 0);
1136 af57b12a 2020-07-23 stsp (*s)[totlen] = '\0';
1137 af57b12a 2020-07-23 stsp return NULL;
1140 af57b12a 2020-07-23 stsp const struct got_error *
1141 af57b12a 2020-07-23 stsp got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1142 af57b12a 2020-07-23 stsp struct got_repository *repo)
1144 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
1145 af57b12a 2020-07-23 stsp struct got_blob_object *blob = NULL;
1147 af57b12a 2020-07-23 stsp *link_target = NULL;
1149 af57b12a 2020-07-23 stsp if (!got_object_tree_entry_is_symlink(te))
1150 af57b12a 2020-07-23 stsp return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1152 af57b12a 2020-07-23 stsp err = got_object_open_as_blob(&blob, repo,
1153 af57b12a 2020-07-23 stsp got_tree_entry_get_id(te), PATH_MAX);
1155 af57b12a 2020-07-23 stsp return err;
1157 af57b12a 2020-07-23 stsp err = got_object_blob_read_to_str(link_target, blob);
1158 af57b12a 2020-07-23 stsp got_object_blob_close(blob);
1159 659dc16e 2020-07-23 stsp if (err) {
1160 659dc16e 2020-07-23 stsp free(*link_target);
1161 659dc16e 2020-07-23 stsp *link_target = NULL;
1163 0d6c6ee3 2020-05-20 stsp return err;
1167 56e0773d 2019-11-28 stsp got_tree_entry_get_index(struct got_tree_entry *te)
1169 56e0773d 2019-11-28 stsp return te->idx;
1172 56e0773d 2019-11-28 stsp struct got_tree_entry *
1173 56e0773d 2019-11-28 stsp got_tree_entry_get_next(struct got_tree_object *tree,
1174 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
1176 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx + 1);
1179 56e0773d 2019-11-28 stsp struct got_tree_entry *
1180 56e0773d 2019-11-28 stsp got_tree_entry_get_prev(struct got_tree_object *tree,
1181 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
1183 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx - 1);
1186 3840f4c9 2018-09-12 stsp static const struct got_error *
1187 ac544f8c 2019-01-13 stsp request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1188 ebc55e2d 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1189 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
1191 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
1192 3840f4c9 2018-09-12 stsp int outfd_child;
1193 3840f4c9 2018-09-12 stsp int basefd, accumfd; /* temporary files for delta application */
1195 3840f4c9 2018-09-12 stsp basefd = got_opentempfd();
1196 3840f4c9 2018-09-12 stsp if (basefd == -1)
1197 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
1198 3840f4c9 2018-09-12 stsp accumfd = got_opentempfd();
1199 3840f4c9 2018-09-12 stsp if (accumfd == -1)
1200 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
1202 3840f4c9 2018-09-12 stsp outfd_child = dup(outfd);
1203 3840f4c9 2018-09-12 stsp if (outfd_child == -1)
1204 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1206 ebc55e2d 2018-12-24 stsp err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1208 3840f4c9 2018-09-12 stsp return err;
1210 3840f4c9 2018-09-12 stsp err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1211 3840f4c9 2018-09-12 stsp outfd_child);
1212 3840f4c9 2018-09-12 stsp if (err) {
1213 41496140 2019-02-21 stsp close(basefd);
1214 41496140 2019-02-21 stsp close(accumfd);
1215 3840f4c9 2018-09-12 stsp return err;
1218 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1220 3840f4c9 2018-09-12 stsp if (err) {
1221 3840f4c9 2018-09-12 stsp close(accumfd);
1222 3840f4c9 2018-09-12 stsp return err;
1225 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1228 3840f4c9 2018-09-12 stsp return err;
1230 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen,
1231 ebc55e2d 2018-12-24 stsp pack->privsep_child->ibuf);
1233 3840f4c9 2018-09-12 stsp return err;
1235 3840f4c9 2018-09-12 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1236 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1238 3840f4c9 2018-09-12 stsp return err;
1241 ebc55e2d 2018-12-24 stsp static const struct got_error *
1242 ac544f8c 2019-01-13 stsp read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1243 ac544f8c 2019-01-13 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1244 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
1246 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
1248 ebc55e2d 2018-12-24 stsp if (pack->privsep_child == NULL) {
1249 ebc55e2d 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
1251 ebc55e2d 2018-12-24 stsp return err;
1254 ac544f8c 2019-01-13 stsp return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1258 9f2369b0 2018-12-24 stsp static const struct got_error *
1259 ac544f8c 2019-01-13 stsp request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1260 d5c81d44 2021-07-08 stsp int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1262 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1263 9f2369b0 2018-12-24 stsp int outfd_child;
1265 9f2369b0 2018-12-24 stsp outfd_child = dup(outfd);
1266 9f2369b0 2018-12-24 stsp if (outfd_child == -1)
1267 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1269 d5c81d44 2021-07-08 stsp err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1271 9f2369b0 2018-12-24 stsp return err;
1273 9f2369b0 2018-12-24 stsp err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1275 9f2369b0 2018-12-24 stsp return err;
1277 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1279 9f2369b0 2018-12-24 stsp return err;
1281 9f2369b0 2018-12-24 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1282 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
1284 9f2369b0 2018-12-24 stsp return err;
1287 9f2369b0 2018-12-24 stsp static const struct got_error *
1288 ac544f8c 2019-01-13 stsp read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1289 d5c81d44 2021-07-08 stsp int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1291 ddc7b220 2019-09-08 stsp const struct got_error *err;
1292 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1293 9f2369b0 2018-12-24 stsp pid_t pid;
1294 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1296 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1297 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1298 d5c81d44 2021-07-08 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1302 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1303 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1304 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1306 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1307 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1308 ddc7b220 2019-09-08 stsp free(ibuf);
1309 ddc7b220 2019-09-08 stsp return err;
1312 9f2369b0 2018-12-24 stsp pid = fork();
1313 ddc7b220 2019-09-08 stsp if (pid == -1) {
1314 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1315 ddc7b220 2019-09-08 stsp free(ibuf);
1316 ddc7b220 2019-09-08 stsp return err;
1318 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1319 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1320 9f2369b0 2018-12-24 stsp repo->path);
1321 9f2369b0 2018-12-24 stsp /* not reached */
1324 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
1325 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1326 ddc7b220 2019-09-08 stsp free(ibuf);
1327 ddc7b220 2019-09-08 stsp return err;
1329 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1330 9f2369b0 2018-12-24 stsp imsg_fds[0];
1331 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1332 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1333 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1335 d5c81d44 2021-07-08 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1338 ebc55e2d 2018-12-24 stsp static const struct got_error *
1339 ebc55e2d 2018-12-24 stsp open_blob(struct got_blob_object **blob, struct got_repository *repo,
1340 ebc55e2d 2018-12-24 stsp struct got_object_id *id, size_t blocksize)
1342 ebc55e2d 2018-12-24 stsp const struct got_error *err = NULL;
1343 ebc55e2d 2018-12-24 stsp struct got_packidx *packidx = NULL;
1345 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1346 ac544f8c 2019-01-13 stsp uint8_t *outbuf;
1347 e82b1d81 2019-07-27 stsp int outfd;
1348 ebc55e2d 2018-12-24 stsp size_t size, hdrlen;
1349 ebc55e2d 2018-12-24 stsp struct stat sb;
1351 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
1352 4558fcd4 2018-01-14 stsp if (*blob == NULL)
1353 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1355 55da3778 2018-09-10 stsp outfd = got_opentempfd();
1356 55da3778 2018-09-10 stsp if (outfd == -1)
1357 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
1359 062ebb78 2018-07-12 stsp (*blob)->read_buf = malloc(blocksize);
1360 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
1361 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1362 c7254d79 2018-04-24 stsp goto done;
1365 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1366 e82b1d81 2019-07-27 stsp if (err == NULL) {
1367 ebc55e2d 2018-12-24 stsp struct got_pack *pack = NULL;
1369 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
1370 aea75d87 2021-07-06 stsp packidx->path_packidx);
1372 ebc55e2d 2018-12-24 stsp goto done;
1374 ebc55e2d 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1375 55da3778 2018-09-10 stsp if (pack == NULL) {
1376 ebc55e2d 2018-12-24 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1379 55da3778 2018-09-10 stsp goto done;
1381 ac544f8c 2019-01-13 stsp err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1382 ac544f8c 2019-01-13 stsp pack, packidx, idx, id);
1383 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1386 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&infd, id, repo);
1388 e82b1d81 2019-07-27 stsp goto done;
1389 ac544f8c 2019-01-13 stsp err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1390 d5c81d44 2021-07-08 stsp id, repo);
1393 55da3778 2018-09-10 stsp goto done;
1395 de060dff 2018-12-24 stsp if (hdrlen > size) {
1396 ebc55e2d 2018-12-24 stsp err = got_error(GOT_ERR_BAD_OBJ_HDR);
1397 ebc55e2d 2018-12-24 stsp goto done;
1400 ac544f8c 2019-01-13 stsp if (outbuf) {
1401 08578a35 2021-01-22 stsp if (close(outfd) == -1 && err == NULL)
1402 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1403 ac544f8c 2019-01-13 stsp outfd = -1;
1404 ac544f8c 2019-01-13 stsp (*blob)->f = fmemopen(outbuf, size, "rb");
1405 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1406 638f9024 2019-05-13 stsp err = got_error_from_errno("fmemopen");
1407 ac544f8c 2019-01-13 stsp free(outbuf);
1408 ac544f8c 2019-01-13 stsp goto done;
1410 ac544f8c 2019-01-13 stsp (*blob)->data = outbuf;
1412 ac544f8c 2019-01-13 stsp if (fstat(outfd, &sb) == -1) {
1413 638f9024 2019-05-13 stsp err = got_error_from_errno("fstat");
1414 ac544f8c 2019-01-13 stsp goto done;
1417 ac544f8c 2019-01-13 stsp if (sb.st_size != size) {
1418 ac544f8c 2019-01-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1419 ac544f8c 2019-01-13 stsp goto done;
1422 ac544f8c 2019-01-13 stsp (*blob)->f = fdopen(outfd, "rb");
1423 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1424 638f9024 2019-05-13 stsp err = got_error_from_errno("fdopen");
1425 ac544f8c 2019-01-13 stsp close(outfd);
1426 ac544f8c 2019-01-13 stsp outfd = -1;
1427 ac544f8c 2019-01-13 stsp goto done;
1431 ebc55e2d 2018-12-24 stsp (*blob)->hdrlen = hdrlen;
1432 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
1433 ebc55e2d 2018-12-24 stsp memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1436 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1437 55da3778 2018-09-10 stsp if (err) {
1438 55da3778 2018-09-10 stsp if (*blob) {
1439 7baf5860 2019-03-19 stsp got_object_blob_close(*blob);
1440 55da3778 2018-09-10 stsp *blob = NULL;
1441 55da3778 2018-09-10 stsp } else if (outfd != -1)
1442 55da3778 2018-09-10 stsp close(outfd);
1444 a19581a2 2018-06-21 stsp return err;
1447 a19581a2 2018-06-21 stsp const struct got_error *
1448 a19581a2 2018-06-21 stsp got_object_open_as_blob(struct got_blob_object **blob,
1449 a19581a2 2018-06-21 stsp struct got_repository *repo, struct got_object_id *id,
1450 a19581a2 2018-06-21 stsp size_t blocksize)
1452 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, id, blocksize);
1455 ebc55e2d 2018-12-24 stsp const struct got_error *
1456 ebc55e2d 2018-12-24 stsp got_object_blob_open(struct got_blob_object **blob,
1457 ebc55e2d 2018-12-24 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
1459 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1462 fb43ecf1 2019-02-11 stsp const struct got_error *
1463 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
1465 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL;
1466 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
1467 56b63ca4 2021-01-22 stsp if (blob->f && fclose(blob->f) == EOF)
1468 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1469 ac544f8c 2019-01-13 stsp free(blob->data);
1470 68482ea3 2017-11-27 stsp free(blob);
1471 fb43ecf1 2019-02-11 stsp return err;
1475 8ba819a3 2020-07-23 stsp got_object_blob_rewind(struct got_blob_object *blob)
1477 8ba819a3 2020-07-23 stsp if (blob->f)
1478 8ba819a3 2020-07-23 stsp rewind(blob->f);
1482 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1484 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1488 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
1490 f934cf2c 2018-02-12 stsp return blob->hdrlen;
1493 f934cf2c 2018-02-12 stsp const uint8_t *
1494 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
1496 f934cf2c 2018-02-12 stsp return blob->read_buf;
1499 68482ea3 2017-11-27 stsp const struct got_error *
1500 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1504 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1505 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
1506 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
1507 eb651edf 2018-02-11 stsp *outlenp = n;
1508 35e9ba5d 2018-06-21 stsp return NULL;
1511 35e9ba5d 2018-06-21 stsp const struct got_error *
1512 be659d10 2020-11-18 stsp got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1513 6c4c42e0 2019-06-24 stsp off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1515 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
1516 b6752625 2018-12-24 stsp size_t n, len, hdrlen;
1517 84451b3e 2018-07-10 stsp const uint8_t *buf;
1519 c33ebc60 2020-11-18 stsp const int alloc_chunksz = 512;
1520 c33ebc60 2020-11-18 stsp size_t nalloc = 0;
1521 f595d9bd 2019-08-14 stsp off_t off = 0, total_len = 0;
1523 6c4c42e0 2019-06-24 stsp if (line_offsets)
1524 6c4c42e0 2019-06-24 stsp *line_offsets = NULL;
1525 f595d9bd 2019-08-14 stsp if (filesize)
1526 f595d9bd 2019-08-14 stsp *filesize = 0;
1527 84451b3e 2018-07-10 stsp if (nlines)
1528 84451b3e 2018-07-10 stsp *nlines = 0;
1530 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1532 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
1534 35e9ba5d 2018-06-21 stsp return err;
1535 35e9ba5d 2018-06-21 stsp if (len == 0)
1537 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
1538 b02560ec 2019-08-19 stsp i = hdrlen;
1539 f1cbc3bc 2020-11-18 stsp if (nlines) {
1540 f1cbc3bc 2020-11-18 stsp if (line_offsets && *line_offsets == NULL) {
1541 78695fb7 2019-08-12 stsp /* Have some data but perhaps no '\n'. */
1542 78695fb7 2019-08-12 stsp *nlines = 1;
1543 c33ebc60 2020-11-18 stsp nalloc = alloc_chunksz;
1544 c33ebc60 2020-11-18 stsp *line_offsets = calloc(nalloc,
1545 c33ebc60 2020-11-18 stsp sizeof(**line_offsets));
1546 78695fb7 2019-08-12 stsp if (*line_offsets == NULL)
1547 845785d4 2020-02-02 tracey return got_error_from_errno("calloc");
1549 b02560ec 2019-08-19 stsp /* Skip forward over end of first line. */
1550 b02560ec 2019-08-19 stsp while (i < len) {
1551 b02560ec 2019-08-19 stsp if (buf[i] == '\n')
1556 b02560ec 2019-08-19 stsp /* Scan '\n' offsets in remaining chunk of data. */
1557 b02560ec 2019-08-19 stsp while (i < len) {
1558 b02560ec 2019-08-19 stsp if (buf[i] != '\n') {
1562 f595d9bd 2019-08-14 stsp (*nlines)++;
1563 c33ebc60 2020-11-18 stsp if (line_offsets && nalloc < *nlines) {
1564 c33ebc60 2020-11-18 stsp size_t n = *nlines + alloc_chunksz;
1565 78695fb7 2019-08-12 stsp off_t *o = recallocarray(*line_offsets,
1566 c33ebc60 2020-11-18 stsp nalloc, n, sizeof(**line_offsets));
1567 78695fb7 2019-08-12 stsp if (o == NULL) {
1568 78695fb7 2019-08-12 stsp free(*line_offsets);
1569 78695fb7 2019-08-12 stsp *line_offsets = NULL;
1570 78695fb7 2019-08-12 stsp return got_error_from_errno(
1571 78695fb7 2019-08-12 stsp "recallocarray");
1573 78695fb7 2019-08-12 stsp *line_offsets = o;
1574 c33ebc60 2020-11-18 stsp nalloc = n;
1576 f1cbc3bc 2020-11-18 stsp if (line_offsets) {
1577 f1cbc3bc 2020-11-18 stsp off = total_len + i - hdrlen + 1;
1578 f1cbc3bc 2020-11-18 stsp (*line_offsets)[*nlines - 1] = off;
1583 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
1584 454a6b59 2018-12-24 stsp n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1585 b6752625 2018-12-24 stsp if (n != len - hdrlen)
1586 b6752625 2018-12-24 stsp return got_ferror(outfile, GOT_ERR_IO);
1587 f595d9bd 2019-08-14 stsp total_len += len - hdrlen;
1588 35e9ba5d 2018-06-21 stsp hdrlen = 0;
1589 35e9ba5d 2018-06-21 stsp } while (len != 0);
1591 cbe7f848 2019-02-11 stsp if (fflush(outfile) != 0)
1592 638f9024 2019-05-13 stsp return got_error_from_errno("fflush");
1593 35e9ba5d 2018-06-21 stsp rewind(outfile);
1595 f595d9bd 2019-08-14 stsp if (filesize)
1596 f595d9bd 2019-08-14 stsp *filesize = total_len;
1598 776d4d29 2018-06-17 stsp return NULL;
1601 f4a881ce 2018-11-17 stsp static const struct got_error *
1602 268f7291 2018-12-24 stsp request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1603 268f7291 2018-12-24 stsp int pack_idx, struct got_object_id *id)
1605 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
1607 268f7291 2018-12-24 stsp err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1608 268f7291 2018-12-24 stsp pack_idx);
1610 a158c901 2018-12-23 stsp return err;
1612 a158c901 2018-12-23 stsp return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1615 268f7291 2018-12-24 stsp static const struct got_error *
1616 268f7291 2018-12-24 stsp read_packed_tag_privsep(struct got_tag_object **tag,
1617 268f7291 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1618 268f7291 2018-12-24 stsp struct got_object_id *id)
1620 268f7291 2018-12-24 stsp const struct got_error *err = NULL;
1622 268f7291 2018-12-24 stsp if (pack->privsep_child)
1623 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1625 268f7291 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
1627 268f7291 2018-12-24 stsp return err;
1629 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1632 9f2369b0 2018-12-24 stsp static const struct got_error *
1633 9f2369b0 2018-12-24 stsp request_tag(struct got_tag_object **tag, struct got_repository *repo,
1634 d5c81d44 2021-07-08 stsp int fd, struct got_object_id *id)
1636 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1637 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1639 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1641 d5c81d44 2021-07-08 stsp err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1643 9f2369b0 2018-12-24 stsp return err;
1645 9f2369b0 2018-12-24 stsp return got_privsep_recv_tag(tag, ibuf);
1648 9f2369b0 2018-12-24 stsp static const struct got_error *
1649 9f2369b0 2018-12-24 stsp read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1650 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo)
1652 ddc7b220 2019-09-08 stsp const struct got_error *err;
1653 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1654 9f2369b0 2018-12-24 stsp pid_t pid;
1655 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1657 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1658 d5c81d44 2021-07-08 stsp return request_tag(tag, repo, obj_fd, id);
1660 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1661 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1662 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1664 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1665 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1666 ddc7b220 2019-09-08 stsp free(ibuf);
1667 ddc7b220 2019-09-08 stsp return err;
1670 9f2369b0 2018-12-24 stsp pid = fork();
1671 ddc7b220 2019-09-08 stsp if (pid == -1) {
1672 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1673 ddc7b220 2019-09-08 stsp free(ibuf);
1674 ddc7b220 2019-09-08 stsp return err;
1676 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1677 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1678 9f2369b0 2018-12-24 stsp repo->path);
1679 9f2369b0 2018-12-24 stsp /* not reached */
1682 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
1683 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1684 ddc7b220 2019-09-08 stsp free(ibuf);
1685 ddc7b220 2019-09-08 stsp return err;
1687 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1688 9f2369b0 2018-12-24 stsp imsg_fds[0];
1689 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1690 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1691 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1693 d5c81d44 2021-07-08 stsp return request_tag(tag, repo, obj_fd, id);
1696 a158c901 2018-12-23 stsp static const struct got_error *
1697 268f7291 2018-12-24 stsp open_tag(struct got_tag_object **tag, struct got_repository *repo,
1698 268f7291 2018-12-24 stsp struct got_object_id *id, int check_cache)
1700 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1701 268f7291 2018-12-24 stsp struct got_packidx *packidx = NULL;
1703 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1704 5d844a1e 2019-08-13 stsp struct got_object *obj = NULL;
1705 5d844a1e 2019-08-13 stsp int obj_type = GOT_OBJ_TYPE_ANY;
1707 f4a881ce 2018-11-17 stsp if (check_cache) {
1708 268f7291 2018-12-24 stsp *tag = got_repo_get_cached_tag(repo, id);
1709 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1710 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1711 f4a881ce 2018-11-17 stsp return NULL;
1714 f4a881ce 2018-11-17 stsp *tag = NULL;
1716 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1717 e82b1d81 2019-07-27 stsp if (err == NULL) {
1718 268f7291 2018-12-24 stsp struct got_pack *pack = NULL;
1720 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
1721 aea75d87 2021-07-06 stsp packidx->path_packidx);
1723 268f7291 2018-12-24 stsp return err;
1725 268f7291 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1726 f4a881ce 2018-11-17 stsp if (pack == NULL) {
1727 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1730 8d2c5ea3 2019-08-13 stsp goto done;
1733 992eb9d8 2020-02-07 tracey /* Beware of "lightweight" tags: Check object type first. */
1734 5d844a1e 2019-08-13 stsp err = read_packed_object_privsep(&obj, repo, pack, packidx,
1737 5d844a1e 2019-08-13 stsp goto done;
1738 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1739 5d844a1e 2019-08-13 stsp got_object_close(obj);
1740 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG) {
1741 5d844a1e 2019-08-13 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1742 5d844a1e 2019-08-13 stsp goto done;
1744 5d844a1e 2019-08-13 stsp err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1745 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1748 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1750 e82b1d81 2019-07-27 stsp return err;
1751 d5c81d44 2021-07-08 stsp err = got_object_read_header_privsep(&obj, id, repo, fd);
1753 5d844a1e 2019-08-13 stsp return err;
1754 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1755 5d844a1e 2019-08-13 stsp got_object_close(obj);
1756 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
1757 5d844a1e 2019-08-13 stsp return got_error(GOT_ERR_OBJ_TYPE);
1759 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1761 5d844a1e 2019-08-13 stsp return err;
1762 d5c81d44 2021-07-08 stsp err = read_tag_privsep(tag, fd, id, repo);
1765 f4a881ce 2018-11-17 stsp if (err == NULL) {
1766 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1767 268f7291 2018-12-24 stsp err = got_repo_cache_tag(repo, id, *tag);
1770 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1771 f4a881ce 2018-11-17 stsp return err;
1774 f4a881ce 2018-11-17 stsp const struct got_error *
1775 f4a881ce 2018-11-17 stsp got_object_open_as_tag(struct got_tag_object **tag,
1776 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object_id *id)
1778 f4a881ce 2018-11-17 stsp *tag = got_repo_get_cached_tag(repo, id);
1779 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1780 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1781 f4a881ce 2018-11-17 stsp return NULL;
1784 268f7291 2018-12-24 stsp return open_tag(tag, repo, id, 0);
1787 f4a881ce 2018-11-17 stsp const struct got_error *
1788 f4a881ce 2018-11-17 stsp got_object_tag_open(struct got_tag_object **tag,
1789 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object *obj)
1791 268f7291 2018-12-24 stsp return open_tag(tag, repo, got_object_get_id(obj), 1);
1794 d24820bf 2019-08-11 stsp const char *
1795 d24820bf 2019-08-11 stsp got_object_tag_get_name(struct got_tag_object *tag)
1797 d24820bf 2019-08-11 stsp return tag->tag;
1801 0bd18d37 2019-02-01 stsp got_object_tag_get_object_type(struct got_tag_object *tag)
1803 0bd18d37 2019-02-01 stsp return tag->obj_type;
1806 0bd18d37 2019-02-01 stsp struct got_object_id *
1807 0bd18d37 2019-02-01 stsp got_object_tag_get_object_id(struct got_tag_object *tag)
1809 0bd18d37 2019-02-01 stsp return &tag->id;
1813 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_time(struct got_tag_object *tag)
1815 01073a5d 2019-08-22 stsp return tag->tagger_time;
1819 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1821 01073a5d 2019-08-22 stsp return tag->tagger_gmtoff;
1824 01073a5d 2019-08-22 stsp const char *
1825 01073a5d 2019-08-22 stsp got_object_tag_get_tagger(struct got_tag_object *tag)
1827 01073a5d 2019-08-22 stsp return tag->tagger;
1830 01073a5d 2019-08-22 stsp const char *
1831 01073a5d 2019-08-22 stsp got_object_tag_get_message(struct got_tag_object *tag)
1833 01073a5d 2019-08-22 stsp return tag->tagmsg;
1836 776d4d29 2018-06-17 stsp static struct got_tree_entry *
1837 65a9bbe9 2018-09-15 stsp find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1841 63da309a 2018-11-07 stsp /* Note that tree entries are sorted in strncmp() order. */
1842 56e0773d 2019-11-28 stsp for (i = 0; i < tree->nentries; i++) {
1843 56e0773d 2019-11-28 stsp struct got_tree_entry *te = &tree->entries[i];
1844 63da309a 2018-11-07 stsp int cmp = strncmp(te->name, name, len);
1845 63da309a 2018-11-07 stsp if (cmp < 0)
1847 63da309a 2018-11-07 stsp if (cmp > 0)
1849 63da309a 2018-11-07 stsp if (te->name[len] == '\0')
1850 776d4d29 2018-06-17 stsp return te;
1852 eb651edf 2018-02-11 stsp return NULL;
1855 56e0773d 2019-11-28 stsp struct got_tree_entry *
1856 a129376b 2019-03-28 stsp got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1858 a129376b 2019-03-28 stsp return find_entry_by_name(tree, name, strlen(name));
1861 776d4d29 2018-06-17 stsp const struct got_error *
1862 cc8021af 2021-10-12 thomas got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1863 cc8021af 2021-10-12 thomas struct got_repository *repo, struct got_tree_object *tree,
1864 cc8021af 2021-10-12 thomas const char *path)
1866 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
1867 cc8021af 2021-10-12 thomas struct got_tree_object *subtree = NULL;
1868 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
1869 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1870 b7cd37e5 2018-11-18 stsp size_t seglen;
1872 27d434c2 2018-09-15 stsp *id = NULL;
1875 5e54fb30 2019-05-31 stsp while (s[0] == '/')
1878 65a9bbe9 2018-09-15 stsp seglen = 0;
1879 cc8021af 2021-10-12 thomas subtree = tree;
1880 b7cd37e5 2018-11-18 stsp while (*s) {
1881 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
1883 776d4d29 2018-06-17 stsp if (*s != '/') {
1890 cc8021af 2021-10-12 thomas te = find_entry_by_name(subtree, seg, seglen);
1891 db37e2c0 2018-06-21 stsp if (te == NULL) {
1892 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1893 776d4d29 2018-06-17 stsp goto done;
1896 b7cd37e5 2018-11-18 stsp if (*s == '\0')
1899 776d4d29 2018-06-17 stsp seg = s + 1;
1900 65a9bbe9 2018-09-15 stsp seglen = 0;
1903 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
1905 db37e2c0 2018-06-21 stsp te = NULL;
1907 776d4d29 2018-06-17 stsp goto done;
1908 cc8021af 2021-10-12 thomas if (subtree != tree)
1909 cc8021af 2021-10-12 thomas got_object_tree_close(subtree);
1910 cc8021af 2021-10-12 thomas subtree = next_tree;
1915 56e0773d 2019-11-28 stsp *id = got_object_id_dup(&te->id);
1916 27d434c2 2018-09-15 stsp if (*id == NULL)
1917 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
1918 cc8021af 2021-10-12 thomas if (mode)
1919 cc8021af 2021-10-12 thomas *mode = te->mode;
1921 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1923 cc8021af 2021-10-12 thomas if (subtree && subtree != tree)
1924 cc8021af 2021-10-12 thomas got_object_tree_close(subtree);
1925 cc8021af 2021-10-12 thomas return err;
1927 cc8021af 2021-10-12 thomas const struct got_error *
1928 cc8021af 2021-10-12 thomas got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1929 cc8021af 2021-10-12 thomas struct got_object_id *commit_id, const char *path)
1931 cc8021af 2021-10-12 thomas const struct got_error *err = NULL;
1932 cc8021af 2021-10-12 thomas struct got_commit_object *commit = NULL;
1933 cc8021af 2021-10-12 thomas struct got_tree_object *tree = NULL;
1935 cc8021af 2021-10-12 thomas *id = NULL;
1937 cc8021af 2021-10-12 thomas err = got_object_open_as_commit(&commit, repo, commit_id);
1938 cc8021af 2021-10-12 thomas if (err)
1939 cc8021af 2021-10-12 thomas goto done;
1941 cc8021af 2021-10-12 thomas /* Handle opening of root of commit's tree. */
1942 cc8021af 2021-10-12 thomas if (got_path_is_root_dir(path)) {
1943 cc8021af 2021-10-12 thomas *id = got_object_id_dup(commit->tree_id);
1944 cc8021af 2021-10-12 thomas if (*id == NULL)
1945 cc8021af 2021-10-12 thomas err = got_error_from_errno("got_object_id_dup");
1946 cc8021af 2021-10-12 thomas } else {
1947 cc8021af 2021-10-12 thomas err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1948 cc8021af 2021-10-12 thomas if (err)
1949 cc8021af 2021-10-12 thomas goto done;
1950 cc8021af 2021-10-12 thomas err = got_object_tree_find_path(id, NULL, repo, tree, path);
1953 776d4d29 2018-06-17 stsp if (commit)
1954 776d4d29 2018-06-17 stsp got_object_commit_close(commit);
1956 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
1957 776d4d29 2018-06-17 stsp return err;
1961 ac5f2b26 2020-05-05 stsp * Normalize file mode bits to avoid false positive tree entry differences
1962 ac5f2b26 2020-05-05 stsp * in case tree entries have unexpected mode bits set.
1964 ac5f2b26 2020-05-05 stsp static mode_t
1965 ac5f2b26 2020-05-05 stsp normalize_mode_for_comparison(mode_t mode)
1968 ac5f2b26 2020-05-05 stsp * For directories, the only relevant bit is the IFDIR bit.
1969 ac5f2b26 2020-05-05 stsp * This allows us to detect paths changing from a directory
1970 ac5f2b26 2020-05-05 stsp * to a file and vice versa.
1972 ac5f2b26 2020-05-05 stsp if (S_ISDIR(mode))
1973 ac5f2b26 2020-05-05 stsp return mode & S_IFDIR;
1976 40dde666 2020-07-23 stsp * For symlinks, the only relevant bit is the IFLNK bit.
1977 40dde666 2020-07-23 stsp * This allows us to detect paths changing from a symlinks
1978 40dde666 2020-07-23 stsp * to a file or directory and vice versa.
1980 40dde666 2020-07-23 stsp if (S_ISLNK(mode))
1981 40dde666 2020-07-23 stsp return mode & S_IFLNK;
1983 ac5f2b26 2020-05-05 stsp /* For files, the only change we care about is the executable bit. */
1984 ac5f2b26 2020-05-05 stsp return mode & S_IXUSR;
1987 07862c20 2018-09-15 stsp const struct got_error *
1988 07862c20 2018-09-15 stsp got_object_tree_path_changed(int *changed,
1989 07862c20 2018-09-15 stsp struct got_tree_object *tree01, struct got_tree_object *tree02,
1990 07862c20 2018-09-15 stsp const char *path, struct got_repository *repo)
1992 07862c20 2018-09-15 stsp const struct got_error *err = NULL;
1993 07862c20 2018-09-15 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1994 07862c20 2018-09-15 stsp struct got_tree_entry *te1 = NULL, *te2 = NULL;
1995 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1996 3b7f9878 2018-11-18 stsp size_t seglen;
1998 07862c20 2018-09-15 stsp *changed = 0;
2000 07862c20 2018-09-15 stsp /* We not do support comparing the root path. */
2001 61a7d79f 2020-02-29 stsp if (got_path_is_root_dir(path))
2002 63f810e6 2020-02-29 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
2004 07862c20 2018-09-15 stsp tree1 = tree01;
2005 07862c20 2018-09-15 stsp tree2 = tree02;
2007 61a7d79f 2020-02-29 stsp while (*s == '/')
2010 65a9bbe9 2018-09-15 stsp seglen = 0;
2011 3b7f9878 2018-11-18 stsp while (*s) {
2012 07862c20 2018-09-15 stsp struct got_tree_object *next_tree1, *next_tree2;
2013 ac5f2b26 2020-05-05 stsp mode_t mode1, mode2;
2015 07862c20 2018-09-15 stsp if (*s != '/') {
2022 65a9bbe9 2018-09-15 stsp te1 = find_entry_by_name(tree1, seg, seglen);
2023 07862c20 2018-09-15 stsp if (te1 == NULL) {
2024 07862c20 2018-09-15 stsp err = got_error(GOT_ERR_NO_OBJ);
2025 07862c20 2018-09-15 stsp goto done;
2028 e8bfb8f3 2020-12-18 stsp if (tree2)
2029 e8bfb8f3 2020-12-18 stsp te2 = find_entry_by_name(tree2, seg, seglen);
2031 e8bfb8f3 2020-12-18 stsp if (te2) {
2032 e8bfb8f3 2020-12-18 stsp mode1 = normalize_mode_for_comparison(te1->mode);
2033 e8bfb8f3 2020-12-18 stsp mode2 = normalize_mode_for_comparison(te2->mode);
2034 e8bfb8f3 2020-12-18 stsp if (mode1 != mode2) {
2035 e8bfb8f3 2020-12-18 stsp *changed = 1;
2036 e8bfb8f3 2020-12-18 stsp goto done;
2039 e8bfb8f3 2020-12-18 stsp if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2040 e8bfb8f3 2020-12-18 stsp *changed = 0;
2041 e8bfb8f3 2020-12-18 stsp goto done;
2045 3b7f9878 2018-11-18 stsp if (*s == '\0') { /* final path element */
2046 07862c20 2018-09-15 stsp *changed = 1;
2047 07862c20 2018-09-15 stsp goto done;
2050 07862c20 2018-09-15 stsp seg = s + 1;
2052 65a9bbe9 2018-09-15 stsp seglen = 0;
2054 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree1, repo,
2055 56e0773d 2019-11-28 stsp &te1->id);
2056 07862c20 2018-09-15 stsp te1 = NULL;
2058 07862c20 2018-09-15 stsp goto done;
2059 a31cea73 2018-09-15 stsp if (tree1 != tree01)
2060 a31cea73 2018-09-15 stsp got_object_tree_close(tree1);
2061 07862c20 2018-09-15 stsp tree1 = next_tree1;
2063 e8bfb8f3 2020-12-18 stsp if (te2) {
2064 e8bfb8f3 2020-12-18 stsp err = got_object_open_as_tree(&next_tree2, repo,
2065 e8bfb8f3 2020-12-18 stsp &te2->id);
2066 e8bfb8f3 2020-12-18 stsp te2 = NULL;
2068 e8bfb8f3 2020-12-18 stsp goto done;
2069 e8bfb8f3 2020-12-18 stsp if (tree2 != tree02)
2070 e8bfb8f3 2020-12-18 stsp got_object_tree_close(tree2);
2071 e8bfb8f3 2020-12-18 stsp tree2 = next_tree2;
2072 e8bfb8f3 2020-12-18 stsp } else if (tree2) {
2073 e8bfb8f3 2020-12-18 stsp if (tree2 != tree02)
2074 e8bfb8f3 2020-12-18 stsp got_object_tree_close(tree2);
2075 e8bfb8f3 2020-12-18 stsp tree2 = NULL;
2080 a31cea73 2018-09-15 stsp if (tree1 && tree1 != tree01)
2081 07862c20 2018-09-15 stsp got_object_tree_close(tree1);
2082 a31cea73 2018-09-15 stsp if (tree2 && tree2 != tree02)
2083 07862c20 2018-09-15 stsp got_object_tree_close(tree2);
2084 77880158 2018-11-04 stsp return err;
2087 ed175427 2019-05-09 stsp const struct got_error *
2088 ed175427 2019-05-09 stsp got_object_tree_entry_dup(struct got_tree_entry **new_te,
2089 ed175427 2019-05-09 stsp struct got_tree_entry *te)
2091 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
2093 ed175427 2019-05-09 stsp *new_te = calloc(1, sizeof(**new_te));
2094 ed175427 2019-05-09 stsp if (*new_te == NULL)
2095 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
2097 ed175427 2019-05-09 stsp (*new_te)->mode = te->mode;
2098 56e0773d 2019-11-28 stsp memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2099 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2100 8c4eabf2 2019-05-10 stsp return err;
2104 56e0773d 2019-11-28 stsp got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2106 63c5ca5d 2019-08-24 stsp return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2110 e40622f4 2020-07-23 stsp got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2112 e40622f4 2020-07-23 stsp /* S_IFDIR check avoids confusing symlinks with submodules. */
2113 e40622f4 2020-07-23 stsp return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2116 e40622f4 2020-07-23 stsp static const struct got_error *
2117 e40622f4 2020-07-23 stsp resolve_symlink(char **link_target, const char *path,
2118 e40622f4 2020-07-23 stsp struct got_object_id *commit_id, struct got_repository *repo)
2120 e40622f4 2020-07-23 stsp const struct got_error *err = NULL;
2121 dbdd6209 2020-10-19 stsp char buf[PATH_MAX];
2122 e40622f4 2020-07-23 stsp char *name, *parent_path = NULL;
2123 e40622f4 2020-07-23 stsp struct got_object_id *tree_obj_id = NULL;
2124 e40622f4 2020-07-23 stsp struct got_tree_object *tree = NULL;
2125 e40622f4 2020-07-23 stsp struct got_tree_entry *te = NULL;
2127 e40622f4 2020-07-23 stsp *link_target = NULL;
2129 dbdd6209 2020-10-19 stsp if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2130 dbdd6209 2020-10-19 stsp return got_error(GOT_ERR_NO_SPACE);
2132 dbdd6209 2020-10-19 stsp name = basename(buf);
2133 e40622f4 2020-07-23 stsp if (name == NULL)
2134 e40622f4 2020-07-23 stsp return got_error_from_errno2("basename", path);
2136 e40622f4 2020-07-23 stsp err = got_path_dirname(&parent_path, path);
2138 e40622f4 2020-07-23 stsp return err;
2140 e40622f4 2020-07-23 stsp err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
2141 e40622f4 2020-07-23 stsp parent_path);
2142 e40622f4 2020-07-23 stsp if (err) {
2143 e40622f4 2020-07-23 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2144 e40622f4 2020-07-23 stsp /* Display the complete path in error message. */
2145 e40622f4 2020-07-23 stsp err = got_error_path(path, err->code);
2147 e40622f4 2020-07-23 stsp goto done;
2150 e40622f4 2020-07-23 stsp err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2152 e40622f4 2020-07-23 stsp goto done;
2154 e40622f4 2020-07-23 stsp te = got_object_tree_find_entry(tree, name);
2155 e40622f4 2020-07-23 stsp if (te == NULL) {
2156 e40622f4 2020-07-23 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2157 e40622f4 2020-07-23 stsp goto done;
2160 e40622f4 2020-07-23 stsp if (got_object_tree_entry_is_symlink(te)) {
2161 e40622f4 2020-07-23 stsp err = got_tree_entry_get_symlink_target(link_target, te, repo);
2163 e40622f4 2020-07-23 stsp goto done;
2164 e40622f4 2020-07-23 stsp if (!got_path_is_absolute(*link_target)) {
2165 e40622f4 2020-07-23 stsp char *abspath;
2166 e40622f4 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", parent_path,
2167 e40622f4 2020-07-23 stsp *link_target) == -1) {
2168 e40622f4 2020-07-23 stsp err = got_error_from_errno("asprintf");
2169 e40622f4 2020-07-23 stsp goto done;
2171 e40622f4 2020-07-23 stsp free(*link_target);
2172 e40622f4 2020-07-23 stsp *link_target = malloc(PATH_MAX);
2173 e40622f4 2020-07-23 stsp if (*link_target == NULL) {
2174 e40622f4 2020-07-23 stsp err = got_error_from_errno("malloc");
2175 e40622f4 2020-07-23 stsp goto done;
2177 e40622f4 2020-07-23 stsp err = got_canonpath(abspath, *link_target, PATH_MAX);
2178 e40622f4 2020-07-23 stsp free(abspath);
2180 e40622f4 2020-07-23 stsp goto done;
2184 e40622f4 2020-07-23 stsp free(tree_obj_id);
2186 e40622f4 2020-07-23 stsp got_object_tree_close(tree);
2187 e40622f4 2020-07-23 stsp if (err) {
2188 e40622f4 2020-07-23 stsp free(*link_target);
2189 e40622f4 2020-07-23 stsp *link_target = NULL;
2191 e40622f4 2020-07-23 stsp return err;
2194 ca6e02ac 2020-01-07 stsp const struct got_error *
2195 e40622f4 2020-07-23 stsp got_object_resolve_symlinks(char **link_target, const char *path,
2196 e40622f4 2020-07-23 stsp struct got_object_id *commit_id, struct got_repository *repo)
2198 e40622f4 2020-07-23 stsp const struct got_error *err = NULL;
2199 e40622f4 2020-07-23 stsp char *next_target = NULL;
2200 e40622f4 2020-07-23 stsp int max_recursion = 40; /* matches Git */
2202 e40622f4 2020-07-23 stsp *link_target = NULL;
2205 e40622f4 2020-07-23 stsp err = resolve_symlink(&next_target,
2206 e40622f4 2020-07-23 stsp *link_target ? *link_target : path, commit_id, repo);
2209 e40622f4 2020-07-23 stsp if (next_target) {
2210 e40622f4 2020-07-23 stsp free(*link_target);
2211 e40622f4 2020-07-23 stsp if (--max_recursion == 0) {
2212 e40622f4 2020-07-23 stsp err = got_error_path(path, GOT_ERR_RECURSION);
2213 e40622f4 2020-07-23 stsp *link_target = NULL;
2216 e40622f4 2020-07-23 stsp *link_target = next_target;
2218 e40622f4 2020-07-23 stsp } while (next_target);
2220 e40622f4 2020-07-23 stsp return err;
2223 e40622f4 2020-07-23 stsp const struct got_error *
2224 ca6e02ac 2020-01-07 stsp got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2225 ca6e02ac 2020-01-07 stsp struct got_object_id *commit_id, const char *path,
2226 ca6e02ac 2020-01-07 stsp struct got_repository *repo)
2228 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
2229 ca6e02ac 2020-01-07 stsp struct got_pack *pack = NULL;
2230 ca6e02ac 2020-01-07 stsp struct got_packidx *packidx = NULL;
2231 ca6e02ac 2020-01-07 stsp char *path_packfile = NULL;
2232 ca6e02ac 2020-01-07 stsp struct got_commit_object *changed_commit = NULL;
2233 ca6e02ac 2020-01-07 stsp struct got_object_id *changed_commit_id = NULL;
2236 ca6e02ac 2020-01-07 stsp err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2237 ca6e02ac 2020-01-07 stsp if (err) {
2238 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
2239 ca6e02ac 2020-01-07 stsp return err;
2240 ca6e02ac 2020-01-07 stsp return NULL;
2243 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
2244 aea75d87 2021-07-06 stsp packidx->path_packidx);
2246 ca6e02ac 2020-01-07 stsp return err;
2248 ca6e02ac 2020-01-07 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
2249 ca6e02ac 2020-01-07 stsp if (pack == NULL) {
2250 ca6e02ac 2020-01-07 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2252 ca6e02ac 2020-01-07 stsp goto done;
2255 ca6e02ac 2020-01-07 stsp if (pack->privsep_child == NULL) {
2256 ca6e02ac 2020-01-07 stsp err = start_pack_privsep_child(pack, packidx);
2258 ca6e02ac 2020-01-07 stsp goto done;
2261 ca6e02ac 2020-01-07 stsp err = got_privsep_send_commit_traversal_request(
2262 ca6e02ac 2020-01-07 stsp pack->privsep_child->ibuf, commit_id, idx, path);
2264 ca6e02ac 2020-01-07 stsp goto done;
2266 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_traversed_commits(&changed_commit,
2267 ca6e02ac 2020-01-07 stsp &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2269 ca6e02ac 2020-01-07 stsp goto done;
2271 ca6e02ac 2020-01-07 stsp if (changed_commit) {
2273 ca6e02ac 2020-01-07 stsp * Cache the commit in which the path was changed.
2274 ca6e02ac 2020-01-07 stsp * This commit might be opened again soon.
2276 ca6e02ac 2020-01-07 stsp changed_commit->refcnt++;
2277 ca6e02ac 2020-01-07 stsp err = got_repo_cache_commit(repo, changed_commit_id,
2278 ca6e02ac 2020-01-07 stsp changed_commit);
2279 ca6e02ac 2020-01-07 stsp got_object_commit_close(changed_commit);
2282 ca6e02ac 2020-01-07 stsp free(path_packfile);
2283 ca6e02ac 2020-01-07 stsp free(changed_commit_id);
2284 ca6e02ac 2020-01-07 stsp return err;