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 81a12da5 2020-09-09 naddy #include <unistd.h>
34 ab9a70b2 2017-11-06 stsp #include <zlib.h>
35 ab9a70b2 2017-11-06 stsp #include <ctype.h>
36 e40622f4 2020-07-23 stsp #include <libgen.h>
37 ab9a70b2 2017-11-06 stsp #include <limits.h>
38 2178c42e 2018-04-22 stsp #include <imsg.h>
39 788c352e 2018-06-16 stsp #include <time.h>
41 ab9a70b2 2017-11-06 stsp #include "got_error.h"
42 d71d75ad 2017-11-05 stsp #include "got_object.h"
43 ab9a70b2 2017-11-06 stsp #include "got_repository.h"
44 511a516b 2018-05-19 stsp #include "got_opentemp.h"
45 324d37e7 2019-05-11 stsp #include "got_path.h"
47 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
48 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
49 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
51 2178c42e 2018-04-22 stsp #include "got_lib_privsep.h"
52 6bef87be 2018-09-11 stsp #include "got_lib_object_idcache.h"
53 6bef87be 2018-09-11 stsp #include "got_lib_object_cache.h"
54 ad242220 2018-09-08 stsp #include "got_lib_object_parse.h"
55 15a94983 2018-12-23 stsp #include "got_lib_pack.h"
56 7bb0daa1 2018-06-21 stsp #include "got_lib_repository.h"
59 ab9a70b2 2017-11-06 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 3235492e 2018-04-01 stsp struct got_object_id *
63 3235492e 2018-04-01 stsp got_object_get_id(struct got_object *obj)
65 6402fb3c 2018-09-15 stsp return &obj->id;
68 bacc9935 2018-05-20 stsp const struct got_error *
69 bacc9935 2018-05-20 stsp got_object_get_id_str(char **outbuf, struct got_object *obj)
71 bacc9935 2018-05-20 stsp return got_object_id_str(outbuf, &obj->id);
74 15a94983 2018-12-23 stsp const struct got_error *
75 15a94983 2018-12-23 stsp got_object_get_type(int *type, struct got_repository *repo,
76 15a94983 2018-12-23 stsp struct got_object_id *id)
78 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
79 15a94983 2018-12-23 stsp struct got_object *obj;
81 15a94983 2018-12-23 stsp err = got_object_open(&obj, repo, id);
85 b107e67f 2018-01-19 stsp switch (obj->type) {
86 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_COMMIT:
87 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_TREE:
88 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_BLOB:
89 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
90 15a94983 2018-12-23 stsp *type = obj->type;
93 15a94983 2018-12-23 stsp err = got_error(GOT_ERR_OBJ_TYPE);
97 15a94983 2018-12-23 stsp got_object_close(obj);
101 90bdb554 2019-04-11 stsp const struct got_error *
102 90bdb554 2019-04-11 stsp got_object_get_path(char **path, struct got_object_id *id,
103 90bdb554 2019-04-11 stsp struct got_repository *repo)
105 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
106 7a132809 2018-07-23 stsp char *hex = NULL;
107 41d2888b 2019-08-11 stsp char *path_objects;
109 e6b1056e 2018-04-22 stsp *path = NULL;
111 41d2888b 2019-08-11 stsp path_objects = got_repo_get_path_objects(repo);
112 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
113 638f9024 2019-05-13 stsp return got_error_from_errno("got_repo_get_path_objects");
115 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
119 d1cda826 2017-11-06 stsp if (asprintf(path, "%s/%.2x/%s", path_objects,
120 d1cda826 2017-11-06 stsp id->sha1[0], hex + 2) == -1)
121 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
125 d1cda826 2017-11-06 stsp free(path_objects);
126 d1cda826 2017-11-06 stsp return err;
129 4ee4114f 2018-01-23 stsp static const struct got_error *
130 4796fb13 2018-12-23 stsp open_loose_object(int *fd, struct got_object_id *id,
131 4796fb13 2018-12-23 stsp struct got_repository *repo)
133 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
134 a1fd68d8 2018-01-12 stsp char *path;
136 90bdb554 2019-04-11 stsp err = got_object_get_path(&path, id, repo);
138 d1cda826 2017-11-06 stsp return err;
139 a5b57ccf 2019-04-11 stsp *fd = open(path, O_RDONLY | O_NOFOLLOW);
140 d5003b79 2018-04-22 stsp if (*fd == -1) {
141 e82b1d81 2019-07-27 stsp err = got_error_from_errno2("open", path);
145 4558fcd4 2018-01-14 stsp free(path);
146 2090a03d 2018-09-09 stsp return err;
149 2090a03d 2018-09-09 stsp static const struct got_error *
150 2090a03d 2018-09-09 stsp get_packfile_path(char **path_packfile, struct got_packidx *packidx)
152 2090a03d 2018-09-09 stsp size_t size;
154 2090a03d 2018-09-09 stsp /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
155 2090a03d 2018-09-09 stsp size = strlen(packidx->path_packidx) + 2;
156 2090a03d 2018-09-09 stsp if (size < GOT_PACKFILE_NAMELEN + 1)
157 63f810e6 2020-02-29 stsp return got_error_path(packidx->path_packidx, GOT_ERR_BAD_PATH);
159 08451938 2018-11-05 stsp *path_packfile = malloc(size);
160 2090a03d 2018-09-09 stsp if (*path_packfile == NULL)
161 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
163 2090a03d 2018-09-09 stsp /* Copy up to and excluding ".idx". */
164 2090a03d 2018-09-09 stsp if (strlcpy(*path_packfile, packidx->path_packidx,
165 2090a03d 2018-09-09 stsp size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
166 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_NO_SPACE);
168 2090a03d 2018-09-09 stsp if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
169 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_NO_SPACE);
171 2090a03d 2018-09-09 stsp return NULL;
174 2090a03d 2018-09-09 stsp static const struct got_error *
175 a158c901 2018-12-23 stsp request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
176 a158c901 2018-12-23 stsp struct got_object_id *id)
178 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
179 a158c901 2018-12-23 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
181 a158c901 2018-12-23 stsp err = got_privsep_send_packed_obj_req(ibuf, idx, id);
183 a158c901 2018-12-23 stsp return err;
185 a158c901 2018-12-23 stsp err = got_privsep_recv_obj(obj, ibuf);
187 a158c901 2018-12-23 stsp return err;
189 a158c901 2018-12-23 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
191 a158c901 2018-12-23 stsp return NULL;
194 da506691 2019-05-22 stsp static void
195 b48e2ddb 2019-05-22 stsp set_max_datasize(void)
197 b48e2ddb 2019-05-22 stsp struct rlimit rl;
199 b48e2ddb 2019-05-22 stsp if (getrlimit(RLIMIT_DATA, &rl) != 0)
202 b48e2ddb 2019-05-22 stsp rl.rlim_cur = rl.rlim_max;
203 b48e2ddb 2019-05-22 stsp setrlimit(RLIMIT_DATA, &rl);
206 a158c901 2018-12-23 stsp static const struct got_error *
207 711fb6e8 2018-12-23 stsp start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
209 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
210 a158c901 2018-12-23 stsp int imsg_fds[2];
212 a158c901 2018-12-23 stsp struct imsgbuf *ibuf;
214 a158c901 2018-12-23 stsp ibuf = calloc(1, sizeof(*ibuf));
215 a158c901 2018-12-23 stsp if (ibuf == NULL)
216 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
218 a158c901 2018-12-23 stsp pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
219 a158c901 2018-12-23 stsp if (pack->privsep_child == NULL) {
220 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
221 a158c901 2018-12-23 stsp free(ibuf);
222 a158c901 2018-12-23 stsp return err;
225 a158c901 2018-12-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
226 638f9024 2019-05-13 stsp err = got_error_from_errno("socketpair");
230 a158c901 2018-12-23 stsp pid = fork();
231 a158c901 2018-12-23 stsp if (pid == -1) {
232 638f9024 2019-05-13 stsp err = got_error_from_errno("fork");
234 a158c901 2018-12-23 stsp } else if (pid == 0) {
235 b48e2ddb 2019-05-22 stsp set_max_datasize();
236 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
237 a158c901 2018-12-23 stsp pack->path_packfile);
238 a158c901 2018-12-23 stsp /* not reached */
241 3a6ce05a 2019-02-11 stsp if (close(imsg_fds[1]) != 0)
242 638f9024 2019-05-13 stsp return got_error_from_errno("close");
243 a158c901 2018-12-23 stsp pack->privsep_child->imsg_fd = imsg_fds[0];
244 a158c901 2018-12-23 stsp pack->privsep_child->pid = pid;
245 a158c901 2018-12-23 stsp imsg_init(ibuf, imsg_fds[0]);
246 a158c901 2018-12-23 stsp pack->privsep_child->ibuf = ibuf;
248 a158c901 2018-12-23 stsp err = got_privsep_init_pack_child(ibuf, pack, packidx);
250 a158c901 2018-12-23 stsp const struct got_error *child_err;
251 a158c901 2018-12-23 stsp err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
252 a158c901 2018-12-23 stsp child_err = got_privsep_wait_for_child(
253 a158c901 2018-12-23 stsp pack->privsep_child->pid);
254 a158c901 2018-12-23 stsp if (child_err && err == NULL)
255 a158c901 2018-12-23 stsp err = child_err;
259 a158c901 2018-12-23 stsp free(ibuf);
260 a158c901 2018-12-23 stsp free(pack->privsep_child);
261 a158c901 2018-12-23 stsp pack->privsep_child = NULL;
263 a158c901 2018-12-23 stsp return err;
266 711fb6e8 2018-12-23 stsp static const struct got_error *
267 711fb6e8 2018-12-23 stsp read_packed_object_privsep(struct got_object **obj,
268 711fb6e8 2018-12-23 stsp struct got_repository *repo, struct got_pack *pack,
269 711fb6e8 2018-12-23 stsp struct got_packidx *packidx, int idx, struct got_object_id *id)
271 711fb6e8 2018-12-23 stsp const struct got_error *err = NULL;
273 711fb6e8 2018-12-23 stsp if (pack->privsep_child)
274 711fb6e8 2018-12-23 stsp return request_packed_object(obj, pack, idx, id);
276 711fb6e8 2018-12-23 stsp err = start_pack_privsep_child(pack, packidx);
278 711fb6e8 2018-12-23 stsp return err;
280 711fb6e8 2018-12-23 stsp return request_packed_object(obj, pack, idx, id);
284 a158c901 2018-12-23 stsp static const struct got_error *
285 2090a03d 2018-09-09 stsp open_packed_object(struct got_object **obj, struct got_object_id *id,
286 2090a03d 2018-09-09 stsp struct got_repository *repo)
288 2090a03d 2018-09-09 stsp const struct got_error *err = NULL;
289 2090a03d 2018-09-09 stsp struct got_pack *pack = NULL;
290 2090a03d 2018-09-09 stsp struct got_packidx *packidx = NULL;
292 2090a03d 2018-09-09 stsp char *path_packfile;
294 2090a03d 2018-09-09 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
296 2090a03d 2018-09-09 stsp return err;
298 2090a03d 2018-09-09 stsp err = get_packfile_path(&path_packfile, packidx);
300 2090a03d 2018-09-09 stsp return err;
302 2090a03d 2018-09-09 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
303 2090a03d 2018-09-09 stsp if (pack == NULL) {
304 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
309 a158c901 2018-12-23 stsp err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
313 2090a03d 2018-09-09 stsp free(path_packfile);
314 4558fcd4 2018-01-14 stsp return err;
317 9f2369b0 2018-12-24 stsp static const struct got_error *
318 9f2369b0 2018-12-24 stsp request_object(struct got_object **obj, struct got_repository *repo, int fd)
320 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
321 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
323 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
325 aea5f015 2018-12-24 stsp err = got_privsep_send_obj_req(ibuf, fd);
327 9f2369b0 2018-12-24 stsp return err;
329 9f2369b0 2018-12-24 stsp return got_privsep_recv_obj(obj, ibuf);
332 9f2369b0 2018-12-24 stsp static const struct got_error *
333 9f2369b0 2018-12-24 stsp read_object_header_privsep(struct got_object **obj, struct got_repository *repo,
334 9f2369b0 2018-12-24 stsp int obj_fd)
336 ddc7b220 2019-09-08 stsp const struct got_error *err;
337 9f2369b0 2018-12-24 stsp int imsg_fds[2];
339 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
341 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
342 9f2369b0 2018-12-24 stsp return request_object(obj, repo, obj_fd);
344 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
345 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
346 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
348 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
349 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
350 ddc7b220 2019-09-08 stsp free(ibuf);
351 ddc7b220 2019-09-08 stsp return err;
354 9f2369b0 2018-12-24 stsp pid = fork();
355 ddc7b220 2019-09-08 stsp if (pid == -1) {
356 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
357 ddc7b220 2019-09-08 stsp free(ibuf);
358 ddc7b220 2019-09-08 stsp return err;
360 9f2369b0 2018-12-24 stsp else if (pid == 0) {
361 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
362 9f2369b0 2018-12-24 stsp repo->path);
363 9f2369b0 2018-12-24 stsp /* not reached */
366 ddc7b220 2019-09-08 stsp if (close(imsg_fds[1]) != 0) {
367 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
368 ddc7b220 2019-09-08 stsp free(ibuf);
369 ddc7b220 2019-09-08 stsp return err;
371 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
372 9f2369b0 2018-12-24 stsp imsg_fds[0];
373 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
374 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
375 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
377 9f2369b0 2018-12-24 stsp return request_object(obj, repo, obj_fd);
381 4558fcd4 2018-01-14 stsp const struct got_error *
382 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
383 4558fcd4 2018-01-14 stsp struct got_object_id *id)
385 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
386 6c00b545 2018-01-17 stsp char *path;
389 7bb0daa1 2018-06-21 stsp *obj = got_repo_get_cached_object(repo, id);
390 7bb0daa1 2018-06-21 stsp if (*obj != NULL) {
391 7bb0daa1 2018-06-21 stsp (*obj)->refcnt++;
392 7bb0daa1 2018-06-21 stsp return NULL;
395 e82b1d81 2019-07-27 stsp err = open_packed_object(obj, id, repo);
396 e82b1d81 2019-07-27 stsp if (err && err->code != GOT_ERR_NO_OBJ)
397 e82b1d81 2019-07-27 stsp return err;
398 e82b1d81 2019-07-27 stsp if (*obj) {
399 e82b1d81 2019-07-27 stsp (*obj)->refcnt++;
400 e82b1d81 2019-07-27 stsp return got_repo_cache_object(repo, id, *obj);
403 90bdb554 2019-04-11 stsp err = got_object_get_path(&path, id, repo);
405 4558fcd4 2018-01-14 stsp return err;
407 a5b57ccf 2019-04-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
408 2178c42e 2018-04-22 stsp if (fd == -1) {
409 e82b1d81 2019-07-27 stsp if (errno == ENOENT)
410 e82b1d81 2019-07-27 stsp err = got_error_no_obj(id);
412 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", path);
415 9f2369b0 2018-12-24 stsp err = read_object_header_privsep(obj, repo, fd);
418 ab9a70b2 2017-11-06 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
421 59790a32 2018-09-15 stsp (*obj)->refcnt++;
422 59790a32 2018-09-15 stsp err = got_repo_cache_object(repo, id, *obj);
424 6c00b545 2018-01-17 stsp free(path);
425 ab9a70b2 2017-11-06 stsp return err;
429 6dfa2fd3 2018-02-12 stsp const struct got_error *
430 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
431 6dfa2fd3 2018-02-12 stsp const char *id_str)
433 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
435 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
436 6dd1ece6 2019-11-10 stsp return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
438 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
441 15a94983 2018-12-23 stsp const struct got_error *
442 15a94983 2018-12-23 stsp got_object_resolve_id_str(struct got_object_id **id,
443 15a94983 2018-12-23 stsp struct got_repository *repo, const char *id_str)
445 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
446 15a94983 2018-12-23 stsp struct got_object *obj;
448 15a94983 2018-12-23 stsp err = got_object_open_by_id_str(&obj, repo, id_str);
450 15a94983 2018-12-23 stsp return err;
452 15a94983 2018-12-23 stsp *id = got_object_id_dup(got_object_get_id(obj));
453 15a94983 2018-12-23 stsp got_object_close(obj);
454 15a94983 2018-12-23 stsp if (*id == NULL)
455 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
457 15a94983 2018-12-23 stsp return NULL;
460 434025f3 2018-09-16 stsp static const struct got_error *
461 a158c901 2018-12-23 stsp request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
462 a158c901 2018-12-23 stsp int pack_idx, struct got_object_id *id)
464 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
466 a158c901 2018-12-23 stsp err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
469 a158c901 2018-12-23 stsp return err;
471 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
473 ca6e02ac 2020-01-07 stsp return err;
475 ca6e02ac 2020-01-07 stsp (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
476 ca6e02ac 2020-01-07 stsp return NULL;
479 a158c901 2018-12-23 stsp static const struct got_error *
480 a158c901 2018-12-23 stsp read_packed_commit_privsep(struct got_commit_object **commit,
481 a158c901 2018-12-23 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
482 a158c901 2018-12-23 stsp struct got_object_id *id)
484 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
486 a158c901 2018-12-23 stsp if (pack->privsep_child)
487 a158c901 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
489 711fb6e8 2018-12-23 stsp err = start_pack_privsep_child(pack, packidx);
491 a158c901 2018-12-23 stsp return err;
493 711fb6e8 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
496 9f2369b0 2018-12-24 stsp static const struct got_error *
497 9f2369b0 2018-12-24 stsp request_commit(struct got_commit_object **commit, struct got_repository *repo,
500 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
501 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
503 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
505 9f2369b0 2018-12-24 stsp err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
507 9f2369b0 2018-12-24 stsp return err;
509 9f2369b0 2018-12-24 stsp return got_privsep_recv_commit(commit, ibuf);
512 9f2369b0 2018-12-24 stsp static const struct got_error *
513 9f2369b0 2018-12-24 stsp read_commit_privsep(struct got_commit_object **commit, int obj_fd,
514 9f2369b0 2018-12-24 stsp struct got_repository *repo)
516 ddc7b220 2019-09-08 stsp const struct got_error *err;
517 9f2369b0 2018-12-24 stsp int imsg_fds[2];
519 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
521 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
522 9f2369b0 2018-12-24 stsp return request_commit(commit, repo, obj_fd);
524 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
525 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
526 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
528 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
529 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
530 ddc7b220 2019-09-08 stsp free(ibuf);
531 ddc7b220 2019-09-08 stsp return err;
534 9f2369b0 2018-12-24 stsp pid = fork();
535 ddc7b220 2019-09-08 stsp if (pid == -1) {
536 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
537 ddc7b220 2019-09-08 stsp free(ibuf);
538 ddc7b220 2019-09-08 stsp return err;
540 9f2369b0 2018-12-24 stsp else if (pid == 0) {
541 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
542 9f2369b0 2018-12-24 stsp repo->path);
543 9f2369b0 2018-12-24 stsp /* not reached */
546 ddc7b220 2019-09-08 stsp if (close(imsg_fds[1]) != 0) {
547 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
548 ddc7b220 2019-09-08 stsp free(ibuf);
549 ddc7b220 2019-09-08 stsp return err;
551 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
552 9f2369b0 2018-12-24 stsp imsg_fds[0];
553 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
554 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
555 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
557 9f2369b0 2018-12-24 stsp return request_commit(commit, repo, obj_fd);
561 a158c901 2018-12-23 stsp static const struct got_error *
562 434025f3 2018-09-16 stsp open_commit(struct got_commit_object **commit,
563 1785f84a 2018-12-23 stsp struct got_repository *repo, struct got_object_id *id, int check_cache)
565 434025f3 2018-09-16 stsp const struct got_error *err = NULL;
566 1785f84a 2018-12-23 stsp struct got_packidx *packidx = NULL;
568 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
570 434025f3 2018-09-16 stsp if (check_cache) {
571 1785f84a 2018-12-23 stsp *commit = got_repo_get_cached_commit(repo, id);
572 434025f3 2018-09-16 stsp if (*commit != NULL) {
573 434025f3 2018-09-16 stsp (*commit)->refcnt++;
574 434025f3 2018-09-16 stsp return NULL;
577 434025f3 2018-09-16 stsp *commit = NULL;
579 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
580 e82b1d81 2019-07-27 stsp if (err == NULL) {
581 1785f84a 2018-12-23 stsp struct got_pack *pack = NULL;
583 1785f84a 2018-12-23 stsp err = get_packfile_path(&path_packfile, packidx);
585 1785f84a 2018-12-23 stsp return err;
587 1785f84a 2018-12-23 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
588 434025f3 2018-09-16 stsp if (pack == NULL) {
589 1785f84a 2018-12-23 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
594 a158c901 2018-12-23 stsp err = read_packed_commit_privsep(commit, pack,
595 1785f84a 2018-12-23 stsp packidx, idx, id);
596 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
599 e82b1d81 2019-07-27 stsp err = open_loose_object(&fd, id, repo);
601 e82b1d81 2019-07-27 stsp return err;
602 9f2369b0 2018-12-24 stsp err = read_commit_privsep(commit, fd, repo);
605 434025f3 2018-09-16 stsp if (err == NULL) {
606 434025f3 2018-09-16 stsp (*commit)->refcnt++;
607 1785f84a 2018-12-23 stsp err = got_repo_cache_commit(repo, id, *commit);
610 8d2c5ea3 2019-08-13 stsp free(path_packfile);
611 e32baab7 2018-11-05 stsp return err;
614 e32baab7 2018-11-05 stsp const struct got_error *
615 e32baab7 2018-11-05 stsp got_object_open_as_commit(struct got_commit_object **commit,
616 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object_id *id)
618 e32baab7 2018-11-05 stsp *commit = got_repo_get_cached_commit(repo, id);
619 e32baab7 2018-11-05 stsp if (*commit != NULL) {
620 e32baab7 2018-11-05 stsp (*commit)->refcnt++;
621 e32baab7 2018-11-05 stsp return NULL;
624 1785f84a 2018-12-23 stsp return open_commit(commit, repo, id, 0);
627 e32baab7 2018-11-05 stsp const struct got_error *
628 e32baab7 2018-11-05 stsp got_object_commit_open(struct got_commit_object **commit,
629 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object *obj)
631 1785f84a 2018-12-23 stsp return open_commit(commit, repo, got_object_get_id(obj), 1);
634 434025f3 2018-09-16 stsp const struct got_error *
635 dbc6a6b6 2018-07-12 stsp got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
637 dbc6a6b6 2018-07-12 stsp const struct got_error *err = NULL;
639 dbc6a6b6 2018-07-12 stsp *qid = calloc(1, sizeof(**qid));
640 dbc6a6b6 2018-07-12 stsp if (*qid == NULL)
641 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
643 dbc6a6b6 2018-07-12 stsp (*qid)->id = got_object_id_dup(id);
644 dbc6a6b6 2018-07-12 stsp if ((*qid)->id == NULL) {
645 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
646 fa2f6902 2018-07-23 stsp got_object_qid_free(*qid);
647 dbc6a6b6 2018-07-12 stsp *qid = NULL;
648 dbc6a6b6 2018-07-12 stsp return err;
651 dbc6a6b6 2018-07-12 stsp return NULL;
654 a158c901 2018-12-23 stsp static const struct got_error *
655 13c729f7 2018-12-24 stsp request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
656 13c729f7 2018-12-24 stsp int pack_idx, struct got_object_id *id)
658 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
660 13c729f7 2018-12-24 stsp err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
663 a158c901 2018-12-23 stsp return err;
665 a158c901 2018-12-23 stsp return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
668 71eb0e7f 2018-09-16 stsp static const struct got_error *
669 13c729f7 2018-12-24 stsp read_packed_tree_privsep(struct got_tree_object **tree,
670 13c729f7 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
671 13c729f7 2018-12-24 stsp struct got_object_id *id)
673 13c729f7 2018-12-24 stsp const struct got_error *err = NULL;
675 13c729f7 2018-12-24 stsp if (pack->privsep_child)
676 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
678 13c729f7 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
680 13c729f7 2018-12-24 stsp return err;
682 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
685 13c729f7 2018-12-24 stsp static const struct got_error *
686 9f2369b0 2018-12-24 stsp request_tree(struct got_tree_object **tree, struct got_repository *repo,
689 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
690 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
692 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
694 9f2369b0 2018-12-24 stsp err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
696 9f2369b0 2018-12-24 stsp return err;
698 9f2369b0 2018-12-24 stsp return got_privsep_recv_tree(tree, ibuf);
701 9f2369b0 2018-12-24 stsp const struct got_error *
702 9f2369b0 2018-12-24 stsp read_tree_privsep(struct got_tree_object **tree, int obj_fd,
703 9f2369b0 2018-12-24 stsp struct got_repository *repo)
705 ddc7b220 2019-09-08 stsp const struct got_error *err;
706 9f2369b0 2018-12-24 stsp int imsg_fds[2];
708 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
710 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
711 9f2369b0 2018-12-24 stsp return request_tree(tree, repo, obj_fd);
713 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
714 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
715 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
717 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
718 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
719 ddc7b220 2019-09-08 stsp free(ibuf);
720 ddc7b220 2019-09-08 stsp return err;
723 9f2369b0 2018-12-24 stsp pid = fork();
724 ddc7b220 2019-09-08 stsp if (pid == -1) {
725 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
726 ddc7b220 2019-09-08 stsp free(ibuf);
727 ddc7b220 2019-09-08 stsp return err;
729 9f2369b0 2018-12-24 stsp else if (pid == 0) {
730 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
731 9f2369b0 2018-12-24 stsp repo->path);
732 9f2369b0 2018-12-24 stsp /* not reached */
735 ddc7b220 2019-09-08 stsp if (close(imsg_fds[1]) != 0) {
736 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
737 ddc7b220 2019-09-08 stsp free(ibuf);
738 ddc7b220 2019-09-08 stsp return err;
740 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
741 9f2369b0 2018-12-24 stsp imsg_fds[0];
742 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
743 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
744 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
747 9f2369b0 2018-12-24 stsp return request_tree(tree, repo, obj_fd);
750 9f2369b0 2018-12-24 stsp static const struct got_error *
751 13c729f7 2018-12-24 stsp open_tree(struct got_tree_object **tree, struct got_repository *repo,
752 13c729f7 2018-12-24 stsp struct got_object_id *id, int check_cache)
754 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
755 13c729f7 2018-12-24 stsp struct got_packidx *packidx = NULL;
757 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
759 71eb0e7f 2018-09-16 stsp if (check_cache) {
760 13c729f7 2018-12-24 stsp *tree = got_repo_get_cached_tree(repo, id);
761 71eb0e7f 2018-09-16 stsp if (*tree != NULL) {
762 71eb0e7f 2018-09-16 stsp (*tree)->refcnt++;
763 71eb0e7f 2018-09-16 stsp return NULL;
766 71eb0e7f 2018-09-16 stsp *tree = NULL;
768 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
769 e82b1d81 2019-07-27 stsp if (err == NULL) {
770 13c729f7 2018-12-24 stsp struct got_pack *pack = NULL;
772 13c729f7 2018-12-24 stsp err = get_packfile_path(&path_packfile, packidx);
774 13c729f7 2018-12-24 stsp return err;
776 13c729f7 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
777 e7885405 2018-09-10 stsp if (pack == NULL) {
778 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
783 13c729f7 2018-12-24 stsp err = read_packed_tree_privsep(tree, pack,
784 13c729f7 2018-12-24 stsp packidx, idx, id);
785 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
788 e82b1d81 2019-07-27 stsp err = open_loose_object(&fd, id, repo);
790 e82b1d81 2019-07-27 stsp return err;
791 9f2369b0 2018-12-24 stsp err = read_tree_privsep(tree, fd, repo);
794 f6be5c30 2018-06-22 stsp if (err == NULL) {
795 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
796 13c729f7 2018-12-24 stsp err = got_repo_cache_tree(repo, id, *tree);
799 8d2c5ea3 2019-08-13 stsp free(path_packfile);
800 776d4d29 2018-06-17 stsp return err;
803 776d4d29 2018-06-17 stsp const struct got_error *
804 776d4d29 2018-06-17 stsp got_object_open_as_tree(struct got_tree_object **tree,
805 776d4d29 2018-06-17 stsp struct got_repository *repo, struct got_object_id *id)
807 e8eb494a 2018-09-16 stsp *tree = got_repo_get_cached_tree(repo, id);
808 e8eb494a 2018-09-16 stsp if (*tree != NULL) {
809 e8eb494a 2018-09-16 stsp (*tree)->refcnt++;
810 e8eb494a 2018-09-16 stsp return NULL;
813 13c729f7 2018-12-24 stsp return open_tree(tree, repo, id, 0);
816 71eb0e7f 2018-09-16 stsp const struct got_error *
817 71eb0e7f 2018-09-16 stsp got_object_tree_open(struct got_tree_object **tree,
818 71eb0e7f 2018-09-16 stsp struct got_repository *repo, struct got_object *obj)
820 13c729f7 2018-12-24 stsp return open_tree(tree, repo, got_object_get_id(obj), 1);
824 56e0773d 2019-11-28 stsp got_object_tree_get_nentries(struct got_tree_object *tree)
826 56e0773d 2019-11-28 stsp return tree->nentries;
829 56e0773d 2019-11-28 stsp struct got_tree_entry *
830 56e0773d 2019-11-28 stsp got_object_tree_get_first_entry(struct got_tree_object *tree)
832 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, 0);
835 56e0773d 2019-11-28 stsp struct got_tree_entry *
836 56e0773d 2019-11-28 stsp got_object_tree_get_last_entry(struct got_tree_object *tree)
838 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, tree->nentries - 1);
841 56e0773d 2019-11-28 stsp struct got_tree_entry *
842 56e0773d 2019-11-28 stsp got_object_tree_get_entry(struct got_tree_object *tree, int i)
844 56e0773d 2019-11-28 stsp if (i < 0 || i >= tree->nentries)
845 56e0773d 2019-11-28 stsp return NULL;
846 56e0773d 2019-11-28 stsp return &tree->entries[i];
850 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(struct got_tree_entry *te)
852 56e0773d 2019-11-28 stsp return te->mode;
855 56e0773d 2019-11-28 stsp const char *
856 56e0773d 2019-11-28 stsp got_tree_entry_get_name(struct got_tree_entry *te)
858 56e0773d 2019-11-28 stsp return &te->name[0];
861 56e0773d 2019-11-28 stsp struct got_object_id *
862 56e0773d 2019-11-28 stsp got_tree_entry_get_id(struct got_tree_entry *te)
864 56e0773d 2019-11-28 stsp return &te->id;
867 0d6c6ee3 2020-05-20 stsp const struct got_error *
868 af57b12a 2020-07-23 stsp got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
870 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
871 32596e16 2020-07-23 stsp size_t len, totlen, hdrlen, offset;
875 659dc16e 2020-07-23 stsp hdrlen = got_object_blob_get_hdrlen(blob);
876 659dc16e 2020-07-23 stsp totlen = 0;
877 32596e16 2020-07-23 stsp offset = 0;
881 659dc16e 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
883 af57b12a 2020-07-23 stsp return err;
885 659dc16e 2020-07-23 stsp if (len == 0)
888 659dc16e 2020-07-23 stsp totlen += len - hdrlen;
889 af57b12a 2020-07-23 stsp p = realloc(*s, totlen + 1);
890 659dc16e 2020-07-23 stsp if (p == NULL) {
891 659dc16e 2020-07-23 stsp err = got_error_from_errno("realloc");
894 af57b12a 2020-07-23 stsp return err;
897 659dc16e 2020-07-23 stsp /* Skip blob object header first time around. */
898 af57b12a 2020-07-23 stsp memcpy(*s + offset,
899 f8f7c882 2020-07-23 stsp got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
900 659dc16e 2020-07-23 stsp hdrlen = 0;
901 32596e16 2020-07-23 stsp offset = totlen;
902 659dc16e 2020-07-23 stsp } while (len > 0);
904 af57b12a 2020-07-23 stsp (*s)[totlen] = '\0';
905 af57b12a 2020-07-23 stsp return NULL;
908 af57b12a 2020-07-23 stsp const struct got_error *
909 af57b12a 2020-07-23 stsp got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
910 af57b12a 2020-07-23 stsp struct got_repository *repo)
912 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
913 af57b12a 2020-07-23 stsp struct got_blob_object *blob = NULL;
915 af57b12a 2020-07-23 stsp *link_target = NULL;
917 af57b12a 2020-07-23 stsp if (!got_object_tree_entry_is_symlink(te))
918 af57b12a 2020-07-23 stsp return got_error(GOT_ERR_TREE_ENTRY_TYPE);
920 af57b12a 2020-07-23 stsp err = got_object_open_as_blob(&blob, repo,
921 af57b12a 2020-07-23 stsp got_tree_entry_get_id(te), PATH_MAX);
923 af57b12a 2020-07-23 stsp return err;
925 af57b12a 2020-07-23 stsp err = got_object_blob_read_to_str(link_target, blob);
926 af57b12a 2020-07-23 stsp got_object_blob_close(blob);
928 659dc16e 2020-07-23 stsp free(*link_target);
929 659dc16e 2020-07-23 stsp *link_target = NULL;
931 0d6c6ee3 2020-05-20 stsp return err;
935 56e0773d 2019-11-28 stsp got_tree_entry_get_index(struct got_tree_entry *te)
937 56e0773d 2019-11-28 stsp return te->idx;
940 56e0773d 2019-11-28 stsp struct got_tree_entry *
941 56e0773d 2019-11-28 stsp got_tree_entry_get_next(struct got_tree_object *tree,
942 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
944 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx + 1);
947 56e0773d 2019-11-28 stsp struct got_tree_entry *
948 56e0773d 2019-11-28 stsp got_tree_entry_get_prev(struct got_tree_object *tree,
949 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
951 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx - 1);
954 3840f4c9 2018-09-12 stsp static const struct got_error *
955 ac544f8c 2019-01-13 stsp request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
956 ebc55e2d 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
957 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
959 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
960 3840f4c9 2018-09-12 stsp int outfd_child;
961 3840f4c9 2018-09-12 stsp int basefd, accumfd; /* temporary files for delta application */
963 3840f4c9 2018-09-12 stsp basefd = got_opentempfd();
964 3840f4c9 2018-09-12 stsp if (basefd == -1)
965 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
966 3840f4c9 2018-09-12 stsp accumfd = got_opentempfd();
967 3840f4c9 2018-09-12 stsp if (accumfd == -1)
968 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
970 3840f4c9 2018-09-12 stsp outfd_child = dup(outfd);
971 3840f4c9 2018-09-12 stsp if (outfd_child == -1)
972 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
974 ebc55e2d 2018-12-24 stsp err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
976 3840f4c9 2018-09-12 stsp return err;
978 3840f4c9 2018-09-12 stsp err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
979 3840f4c9 2018-09-12 stsp outfd_child);
981 41496140 2019-02-21 stsp close(basefd);
982 41496140 2019-02-21 stsp close(accumfd);
983 3840f4c9 2018-09-12 stsp return err;
986 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
989 3840f4c9 2018-09-12 stsp close(accumfd);
990 3840f4c9 2018-09-12 stsp return err;
993 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
996 3840f4c9 2018-09-12 stsp return err;
998 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen,
999 ebc55e2d 2018-12-24 stsp pack->privsep_child->ibuf);
1001 3840f4c9 2018-09-12 stsp return err;
1003 3840f4c9 2018-09-12 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1004 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1006 3840f4c9 2018-09-12 stsp return err;
1009 ebc55e2d 2018-12-24 stsp static const struct got_error *
1010 ac544f8c 2019-01-13 stsp read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1011 ac544f8c 2019-01-13 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1012 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
1014 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
1016 ebc55e2d 2018-12-24 stsp if (pack->privsep_child == NULL) {
1017 ebc55e2d 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
1019 ebc55e2d 2018-12-24 stsp return err;
1022 ac544f8c 2019-01-13 stsp return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1026 9f2369b0 2018-12-24 stsp static const struct got_error *
1027 ac544f8c 2019-01-13 stsp request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1028 ac544f8c 2019-01-13 stsp int infd, struct imsgbuf *ibuf)
1030 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1031 9f2369b0 2018-12-24 stsp int outfd_child;
1033 9f2369b0 2018-12-24 stsp outfd_child = dup(outfd);
1034 9f2369b0 2018-12-24 stsp if (outfd_child == -1)
1035 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1037 9f2369b0 2018-12-24 stsp err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
1039 9f2369b0 2018-12-24 stsp return err;
1041 9f2369b0 2018-12-24 stsp err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1043 9f2369b0 2018-12-24 stsp return err;
1045 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1047 9f2369b0 2018-12-24 stsp return err;
1049 9f2369b0 2018-12-24 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1050 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
1052 9f2369b0 2018-12-24 stsp return err;
1055 9f2369b0 2018-12-24 stsp static const struct got_error *
1056 ac544f8c 2019-01-13 stsp read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1057 9f2369b0 2018-12-24 stsp int outfd, int infd, struct got_repository *repo)
1059 ddc7b220 2019-09-08 stsp const struct got_error *err;
1060 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1061 9f2369b0 2018-12-24 stsp pid_t pid;
1062 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1064 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1065 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1066 ac544f8c 2019-01-13 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1069 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1070 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1071 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1073 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1074 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1075 ddc7b220 2019-09-08 stsp free(ibuf);
1076 ddc7b220 2019-09-08 stsp return err;
1079 9f2369b0 2018-12-24 stsp pid = fork();
1080 ddc7b220 2019-09-08 stsp if (pid == -1) {
1081 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1082 ddc7b220 2019-09-08 stsp free(ibuf);
1083 ddc7b220 2019-09-08 stsp return err;
1085 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1086 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1087 9f2369b0 2018-12-24 stsp repo->path);
1088 9f2369b0 2018-12-24 stsp /* not reached */
1091 ddc7b220 2019-09-08 stsp if (close(imsg_fds[1]) != 0) {
1092 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1093 ddc7b220 2019-09-08 stsp free(ibuf);
1094 ddc7b220 2019-09-08 stsp return err;
1096 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1097 9f2369b0 2018-12-24 stsp imsg_fds[0];
1098 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1099 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1100 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1102 ac544f8c 2019-01-13 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1105 ebc55e2d 2018-12-24 stsp static const struct got_error *
1106 ebc55e2d 2018-12-24 stsp open_blob(struct got_blob_object **blob, struct got_repository *repo,
1107 ebc55e2d 2018-12-24 stsp struct got_object_id *id, size_t blocksize)
1109 ebc55e2d 2018-12-24 stsp const struct got_error *err = NULL;
1110 ebc55e2d 2018-12-24 stsp struct got_packidx *packidx = NULL;
1112 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1113 ac544f8c 2019-01-13 stsp uint8_t *outbuf;
1114 e82b1d81 2019-07-27 stsp int outfd;
1115 ebc55e2d 2018-12-24 stsp size_t size, hdrlen;
1116 ebc55e2d 2018-12-24 stsp struct stat sb;
1118 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
1119 4558fcd4 2018-01-14 stsp if (*blob == NULL)
1120 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1122 55da3778 2018-09-10 stsp outfd = got_opentempfd();
1123 55da3778 2018-09-10 stsp if (outfd == -1)
1124 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
1126 062ebb78 2018-07-12 stsp (*blob)->read_buf = malloc(blocksize);
1127 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
1128 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1129 c7254d79 2018-04-24 stsp goto done;
1132 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1133 e82b1d81 2019-07-27 stsp if (err == NULL) {
1134 ebc55e2d 2018-12-24 stsp struct got_pack *pack = NULL;
1136 ebc55e2d 2018-12-24 stsp err = get_packfile_path(&path_packfile, packidx);
1138 ebc55e2d 2018-12-24 stsp goto done;
1140 ebc55e2d 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1141 55da3778 2018-09-10 stsp if (pack == NULL) {
1142 ebc55e2d 2018-12-24 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1145 55da3778 2018-09-10 stsp goto done;
1147 ac544f8c 2019-01-13 stsp err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1148 ac544f8c 2019-01-13 stsp pack, packidx, idx, id);
1149 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1152 e82b1d81 2019-07-27 stsp err = open_loose_object(&infd, id, repo);
1154 e82b1d81 2019-07-27 stsp goto done;
1155 ac544f8c 2019-01-13 stsp err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1159 55da3778 2018-09-10 stsp goto done;
1161 de060dff 2018-12-24 stsp if (hdrlen > size) {
1162 ebc55e2d 2018-12-24 stsp err = got_error(GOT_ERR_BAD_OBJ_HDR);
1163 ebc55e2d 2018-12-24 stsp goto done;
1166 ac544f8c 2019-01-13 stsp if (outbuf) {
1167 3a6ce05a 2019-02-11 stsp if (close(outfd) != 0 && err == NULL)
1168 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1169 ac544f8c 2019-01-13 stsp outfd = -1;
1170 ac544f8c 2019-01-13 stsp (*blob)->f = fmemopen(outbuf, size, "rb");
1171 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1172 638f9024 2019-05-13 stsp err = got_error_from_errno("fmemopen");
1173 ac544f8c 2019-01-13 stsp free(outbuf);
1174 ac544f8c 2019-01-13 stsp goto done;
1176 ac544f8c 2019-01-13 stsp (*blob)->data = outbuf;
1178 ac544f8c 2019-01-13 stsp if (fstat(outfd, &sb) == -1) {
1179 638f9024 2019-05-13 stsp err = got_error_from_errno("fstat");
1180 ac544f8c 2019-01-13 stsp goto done;
1183 ac544f8c 2019-01-13 stsp if (sb.st_size != size) {
1184 ac544f8c 2019-01-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1185 ac544f8c 2019-01-13 stsp goto done;
1188 ac544f8c 2019-01-13 stsp (*blob)->f = fdopen(outfd, "rb");
1189 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1190 638f9024 2019-05-13 stsp err = got_error_from_errno("fdopen");
1191 ac544f8c 2019-01-13 stsp close(outfd);
1192 ac544f8c 2019-01-13 stsp outfd = -1;
1193 ac544f8c 2019-01-13 stsp goto done;
1197 ebc55e2d 2018-12-24 stsp (*blob)->hdrlen = hdrlen;
1198 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
1199 ebc55e2d 2018-12-24 stsp memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1202 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1203 55da3778 2018-09-10 stsp if (err) {
1204 55da3778 2018-09-10 stsp if (*blob) {
1205 7baf5860 2019-03-19 stsp got_object_blob_close(*blob);
1206 55da3778 2018-09-10 stsp *blob = NULL;
1207 55da3778 2018-09-10 stsp } else if (outfd != -1)
1208 55da3778 2018-09-10 stsp close(outfd);
1210 a19581a2 2018-06-21 stsp return err;
1213 a19581a2 2018-06-21 stsp const struct got_error *
1214 a19581a2 2018-06-21 stsp got_object_open_as_blob(struct got_blob_object **blob,
1215 a19581a2 2018-06-21 stsp struct got_repository *repo, struct got_object_id *id,
1216 a19581a2 2018-06-21 stsp size_t blocksize)
1218 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, id, blocksize);
1221 ebc55e2d 2018-12-24 stsp const struct got_error *
1222 ebc55e2d 2018-12-24 stsp got_object_blob_open(struct got_blob_object **blob,
1223 ebc55e2d 2018-12-24 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
1225 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1228 fb43ecf1 2019-02-11 stsp const struct got_error *
1229 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
1231 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL;
1232 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
1233 7baf5860 2019-03-19 stsp if (blob->f && fclose(blob->f) != 0)
1234 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1235 ac544f8c 2019-01-13 stsp free(blob->data);
1236 68482ea3 2017-11-27 stsp free(blob);
1237 fb43ecf1 2019-02-11 stsp return err;
1241 8ba819a3 2020-07-23 stsp got_object_blob_rewind(struct got_blob_object *blob)
1243 8ba819a3 2020-07-23 stsp if (blob->f)
1244 8ba819a3 2020-07-23 stsp rewind(blob->f);
1248 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1250 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1254 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
1256 f934cf2c 2018-02-12 stsp return blob->hdrlen;
1259 f934cf2c 2018-02-12 stsp const uint8_t *
1260 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
1262 f934cf2c 2018-02-12 stsp return blob->read_buf;
1265 68482ea3 2017-11-27 stsp const struct got_error *
1266 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1270 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1271 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
1272 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
1273 eb651edf 2018-02-11 stsp *outlenp = n;
1274 35e9ba5d 2018-06-21 stsp return NULL;
1277 35e9ba5d 2018-06-21 stsp const struct got_error *
1278 f595d9bd 2019-08-14 stsp got_object_blob_dump_to_file(size_t *filesize, int *nlines,
1279 6c4c42e0 2019-06-24 stsp off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1281 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
1282 b6752625 2018-12-24 stsp size_t n, len, hdrlen;
1283 84451b3e 2018-07-10 stsp const uint8_t *buf;
1285 6c4c42e0 2019-06-24 stsp size_t noffsets = 0;
1286 f595d9bd 2019-08-14 stsp off_t off = 0, total_len = 0;
1288 6c4c42e0 2019-06-24 stsp if (line_offsets)
1289 6c4c42e0 2019-06-24 stsp *line_offsets = NULL;
1290 f595d9bd 2019-08-14 stsp if (filesize)
1291 f595d9bd 2019-08-14 stsp *filesize = 0;
1292 84451b3e 2018-07-10 stsp if (nlines)
1293 84451b3e 2018-07-10 stsp *nlines = 0;
1295 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1297 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
1299 35e9ba5d 2018-06-21 stsp return err;
1300 35e9ba5d 2018-06-21 stsp if (len == 0)
1302 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
1303 b02560ec 2019-08-19 stsp i = hdrlen;
1304 78695fb7 2019-08-12 stsp if (line_offsets && nlines) {
1305 78695fb7 2019-08-12 stsp if (*line_offsets == NULL) {
1306 78695fb7 2019-08-12 stsp /* Have some data but perhaps no '\n'. */
1307 78695fb7 2019-08-12 stsp noffsets = 1;
1308 78695fb7 2019-08-12 stsp *nlines = 1;
1309 f595d9bd 2019-08-14 stsp *line_offsets = calloc(1, sizeof(**line_offsets));
1310 78695fb7 2019-08-12 stsp if (*line_offsets == NULL)
1311 845785d4 2020-02-02 tracey return got_error_from_errno("calloc");
1313 b02560ec 2019-08-19 stsp /* Skip forward over end of first line. */
1314 b02560ec 2019-08-19 stsp while (i < len) {
1315 b02560ec 2019-08-19 stsp if (buf[i] == '\n')
1320 b02560ec 2019-08-19 stsp /* Scan '\n' offsets in remaining chunk of data. */
1321 b02560ec 2019-08-19 stsp while (i < len) {
1322 b02560ec 2019-08-19 stsp if (buf[i] != '\n') {
1326 f595d9bd 2019-08-14 stsp (*nlines)++;
1327 78695fb7 2019-08-12 stsp if (noffsets < *nlines) {
1328 78695fb7 2019-08-12 stsp off_t *o = recallocarray(*line_offsets,
1329 78695fb7 2019-08-12 stsp noffsets, *nlines,
1330 78695fb7 2019-08-12 stsp sizeof(**line_offsets));
1331 78695fb7 2019-08-12 stsp if (o == NULL) {
1332 78695fb7 2019-08-12 stsp free(*line_offsets);
1333 78695fb7 2019-08-12 stsp *line_offsets = NULL;
1334 78695fb7 2019-08-12 stsp return got_error_from_errno(
1335 78695fb7 2019-08-12 stsp "recallocarray");
1337 78695fb7 2019-08-12 stsp *line_offsets = o;
1338 78695fb7 2019-08-12 stsp noffsets = *nlines;
1340 f595d9bd 2019-08-14 stsp off = total_len + i - hdrlen + 1;
1341 f595d9bd 2019-08-14 stsp (*line_offsets)[*nlines - 1] = off;
1345 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
1346 454a6b59 2018-12-24 stsp n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1347 b6752625 2018-12-24 stsp if (n != len - hdrlen)
1348 b6752625 2018-12-24 stsp return got_ferror(outfile, GOT_ERR_IO);
1349 f595d9bd 2019-08-14 stsp total_len += len - hdrlen;
1350 35e9ba5d 2018-06-21 stsp hdrlen = 0;
1351 35e9ba5d 2018-06-21 stsp } while (len != 0);
1353 cbe7f848 2019-02-11 stsp if (fflush(outfile) != 0)
1354 638f9024 2019-05-13 stsp return got_error_from_errno("fflush");
1355 35e9ba5d 2018-06-21 stsp rewind(outfile);
1357 f595d9bd 2019-08-14 stsp if (filesize)
1358 f595d9bd 2019-08-14 stsp *filesize = total_len;
1360 776d4d29 2018-06-17 stsp return NULL;
1363 f4a881ce 2018-11-17 stsp static const struct got_error *
1364 268f7291 2018-12-24 stsp request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1365 268f7291 2018-12-24 stsp int pack_idx, struct got_object_id *id)
1367 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
1369 268f7291 2018-12-24 stsp err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1370 268f7291 2018-12-24 stsp pack_idx);
1372 a158c901 2018-12-23 stsp return err;
1374 a158c901 2018-12-23 stsp return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1377 268f7291 2018-12-24 stsp static const struct got_error *
1378 268f7291 2018-12-24 stsp read_packed_tag_privsep(struct got_tag_object **tag,
1379 268f7291 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1380 268f7291 2018-12-24 stsp struct got_object_id *id)
1382 268f7291 2018-12-24 stsp const struct got_error *err = NULL;
1384 268f7291 2018-12-24 stsp if (pack->privsep_child)
1385 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1387 268f7291 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
1389 268f7291 2018-12-24 stsp return err;
1391 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1394 9f2369b0 2018-12-24 stsp static const struct got_error *
1395 9f2369b0 2018-12-24 stsp request_tag(struct got_tag_object **tag, struct got_repository *repo,
1398 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1399 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1401 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1403 9f2369b0 2018-12-24 stsp err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1405 9f2369b0 2018-12-24 stsp return err;
1407 9f2369b0 2018-12-24 stsp return got_privsep_recv_tag(tag, ibuf);
1410 9f2369b0 2018-12-24 stsp static const struct got_error *
1411 9f2369b0 2018-12-24 stsp read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1412 9f2369b0 2018-12-24 stsp struct got_repository *repo)
1414 ddc7b220 2019-09-08 stsp const struct got_error *err;
1415 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1416 9f2369b0 2018-12-24 stsp pid_t pid;
1417 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1419 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1420 9f2369b0 2018-12-24 stsp return request_tag(tag, repo, obj_fd);
1422 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1423 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1424 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1426 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1427 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1428 ddc7b220 2019-09-08 stsp free(ibuf);
1429 ddc7b220 2019-09-08 stsp return err;
1432 9f2369b0 2018-12-24 stsp pid = fork();
1433 ddc7b220 2019-09-08 stsp if (pid == -1) {
1434 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1435 ddc7b220 2019-09-08 stsp free(ibuf);
1436 ddc7b220 2019-09-08 stsp return err;
1438 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1439 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1440 9f2369b0 2018-12-24 stsp repo->path);
1441 9f2369b0 2018-12-24 stsp /* not reached */
1444 ddc7b220 2019-09-08 stsp if (close(imsg_fds[1]) != 0) {
1445 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1446 ddc7b220 2019-09-08 stsp free(ibuf);
1447 ddc7b220 2019-09-08 stsp return err;
1449 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1450 9f2369b0 2018-12-24 stsp imsg_fds[0];
1451 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1452 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1453 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1455 9f2369b0 2018-12-24 stsp return request_tag(tag, repo, obj_fd);
1458 a158c901 2018-12-23 stsp static const struct got_error *
1459 268f7291 2018-12-24 stsp open_tag(struct got_tag_object **tag, struct got_repository *repo,
1460 268f7291 2018-12-24 stsp struct got_object_id *id, int check_cache)
1462 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1463 268f7291 2018-12-24 stsp struct got_packidx *packidx = NULL;
1465 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1466 5d844a1e 2019-08-13 stsp struct got_object *obj = NULL;
1467 5d844a1e 2019-08-13 stsp int obj_type = GOT_OBJ_TYPE_ANY;
1469 f4a881ce 2018-11-17 stsp if (check_cache) {
1470 268f7291 2018-12-24 stsp *tag = got_repo_get_cached_tag(repo, id);
1471 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1472 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1473 f4a881ce 2018-11-17 stsp return NULL;
1476 f4a881ce 2018-11-17 stsp *tag = NULL;
1478 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1479 e82b1d81 2019-07-27 stsp if (err == NULL) {
1480 268f7291 2018-12-24 stsp struct got_pack *pack = NULL;
1482 268f7291 2018-12-24 stsp err = get_packfile_path(&path_packfile, packidx);
1484 268f7291 2018-12-24 stsp return err;
1486 268f7291 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1487 f4a881ce 2018-11-17 stsp if (pack == NULL) {
1488 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1491 8d2c5ea3 2019-08-13 stsp goto done;
1494 992eb9d8 2020-02-07 tracey /* Beware of "lightweight" tags: Check object type first. */
1495 5d844a1e 2019-08-13 stsp err = read_packed_object_privsep(&obj, repo, pack, packidx,
1498 5d844a1e 2019-08-13 stsp goto done;
1499 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1500 5d844a1e 2019-08-13 stsp got_object_close(obj);
1501 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG) {
1502 5d844a1e 2019-08-13 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1503 5d844a1e 2019-08-13 stsp goto done;
1505 5d844a1e 2019-08-13 stsp err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1506 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1509 e82b1d81 2019-07-27 stsp err = open_loose_object(&fd, id, repo);
1511 e82b1d81 2019-07-27 stsp return err;
1512 5d844a1e 2019-08-13 stsp err = read_object_header_privsep(&obj, repo, fd);
1514 5d844a1e 2019-08-13 stsp return err;
1515 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1516 5d844a1e 2019-08-13 stsp got_object_close(obj);
1517 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
1518 5d844a1e 2019-08-13 stsp return got_error(GOT_ERR_OBJ_TYPE);
1520 5d844a1e 2019-08-13 stsp err = open_loose_object(&fd, id, repo);
1522 5d844a1e 2019-08-13 stsp return err;
1523 9f2369b0 2018-12-24 stsp err = read_tag_privsep(tag, fd, repo);
1526 f4a881ce 2018-11-17 stsp if (err == NULL) {
1527 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1528 268f7291 2018-12-24 stsp err = got_repo_cache_tag(repo, id, *tag);
1531 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1532 f4a881ce 2018-11-17 stsp return err;
1535 f4a881ce 2018-11-17 stsp const struct got_error *
1536 f4a881ce 2018-11-17 stsp got_object_open_as_tag(struct got_tag_object **tag,
1537 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object_id *id)
1539 f4a881ce 2018-11-17 stsp *tag = got_repo_get_cached_tag(repo, id);
1540 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1541 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1542 f4a881ce 2018-11-17 stsp return NULL;
1545 268f7291 2018-12-24 stsp return open_tag(tag, repo, id, 0);
1548 f4a881ce 2018-11-17 stsp const struct got_error *
1549 f4a881ce 2018-11-17 stsp got_object_tag_open(struct got_tag_object **tag,
1550 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object *obj)
1552 268f7291 2018-12-24 stsp return open_tag(tag, repo, got_object_get_id(obj), 1);
1555 d24820bf 2019-08-11 stsp const char *
1556 d24820bf 2019-08-11 stsp got_object_tag_get_name(struct got_tag_object *tag)
1558 d24820bf 2019-08-11 stsp return tag->tag;
1562 0bd18d37 2019-02-01 stsp got_object_tag_get_object_type(struct got_tag_object *tag)
1564 0bd18d37 2019-02-01 stsp return tag->obj_type;
1567 0bd18d37 2019-02-01 stsp struct got_object_id *
1568 0bd18d37 2019-02-01 stsp got_object_tag_get_object_id(struct got_tag_object *tag)
1570 0bd18d37 2019-02-01 stsp return &tag->id;
1574 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_time(struct got_tag_object *tag)
1576 01073a5d 2019-08-22 stsp return tag->tagger_time;
1580 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1582 01073a5d 2019-08-22 stsp return tag->tagger_gmtoff;
1585 01073a5d 2019-08-22 stsp const char *
1586 01073a5d 2019-08-22 stsp got_object_tag_get_tagger(struct got_tag_object *tag)
1588 01073a5d 2019-08-22 stsp return tag->tagger;
1591 01073a5d 2019-08-22 stsp const char *
1592 01073a5d 2019-08-22 stsp got_object_tag_get_message(struct got_tag_object *tag)
1594 01073a5d 2019-08-22 stsp return tag->tagmsg;
1597 776d4d29 2018-06-17 stsp static struct got_tree_entry *
1598 65a9bbe9 2018-09-15 stsp find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1602 63da309a 2018-11-07 stsp /* Note that tree entries are sorted in strncmp() order. */
1603 56e0773d 2019-11-28 stsp for (i = 0; i < tree->nentries; i++) {
1604 56e0773d 2019-11-28 stsp struct got_tree_entry *te = &tree->entries[i];
1605 63da309a 2018-11-07 stsp int cmp = strncmp(te->name, name, len);
1606 63da309a 2018-11-07 stsp if (cmp < 0)
1608 63da309a 2018-11-07 stsp if (cmp > 0)
1610 63da309a 2018-11-07 stsp if (te->name[len] == '\0')
1611 776d4d29 2018-06-17 stsp return te;
1613 eb651edf 2018-02-11 stsp return NULL;
1616 56e0773d 2019-11-28 stsp struct got_tree_entry *
1617 a129376b 2019-03-28 stsp got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1619 a129376b 2019-03-28 stsp return find_entry_by_name(tree, name, strlen(name));
1622 776d4d29 2018-06-17 stsp const struct got_error *
1623 27d434c2 2018-09-15 stsp got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1624 776d4d29 2018-06-17 stsp struct got_object_id *commit_id, const char *path)
1626 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
1627 776d4d29 2018-06-17 stsp struct got_commit_object *commit = NULL;
1628 776d4d29 2018-06-17 stsp struct got_tree_object *tree = NULL;
1629 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
1630 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1631 b7cd37e5 2018-11-18 stsp size_t seglen;
1633 27d434c2 2018-09-15 stsp *id = NULL;
1635 776d4d29 2018-06-17 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
1637 776d4d29 2018-06-17 stsp goto done;
1639 776d4d29 2018-06-17 stsp /* Handle opening of root of commit's tree. */
1640 5e54fb30 2019-05-31 stsp if (got_path_is_root_dir(path)) {
1641 27d434c2 2018-09-15 stsp *id = got_object_id_dup(commit->tree_id);
1642 27d434c2 2018-09-15 stsp if (*id == NULL)
1643 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
1644 ca008b32 2018-07-22 stsp goto done;
1647 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1649 776d4d29 2018-06-17 stsp goto done;
1652 5e54fb30 2019-05-31 stsp while (s[0] == '/')
1655 65a9bbe9 2018-09-15 stsp seglen = 0;
1656 b7cd37e5 2018-11-18 stsp while (*s) {
1657 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
1659 776d4d29 2018-06-17 stsp if (*s != '/') {
1666 65a9bbe9 2018-09-15 stsp te = find_entry_by_name(tree, seg, seglen);
1667 db37e2c0 2018-06-21 stsp if (te == NULL) {
1668 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1669 776d4d29 2018-06-17 stsp goto done;
1672 b7cd37e5 2018-11-18 stsp if (*s == '\0')
1675 776d4d29 2018-06-17 stsp seg = s + 1;
1676 65a9bbe9 2018-09-15 stsp seglen = 0;
1679 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
1681 db37e2c0 2018-06-21 stsp te = NULL;
1683 776d4d29 2018-06-17 stsp goto done;
1684 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
1685 776d4d29 2018-06-17 stsp tree = next_tree;
1690 56e0773d 2019-11-28 stsp *id = got_object_id_dup(&te->id);
1691 27d434c2 2018-09-15 stsp if (*id == NULL)
1692 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
1694 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1696 776d4d29 2018-06-17 stsp if (commit)
1697 776d4d29 2018-06-17 stsp got_object_commit_close(commit);
1699 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
1700 776d4d29 2018-06-17 stsp return err;
1704 ac5f2b26 2020-05-05 stsp * Normalize file mode bits to avoid false positive tree entry differences
1705 ac5f2b26 2020-05-05 stsp * in case tree entries have unexpected mode bits set.
1707 ac5f2b26 2020-05-05 stsp static mode_t
1708 ac5f2b26 2020-05-05 stsp normalize_mode_for_comparison(mode_t mode)
1711 ac5f2b26 2020-05-05 stsp * For directories, the only relevant bit is the IFDIR bit.
1712 ac5f2b26 2020-05-05 stsp * This allows us to detect paths changing from a directory
1713 ac5f2b26 2020-05-05 stsp * to a file and vice versa.
1715 ac5f2b26 2020-05-05 stsp if (S_ISDIR(mode))
1716 ac5f2b26 2020-05-05 stsp return mode & S_IFDIR;
1719 40dde666 2020-07-23 stsp * For symlinks, the only relevant bit is the IFLNK bit.
1720 40dde666 2020-07-23 stsp * This allows us to detect paths changing from a symlinks
1721 40dde666 2020-07-23 stsp * to a file or directory and vice versa.
1723 40dde666 2020-07-23 stsp if (S_ISLNK(mode))
1724 40dde666 2020-07-23 stsp return mode & S_IFLNK;
1726 ac5f2b26 2020-05-05 stsp /* For files, the only change we care about is the executable bit. */
1727 ac5f2b26 2020-05-05 stsp return mode & S_IXUSR;
1730 07862c20 2018-09-15 stsp const struct got_error *
1731 07862c20 2018-09-15 stsp got_object_tree_path_changed(int *changed,
1732 07862c20 2018-09-15 stsp struct got_tree_object *tree01, struct got_tree_object *tree02,
1733 07862c20 2018-09-15 stsp const char *path, struct got_repository *repo)
1735 07862c20 2018-09-15 stsp const struct got_error *err = NULL;
1736 07862c20 2018-09-15 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1737 07862c20 2018-09-15 stsp struct got_tree_entry *te1 = NULL, *te2 = NULL;
1738 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1739 3b7f9878 2018-11-18 stsp size_t seglen;
1741 07862c20 2018-09-15 stsp *changed = 0;
1743 07862c20 2018-09-15 stsp /* We not do support comparing the root path. */
1744 61a7d79f 2020-02-29 stsp if (got_path_is_root_dir(path))
1745 63f810e6 2020-02-29 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
1747 07862c20 2018-09-15 stsp tree1 = tree01;
1748 07862c20 2018-09-15 stsp tree2 = tree02;
1750 61a7d79f 2020-02-29 stsp while (*s == '/')
1753 65a9bbe9 2018-09-15 stsp seglen = 0;
1754 3b7f9878 2018-11-18 stsp while (*s) {
1755 07862c20 2018-09-15 stsp struct got_tree_object *next_tree1, *next_tree2;
1756 ac5f2b26 2020-05-05 stsp mode_t mode1, mode2;
1758 07862c20 2018-09-15 stsp if (*s != '/') {
1765 65a9bbe9 2018-09-15 stsp te1 = find_entry_by_name(tree1, seg, seglen);
1766 07862c20 2018-09-15 stsp if (te1 == NULL) {
1767 07862c20 2018-09-15 stsp err = got_error(GOT_ERR_NO_OBJ);
1768 07862c20 2018-09-15 stsp goto done;
1771 65a9bbe9 2018-09-15 stsp te2 = find_entry_by_name(tree2, seg, seglen);
1772 07862c20 2018-09-15 stsp if (te2 == NULL) {
1773 07862c20 2018-09-15 stsp *changed = 1;
1774 07862c20 2018-09-15 stsp goto done;
1777 ac5f2b26 2020-05-05 stsp mode1 = normalize_mode_for_comparison(te1->mode);
1778 ac5f2b26 2020-05-05 stsp mode2 = normalize_mode_for_comparison(te2->mode);
1779 ac5f2b26 2020-05-05 stsp if (mode1 != mode2) {
1780 07862c20 2018-09-15 stsp *changed = 1;
1781 07862c20 2018-09-15 stsp goto done;
1784 56e0773d 2019-11-28 stsp if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
1785 07862c20 2018-09-15 stsp *changed = 0;
1786 07862c20 2018-09-15 stsp goto done;
1789 3b7f9878 2018-11-18 stsp if (*s == '\0') { /* final path element */
1790 07862c20 2018-09-15 stsp *changed = 1;
1791 07862c20 2018-09-15 stsp goto done;
1794 07862c20 2018-09-15 stsp seg = s + 1;
1796 65a9bbe9 2018-09-15 stsp seglen = 0;
1798 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree1, repo,
1799 56e0773d 2019-11-28 stsp &te1->id);
1800 07862c20 2018-09-15 stsp te1 = NULL;
1802 07862c20 2018-09-15 stsp goto done;
1803 a31cea73 2018-09-15 stsp if (tree1 != tree01)
1804 a31cea73 2018-09-15 stsp got_object_tree_close(tree1);
1805 07862c20 2018-09-15 stsp tree1 = next_tree1;
1807 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree2, repo,
1808 56e0773d 2019-11-28 stsp &te2->id);
1809 07862c20 2018-09-15 stsp te2 = NULL;
1811 07862c20 2018-09-15 stsp goto done;
1812 a31cea73 2018-09-15 stsp if (tree2 != tree02)
1813 a31cea73 2018-09-15 stsp got_object_tree_close(tree2);
1814 07862c20 2018-09-15 stsp tree2 = next_tree2;
1818 a31cea73 2018-09-15 stsp if (tree1 && tree1 != tree01)
1819 07862c20 2018-09-15 stsp got_object_tree_close(tree1);
1820 a31cea73 2018-09-15 stsp if (tree2 && tree2 != tree02)
1821 07862c20 2018-09-15 stsp got_object_tree_close(tree2);
1822 77880158 2018-11-04 stsp return err;
1825 ed175427 2019-05-09 stsp const struct got_error *
1826 ed175427 2019-05-09 stsp got_object_tree_entry_dup(struct got_tree_entry **new_te,
1827 ed175427 2019-05-09 stsp struct got_tree_entry *te)
1829 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
1831 ed175427 2019-05-09 stsp *new_te = calloc(1, sizeof(**new_te));
1832 ed175427 2019-05-09 stsp if (*new_te == NULL)
1833 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1835 ed175427 2019-05-09 stsp (*new_te)->mode = te->mode;
1836 56e0773d 2019-11-28 stsp memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
1837 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
1838 8c4eabf2 2019-05-10 stsp return err;
1842 56e0773d 2019-11-28 stsp got_object_tree_entry_is_submodule(struct got_tree_entry *te)
1844 63c5ca5d 2019-08-24 stsp return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
1848 e40622f4 2020-07-23 stsp got_object_tree_entry_is_symlink(struct got_tree_entry *te)
1850 e40622f4 2020-07-23 stsp /* S_IFDIR check avoids confusing symlinks with submodules. */
1851 e40622f4 2020-07-23 stsp return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
1854 e40622f4 2020-07-23 stsp static const struct got_error *
1855 e40622f4 2020-07-23 stsp resolve_symlink(char **link_target, const char *path,
1856 e40622f4 2020-07-23 stsp struct got_object_id *commit_id, struct got_repository *repo)
1858 e40622f4 2020-07-23 stsp const struct got_error *err = NULL;
1859 e40622f4 2020-07-23 stsp char *name, *parent_path = NULL;
1860 e40622f4 2020-07-23 stsp struct got_object_id *tree_obj_id = NULL;
1861 e40622f4 2020-07-23 stsp struct got_tree_object *tree = NULL;
1862 e40622f4 2020-07-23 stsp struct got_tree_entry *te = NULL;
1864 e40622f4 2020-07-23 stsp *link_target = NULL;
1866 e40622f4 2020-07-23 stsp name = basename(path);
1867 e40622f4 2020-07-23 stsp if (name == NULL)
1868 e40622f4 2020-07-23 stsp return got_error_from_errno2("basename", path);
1870 e40622f4 2020-07-23 stsp err = got_path_dirname(&parent_path, path);
1872 e40622f4 2020-07-23 stsp return err;
1874 e40622f4 2020-07-23 stsp err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
1875 e40622f4 2020-07-23 stsp parent_path);
1876 e40622f4 2020-07-23 stsp if (err) {
1877 e40622f4 2020-07-23 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY) {
1878 e40622f4 2020-07-23 stsp /* Display the complete path in error message. */
1879 e40622f4 2020-07-23 stsp err = got_error_path(path, err->code);
1881 e40622f4 2020-07-23 stsp goto done;
1884 e40622f4 2020-07-23 stsp err = got_object_open_as_tree(&tree, repo, tree_obj_id);
1886 e40622f4 2020-07-23 stsp goto done;
1888 e40622f4 2020-07-23 stsp te = got_object_tree_find_entry(tree, name);
1889 e40622f4 2020-07-23 stsp if (te == NULL) {
1890 e40622f4 2020-07-23 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1891 e40622f4 2020-07-23 stsp goto done;
1894 e40622f4 2020-07-23 stsp if (got_object_tree_entry_is_symlink(te)) {
1895 e40622f4 2020-07-23 stsp err = got_tree_entry_get_symlink_target(link_target, te, repo);
1897 e40622f4 2020-07-23 stsp goto done;
1898 e40622f4 2020-07-23 stsp if (!got_path_is_absolute(*link_target)) {
1899 e40622f4 2020-07-23 stsp char *abspath;
1900 e40622f4 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", parent_path,
1901 e40622f4 2020-07-23 stsp *link_target) == -1) {
1902 e40622f4 2020-07-23 stsp err = got_error_from_errno("asprintf");
1903 e40622f4 2020-07-23 stsp goto done;
1905 e40622f4 2020-07-23 stsp free(*link_target);
1906 e40622f4 2020-07-23 stsp *link_target = malloc(PATH_MAX);
1907 e40622f4 2020-07-23 stsp if (*link_target == NULL) {
1908 e40622f4 2020-07-23 stsp err = got_error_from_errno("malloc");
1909 e40622f4 2020-07-23 stsp goto done;
1911 e40622f4 2020-07-23 stsp err = got_canonpath(abspath, *link_target, PATH_MAX);
1912 e40622f4 2020-07-23 stsp free(abspath);
1914 e40622f4 2020-07-23 stsp goto done;
1918 e40622f4 2020-07-23 stsp free(tree_obj_id);
1920 e40622f4 2020-07-23 stsp got_object_tree_close(tree);
1921 e40622f4 2020-07-23 stsp if (err) {
1922 e40622f4 2020-07-23 stsp free(*link_target);
1923 e40622f4 2020-07-23 stsp *link_target = NULL;
1925 e40622f4 2020-07-23 stsp return err;
1928 ca6e02ac 2020-01-07 stsp const struct got_error *
1929 e40622f4 2020-07-23 stsp got_object_resolve_symlinks(char **link_target, const char *path,
1930 e40622f4 2020-07-23 stsp struct got_object_id *commit_id, struct got_repository *repo)
1932 e40622f4 2020-07-23 stsp const struct got_error *err = NULL;
1933 e40622f4 2020-07-23 stsp char *next_target = NULL;
1934 e40622f4 2020-07-23 stsp int max_recursion = 40; /* matches Git */
1936 e40622f4 2020-07-23 stsp *link_target = NULL;
1939 e40622f4 2020-07-23 stsp err = resolve_symlink(&next_target,
1940 e40622f4 2020-07-23 stsp *link_target ? *link_target : path, commit_id, repo);
1943 e40622f4 2020-07-23 stsp if (next_target) {
1944 e40622f4 2020-07-23 stsp free(*link_target);
1945 e40622f4 2020-07-23 stsp if (--max_recursion == 0) {
1946 e40622f4 2020-07-23 stsp err = got_error_path(path, GOT_ERR_RECURSION);
1947 e40622f4 2020-07-23 stsp *link_target = NULL;
1950 e40622f4 2020-07-23 stsp *link_target = next_target;
1952 e40622f4 2020-07-23 stsp } while (next_target);
1954 e40622f4 2020-07-23 stsp return err;
1957 e40622f4 2020-07-23 stsp const struct got_error *
1958 ca6e02ac 2020-01-07 stsp got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
1959 ca6e02ac 2020-01-07 stsp struct got_object_id *commit_id, const char *path,
1960 ca6e02ac 2020-01-07 stsp struct got_repository *repo)
1962 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
1963 ca6e02ac 2020-01-07 stsp struct got_pack *pack = NULL;
1964 ca6e02ac 2020-01-07 stsp struct got_packidx *packidx = NULL;
1965 ca6e02ac 2020-01-07 stsp char *path_packfile = NULL;
1966 ca6e02ac 2020-01-07 stsp struct got_commit_object *changed_commit = NULL;
1967 ca6e02ac 2020-01-07 stsp struct got_object_id *changed_commit_id = NULL;
1970 ca6e02ac 2020-01-07 stsp err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
1971 ca6e02ac 2020-01-07 stsp if (err) {
1972 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
1973 ca6e02ac 2020-01-07 stsp return err;
1974 ca6e02ac 2020-01-07 stsp return NULL;
1977 ca6e02ac 2020-01-07 stsp err = get_packfile_path(&path_packfile, packidx);
1979 ca6e02ac 2020-01-07 stsp return err;
1981 ca6e02ac 2020-01-07 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1982 ca6e02ac 2020-01-07 stsp if (pack == NULL) {
1983 ca6e02ac 2020-01-07 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
1985 ca6e02ac 2020-01-07 stsp goto done;
1988 ca6e02ac 2020-01-07 stsp if (pack->privsep_child == NULL) {
1989 ca6e02ac 2020-01-07 stsp err = start_pack_privsep_child(pack, packidx);
1991 ca6e02ac 2020-01-07 stsp goto done;
1994 ca6e02ac 2020-01-07 stsp err = got_privsep_send_commit_traversal_request(
1995 ca6e02ac 2020-01-07 stsp pack->privsep_child->ibuf, commit_id, idx, path);
1997 ca6e02ac 2020-01-07 stsp goto done;
1999 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_traversed_commits(&changed_commit,
2000 ca6e02ac 2020-01-07 stsp &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2002 ca6e02ac 2020-01-07 stsp goto done;
2004 ca6e02ac 2020-01-07 stsp if (changed_commit) {
2006 ca6e02ac 2020-01-07 stsp * Cache the commit in which the path was changed.
2007 ca6e02ac 2020-01-07 stsp * This commit might be opened again soon.
2009 ca6e02ac 2020-01-07 stsp changed_commit->refcnt++;
2010 ca6e02ac 2020-01-07 stsp err = got_repo_cache_commit(repo, changed_commit_id,
2011 ca6e02ac 2020-01-07 stsp changed_commit);
2012 ca6e02ac 2020-01-07 stsp got_object_commit_close(changed_commit);
2015 ca6e02ac 2020-01-07 stsp free(path_packfile);
2016 ca6e02ac 2020-01-07 stsp free(changed_commit_id);
2017 ca6e02ac 2020-01-07 stsp return err;