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 2178c42e 2018-04-22 stsp #include <sys/uio.h>
21 2178c42e 2018-04-22 stsp #include <sys/socket.h>
22 2178c42e 2018-04-22 stsp #include <sys/wait.h>
23 b48e2ddb 2019-05-22 stsp #include <sys/resource.h>
25 a1fd68d8 2018-01-12 stsp #include <errno.h>
26 2178c42e 2018-04-22 stsp #include <fcntl.h>
27 d71d75ad 2017-11-05 stsp #include <stdio.h>
28 ab9a70b2 2017-11-06 stsp #include <stdlib.h>
29 ab9a70b2 2017-11-06 stsp #include <string.h>
30 2178c42e 2018-04-22 stsp #include <stdint.h>
31 d71d75ad 2017-11-05 stsp #include <sha1.h>
32 81a12da5 2020-09-09 naddy #include <unistd.h>
33 ab9a70b2 2017-11-06 stsp #include <zlib.h>
34 ab9a70b2 2017-11-06 stsp #include <ctype.h>
35 e40622f4 2020-07-23 stsp #include <libgen.h>
36 ab9a70b2 2017-11-06 stsp #include <limits.h>
37 2178c42e 2018-04-22 stsp #include <imsg.h>
38 788c352e 2018-06-16 stsp #include <time.h>
40 ab9a70b2 2017-11-06 stsp #include "got_error.h"
41 d71d75ad 2017-11-05 stsp #include "got_object.h"
42 ab9a70b2 2017-11-06 stsp #include "got_repository.h"
43 511a516b 2018-05-19 stsp #include "got_opentemp.h"
44 324d37e7 2019-05-11 stsp #include "got_path.h"
46 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
47 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
48 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
49 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
50 2178c42e 2018-04-22 stsp #include "got_lib_privsep.h"
51 6bef87be 2018-09-11 stsp #include "got_lib_object_idcache.h"
52 6bef87be 2018-09-11 stsp #include "got_lib_object_cache.h"
53 ad242220 2018-09-08 stsp #include "got_lib_object_parse.h"
54 15a94983 2018-12-23 stsp #include "got_lib_pack.h"
55 7bb0daa1 2018-06-21 stsp #include "got_lib_repository.h"
58 ab9a70b2 2017-11-06 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 3235492e 2018-04-01 stsp struct got_object_id *
62 3235492e 2018-04-01 stsp got_object_get_id(struct got_object *obj)
64 6402fb3c 2018-09-15 stsp return &obj->id;
67 bacc9935 2018-05-20 stsp const struct got_error *
68 bacc9935 2018-05-20 stsp got_object_get_id_str(char **outbuf, struct got_object *obj)
70 bacc9935 2018-05-20 stsp return got_object_id_str(outbuf, &obj->id);
73 15a94983 2018-12-23 stsp const struct got_error *
74 15a94983 2018-12-23 stsp got_object_get_type(int *type, struct got_repository *repo,
75 15a94983 2018-12-23 stsp struct got_object_id *id)
77 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
78 15a94983 2018-12-23 stsp struct got_object *obj;
80 15a94983 2018-12-23 stsp err = got_object_open(&obj, repo, id);
84 b107e67f 2018-01-19 stsp switch (obj->type) {
85 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_COMMIT:
86 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_TREE:
87 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_BLOB:
88 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
89 15a94983 2018-12-23 stsp *type = obj->type;
92 15a94983 2018-12-23 stsp err = got_error(GOT_ERR_OBJ_TYPE);
96 15a94983 2018-12-23 stsp got_object_close(obj);
100 90bdb554 2019-04-11 stsp const struct got_error *
101 90bdb554 2019-04-11 stsp got_object_get_path(char **path, struct got_object_id *id,
102 90bdb554 2019-04-11 stsp struct got_repository *repo)
104 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
105 7a132809 2018-07-23 stsp char *hex = NULL;
106 41d2888b 2019-08-11 stsp char *path_objects;
108 e6b1056e 2018-04-22 stsp *path = NULL;
110 41d2888b 2019-08-11 stsp path_objects = got_repo_get_path_objects(repo);
111 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
112 638f9024 2019-05-13 stsp return got_error_from_errno("got_repo_get_path_objects");
114 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
118 d1cda826 2017-11-06 stsp if (asprintf(path, "%s/%.2x/%s", path_objects,
119 d1cda826 2017-11-06 stsp id->sha1[0], hex + 2) == -1)
120 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
124 d1cda826 2017-11-06 stsp free(path_objects);
125 d1cda826 2017-11-06 stsp return err;
128 762d73f4 2021-04-10 stsp const struct got_error *
129 762d73f4 2021-04-10 stsp got_object_open_loose_fd(int *fd, struct got_object_id *id,
130 4796fb13 2018-12-23 stsp struct got_repository *repo)
132 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
133 a1fd68d8 2018-01-12 stsp char *path;
135 90bdb554 2019-04-11 stsp err = got_object_get_path(&path, id, repo);
137 d1cda826 2017-11-06 stsp return err;
138 a5b57ccf 2019-04-11 stsp *fd = open(path, O_RDONLY | O_NOFOLLOW);
139 d5003b79 2018-04-22 stsp if (*fd == -1) {
140 e82b1d81 2019-07-27 stsp err = got_error_from_errno2("open", path);
144 4558fcd4 2018-01-14 stsp free(path);
145 2090a03d 2018-09-09 stsp return err;
148 2090a03d 2018-09-09 stsp static const struct got_error *
149 a158c901 2018-12-23 stsp request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
150 a158c901 2018-12-23 stsp struct got_object_id *id)
152 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
153 a158c901 2018-12-23 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
155 a158c901 2018-12-23 stsp err = got_privsep_send_packed_obj_req(ibuf, idx, id);
157 a158c901 2018-12-23 stsp return err;
159 a158c901 2018-12-23 stsp err = got_privsep_recv_obj(obj, ibuf);
161 a158c901 2018-12-23 stsp return err;
163 a158c901 2018-12-23 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
165 a158c901 2018-12-23 stsp return NULL;
168 59d1e4a0 2021-03-10 stsp static const struct got_error *
169 59d1e4a0 2021-03-10 stsp request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
170 59d1e4a0 2021-03-10 stsp int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
172 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
173 59d1e4a0 2021-03-10 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
174 59d1e4a0 2021-03-10 stsp int outfd_child;
175 59d1e4a0 2021-03-10 stsp int basefd, accumfd; /* temporary files for delta application */
177 59d1e4a0 2021-03-10 stsp basefd = got_opentempfd();
178 59d1e4a0 2021-03-10 stsp if (basefd == -1)
179 59d1e4a0 2021-03-10 stsp return got_error_from_errno("got_opentempfd");
181 59d1e4a0 2021-03-10 stsp accumfd = got_opentempfd();
182 59d1e4a0 2021-03-10 stsp if (accumfd == -1) {
183 59d1e4a0 2021-03-10 stsp close(basefd);
184 59d1e4a0 2021-03-10 stsp return got_error_from_errno("got_opentempfd");
187 59d1e4a0 2021-03-10 stsp outfd_child = dup(outfd);
188 59d1e4a0 2021-03-10 stsp if (outfd_child == -1) {
189 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("dup");
190 59d1e4a0 2021-03-10 stsp close(basefd);
191 59d1e4a0 2021-03-10 stsp close(accumfd);
192 59d1e4a0 2021-03-10 stsp return err;
195 59d1e4a0 2021-03-10 stsp err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
197 59d1e4a0 2021-03-10 stsp close(basefd);
198 59d1e4a0 2021-03-10 stsp close(accumfd);
199 59d1e4a0 2021-03-10 stsp close(outfd_child);
200 59d1e4a0 2021-03-10 stsp return err;
203 59d1e4a0 2021-03-10 stsp err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
205 59d1e4a0 2021-03-10 stsp close(basefd);
206 59d1e4a0 2021-03-10 stsp close(accumfd);
207 59d1e4a0 2021-03-10 stsp return err;
211 59d1e4a0 2021-03-10 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
214 59d1e4a0 2021-03-10 stsp close(accumfd);
215 59d1e4a0 2021-03-10 stsp return err;
218 59d1e4a0 2021-03-10 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
221 59d1e4a0 2021-03-10 stsp return err;
223 59d1e4a0 2021-03-10 stsp err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
225 59d1e4a0 2021-03-10 stsp return err;
227 59d1e4a0 2021-03-10 stsp return NULL;
230 da506691 2019-05-22 stsp static void
231 b48e2ddb 2019-05-22 stsp set_max_datasize(void)
233 b48e2ddb 2019-05-22 stsp struct rlimit rl;
235 b48e2ddb 2019-05-22 stsp if (getrlimit(RLIMIT_DATA, &rl) != 0)
238 b48e2ddb 2019-05-22 stsp rl.rlim_cur = rl.rlim_max;
239 b48e2ddb 2019-05-22 stsp setrlimit(RLIMIT_DATA, &rl);
242 a158c901 2018-12-23 stsp static const struct got_error *
243 711fb6e8 2018-12-23 stsp start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
245 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
246 a158c901 2018-12-23 stsp int imsg_fds[2];
248 a158c901 2018-12-23 stsp struct imsgbuf *ibuf;
250 a158c901 2018-12-23 stsp ibuf = calloc(1, sizeof(*ibuf));
251 a158c901 2018-12-23 stsp if (ibuf == NULL)
252 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
254 a158c901 2018-12-23 stsp pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
255 a158c901 2018-12-23 stsp if (pack->privsep_child == NULL) {
256 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
257 a158c901 2018-12-23 stsp free(ibuf);
258 a158c901 2018-12-23 stsp return err;
261 a158c901 2018-12-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
262 638f9024 2019-05-13 stsp err = got_error_from_errno("socketpair");
266 a158c901 2018-12-23 stsp pid = fork();
267 a158c901 2018-12-23 stsp if (pid == -1) {
268 638f9024 2019-05-13 stsp err = got_error_from_errno("fork");
270 a158c901 2018-12-23 stsp } else if (pid == 0) {
271 b48e2ddb 2019-05-22 stsp set_max_datasize();
272 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
273 a158c901 2018-12-23 stsp pack->path_packfile);
274 a158c901 2018-12-23 stsp /* not reached */
277 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1)
278 638f9024 2019-05-13 stsp return got_error_from_errno("close");
279 a158c901 2018-12-23 stsp pack->privsep_child->imsg_fd = imsg_fds[0];
280 a158c901 2018-12-23 stsp pack->privsep_child->pid = pid;
281 a158c901 2018-12-23 stsp imsg_init(ibuf, imsg_fds[0]);
282 a158c901 2018-12-23 stsp pack->privsep_child->ibuf = ibuf;
284 a158c901 2018-12-23 stsp err = got_privsep_init_pack_child(ibuf, pack, packidx);
286 a158c901 2018-12-23 stsp const struct got_error *child_err;
287 a158c901 2018-12-23 stsp err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
288 a158c901 2018-12-23 stsp child_err = got_privsep_wait_for_child(
289 a158c901 2018-12-23 stsp pack->privsep_child->pid);
290 a158c901 2018-12-23 stsp if (child_err && err == NULL)
291 a158c901 2018-12-23 stsp err = child_err;
295 a158c901 2018-12-23 stsp free(ibuf);
296 a158c901 2018-12-23 stsp free(pack->privsep_child);
297 a158c901 2018-12-23 stsp pack->privsep_child = NULL;
299 a158c901 2018-12-23 stsp return err;
302 711fb6e8 2018-12-23 stsp static const struct got_error *
303 711fb6e8 2018-12-23 stsp read_packed_object_privsep(struct got_object **obj,
304 711fb6e8 2018-12-23 stsp struct got_repository *repo, struct got_pack *pack,
305 711fb6e8 2018-12-23 stsp struct got_packidx *packidx, int idx, struct got_object_id *id)
307 711fb6e8 2018-12-23 stsp const struct got_error *err = NULL;
309 59d1e4a0 2021-03-10 stsp if (pack->privsep_child == NULL) {
310 59d1e4a0 2021-03-10 stsp err = start_pack_privsep_child(pack, packidx);
312 59d1e4a0 2021-03-10 stsp return err;
315 711fb6e8 2018-12-23 stsp return request_packed_object(obj, pack, idx, id);
318 59d1e4a0 2021-03-10 stsp static const struct got_error *
319 59d1e4a0 2021-03-10 stsp read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
320 59d1e4a0 2021-03-10 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
321 59d1e4a0 2021-03-10 stsp struct got_object_id *id)
323 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
325 59d1e4a0 2021-03-10 stsp if (pack->privsep_child == NULL) {
326 59d1e4a0 2021-03-10 stsp err = start_pack_privsep_child(pack, packidx);
328 59d1e4a0 2021-03-10 stsp return err;
331 59d1e4a0 2021-03-10 stsp return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
335 a158c901 2018-12-23 stsp static const struct got_error *
336 2090a03d 2018-09-09 stsp open_packed_object(struct got_object **obj, struct got_object_id *id,
337 2090a03d 2018-09-09 stsp struct got_repository *repo)
339 2090a03d 2018-09-09 stsp const struct got_error *err = NULL;
340 2090a03d 2018-09-09 stsp struct got_pack *pack = NULL;
341 2090a03d 2018-09-09 stsp struct got_packidx *packidx = NULL;
343 2090a03d 2018-09-09 stsp char *path_packfile;
345 2090a03d 2018-09-09 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
347 2090a03d 2018-09-09 stsp return err;
349 509c9973 2021-04-10 stsp err = got_packidx_get_packfile_path(&path_packfile, 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 9f2369b0 2018-12-24 stsp request_object(struct got_object **obj, struct got_repository *repo, int fd)
371 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
372 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
374 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
376 aea5f015 2018-12-24 stsp err = got_privsep_send_obj_req(ibuf, fd);
378 9f2369b0 2018-12-24 stsp return err;
380 9f2369b0 2018-12-24 stsp return got_privsep_recv_obj(obj, ibuf);
383 9f2369b0 2018-12-24 stsp static const struct got_error *
384 59d1e4a0 2021-03-10 stsp request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
385 59d1e4a0 2021-03-10 stsp struct got_repository *repo, int infd)
387 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
388 59d1e4a0 2021-03-10 stsp struct imsgbuf *ibuf;
389 59d1e4a0 2021-03-10 stsp int outfd_child;
391 59d1e4a0 2021-03-10 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
393 59d1e4a0 2021-03-10 stsp outfd_child = dup(outfd);
394 59d1e4a0 2021-03-10 stsp if (outfd_child == -1)
395 59d1e4a0 2021-03-10 stsp return got_error_from_errno("dup");
397 59d1e4a0 2021-03-10 stsp err = got_privsep_send_raw_obj_req(ibuf, infd);
399 59d1e4a0 2021-03-10 stsp return err;
401 59d1e4a0 2021-03-10 stsp err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
403 59d1e4a0 2021-03-10 stsp return err;
405 59d1e4a0 2021-03-10 stsp return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
408 59d1e4a0 2021-03-10 stsp static const struct got_error *
409 59d1e4a0 2021-03-10 stsp start_read_object_child(struct got_repository *repo)
411 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
412 9f2369b0 2018-12-24 stsp int imsg_fds[2];
414 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
416 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
417 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
418 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
420 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
421 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
422 ddc7b220 2019-09-08 stsp free(ibuf);
423 ddc7b220 2019-09-08 stsp return err;
426 9f2369b0 2018-12-24 stsp pid = fork();
427 ddc7b220 2019-09-08 stsp if (pid == -1) {
428 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
429 ddc7b220 2019-09-08 stsp free(ibuf);
430 ddc7b220 2019-09-08 stsp return err;
432 9f2369b0 2018-12-24 stsp else if (pid == 0) {
433 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
434 9f2369b0 2018-12-24 stsp repo->path);
435 9f2369b0 2018-12-24 stsp /* not reached */
438 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
439 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
440 ddc7b220 2019-09-08 stsp free(ibuf);
441 ddc7b220 2019-09-08 stsp return err;
444 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
445 9f2369b0 2018-12-24 stsp imsg_fds[0];
446 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
447 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
448 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
450 59d1e4a0 2021-03-10 stsp return NULL;
453 59d1e4a0 2021-03-10 stsp static const struct got_error *
454 59d1e4a0 2021-03-10 stsp read_object_header_privsep(struct got_object **obj, struct got_repository *repo,
455 59d1e4a0 2021-03-10 stsp int obj_fd)
457 59d1e4a0 2021-03-10 stsp const struct got_error *err;
459 59d1e4a0 2021-03-10 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
460 59d1e4a0 2021-03-10 stsp return request_object(obj, repo, obj_fd);
462 59d1e4a0 2021-03-10 stsp err = start_read_object_child(repo);
464 7495ec13 2021-04-04 stsp close(obj_fd);
465 59d1e4a0 2021-03-10 stsp return err;
468 9f2369b0 2018-12-24 stsp return request_object(obj, repo, obj_fd);
471 59d1e4a0 2021-03-10 stsp static const struct got_error *
472 59d1e4a0 2021-03-10 stsp read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
473 59d1e4a0 2021-03-10 stsp int outfd, struct got_repository *repo, int obj_fd)
475 59d1e4a0 2021-03-10 stsp const struct got_error *err;
477 59d1e4a0 2021-03-10 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
478 59d1e4a0 2021-03-10 stsp return request_raw_object(outbuf, size, hdrlen, outfd, repo,
481 59d1e4a0 2021-03-10 stsp err = start_read_object_child(repo);
483 59d1e4a0 2021-03-10 stsp return err;
485 59d1e4a0 2021-03-10 stsp return request_raw_object(outbuf, size, hdrlen, outfd, repo, obj_fd);
488 4558fcd4 2018-01-14 stsp const struct got_error *
489 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
490 4558fcd4 2018-01-14 stsp struct got_object_id *id)
492 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
495 7bb0daa1 2018-06-21 stsp *obj = got_repo_get_cached_object(repo, id);
496 7bb0daa1 2018-06-21 stsp if (*obj != NULL) {
497 7bb0daa1 2018-06-21 stsp (*obj)->refcnt++;
498 7bb0daa1 2018-06-21 stsp return NULL;
501 e82b1d81 2019-07-27 stsp err = open_packed_object(obj, id, repo);
502 e82b1d81 2019-07-27 stsp if (err && err->code != GOT_ERR_NO_OBJ)
503 e82b1d81 2019-07-27 stsp return err;
504 e82b1d81 2019-07-27 stsp if (*obj) {
505 e82b1d81 2019-07-27 stsp (*obj)->refcnt++;
506 e82b1d81 2019-07-27 stsp return got_repo_cache_object(repo, id, *obj);
509 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
511 762d73f4 2021-04-10 stsp if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
512 762d73f4 2021-04-10 stsp err = got_error_no_obj(id);
513 762d73f4 2021-04-10 stsp return err;
516 762d73f4 2021-04-10 stsp err = read_object_header_privsep(obj, repo, fd);
518 4558fcd4 2018-01-14 stsp return err;
520 762d73f4 2021-04-10 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
522 59790a32 2018-09-15 stsp (*obj)->refcnt++;
523 762d73f4 2021-04-10 stsp return got_repo_cache_object(repo, id, *obj);
526 59d1e4a0 2021-03-10 stsp const struct got_error *
527 59d1e4a0 2021-03-10 stsp got_object_raw_open(struct got_raw_object **obj, struct got_repository *repo,
528 59d1e4a0 2021-03-10 stsp struct got_object_id *id, size_t blocksize)
530 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
531 59d1e4a0 2021-03-10 stsp struct got_packidx *packidx = NULL;
533 59d1e4a0 2021-03-10 stsp uint8_t *outbuf = NULL;
534 59d1e4a0 2021-03-10 stsp int outfd = -1;
535 59d1e4a0 2021-03-10 stsp off_t size = 0;
536 59d1e4a0 2021-03-10 stsp size_t hdrlen = 0;
537 59d1e4a0 2021-03-10 stsp char *path_packfile = NULL;
539 59d1e4a0 2021-03-10 stsp *obj = NULL;
541 59d1e4a0 2021-03-10 stsp outfd = got_opentempfd();
542 59d1e4a0 2021-03-10 stsp if (outfd == -1)
543 59d1e4a0 2021-03-10 stsp return got_error_from_errno("got_opentempfd");
545 59d1e4a0 2021-03-10 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
546 59d1e4a0 2021-03-10 stsp if (err == NULL) {
547 59d1e4a0 2021-03-10 stsp struct got_pack *pack = NULL;
549 509c9973 2021-04-10 stsp err = got_packidx_get_packfile_path(&path_packfile, packidx);
553 59d1e4a0 2021-03-10 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
554 59d1e4a0 2021-03-10 stsp if (pack == NULL) {
555 59d1e4a0 2021-03-10 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
560 59d1e4a0 2021-03-10 stsp err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
561 59d1e4a0 2021-03-10 stsp outfd, pack, packidx, idx, id);
562 59d1e4a0 2021-03-10 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
565 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
568 59d1e4a0 2021-03-10 stsp err = read_object_raw_privsep(&outbuf, &size, &hdrlen, outfd,
572 59d1e4a0 2021-03-10 stsp if (hdrlen > size) {
573 59d1e4a0 2021-03-10 stsp err = got_error(GOT_ERR_BAD_OBJ_HDR);
577 59d1e4a0 2021-03-10 stsp *obj = calloc(1, sizeof(**obj));
578 59d1e4a0 2021-03-10 stsp if (*obj == NULL) {
579 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("calloc");
583 59d1e4a0 2021-03-10 stsp (*obj)->read_buf = malloc(blocksize);
584 59d1e4a0 2021-03-10 stsp if ((*obj)->read_buf == NULL) {
585 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("malloc");
589 59d1e4a0 2021-03-10 stsp if (outbuf) {
590 59d1e4a0 2021-03-10 stsp if (close(outfd) == -1) {
591 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("close");
594 59d1e4a0 2021-03-10 stsp outfd = -1;
595 59d1e4a0 2021-03-10 stsp (*obj)->f = fmemopen(outbuf, hdrlen + size, "r");
596 59d1e4a0 2021-03-10 stsp if ((*obj)->f == NULL) {
597 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fdopen");
600 59d1e4a0 2021-03-10 stsp (*obj)->data = outbuf;
602 59d1e4a0 2021-03-10 stsp struct stat sb;
603 59d1e4a0 2021-03-10 stsp if (fstat(outfd, &sb) == -1) {
604 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fstat");
608 59d1e4a0 2021-03-10 stsp if (sb.st_size != size) {
609 59d1e4a0 2021-03-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
613 59d1e4a0 2021-03-10 stsp (*obj)->f = fdopen(outfd, "r");
614 59d1e4a0 2021-03-10 stsp if ((*obj)->f == NULL) {
615 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fdopen");
618 59d1e4a0 2021-03-10 stsp outfd = -1;
619 59d1e4a0 2021-03-10 stsp (*obj)->data = NULL;
621 59d1e4a0 2021-03-10 stsp (*obj)->hdrlen = hdrlen;
622 59d1e4a0 2021-03-10 stsp (*obj)->size = size;
623 59d1e4a0 2021-03-10 stsp (*obj)->blocksize = blocksize;
625 59d1e4a0 2021-03-10 stsp free(path_packfile);
627 59d1e4a0 2021-03-10 stsp if (*obj) {
628 59d1e4a0 2021-03-10 stsp got_object_raw_close(*obj);
629 59d1e4a0 2021-03-10 stsp *obj = NULL;
631 59d1e4a0 2021-03-10 stsp if (outfd != -1)
632 59d1e4a0 2021-03-10 stsp close(outfd);
633 59d1e4a0 2021-03-10 stsp free(outbuf);
635 59d1e4a0 2021-03-10 stsp return err;
639 59d1e4a0 2021-03-10 stsp got_object_raw_rewind(struct got_raw_object *obj)
641 59d1e4a0 2021-03-10 stsp if (obj->f)
642 59d1e4a0 2021-03-10 stsp rewind(obj->f);
646 59d1e4a0 2021-03-10 stsp got_object_raw_get_hdrlen(struct got_raw_object *obj)
648 59d1e4a0 2021-03-10 stsp return obj->hdrlen;
651 59d1e4a0 2021-03-10 stsp const uint8_t *
652 59d1e4a0 2021-03-10 stsp got_object_raw_get_read_buf(struct got_raw_object *obj)
654 59d1e4a0 2021-03-10 stsp return obj->read_buf;
657 59d1e4a0 2021-03-10 stsp const struct got_error *
658 59d1e4a0 2021-03-10 stsp got_object_raw_read_block(size_t *outlenp, struct got_raw_object *obj)
662 59d1e4a0 2021-03-10 stsp n = fread(obj->read_buf, 1, obj->blocksize, obj->f);
663 59d1e4a0 2021-03-10 stsp if (n == 0 && ferror(obj->f))
664 59d1e4a0 2021-03-10 stsp return got_ferror(obj->f, GOT_ERR_IO);
665 59d1e4a0 2021-03-10 stsp *outlenp = n;
666 59d1e4a0 2021-03-10 stsp return NULL;
669 59d1e4a0 2021-03-10 stsp const struct got_error *
670 59d1e4a0 2021-03-10 stsp got_object_raw_close(struct got_raw_object *obj)
672 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
674 59d1e4a0 2021-03-10 stsp free(obj->read_buf);
675 59d1e4a0 2021-03-10 stsp if (obj->f != NULL && fclose(obj->f) == EOF && err == NULL)
676 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fclose");
677 59d1e4a0 2021-03-10 stsp free(obj->data);
679 59d1e4a0 2021-03-10 stsp return err;
682 6dfa2fd3 2018-02-12 stsp const struct got_error *
683 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
684 6dfa2fd3 2018-02-12 stsp const char *id_str)
686 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
688 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
689 6dd1ece6 2019-11-10 stsp return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
691 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
694 15a94983 2018-12-23 stsp const struct got_error *
695 15a94983 2018-12-23 stsp got_object_resolve_id_str(struct got_object_id **id,
696 15a94983 2018-12-23 stsp struct got_repository *repo, const char *id_str)
698 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
699 15a94983 2018-12-23 stsp struct got_object *obj;
701 15a94983 2018-12-23 stsp err = got_object_open_by_id_str(&obj, repo, id_str);
703 15a94983 2018-12-23 stsp return err;
705 15a94983 2018-12-23 stsp *id = got_object_id_dup(got_object_get_id(obj));
706 15a94983 2018-12-23 stsp got_object_close(obj);
707 15a94983 2018-12-23 stsp if (*id == NULL)
708 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
710 15a94983 2018-12-23 stsp return NULL;
713 434025f3 2018-09-16 stsp static const struct got_error *
714 a158c901 2018-12-23 stsp request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
715 a158c901 2018-12-23 stsp int pack_idx, struct got_object_id *id)
717 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
719 a158c901 2018-12-23 stsp err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
722 a158c901 2018-12-23 stsp return err;
724 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
726 ca6e02ac 2020-01-07 stsp return err;
728 ca6e02ac 2020-01-07 stsp (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
729 ca6e02ac 2020-01-07 stsp return NULL;
732 a158c901 2018-12-23 stsp static const struct got_error *
733 a158c901 2018-12-23 stsp read_packed_commit_privsep(struct got_commit_object **commit,
734 a158c901 2018-12-23 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
735 a158c901 2018-12-23 stsp struct got_object_id *id)
737 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
739 a158c901 2018-12-23 stsp if (pack->privsep_child)
740 a158c901 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
742 711fb6e8 2018-12-23 stsp err = start_pack_privsep_child(pack, packidx);
744 a158c901 2018-12-23 stsp return err;
746 711fb6e8 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
749 9f2369b0 2018-12-24 stsp static const struct got_error *
750 9f2369b0 2018-12-24 stsp request_commit(struct got_commit_object **commit, struct got_repository *repo,
753 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
754 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
756 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
758 9f2369b0 2018-12-24 stsp err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
760 9f2369b0 2018-12-24 stsp return err;
762 9f2369b0 2018-12-24 stsp return got_privsep_recv_commit(commit, ibuf);
765 9f2369b0 2018-12-24 stsp static const struct got_error *
766 9f2369b0 2018-12-24 stsp read_commit_privsep(struct got_commit_object **commit, int obj_fd,
767 9f2369b0 2018-12-24 stsp struct got_repository *repo)
769 ddc7b220 2019-09-08 stsp const struct got_error *err;
770 9f2369b0 2018-12-24 stsp int imsg_fds[2];
772 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
774 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
775 9f2369b0 2018-12-24 stsp return request_commit(commit, repo, obj_fd);
777 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
778 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
779 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
781 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
782 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
783 ddc7b220 2019-09-08 stsp free(ibuf);
784 ddc7b220 2019-09-08 stsp return err;
787 9f2369b0 2018-12-24 stsp pid = fork();
788 ddc7b220 2019-09-08 stsp if (pid == -1) {
789 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
790 ddc7b220 2019-09-08 stsp free(ibuf);
791 ddc7b220 2019-09-08 stsp return err;
793 9f2369b0 2018-12-24 stsp else if (pid == 0) {
794 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
795 9f2369b0 2018-12-24 stsp repo->path);
796 9f2369b0 2018-12-24 stsp /* not reached */
799 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
800 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
801 ddc7b220 2019-09-08 stsp free(ibuf);
802 ddc7b220 2019-09-08 stsp return err;
804 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
805 9f2369b0 2018-12-24 stsp imsg_fds[0];
806 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
807 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
808 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
810 9f2369b0 2018-12-24 stsp return request_commit(commit, repo, obj_fd);
814 a158c901 2018-12-23 stsp static const struct got_error *
815 434025f3 2018-09-16 stsp open_commit(struct got_commit_object **commit,
816 1785f84a 2018-12-23 stsp struct got_repository *repo, struct got_object_id *id, int check_cache)
818 434025f3 2018-09-16 stsp const struct got_error *err = NULL;
819 1785f84a 2018-12-23 stsp struct got_packidx *packidx = NULL;
821 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
823 434025f3 2018-09-16 stsp if (check_cache) {
824 1785f84a 2018-12-23 stsp *commit = got_repo_get_cached_commit(repo, id);
825 434025f3 2018-09-16 stsp if (*commit != NULL) {
826 434025f3 2018-09-16 stsp (*commit)->refcnt++;
827 434025f3 2018-09-16 stsp return NULL;
830 434025f3 2018-09-16 stsp *commit = NULL;
832 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
833 e82b1d81 2019-07-27 stsp if (err == NULL) {
834 1785f84a 2018-12-23 stsp struct got_pack *pack = NULL;
836 509c9973 2021-04-10 stsp err = got_packidx_get_packfile_path(&path_packfile, packidx);
838 1785f84a 2018-12-23 stsp return err;
840 1785f84a 2018-12-23 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
841 434025f3 2018-09-16 stsp if (pack == NULL) {
842 1785f84a 2018-12-23 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
847 a158c901 2018-12-23 stsp err = read_packed_commit_privsep(commit, pack,
848 1785f84a 2018-12-23 stsp packidx, idx, id);
849 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
852 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
854 e82b1d81 2019-07-27 stsp return err;
855 9f2369b0 2018-12-24 stsp err = read_commit_privsep(commit, fd, repo);
858 434025f3 2018-09-16 stsp if (err == NULL) {
859 434025f3 2018-09-16 stsp (*commit)->refcnt++;
860 1785f84a 2018-12-23 stsp err = got_repo_cache_commit(repo, id, *commit);
863 8d2c5ea3 2019-08-13 stsp free(path_packfile);
864 e32baab7 2018-11-05 stsp return err;
867 e32baab7 2018-11-05 stsp const struct got_error *
868 e32baab7 2018-11-05 stsp got_object_open_as_commit(struct got_commit_object **commit,
869 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object_id *id)
871 e32baab7 2018-11-05 stsp *commit = got_repo_get_cached_commit(repo, id);
872 e32baab7 2018-11-05 stsp if (*commit != NULL) {
873 e32baab7 2018-11-05 stsp (*commit)->refcnt++;
874 e32baab7 2018-11-05 stsp return NULL;
877 1785f84a 2018-12-23 stsp return open_commit(commit, repo, id, 0);
880 e32baab7 2018-11-05 stsp const struct got_error *
881 e32baab7 2018-11-05 stsp got_object_commit_open(struct got_commit_object **commit,
882 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object *obj)
884 1785f84a 2018-12-23 stsp return open_commit(commit, repo, got_object_get_id(obj), 1);
887 434025f3 2018-09-16 stsp const struct got_error *
888 dbc6a6b6 2018-07-12 stsp got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
890 dbc6a6b6 2018-07-12 stsp const struct got_error *err = NULL;
892 dbc6a6b6 2018-07-12 stsp *qid = calloc(1, sizeof(**qid));
893 dbc6a6b6 2018-07-12 stsp if (*qid == NULL)
894 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
896 dbc6a6b6 2018-07-12 stsp (*qid)->id = got_object_id_dup(id);
897 dbc6a6b6 2018-07-12 stsp if ((*qid)->id == NULL) {
898 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
899 fa2f6902 2018-07-23 stsp got_object_qid_free(*qid);
900 dbc6a6b6 2018-07-12 stsp *qid = NULL;
901 dbc6a6b6 2018-07-12 stsp return err;
904 dbc6a6b6 2018-07-12 stsp return NULL;
907 a158c901 2018-12-23 stsp static const struct got_error *
908 13c729f7 2018-12-24 stsp request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
909 13c729f7 2018-12-24 stsp int pack_idx, struct got_object_id *id)
911 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
913 13c729f7 2018-12-24 stsp err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
916 a158c901 2018-12-23 stsp return err;
918 a158c901 2018-12-23 stsp return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
921 71eb0e7f 2018-09-16 stsp static const struct got_error *
922 13c729f7 2018-12-24 stsp read_packed_tree_privsep(struct got_tree_object **tree,
923 13c729f7 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
924 13c729f7 2018-12-24 stsp struct got_object_id *id)
926 13c729f7 2018-12-24 stsp const struct got_error *err = NULL;
928 13c729f7 2018-12-24 stsp if (pack->privsep_child)
929 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
931 13c729f7 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
933 13c729f7 2018-12-24 stsp return err;
935 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
938 13c729f7 2018-12-24 stsp static const struct got_error *
939 9f2369b0 2018-12-24 stsp request_tree(struct got_tree_object **tree, struct got_repository *repo,
942 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
943 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
945 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
947 9f2369b0 2018-12-24 stsp err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
949 9f2369b0 2018-12-24 stsp return err;
951 9f2369b0 2018-12-24 stsp return got_privsep_recv_tree(tree, ibuf);
954 9f2369b0 2018-12-24 stsp const struct got_error *
955 9f2369b0 2018-12-24 stsp read_tree_privsep(struct got_tree_object **tree, int obj_fd,
956 9f2369b0 2018-12-24 stsp struct got_repository *repo)
958 ddc7b220 2019-09-08 stsp const struct got_error *err;
959 9f2369b0 2018-12-24 stsp int imsg_fds[2];
961 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
963 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
964 9f2369b0 2018-12-24 stsp return request_tree(tree, repo, obj_fd);
966 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
967 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
968 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
970 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
971 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
972 ddc7b220 2019-09-08 stsp free(ibuf);
973 ddc7b220 2019-09-08 stsp return err;
976 9f2369b0 2018-12-24 stsp pid = fork();
977 ddc7b220 2019-09-08 stsp if (pid == -1) {
978 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
979 ddc7b220 2019-09-08 stsp free(ibuf);
980 ddc7b220 2019-09-08 stsp return err;
982 9f2369b0 2018-12-24 stsp else if (pid == 0) {
983 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
984 9f2369b0 2018-12-24 stsp repo->path);
985 9f2369b0 2018-12-24 stsp /* not reached */
988 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
989 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
990 ddc7b220 2019-09-08 stsp free(ibuf);
991 ddc7b220 2019-09-08 stsp return err;
993 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
994 9f2369b0 2018-12-24 stsp imsg_fds[0];
995 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
996 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
997 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1000 9f2369b0 2018-12-24 stsp return request_tree(tree, repo, obj_fd);
1003 9f2369b0 2018-12-24 stsp static const struct got_error *
1004 13c729f7 2018-12-24 stsp open_tree(struct got_tree_object **tree, struct got_repository *repo,
1005 13c729f7 2018-12-24 stsp struct got_object_id *id, int check_cache)
1007 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
1008 13c729f7 2018-12-24 stsp struct got_packidx *packidx = NULL;
1010 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1012 71eb0e7f 2018-09-16 stsp if (check_cache) {
1013 13c729f7 2018-12-24 stsp *tree = got_repo_get_cached_tree(repo, id);
1014 71eb0e7f 2018-09-16 stsp if (*tree != NULL) {
1015 71eb0e7f 2018-09-16 stsp (*tree)->refcnt++;
1016 71eb0e7f 2018-09-16 stsp return NULL;
1019 71eb0e7f 2018-09-16 stsp *tree = NULL;
1021 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1022 e82b1d81 2019-07-27 stsp if (err == NULL) {
1023 13c729f7 2018-12-24 stsp struct got_pack *pack = NULL;
1025 509c9973 2021-04-10 stsp err = got_packidx_get_packfile_path(&path_packfile, packidx);
1027 13c729f7 2018-12-24 stsp return err;
1029 13c729f7 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1030 e7885405 2018-09-10 stsp if (pack == NULL) {
1031 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1034 8d2c5ea3 2019-08-13 stsp goto done;
1036 13c729f7 2018-12-24 stsp err = read_packed_tree_privsep(tree, pack,
1037 13c729f7 2018-12-24 stsp packidx, idx, id);
1038 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1041 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1043 e82b1d81 2019-07-27 stsp return err;
1044 9f2369b0 2018-12-24 stsp err = read_tree_privsep(tree, fd, repo);
1047 f6be5c30 2018-06-22 stsp if (err == NULL) {
1048 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
1049 13c729f7 2018-12-24 stsp err = got_repo_cache_tree(repo, id, *tree);
1052 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1053 776d4d29 2018-06-17 stsp return err;
1056 776d4d29 2018-06-17 stsp const struct got_error *
1057 776d4d29 2018-06-17 stsp got_object_open_as_tree(struct got_tree_object **tree,
1058 776d4d29 2018-06-17 stsp struct got_repository *repo, struct got_object_id *id)
1060 e8eb494a 2018-09-16 stsp *tree = got_repo_get_cached_tree(repo, id);
1061 e8eb494a 2018-09-16 stsp if (*tree != NULL) {
1062 e8eb494a 2018-09-16 stsp (*tree)->refcnt++;
1063 e8eb494a 2018-09-16 stsp return NULL;
1066 13c729f7 2018-12-24 stsp return open_tree(tree, repo, id, 0);
1069 71eb0e7f 2018-09-16 stsp const struct got_error *
1070 71eb0e7f 2018-09-16 stsp got_object_tree_open(struct got_tree_object **tree,
1071 71eb0e7f 2018-09-16 stsp struct got_repository *repo, struct got_object *obj)
1073 13c729f7 2018-12-24 stsp return open_tree(tree, repo, got_object_get_id(obj), 1);
1077 56e0773d 2019-11-28 stsp got_object_tree_get_nentries(struct got_tree_object *tree)
1079 56e0773d 2019-11-28 stsp return tree->nentries;
1082 56e0773d 2019-11-28 stsp struct got_tree_entry *
1083 56e0773d 2019-11-28 stsp got_object_tree_get_first_entry(struct got_tree_object *tree)
1085 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, 0);
1088 56e0773d 2019-11-28 stsp struct got_tree_entry *
1089 56e0773d 2019-11-28 stsp got_object_tree_get_last_entry(struct got_tree_object *tree)
1091 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, tree->nentries - 1);
1094 56e0773d 2019-11-28 stsp struct got_tree_entry *
1095 56e0773d 2019-11-28 stsp got_object_tree_get_entry(struct got_tree_object *tree, int i)
1097 56e0773d 2019-11-28 stsp if (i < 0 || i >= tree->nentries)
1098 56e0773d 2019-11-28 stsp return NULL;
1099 56e0773d 2019-11-28 stsp return &tree->entries[i];
1103 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(struct got_tree_entry *te)
1105 56e0773d 2019-11-28 stsp return te->mode;
1108 56e0773d 2019-11-28 stsp const char *
1109 56e0773d 2019-11-28 stsp got_tree_entry_get_name(struct got_tree_entry *te)
1111 56e0773d 2019-11-28 stsp return &te->name[0];
1114 56e0773d 2019-11-28 stsp struct got_object_id *
1115 56e0773d 2019-11-28 stsp got_tree_entry_get_id(struct got_tree_entry *te)
1117 56e0773d 2019-11-28 stsp return &te->id;
1120 0d6c6ee3 2020-05-20 stsp const struct got_error *
1121 af57b12a 2020-07-23 stsp got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1123 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
1124 32596e16 2020-07-23 stsp size_t len, totlen, hdrlen, offset;
1126 aa092692 2020-07-23 stsp *s = NULL;
1128 659dc16e 2020-07-23 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1129 659dc16e 2020-07-23 stsp totlen = 0;
1130 32596e16 2020-07-23 stsp offset = 0;
1134 659dc16e 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1136 af57b12a 2020-07-23 stsp return err;
1138 659dc16e 2020-07-23 stsp if (len == 0)
1141 659dc16e 2020-07-23 stsp totlen += len - hdrlen;
1142 af57b12a 2020-07-23 stsp p = realloc(*s, totlen + 1);
1143 659dc16e 2020-07-23 stsp if (p == NULL) {
1144 659dc16e 2020-07-23 stsp err = got_error_from_errno("realloc");
1146 af57b12a 2020-07-23 stsp *s = NULL;
1147 af57b12a 2020-07-23 stsp return err;
1150 659dc16e 2020-07-23 stsp /* Skip blob object header first time around. */
1151 af57b12a 2020-07-23 stsp memcpy(*s + offset,
1152 f8f7c882 2020-07-23 stsp got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1153 659dc16e 2020-07-23 stsp hdrlen = 0;
1154 32596e16 2020-07-23 stsp offset = totlen;
1155 659dc16e 2020-07-23 stsp } while (len > 0);
1157 af57b12a 2020-07-23 stsp (*s)[totlen] = '\0';
1158 af57b12a 2020-07-23 stsp return NULL;
1161 af57b12a 2020-07-23 stsp const struct got_error *
1162 af57b12a 2020-07-23 stsp got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1163 af57b12a 2020-07-23 stsp struct got_repository *repo)
1165 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
1166 af57b12a 2020-07-23 stsp struct got_blob_object *blob = NULL;
1168 af57b12a 2020-07-23 stsp *link_target = NULL;
1170 af57b12a 2020-07-23 stsp if (!got_object_tree_entry_is_symlink(te))
1171 af57b12a 2020-07-23 stsp return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1173 af57b12a 2020-07-23 stsp err = got_object_open_as_blob(&blob, repo,
1174 af57b12a 2020-07-23 stsp got_tree_entry_get_id(te), PATH_MAX);
1176 af57b12a 2020-07-23 stsp return err;
1178 af57b12a 2020-07-23 stsp err = got_object_blob_read_to_str(link_target, blob);
1179 af57b12a 2020-07-23 stsp got_object_blob_close(blob);
1180 659dc16e 2020-07-23 stsp if (err) {
1181 659dc16e 2020-07-23 stsp free(*link_target);
1182 659dc16e 2020-07-23 stsp *link_target = NULL;
1184 0d6c6ee3 2020-05-20 stsp return err;
1188 56e0773d 2019-11-28 stsp got_tree_entry_get_index(struct got_tree_entry *te)
1190 56e0773d 2019-11-28 stsp return te->idx;
1193 56e0773d 2019-11-28 stsp struct got_tree_entry *
1194 56e0773d 2019-11-28 stsp got_tree_entry_get_next(struct got_tree_object *tree,
1195 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
1197 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx + 1);
1200 56e0773d 2019-11-28 stsp struct got_tree_entry *
1201 56e0773d 2019-11-28 stsp got_tree_entry_get_prev(struct got_tree_object *tree,
1202 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
1204 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx - 1);
1207 3840f4c9 2018-09-12 stsp static const struct got_error *
1208 ac544f8c 2019-01-13 stsp request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1209 ebc55e2d 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1210 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
1212 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
1213 3840f4c9 2018-09-12 stsp int outfd_child;
1214 3840f4c9 2018-09-12 stsp int basefd, accumfd; /* temporary files for delta application */
1216 3840f4c9 2018-09-12 stsp basefd = got_opentempfd();
1217 3840f4c9 2018-09-12 stsp if (basefd == -1)
1218 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
1219 3840f4c9 2018-09-12 stsp accumfd = got_opentempfd();
1220 3840f4c9 2018-09-12 stsp if (accumfd == -1)
1221 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
1223 3840f4c9 2018-09-12 stsp outfd_child = dup(outfd);
1224 3840f4c9 2018-09-12 stsp if (outfd_child == -1)
1225 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1227 ebc55e2d 2018-12-24 stsp err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1229 3840f4c9 2018-09-12 stsp return err;
1231 3840f4c9 2018-09-12 stsp err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1232 3840f4c9 2018-09-12 stsp outfd_child);
1233 3840f4c9 2018-09-12 stsp if (err) {
1234 41496140 2019-02-21 stsp close(basefd);
1235 41496140 2019-02-21 stsp close(accumfd);
1236 3840f4c9 2018-09-12 stsp return err;
1239 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1241 3840f4c9 2018-09-12 stsp if (err) {
1242 3840f4c9 2018-09-12 stsp close(accumfd);
1243 3840f4c9 2018-09-12 stsp return err;
1246 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1249 3840f4c9 2018-09-12 stsp return err;
1251 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen,
1252 ebc55e2d 2018-12-24 stsp pack->privsep_child->ibuf);
1254 3840f4c9 2018-09-12 stsp return err;
1256 3840f4c9 2018-09-12 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1257 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1259 3840f4c9 2018-09-12 stsp return err;
1262 ebc55e2d 2018-12-24 stsp static const struct got_error *
1263 ac544f8c 2019-01-13 stsp read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1264 ac544f8c 2019-01-13 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1265 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
1267 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
1269 ebc55e2d 2018-12-24 stsp if (pack->privsep_child == NULL) {
1270 ebc55e2d 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
1272 ebc55e2d 2018-12-24 stsp return err;
1275 ac544f8c 2019-01-13 stsp return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1279 9f2369b0 2018-12-24 stsp static const struct got_error *
1280 ac544f8c 2019-01-13 stsp request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1281 ac544f8c 2019-01-13 stsp int infd, struct imsgbuf *ibuf)
1283 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1284 9f2369b0 2018-12-24 stsp int outfd_child;
1286 9f2369b0 2018-12-24 stsp outfd_child = dup(outfd);
1287 9f2369b0 2018-12-24 stsp if (outfd_child == -1)
1288 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1290 9f2369b0 2018-12-24 stsp err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
1292 9f2369b0 2018-12-24 stsp return err;
1294 9f2369b0 2018-12-24 stsp err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1296 9f2369b0 2018-12-24 stsp return err;
1298 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1300 9f2369b0 2018-12-24 stsp return err;
1302 9f2369b0 2018-12-24 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1303 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
1305 9f2369b0 2018-12-24 stsp return err;
1308 9f2369b0 2018-12-24 stsp static const struct got_error *
1309 ac544f8c 2019-01-13 stsp read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1310 9f2369b0 2018-12-24 stsp int outfd, int infd, struct got_repository *repo)
1312 ddc7b220 2019-09-08 stsp const struct got_error *err;
1313 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1314 9f2369b0 2018-12-24 stsp pid_t pid;
1315 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1317 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1318 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1319 ac544f8c 2019-01-13 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1322 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1323 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1324 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1326 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1327 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1328 ddc7b220 2019-09-08 stsp free(ibuf);
1329 ddc7b220 2019-09-08 stsp return err;
1332 9f2369b0 2018-12-24 stsp pid = fork();
1333 ddc7b220 2019-09-08 stsp if (pid == -1) {
1334 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1335 ddc7b220 2019-09-08 stsp free(ibuf);
1336 ddc7b220 2019-09-08 stsp return err;
1338 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1339 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1340 9f2369b0 2018-12-24 stsp repo->path);
1341 9f2369b0 2018-12-24 stsp /* not reached */
1344 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
1345 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1346 ddc7b220 2019-09-08 stsp free(ibuf);
1347 ddc7b220 2019-09-08 stsp return err;
1349 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1350 9f2369b0 2018-12-24 stsp imsg_fds[0];
1351 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1352 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1353 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1355 ac544f8c 2019-01-13 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1358 ebc55e2d 2018-12-24 stsp static const struct got_error *
1359 ebc55e2d 2018-12-24 stsp open_blob(struct got_blob_object **blob, struct got_repository *repo,
1360 ebc55e2d 2018-12-24 stsp struct got_object_id *id, size_t blocksize)
1362 ebc55e2d 2018-12-24 stsp const struct got_error *err = NULL;
1363 ebc55e2d 2018-12-24 stsp struct got_packidx *packidx = NULL;
1365 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1366 ac544f8c 2019-01-13 stsp uint8_t *outbuf;
1367 e82b1d81 2019-07-27 stsp int outfd;
1368 ebc55e2d 2018-12-24 stsp size_t size, hdrlen;
1369 ebc55e2d 2018-12-24 stsp struct stat sb;
1371 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
1372 4558fcd4 2018-01-14 stsp if (*blob == NULL)
1373 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1375 55da3778 2018-09-10 stsp outfd = got_opentempfd();
1376 55da3778 2018-09-10 stsp if (outfd == -1)
1377 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
1379 062ebb78 2018-07-12 stsp (*blob)->read_buf = malloc(blocksize);
1380 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
1381 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1382 c7254d79 2018-04-24 stsp goto done;
1385 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1386 e82b1d81 2019-07-27 stsp if (err == NULL) {
1387 ebc55e2d 2018-12-24 stsp struct got_pack *pack = NULL;
1389 509c9973 2021-04-10 stsp err = got_packidx_get_packfile_path(&path_packfile, packidx);
1391 ebc55e2d 2018-12-24 stsp goto done;
1393 ebc55e2d 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1394 55da3778 2018-09-10 stsp if (pack == NULL) {
1395 ebc55e2d 2018-12-24 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1398 55da3778 2018-09-10 stsp goto done;
1400 ac544f8c 2019-01-13 stsp err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1401 ac544f8c 2019-01-13 stsp pack, packidx, idx, id);
1402 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1405 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&infd, id, repo);
1407 e82b1d81 2019-07-27 stsp goto done;
1408 ac544f8c 2019-01-13 stsp err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1412 55da3778 2018-09-10 stsp goto done;
1414 de060dff 2018-12-24 stsp if (hdrlen > size) {
1415 ebc55e2d 2018-12-24 stsp err = got_error(GOT_ERR_BAD_OBJ_HDR);
1416 ebc55e2d 2018-12-24 stsp goto done;
1419 ac544f8c 2019-01-13 stsp if (outbuf) {
1420 08578a35 2021-01-22 stsp if (close(outfd) == -1 && err == NULL)
1421 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1422 ac544f8c 2019-01-13 stsp outfd = -1;
1423 ac544f8c 2019-01-13 stsp (*blob)->f = fmemopen(outbuf, size, "rb");
1424 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1425 638f9024 2019-05-13 stsp err = got_error_from_errno("fmemopen");
1426 ac544f8c 2019-01-13 stsp free(outbuf);
1427 ac544f8c 2019-01-13 stsp goto done;
1429 ac544f8c 2019-01-13 stsp (*blob)->data = outbuf;
1431 ac544f8c 2019-01-13 stsp if (fstat(outfd, &sb) == -1) {
1432 638f9024 2019-05-13 stsp err = got_error_from_errno("fstat");
1433 ac544f8c 2019-01-13 stsp goto done;
1436 ac544f8c 2019-01-13 stsp if (sb.st_size != size) {
1437 ac544f8c 2019-01-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1438 ac544f8c 2019-01-13 stsp goto done;
1441 ac544f8c 2019-01-13 stsp (*blob)->f = fdopen(outfd, "rb");
1442 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1443 638f9024 2019-05-13 stsp err = got_error_from_errno("fdopen");
1444 ac544f8c 2019-01-13 stsp close(outfd);
1445 ac544f8c 2019-01-13 stsp outfd = -1;
1446 ac544f8c 2019-01-13 stsp goto done;
1450 ebc55e2d 2018-12-24 stsp (*blob)->hdrlen = hdrlen;
1451 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
1452 ebc55e2d 2018-12-24 stsp memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1455 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1456 55da3778 2018-09-10 stsp if (err) {
1457 55da3778 2018-09-10 stsp if (*blob) {
1458 7baf5860 2019-03-19 stsp got_object_blob_close(*blob);
1459 55da3778 2018-09-10 stsp *blob = NULL;
1460 55da3778 2018-09-10 stsp } else if (outfd != -1)
1461 55da3778 2018-09-10 stsp close(outfd);
1463 a19581a2 2018-06-21 stsp return err;
1466 a19581a2 2018-06-21 stsp const struct got_error *
1467 a19581a2 2018-06-21 stsp got_object_open_as_blob(struct got_blob_object **blob,
1468 a19581a2 2018-06-21 stsp struct got_repository *repo, struct got_object_id *id,
1469 a19581a2 2018-06-21 stsp size_t blocksize)
1471 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, id, blocksize);
1474 ebc55e2d 2018-12-24 stsp const struct got_error *
1475 ebc55e2d 2018-12-24 stsp got_object_blob_open(struct got_blob_object **blob,
1476 ebc55e2d 2018-12-24 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
1478 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1481 fb43ecf1 2019-02-11 stsp const struct got_error *
1482 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
1484 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL;
1485 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
1486 56b63ca4 2021-01-22 stsp if (blob->f && fclose(blob->f) == EOF)
1487 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1488 ac544f8c 2019-01-13 stsp free(blob->data);
1489 68482ea3 2017-11-27 stsp free(blob);
1490 fb43ecf1 2019-02-11 stsp return err;
1494 8ba819a3 2020-07-23 stsp got_object_blob_rewind(struct got_blob_object *blob)
1496 8ba819a3 2020-07-23 stsp if (blob->f)
1497 8ba819a3 2020-07-23 stsp rewind(blob->f);
1501 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1503 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1507 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
1509 f934cf2c 2018-02-12 stsp return blob->hdrlen;
1512 f934cf2c 2018-02-12 stsp const uint8_t *
1513 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
1515 f934cf2c 2018-02-12 stsp return blob->read_buf;
1518 68482ea3 2017-11-27 stsp const struct got_error *
1519 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1523 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1524 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
1525 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
1526 eb651edf 2018-02-11 stsp *outlenp = n;
1527 35e9ba5d 2018-06-21 stsp return NULL;
1530 35e9ba5d 2018-06-21 stsp const struct got_error *
1531 be659d10 2020-11-18 stsp got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1532 6c4c42e0 2019-06-24 stsp off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1534 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
1535 b6752625 2018-12-24 stsp size_t n, len, hdrlen;
1536 84451b3e 2018-07-10 stsp const uint8_t *buf;
1538 c33ebc60 2020-11-18 stsp const int alloc_chunksz = 512;
1539 c33ebc60 2020-11-18 stsp size_t nalloc = 0;
1540 f595d9bd 2019-08-14 stsp off_t off = 0, total_len = 0;
1542 6c4c42e0 2019-06-24 stsp if (line_offsets)
1543 6c4c42e0 2019-06-24 stsp *line_offsets = NULL;
1544 f595d9bd 2019-08-14 stsp if (filesize)
1545 f595d9bd 2019-08-14 stsp *filesize = 0;
1546 84451b3e 2018-07-10 stsp if (nlines)
1547 84451b3e 2018-07-10 stsp *nlines = 0;
1549 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1551 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
1553 35e9ba5d 2018-06-21 stsp return err;
1554 35e9ba5d 2018-06-21 stsp if (len == 0)
1556 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
1557 b02560ec 2019-08-19 stsp i = hdrlen;
1558 f1cbc3bc 2020-11-18 stsp if (nlines) {
1559 f1cbc3bc 2020-11-18 stsp if (line_offsets && *line_offsets == NULL) {
1560 78695fb7 2019-08-12 stsp /* Have some data but perhaps no '\n'. */
1561 78695fb7 2019-08-12 stsp *nlines = 1;
1562 c33ebc60 2020-11-18 stsp nalloc = alloc_chunksz;
1563 c33ebc60 2020-11-18 stsp *line_offsets = calloc(nalloc,
1564 c33ebc60 2020-11-18 stsp sizeof(**line_offsets));
1565 78695fb7 2019-08-12 stsp if (*line_offsets == NULL)
1566 845785d4 2020-02-02 tracey return got_error_from_errno("calloc");
1568 b02560ec 2019-08-19 stsp /* Skip forward over end of first line. */
1569 b02560ec 2019-08-19 stsp while (i < len) {
1570 b02560ec 2019-08-19 stsp if (buf[i] == '\n')
1575 b02560ec 2019-08-19 stsp /* Scan '\n' offsets in remaining chunk of data. */
1576 b02560ec 2019-08-19 stsp while (i < len) {
1577 b02560ec 2019-08-19 stsp if (buf[i] != '\n') {
1581 f595d9bd 2019-08-14 stsp (*nlines)++;
1582 c33ebc60 2020-11-18 stsp if (line_offsets && nalloc < *nlines) {
1583 c33ebc60 2020-11-18 stsp size_t n = *nlines + alloc_chunksz;
1584 78695fb7 2019-08-12 stsp off_t *o = recallocarray(*line_offsets,
1585 c33ebc60 2020-11-18 stsp nalloc, n, sizeof(**line_offsets));
1586 78695fb7 2019-08-12 stsp if (o == NULL) {
1587 78695fb7 2019-08-12 stsp free(*line_offsets);
1588 78695fb7 2019-08-12 stsp *line_offsets = NULL;
1589 78695fb7 2019-08-12 stsp return got_error_from_errno(
1590 78695fb7 2019-08-12 stsp "recallocarray");
1592 78695fb7 2019-08-12 stsp *line_offsets = o;
1593 c33ebc60 2020-11-18 stsp nalloc = n;
1595 f1cbc3bc 2020-11-18 stsp if (line_offsets) {
1596 f1cbc3bc 2020-11-18 stsp off = total_len + i - hdrlen + 1;
1597 f1cbc3bc 2020-11-18 stsp (*line_offsets)[*nlines - 1] = off;
1602 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
1603 454a6b59 2018-12-24 stsp n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1604 b6752625 2018-12-24 stsp if (n != len - hdrlen)
1605 b6752625 2018-12-24 stsp return got_ferror(outfile, GOT_ERR_IO);
1606 f595d9bd 2019-08-14 stsp total_len += len - hdrlen;
1607 35e9ba5d 2018-06-21 stsp hdrlen = 0;
1608 35e9ba5d 2018-06-21 stsp } while (len != 0);
1610 cbe7f848 2019-02-11 stsp if (fflush(outfile) != 0)
1611 638f9024 2019-05-13 stsp return got_error_from_errno("fflush");
1612 35e9ba5d 2018-06-21 stsp rewind(outfile);
1614 f595d9bd 2019-08-14 stsp if (filesize)
1615 f595d9bd 2019-08-14 stsp *filesize = total_len;
1617 776d4d29 2018-06-17 stsp return NULL;
1620 f4a881ce 2018-11-17 stsp static const struct got_error *
1621 268f7291 2018-12-24 stsp request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1622 268f7291 2018-12-24 stsp int pack_idx, struct got_object_id *id)
1624 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
1626 268f7291 2018-12-24 stsp err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1627 268f7291 2018-12-24 stsp pack_idx);
1629 a158c901 2018-12-23 stsp return err;
1631 a158c901 2018-12-23 stsp return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1634 268f7291 2018-12-24 stsp static const struct got_error *
1635 268f7291 2018-12-24 stsp read_packed_tag_privsep(struct got_tag_object **tag,
1636 268f7291 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1637 268f7291 2018-12-24 stsp struct got_object_id *id)
1639 268f7291 2018-12-24 stsp const struct got_error *err = NULL;
1641 268f7291 2018-12-24 stsp if (pack->privsep_child)
1642 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1644 268f7291 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
1646 268f7291 2018-12-24 stsp return err;
1648 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1651 9f2369b0 2018-12-24 stsp static const struct got_error *
1652 9f2369b0 2018-12-24 stsp request_tag(struct got_tag_object **tag, struct got_repository *repo,
1655 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1656 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1658 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1660 9f2369b0 2018-12-24 stsp err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1662 9f2369b0 2018-12-24 stsp return err;
1664 9f2369b0 2018-12-24 stsp return got_privsep_recv_tag(tag, ibuf);
1667 9f2369b0 2018-12-24 stsp static const struct got_error *
1668 9f2369b0 2018-12-24 stsp read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1669 9f2369b0 2018-12-24 stsp struct got_repository *repo)
1671 ddc7b220 2019-09-08 stsp const struct got_error *err;
1672 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1673 9f2369b0 2018-12-24 stsp pid_t pid;
1674 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1676 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1677 9f2369b0 2018-12-24 stsp return request_tag(tag, repo, obj_fd);
1679 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1680 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1681 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1683 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1684 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1685 ddc7b220 2019-09-08 stsp free(ibuf);
1686 ddc7b220 2019-09-08 stsp return err;
1689 9f2369b0 2018-12-24 stsp pid = fork();
1690 ddc7b220 2019-09-08 stsp if (pid == -1) {
1691 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1692 ddc7b220 2019-09-08 stsp free(ibuf);
1693 ddc7b220 2019-09-08 stsp return err;
1695 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1696 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1697 9f2369b0 2018-12-24 stsp repo->path);
1698 9f2369b0 2018-12-24 stsp /* not reached */
1701 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
1702 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1703 ddc7b220 2019-09-08 stsp free(ibuf);
1704 ddc7b220 2019-09-08 stsp return err;
1706 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1707 9f2369b0 2018-12-24 stsp imsg_fds[0];
1708 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1709 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1710 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1712 9f2369b0 2018-12-24 stsp return request_tag(tag, repo, obj_fd);
1715 a158c901 2018-12-23 stsp static const struct got_error *
1716 268f7291 2018-12-24 stsp open_tag(struct got_tag_object **tag, struct got_repository *repo,
1717 268f7291 2018-12-24 stsp struct got_object_id *id, int check_cache)
1719 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1720 268f7291 2018-12-24 stsp struct got_packidx *packidx = NULL;
1722 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1723 5d844a1e 2019-08-13 stsp struct got_object *obj = NULL;
1724 5d844a1e 2019-08-13 stsp int obj_type = GOT_OBJ_TYPE_ANY;
1726 f4a881ce 2018-11-17 stsp if (check_cache) {
1727 268f7291 2018-12-24 stsp *tag = got_repo_get_cached_tag(repo, id);
1728 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1729 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1730 f4a881ce 2018-11-17 stsp return NULL;
1733 f4a881ce 2018-11-17 stsp *tag = NULL;
1735 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1736 e82b1d81 2019-07-27 stsp if (err == NULL) {
1737 268f7291 2018-12-24 stsp struct got_pack *pack = NULL;
1739 509c9973 2021-04-10 stsp err = got_packidx_get_packfile_path(&path_packfile, packidx);
1741 268f7291 2018-12-24 stsp return err;
1743 268f7291 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1744 f4a881ce 2018-11-17 stsp if (pack == NULL) {
1745 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1748 8d2c5ea3 2019-08-13 stsp goto done;
1751 992eb9d8 2020-02-07 tracey /* Beware of "lightweight" tags: Check object type first. */
1752 5d844a1e 2019-08-13 stsp err = read_packed_object_privsep(&obj, repo, pack, packidx,
1755 5d844a1e 2019-08-13 stsp goto done;
1756 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1757 5d844a1e 2019-08-13 stsp got_object_close(obj);
1758 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG) {
1759 5d844a1e 2019-08-13 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1760 5d844a1e 2019-08-13 stsp goto done;
1762 5d844a1e 2019-08-13 stsp err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1763 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1766 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1768 e82b1d81 2019-07-27 stsp return err;
1769 5d844a1e 2019-08-13 stsp err = read_object_header_privsep(&obj, repo, fd);
1771 5d844a1e 2019-08-13 stsp return err;
1772 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1773 5d844a1e 2019-08-13 stsp got_object_close(obj);
1774 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
1775 5d844a1e 2019-08-13 stsp return got_error(GOT_ERR_OBJ_TYPE);
1777 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1779 5d844a1e 2019-08-13 stsp return err;
1780 9f2369b0 2018-12-24 stsp err = read_tag_privsep(tag, fd, repo);
1783 f4a881ce 2018-11-17 stsp if (err == NULL) {
1784 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1785 268f7291 2018-12-24 stsp err = got_repo_cache_tag(repo, id, *tag);
1788 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1789 f4a881ce 2018-11-17 stsp return err;
1792 f4a881ce 2018-11-17 stsp const struct got_error *
1793 f4a881ce 2018-11-17 stsp got_object_open_as_tag(struct got_tag_object **tag,
1794 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object_id *id)
1796 f4a881ce 2018-11-17 stsp *tag = got_repo_get_cached_tag(repo, id);
1797 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1798 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1799 f4a881ce 2018-11-17 stsp return NULL;
1802 268f7291 2018-12-24 stsp return open_tag(tag, repo, id, 0);
1805 f4a881ce 2018-11-17 stsp const struct got_error *
1806 f4a881ce 2018-11-17 stsp got_object_tag_open(struct got_tag_object **tag,
1807 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object *obj)
1809 268f7291 2018-12-24 stsp return open_tag(tag, repo, got_object_get_id(obj), 1);
1812 d24820bf 2019-08-11 stsp const char *
1813 d24820bf 2019-08-11 stsp got_object_tag_get_name(struct got_tag_object *tag)
1815 d24820bf 2019-08-11 stsp return tag->tag;
1819 0bd18d37 2019-02-01 stsp got_object_tag_get_object_type(struct got_tag_object *tag)
1821 0bd18d37 2019-02-01 stsp return tag->obj_type;
1824 0bd18d37 2019-02-01 stsp struct got_object_id *
1825 0bd18d37 2019-02-01 stsp got_object_tag_get_object_id(struct got_tag_object *tag)
1827 0bd18d37 2019-02-01 stsp return &tag->id;
1831 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_time(struct got_tag_object *tag)
1833 01073a5d 2019-08-22 stsp return tag->tagger_time;
1837 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1839 01073a5d 2019-08-22 stsp return tag->tagger_gmtoff;
1842 01073a5d 2019-08-22 stsp const char *
1843 01073a5d 2019-08-22 stsp got_object_tag_get_tagger(struct got_tag_object *tag)
1845 01073a5d 2019-08-22 stsp return tag->tagger;
1848 01073a5d 2019-08-22 stsp const char *
1849 01073a5d 2019-08-22 stsp got_object_tag_get_message(struct got_tag_object *tag)
1851 01073a5d 2019-08-22 stsp return tag->tagmsg;
1854 776d4d29 2018-06-17 stsp static struct got_tree_entry *
1855 65a9bbe9 2018-09-15 stsp find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1859 63da309a 2018-11-07 stsp /* Note that tree entries are sorted in strncmp() order. */
1860 56e0773d 2019-11-28 stsp for (i = 0; i < tree->nentries; i++) {
1861 56e0773d 2019-11-28 stsp struct got_tree_entry *te = &tree->entries[i];
1862 63da309a 2018-11-07 stsp int cmp = strncmp(te->name, name, len);
1863 63da309a 2018-11-07 stsp if (cmp < 0)
1865 63da309a 2018-11-07 stsp if (cmp > 0)
1867 63da309a 2018-11-07 stsp if (te->name[len] == '\0')
1868 776d4d29 2018-06-17 stsp return te;
1870 eb651edf 2018-02-11 stsp return NULL;
1873 56e0773d 2019-11-28 stsp struct got_tree_entry *
1874 a129376b 2019-03-28 stsp got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1876 a129376b 2019-03-28 stsp return find_entry_by_name(tree, name, strlen(name));
1879 776d4d29 2018-06-17 stsp const struct got_error *
1880 27d434c2 2018-09-15 stsp got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1881 776d4d29 2018-06-17 stsp struct got_object_id *commit_id, const char *path)
1883 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
1884 776d4d29 2018-06-17 stsp struct got_commit_object *commit = NULL;
1885 776d4d29 2018-06-17 stsp struct got_tree_object *tree = NULL;
1886 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
1887 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1888 b7cd37e5 2018-11-18 stsp size_t seglen;
1890 27d434c2 2018-09-15 stsp *id = NULL;
1892 776d4d29 2018-06-17 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
1894 776d4d29 2018-06-17 stsp goto done;
1896 776d4d29 2018-06-17 stsp /* Handle opening of root of commit's tree. */
1897 5e54fb30 2019-05-31 stsp if (got_path_is_root_dir(path)) {
1898 27d434c2 2018-09-15 stsp *id = got_object_id_dup(commit->tree_id);
1899 27d434c2 2018-09-15 stsp if (*id == NULL)
1900 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
1901 ca008b32 2018-07-22 stsp goto done;
1904 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1906 776d4d29 2018-06-17 stsp goto done;
1909 5e54fb30 2019-05-31 stsp while (s[0] == '/')
1912 65a9bbe9 2018-09-15 stsp seglen = 0;
1913 b7cd37e5 2018-11-18 stsp while (*s) {
1914 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
1916 776d4d29 2018-06-17 stsp if (*s != '/') {
1923 65a9bbe9 2018-09-15 stsp te = find_entry_by_name(tree, seg, seglen);
1924 db37e2c0 2018-06-21 stsp if (te == NULL) {
1925 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1926 776d4d29 2018-06-17 stsp goto done;
1929 b7cd37e5 2018-11-18 stsp if (*s == '\0')
1932 776d4d29 2018-06-17 stsp seg = s + 1;
1933 65a9bbe9 2018-09-15 stsp seglen = 0;
1936 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
1938 db37e2c0 2018-06-21 stsp te = NULL;
1940 776d4d29 2018-06-17 stsp goto done;
1941 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
1942 776d4d29 2018-06-17 stsp tree = next_tree;
1947 56e0773d 2019-11-28 stsp *id = got_object_id_dup(&te->id);
1948 27d434c2 2018-09-15 stsp if (*id == NULL)
1949 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
1951 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
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 509c9973 2021-04-10 stsp err = got_packidx_get_packfile_path(&path_packfile, packidx);
2245 ca6e02ac 2020-01-07 stsp return err;
2247 ca6e02ac 2020-01-07 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
2248 ca6e02ac 2020-01-07 stsp if (pack == NULL) {
2249 ca6e02ac 2020-01-07 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2251 ca6e02ac 2020-01-07 stsp goto done;
2254 ca6e02ac 2020-01-07 stsp if (pack->privsep_child == NULL) {
2255 ca6e02ac 2020-01-07 stsp err = start_pack_privsep_child(pack, packidx);
2257 ca6e02ac 2020-01-07 stsp goto done;
2260 ca6e02ac 2020-01-07 stsp err = got_privsep_send_commit_traversal_request(
2261 ca6e02ac 2020-01-07 stsp pack->privsep_child->ibuf, commit_id, idx, path);
2263 ca6e02ac 2020-01-07 stsp goto done;
2265 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_traversed_commits(&changed_commit,
2266 ca6e02ac 2020-01-07 stsp &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2268 ca6e02ac 2020-01-07 stsp goto done;
2270 ca6e02ac 2020-01-07 stsp if (changed_commit) {
2272 ca6e02ac 2020-01-07 stsp * Cache the commit in which the path was changed.
2273 ca6e02ac 2020-01-07 stsp * This commit might be opened again soon.
2275 ca6e02ac 2020-01-07 stsp changed_commit->refcnt++;
2276 ca6e02ac 2020-01-07 stsp err = got_repo_cache_commit(repo, changed_commit_id,
2277 ca6e02ac 2020-01-07 stsp changed_commit);
2278 ca6e02ac 2020-01-07 stsp got_object_commit_close(changed_commit);
2281 ca6e02ac 2020-01-07 stsp free(path_packfile);
2282 ca6e02ac 2020-01-07 stsp free(changed_commit_id);
2283 ca6e02ac 2020-01-07 stsp return err;