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 7bb0daa1 2018-06-21 stsp #include "got_lib_repository.h"
57 ab9a70b2 2017-11-06 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 3235492e 2018-04-01 stsp struct got_object_id *
61 3235492e 2018-04-01 stsp got_object_get_id(struct got_object *obj)
63 6402fb3c 2018-09-15 stsp return &obj->id;
66 bacc9935 2018-05-20 stsp const struct got_error *
67 bacc9935 2018-05-20 stsp got_object_get_id_str(char **outbuf, struct got_object *obj)
69 bacc9935 2018-05-20 stsp return got_object_id_str(outbuf, &obj->id);
72 15a94983 2018-12-23 stsp const struct got_error *
73 15a94983 2018-12-23 stsp got_object_get_type(int *type, struct got_repository *repo,
74 15a94983 2018-12-23 stsp struct got_object_id *id)
76 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
77 15a94983 2018-12-23 stsp struct got_object *obj;
79 15a94983 2018-12-23 stsp err = got_object_open(&obj, repo, id);
83 b107e67f 2018-01-19 stsp switch (obj->type) {
84 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_COMMIT:
85 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_TREE:
86 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_BLOB:
87 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
88 15a94983 2018-12-23 stsp *type = obj->type;
91 15a94983 2018-12-23 stsp err = got_error(GOT_ERR_OBJ_TYPE);
95 15a94983 2018-12-23 stsp got_object_close(obj);
99 90bdb554 2019-04-11 stsp const struct got_error *
100 90bdb554 2019-04-11 stsp got_object_get_path(char **path, struct got_object_id *id,
101 90bdb554 2019-04-11 stsp struct got_repository *repo)
103 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
104 7a132809 2018-07-23 stsp char *hex = NULL;
105 41d2888b 2019-08-11 stsp char *path_objects;
107 e6b1056e 2018-04-22 stsp *path = NULL;
109 41d2888b 2019-08-11 stsp path_objects = got_repo_get_path_objects(repo);
110 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
111 638f9024 2019-05-13 stsp return got_error_from_errno("got_repo_get_path_objects");
113 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
117 d1cda826 2017-11-06 stsp if (asprintf(path, "%s/%.2x/%s", path_objects,
118 d1cda826 2017-11-06 stsp id->sha1[0], hex + 2) == -1)
119 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
123 d1cda826 2017-11-06 stsp free(path_objects);
124 d1cda826 2017-11-06 stsp return err;
127 4ee4114f 2018-01-23 stsp static const struct got_error *
128 4796fb13 2018-12-23 stsp open_loose_object(int *fd, struct got_object_id *id,
129 4796fb13 2018-12-23 stsp struct got_repository *repo)
131 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
132 a1fd68d8 2018-01-12 stsp char *path;
134 90bdb554 2019-04-11 stsp err = got_object_get_path(&path, id, repo);
136 d1cda826 2017-11-06 stsp return err;
137 a5b57ccf 2019-04-11 stsp *fd = open(path, O_RDONLY | O_NOFOLLOW);
138 d5003b79 2018-04-22 stsp if (*fd == -1) {
139 e82b1d81 2019-07-27 stsp err = got_error_from_errno2("open", path);
143 4558fcd4 2018-01-14 stsp free(path);
144 2090a03d 2018-09-09 stsp return err;
147 2090a03d 2018-09-09 stsp static const struct got_error *
148 2090a03d 2018-09-09 stsp get_packfile_path(char **path_packfile, struct got_packidx *packidx)
150 2090a03d 2018-09-09 stsp size_t size;
152 2090a03d 2018-09-09 stsp /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
153 2090a03d 2018-09-09 stsp size = strlen(packidx->path_packidx) + 2;
154 2090a03d 2018-09-09 stsp if (size < GOT_PACKFILE_NAMELEN + 1)
155 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_BAD_PATH);
157 08451938 2018-11-05 stsp *path_packfile = malloc(size);
158 2090a03d 2018-09-09 stsp if (*path_packfile == NULL)
159 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
161 2090a03d 2018-09-09 stsp /* Copy up to and excluding ".idx". */
162 2090a03d 2018-09-09 stsp if (strlcpy(*path_packfile, packidx->path_packidx,
163 2090a03d 2018-09-09 stsp size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
164 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_NO_SPACE);
166 2090a03d 2018-09-09 stsp if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
167 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_NO_SPACE);
169 2090a03d 2018-09-09 stsp return NULL;
172 2090a03d 2018-09-09 stsp static const struct got_error *
173 a158c901 2018-12-23 stsp request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
174 a158c901 2018-12-23 stsp struct got_object_id *id)
176 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
177 a158c901 2018-12-23 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
179 a158c901 2018-12-23 stsp err = got_privsep_send_packed_obj_req(ibuf, idx, id);
181 a158c901 2018-12-23 stsp return err;
183 a158c901 2018-12-23 stsp err = got_privsep_recv_obj(obj, ibuf);
185 a158c901 2018-12-23 stsp return err;
187 a158c901 2018-12-23 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
189 a158c901 2018-12-23 stsp return NULL;
192 da506691 2019-05-22 stsp static void
193 b48e2ddb 2019-05-22 stsp set_max_datasize(void)
195 b48e2ddb 2019-05-22 stsp struct rlimit rl;
197 b48e2ddb 2019-05-22 stsp if (getrlimit(RLIMIT_DATA, &rl) != 0)
200 b48e2ddb 2019-05-22 stsp rl.rlim_cur = rl.rlim_max;
201 b48e2ddb 2019-05-22 stsp setrlimit(RLIMIT_DATA, &rl);
204 a158c901 2018-12-23 stsp static const struct got_error *
205 711fb6e8 2018-12-23 stsp start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
207 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
208 a158c901 2018-12-23 stsp int imsg_fds[2];
210 a158c901 2018-12-23 stsp struct imsgbuf *ibuf;
212 a158c901 2018-12-23 stsp ibuf = calloc(1, sizeof(*ibuf));
213 a158c901 2018-12-23 stsp if (ibuf == NULL)
214 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
216 a158c901 2018-12-23 stsp pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
217 a158c901 2018-12-23 stsp if (pack->privsep_child == NULL) {
218 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
219 a158c901 2018-12-23 stsp free(ibuf);
220 a158c901 2018-12-23 stsp return err;
223 a158c901 2018-12-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
224 638f9024 2019-05-13 stsp err = got_error_from_errno("socketpair");
228 a158c901 2018-12-23 stsp pid = fork();
229 a158c901 2018-12-23 stsp if (pid == -1) {
230 638f9024 2019-05-13 stsp err = got_error_from_errno("fork");
232 a158c901 2018-12-23 stsp } else if (pid == 0) {
233 b48e2ddb 2019-05-22 stsp set_max_datasize();
234 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
235 a158c901 2018-12-23 stsp pack->path_packfile);
236 a158c901 2018-12-23 stsp /* not reached */
239 3a6ce05a 2019-02-11 stsp if (close(imsg_fds[1]) != 0)
240 638f9024 2019-05-13 stsp return got_error_from_errno("close");
241 a158c901 2018-12-23 stsp pack->privsep_child->imsg_fd = imsg_fds[0];
242 a158c901 2018-12-23 stsp pack->privsep_child->pid = pid;
243 a158c901 2018-12-23 stsp imsg_init(ibuf, imsg_fds[0]);
244 a158c901 2018-12-23 stsp pack->privsep_child->ibuf = ibuf;
246 a158c901 2018-12-23 stsp err = got_privsep_init_pack_child(ibuf, pack, packidx);
248 a158c901 2018-12-23 stsp const struct got_error *child_err;
249 a158c901 2018-12-23 stsp err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
250 a158c901 2018-12-23 stsp child_err = got_privsep_wait_for_child(
251 a158c901 2018-12-23 stsp pack->privsep_child->pid);
252 a158c901 2018-12-23 stsp if (child_err && err == NULL)
253 a158c901 2018-12-23 stsp err = child_err;
257 a158c901 2018-12-23 stsp free(ibuf);
258 a158c901 2018-12-23 stsp free(pack->privsep_child);
259 a158c901 2018-12-23 stsp pack->privsep_child = NULL;
261 a158c901 2018-12-23 stsp return err;
264 711fb6e8 2018-12-23 stsp static const struct got_error *
265 711fb6e8 2018-12-23 stsp read_packed_object_privsep(struct got_object **obj,
266 711fb6e8 2018-12-23 stsp struct got_repository *repo, struct got_pack *pack,
267 711fb6e8 2018-12-23 stsp struct got_packidx *packidx, int idx, struct got_object_id *id)
269 711fb6e8 2018-12-23 stsp const struct got_error *err = NULL;
271 711fb6e8 2018-12-23 stsp if (pack->privsep_child)
272 711fb6e8 2018-12-23 stsp return request_packed_object(obj, pack, idx, id);
274 711fb6e8 2018-12-23 stsp err = start_pack_privsep_child(pack, packidx);
276 711fb6e8 2018-12-23 stsp return err;
278 711fb6e8 2018-12-23 stsp return request_packed_object(obj, pack, idx, id);
282 a158c901 2018-12-23 stsp static const struct got_error *
283 2090a03d 2018-09-09 stsp open_packed_object(struct got_object **obj, struct got_object_id *id,
284 2090a03d 2018-09-09 stsp struct got_repository *repo)
286 2090a03d 2018-09-09 stsp const struct got_error *err = NULL;
287 2090a03d 2018-09-09 stsp struct got_pack *pack = NULL;
288 2090a03d 2018-09-09 stsp struct got_packidx *packidx = NULL;
290 2090a03d 2018-09-09 stsp char *path_packfile;
292 2090a03d 2018-09-09 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
294 2090a03d 2018-09-09 stsp return err;
296 2090a03d 2018-09-09 stsp err = get_packfile_path(&path_packfile, packidx);
298 2090a03d 2018-09-09 stsp return err;
300 2090a03d 2018-09-09 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
301 2090a03d 2018-09-09 stsp if (pack == NULL) {
302 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
307 a158c901 2018-12-23 stsp err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
311 5f25cc85 2019-11-26 stsp err = got_repo_cache_pack(NULL, repo, path_packfile, packidx);
313 2090a03d 2018-09-09 stsp free(path_packfile);
314 4558fcd4 2018-01-14 stsp return err;
317 9f2369b0 2018-12-24 stsp static const struct got_error *
318 9f2369b0 2018-12-24 stsp request_object(struct got_object **obj, struct got_repository *repo, int fd)
320 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
321 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
323 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
325 aea5f015 2018-12-24 stsp err = got_privsep_send_obj_req(ibuf, fd);
327 9f2369b0 2018-12-24 stsp return err;
329 9f2369b0 2018-12-24 stsp return got_privsep_recv_obj(obj, ibuf);
332 9f2369b0 2018-12-24 stsp static const struct got_error *
333 9f2369b0 2018-12-24 stsp read_object_header_privsep(struct got_object **obj, struct got_repository *repo,
334 9f2369b0 2018-12-24 stsp int obj_fd)
336 ddc7b220 2019-09-08 stsp const struct got_error *err;
337 9f2369b0 2018-12-24 stsp int imsg_fds[2];
339 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
341 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
342 9f2369b0 2018-12-24 stsp return request_object(obj, repo, obj_fd);
344 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
345 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
346 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
348 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
349 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
350 ddc7b220 2019-09-08 stsp free(ibuf);
351 ddc7b220 2019-09-08 stsp return err;
354 9f2369b0 2018-12-24 stsp pid = fork();
355 ddc7b220 2019-09-08 stsp if (pid == -1) {
356 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
357 ddc7b220 2019-09-08 stsp free(ibuf);
358 ddc7b220 2019-09-08 stsp return err;
360 9f2369b0 2018-12-24 stsp else if (pid == 0) {
361 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
362 9f2369b0 2018-12-24 stsp repo->path);
363 9f2369b0 2018-12-24 stsp /* not reached */
366 ddc7b220 2019-09-08 stsp if (close(imsg_fds[1]) != 0) {
367 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
368 ddc7b220 2019-09-08 stsp free(ibuf);
369 ddc7b220 2019-09-08 stsp return err;
371 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
372 9f2369b0 2018-12-24 stsp imsg_fds[0];
373 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
374 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
375 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
377 9f2369b0 2018-12-24 stsp return request_object(obj, repo, obj_fd);
381 4558fcd4 2018-01-14 stsp const struct got_error *
382 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
383 4558fcd4 2018-01-14 stsp struct got_object_id *id)
385 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
386 6c00b545 2018-01-17 stsp char *path;
389 7bb0daa1 2018-06-21 stsp *obj = got_repo_get_cached_object(repo, id);
390 7bb0daa1 2018-06-21 stsp if (*obj != NULL) {
391 7bb0daa1 2018-06-21 stsp (*obj)->refcnt++;
392 7bb0daa1 2018-06-21 stsp return NULL;
395 e82b1d81 2019-07-27 stsp err = open_packed_object(obj, id, repo);
396 e82b1d81 2019-07-27 stsp if (err && err->code != GOT_ERR_NO_OBJ)
397 e82b1d81 2019-07-27 stsp return err;
398 e82b1d81 2019-07-27 stsp if (*obj) {
399 e82b1d81 2019-07-27 stsp (*obj)->refcnt++;
400 e82b1d81 2019-07-27 stsp return got_repo_cache_object(repo, id, *obj);
403 90bdb554 2019-04-11 stsp err = got_object_get_path(&path, id, repo);
405 4558fcd4 2018-01-14 stsp return err;
407 a5b57ccf 2019-04-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
408 2178c42e 2018-04-22 stsp if (fd == -1) {
409 e82b1d81 2019-07-27 stsp if (errno == ENOENT)
410 e82b1d81 2019-07-27 stsp err = got_error_no_obj(id);
412 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", path);
415 9f2369b0 2018-12-24 stsp err = read_object_header_privsep(obj, repo, fd);
418 ab9a70b2 2017-11-06 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
421 59790a32 2018-09-15 stsp (*obj)->refcnt++;
422 59790a32 2018-09-15 stsp err = got_repo_cache_object(repo, id, *obj);
424 6c00b545 2018-01-17 stsp free(path);
425 ab9a70b2 2017-11-06 stsp return err;
429 6dfa2fd3 2018-02-12 stsp const struct got_error *
430 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
431 6dfa2fd3 2018-02-12 stsp const char *id_str)
433 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
435 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
436 6dd1ece6 2019-11-10 stsp return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
438 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
441 15a94983 2018-12-23 stsp const struct got_error *
442 15a94983 2018-12-23 stsp got_object_resolve_id_str(struct got_object_id **id,
443 15a94983 2018-12-23 stsp struct got_repository *repo, const char *id_str)
445 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
446 15a94983 2018-12-23 stsp struct got_object *obj;
448 15a94983 2018-12-23 stsp err = got_object_open_by_id_str(&obj, repo, id_str);
450 15a94983 2018-12-23 stsp return err;
452 15a94983 2018-12-23 stsp *id = got_object_id_dup(got_object_get_id(obj));
453 15a94983 2018-12-23 stsp got_object_close(obj);
454 15a94983 2018-12-23 stsp if (*id == NULL)
455 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
457 15a94983 2018-12-23 stsp return NULL;
460 434025f3 2018-09-16 stsp static const struct got_error *
461 a158c901 2018-12-23 stsp request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
462 a158c901 2018-12-23 stsp int pack_idx, struct got_object_id *id)
464 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
466 a158c901 2018-12-23 stsp err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
469 a158c901 2018-12-23 stsp return err;
471 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
473 ca6e02ac 2020-01-07 stsp return err;
475 ca6e02ac 2020-01-07 stsp (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
476 ca6e02ac 2020-01-07 stsp return NULL;
479 a158c901 2018-12-23 stsp static const struct got_error *
480 a158c901 2018-12-23 stsp read_packed_commit_privsep(struct got_commit_object **commit,
481 a158c901 2018-12-23 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
482 a158c901 2018-12-23 stsp struct got_object_id *id)
484 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
486 a158c901 2018-12-23 stsp if (pack->privsep_child)
487 a158c901 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
489 711fb6e8 2018-12-23 stsp err = start_pack_privsep_child(pack, packidx);
491 a158c901 2018-12-23 stsp return err;
493 711fb6e8 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
496 9f2369b0 2018-12-24 stsp static const struct got_error *
497 9f2369b0 2018-12-24 stsp request_commit(struct got_commit_object **commit, struct got_repository *repo,
500 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
501 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
503 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
505 9f2369b0 2018-12-24 stsp err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
507 9f2369b0 2018-12-24 stsp return err;
509 9f2369b0 2018-12-24 stsp return got_privsep_recv_commit(commit, ibuf);
512 9f2369b0 2018-12-24 stsp static const struct got_error *
513 9f2369b0 2018-12-24 stsp read_commit_privsep(struct got_commit_object **commit, int obj_fd,
514 9f2369b0 2018-12-24 stsp struct got_repository *repo)
516 ddc7b220 2019-09-08 stsp const struct got_error *err;
517 9f2369b0 2018-12-24 stsp int imsg_fds[2];
519 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
521 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
522 9f2369b0 2018-12-24 stsp return request_commit(commit, repo, obj_fd);
524 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
525 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
526 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
528 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
529 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
530 ddc7b220 2019-09-08 stsp free(ibuf);
531 ddc7b220 2019-09-08 stsp return err;
534 9f2369b0 2018-12-24 stsp pid = fork();
535 ddc7b220 2019-09-08 stsp if (pid == -1) {
536 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
537 ddc7b220 2019-09-08 stsp free(ibuf);
538 ddc7b220 2019-09-08 stsp return err;
540 9f2369b0 2018-12-24 stsp else if (pid == 0) {
541 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
542 9f2369b0 2018-12-24 stsp repo->path);
543 9f2369b0 2018-12-24 stsp /* not reached */
546 ddc7b220 2019-09-08 stsp if (close(imsg_fds[1]) != 0) {
547 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
548 ddc7b220 2019-09-08 stsp free(ibuf);
549 ddc7b220 2019-09-08 stsp return err;
551 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
552 9f2369b0 2018-12-24 stsp imsg_fds[0];
553 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
554 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
555 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
557 9f2369b0 2018-12-24 stsp return request_commit(commit, repo, obj_fd);
561 a158c901 2018-12-23 stsp static const struct got_error *
562 434025f3 2018-09-16 stsp open_commit(struct got_commit_object **commit,
563 1785f84a 2018-12-23 stsp struct got_repository *repo, struct got_object_id *id, int check_cache)
565 434025f3 2018-09-16 stsp const struct got_error *err = NULL;
566 1785f84a 2018-12-23 stsp struct got_packidx *packidx = NULL;
568 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
570 434025f3 2018-09-16 stsp if (check_cache) {
571 1785f84a 2018-12-23 stsp *commit = got_repo_get_cached_commit(repo, id);
572 434025f3 2018-09-16 stsp if (*commit != NULL) {
573 434025f3 2018-09-16 stsp (*commit)->refcnt++;
574 434025f3 2018-09-16 stsp return NULL;
577 434025f3 2018-09-16 stsp *commit = NULL;
579 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
580 e82b1d81 2019-07-27 stsp if (err == NULL) {
581 1785f84a 2018-12-23 stsp struct got_pack *pack = NULL;
583 1785f84a 2018-12-23 stsp err = get_packfile_path(&path_packfile, packidx);
585 1785f84a 2018-12-23 stsp return err;
587 1785f84a 2018-12-23 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
588 434025f3 2018-09-16 stsp if (pack == NULL) {
589 1785f84a 2018-12-23 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
594 a158c901 2018-12-23 stsp err = read_packed_commit_privsep(commit, pack,
595 1785f84a 2018-12-23 stsp packidx, idx, id);
596 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
599 e82b1d81 2019-07-27 stsp err = open_loose_object(&fd, id, repo);
601 e82b1d81 2019-07-27 stsp return err;
602 9f2369b0 2018-12-24 stsp err = read_commit_privsep(commit, fd, repo);
605 434025f3 2018-09-16 stsp if (err == NULL) {
606 434025f3 2018-09-16 stsp (*commit)->refcnt++;
607 1785f84a 2018-12-23 stsp err = got_repo_cache_commit(repo, id, *commit);
610 8d2c5ea3 2019-08-13 stsp free(path_packfile);
611 e32baab7 2018-11-05 stsp return err;
614 e32baab7 2018-11-05 stsp const struct got_error *
615 e32baab7 2018-11-05 stsp got_object_open_as_commit(struct got_commit_object **commit,
616 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object_id *id)
618 e32baab7 2018-11-05 stsp *commit = got_repo_get_cached_commit(repo, id);
619 e32baab7 2018-11-05 stsp if (*commit != NULL) {
620 e32baab7 2018-11-05 stsp (*commit)->refcnt++;
621 e32baab7 2018-11-05 stsp return NULL;
624 1785f84a 2018-12-23 stsp return open_commit(commit, repo, id, 0);
627 e32baab7 2018-11-05 stsp const struct got_error *
628 e32baab7 2018-11-05 stsp got_object_commit_open(struct got_commit_object **commit,
629 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object *obj)
631 1785f84a 2018-12-23 stsp return open_commit(commit, repo, got_object_get_id(obj), 1);
634 434025f3 2018-09-16 stsp const struct got_error *
635 dbc6a6b6 2018-07-12 stsp got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
637 dbc6a6b6 2018-07-12 stsp const struct got_error *err = NULL;
639 dbc6a6b6 2018-07-12 stsp *qid = calloc(1, sizeof(**qid));
640 dbc6a6b6 2018-07-12 stsp if (*qid == NULL)
641 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
643 dbc6a6b6 2018-07-12 stsp (*qid)->id = got_object_id_dup(id);
644 dbc6a6b6 2018-07-12 stsp if ((*qid)->id == NULL) {
645 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
646 fa2f6902 2018-07-23 stsp got_object_qid_free(*qid);
647 dbc6a6b6 2018-07-12 stsp *qid = NULL;
648 dbc6a6b6 2018-07-12 stsp return err;
651 dbc6a6b6 2018-07-12 stsp return NULL;
654 a158c901 2018-12-23 stsp static const struct got_error *
655 13c729f7 2018-12-24 stsp request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
656 13c729f7 2018-12-24 stsp int pack_idx, struct got_object_id *id)
658 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
660 13c729f7 2018-12-24 stsp err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
663 a158c901 2018-12-23 stsp return err;
665 a158c901 2018-12-23 stsp return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
668 71eb0e7f 2018-09-16 stsp static const struct got_error *
669 13c729f7 2018-12-24 stsp read_packed_tree_privsep(struct got_tree_object **tree,
670 13c729f7 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
671 13c729f7 2018-12-24 stsp struct got_object_id *id)
673 13c729f7 2018-12-24 stsp const struct got_error *err = NULL;
675 13c729f7 2018-12-24 stsp if (pack->privsep_child)
676 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
678 13c729f7 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
680 13c729f7 2018-12-24 stsp return err;
682 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
685 13c729f7 2018-12-24 stsp static const struct got_error *
686 9f2369b0 2018-12-24 stsp request_tree(struct got_tree_object **tree, struct got_repository *repo,
689 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
690 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
692 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
694 9f2369b0 2018-12-24 stsp err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
696 9f2369b0 2018-12-24 stsp return err;
698 9f2369b0 2018-12-24 stsp return got_privsep_recv_tree(tree, ibuf);
701 9f2369b0 2018-12-24 stsp const struct got_error *
702 9f2369b0 2018-12-24 stsp read_tree_privsep(struct got_tree_object **tree, int obj_fd,
703 9f2369b0 2018-12-24 stsp struct got_repository *repo)
705 ddc7b220 2019-09-08 stsp const struct got_error *err;
706 9f2369b0 2018-12-24 stsp int imsg_fds[2];
708 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
710 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
711 9f2369b0 2018-12-24 stsp return request_tree(tree, repo, obj_fd);
713 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
714 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
715 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
717 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
718 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
719 ddc7b220 2019-09-08 stsp free(ibuf);
720 ddc7b220 2019-09-08 stsp return err;
723 9f2369b0 2018-12-24 stsp pid = fork();
724 ddc7b220 2019-09-08 stsp if (pid == -1) {
725 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
726 ddc7b220 2019-09-08 stsp free(ibuf);
727 ddc7b220 2019-09-08 stsp return err;
729 9f2369b0 2018-12-24 stsp else if (pid == 0) {
730 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
731 9f2369b0 2018-12-24 stsp repo->path);
732 9f2369b0 2018-12-24 stsp /* not reached */
735 ddc7b220 2019-09-08 stsp if (close(imsg_fds[1]) != 0) {
736 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
737 ddc7b220 2019-09-08 stsp free(ibuf);
738 ddc7b220 2019-09-08 stsp return err;
740 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
741 9f2369b0 2018-12-24 stsp imsg_fds[0];
742 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
743 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
744 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
747 9f2369b0 2018-12-24 stsp return request_tree(tree, repo, obj_fd);
750 9f2369b0 2018-12-24 stsp static const struct got_error *
751 13c729f7 2018-12-24 stsp open_tree(struct got_tree_object **tree, struct got_repository *repo,
752 13c729f7 2018-12-24 stsp struct got_object_id *id, int check_cache)
754 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
755 13c729f7 2018-12-24 stsp struct got_packidx *packidx = NULL;
757 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
759 71eb0e7f 2018-09-16 stsp if (check_cache) {
760 13c729f7 2018-12-24 stsp *tree = got_repo_get_cached_tree(repo, id);
761 71eb0e7f 2018-09-16 stsp if (*tree != NULL) {
762 71eb0e7f 2018-09-16 stsp (*tree)->refcnt++;
763 71eb0e7f 2018-09-16 stsp return NULL;
766 71eb0e7f 2018-09-16 stsp *tree = NULL;
768 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
769 e82b1d81 2019-07-27 stsp if (err == NULL) {
770 13c729f7 2018-12-24 stsp struct got_pack *pack = NULL;
772 13c729f7 2018-12-24 stsp err = get_packfile_path(&path_packfile, packidx);
774 13c729f7 2018-12-24 stsp return err;
776 13c729f7 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
777 e7885405 2018-09-10 stsp if (pack == NULL) {
778 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
783 13c729f7 2018-12-24 stsp err = read_packed_tree_privsep(tree, pack,
784 13c729f7 2018-12-24 stsp packidx, idx, id);
785 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
788 e82b1d81 2019-07-27 stsp err = open_loose_object(&fd, id, repo);
790 e82b1d81 2019-07-27 stsp return err;
791 9f2369b0 2018-12-24 stsp err = read_tree_privsep(tree, fd, repo);
794 f6be5c30 2018-06-22 stsp if (err == NULL) {
795 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
796 13c729f7 2018-12-24 stsp err = got_repo_cache_tree(repo, id, *tree);
799 8d2c5ea3 2019-08-13 stsp free(path_packfile);
800 776d4d29 2018-06-17 stsp return err;
803 776d4d29 2018-06-17 stsp const struct got_error *
804 776d4d29 2018-06-17 stsp got_object_open_as_tree(struct got_tree_object **tree,
805 776d4d29 2018-06-17 stsp struct got_repository *repo, struct got_object_id *id)
807 e8eb494a 2018-09-16 stsp *tree = got_repo_get_cached_tree(repo, id);
808 e8eb494a 2018-09-16 stsp if (*tree != NULL) {
809 e8eb494a 2018-09-16 stsp (*tree)->refcnt++;
810 e8eb494a 2018-09-16 stsp return NULL;
813 13c729f7 2018-12-24 stsp return open_tree(tree, repo, id, 0);
816 71eb0e7f 2018-09-16 stsp const struct got_error *
817 71eb0e7f 2018-09-16 stsp got_object_tree_open(struct got_tree_object **tree,
818 71eb0e7f 2018-09-16 stsp struct got_repository *repo, struct got_object *obj)
820 13c729f7 2018-12-24 stsp return open_tree(tree, repo, got_object_get_id(obj), 1);
824 56e0773d 2019-11-28 stsp got_object_tree_get_nentries(struct got_tree_object *tree)
826 56e0773d 2019-11-28 stsp return tree->nentries;
829 56e0773d 2019-11-28 stsp struct got_tree_entry *
830 56e0773d 2019-11-28 stsp got_object_tree_get_first_entry(struct got_tree_object *tree)
832 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, 0);
835 56e0773d 2019-11-28 stsp struct got_tree_entry *
836 56e0773d 2019-11-28 stsp got_object_tree_get_last_entry(struct got_tree_object *tree)
838 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, tree->nentries - 1);
841 56e0773d 2019-11-28 stsp struct got_tree_entry *
842 56e0773d 2019-11-28 stsp got_object_tree_get_entry(struct got_tree_object *tree, int i)
844 56e0773d 2019-11-28 stsp if (i < 0 || i >= tree->nentries)
845 56e0773d 2019-11-28 stsp return NULL;
846 56e0773d 2019-11-28 stsp return &tree->entries[i];
850 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(struct got_tree_entry *te)
852 56e0773d 2019-11-28 stsp return te->mode;
855 56e0773d 2019-11-28 stsp const char *
856 56e0773d 2019-11-28 stsp got_tree_entry_get_name(struct got_tree_entry *te)
858 56e0773d 2019-11-28 stsp return &te->name[0];
861 56e0773d 2019-11-28 stsp struct got_object_id *
862 56e0773d 2019-11-28 stsp got_tree_entry_get_id(struct got_tree_entry *te)
864 56e0773d 2019-11-28 stsp return &te->id;
868 56e0773d 2019-11-28 stsp got_tree_entry_get_index(struct got_tree_entry *te)
870 56e0773d 2019-11-28 stsp return te->idx;
873 56e0773d 2019-11-28 stsp struct got_tree_entry *
874 56e0773d 2019-11-28 stsp got_tree_entry_get_next(struct got_tree_object *tree,
875 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
877 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx + 1);
880 56e0773d 2019-11-28 stsp struct got_tree_entry *
881 56e0773d 2019-11-28 stsp got_tree_entry_get_prev(struct got_tree_object *tree,
882 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
884 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx - 1);
887 3840f4c9 2018-09-12 stsp static const struct got_error *
888 ac544f8c 2019-01-13 stsp request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
889 ebc55e2d 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
890 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
892 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
893 3840f4c9 2018-09-12 stsp int outfd_child;
894 3840f4c9 2018-09-12 stsp int basefd, accumfd; /* temporary files for delta application */
896 3840f4c9 2018-09-12 stsp basefd = got_opentempfd();
897 3840f4c9 2018-09-12 stsp if (basefd == -1)
898 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
899 3840f4c9 2018-09-12 stsp accumfd = got_opentempfd();
900 3840f4c9 2018-09-12 stsp if (accumfd == -1)
901 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
903 3840f4c9 2018-09-12 stsp outfd_child = dup(outfd);
904 3840f4c9 2018-09-12 stsp if (outfd_child == -1)
905 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
907 ebc55e2d 2018-12-24 stsp err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
909 3840f4c9 2018-09-12 stsp return err;
911 3840f4c9 2018-09-12 stsp err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
912 3840f4c9 2018-09-12 stsp outfd_child);
914 41496140 2019-02-21 stsp close(basefd);
915 41496140 2019-02-21 stsp close(accumfd);
916 3840f4c9 2018-09-12 stsp return err;
919 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
922 3840f4c9 2018-09-12 stsp close(accumfd);
923 3840f4c9 2018-09-12 stsp return err;
926 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
929 3840f4c9 2018-09-12 stsp return err;
931 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen,
932 ebc55e2d 2018-12-24 stsp pack->privsep_child->ibuf);
934 3840f4c9 2018-09-12 stsp return err;
936 3840f4c9 2018-09-12 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
937 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
939 3840f4c9 2018-09-12 stsp return err;
942 ebc55e2d 2018-12-24 stsp static const struct got_error *
943 ac544f8c 2019-01-13 stsp read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
944 ac544f8c 2019-01-13 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
945 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
947 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
949 ebc55e2d 2018-12-24 stsp if (pack->privsep_child == NULL) {
950 ebc55e2d 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
952 ebc55e2d 2018-12-24 stsp return err;
955 ac544f8c 2019-01-13 stsp return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
959 9f2369b0 2018-12-24 stsp static const struct got_error *
960 ac544f8c 2019-01-13 stsp request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
961 ac544f8c 2019-01-13 stsp int infd, struct imsgbuf *ibuf)
963 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
964 9f2369b0 2018-12-24 stsp int outfd_child;
966 9f2369b0 2018-12-24 stsp outfd_child = dup(outfd);
967 9f2369b0 2018-12-24 stsp if (outfd_child == -1)
968 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
970 9f2369b0 2018-12-24 stsp err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
972 9f2369b0 2018-12-24 stsp return err;
974 9f2369b0 2018-12-24 stsp err = got_privsep_send_blob_outfd(ibuf, outfd_child);
976 9f2369b0 2018-12-24 stsp return err;
978 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
980 9f2369b0 2018-12-24 stsp return err;
982 9f2369b0 2018-12-24 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
983 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
985 9f2369b0 2018-12-24 stsp return err;
988 9f2369b0 2018-12-24 stsp static const struct got_error *
989 ac544f8c 2019-01-13 stsp read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
990 9f2369b0 2018-12-24 stsp int outfd, int infd, struct got_repository *repo)
992 ddc7b220 2019-09-08 stsp const struct got_error *err;
993 9f2369b0 2018-12-24 stsp int imsg_fds[2];
995 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
997 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
998 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
999 ac544f8c 2019-01-13 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1002 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1003 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1004 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1006 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1007 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1008 ddc7b220 2019-09-08 stsp free(ibuf);
1009 ddc7b220 2019-09-08 stsp return err;
1012 9f2369b0 2018-12-24 stsp pid = fork();
1013 ddc7b220 2019-09-08 stsp if (pid == -1) {
1014 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1015 ddc7b220 2019-09-08 stsp free(ibuf);
1016 ddc7b220 2019-09-08 stsp return err;
1018 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1019 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1020 9f2369b0 2018-12-24 stsp repo->path);
1021 9f2369b0 2018-12-24 stsp /* not reached */
1024 ddc7b220 2019-09-08 stsp if (close(imsg_fds[1]) != 0) {
1025 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1026 ddc7b220 2019-09-08 stsp free(ibuf);
1027 ddc7b220 2019-09-08 stsp return err;
1029 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1030 9f2369b0 2018-12-24 stsp imsg_fds[0];
1031 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1032 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1033 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1035 ac544f8c 2019-01-13 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1038 ebc55e2d 2018-12-24 stsp static const struct got_error *
1039 ebc55e2d 2018-12-24 stsp open_blob(struct got_blob_object **blob, struct got_repository *repo,
1040 ebc55e2d 2018-12-24 stsp struct got_object_id *id, size_t blocksize)
1042 ebc55e2d 2018-12-24 stsp const struct got_error *err = NULL;
1043 ebc55e2d 2018-12-24 stsp struct got_packidx *packidx = NULL;
1045 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1046 ac544f8c 2019-01-13 stsp uint8_t *outbuf;
1047 e82b1d81 2019-07-27 stsp int outfd;
1048 ebc55e2d 2018-12-24 stsp size_t size, hdrlen;
1049 ebc55e2d 2018-12-24 stsp struct stat sb;
1051 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
1052 4558fcd4 2018-01-14 stsp if (*blob == NULL)
1053 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1055 55da3778 2018-09-10 stsp outfd = got_opentempfd();
1056 55da3778 2018-09-10 stsp if (outfd == -1)
1057 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
1059 062ebb78 2018-07-12 stsp (*blob)->read_buf = malloc(blocksize);
1060 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
1061 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1062 c7254d79 2018-04-24 stsp goto done;
1065 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1066 e82b1d81 2019-07-27 stsp if (err == NULL) {
1067 ebc55e2d 2018-12-24 stsp struct got_pack *pack = NULL;
1069 ebc55e2d 2018-12-24 stsp err = get_packfile_path(&path_packfile, packidx);
1071 ebc55e2d 2018-12-24 stsp goto done;
1073 ebc55e2d 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1074 55da3778 2018-09-10 stsp if (pack == NULL) {
1075 ebc55e2d 2018-12-24 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1078 55da3778 2018-09-10 stsp goto done;
1080 ac544f8c 2019-01-13 stsp err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1081 ac544f8c 2019-01-13 stsp pack, packidx, idx, id);
1082 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1085 e82b1d81 2019-07-27 stsp err = open_loose_object(&infd, id, repo);
1087 e82b1d81 2019-07-27 stsp goto done;
1088 ac544f8c 2019-01-13 stsp err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1092 55da3778 2018-09-10 stsp goto done;
1094 de060dff 2018-12-24 stsp if (hdrlen > size) {
1095 ebc55e2d 2018-12-24 stsp err = got_error(GOT_ERR_BAD_OBJ_HDR);
1096 ebc55e2d 2018-12-24 stsp goto done;
1099 ac544f8c 2019-01-13 stsp if (outbuf) {
1100 3a6ce05a 2019-02-11 stsp if (close(outfd) != 0 && err == NULL)
1101 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1102 ac544f8c 2019-01-13 stsp outfd = -1;
1103 ac544f8c 2019-01-13 stsp (*blob)->f = fmemopen(outbuf, size, "rb");
1104 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1105 638f9024 2019-05-13 stsp err = got_error_from_errno("fmemopen");
1106 ac544f8c 2019-01-13 stsp free(outbuf);
1107 ac544f8c 2019-01-13 stsp goto done;
1109 ac544f8c 2019-01-13 stsp (*blob)->data = outbuf;
1111 ac544f8c 2019-01-13 stsp if (fstat(outfd, &sb) == -1) {
1112 638f9024 2019-05-13 stsp err = got_error_from_errno("fstat");
1113 ac544f8c 2019-01-13 stsp goto done;
1116 ac544f8c 2019-01-13 stsp if (sb.st_size != size) {
1117 ac544f8c 2019-01-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1118 ac544f8c 2019-01-13 stsp goto done;
1121 ac544f8c 2019-01-13 stsp (*blob)->f = fdopen(outfd, "rb");
1122 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1123 638f9024 2019-05-13 stsp err = got_error_from_errno("fdopen");
1124 ac544f8c 2019-01-13 stsp close(outfd);
1125 ac544f8c 2019-01-13 stsp outfd = -1;
1126 ac544f8c 2019-01-13 stsp goto done;
1130 ebc55e2d 2018-12-24 stsp (*blob)->hdrlen = hdrlen;
1131 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
1132 ebc55e2d 2018-12-24 stsp memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1135 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1136 55da3778 2018-09-10 stsp if (err) {
1137 55da3778 2018-09-10 stsp if (*blob) {
1138 7baf5860 2019-03-19 stsp got_object_blob_close(*blob);
1139 55da3778 2018-09-10 stsp *blob = NULL;
1140 55da3778 2018-09-10 stsp } else if (outfd != -1)
1141 55da3778 2018-09-10 stsp close(outfd);
1143 a19581a2 2018-06-21 stsp return err;
1146 a19581a2 2018-06-21 stsp const struct got_error *
1147 a19581a2 2018-06-21 stsp got_object_open_as_blob(struct got_blob_object **blob,
1148 a19581a2 2018-06-21 stsp struct got_repository *repo, struct got_object_id *id,
1149 a19581a2 2018-06-21 stsp size_t blocksize)
1151 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, id, blocksize);
1154 ebc55e2d 2018-12-24 stsp const struct got_error *
1155 ebc55e2d 2018-12-24 stsp got_object_blob_open(struct got_blob_object **blob,
1156 ebc55e2d 2018-12-24 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
1158 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1161 fb43ecf1 2019-02-11 stsp const struct got_error *
1162 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
1164 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL;
1165 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
1166 7baf5860 2019-03-19 stsp if (blob->f && fclose(blob->f) != 0)
1167 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1168 ac544f8c 2019-01-13 stsp free(blob->data);
1169 68482ea3 2017-11-27 stsp free(blob);
1170 fb43ecf1 2019-02-11 stsp return err;
1174 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1176 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1180 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
1182 f934cf2c 2018-02-12 stsp return blob->hdrlen;
1185 f934cf2c 2018-02-12 stsp const uint8_t *
1186 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
1188 f934cf2c 2018-02-12 stsp return blob->read_buf;
1191 68482ea3 2017-11-27 stsp const struct got_error *
1192 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1196 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1197 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
1198 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
1199 eb651edf 2018-02-11 stsp *outlenp = n;
1200 35e9ba5d 2018-06-21 stsp return NULL;
1203 35e9ba5d 2018-06-21 stsp const struct got_error *
1204 f595d9bd 2019-08-14 stsp got_object_blob_dump_to_file(size_t *filesize, int *nlines,
1205 6c4c42e0 2019-06-24 stsp off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1207 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
1208 b6752625 2018-12-24 stsp size_t n, len, hdrlen;
1209 84451b3e 2018-07-10 stsp const uint8_t *buf;
1211 6c4c42e0 2019-06-24 stsp size_t noffsets = 0;
1212 f595d9bd 2019-08-14 stsp off_t off = 0, total_len = 0;
1214 6c4c42e0 2019-06-24 stsp if (line_offsets)
1215 6c4c42e0 2019-06-24 stsp *line_offsets = NULL;
1216 f595d9bd 2019-08-14 stsp if (filesize)
1217 f595d9bd 2019-08-14 stsp *filesize = 0;
1218 84451b3e 2018-07-10 stsp if (nlines)
1219 84451b3e 2018-07-10 stsp *nlines = 0;
1221 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1223 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
1225 35e9ba5d 2018-06-21 stsp return err;
1226 35e9ba5d 2018-06-21 stsp if (len == 0)
1228 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
1229 b02560ec 2019-08-19 stsp i = hdrlen;
1230 78695fb7 2019-08-12 stsp if (line_offsets && nlines) {
1231 78695fb7 2019-08-12 stsp if (*line_offsets == NULL) {
1232 78695fb7 2019-08-12 stsp /* Have some data but perhaps no '\n'. */
1233 78695fb7 2019-08-12 stsp noffsets = 1;
1234 78695fb7 2019-08-12 stsp *nlines = 1;
1235 f595d9bd 2019-08-14 stsp *line_offsets = calloc(1, sizeof(**line_offsets));
1236 78695fb7 2019-08-12 stsp if (*line_offsets == NULL)
1237 78695fb7 2019-08-12 stsp return got_error_from_errno("malloc");
1239 b02560ec 2019-08-19 stsp /* Skip forward over end of first line. */
1240 b02560ec 2019-08-19 stsp while (i < len) {
1241 b02560ec 2019-08-19 stsp if (buf[i] == '\n')
1246 b02560ec 2019-08-19 stsp /* Scan '\n' offsets in remaining chunk of data. */
1247 b02560ec 2019-08-19 stsp while (i < len) {
1248 b02560ec 2019-08-19 stsp if (buf[i] != '\n') {
1252 f595d9bd 2019-08-14 stsp (*nlines)++;
1253 78695fb7 2019-08-12 stsp if (noffsets < *nlines) {
1254 78695fb7 2019-08-12 stsp off_t *o = recallocarray(*line_offsets,
1255 78695fb7 2019-08-12 stsp noffsets, *nlines,
1256 78695fb7 2019-08-12 stsp sizeof(**line_offsets));
1257 78695fb7 2019-08-12 stsp if (o == NULL) {
1258 78695fb7 2019-08-12 stsp free(*line_offsets);
1259 78695fb7 2019-08-12 stsp *line_offsets = NULL;
1260 78695fb7 2019-08-12 stsp return got_error_from_errno(
1261 78695fb7 2019-08-12 stsp "recallocarray");
1263 78695fb7 2019-08-12 stsp *line_offsets = o;
1264 78695fb7 2019-08-12 stsp noffsets = *nlines;
1266 f595d9bd 2019-08-14 stsp off = total_len + i - hdrlen + 1;
1267 f595d9bd 2019-08-14 stsp (*line_offsets)[*nlines - 1] = off;
1271 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
1272 454a6b59 2018-12-24 stsp n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1273 b6752625 2018-12-24 stsp if (n != len - hdrlen)
1274 b6752625 2018-12-24 stsp return got_ferror(outfile, GOT_ERR_IO);
1275 f595d9bd 2019-08-14 stsp total_len += len - hdrlen;
1276 35e9ba5d 2018-06-21 stsp hdrlen = 0;
1277 35e9ba5d 2018-06-21 stsp } while (len != 0);
1279 cbe7f848 2019-02-11 stsp if (fflush(outfile) != 0)
1280 638f9024 2019-05-13 stsp return got_error_from_errno("fflush");
1281 35e9ba5d 2018-06-21 stsp rewind(outfile);
1283 f595d9bd 2019-08-14 stsp if (filesize)
1284 f595d9bd 2019-08-14 stsp *filesize = total_len;
1286 776d4d29 2018-06-17 stsp return NULL;
1289 f4a881ce 2018-11-17 stsp static const struct got_error *
1290 268f7291 2018-12-24 stsp request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1291 268f7291 2018-12-24 stsp int pack_idx, struct got_object_id *id)
1293 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
1295 268f7291 2018-12-24 stsp err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1296 268f7291 2018-12-24 stsp pack_idx);
1298 a158c901 2018-12-23 stsp return err;
1300 a158c901 2018-12-23 stsp return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1303 268f7291 2018-12-24 stsp static const struct got_error *
1304 268f7291 2018-12-24 stsp read_packed_tag_privsep(struct got_tag_object **tag,
1305 268f7291 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1306 268f7291 2018-12-24 stsp struct got_object_id *id)
1308 268f7291 2018-12-24 stsp const struct got_error *err = NULL;
1310 268f7291 2018-12-24 stsp if (pack->privsep_child)
1311 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1313 268f7291 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
1315 268f7291 2018-12-24 stsp return err;
1317 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1320 9f2369b0 2018-12-24 stsp static const struct got_error *
1321 9f2369b0 2018-12-24 stsp request_tag(struct got_tag_object **tag, struct got_repository *repo,
1324 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1325 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1327 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1329 9f2369b0 2018-12-24 stsp err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1331 9f2369b0 2018-12-24 stsp return err;
1333 9f2369b0 2018-12-24 stsp return got_privsep_recv_tag(tag, ibuf);
1336 9f2369b0 2018-12-24 stsp static const struct got_error *
1337 9f2369b0 2018-12-24 stsp read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1338 9f2369b0 2018-12-24 stsp struct got_repository *repo)
1340 ddc7b220 2019-09-08 stsp const struct got_error *err;
1341 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1342 9f2369b0 2018-12-24 stsp pid_t pid;
1343 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1345 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1346 9f2369b0 2018-12-24 stsp return request_tag(tag, repo, obj_fd);
1348 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1349 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1350 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1352 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1353 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1354 ddc7b220 2019-09-08 stsp free(ibuf);
1355 ddc7b220 2019-09-08 stsp return err;
1358 9f2369b0 2018-12-24 stsp pid = fork();
1359 ddc7b220 2019-09-08 stsp if (pid == -1) {
1360 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1361 ddc7b220 2019-09-08 stsp free(ibuf);
1362 ddc7b220 2019-09-08 stsp return err;
1364 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1365 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1366 9f2369b0 2018-12-24 stsp repo->path);
1367 9f2369b0 2018-12-24 stsp /* not reached */
1370 ddc7b220 2019-09-08 stsp if (close(imsg_fds[1]) != 0) {
1371 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1372 ddc7b220 2019-09-08 stsp free(ibuf);
1373 ddc7b220 2019-09-08 stsp return err;
1375 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1376 9f2369b0 2018-12-24 stsp imsg_fds[0];
1377 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1378 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1379 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1381 9f2369b0 2018-12-24 stsp return request_tag(tag, repo, obj_fd);
1384 a158c901 2018-12-23 stsp static const struct got_error *
1385 268f7291 2018-12-24 stsp open_tag(struct got_tag_object **tag, struct got_repository *repo,
1386 268f7291 2018-12-24 stsp struct got_object_id *id, int check_cache)
1388 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1389 268f7291 2018-12-24 stsp struct got_packidx *packidx = NULL;
1391 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1392 5d844a1e 2019-08-13 stsp struct got_object *obj = NULL;
1393 5d844a1e 2019-08-13 stsp int obj_type = GOT_OBJ_TYPE_ANY;
1395 f4a881ce 2018-11-17 stsp if (check_cache) {
1396 268f7291 2018-12-24 stsp *tag = got_repo_get_cached_tag(repo, id);
1397 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1398 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1399 f4a881ce 2018-11-17 stsp return NULL;
1402 f4a881ce 2018-11-17 stsp *tag = NULL;
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 268f7291 2018-12-24 stsp struct got_pack *pack = NULL;
1408 268f7291 2018-12-24 stsp err = get_packfile_path(&path_packfile, packidx);
1410 268f7291 2018-12-24 stsp return err;
1412 268f7291 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1413 f4a881ce 2018-11-17 stsp if (pack == NULL) {
1414 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1417 8d2c5ea3 2019-08-13 stsp goto done;
1420 5d844a1e 2019-08-13 stsp /* Beware of "leightweight" tags: Check object type first. */
1421 5d844a1e 2019-08-13 stsp err = read_packed_object_privsep(&obj, repo, pack, packidx,
1424 5d844a1e 2019-08-13 stsp goto done;
1425 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1426 5d844a1e 2019-08-13 stsp got_object_close(obj);
1427 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG) {
1428 5d844a1e 2019-08-13 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1429 5d844a1e 2019-08-13 stsp goto done;
1431 5d844a1e 2019-08-13 stsp err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1432 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1435 e82b1d81 2019-07-27 stsp err = open_loose_object(&fd, id, repo);
1437 e82b1d81 2019-07-27 stsp return err;
1438 5d844a1e 2019-08-13 stsp err = read_object_header_privsep(&obj, repo, fd);
1440 5d844a1e 2019-08-13 stsp return err;
1441 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1442 5d844a1e 2019-08-13 stsp got_object_close(obj);
1443 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
1444 5d844a1e 2019-08-13 stsp return got_error(GOT_ERR_OBJ_TYPE);
1446 5d844a1e 2019-08-13 stsp err = open_loose_object(&fd, id, repo);
1448 5d844a1e 2019-08-13 stsp return err;
1449 9f2369b0 2018-12-24 stsp err = read_tag_privsep(tag, fd, repo);
1452 f4a881ce 2018-11-17 stsp if (err == NULL) {
1453 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1454 268f7291 2018-12-24 stsp err = got_repo_cache_tag(repo, id, *tag);
1457 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1458 f4a881ce 2018-11-17 stsp return err;
1461 f4a881ce 2018-11-17 stsp const struct got_error *
1462 f4a881ce 2018-11-17 stsp got_object_open_as_tag(struct got_tag_object **tag,
1463 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object_id *id)
1465 f4a881ce 2018-11-17 stsp *tag = got_repo_get_cached_tag(repo, id);
1466 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1467 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1468 f4a881ce 2018-11-17 stsp return NULL;
1471 268f7291 2018-12-24 stsp return open_tag(tag, repo, id, 0);
1474 f4a881ce 2018-11-17 stsp const struct got_error *
1475 f4a881ce 2018-11-17 stsp got_object_tag_open(struct got_tag_object **tag,
1476 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object *obj)
1478 268f7291 2018-12-24 stsp return open_tag(tag, repo, got_object_get_id(obj), 1);
1481 d24820bf 2019-08-11 stsp const char *
1482 d24820bf 2019-08-11 stsp got_object_tag_get_name(struct got_tag_object *tag)
1484 d24820bf 2019-08-11 stsp return tag->tag;
1488 0bd18d37 2019-02-01 stsp got_object_tag_get_object_type(struct got_tag_object *tag)
1490 0bd18d37 2019-02-01 stsp return tag->obj_type;
1493 0bd18d37 2019-02-01 stsp struct got_object_id *
1494 0bd18d37 2019-02-01 stsp got_object_tag_get_object_id(struct got_tag_object *tag)
1496 0bd18d37 2019-02-01 stsp return &tag->id;
1500 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_time(struct got_tag_object *tag)
1502 01073a5d 2019-08-22 stsp return tag->tagger_time;
1506 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1508 01073a5d 2019-08-22 stsp return tag->tagger_gmtoff;
1511 01073a5d 2019-08-22 stsp const char *
1512 01073a5d 2019-08-22 stsp got_object_tag_get_tagger(struct got_tag_object *tag)
1514 01073a5d 2019-08-22 stsp return tag->tagger;
1517 01073a5d 2019-08-22 stsp const char *
1518 01073a5d 2019-08-22 stsp got_object_tag_get_message(struct got_tag_object *tag)
1520 01073a5d 2019-08-22 stsp return tag->tagmsg;
1523 776d4d29 2018-06-17 stsp static struct got_tree_entry *
1524 65a9bbe9 2018-09-15 stsp find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1528 63da309a 2018-11-07 stsp /* Note that tree entries are sorted in strncmp() order. */
1529 56e0773d 2019-11-28 stsp for (i = 0; i < tree->nentries; i++) {
1530 56e0773d 2019-11-28 stsp struct got_tree_entry *te = &tree->entries[i];
1531 63da309a 2018-11-07 stsp int cmp = strncmp(te->name, name, len);
1532 63da309a 2018-11-07 stsp if (cmp < 0)
1534 63da309a 2018-11-07 stsp if (cmp > 0)
1536 63da309a 2018-11-07 stsp if (te->name[len] == '\0')
1537 776d4d29 2018-06-17 stsp return te;
1539 eb651edf 2018-02-11 stsp return NULL;
1542 56e0773d 2019-11-28 stsp struct got_tree_entry *
1543 a129376b 2019-03-28 stsp got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1545 a129376b 2019-03-28 stsp return find_entry_by_name(tree, name, strlen(name));
1548 776d4d29 2018-06-17 stsp const struct got_error *
1549 27d434c2 2018-09-15 stsp got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1550 776d4d29 2018-06-17 stsp struct got_object_id *commit_id, const char *path)
1552 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
1553 776d4d29 2018-06-17 stsp struct got_commit_object *commit = NULL;
1554 776d4d29 2018-06-17 stsp struct got_tree_object *tree = NULL;
1555 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
1556 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1557 b7cd37e5 2018-11-18 stsp size_t seglen;
1559 27d434c2 2018-09-15 stsp *id = NULL;
1561 776d4d29 2018-06-17 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
1563 776d4d29 2018-06-17 stsp goto done;
1565 776d4d29 2018-06-17 stsp /* Handle opening of root of commit's tree. */
1566 5e54fb30 2019-05-31 stsp if (got_path_is_root_dir(path)) {
1567 27d434c2 2018-09-15 stsp *id = got_object_id_dup(commit->tree_id);
1568 27d434c2 2018-09-15 stsp if (*id == NULL)
1569 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
1570 ca008b32 2018-07-22 stsp goto done;
1573 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1575 776d4d29 2018-06-17 stsp goto done;
1578 5e54fb30 2019-05-31 stsp while (s[0] == '/')
1581 65a9bbe9 2018-09-15 stsp seglen = 0;
1582 b7cd37e5 2018-11-18 stsp while (*s) {
1583 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
1585 776d4d29 2018-06-17 stsp if (*s != '/') {
1592 65a9bbe9 2018-09-15 stsp te = find_entry_by_name(tree, seg, seglen);
1593 db37e2c0 2018-06-21 stsp if (te == NULL) {
1594 d1451975 2018-11-11 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1595 776d4d29 2018-06-17 stsp goto done;
1598 b7cd37e5 2018-11-18 stsp if (*s == '\0')
1601 776d4d29 2018-06-17 stsp seg = s + 1;
1602 65a9bbe9 2018-09-15 stsp seglen = 0;
1605 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
1607 db37e2c0 2018-06-21 stsp te = NULL;
1609 776d4d29 2018-06-17 stsp goto done;
1610 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
1611 776d4d29 2018-06-17 stsp tree = next_tree;
1616 56e0773d 2019-11-28 stsp *id = got_object_id_dup(&te->id);
1617 27d434c2 2018-09-15 stsp if (*id == NULL)
1618 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
1620 d1451975 2018-11-11 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1622 776d4d29 2018-06-17 stsp if (commit)
1623 776d4d29 2018-06-17 stsp got_object_commit_close(commit);
1625 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
1626 776d4d29 2018-06-17 stsp return err;
1629 07862c20 2018-09-15 stsp const struct got_error *
1630 07862c20 2018-09-15 stsp got_object_tree_path_changed(int *changed,
1631 07862c20 2018-09-15 stsp struct got_tree_object *tree01, struct got_tree_object *tree02,
1632 07862c20 2018-09-15 stsp const char *path, struct got_repository *repo)
1634 07862c20 2018-09-15 stsp const struct got_error *err = NULL;
1635 07862c20 2018-09-15 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1636 07862c20 2018-09-15 stsp struct got_tree_entry *te1 = NULL, *te2 = NULL;
1637 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1638 3b7f9878 2018-11-18 stsp size_t seglen;
1640 07862c20 2018-09-15 stsp *changed = 0;
1642 07862c20 2018-09-15 stsp /* We are expecting an absolute in-repository path. */
1643 07862c20 2018-09-15 stsp if (path[0] != '/')
1644 07862c20 2018-09-15 stsp return got_error(GOT_ERR_NOT_ABSPATH);
1646 07862c20 2018-09-15 stsp /* We not do support comparing the root path. */
1647 07862c20 2018-09-15 stsp if (path[1] == '\0')
1648 07862c20 2018-09-15 stsp return got_error(GOT_ERR_BAD_PATH);
1650 07862c20 2018-09-15 stsp tree1 = tree01;
1651 07862c20 2018-09-15 stsp tree2 = tree02;
1653 07862c20 2018-09-15 stsp s++; /* skip leading '/' */
1655 65a9bbe9 2018-09-15 stsp seglen = 0;
1656 3b7f9878 2018-11-18 stsp while (*s) {
1657 07862c20 2018-09-15 stsp struct got_tree_object *next_tree1, *next_tree2;
1659 07862c20 2018-09-15 stsp if (*s != '/') {
1666 65a9bbe9 2018-09-15 stsp te1 = find_entry_by_name(tree1, seg, seglen);
1667 07862c20 2018-09-15 stsp if (te1 == NULL) {
1668 07862c20 2018-09-15 stsp err = got_error(GOT_ERR_NO_OBJ);
1669 07862c20 2018-09-15 stsp goto done;
1672 65a9bbe9 2018-09-15 stsp te2 = find_entry_by_name(tree2, seg, seglen);
1673 07862c20 2018-09-15 stsp if (te2 == NULL) {
1674 07862c20 2018-09-15 stsp *changed = 1;
1675 07862c20 2018-09-15 stsp goto done;
1678 07862c20 2018-09-15 stsp if (te1->mode != te2->mode) {
1679 07862c20 2018-09-15 stsp *changed = 1;
1680 07862c20 2018-09-15 stsp goto done;
1683 56e0773d 2019-11-28 stsp if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
1684 07862c20 2018-09-15 stsp *changed = 0;
1685 07862c20 2018-09-15 stsp goto done;
1688 3b7f9878 2018-11-18 stsp if (*s == '\0') { /* final path element */
1689 07862c20 2018-09-15 stsp *changed = 1;
1690 07862c20 2018-09-15 stsp goto done;
1693 07862c20 2018-09-15 stsp seg = s + 1;
1695 65a9bbe9 2018-09-15 stsp seglen = 0;
1697 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree1, repo,
1698 56e0773d 2019-11-28 stsp &te1->id);
1699 07862c20 2018-09-15 stsp te1 = NULL;
1701 07862c20 2018-09-15 stsp goto done;
1702 a31cea73 2018-09-15 stsp if (tree1 != tree01)
1703 a31cea73 2018-09-15 stsp got_object_tree_close(tree1);
1704 07862c20 2018-09-15 stsp tree1 = next_tree1;
1706 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree2, repo,
1707 56e0773d 2019-11-28 stsp &te2->id);
1708 07862c20 2018-09-15 stsp te2 = NULL;
1710 07862c20 2018-09-15 stsp goto done;
1711 a31cea73 2018-09-15 stsp if (tree2 != tree02)
1712 a31cea73 2018-09-15 stsp got_object_tree_close(tree2);
1713 07862c20 2018-09-15 stsp tree2 = next_tree2;
1717 a31cea73 2018-09-15 stsp if (tree1 && tree1 != tree01)
1718 07862c20 2018-09-15 stsp got_object_tree_close(tree1);
1719 a31cea73 2018-09-15 stsp if (tree2 && tree2 != tree02)
1720 07862c20 2018-09-15 stsp got_object_tree_close(tree2);
1721 77880158 2018-11-04 stsp return err;
1724 ed175427 2019-05-09 stsp const struct got_error *
1725 ed175427 2019-05-09 stsp got_object_tree_entry_dup(struct got_tree_entry **new_te,
1726 ed175427 2019-05-09 stsp struct got_tree_entry *te)
1728 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
1730 ed175427 2019-05-09 stsp *new_te = calloc(1, sizeof(**new_te));
1731 ed175427 2019-05-09 stsp if (*new_te == NULL)
1732 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1734 ed175427 2019-05-09 stsp (*new_te)->mode = te->mode;
1735 56e0773d 2019-11-28 stsp memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
1736 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
1737 8c4eabf2 2019-05-10 stsp return err;
1741 56e0773d 2019-11-28 stsp got_object_tree_entry_is_submodule(struct got_tree_entry *te)
1743 63c5ca5d 2019-08-24 stsp return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
1746 ca6e02ac 2020-01-07 stsp const struct got_error *
1747 ca6e02ac 2020-01-07 stsp got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
1748 ca6e02ac 2020-01-07 stsp struct got_object_id *commit_id, const char *path,
1749 ca6e02ac 2020-01-07 stsp struct got_repository *repo)
1751 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
1752 ca6e02ac 2020-01-07 stsp struct got_pack *pack = NULL;
1753 ca6e02ac 2020-01-07 stsp struct got_packidx *packidx = NULL;
1754 ca6e02ac 2020-01-07 stsp char *path_packfile = NULL;
1755 ca6e02ac 2020-01-07 stsp struct got_commit_object *changed_commit = NULL;
1756 ca6e02ac 2020-01-07 stsp struct got_object_id *changed_commit_id = NULL;
1759 ca6e02ac 2020-01-07 stsp err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
1760 ca6e02ac 2020-01-07 stsp if (err) {
1761 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
1762 ca6e02ac 2020-01-07 stsp return err;
1763 ca6e02ac 2020-01-07 stsp return NULL;
1766 ca6e02ac 2020-01-07 stsp err = get_packfile_path(&path_packfile, packidx);
1768 ca6e02ac 2020-01-07 stsp return err;
1770 ca6e02ac 2020-01-07 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1771 ca6e02ac 2020-01-07 stsp if (pack == NULL) {
1772 ca6e02ac 2020-01-07 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
1774 ca6e02ac 2020-01-07 stsp goto done;
1777 ca6e02ac 2020-01-07 stsp if (pack->privsep_child == NULL) {
1778 ca6e02ac 2020-01-07 stsp err = start_pack_privsep_child(pack, packidx);
1780 ca6e02ac 2020-01-07 stsp goto done;
1783 ca6e02ac 2020-01-07 stsp err = got_privsep_send_commit_traversal_request(
1784 ca6e02ac 2020-01-07 stsp pack->privsep_child->ibuf, commit_id, idx, path);
1786 ca6e02ac 2020-01-07 stsp goto done;
1788 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_traversed_commits(&changed_commit,
1789 ca6e02ac 2020-01-07 stsp &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
1791 ca6e02ac 2020-01-07 stsp goto done;
1793 ca6e02ac 2020-01-07 stsp if (changed_commit) {
1795 ca6e02ac 2020-01-07 stsp * Cache the commit in which the path was changed.
1796 ca6e02ac 2020-01-07 stsp * This commit might be opened again soon.
1798 ca6e02ac 2020-01-07 stsp changed_commit->refcnt++;
1799 ca6e02ac 2020-01-07 stsp err = got_repo_cache_commit(repo, changed_commit_id,
1800 ca6e02ac 2020-01-07 stsp changed_commit);
1801 ca6e02ac 2020-01-07 stsp got_object_commit_close(changed_commit);
1804 ca6e02ac 2020-01-07 stsp free(path_packfile);
1805 ca6e02ac 2020-01-07 stsp free(changed_commit_id);
1806 ca6e02ac 2020-01-07 stsp return err;