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 d1cda826 2017-11-06 stsp char *path_objects = got_repo_get_path_objects(repo);
119 e6b1056e 2018-04-22 stsp *path = NULL;
121 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
122 638f9024 2019-05-13 stsp return got_error_from_errno("got_repo_get_path_objects");
124 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
128 d1cda826 2017-11-06 stsp if (asprintf(path, "%s/%.2x/%s", path_objects,
129 d1cda826 2017-11-06 stsp id->sha1[0], hex + 2) == -1)
130 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
134 d1cda826 2017-11-06 stsp free(path_objects);
135 d1cda826 2017-11-06 stsp return err;
138 4ee4114f 2018-01-23 stsp static const struct got_error *
139 4796fb13 2018-12-23 stsp open_loose_object(int *fd, struct got_object_id *id,
140 4796fb13 2018-12-23 stsp struct got_repository *repo)
142 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
143 a1fd68d8 2018-01-12 stsp char *path;
145 90bdb554 2019-04-11 stsp err = got_object_get_path(&path, id, repo);
147 d1cda826 2017-11-06 stsp return err;
148 a5b57ccf 2019-04-11 stsp *fd = open(path, O_RDONLY | O_NOFOLLOW);
149 d5003b79 2018-04-22 stsp if (*fd == -1) {
150 e82b1d81 2019-07-27 stsp err = got_error_from_errno2("open", path);
154 4558fcd4 2018-01-14 stsp free(path);
155 2090a03d 2018-09-09 stsp return err;
158 2090a03d 2018-09-09 stsp static const struct got_error *
159 2090a03d 2018-09-09 stsp get_packfile_path(char **path_packfile, struct got_packidx *packidx)
161 2090a03d 2018-09-09 stsp size_t size;
163 2090a03d 2018-09-09 stsp /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
164 2090a03d 2018-09-09 stsp size = strlen(packidx->path_packidx) + 2;
165 2090a03d 2018-09-09 stsp if (size < GOT_PACKFILE_NAMELEN + 1)
166 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_BAD_PATH);
168 08451938 2018-11-05 stsp *path_packfile = malloc(size);
169 2090a03d 2018-09-09 stsp if (*path_packfile == NULL)
170 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
172 2090a03d 2018-09-09 stsp /* Copy up to and excluding ".idx". */
173 2090a03d 2018-09-09 stsp if (strlcpy(*path_packfile, packidx->path_packidx,
174 2090a03d 2018-09-09 stsp size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
175 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_NO_SPACE);
177 2090a03d 2018-09-09 stsp if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
178 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_NO_SPACE);
180 2090a03d 2018-09-09 stsp return NULL;
183 a158c901 2018-12-23 stsp static void
184 a158c901 2018-12-23 stsp exec_privsep_child(int imsg_fds[2], const char *path, const char *repo_path)
186 3a6ce05a 2019-02-11 stsp if (close(imsg_fds[0]) != 0) {
187 48b8b0eb 2019-05-11 jcs fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
191 a158c901 2018-12-23 stsp if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
192 48b8b0eb 2019-05-11 jcs fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
195 a158c901 2018-12-23 stsp if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
196 48b8b0eb 2019-05-11 jcs fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
200 a158c901 2018-12-23 stsp if (execl(path, path, repo_path, (char *)NULL) == -1) {
201 a158c901 2018-12-23 stsp fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
202 a158c901 2018-12-23 stsp strerror(errno));
207 2090a03d 2018-09-09 stsp static const struct got_error *
208 a158c901 2018-12-23 stsp request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
209 a158c901 2018-12-23 stsp struct got_object_id *id)
211 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
212 a158c901 2018-12-23 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
214 a158c901 2018-12-23 stsp err = got_privsep_send_packed_obj_req(ibuf, idx, id);
216 a158c901 2018-12-23 stsp return err;
218 a158c901 2018-12-23 stsp err = got_privsep_recv_obj(obj, ibuf);
220 a158c901 2018-12-23 stsp return err;
222 a158c901 2018-12-23 stsp (*obj)->path_packfile = strdup(pack->path_packfile);
223 a158c901 2018-12-23 stsp if ((*obj)->path_packfile == NULL) {
224 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
225 a158c901 2018-12-23 stsp return err;
227 a158c901 2018-12-23 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
229 a158c901 2018-12-23 stsp return NULL;
232 da506691 2019-05-22 stsp static void
233 b48e2ddb 2019-05-22 stsp set_max_datasize(void)
235 b48e2ddb 2019-05-22 stsp struct rlimit rl;
237 b48e2ddb 2019-05-22 stsp if (getrlimit(RLIMIT_DATA, &rl) != 0)
240 b48e2ddb 2019-05-22 stsp rl.rlim_cur = rl.rlim_max;
241 b48e2ddb 2019-05-22 stsp setrlimit(RLIMIT_DATA, &rl);
244 a158c901 2018-12-23 stsp static const struct got_error *
245 711fb6e8 2018-12-23 stsp start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
247 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
248 a158c901 2018-12-23 stsp int imsg_fds[2];
250 a158c901 2018-12-23 stsp struct imsgbuf *ibuf;
252 a158c901 2018-12-23 stsp ibuf = calloc(1, sizeof(*ibuf));
253 a158c901 2018-12-23 stsp if (ibuf == NULL)
254 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
256 a158c901 2018-12-23 stsp pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
257 a158c901 2018-12-23 stsp if (pack->privsep_child == NULL) {
258 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
259 a158c901 2018-12-23 stsp free(ibuf);
260 a158c901 2018-12-23 stsp return err;
263 a158c901 2018-12-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
264 638f9024 2019-05-13 stsp err = got_error_from_errno("socketpair");
268 a158c901 2018-12-23 stsp pid = fork();
269 a158c901 2018-12-23 stsp if (pid == -1) {
270 638f9024 2019-05-13 stsp err = got_error_from_errno("fork");
272 a158c901 2018-12-23 stsp } else if (pid == 0) {
273 b48e2ddb 2019-05-22 stsp set_max_datasize();
274 a158c901 2018-12-23 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
275 a158c901 2018-12-23 stsp pack->path_packfile);
276 a158c901 2018-12-23 stsp /* not reached */
279 3a6ce05a 2019-02-11 stsp if (close(imsg_fds[1]) != 0)
280 638f9024 2019-05-13 stsp return got_error_from_errno("close");
281 a158c901 2018-12-23 stsp pack->privsep_child->imsg_fd = imsg_fds[0];
282 a158c901 2018-12-23 stsp pack->privsep_child->pid = pid;
283 a158c901 2018-12-23 stsp imsg_init(ibuf, imsg_fds[0]);
284 a158c901 2018-12-23 stsp pack->privsep_child->ibuf = ibuf;
286 a158c901 2018-12-23 stsp err = got_privsep_init_pack_child(ibuf, pack, packidx);
288 a158c901 2018-12-23 stsp const struct got_error *child_err;
289 a158c901 2018-12-23 stsp err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
290 a158c901 2018-12-23 stsp child_err = got_privsep_wait_for_child(
291 a158c901 2018-12-23 stsp pack->privsep_child->pid);
292 a158c901 2018-12-23 stsp if (child_err && err == NULL)
293 a158c901 2018-12-23 stsp err = child_err;
297 a158c901 2018-12-23 stsp free(ibuf);
298 a158c901 2018-12-23 stsp free(pack->privsep_child);
299 a158c901 2018-12-23 stsp pack->privsep_child = NULL;
301 a158c901 2018-12-23 stsp return err;
304 711fb6e8 2018-12-23 stsp static const struct got_error *
305 711fb6e8 2018-12-23 stsp read_packed_object_privsep(struct got_object **obj,
306 711fb6e8 2018-12-23 stsp struct got_repository *repo, struct got_pack *pack,
307 711fb6e8 2018-12-23 stsp struct got_packidx *packidx, int idx, struct got_object_id *id)
309 711fb6e8 2018-12-23 stsp const struct got_error *err = NULL;
311 711fb6e8 2018-12-23 stsp if (pack->privsep_child)
312 711fb6e8 2018-12-23 stsp return request_packed_object(obj, pack, idx, id);
314 711fb6e8 2018-12-23 stsp err = start_pack_privsep_child(pack, packidx);
316 711fb6e8 2018-12-23 stsp return err;
318 711fb6e8 2018-12-23 stsp return request_packed_object(obj, pack, idx, id);
322 a158c901 2018-12-23 stsp static const struct got_error *
323 2090a03d 2018-09-09 stsp open_packed_object(struct got_object **obj, struct got_object_id *id,
324 2090a03d 2018-09-09 stsp struct got_repository *repo)
326 2090a03d 2018-09-09 stsp const struct got_error *err = NULL;
327 2090a03d 2018-09-09 stsp struct got_pack *pack = NULL;
328 2090a03d 2018-09-09 stsp struct got_packidx *packidx = NULL;
330 2090a03d 2018-09-09 stsp char *path_packfile;
332 2090a03d 2018-09-09 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
334 2090a03d 2018-09-09 stsp return err;
336 2090a03d 2018-09-09 stsp err = get_packfile_path(&path_packfile, packidx);
338 2090a03d 2018-09-09 stsp return err;
340 2090a03d 2018-09-09 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
341 2090a03d 2018-09-09 stsp if (pack == NULL) {
342 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
347 a158c901 2018-12-23 stsp err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
351 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
353 2090a03d 2018-09-09 stsp free(path_packfile);
354 4558fcd4 2018-01-14 stsp return err;
357 9f2369b0 2018-12-24 stsp static const struct got_error *
358 9f2369b0 2018-12-24 stsp request_object(struct got_object **obj, struct got_repository *repo, int fd)
360 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
361 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
363 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
365 aea5f015 2018-12-24 stsp err = got_privsep_send_obj_req(ibuf, fd);
367 9f2369b0 2018-12-24 stsp return err;
369 9f2369b0 2018-12-24 stsp return got_privsep_recv_obj(obj, ibuf);
372 9f2369b0 2018-12-24 stsp static const struct got_error *
373 9f2369b0 2018-12-24 stsp read_object_header_privsep(struct got_object **obj, struct got_repository *repo,
374 9f2369b0 2018-12-24 stsp int obj_fd)
376 9f2369b0 2018-12-24 stsp int imsg_fds[2];
378 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
380 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
381 9f2369b0 2018-12-24 stsp return request_object(obj, repo, obj_fd);
383 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
384 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
385 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
387 9f2369b0 2018-12-24 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
388 638f9024 2019-05-13 stsp return got_error_from_errno("socketpair");
390 9f2369b0 2018-12-24 stsp pid = fork();
391 9f2369b0 2018-12-24 stsp if (pid == -1)
392 638f9024 2019-05-13 stsp return got_error_from_errno("fork");
393 9f2369b0 2018-12-24 stsp else if (pid == 0) {
394 9f2369b0 2018-12-24 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
395 9f2369b0 2018-12-24 stsp repo->path);
396 9f2369b0 2018-12-24 stsp /* not reached */
399 3a6ce05a 2019-02-11 stsp if (close(imsg_fds[1]) != 0)
400 638f9024 2019-05-13 stsp return got_error_from_errno("close");
401 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
402 9f2369b0 2018-12-24 stsp imsg_fds[0];
403 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
404 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
405 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
407 9f2369b0 2018-12-24 stsp return request_object(obj, repo, obj_fd);
411 4558fcd4 2018-01-14 stsp const struct got_error *
412 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
413 4558fcd4 2018-01-14 stsp struct got_object_id *id)
415 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
416 6c00b545 2018-01-17 stsp char *path;
419 7bb0daa1 2018-06-21 stsp *obj = got_repo_get_cached_object(repo, id);
420 7bb0daa1 2018-06-21 stsp if (*obj != NULL) {
421 7bb0daa1 2018-06-21 stsp (*obj)->refcnt++;
422 7bb0daa1 2018-06-21 stsp return NULL;
425 e82b1d81 2019-07-27 stsp err = open_packed_object(obj, id, repo);
426 e82b1d81 2019-07-27 stsp if (err && err->code != GOT_ERR_NO_OBJ)
427 e82b1d81 2019-07-27 stsp return err;
428 e82b1d81 2019-07-27 stsp if (*obj) {
429 e82b1d81 2019-07-27 stsp (*obj)->refcnt++;
430 e82b1d81 2019-07-27 stsp return got_repo_cache_object(repo, id, *obj);
433 90bdb554 2019-04-11 stsp err = got_object_get_path(&path, id, repo);
435 4558fcd4 2018-01-14 stsp return err;
437 a5b57ccf 2019-04-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
438 2178c42e 2018-04-22 stsp if (fd == -1) {
439 e82b1d81 2019-07-27 stsp if (errno == ENOENT)
440 e82b1d81 2019-07-27 stsp err = got_error_no_obj(id);
442 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", path);
445 9f2369b0 2018-12-24 stsp err = read_object_header_privsep(obj, repo, fd);
448 ab9a70b2 2017-11-06 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
451 59790a32 2018-09-15 stsp (*obj)->refcnt++;
452 59790a32 2018-09-15 stsp err = got_repo_cache_object(repo, id, *obj);
454 6c00b545 2018-01-17 stsp free(path);
455 ab9a70b2 2017-11-06 stsp return err;
459 6dfa2fd3 2018-02-12 stsp const struct got_error *
460 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
461 6dfa2fd3 2018-02-12 stsp const char *id_str)
463 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
465 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
466 6dfa2fd3 2018-02-12 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
468 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
471 15a94983 2018-12-23 stsp const struct got_error *
472 15a94983 2018-12-23 stsp got_object_resolve_id_str(struct got_object_id **id,
473 15a94983 2018-12-23 stsp struct got_repository *repo, const char *id_str)
475 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
476 15a94983 2018-12-23 stsp struct got_object *obj;
478 15a94983 2018-12-23 stsp err = got_object_open_by_id_str(&obj, repo, id_str);
480 15a94983 2018-12-23 stsp return err;
482 15a94983 2018-12-23 stsp *id = got_object_id_dup(got_object_get_id(obj));
483 15a94983 2018-12-23 stsp got_object_close(obj);
484 15a94983 2018-12-23 stsp if (*id == NULL)
485 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
487 15a94983 2018-12-23 stsp return NULL;
490 434025f3 2018-09-16 stsp static const struct got_error *
491 a158c901 2018-12-23 stsp request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
492 a158c901 2018-12-23 stsp int pack_idx, struct got_object_id *id)
494 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
496 a158c901 2018-12-23 stsp err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
499 a158c901 2018-12-23 stsp return err;
501 a158c901 2018-12-23 stsp return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
504 a158c901 2018-12-23 stsp static const struct got_error *
505 a158c901 2018-12-23 stsp read_packed_commit_privsep(struct got_commit_object **commit,
506 a158c901 2018-12-23 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
507 a158c901 2018-12-23 stsp struct got_object_id *id)
509 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
511 a158c901 2018-12-23 stsp if (pack->privsep_child)
512 a158c901 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
514 711fb6e8 2018-12-23 stsp err = start_pack_privsep_child(pack, packidx);
516 a158c901 2018-12-23 stsp return err;
518 711fb6e8 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
521 9f2369b0 2018-12-24 stsp static const struct got_error *
522 9f2369b0 2018-12-24 stsp request_commit(struct got_commit_object **commit, struct got_repository *repo,
525 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
526 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
528 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
530 9f2369b0 2018-12-24 stsp err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
532 9f2369b0 2018-12-24 stsp return err;
534 9f2369b0 2018-12-24 stsp return got_privsep_recv_commit(commit, ibuf);
537 9f2369b0 2018-12-24 stsp static const struct got_error *
538 9f2369b0 2018-12-24 stsp read_commit_privsep(struct got_commit_object **commit, int obj_fd,
539 9f2369b0 2018-12-24 stsp struct got_repository *repo)
541 9f2369b0 2018-12-24 stsp int imsg_fds[2];
543 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
545 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
546 9f2369b0 2018-12-24 stsp return request_commit(commit, repo, obj_fd);
548 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
549 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
550 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
552 9f2369b0 2018-12-24 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
553 638f9024 2019-05-13 stsp return got_error_from_errno("socketpair");
555 9f2369b0 2018-12-24 stsp pid = fork();
556 9f2369b0 2018-12-24 stsp if (pid == -1)
557 638f9024 2019-05-13 stsp return got_error_from_errno("fork");
558 9f2369b0 2018-12-24 stsp else if (pid == 0) {
559 9f2369b0 2018-12-24 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
560 9f2369b0 2018-12-24 stsp repo->path);
561 9f2369b0 2018-12-24 stsp /* not reached */
564 3a6ce05a 2019-02-11 stsp if (close(imsg_fds[1]) != 0)
565 638f9024 2019-05-13 stsp return got_error_from_errno("close");
566 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
567 9f2369b0 2018-12-24 stsp imsg_fds[0];
568 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
569 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
570 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
572 9f2369b0 2018-12-24 stsp return request_commit(commit, repo, obj_fd);
576 a158c901 2018-12-23 stsp static const struct got_error *
577 434025f3 2018-09-16 stsp open_commit(struct got_commit_object **commit,
578 1785f84a 2018-12-23 stsp struct got_repository *repo, struct got_object_id *id, int check_cache)
580 434025f3 2018-09-16 stsp const struct got_error *err = NULL;
581 1785f84a 2018-12-23 stsp struct got_packidx *packidx = NULL;
583 1785f84a 2018-12-23 stsp char *path_packfile;
585 434025f3 2018-09-16 stsp if (check_cache) {
586 1785f84a 2018-12-23 stsp *commit = got_repo_get_cached_commit(repo, id);
587 434025f3 2018-09-16 stsp if (*commit != NULL) {
588 434025f3 2018-09-16 stsp (*commit)->refcnt++;
589 434025f3 2018-09-16 stsp return NULL;
592 434025f3 2018-09-16 stsp *commit = NULL;
594 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
595 e82b1d81 2019-07-27 stsp if (err == NULL) {
596 1785f84a 2018-12-23 stsp struct got_pack *pack = NULL;
598 1785f84a 2018-12-23 stsp err = get_packfile_path(&path_packfile, packidx);
600 1785f84a 2018-12-23 stsp return err;
602 1785f84a 2018-12-23 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
603 434025f3 2018-09-16 stsp if (pack == NULL) {
604 1785f84a 2018-12-23 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
607 434025f3 2018-09-16 stsp return err;
609 a158c901 2018-12-23 stsp err = read_packed_commit_privsep(commit, pack,
610 1785f84a 2018-12-23 stsp packidx, idx, id);
611 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
614 e82b1d81 2019-07-27 stsp err = open_loose_object(&fd, id, repo);
616 e82b1d81 2019-07-27 stsp return err;
617 9f2369b0 2018-12-24 stsp err = read_commit_privsep(commit, fd, repo);
620 434025f3 2018-09-16 stsp if (err == NULL) {
621 434025f3 2018-09-16 stsp (*commit)->refcnt++;
622 1785f84a 2018-12-23 stsp err = got_repo_cache_commit(repo, id, *commit);
625 e32baab7 2018-11-05 stsp return err;
628 e32baab7 2018-11-05 stsp const struct got_error *
629 e32baab7 2018-11-05 stsp got_object_open_as_commit(struct got_commit_object **commit,
630 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object_id *id)
632 e32baab7 2018-11-05 stsp *commit = got_repo_get_cached_commit(repo, id);
633 e32baab7 2018-11-05 stsp if (*commit != NULL) {
634 e32baab7 2018-11-05 stsp (*commit)->refcnt++;
635 e32baab7 2018-11-05 stsp return NULL;
638 1785f84a 2018-12-23 stsp return open_commit(commit, repo, id, 0);
641 e32baab7 2018-11-05 stsp const struct got_error *
642 e32baab7 2018-11-05 stsp got_object_commit_open(struct got_commit_object **commit,
643 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object *obj)
645 1785f84a 2018-12-23 stsp return open_commit(commit, repo, got_object_get_id(obj), 1);
648 434025f3 2018-09-16 stsp const struct got_error *
649 dbc6a6b6 2018-07-12 stsp got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
651 dbc6a6b6 2018-07-12 stsp const struct got_error *err = NULL;
653 dbc6a6b6 2018-07-12 stsp *qid = calloc(1, sizeof(**qid));
654 dbc6a6b6 2018-07-12 stsp if (*qid == NULL)
655 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
657 dbc6a6b6 2018-07-12 stsp (*qid)->id = got_object_id_dup(id);
658 dbc6a6b6 2018-07-12 stsp if ((*qid)->id == NULL) {
659 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
660 fa2f6902 2018-07-23 stsp got_object_qid_free(*qid);
661 dbc6a6b6 2018-07-12 stsp *qid = NULL;
662 dbc6a6b6 2018-07-12 stsp return err;
665 dbc6a6b6 2018-07-12 stsp return NULL;
668 a158c901 2018-12-23 stsp static const struct got_error *
669 13c729f7 2018-12-24 stsp request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
670 13c729f7 2018-12-24 stsp int pack_idx, struct got_object_id *id)
672 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
674 13c729f7 2018-12-24 stsp err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
677 a158c901 2018-12-23 stsp return err;
679 a158c901 2018-12-23 stsp return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
682 71eb0e7f 2018-09-16 stsp static const struct got_error *
683 13c729f7 2018-12-24 stsp read_packed_tree_privsep(struct got_tree_object **tree,
684 13c729f7 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
685 13c729f7 2018-12-24 stsp struct got_object_id *id)
687 13c729f7 2018-12-24 stsp const struct got_error *err = NULL;
689 13c729f7 2018-12-24 stsp if (pack->privsep_child)
690 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
692 13c729f7 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
694 13c729f7 2018-12-24 stsp return err;
696 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
699 13c729f7 2018-12-24 stsp static const struct got_error *
700 9f2369b0 2018-12-24 stsp request_tree(struct got_tree_object **tree, struct got_repository *repo,
703 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
704 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
706 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
708 9f2369b0 2018-12-24 stsp err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
710 9f2369b0 2018-12-24 stsp return err;
712 9f2369b0 2018-12-24 stsp return got_privsep_recv_tree(tree, ibuf);
715 9f2369b0 2018-12-24 stsp const struct got_error *
716 9f2369b0 2018-12-24 stsp read_tree_privsep(struct got_tree_object **tree, int obj_fd,
717 9f2369b0 2018-12-24 stsp struct got_repository *repo)
719 9f2369b0 2018-12-24 stsp int imsg_fds[2];
721 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
723 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
724 9f2369b0 2018-12-24 stsp return request_tree(tree, repo, obj_fd);
726 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
727 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
728 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
730 9f2369b0 2018-12-24 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
731 638f9024 2019-05-13 stsp return got_error_from_errno("socketpair");
733 9f2369b0 2018-12-24 stsp pid = fork();
734 9f2369b0 2018-12-24 stsp if (pid == -1)
735 638f9024 2019-05-13 stsp return got_error_from_errno("fork");
736 9f2369b0 2018-12-24 stsp else if (pid == 0) {
737 9f2369b0 2018-12-24 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
738 9f2369b0 2018-12-24 stsp repo->path);
739 9f2369b0 2018-12-24 stsp /* not reached */
742 3a6ce05a 2019-02-11 stsp if (close(imsg_fds[1]) != 0)
743 638f9024 2019-05-13 stsp return got_error_from_errno("close");
744 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
745 9f2369b0 2018-12-24 stsp imsg_fds[0];
746 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
747 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
748 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
751 9f2369b0 2018-12-24 stsp return request_tree(tree, repo, obj_fd);
754 9f2369b0 2018-12-24 stsp static const struct got_error *
755 13c729f7 2018-12-24 stsp open_tree(struct got_tree_object **tree, struct got_repository *repo,
756 13c729f7 2018-12-24 stsp struct got_object_id *id, int check_cache)
758 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
759 13c729f7 2018-12-24 stsp struct got_packidx *packidx = NULL;
761 13c729f7 2018-12-24 stsp char *path_packfile;
763 71eb0e7f 2018-09-16 stsp if (check_cache) {
764 13c729f7 2018-12-24 stsp *tree = got_repo_get_cached_tree(repo, id);
765 71eb0e7f 2018-09-16 stsp if (*tree != NULL) {
766 71eb0e7f 2018-09-16 stsp (*tree)->refcnt++;
767 71eb0e7f 2018-09-16 stsp return NULL;
770 71eb0e7f 2018-09-16 stsp *tree = NULL;
772 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
773 e82b1d81 2019-07-27 stsp if (err == NULL) {
774 13c729f7 2018-12-24 stsp struct got_pack *pack = NULL;
776 13c729f7 2018-12-24 stsp err = get_packfile_path(&path_packfile, packidx);
778 13c729f7 2018-12-24 stsp return err;
780 13c729f7 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
781 e7885405 2018-09-10 stsp if (pack == NULL) {
782 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
785 e7885405 2018-09-10 stsp return err;
787 13c729f7 2018-12-24 stsp err = read_packed_tree_privsep(tree, pack,
788 13c729f7 2018-12-24 stsp packidx, idx, id);
789 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
792 e82b1d81 2019-07-27 stsp err = open_loose_object(&fd, id, repo);
794 e82b1d81 2019-07-27 stsp return err;
795 9f2369b0 2018-12-24 stsp err = read_tree_privsep(tree, fd, repo);
798 f6be5c30 2018-06-22 stsp if (err == NULL) {
799 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
800 13c729f7 2018-12-24 stsp err = got_repo_cache_tree(repo, id, *tree);
803 776d4d29 2018-06-17 stsp return err;
806 776d4d29 2018-06-17 stsp const struct got_error *
807 776d4d29 2018-06-17 stsp got_object_open_as_tree(struct got_tree_object **tree,
808 776d4d29 2018-06-17 stsp struct got_repository *repo, struct got_object_id *id)
810 e8eb494a 2018-09-16 stsp *tree = got_repo_get_cached_tree(repo, id);
811 e8eb494a 2018-09-16 stsp if (*tree != NULL) {
812 e8eb494a 2018-09-16 stsp (*tree)->refcnt++;
813 e8eb494a 2018-09-16 stsp return NULL;
816 13c729f7 2018-12-24 stsp return open_tree(tree, repo, id, 0);
819 71eb0e7f 2018-09-16 stsp const struct got_error *
820 71eb0e7f 2018-09-16 stsp got_object_tree_open(struct got_tree_object **tree,
821 71eb0e7f 2018-09-16 stsp struct got_repository *repo, struct got_object *obj)
823 13c729f7 2018-12-24 stsp return open_tree(tree, repo, got_object_get_id(obj), 1);
826 883f0469 2018-06-23 stsp const struct got_tree_entries *
827 883f0469 2018-06-23 stsp got_object_tree_get_entries(struct got_tree_object *tree)
829 883f0469 2018-06-23 stsp return &tree->entries;
832 3840f4c9 2018-09-12 stsp static const struct got_error *
833 ac544f8c 2019-01-13 stsp request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
834 ebc55e2d 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
835 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
837 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
838 3840f4c9 2018-09-12 stsp int outfd_child;
839 3840f4c9 2018-09-12 stsp int basefd, accumfd; /* temporary files for delta application */
841 3840f4c9 2018-09-12 stsp basefd = got_opentempfd();
842 3840f4c9 2018-09-12 stsp if (basefd == -1)
843 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
844 3840f4c9 2018-09-12 stsp accumfd = got_opentempfd();
845 3840f4c9 2018-09-12 stsp if (accumfd == -1)
846 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
848 3840f4c9 2018-09-12 stsp outfd_child = dup(outfd);
849 3840f4c9 2018-09-12 stsp if (outfd_child == -1)
850 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
852 ebc55e2d 2018-12-24 stsp err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
854 3840f4c9 2018-09-12 stsp return err;
856 3840f4c9 2018-09-12 stsp err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
857 3840f4c9 2018-09-12 stsp outfd_child);
859 41496140 2019-02-21 stsp close(basefd);
860 41496140 2019-02-21 stsp close(accumfd);
861 3840f4c9 2018-09-12 stsp return err;
864 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
867 3840f4c9 2018-09-12 stsp close(accumfd);
868 3840f4c9 2018-09-12 stsp return err;
871 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
874 3840f4c9 2018-09-12 stsp return err;
876 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen,
877 ebc55e2d 2018-12-24 stsp pack->privsep_child->ibuf);
879 3840f4c9 2018-09-12 stsp return err;
881 3840f4c9 2018-09-12 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
882 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
884 3840f4c9 2018-09-12 stsp return err;
887 ebc55e2d 2018-12-24 stsp static const struct got_error *
888 ac544f8c 2019-01-13 stsp read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
889 ac544f8c 2019-01-13 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
890 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
892 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
894 ebc55e2d 2018-12-24 stsp if (pack->privsep_child == NULL) {
895 ebc55e2d 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
897 ebc55e2d 2018-12-24 stsp return err;
900 ac544f8c 2019-01-13 stsp return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
904 9f2369b0 2018-12-24 stsp static const struct got_error *
905 ac544f8c 2019-01-13 stsp request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
906 ac544f8c 2019-01-13 stsp int infd, struct imsgbuf *ibuf)
908 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
909 9f2369b0 2018-12-24 stsp int outfd_child;
911 9f2369b0 2018-12-24 stsp outfd_child = dup(outfd);
912 9f2369b0 2018-12-24 stsp if (outfd_child == -1)
913 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
915 9f2369b0 2018-12-24 stsp err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
917 9f2369b0 2018-12-24 stsp return err;
919 9f2369b0 2018-12-24 stsp err = got_privsep_send_blob_outfd(ibuf, outfd_child);
921 9f2369b0 2018-12-24 stsp return err;
923 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
925 9f2369b0 2018-12-24 stsp return err;
927 9f2369b0 2018-12-24 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
928 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
930 9f2369b0 2018-12-24 stsp return err;
933 9f2369b0 2018-12-24 stsp static const struct got_error *
934 ac544f8c 2019-01-13 stsp read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
935 9f2369b0 2018-12-24 stsp int outfd, int infd, struct got_repository *repo)
937 9f2369b0 2018-12-24 stsp int imsg_fds[2];
939 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
941 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
942 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
943 ac544f8c 2019-01-13 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
946 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
947 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
948 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
950 9f2369b0 2018-12-24 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
951 638f9024 2019-05-13 stsp return got_error_from_errno("socketpair");
953 9f2369b0 2018-12-24 stsp pid = fork();
954 9f2369b0 2018-12-24 stsp if (pid == -1)
955 638f9024 2019-05-13 stsp return got_error_from_errno("fork");
956 9f2369b0 2018-12-24 stsp else if (pid == 0) {
957 9f2369b0 2018-12-24 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
958 9f2369b0 2018-12-24 stsp repo->path);
959 9f2369b0 2018-12-24 stsp /* not reached */
962 3a6ce05a 2019-02-11 stsp if (close(imsg_fds[1]) != 0)
963 638f9024 2019-05-13 stsp return got_error_from_errno("close");
964 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
965 9f2369b0 2018-12-24 stsp imsg_fds[0];
966 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
967 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
968 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
970 ac544f8c 2019-01-13 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
973 ebc55e2d 2018-12-24 stsp static const struct got_error *
974 ebc55e2d 2018-12-24 stsp open_blob(struct got_blob_object **blob, struct got_repository *repo,
975 ebc55e2d 2018-12-24 stsp struct got_object_id *id, size_t blocksize)
977 ebc55e2d 2018-12-24 stsp const struct got_error *err = NULL;
978 ebc55e2d 2018-12-24 stsp struct got_packidx *packidx = NULL;
980 ebc55e2d 2018-12-24 stsp char *path_packfile;
981 ac544f8c 2019-01-13 stsp uint8_t *outbuf;
983 ebc55e2d 2018-12-24 stsp size_t size, hdrlen;
984 ebc55e2d 2018-12-24 stsp struct stat sb;
986 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
987 4558fcd4 2018-01-14 stsp if (*blob == NULL)
988 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
990 55da3778 2018-09-10 stsp outfd = got_opentempfd();
991 55da3778 2018-09-10 stsp if (outfd == -1)
992 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
994 062ebb78 2018-07-12 stsp (*blob)->read_buf = malloc(blocksize);
995 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
996 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1000 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1001 e82b1d81 2019-07-27 stsp if (err == NULL) {
1002 ebc55e2d 2018-12-24 stsp struct got_pack *pack = NULL;
1004 ebc55e2d 2018-12-24 stsp err = get_packfile_path(&path_packfile, packidx);
1006 ebc55e2d 2018-12-24 stsp goto done;
1008 ebc55e2d 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1009 55da3778 2018-09-10 stsp if (pack == NULL) {
1010 ebc55e2d 2018-12-24 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1013 55da3778 2018-09-10 stsp goto done;
1015 ac544f8c 2019-01-13 stsp err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1016 ac544f8c 2019-01-13 stsp pack, packidx, idx, id);
1017 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1020 e82b1d81 2019-07-27 stsp err = open_loose_object(&infd, id, repo);
1022 e82b1d81 2019-07-27 stsp goto done;
1023 ac544f8c 2019-01-13 stsp err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1027 55da3778 2018-09-10 stsp goto done;
1029 de060dff 2018-12-24 stsp if (hdrlen > size) {
1030 ebc55e2d 2018-12-24 stsp err = got_error(GOT_ERR_BAD_OBJ_HDR);
1031 ebc55e2d 2018-12-24 stsp goto done;
1034 ac544f8c 2019-01-13 stsp if (outbuf) {
1035 3a6ce05a 2019-02-11 stsp if (close(outfd) != 0 && err == NULL)
1036 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1037 ac544f8c 2019-01-13 stsp outfd = -1;
1038 ac544f8c 2019-01-13 stsp (*blob)->f = fmemopen(outbuf, size, "rb");
1039 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1040 638f9024 2019-05-13 stsp err = got_error_from_errno("fmemopen");
1041 ac544f8c 2019-01-13 stsp free(outbuf);
1042 ac544f8c 2019-01-13 stsp goto done;
1044 ac544f8c 2019-01-13 stsp (*blob)->data = outbuf;
1046 ac544f8c 2019-01-13 stsp if (fstat(outfd, &sb) == -1) {
1047 638f9024 2019-05-13 stsp err = got_error_from_errno("fstat");
1048 ac544f8c 2019-01-13 stsp goto done;
1051 ac544f8c 2019-01-13 stsp if (sb.st_size != size) {
1052 ac544f8c 2019-01-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1053 ac544f8c 2019-01-13 stsp goto done;
1056 ac544f8c 2019-01-13 stsp (*blob)->f = fdopen(outfd, "rb");
1057 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1058 638f9024 2019-05-13 stsp err = got_error_from_errno("fdopen");
1059 ac544f8c 2019-01-13 stsp close(outfd);
1060 ac544f8c 2019-01-13 stsp outfd = -1;
1061 ac544f8c 2019-01-13 stsp goto done;
1065 ebc55e2d 2018-12-24 stsp (*blob)->hdrlen = hdrlen;
1066 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
1067 ebc55e2d 2018-12-24 stsp memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1070 55da3778 2018-09-10 stsp if (err) {
1071 55da3778 2018-09-10 stsp if (*blob) {
1072 7baf5860 2019-03-19 stsp got_object_blob_close(*blob);
1073 55da3778 2018-09-10 stsp *blob = NULL;
1074 55da3778 2018-09-10 stsp } else if (outfd != -1)
1075 55da3778 2018-09-10 stsp close(outfd);
1077 a19581a2 2018-06-21 stsp return err;
1080 a19581a2 2018-06-21 stsp const struct got_error *
1081 a19581a2 2018-06-21 stsp got_object_open_as_blob(struct got_blob_object **blob,
1082 a19581a2 2018-06-21 stsp struct got_repository *repo, struct got_object_id *id,
1083 a19581a2 2018-06-21 stsp size_t blocksize)
1085 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, id, blocksize);
1088 ebc55e2d 2018-12-24 stsp const struct got_error *
1089 ebc55e2d 2018-12-24 stsp got_object_blob_open(struct got_blob_object **blob,
1090 ebc55e2d 2018-12-24 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
1092 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1095 fb43ecf1 2019-02-11 stsp const struct got_error *
1096 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
1098 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL;
1099 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
1100 7baf5860 2019-03-19 stsp if (blob->f && fclose(blob->f) != 0)
1101 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1102 ac544f8c 2019-01-13 stsp free(blob->data);
1103 68482ea3 2017-11-27 stsp free(blob);
1104 fb43ecf1 2019-02-11 stsp return err;
1108 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1110 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1114 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
1116 f934cf2c 2018-02-12 stsp return blob->hdrlen;
1119 f934cf2c 2018-02-12 stsp const uint8_t *
1120 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
1122 f934cf2c 2018-02-12 stsp return blob->read_buf;
1125 68482ea3 2017-11-27 stsp const struct got_error *
1126 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1130 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1131 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
1132 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
1133 eb651edf 2018-02-11 stsp *outlenp = n;
1134 35e9ba5d 2018-06-21 stsp return NULL;
1137 35e9ba5d 2018-06-21 stsp const struct got_error *
1138 6fcac457 2018-11-19 stsp got_object_blob_dump_to_file(size_t *total_len, int *nlines,
1139 6c4c42e0 2019-06-24 stsp off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1141 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
1142 b6752625 2018-12-24 stsp size_t n, len, hdrlen;
1143 84451b3e 2018-07-10 stsp const uint8_t *buf;
1145 6c4c42e0 2019-06-24 stsp size_t noffsets = 0;
1146 6c4c42e0 2019-06-24 stsp off_t off = 0;
1148 6c4c42e0 2019-06-24 stsp if (line_offsets)
1149 6c4c42e0 2019-06-24 stsp *line_offsets = NULL;
1150 84451b3e 2018-07-10 stsp if (total_len)
1151 84451b3e 2018-07-10 stsp *total_len = 0;
1152 84451b3e 2018-07-10 stsp if (nlines)
1153 84451b3e 2018-07-10 stsp *nlines = 0;
1155 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1157 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
1159 35e9ba5d 2018-06-21 stsp return err;
1160 35e9ba5d 2018-06-21 stsp if (len == 0)
1162 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
1163 2246482e 2019-06-25 stsp for (i = hdrlen; i < len; i++) {
1164 6c4c42e0 2019-06-24 stsp if (buf[i] != '\n')
1166 6c4c42e0 2019-06-24 stsp if (nlines)
1167 6c4c42e0 2019-06-24 stsp (*nlines)++;
1168 6c4c42e0 2019-06-24 stsp if (line_offsets && nlines && noffsets < *nlines) {
1169 6c4c42e0 2019-06-24 stsp off_t *o = recallocarray(*line_offsets,
1170 6c4c42e0 2019-06-24 stsp noffsets, *nlines, sizeof(**line_offsets));
1171 6c4c42e0 2019-06-24 stsp if (o == NULL) {
1172 6c4c42e0 2019-06-24 stsp free(*line_offsets);
1173 6c4c42e0 2019-06-24 stsp *line_offsets = NULL;
1174 6c4c42e0 2019-06-24 stsp return got_error_from_errno(
1175 6c4c42e0 2019-06-24 stsp "recallocarray");
1177 6c4c42e0 2019-06-24 stsp *line_offsets = o;
1178 6c4c42e0 2019-06-24 stsp noffsets = *nlines;
1180 6c4c42e0 2019-06-24 stsp if (line_offsets && nlines && total_len) {
1181 6c4c42e0 2019-06-24 stsp (*line_offsets)[*nlines - 1] = off;
1182 2246482e 2019-06-25 stsp off = *total_len + i + 1 - got_object_blob_get_hdrlen(blob);
1185 6c4c42e0 2019-06-24 stsp if (total_len)
1186 6c4c42e0 2019-06-24 stsp *total_len += len;
1187 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
1188 454a6b59 2018-12-24 stsp n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1189 b6752625 2018-12-24 stsp if (n != len - hdrlen)
1190 b6752625 2018-12-24 stsp return got_ferror(outfile, GOT_ERR_IO);
1191 35e9ba5d 2018-06-21 stsp hdrlen = 0;
1192 35e9ba5d 2018-06-21 stsp } while (len != 0);
1194 cbe7f848 2019-02-11 stsp if (fflush(outfile) != 0)
1195 638f9024 2019-05-13 stsp return got_error_from_errno("fflush");
1196 35e9ba5d 2018-06-21 stsp rewind(outfile);
1198 776d4d29 2018-06-17 stsp return NULL;
1201 f4a881ce 2018-11-17 stsp static const struct got_error *
1202 268f7291 2018-12-24 stsp request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1203 268f7291 2018-12-24 stsp int pack_idx, struct got_object_id *id)
1205 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
1207 268f7291 2018-12-24 stsp err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1208 268f7291 2018-12-24 stsp pack_idx);
1210 a158c901 2018-12-23 stsp return err;
1212 a158c901 2018-12-23 stsp return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1215 268f7291 2018-12-24 stsp static const struct got_error *
1216 268f7291 2018-12-24 stsp read_packed_tag_privsep(struct got_tag_object **tag,
1217 268f7291 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1218 268f7291 2018-12-24 stsp struct got_object_id *id)
1220 268f7291 2018-12-24 stsp const struct got_error *err = NULL;
1222 268f7291 2018-12-24 stsp if (pack->privsep_child)
1223 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1225 268f7291 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
1227 268f7291 2018-12-24 stsp return err;
1229 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1232 9f2369b0 2018-12-24 stsp static const struct got_error *
1233 9f2369b0 2018-12-24 stsp request_tag(struct got_tag_object **tag, struct got_repository *repo,
1236 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1237 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1239 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1241 9f2369b0 2018-12-24 stsp err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1243 9f2369b0 2018-12-24 stsp return err;
1245 9f2369b0 2018-12-24 stsp return got_privsep_recv_tag(tag, ibuf);
1248 9f2369b0 2018-12-24 stsp static const struct got_error *
1249 9f2369b0 2018-12-24 stsp read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1250 9f2369b0 2018-12-24 stsp struct got_repository *repo)
1252 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1253 9f2369b0 2018-12-24 stsp pid_t pid;
1254 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1256 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1257 9f2369b0 2018-12-24 stsp return request_tag(tag, repo, obj_fd);
1259 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1260 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1261 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1263 9f2369b0 2018-12-24 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1264 638f9024 2019-05-13 stsp return got_error_from_errno("socketpair");
1266 9f2369b0 2018-12-24 stsp pid = fork();
1267 9f2369b0 2018-12-24 stsp if (pid == -1)
1268 638f9024 2019-05-13 stsp return got_error_from_errno("fork");
1269 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1270 9f2369b0 2018-12-24 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1271 9f2369b0 2018-12-24 stsp repo->path);
1272 9f2369b0 2018-12-24 stsp /* not reached */
1275 3a6ce05a 2019-02-11 stsp if (close(imsg_fds[1]) != 0)
1276 638f9024 2019-05-13 stsp return got_error_from_errno("close");
1277 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1278 9f2369b0 2018-12-24 stsp imsg_fds[0];
1279 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1280 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1281 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1283 9f2369b0 2018-12-24 stsp return request_tag(tag, repo, obj_fd);
1286 a158c901 2018-12-23 stsp static const struct got_error *
1287 268f7291 2018-12-24 stsp open_tag(struct got_tag_object **tag, struct got_repository *repo,
1288 268f7291 2018-12-24 stsp struct got_object_id *id, int check_cache)
1290 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1291 268f7291 2018-12-24 stsp struct got_packidx *packidx = NULL;
1293 268f7291 2018-12-24 stsp char *path_packfile;
1295 f4a881ce 2018-11-17 stsp if (check_cache) {
1296 268f7291 2018-12-24 stsp *tag = got_repo_get_cached_tag(repo, id);
1297 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1298 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1299 f4a881ce 2018-11-17 stsp return NULL;
1302 f4a881ce 2018-11-17 stsp *tag = NULL;
1304 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1305 e82b1d81 2019-07-27 stsp if (err == NULL) {
1306 268f7291 2018-12-24 stsp struct got_pack *pack = NULL;
1308 268f7291 2018-12-24 stsp err = get_packfile_path(&path_packfile, packidx);
1310 268f7291 2018-12-24 stsp return err;
1312 268f7291 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1313 f4a881ce 2018-11-17 stsp if (pack == NULL) {
1314 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1317 f4a881ce 2018-11-17 stsp return err;
1319 e82b1d81 2019-07-27 stsp err = read_packed_tag_privsep(tag, pack,
1320 e82b1d81 2019-07-27 stsp packidx, idx, id);
1321 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1324 e82b1d81 2019-07-27 stsp err = open_loose_object(&fd, id, repo);
1326 e82b1d81 2019-07-27 stsp return err;
1327 9f2369b0 2018-12-24 stsp err = read_tag_privsep(tag, fd, repo);
1330 f4a881ce 2018-11-17 stsp if (err == NULL) {
1331 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1332 268f7291 2018-12-24 stsp err = got_repo_cache_tag(repo, id, *tag);
1335 f4a881ce 2018-11-17 stsp return err;
1338 f4a881ce 2018-11-17 stsp const struct got_error *
1339 f4a881ce 2018-11-17 stsp got_object_open_as_tag(struct got_tag_object **tag,
1340 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object_id *id)
1342 f4a881ce 2018-11-17 stsp *tag = got_repo_get_cached_tag(repo, id);
1343 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1344 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1345 f4a881ce 2018-11-17 stsp return NULL;
1348 268f7291 2018-12-24 stsp return open_tag(tag, repo, id, 0);
1351 f4a881ce 2018-11-17 stsp const struct got_error *
1352 f4a881ce 2018-11-17 stsp got_object_tag_open(struct got_tag_object **tag,
1353 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object *obj)
1355 268f7291 2018-12-24 stsp return open_tag(tag, repo, got_object_get_id(obj), 1);
1359 0bd18d37 2019-02-01 stsp got_object_tag_get_object_type(struct got_tag_object *tag)
1361 0bd18d37 2019-02-01 stsp return tag->obj_type;
1364 0bd18d37 2019-02-01 stsp struct got_object_id *
1365 0bd18d37 2019-02-01 stsp got_object_tag_get_object_id(struct got_tag_object *tag)
1367 0bd18d37 2019-02-01 stsp return &tag->id;
1370 776d4d29 2018-06-17 stsp static struct got_tree_entry *
1371 65a9bbe9 2018-09-15 stsp find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1373 776d4d29 2018-06-17 stsp struct got_tree_entry *te;
1375 63da309a 2018-11-07 stsp /* Note that tree entries are sorted in strncmp() order. */
1376 883f0469 2018-06-23 stsp SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
1377 63da309a 2018-11-07 stsp int cmp = strncmp(te->name, name, len);
1378 63da309a 2018-11-07 stsp if (cmp < 0)
1380 63da309a 2018-11-07 stsp if (cmp > 0)
1382 63da309a 2018-11-07 stsp if (te->name[len] == '\0')
1383 776d4d29 2018-06-17 stsp return te;
1385 eb651edf 2018-02-11 stsp return NULL;
1388 a129376b 2019-03-28 stsp const struct got_tree_entry *
1389 a129376b 2019-03-28 stsp got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1391 a129376b 2019-03-28 stsp return find_entry_by_name(tree, name, strlen(name));
1394 776d4d29 2018-06-17 stsp const struct got_error *
1395 27d434c2 2018-09-15 stsp got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1396 776d4d29 2018-06-17 stsp struct got_object_id *commit_id, const char *path)
1398 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
1399 776d4d29 2018-06-17 stsp struct got_commit_object *commit = NULL;
1400 776d4d29 2018-06-17 stsp struct got_tree_object *tree = NULL;
1401 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
1402 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1403 b7cd37e5 2018-11-18 stsp size_t seglen;
1405 27d434c2 2018-09-15 stsp *id = NULL;
1407 776d4d29 2018-06-17 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
1409 776d4d29 2018-06-17 stsp goto done;
1411 776d4d29 2018-06-17 stsp /* Handle opening of root of commit's tree. */
1412 5e54fb30 2019-05-31 stsp if (got_path_is_root_dir(path)) {
1413 27d434c2 2018-09-15 stsp *id = got_object_id_dup(commit->tree_id);
1414 27d434c2 2018-09-15 stsp if (*id == NULL)
1415 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
1416 ca008b32 2018-07-22 stsp goto done;
1419 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1421 776d4d29 2018-06-17 stsp goto done;
1424 5e54fb30 2019-05-31 stsp while (s[0] == '/')
1427 65a9bbe9 2018-09-15 stsp seglen = 0;
1428 b7cd37e5 2018-11-18 stsp while (*s) {
1429 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
1431 776d4d29 2018-06-17 stsp if (*s != '/') {
1438 65a9bbe9 2018-09-15 stsp te = find_entry_by_name(tree, seg, seglen);
1439 db37e2c0 2018-06-21 stsp if (te == NULL) {
1440 d1451975 2018-11-11 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1441 776d4d29 2018-06-17 stsp goto done;
1444 b7cd37e5 2018-11-18 stsp if (*s == '\0')
1447 776d4d29 2018-06-17 stsp seg = s + 1;
1448 65a9bbe9 2018-09-15 stsp seglen = 0;
1451 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
1453 db37e2c0 2018-06-21 stsp te = NULL;
1455 776d4d29 2018-06-17 stsp goto done;
1456 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
1457 776d4d29 2018-06-17 stsp tree = next_tree;
1462 27d434c2 2018-09-15 stsp *id = got_object_id_dup(te->id);
1463 27d434c2 2018-09-15 stsp if (*id == NULL)
1464 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
1466 d1451975 2018-11-11 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1468 776d4d29 2018-06-17 stsp if (commit)
1469 776d4d29 2018-06-17 stsp got_object_commit_close(commit);
1471 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
1472 776d4d29 2018-06-17 stsp return err;
1475 07862c20 2018-09-15 stsp const struct got_error *
1476 07862c20 2018-09-15 stsp got_object_tree_path_changed(int *changed,
1477 07862c20 2018-09-15 stsp struct got_tree_object *tree01, struct got_tree_object *tree02,
1478 07862c20 2018-09-15 stsp const char *path, struct got_repository *repo)
1480 07862c20 2018-09-15 stsp const struct got_error *err = NULL;
1481 07862c20 2018-09-15 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1482 07862c20 2018-09-15 stsp struct got_tree_entry *te1 = NULL, *te2 = NULL;
1483 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1484 3b7f9878 2018-11-18 stsp size_t seglen;
1486 07862c20 2018-09-15 stsp *changed = 0;
1488 07862c20 2018-09-15 stsp /* We are expecting an absolute in-repository path. */
1489 07862c20 2018-09-15 stsp if (path[0] != '/')
1490 07862c20 2018-09-15 stsp return got_error(GOT_ERR_NOT_ABSPATH);
1492 07862c20 2018-09-15 stsp /* We not do support comparing the root path. */
1493 07862c20 2018-09-15 stsp if (path[1] == '\0')
1494 07862c20 2018-09-15 stsp return got_error(GOT_ERR_BAD_PATH);
1496 07862c20 2018-09-15 stsp tree1 = tree01;
1497 07862c20 2018-09-15 stsp tree2 = tree02;
1499 07862c20 2018-09-15 stsp s++; /* skip leading '/' */
1501 65a9bbe9 2018-09-15 stsp seglen = 0;
1502 3b7f9878 2018-11-18 stsp while (*s) {
1503 07862c20 2018-09-15 stsp struct got_tree_object *next_tree1, *next_tree2;
1505 07862c20 2018-09-15 stsp if (*s != '/') {
1512 65a9bbe9 2018-09-15 stsp te1 = find_entry_by_name(tree1, seg, seglen);
1513 07862c20 2018-09-15 stsp if (te1 == NULL) {
1514 07862c20 2018-09-15 stsp err = got_error(GOT_ERR_NO_OBJ);
1515 07862c20 2018-09-15 stsp goto done;
1518 65a9bbe9 2018-09-15 stsp te2 = find_entry_by_name(tree2, seg, seglen);
1519 07862c20 2018-09-15 stsp if (te2 == NULL) {
1520 07862c20 2018-09-15 stsp *changed = 1;
1521 07862c20 2018-09-15 stsp goto done;
1524 07862c20 2018-09-15 stsp if (te1->mode != te2->mode) {
1525 07862c20 2018-09-15 stsp *changed = 1;
1526 07862c20 2018-09-15 stsp goto done;
1529 07862c20 2018-09-15 stsp if (got_object_id_cmp(te1->id, te2->id) == 0) {
1530 07862c20 2018-09-15 stsp *changed = 0;
1531 07862c20 2018-09-15 stsp goto done;
1534 3b7f9878 2018-11-18 stsp if (*s == '\0') { /* final path element */
1535 07862c20 2018-09-15 stsp *changed = 1;
1536 07862c20 2018-09-15 stsp goto done;
1539 07862c20 2018-09-15 stsp seg = s + 1;
1541 65a9bbe9 2018-09-15 stsp seglen = 0;
1543 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree1, repo,
1545 07862c20 2018-09-15 stsp te1 = NULL;
1547 07862c20 2018-09-15 stsp goto done;
1548 a31cea73 2018-09-15 stsp if (tree1 != tree01)
1549 a31cea73 2018-09-15 stsp got_object_tree_close(tree1);
1550 07862c20 2018-09-15 stsp tree1 = next_tree1;
1552 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree2, repo,
1554 07862c20 2018-09-15 stsp te2 = NULL;
1556 07862c20 2018-09-15 stsp goto done;
1557 a31cea73 2018-09-15 stsp if (tree2 != tree02)
1558 a31cea73 2018-09-15 stsp got_object_tree_close(tree2);
1559 07862c20 2018-09-15 stsp tree2 = next_tree2;
1563 a31cea73 2018-09-15 stsp if (tree1 && tree1 != tree01)
1564 07862c20 2018-09-15 stsp got_object_tree_close(tree1);
1565 a31cea73 2018-09-15 stsp if (tree2 && tree2 != tree02)
1566 07862c20 2018-09-15 stsp got_object_tree_close(tree2);
1567 77880158 2018-11-04 stsp return err;
1570 ed175427 2019-05-09 stsp const struct got_error *
1571 ed175427 2019-05-09 stsp got_object_tree_entry_dup(struct got_tree_entry **new_te,
1572 ed175427 2019-05-09 stsp struct got_tree_entry *te)
1574 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
1576 ed175427 2019-05-09 stsp *new_te = calloc(1, sizeof(**new_te));
1577 ed175427 2019-05-09 stsp if (*new_te == NULL)
1578 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1580 ed175427 2019-05-09 stsp (*new_te)->mode = te->mode;
1581 ed175427 2019-05-09 stsp (*new_te)->name = strdup(te->name);
1582 ed175427 2019-05-09 stsp if ((*new_te)->name == NULL) {
1583 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1584 8c4eabf2 2019-05-10 stsp goto done;
1587 ed175427 2019-05-09 stsp (*new_te)->id = got_object_id_dup(te->id);
1588 ed175427 2019-05-09 stsp if ((*new_te)->id == NULL) {
1589 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
1590 8c4eabf2 2019-05-10 stsp goto done;
1593 8c4eabf2 2019-05-10 stsp if (err) {
1594 8c4eabf2 2019-05-10 stsp got_object_tree_entry_close(*new_te);
1595 8c4eabf2 2019-05-10 stsp *new_te = NULL;
1597 8c4eabf2 2019-05-10 stsp return err;