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 8bf5b3c9 2018-03-17 stsp struct got_object_id *
61 8bf5b3c9 2018-03-17 stsp got_object_id_dup(struct got_object_id *id1)
63 8bf5b3c9 2018-03-17 stsp struct got_object_id *id2;
65 8bf5b3c9 2018-03-17 stsp id2 = malloc(sizeof(*id2));
66 8bf5b3c9 2018-03-17 stsp if (id2 == NULL)
67 8bf5b3c9 2018-03-17 stsp return NULL;
68 8bf5b3c9 2018-03-17 stsp memcpy(id2, id1, sizeof(*id2));
72 3235492e 2018-04-01 stsp struct got_object_id *
73 3235492e 2018-04-01 stsp got_object_get_id(struct got_object *obj)
75 6402fb3c 2018-09-15 stsp return &obj->id;
78 bacc9935 2018-05-20 stsp const struct got_error *
79 bacc9935 2018-05-20 stsp got_object_get_id_str(char **outbuf, struct got_object *obj)
81 bacc9935 2018-05-20 stsp return got_object_id_str(outbuf, &obj->id);
84 15a94983 2018-12-23 stsp const struct got_error *
85 15a94983 2018-12-23 stsp got_object_get_type(int *type, struct got_repository *repo,
86 15a94983 2018-12-23 stsp struct got_object_id *id)
88 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
89 15a94983 2018-12-23 stsp struct got_object *obj;
91 15a94983 2018-12-23 stsp err = got_object_open(&obj, repo, id);
95 b107e67f 2018-01-19 stsp switch (obj->type) {
96 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_COMMIT:
97 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_TREE:
98 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_BLOB:
99 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
100 15a94983 2018-12-23 stsp *type = obj->type;
103 15a94983 2018-12-23 stsp err = got_error(GOT_ERR_OBJ_TYPE);
107 15a94983 2018-12-23 stsp got_object_close(obj);
108 15a94983 2018-12-23 stsp return err;
111 90bdb554 2019-04-11 stsp const struct got_error *
112 90bdb554 2019-04-11 stsp got_object_get_path(char **path, struct got_object_id *id,
113 90bdb554 2019-04-11 stsp struct got_repository *repo)
115 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
116 7a132809 2018-07-23 stsp char *hex = NULL;
117 41d2888b 2019-08-11 stsp char *path_objects;
119 e6b1056e 2018-04-22 stsp *path = NULL;
121 41d2888b 2019-08-11 stsp path_objects = got_repo_get_path_objects(repo);
122 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
123 638f9024 2019-05-13 stsp return got_error_from_errno("got_repo_get_path_objects");
125 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
129 d1cda826 2017-11-06 stsp if (asprintf(path, "%s/%.2x/%s", path_objects,
130 d1cda826 2017-11-06 stsp id->sha1[0], hex + 2) == -1)
131 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
135 d1cda826 2017-11-06 stsp free(path_objects);
136 d1cda826 2017-11-06 stsp return err;
139 4ee4114f 2018-01-23 stsp static const struct got_error *
140 4796fb13 2018-12-23 stsp open_loose_object(int *fd, struct got_object_id *id,
141 4796fb13 2018-12-23 stsp struct got_repository *repo)
143 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
144 a1fd68d8 2018-01-12 stsp char *path;
146 90bdb554 2019-04-11 stsp err = got_object_get_path(&path, id, repo);
148 d1cda826 2017-11-06 stsp return err;
149 a5b57ccf 2019-04-11 stsp *fd = open(path, O_RDONLY | O_NOFOLLOW);
150 d5003b79 2018-04-22 stsp if (*fd == -1) {
151 e82b1d81 2019-07-27 stsp err = got_error_from_errno2("open", path);
155 4558fcd4 2018-01-14 stsp free(path);
156 2090a03d 2018-09-09 stsp return err;
159 2090a03d 2018-09-09 stsp static const struct got_error *
160 2090a03d 2018-09-09 stsp get_packfile_path(char **path_packfile, struct got_packidx *packidx)
162 2090a03d 2018-09-09 stsp size_t size;
164 2090a03d 2018-09-09 stsp /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
165 2090a03d 2018-09-09 stsp size = strlen(packidx->path_packidx) + 2;
166 2090a03d 2018-09-09 stsp if (size < GOT_PACKFILE_NAMELEN + 1)
167 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_BAD_PATH);
169 08451938 2018-11-05 stsp *path_packfile = malloc(size);
170 2090a03d 2018-09-09 stsp if (*path_packfile == NULL)
171 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
173 2090a03d 2018-09-09 stsp /* Copy up to and excluding ".idx". */
174 2090a03d 2018-09-09 stsp if (strlcpy(*path_packfile, packidx->path_packidx,
175 2090a03d 2018-09-09 stsp size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
176 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_NO_SPACE);
178 2090a03d 2018-09-09 stsp if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
179 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_NO_SPACE);
181 2090a03d 2018-09-09 stsp return NULL;
184 2090a03d 2018-09-09 stsp static const struct got_error *
185 a158c901 2018-12-23 stsp request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
186 a158c901 2018-12-23 stsp struct got_object_id *id)
188 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
189 a158c901 2018-12-23 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
191 a158c901 2018-12-23 stsp err = got_privsep_send_packed_obj_req(ibuf, idx, id);
193 a158c901 2018-12-23 stsp return err;
195 a158c901 2018-12-23 stsp err = got_privsep_recv_obj(obj, ibuf);
197 a158c901 2018-12-23 stsp return err;
199 a158c901 2018-12-23 stsp (*obj)->path_packfile = strdup(pack->path_packfile);
200 a158c901 2018-12-23 stsp if ((*obj)->path_packfile == NULL) {
201 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
202 a158c901 2018-12-23 stsp return err;
204 a158c901 2018-12-23 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
206 a158c901 2018-12-23 stsp return NULL;
209 da506691 2019-05-22 stsp static void
210 b48e2ddb 2019-05-22 stsp set_max_datasize(void)
212 b48e2ddb 2019-05-22 stsp struct rlimit rl;
214 b48e2ddb 2019-05-22 stsp if (getrlimit(RLIMIT_DATA, &rl) != 0)
217 b48e2ddb 2019-05-22 stsp rl.rlim_cur = rl.rlim_max;
218 b48e2ddb 2019-05-22 stsp setrlimit(RLIMIT_DATA, &rl);
221 a158c901 2018-12-23 stsp static const struct got_error *
222 711fb6e8 2018-12-23 stsp start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
224 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
225 a158c901 2018-12-23 stsp int imsg_fds[2];
227 a158c901 2018-12-23 stsp struct imsgbuf *ibuf;
229 a158c901 2018-12-23 stsp ibuf = calloc(1, sizeof(*ibuf));
230 a158c901 2018-12-23 stsp if (ibuf == NULL)
231 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
233 a158c901 2018-12-23 stsp pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
234 a158c901 2018-12-23 stsp if (pack->privsep_child == NULL) {
235 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
236 a158c901 2018-12-23 stsp free(ibuf);
237 a158c901 2018-12-23 stsp return err;
240 a158c901 2018-12-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
241 638f9024 2019-05-13 stsp err = got_error_from_errno("socketpair");
245 a158c901 2018-12-23 stsp pid = fork();
246 a158c901 2018-12-23 stsp if (pid == -1) {
247 638f9024 2019-05-13 stsp err = got_error_from_errno("fork");
249 a158c901 2018-12-23 stsp } else if (pid == 0) {
250 b48e2ddb 2019-05-22 stsp set_max_datasize();
251 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
252 a158c901 2018-12-23 stsp pack->path_packfile);
253 a158c901 2018-12-23 stsp /* not reached */
256 3a6ce05a 2019-02-11 stsp if (close(imsg_fds[1]) != 0)
257 638f9024 2019-05-13 stsp return got_error_from_errno("close");
258 a158c901 2018-12-23 stsp pack->privsep_child->imsg_fd = imsg_fds[0];
259 a158c901 2018-12-23 stsp pack->privsep_child->pid = pid;
260 a158c901 2018-12-23 stsp imsg_init(ibuf, imsg_fds[0]);
261 a158c901 2018-12-23 stsp pack->privsep_child->ibuf = ibuf;
263 a158c901 2018-12-23 stsp err = got_privsep_init_pack_child(ibuf, pack, packidx);
265 a158c901 2018-12-23 stsp const struct got_error *child_err;
266 a158c901 2018-12-23 stsp err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
267 a158c901 2018-12-23 stsp child_err = got_privsep_wait_for_child(
268 a158c901 2018-12-23 stsp pack->privsep_child->pid);
269 a158c901 2018-12-23 stsp if (child_err && err == NULL)
270 a158c901 2018-12-23 stsp err = child_err;
274 a158c901 2018-12-23 stsp free(ibuf);
275 a158c901 2018-12-23 stsp free(pack->privsep_child);
276 a158c901 2018-12-23 stsp pack->privsep_child = NULL;
278 a158c901 2018-12-23 stsp return err;
281 711fb6e8 2018-12-23 stsp static const struct got_error *
282 711fb6e8 2018-12-23 stsp read_packed_object_privsep(struct got_object **obj,
283 711fb6e8 2018-12-23 stsp struct got_repository *repo, struct got_pack *pack,
284 711fb6e8 2018-12-23 stsp struct got_packidx *packidx, int idx, struct got_object_id *id)
286 711fb6e8 2018-12-23 stsp const struct got_error *err = NULL;
288 711fb6e8 2018-12-23 stsp if (pack->privsep_child)
289 711fb6e8 2018-12-23 stsp return request_packed_object(obj, pack, idx, id);
291 711fb6e8 2018-12-23 stsp err = start_pack_privsep_child(pack, packidx);
293 711fb6e8 2018-12-23 stsp return err;
295 711fb6e8 2018-12-23 stsp return request_packed_object(obj, pack, idx, id);
299 a158c901 2018-12-23 stsp static const struct got_error *
300 2090a03d 2018-09-09 stsp open_packed_object(struct got_object **obj, struct got_object_id *id,
301 2090a03d 2018-09-09 stsp struct got_repository *repo)
303 2090a03d 2018-09-09 stsp const struct got_error *err = NULL;
304 2090a03d 2018-09-09 stsp struct got_pack *pack = NULL;
305 2090a03d 2018-09-09 stsp struct got_packidx *packidx = NULL;
307 2090a03d 2018-09-09 stsp char *path_packfile;
309 2090a03d 2018-09-09 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
311 2090a03d 2018-09-09 stsp return err;
313 2090a03d 2018-09-09 stsp err = get_packfile_path(&path_packfile, packidx);
315 2090a03d 2018-09-09 stsp return err;
317 2090a03d 2018-09-09 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
318 2090a03d 2018-09-09 stsp if (pack == NULL) {
319 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
324 a158c901 2018-12-23 stsp err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
328 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
330 2090a03d 2018-09-09 stsp free(path_packfile);
331 4558fcd4 2018-01-14 stsp return err;
334 9f2369b0 2018-12-24 stsp static const struct got_error *
335 9f2369b0 2018-12-24 stsp request_object(struct got_object **obj, struct got_repository *repo, int fd)
337 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
338 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
340 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
342 aea5f015 2018-12-24 stsp err = got_privsep_send_obj_req(ibuf, fd);
344 9f2369b0 2018-12-24 stsp return err;
346 9f2369b0 2018-12-24 stsp return got_privsep_recv_obj(obj, ibuf);
349 9f2369b0 2018-12-24 stsp static const struct got_error *
350 9f2369b0 2018-12-24 stsp read_object_header_privsep(struct got_object **obj, struct got_repository *repo,
351 9f2369b0 2018-12-24 stsp int obj_fd)
353 9f2369b0 2018-12-24 stsp int imsg_fds[2];
355 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
357 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
358 9f2369b0 2018-12-24 stsp return request_object(obj, repo, obj_fd);
360 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
361 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
362 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
364 9f2369b0 2018-12-24 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
365 638f9024 2019-05-13 stsp return got_error_from_errno("socketpair");
367 9f2369b0 2018-12-24 stsp pid = fork();
368 9f2369b0 2018-12-24 stsp if (pid == -1)
369 638f9024 2019-05-13 stsp return got_error_from_errno("fork");
370 9f2369b0 2018-12-24 stsp else if (pid == 0) {
371 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
372 9f2369b0 2018-12-24 stsp repo->path);
373 9f2369b0 2018-12-24 stsp /* not reached */
376 3a6ce05a 2019-02-11 stsp if (close(imsg_fds[1]) != 0)
377 638f9024 2019-05-13 stsp return got_error_from_errno("close");
378 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
379 9f2369b0 2018-12-24 stsp imsg_fds[0];
380 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
381 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
382 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
384 9f2369b0 2018-12-24 stsp return request_object(obj, repo, obj_fd);
388 4558fcd4 2018-01-14 stsp const struct got_error *
389 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
390 4558fcd4 2018-01-14 stsp struct got_object_id *id)
392 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
393 6c00b545 2018-01-17 stsp char *path;
396 7bb0daa1 2018-06-21 stsp *obj = got_repo_get_cached_object(repo, id);
397 7bb0daa1 2018-06-21 stsp if (*obj != NULL) {
398 7bb0daa1 2018-06-21 stsp (*obj)->refcnt++;
399 7bb0daa1 2018-06-21 stsp return NULL;
402 e82b1d81 2019-07-27 stsp err = open_packed_object(obj, id, repo);
403 e82b1d81 2019-07-27 stsp if (err && err->code != GOT_ERR_NO_OBJ)
404 e82b1d81 2019-07-27 stsp return err;
405 e82b1d81 2019-07-27 stsp if (*obj) {
406 e82b1d81 2019-07-27 stsp (*obj)->refcnt++;
407 e82b1d81 2019-07-27 stsp return got_repo_cache_object(repo, id, *obj);
410 90bdb554 2019-04-11 stsp err = got_object_get_path(&path, id, repo);
412 4558fcd4 2018-01-14 stsp return err;
414 a5b57ccf 2019-04-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
415 2178c42e 2018-04-22 stsp if (fd == -1) {
416 e82b1d81 2019-07-27 stsp if (errno == ENOENT)
417 e82b1d81 2019-07-27 stsp err = got_error_no_obj(id);
419 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", path);
422 9f2369b0 2018-12-24 stsp err = read_object_header_privsep(obj, repo, fd);
425 ab9a70b2 2017-11-06 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
428 59790a32 2018-09-15 stsp (*obj)->refcnt++;
429 59790a32 2018-09-15 stsp err = got_repo_cache_object(repo, id, *obj);
431 6c00b545 2018-01-17 stsp free(path);
432 ab9a70b2 2017-11-06 stsp return err;
436 6dfa2fd3 2018-02-12 stsp const struct got_error *
437 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
438 6dfa2fd3 2018-02-12 stsp const char *id_str)
440 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
442 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
443 6dfa2fd3 2018-02-12 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
445 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
448 15a94983 2018-12-23 stsp const struct got_error *
449 15a94983 2018-12-23 stsp got_object_resolve_id_str(struct got_object_id **id,
450 15a94983 2018-12-23 stsp struct got_repository *repo, const char *id_str)
452 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
453 15a94983 2018-12-23 stsp struct got_object *obj;
455 15a94983 2018-12-23 stsp err = got_object_open_by_id_str(&obj, repo, id_str);
457 15a94983 2018-12-23 stsp return err;
459 15a94983 2018-12-23 stsp *id = got_object_id_dup(got_object_get_id(obj));
460 15a94983 2018-12-23 stsp got_object_close(obj);
461 15a94983 2018-12-23 stsp if (*id == NULL)
462 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
464 15a94983 2018-12-23 stsp return NULL;
467 434025f3 2018-09-16 stsp static const struct got_error *
468 a158c901 2018-12-23 stsp request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
469 a158c901 2018-12-23 stsp int pack_idx, struct got_object_id *id)
471 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
473 a158c901 2018-12-23 stsp err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
476 a158c901 2018-12-23 stsp return err;
478 a158c901 2018-12-23 stsp return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
481 a158c901 2018-12-23 stsp static const struct got_error *
482 a158c901 2018-12-23 stsp read_packed_commit_privsep(struct got_commit_object **commit,
483 a158c901 2018-12-23 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
484 a158c901 2018-12-23 stsp struct got_object_id *id)
486 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
488 a158c901 2018-12-23 stsp if (pack->privsep_child)
489 a158c901 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
491 711fb6e8 2018-12-23 stsp err = start_pack_privsep_child(pack, packidx);
493 a158c901 2018-12-23 stsp return err;
495 711fb6e8 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
498 9f2369b0 2018-12-24 stsp static const struct got_error *
499 9f2369b0 2018-12-24 stsp request_commit(struct got_commit_object **commit, struct got_repository *repo,
502 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
503 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
505 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
507 9f2369b0 2018-12-24 stsp err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
509 9f2369b0 2018-12-24 stsp return err;
511 9f2369b0 2018-12-24 stsp return got_privsep_recv_commit(commit, ibuf);
514 9f2369b0 2018-12-24 stsp static const struct got_error *
515 9f2369b0 2018-12-24 stsp read_commit_privsep(struct got_commit_object **commit, int obj_fd,
516 9f2369b0 2018-12-24 stsp struct got_repository *repo)
518 9f2369b0 2018-12-24 stsp int imsg_fds[2];
520 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
522 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
523 9f2369b0 2018-12-24 stsp return request_commit(commit, repo, obj_fd);
525 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
526 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
527 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
529 9f2369b0 2018-12-24 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
530 638f9024 2019-05-13 stsp return got_error_from_errno("socketpair");
532 9f2369b0 2018-12-24 stsp pid = fork();
533 9f2369b0 2018-12-24 stsp if (pid == -1)
534 638f9024 2019-05-13 stsp return got_error_from_errno("fork");
535 9f2369b0 2018-12-24 stsp else if (pid == 0) {
536 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
537 9f2369b0 2018-12-24 stsp repo->path);
538 9f2369b0 2018-12-24 stsp /* not reached */
541 3a6ce05a 2019-02-11 stsp if (close(imsg_fds[1]) != 0)
542 638f9024 2019-05-13 stsp return got_error_from_errno("close");
543 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
544 9f2369b0 2018-12-24 stsp imsg_fds[0];
545 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
546 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
547 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
549 9f2369b0 2018-12-24 stsp return request_commit(commit, repo, obj_fd);
553 a158c901 2018-12-23 stsp static const struct got_error *
554 434025f3 2018-09-16 stsp open_commit(struct got_commit_object **commit,
555 1785f84a 2018-12-23 stsp struct got_repository *repo, struct got_object_id *id, int check_cache)
557 434025f3 2018-09-16 stsp const struct got_error *err = NULL;
558 1785f84a 2018-12-23 stsp struct got_packidx *packidx = NULL;
560 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
562 434025f3 2018-09-16 stsp if (check_cache) {
563 1785f84a 2018-12-23 stsp *commit = got_repo_get_cached_commit(repo, id);
564 434025f3 2018-09-16 stsp if (*commit != NULL) {
565 434025f3 2018-09-16 stsp (*commit)->refcnt++;
566 434025f3 2018-09-16 stsp return NULL;
569 434025f3 2018-09-16 stsp *commit = NULL;
571 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
572 e82b1d81 2019-07-27 stsp if (err == NULL) {
573 1785f84a 2018-12-23 stsp struct got_pack *pack = NULL;
575 1785f84a 2018-12-23 stsp err = get_packfile_path(&path_packfile, packidx);
577 1785f84a 2018-12-23 stsp return err;
579 1785f84a 2018-12-23 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
580 434025f3 2018-09-16 stsp if (pack == NULL) {
581 1785f84a 2018-12-23 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
586 a158c901 2018-12-23 stsp err = read_packed_commit_privsep(commit, pack,
587 1785f84a 2018-12-23 stsp packidx, idx, id);
588 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
591 e82b1d81 2019-07-27 stsp err = open_loose_object(&fd, id, repo);
593 e82b1d81 2019-07-27 stsp return err;
594 9f2369b0 2018-12-24 stsp err = read_commit_privsep(commit, fd, repo);
597 434025f3 2018-09-16 stsp if (err == NULL) {
598 434025f3 2018-09-16 stsp (*commit)->refcnt++;
599 1785f84a 2018-12-23 stsp err = got_repo_cache_commit(repo, id, *commit);
602 8d2c5ea3 2019-08-13 stsp free(path_packfile);
603 e32baab7 2018-11-05 stsp return err;
606 e32baab7 2018-11-05 stsp const struct got_error *
607 e32baab7 2018-11-05 stsp got_object_open_as_commit(struct got_commit_object **commit,
608 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object_id *id)
610 e32baab7 2018-11-05 stsp *commit = got_repo_get_cached_commit(repo, id);
611 e32baab7 2018-11-05 stsp if (*commit != NULL) {
612 e32baab7 2018-11-05 stsp (*commit)->refcnt++;
613 e32baab7 2018-11-05 stsp return NULL;
616 1785f84a 2018-12-23 stsp return open_commit(commit, repo, id, 0);
619 e32baab7 2018-11-05 stsp const struct got_error *
620 e32baab7 2018-11-05 stsp got_object_commit_open(struct got_commit_object **commit,
621 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object *obj)
623 1785f84a 2018-12-23 stsp return open_commit(commit, repo, got_object_get_id(obj), 1);
626 434025f3 2018-09-16 stsp const struct got_error *
627 dbc6a6b6 2018-07-12 stsp got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
629 dbc6a6b6 2018-07-12 stsp const struct got_error *err = NULL;
631 dbc6a6b6 2018-07-12 stsp *qid = calloc(1, sizeof(**qid));
632 dbc6a6b6 2018-07-12 stsp if (*qid == NULL)
633 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
635 dbc6a6b6 2018-07-12 stsp (*qid)->id = got_object_id_dup(id);
636 dbc6a6b6 2018-07-12 stsp if ((*qid)->id == NULL) {
637 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
638 fa2f6902 2018-07-23 stsp got_object_qid_free(*qid);
639 dbc6a6b6 2018-07-12 stsp *qid = NULL;
640 dbc6a6b6 2018-07-12 stsp return err;
643 dbc6a6b6 2018-07-12 stsp return NULL;
646 a158c901 2018-12-23 stsp static const struct got_error *
647 13c729f7 2018-12-24 stsp request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
648 13c729f7 2018-12-24 stsp int pack_idx, struct got_object_id *id)
650 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
652 13c729f7 2018-12-24 stsp err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
655 a158c901 2018-12-23 stsp return err;
657 a158c901 2018-12-23 stsp return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
660 71eb0e7f 2018-09-16 stsp static const struct got_error *
661 13c729f7 2018-12-24 stsp read_packed_tree_privsep(struct got_tree_object **tree,
662 13c729f7 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
663 13c729f7 2018-12-24 stsp struct got_object_id *id)
665 13c729f7 2018-12-24 stsp const struct got_error *err = NULL;
667 13c729f7 2018-12-24 stsp if (pack->privsep_child)
668 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
670 13c729f7 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
672 13c729f7 2018-12-24 stsp return err;
674 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
677 13c729f7 2018-12-24 stsp static const struct got_error *
678 9f2369b0 2018-12-24 stsp request_tree(struct got_tree_object **tree, struct got_repository *repo,
681 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
682 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
684 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
686 9f2369b0 2018-12-24 stsp err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
688 9f2369b0 2018-12-24 stsp return err;
690 9f2369b0 2018-12-24 stsp return got_privsep_recv_tree(tree, ibuf);
693 9f2369b0 2018-12-24 stsp const struct got_error *
694 9f2369b0 2018-12-24 stsp read_tree_privsep(struct got_tree_object **tree, int obj_fd,
695 9f2369b0 2018-12-24 stsp struct got_repository *repo)
697 9f2369b0 2018-12-24 stsp int imsg_fds[2];
699 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
701 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
702 9f2369b0 2018-12-24 stsp return request_tree(tree, repo, obj_fd);
704 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
705 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
706 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
708 9f2369b0 2018-12-24 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
709 638f9024 2019-05-13 stsp return got_error_from_errno("socketpair");
711 9f2369b0 2018-12-24 stsp pid = fork();
712 9f2369b0 2018-12-24 stsp if (pid == -1)
713 638f9024 2019-05-13 stsp return got_error_from_errno("fork");
714 9f2369b0 2018-12-24 stsp else if (pid == 0) {
715 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
716 9f2369b0 2018-12-24 stsp repo->path);
717 9f2369b0 2018-12-24 stsp /* not reached */
720 3a6ce05a 2019-02-11 stsp if (close(imsg_fds[1]) != 0)
721 638f9024 2019-05-13 stsp return got_error_from_errno("close");
722 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
723 9f2369b0 2018-12-24 stsp imsg_fds[0];
724 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
725 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
726 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
729 9f2369b0 2018-12-24 stsp return request_tree(tree, repo, obj_fd);
732 9f2369b0 2018-12-24 stsp static const struct got_error *
733 13c729f7 2018-12-24 stsp open_tree(struct got_tree_object **tree, struct got_repository *repo,
734 13c729f7 2018-12-24 stsp struct got_object_id *id, int check_cache)
736 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
737 13c729f7 2018-12-24 stsp struct got_packidx *packidx = NULL;
739 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
741 71eb0e7f 2018-09-16 stsp if (check_cache) {
742 13c729f7 2018-12-24 stsp *tree = got_repo_get_cached_tree(repo, id);
743 71eb0e7f 2018-09-16 stsp if (*tree != NULL) {
744 71eb0e7f 2018-09-16 stsp (*tree)->refcnt++;
745 71eb0e7f 2018-09-16 stsp return NULL;
748 71eb0e7f 2018-09-16 stsp *tree = NULL;
750 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
751 e82b1d81 2019-07-27 stsp if (err == NULL) {
752 13c729f7 2018-12-24 stsp struct got_pack *pack = NULL;
754 13c729f7 2018-12-24 stsp err = get_packfile_path(&path_packfile, packidx);
756 13c729f7 2018-12-24 stsp return err;
758 13c729f7 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
759 e7885405 2018-09-10 stsp if (pack == NULL) {
760 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
765 13c729f7 2018-12-24 stsp err = read_packed_tree_privsep(tree, pack,
766 13c729f7 2018-12-24 stsp packidx, idx, id);
767 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
770 e82b1d81 2019-07-27 stsp err = open_loose_object(&fd, id, repo);
772 e82b1d81 2019-07-27 stsp return err;
773 9f2369b0 2018-12-24 stsp err = read_tree_privsep(tree, fd, repo);
776 f6be5c30 2018-06-22 stsp if (err == NULL) {
777 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
778 13c729f7 2018-12-24 stsp err = got_repo_cache_tree(repo, id, *tree);
781 8d2c5ea3 2019-08-13 stsp free(path_packfile);
782 776d4d29 2018-06-17 stsp return err;
785 776d4d29 2018-06-17 stsp const struct got_error *
786 776d4d29 2018-06-17 stsp got_object_open_as_tree(struct got_tree_object **tree,
787 776d4d29 2018-06-17 stsp struct got_repository *repo, struct got_object_id *id)
789 e8eb494a 2018-09-16 stsp *tree = got_repo_get_cached_tree(repo, id);
790 e8eb494a 2018-09-16 stsp if (*tree != NULL) {
791 e8eb494a 2018-09-16 stsp (*tree)->refcnt++;
792 e8eb494a 2018-09-16 stsp return NULL;
795 13c729f7 2018-12-24 stsp return open_tree(tree, repo, id, 0);
798 71eb0e7f 2018-09-16 stsp const struct got_error *
799 71eb0e7f 2018-09-16 stsp got_object_tree_open(struct got_tree_object **tree,
800 71eb0e7f 2018-09-16 stsp struct got_repository *repo, struct got_object *obj)
802 13c729f7 2018-12-24 stsp return open_tree(tree, repo, got_object_get_id(obj), 1);
805 883f0469 2018-06-23 stsp const struct got_tree_entries *
806 883f0469 2018-06-23 stsp got_object_tree_get_entries(struct got_tree_object *tree)
808 883f0469 2018-06-23 stsp return &tree->entries;
811 3840f4c9 2018-09-12 stsp static const struct got_error *
812 ac544f8c 2019-01-13 stsp request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
813 ebc55e2d 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
814 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
816 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
817 3840f4c9 2018-09-12 stsp int outfd_child;
818 3840f4c9 2018-09-12 stsp int basefd, accumfd; /* temporary files for delta application */
820 3840f4c9 2018-09-12 stsp basefd = got_opentempfd();
821 3840f4c9 2018-09-12 stsp if (basefd == -1)
822 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
823 3840f4c9 2018-09-12 stsp accumfd = got_opentempfd();
824 3840f4c9 2018-09-12 stsp if (accumfd == -1)
825 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
827 3840f4c9 2018-09-12 stsp outfd_child = dup(outfd);
828 3840f4c9 2018-09-12 stsp if (outfd_child == -1)
829 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
831 ebc55e2d 2018-12-24 stsp err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
833 3840f4c9 2018-09-12 stsp return err;
835 3840f4c9 2018-09-12 stsp err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
836 3840f4c9 2018-09-12 stsp outfd_child);
838 41496140 2019-02-21 stsp close(basefd);
839 41496140 2019-02-21 stsp close(accumfd);
840 3840f4c9 2018-09-12 stsp return err;
843 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
846 3840f4c9 2018-09-12 stsp close(accumfd);
847 3840f4c9 2018-09-12 stsp return err;
850 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
853 3840f4c9 2018-09-12 stsp return err;
855 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen,
856 ebc55e2d 2018-12-24 stsp pack->privsep_child->ibuf);
858 3840f4c9 2018-09-12 stsp return err;
860 3840f4c9 2018-09-12 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
861 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
863 3840f4c9 2018-09-12 stsp return err;
866 ebc55e2d 2018-12-24 stsp static const struct got_error *
867 ac544f8c 2019-01-13 stsp read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
868 ac544f8c 2019-01-13 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
869 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
871 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
873 ebc55e2d 2018-12-24 stsp if (pack->privsep_child == NULL) {
874 ebc55e2d 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
876 ebc55e2d 2018-12-24 stsp return err;
879 ac544f8c 2019-01-13 stsp return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
883 9f2369b0 2018-12-24 stsp static const struct got_error *
884 ac544f8c 2019-01-13 stsp request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
885 ac544f8c 2019-01-13 stsp int infd, struct imsgbuf *ibuf)
887 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
888 9f2369b0 2018-12-24 stsp int outfd_child;
890 9f2369b0 2018-12-24 stsp outfd_child = dup(outfd);
891 9f2369b0 2018-12-24 stsp if (outfd_child == -1)
892 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
894 9f2369b0 2018-12-24 stsp err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
896 9f2369b0 2018-12-24 stsp return err;
898 9f2369b0 2018-12-24 stsp err = got_privsep_send_blob_outfd(ibuf, outfd_child);
900 9f2369b0 2018-12-24 stsp return err;
902 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
904 9f2369b0 2018-12-24 stsp return err;
906 9f2369b0 2018-12-24 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
907 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
909 9f2369b0 2018-12-24 stsp return err;
912 9f2369b0 2018-12-24 stsp static const struct got_error *
913 ac544f8c 2019-01-13 stsp read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
914 9f2369b0 2018-12-24 stsp int outfd, int infd, struct got_repository *repo)
916 9f2369b0 2018-12-24 stsp int imsg_fds[2];
918 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
920 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
921 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
922 ac544f8c 2019-01-13 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
925 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
926 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
927 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
929 9f2369b0 2018-12-24 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
930 638f9024 2019-05-13 stsp return got_error_from_errno("socketpair");
932 9f2369b0 2018-12-24 stsp pid = fork();
933 9f2369b0 2018-12-24 stsp if (pid == -1)
934 638f9024 2019-05-13 stsp return got_error_from_errno("fork");
935 9f2369b0 2018-12-24 stsp else if (pid == 0) {
936 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
937 9f2369b0 2018-12-24 stsp repo->path);
938 9f2369b0 2018-12-24 stsp /* not reached */
941 3a6ce05a 2019-02-11 stsp if (close(imsg_fds[1]) != 0)
942 638f9024 2019-05-13 stsp return got_error_from_errno("close");
943 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
944 9f2369b0 2018-12-24 stsp imsg_fds[0];
945 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
946 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
947 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
949 ac544f8c 2019-01-13 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
952 ebc55e2d 2018-12-24 stsp static const struct got_error *
953 ebc55e2d 2018-12-24 stsp open_blob(struct got_blob_object **blob, struct got_repository *repo,
954 ebc55e2d 2018-12-24 stsp struct got_object_id *id, size_t blocksize)
956 ebc55e2d 2018-12-24 stsp const struct got_error *err = NULL;
957 ebc55e2d 2018-12-24 stsp struct got_packidx *packidx = NULL;
959 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
960 ac544f8c 2019-01-13 stsp uint8_t *outbuf;
962 ebc55e2d 2018-12-24 stsp size_t size, hdrlen;
963 ebc55e2d 2018-12-24 stsp struct stat sb;
965 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
966 4558fcd4 2018-01-14 stsp if (*blob == NULL)
967 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
969 55da3778 2018-09-10 stsp outfd = got_opentempfd();
970 55da3778 2018-09-10 stsp if (outfd == -1)
971 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
973 062ebb78 2018-07-12 stsp (*blob)->read_buf = malloc(blocksize);
974 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
975 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
979 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
980 e82b1d81 2019-07-27 stsp if (err == NULL) {
981 ebc55e2d 2018-12-24 stsp struct got_pack *pack = NULL;
983 ebc55e2d 2018-12-24 stsp err = get_packfile_path(&path_packfile, packidx);
987 ebc55e2d 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
988 55da3778 2018-09-10 stsp if (pack == NULL) {
989 ebc55e2d 2018-12-24 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
994 ac544f8c 2019-01-13 stsp err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
995 ac544f8c 2019-01-13 stsp pack, packidx, idx, id);
996 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
999 e82b1d81 2019-07-27 stsp err = open_loose_object(&infd, id, repo);
1001 e82b1d81 2019-07-27 stsp goto done;
1002 ac544f8c 2019-01-13 stsp err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1006 55da3778 2018-09-10 stsp goto done;
1008 de060dff 2018-12-24 stsp if (hdrlen > size) {
1009 ebc55e2d 2018-12-24 stsp err = got_error(GOT_ERR_BAD_OBJ_HDR);
1010 ebc55e2d 2018-12-24 stsp goto done;
1013 ac544f8c 2019-01-13 stsp if (outbuf) {
1014 3a6ce05a 2019-02-11 stsp if (close(outfd) != 0 && err == NULL)
1015 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1016 ac544f8c 2019-01-13 stsp outfd = -1;
1017 ac544f8c 2019-01-13 stsp (*blob)->f = fmemopen(outbuf, size, "rb");
1018 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1019 638f9024 2019-05-13 stsp err = got_error_from_errno("fmemopen");
1020 ac544f8c 2019-01-13 stsp free(outbuf);
1021 ac544f8c 2019-01-13 stsp goto done;
1023 ac544f8c 2019-01-13 stsp (*blob)->data = outbuf;
1025 ac544f8c 2019-01-13 stsp if (fstat(outfd, &sb) == -1) {
1026 638f9024 2019-05-13 stsp err = got_error_from_errno("fstat");
1027 ac544f8c 2019-01-13 stsp goto done;
1030 ac544f8c 2019-01-13 stsp if (sb.st_size != size) {
1031 ac544f8c 2019-01-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1032 ac544f8c 2019-01-13 stsp goto done;
1035 ac544f8c 2019-01-13 stsp (*blob)->f = fdopen(outfd, "rb");
1036 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1037 638f9024 2019-05-13 stsp err = got_error_from_errno("fdopen");
1038 ac544f8c 2019-01-13 stsp close(outfd);
1039 ac544f8c 2019-01-13 stsp outfd = -1;
1040 ac544f8c 2019-01-13 stsp goto done;
1044 ebc55e2d 2018-12-24 stsp (*blob)->hdrlen = hdrlen;
1045 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
1046 ebc55e2d 2018-12-24 stsp memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1049 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1050 55da3778 2018-09-10 stsp if (err) {
1051 55da3778 2018-09-10 stsp if (*blob) {
1052 7baf5860 2019-03-19 stsp got_object_blob_close(*blob);
1053 55da3778 2018-09-10 stsp *blob = NULL;
1054 55da3778 2018-09-10 stsp } else if (outfd != -1)
1055 55da3778 2018-09-10 stsp close(outfd);
1057 a19581a2 2018-06-21 stsp return err;
1060 a19581a2 2018-06-21 stsp const struct got_error *
1061 a19581a2 2018-06-21 stsp got_object_open_as_blob(struct got_blob_object **blob,
1062 a19581a2 2018-06-21 stsp struct got_repository *repo, struct got_object_id *id,
1063 a19581a2 2018-06-21 stsp size_t blocksize)
1065 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, id, blocksize);
1068 ebc55e2d 2018-12-24 stsp const struct got_error *
1069 ebc55e2d 2018-12-24 stsp got_object_blob_open(struct got_blob_object **blob,
1070 ebc55e2d 2018-12-24 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
1072 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1075 fb43ecf1 2019-02-11 stsp const struct got_error *
1076 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
1078 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL;
1079 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
1080 7baf5860 2019-03-19 stsp if (blob->f && fclose(blob->f) != 0)
1081 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1082 ac544f8c 2019-01-13 stsp free(blob->data);
1083 68482ea3 2017-11-27 stsp free(blob);
1084 fb43ecf1 2019-02-11 stsp return err;
1088 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1090 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1094 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
1096 f934cf2c 2018-02-12 stsp return blob->hdrlen;
1099 f934cf2c 2018-02-12 stsp const uint8_t *
1100 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
1102 f934cf2c 2018-02-12 stsp return blob->read_buf;
1105 68482ea3 2017-11-27 stsp const struct got_error *
1106 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1110 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1111 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
1112 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
1113 eb651edf 2018-02-11 stsp *outlenp = n;
1114 35e9ba5d 2018-06-21 stsp return NULL;
1117 35e9ba5d 2018-06-21 stsp const struct got_error *
1118 f595d9bd 2019-08-14 stsp got_object_blob_dump_to_file(size_t *filesize, int *nlines,
1119 6c4c42e0 2019-06-24 stsp off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1121 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
1122 b6752625 2018-12-24 stsp size_t n, len, hdrlen;
1123 84451b3e 2018-07-10 stsp const uint8_t *buf;
1125 6c4c42e0 2019-06-24 stsp size_t noffsets = 0;
1126 f595d9bd 2019-08-14 stsp off_t off = 0, total_len = 0;
1128 6c4c42e0 2019-06-24 stsp if (line_offsets)
1129 6c4c42e0 2019-06-24 stsp *line_offsets = NULL;
1130 f595d9bd 2019-08-14 stsp if (filesize)
1131 f595d9bd 2019-08-14 stsp *filesize = 0;
1132 84451b3e 2018-07-10 stsp if (nlines)
1133 84451b3e 2018-07-10 stsp *nlines = 0;
1135 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1137 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
1139 35e9ba5d 2018-06-21 stsp return err;
1140 35e9ba5d 2018-06-21 stsp if (len == 0)
1142 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
1143 b02560ec 2019-08-19 stsp i = hdrlen;
1144 78695fb7 2019-08-12 stsp if (line_offsets && nlines) {
1145 78695fb7 2019-08-12 stsp if (*line_offsets == NULL) {
1146 78695fb7 2019-08-12 stsp /* Have some data but perhaps no '\n'. */
1147 78695fb7 2019-08-12 stsp noffsets = 1;
1148 78695fb7 2019-08-12 stsp *nlines = 1;
1149 f595d9bd 2019-08-14 stsp *line_offsets = calloc(1, sizeof(**line_offsets));
1150 78695fb7 2019-08-12 stsp if (*line_offsets == NULL)
1151 78695fb7 2019-08-12 stsp return got_error_from_errno("malloc");
1153 b02560ec 2019-08-19 stsp /* Skip forward over end of first line. */
1154 b02560ec 2019-08-19 stsp while (i < len) {
1155 b02560ec 2019-08-19 stsp if (buf[i] == '\n')
1160 b02560ec 2019-08-19 stsp /* Scan '\n' offsets in remaining chunk of data. */
1161 b02560ec 2019-08-19 stsp while (i < len) {
1162 b02560ec 2019-08-19 stsp if (buf[i] != '\n') {
1166 f595d9bd 2019-08-14 stsp (*nlines)++;
1167 78695fb7 2019-08-12 stsp if (noffsets < *nlines) {
1168 78695fb7 2019-08-12 stsp off_t *o = recallocarray(*line_offsets,
1169 78695fb7 2019-08-12 stsp noffsets, *nlines,
1170 78695fb7 2019-08-12 stsp sizeof(**line_offsets));
1171 78695fb7 2019-08-12 stsp if (o == NULL) {
1172 78695fb7 2019-08-12 stsp free(*line_offsets);
1173 78695fb7 2019-08-12 stsp *line_offsets = NULL;
1174 78695fb7 2019-08-12 stsp return got_error_from_errno(
1175 78695fb7 2019-08-12 stsp "recallocarray");
1177 78695fb7 2019-08-12 stsp *line_offsets = o;
1178 78695fb7 2019-08-12 stsp noffsets = *nlines;
1180 f595d9bd 2019-08-14 stsp off = total_len + i - hdrlen + 1;
1181 f595d9bd 2019-08-14 stsp (*line_offsets)[*nlines - 1] = off;
1185 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
1186 454a6b59 2018-12-24 stsp n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1187 b6752625 2018-12-24 stsp if (n != len - hdrlen)
1188 b6752625 2018-12-24 stsp return got_ferror(outfile, GOT_ERR_IO);
1189 f595d9bd 2019-08-14 stsp total_len += len - hdrlen;
1190 35e9ba5d 2018-06-21 stsp hdrlen = 0;
1191 35e9ba5d 2018-06-21 stsp } while (len != 0);
1193 cbe7f848 2019-02-11 stsp if (fflush(outfile) != 0)
1194 638f9024 2019-05-13 stsp return got_error_from_errno("fflush");
1195 35e9ba5d 2018-06-21 stsp rewind(outfile);
1197 f595d9bd 2019-08-14 stsp if (filesize)
1198 f595d9bd 2019-08-14 stsp *filesize = total_len;
1200 776d4d29 2018-06-17 stsp return NULL;
1203 f4a881ce 2018-11-17 stsp static const struct got_error *
1204 268f7291 2018-12-24 stsp request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1205 268f7291 2018-12-24 stsp int pack_idx, struct got_object_id *id)
1207 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
1209 268f7291 2018-12-24 stsp err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1210 268f7291 2018-12-24 stsp pack_idx);
1212 a158c901 2018-12-23 stsp return err;
1214 a158c901 2018-12-23 stsp return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1217 268f7291 2018-12-24 stsp static const struct got_error *
1218 268f7291 2018-12-24 stsp read_packed_tag_privsep(struct got_tag_object **tag,
1219 268f7291 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1220 268f7291 2018-12-24 stsp struct got_object_id *id)
1222 268f7291 2018-12-24 stsp const struct got_error *err = NULL;
1224 268f7291 2018-12-24 stsp if (pack->privsep_child)
1225 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1227 268f7291 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
1229 268f7291 2018-12-24 stsp return err;
1231 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1234 9f2369b0 2018-12-24 stsp static const struct got_error *
1235 9f2369b0 2018-12-24 stsp request_tag(struct got_tag_object **tag, struct got_repository *repo,
1238 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1239 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1241 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1243 9f2369b0 2018-12-24 stsp err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1245 9f2369b0 2018-12-24 stsp return err;
1247 9f2369b0 2018-12-24 stsp return got_privsep_recv_tag(tag, ibuf);
1250 9f2369b0 2018-12-24 stsp static const struct got_error *
1251 9f2369b0 2018-12-24 stsp read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1252 9f2369b0 2018-12-24 stsp struct got_repository *repo)
1254 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1255 9f2369b0 2018-12-24 stsp pid_t pid;
1256 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1258 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1259 9f2369b0 2018-12-24 stsp return request_tag(tag, repo, obj_fd);
1261 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1262 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1263 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1265 9f2369b0 2018-12-24 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1266 638f9024 2019-05-13 stsp return got_error_from_errno("socketpair");
1268 9f2369b0 2018-12-24 stsp pid = fork();
1269 9f2369b0 2018-12-24 stsp if (pid == -1)
1270 638f9024 2019-05-13 stsp return got_error_from_errno("fork");
1271 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1272 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1273 9f2369b0 2018-12-24 stsp repo->path);
1274 9f2369b0 2018-12-24 stsp /* not reached */
1277 3a6ce05a 2019-02-11 stsp if (close(imsg_fds[1]) != 0)
1278 638f9024 2019-05-13 stsp return got_error_from_errno("close");
1279 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1280 9f2369b0 2018-12-24 stsp imsg_fds[0];
1281 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1282 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1283 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1285 9f2369b0 2018-12-24 stsp return request_tag(tag, repo, obj_fd);
1288 a158c901 2018-12-23 stsp static const struct got_error *
1289 268f7291 2018-12-24 stsp open_tag(struct got_tag_object **tag, struct got_repository *repo,
1290 268f7291 2018-12-24 stsp struct got_object_id *id, int check_cache)
1292 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1293 268f7291 2018-12-24 stsp struct got_packidx *packidx = NULL;
1295 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1296 5d844a1e 2019-08-13 stsp struct got_object *obj = NULL;
1297 5d844a1e 2019-08-13 stsp int obj_type = GOT_OBJ_TYPE_ANY;
1299 f4a881ce 2018-11-17 stsp if (check_cache) {
1300 268f7291 2018-12-24 stsp *tag = got_repo_get_cached_tag(repo, id);
1301 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1302 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1303 f4a881ce 2018-11-17 stsp return NULL;
1306 f4a881ce 2018-11-17 stsp *tag = NULL;
1308 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1309 e82b1d81 2019-07-27 stsp if (err == NULL) {
1310 268f7291 2018-12-24 stsp struct got_pack *pack = NULL;
1312 268f7291 2018-12-24 stsp err = get_packfile_path(&path_packfile, packidx);
1314 268f7291 2018-12-24 stsp return err;
1316 268f7291 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1317 f4a881ce 2018-11-17 stsp if (pack == NULL) {
1318 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1321 8d2c5ea3 2019-08-13 stsp goto done;
1324 5d844a1e 2019-08-13 stsp /* Beware of "leightweight" tags: Check object type first. */
1325 5d844a1e 2019-08-13 stsp err = read_packed_object_privsep(&obj, repo, pack, packidx,
1328 5d844a1e 2019-08-13 stsp goto done;
1329 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1330 5d844a1e 2019-08-13 stsp got_object_close(obj);
1331 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG) {
1332 5d844a1e 2019-08-13 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1333 5d844a1e 2019-08-13 stsp goto done;
1335 5d844a1e 2019-08-13 stsp err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1336 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1339 e82b1d81 2019-07-27 stsp err = open_loose_object(&fd, id, repo);
1341 e82b1d81 2019-07-27 stsp return err;
1342 5d844a1e 2019-08-13 stsp err = read_object_header_privsep(&obj, repo, fd);
1344 5d844a1e 2019-08-13 stsp return err;
1345 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1346 5d844a1e 2019-08-13 stsp got_object_close(obj);
1347 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
1348 5d844a1e 2019-08-13 stsp return got_error(GOT_ERR_OBJ_TYPE);
1350 5d844a1e 2019-08-13 stsp err = open_loose_object(&fd, id, repo);
1352 5d844a1e 2019-08-13 stsp return err;
1353 9f2369b0 2018-12-24 stsp err = read_tag_privsep(tag, fd, repo);
1356 f4a881ce 2018-11-17 stsp if (err == NULL) {
1357 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1358 268f7291 2018-12-24 stsp err = got_repo_cache_tag(repo, id, *tag);
1361 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1362 f4a881ce 2018-11-17 stsp return err;
1365 f4a881ce 2018-11-17 stsp const struct got_error *
1366 f4a881ce 2018-11-17 stsp got_object_open_as_tag(struct got_tag_object **tag,
1367 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object_id *id)
1369 f4a881ce 2018-11-17 stsp *tag = got_repo_get_cached_tag(repo, id);
1370 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1371 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1372 f4a881ce 2018-11-17 stsp return NULL;
1375 268f7291 2018-12-24 stsp return open_tag(tag, repo, id, 0);
1378 f4a881ce 2018-11-17 stsp const struct got_error *
1379 f4a881ce 2018-11-17 stsp got_object_tag_open(struct got_tag_object **tag,
1380 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object *obj)
1382 268f7291 2018-12-24 stsp return open_tag(tag, repo, got_object_get_id(obj), 1);
1385 d24820bf 2019-08-11 stsp const char *
1386 d24820bf 2019-08-11 stsp got_object_tag_get_name(struct got_tag_object *tag)
1388 d24820bf 2019-08-11 stsp return tag->tag;
1392 0bd18d37 2019-02-01 stsp got_object_tag_get_object_type(struct got_tag_object *tag)
1394 0bd18d37 2019-02-01 stsp return tag->obj_type;
1397 0bd18d37 2019-02-01 stsp struct got_object_id *
1398 0bd18d37 2019-02-01 stsp got_object_tag_get_object_id(struct got_tag_object *tag)
1400 0bd18d37 2019-02-01 stsp return &tag->id;
1404 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_time(struct got_tag_object *tag)
1406 01073a5d 2019-08-22 stsp return tag->tagger_time;
1410 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1412 01073a5d 2019-08-22 stsp return tag->tagger_gmtoff;
1415 01073a5d 2019-08-22 stsp const char *
1416 01073a5d 2019-08-22 stsp got_object_tag_get_tagger(struct got_tag_object *tag)
1418 01073a5d 2019-08-22 stsp return tag->tagger;
1421 01073a5d 2019-08-22 stsp const char *
1422 01073a5d 2019-08-22 stsp got_object_tag_get_message(struct got_tag_object *tag)
1424 01073a5d 2019-08-22 stsp return tag->tagmsg;
1427 776d4d29 2018-06-17 stsp static struct got_tree_entry *
1428 65a9bbe9 2018-09-15 stsp find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1430 776d4d29 2018-06-17 stsp struct got_tree_entry *te;
1432 63da309a 2018-11-07 stsp /* Note that tree entries are sorted in strncmp() order. */
1433 883f0469 2018-06-23 stsp SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
1434 63da309a 2018-11-07 stsp int cmp = strncmp(te->name, name, len);
1435 63da309a 2018-11-07 stsp if (cmp < 0)
1437 63da309a 2018-11-07 stsp if (cmp > 0)
1439 63da309a 2018-11-07 stsp if (te->name[len] == '\0')
1440 776d4d29 2018-06-17 stsp return te;
1442 eb651edf 2018-02-11 stsp return NULL;
1445 a129376b 2019-03-28 stsp const struct got_tree_entry *
1446 a129376b 2019-03-28 stsp got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1448 a129376b 2019-03-28 stsp return find_entry_by_name(tree, name, strlen(name));
1451 776d4d29 2018-06-17 stsp const struct got_error *
1452 27d434c2 2018-09-15 stsp got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1453 776d4d29 2018-06-17 stsp struct got_object_id *commit_id, const char *path)
1455 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
1456 776d4d29 2018-06-17 stsp struct got_commit_object *commit = NULL;
1457 776d4d29 2018-06-17 stsp struct got_tree_object *tree = NULL;
1458 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
1459 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1460 b7cd37e5 2018-11-18 stsp size_t seglen;
1462 27d434c2 2018-09-15 stsp *id = NULL;
1464 776d4d29 2018-06-17 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
1466 776d4d29 2018-06-17 stsp goto done;
1468 776d4d29 2018-06-17 stsp /* Handle opening of root of commit's tree. */
1469 5e54fb30 2019-05-31 stsp if (got_path_is_root_dir(path)) {
1470 27d434c2 2018-09-15 stsp *id = got_object_id_dup(commit->tree_id);
1471 27d434c2 2018-09-15 stsp if (*id == NULL)
1472 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
1473 ca008b32 2018-07-22 stsp goto done;
1476 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1478 776d4d29 2018-06-17 stsp goto done;
1481 5e54fb30 2019-05-31 stsp while (s[0] == '/')
1484 65a9bbe9 2018-09-15 stsp seglen = 0;
1485 b7cd37e5 2018-11-18 stsp while (*s) {
1486 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
1488 776d4d29 2018-06-17 stsp if (*s != '/') {
1495 65a9bbe9 2018-09-15 stsp te = find_entry_by_name(tree, seg, seglen);
1496 db37e2c0 2018-06-21 stsp if (te == NULL) {
1497 d1451975 2018-11-11 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1498 776d4d29 2018-06-17 stsp goto done;
1501 b7cd37e5 2018-11-18 stsp if (*s == '\0')
1504 776d4d29 2018-06-17 stsp seg = s + 1;
1505 65a9bbe9 2018-09-15 stsp seglen = 0;
1508 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
1510 db37e2c0 2018-06-21 stsp te = NULL;
1512 776d4d29 2018-06-17 stsp goto done;
1513 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
1514 776d4d29 2018-06-17 stsp tree = next_tree;
1519 27d434c2 2018-09-15 stsp *id = got_object_id_dup(te->id);
1520 27d434c2 2018-09-15 stsp if (*id == NULL)
1521 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
1523 d1451975 2018-11-11 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1525 776d4d29 2018-06-17 stsp if (commit)
1526 776d4d29 2018-06-17 stsp got_object_commit_close(commit);
1528 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
1529 776d4d29 2018-06-17 stsp return err;
1532 07862c20 2018-09-15 stsp const struct got_error *
1533 07862c20 2018-09-15 stsp got_object_tree_path_changed(int *changed,
1534 07862c20 2018-09-15 stsp struct got_tree_object *tree01, struct got_tree_object *tree02,
1535 07862c20 2018-09-15 stsp const char *path, struct got_repository *repo)
1537 07862c20 2018-09-15 stsp const struct got_error *err = NULL;
1538 07862c20 2018-09-15 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1539 07862c20 2018-09-15 stsp struct got_tree_entry *te1 = NULL, *te2 = NULL;
1540 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1541 3b7f9878 2018-11-18 stsp size_t seglen;
1543 07862c20 2018-09-15 stsp *changed = 0;
1545 07862c20 2018-09-15 stsp /* We are expecting an absolute in-repository path. */
1546 07862c20 2018-09-15 stsp if (path[0] != '/')
1547 07862c20 2018-09-15 stsp return got_error(GOT_ERR_NOT_ABSPATH);
1549 07862c20 2018-09-15 stsp /* We not do support comparing the root path. */
1550 07862c20 2018-09-15 stsp if (path[1] == '\0')
1551 07862c20 2018-09-15 stsp return got_error(GOT_ERR_BAD_PATH);
1553 07862c20 2018-09-15 stsp tree1 = tree01;
1554 07862c20 2018-09-15 stsp tree2 = tree02;
1556 07862c20 2018-09-15 stsp s++; /* skip leading '/' */
1558 65a9bbe9 2018-09-15 stsp seglen = 0;
1559 3b7f9878 2018-11-18 stsp while (*s) {
1560 07862c20 2018-09-15 stsp struct got_tree_object *next_tree1, *next_tree2;
1562 07862c20 2018-09-15 stsp if (*s != '/') {
1569 65a9bbe9 2018-09-15 stsp te1 = find_entry_by_name(tree1, seg, seglen);
1570 07862c20 2018-09-15 stsp if (te1 == NULL) {
1571 07862c20 2018-09-15 stsp err = got_error(GOT_ERR_NO_OBJ);
1572 07862c20 2018-09-15 stsp goto done;
1575 65a9bbe9 2018-09-15 stsp te2 = find_entry_by_name(tree2, seg, seglen);
1576 07862c20 2018-09-15 stsp if (te2 == NULL) {
1577 07862c20 2018-09-15 stsp *changed = 1;
1578 07862c20 2018-09-15 stsp goto done;
1581 07862c20 2018-09-15 stsp if (te1->mode != te2->mode) {
1582 07862c20 2018-09-15 stsp *changed = 1;
1583 07862c20 2018-09-15 stsp goto done;
1586 07862c20 2018-09-15 stsp if (got_object_id_cmp(te1->id, te2->id) == 0) {
1587 07862c20 2018-09-15 stsp *changed = 0;
1588 07862c20 2018-09-15 stsp goto done;
1591 3b7f9878 2018-11-18 stsp if (*s == '\0') { /* final path element */
1592 07862c20 2018-09-15 stsp *changed = 1;
1593 07862c20 2018-09-15 stsp goto done;
1596 07862c20 2018-09-15 stsp seg = s + 1;
1598 65a9bbe9 2018-09-15 stsp seglen = 0;
1600 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree1, repo,
1602 07862c20 2018-09-15 stsp te1 = NULL;
1604 07862c20 2018-09-15 stsp goto done;
1605 a31cea73 2018-09-15 stsp if (tree1 != tree01)
1606 a31cea73 2018-09-15 stsp got_object_tree_close(tree1);
1607 07862c20 2018-09-15 stsp tree1 = next_tree1;
1609 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree2, repo,
1611 07862c20 2018-09-15 stsp te2 = NULL;
1613 07862c20 2018-09-15 stsp goto done;
1614 a31cea73 2018-09-15 stsp if (tree2 != tree02)
1615 a31cea73 2018-09-15 stsp got_object_tree_close(tree2);
1616 07862c20 2018-09-15 stsp tree2 = next_tree2;
1620 a31cea73 2018-09-15 stsp if (tree1 && tree1 != tree01)
1621 07862c20 2018-09-15 stsp got_object_tree_close(tree1);
1622 a31cea73 2018-09-15 stsp if (tree2 && tree2 != tree02)
1623 07862c20 2018-09-15 stsp got_object_tree_close(tree2);
1624 77880158 2018-11-04 stsp return err;
1627 ed175427 2019-05-09 stsp const struct got_error *
1628 ed175427 2019-05-09 stsp got_object_tree_entry_dup(struct got_tree_entry **new_te,
1629 ed175427 2019-05-09 stsp struct got_tree_entry *te)
1631 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
1633 ed175427 2019-05-09 stsp *new_te = calloc(1, sizeof(**new_te));
1634 ed175427 2019-05-09 stsp if (*new_te == NULL)
1635 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1637 ed175427 2019-05-09 stsp (*new_te)->mode = te->mode;
1638 ed175427 2019-05-09 stsp (*new_te)->name = strdup(te->name);
1639 ed175427 2019-05-09 stsp if ((*new_te)->name == NULL) {
1640 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1641 8c4eabf2 2019-05-10 stsp goto done;
1644 ed175427 2019-05-09 stsp (*new_te)->id = got_object_id_dup(te->id);
1645 ed175427 2019-05-09 stsp if ((*new_te)->id == NULL) {
1646 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
1647 8c4eabf2 2019-05-10 stsp goto done;
1650 8c4eabf2 2019-05-10 stsp if (err) {
1651 8c4eabf2 2019-05-10 stsp got_object_tree_entry_close(*new_te);
1652 8c4eabf2 2019-05-10 stsp *new_te = NULL;
1654 8c4eabf2 2019-05-10 stsp return err;
1658 63c5ca5d 2019-08-24 stsp got_object_tree_entry_is_submodule(const struct got_tree_entry *te)
1660 63c5ca5d 2019-08-24 stsp return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);