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 *obj = calloc(1, sizeof(**obj));
573 59d1e4a0 2021-03-10 stsp if (*obj == NULL) {
574 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("calloc");
578 59d1e4a0 2021-03-10 stsp (*obj)->read_buf = malloc(blocksize);
579 59d1e4a0 2021-03-10 stsp if ((*obj)->read_buf == NULL) {
580 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("malloc");
584 59d1e4a0 2021-03-10 stsp if (outbuf) {
585 59d1e4a0 2021-03-10 stsp if (close(outfd) == -1) {
586 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("close");
589 59d1e4a0 2021-03-10 stsp outfd = -1;
590 59d1e4a0 2021-03-10 stsp (*obj)->f = fmemopen(outbuf, hdrlen + size, "r");
591 59d1e4a0 2021-03-10 stsp if ((*obj)->f == NULL) {
592 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fdopen");
595 59d1e4a0 2021-03-10 stsp (*obj)->data = outbuf;
597 59d1e4a0 2021-03-10 stsp struct stat sb;
598 59d1e4a0 2021-03-10 stsp if (fstat(outfd, &sb) == -1) {
599 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fstat");
603 a8591711 2021-06-18 stsp if (sb.st_size != hdrlen + size) {
604 59d1e4a0 2021-03-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
608 59d1e4a0 2021-03-10 stsp (*obj)->f = fdopen(outfd, "r");
609 59d1e4a0 2021-03-10 stsp if ((*obj)->f == NULL) {
610 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fdopen");
613 59d1e4a0 2021-03-10 stsp outfd = -1;
614 59d1e4a0 2021-03-10 stsp (*obj)->data = NULL;
616 59d1e4a0 2021-03-10 stsp (*obj)->hdrlen = hdrlen;
617 59d1e4a0 2021-03-10 stsp (*obj)->size = size;
618 59d1e4a0 2021-03-10 stsp (*obj)->blocksize = blocksize;
620 59d1e4a0 2021-03-10 stsp free(path_packfile);
622 59d1e4a0 2021-03-10 stsp if (*obj) {
623 59d1e4a0 2021-03-10 stsp got_object_raw_close(*obj);
624 59d1e4a0 2021-03-10 stsp *obj = NULL;
626 59d1e4a0 2021-03-10 stsp if (outfd != -1)
627 59d1e4a0 2021-03-10 stsp close(outfd);
628 59d1e4a0 2021-03-10 stsp free(outbuf);
630 59d1e4a0 2021-03-10 stsp return err;
634 59d1e4a0 2021-03-10 stsp got_object_raw_rewind(struct got_raw_object *obj)
636 59d1e4a0 2021-03-10 stsp if (obj->f)
637 59d1e4a0 2021-03-10 stsp rewind(obj->f);
641 59d1e4a0 2021-03-10 stsp got_object_raw_get_hdrlen(struct got_raw_object *obj)
643 59d1e4a0 2021-03-10 stsp return obj->hdrlen;
646 59d1e4a0 2021-03-10 stsp const uint8_t *
647 59d1e4a0 2021-03-10 stsp got_object_raw_get_read_buf(struct got_raw_object *obj)
649 59d1e4a0 2021-03-10 stsp return obj->read_buf;
652 59d1e4a0 2021-03-10 stsp const struct got_error *
653 59d1e4a0 2021-03-10 stsp got_object_raw_read_block(size_t *outlenp, struct got_raw_object *obj)
657 59d1e4a0 2021-03-10 stsp n = fread(obj->read_buf, 1, obj->blocksize, obj->f);
658 59d1e4a0 2021-03-10 stsp if (n == 0 && ferror(obj->f))
659 59d1e4a0 2021-03-10 stsp return got_ferror(obj->f, GOT_ERR_IO);
660 59d1e4a0 2021-03-10 stsp *outlenp = n;
661 59d1e4a0 2021-03-10 stsp return NULL;
664 59d1e4a0 2021-03-10 stsp const struct got_error *
665 59d1e4a0 2021-03-10 stsp got_object_raw_close(struct got_raw_object *obj)
667 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
669 59d1e4a0 2021-03-10 stsp free(obj->read_buf);
670 59d1e4a0 2021-03-10 stsp if (obj->f != NULL && fclose(obj->f) == EOF && err == NULL)
671 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fclose");
672 59d1e4a0 2021-03-10 stsp free(obj->data);
674 59d1e4a0 2021-03-10 stsp return err;
677 6dfa2fd3 2018-02-12 stsp const struct got_error *
678 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
679 6dfa2fd3 2018-02-12 stsp const char *id_str)
681 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
683 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
684 6dd1ece6 2019-11-10 stsp return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
686 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
689 15a94983 2018-12-23 stsp const struct got_error *
690 15a94983 2018-12-23 stsp got_object_resolve_id_str(struct got_object_id **id,
691 15a94983 2018-12-23 stsp struct got_repository *repo, const char *id_str)
693 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
694 15a94983 2018-12-23 stsp struct got_object *obj;
696 15a94983 2018-12-23 stsp err = got_object_open_by_id_str(&obj, repo, id_str);
698 15a94983 2018-12-23 stsp return err;
700 15a94983 2018-12-23 stsp *id = got_object_id_dup(got_object_get_id(obj));
701 15a94983 2018-12-23 stsp got_object_close(obj);
702 15a94983 2018-12-23 stsp if (*id == NULL)
703 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
705 15a94983 2018-12-23 stsp return NULL;
708 434025f3 2018-09-16 stsp static const struct got_error *
709 a158c901 2018-12-23 stsp request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
710 a158c901 2018-12-23 stsp int pack_idx, struct got_object_id *id)
712 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
714 a158c901 2018-12-23 stsp err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
717 a158c901 2018-12-23 stsp return err;
719 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
721 ca6e02ac 2020-01-07 stsp return err;
723 ca6e02ac 2020-01-07 stsp (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
724 ca6e02ac 2020-01-07 stsp return NULL;
727 a158c901 2018-12-23 stsp static const struct got_error *
728 a158c901 2018-12-23 stsp read_packed_commit_privsep(struct got_commit_object **commit,
729 a158c901 2018-12-23 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
730 a158c901 2018-12-23 stsp struct got_object_id *id)
732 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
734 a158c901 2018-12-23 stsp if (pack->privsep_child)
735 a158c901 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
737 711fb6e8 2018-12-23 stsp err = start_pack_privsep_child(pack, packidx);
739 a158c901 2018-12-23 stsp return err;
741 711fb6e8 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
744 9f2369b0 2018-12-24 stsp static const struct got_error *
745 9f2369b0 2018-12-24 stsp request_commit(struct got_commit_object **commit, struct got_repository *repo,
748 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
749 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
751 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
753 9f2369b0 2018-12-24 stsp err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
755 9f2369b0 2018-12-24 stsp return err;
757 9f2369b0 2018-12-24 stsp return got_privsep_recv_commit(commit, ibuf);
760 9f2369b0 2018-12-24 stsp static const struct got_error *
761 9f2369b0 2018-12-24 stsp read_commit_privsep(struct got_commit_object **commit, int obj_fd,
762 9f2369b0 2018-12-24 stsp struct got_repository *repo)
764 ddc7b220 2019-09-08 stsp const struct got_error *err;
765 9f2369b0 2018-12-24 stsp int imsg_fds[2];
767 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
769 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
770 9f2369b0 2018-12-24 stsp return request_commit(commit, repo, obj_fd);
772 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
773 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
774 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
776 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
777 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
778 ddc7b220 2019-09-08 stsp free(ibuf);
779 ddc7b220 2019-09-08 stsp return err;
782 9f2369b0 2018-12-24 stsp pid = fork();
783 ddc7b220 2019-09-08 stsp if (pid == -1) {
784 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
785 ddc7b220 2019-09-08 stsp free(ibuf);
786 ddc7b220 2019-09-08 stsp return err;
788 9f2369b0 2018-12-24 stsp else if (pid == 0) {
789 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
790 9f2369b0 2018-12-24 stsp repo->path);
791 9f2369b0 2018-12-24 stsp /* not reached */
794 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
795 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
796 ddc7b220 2019-09-08 stsp free(ibuf);
797 ddc7b220 2019-09-08 stsp return err;
799 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
800 9f2369b0 2018-12-24 stsp imsg_fds[0];
801 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
802 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
803 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
805 9f2369b0 2018-12-24 stsp return request_commit(commit, repo, obj_fd);
809 a158c901 2018-12-23 stsp static const struct got_error *
810 434025f3 2018-09-16 stsp open_commit(struct got_commit_object **commit,
811 1785f84a 2018-12-23 stsp struct got_repository *repo, struct got_object_id *id, int check_cache)
813 434025f3 2018-09-16 stsp const struct got_error *err = NULL;
814 1785f84a 2018-12-23 stsp struct got_packidx *packidx = NULL;
816 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
818 434025f3 2018-09-16 stsp if (check_cache) {
819 1785f84a 2018-12-23 stsp *commit = got_repo_get_cached_commit(repo, id);
820 434025f3 2018-09-16 stsp if (*commit != NULL) {
821 434025f3 2018-09-16 stsp (*commit)->refcnt++;
822 434025f3 2018-09-16 stsp return NULL;
825 434025f3 2018-09-16 stsp *commit = NULL;
827 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
828 e82b1d81 2019-07-27 stsp if (err == NULL) {
829 1785f84a 2018-12-23 stsp struct got_pack *pack = NULL;
831 509c9973 2021-04-10 stsp err = got_packidx_get_packfile_path(&path_packfile, packidx);
833 1785f84a 2018-12-23 stsp return err;
835 1785f84a 2018-12-23 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
836 434025f3 2018-09-16 stsp if (pack == NULL) {
837 1785f84a 2018-12-23 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
842 a158c901 2018-12-23 stsp err = read_packed_commit_privsep(commit, pack,
843 1785f84a 2018-12-23 stsp packidx, idx, id);
844 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
847 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
849 e82b1d81 2019-07-27 stsp return err;
850 9f2369b0 2018-12-24 stsp err = read_commit_privsep(commit, fd, repo);
853 434025f3 2018-09-16 stsp if (err == NULL) {
854 434025f3 2018-09-16 stsp (*commit)->refcnt++;
855 1785f84a 2018-12-23 stsp err = got_repo_cache_commit(repo, id, *commit);
858 8d2c5ea3 2019-08-13 stsp free(path_packfile);
859 e32baab7 2018-11-05 stsp return err;
862 e32baab7 2018-11-05 stsp const struct got_error *
863 e32baab7 2018-11-05 stsp got_object_open_as_commit(struct got_commit_object **commit,
864 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object_id *id)
866 e32baab7 2018-11-05 stsp *commit = got_repo_get_cached_commit(repo, id);
867 e32baab7 2018-11-05 stsp if (*commit != NULL) {
868 e32baab7 2018-11-05 stsp (*commit)->refcnt++;
869 e32baab7 2018-11-05 stsp return NULL;
872 1785f84a 2018-12-23 stsp return open_commit(commit, repo, id, 0);
875 e32baab7 2018-11-05 stsp const struct got_error *
876 e32baab7 2018-11-05 stsp got_object_commit_open(struct got_commit_object **commit,
877 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object *obj)
879 1785f84a 2018-12-23 stsp return open_commit(commit, repo, got_object_get_id(obj), 1);
882 434025f3 2018-09-16 stsp const struct got_error *
883 dbc6a6b6 2018-07-12 stsp got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
885 dbc6a6b6 2018-07-12 stsp const struct got_error *err = NULL;
887 dbc6a6b6 2018-07-12 stsp *qid = calloc(1, sizeof(**qid));
888 dbc6a6b6 2018-07-12 stsp if (*qid == NULL)
889 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
891 dbc6a6b6 2018-07-12 stsp (*qid)->id = got_object_id_dup(id);
892 dbc6a6b6 2018-07-12 stsp if ((*qid)->id == NULL) {
893 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
894 fa2f6902 2018-07-23 stsp got_object_qid_free(*qid);
895 dbc6a6b6 2018-07-12 stsp *qid = NULL;
896 dbc6a6b6 2018-07-12 stsp return err;
899 9ca9aafb 2021-06-18 stsp return NULL;
902 9ca9aafb 2021-06-18 stsp const struct got_error *
903 9ca9aafb 2021-06-18 stsp got_object_id_queue_copy(const struct got_object_id_queue *src,
904 9ca9aafb 2021-06-18 stsp struct got_object_id_queue *dest)
906 9ca9aafb 2021-06-18 stsp const struct got_error *err;
907 9ca9aafb 2021-06-18 stsp struct got_object_qid *qid;
909 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, src, entry) {
910 9ca9aafb 2021-06-18 stsp struct got_object_qid *new;
912 9ca9aafb 2021-06-18 stsp * Deep-copy the object ID only. Let the caller deal
913 9ca9aafb 2021-06-18 stsp * with setting up the new->data pointer if needed.
915 9ca9aafb 2021-06-18 stsp err = got_object_qid_alloc(&new, qid->id);
917 9ca9aafb 2021-06-18 stsp got_object_id_queue_free(dest);
918 9ca9aafb 2021-06-18 stsp return err;
920 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(dest, new, entry);
923 dbc6a6b6 2018-07-12 stsp return NULL;
926 a158c901 2018-12-23 stsp static const struct got_error *
927 13c729f7 2018-12-24 stsp request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
928 13c729f7 2018-12-24 stsp int pack_idx, struct got_object_id *id)
930 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
932 13c729f7 2018-12-24 stsp err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
935 a158c901 2018-12-23 stsp return err;
937 a158c901 2018-12-23 stsp return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
940 71eb0e7f 2018-09-16 stsp static const struct got_error *
941 13c729f7 2018-12-24 stsp read_packed_tree_privsep(struct got_tree_object **tree,
942 13c729f7 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
943 13c729f7 2018-12-24 stsp struct got_object_id *id)
945 13c729f7 2018-12-24 stsp const struct got_error *err = NULL;
947 13c729f7 2018-12-24 stsp if (pack->privsep_child)
948 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
950 13c729f7 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
952 13c729f7 2018-12-24 stsp return err;
954 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
957 13c729f7 2018-12-24 stsp static const struct got_error *
958 9f2369b0 2018-12-24 stsp request_tree(struct got_tree_object **tree, struct got_repository *repo,
961 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
962 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
964 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
966 9f2369b0 2018-12-24 stsp err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
968 9f2369b0 2018-12-24 stsp return err;
970 9f2369b0 2018-12-24 stsp return got_privsep_recv_tree(tree, ibuf);
973 9f2369b0 2018-12-24 stsp const struct got_error *
974 9f2369b0 2018-12-24 stsp read_tree_privsep(struct got_tree_object **tree, int obj_fd,
975 9f2369b0 2018-12-24 stsp struct got_repository *repo)
977 ddc7b220 2019-09-08 stsp const struct got_error *err;
978 9f2369b0 2018-12-24 stsp int imsg_fds[2];
980 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
982 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
983 9f2369b0 2018-12-24 stsp return request_tree(tree, repo, obj_fd);
985 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
986 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
987 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
989 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
990 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
991 ddc7b220 2019-09-08 stsp free(ibuf);
992 ddc7b220 2019-09-08 stsp return err;
995 9f2369b0 2018-12-24 stsp pid = fork();
996 ddc7b220 2019-09-08 stsp if (pid == -1) {
997 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
998 ddc7b220 2019-09-08 stsp free(ibuf);
999 ddc7b220 2019-09-08 stsp return err;
1001 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1002 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1003 9f2369b0 2018-12-24 stsp repo->path);
1004 9f2369b0 2018-12-24 stsp /* not reached */
1007 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
1008 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1009 ddc7b220 2019-09-08 stsp free(ibuf);
1010 ddc7b220 2019-09-08 stsp return err;
1012 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1013 9f2369b0 2018-12-24 stsp imsg_fds[0];
1014 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1015 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1016 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1019 9f2369b0 2018-12-24 stsp return request_tree(tree, repo, obj_fd);
1022 9f2369b0 2018-12-24 stsp static const struct got_error *
1023 13c729f7 2018-12-24 stsp open_tree(struct got_tree_object **tree, struct got_repository *repo,
1024 13c729f7 2018-12-24 stsp struct got_object_id *id, int check_cache)
1026 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
1027 13c729f7 2018-12-24 stsp struct got_packidx *packidx = NULL;
1029 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1031 71eb0e7f 2018-09-16 stsp if (check_cache) {
1032 13c729f7 2018-12-24 stsp *tree = got_repo_get_cached_tree(repo, id);
1033 71eb0e7f 2018-09-16 stsp if (*tree != NULL) {
1034 71eb0e7f 2018-09-16 stsp (*tree)->refcnt++;
1035 71eb0e7f 2018-09-16 stsp return NULL;
1038 71eb0e7f 2018-09-16 stsp *tree = NULL;
1040 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1041 e82b1d81 2019-07-27 stsp if (err == NULL) {
1042 13c729f7 2018-12-24 stsp struct got_pack *pack = NULL;
1044 509c9973 2021-04-10 stsp err = got_packidx_get_packfile_path(&path_packfile, packidx);
1046 13c729f7 2018-12-24 stsp return err;
1048 13c729f7 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1049 e7885405 2018-09-10 stsp if (pack == NULL) {
1050 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1053 8d2c5ea3 2019-08-13 stsp goto done;
1055 13c729f7 2018-12-24 stsp err = read_packed_tree_privsep(tree, pack,
1056 13c729f7 2018-12-24 stsp packidx, idx, id);
1057 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1060 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1062 e82b1d81 2019-07-27 stsp return err;
1063 9f2369b0 2018-12-24 stsp err = read_tree_privsep(tree, fd, repo);
1066 f6be5c30 2018-06-22 stsp if (err == NULL) {
1067 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
1068 13c729f7 2018-12-24 stsp err = got_repo_cache_tree(repo, id, *tree);
1071 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1072 776d4d29 2018-06-17 stsp return err;
1075 776d4d29 2018-06-17 stsp const struct got_error *
1076 776d4d29 2018-06-17 stsp got_object_open_as_tree(struct got_tree_object **tree,
1077 776d4d29 2018-06-17 stsp struct got_repository *repo, struct got_object_id *id)
1079 e8eb494a 2018-09-16 stsp *tree = got_repo_get_cached_tree(repo, id);
1080 e8eb494a 2018-09-16 stsp if (*tree != NULL) {
1081 e8eb494a 2018-09-16 stsp (*tree)->refcnt++;
1082 e8eb494a 2018-09-16 stsp return NULL;
1085 13c729f7 2018-12-24 stsp return open_tree(tree, repo, id, 0);
1088 71eb0e7f 2018-09-16 stsp const struct got_error *
1089 71eb0e7f 2018-09-16 stsp got_object_tree_open(struct got_tree_object **tree,
1090 71eb0e7f 2018-09-16 stsp struct got_repository *repo, struct got_object *obj)
1092 13c729f7 2018-12-24 stsp return open_tree(tree, repo, got_object_get_id(obj), 1);
1096 56e0773d 2019-11-28 stsp got_object_tree_get_nentries(struct got_tree_object *tree)
1098 56e0773d 2019-11-28 stsp return tree->nentries;
1101 56e0773d 2019-11-28 stsp struct got_tree_entry *
1102 56e0773d 2019-11-28 stsp got_object_tree_get_first_entry(struct got_tree_object *tree)
1104 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, 0);
1107 56e0773d 2019-11-28 stsp struct got_tree_entry *
1108 56e0773d 2019-11-28 stsp got_object_tree_get_last_entry(struct got_tree_object *tree)
1110 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, tree->nentries - 1);
1113 56e0773d 2019-11-28 stsp struct got_tree_entry *
1114 56e0773d 2019-11-28 stsp got_object_tree_get_entry(struct got_tree_object *tree, int i)
1116 56e0773d 2019-11-28 stsp if (i < 0 || i >= tree->nentries)
1117 56e0773d 2019-11-28 stsp return NULL;
1118 56e0773d 2019-11-28 stsp return &tree->entries[i];
1122 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(struct got_tree_entry *te)
1124 56e0773d 2019-11-28 stsp return te->mode;
1127 56e0773d 2019-11-28 stsp const char *
1128 56e0773d 2019-11-28 stsp got_tree_entry_get_name(struct got_tree_entry *te)
1130 56e0773d 2019-11-28 stsp return &te->name[0];
1133 56e0773d 2019-11-28 stsp struct got_object_id *
1134 56e0773d 2019-11-28 stsp got_tree_entry_get_id(struct got_tree_entry *te)
1136 56e0773d 2019-11-28 stsp return &te->id;
1139 0d6c6ee3 2020-05-20 stsp const struct got_error *
1140 af57b12a 2020-07-23 stsp got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1142 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
1143 32596e16 2020-07-23 stsp size_t len, totlen, hdrlen, offset;
1145 aa092692 2020-07-23 stsp *s = NULL;
1147 659dc16e 2020-07-23 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1148 659dc16e 2020-07-23 stsp totlen = 0;
1149 32596e16 2020-07-23 stsp offset = 0;
1153 659dc16e 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1155 af57b12a 2020-07-23 stsp return err;
1157 659dc16e 2020-07-23 stsp if (len == 0)
1160 659dc16e 2020-07-23 stsp totlen += len - hdrlen;
1161 af57b12a 2020-07-23 stsp p = realloc(*s, totlen + 1);
1162 659dc16e 2020-07-23 stsp if (p == NULL) {
1163 659dc16e 2020-07-23 stsp err = got_error_from_errno("realloc");
1165 af57b12a 2020-07-23 stsp *s = NULL;
1166 af57b12a 2020-07-23 stsp return err;
1169 659dc16e 2020-07-23 stsp /* Skip blob object header first time around. */
1170 af57b12a 2020-07-23 stsp memcpy(*s + offset,
1171 f8f7c882 2020-07-23 stsp got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1172 659dc16e 2020-07-23 stsp hdrlen = 0;
1173 32596e16 2020-07-23 stsp offset = totlen;
1174 659dc16e 2020-07-23 stsp } while (len > 0);
1176 af57b12a 2020-07-23 stsp (*s)[totlen] = '\0';
1177 af57b12a 2020-07-23 stsp return NULL;
1180 af57b12a 2020-07-23 stsp const struct got_error *
1181 af57b12a 2020-07-23 stsp got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1182 af57b12a 2020-07-23 stsp struct got_repository *repo)
1184 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
1185 af57b12a 2020-07-23 stsp struct got_blob_object *blob = NULL;
1187 af57b12a 2020-07-23 stsp *link_target = NULL;
1189 af57b12a 2020-07-23 stsp if (!got_object_tree_entry_is_symlink(te))
1190 af57b12a 2020-07-23 stsp return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1192 af57b12a 2020-07-23 stsp err = got_object_open_as_blob(&blob, repo,
1193 af57b12a 2020-07-23 stsp got_tree_entry_get_id(te), PATH_MAX);
1195 af57b12a 2020-07-23 stsp return err;
1197 af57b12a 2020-07-23 stsp err = got_object_blob_read_to_str(link_target, blob);
1198 af57b12a 2020-07-23 stsp got_object_blob_close(blob);
1199 659dc16e 2020-07-23 stsp if (err) {
1200 659dc16e 2020-07-23 stsp free(*link_target);
1201 659dc16e 2020-07-23 stsp *link_target = NULL;
1203 0d6c6ee3 2020-05-20 stsp return err;
1207 56e0773d 2019-11-28 stsp got_tree_entry_get_index(struct got_tree_entry *te)
1209 56e0773d 2019-11-28 stsp return te->idx;
1212 56e0773d 2019-11-28 stsp struct got_tree_entry *
1213 56e0773d 2019-11-28 stsp got_tree_entry_get_next(struct got_tree_object *tree,
1214 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
1216 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx + 1);
1219 56e0773d 2019-11-28 stsp struct got_tree_entry *
1220 56e0773d 2019-11-28 stsp got_tree_entry_get_prev(struct got_tree_object *tree,
1221 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
1223 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx - 1);
1226 3840f4c9 2018-09-12 stsp static const struct got_error *
1227 ac544f8c 2019-01-13 stsp request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1228 ebc55e2d 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1229 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
1231 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
1232 3840f4c9 2018-09-12 stsp int outfd_child;
1233 3840f4c9 2018-09-12 stsp int basefd, accumfd; /* temporary files for delta application */
1235 3840f4c9 2018-09-12 stsp basefd = got_opentempfd();
1236 3840f4c9 2018-09-12 stsp if (basefd == -1)
1237 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
1238 3840f4c9 2018-09-12 stsp accumfd = got_opentempfd();
1239 3840f4c9 2018-09-12 stsp if (accumfd == -1)
1240 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
1242 3840f4c9 2018-09-12 stsp outfd_child = dup(outfd);
1243 3840f4c9 2018-09-12 stsp if (outfd_child == -1)
1244 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1246 ebc55e2d 2018-12-24 stsp err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1248 3840f4c9 2018-09-12 stsp return err;
1250 3840f4c9 2018-09-12 stsp err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1251 3840f4c9 2018-09-12 stsp outfd_child);
1252 3840f4c9 2018-09-12 stsp if (err) {
1253 41496140 2019-02-21 stsp close(basefd);
1254 41496140 2019-02-21 stsp close(accumfd);
1255 3840f4c9 2018-09-12 stsp return err;
1258 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1260 3840f4c9 2018-09-12 stsp if (err) {
1261 3840f4c9 2018-09-12 stsp close(accumfd);
1262 3840f4c9 2018-09-12 stsp return err;
1265 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1268 3840f4c9 2018-09-12 stsp return err;
1270 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen,
1271 ebc55e2d 2018-12-24 stsp pack->privsep_child->ibuf);
1273 3840f4c9 2018-09-12 stsp return err;
1275 3840f4c9 2018-09-12 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1276 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1278 3840f4c9 2018-09-12 stsp return err;
1281 ebc55e2d 2018-12-24 stsp static const struct got_error *
1282 ac544f8c 2019-01-13 stsp read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1283 ac544f8c 2019-01-13 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1284 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
1286 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
1288 ebc55e2d 2018-12-24 stsp if (pack->privsep_child == NULL) {
1289 ebc55e2d 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
1291 ebc55e2d 2018-12-24 stsp return err;
1294 ac544f8c 2019-01-13 stsp return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1298 9f2369b0 2018-12-24 stsp static const struct got_error *
1299 ac544f8c 2019-01-13 stsp request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1300 ac544f8c 2019-01-13 stsp int infd, struct imsgbuf *ibuf)
1302 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1303 9f2369b0 2018-12-24 stsp int outfd_child;
1305 9f2369b0 2018-12-24 stsp outfd_child = dup(outfd);
1306 9f2369b0 2018-12-24 stsp if (outfd_child == -1)
1307 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1309 9f2369b0 2018-12-24 stsp err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
1311 9f2369b0 2018-12-24 stsp return err;
1313 9f2369b0 2018-12-24 stsp err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1315 9f2369b0 2018-12-24 stsp return err;
1317 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1319 9f2369b0 2018-12-24 stsp return err;
1321 9f2369b0 2018-12-24 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1322 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
1324 9f2369b0 2018-12-24 stsp return err;
1327 9f2369b0 2018-12-24 stsp static const struct got_error *
1328 ac544f8c 2019-01-13 stsp read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1329 9f2369b0 2018-12-24 stsp int outfd, int infd, struct got_repository *repo)
1331 ddc7b220 2019-09-08 stsp const struct got_error *err;
1332 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1333 9f2369b0 2018-12-24 stsp pid_t pid;
1334 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1336 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1337 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1338 ac544f8c 2019-01-13 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1341 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1342 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1343 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1345 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1346 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1347 ddc7b220 2019-09-08 stsp free(ibuf);
1348 ddc7b220 2019-09-08 stsp return err;
1351 9f2369b0 2018-12-24 stsp pid = fork();
1352 ddc7b220 2019-09-08 stsp if (pid == -1) {
1353 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1354 ddc7b220 2019-09-08 stsp free(ibuf);
1355 ddc7b220 2019-09-08 stsp return err;
1357 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1358 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1359 9f2369b0 2018-12-24 stsp repo->path);
1360 9f2369b0 2018-12-24 stsp /* not reached */
1363 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
1364 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1365 ddc7b220 2019-09-08 stsp free(ibuf);
1366 ddc7b220 2019-09-08 stsp return err;
1368 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1369 9f2369b0 2018-12-24 stsp imsg_fds[0];
1370 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1371 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1372 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1374 ac544f8c 2019-01-13 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1377 ebc55e2d 2018-12-24 stsp static const struct got_error *
1378 ebc55e2d 2018-12-24 stsp open_blob(struct got_blob_object **blob, struct got_repository *repo,
1379 ebc55e2d 2018-12-24 stsp struct got_object_id *id, size_t blocksize)
1381 ebc55e2d 2018-12-24 stsp const struct got_error *err = NULL;
1382 ebc55e2d 2018-12-24 stsp struct got_packidx *packidx = NULL;
1384 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1385 ac544f8c 2019-01-13 stsp uint8_t *outbuf;
1386 e82b1d81 2019-07-27 stsp int outfd;
1387 ebc55e2d 2018-12-24 stsp size_t size, hdrlen;
1388 ebc55e2d 2018-12-24 stsp struct stat sb;
1390 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
1391 4558fcd4 2018-01-14 stsp if (*blob == NULL)
1392 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1394 55da3778 2018-09-10 stsp outfd = got_opentempfd();
1395 55da3778 2018-09-10 stsp if (outfd == -1)
1396 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
1398 062ebb78 2018-07-12 stsp (*blob)->read_buf = malloc(blocksize);
1399 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
1400 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1401 c7254d79 2018-04-24 stsp goto done;
1404 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1405 e82b1d81 2019-07-27 stsp if (err == NULL) {
1406 ebc55e2d 2018-12-24 stsp struct got_pack *pack = NULL;
1408 509c9973 2021-04-10 stsp err = got_packidx_get_packfile_path(&path_packfile, packidx);
1410 ebc55e2d 2018-12-24 stsp goto done;
1412 ebc55e2d 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1413 55da3778 2018-09-10 stsp if (pack == NULL) {
1414 ebc55e2d 2018-12-24 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1417 55da3778 2018-09-10 stsp goto done;
1419 ac544f8c 2019-01-13 stsp err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1420 ac544f8c 2019-01-13 stsp pack, packidx, idx, id);
1421 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1424 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&infd, id, repo);
1426 e82b1d81 2019-07-27 stsp goto done;
1427 ac544f8c 2019-01-13 stsp err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1431 55da3778 2018-09-10 stsp goto done;
1433 de060dff 2018-12-24 stsp if (hdrlen > size) {
1434 ebc55e2d 2018-12-24 stsp err = got_error(GOT_ERR_BAD_OBJ_HDR);
1435 ebc55e2d 2018-12-24 stsp goto done;
1438 ac544f8c 2019-01-13 stsp if (outbuf) {
1439 08578a35 2021-01-22 stsp if (close(outfd) == -1 && err == NULL)
1440 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1441 ac544f8c 2019-01-13 stsp outfd = -1;
1442 ac544f8c 2019-01-13 stsp (*blob)->f = fmemopen(outbuf, size, "rb");
1443 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1444 638f9024 2019-05-13 stsp err = got_error_from_errno("fmemopen");
1445 ac544f8c 2019-01-13 stsp free(outbuf);
1446 ac544f8c 2019-01-13 stsp goto done;
1448 ac544f8c 2019-01-13 stsp (*blob)->data = outbuf;
1450 ac544f8c 2019-01-13 stsp if (fstat(outfd, &sb) == -1) {
1451 638f9024 2019-05-13 stsp err = got_error_from_errno("fstat");
1452 ac544f8c 2019-01-13 stsp goto done;
1455 ac544f8c 2019-01-13 stsp if (sb.st_size != size) {
1456 ac544f8c 2019-01-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1457 ac544f8c 2019-01-13 stsp goto done;
1460 ac544f8c 2019-01-13 stsp (*blob)->f = fdopen(outfd, "rb");
1461 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1462 638f9024 2019-05-13 stsp err = got_error_from_errno("fdopen");
1463 ac544f8c 2019-01-13 stsp close(outfd);
1464 ac544f8c 2019-01-13 stsp outfd = -1;
1465 ac544f8c 2019-01-13 stsp goto done;
1469 ebc55e2d 2018-12-24 stsp (*blob)->hdrlen = hdrlen;
1470 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
1471 ebc55e2d 2018-12-24 stsp memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1474 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1475 55da3778 2018-09-10 stsp if (err) {
1476 55da3778 2018-09-10 stsp if (*blob) {
1477 7baf5860 2019-03-19 stsp got_object_blob_close(*blob);
1478 55da3778 2018-09-10 stsp *blob = NULL;
1479 55da3778 2018-09-10 stsp } else if (outfd != -1)
1480 55da3778 2018-09-10 stsp close(outfd);
1482 a19581a2 2018-06-21 stsp return err;
1485 a19581a2 2018-06-21 stsp const struct got_error *
1486 a19581a2 2018-06-21 stsp got_object_open_as_blob(struct got_blob_object **blob,
1487 a19581a2 2018-06-21 stsp struct got_repository *repo, struct got_object_id *id,
1488 a19581a2 2018-06-21 stsp size_t blocksize)
1490 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, id, blocksize);
1493 ebc55e2d 2018-12-24 stsp const struct got_error *
1494 ebc55e2d 2018-12-24 stsp got_object_blob_open(struct got_blob_object **blob,
1495 ebc55e2d 2018-12-24 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
1497 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1500 fb43ecf1 2019-02-11 stsp const struct got_error *
1501 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
1503 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL;
1504 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
1505 56b63ca4 2021-01-22 stsp if (blob->f && fclose(blob->f) == EOF)
1506 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1507 ac544f8c 2019-01-13 stsp free(blob->data);
1508 68482ea3 2017-11-27 stsp free(blob);
1509 fb43ecf1 2019-02-11 stsp return err;
1513 8ba819a3 2020-07-23 stsp got_object_blob_rewind(struct got_blob_object *blob)
1515 8ba819a3 2020-07-23 stsp if (blob->f)
1516 8ba819a3 2020-07-23 stsp rewind(blob->f);
1520 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1522 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1526 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
1528 f934cf2c 2018-02-12 stsp return blob->hdrlen;
1531 f934cf2c 2018-02-12 stsp const uint8_t *
1532 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
1534 f934cf2c 2018-02-12 stsp return blob->read_buf;
1537 68482ea3 2017-11-27 stsp const struct got_error *
1538 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1542 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1543 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
1544 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
1545 eb651edf 2018-02-11 stsp *outlenp = n;
1546 35e9ba5d 2018-06-21 stsp return NULL;
1549 35e9ba5d 2018-06-21 stsp const struct got_error *
1550 be659d10 2020-11-18 stsp got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1551 6c4c42e0 2019-06-24 stsp off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1553 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
1554 b6752625 2018-12-24 stsp size_t n, len, hdrlen;
1555 84451b3e 2018-07-10 stsp const uint8_t *buf;
1557 c33ebc60 2020-11-18 stsp const int alloc_chunksz = 512;
1558 c33ebc60 2020-11-18 stsp size_t nalloc = 0;
1559 f595d9bd 2019-08-14 stsp off_t off = 0, total_len = 0;
1561 6c4c42e0 2019-06-24 stsp if (line_offsets)
1562 6c4c42e0 2019-06-24 stsp *line_offsets = NULL;
1563 f595d9bd 2019-08-14 stsp if (filesize)
1564 f595d9bd 2019-08-14 stsp *filesize = 0;
1565 84451b3e 2018-07-10 stsp if (nlines)
1566 84451b3e 2018-07-10 stsp *nlines = 0;
1568 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1570 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
1572 35e9ba5d 2018-06-21 stsp return err;
1573 35e9ba5d 2018-06-21 stsp if (len == 0)
1575 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
1576 b02560ec 2019-08-19 stsp i = hdrlen;
1577 f1cbc3bc 2020-11-18 stsp if (nlines) {
1578 f1cbc3bc 2020-11-18 stsp if (line_offsets && *line_offsets == NULL) {
1579 78695fb7 2019-08-12 stsp /* Have some data but perhaps no '\n'. */
1580 78695fb7 2019-08-12 stsp *nlines = 1;
1581 c33ebc60 2020-11-18 stsp nalloc = alloc_chunksz;
1582 c33ebc60 2020-11-18 stsp *line_offsets = calloc(nalloc,
1583 c33ebc60 2020-11-18 stsp sizeof(**line_offsets));
1584 78695fb7 2019-08-12 stsp if (*line_offsets == NULL)
1585 845785d4 2020-02-02 tracey return got_error_from_errno("calloc");
1587 b02560ec 2019-08-19 stsp /* Skip forward over end of first line. */
1588 b02560ec 2019-08-19 stsp while (i < len) {
1589 b02560ec 2019-08-19 stsp if (buf[i] == '\n')
1594 b02560ec 2019-08-19 stsp /* Scan '\n' offsets in remaining chunk of data. */
1595 b02560ec 2019-08-19 stsp while (i < len) {
1596 b02560ec 2019-08-19 stsp if (buf[i] != '\n') {
1600 f595d9bd 2019-08-14 stsp (*nlines)++;
1601 c33ebc60 2020-11-18 stsp if (line_offsets && nalloc < *nlines) {
1602 c33ebc60 2020-11-18 stsp size_t n = *nlines + alloc_chunksz;
1603 78695fb7 2019-08-12 stsp off_t *o = recallocarray(*line_offsets,
1604 c33ebc60 2020-11-18 stsp nalloc, n, sizeof(**line_offsets));
1605 78695fb7 2019-08-12 stsp if (o == NULL) {
1606 78695fb7 2019-08-12 stsp free(*line_offsets);
1607 78695fb7 2019-08-12 stsp *line_offsets = NULL;
1608 78695fb7 2019-08-12 stsp return got_error_from_errno(
1609 78695fb7 2019-08-12 stsp "recallocarray");
1611 78695fb7 2019-08-12 stsp *line_offsets = o;
1612 c33ebc60 2020-11-18 stsp nalloc = n;
1614 f1cbc3bc 2020-11-18 stsp if (line_offsets) {
1615 f1cbc3bc 2020-11-18 stsp off = total_len + i - hdrlen + 1;
1616 f1cbc3bc 2020-11-18 stsp (*line_offsets)[*nlines - 1] = off;
1621 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
1622 454a6b59 2018-12-24 stsp n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1623 b6752625 2018-12-24 stsp if (n != len - hdrlen)
1624 b6752625 2018-12-24 stsp return got_ferror(outfile, GOT_ERR_IO);
1625 f595d9bd 2019-08-14 stsp total_len += len - hdrlen;
1626 35e9ba5d 2018-06-21 stsp hdrlen = 0;
1627 35e9ba5d 2018-06-21 stsp } while (len != 0);
1629 cbe7f848 2019-02-11 stsp if (fflush(outfile) != 0)
1630 638f9024 2019-05-13 stsp return got_error_from_errno("fflush");
1631 35e9ba5d 2018-06-21 stsp rewind(outfile);
1633 f595d9bd 2019-08-14 stsp if (filesize)
1634 f595d9bd 2019-08-14 stsp *filesize = total_len;
1636 776d4d29 2018-06-17 stsp return NULL;
1639 f4a881ce 2018-11-17 stsp static const struct got_error *
1640 268f7291 2018-12-24 stsp request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1641 268f7291 2018-12-24 stsp int pack_idx, struct got_object_id *id)
1643 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
1645 268f7291 2018-12-24 stsp err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1646 268f7291 2018-12-24 stsp pack_idx);
1648 a158c901 2018-12-23 stsp return err;
1650 a158c901 2018-12-23 stsp return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1653 268f7291 2018-12-24 stsp static const struct got_error *
1654 268f7291 2018-12-24 stsp read_packed_tag_privsep(struct got_tag_object **tag,
1655 268f7291 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1656 268f7291 2018-12-24 stsp struct got_object_id *id)
1658 268f7291 2018-12-24 stsp const struct got_error *err = NULL;
1660 268f7291 2018-12-24 stsp if (pack->privsep_child)
1661 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1663 268f7291 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
1665 268f7291 2018-12-24 stsp return err;
1667 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1670 9f2369b0 2018-12-24 stsp static const struct got_error *
1671 9f2369b0 2018-12-24 stsp request_tag(struct got_tag_object **tag, struct got_repository *repo,
1674 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1675 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1677 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1679 9f2369b0 2018-12-24 stsp err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1681 9f2369b0 2018-12-24 stsp return err;
1683 9f2369b0 2018-12-24 stsp return got_privsep_recv_tag(tag, ibuf);
1686 9f2369b0 2018-12-24 stsp static const struct got_error *
1687 9f2369b0 2018-12-24 stsp read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1688 9f2369b0 2018-12-24 stsp struct got_repository *repo)
1690 ddc7b220 2019-09-08 stsp const struct got_error *err;
1691 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1692 9f2369b0 2018-12-24 stsp pid_t pid;
1693 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1695 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1696 9f2369b0 2018-12-24 stsp return request_tag(tag, repo, obj_fd);
1698 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1699 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1700 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1702 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1703 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1704 ddc7b220 2019-09-08 stsp free(ibuf);
1705 ddc7b220 2019-09-08 stsp return err;
1708 9f2369b0 2018-12-24 stsp pid = fork();
1709 ddc7b220 2019-09-08 stsp if (pid == -1) {
1710 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1711 ddc7b220 2019-09-08 stsp free(ibuf);
1712 ddc7b220 2019-09-08 stsp return err;
1714 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1715 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1716 9f2369b0 2018-12-24 stsp repo->path);
1717 9f2369b0 2018-12-24 stsp /* not reached */
1720 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
1721 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1722 ddc7b220 2019-09-08 stsp free(ibuf);
1723 ddc7b220 2019-09-08 stsp return err;
1725 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1726 9f2369b0 2018-12-24 stsp imsg_fds[0];
1727 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1728 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1729 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1731 9f2369b0 2018-12-24 stsp return request_tag(tag, repo, obj_fd);
1734 a158c901 2018-12-23 stsp static const struct got_error *
1735 268f7291 2018-12-24 stsp open_tag(struct got_tag_object **tag, struct got_repository *repo,
1736 268f7291 2018-12-24 stsp struct got_object_id *id, int check_cache)
1738 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1739 268f7291 2018-12-24 stsp struct got_packidx *packidx = NULL;
1741 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1742 5d844a1e 2019-08-13 stsp struct got_object *obj = NULL;
1743 5d844a1e 2019-08-13 stsp int obj_type = GOT_OBJ_TYPE_ANY;
1745 f4a881ce 2018-11-17 stsp if (check_cache) {
1746 268f7291 2018-12-24 stsp *tag = got_repo_get_cached_tag(repo, id);
1747 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1748 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1749 f4a881ce 2018-11-17 stsp return NULL;
1752 f4a881ce 2018-11-17 stsp *tag = NULL;
1754 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1755 e82b1d81 2019-07-27 stsp if (err == NULL) {
1756 268f7291 2018-12-24 stsp struct got_pack *pack = NULL;
1758 509c9973 2021-04-10 stsp err = got_packidx_get_packfile_path(&path_packfile, packidx);
1760 268f7291 2018-12-24 stsp return err;
1762 268f7291 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1763 f4a881ce 2018-11-17 stsp if (pack == NULL) {
1764 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1767 8d2c5ea3 2019-08-13 stsp goto done;
1770 992eb9d8 2020-02-07 tracey /* Beware of "lightweight" tags: Check object type first. */
1771 5d844a1e 2019-08-13 stsp err = read_packed_object_privsep(&obj, repo, pack, packidx,
1774 5d844a1e 2019-08-13 stsp goto done;
1775 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1776 5d844a1e 2019-08-13 stsp got_object_close(obj);
1777 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG) {
1778 5d844a1e 2019-08-13 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1779 5d844a1e 2019-08-13 stsp goto done;
1781 5d844a1e 2019-08-13 stsp err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1782 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1785 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1787 e82b1d81 2019-07-27 stsp return err;
1788 5d844a1e 2019-08-13 stsp err = read_object_header_privsep(&obj, repo, fd);
1790 5d844a1e 2019-08-13 stsp return err;
1791 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1792 5d844a1e 2019-08-13 stsp got_object_close(obj);
1793 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
1794 5d844a1e 2019-08-13 stsp return got_error(GOT_ERR_OBJ_TYPE);
1796 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1798 5d844a1e 2019-08-13 stsp return err;
1799 9f2369b0 2018-12-24 stsp err = read_tag_privsep(tag, fd, repo);
1802 f4a881ce 2018-11-17 stsp if (err == NULL) {
1803 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1804 268f7291 2018-12-24 stsp err = got_repo_cache_tag(repo, id, *tag);
1807 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1808 f4a881ce 2018-11-17 stsp return err;
1811 f4a881ce 2018-11-17 stsp const struct got_error *
1812 f4a881ce 2018-11-17 stsp got_object_open_as_tag(struct got_tag_object **tag,
1813 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object_id *id)
1815 f4a881ce 2018-11-17 stsp *tag = got_repo_get_cached_tag(repo, id);
1816 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1817 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1818 f4a881ce 2018-11-17 stsp return NULL;
1821 268f7291 2018-12-24 stsp return open_tag(tag, repo, id, 0);
1824 f4a881ce 2018-11-17 stsp const struct got_error *
1825 f4a881ce 2018-11-17 stsp got_object_tag_open(struct got_tag_object **tag,
1826 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object *obj)
1828 268f7291 2018-12-24 stsp return open_tag(tag, repo, got_object_get_id(obj), 1);
1831 d24820bf 2019-08-11 stsp const char *
1832 d24820bf 2019-08-11 stsp got_object_tag_get_name(struct got_tag_object *tag)
1834 d24820bf 2019-08-11 stsp return tag->tag;
1838 0bd18d37 2019-02-01 stsp got_object_tag_get_object_type(struct got_tag_object *tag)
1840 0bd18d37 2019-02-01 stsp return tag->obj_type;
1843 0bd18d37 2019-02-01 stsp struct got_object_id *
1844 0bd18d37 2019-02-01 stsp got_object_tag_get_object_id(struct got_tag_object *tag)
1846 0bd18d37 2019-02-01 stsp return &tag->id;
1850 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_time(struct got_tag_object *tag)
1852 01073a5d 2019-08-22 stsp return tag->tagger_time;
1856 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1858 01073a5d 2019-08-22 stsp return tag->tagger_gmtoff;
1861 01073a5d 2019-08-22 stsp const char *
1862 01073a5d 2019-08-22 stsp got_object_tag_get_tagger(struct got_tag_object *tag)
1864 01073a5d 2019-08-22 stsp return tag->tagger;
1867 01073a5d 2019-08-22 stsp const char *
1868 01073a5d 2019-08-22 stsp got_object_tag_get_message(struct got_tag_object *tag)
1870 01073a5d 2019-08-22 stsp return tag->tagmsg;
1873 776d4d29 2018-06-17 stsp static struct got_tree_entry *
1874 65a9bbe9 2018-09-15 stsp find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1878 63da309a 2018-11-07 stsp /* Note that tree entries are sorted in strncmp() order. */
1879 56e0773d 2019-11-28 stsp for (i = 0; i < tree->nentries; i++) {
1880 56e0773d 2019-11-28 stsp struct got_tree_entry *te = &tree->entries[i];
1881 63da309a 2018-11-07 stsp int cmp = strncmp(te->name, name, len);
1882 63da309a 2018-11-07 stsp if (cmp < 0)
1884 63da309a 2018-11-07 stsp if (cmp > 0)
1886 63da309a 2018-11-07 stsp if (te->name[len] == '\0')
1887 776d4d29 2018-06-17 stsp return te;
1889 eb651edf 2018-02-11 stsp return NULL;
1892 56e0773d 2019-11-28 stsp struct got_tree_entry *
1893 a129376b 2019-03-28 stsp got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1895 a129376b 2019-03-28 stsp return find_entry_by_name(tree, name, strlen(name));
1898 776d4d29 2018-06-17 stsp const struct got_error *
1899 27d434c2 2018-09-15 stsp got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1900 776d4d29 2018-06-17 stsp struct got_object_id *commit_id, const char *path)
1902 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
1903 776d4d29 2018-06-17 stsp struct got_commit_object *commit = NULL;
1904 776d4d29 2018-06-17 stsp struct got_tree_object *tree = NULL;
1905 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
1906 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1907 b7cd37e5 2018-11-18 stsp size_t seglen;
1909 27d434c2 2018-09-15 stsp *id = NULL;
1911 776d4d29 2018-06-17 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
1913 776d4d29 2018-06-17 stsp goto done;
1915 776d4d29 2018-06-17 stsp /* Handle opening of root of commit's tree. */
1916 5e54fb30 2019-05-31 stsp if (got_path_is_root_dir(path)) {
1917 27d434c2 2018-09-15 stsp *id = got_object_id_dup(commit->tree_id);
1918 27d434c2 2018-09-15 stsp if (*id == NULL)
1919 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
1920 ca008b32 2018-07-22 stsp goto done;
1923 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1925 776d4d29 2018-06-17 stsp goto done;
1928 5e54fb30 2019-05-31 stsp while (s[0] == '/')
1931 65a9bbe9 2018-09-15 stsp seglen = 0;
1932 b7cd37e5 2018-11-18 stsp while (*s) {
1933 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
1935 776d4d29 2018-06-17 stsp if (*s != '/') {
1942 65a9bbe9 2018-09-15 stsp te = find_entry_by_name(tree, seg, seglen);
1943 db37e2c0 2018-06-21 stsp if (te == NULL) {
1944 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1945 776d4d29 2018-06-17 stsp goto done;
1948 b7cd37e5 2018-11-18 stsp if (*s == '\0')
1951 776d4d29 2018-06-17 stsp seg = s + 1;
1952 65a9bbe9 2018-09-15 stsp seglen = 0;
1955 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
1957 db37e2c0 2018-06-21 stsp te = NULL;
1959 776d4d29 2018-06-17 stsp goto done;
1960 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
1961 776d4d29 2018-06-17 stsp tree = next_tree;
1966 56e0773d 2019-11-28 stsp *id = got_object_id_dup(&te->id);
1967 27d434c2 2018-09-15 stsp if (*id == NULL)
1968 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
1970 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1972 776d4d29 2018-06-17 stsp if (commit)
1973 776d4d29 2018-06-17 stsp got_object_commit_close(commit);
1975 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
1976 776d4d29 2018-06-17 stsp return err;
1980 ac5f2b26 2020-05-05 stsp * Normalize file mode bits to avoid false positive tree entry differences
1981 ac5f2b26 2020-05-05 stsp * in case tree entries have unexpected mode bits set.
1983 ac5f2b26 2020-05-05 stsp static mode_t
1984 ac5f2b26 2020-05-05 stsp normalize_mode_for_comparison(mode_t mode)
1987 ac5f2b26 2020-05-05 stsp * For directories, the only relevant bit is the IFDIR bit.
1988 ac5f2b26 2020-05-05 stsp * This allows us to detect paths changing from a directory
1989 ac5f2b26 2020-05-05 stsp * to a file and vice versa.
1991 ac5f2b26 2020-05-05 stsp if (S_ISDIR(mode))
1992 ac5f2b26 2020-05-05 stsp return mode & S_IFDIR;
1995 40dde666 2020-07-23 stsp * For symlinks, the only relevant bit is the IFLNK bit.
1996 40dde666 2020-07-23 stsp * This allows us to detect paths changing from a symlinks
1997 40dde666 2020-07-23 stsp * to a file or directory and vice versa.
1999 40dde666 2020-07-23 stsp if (S_ISLNK(mode))
2000 40dde666 2020-07-23 stsp return mode & S_IFLNK;
2002 ac5f2b26 2020-05-05 stsp /* For files, the only change we care about is the executable bit. */
2003 ac5f2b26 2020-05-05 stsp return mode & S_IXUSR;
2006 07862c20 2018-09-15 stsp const struct got_error *
2007 07862c20 2018-09-15 stsp got_object_tree_path_changed(int *changed,
2008 07862c20 2018-09-15 stsp struct got_tree_object *tree01, struct got_tree_object *tree02,
2009 07862c20 2018-09-15 stsp const char *path, struct got_repository *repo)
2011 07862c20 2018-09-15 stsp const struct got_error *err = NULL;
2012 07862c20 2018-09-15 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2013 07862c20 2018-09-15 stsp struct got_tree_entry *te1 = NULL, *te2 = NULL;
2014 65a9bbe9 2018-09-15 stsp const char *seg, *s;
2015 3b7f9878 2018-11-18 stsp size_t seglen;
2017 07862c20 2018-09-15 stsp *changed = 0;
2019 07862c20 2018-09-15 stsp /* We not do support comparing the root path. */
2020 61a7d79f 2020-02-29 stsp if (got_path_is_root_dir(path))
2021 63f810e6 2020-02-29 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
2023 07862c20 2018-09-15 stsp tree1 = tree01;
2024 07862c20 2018-09-15 stsp tree2 = tree02;
2026 61a7d79f 2020-02-29 stsp while (*s == '/')
2029 65a9bbe9 2018-09-15 stsp seglen = 0;
2030 3b7f9878 2018-11-18 stsp while (*s) {
2031 07862c20 2018-09-15 stsp struct got_tree_object *next_tree1, *next_tree2;
2032 ac5f2b26 2020-05-05 stsp mode_t mode1, mode2;
2034 07862c20 2018-09-15 stsp if (*s != '/') {
2041 65a9bbe9 2018-09-15 stsp te1 = find_entry_by_name(tree1, seg, seglen);
2042 07862c20 2018-09-15 stsp if (te1 == NULL) {
2043 07862c20 2018-09-15 stsp err = got_error(GOT_ERR_NO_OBJ);
2044 07862c20 2018-09-15 stsp goto done;
2047 e8bfb8f3 2020-12-18 stsp if (tree2)
2048 e8bfb8f3 2020-12-18 stsp te2 = find_entry_by_name(tree2, seg, seglen);
2050 e8bfb8f3 2020-12-18 stsp if (te2) {
2051 e8bfb8f3 2020-12-18 stsp mode1 = normalize_mode_for_comparison(te1->mode);
2052 e8bfb8f3 2020-12-18 stsp mode2 = normalize_mode_for_comparison(te2->mode);
2053 e8bfb8f3 2020-12-18 stsp if (mode1 != mode2) {
2054 e8bfb8f3 2020-12-18 stsp *changed = 1;
2055 e8bfb8f3 2020-12-18 stsp goto done;
2058 e8bfb8f3 2020-12-18 stsp if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2059 e8bfb8f3 2020-12-18 stsp *changed = 0;
2060 e8bfb8f3 2020-12-18 stsp goto done;
2064 3b7f9878 2018-11-18 stsp if (*s == '\0') { /* final path element */
2065 07862c20 2018-09-15 stsp *changed = 1;
2066 07862c20 2018-09-15 stsp goto done;
2069 07862c20 2018-09-15 stsp seg = s + 1;
2071 65a9bbe9 2018-09-15 stsp seglen = 0;
2073 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree1, repo,
2074 56e0773d 2019-11-28 stsp &te1->id);
2075 07862c20 2018-09-15 stsp te1 = NULL;
2077 07862c20 2018-09-15 stsp goto done;
2078 a31cea73 2018-09-15 stsp if (tree1 != tree01)
2079 a31cea73 2018-09-15 stsp got_object_tree_close(tree1);
2080 07862c20 2018-09-15 stsp tree1 = next_tree1;
2082 e8bfb8f3 2020-12-18 stsp if (te2) {
2083 e8bfb8f3 2020-12-18 stsp err = got_object_open_as_tree(&next_tree2, repo,
2084 e8bfb8f3 2020-12-18 stsp &te2->id);
2085 e8bfb8f3 2020-12-18 stsp te2 = NULL;
2087 e8bfb8f3 2020-12-18 stsp goto done;
2088 e8bfb8f3 2020-12-18 stsp if (tree2 != tree02)
2089 e8bfb8f3 2020-12-18 stsp got_object_tree_close(tree2);
2090 e8bfb8f3 2020-12-18 stsp tree2 = next_tree2;
2091 e8bfb8f3 2020-12-18 stsp } else if (tree2) {
2092 e8bfb8f3 2020-12-18 stsp if (tree2 != tree02)
2093 e8bfb8f3 2020-12-18 stsp got_object_tree_close(tree2);
2094 e8bfb8f3 2020-12-18 stsp tree2 = NULL;
2099 a31cea73 2018-09-15 stsp if (tree1 && tree1 != tree01)
2100 07862c20 2018-09-15 stsp got_object_tree_close(tree1);
2101 a31cea73 2018-09-15 stsp if (tree2 && tree2 != tree02)
2102 07862c20 2018-09-15 stsp got_object_tree_close(tree2);
2103 77880158 2018-11-04 stsp return err;
2106 ed175427 2019-05-09 stsp const struct got_error *
2107 ed175427 2019-05-09 stsp got_object_tree_entry_dup(struct got_tree_entry **new_te,
2108 ed175427 2019-05-09 stsp struct got_tree_entry *te)
2110 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
2112 ed175427 2019-05-09 stsp *new_te = calloc(1, sizeof(**new_te));
2113 ed175427 2019-05-09 stsp if (*new_te == NULL)
2114 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
2116 ed175427 2019-05-09 stsp (*new_te)->mode = te->mode;
2117 56e0773d 2019-11-28 stsp memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2118 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2119 8c4eabf2 2019-05-10 stsp return err;
2123 56e0773d 2019-11-28 stsp got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2125 63c5ca5d 2019-08-24 stsp return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2129 e40622f4 2020-07-23 stsp got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2131 e40622f4 2020-07-23 stsp /* S_IFDIR check avoids confusing symlinks with submodules. */
2132 e40622f4 2020-07-23 stsp return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2135 e40622f4 2020-07-23 stsp static const struct got_error *
2136 e40622f4 2020-07-23 stsp resolve_symlink(char **link_target, const char *path,
2137 e40622f4 2020-07-23 stsp struct got_object_id *commit_id, struct got_repository *repo)
2139 e40622f4 2020-07-23 stsp const struct got_error *err = NULL;
2140 dbdd6209 2020-10-19 stsp char buf[PATH_MAX];
2141 e40622f4 2020-07-23 stsp char *name, *parent_path = NULL;
2142 e40622f4 2020-07-23 stsp struct got_object_id *tree_obj_id = NULL;
2143 e40622f4 2020-07-23 stsp struct got_tree_object *tree = NULL;
2144 e40622f4 2020-07-23 stsp struct got_tree_entry *te = NULL;
2146 e40622f4 2020-07-23 stsp *link_target = NULL;
2148 dbdd6209 2020-10-19 stsp if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2149 dbdd6209 2020-10-19 stsp return got_error(GOT_ERR_NO_SPACE);
2151 dbdd6209 2020-10-19 stsp name = basename(buf);
2152 e40622f4 2020-07-23 stsp if (name == NULL)
2153 e40622f4 2020-07-23 stsp return got_error_from_errno2("basename", path);
2155 e40622f4 2020-07-23 stsp err = got_path_dirname(&parent_path, path);
2157 e40622f4 2020-07-23 stsp return err;
2159 e40622f4 2020-07-23 stsp err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
2160 e40622f4 2020-07-23 stsp parent_path);
2161 e40622f4 2020-07-23 stsp if (err) {
2162 e40622f4 2020-07-23 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2163 e40622f4 2020-07-23 stsp /* Display the complete path in error message. */
2164 e40622f4 2020-07-23 stsp err = got_error_path(path, err->code);
2166 e40622f4 2020-07-23 stsp goto done;
2169 e40622f4 2020-07-23 stsp err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2171 e40622f4 2020-07-23 stsp goto done;
2173 e40622f4 2020-07-23 stsp te = got_object_tree_find_entry(tree, name);
2174 e40622f4 2020-07-23 stsp if (te == NULL) {
2175 e40622f4 2020-07-23 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2176 e40622f4 2020-07-23 stsp goto done;
2179 e40622f4 2020-07-23 stsp if (got_object_tree_entry_is_symlink(te)) {
2180 e40622f4 2020-07-23 stsp err = got_tree_entry_get_symlink_target(link_target, te, repo);
2182 e40622f4 2020-07-23 stsp goto done;
2183 e40622f4 2020-07-23 stsp if (!got_path_is_absolute(*link_target)) {
2184 e40622f4 2020-07-23 stsp char *abspath;
2185 e40622f4 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", parent_path,
2186 e40622f4 2020-07-23 stsp *link_target) == -1) {
2187 e40622f4 2020-07-23 stsp err = got_error_from_errno("asprintf");
2188 e40622f4 2020-07-23 stsp goto done;
2190 e40622f4 2020-07-23 stsp free(*link_target);
2191 e40622f4 2020-07-23 stsp *link_target = malloc(PATH_MAX);
2192 e40622f4 2020-07-23 stsp if (*link_target == NULL) {
2193 e40622f4 2020-07-23 stsp err = got_error_from_errno("malloc");
2194 e40622f4 2020-07-23 stsp goto done;
2196 e40622f4 2020-07-23 stsp err = got_canonpath(abspath, *link_target, PATH_MAX);
2197 e40622f4 2020-07-23 stsp free(abspath);
2199 e40622f4 2020-07-23 stsp goto done;
2203 e40622f4 2020-07-23 stsp free(tree_obj_id);
2205 e40622f4 2020-07-23 stsp got_object_tree_close(tree);
2206 e40622f4 2020-07-23 stsp if (err) {
2207 e40622f4 2020-07-23 stsp free(*link_target);
2208 e40622f4 2020-07-23 stsp *link_target = NULL;
2210 e40622f4 2020-07-23 stsp return err;
2213 ca6e02ac 2020-01-07 stsp const struct got_error *
2214 e40622f4 2020-07-23 stsp got_object_resolve_symlinks(char **link_target, const char *path,
2215 e40622f4 2020-07-23 stsp struct got_object_id *commit_id, struct got_repository *repo)
2217 e40622f4 2020-07-23 stsp const struct got_error *err = NULL;
2218 e40622f4 2020-07-23 stsp char *next_target = NULL;
2219 e40622f4 2020-07-23 stsp int max_recursion = 40; /* matches Git */
2221 e40622f4 2020-07-23 stsp *link_target = NULL;
2224 e40622f4 2020-07-23 stsp err = resolve_symlink(&next_target,
2225 e40622f4 2020-07-23 stsp *link_target ? *link_target : path, commit_id, repo);
2228 e40622f4 2020-07-23 stsp if (next_target) {
2229 e40622f4 2020-07-23 stsp free(*link_target);
2230 e40622f4 2020-07-23 stsp if (--max_recursion == 0) {
2231 e40622f4 2020-07-23 stsp err = got_error_path(path, GOT_ERR_RECURSION);
2232 e40622f4 2020-07-23 stsp *link_target = NULL;
2235 e40622f4 2020-07-23 stsp *link_target = next_target;
2237 e40622f4 2020-07-23 stsp } while (next_target);
2239 e40622f4 2020-07-23 stsp return err;
2242 e40622f4 2020-07-23 stsp const struct got_error *
2243 ca6e02ac 2020-01-07 stsp got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2244 ca6e02ac 2020-01-07 stsp struct got_object_id *commit_id, const char *path,
2245 ca6e02ac 2020-01-07 stsp struct got_repository *repo)
2247 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
2248 ca6e02ac 2020-01-07 stsp struct got_pack *pack = NULL;
2249 ca6e02ac 2020-01-07 stsp struct got_packidx *packidx = NULL;
2250 ca6e02ac 2020-01-07 stsp char *path_packfile = NULL;
2251 ca6e02ac 2020-01-07 stsp struct got_commit_object *changed_commit = NULL;
2252 ca6e02ac 2020-01-07 stsp struct got_object_id *changed_commit_id = NULL;
2255 ca6e02ac 2020-01-07 stsp err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2256 ca6e02ac 2020-01-07 stsp if (err) {
2257 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
2258 ca6e02ac 2020-01-07 stsp return err;
2259 ca6e02ac 2020-01-07 stsp return NULL;
2262 509c9973 2021-04-10 stsp err = got_packidx_get_packfile_path(&path_packfile, packidx);
2264 ca6e02ac 2020-01-07 stsp return err;
2266 ca6e02ac 2020-01-07 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
2267 ca6e02ac 2020-01-07 stsp if (pack == NULL) {
2268 ca6e02ac 2020-01-07 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2270 ca6e02ac 2020-01-07 stsp goto done;
2273 ca6e02ac 2020-01-07 stsp if (pack->privsep_child == NULL) {
2274 ca6e02ac 2020-01-07 stsp err = start_pack_privsep_child(pack, packidx);
2276 ca6e02ac 2020-01-07 stsp goto done;
2279 ca6e02ac 2020-01-07 stsp err = got_privsep_send_commit_traversal_request(
2280 ca6e02ac 2020-01-07 stsp pack->privsep_child->ibuf, commit_id, idx, path);
2282 ca6e02ac 2020-01-07 stsp goto done;
2284 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_traversed_commits(&changed_commit,
2285 ca6e02ac 2020-01-07 stsp &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2287 ca6e02ac 2020-01-07 stsp goto done;
2289 ca6e02ac 2020-01-07 stsp if (changed_commit) {
2291 ca6e02ac 2020-01-07 stsp * Cache the commit in which the path was changed.
2292 ca6e02ac 2020-01-07 stsp * This commit might be opened again soon.
2294 ca6e02ac 2020-01-07 stsp changed_commit->refcnt++;
2295 ca6e02ac 2020-01-07 stsp err = got_repo_cache_commit(repo, changed_commit_id,
2296 ca6e02ac 2020-01-07 stsp changed_commit);
2297 ca6e02ac 2020-01-07 stsp got_object_commit_close(changed_commit);
2300 ca6e02ac 2020-01-07 stsp free(path_packfile);
2301 ca6e02ac 2020-01-07 stsp free(changed_commit_id);
2302 ca6e02ac 2020-01-07 stsp return err;