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 876c234b 2018-09-10 stsp #include <sys/syslimits.h>
24 b48e2ddb 2019-05-22 stsp #include <sys/resource.h>
26 a1fd68d8 2018-01-12 stsp #include <errno.h>
27 2178c42e 2018-04-22 stsp #include <fcntl.h>
28 d71d75ad 2017-11-05 stsp #include <stdio.h>
29 ab9a70b2 2017-11-06 stsp #include <stdlib.h>
30 ab9a70b2 2017-11-06 stsp #include <string.h>
31 2178c42e 2018-04-22 stsp #include <stdint.h>
32 d71d75ad 2017-11-05 stsp #include <sha1.h>
33 ab9a70b2 2017-11-06 stsp #include <zlib.h>
34 ab9a70b2 2017-11-06 stsp #include <ctype.h>
35 ab9a70b2 2017-11-06 stsp #include <limits.h>
36 2178c42e 2018-04-22 stsp #include <imsg.h>
37 788c352e 2018-06-16 stsp #include <time.h>
39 ab9a70b2 2017-11-06 stsp #include "got_error.h"
40 d71d75ad 2017-11-05 stsp #include "got_object.h"
41 ab9a70b2 2017-11-06 stsp #include "got_repository.h"
42 511a516b 2018-05-19 stsp #include "got_opentemp.h"
43 324d37e7 2019-05-11 stsp #include "got_path.h"
45 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
46 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
47 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
48 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
49 2178c42e 2018-04-22 stsp #include "got_lib_privsep.h"
50 6bef87be 2018-09-11 stsp #include "got_lib_object_idcache.h"
51 6bef87be 2018-09-11 stsp #include "got_lib_object_cache.h"
52 ad242220 2018-09-08 stsp #include "got_lib_object_parse.h"
53 15a94983 2018-12-23 stsp #include "got_lib_pack.h"
54 93658fb9 2020-03-18 stsp #include "got_lib_fetch.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 4ee4114f 2018-01-23 stsp static const struct got_error *
129 4796fb13 2018-12-23 stsp open_loose_object(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 2090a03d 2018-09-09 stsp get_packfile_path(char **path_packfile, struct got_packidx *packidx)
151 2090a03d 2018-09-09 stsp size_t size;
153 2090a03d 2018-09-09 stsp /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
154 2090a03d 2018-09-09 stsp size = strlen(packidx->path_packidx) + 2;
155 2090a03d 2018-09-09 stsp if (size < GOT_PACKFILE_NAMELEN + 1)
156 63f810e6 2020-02-29 stsp return got_error_path(packidx->path_packidx, GOT_ERR_BAD_PATH);
158 08451938 2018-11-05 stsp *path_packfile = malloc(size);
159 2090a03d 2018-09-09 stsp if (*path_packfile == NULL)
160 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
162 2090a03d 2018-09-09 stsp /* Copy up to and excluding ".idx". */
163 2090a03d 2018-09-09 stsp if (strlcpy(*path_packfile, packidx->path_packidx,
164 2090a03d 2018-09-09 stsp size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
165 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_NO_SPACE);
167 2090a03d 2018-09-09 stsp if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
168 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_NO_SPACE);
170 2090a03d 2018-09-09 stsp return NULL;
173 2090a03d 2018-09-09 stsp static const struct got_error *
174 a158c901 2018-12-23 stsp request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
175 a158c901 2018-12-23 stsp struct got_object_id *id)
177 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
178 a158c901 2018-12-23 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
180 a158c901 2018-12-23 stsp err = got_privsep_send_packed_obj_req(ibuf, idx, id);
182 a158c901 2018-12-23 stsp return err;
184 a158c901 2018-12-23 stsp err = got_privsep_recv_obj(obj, ibuf);
186 a158c901 2018-12-23 stsp return err;
188 a158c901 2018-12-23 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
190 a158c901 2018-12-23 stsp return NULL;
193 da506691 2019-05-22 stsp static void
194 b48e2ddb 2019-05-22 stsp set_max_datasize(void)
196 b48e2ddb 2019-05-22 stsp struct rlimit rl;
198 b48e2ddb 2019-05-22 stsp if (getrlimit(RLIMIT_DATA, &rl) != 0)
201 b48e2ddb 2019-05-22 stsp rl.rlim_cur = rl.rlim_max;
202 b48e2ddb 2019-05-22 stsp setrlimit(RLIMIT_DATA, &rl);
205 a158c901 2018-12-23 stsp static const struct got_error *
206 711fb6e8 2018-12-23 stsp start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
208 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
209 a158c901 2018-12-23 stsp int imsg_fds[2];
211 a158c901 2018-12-23 stsp struct imsgbuf *ibuf;
213 a158c901 2018-12-23 stsp ibuf = calloc(1, sizeof(*ibuf));
214 a158c901 2018-12-23 stsp if (ibuf == NULL)
215 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
217 a158c901 2018-12-23 stsp pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
218 a158c901 2018-12-23 stsp if (pack->privsep_child == NULL) {
219 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
220 a158c901 2018-12-23 stsp free(ibuf);
221 a158c901 2018-12-23 stsp return err;
224 a158c901 2018-12-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
225 638f9024 2019-05-13 stsp err = got_error_from_errno("socketpair");
229 a158c901 2018-12-23 stsp pid = fork();
230 a158c901 2018-12-23 stsp if (pid == -1) {
231 638f9024 2019-05-13 stsp err = got_error_from_errno("fork");
233 a158c901 2018-12-23 stsp } else if (pid == 0) {
234 b48e2ddb 2019-05-22 stsp set_max_datasize();
235 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
236 a158c901 2018-12-23 stsp pack->path_packfile);
237 a158c901 2018-12-23 stsp /* not reached */
240 3a6ce05a 2019-02-11 stsp if (close(imsg_fds[1]) != 0)
241 638f9024 2019-05-13 stsp return got_error_from_errno("close");
242 a158c901 2018-12-23 stsp pack->privsep_child->imsg_fd = imsg_fds[0];
243 a158c901 2018-12-23 stsp pack->privsep_child->pid = pid;
244 a158c901 2018-12-23 stsp imsg_init(ibuf, imsg_fds[0]);
245 a158c901 2018-12-23 stsp pack->privsep_child->ibuf = ibuf;
247 a158c901 2018-12-23 stsp err = got_privsep_init_pack_child(ibuf, pack, packidx);
249 a158c901 2018-12-23 stsp const struct got_error *child_err;
250 a158c901 2018-12-23 stsp err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
251 a158c901 2018-12-23 stsp child_err = got_privsep_wait_for_child(
252 a158c901 2018-12-23 stsp pack->privsep_child->pid);
253 a158c901 2018-12-23 stsp if (child_err && err == NULL)
254 a158c901 2018-12-23 stsp err = child_err;
258 a158c901 2018-12-23 stsp free(ibuf);
259 a158c901 2018-12-23 stsp free(pack->privsep_child);
260 a158c901 2018-12-23 stsp pack->privsep_child = NULL;
262 a158c901 2018-12-23 stsp return err;
265 711fb6e8 2018-12-23 stsp static const struct got_error *
266 711fb6e8 2018-12-23 stsp read_packed_object_privsep(struct got_object **obj,
267 711fb6e8 2018-12-23 stsp struct got_repository *repo, struct got_pack *pack,
268 711fb6e8 2018-12-23 stsp struct got_packidx *packidx, int idx, struct got_object_id *id)
270 711fb6e8 2018-12-23 stsp const struct got_error *err = NULL;
272 711fb6e8 2018-12-23 stsp if (pack->privsep_child)
273 711fb6e8 2018-12-23 stsp return request_packed_object(obj, pack, idx, id);
275 711fb6e8 2018-12-23 stsp err = start_pack_privsep_child(pack, packidx);
277 711fb6e8 2018-12-23 stsp return err;
279 711fb6e8 2018-12-23 stsp return request_packed_object(obj, pack, idx, id);
283 a158c901 2018-12-23 stsp static const struct got_error *
284 2090a03d 2018-09-09 stsp open_packed_object(struct got_object **obj, struct got_object_id *id,
285 2090a03d 2018-09-09 stsp struct got_repository *repo)
287 2090a03d 2018-09-09 stsp const struct got_error *err = NULL;
288 2090a03d 2018-09-09 stsp struct got_pack *pack = NULL;
289 2090a03d 2018-09-09 stsp struct got_packidx *packidx = NULL;
291 2090a03d 2018-09-09 stsp char *path_packfile;
293 2090a03d 2018-09-09 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
295 2090a03d 2018-09-09 stsp return err;
297 2090a03d 2018-09-09 stsp err = get_packfile_path(&path_packfile, packidx);
299 2090a03d 2018-09-09 stsp return err;
301 2090a03d 2018-09-09 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
302 2090a03d 2018-09-09 stsp if (pack == NULL) {
303 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
308 a158c901 2018-12-23 stsp err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
312 2090a03d 2018-09-09 stsp free(path_packfile);
313 4558fcd4 2018-01-14 stsp return err;
316 9f2369b0 2018-12-24 stsp static const struct got_error *
317 9f2369b0 2018-12-24 stsp request_object(struct got_object **obj, struct got_repository *repo, int fd)
319 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
320 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
322 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
324 aea5f015 2018-12-24 stsp err = got_privsep_send_obj_req(ibuf, fd);
326 9f2369b0 2018-12-24 stsp return err;
328 9f2369b0 2018-12-24 stsp return got_privsep_recv_obj(obj, ibuf);
331 9f2369b0 2018-12-24 stsp static const struct got_error *
332 9f2369b0 2018-12-24 stsp read_object_header_privsep(struct got_object **obj, struct got_repository *repo,
333 9f2369b0 2018-12-24 stsp int obj_fd)
335 ddc7b220 2019-09-08 stsp const struct got_error *err;
336 9f2369b0 2018-12-24 stsp int imsg_fds[2];
338 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
340 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
341 9f2369b0 2018-12-24 stsp return request_object(obj, repo, obj_fd);
343 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
344 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
345 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
347 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
348 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
349 ddc7b220 2019-09-08 stsp free(ibuf);
350 ddc7b220 2019-09-08 stsp return err;
353 9f2369b0 2018-12-24 stsp pid = fork();
354 ddc7b220 2019-09-08 stsp if (pid == -1) {
355 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
356 ddc7b220 2019-09-08 stsp free(ibuf);
357 ddc7b220 2019-09-08 stsp return err;
359 9f2369b0 2018-12-24 stsp else if (pid == 0) {
360 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
361 9f2369b0 2018-12-24 stsp repo->path);
362 9f2369b0 2018-12-24 stsp /* not reached */
365 ddc7b220 2019-09-08 stsp if (close(imsg_fds[1]) != 0) {
366 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
367 ddc7b220 2019-09-08 stsp free(ibuf);
368 ddc7b220 2019-09-08 stsp return err;
370 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
371 9f2369b0 2018-12-24 stsp imsg_fds[0];
372 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
373 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
374 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
376 9f2369b0 2018-12-24 stsp return request_object(obj, repo, obj_fd);
380 4558fcd4 2018-01-14 stsp const struct got_error *
381 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
382 4558fcd4 2018-01-14 stsp struct got_object_id *id)
384 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
385 6c00b545 2018-01-17 stsp char *path;
388 7bb0daa1 2018-06-21 stsp *obj = got_repo_get_cached_object(repo, id);
389 7bb0daa1 2018-06-21 stsp if (*obj != NULL) {
390 7bb0daa1 2018-06-21 stsp (*obj)->refcnt++;
391 7bb0daa1 2018-06-21 stsp return NULL;
394 e82b1d81 2019-07-27 stsp err = open_packed_object(obj, id, repo);
395 e82b1d81 2019-07-27 stsp if (err && err->code != GOT_ERR_NO_OBJ)
396 e82b1d81 2019-07-27 stsp return err;
397 e82b1d81 2019-07-27 stsp if (*obj) {
398 e82b1d81 2019-07-27 stsp (*obj)->refcnt++;
399 e82b1d81 2019-07-27 stsp return got_repo_cache_object(repo, id, *obj);
402 90bdb554 2019-04-11 stsp err = got_object_get_path(&path, id, repo);
404 4558fcd4 2018-01-14 stsp return err;
406 a5b57ccf 2019-04-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
407 2178c42e 2018-04-22 stsp if (fd == -1) {
408 e82b1d81 2019-07-27 stsp if (errno == ENOENT)
409 e82b1d81 2019-07-27 stsp err = got_error_no_obj(id);
411 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", path);
414 9f2369b0 2018-12-24 stsp err = read_object_header_privsep(obj, repo, fd);
417 ab9a70b2 2017-11-06 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
420 59790a32 2018-09-15 stsp (*obj)->refcnt++;
421 59790a32 2018-09-15 stsp err = got_repo_cache_object(repo, id, *obj);
423 6c00b545 2018-01-17 stsp free(path);
424 ab9a70b2 2017-11-06 stsp return err;
428 6dfa2fd3 2018-02-12 stsp const struct got_error *
429 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
430 6dfa2fd3 2018-02-12 stsp const char *id_str)
432 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
434 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
435 6dd1ece6 2019-11-10 stsp return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
437 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
440 15a94983 2018-12-23 stsp const struct got_error *
441 15a94983 2018-12-23 stsp got_object_resolve_id_str(struct got_object_id **id,
442 15a94983 2018-12-23 stsp struct got_repository *repo, const char *id_str)
444 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
445 15a94983 2018-12-23 stsp struct got_object *obj;
447 15a94983 2018-12-23 stsp err = got_object_open_by_id_str(&obj, repo, id_str);
449 15a94983 2018-12-23 stsp return err;
451 15a94983 2018-12-23 stsp *id = got_object_id_dup(got_object_get_id(obj));
452 15a94983 2018-12-23 stsp got_object_close(obj);
453 15a94983 2018-12-23 stsp if (*id == NULL)
454 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
456 15a94983 2018-12-23 stsp return NULL;
459 434025f3 2018-09-16 stsp static const struct got_error *
460 a158c901 2018-12-23 stsp request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
461 a158c901 2018-12-23 stsp int pack_idx, struct got_object_id *id)
463 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
465 a158c901 2018-12-23 stsp err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
468 a158c901 2018-12-23 stsp return err;
470 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
472 ca6e02ac 2020-01-07 stsp return err;
474 ca6e02ac 2020-01-07 stsp (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
475 ca6e02ac 2020-01-07 stsp return NULL;
478 a158c901 2018-12-23 stsp static const struct got_error *
479 a158c901 2018-12-23 stsp read_packed_commit_privsep(struct got_commit_object **commit,
480 a158c901 2018-12-23 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
481 a158c901 2018-12-23 stsp struct got_object_id *id)
483 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
485 a158c901 2018-12-23 stsp if (pack->privsep_child)
486 a158c901 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
488 711fb6e8 2018-12-23 stsp err = start_pack_privsep_child(pack, packidx);
490 a158c901 2018-12-23 stsp return err;
492 711fb6e8 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
495 9f2369b0 2018-12-24 stsp static const struct got_error *
496 9f2369b0 2018-12-24 stsp request_commit(struct got_commit_object **commit, struct got_repository *repo,
499 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
500 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
502 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
504 9f2369b0 2018-12-24 stsp err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
506 9f2369b0 2018-12-24 stsp return err;
508 9f2369b0 2018-12-24 stsp return got_privsep_recv_commit(commit, ibuf);
511 9f2369b0 2018-12-24 stsp static const struct got_error *
512 9f2369b0 2018-12-24 stsp read_commit_privsep(struct got_commit_object **commit, int obj_fd,
513 9f2369b0 2018-12-24 stsp struct got_repository *repo)
515 ddc7b220 2019-09-08 stsp const struct got_error *err;
516 9f2369b0 2018-12-24 stsp int imsg_fds[2];
518 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
520 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
521 9f2369b0 2018-12-24 stsp return request_commit(commit, repo, obj_fd);
523 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
524 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
525 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
527 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
528 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
529 ddc7b220 2019-09-08 stsp free(ibuf);
530 ddc7b220 2019-09-08 stsp return err;
533 9f2369b0 2018-12-24 stsp pid = fork();
534 ddc7b220 2019-09-08 stsp if (pid == -1) {
535 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
536 ddc7b220 2019-09-08 stsp free(ibuf);
537 ddc7b220 2019-09-08 stsp return err;
539 9f2369b0 2018-12-24 stsp else if (pid == 0) {
540 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
541 9f2369b0 2018-12-24 stsp repo->path);
542 9f2369b0 2018-12-24 stsp /* not reached */
545 ddc7b220 2019-09-08 stsp if (close(imsg_fds[1]) != 0) {
546 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
547 ddc7b220 2019-09-08 stsp free(ibuf);
548 ddc7b220 2019-09-08 stsp return err;
550 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
551 9f2369b0 2018-12-24 stsp imsg_fds[0];
552 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
553 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
554 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
556 9f2369b0 2018-12-24 stsp return request_commit(commit, repo, obj_fd);
560 a158c901 2018-12-23 stsp static const struct got_error *
561 434025f3 2018-09-16 stsp open_commit(struct got_commit_object **commit,
562 1785f84a 2018-12-23 stsp struct got_repository *repo, struct got_object_id *id, int check_cache)
564 434025f3 2018-09-16 stsp const struct got_error *err = NULL;
565 1785f84a 2018-12-23 stsp struct got_packidx *packidx = NULL;
567 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
569 434025f3 2018-09-16 stsp if (check_cache) {
570 1785f84a 2018-12-23 stsp *commit = got_repo_get_cached_commit(repo, id);
571 434025f3 2018-09-16 stsp if (*commit != NULL) {
572 434025f3 2018-09-16 stsp (*commit)->refcnt++;
573 434025f3 2018-09-16 stsp return NULL;
576 434025f3 2018-09-16 stsp *commit = NULL;
578 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
579 e82b1d81 2019-07-27 stsp if (err == NULL) {
580 1785f84a 2018-12-23 stsp struct got_pack *pack = NULL;
582 1785f84a 2018-12-23 stsp err = get_packfile_path(&path_packfile, packidx);
584 1785f84a 2018-12-23 stsp return err;
586 1785f84a 2018-12-23 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
587 434025f3 2018-09-16 stsp if (pack == NULL) {
588 1785f84a 2018-12-23 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
593 a158c901 2018-12-23 stsp err = read_packed_commit_privsep(commit, pack,
594 1785f84a 2018-12-23 stsp packidx, idx, id);
595 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
598 e82b1d81 2019-07-27 stsp err = open_loose_object(&fd, id, repo);
600 e82b1d81 2019-07-27 stsp return err;
601 9f2369b0 2018-12-24 stsp err = read_commit_privsep(commit, fd, repo);
604 434025f3 2018-09-16 stsp if (err == NULL) {
605 434025f3 2018-09-16 stsp (*commit)->refcnt++;
606 1785f84a 2018-12-23 stsp err = got_repo_cache_commit(repo, id, *commit);
609 8d2c5ea3 2019-08-13 stsp free(path_packfile);
610 e32baab7 2018-11-05 stsp return err;
613 e32baab7 2018-11-05 stsp const struct got_error *
614 e32baab7 2018-11-05 stsp got_object_open_as_commit(struct got_commit_object **commit,
615 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object_id *id)
617 e32baab7 2018-11-05 stsp *commit = got_repo_get_cached_commit(repo, id);
618 e32baab7 2018-11-05 stsp if (*commit != NULL) {
619 e32baab7 2018-11-05 stsp (*commit)->refcnt++;
620 e32baab7 2018-11-05 stsp return NULL;
623 1785f84a 2018-12-23 stsp return open_commit(commit, repo, id, 0);
626 e32baab7 2018-11-05 stsp const struct got_error *
627 e32baab7 2018-11-05 stsp got_object_commit_open(struct got_commit_object **commit,
628 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object *obj)
630 1785f84a 2018-12-23 stsp return open_commit(commit, repo, got_object_get_id(obj), 1);
633 434025f3 2018-09-16 stsp const struct got_error *
634 dbc6a6b6 2018-07-12 stsp got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
636 dbc6a6b6 2018-07-12 stsp const struct got_error *err = NULL;
638 dbc6a6b6 2018-07-12 stsp *qid = calloc(1, sizeof(**qid));
639 dbc6a6b6 2018-07-12 stsp if (*qid == NULL)
640 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
642 dbc6a6b6 2018-07-12 stsp (*qid)->id = got_object_id_dup(id);
643 dbc6a6b6 2018-07-12 stsp if ((*qid)->id == NULL) {
644 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
645 fa2f6902 2018-07-23 stsp got_object_qid_free(*qid);
646 dbc6a6b6 2018-07-12 stsp *qid = NULL;
647 dbc6a6b6 2018-07-12 stsp return err;
650 dbc6a6b6 2018-07-12 stsp return NULL;
653 a158c901 2018-12-23 stsp static const struct got_error *
654 13c729f7 2018-12-24 stsp request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
655 13c729f7 2018-12-24 stsp int pack_idx, struct got_object_id *id)
657 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
659 13c729f7 2018-12-24 stsp err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
662 a158c901 2018-12-23 stsp return err;
664 a158c901 2018-12-23 stsp return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
667 71eb0e7f 2018-09-16 stsp static const struct got_error *
668 13c729f7 2018-12-24 stsp read_packed_tree_privsep(struct got_tree_object **tree,
669 13c729f7 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
670 13c729f7 2018-12-24 stsp struct got_object_id *id)
672 13c729f7 2018-12-24 stsp const struct got_error *err = NULL;
674 13c729f7 2018-12-24 stsp if (pack->privsep_child)
675 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
677 13c729f7 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
679 13c729f7 2018-12-24 stsp return err;
681 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
684 13c729f7 2018-12-24 stsp static const struct got_error *
685 9f2369b0 2018-12-24 stsp request_tree(struct got_tree_object **tree, struct got_repository *repo,
688 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
689 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
691 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
693 9f2369b0 2018-12-24 stsp err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
695 9f2369b0 2018-12-24 stsp return err;
697 9f2369b0 2018-12-24 stsp return got_privsep_recv_tree(tree, ibuf);
700 9f2369b0 2018-12-24 stsp const struct got_error *
701 9f2369b0 2018-12-24 stsp read_tree_privsep(struct got_tree_object **tree, int obj_fd,
702 9f2369b0 2018-12-24 stsp struct got_repository *repo)
704 ddc7b220 2019-09-08 stsp const struct got_error *err;
705 9f2369b0 2018-12-24 stsp int imsg_fds[2];
707 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
709 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
710 9f2369b0 2018-12-24 stsp return request_tree(tree, repo, obj_fd);
712 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
713 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
714 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
716 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
717 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
718 ddc7b220 2019-09-08 stsp free(ibuf);
719 ddc7b220 2019-09-08 stsp return err;
722 9f2369b0 2018-12-24 stsp pid = fork();
723 ddc7b220 2019-09-08 stsp if (pid == -1) {
724 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
725 ddc7b220 2019-09-08 stsp free(ibuf);
726 ddc7b220 2019-09-08 stsp return err;
728 9f2369b0 2018-12-24 stsp else if (pid == 0) {
729 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
730 9f2369b0 2018-12-24 stsp repo->path);
731 9f2369b0 2018-12-24 stsp /* not reached */
734 ddc7b220 2019-09-08 stsp if (close(imsg_fds[1]) != 0) {
735 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
736 ddc7b220 2019-09-08 stsp free(ibuf);
737 ddc7b220 2019-09-08 stsp return err;
739 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
740 9f2369b0 2018-12-24 stsp imsg_fds[0];
741 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
742 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
743 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
746 9f2369b0 2018-12-24 stsp return request_tree(tree, repo, obj_fd);
749 9f2369b0 2018-12-24 stsp static const struct got_error *
750 13c729f7 2018-12-24 stsp open_tree(struct got_tree_object **tree, struct got_repository *repo,
751 13c729f7 2018-12-24 stsp struct got_object_id *id, int check_cache)
753 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
754 13c729f7 2018-12-24 stsp struct got_packidx *packidx = NULL;
756 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
758 71eb0e7f 2018-09-16 stsp if (check_cache) {
759 13c729f7 2018-12-24 stsp *tree = got_repo_get_cached_tree(repo, id);
760 71eb0e7f 2018-09-16 stsp if (*tree != NULL) {
761 71eb0e7f 2018-09-16 stsp (*tree)->refcnt++;
762 71eb0e7f 2018-09-16 stsp return NULL;
765 71eb0e7f 2018-09-16 stsp *tree = NULL;
767 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
768 e82b1d81 2019-07-27 stsp if (err == NULL) {
769 13c729f7 2018-12-24 stsp struct got_pack *pack = NULL;
771 13c729f7 2018-12-24 stsp err = get_packfile_path(&path_packfile, packidx);
773 13c729f7 2018-12-24 stsp return err;
775 13c729f7 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
776 e7885405 2018-09-10 stsp if (pack == NULL) {
777 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
782 13c729f7 2018-12-24 stsp err = read_packed_tree_privsep(tree, pack,
783 13c729f7 2018-12-24 stsp packidx, idx, id);
784 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
787 e82b1d81 2019-07-27 stsp err = open_loose_object(&fd, id, repo);
789 e82b1d81 2019-07-27 stsp return err;
790 9f2369b0 2018-12-24 stsp err = read_tree_privsep(tree, fd, repo);
793 f6be5c30 2018-06-22 stsp if (err == NULL) {
794 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
795 13c729f7 2018-12-24 stsp err = got_repo_cache_tree(repo, id, *tree);
798 8d2c5ea3 2019-08-13 stsp free(path_packfile);
799 776d4d29 2018-06-17 stsp return err;
802 776d4d29 2018-06-17 stsp const struct got_error *
803 776d4d29 2018-06-17 stsp got_object_open_as_tree(struct got_tree_object **tree,
804 776d4d29 2018-06-17 stsp struct got_repository *repo, struct got_object_id *id)
806 e8eb494a 2018-09-16 stsp *tree = got_repo_get_cached_tree(repo, id);
807 e8eb494a 2018-09-16 stsp if (*tree != NULL) {
808 e8eb494a 2018-09-16 stsp (*tree)->refcnt++;
809 e8eb494a 2018-09-16 stsp return NULL;
812 13c729f7 2018-12-24 stsp return open_tree(tree, repo, id, 0);
815 71eb0e7f 2018-09-16 stsp const struct got_error *
816 71eb0e7f 2018-09-16 stsp got_object_tree_open(struct got_tree_object **tree,
817 71eb0e7f 2018-09-16 stsp struct got_repository *repo, struct got_object *obj)
819 13c729f7 2018-12-24 stsp return open_tree(tree, repo, got_object_get_id(obj), 1);
823 56e0773d 2019-11-28 stsp got_object_tree_get_nentries(struct got_tree_object *tree)
825 56e0773d 2019-11-28 stsp return tree->nentries;
828 56e0773d 2019-11-28 stsp struct got_tree_entry *
829 56e0773d 2019-11-28 stsp got_object_tree_get_first_entry(struct got_tree_object *tree)
831 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, 0);
834 56e0773d 2019-11-28 stsp struct got_tree_entry *
835 56e0773d 2019-11-28 stsp got_object_tree_get_last_entry(struct got_tree_object *tree)
837 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, tree->nentries - 1);
840 56e0773d 2019-11-28 stsp struct got_tree_entry *
841 56e0773d 2019-11-28 stsp got_object_tree_get_entry(struct got_tree_object *tree, int i)
843 56e0773d 2019-11-28 stsp if (i < 0 || i >= tree->nentries)
844 56e0773d 2019-11-28 stsp return NULL;
845 56e0773d 2019-11-28 stsp return &tree->entries[i];
849 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(struct got_tree_entry *te)
851 56e0773d 2019-11-28 stsp return te->mode;
854 56e0773d 2019-11-28 stsp const char *
855 56e0773d 2019-11-28 stsp got_tree_entry_get_name(struct got_tree_entry *te)
857 56e0773d 2019-11-28 stsp return &te->name[0];
860 56e0773d 2019-11-28 stsp struct got_object_id *
861 56e0773d 2019-11-28 stsp got_tree_entry_get_id(struct got_tree_entry *te)
863 56e0773d 2019-11-28 stsp return &te->id;
867 56e0773d 2019-11-28 stsp got_tree_entry_get_index(struct got_tree_entry *te)
869 56e0773d 2019-11-28 stsp return te->idx;
872 56e0773d 2019-11-28 stsp struct got_tree_entry *
873 56e0773d 2019-11-28 stsp got_tree_entry_get_next(struct got_tree_object *tree,
874 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
876 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx + 1);
879 56e0773d 2019-11-28 stsp struct got_tree_entry *
880 56e0773d 2019-11-28 stsp got_tree_entry_get_prev(struct got_tree_object *tree,
881 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
883 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx - 1);
886 3840f4c9 2018-09-12 stsp static const struct got_error *
887 ac544f8c 2019-01-13 stsp request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
888 ebc55e2d 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
889 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
891 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
892 3840f4c9 2018-09-12 stsp int outfd_child;
893 3840f4c9 2018-09-12 stsp int basefd, accumfd; /* temporary files for delta application */
895 3840f4c9 2018-09-12 stsp basefd = got_opentempfd();
896 3840f4c9 2018-09-12 stsp if (basefd == -1)
897 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
898 3840f4c9 2018-09-12 stsp accumfd = got_opentempfd();
899 3840f4c9 2018-09-12 stsp if (accumfd == -1)
900 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
902 3840f4c9 2018-09-12 stsp outfd_child = dup(outfd);
903 3840f4c9 2018-09-12 stsp if (outfd_child == -1)
904 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
906 ebc55e2d 2018-12-24 stsp err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
908 3840f4c9 2018-09-12 stsp return err;
910 3840f4c9 2018-09-12 stsp err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
911 3840f4c9 2018-09-12 stsp outfd_child);
913 41496140 2019-02-21 stsp close(basefd);
914 41496140 2019-02-21 stsp close(accumfd);
915 3840f4c9 2018-09-12 stsp return err;
918 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
921 3840f4c9 2018-09-12 stsp close(accumfd);
922 3840f4c9 2018-09-12 stsp return err;
925 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
928 3840f4c9 2018-09-12 stsp return err;
930 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen,
931 ebc55e2d 2018-12-24 stsp pack->privsep_child->ibuf);
933 3840f4c9 2018-09-12 stsp return err;
935 3840f4c9 2018-09-12 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
936 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
938 3840f4c9 2018-09-12 stsp return err;
941 ebc55e2d 2018-12-24 stsp static const struct got_error *
942 ac544f8c 2019-01-13 stsp read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
943 ac544f8c 2019-01-13 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
944 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
946 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
948 ebc55e2d 2018-12-24 stsp if (pack->privsep_child == NULL) {
949 ebc55e2d 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
951 ebc55e2d 2018-12-24 stsp return err;
954 ac544f8c 2019-01-13 stsp return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
958 9f2369b0 2018-12-24 stsp static const struct got_error *
959 ac544f8c 2019-01-13 stsp request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
960 ac544f8c 2019-01-13 stsp int infd, struct imsgbuf *ibuf)
962 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
963 9f2369b0 2018-12-24 stsp int outfd_child;
965 9f2369b0 2018-12-24 stsp outfd_child = dup(outfd);
966 9f2369b0 2018-12-24 stsp if (outfd_child == -1)
967 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
969 9f2369b0 2018-12-24 stsp err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
971 9f2369b0 2018-12-24 stsp return err;
973 9f2369b0 2018-12-24 stsp err = got_privsep_send_blob_outfd(ibuf, outfd_child);
975 9f2369b0 2018-12-24 stsp return err;
977 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
979 9f2369b0 2018-12-24 stsp return err;
981 9f2369b0 2018-12-24 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
982 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
984 9f2369b0 2018-12-24 stsp return err;
987 9f2369b0 2018-12-24 stsp static const struct got_error *
988 ac544f8c 2019-01-13 stsp read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
989 9f2369b0 2018-12-24 stsp int outfd, int infd, struct got_repository *repo)
991 ddc7b220 2019-09-08 stsp const struct got_error *err;
992 9f2369b0 2018-12-24 stsp int imsg_fds[2];
994 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
996 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
997 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
998 ac544f8c 2019-01-13 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1001 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1002 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1003 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1005 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1006 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1007 ddc7b220 2019-09-08 stsp free(ibuf);
1008 ddc7b220 2019-09-08 stsp return err;
1011 9f2369b0 2018-12-24 stsp pid = fork();
1012 ddc7b220 2019-09-08 stsp if (pid == -1) {
1013 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1014 ddc7b220 2019-09-08 stsp free(ibuf);
1015 ddc7b220 2019-09-08 stsp return err;
1017 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1018 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1019 9f2369b0 2018-12-24 stsp repo->path);
1020 9f2369b0 2018-12-24 stsp /* not reached */
1023 ddc7b220 2019-09-08 stsp if (close(imsg_fds[1]) != 0) {
1024 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1025 ddc7b220 2019-09-08 stsp free(ibuf);
1026 ddc7b220 2019-09-08 stsp return err;
1028 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1029 9f2369b0 2018-12-24 stsp imsg_fds[0];
1030 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1031 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1032 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1034 ac544f8c 2019-01-13 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1037 ebc55e2d 2018-12-24 stsp static const struct got_error *
1038 ebc55e2d 2018-12-24 stsp open_blob(struct got_blob_object **blob, struct got_repository *repo,
1039 ebc55e2d 2018-12-24 stsp struct got_object_id *id, size_t blocksize)
1041 ebc55e2d 2018-12-24 stsp const struct got_error *err = NULL;
1042 ebc55e2d 2018-12-24 stsp struct got_packidx *packidx = NULL;
1044 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1045 ac544f8c 2019-01-13 stsp uint8_t *outbuf;
1046 e82b1d81 2019-07-27 stsp int outfd;
1047 ebc55e2d 2018-12-24 stsp size_t size, hdrlen;
1048 ebc55e2d 2018-12-24 stsp struct stat sb;
1050 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
1051 4558fcd4 2018-01-14 stsp if (*blob == NULL)
1052 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1054 55da3778 2018-09-10 stsp outfd = got_opentempfd();
1055 55da3778 2018-09-10 stsp if (outfd == -1)
1056 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
1058 062ebb78 2018-07-12 stsp (*blob)->read_buf = malloc(blocksize);
1059 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
1060 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1061 c7254d79 2018-04-24 stsp goto done;
1064 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1065 e82b1d81 2019-07-27 stsp if (err == NULL) {
1066 ebc55e2d 2018-12-24 stsp struct got_pack *pack = NULL;
1068 ebc55e2d 2018-12-24 stsp err = get_packfile_path(&path_packfile, packidx);
1070 ebc55e2d 2018-12-24 stsp goto done;
1072 ebc55e2d 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1073 55da3778 2018-09-10 stsp if (pack == NULL) {
1074 ebc55e2d 2018-12-24 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1077 55da3778 2018-09-10 stsp goto done;
1079 ac544f8c 2019-01-13 stsp err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1080 ac544f8c 2019-01-13 stsp pack, packidx, idx, id);
1081 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1084 e82b1d81 2019-07-27 stsp err = open_loose_object(&infd, id, repo);
1086 e82b1d81 2019-07-27 stsp goto done;
1087 ac544f8c 2019-01-13 stsp err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1091 55da3778 2018-09-10 stsp goto done;
1093 de060dff 2018-12-24 stsp if (hdrlen > size) {
1094 ebc55e2d 2018-12-24 stsp err = got_error(GOT_ERR_BAD_OBJ_HDR);
1095 ebc55e2d 2018-12-24 stsp goto done;
1098 ac544f8c 2019-01-13 stsp if (outbuf) {
1099 3a6ce05a 2019-02-11 stsp if (close(outfd) != 0 && err == NULL)
1100 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1101 ac544f8c 2019-01-13 stsp outfd = -1;
1102 ac544f8c 2019-01-13 stsp (*blob)->f = fmemopen(outbuf, size, "rb");
1103 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1104 638f9024 2019-05-13 stsp err = got_error_from_errno("fmemopen");
1105 ac544f8c 2019-01-13 stsp free(outbuf);
1106 ac544f8c 2019-01-13 stsp goto done;
1108 ac544f8c 2019-01-13 stsp (*blob)->data = outbuf;
1110 ac544f8c 2019-01-13 stsp if (fstat(outfd, &sb) == -1) {
1111 638f9024 2019-05-13 stsp err = got_error_from_errno("fstat");
1112 ac544f8c 2019-01-13 stsp goto done;
1115 ac544f8c 2019-01-13 stsp if (sb.st_size != size) {
1116 ac544f8c 2019-01-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1117 ac544f8c 2019-01-13 stsp goto done;
1120 ac544f8c 2019-01-13 stsp (*blob)->f = fdopen(outfd, "rb");
1121 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1122 638f9024 2019-05-13 stsp err = got_error_from_errno("fdopen");
1123 ac544f8c 2019-01-13 stsp close(outfd);
1124 ac544f8c 2019-01-13 stsp outfd = -1;
1125 ac544f8c 2019-01-13 stsp goto done;
1129 ebc55e2d 2018-12-24 stsp (*blob)->hdrlen = hdrlen;
1130 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
1131 ebc55e2d 2018-12-24 stsp memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1134 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1135 55da3778 2018-09-10 stsp if (err) {
1136 55da3778 2018-09-10 stsp if (*blob) {
1137 7baf5860 2019-03-19 stsp got_object_blob_close(*blob);
1138 55da3778 2018-09-10 stsp *blob = NULL;
1139 55da3778 2018-09-10 stsp } else if (outfd != -1)
1140 55da3778 2018-09-10 stsp close(outfd);
1142 a19581a2 2018-06-21 stsp return err;
1145 a19581a2 2018-06-21 stsp const struct got_error *
1146 a19581a2 2018-06-21 stsp got_object_open_as_blob(struct got_blob_object **blob,
1147 a19581a2 2018-06-21 stsp struct got_repository *repo, struct got_object_id *id,
1148 a19581a2 2018-06-21 stsp size_t blocksize)
1150 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, id, blocksize);
1153 ebc55e2d 2018-12-24 stsp const struct got_error *
1154 ebc55e2d 2018-12-24 stsp got_object_blob_open(struct got_blob_object **blob,
1155 ebc55e2d 2018-12-24 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
1157 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1160 fb43ecf1 2019-02-11 stsp const struct got_error *
1161 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
1163 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL;
1164 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
1165 7baf5860 2019-03-19 stsp if (blob->f && fclose(blob->f) != 0)
1166 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1167 ac544f8c 2019-01-13 stsp free(blob->data);
1168 68482ea3 2017-11-27 stsp free(blob);
1169 fb43ecf1 2019-02-11 stsp return err;
1173 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1175 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1179 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
1181 f934cf2c 2018-02-12 stsp return blob->hdrlen;
1184 f934cf2c 2018-02-12 stsp const uint8_t *
1185 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
1187 f934cf2c 2018-02-12 stsp return blob->read_buf;
1190 68482ea3 2017-11-27 stsp const struct got_error *
1191 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1195 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1196 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
1197 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
1198 eb651edf 2018-02-11 stsp *outlenp = n;
1199 35e9ba5d 2018-06-21 stsp return NULL;
1202 35e9ba5d 2018-06-21 stsp const struct got_error *
1203 f595d9bd 2019-08-14 stsp got_object_blob_dump_to_file(size_t *filesize, int *nlines,
1204 6c4c42e0 2019-06-24 stsp off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1206 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
1207 b6752625 2018-12-24 stsp size_t n, len, hdrlen;
1208 84451b3e 2018-07-10 stsp const uint8_t *buf;
1210 6c4c42e0 2019-06-24 stsp size_t noffsets = 0;
1211 f595d9bd 2019-08-14 stsp off_t off = 0, total_len = 0;
1213 6c4c42e0 2019-06-24 stsp if (line_offsets)
1214 6c4c42e0 2019-06-24 stsp *line_offsets = NULL;
1215 f595d9bd 2019-08-14 stsp if (filesize)
1216 f595d9bd 2019-08-14 stsp *filesize = 0;
1217 84451b3e 2018-07-10 stsp if (nlines)
1218 84451b3e 2018-07-10 stsp *nlines = 0;
1220 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1222 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
1224 35e9ba5d 2018-06-21 stsp return err;
1225 35e9ba5d 2018-06-21 stsp if (len == 0)
1227 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
1228 b02560ec 2019-08-19 stsp i = hdrlen;
1229 78695fb7 2019-08-12 stsp if (line_offsets && nlines) {
1230 78695fb7 2019-08-12 stsp if (*line_offsets == NULL) {
1231 78695fb7 2019-08-12 stsp /* Have some data but perhaps no '\n'. */
1232 78695fb7 2019-08-12 stsp noffsets = 1;
1233 78695fb7 2019-08-12 stsp *nlines = 1;
1234 f595d9bd 2019-08-14 stsp *line_offsets = calloc(1, sizeof(**line_offsets));
1235 78695fb7 2019-08-12 stsp if (*line_offsets == NULL)
1236 845785d4 2020-02-02 tracey return got_error_from_errno("calloc");
1238 b02560ec 2019-08-19 stsp /* Skip forward over end of first line. */
1239 b02560ec 2019-08-19 stsp while (i < len) {
1240 b02560ec 2019-08-19 stsp if (buf[i] == '\n')
1245 b02560ec 2019-08-19 stsp /* Scan '\n' offsets in remaining chunk of data. */
1246 b02560ec 2019-08-19 stsp while (i < len) {
1247 b02560ec 2019-08-19 stsp if (buf[i] != '\n') {
1251 f595d9bd 2019-08-14 stsp (*nlines)++;
1252 78695fb7 2019-08-12 stsp if (noffsets < *nlines) {
1253 78695fb7 2019-08-12 stsp off_t *o = recallocarray(*line_offsets,
1254 78695fb7 2019-08-12 stsp noffsets, *nlines,
1255 78695fb7 2019-08-12 stsp sizeof(**line_offsets));
1256 78695fb7 2019-08-12 stsp if (o == NULL) {
1257 78695fb7 2019-08-12 stsp free(*line_offsets);
1258 78695fb7 2019-08-12 stsp *line_offsets = NULL;
1259 78695fb7 2019-08-12 stsp return got_error_from_errno(
1260 78695fb7 2019-08-12 stsp "recallocarray");
1262 78695fb7 2019-08-12 stsp *line_offsets = o;
1263 78695fb7 2019-08-12 stsp noffsets = *nlines;
1265 f595d9bd 2019-08-14 stsp off = total_len + i - hdrlen + 1;
1266 f595d9bd 2019-08-14 stsp (*line_offsets)[*nlines - 1] = off;
1270 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
1271 454a6b59 2018-12-24 stsp n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1272 b6752625 2018-12-24 stsp if (n != len - hdrlen)
1273 b6752625 2018-12-24 stsp return got_ferror(outfile, GOT_ERR_IO);
1274 f595d9bd 2019-08-14 stsp total_len += len - hdrlen;
1275 35e9ba5d 2018-06-21 stsp hdrlen = 0;
1276 35e9ba5d 2018-06-21 stsp } while (len != 0);
1278 cbe7f848 2019-02-11 stsp if (fflush(outfile) != 0)
1279 638f9024 2019-05-13 stsp return got_error_from_errno("fflush");
1280 35e9ba5d 2018-06-21 stsp rewind(outfile);
1282 f595d9bd 2019-08-14 stsp if (filesize)
1283 f595d9bd 2019-08-14 stsp *filesize = total_len;
1285 776d4d29 2018-06-17 stsp return NULL;
1288 f4a881ce 2018-11-17 stsp static const struct got_error *
1289 268f7291 2018-12-24 stsp request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1290 268f7291 2018-12-24 stsp int pack_idx, struct got_object_id *id)
1292 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
1294 268f7291 2018-12-24 stsp err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1295 268f7291 2018-12-24 stsp pack_idx);
1297 a158c901 2018-12-23 stsp return err;
1299 a158c901 2018-12-23 stsp return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1302 268f7291 2018-12-24 stsp static const struct got_error *
1303 268f7291 2018-12-24 stsp read_packed_tag_privsep(struct got_tag_object **tag,
1304 268f7291 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1305 268f7291 2018-12-24 stsp struct got_object_id *id)
1307 268f7291 2018-12-24 stsp const struct got_error *err = NULL;
1309 268f7291 2018-12-24 stsp if (pack->privsep_child)
1310 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1312 268f7291 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
1314 268f7291 2018-12-24 stsp return err;
1316 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1319 9f2369b0 2018-12-24 stsp static const struct got_error *
1320 9f2369b0 2018-12-24 stsp request_tag(struct got_tag_object **tag, struct got_repository *repo,
1323 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1324 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1326 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1328 9f2369b0 2018-12-24 stsp err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1330 9f2369b0 2018-12-24 stsp return err;
1332 9f2369b0 2018-12-24 stsp return got_privsep_recv_tag(tag, ibuf);
1335 9f2369b0 2018-12-24 stsp static const struct got_error *
1336 9f2369b0 2018-12-24 stsp read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1337 9f2369b0 2018-12-24 stsp struct got_repository *repo)
1339 ddc7b220 2019-09-08 stsp const struct got_error *err;
1340 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1341 9f2369b0 2018-12-24 stsp pid_t pid;
1342 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1344 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1345 9f2369b0 2018-12-24 stsp return request_tag(tag, repo, obj_fd);
1347 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1348 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1349 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1351 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1352 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1353 ddc7b220 2019-09-08 stsp free(ibuf);
1354 ddc7b220 2019-09-08 stsp return err;
1357 9f2369b0 2018-12-24 stsp pid = fork();
1358 ddc7b220 2019-09-08 stsp if (pid == -1) {
1359 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1360 ddc7b220 2019-09-08 stsp free(ibuf);
1361 ddc7b220 2019-09-08 stsp return err;
1363 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1364 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1365 9f2369b0 2018-12-24 stsp repo->path);
1366 9f2369b0 2018-12-24 stsp /* not reached */
1369 ddc7b220 2019-09-08 stsp if (close(imsg_fds[1]) != 0) {
1370 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1371 ddc7b220 2019-09-08 stsp free(ibuf);
1372 ddc7b220 2019-09-08 stsp return err;
1374 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1375 9f2369b0 2018-12-24 stsp imsg_fds[0];
1376 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1377 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1378 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1380 9f2369b0 2018-12-24 stsp return request_tag(tag, repo, obj_fd);
1383 a158c901 2018-12-23 stsp static const struct got_error *
1384 268f7291 2018-12-24 stsp open_tag(struct got_tag_object **tag, struct got_repository *repo,
1385 268f7291 2018-12-24 stsp struct got_object_id *id, int check_cache)
1387 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1388 268f7291 2018-12-24 stsp struct got_packidx *packidx = NULL;
1390 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1391 5d844a1e 2019-08-13 stsp struct got_object *obj = NULL;
1392 5d844a1e 2019-08-13 stsp int obj_type = GOT_OBJ_TYPE_ANY;
1394 f4a881ce 2018-11-17 stsp if (check_cache) {
1395 268f7291 2018-12-24 stsp *tag = got_repo_get_cached_tag(repo, id);
1396 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1397 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1398 f4a881ce 2018-11-17 stsp return NULL;
1401 f4a881ce 2018-11-17 stsp *tag = NULL;
1403 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1404 e82b1d81 2019-07-27 stsp if (err == NULL) {
1405 268f7291 2018-12-24 stsp struct got_pack *pack = NULL;
1407 268f7291 2018-12-24 stsp err = get_packfile_path(&path_packfile, packidx);
1409 268f7291 2018-12-24 stsp return err;
1411 268f7291 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1412 f4a881ce 2018-11-17 stsp if (pack == NULL) {
1413 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1416 8d2c5ea3 2019-08-13 stsp goto done;
1419 992eb9d8 2020-02-07 tracey /* Beware of "lightweight" tags: Check object type first. */
1420 5d844a1e 2019-08-13 stsp err = read_packed_object_privsep(&obj, repo, pack, packidx,
1423 5d844a1e 2019-08-13 stsp goto done;
1424 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1425 5d844a1e 2019-08-13 stsp got_object_close(obj);
1426 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG) {
1427 5d844a1e 2019-08-13 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1428 5d844a1e 2019-08-13 stsp goto done;
1430 5d844a1e 2019-08-13 stsp err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1431 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1434 e82b1d81 2019-07-27 stsp err = open_loose_object(&fd, id, repo);
1436 e82b1d81 2019-07-27 stsp return err;
1437 5d844a1e 2019-08-13 stsp err = read_object_header_privsep(&obj, repo, fd);
1439 5d844a1e 2019-08-13 stsp return err;
1440 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1441 5d844a1e 2019-08-13 stsp got_object_close(obj);
1442 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
1443 5d844a1e 2019-08-13 stsp return got_error(GOT_ERR_OBJ_TYPE);
1445 5d844a1e 2019-08-13 stsp err = open_loose_object(&fd, id, repo);
1447 5d844a1e 2019-08-13 stsp return err;
1448 9f2369b0 2018-12-24 stsp err = read_tag_privsep(tag, fd, repo);
1451 f4a881ce 2018-11-17 stsp if (err == NULL) {
1452 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1453 268f7291 2018-12-24 stsp err = got_repo_cache_tag(repo, id, *tag);
1456 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1457 f4a881ce 2018-11-17 stsp return err;
1460 f4a881ce 2018-11-17 stsp const struct got_error *
1461 f4a881ce 2018-11-17 stsp got_object_open_as_tag(struct got_tag_object **tag,
1462 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object_id *id)
1464 f4a881ce 2018-11-17 stsp *tag = got_repo_get_cached_tag(repo, id);
1465 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1466 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1467 f4a881ce 2018-11-17 stsp return NULL;
1470 268f7291 2018-12-24 stsp return open_tag(tag, repo, id, 0);
1473 f4a881ce 2018-11-17 stsp const struct got_error *
1474 f4a881ce 2018-11-17 stsp got_object_tag_open(struct got_tag_object **tag,
1475 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object *obj)
1477 268f7291 2018-12-24 stsp return open_tag(tag, repo, got_object_get_id(obj), 1);
1480 d24820bf 2019-08-11 stsp const char *
1481 d24820bf 2019-08-11 stsp got_object_tag_get_name(struct got_tag_object *tag)
1483 d24820bf 2019-08-11 stsp return tag->tag;
1487 0bd18d37 2019-02-01 stsp got_object_tag_get_object_type(struct got_tag_object *tag)
1489 0bd18d37 2019-02-01 stsp return tag->obj_type;
1492 0bd18d37 2019-02-01 stsp struct got_object_id *
1493 0bd18d37 2019-02-01 stsp got_object_tag_get_object_id(struct got_tag_object *tag)
1495 0bd18d37 2019-02-01 stsp return &tag->id;
1499 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_time(struct got_tag_object *tag)
1501 01073a5d 2019-08-22 stsp return tag->tagger_time;
1505 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1507 01073a5d 2019-08-22 stsp return tag->tagger_gmtoff;
1510 01073a5d 2019-08-22 stsp const char *
1511 01073a5d 2019-08-22 stsp got_object_tag_get_tagger(struct got_tag_object *tag)
1513 01073a5d 2019-08-22 stsp return tag->tagger;
1516 01073a5d 2019-08-22 stsp const char *
1517 01073a5d 2019-08-22 stsp got_object_tag_get_message(struct got_tag_object *tag)
1519 01073a5d 2019-08-22 stsp return tag->tagmsg;
1522 776d4d29 2018-06-17 stsp static struct got_tree_entry *
1523 65a9bbe9 2018-09-15 stsp find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1527 63da309a 2018-11-07 stsp /* Note that tree entries are sorted in strncmp() order. */
1528 56e0773d 2019-11-28 stsp for (i = 0; i < tree->nentries; i++) {
1529 56e0773d 2019-11-28 stsp struct got_tree_entry *te = &tree->entries[i];
1530 63da309a 2018-11-07 stsp int cmp = strncmp(te->name, name, len);
1531 63da309a 2018-11-07 stsp if (cmp < 0)
1533 63da309a 2018-11-07 stsp if (cmp > 0)
1535 63da309a 2018-11-07 stsp if (te->name[len] == '\0')
1536 776d4d29 2018-06-17 stsp return te;
1538 eb651edf 2018-02-11 stsp return NULL;
1541 56e0773d 2019-11-28 stsp struct got_tree_entry *
1542 a129376b 2019-03-28 stsp got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1544 a129376b 2019-03-28 stsp return find_entry_by_name(tree, name, strlen(name));
1547 776d4d29 2018-06-17 stsp const struct got_error *
1548 27d434c2 2018-09-15 stsp got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1549 776d4d29 2018-06-17 stsp struct got_object_id *commit_id, const char *path)
1551 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
1552 776d4d29 2018-06-17 stsp struct got_commit_object *commit = NULL;
1553 776d4d29 2018-06-17 stsp struct got_tree_object *tree = NULL;
1554 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
1555 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1556 b7cd37e5 2018-11-18 stsp size_t seglen;
1558 27d434c2 2018-09-15 stsp *id = NULL;
1560 776d4d29 2018-06-17 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
1562 776d4d29 2018-06-17 stsp goto done;
1564 776d4d29 2018-06-17 stsp /* Handle opening of root of commit's tree. */
1565 5e54fb30 2019-05-31 stsp if (got_path_is_root_dir(path)) {
1566 27d434c2 2018-09-15 stsp *id = got_object_id_dup(commit->tree_id);
1567 27d434c2 2018-09-15 stsp if (*id == NULL)
1568 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
1569 ca008b32 2018-07-22 stsp goto done;
1572 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1574 776d4d29 2018-06-17 stsp goto done;
1577 5e54fb30 2019-05-31 stsp while (s[0] == '/')
1580 65a9bbe9 2018-09-15 stsp seglen = 0;
1581 b7cd37e5 2018-11-18 stsp while (*s) {
1582 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
1584 776d4d29 2018-06-17 stsp if (*s != '/') {
1591 65a9bbe9 2018-09-15 stsp te = find_entry_by_name(tree, seg, seglen);
1592 db37e2c0 2018-06-21 stsp if (te == NULL) {
1593 d1451975 2018-11-11 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1594 776d4d29 2018-06-17 stsp goto done;
1597 b7cd37e5 2018-11-18 stsp if (*s == '\0')
1600 776d4d29 2018-06-17 stsp seg = s + 1;
1601 65a9bbe9 2018-09-15 stsp seglen = 0;
1604 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
1606 db37e2c0 2018-06-21 stsp te = NULL;
1608 776d4d29 2018-06-17 stsp goto done;
1609 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
1610 776d4d29 2018-06-17 stsp tree = next_tree;
1615 56e0773d 2019-11-28 stsp *id = got_object_id_dup(&te->id);
1616 27d434c2 2018-09-15 stsp if (*id == NULL)
1617 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
1619 d1451975 2018-11-11 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1621 776d4d29 2018-06-17 stsp if (commit)
1622 776d4d29 2018-06-17 stsp got_object_commit_close(commit);
1624 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
1625 776d4d29 2018-06-17 stsp return err;
1628 07862c20 2018-09-15 stsp const struct got_error *
1629 07862c20 2018-09-15 stsp got_object_tree_path_changed(int *changed,
1630 07862c20 2018-09-15 stsp struct got_tree_object *tree01, struct got_tree_object *tree02,
1631 07862c20 2018-09-15 stsp const char *path, struct got_repository *repo)
1633 07862c20 2018-09-15 stsp const struct got_error *err = NULL;
1634 07862c20 2018-09-15 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1635 07862c20 2018-09-15 stsp struct got_tree_entry *te1 = NULL, *te2 = NULL;
1636 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1637 3b7f9878 2018-11-18 stsp size_t seglen;
1639 07862c20 2018-09-15 stsp *changed = 0;
1641 07862c20 2018-09-15 stsp /* We not do support comparing the root path. */
1642 61a7d79f 2020-02-29 stsp if (got_path_is_root_dir(path))
1643 63f810e6 2020-02-29 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
1645 07862c20 2018-09-15 stsp tree1 = tree01;
1646 07862c20 2018-09-15 stsp tree2 = tree02;
1648 61a7d79f 2020-02-29 stsp while (*s == '/')
1651 65a9bbe9 2018-09-15 stsp seglen = 0;
1652 3b7f9878 2018-11-18 stsp while (*s) {
1653 07862c20 2018-09-15 stsp struct got_tree_object *next_tree1, *next_tree2;
1655 07862c20 2018-09-15 stsp if (*s != '/') {
1662 65a9bbe9 2018-09-15 stsp te1 = find_entry_by_name(tree1, seg, seglen);
1663 07862c20 2018-09-15 stsp if (te1 == NULL) {
1664 07862c20 2018-09-15 stsp err = got_error(GOT_ERR_NO_OBJ);
1665 07862c20 2018-09-15 stsp goto done;
1668 65a9bbe9 2018-09-15 stsp te2 = find_entry_by_name(tree2, seg, seglen);
1669 07862c20 2018-09-15 stsp if (te2 == NULL) {
1670 07862c20 2018-09-15 stsp *changed = 1;
1671 07862c20 2018-09-15 stsp goto done;
1674 07862c20 2018-09-15 stsp if (te1->mode != te2->mode) {
1675 07862c20 2018-09-15 stsp *changed = 1;
1676 07862c20 2018-09-15 stsp goto done;
1679 56e0773d 2019-11-28 stsp if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
1680 07862c20 2018-09-15 stsp *changed = 0;
1681 07862c20 2018-09-15 stsp goto done;
1684 3b7f9878 2018-11-18 stsp if (*s == '\0') { /* final path element */
1685 07862c20 2018-09-15 stsp *changed = 1;
1686 07862c20 2018-09-15 stsp goto done;
1689 07862c20 2018-09-15 stsp seg = s + 1;
1691 65a9bbe9 2018-09-15 stsp seglen = 0;
1693 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree1, repo,
1694 56e0773d 2019-11-28 stsp &te1->id);
1695 07862c20 2018-09-15 stsp te1 = NULL;
1697 07862c20 2018-09-15 stsp goto done;
1698 a31cea73 2018-09-15 stsp if (tree1 != tree01)
1699 a31cea73 2018-09-15 stsp got_object_tree_close(tree1);
1700 07862c20 2018-09-15 stsp tree1 = next_tree1;
1702 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree2, repo,
1703 56e0773d 2019-11-28 stsp &te2->id);
1704 07862c20 2018-09-15 stsp te2 = NULL;
1706 07862c20 2018-09-15 stsp goto done;
1707 a31cea73 2018-09-15 stsp if (tree2 != tree02)
1708 a31cea73 2018-09-15 stsp got_object_tree_close(tree2);
1709 07862c20 2018-09-15 stsp tree2 = next_tree2;
1713 a31cea73 2018-09-15 stsp if (tree1 && tree1 != tree01)
1714 07862c20 2018-09-15 stsp got_object_tree_close(tree1);
1715 a31cea73 2018-09-15 stsp if (tree2 && tree2 != tree02)
1716 07862c20 2018-09-15 stsp got_object_tree_close(tree2);
1717 77880158 2018-11-04 stsp return err;
1720 ed175427 2019-05-09 stsp const struct got_error *
1721 ed175427 2019-05-09 stsp got_object_tree_entry_dup(struct got_tree_entry **new_te,
1722 ed175427 2019-05-09 stsp struct got_tree_entry *te)
1724 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
1726 ed175427 2019-05-09 stsp *new_te = calloc(1, sizeof(**new_te));
1727 ed175427 2019-05-09 stsp if (*new_te == NULL)
1728 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1730 ed175427 2019-05-09 stsp (*new_te)->mode = te->mode;
1731 56e0773d 2019-11-28 stsp memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
1732 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
1733 8c4eabf2 2019-05-10 stsp return err;
1737 56e0773d 2019-11-28 stsp got_object_tree_entry_is_submodule(struct got_tree_entry *te)
1739 63c5ca5d 2019-08-24 stsp return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
1742 ca6e02ac 2020-01-07 stsp const struct got_error *
1743 ca6e02ac 2020-01-07 stsp got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
1744 ca6e02ac 2020-01-07 stsp struct got_object_id *commit_id, const char *path,
1745 ca6e02ac 2020-01-07 stsp struct got_repository *repo)
1747 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
1748 ca6e02ac 2020-01-07 stsp struct got_pack *pack = NULL;
1749 ca6e02ac 2020-01-07 stsp struct got_packidx *packidx = NULL;
1750 ca6e02ac 2020-01-07 stsp char *path_packfile = NULL;
1751 ca6e02ac 2020-01-07 stsp struct got_commit_object *changed_commit = NULL;
1752 ca6e02ac 2020-01-07 stsp struct got_object_id *changed_commit_id = NULL;
1755 ca6e02ac 2020-01-07 stsp err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
1756 ca6e02ac 2020-01-07 stsp if (err) {
1757 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
1758 ca6e02ac 2020-01-07 stsp return err;
1759 ca6e02ac 2020-01-07 stsp return NULL;
1762 ca6e02ac 2020-01-07 stsp err = get_packfile_path(&path_packfile, packidx);
1764 ca6e02ac 2020-01-07 stsp return err;
1766 ca6e02ac 2020-01-07 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1767 ca6e02ac 2020-01-07 stsp if (pack == NULL) {
1768 ca6e02ac 2020-01-07 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
1770 ca6e02ac 2020-01-07 stsp goto done;
1773 ca6e02ac 2020-01-07 stsp if (pack->privsep_child == NULL) {
1774 ca6e02ac 2020-01-07 stsp err = start_pack_privsep_child(pack, packidx);
1776 ca6e02ac 2020-01-07 stsp goto done;
1779 ca6e02ac 2020-01-07 stsp err = got_privsep_send_commit_traversal_request(
1780 ca6e02ac 2020-01-07 stsp pack->privsep_child->ibuf, commit_id, idx, path);
1782 ca6e02ac 2020-01-07 stsp goto done;
1784 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_traversed_commits(&changed_commit,
1785 ca6e02ac 2020-01-07 stsp &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
1787 ca6e02ac 2020-01-07 stsp goto done;
1789 ca6e02ac 2020-01-07 stsp if (changed_commit) {
1791 ca6e02ac 2020-01-07 stsp * Cache the commit in which the path was changed.
1792 ca6e02ac 2020-01-07 stsp * This commit might be opened again soon.
1794 ca6e02ac 2020-01-07 stsp changed_commit->refcnt++;
1795 ca6e02ac 2020-01-07 stsp err = got_repo_cache_commit(repo, changed_commit_id,
1796 ca6e02ac 2020-01-07 stsp changed_commit);
1797 ca6e02ac 2020-01-07 stsp got_object_commit_close(changed_commit);
1800 ca6e02ac 2020-01-07 stsp free(path_packfile);
1801 ca6e02ac 2020-01-07 stsp free(changed_commit_id);
1802 ca6e02ac 2020-01-07 stsp return err;