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 638f9024 2019-05-13 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 59790a32 2018-09-15 stsp err = open_packed_object(obj, id, repo);
426 59790a32 2018-09-15 stsp if (err && err->code != GOT_ERR_NO_OBJ)
427 59790a32 2018-09-15 stsp return err;
428 59790a32 2018-09-15 stsp if (*obj) {
429 59790a32 2018-09-15 stsp (*obj)->refcnt++;
430 59790a32 2018-09-15 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 59790a32 2018-09-15 stsp if (errno == ENOENT)
440 91a3d81f 2018-11-11 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 1785f84a 2018-12-23 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
595 1785f84a 2018-12-23 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 1785f84a 2018-12-23 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
614 1785f84a 2018-12-23 stsp err = open_loose_object(&fd, id, repo);
616 434025f3 2018-09-16 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 13c729f7 2018-12-24 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
773 13c729f7 2018-12-24 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 13c729f7 2018-12-24 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 13c729f7 2018-12-24 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
792 13c729f7 2018-12-24 stsp err = open_loose_object(&fd, id, repo);
794 e0ab43e7 2018-03-16 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 ebc55e2d 2018-12-24 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1001 ebc55e2d 2018-12-24 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 ebc55e2d 2018-12-24 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1020 ebc55e2d 2018-12-24 stsp err = open_loose_object(&infd, id, repo);
1022 c7254d79 2018-04-24 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 84451b3e 2018-07-10 stsp 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;
1146 84451b3e 2018-07-10 stsp if (total_len)
1147 84451b3e 2018-07-10 stsp *total_len = 0;
1148 84451b3e 2018-07-10 stsp if (nlines)
1149 84451b3e 2018-07-10 stsp *nlines = 0;
1151 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1153 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
1155 35e9ba5d 2018-06-21 stsp return err;
1156 35e9ba5d 2018-06-21 stsp if (len == 0)
1158 84451b3e 2018-07-10 stsp if (total_len)
1159 84451b3e 2018-07-10 stsp *total_len += len;
1160 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
1161 84451b3e 2018-07-10 stsp if (nlines) {
1162 84451b3e 2018-07-10 stsp for (i = 0; i < len; i++) {
1163 84451b3e 2018-07-10 stsp if (buf[i] == '\n')
1164 84451b3e 2018-07-10 stsp (*nlines)++;
1167 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
1168 454a6b59 2018-12-24 stsp n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1169 b6752625 2018-12-24 stsp if (n != len - hdrlen)
1170 b6752625 2018-12-24 stsp return got_ferror(outfile, GOT_ERR_IO);
1171 35e9ba5d 2018-06-21 stsp hdrlen = 0;
1172 35e9ba5d 2018-06-21 stsp } while (len != 0);
1174 cbe7f848 2019-02-11 stsp if (fflush(outfile) != 0)
1175 638f9024 2019-05-13 stsp return got_error_from_errno("fflush");
1176 35e9ba5d 2018-06-21 stsp rewind(outfile);
1178 776d4d29 2018-06-17 stsp return NULL;
1181 f4a881ce 2018-11-17 stsp static const struct got_error *
1182 268f7291 2018-12-24 stsp request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1183 268f7291 2018-12-24 stsp int pack_idx, struct got_object_id *id)
1185 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
1187 268f7291 2018-12-24 stsp err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1188 268f7291 2018-12-24 stsp pack_idx);
1190 a158c901 2018-12-23 stsp return err;
1192 a158c901 2018-12-23 stsp return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1195 268f7291 2018-12-24 stsp static const struct got_error *
1196 268f7291 2018-12-24 stsp read_packed_tag_privsep(struct got_tag_object **tag,
1197 268f7291 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1198 268f7291 2018-12-24 stsp struct got_object_id *id)
1200 268f7291 2018-12-24 stsp const struct got_error *err = NULL;
1202 268f7291 2018-12-24 stsp if (pack->privsep_child)
1203 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1205 268f7291 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
1207 268f7291 2018-12-24 stsp return err;
1209 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1212 9f2369b0 2018-12-24 stsp static const struct got_error *
1213 9f2369b0 2018-12-24 stsp request_tag(struct got_tag_object **tag, struct got_repository *repo,
1216 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1217 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1219 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1221 9f2369b0 2018-12-24 stsp err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1223 9f2369b0 2018-12-24 stsp return err;
1225 9f2369b0 2018-12-24 stsp return got_privsep_recv_tag(tag, ibuf);
1228 9f2369b0 2018-12-24 stsp static const struct got_error *
1229 9f2369b0 2018-12-24 stsp read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1230 9f2369b0 2018-12-24 stsp struct got_repository *repo)
1232 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1233 9f2369b0 2018-12-24 stsp pid_t pid;
1234 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1236 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1237 9f2369b0 2018-12-24 stsp return request_tag(tag, repo, obj_fd);
1239 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1240 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1241 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1243 9f2369b0 2018-12-24 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1244 638f9024 2019-05-13 stsp return got_error_from_errno("socketpair");
1246 9f2369b0 2018-12-24 stsp pid = fork();
1247 9f2369b0 2018-12-24 stsp if (pid == -1)
1248 638f9024 2019-05-13 stsp return got_error_from_errno("fork");
1249 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1250 9f2369b0 2018-12-24 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1251 9f2369b0 2018-12-24 stsp repo->path);
1252 9f2369b0 2018-12-24 stsp /* not reached */
1255 3a6ce05a 2019-02-11 stsp if (close(imsg_fds[1]) != 0)
1256 638f9024 2019-05-13 stsp return got_error_from_errno("close");
1257 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1258 9f2369b0 2018-12-24 stsp imsg_fds[0];
1259 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1260 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1261 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1263 9f2369b0 2018-12-24 stsp return request_tag(tag, repo, obj_fd);
1266 a158c901 2018-12-23 stsp static const struct got_error *
1267 268f7291 2018-12-24 stsp open_tag(struct got_tag_object **tag, struct got_repository *repo,
1268 268f7291 2018-12-24 stsp struct got_object_id *id, int check_cache)
1270 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1271 268f7291 2018-12-24 stsp struct got_packidx *packidx = NULL;
1273 268f7291 2018-12-24 stsp char *path_packfile;
1275 f4a881ce 2018-11-17 stsp if (check_cache) {
1276 268f7291 2018-12-24 stsp *tag = got_repo_get_cached_tag(repo, id);
1277 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1278 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1279 f4a881ce 2018-11-17 stsp return NULL;
1282 f4a881ce 2018-11-17 stsp *tag = NULL;
1284 268f7291 2018-12-24 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1285 268f7291 2018-12-24 stsp if (err == NULL) {
1286 268f7291 2018-12-24 stsp struct got_pack *pack = NULL;
1288 268f7291 2018-12-24 stsp err = get_packfile_path(&path_packfile, packidx);
1290 268f7291 2018-12-24 stsp return err;
1292 268f7291 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1293 f4a881ce 2018-11-17 stsp if (pack == NULL) {
1294 268f7291 2018-12-24 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1297 f4a881ce 2018-11-17 stsp return err;
1299 268f7291 2018-12-24 stsp err = read_packed_tag_privsep(tag, pack,
1300 268f7291 2018-12-24 stsp packidx, idx, id);
1301 268f7291 2018-12-24 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1304 268f7291 2018-12-24 stsp err = open_loose_object(&fd, id, repo);
1306 f4a881ce 2018-11-17 stsp return err;
1307 9f2369b0 2018-12-24 stsp err = read_tag_privsep(tag, fd, repo);
1310 f4a881ce 2018-11-17 stsp if (err == NULL) {
1311 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1312 268f7291 2018-12-24 stsp err = got_repo_cache_tag(repo, id, *tag);
1315 f4a881ce 2018-11-17 stsp return err;
1318 f4a881ce 2018-11-17 stsp const struct got_error *
1319 f4a881ce 2018-11-17 stsp got_object_open_as_tag(struct got_tag_object **tag,
1320 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object_id *id)
1322 f4a881ce 2018-11-17 stsp *tag = got_repo_get_cached_tag(repo, id);
1323 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1324 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1325 f4a881ce 2018-11-17 stsp return NULL;
1328 268f7291 2018-12-24 stsp return open_tag(tag, repo, id, 0);
1331 f4a881ce 2018-11-17 stsp const struct got_error *
1332 f4a881ce 2018-11-17 stsp got_object_tag_open(struct got_tag_object **tag,
1333 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object *obj)
1335 268f7291 2018-12-24 stsp return open_tag(tag, repo, got_object_get_id(obj), 1);
1339 0bd18d37 2019-02-01 stsp got_object_tag_get_object_type(struct got_tag_object *tag)
1341 0bd18d37 2019-02-01 stsp return tag->obj_type;
1344 0bd18d37 2019-02-01 stsp struct got_object_id *
1345 0bd18d37 2019-02-01 stsp got_object_tag_get_object_id(struct got_tag_object *tag)
1347 0bd18d37 2019-02-01 stsp return &tag->id;
1350 776d4d29 2018-06-17 stsp static struct got_tree_entry *
1351 65a9bbe9 2018-09-15 stsp find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1353 776d4d29 2018-06-17 stsp struct got_tree_entry *te;
1355 63da309a 2018-11-07 stsp /* Note that tree entries are sorted in strncmp() order. */
1356 883f0469 2018-06-23 stsp SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
1357 63da309a 2018-11-07 stsp int cmp = strncmp(te->name, name, len);
1358 63da309a 2018-11-07 stsp if (cmp < 0)
1360 63da309a 2018-11-07 stsp if (cmp > 0)
1362 63da309a 2018-11-07 stsp if (te->name[len] == '\0')
1363 776d4d29 2018-06-17 stsp return te;
1365 eb651edf 2018-02-11 stsp return NULL;
1368 a129376b 2019-03-28 stsp const struct got_tree_entry *
1369 a129376b 2019-03-28 stsp got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1371 a129376b 2019-03-28 stsp return find_entry_by_name(tree, name, strlen(name));
1374 776d4d29 2018-06-17 stsp const struct got_error *
1375 27d434c2 2018-09-15 stsp got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1376 776d4d29 2018-06-17 stsp struct got_object_id *commit_id, const char *path)
1378 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
1379 776d4d29 2018-06-17 stsp struct got_commit_object *commit = NULL;
1380 776d4d29 2018-06-17 stsp struct got_tree_object *tree = NULL;
1381 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
1382 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1383 b7cd37e5 2018-11-18 stsp size_t seglen;
1385 27d434c2 2018-09-15 stsp *id = NULL;
1387 776d4d29 2018-06-17 stsp /* We are expecting an absolute in-repository path. */
1388 776d4d29 2018-06-17 stsp if (path[0] != '/')
1389 776d4d29 2018-06-17 stsp return got_error(GOT_ERR_NOT_ABSPATH);
1391 776d4d29 2018-06-17 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
1393 776d4d29 2018-06-17 stsp goto done;
1395 776d4d29 2018-06-17 stsp /* Handle opening of root of commit's tree. */
1396 776d4d29 2018-06-17 stsp if (path[1] == '\0') {
1397 27d434c2 2018-09-15 stsp *id = got_object_id_dup(commit->tree_id);
1398 27d434c2 2018-09-15 stsp if (*id == NULL)
1399 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
1400 ca008b32 2018-07-22 stsp goto done;
1403 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1405 776d4d29 2018-06-17 stsp goto done;
1408 776d4d29 2018-06-17 stsp s++; /* skip leading '/' */
1410 65a9bbe9 2018-09-15 stsp seglen = 0;
1411 b7cd37e5 2018-11-18 stsp while (*s) {
1412 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
1414 776d4d29 2018-06-17 stsp if (*s != '/') {
1421 65a9bbe9 2018-09-15 stsp te = find_entry_by_name(tree, seg, seglen);
1422 db37e2c0 2018-06-21 stsp if (te == NULL) {
1423 d1451975 2018-11-11 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1424 776d4d29 2018-06-17 stsp goto done;
1427 b7cd37e5 2018-11-18 stsp if (*s == '\0')
1430 776d4d29 2018-06-17 stsp seg = s + 1;
1431 65a9bbe9 2018-09-15 stsp seglen = 0;
1434 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
1436 db37e2c0 2018-06-21 stsp te = NULL;
1438 776d4d29 2018-06-17 stsp goto done;
1439 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
1440 776d4d29 2018-06-17 stsp tree = next_tree;
1445 27d434c2 2018-09-15 stsp *id = got_object_id_dup(te->id);
1446 27d434c2 2018-09-15 stsp if (*id == NULL)
1447 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
1449 d1451975 2018-11-11 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1451 776d4d29 2018-06-17 stsp if (commit)
1452 776d4d29 2018-06-17 stsp got_object_commit_close(commit);
1454 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
1455 776d4d29 2018-06-17 stsp return err;
1458 07862c20 2018-09-15 stsp const struct got_error *
1459 07862c20 2018-09-15 stsp got_object_tree_path_changed(int *changed,
1460 07862c20 2018-09-15 stsp struct got_tree_object *tree01, struct got_tree_object *tree02,
1461 07862c20 2018-09-15 stsp const char *path, struct got_repository *repo)
1463 07862c20 2018-09-15 stsp const struct got_error *err = NULL;
1464 07862c20 2018-09-15 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1465 07862c20 2018-09-15 stsp struct got_tree_entry *te1 = NULL, *te2 = NULL;
1466 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1467 3b7f9878 2018-11-18 stsp size_t seglen;
1469 07862c20 2018-09-15 stsp *changed = 0;
1471 07862c20 2018-09-15 stsp /* We are expecting an absolute in-repository path. */
1472 07862c20 2018-09-15 stsp if (path[0] != '/')
1473 07862c20 2018-09-15 stsp return got_error(GOT_ERR_NOT_ABSPATH);
1475 07862c20 2018-09-15 stsp /* We not do support comparing the root path. */
1476 07862c20 2018-09-15 stsp if (path[1] == '\0')
1477 07862c20 2018-09-15 stsp return got_error(GOT_ERR_BAD_PATH);
1479 07862c20 2018-09-15 stsp tree1 = tree01;
1480 07862c20 2018-09-15 stsp tree2 = tree02;
1482 07862c20 2018-09-15 stsp s++; /* skip leading '/' */
1484 65a9bbe9 2018-09-15 stsp seglen = 0;
1485 3b7f9878 2018-11-18 stsp while (*s) {
1486 07862c20 2018-09-15 stsp struct got_tree_object *next_tree1, *next_tree2;
1488 07862c20 2018-09-15 stsp if (*s != '/') {
1495 65a9bbe9 2018-09-15 stsp te1 = find_entry_by_name(tree1, seg, seglen);
1496 07862c20 2018-09-15 stsp if (te1 == NULL) {
1497 07862c20 2018-09-15 stsp err = got_error(GOT_ERR_NO_OBJ);
1498 07862c20 2018-09-15 stsp goto done;
1501 65a9bbe9 2018-09-15 stsp te2 = find_entry_by_name(tree2, seg, seglen);
1502 07862c20 2018-09-15 stsp if (te2 == NULL) {
1503 07862c20 2018-09-15 stsp *changed = 1;
1504 07862c20 2018-09-15 stsp goto done;
1507 07862c20 2018-09-15 stsp if (te1->mode != te2->mode) {
1508 07862c20 2018-09-15 stsp *changed = 1;
1509 07862c20 2018-09-15 stsp goto done;
1512 07862c20 2018-09-15 stsp if (got_object_id_cmp(te1->id, te2->id) == 0) {
1513 07862c20 2018-09-15 stsp *changed = 0;
1514 07862c20 2018-09-15 stsp goto done;
1517 3b7f9878 2018-11-18 stsp if (*s == '\0') { /* final path element */
1518 07862c20 2018-09-15 stsp *changed = 1;
1519 07862c20 2018-09-15 stsp goto done;
1522 07862c20 2018-09-15 stsp seg = s + 1;
1524 65a9bbe9 2018-09-15 stsp seglen = 0;
1526 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree1, repo,
1528 07862c20 2018-09-15 stsp te1 = NULL;
1530 07862c20 2018-09-15 stsp goto done;
1531 a31cea73 2018-09-15 stsp if (tree1 != tree01)
1532 a31cea73 2018-09-15 stsp got_object_tree_close(tree1);
1533 07862c20 2018-09-15 stsp tree1 = next_tree1;
1535 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree2, repo,
1537 07862c20 2018-09-15 stsp te2 = NULL;
1539 07862c20 2018-09-15 stsp goto done;
1540 a31cea73 2018-09-15 stsp if (tree2 != tree02)
1541 a31cea73 2018-09-15 stsp got_object_tree_close(tree2);
1542 07862c20 2018-09-15 stsp tree2 = next_tree2;
1546 a31cea73 2018-09-15 stsp if (tree1 && tree1 != tree01)
1547 07862c20 2018-09-15 stsp got_object_tree_close(tree1);
1548 a31cea73 2018-09-15 stsp if (tree2 && tree2 != tree02)
1549 07862c20 2018-09-15 stsp got_object_tree_close(tree2);
1550 77880158 2018-11-04 stsp return err;
1553 ed175427 2019-05-09 stsp const struct got_error *
1554 ed175427 2019-05-09 stsp got_object_tree_entry_dup(struct got_tree_entry **new_te,
1555 ed175427 2019-05-09 stsp struct got_tree_entry *te)
1557 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
1559 ed175427 2019-05-09 stsp *new_te = calloc(1, sizeof(**new_te));
1560 ed175427 2019-05-09 stsp if (*new_te == NULL)
1561 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1563 ed175427 2019-05-09 stsp (*new_te)->mode = te->mode;
1564 ed175427 2019-05-09 stsp (*new_te)->name = strdup(te->name);
1565 ed175427 2019-05-09 stsp if ((*new_te)->name == NULL) {
1566 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1567 8c4eabf2 2019-05-10 stsp goto done;
1570 ed175427 2019-05-09 stsp (*new_te)->id = got_object_id_dup(te->id);
1571 ed175427 2019-05-09 stsp if ((*new_te)->id == NULL) {
1572 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
1573 8c4eabf2 2019-05-10 stsp goto done;
1576 8c4eabf2 2019-05-10 stsp if (err) {
1577 8c4eabf2 2019-05-10 stsp got_object_tree_entry_close(*new_te);
1578 8c4eabf2 2019-05-10 stsp *new_te = NULL;
1580 8c4eabf2 2019-05-10 stsp return err;