2 a1fd68d8 2018-01-12 stsp * Copyright (c) 2018 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>
25 a1fd68d8 2018-01-12 stsp #include <errno.h>
26 2178c42e 2018-04-22 stsp #include <fcntl.h>
27 d71d75ad 2017-11-05 stsp #include <stdio.h>
28 ab9a70b2 2017-11-06 stsp #include <stdlib.h>
29 ab9a70b2 2017-11-06 stsp #include <string.h>
30 2178c42e 2018-04-22 stsp #include <stdint.h>
31 d71d75ad 2017-11-05 stsp #include <sha1.h>
32 ab9a70b2 2017-11-06 stsp #include <zlib.h>
33 ab9a70b2 2017-11-06 stsp #include <ctype.h>
34 ab9a70b2 2017-11-06 stsp #include <limits.h>
35 2178c42e 2018-04-22 stsp #include <imsg.h>
36 788c352e 2018-06-16 stsp #include <time.h>
38 ab9a70b2 2017-11-06 stsp #include "got_error.h"
39 d71d75ad 2017-11-05 stsp #include "got_object.h"
40 ab9a70b2 2017-11-06 stsp #include "got_repository.h"
41 511a516b 2018-05-19 stsp #include "got_opentemp.h"
43 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
44 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
45 2178c42e 2018-04-22 stsp #include "got_lib_path.h"
46 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
47 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
48 2178c42e 2018-04-22 stsp #include "got_lib_privsep.h"
49 6bef87be 2018-09-11 stsp #include "got_lib_object_idcache.h"
50 6bef87be 2018-09-11 stsp #include "got_lib_object_cache.h"
51 ad242220 2018-09-08 stsp #include "got_lib_object_parse.h"
52 15a94983 2018-12-23 stsp #include "got_lib_pack.h"
53 7bb0daa1 2018-06-21 stsp #include "got_lib_repository.h"
56 ab9a70b2 2017-11-06 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 8bf5b3c9 2018-03-17 stsp struct got_object_id *
60 8bf5b3c9 2018-03-17 stsp got_object_id_dup(struct got_object_id *id1)
62 8bf5b3c9 2018-03-17 stsp struct got_object_id *id2;
64 8bf5b3c9 2018-03-17 stsp id2 = malloc(sizeof(*id2));
65 8bf5b3c9 2018-03-17 stsp if (id2 == NULL)
66 8bf5b3c9 2018-03-17 stsp return NULL;
67 8bf5b3c9 2018-03-17 stsp memcpy(id2, id1, sizeof(*id2));
71 3235492e 2018-04-01 stsp struct got_object_id *
72 3235492e 2018-04-01 stsp got_object_get_id(struct got_object *obj)
74 6402fb3c 2018-09-15 stsp return &obj->id;
77 bacc9935 2018-05-20 stsp const struct got_error *
78 bacc9935 2018-05-20 stsp got_object_get_id_str(char **outbuf, struct got_object *obj)
80 bacc9935 2018-05-20 stsp return got_object_id_str(outbuf, &obj->id);
83 15a94983 2018-12-23 stsp const struct got_error *
84 15a94983 2018-12-23 stsp got_object_get_type(int *type, struct got_repository *repo,
85 15a94983 2018-12-23 stsp struct got_object_id *id)
87 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
88 15a94983 2018-12-23 stsp struct got_object *obj;
90 15a94983 2018-12-23 stsp err = got_object_open(&obj, repo, id);
94 b107e67f 2018-01-19 stsp switch (obj->type) {
95 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_COMMIT:
96 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_TREE:
97 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_BLOB:
98 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
99 15a94983 2018-12-23 stsp *type = obj->type;
102 15a94983 2018-12-23 stsp err = got_error(GOT_ERR_OBJ_TYPE);
106 15a94983 2018-12-23 stsp got_object_close(obj);
107 15a94983 2018-12-23 stsp return err;
110 ab9a70b2 2017-11-06 stsp static const struct got_error *
111 4558fcd4 2018-01-14 stsp object_path(char **path, struct got_object_id *id, struct got_repository *repo)
113 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
114 7a132809 2018-07-23 stsp char *hex = NULL;
115 d1cda826 2017-11-06 stsp char *path_objects = got_repo_get_path_objects(repo);
117 e6b1056e 2018-04-22 stsp *path = NULL;
119 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
120 0a585a0d 2018-03-17 stsp return got_error_from_errno();
122 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
126 d1cda826 2017-11-06 stsp if (asprintf(path, "%s/%.2x/%s", path_objects,
127 d1cda826 2017-11-06 stsp id->sha1[0], hex + 2) == -1)
128 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
132 d1cda826 2017-11-06 stsp free(path_objects);
133 d1cda826 2017-11-06 stsp return err;
136 4ee4114f 2018-01-23 stsp static const struct got_error *
137 4796fb13 2018-12-23 stsp open_loose_object(int *fd, struct got_object_id *id,
138 4796fb13 2018-12-23 stsp struct got_repository *repo)
140 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
141 a1fd68d8 2018-01-12 stsp char *path;
143 4796fb13 2018-12-23 stsp err = object_path(&path, id, repo);
145 d1cda826 2017-11-06 stsp return err;
146 d5003b79 2018-04-22 stsp *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
147 d5003b79 2018-04-22 stsp if (*fd == -1) {
148 6c00b545 2018-01-17 stsp err = got_error_from_errno();
152 4558fcd4 2018-01-14 stsp free(path);
153 2090a03d 2018-09-09 stsp return err;
156 2090a03d 2018-09-09 stsp static const struct got_error *
157 2090a03d 2018-09-09 stsp get_packfile_path(char **path_packfile, struct got_packidx *packidx)
159 2090a03d 2018-09-09 stsp size_t size;
161 2090a03d 2018-09-09 stsp /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
162 2090a03d 2018-09-09 stsp size = strlen(packidx->path_packidx) + 2;
163 2090a03d 2018-09-09 stsp if (size < GOT_PACKFILE_NAMELEN + 1)
164 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_BAD_PATH);
166 08451938 2018-11-05 stsp *path_packfile = malloc(size);
167 2090a03d 2018-09-09 stsp if (*path_packfile == NULL)
168 2090a03d 2018-09-09 stsp return got_error_from_errno();
170 2090a03d 2018-09-09 stsp /* Copy up to and excluding ".idx". */
171 2090a03d 2018-09-09 stsp if (strlcpy(*path_packfile, packidx->path_packidx,
172 2090a03d 2018-09-09 stsp size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
173 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_NO_SPACE);
175 2090a03d 2018-09-09 stsp if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
176 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_NO_SPACE);
178 2090a03d 2018-09-09 stsp return NULL;
181 a158c901 2018-12-23 stsp static void
182 a158c901 2018-12-23 stsp exec_privsep_child(int imsg_fds[2], const char *path, const char *repo_path)
184 a158c901 2018-12-23 stsp close(imsg_fds[0]);
186 a158c901 2018-12-23 stsp if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
187 a158c901 2018-12-23 stsp fprintf(stderr, "%s: %s\n", getprogname(),
188 a158c901 2018-12-23 stsp strerror(errno));
191 a158c901 2018-12-23 stsp if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
192 a158c901 2018-12-23 stsp fprintf(stderr, "%s: %s\n", getprogname(),
193 a158c901 2018-12-23 stsp strerror(errno));
197 a158c901 2018-12-23 stsp if (execl(path, path, repo_path, (char *)NULL) == -1) {
198 a158c901 2018-12-23 stsp fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
199 a158c901 2018-12-23 stsp strerror(errno));
204 2090a03d 2018-09-09 stsp static const struct got_error *
205 a158c901 2018-12-23 stsp request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
206 a158c901 2018-12-23 stsp struct got_object_id *id)
208 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
209 a158c901 2018-12-23 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
211 a158c901 2018-12-23 stsp err = got_privsep_send_packed_obj_req(ibuf, idx, id);
213 a158c901 2018-12-23 stsp return err;
215 a158c901 2018-12-23 stsp err = got_privsep_recv_obj(obj, ibuf);
217 a158c901 2018-12-23 stsp return err;
219 a158c901 2018-12-23 stsp (*obj)->path_packfile = strdup(pack->path_packfile);
220 a158c901 2018-12-23 stsp if ((*obj)->path_packfile == NULL) {
221 a158c901 2018-12-23 stsp err = got_error_from_errno();
222 a158c901 2018-12-23 stsp return err;
224 a158c901 2018-12-23 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
226 a158c901 2018-12-23 stsp return NULL;
229 a158c901 2018-12-23 stsp static const struct got_error *
230 711fb6e8 2018-12-23 stsp start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
232 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
233 a158c901 2018-12-23 stsp int imsg_fds[2];
235 a158c901 2018-12-23 stsp struct imsgbuf *ibuf;
237 a158c901 2018-12-23 stsp ibuf = calloc(1, sizeof(*ibuf));
238 a158c901 2018-12-23 stsp if (ibuf == NULL)
239 a158c901 2018-12-23 stsp return got_error_from_errno();
241 a158c901 2018-12-23 stsp pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
242 a158c901 2018-12-23 stsp if (pack->privsep_child == NULL) {
243 a158c901 2018-12-23 stsp err = got_error_from_errno();
244 a158c901 2018-12-23 stsp free(ibuf);
245 a158c901 2018-12-23 stsp return err;
248 a158c901 2018-12-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
249 a158c901 2018-12-23 stsp err = got_error_from_errno();
253 a158c901 2018-12-23 stsp pid = fork();
254 a158c901 2018-12-23 stsp if (pid == -1) {
255 a158c901 2018-12-23 stsp err = got_error_from_errno();
257 a158c901 2018-12-23 stsp } else if (pid == 0) {
258 a158c901 2018-12-23 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
259 a158c901 2018-12-23 stsp pack->path_packfile);
260 a158c901 2018-12-23 stsp /* not reached */
263 a158c901 2018-12-23 stsp close(imsg_fds[1]);
264 a158c901 2018-12-23 stsp pack->privsep_child->imsg_fd = imsg_fds[0];
265 a158c901 2018-12-23 stsp pack->privsep_child->pid = pid;
266 a158c901 2018-12-23 stsp imsg_init(ibuf, imsg_fds[0]);
267 a158c901 2018-12-23 stsp pack->privsep_child->ibuf = ibuf;
269 a158c901 2018-12-23 stsp err = got_privsep_init_pack_child(ibuf, pack, packidx);
271 a158c901 2018-12-23 stsp const struct got_error *child_err;
272 a158c901 2018-12-23 stsp err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
273 a158c901 2018-12-23 stsp child_err = got_privsep_wait_for_child(
274 a158c901 2018-12-23 stsp pack->privsep_child->pid);
275 a158c901 2018-12-23 stsp if (child_err && err == NULL)
276 a158c901 2018-12-23 stsp err = child_err;
280 a158c901 2018-12-23 stsp free(ibuf);
281 a158c901 2018-12-23 stsp free(pack->privsep_child);
282 a158c901 2018-12-23 stsp pack->privsep_child = NULL;
284 a158c901 2018-12-23 stsp return err;
287 711fb6e8 2018-12-23 stsp static const struct got_error *
288 711fb6e8 2018-12-23 stsp read_packed_object_privsep(struct got_object **obj,
289 711fb6e8 2018-12-23 stsp struct got_repository *repo, struct got_pack *pack,
290 711fb6e8 2018-12-23 stsp struct got_packidx *packidx, int idx, struct got_object_id *id)
292 711fb6e8 2018-12-23 stsp const struct got_error *err = NULL;
294 711fb6e8 2018-12-23 stsp if (pack->privsep_child)
295 711fb6e8 2018-12-23 stsp return request_packed_object(obj, pack, idx, id);
297 711fb6e8 2018-12-23 stsp err = start_pack_privsep_child(pack, packidx);
299 711fb6e8 2018-12-23 stsp return err;
301 711fb6e8 2018-12-23 stsp return request_packed_object(obj, pack, idx, id);
305 a158c901 2018-12-23 stsp static const struct got_error *
306 2090a03d 2018-09-09 stsp open_packed_object(struct got_object **obj, struct got_object_id *id,
307 2090a03d 2018-09-09 stsp struct got_repository *repo)
309 2090a03d 2018-09-09 stsp const struct got_error *err = NULL;
310 2090a03d 2018-09-09 stsp struct got_pack *pack = NULL;
311 2090a03d 2018-09-09 stsp struct got_packidx *packidx = NULL;
313 2090a03d 2018-09-09 stsp char *path_packfile;
315 2090a03d 2018-09-09 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
317 2090a03d 2018-09-09 stsp return err;
319 2090a03d 2018-09-09 stsp err = get_packfile_path(&path_packfile, packidx);
321 2090a03d 2018-09-09 stsp return err;
323 2090a03d 2018-09-09 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
324 2090a03d 2018-09-09 stsp if (pack == NULL) {
325 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
330 a158c901 2018-12-23 stsp err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
334 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
336 2090a03d 2018-09-09 stsp free(path_packfile);
337 4558fcd4 2018-01-14 stsp return err;
340 4558fcd4 2018-01-14 stsp const struct got_error *
341 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
342 4558fcd4 2018-01-14 stsp struct got_object_id *id)
344 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
345 6c00b545 2018-01-17 stsp char *path;
348 7bb0daa1 2018-06-21 stsp *obj = got_repo_get_cached_object(repo, id);
349 7bb0daa1 2018-06-21 stsp if (*obj != NULL) {
350 7bb0daa1 2018-06-21 stsp (*obj)->refcnt++;
351 7bb0daa1 2018-06-21 stsp return NULL;
354 59790a32 2018-09-15 stsp err = open_packed_object(obj, id, repo);
355 59790a32 2018-09-15 stsp if (err && err->code != GOT_ERR_NO_OBJ)
356 59790a32 2018-09-15 stsp return err;
357 59790a32 2018-09-15 stsp if (*obj) {
358 59790a32 2018-09-15 stsp (*obj)->refcnt++;
359 59790a32 2018-09-15 stsp return got_repo_cache_object(repo, id, *obj);
362 6c00b545 2018-01-17 stsp err = object_path(&path, id, repo);
364 4558fcd4 2018-01-14 stsp return err;
366 2178c42e 2018-04-22 stsp fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
367 2178c42e 2018-04-22 stsp if (fd == -1) {
368 59790a32 2018-09-15 stsp if (errno == ENOENT)
369 91a3d81f 2018-11-11 stsp err = got_error_no_obj(id);
371 6c00b545 2018-01-17 stsp err = got_error_from_errno();
374 ad242220 2018-09-08 stsp err = got_object_read_header_privsep(obj, repo, fd);
377 ab9a70b2 2017-11-06 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
380 59790a32 2018-09-15 stsp (*obj)->refcnt++;
381 59790a32 2018-09-15 stsp err = got_repo_cache_object(repo, id, *obj);
383 6c00b545 2018-01-17 stsp free(path);
384 2178c42e 2018-04-22 stsp if (fd != -1)
386 ab9a70b2 2017-11-06 stsp return err;
390 6dfa2fd3 2018-02-12 stsp const struct got_error *
391 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
392 6dfa2fd3 2018-02-12 stsp const char *id_str)
394 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
396 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
397 6dfa2fd3 2018-02-12 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
399 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
402 15a94983 2018-12-23 stsp const struct got_error *
403 15a94983 2018-12-23 stsp got_object_resolve_id_str(struct got_object_id **id,
404 15a94983 2018-12-23 stsp struct got_repository *repo, const char *id_str)
406 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
407 15a94983 2018-12-23 stsp struct got_object *obj;
409 15a94983 2018-12-23 stsp err = got_object_open_by_id_str(&obj, repo, id_str);
411 15a94983 2018-12-23 stsp return err;
413 15a94983 2018-12-23 stsp *id = got_object_id_dup(got_object_get_id(obj));
414 15a94983 2018-12-23 stsp got_object_close(obj);
415 15a94983 2018-12-23 stsp if (*id == NULL)
416 15a94983 2018-12-23 stsp return got_error_from_errno();
418 15a94983 2018-12-23 stsp return NULL;
421 434025f3 2018-09-16 stsp static const struct got_error *
422 a158c901 2018-12-23 stsp request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
423 a158c901 2018-12-23 stsp int pack_idx, struct got_object_id *id)
425 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
427 a158c901 2018-12-23 stsp err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
430 a158c901 2018-12-23 stsp return err;
432 a158c901 2018-12-23 stsp return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
435 a158c901 2018-12-23 stsp static const struct got_error *
436 a158c901 2018-12-23 stsp read_packed_commit_privsep(struct got_commit_object **commit,
437 a158c901 2018-12-23 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
438 a158c901 2018-12-23 stsp struct got_object_id *id)
440 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
442 a158c901 2018-12-23 stsp if (pack->privsep_child)
443 a158c901 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
445 711fb6e8 2018-12-23 stsp err = start_pack_privsep_child(pack, packidx);
447 a158c901 2018-12-23 stsp return err;
449 711fb6e8 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
452 a158c901 2018-12-23 stsp static const struct got_error *
453 434025f3 2018-09-16 stsp open_commit(struct got_commit_object **commit,
454 1785f84a 2018-12-23 stsp struct got_repository *repo, struct got_object_id *id, int check_cache)
456 434025f3 2018-09-16 stsp const struct got_error *err = NULL;
457 1785f84a 2018-12-23 stsp struct got_packidx *packidx = NULL;
459 1785f84a 2018-12-23 stsp char *path_packfile;
461 434025f3 2018-09-16 stsp if (check_cache) {
462 1785f84a 2018-12-23 stsp *commit = got_repo_get_cached_commit(repo, id);
463 434025f3 2018-09-16 stsp if (*commit != NULL) {
464 434025f3 2018-09-16 stsp (*commit)->refcnt++;
465 434025f3 2018-09-16 stsp return NULL;
468 434025f3 2018-09-16 stsp *commit = NULL;
470 1785f84a 2018-12-23 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
471 1785f84a 2018-12-23 stsp if (err == NULL) {
472 1785f84a 2018-12-23 stsp struct got_pack *pack = NULL;
474 1785f84a 2018-12-23 stsp err = get_packfile_path(&path_packfile, packidx);
476 1785f84a 2018-12-23 stsp return err;
478 1785f84a 2018-12-23 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
479 434025f3 2018-09-16 stsp if (pack == NULL) {
480 1785f84a 2018-12-23 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
483 434025f3 2018-09-16 stsp return err;
485 a158c901 2018-12-23 stsp err = read_packed_commit_privsep(commit, pack,
486 1785f84a 2018-12-23 stsp packidx, idx, id);
487 1785f84a 2018-12-23 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
490 1785f84a 2018-12-23 stsp err = open_loose_object(&fd, id, repo);
492 434025f3 2018-09-16 stsp return err;
493 1785f84a 2018-12-23 stsp err = got_object_read_commit_privsep(commit, fd, repo);
497 434025f3 2018-09-16 stsp if (err == NULL) {
498 434025f3 2018-09-16 stsp (*commit)->refcnt++;
499 1785f84a 2018-12-23 stsp err = got_repo_cache_commit(repo, id, *commit);
502 e32baab7 2018-11-05 stsp return err;
505 e32baab7 2018-11-05 stsp const struct got_error *
506 e32baab7 2018-11-05 stsp got_object_open_as_commit(struct got_commit_object **commit,
507 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object_id *id)
509 e32baab7 2018-11-05 stsp *commit = got_repo_get_cached_commit(repo, id);
510 e32baab7 2018-11-05 stsp if (*commit != NULL) {
511 e32baab7 2018-11-05 stsp (*commit)->refcnt++;
512 e32baab7 2018-11-05 stsp return NULL;
515 1785f84a 2018-12-23 stsp return open_commit(commit, repo, id, 0);
518 e32baab7 2018-11-05 stsp const struct got_error *
519 e32baab7 2018-11-05 stsp got_object_commit_open(struct got_commit_object **commit,
520 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object *obj)
522 1785f84a 2018-12-23 stsp return open_commit(commit, repo, got_object_get_id(obj), 1);
525 434025f3 2018-09-16 stsp const struct got_error *
526 dbc6a6b6 2018-07-12 stsp got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
528 dbc6a6b6 2018-07-12 stsp const struct got_error *err = NULL;
530 dbc6a6b6 2018-07-12 stsp *qid = calloc(1, sizeof(**qid));
531 dbc6a6b6 2018-07-12 stsp if (*qid == NULL)
532 dbc6a6b6 2018-07-12 stsp return got_error_from_errno();
534 dbc6a6b6 2018-07-12 stsp (*qid)->id = got_object_id_dup(id);
535 dbc6a6b6 2018-07-12 stsp if ((*qid)->id == NULL) {
536 dbc6a6b6 2018-07-12 stsp err = got_error_from_errno();
537 fa2f6902 2018-07-23 stsp got_object_qid_free(*qid);
538 dbc6a6b6 2018-07-12 stsp *qid = NULL;
539 dbc6a6b6 2018-07-12 stsp return err;
542 dbc6a6b6 2018-07-12 stsp return NULL;
545 a158c901 2018-12-23 stsp static const struct got_error *
546 13c729f7 2018-12-24 stsp request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
547 13c729f7 2018-12-24 stsp int pack_idx, struct got_object_id *id)
549 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
551 13c729f7 2018-12-24 stsp err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
554 a158c901 2018-12-23 stsp return err;
556 a158c901 2018-12-23 stsp return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
559 71eb0e7f 2018-09-16 stsp static const struct got_error *
560 13c729f7 2018-12-24 stsp read_packed_tree_privsep(struct got_tree_object **tree,
561 13c729f7 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
562 13c729f7 2018-12-24 stsp struct got_object_id *id)
564 13c729f7 2018-12-24 stsp const struct got_error *err = NULL;
566 13c729f7 2018-12-24 stsp if (pack->privsep_child)
567 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
569 13c729f7 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
571 13c729f7 2018-12-24 stsp return err;
573 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
576 13c729f7 2018-12-24 stsp static const struct got_error *
577 13c729f7 2018-12-24 stsp open_tree(struct got_tree_object **tree, struct got_repository *repo,
578 13c729f7 2018-12-24 stsp struct got_object_id *id, int check_cache)
580 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
581 13c729f7 2018-12-24 stsp struct got_packidx *packidx = NULL;
583 13c729f7 2018-12-24 stsp char *path_packfile;
585 71eb0e7f 2018-09-16 stsp if (check_cache) {
586 13c729f7 2018-12-24 stsp *tree = got_repo_get_cached_tree(repo, id);
587 71eb0e7f 2018-09-16 stsp if (*tree != NULL) {
588 71eb0e7f 2018-09-16 stsp (*tree)->refcnt++;
589 71eb0e7f 2018-09-16 stsp return NULL;
592 71eb0e7f 2018-09-16 stsp *tree = NULL;
594 13c729f7 2018-12-24 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
595 13c729f7 2018-12-24 stsp if (err == NULL) {
596 13c729f7 2018-12-24 stsp struct got_pack *pack = NULL;
598 13c729f7 2018-12-24 stsp err = get_packfile_path(&path_packfile, packidx);
600 13c729f7 2018-12-24 stsp return err;
602 13c729f7 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
603 e7885405 2018-09-10 stsp if (pack == NULL) {
604 13c729f7 2018-12-24 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
607 e7885405 2018-09-10 stsp return err;
609 13c729f7 2018-12-24 stsp err = read_packed_tree_privsep(tree, pack,
610 13c729f7 2018-12-24 stsp packidx, idx, id);
611 13c729f7 2018-12-24 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
614 13c729f7 2018-12-24 stsp err = open_loose_object(&fd, id, repo);
616 e0ab43e7 2018-03-16 stsp return err;
617 13c729f7 2018-12-24 stsp err = got_object_read_tree_privsep(tree, fd, repo);
621 f6be5c30 2018-06-22 stsp if (err == NULL) {
622 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
623 13c729f7 2018-12-24 stsp err = got_repo_cache_tree(repo, id, *tree);
626 776d4d29 2018-06-17 stsp return err;
629 776d4d29 2018-06-17 stsp const struct got_error *
630 776d4d29 2018-06-17 stsp got_object_open_as_tree(struct got_tree_object **tree,
631 776d4d29 2018-06-17 stsp struct got_repository *repo, struct got_object_id *id)
633 e8eb494a 2018-09-16 stsp *tree = got_repo_get_cached_tree(repo, id);
634 e8eb494a 2018-09-16 stsp if (*tree != NULL) {
635 e8eb494a 2018-09-16 stsp (*tree)->refcnt++;
636 e8eb494a 2018-09-16 stsp return NULL;
639 13c729f7 2018-12-24 stsp return open_tree(tree, repo, id, 0);
642 71eb0e7f 2018-09-16 stsp const struct got_error *
643 71eb0e7f 2018-09-16 stsp got_object_tree_open(struct got_tree_object **tree,
644 71eb0e7f 2018-09-16 stsp struct got_repository *repo, struct got_object *obj)
646 13c729f7 2018-12-24 stsp return open_tree(tree, repo, got_object_get_id(obj), 1);
649 883f0469 2018-06-23 stsp const struct got_tree_entries *
650 883f0469 2018-06-23 stsp got_object_tree_get_entries(struct got_tree_object *tree)
652 883f0469 2018-06-23 stsp return &tree->entries;
655 3840f4c9 2018-09-12 stsp static const struct got_error *
656 3840f4c9 2018-09-12 stsp read_packed_blob_privsep(size_t *size, int outfd, struct got_object *obj,
657 3840f4c9 2018-09-12 stsp struct got_pack *pack)
659 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
660 3840f4c9 2018-09-12 stsp int outfd_child;
661 3840f4c9 2018-09-12 stsp int basefd, accumfd; /* temporary files for delta application */
663 3840f4c9 2018-09-12 stsp basefd = got_opentempfd();
664 3840f4c9 2018-09-12 stsp if (basefd == -1)
665 3840f4c9 2018-09-12 stsp return got_error_from_errno();
666 3840f4c9 2018-09-12 stsp accumfd = got_opentempfd();
667 3840f4c9 2018-09-12 stsp if (accumfd == -1)
668 3840f4c9 2018-09-12 stsp return got_error_from_errno();
670 3840f4c9 2018-09-12 stsp outfd_child = dup(outfd);
671 3840f4c9 2018-09-12 stsp if (outfd_child == -1)
672 3840f4c9 2018-09-12 stsp return got_error_from_errno();
674 3840f4c9 2018-09-12 stsp err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
676 3840f4c9 2018-09-12 stsp return err;
678 3840f4c9 2018-09-12 stsp err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
679 3840f4c9 2018-09-12 stsp outfd_child);
681 3840f4c9 2018-09-12 stsp close(outfd_child);
682 3840f4c9 2018-09-12 stsp return err;
684 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
687 3840f4c9 2018-09-12 stsp close(basefd);
688 3840f4c9 2018-09-12 stsp close(accumfd);
689 3840f4c9 2018-09-12 stsp close(outfd_child);
690 3840f4c9 2018-09-12 stsp return err;
693 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
696 3840f4c9 2018-09-12 stsp close(accumfd);
697 3840f4c9 2018-09-12 stsp close(outfd_child);
698 3840f4c9 2018-09-12 stsp return err;
701 3840f4c9 2018-09-12 stsp err = got_privsep_recv_blob(size, pack->privsep_child->ibuf);
703 3840f4c9 2018-09-12 stsp return err;
705 3840f4c9 2018-09-12 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
706 3840f4c9 2018-09-12 stsp err = got_error_from_errno();
708 3840f4c9 2018-09-12 stsp return err;
711 68482ea3 2017-11-27 stsp const struct got_error *
712 68482ea3 2017-11-27 stsp got_object_blob_open(struct got_blob_object **blob,
713 68482ea3 2017-11-27 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
715 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
717 55da3778 2018-09-10 stsp size_t size;
718 55da3778 2018-09-10 stsp struct stat sb;
720 68482ea3 2017-11-27 stsp if (obj->type != GOT_OBJ_TYPE_BLOB)
721 68482ea3 2017-11-27 stsp return got_error(GOT_ERR_OBJ_TYPE);
723 7d283eee 2017-11-29 stsp if (blocksize < obj->hdrlen)
724 7d283eee 2017-11-29 stsp return got_error(GOT_ERR_NO_SPACE);
726 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
727 4558fcd4 2018-01-14 stsp if (*blob == NULL)
728 0a585a0d 2018-03-17 stsp return got_error_from_errno();
730 55da3778 2018-09-10 stsp outfd = got_opentempfd();
731 55da3778 2018-09-10 stsp if (outfd == -1)
732 55da3778 2018-09-10 stsp return got_error_from_errno();
734 062ebb78 2018-07-12 stsp (*blob)->read_buf = malloc(blocksize);
735 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
736 15c8b0e6 2018-04-24 stsp err = got_error_from_errno();
739 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
740 55da3778 2018-09-10 stsp struct got_pack *pack;
741 55da3778 2018-09-10 stsp pack = got_repo_get_cached_pack(repo, obj->path_packfile);
742 55da3778 2018-09-10 stsp if (pack == NULL) {
743 55da3778 2018-09-10 stsp err = got_repo_cache_pack(&pack, repo,
744 55da3778 2018-09-10 stsp obj->path_packfile, NULL);
748 3840f4c9 2018-09-12 stsp err = read_packed_blob_privsep(&size, outfd, obj, pack);
751 55da3778 2018-09-10 stsp obj->size = size;
755 4796fb13 2018-12-23 stsp err = open_loose_object(&infd, got_object_get_id(obj), repo);
759 ad242220 2018-09-08 stsp err = got_object_read_blob_privsep(&size, outfd, infd, repo);
760 3aca5731 2018-04-24 stsp close(infd);
764 2967a784 2018-04-24 stsp if (size != obj->hdrlen + obj->size) {
765 1a6b3ab7 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
770 55da3778 2018-09-10 stsp if (fstat(outfd, &sb) == -1) {
771 55da3778 2018-09-10 stsp err = got_error_from_errno();
775 55da3778 2018-09-10 stsp if (sb.st_size != obj->hdrlen + obj->size) {
776 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
780 55da3778 2018-09-10 stsp (*blob)->f = fdopen(outfd, "rb");
781 55da3778 2018-09-10 stsp if ((*blob)->f == NULL) {
782 55da3778 2018-09-10 stsp err = got_error_from_errno();
783 55da3778 2018-09-10 stsp close(outfd);
787 7d283eee 2017-11-29 stsp (*blob)->hdrlen = obj->hdrlen;
788 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
789 f78b0693 2017-11-29 stsp memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
793 55da3778 2018-09-10 stsp if (*blob) {
794 55da3778 2018-09-10 stsp if ((*blob)->f)
795 55da3778 2018-09-10 stsp fclose((*blob)->f);
796 55da3778 2018-09-10 stsp free((*blob)->read_buf);
797 55da3778 2018-09-10 stsp free(*blob);
798 55da3778 2018-09-10 stsp *blob = NULL;
799 55da3778 2018-09-10 stsp } else if (outfd != -1)
800 55da3778 2018-09-10 stsp close(outfd);
802 a19581a2 2018-06-21 stsp return err;
805 a19581a2 2018-06-21 stsp const struct got_error *
806 a19581a2 2018-06-21 stsp got_object_open_as_blob(struct got_blob_object **blob,
807 a19581a2 2018-06-21 stsp struct got_repository *repo, struct got_object_id *id,
808 a19581a2 2018-06-21 stsp size_t blocksize)
810 a19581a2 2018-06-21 stsp const struct got_error *err;
811 a19581a2 2018-06-21 stsp struct got_object *obj;
813 835e0dbd 2018-06-21 stsp *blob = NULL;
815 a19581a2 2018-06-21 stsp err = got_object_open(&obj, repo, id);
817 a19581a2 2018-06-21 stsp return err;
818 15a94983 2018-12-23 stsp if (obj->type != GOT_OBJ_TYPE_BLOB) {
819 a19581a2 2018-06-21 stsp err = got_error(GOT_ERR_OBJ_TYPE);
823 a19581a2 2018-06-21 stsp err = got_object_blob_open(blob, repo, obj, blocksize);
825 a19581a2 2018-06-21 stsp got_object_close(obj);
826 68482ea3 2017-11-27 stsp return err;
830 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
832 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
833 68482ea3 2017-11-27 stsp fclose(blob->f);
834 68482ea3 2017-11-27 stsp free(blob);
838 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
840 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
844 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
846 f934cf2c 2018-02-12 stsp return blob->hdrlen;
849 f934cf2c 2018-02-12 stsp const uint8_t *
850 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
852 f934cf2c 2018-02-12 stsp return blob->read_buf;
855 68482ea3 2017-11-27 stsp const struct got_error *
856 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
860 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
861 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
862 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
863 eb651edf 2018-02-11 stsp *outlenp = n;
864 35e9ba5d 2018-06-21 stsp return NULL;
867 35e9ba5d 2018-06-21 stsp const struct got_error *
868 6fcac457 2018-11-19 stsp got_object_blob_dump_to_file(size_t *total_len, int *nlines,
869 84451b3e 2018-07-10 stsp FILE *outfile, struct got_blob_object *blob)
871 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
872 35e9ba5d 2018-06-21 stsp size_t len, hdrlen;
873 84451b3e 2018-07-10 stsp const uint8_t *buf;
876 84451b3e 2018-07-10 stsp if (total_len)
877 84451b3e 2018-07-10 stsp *total_len = 0;
878 84451b3e 2018-07-10 stsp if (nlines)
879 84451b3e 2018-07-10 stsp *nlines = 0;
881 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
883 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
885 35e9ba5d 2018-06-21 stsp return err;
886 35e9ba5d 2018-06-21 stsp if (len == 0)
888 84451b3e 2018-07-10 stsp if (total_len)
889 84451b3e 2018-07-10 stsp *total_len += len;
890 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
891 84451b3e 2018-07-10 stsp if (nlines) {
892 84451b3e 2018-07-10 stsp for (i = 0; i < len; i++) {
893 84451b3e 2018-07-10 stsp if (buf[i] == '\n')
894 84451b3e 2018-07-10 stsp (*nlines)++;
897 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
898 84451b3e 2018-07-10 stsp fwrite(buf + hdrlen, len - hdrlen, 1, outfile);
899 35e9ba5d 2018-06-21 stsp hdrlen = 0;
900 35e9ba5d 2018-06-21 stsp } while (len != 0);
902 35e9ba5d 2018-06-21 stsp fflush(outfile);
903 35e9ba5d 2018-06-21 stsp rewind(outfile);
905 776d4d29 2018-06-17 stsp return NULL;
908 f4a881ce 2018-11-17 stsp static const struct got_error *
909 a158c901 2018-12-23 stsp read_packed_tag_privsep(struct got_tag_object **tag,
910 a158c901 2018-12-23 stsp struct got_object *obj, struct got_pack *pack)
912 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
914 a158c901 2018-12-23 stsp err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
916 a158c901 2018-12-23 stsp return err;
918 a158c901 2018-12-23 stsp return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
922 a158c901 2018-12-23 stsp static const struct got_error *
923 f4a881ce 2018-11-17 stsp open_tag(struct got_tag_object **tag,
924 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object *obj, int check_cache)
926 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
928 f4a881ce 2018-11-17 stsp if (check_cache) {
929 f4a881ce 2018-11-17 stsp *tag = got_repo_get_cached_tag(repo, &obj->id);
930 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
931 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
932 f4a881ce 2018-11-17 stsp return NULL;
935 f4a881ce 2018-11-17 stsp *tag = NULL;
937 f4a881ce 2018-11-17 stsp if (obj->type != GOT_OBJ_TYPE_TAG)
938 f4a881ce 2018-11-17 stsp return got_error(GOT_ERR_OBJ_TYPE);
940 f4a881ce 2018-11-17 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
941 f4a881ce 2018-11-17 stsp struct got_pack *pack;
942 f4a881ce 2018-11-17 stsp pack = got_repo_get_cached_pack(repo, obj->path_packfile);
943 f4a881ce 2018-11-17 stsp if (pack == NULL) {
944 f4a881ce 2018-11-17 stsp err = got_repo_cache_pack(&pack, repo,
945 f4a881ce 2018-11-17 stsp obj->path_packfile, NULL);
947 f4a881ce 2018-11-17 stsp return err;
949 a158c901 2018-12-23 stsp err = read_packed_tag_privsep(tag, obj, pack);
952 4796fb13 2018-12-23 stsp err = open_loose_object(&fd, got_object_get_id(obj), repo);
954 f4a881ce 2018-11-17 stsp return err;
955 f4a881ce 2018-11-17 stsp err = got_object_read_tag_privsep(tag, obj, fd, repo);
959 f4a881ce 2018-11-17 stsp if (err == NULL) {
960 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
961 f4a881ce 2018-11-17 stsp err = got_repo_cache_tag(repo, &obj->id, *tag);
964 f4a881ce 2018-11-17 stsp return err;
967 f4a881ce 2018-11-17 stsp const struct got_error *
968 f4a881ce 2018-11-17 stsp got_object_open_as_tag(struct got_tag_object **tag,
969 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object_id *id)
971 f4a881ce 2018-11-17 stsp const struct got_error *err;
972 f4a881ce 2018-11-17 stsp struct got_object *obj;
974 f4a881ce 2018-11-17 stsp *tag = got_repo_get_cached_tag(repo, id);
975 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
976 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
977 f4a881ce 2018-11-17 stsp return NULL;
980 f4a881ce 2018-11-17 stsp err = got_object_open(&obj, repo, id);
982 f4a881ce 2018-11-17 stsp return err;
983 15a94983 2018-12-23 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
984 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_OBJ_TYPE);
988 f4a881ce 2018-11-17 stsp err = open_tag(tag, repo, obj, 0);
990 f4a881ce 2018-11-17 stsp got_object_close(obj);
991 f4a881ce 2018-11-17 stsp return err;
994 f4a881ce 2018-11-17 stsp const struct got_error *
995 f4a881ce 2018-11-17 stsp got_object_tag_open(struct got_tag_object **tag,
996 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object *obj)
998 f4a881ce 2018-11-17 stsp return open_tag(tag, repo, obj, 1);
1001 776d4d29 2018-06-17 stsp static struct got_tree_entry *
1002 65a9bbe9 2018-09-15 stsp find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1004 776d4d29 2018-06-17 stsp struct got_tree_entry *te;
1006 63da309a 2018-11-07 stsp /* Note that tree entries are sorted in strncmp() order. */
1007 883f0469 2018-06-23 stsp SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
1008 63da309a 2018-11-07 stsp int cmp = strncmp(te->name, name, len);
1009 63da309a 2018-11-07 stsp if (cmp < 0)
1011 63da309a 2018-11-07 stsp if (cmp > 0)
1013 63da309a 2018-11-07 stsp if (te->name[len] == '\0')
1014 776d4d29 2018-06-17 stsp return te;
1016 eb651edf 2018-02-11 stsp return NULL;
1019 776d4d29 2018-06-17 stsp const struct got_error *
1020 27d434c2 2018-09-15 stsp got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1021 776d4d29 2018-06-17 stsp struct got_object_id *commit_id, const char *path)
1023 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
1024 776d4d29 2018-06-17 stsp struct got_commit_object *commit = NULL;
1025 776d4d29 2018-06-17 stsp struct got_tree_object *tree = NULL;
1026 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
1027 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1028 b7cd37e5 2018-11-18 stsp size_t seglen;
1030 27d434c2 2018-09-15 stsp *id = NULL;
1032 776d4d29 2018-06-17 stsp /* We are expecting an absolute in-repository path. */
1033 776d4d29 2018-06-17 stsp if (path[0] != '/')
1034 776d4d29 2018-06-17 stsp return got_error(GOT_ERR_NOT_ABSPATH);
1036 776d4d29 2018-06-17 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
1038 776d4d29 2018-06-17 stsp goto done;
1040 776d4d29 2018-06-17 stsp /* Handle opening of root of commit's tree. */
1041 776d4d29 2018-06-17 stsp if (path[1] == '\0') {
1042 27d434c2 2018-09-15 stsp *id = got_object_id_dup(commit->tree_id);
1043 27d434c2 2018-09-15 stsp if (*id == NULL)
1044 27d434c2 2018-09-15 stsp err = got_error_from_errno();
1045 ca008b32 2018-07-22 stsp goto done;
1048 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1050 776d4d29 2018-06-17 stsp goto done;
1053 776d4d29 2018-06-17 stsp s++; /* skip leading '/' */
1055 65a9bbe9 2018-09-15 stsp seglen = 0;
1056 b7cd37e5 2018-11-18 stsp while (*s) {
1057 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
1059 776d4d29 2018-06-17 stsp if (*s != '/') {
1066 65a9bbe9 2018-09-15 stsp te = find_entry_by_name(tree, seg, seglen);
1067 db37e2c0 2018-06-21 stsp if (te == NULL) {
1068 d1451975 2018-11-11 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1069 776d4d29 2018-06-17 stsp goto done;
1072 b7cd37e5 2018-11-18 stsp if (*s == '\0')
1075 776d4d29 2018-06-17 stsp seg = s + 1;
1076 65a9bbe9 2018-09-15 stsp seglen = 0;
1079 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
1081 db37e2c0 2018-06-21 stsp te = NULL;
1083 776d4d29 2018-06-17 stsp goto done;
1084 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
1085 776d4d29 2018-06-17 stsp tree = next_tree;
1090 27d434c2 2018-09-15 stsp *id = got_object_id_dup(te->id);
1091 27d434c2 2018-09-15 stsp if (*id == NULL)
1092 27d434c2 2018-09-15 stsp return got_error_from_errno();
1094 d1451975 2018-11-11 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1096 776d4d29 2018-06-17 stsp if (commit)
1097 776d4d29 2018-06-17 stsp got_object_commit_close(commit);
1099 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
1100 776d4d29 2018-06-17 stsp return err;
1103 07862c20 2018-09-15 stsp const struct got_error *
1104 07862c20 2018-09-15 stsp got_object_tree_path_changed(int *changed,
1105 07862c20 2018-09-15 stsp struct got_tree_object *tree01, struct got_tree_object *tree02,
1106 07862c20 2018-09-15 stsp const char *path, struct got_repository *repo)
1108 07862c20 2018-09-15 stsp const struct got_error *err = NULL;
1109 07862c20 2018-09-15 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1110 07862c20 2018-09-15 stsp struct got_tree_entry *te1 = NULL, *te2 = NULL;
1111 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1112 3b7f9878 2018-11-18 stsp size_t seglen;
1114 07862c20 2018-09-15 stsp *changed = 0;
1116 07862c20 2018-09-15 stsp /* We are expecting an absolute in-repository path. */
1117 07862c20 2018-09-15 stsp if (path[0] != '/')
1118 07862c20 2018-09-15 stsp return got_error(GOT_ERR_NOT_ABSPATH);
1120 07862c20 2018-09-15 stsp /* We not do support comparing the root path. */
1121 07862c20 2018-09-15 stsp if (path[1] == '\0')
1122 07862c20 2018-09-15 stsp return got_error(GOT_ERR_BAD_PATH);
1124 07862c20 2018-09-15 stsp tree1 = tree01;
1125 07862c20 2018-09-15 stsp tree2 = tree02;
1127 07862c20 2018-09-15 stsp s++; /* skip leading '/' */
1129 65a9bbe9 2018-09-15 stsp seglen = 0;
1130 3b7f9878 2018-11-18 stsp while (*s) {
1131 07862c20 2018-09-15 stsp struct got_tree_object *next_tree1, *next_tree2;
1133 07862c20 2018-09-15 stsp if (*s != '/') {
1140 65a9bbe9 2018-09-15 stsp te1 = find_entry_by_name(tree1, seg, seglen);
1141 07862c20 2018-09-15 stsp if (te1 == NULL) {
1142 07862c20 2018-09-15 stsp err = got_error(GOT_ERR_NO_OBJ);
1143 07862c20 2018-09-15 stsp goto done;
1146 65a9bbe9 2018-09-15 stsp te2 = find_entry_by_name(tree2, seg, seglen);
1147 07862c20 2018-09-15 stsp if (te2 == NULL) {
1148 07862c20 2018-09-15 stsp *changed = 1;
1149 07862c20 2018-09-15 stsp goto done;
1152 07862c20 2018-09-15 stsp if (te1->mode != te2->mode) {
1153 07862c20 2018-09-15 stsp *changed = 1;
1154 07862c20 2018-09-15 stsp goto done;
1157 07862c20 2018-09-15 stsp if (got_object_id_cmp(te1->id, te2->id) == 0) {
1158 07862c20 2018-09-15 stsp *changed = 0;
1159 07862c20 2018-09-15 stsp goto done;
1162 3b7f9878 2018-11-18 stsp if (*s == '\0') { /* final path element */
1163 07862c20 2018-09-15 stsp *changed = 1;
1164 07862c20 2018-09-15 stsp goto done;
1167 07862c20 2018-09-15 stsp seg = s + 1;
1169 65a9bbe9 2018-09-15 stsp seglen = 0;
1171 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree1, repo,
1173 07862c20 2018-09-15 stsp te1 = NULL;
1175 07862c20 2018-09-15 stsp goto done;
1176 a31cea73 2018-09-15 stsp if (tree1 != tree01)
1177 a31cea73 2018-09-15 stsp got_object_tree_close(tree1);
1178 07862c20 2018-09-15 stsp tree1 = next_tree1;
1180 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree2, repo,
1182 07862c20 2018-09-15 stsp te2 = NULL;
1184 07862c20 2018-09-15 stsp goto done;
1185 a31cea73 2018-09-15 stsp if (tree2 != tree02)
1186 a31cea73 2018-09-15 stsp got_object_tree_close(tree2);
1187 07862c20 2018-09-15 stsp tree2 = next_tree2;
1191 a31cea73 2018-09-15 stsp if (tree1 && tree1 != tree01)
1192 07862c20 2018-09-15 stsp got_object_tree_close(tree1);
1193 a31cea73 2018-09-15 stsp if (tree2 && tree2 != tree02)
1194 07862c20 2018-09-15 stsp got_object_tree_close(tree2);
1195 77880158 2018-11-04 stsp return err;
1198 77880158 2018-11-04 stsp static const struct got_error *
1199 77880158 2018-11-04 stsp request_object(struct got_object **obj, struct got_repository *repo, int fd)
1201 77880158 2018-11-04 stsp const struct got_error *err = NULL;
1202 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
1204 77880158 2018-11-04 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
1206 77880158 2018-11-04 stsp err = got_privsep_send_obj_req(ibuf, fd, NULL);
1208 77880158 2018-11-04 stsp return err;
1210 77880158 2018-11-04 stsp return got_privsep_recv_obj(obj, ibuf);
1213 77880158 2018-11-04 stsp const struct got_error *
1214 77880158 2018-11-04 stsp got_object_read_header_privsep(struct got_object **obj,
1215 77880158 2018-11-04 stsp struct got_repository *repo, int obj_fd)
1217 77880158 2018-11-04 stsp int imsg_fds[2];
1218 77880158 2018-11-04 stsp pid_t pid;
1219 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
1221 77880158 2018-11-04 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
1222 77880158 2018-11-04 stsp return request_object(obj, repo, obj_fd);
1224 77880158 2018-11-04 stsp ibuf = calloc(1, sizeof(*ibuf));
1225 77880158 2018-11-04 stsp if (ibuf == NULL)
1226 77880158 2018-11-04 stsp return got_error_from_errno();
1228 77880158 2018-11-04 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1229 77880158 2018-11-04 stsp return got_error_from_errno();
1231 77880158 2018-11-04 stsp pid = fork();
1232 77880158 2018-11-04 stsp if (pid == -1)
1233 77880158 2018-11-04 stsp return got_error_from_errno();
1234 77880158 2018-11-04 stsp else if (pid == 0) {
1235 77880158 2018-11-04 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
1236 77880158 2018-11-04 stsp repo->path);
1237 77880158 2018-11-04 stsp /* not reached */
1240 77880158 2018-11-04 stsp close(imsg_fds[1]);
1241 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
1242 77880158 2018-11-04 stsp imsg_fds[0];
1243 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
1244 77880158 2018-11-04 stsp imsg_init(ibuf, imsg_fds[0]);
1245 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
1247 77880158 2018-11-04 stsp return request_object(obj, repo, obj_fd);
1250 77880158 2018-11-04 stsp static const struct got_error *
1251 77880158 2018-11-04 stsp request_commit(struct got_commit_object **commit, struct got_repository *repo,
1254 77880158 2018-11-04 stsp const struct got_error *err = NULL;
1255 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
1257 77880158 2018-11-04 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
1259 1785f84a 2018-12-23 stsp err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
1261 77880158 2018-11-04 stsp return err;
1263 6e72e6a3 2018-12-23 stsp return got_privsep_recv_commit(commit, ibuf);
1266 77880158 2018-11-04 stsp const struct got_error *
1267 77880158 2018-11-04 stsp got_object_read_commit_privsep(struct got_commit_object **commit,
1268 1785f84a 2018-12-23 stsp int obj_fd, struct got_repository *repo)
1270 77880158 2018-11-04 stsp int imsg_fds[2];
1271 77880158 2018-11-04 stsp pid_t pid;
1272 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
1274 77880158 2018-11-04 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
1275 1785f84a 2018-12-23 stsp return request_commit(commit, repo, obj_fd);
1277 77880158 2018-11-04 stsp ibuf = calloc(1, sizeof(*ibuf));
1278 77880158 2018-11-04 stsp if (ibuf == NULL)
1279 77880158 2018-11-04 stsp return got_error_from_errno();
1281 77880158 2018-11-04 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1282 77880158 2018-11-04 stsp return got_error_from_errno();
1284 77880158 2018-11-04 stsp pid = fork();
1285 77880158 2018-11-04 stsp if (pid == -1)
1286 77880158 2018-11-04 stsp return got_error_from_errno();
1287 77880158 2018-11-04 stsp else if (pid == 0) {
1288 77880158 2018-11-04 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
1289 77880158 2018-11-04 stsp repo->path);
1290 77880158 2018-11-04 stsp /* not reached */
1293 77880158 2018-11-04 stsp close(imsg_fds[1]);
1294 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
1295 77880158 2018-11-04 stsp imsg_fds[0];
1296 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
1297 77880158 2018-11-04 stsp imsg_init(ibuf, imsg_fds[0]);
1298 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
1300 1785f84a 2018-12-23 stsp return request_commit(commit, repo, obj_fd);
1303 77880158 2018-11-04 stsp static const struct got_error *
1304 77880158 2018-11-04 stsp request_tree(struct got_tree_object **tree, struct got_repository *repo,
1307 77880158 2018-11-04 stsp const struct got_error *err = NULL;
1308 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
1310 77880158 2018-11-04 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
1312 13c729f7 2018-12-24 stsp err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
1314 77880158 2018-11-04 stsp return err;
1316 77880158 2018-11-04 stsp return got_privsep_recv_tree(tree, ibuf);
1319 77880158 2018-11-04 stsp const struct got_error *
1320 77880158 2018-11-04 stsp got_object_read_tree_privsep(struct got_tree_object **tree,
1321 13c729f7 2018-12-24 stsp int obj_fd, struct got_repository *repo)
1323 77880158 2018-11-04 stsp int imsg_fds[2];
1324 77880158 2018-11-04 stsp pid_t pid;
1325 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
1327 77880158 2018-11-04 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
1328 13c729f7 2018-12-24 stsp return request_tree(tree, repo, obj_fd);
1330 77880158 2018-11-04 stsp ibuf = calloc(1, sizeof(*ibuf));
1331 77880158 2018-11-04 stsp if (ibuf == NULL)
1332 77880158 2018-11-04 stsp return got_error_from_errno();
1334 77880158 2018-11-04 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1335 77880158 2018-11-04 stsp return got_error_from_errno();
1337 77880158 2018-11-04 stsp pid = fork();
1338 77880158 2018-11-04 stsp if (pid == -1)
1339 77880158 2018-11-04 stsp return got_error_from_errno();
1340 77880158 2018-11-04 stsp else if (pid == 0) {
1341 77880158 2018-11-04 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1342 77880158 2018-11-04 stsp repo->path);
1343 77880158 2018-11-04 stsp /* not reached */
1346 77880158 2018-11-04 stsp close(imsg_fds[1]);
1348 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1349 77880158 2018-11-04 stsp imsg_fds[0];
1350 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1351 77880158 2018-11-04 stsp imsg_init(ibuf, imsg_fds[0]);
1352 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1355 13c729f7 2018-12-24 stsp return request_tree(tree, repo, obj_fd);
1358 77880158 2018-11-04 stsp static const struct got_error *
1359 77880158 2018-11-04 stsp request_blob(size_t *size, int outfd, int infd, struct imsgbuf *ibuf)
1361 77880158 2018-11-04 stsp const struct got_error *err = NULL;
1362 77880158 2018-11-04 stsp int outfd_child;
1364 77880158 2018-11-04 stsp outfd_child = dup(outfd);
1365 77880158 2018-11-04 stsp if (outfd_child == -1)
1366 77880158 2018-11-04 stsp return got_error_from_errno();
1368 77880158 2018-11-04 stsp err = got_privsep_send_blob_req(ibuf, infd);
1370 77880158 2018-11-04 stsp return err;
1372 77880158 2018-11-04 stsp err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1373 77880158 2018-11-04 stsp if (err) {
1374 77880158 2018-11-04 stsp close(outfd_child);
1375 77880158 2018-11-04 stsp return err;
1378 77880158 2018-11-04 stsp err = got_privsep_recv_blob(size, ibuf);
1380 77880158 2018-11-04 stsp return err;
1382 77880158 2018-11-04 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1383 77880158 2018-11-04 stsp return got_error_from_errno();
1385 77880158 2018-11-04 stsp return err;
1388 77880158 2018-11-04 stsp const struct got_error *
1389 77880158 2018-11-04 stsp got_object_read_blob_privsep(size_t *size, int outfd, int infd,
1390 77880158 2018-11-04 stsp struct got_repository *repo)
1392 77880158 2018-11-04 stsp int imsg_fds[2];
1393 77880158 2018-11-04 stsp pid_t pid;
1394 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
1396 77880158 2018-11-04 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1397 77880158 2018-11-04 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1398 77880158 2018-11-04 stsp return request_blob(size, outfd, infd, ibuf);
1401 77880158 2018-11-04 stsp ibuf = calloc(1, sizeof(*ibuf));
1402 77880158 2018-11-04 stsp if (ibuf == NULL)
1403 77880158 2018-11-04 stsp return got_error_from_errno();
1405 77880158 2018-11-04 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1406 77880158 2018-11-04 stsp return got_error_from_errno();
1408 77880158 2018-11-04 stsp pid = fork();
1409 77880158 2018-11-04 stsp if (pid == -1)
1410 77880158 2018-11-04 stsp return got_error_from_errno();
1411 77880158 2018-11-04 stsp else if (pid == 0) {
1412 77880158 2018-11-04 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1413 77880158 2018-11-04 stsp repo->path);
1414 77880158 2018-11-04 stsp /* not reached */
1417 77880158 2018-11-04 stsp close(imsg_fds[1]);
1418 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1419 77880158 2018-11-04 stsp imsg_fds[0];
1420 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1421 77880158 2018-11-04 stsp imsg_init(ibuf, imsg_fds[0]);
1422 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1424 77880158 2018-11-04 stsp return request_blob(size, outfd, infd, ibuf);
1427 f4a881ce 2018-11-17 stsp static const struct got_error *
1428 f4a881ce 2018-11-17 stsp request_tag(struct got_tag_object **tag, struct got_repository *repo,
1429 f4a881ce 2018-11-17 stsp struct got_object *obj, int fd)
1431 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1432 f4a881ce 2018-11-17 stsp struct imsgbuf *ibuf;
1434 f4a881ce 2018-11-17 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1436 f4a881ce 2018-11-17 stsp err = got_privsep_send_obj_req(ibuf, fd, obj);
1438 f4a881ce 2018-11-17 stsp return err;
1440 f4a881ce 2018-11-17 stsp return got_privsep_recv_tag(tag, ibuf);
1443 f4a881ce 2018-11-17 stsp const struct got_error *
1444 f4a881ce 2018-11-17 stsp got_object_read_tag_privsep(struct got_tag_object **tag,
1445 f4a881ce 2018-11-17 stsp struct got_object *obj, int obj_fd, struct got_repository *repo)
1447 f4a881ce 2018-11-17 stsp int imsg_fds[2];
1448 f4a881ce 2018-11-17 stsp pid_t pid;
1449 f4a881ce 2018-11-17 stsp struct imsgbuf *ibuf;
1451 f4a881ce 2018-11-17 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1452 f4a881ce 2018-11-17 stsp return request_tag(tag, repo, obj, obj_fd);
1454 f4a881ce 2018-11-17 stsp ibuf = calloc(1, sizeof(*ibuf));
1455 f4a881ce 2018-11-17 stsp if (ibuf == NULL)
1456 f4a881ce 2018-11-17 stsp return got_error_from_errno();
1458 f4a881ce 2018-11-17 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1459 f4a881ce 2018-11-17 stsp return got_error_from_errno();
1461 f4a881ce 2018-11-17 stsp pid = fork();
1462 f4a881ce 2018-11-17 stsp if (pid == -1)
1463 f4a881ce 2018-11-17 stsp return got_error_from_errno();
1464 f4a881ce 2018-11-17 stsp else if (pid == 0) {
1465 f4a881ce 2018-11-17 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1466 f4a881ce 2018-11-17 stsp repo->path);
1467 f4a881ce 2018-11-17 stsp /* not reached */
1470 f4a881ce 2018-11-17 stsp close(imsg_fds[1]);
1471 f4a881ce 2018-11-17 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1472 f4a881ce 2018-11-17 stsp imsg_fds[0];
1473 f4a881ce 2018-11-17 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1474 f4a881ce 2018-11-17 stsp imsg_init(ibuf, imsg_fds[0]);
1475 f4a881ce 2018-11-17 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1477 f4a881ce 2018-11-17 stsp return request_tag(tag, repo, obj, obj_fd);