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 a158c901 2018-12-23 stsp static void
185 a158c901 2018-12-23 stsp exec_privsep_child(int imsg_fds[2], const char *path, const char *repo_path)
187 3a6ce05a 2019-02-11 stsp if (close(imsg_fds[0]) != 0) {
188 48b8b0eb 2019-05-11 jcs fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
192 a158c901 2018-12-23 stsp if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
193 48b8b0eb 2019-05-11 jcs fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
196 a158c901 2018-12-23 stsp if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
197 48b8b0eb 2019-05-11 jcs fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
201 a158c901 2018-12-23 stsp if (execl(path, path, repo_path, (char *)NULL) == -1) {
202 a158c901 2018-12-23 stsp fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
203 a158c901 2018-12-23 stsp strerror(errno));
208 2090a03d 2018-09-09 stsp static const struct got_error *
209 a158c901 2018-12-23 stsp request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
210 a158c901 2018-12-23 stsp struct got_object_id *id)
212 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
213 a158c901 2018-12-23 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
215 a158c901 2018-12-23 stsp err = got_privsep_send_packed_obj_req(ibuf, idx, id);
217 a158c901 2018-12-23 stsp return err;
219 a158c901 2018-12-23 stsp err = got_privsep_recv_obj(obj, ibuf);
221 a158c901 2018-12-23 stsp return err;
223 a158c901 2018-12-23 stsp (*obj)->path_packfile = strdup(pack->path_packfile);
224 a158c901 2018-12-23 stsp if ((*obj)->path_packfile == NULL) {
225 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
226 a158c901 2018-12-23 stsp return err;
228 a158c901 2018-12-23 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
230 a158c901 2018-12-23 stsp return NULL;
233 da506691 2019-05-22 stsp static void
234 b48e2ddb 2019-05-22 stsp set_max_datasize(void)
236 b48e2ddb 2019-05-22 stsp struct rlimit rl;
238 b48e2ddb 2019-05-22 stsp if (getrlimit(RLIMIT_DATA, &rl) != 0)
241 b48e2ddb 2019-05-22 stsp rl.rlim_cur = rl.rlim_max;
242 b48e2ddb 2019-05-22 stsp setrlimit(RLIMIT_DATA, &rl);
245 a158c901 2018-12-23 stsp static const struct got_error *
246 711fb6e8 2018-12-23 stsp start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
248 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
249 a158c901 2018-12-23 stsp int imsg_fds[2];
251 a158c901 2018-12-23 stsp struct imsgbuf *ibuf;
253 a158c901 2018-12-23 stsp ibuf = calloc(1, sizeof(*ibuf));
254 a158c901 2018-12-23 stsp if (ibuf == NULL)
255 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
257 a158c901 2018-12-23 stsp pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
258 a158c901 2018-12-23 stsp if (pack->privsep_child == NULL) {
259 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
260 a158c901 2018-12-23 stsp free(ibuf);
261 a158c901 2018-12-23 stsp return err;
264 a158c901 2018-12-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
265 638f9024 2019-05-13 stsp err = got_error_from_errno("socketpair");
269 a158c901 2018-12-23 stsp pid = fork();
270 a158c901 2018-12-23 stsp if (pid == -1) {
271 638f9024 2019-05-13 stsp err = got_error_from_errno("fork");
273 a158c901 2018-12-23 stsp } else if (pid == 0) {
274 b48e2ddb 2019-05-22 stsp set_max_datasize();
275 a158c901 2018-12-23 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
276 a158c901 2018-12-23 stsp pack->path_packfile);
277 a158c901 2018-12-23 stsp /* not reached */
280 3a6ce05a 2019-02-11 stsp if (close(imsg_fds[1]) != 0)
281 638f9024 2019-05-13 stsp return got_error_from_errno("close");
282 a158c901 2018-12-23 stsp pack->privsep_child->imsg_fd = imsg_fds[0];
283 a158c901 2018-12-23 stsp pack->privsep_child->pid = pid;
284 a158c901 2018-12-23 stsp imsg_init(ibuf, imsg_fds[0]);
285 a158c901 2018-12-23 stsp pack->privsep_child->ibuf = ibuf;
287 a158c901 2018-12-23 stsp err = got_privsep_init_pack_child(ibuf, pack, packidx);
289 a158c901 2018-12-23 stsp const struct got_error *child_err;
290 a158c901 2018-12-23 stsp err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
291 a158c901 2018-12-23 stsp child_err = got_privsep_wait_for_child(
292 a158c901 2018-12-23 stsp pack->privsep_child->pid);
293 a158c901 2018-12-23 stsp if (child_err && err == NULL)
294 a158c901 2018-12-23 stsp err = child_err;
298 a158c901 2018-12-23 stsp free(ibuf);
299 a158c901 2018-12-23 stsp free(pack->privsep_child);
300 a158c901 2018-12-23 stsp pack->privsep_child = NULL;
302 a158c901 2018-12-23 stsp return err;
305 711fb6e8 2018-12-23 stsp static const struct got_error *
306 711fb6e8 2018-12-23 stsp read_packed_object_privsep(struct got_object **obj,
307 711fb6e8 2018-12-23 stsp struct got_repository *repo, struct got_pack *pack,
308 711fb6e8 2018-12-23 stsp struct got_packidx *packidx, int idx, struct got_object_id *id)
310 711fb6e8 2018-12-23 stsp const struct got_error *err = NULL;
312 711fb6e8 2018-12-23 stsp if (pack->privsep_child)
313 711fb6e8 2018-12-23 stsp return request_packed_object(obj, pack, idx, id);
315 711fb6e8 2018-12-23 stsp err = start_pack_privsep_child(pack, packidx);
317 711fb6e8 2018-12-23 stsp return err;
319 711fb6e8 2018-12-23 stsp return request_packed_object(obj, pack, idx, id);
323 a158c901 2018-12-23 stsp static const struct got_error *
324 2090a03d 2018-09-09 stsp open_packed_object(struct got_object **obj, struct got_object_id *id,
325 2090a03d 2018-09-09 stsp struct got_repository *repo)
327 2090a03d 2018-09-09 stsp const struct got_error *err = NULL;
328 2090a03d 2018-09-09 stsp struct got_pack *pack = NULL;
329 2090a03d 2018-09-09 stsp struct got_packidx *packidx = NULL;
331 2090a03d 2018-09-09 stsp char *path_packfile;
333 2090a03d 2018-09-09 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
335 2090a03d 2018-09-09 stsp return err;
337 2090a03d 2018-09-09 stsp err = get_packfile_path(&path_packfile, packidx);
339 2090a03d 2018-09-09 stsp return err;
341 2090a03d 2018-09-09 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
342 2090a03d 2018-09-09 stsp if (pack == NULL) {
343 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
348 a158c901 2018-12-23 stsp err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
352 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
354 2090a03d 2018-09-09 stsp free(path_packfile);
355 4558fcd4 2018-01-14 stsp return err;
358 9f2369b0 2018-12-24 stsp static const struct got_error *
359 9f2369b0 2018-12-24 stsp request_object(struct got_object **obj, struct got_repository *repo, int fd)
361 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
362 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
364 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
366 aea5f015 2018-12-24 stsp err = got_privsep_send_obj_req(ibuf, fd);
368 9f2369b0 2018-12-24 stsp return err;
370 9f2369b0 2018-12-24 stsp return got_privsep_recv_obj(obj, ibuf);
373 9f2369b0 2018-12-24 stsp static const struct got_error *
374 9f2369b0 2018-12-24 stsp read_object_header_privsep(struct got_object **obj, struct got_repository *repo,
375 9f2369b0 2018-12-24 stsp int obj_fd)
377 9f2369b0 2018-12-24 stsp int imsg_fds[2];
379 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
381 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
382 9f2369b0 2018-12-24 stsp return request_object(obj, repo, obj_fd);
384 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
385 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
386 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
388 9f2369b0 2018-12-24 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
389 638f9024 2019-05-13 stsp return got_error_from_errno("socketpair");
391 9f2369b0 2018-12-24 stsp pid = fork();
392 9f2369b0 2018-12-24 stsp if (pid == -1)
393 638f9024 2019-05-13 stsp return got_error_from_errno("fork");
394 9f2369b0 2018-12-24 stsp else if (pid == 0) {
395 9f2369b0 2018-12-24 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
396 9f2369b0 2018-12-24 stsp repo->path);
397 9f2369b0 2018-12-24 stsp /* not reached */
400 3a6ce05a 2019-02-11 stsp if (close(imsg_fds[1]) != 0)
401 638f9024 2019-05-13 stsp return got_error_from_errno("close");
402 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
403 9f2369b0 2018-12-24 stsp imsg_fds[0];
404 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
405 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
406 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
408 9f2369b0 2018-12-24 stsp return request_object(obj, repo, obj_fd);
412 4558fcd4 2018-01-14 stsp const struct got_error *
413 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
414 4558fcd4 2018-01-14 stsp struct got_object_id *id)
416 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
417 6c00b545 2018-01-17 stsp char *path;
420 7bb0daa1 2018-06-21 stsp *obj = got_repo_get_cached_object(repo, id);
421 7bb0daa1 2018-06-21 stsp if (*obj != NULL) {
422 7bb0daa1 2018-06-21 stsp (*obj)->refcnt++;
423 7bb0daa1 2018-06-21 stsp return NULL;
426 e82b1d81 2019-07-27 stsp err = open_packed_object(obj, id, repo);
427 e82b1d81 2019-07-27 stsp if (err && err->code != GOT_ERR_NO_OBJ)
428 e82b1d81 2019-07-27 stsp return err;
429 e82b1d81 2019-07-27 stsp if (*obj) {
430 e82b1d81 2019-07-27 stsp (*obj)->refcnt++;
431 e82b1d81 2019-07-27 stsp return got_repo_cache_object(repo, id, *obj);
434 90bdb554 2019-04-11 stsp err = got_object_get_path(&path, id, repo);
436 4558fcd4 2018-01-14 stsp return err;
438 a5b57ccf 2019-04-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
439 2178c42e 2018-04-22 stsp if (fd == -1) {
440 e82b1d81 2019-07-27 stsp if (errno == ENOENT)
441 e82b1d81 2019-07-27 stsp err = got_error_no_obj(id);
443 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", path);
446 9f2369b0 2018-12-24 stsp err = read_object_header_privsep(obj, repo, fd);
449 ab9a70b2 2017-11-06 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
452 59790a32 2018-09-15 stsp (*obj)->refcnt++;
453 59790a32 2018-09-15 stsp err = got_repo_cache_object(repo, id, *obj);
455 6c00b545 2018-01-17 stsp free(path);
456 ab9a70b2 2017-11-06 stsp return err;
460 6dfa2fd3 2018-02-12 stsp const struct got_error *
461 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
462 6dfa2fd3 2018-02-12 stsp const char *id_str)
464 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
466 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
467 6dfa2fd3 2018-02-12 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
469 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
472 15a94983 2018-12-23 stsp const struct got_error *
473 15a94983 2018-12-23 stsp got_object_resolve_id_str(struct got_object_id **id,
474 15a94983 2018-12-23 stsp struct got_repository *repo, const char *id_str)
476 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
477 15a94983 2018-12-23 stsp struct got_object *obj;
479 15a94983 2018-12-23 stsp err = got_object_open_by_id_str(&obj, repo, id_str);
481 15a94983 2018-12-23 stsp return err;
483 15a94983 2018-12-23 stsp *id = got_object_id_dup(got_object_get_id(obj));
484 15a94983 2018-12-23 stsp got_object_close(obj);
485 15a94983 2018-12-23 stsp if (*id == NULL)
486 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
488 15a94983 2018-12-23 stsp return NULL;
491 434025f3 2018-09-16 stsp static const struct got_error *
492 a158c901 2018-12-23 stsp request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
493 a158c901 2018-12-23 stsp int pack_idx, struct got_object_id *id)
495 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
497 a158c901 2018-12-23 stsp err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
500 a158c901 2018-12-23 stsp return err;
502 a158c901 2018-12-23 stsp return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
505 a158c901 2018-12-23 stsp static const struct got_error *
506 a158c901 2018-12-23 stsp read_packed_commit_privsep(struct got_commit_object **commit,
507 a158c901 2018-12-23 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
508 a158c901 2018-12-23 stsp struct got_object_id *id)
510 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
512 a158c901 2018-12-23 stsp if (pack->privsep_child)
513 a158c901 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
515 711fb6e8 2018-12-23 stsp err = start_pack_privsep_child(pack, packidx);
517 a158c901 2018-12-23 stsp return err;
519 711fb6e8 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
522 9f2369b0 2018-12-24 stsp static const struct got_error *
523 9f2369b0 2018-12-24 stsp request_commit(struct got_commit_object **commit, struct got_repository *repo,
526 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
527 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
529 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
531 9f2369b0 2018-12-24 stsp err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
533 9f2369b0 2018-12-24 stsp return err;
535 9f2369b0 2018-12-24 stsp return got_privsep_recv_commit(commit, ibuf);
538 9f2369b0 2018-12-24 stsp static const struct got_error *
539 9f2369b0 2018-12-24 stsp read_commit_privsep(struct got_commit_object **commit, int obj_fd,
540 9f2369b0 2018-12-24 stsp struct got_repository *repo)
542 9f2369b0 2018-12-24 stsp int imsg_fds[2];
544 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
546 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
547 9f2369b0 2018-12-24 stsp return request_commit(commit, repo, obj_fd);
549 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
550 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
551 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
553 9f2369b0 2018-12-24 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
554 638f9024 2019-05-13 stsp return got_error_from_errno("socketpair");
556 9f2369b0 2018-12-24 stsp pid = fork();
557 9f2369b0 2018-12-24 stsp if (pid == -1)
558 638f9024 2019-05-13 stsp return got_error_from_errno("fork");
559 9f2369b0 2018-12-24 stsp else if (pid == 0) {
560 9f2369b0 2018-12-24 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
561 9f2369b0 2018-12-24 stsp repo->path);
562 9f2369b0 2018-12-24 stsp /* not reached */
565 3a6ce05a 2019-02-11 stsp if (close(imsg_fds[1]) != 0)
566 638f9024 2019-05-13 stsp return got_error_from_errno("close");
567 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
568 9f2369b0 2018-12-24 stsp imsg_fds[0];
569 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
570 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
571 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
573 9f2369b0 2018-12-24 stsp return request_commit(commit, repo, obj_fd);
577 a158c901 2018-12-23 stsp static const struct got_error *
578 434025f3 2018-09-16 stsp open_commit(struct got_commit_object **commit,
579 1785f84a 2018-12-23 stsp struct got_repository *repo, struct got_object_id *id, int check_cache)
581 434025f3 2018-09-16 stsp const struct got_error *err = NULL;
582 1785f84a 2018-12-23 stsp struct got_packidx *packidx = NULL;
584 1785f84a 2018-12-23 stsp char *path_packfile;
586 434025f3 2018-09-16 stsp if (check_cache) {
587 1785f84a 2018-12-23 stsp *commit = got_repo_get_cached_commit(repo, id);
588 434025f3 2018-09-16 stsp if (*commit != NULL) {
589 434025f3 2018-09-16 stsp (*commit)->refcnt++;
590 434025f3 2018-09-16 stsp return NULL;
593 434025f3 2018-09-16 stsp *commit = NULL;
595 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
596 e82b1d81 2019-07-27 stsp if (err == NULL) {
597 1785f84a 2018-12-23 stsp struct got_pack *pack = NULL;
599 1785f84a 2018-12-23 stsp err = get_packfile_path(&path_packfile, packidx);
601 1785f84a 2018-12-23 stsp return err;
603 1785f84a 2018-12-23 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
604 434025f3 2018-09-16 stsp if (pack == NULL) {
605 1785f84a 2018-12-23 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
608 434025f3 2018-09-16 stsp return err;
610 a158c901 2018-12-23 stsp err = read_packed_commit_privsep(commit, pack,
611 1785f84a 2018-12-23 stsp packidx, idx, id);
612 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
615 e82b1d81 2019-07-27 stsp err = open_loose_object(&fd, id, repo);
617 e82b1d81 2019-07-27 stsp return err;
618 9f2369b0 2018-12-24 stsp err = read_commit_privsep(commit, fd, repo);
621 434025f3 2018-09-16 stsp if (err == NULL) {
622 434025f3 2018-09-16 stsp (*commit)->refcnt++;
623 1785f84a 2018-12-23 stsp err = got_repo_cache_commit(repo, id, *commit);
626 e32baab7 2018-11-05 stsp return err;
629 e32baab7 2018-11-05 stsp const struct got_error *
630 e32baab7 2018-11-05 stsp got_object_open_as_commit(struct got_commit_object **commit,
631 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object_id *id)
633 e32baab7 2018-11-05 stsp *commit = got_repo_get_cached_commit(repo, id);
634 e32baab7 2018-11-05 stsp if (*commit != NULL) {
635 e32baab7 2018-11-05 stsp (*commit)->refcnt++;
636 e32baab7 2018-11-05 stsp return NULL;
639 1785f84a 2018-12-23 stsp return open_commit(commit, repo, id, 0);
642 e32baab7 2018-11-05 stsp const struct got_error *
643 e32baab7 2018-11-05 stsp got_object_commit_open(struct got_commit_object **commit,
644 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object *obj)
646 1785f84a 2018-12-23 stsp return open_commit(commit, repo, got_object_get_id(obj), 1);
649 434025f3 2018-09-16 stsp const struct got_error *
650 dbc6a6b6 2018-07-12 stsp got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
652 dbc6a6b6 2018-07-12 stsp const struct got_error *err = NULL;
654 dbc6a6b6 2018-07-12 stsp *qid = calloc(1, sizeof(**qid));
655 dbc6a6b6 2018-07-12 stsp if (*qid == NULL)
656 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
658 dbc6a6b6 2018-07-12 stsp (*qid)->id = got_object_id_dup(id);
659 dbc6a6b6 2018-07-12 stsp if ((*qid)->id == NULL) {
660 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
661 fa2f6902 2018-07-23 stsp got_object_qid_free(*qid);
662 dbc6a6b6 2018-07-12 stsp *qid = NULL;
663 dbc6a6b6 2018-07-12 stsp return err;
666 dbc6a6b6 2018-07-12 stsp return NULL;
669 a158c901 2018-12-23 stsp static const struct got_error *
670 13c729f7 2018-12-24 stsp request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
671 13c729f7 2018-12-24 stsp int pack_idx, struct got_object_id *id)
673 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
675 13c729f7 2018-12-24 stsp err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
678 a158c901 2018-12-23 stsp return err;
680 a158c901 2018-12-23 stsp return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
683 71eb0e7f 2018-09-16 stsp static const struct got_error *
684 13c729f7 2018-12-24 stsp read_packed_tree_privsep(struct got_tree_object **tree,
685 13c729f7 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
686 13c729f7 2018-12-24 stsp struct got_object_id *id)
688 13c729f7 2018-12-24 stsp const struct got_error *err = NULL;
690 13c729f7 2018-12-24 stsp if (pack->privsep_child)
691 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
693 13c729f7 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
695 13c729f7 2018-12-24 stsp return err;
697 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
700 13c729f7 2018-12-24 stsp static const struct got_error *
701 9f2369b0 2018-12-24 stsp request_tree(struct got_tree_object **tree, struct got_repository *repo,
704 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
705 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
707 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
709 9f2369b0 2018-12-24 stsp err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
711 9f2369b0 2018-12-24 stsp return err;
713 9f2369b0 2018-12-24 stsp return got_privsep_recv_tree(tree, ibuf);
716 9f2369b0 2018-12-24 stsp const struct got_error *
717 9f2369b0 2018-12-24 stsp read_tree_privsep(struct got_tree_object **tree, int obj_fd,
718 9f2369b0 2018-12-24 stsp struct got_repository *repo)
720 9f2369b0 2018-12-24 stsp int imsg_fds[2];
722 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
724 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
725 9f2369b0 2018-12-24 stsp return request_tree(tree, repo, obj_fd);
727 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
728 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
729 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
731 9f2369b0 2018-12-24 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
732 638f9024 2019-05-13 stsp return got_error_from_errno("socketpair");
734 9f2369b0 2018-12-24 stsp pid = fork();
735 9f2369b0 2018-12-24 stsp if (pid == -1)
736 638f9024 2019-05-13 stsp return got_error_from_errno("fork");
737 9f2369b0 2018-12-24 stsp else if (pid == 0) {
738 9f2369b0 2018-12-24 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
739 9f2369b0 2018-12-24 stsp repo->path);
740 9f2369b0 2018-12-24 stsp /* not reached */
743 3a6ce05a 2019-02-11 stsp if (close(imsg_fds[1]) != 0)
744 638f9024 2019-05-13 stsp return got_error_from_errno("close");
745 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
746 9f2369b0 2018-12-24 stsp imsg_fds[0];
747 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
748 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
749 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
752 9f2369b0 2018-12-24 stsp return request_tree(tree, repo, obj_fd);
755 9f2369b0 2018-12-24 stsp static const struct got_error *
756 13c729f7 2018-12-24 stsp open_tree(struct got_tree_object **tree, struct got_repository *repo,
757 13c729f7 2018-12-24 stsp struct got_object_id *id, int check_cache)
759 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
760 13c729f7 2018-12-24 stsp struct got_packidx *packidx = NULL;
762 13c729f7 2018-12-24 stsp char *path_packfile;
764 71eb0e7f 2018-09-16 stsp if (check_cache) {
765 13c729f7 2018-12-24 stsp *tree = got_repo_get_cached_tree(repo, id);
766 71eb0e7f 2018-09-16 stsp if (*tree != NULL) {
767 71eb0e7f 2018-09-16 stsp (*tree)->refcnt++;
768 71eb0e7f 2018-09-16 stsp return NULL;
771 71eb0e7f 2018-09-16 stsp *tree = NULL;
773 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
774 e82b1d81 2019-07-27 stsp if (err == NULL) {
775 13c729f7 2018-12-24 stsp struct got_pack *pack = NULL;
777 13c729f7 2018-12-24 stsp err = get_packfile_path(&path_packfile, packidx);
779 13c729f7 2018-12-24 stsp return err;
781 13c729f7 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
782 e7885405 2018-09-10 stsp if (pack == NULL) {
783 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
786 e7885405 2018-09-10 stsp return err;
788 13c729f7 2018-12-24 stsp err = read_packed_tree_privsep(tree, pack,
789 13c729f7 2018-12-24 stsp packidx, idx, id);
790 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
793 e82b1d81 2019-07-27 stsp err = open_loose_object(&fd, id, repo);
795 e82b1d81 2019-07-27 stsp return err;
796 9f2369b0 2018-12-24 stsp err = read_tree_privsep(tree, fd, repo);
799 f6be5c30 2018-06-22 stsp if (err == NULL) {
800 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
801 13c729f7 2018-12-24 stsp err = got_repo_cache_tree(repo, id, *tree);
804 776d4d29 2018-06-17 stsp return err;
807 776d4d29 2018-06-17 stsp const struct got_error *
808 776d4d29 2018-06-17 stsp got_object_open_as_tree(struct got_tree_object **tree,
809 776d4d29 2018-06-17 stsp struct got_repository *repo, struct got_object_id *id)
811 e8eb494a 2018-09-16 stsp *tree = got_repo_get_cached_tree(repo, id);
812 e8eb494a 2018-09-16 stsp if (*tree != NULL) {
813 e8eb494a 2018-09-16 stsp (*tree)->refcnt++;
814 e8eb494a 2018-09-16 stsp return NULL;
817 13c729f7 2018-12-24 stsp return open_tree(tree, repo, id, 0);
820 71eb0e7f 2018-09-16 stsp const struct got_error *
821 71eb0e7f 2018-09-16 stsp got_object_tree_open(struct got_tree_object **tree,
822 71eb0e7f 2018-09-16 stsp struct got_repository *repo, struct got_object *obj)
824 13c729f7 2018-12-24 stsp return open_tree(tree, repo, got_object_get_id(obj), 1);
827 883f0469 2018-06-23 stsp const struct got_tree_entries *
828 883f0469 2018-06-23 stsp got_object_tree_get_entries(struct got_tree_object *tree)
830 883f0469 2018-06-23 stsp return &tree->entries;
833 3840f4c9 2018-09-12 stsp static const struct got_error *
834 ac544f8c 2019-01-13 stsp request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
835 ebc55e2d 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
836 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
838 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
839 3840f4c9 2018-09-12 stsp int outfd_child;
840 3840f4c9 2018-09-12 stsp int basefd, accumfd; /* temporary files for delta application */
842 3840f4c9 2018-09-12 stsp basefd = got_opentempfd();
843 3840f4c9 2018-09-12 stsp if (basefd == -1)
844 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
845 3840f4c9 2018-09-12 stsp accumfd = got_opentempfd();
846 3840f4c9 2018-09-12 stsp if (accumfd == -1)
847 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
849 3840f4c9 2018-09-12 stsp outfd_child = dup(outfd);
850 3840f4c9 2018-09-12 stsp if (outfd_child == -1)
851 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
853 ebc55e2d 2018-12-24 stsp err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
855 3840f4c9 2018-09-12 stsp return err;
857 3840f4c9 2018-09-12 stsp err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
858 3840f4c9 2018-09-12 stsp outfd_child);
860 41496140 2019-02-21 stsp close(basefd);
861 41496140 2019-02-21 stsp close(accumfd);
862 3840f4c9 2018-09-12 stsp return err;
865 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
868 3840f4c9 2018-09-12 stsp close(accumfd);
869 3840f4c9 2018-09-12 stsp return err;
872 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
875 3840f4c9 2018-09-12 stsp return err;
877 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen,
878 ebc55e2d 2018-12-24 stsp pack->privsep_child->ibuf);
880 3840f4c9 2018-09-12 stsp return err;
882 3840f4c9 2018-09-12 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
883 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
885 3840f4c9 2018-09-12 stsp return err;
888 ebc55e2d 2018-12-24 stsp static const struct got_error *
889 ac544f8c 2019-01-13 stsp read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
890 ac544f8c 2019-01-13 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
891 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
893 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
895 ebc55e2d 2018-12-24 stsp if (pack->privsep_child == NULL) {
896 ebc55e2d 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
898 ebc55e2d 2018-12-24 stsp return err;
901 ac544f8c 2019-01-13 stsp return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
905 9f2369b0 2018-12-24 stsp static const struct got_error *
906 ac544f8c 2019-01-13 stsp request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
907 ac544f8c 2019-01-13 stsp int infd, struct imsgbuf *ibuf)
909 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
910 9f2369b0 2018-12-24 stsp int outfd_child;
912 9f2369b0 2018-12-24 stsp outfd_child = dup(outfd);
913 9f2369b0 2018-12-24 stsp if (outfd_child == -1)
914 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
916 9f2369b0 2018-12-24 stsp err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
918 9f2369b0 2018-12-24 stsp return err;
920 9f2369b0 2018-12-24 stsp err = got_privsep_send_blob_outfd(ibuf, outfd_child);
922 9f2369b0 2018-12-24 stsp return err;
924 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
926 9f2369b0 2018-12-24 stsp return err;
928 9f2369b0 2018-12-24 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
929 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
931 9f2369b0 2018-12-24 stsp return err;
934 9f2369b0 2018-12-24 stsp static const struct got_error *
935 ac544f8c 2019-01-13 stsp read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
936 9f2369b0 2018-12-24 stsp int outfd, int infd, struct got_repository *repo)
938 9f2369b0 2018-12-24 stsp int imsg_fds[2];
940 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
942 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
943 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
944 ac544f8c 2019-01-13 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
947 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
948 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
949 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
951 9f2369b0 2018-12-24 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
952 638f9024 2019-05-13 stsp return got_error_from_errno("socketpair");
954 9f2369b0 2018-12-24 stsp pid = fork();
955 9f2369b0 2018-12-24 stsp if (pid == -1)
956 638f9024 2019-05-13 stsp return got_error_from_errno("fork");
957 9f2369b0 2018-12-24 stsp else if (pid == 0) {
958 9f2369b0 2018-12-24 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
959 9f2369b0 2018-12-24 stsp repo->path);
960 9f2369b0 2018-12-24 stsp /* not reached */
963 3a6ce05a 2019-02-11 stsp if (close(imsg_fds[1]) != 0)
964 638f9024 2019-05-13 stsp return got_error_from_errno("close");
965 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
966 9f2369b0 2018-12-24 stsp imsg_fds[0];
967 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
968 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
969 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
971 ac544f8c 2019-01-13 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
974 ebc55e2d 2018-12-24 stsp static const struct got_error *
975 ebc55e2d 2018-12-24 stsp open_blob(struct got_blob_object **blob, struct got_repository *repo,
976 ebc55e2d 2018-12-24 stsp struct got_object_id *id, size_t blocksize)
978 ebc55e2d 2018-12-24 stsp const struct got_error *err = NULL;
979 ebc55e2d 2018-12-24 stsp struct got_packidx *packidx = NULL;
981 ebc55e2d 2018-12-24 stsp char *path_packfile;
982 ac544f8c 2019-01-13 stsp uint8_t *outbuf;
984 ebc55e2d 2018-12-24 stsp size_t size, hdrlen;
985 ebc55e2d 2018-12-24 stsp struct stat sb;
987 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
988 4558fcd4 2018-01-14 stsp if (*blob == NULL)
989 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
991 55da3778 2018-09-10 stsp outfd = got_opentempfd();
992 55da3778 2018-09-10 stsp if (outfd == -1)
993 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
995 062ebb78 2018-07-12 stsp (*blob)->read_buf = malloc(blocksize);
996 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
997 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1001 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1002 e82b1d81 2019-07-27 stsp if (err == NULL) {
1003 ebc55e2d 2018-12-24 stsp struct got_pack *pack = NULL;
1005 ebc55e2d 2018-12-24 stsp err = get_packfile_path(&path_packfile, packidx);
1007 ebc55e2d 2018-12-24 stsp goto done;
1009 ebc55e2d 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1010 55da3778 2018-09-10 stsp if (pack == NULL) {
1011 ebc55e2d 2018-12-24 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1014 55da3778 2018-09-10 stsp goto done;
1016 ac544f8c 2019-01-13 stsp err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1017 ac544f8c 2019-01-13 stsp pack, packidx, idx, id);
1018 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1021 e82b1d81 2019-07-27 stsp err = open_loose_object(&infd, id, repo);
1023 e82b1d81 2019-07-27 stsp goto done;
1024 ac544f8c 2019-01-13 stsp err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1028 55da3778 2018-09-10 stsp goto done;
1030 de060dff 2018-12-24 stsp if (hdrlen > size) {
1031 ebc55e2d 2018-12-24 stsp err = got_error(GOT_ERR_BAD_OBJ_HDR);
1032 ebc55e2d 2018-12-24 stsp goto done;
1035 ac544f8c 2019-01-13 stsp if (outbuf) {
1036 3a6ce05a 2019-02-11 stsp if (close(outfd) != 0 && err == NULL)
1037 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1038 ac544f8c 2019-01-13 stsp outfd = -1;
1039 ac544f8c 2019-01-13 stsp (*blob)->f = fmemopen(outbuf, size, "rb");
1040 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1041 638f9024 2019-05-13 stsp err = got_error_from_errno("fmemopen");
1042 ac544f8c 2019-01-13 stsp free(outbuf);
1043 ac544f8c 2019-01-13 stsp goto done;
1045 ac544f8c 2019-01-13 stsp (*blob)->data = outbuf;
1047 ac544f8c 2019-01-13 stsp if (fstat(outfd, &sb) == -1) {
1048 638f9024 2019-05-13 stsp err = got_error_from_errno("fstat");
1049 ac544f8c 2019-01-13 stsp goto done;
1052 ac544f8c 2019-01-13 stsp if (sb.st_size != size) {
1053 ac544f8c 2019-01-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1054 ac544f8c 2019-01-13 stsp goto done;
1057 ac544f8c 2019-01-13 stsp (*blob)->f = fdopen(outfd, "rb");
1058 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1059 638f9024 2019-05-13 stsp err = got_error_from_errno("fdopen");
1060 ac544f8c 2019-01-13 stsp close(outfd);
1061 ac544f8c 2019-01-13 stsp outfd = -1;
1062 ac544f8c 2019-01-13 stsp goto done;
1066 ebc55e2d 2018-12-24 stsp (*blob)->hdrlen = hdrlen;
1067 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
1068 ebc55e2d 2018-12-24 stsp memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1071 55da3778 2018-09-10 stsp if (err) {
1072 55da3778 2018-09-10 stsp if (*blob) {
1073 7baf5860 2019-03-19 stsp got_object_blob_close(*blob);
1074 55da3778 2018-09-10 stsp *blob = NULL;
1075 55da3778 2018-09-10 stsp } else if (outfd != -1)
1076 55da3778 2018-09-10 stsp close(outfd);
1078 a19581a2 2018-06-21 stsp return err;
1081 a19581a2 2018-06-21 stsp const struct got_error *
1082 a19581a2 2018-06-21 stsp got_object_open_as_blob(struct got_blob_object **blob,
1083 a19581a2 2018-06-21 stsp struct got_repository *repo, struct got_object_id *id,
1084 a19581a2 2018-06-21 stsp size_t blocksize)
1086 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, id, blocksize);
1089 ebc55e2d 2018-12-24 stsp const struct got_error *
1090 ebc55e2d 2018-12-24 stsp got_object_blob_open(struct got_blob_object **blob,
1091 ebc55e2d 2018-12-24 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
1093 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1096 fb43ecf1 2019-02-11 stsp const struct got_error *
1097 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
1099 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL;
1100 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
1101 7baf5860 2019-03-19 stsp if (blob->f && fclose(blob->f) != 0)
1102 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1103 ac544f8c 2019-01-13 stsp free(blob->data);
1104 68482ea3 2017-11-27 stsp free(blob);
1105 fb43ecf1 2019-02-11 stsp return err;
1109 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1111 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1115 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
1117 f934cf2c 2018-02-12 stsp return blob->hdrlen;
1120 f934cf2c 2018-02-12 stsp const uint8_t *
1121 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
1123 f934cf2c 2018-02-12 stsp return blob->read_buf;
1126 68482ea3 2017-11-27 stsp const struct got_error *
1127 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1131 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1132 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
1133 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
1134 eb651edf 2018-02-11 stsp *outlenp = n;
1135 35e9ba5d 2018-06-21 stsp return NULL;
1138 35e9ba5d 2018-06-21 stsp const struct got_error *
1139 6fcac457 2018-11-19 stsp got_object_blob_dump_to_file(size_t *total_len, int *nlines,
1140 6c4c42e0 2019-06-24 stsp off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1142 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
1143 b6752625 2018-12-24 stsp size_t n, len, hdrlen;
1144 84451b3e 2018-07-10 stsp const uint8_t *buf;
1146 6c4c42e0 2019-06-24 stsp size_t noffsets = 0;
1147 6c4c42e0 2019-06-24 stsp off_t off = 0;
1149 6c4c42e0 2019-06-24 stsp if (line_offsets)
1150 6c4c42e0 2019-06-24 stsp *line_offsets = NULL;
1151 84451b3e 2018-07-10 stsp if (total_len)
1152 84451b3e 2018-07-10 stsp *total_len = 0;
1153 84451b3e 2018-07-10 stsp if (nlines)
1154 84451b3e 2018-07-10 stsp *nlines = 0;
1156 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1158 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
1160 35e9ba5d 2018-06-21 stsp return err;
1161 35e9ba5d 2018-06-21 stsp if (len == 0)
1163 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
1164 78695fb7 2019-08-12 stsp if (line_offsets && nlines) {
1165 78695fb7 2019-08-12 stsp if (*line_offsets == NULL) {
1166 78695fb7 2019-08-12 stsp /* Have some data but perhaps no '\n'. */
1167 78695fb7 2019-08-12 stsp noffsets = 1;
1168 78695fb7 2019-08-12 stsp *nlines = 1;
1169 78695fb7 2019-08-12 stsp *line_offsets = malloc(sizeof(**line_offsets));
1170 78695fb7 2019-08-12 stsp if (*line_offsets == NULL)
1171 78695fb7 2019-08-12 stsp return got_error_from_errno("malloc");
1172 78695fb7 2019-08-12 stsp (*line_offsets)[0] = 0;
1174 78695fb7 2019-08-12 stsp /* Scan '\n' offsets in this chunk of data. */
1175 78695fb7 2019-08-12 stsp for (i = hdrlen; i < len; i++) {
1176 78695fb7 2019-08-12 stsp if (i > hdrlen && buf[i] == '\n')
1177 78695fb7 2019-08-12 stsp (*nlines)++;
1178 78695fb7 2019-08-12 stsp if (noffsets < *nlines) {
1179 78695fb7 2019-08-12 stsp off_t *o = recallocarray(*line_offsets,
1180 78695fb7 2019-08-12 stsp noffsets, *nlines,
1181 78695fb7 2019-08-12 stsp sizeof(**line_offsets));
1182 78695fb7 2019-08-12 stsp if (o == NULL) {
1183 78695fb7 2019-08-12 stsp free(*line_offsets);
1184 78695fb7 2019-08-12 stsp *line_offsets = NULL;
1185 78695fb7 2019-08-12 stsp return got_error_from_errno(
1186 78695fb7 2019-08-12 stsp "recallocarray");
1188 78695fb7 2019-08-12 stsp *line_offsets = o;
1189 78695fb7 2019-08-12 stsp noffsets = *nlines;
1191 78695fb7 2019-08-12 stsp if (total_len) {
1192 78695fb7 2019-08-12 stsp (*line_offsets)[*nlines - 1] = off;
1193 78695fb7 2019-08-12 stsp off = *total_len + i -
1194 78695fb7 2019-08-12 stsp got_object_blob_get_hdrlen(blob);
1198 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
1199 454a6b59 2018-12-24 stsp n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1200 b6752625 2018-12-24 stsp if (n != len - hdrlen)
1201 b6752625 2018-12-24 stsp return got_ferror(outfile, GOT_ERR_IO);
1202 78695fb7 2019-08-12 stsp if (total_len)
1203 78695fb7 2019-08-12 stsp *total_len += len - hdrlen;
1204 35e9ba5d 2018-06-21 stsp hdrlen = 0;
1205 35e9ba5d 2018-06-21 stsp } while (len != 0);
1207 cbe7f848 2019-02-11 stsp if (fflush(outfile) != 0)
1208 638f9024 2019-05-13 stsp return got_error_from_errno("fflush");
1209 35e9ba5d 2018-06-21 stsp rewind(outfile);
1211 776d4d29 2018-06-17 stsp return NULL;
1214 f4a881ce 2018-11-17 stsp static const struct got_error *
1215 268f7291 2018-12-24 stsp request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1216 268f7291 2018-12-24 stsp int pack_idx, struct got_object_id *id)
1218 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
1220 268f7291 2018-12-24 stsp err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1221 268f7291 2018-12-24 stsp pack_idx);
1223 a158c901 2018-12-23 stsp return err;
1225 a158c901 2018-12-23 stsp return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1228 268f7291 2018-12-24 stsp static const struct got_error *
1229 268f7291 2018-12-24 stsp read_packed_tag_privsep(struct got_tag_object **tag,
1230 268f7291 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1231 268f7291 2018-12-24 stsp struct got_object_id *id)
1233 268f7291 2018-12-24 stsp const struct got_error *err = NULL;
1235 268f7291 2018-12-24 stsp if (pack->privsep_child)
1236 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1238 268f7291 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
1240 268f7291 2018-12-24 stsp return err;
1242 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1245 9f2369b0 2018-12-24 stsp static const struct got_error *
1246 9f2369b0 2018-12-24 stsp request_tag(struct got_tag_object **tag, struct got_repository *repo,
1249 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1250 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1252 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1254 9f2369b0 2018-12-24 stsp err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1256 9f2369b0 2018-12-24 stsp return err;
1258 9f2369b0 2018-12-24 stsp return got_privsep_recv_tag(tag, ibuf);
1261 9f2369b0 2018-12-24 stsp static const struct got_error *
1262 9f2369b0 2018-12-24 stsp read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1263 9f2369b0 2018-12-24 stsp struct got_repository *repo)
1265 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1266 9f2369b0 2018-12-24 stsp pid_t pid;
1267 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1269 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1270 9f2369b0 2018-12-24 stsp return request_tag(tag, repo, obj_fd);
1272 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1273 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1274 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1276 9f2369b0 2018-12-24 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1277 638f9024 2019-05-13 stsp return got_error_from_errno("socketpair");
1279 9f2369b0 2018-12-24 stsp pid = fork();
1280 9f2369b0 2018-12-24 stsp if (pid == -1)
1281 638f9024 2019-05-13 stsp return got_error_from_errno("fork");
1282 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1283 9f2369b0 2018-12-24 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1284 9f2369b0 2018-12-24 stsp repo->path);
1285 9f2369b0 2018-12-24 stsp /* not reached */
1288 3a6ce05a 2019-02-11 stsp if (close(imsg_fds[1]) != 0)
1289 638f9024 2019-05-13 stsp return got_error_from_errno("close");
1290 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1291 9f2369b0 2018-12-24 stsp imsg_fds[0];
1292 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1293 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1294 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1296 9f2369b0 2018-12-24 stsp return request_tag(tag, repo, obj_fd);
1299 a158c901 2018-12-23 stsp static const struct got_error *
1300 268f7291 2018-12-24 stsp open_tag(struct got_tag_object **tag, struct got_repository *repo,
1301 268f7291 2018-12-24 stsp struct got_object_id *id, int check_cache)
1303 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1304 268f7291 2018-12-24 stsp struct got_packidx *packidx = NULL;
1306 268f7291 2018-12-24 stsp char *path_packfile;
1308 f4a881ce 2018-11-17 stsp if (check_cache) {
1309 268f7291 2018-12-24 stsp *tag = got_repo_get_cached_tag(repo, id);
1310 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1311 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1312 f4a881ce 2018-11-17 stsp return NULL;
1315 f4a881ce 2018-11-17 stsp *tag = NULL;
1317 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1318 e82b1d81 2019-07-27 stsp if (err == NULL) {
1319 268f7291 2018-12-24 stsp struct got_pack *pack = NULL;
1321 268f7291 2018-12-24 stsp err = get_packfile_path(&path_packfile, packidx);
1323 268f7291 2018-12-24 stsp return err;
1325 268f7291 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1326 f4a881ce 2018-11-17 stsp if (pack == NULL) {
1327 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1330 f4a881ce 2018-11-17 stsp return err;
1332 e82b1d81 2019-07-27 stsp err = read_packed_tag_privsep(tag, pack,
1333 e82b1d81 2019-07-27 stsp packidx, idx, id);
1334 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1337 e82b1d81 2019-07-27 stsp err = open_loose_object(&fd, id, repo);
1339 e82b1d81 2019-07-27 stsp return err;
1340 9f2369b0 2018-12-24 stsp err = read_tag_privsep(tag, fd, repo);
1343 f4a881ce 2018-11-17 stsp if (err == NULL) {
1344 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1345 268f7291 2018-12-24 stsp err = got_repo_cache_tag(repo, id, *tag);
1348 f4a881ce 2018-11-17 stsp return err;
1351 f4a881ce 2018-11-17 stsp const struct got_error *
1352 f4a881ce 2018-11-17 stsp got_object_open_as_tag(struct got_tag_object **tag,
1353 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object_id *id)
1355 f4a881ce 2018-11-17 stsp *tag = got_repo_get_cached_tag(repo, id);
1356 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1357 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1358 f4a881ce 2018-11-17 stsp return NULL;
1361 268f7291 2018-12-24 stsp return open_tag(tag, repo, id, 0);
1364 f4a881ce 2018-11-17 stsp const struct got_error *
1365 f4a881ce 2018-11-17 stsp got_object_tag_open(struct got_tag_object **tag,
1366 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object *obj)
1368 268f7291 2018-12-24 stsp return open_tag(tag, repo, got_object_get_id(obj), 1);
1371 d24820bf 2019-08-11 stsp const char *
1372 d24820bf 2019-08-11 stsp got_object_tag_get_name(struct got_tag_object *tag)
1374 d24820bf 2019-08-11 stsp return tag->tag;
1378 0bd18d37 2019-02-01 stsp got_object_tag_get_object_type(struct got_tag_object *tag)
1380 0bd18d37 2019-02-01 stsp return tag->obj_type;
1383 0bd18d37 2019-02-01 stsp struct got_object_id *
1384 0bd18d37 2019-02-01 stsp got_object_tag_get_object_id(struct got_tag_object *tag)
1386 0bd18d37 2019-02-01 stsp return &tag->id;
1389 776d4d29 2018-06-17 stsp static struct got_tree_entry *
1390 65a9bbe9 2018-09-15 stsp find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1392 776d4d29 2018-06-17 stsp struct got_tree_entry *te;
1394 63da309a 2018-11-07 stsp /* Note that tree entries are sorted in strncmp() order. */
1395 883f0469 2018-06-23 stsp SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
1396 63da309a 2018-11-07 stsp int cmp = strncmp(te->name, name, len);
1397 63da309a 2018-11-07 stsp if (cmp < 0)
1399 63da309a 2018-11-07 stsp if (cmp > 0)
1401 63da309a 2018-11-07 stsp if (te->name[len] == '\0')
1402 776d4d29 2018-06-17 stsp return te;
1404 eb651edf 2018-02-11 stsp return NULL;
1407 a129376b 2019-03-28 stsp const struct got_tree_entry *
1408 a129376b 2019-03-28 stsp got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1410 a129376b 2019-03-28 stsp return find_entry_by_name(tree, name, strlen(name));
1413 776d4d29 2018-06-17 stsp const struct got_error *
1414 27d434c2 2018-09-15 stsp got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1415 776d4d29 2018-06-17 stsp struct got_object_id *commit_id, const char *path)
1417 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
1418 776d4d29 2018-06-17 stsp struct got_commit_object *commit = NULL;
1419 776d4d29 2018-06-17 stsp struct got_tree_object *tree = NULL;
1420 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
1421 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1422 b7cd37e5 2018-11-18 stsp size_t seglen;
1424 27d434c2 2018-09-15 stsp *id = NULL;
1426 776d4d29 2018-06-17 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
1428 776d4d29 2018-06-17 stsp goto done;
1430 776d4d29 2018-06-17 stsp /* Handle opening of root of commit's tree. */
1431 5e54fb30 2019-05-31 stsp if (got_path_is_root_dir(path)) {
1432 27d434c2 2018-09-15 stsp *id = got_object_id_dup(commit->tree_id);
1433 27d434c2 2018-09-15 stsp if (*id == NULL)
1434 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
1435 ca008b32 2018-07-22 stsp goto done;
1438 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1440 776d4d29 2018-06-17 stsp goto done;
1443 5e54fb30 2019-05-31 stsp while (s[0] == '/')
1446 65a9bbe9 2018-09-15 stsp seglen = 0;
1447 b7cd37e5 2018-11-18 stsp while (*s) {
1448 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
1450 776d4d29 2018-06-17 stsp if (*s != '/') {
1457 65a9bbe9 2018-09-15 stsp te = find_entry_by_name(tree, seg, seglen);
1458 db37e2c0 2018-06-21 stsp if (te == NULL) {
1459 d1451975 2018-11-11 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1460 776d4d29 2018-06-17 stsp goto done;
1463 b7cd37e5 2018-11-18 stsp if (*s == '\0')
1466 776d4d29 2018-06-17 stsp seg = s + 1;
1467 65a9bbe9 2018-09-15 stsp seglen = 0;
1470 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
1472 db37e2c0 2018-06-21 stsp te = NULL;
1474 776d4d29 2018-06-17 stsp goto done;
1475 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
1476 776d4d29 2018-06-17 stsp tree = next_tree;
1481 27d434c2 2018-09-15 stsp *id = got_object_id_dup(te->id);
1482 27d434c2 2018-09-15 stsp if (*id == NULL)
1483 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
1485 d1451975 2018-11-11 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1487 776d4d29 2018-06-17 stsp if (commit)
1488 776d4d29 2018-06-17 stsp got_object_commit_close(commit);
1490 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
1491 776d4d29 2018-06-17 stsp return err;
1494 07862c20 2018-09-15 stsp const struct got_error *
1495 07862c20 2018-09-15 stsp got_object_tree_path_changed(int *changed,
1496 07862c20 2018-09-15 stsp struct got_tree_object *tree01, struct got_tree_object *tree02,
1497 07862c20 2018-09-15 stsp const char *path, struct got_repository *repo)
1499 07862c20 2018-09-15 stsp const struct got_error *err = NULL;
1500 07862c20 2018-09-15 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1501 07862c20 2018-09-15 stsp struct got_tree_entry *te1 = NULL, *te2 = NULL;
1502 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1503 3b7f9878 2018-11-18 stsp size_t seglen;
1505 07862c20 2018-09-15 stsp *changed = 0;
1507 07862c20 2018-09-15 stsp /* We are expecting an absolute in-repository path. */
1508 07862c20 2018-09-15 stsp if (path[0] != '/')
1509 07862c20 2018-09-15 stsp return got_error(GOT_ERR_NOT_ABSPATH);
1511 07862c20 2018-09-15 stsp /* We not do support comparing the root path. */
1512 07862c20 2018-09-15 stsp if (path[1] == '\0')
1513 07862c20 2018-09-15 stsp return got_error(GOT_ERR_BAD_PATH);
1515 07862c20 2018-09-15 stsp tree1 = tree01;
1516 07862c20 2018-09-15 stsp tree2 = tree02;
1518 07862c20 2018-09-15 stsp s++; /* skip leading '/' */
1520 65a9bbe9 2018-09-15 stsp seglen = 0;
1521 3b7f9878 2018-11-18 stsp while (*s) {
1522 07862c20 2018-09-15 stsp struct got_tree_object *next_tree1, *next_tree2;
1524 07862c20 2018-09-15 stsp if (*s != '/') {
1531 65a9bbe9 2018-09-15 stsp te1 = find_entry_by_name(tree1, seg, seglen);
1532 07862c20 2018-09-15 stsp if (te1 == NULL) {
1533 07862c20 2018-09-15 stsp err = got_error(GOT_ERR_NO_OBJ);
1534 07862c20 2018-09-15 stsp goto done;
1537 65a9bbe9 2018-09-15 stsp te2 = find_entry_by_name(tree2, seg, seglen);
1538 07862c20 2018-09-15 stsp if (te2 == NULL) {
1539 07862c20 2018-09-15 stsp *changed = 1;
1540 07862c20 2018-09-15 stsp goto done;
1543 07862c20 2018-09-15 stsp if (te1->mode != te2->mode) {
1544 07862c20 2018-09-15 stsp *changed = 1;
1545 07862c20 2018-09-15 stsp goto done;
1548 07862c20 2018-09-15 stsp if (got_object_id_cmp(te1->id, te2->id) == 0) {
1549 07862c20 2018-09-15 stsp *changed = 0;
1550 07862c20 2018-09-15 stsp goto done;
1553 3b7f9878 2018-11-18 stsp if (*s == '\0') { /* final path element */
1554 07862c20 2018-09-15 stsp *changed = 1;
1555 07862c20 2018-09-15 stsp goto done;
1558 07862c20 2018-09-15 stsp seg = s + 1;
1560 65a9bbe9 2018-09-15 stsp seglen = 0;
1562 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree1, repo,
1564 07862c20 2018-09-15 stsp te1 = NULL;
1566 07862c20 2018-09-15 stsp goto done;
1567 a31cea73 2018-09-15 stsp if (tree1 != tree01)
1568 a31cea73 2018-09-15 stsp got_object_tree_close(tree1);
1569 07862c20 2018-09-15 stsp tree1 = next_tree1;
1571 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree2, repo,
1573 07862c20 2018-09-15 stsp te2 = NULL;
1575 07862c20 2018-09-15 stsp goto done;
1576 a31cea73 2018-09-15 stsp if (tree2 != tree02)
1577 a31cea73 2018-09-15 stsp got_object_tree_close(tree2);
1578 07862c20 2018-09-15 stsp tree2 = next_tree2;
1582 a31cea73 2018-09-15 stsp if (tree1 && tree1 != tree01)
1583 07862c20 2018-09-15 stsp got_object_tree_close(tree1);
1584 a31cea73 2018-09-15 stsp if (tree2 && tree2 != tree02)
1585 07862c20 2018-09-15 stsp got_object_tree_close(tree2);
1586 77880158 2018-11-04 stsp return err;
1589 ed175427 2019-05-09 stsp const struct got_error *
1590 ed175427 2019-05-09 stsp got_object_tree_entry_dup(struct got_tree_entry **new_te,
1591 ed175427 2019-05-09 stsp struct got_tree_entry *te)
1593 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
1595 ed175427 2019-05-09 stsp *new_te = calloc(1, sizeof(**new_te));
1596 ed175427 2019-05-09 stsp if (*new_te == NULL)
1597 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1599 ed175427 2019-05-09 stsp (*new_te)->mode = te->mode;
1600 ed175427 2019-05-09 stsp (*new_te)->name = strdup(te->name);
1601 ed175427 2019-05-09 stsp if ((*new_te)->name == NULL) {
1602 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1603 8c4eabf2 2019-05-10 stsp goto done;
1606 ed175427 2019-05-09 stsp (*new_te)->id = got_object_id_dup(te->id);
1607 ed175427 2019-05-09 stsp if ((*new_te)->id == NULL) {
1608 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
1609 8c4eabf2 2019-05-10 stsp goto done;
1612 8c4eabf2 2019-05-10 stsp if (err) {
1613 8c4eabf2 2019-05-10 stsp got_object_tree_entry_close(*new_te);
1614 8c4eabf2 2019-05-10 stsp *new_te = NULL;
1616 8c4eabf2 2019-05-10 stsp return err;