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 2178c42e 2018-04-22 stsp #include <sys/uio.h>
20 2178c42e 2018-04-22 stsp #include <sys/socket.h>
21 2178c42e 2018-04-22 stsp #include <sys/wait.h>
22 b48e2ddb 2019-05-22 stsp #include <sys/resource.h>
23 2b0ae357 2022-01-10 thomas #include <sys/mman.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 81a12da5 2020-09-09 naddy #include <unistd.h>
32 ab9a70b2 2017-11-06 stsp #include <zlib.h>
33 ab9a70b2 2017-11-06 stsp #include <ctype.h>
34 e40622f4 2020-07-23 stsp #include <libgen.h>
35 ab9a70b2 2017-11-06 stsp #include <limits.h>
36 788c352e 2018-06-16 stsp #include <time.h>
38 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
40 ab9a70b2 2017-11-06 stsp #include "got_error.h"
41 d71d75ad 2017-11-05 stsp #include "got_object.h"
42 ab9a70b2 2017-11-06 stsp #include "got_repository.h"
43 511a516b 2018-05-19 stsp #include "got_opentemp.h"
44 324d37e7 2019-05-11 stsp #include "got_path.h"
46 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
47 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
48 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
49 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
50 2178c42e 2018-04-22 stsp #include "got_lib_privsep.h"
51 6bef87be 2018-09-11 stsp #include "got_lib_object_idcache.h"
52 6bef87be 2018-09-11 stsp #include "got_lib_object_cache.h"
53 ad242220 2018-09-08 stsp #include "got_lib_object_parse.h"
54 15a94983 2018-12-23 stsp #include "got_lib_pack.h"
55 7bb0daa1 2018-06-21 stsp #include "got_lib_repository.h"
58 ab9a70b2 2017-11-06 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 63915ee5 2022-06-23 thomas #ifndef nitems
62 63915ee5 2022-06-23 thomas #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
65 3235492e 2018-04-01 stsp struct got_object_id *
66 3235492e 2018-04-01 stsp got_object_get_id(struct got_object *obj)
68 6402fb3c 2018-09-15 stsp return &obj->id;
71 bacc9935 2018-05-20 stsp const struct got_error *
72 bacc9935 2018-05-20 stsp got_object_get_id_str(char **outbuf, struct got_object *obj)
74 bacc9935 2018-05-20 stsp return got_object_id_str(outbuf, &obj->id);
77 15a94983 2018-12-23 stsp const struct got_error *
78 15a94983 2018-12-23 stsp got_object_get_type(int *type, struct got_repository *repo,
79 15a94983 2018-12-23 stsp struct got_object_id *id)
81 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
82 15a94983 2018-12-23 stsp struct got_object *obj;
84 15a94983 2018-12-23 stsp err = got_object_open(&obj, repo, id);
88 b107e67f 2018-01-19 stsp switch (obj->type) {
89 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_COMMIT:
90 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_TREE:
91 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_BLOB:
92 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
93 15a94983 2018-12-23 stsp *type = obj->type;
96 15a94983 2018-12-23 stsp err = got_error(GOT_ERR_OBJ_TYPE);
100 15a94983 2018-12-23 stsp got_object_close(obj);
101 15a94983 2018-12-23 stsp return err;
104 90bdb554 2019-04-11 stsp const struct got_error *
105 90bdb554 2019-04-11 stsp got_object_get_path(char **path, struct got_object_id *id,
106 90bdb554 2019-04-11 stsp struct got_repository *repo)
108 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
109 7a132809 2018-07-23 stsp char *hex = NULL;
110 41d2888b 2019-08-11 stsp char *path_objects;
112 e6b1056e 2018-04-22 stsp *path = NULL;
114 41d2888b 2019-08-11 stsp path_objects = got_repo_get_path_objects(repo);
115 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
116 638f9024 2019-05-13 stsp return got_error_from_errno("got_repo_get_path_objects");
118 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
122 d1cda826 2017-11-06 stsp if (asprintf(path, "%s/%.2x/%s", path_objects,
123 d1cda826 2017-11-06 stsp id->sha1[0], hex + 2) == -1)
124 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
128 d1cda826 2017-11-06 stsp free(path_objects);
129 d1cda826 2017-11-06 stsp return err;
132 762d73f4 2021-04-10 stsp const struct got_error *
133 762d73f4 2021-04-10 stsp got_object_open_loose_fd(int *fd, struct got_object_id *id,
134 4796fb13 2018-12-23 stsp struct got_repository *repo)
136 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
137 a1fd68d8 2018-01-12 stsp char *path;
139 90bdb554 2019-04-11 stsp err = got_object_get_path(&path, id, repo);
141 d1cda826 2017-11-06 stsp return err;
142 06340621 2021-12-31 thomas *fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
143 d5003b79 2018-04-22 stsp if (*fd == -1) {
144 e82b1d81 2019-07-27 stsp err = got_error_from_errno2("open", path);
148 4558fcd4 2018-01-14 stsp free(path);
149 2090a03d 2018-09-09 stsp return err;
152 2090a03d 2018-09-09 stsp static const struct got_error *
153 a158c901 2018-12-23 stsp request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
154 a158c901 2018-12-23 stsp struct got_object_id *id)
156 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
157 a158c901 2018-12-23 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
159 a158c901 2018-12-23 stsp err = got_privsep_send_packed_obj_req(ibuf, idx, id);
161 a158c901 2018-12-23 stsp return err;
163 a158c901 2018-12-23 stsp err = got_privsep_recv_obj(obj, ibuf);
165 a158c901 2018-12-23 stsp return err;
167 a158c901 2018-12-23 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
169 a158c901 2018-12-23 stsp return NULL;
172 bc1f382f 2022-01-05 thomas /* Create temporary files used during delta application. */
173 59d1e4a0 2021-03-10 stsp static const struct got_error *
174 bc1f382f 2022-01-05 thomas pack_child_send_tempfiles(struct imsgbuf *ibuf, struct got_pack *pack)
176 bc1f382f 2022-01-05 thomas const struct got_error *err;
177 bfb5ee0b 2022-05-31 thomas int basefd = -1, accumfd = -1;
180 bc1f382f 2022-01-05 thomas * For performance reasons, the child will keep reusing the
181 bc1f382f 2022-01-05 thomas * same temporary files during every object request.
182 bc1f382f 2022-01-05 thomas * Opening and closing new files for every object request is
183 bc1f382f 2022-01-05 thomas * too expensive during operations such as 'gotadmin pack'.
185 bc1f382f 2022-01-05 thomas if (pack->child_has_tempfiles)
186 bc1f382f 2022-01-05 thomas return NULL;
188 bfb5ee0b 2022-05-31 thomas basefd = dup(pack->basefd);
189 59d1e4a0 2021-03-10 stsp if (basefd == -1)
190 bfb5ee0b 2022-05-31 thomas return got_error_from_errno("dup");
192 bfb5ee0b 2022-05-31 thomas accumfd = dup(pack->accumfd);
193 bfb5ee0b 2022-05-31 thomas if (accumfd == -1) {
194 bfb5ee0b 2022-05-31 thomas err = got_error_from_errno("dup");
195 bfb5ee0b 2022-05-31 thomas goto done;
198 bc1f382f 2022-01-05 thomas err = got_privsep_send_tmpfd(ibuf, basefd);
200 bfb5ee0b 2022-05-31 thomas goto done;
202 bc1f382f 2022-01-05 thomas err = got_privsep_send_tmpfd(ibuf, accumfd);
204 bfb5ee0b 2022-05-31 thomas if (err) {
205 bfb5ee0b 2022-05-31 thomas if (basefd != -1)
206 bfb5ee0b 2022-05-31 thomas close(basefd);
207 bfb5ee0b 2022-05-31 thomas if (accumfd != -1)
208 bfb5ee0b 2022-05-31 thomas close(accumfd);
210 bfb5ee0b 2022-05-31 thomas pack->child_has_tempfiles = 1;
211 bc1f382f 2022-01-05 thomas return NULL;
214 bc1f382f 2022-01-05 thomas static const struct got_error *
215 bc1f382f 2022-01-05 thomas request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
216 bc1f382f 2022-01-05 thomas int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
218 bc1f382f 2022-01-05 thomas const struct got_error *err = NULL;
219 bc1f382f 2022-01-05 thomas struct imsgbuf *ibuf = pack->privsep_child->ibuf;
220 bc1f382f 2022-01-05 thomas int outfd_child;
222 bc1f382f 2022-01-05 thomas err = pack_child_send_tempfiles(ibuf, pack);
224 bc1f382f 2022-01-05 thomas return err;
226 bc1f382f 2022-01-05 thomas outfd_child = dup(outfd);
227 bc1f382f 2022-01-05 thomas if (outfd_child == -1)
228 bc1f382f 2022-01-05 thomas return got_error_from_errno("dup");
230 59d1e4a0 2021-03-10 stsp err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
232 59d1e4a0 2021-03-10 stsp close(outfd_child);
233 59d1e4a0 2021-03-10 stsp return err;
236 59d1e4a0 2021-03-10 stsp err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
238 59d1e4a0 2021-03-10 stsp return err;
240 59d1e4a0 2021-03-10 stsp err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
242 59d1e4a0 2021-03-10 stsp return err;
244 59d1e4a0 2021-03-10 stsp return NULL;
247 da506691 2019-05-22 stsp static void
248 b48e2ddb 2019-05-22 stsp set_max_datasize(void)
250 b48e2ddb 2019-05-22 stsp struct rlimit rl;
252 b48e2ddb 2019-05-22 stsp if (getrlimit(RLIMIT_DATA, &rl) != 0)
255 b48e2ddb 2019-05-22 stsp rl.rlim_cur = rl.rlim_max;
256 b48e2ddb 2019-05-22 stsp setrlimit(RLIMIT_DATA, &rl);
259 a158c901 2018-12-23 stsp static const struct got_error *
260 711fb6e8 2018-12-23 stsp start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
262 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
263 a158c901 2018-12-23 stsp int imsg_fds[2];
265 a158c901 2018-12-23 stsp struct imsgbuf *ibuf;
267 a158c901 2018-12-23 stsp ibuf = calloc(1, sizeof(*ibuf));
268 a158c901 2018-12-23 stsp if (ibuf == NULL)
269 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
271 a158c901 2018-12-23 stsp pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
272 a158c901 2018-12-23 stsp if (pack->privsep_child == NULL) {
273 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
274 a158c901 2018-12-23 stsp free(ibuf);
275 a158c901 2018-12-23 stsp return err;
277 bc1f382f 2022-01-05 thomas pack->child_has_tempfiles = 0;
278 f9c2e8e5 2022-02-13 thomas pack->child_has_delta_outfd = 0;
280 a158c901 2018-12-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
281 638f9024 2019-05-13 stsp err = got_error_from_errno("socketpair");
285 a158c901 2018-12-23 stsp pid = fork();
286 a158c901 2018-12-23 stsp if (pid == -1) {
287 638f9024 2019-05-13 stsp err = got_error_from_errno("fork");
289 a158c901 2018-12-23 stsp } else if (pid == 0) {
290 b48e2ddb 2019-05-22 stsp set_max_datasize();
291 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
292 a158c901 2018-12-23 stsp pack->path_packfile);
293 a158c901 2018-12-23 stsp /* not reached */
296 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1)
297 638f9024 2019-05-13 stsp return got_error_from_errno("close");
298 a158c901 2018-12-23 stsp pack->privsep_child->imsg_fd = imsg_fds[0];
299 a158c901 2018-12-23 stsp pack->privsep_child->pid = pid;
300 a158c901 2018-12-23 stsp imsg_init(ibuf, imsg_fds[0]);
301 a158c901 2018-12-23 stsp pack->privsep_child->ibuf = ibuf;
303 a158c901 2018-12-23 stsp err = got_privsep_init_pack_child(ibuf, pack, packidx);
305 a158c901 2018-12-23 stsp const struct got_error *child_err;
306 a158c901 2018-12-23 stsp err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
307 a158c901 2018-12-23 stsp child_err = got_privsep_wait_for_child(
308 a158c901 2018-12-23 stsp pack->privsep_child->pid);
309 a158c901 2018-12-23 stsp if (child_err && err == NULL)
310 a158c901 2018-12-23 stsp err = child_err;
314 a158c901 2018-12-23 stsp free(ibuf);
315 a158c901 2018-12-23 stsp free(pack->privsep_child);
316 a158c901 2018-12-23 stsp pack->privsep_child = NULL;
318 a158c901 2018-12-23 stsp return err;
321 711fb6e8 2018-12-23 stsp static const struct got_error *
322 711fb6e8 2018-12-23 stsp read_packed_object_privsep(struct got_object **obj,
323 711fb6e8 2018-12-23 stsp struct got_repository *repo, struct got_pack *pack,
324 711fb6e8 2018-12-23 stsp struct got_packidx *packidx, int idx, struct got_object_id *id)
326 711fb6e8 2018-12-23 stsp const struct got_error *err = NULL;
328 59d1e4a0 2021-03-10 stsp if (pack->privsep_child == NULL) {
329 59d1e4a0 2021-03-10 stsp err = start_pack_privsep_child(pack, packidx);
331 59d1e4a0 2021-03-10 stsp return err;
334 711fb6e8 2018-12-23 stsp return request_packed_object(obj, pack, idx, id);
337 59d1e4a0 2021-03-10 stsp static const struct got_error *
338 59d1e4a0 2021-03-10 stsp read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
339 59d1e4a0 2021-03-10 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
340 59d1e4a0 2021-03-10 stsp struct got_object_id *id)
342 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
344 59d1e4a0 2021-03-10 stsp if (pack->privsep_child == NULL) {
345 59d1e4a0 2021-03-10 stsp err = start_pack_privsep_child(pack, packidx);
347 59d1e4a0 2021-03-10 stsp return err;
350 59d1e4a0 2021-03-10 stsp return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
354 b3d68e7f 2021-07-03 stsp const struct got_error *
355 b3d68e7f 2021-07-03 stsp got_object_open_packed(struct got_object **obj, struct got_object_id *id,
356 2090a03d 2018-09-09 stsp struct got_repository *repo)
358 2090a03d 2018-09-09 stsp const struct got_error *err = NULL;
359 2090a03d 2018-09-09 stsp struct got_pack *pack = NULL;
360 2090a03d 2018-09-09 stsp struct got_packidx *packidx = NULL;
362 2090a03d 2018-09-09 stsp char *path_packfile;
364 2090a03d 2018-09-09 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
366 2090a03d 2018-09-09 stsp return err;
368 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
369 aea75d87 2021-07-06 stsp packidx->path_packidx);
371 2090a03d 2018-09-09 stsp return err;
373 2090a03d 2018-09-09 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
374 2090a03d 2018-09-09 stsp if (pack == NULL) {
375 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
380 a158c901 2018-12-23 stsp err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
384 2090a03d 2018-09-09 stsp free(path_packfile);
385 4558fcd4 2018-01-14 stsp return err;
388 f9c2e8e5 2022-02-13 thomas const struct got_error *
389 f9c2e8e5 2022-02-13 thomas got_object_open_from_packfile(struct got_object **obj, struct got_object_id *id,
390 f9c2e8e5 2022-02-13 thomas struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
391 f9c2e8e5 2022-02-13 thomas struct got_repository *repo)
393 f9c2e8e5 2022-02-13 thomas return read_packed_object_privsep(obj, repo, pack, packidx,
394 f9c2e8e5 2022-02-13 thomas obj_idx, id);
397 f9c2e8e5 2022-02-13 thomas const struct got_error *
398 f9c2e8e5 2022-02-13 thomas got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
399 9249e7e3 2022-05-12 thomas off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
400 9249e7e3 2022-05-12 thomas off_t *delta_out_offset, struct got_object_id **base_id, int delta_cache_fd,
401 f9c2e8e5 2022-02-13 thomas struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
402 f9c2e8e5 2022-02-13 thomas struct got_repository *repo)
404 f9c2e8e5 2022-02-13 thomas const struct got_error *err = NULL;
405 f9c2e8e5 2022-02-13 thomas struct got_pack *pack = NULL;
406 f9c2e8e5 2022-02-13 thomas char *path_packfile;
408 f9c2e8e5 2022-02-13 thomas *base_size = 0;
409 f9c2e8e5 2022-02-13 thomas *result_size = 0;
410 f9c2e8e5 2022-02-13 thomas *delta_size = 0;
411 9249e7e3 2022-05-12 thomas *delta_compressed_size = 0;
412 f9c2e8e5 2022-02-13 thomas *delta_offset = 0;
413 f9c2e8e5 2022-02-13 thomas *delta_out_offset = 0;
415 f9c2e8e5 2022-02-13 thomas err = got_packidx_get_packfile_path(&path_packfile,
416 f9c2e8e5 2022-02-13 thomas packidx->path_packidx);
418 f9c2e8e5 2022-02-13 thomas return err;
420 f9c2e8e5 2022-02-13 thomas pack = got_repo_get_cached_pack(repo, path_packfile);
421 f9c2e8e5 2022-02-13 thomas if (pack == NULL) {
422 f9c2e8e5 2022-02-13 thomas err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
424 f9c2e8e5 2022-02-13 thomas return err;
427 f9c2e8e5 2022-02-13 thomas if (pack->privsep_child == NULL) {
428 f9c2e8e5 2022-02-13 thomas err = start_pack_privsep_child(pack, packidx);
430 f9c2e8e5 2022-02-13 thomas return err;
433 f9c2e8e5 2022-02-13 thomas if (!pack->child_has_delta_outfd) {
434 f9c2e8e5 2022-02-13 thomas int outfd_child;
435 f9c2e8e5 2022-02-13 thomas outfd_child = dup(delta_cache_fd);
436 f9c2e8e5 2022-02-13 thomas if (outfd_child == -1)
437 f9c2e8e5 2022-02-13 thomas return got_error_from_errno("dup");
438 f9c2e8e5 2022-02-13 thomas err = got_privsep_send_raw_delta_outfd(
439 f9c2e8e5 2022-02-13 thomas pack->privsep_child->ibuf, outfd_child);
441 f9c2e8e5 2022-02-13 thomas return err;
442 f9c2e8e5 2022-02-13 thomas pack->child_has_delta_outfd = 1;
445 f9c2e8e5 2022-02-13 thomas err = got_privsep_send_raw_delta_req(pack->privsep_child->ibuf,
446 f9c2e8e5 2022-02-13 thomas obj_idx, id);
448 f9c2e8e5 2022-02-13 thomas return err;
450 f9c2e8e5 2022-02-13 thomas return got_privsep_recv_raw_delta(base_size, result_size, delta_size,
451 9249e7e3 2022-05-12 thomas delta_compressed_size, delta_offset, delta_out_offset, base_id,
452 9249e7e3 2022-05-12 thomas pack->privsep_child->ibuf);
456 7d0d4920 2022-05-12 thomas * XXX This function does not really belong in object.c. It is only here
457 7d0d4920 2022-05-12 thomas * because it needs start_pack_privsep_child(); relevant code should
458 7d0d4920 2022-05-12 thomas * probably be moved to pack.c/pack_create.c.
460 7d0d4920 2022-05-12 thomas const struct got_error *
461 7d0d4920 2022-05-12 thomas got_object_prepare_delta_reuse(struct got_pack **pack,
462 7d0d4920 2022-05-12 thomas struct got_packidx *packidx, int delta_outfd, struct got_repository *repo)
464 7d0d4920 2022-05-12 thomas const struct got_error *err = NULL;
465 7d0d4920 2022-05-12 thomas char *path_packfile = NULL;
467 7d0d4920 2022-05-12 thomas err = got_packidx_get_packfile_path(&path_packfile,
468 7d0d4920 2022-05-12 thomas packidx->path_packidx);
470 7d0d4920 2022-05-12 thomas return err;
472 7d0d4920 2022-05-12 thomas *pack = got_repo_get_cached_pack(repo, path_packfile);
473 7d0d4920 2022-05-12 thomas if (*pack == NULL) {
474 7d0d4920 2022-05-12 thomas err = got_repo_cache_pack(pack, repo, path_packfile, packidx);
476 7d0d4920 2022-05-12 thomas goto done;
478 7d0d4920 2022-05-12 thomas if ((*pack)->privsep_child == NULL) {
479 7d0d4920 2022-05-12 thomas err = start_pack_privsep_child(*pack, packidx);
481 7d0d4920 2022-05-12 thomas goto done;
484 7d0d4920 2022-05-12 thomas if (!(*pack)->child_has_delta_outfd) {
485 7d0d4920 2022-05-12 thomas int outfd_child;
486 7d0d4920 2022-05-12 thomas outfd_child = dup(delta_outfd);
487 7d0d4920 2022-05-12 thomas if (outfd_child == -1) {
488 7d0d4920 2022-05-12 thomas err = got_error_from_errno("dup");
489 7d0d4920 2022-05-12 thomas goto done;
491 7d0d4920 2022-05-12 thomas err = got_privsep_send_raw_delta_outfd(
492 7d0d4920 2022-05-12 thomas (*pack)->privsep_child->ibuf, outfd_child);
494 7d0d4920 2022-05-12 thomas goto done;
495 7d0d4920 2022-05-12 thomas (*pack)->child_has_delta_outfd = 1;
498 7d0d4920 2022-05-12 thomas err = got_privsep_send_delta_reuse_req((*pack)->privsep_child->ibuf);
500 7d0d4920 2022-05-12 thomas free(path_packfile);
501 7d0d4920 2022-05-12 thomas return err;
504 9f2369b0 2018-12-24 stsp static const struct got_error *
505 d5c81d44 2021-07-08 stsp request_object(struct got_object **obj, struct got_object_id *id,
506 d5c81d44 2021-07-08 stsp struct got_repository *repo, int fd)
508 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
509 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
511 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
513 d5c81d44 2021-07-08 stsp err = got_privsep_send_obj_req(ibuf, fd, id);
515 9f2369b0 2018-12-24 stsp return err;
517 9f2369b0 2018-12-24 stsp return got_privsep_recv_obj(obj, ibuf);
520 9f2369b0 2018-12-24 stsp static const struct got_error *
521 59d1e4a0 2021-03-10 stsp request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
522 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo, int infd)
524 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
525 59d1e4a0 2021-03-10 stsp struct imsgbuf *ibuf;
526 59d1e4a0 2021-03-10 stsp int outfd_child;
528 59d1e4a0 2021-03-10 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
530 59d1e4a0 2021-03-10 stsp outfd_child = dup(outfd);
531 59d1e4a0 2021-03-10 stsp if (outfd_child == -1)
532 59d1e4a0 2021-03-10 stsp return got_error_from_errno("dup");
534 d5c81d44 2021-07-08 stsp err = got_privsep_send_raw_obj_req(ibuf, infd, id);
536 59d1e4a0 2021-03-10 stsp return err;
538 59d1e4a0 2021-03-10 stsp err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
540 59d1e4a0 2021-03-10 stsp return err;
542 59d1e4a0 2021-03-10 stsp return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
545 59d1e4a0 2021-03-10 stsp static const struct got_error *
546 59d1e4a0 2021-03-10 stsp start_read_object_child(struct got_repository *repo)
548 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
549 9f2369b0 2018-12-24 stsp int imsg_fds[2];
551 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
553 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
554 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
555 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
557 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
558 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
559 ddc7b220 2019-09-08 stsp free(ibuf);
560 ddc7b220 2019-09-08 stsp return err;
563 9f2369b0 2018-12-24 stsp pid = fork();
564 ddc7b220 2019-09-08 stsp if (pid == -1) {
565 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
566 ddc7b220 2019-09-08 stsp free(ibuf);
567 ddc7b220 2019-09-08 stsp return err;
569 9f2369b0 2018-12-24 stsp else if (pid == 0) {
570 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
571 9f2369b0 2018-12-24 stsp repo->path);
572 9f2369b0 2018-12-24 stsp /* not reached */
575 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
576 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
577 ddc7b220 2019-09-08 stsp free(ibuf);
578 ddc7b220 2019-09-08 stsp return err;
581 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
582 9f2369b0 2018-12-24 stsp imsg_fds[0];
583 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
584 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
585 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
587 59d1e4a0 2021-03-10 stsp return NULL;
590 b3d68e7f 2021-07-03 stsp const struct got_error *
591 b3d68e7f 2021-07-03 stsp got_object_read_header_privsep(struct got_object **obj,
592 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo, int obj_fd)
594 59d1e4a0 2021-03-10 stsp const struct got_error *err;
596 59d1e4a0 2021-03-10 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
597 d5c81d44 2021-07-08 stsp return request_object(obj, id, repo, obj_fd);
599 59d1e4a0 2021-03-10 stsp err = start_read_object_child(repo);
601 7495ec13 2021-04-04 stsp close(obj_fd);
602 59d1e4a0 2021-03-10 stsp return err;
605 d5c81d44 2021-07-08 stsp return request_object(obj, id, repo, obj_fd);
608 59d1e4a0 2021-03-10 stsp static const struct got_error *
609 59d1e4a0 2021-03-10 stsp read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
610 d5c81d44 2021-07-08 stsp int outfd, struct got_object_id *id, struct got_repository *repo,
611 d5c81d44 2021-07-08 stsp int obj_fd)
613 59d1e4a0 2021-03-10 stsp const struct got_error *err;
615 59d1e4a0 2021-03-10 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
616 d5c81d44 2021-07-08 stsp return request_raw_object(outbuf, size, hdrlen, outfd, id,
617 d5c81d44 2021-07-08 stsp repo, obj_fd);
619 59d1e4a0 2021-03-10 stsp err = start_read_object_child(repo);
621 59d1e4a0 2021-03-10 stsp return err;
623 d5c81d44 2021-07-08 stsp return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
627 4558fcd4 2018-01-14 stsp const struct got_error *
628 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
629 4558fcd4 2018-01-14 stsp struct got_object_id *id)
631 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
634 7bb0daa1 2018-06-21 stsp *obj = got_repo_get_cached_object(repo, id);
635 7bb0daa1 2018-06-21 stsp if (*obj != NULL) {
636 7bb0daa1 2018-06-21 stsp (*obj)->refcnt++;
637 7bb0daa1 2018-06-21 stsp return NULL;
640 b3d68e7f 2021-07-03 stsp err = got_object_open_packed(obj, id, repo);
641 e82b1d81 2019-07-27 stsp if (err && err->code != GOT_ERR_NO_OBJ)
642 e82b1d81 2019-07-27 stsp return err;
643 e82b1d81 2019-07-27 stsp if (*obj) {
644 e82b1d81 2019-07-27 stsp (*obj)->refcnt++;
645 e82b1d81 2019-07-27 stsp return got_repo_cache_object(repo, id, *obj);
648 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
650 762d73f4 2021-04-10 stsp if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
651 762d73f4 2021-04-10 stsp err = got_error_no_obj(id);
652 762d73f4 2021-04-10 stsp return err;
655 d5c81d44 2021-07-08 stsp err = got_object_read_header_privsep(obj, id, repo, fd);
657 4558fcd4 2018-01-14 stsp return err;
659 762d73f4 2021-04-10 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
661 59790a32 2018-09-15 stsp (*obj)->refcnt++;
662 762d73f4 2021-04-10 stsp return got_repo_cache_object(repo, id, *obj);
665 8ab9215c 2021-10-15 thomas /* *outfd must be initialized to -1 by caller */
666 59d1e4a0 2021-03-10 stsp const struct got_error *
667 8ab9215c 2021-10-15 thomas got_object_raw_open(struct got_raw_object **obj, int *outfd,
668 f8bb1d3e 2021-10-17 thomas struct got_repository *repo, struct got_object_id *id)
670 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
671 59d1e4a0 2021-03-10 stsp struct got_packidx *packidx = NULL;
673 59d1e4a0 2021-03-10 stsp uint8_t *outbuf = NULL;
674 59d1e4a0 2021-03-10 stsp off_t size = 0;
675 59d1e4a0 2021-03-10 stsp size_t hdrlen = 0;
676 59d1e4a0 2021-03-10 stsp char *path_packfile = NULL;
678 8ab9215c 2021-10-15 thomas *obj = got_repo_get_cached_raw_object(repo, id);
679 8ab9215c 2021-10-15 thomas if (*obj != NULL) {
680 8ab9215c 2021-10-15 thomas (*obj)->refcnt++;
681 8ab9215c 2021-10-15 thomas return NULL;
684 8ab9215c 2021-10-15 thomas if (*outfd == -1) {
685 8ab9215c 2021-10-15 thomas *outfd = got_opentempfd();
686 8ab9215c 2021-10-15 thomas if (*outfd == -1)
687 8ab9215c 2021-10-15 thomas return got_error_from_errno("got_opentempfd");
690 59d1e4a0 2021-03-10 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
691 59d1e4a0 2021-03-10 stsp if (err == NULL) {
692 59d1e4a0 2021-03-10 stsp struct got_pack *pack = NULL;
694 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
695 aea75d87 2021-07-06 stsp packidx->path_packidx);
699 59d1e4a0 2021-03-10 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
700 59d1e4a0 2021-03-10 stsp if (pack == NULL) {
701 59d1e4a0 2021-03-10 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
706 59d1e4a0 2021-03-10 stsp err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
707 8ab9215c 2021-10-15 thomas *outfd, pack, packidx, idx, id);
709 dd3af45a 2021-10-15 thomas goto done;
710 59d1e4a0 2021-03-10 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
713 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
716 8ab9215c 2021-10-15 thomas err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
717 d5c81d44 2021-07-08 stsp id, repo, fd);
719 dd3af45a 2021-10-15 thomas goto done;
722 59d1e4a0 2021-03-10 stsp *obj = calloc(1, sizeof(**obj));
723 59d1e4a0 2021-03-10 stsp if (*obj == NULL) {
724 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("calloc");
727 2b0ae357 2022-01-10 thomas (*obj)->fd = -1;
729 59d1e4a0 2021-03-10 stsp if (outbuf) {
730 59d1e4a0 2021-03-10 stsp (*obj)->data = outbuf;
732 59d1e4a0 2021-03-10 stsp struct stat sb;
733 8ab9215c 2021-10-15 thomas if (fstat(*outfd, &sb) == -1) {
734 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fstat");
738 a8591711 2021-06-18 stsp if (sb.st_size != hdrlen + size) {
739 59d1e4a0 2021-03-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
742 2b0ae357 2022-01-10 thomas #ifndef GOT_PACK_NO_MMAP
743 2b0ae357 2022-01-10 thomas if (hdrlen + size > 0) {
744 2b0ae357 2022-01-10 thomas (*obj)->data = mmap(NULL, hdrlen + size, PROT_READ,
745 2b0ae357 2022-01-10 thomas MAP_PRIVATE, *outfd, 0);
746 2b0ae357 2022-01-10 thomas if ((*obj)->data == MAP_FAILED) {
747 2b0ae357 2022-01-10 thomas if (errno != ENOMEM) {
748 2b0ae357 2022-01-10 thomas err = got_error_from_errno("mmap");
749 2b0ae357 2022-01-10 thomas goto done;
751 2b0ae357 2022-01-10 thomas (*obj)->data = NULL;
753 2b0ae357 2022-01-10 thomas (*obj)->fd = *outfd;
754 2b0ae357 2022-01-10 thomas *outfd = -1;
758 2b0ae357 2022-01-10 thomas if (*outfd != -1) {
759 2b0ae357 2022-01-10 thomas (*obj)->f = fdopen(*outfd, "r");
760 2b0ae357 2022-01-10 thomas if ((*obj)->f == NULL) {
761 2b0ae357 2022-01-10 thomas err = got_error_from_errno("fdopen");
762 2b0ae357 2022-01-10 thomas goto done;
764 2b0ae357 2022-01-10 thomas *outfd = -1;
767 59d1e4a0 2021-03-10 stsp (*obj)->hdrlen = hdrlen;
768 59d1e4a0 2021-03-10 stsp (*obj)->size = size;
769 8ab9215c 2021-10-15 thomas err = got_repo_cache_raw_object(repo, id, *obj);
771 59d1e4a0 2021-03-10 stsp free(path_packfile);
773 59d1e4a0 2021-03-10 stsp if (*obj) {
774 59d1e4a0 2021-03-10 stsp got_object_raw_close(*obj);
775 59d1e4a0 2021-03-10 stsp *obj = NULL;
777 59d1e4a0 2021-03-10 stsp free(outbuf);
779 8ab9215c 2021-10-15 thomas (*obj)->refcnt++;
780 59d1e4a0 2021-03-10 stsp return err;
783 59d1e4a0 2021-03-10 stsp const struct got_error *
784 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
785 6dfa2fd3 2018-02-12 stsp const char *id_str)
787 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
789 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
790 6dd1ece6 2019-11-10 stsp return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
792 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
795 15a94983 2018-12-23 stsp const struct got_error *
796 15a94983 2018-12-23 stsp got_object_resolve_id_str(struct got_object_id **id,
797 15a94983 2018-12-23 stsp struct got_repository *repo, const char *id_str)
799 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
800 15a94983 2018-12-23 stsp struct got_object *obj;
802 15a94983 2018-12-23 stsp err = got_object_open_by_id_str(&obj, repo, id_str);
804 15a94983 2018-12-23 stsp return err;
806 15a94983 2018-12-23 stsp *id = got_object_id_dup(got_object_get_id(obj));
807 15a94983 2018-12-23 stsp got_object_close(obj);
808 15a94983 2018-12-23 stsp if (*id == NULL)
809 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
811 15a94983 2018-12-23 stsp return NULL;
814 434025f3 2018-09-16 stsp static const struct got_error *
815 a158c901 2018-12-23 stsp request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
816 a158c901 2018-12-23 stsp int pack_idx, struct got_object_id *id)
818 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
820 a158c901 2018-12-23 stsp err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
823 a158c901 2018-12-23 stsp return err;
825 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
827 ca6e02ac 2020-01-07 stsp return err;
829 ca6e02ac 2020-01-07 stsp (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
830 ca6e02ac 2020-01-07 stsp return NULL;
833 a158c901 2018-12-23 stsp static const struct got_error *
834 a158c901 2018-12-23 stsp read_packed_commit_privsep(struct got_commit_object **commit,
835 a158c901 2018-12-23 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
836 a158c901 2018-12-23 stsp struct got_object_id *id)
838 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
840 a158c901 2018-12-23 stsp if (pack->privsep_child)
841 a158c901 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
843 711fb6e8 2018-12-23 stsp err = start_pack_privsep_child(pack, packidx);
845 a158c901 2018-12-23 stsp return err;
847 711fb6e8 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
850 9f2369b0 2018-12-24 stsp static const struct got_error *
851 9f2369b0 2018-12-24 stsp request_commit(struct got_commit_object **commit, struct got_repository *repo,
852 d5c81d44 2021-07-08 stsp int fd, struct got_object_id *id)
854 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
855 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
857 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
859 d5c81d44 2021-07-08 stsp err = got_privsep_send_commit_req(ibuf, fd, id, -1);
861 9f2369b0 2018-12-24 stsp return err;
863 9f2369b0 2018-12-24 stsp return got_privsep_recv_commit(commit, ibuf);
866 9f2369b0 2018-12-24 stsp static const struct got_error *
867 9f2369b0 2018-12-24 stsp read_commit_privsep(struct got_commit_object **commit, int obj_fd,
868 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo)
870 ddc7b220 2019-09-08 stsp const struct got_error *err;
871 9f2369b0 2018-12-24 stsp int imsg_fds[2];
873 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
875 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
876 d5c81d44 2021-07-08 stsp return request_commit(commit, repo, obj_fd, id);
878 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
879 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
880 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
882 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
883 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
884 ddc7b220 2019-09-08 stsp free(ibuf);
885 ddc7b220 2019-09-08 stsp return err;
888 9f2369b0 2018-12-24 stsp pid = fork();
889 ddc7b220 2019-09-08 stsp if (pid == -1) {
890 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
891 ddc7b220 2019-09-08 stsp free(ibuf);
892 ddc7b220 2019-09-08 stsp return err;
894 9f2369b0 2018-12-24 stsp else if (pid == 0) {
895 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
896 9f2369b0 2018-12-24 stsp repo->path);
897 9f2369b0 2018-12-24 stsp /* not reached */
900 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
901 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
902 ddc7b220 2019-09-08 stsp free(ibuf);
903 ddc7b220 2019-09-08 stsp return err;
905 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
906 9f2369b0 2018-12-24 stsp imsg_fds[0];
907 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
908 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
909 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
911 d5c81d44 2021-07-08 stsp return request_commit(commit, repo, obj_fd, id);
915 a158c901 2018-12-23 stsp static const struct got_error *
916 434025f3 2018-09-16 stsp open_commit(struct got_commit_object **commit,
917 1785f84a 2018-12-23 stsp struct got_repository *repo, struct got_object_id *id, int check_cache)
919 434025f3 2018-09-16 stsp const struct got_error *err = NULL;
920 1785f84a 2018-12-23 stsp struct got_packidx *packidx = NULL;
922 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
924 434025f3 2018-09-16 stsp if (check_cache) {
925 1785f84a 2018-12-23 stsp *commit = got_repo_get_cached_commit(repo, id);
926 434025f3 2018-09-16 stsp if (*commit != NULL) {
927 434025f3 2018-09-16 stsp (*commit)->refcnt++;
928 434025f3 2018-09-16 stsp return NULL;
931 434025f3 2018-09-16 stsp *commit = NULL;
933 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
934 e82b1d81 2019-07-27 stsp if (err == NULL) {
935 1785f84a 2018-12-23 stsp struct got_pack *pack = NULL;
937 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
938 aea75d87 2021-07-06 stsp packidx->path_packidx);
940 1785f84a 2018-12-23 stsp return err;
942 1785f84a 2018-12-23 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
943 434025f3 2018-09-16 stsp if (pack == NULL) {
944 1785f84a 2018-12-23 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
949 a158c901 2018-12-23 stsp err = read_packed_commit_privsep(commit, pack,
950 1785f84a 2018-12-23 stsp packidx, idx, id);
951 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
954 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
956 e82b1d81 2019-07-27 stsp return err;
957 d5c81d44 2021-07-08 stsp err = read_commit_privsep(commit, fd, id, repo);
960 434025f3 2018-09-16 stsp if (err == NULL) {
961 434025f3 2018-09-16 stsp (*commit)->refcnt++;
962 1785f84a 2018-12-23 stsp err = got_repo_cache_commit(repo, id, *commit);
965 8d2c5ea3 2019-08-13 stsp free(path_packfile);
966 e32baab7 2018-11-05 stsp return err;
969 e32baab7 2018-11-05 stsp const struct got_error *
970 e32baab7 2018-11-05 stsp got_object_open_as_commit(struct got_commit_object **commit,
971 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object_id *id)
973 e32baab7 2018-11-05 stsp *commit = got_repo_get_cached_commit(repo, id);
974 e32baab7 2018-11-05 stsp if (*commit != NULL) {
975 e32baab7 2018-11-05 stsp (*commit)->refcnt++;
976 e32baab7 2018-11-05 stsp return NULL;
979 1785f84a 2018-12-23 stsp return open_commit(commit, repo, id, 0);
982 e32baab7 2018-11-05 stsp const struct got_error *
983 e32baab7 2018-11-05 stsp got_object_commit_open(struct got_commit_object **commit,
984 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object *obj)
986 1785f84a 2018-12-23 stsp return open_commit(commit, repo, got_object_get_id(obj), 1);
989 434025f3 2018-09-16 stsp const struct got_error *
990 dbc6a6b6 2018-07-12 stsp got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
992 dbc6a6b6 2018-07-12 stsp *qid = calloc(1, sizeof(**qid));
993 dbc6a6b6 2018-07-12 stsp if (*qid == NULL)
994 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
996 ec242592 2022-04-22 thomas memcpy(&(*qid)->id, id, sizeof((*qid)->id));
997 9ca9aafb 2021-06-18 stsp return NULL;
1000 9ca9aafb 2021-06-18 stsp const struct got_error *
1001 9ca9aafb 2021-06-18 stsp got_object_id_queue_copy(const struct got_object_id_queue *src,
1002 9ca9aafb 2021-06-18 stsp struct got_object_id_queue *dest)
1004 9ca9aafb 2021-06-18 stsp const struct got_error *err;
1005 9ca9aafb 2021-06-18 stsp struct got_object_qid *qid;
1007 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, src, entry) {
1008 9ca9aafb 2021-06-18 stsp struct got_object_qid *new;
1010 9ca9aafb 2021-06-18 stsp * Deep-copy the object ID only. Let the caller deal
1011 9ca9aafb 2021-06-18 stsp * with setting up the new->data pointer if needed.
1013 ec242592 2022-04-22 thomas err = got_object_qid_alloc(&new, &qid->id);
1014 9ca9aafb 2021-06-18 stsp if (err) {
1015 9ca9aafb 2021-06-18 stsp got_object_id_queue_free(dest);
1016 9ca9aafb 2021-06-18 stsp return err;
1018 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(dest, new, entry);
1021 dbc6a6b6 2018-07-12 stsp return NULL;
1024 a158c901 2018-12-23 stsp static const struct got_error *
1025 13c729f7 2018-12-24 stsp request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
1026 13c729f7 2018-12-24 stsp int pack_idx, struct got_object_id *id)
1028 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
1030 13c729f7 2018-12-24 stsp err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
1031 13c729f7 2018-12-24 stsp pack_idx);
1033 a158c901 2018-12-23 stsp return err;
1035 a158c901 2018-12-23 stsp return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
1038 71eb0e7f 2018-09-16 stsp static const struct got_error *
1039 13c729f7 2018-12-24 stsp read_packed_tree_privsep(struct got_tree_object **tree,
1040 13c729f7 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1041 13c729f7 2018-12-24 stsp struct got_object_id *id)
1043 13c729f7 2018-12-24 stsp const struct got_error *err = NULL;
1045 13c729f7 2018-12-24 stsp if (pack->privsep_child)
1046 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
1048 13c729f7 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
1050 13c729f7 2018-12-24 stsp return err;
1052 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
1055 13c729f7 2018-12-24 stsp static const struct got_error *
1056 9f2369b0 2018-12-24 stsp request_tree(struct got_tree_object **tree, struct got_repository *repo,
1057 d5c81d44 2021-07-08 stsp int fd, struct got_object_id *id)
1059 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1060 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1062 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
1064 d5c81d44 2021-07-08 stsp err = got_privsep_send_tree_req(ibuf, fd, id, -1);
1066 9f2369b0 2018-12-24 stsp return err;
1068 9f2369b0 2018-12-24 stsp return got_privsep_recv_tree(tree, ibuf);
1071 9f2369b0 2018-12-24 stsp const struct got_error *
1072 9f2369b0 2018-12-24 stsp read_tree_privsep(struct got_tree_object **tree, int obj_fd,
1073 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo)
1075 ddc7b220 2019-09-08 stsp const struct got_error *err;
1076 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1077 9f2369b0 2018-12-24 stsp pid_t pid;
1078 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1080 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
1081 d5c81d44 2021-07-08 stsp return request_tree(tree, repo, obj_fd, id);
1083 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1084 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1085 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1087 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1088 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1089 ddc7b220 2019-09-08 stsp free(ibuf);
1090 ddc7b220 2019-09-08 stsp return err;
1093 9f2369b0 2018-12-24 stsp pid = fork();
1094 ddc7b220 2019-09-08 stsp if (pid == -1) {
1095 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1096 ddc7b220 2019-09-08 stsp free(ibuf);
1097 ddc7b220 2019-09-08 stsp return err;
1099 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1100 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1101 9f2369b0 2018-12-24 stsp repo->path);
1102 9f2369b0 2018-12-24 stsp /* not reached */
1105 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
1106 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1107 ddc7b220 2019-09-08 stsp free(ibuf);
1108 ddc7b220 2019-09-08 stsp return err;
1110 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1111 9f2369b0 2018-12-24 stsp imsg_fds[0];
1112 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1113 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1114 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1117 d5c81d44 2021-07-08 stsp return request_tree(tree, repo, obj_fd, id);
1120 9f2369b0 2018-12-24 stsp static const struct got_error *
1121 13c729f7 2018-12-24 stsp open_tree(struct got_tree_object **tree, struct got_repository *repo,
1122 13c729f7 2018-12-24 stsp struct got_object_id *id, int check_cache)
1124 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
1125 13c729f7 2018-12-24 stsp struct got_packidx *packidx = NULL;
1127 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1129 71eb0e7f 2018-09-16 stsp if (check_cache) {
1130 13c729f7 2018-12-24 stsp *tree = got_repo_get_cached_tree(repo, id);
1131 71eb0e7f 2018-09-16 stsp if (*tree != NULL) {
1132 71eb0e7f 2018-09-16 stsp (*tree)->refcnt++;
1133 71eb0e7f 2018-09-16 stsp return NULL;
1136 71eb0e7f 2018-09-16 stsp *tree = NULL;
1138 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1139 e82b1d81 2019-07-27 stsp if (err == NULL) {
1140 13c729f7 2018-12-24 stsp struct got_pack *pack = NULL;
1142 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
1143 aea75d87 2021-07-06 stsp packidx->path_packidx);
1145 13c729f7 2018-12-24 stsp return err;
1147 13c729f7 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1148 e7885405 2018-09-10 stsp if (pack == NULL) {
1149 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1152 8d2c5ea3 2019-08-13 stsp goto done;
1154 13c729f7 2018-12-24 stsp err = read_packed_tree_privsep(tree, pack,
1155 13c729f7 2018-12-24 stsp packidx, idx, id);
1156 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1159 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1161 e82b1d81 2019-07-27 stsp return err;
1162 d5c81d44 2021-07-08 stsp err = read_tree_privsep(tree, fd, id, repo);
1165 f6be5c30 2018-06-22 stsp if (err == NULL) {
1166 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
1167 13c729f7 2018-12-24 stsp err = got_repo_cache_tree(repo, id, *tree);
1170 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1171 776d4d29 2018-06-17 stsp return err;
1174 776d4d29 2018-06-17 stsp const struct got_error *
1175 776d4d29 2018-06-17 stsp got_object_open_as_tree(struct got_tree_object **tree,
1176 776d4d29 2018-06-17 stsp struct got_repository *repo, struct got_object_id *id)
1178 e8eb494a 2018-09-16 stsp *tree = got_repo_get_cached_tree(repo, id);
1179 e8eb494a 2018-09-16 stsp if (*tree != NULL) {
1180 e8eb494a 2018-09-16 stsp (*tree)->refcnt++;
1181 e8eb494a 2018-09-16 stsp return NULL;
1184 13c729f7 2018-12-24 stsp return open_tree(tree, repo, id, 0);
1187 71eb0e7f 2018-09-16 stsp const struct got_error *
1188 71eb0e7f 2018-09-16 stsp got_object_tree_open(struct got_tree_object **tree,
1189 71eb0e7f 2018-09-16 stsp struct got_repository *repo, struct got_object *obj)
1191 13c729f7 2018-12-24 stsp return open_tree(tree, repo, got_object_get_id(obj), 1);
1195 56e0773d 2019-11-28 stsp got_object_tree_get_nentries(struct got_tree_object *tree)
1197 56e0773d 2019-11-28 stsp return tree->nentries;
1200 56e0773d 2019-11-28 stsp struct got_tree_entry *
1201 56e0773d 2019-11-28 stsp got_object_tree_get_first_entry(struct got_tree_object *tree)
1203 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, 0);
1206 56e0773d 2019-11-28 stsp struct got_tree_entry *
1207 56e0773d 2019-11-28 stsp got_object_tree_get_last_entry(struct got_tree_object *tree)
1209 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, tree->nentries - 1);
1212 56e0773d 2019-11-28 stsp struct got_tree_entry *
1213 56e0773d 2019-11-28 stsp got_object_tree_get_entry(struct got_tree_object *tree, int i)
1215 56e0773d 2019-11-28 stsp if (i < 0 || i >= tree->nentries)
1216 56e0773d 2019-11-28 stsp return NULL;
1217 56e0773d 2019-11-28 stsp return &tree->entries[i];
1221 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(struct got_tree_entry *te)
1223 56e0773d 2019-11-28 stsp return te->mode;
1226 56e0773d 2019-11-28 stsp const char *
1227 56e0773d 2019-11-28 stsp got_tree_entry_get_name(struct got_tree_entry *te)
1229 56e0773d 2019-11-28 stsp return &te->name[0];
1232 56e0773d 2019-11-28 stsp struct got_object_id *
1233 56e0773d 2019-11-28 stsp got_tree_entry_get_id(struct got_tree_entry *te)
1235 56e0773d 2019-11-28 stsp return &te->id;
1238 0d6c6ee3 2020-05-20 stsp const struct got_error *
1239 af57b12a 2020-07-23 stsp got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1241 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
1242 32596e16 2020-07-23 stsp size_t len, totlen, hdrlen, offset;
1244 aa092692 2020-07-23 stsp *s = NULL;
1246 659dc16e 2020-07-23 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1247 659dc16e 2020-07-23 stsp totlen = 0;
1248 32596e16 2020-07-23 stsp offset = 0;
1252 659dc16e 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1254 af57b12a 2020-07-23 stsp return err;
1256 659dc16e 2020-07-23 stsp if (len == 0)
1259 659dc16e 2020-07-23 stsp totlen += len - hdrlen;
1260 af57b12a 2020-07-23 stsp p = realloc(*s, totlen + 1);
1261 659dc16e 2020-07-23 stsp if (p == NULL) {
1262 659dc16e 2020-07-23 stsp err = got_error_from_errno("realloc");
1264 af57b12a 2020-07-23 stsp *s = NULL;
1265 af57b12a 2020-07-23 stsp return err;
1268 659dc16e 2020-07-23 stsp /* Skip blob object header first time around. */
1269 af57b12a 2020-07-23 stsp memcpy(*s + offset,
1270 f8f7c882 2020-07-23 stsp got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1271 659dc16e 2020-07-23 stsp hdrlen = 0;
1272 32596e16 2020-07-23 stsp offset = totlen;
1273 659dc16e 2020-07-23 stsp } while (len > 0);
1275 af57b12a 2020-07-23 stsp (*s)[totlen] = '\0';
1276 af57b12a 2020-07-23 stsp return NULL;
1279 af57b12a 2020-07-23 stsp const struct got_error *
1280 af57b12a 2020-07-23 stsp got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1281 af57b12a 2020-07-23 stsp struct got_repository *repo)
1283 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
1284 af57b12a 2020-07-23 stsp struct got_blob_object *blob = NULL;
1286 af57b12a 2020-07-23 stsp *link_target = NULL;
1288 af57b12a 2020-07-23 stsp if (!got_object_tree_entry_is_symlink(te))
1289 af57b12a 2020-07-23 stsp return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1291 af57b12a 2020-07-23 stsp err = got_object_open_as_blob(&blob, repo,
1292 af57b12a 2020-07-23 stsp got_tree_entry_get_id(te), PATH_MAX);
1294 af57b12a 2020-07-23 stsp return err;
1296 af57b12a 2020-07-23 stsp err = got_object_blob_read_to_str(link_target, blob);
1297 af57b12a 2020-07-23 stsp got_object_blob_close(blob);
1298 659dc16e 2020-07-23 stsp if (err) {
1299 659dc16e 2020-07-23 stsp free(*link_target);
1300 659dc16e 2020-07-23 stsp *link_target = NULL;
1302 0d6c6ee3 2020-05-20 stsp return err;
1306 56e0773d 2019-11-28 stsp got_tree_entry_get_index(struct got_tree_entry *te)
1308 56e0773d 2019-11-28 stsp return te->idx;
1311 56e0773d 2019-11-28 stsp struct got_tree_entry *
1312 56e0773d 2019-11-28 stsp got_tree_entry_get_next(struct got_tree_object *tree,
1313 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
1315 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx + 1);
1318 56e0773d 2019-11-28 stsp struct got_tree_entry *
1319 56e0773d 2019-11-28 stsp got_tree_entry_get_prev(struct got_tree_object *tree,
1320 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
1322 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx - 1);
1325 3840f4c9 2018-09-12 stsp static const struct got_error *
1326 ac544f8c 2019-01-13 stsp request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1327 ebc55e2d 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1328 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
1330 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
1331 bc1f382f 2022-01-05 thomas struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1332 bc1f382f 2022-01-05 thomas int outfd_child;
1334 bc1f382f 2022-01-05 thomas err = pack_child_send_tempfiles(ibuf, pack);
1335 bc1f382f 2022-01-05 thomas if (err)
1336 bc1f382f 2022-01-05 thomas return err;
1338 3840f4c9 2018-09-12 stsp outfd_child = dup(outfd);
1339 3840f4c9 2018-09-12 stsp if (outfd_child == -1)
1340 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1342 ebc55e2d 2018-12-24 stsp err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1344 3840f4c9 2018-09-12 stsp return err;
1346 3840f4c9 2018-09-12 stsp err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1347 3840f4c9 2018-09-12 stsp outfd_child);
1348 3840f4c9 2018-09-12 stsp if (err) {
1349 3840f4c9 2018-09-12 stsp return err;
1352 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen,
1353 ebc55e2d 2018-12-24 stsp pack->privsep_child->ibuf);
1355 3840f4c9 2018-09-12 stsp return err;
1357 3840f4c9 2018-09-12 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1358 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1360 3840f4c9 2018-09-12 stsp return err;
1363 ebc55e2d 2018-12-24 stsp static const struct got_error *
1364 ac544f8c 2019-01-13 stsp read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1365 ac544f8c 2019-01-13 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1366 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
1368 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
1370 ebc55e2d 2018-12-24 stsp if (pack->privsep_child == NULL) {
1371 ebc55e2d 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
1373 ebc55e2d 2018-12-24 stsp return err;
1376 ac544f8c 2019-01-13 stsp return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1380 9f2369b0 2018-12-24 stsp static const struct got_error *
1381 ac544f8c 2019-01-13 stsp request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1382 d5c81d44 2021-07-08 stsp int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1384 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1385 9f2369b0 2018-12-24 stsp int outfd_child;
1387 9f2369b0 2018-12-24 stsp outfd_child = dup(outfd);
1388 9f2369b0 2018-12-24 stsp if (outfd_child == -1)
1389 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1391 d5c81d44 2021-07-08 stsp err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1393 9f2369b0 2018-12-24 stsp return err;
1395 9f2369b0 2018-12-24 stsp err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1397 9f2369b0 2018-12-24 stsp return err;
1399 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1401 9f2369b0 2018-12-24 stsp return err;
1403 9f2369b0 2018-12-24 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1404 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
1406 9f2369b0 2018-12-24 stsp return err;
1409 9f2369b0 2018-12-24 stsp static const struct got_error *
1410 ac544f8c 2019-01-13 stsp read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1411 d5c81d44 2021-07-08 stsp int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1413 ddc7b220 2019-09-08 stsp const struct got_error *err;
1414 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1415 9f2369b0 2018-12-24 stsp pid_t pid;
1416 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1418 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1419 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1420 d5c81d44 2021-07-08 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1424 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1425 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1426 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1428 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1429 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1430 ddc7b220 2019-09-08 stsp free(ibuf);
1431 ddc7b220 2019-09-08 stsp return err;
1434 9f2369b0 2018-12-24 stsp pid = fork();
1435 ddc7b220 2019-09-08 stsp if (pid == -1) {
1436 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1437 ddc7b220 2019-09-08 stsp free(ibuf);
1438 ddc7b220 2019-09-08 stsp return err;
1440 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1441 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1442 9f2369b0 2018-12-24 stsp repo->path);
1443 9f2369b0 2018-12-24 stsp /* not reached */
1446 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
1447 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1448 ddc7b220 2019-09-08 stsp free(ibuf);
1449 ddc7b220 2019-09-08 stsp return err;
1451 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1452 9f2369b0 2018-12-24 stsp imsg_fds[0];
1453 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1454 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1455 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1457 d5c81d44 2021-07-08 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1460 ebc55e2d 2018-12-24 stsp static const struct got_error *
1461 ebc55e2d 2018-12-24 stsp open_blob(struct got_blob_object **blob, struct got_repository *repo,
1462 ebc55e2d 2018-12-24 stsp struct got_object_id *id, size_t blocksize)
1464 ebc55e2d 2018-12-24 stsp const struct got_error *err = NULL;
1465 ebc55e2d 2018-12-24 stsp struct got_packidx *packidx = NULL;
1467 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1468 ac544f8c 2019-01-13 stsp uint8_t *outbuf;
1469 e82b1d81 2019-07-27 stsp int outfd;
1470 ebc55e2d 2018-12-24 stsp size_t size, hdrlen;
1471 ebc55e2d 2018-12-24 stsp struct stat sb;
1473 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
1474 4558fcd4 2018-01-14 stsp if (*blob == NULL)
1475 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1477 55da3778 2018-09-10 stsp outfd = got_opentempfd();
1478 55da3778 2018-09-10 stsp if (outfd == -1)
1479 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
1481 062ebb78 2018-07-12 stsp (*blob)->read_buf = malloc(blocksize);
1482 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
1483 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1484 c7254d79 2018-04-24 stsp goto done;
1487 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1488 e82b1d81 2019-07-27 stsp if (err == NULL) {
1489 ebc55e2d 2018-12-24 stsp struct got_pack *pack = NULL;
1491 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
1492 aea75d87 2021-07-06 stsp packidx->path_packidx);
1494 ebc55e2d 2018-12-24 stsp goto done;
1496 ebc55e2d 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1497 55da3778 2018-09-10 stsp if (pack == NULL) {
1498 ebc55e2d 2018-12-24 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1501 55da3778 2018-09-10 stsp goto done;
1503 ac544f8c 2019-01-13 stsp err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1504 ac544f8c 2019-01-13 stsp pack, packidx, idx, id);
1505 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1508 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&infd, id, repo);
1510 e82b1d81 2019-07-27 stsp goto done;
1511 ac544f8c 2019-01-13 stsp err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1512 d5c81d44 2021-07-08 stsp id, repo);
1515 55da3778 2018-09-10 stsp goto done;
1517 de060dff 2018-12-24 stsp if (hdrlen > size) {
1518 ebc55e2d 2018-12-24 stsp err = got_error(GOT_ERR_BAD_OBJ_HDR);
1519 ebc55e2d 2018-12-24 stsp goto done;
1522 ac544f8c 2019-01-13 stsp if (outbuf) {
1523 08578a35 2021-01-22 stsp if (close(outfd) == -1 && err == NULL)
1524 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1525 ac544f8c 2019-01-13 stsp outfd = -1;
1526 ac544f8c 2019-01-13 stsp (*blob)->f = fmemopen(outbuf, size, "rb");
1527 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1528 638f9024 2019-05-13 stsp err = got_error_from_errno("fmemopen");
1529 ac544f8c 2019-01-13 stsp free(outbuf);
1530 ac544f8c 2019-01-13 stsp goto done;
1532 ac544f8c 2019-01-13 stsp (*blob)->data = outbuf;
1534 ac544f8c 2019-01-13 stsp if (fstat(outfd, &sb) == -1) {
1535 638f9024 2019-05-13 stsp err = got_error_from_errno("fstat");
1536 ac544f8c 2019-01-13 stsp goto done;
1539 ac544f8c 2019-01-13 stsp if (sb.st_size != size) {
1540 ac544f8c 2019-01-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1541 ac544f8c 2019-01-13 stsp goto done;
1544 ac544f8c 2019-01-13 stsp (*blob)->f = fdopen(outfd, "rb");
1545 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1546 638f9024 2019-05-13 stsp err = got_error_from_errno("fdopen");
1547 ac544f8c 2019-01-13 stsp close(outfd);
1548 ac544f8c 2019-01-13 stsp outfd = -1;
1549 ac544f8c 2019-01-13 stsp goto done;
1553 ebc55e2d 2018-12-24 stsp (*blob)->hdrlen = hdrlen;
1554 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
1555 ebc55e2d 2018-12-24 stsp memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1558 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1559 55da3778 2018-09-10 stsp if (err) {
1560 55da3778 2018-09-10 stsp if (*blob) {
1561 7baf5860 2019-03-19 stsp got_object_blob_close(*blob);
1562 55da3778 2018-09-10 stsp *blob = NULL;
1563 55da3778 2018-09-10 stsp } else if (outfd != -1)
1564 55da3778 2018-09-10 stsp close(outfd);
1566 a19581a2 2018-06-21 stsp return err;
1569 a19581a2 2018-06-21 stsp const struct got_error *
1570 a19581a2 2018-06-21 stsp got_object_open_as_blob(struct got_blob_object **blob,
1571 a19581a2 2018-06-21 stsp struct got_repository *repo, struct got_object_id *id,
1572 a19581a2 2018-06-21 stsp size_t blocksize)
1574 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, id, blocksize);
1577 ebc55e2d 2018-12-24 stsp const struct got_error *
1578 ebc55e2d 2018-12-24 stsp got_object_blob_open(struct got_blob_object **blob,
1579 ebc55e2d 2018-12-24 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
1581 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1584 fb43ecf1 2019-02-11 stsp const struct got_error *
1585 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
1587 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL;
1588 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
1589 56b63ca4 2021-01-22 stsp if (blob->f && fclose(blob->f) == EOF)
1590 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1591 ac544f8c 2019-01-13 stsp free(blob->data);
1592 68482ea3 2017-11-27 stsp free(blob);
1593 fb43ecf1 2019-02-11 stsp return err;
1597 8ba819a3 2020-07-23 stsp got_object_blob_rewind(struct got_blob_object *blob)
1599 8ba819a3 2020-07-23 stsp if (blob->f)
1600 8ba819a3 2020-07-23 stsp rewind(blob->f);
1604 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1606 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1610 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
1612 f934cf2c 2018-02-12 stsp return blob->hdrlen;
1615 f934cf2c 2018-02-12 stsp const uint8_t *
1616 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
1618 f934cf2c 2018-02-12 stsp return blob->read_buf;
1621 68482ea3 2017-11-27 stsp const struct got_error *
1622 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1626 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1627 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
1628 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
1629 eb651edf 2018-02-11 stsp *outlenp = n;
1630 35e9ba5d 2018-06-21 stsp return NULL;
1633 35e9ba5d 2018-06-21 stsp const struct got_error *
1634 be659d10 2020-11-18 stsp got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1635 6c4c42e0 2019-06-24 stsp off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1637 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
1638 b6752625 2018-12-24 stsp size_t n, len, hdrlen;
1639 84451b3e 2018-07-10 stsp const uint8_t *buf;
1641 c33ebc60 2020-11-18 stsp const int alloc_chunksz = 512;
1642 c33ebc60 2020-11-18 stsp size_t nalloc = 0;
1643 f595d9bd 2019-08-14 stsp off_t off = 0, total_len = 0;
1645 6c4c42e0 2019-06-24 stsp if (line_offsets)
1646 6c4c42e0 2019-06-24 stsp *line_offsets = NULL;
1647 f595d9bd 2019-08-14 stsp if (filesize)
1648 f595d9bd 2019-08-14 stsp *filesize = 0;
1649 84451b3e 2018-07-10 stsp if (nlines)
1650 84451b3e 2018-07-10 stsp *nlines = 0;
1652 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1654 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
1656 35e9ba5d 2018-06-21 stsp return err;
1657 35e9ba5d 2018-06-21 stsp if (len == 0)
1659 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
1660 b02560ec 2019-08-19 stsp i = hdrlen;
1661 f1cbc3bc 2020-11-18 stsp if (nlines) {
1662 f1cbc3bc 2020-11-18 stsp if (line_offsets && *line_offsets == NULL) {
1663 78695fb7 2019-08-12 stsp /* Have some data but perhaps no '\n'. */
1664 78695fb7 2019-08-12 stsp *nlines = 1;
1665 c33ebc60 2020-11-18 stsp nalloc = alloc_chunksz;
1666 c33ebc60 2020-11-18 stsp *line_offsets = calloc(nalloc,
1667 c33ebc60 2020-11-18 stsp sizeof(**line_offsets));
1668 78695fb7 2019-08-12 stsp if (*line_offsets == NULL)
1669 845785d4 2020-02-02 tracey return got_error_from_errno("calloc");
1671 b02560ec 2019-08-19 stsp /* Skip forward over end of first line. */
1672 b02560ec 2019-08-19 stsp while (i < len) {
1673 b02560ec 2019-08-19 stsp if (buf[i] == '\n')
1678 b02560ec 2019-08-19 stsp /* Scan '\n' offsets in remaining chunk of data. */
1679 b02560ec 2019-08-19 stsp while (i < len) {
1680 b02560ec 2019-08-19 stsp if (buf[i] != '\n') {
1684 f595d9bd 2019-08-14 stsp (*nlines)++;
1685 c33ebc60 2020-11-18 stsp if (line_offsets && nalloc < *nlines) {
1686 c33ebc60 2020-11-18 stsp size_t n = *nlines + alloc_chunksz;
1687 78695fb7 2019-08-12 stsp off_t *o = recallocarray(*line_offsets,
1688 c33ebc60 2020-11-18 stsp nalloc, n, sizeof(**line_offsets));
1689 78695fb7 2019-08-12 stsp if (o == NULL) {
1690 78695fb7 2019-08-12 stsp free(*line_offsets);
1691 78695fb7 2019-08-12 stsp *line_offsets = NULL;
1692 78695fb7 2019-08-12 stsp return got_error_from_errno(
1693 78695fb7 2019-08-12 stsp "recallocarray");
1695 78695fb7 2019-08-12 stsp *line_offsets = o;
1696 c33ebc60 2020-11-18 stsp nalloc = n;
1698 f1cbc3bc 2020-11-18 stsp if (line_offsets) {
1699 f1cbc3bc 2020-11-18 stsp off = total_len + i - hdrlen + 1;
1700 f1cbc3bc 2020-11-18 stsp (*line_offsets)[*nlines - 1] = off;
1705 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
1706 454a6b59 2018-12-24 stsp n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1707 b6752625 2018-12-24 stsp if (n != len - hdrlen)
1708 b6752625 2018-12-24 stsp return got_ferror(outfile, GOT_ERR_IO);
1709 f595d9bd 2019-08-14 stsp total_len += len - hdrlen;
1710 35e9ba5d 2018-06-21 stsp hdrlen = 0;
1711 35e9ba5d 2018-06-21 stsp } while (len != 0);
1713 cbe7f848 2019-02-11 stsp if (fflush(outfile) != 0)
1714 638f9024 2019-05-13 stsp return got_error_from_errno("fflush");
1715 35e9ba5d 2018-06-21 stsp rewind(outfile);
1717 f595d9bd 2019-08-14 stsp if (filesize)
1718 f595d9bd 2019-08-14 stsp *filesize = total_len;
1720 776d4d29 2018-06-17 stsp return NULL;
1723 f4a881ce 2018-11-17 stsp static const struct got_error *
1724 268f7291 2018-12-24 stsp request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1725 268f7291 2018-12-24 stsp int pack_idx, struct got_object_id *id)
1727 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
1729 268f7291 2018-12-24 stsp err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1730 268f7291 2018-12-24 stsp pack_idx);
1732 a158c901 2018-12-23 stsp return err;
1734 a158c901 2018-12-23 stsp return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1737 268f7291 2018-12-24 stsp static const struct got_error *
1738 268f7291 2018-12-24 stsp read_packed_tag_privsep(struct got_tag_object **tag,
1739 268f7291 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1740 268f7291 2018-12-24 stsp struct got_object_id *id)
1742 268f7291 2018-12-24 stsp const struct got_error *err = NULL;
1744 268f7291 2018-12-24 stsp if (pack->privsep_child)
1745 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1747 268f7291 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
1749 268f7291 2018-12-24 stsp return err;
1751 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1754 9f2369b0 2018-12-24 stsp static const struct got_error *
1755 9f2369b0 2018-12-24 stsp request_tag(struct got_tag_object **tag, struct got_repository *repo,
1756 d5c81d44 2021-07-08 stsp int fd, struct got_object_id *id)
1758 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1759 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1761 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1763 d5c81d44 2021-07-08 stsp err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1765 9f2369b0 2018-12-24 stsp return err;
1767 9f2369b0 2018-12-24 stsp return got_privsep_recv_tag(tag, ibuf);
1770 9f2369b0 2018-12-24 stsp static const struct got_error *
1771 9f2369b0 2018-12-24 stsp read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1772 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo)
1774 ddc7b220 2019-09-08 stsp const struct got_error *err;
1775 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1776 9f2369b0 2018-12-24 stsp pid_t pid;
1777 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1779 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1780 d5c81d44 2021-07-08 stsp return request_tag(tag, repo, obj_fd, id);
1782 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1783 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1784 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1786 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1787 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1788 ddc7b220 2019-09-08 stsp free(ibuf);
1789 ddc7b220 2019-09-08 stsp return err;
1792 9f2369b0 2018-12-24 stsp pid = fork();
1793 ddc7b220 2019-09-08 stsp if (pid == -1) {
1794 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1795 ddc7b220 2019-09-08 stsp free(ibuf);
1796 ddc7b220 2019-09-08 stsp return err;
1798 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1799 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1800 9f2369b0 2018-12-24 stsp repo->path);
1801 9f2369b0 2018-12-24 stsp /* not reached */
1804 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
1805 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1806 ddc7b220 2019-09-08 stsp free(ibuf);
1807 ddc7b220 2019-09-08 stsp return err;
1809 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1810 9f2369b0 2018-12-24 stsp imsg_fds[0];
1811 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1812 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1813 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1815 d5c81d44 2021-07-08 stsp return request_tag(tag, repo, obj_fd, id);
1818 a158c901 2018-12-23 stsp static const struct got_error *
1819 268f7291 2018-12-24 stsp open_tag(struct got_tag_object **tag, struct got_repository *repo,
1820 268f7291 2018-12-24 stsp struct got_object_id *id, int check_cache)
1822 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1823 268f7291 2018-12-24 stsp struct got_packidx *packidx = NULL;
1825 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1826 5d844a1e 2019-08-13 stsp struct got_object *obj = NULL;
1827 5d844a1e 2019-08-13 stsp int obj_type = GOT_OBJ_TYPE_ANY;
1829 f4a881ce 2018-11-17 stsp if (check_cache) {
1830 268f7291 2018-12-24 stsp *tag = got_repo_get_cached_tag(repo, id);
1831 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1832 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1833 f4a881ce 2018-11-17 stsp return NULL;
1836 f4a881ce 2018-11-17 stsp *tag = NULL;
1838 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1839 e82b1d81 2019-07-27 stsp if (err == NULL) {
1840 268f7291 2018-12-24 stsp struct got_pack *pack = NULL;
1842 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
1843 aea75d87 2021-07-06 stsp packidx->path_packidx);
1845 268f7291 2018-12-24 stsp return err;
1847 268f7291 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1848 f4a881ce 2018-11-17 stsp if (pack == NULL) {
1849 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1852 8d2c5ea3 2019-08-13 stsp goto done;
1855 992eb9d8 2020-02-07 tracey /* Beware of "lightweight" tags: Check object type first. */
1856 5d844a1e 2019-08-13 stsp err = read_packed_object_privsep(&obj, repo, pack, packidx,
1859 5d844a1e 2019-08-13 stsp goto done;
1860 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1861 5d844a1e 2019-08-13 stsp got_object_close(obj);
1862 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG) {
1863 5d844a1e 2019-08-13 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1864 5d844a1e 2019-08-13 stsp goto done;
1866 5d844a1e 2019-08-13 stsp err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1867 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1870 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1872 e82b1d81 2019-07-27 stsp return err;
1873 d5c81d44 2021-07-08 stsp err = got_object_read_header_privsep(&obj, id, repo, fd);
1875 5d844a1e 2019-08-13 stsp return err;
1876 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1877 5d844a1e 2019-08-13 stsp got_object_close(obj);
1878 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
1879 5d844a1e 2019-08-13 stsp return got_error(GOT_ERR_OBJ_TYPE);
1881 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1883 5d844a1e 2019-08-13 stsp return err;
1884 d5c81d44 2021-07-08 stsp err = read_tag_privsep(tag, fd, id, repo);
1887 f4a881ce 2018-11-17 stsp if (err == NULL) {
1888 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1889 268f7291 2018-12-24 stsp err = got_repo_cache_tag(repo, id, *tag);
1892 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1893 f4a881ce 2018-11-17 stsp return err;
1896 f4a881ce 2018-11-17 stsp const struct got_error *
1897 f4a881ce 2018-11-17 stsp got_object_open_as_tag(struct got_tag_object **tag,
1898 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object_id *id)
1900 f4a881ce 2018-11-17 stsp *tag = got_repo_get_cached_tag(repo, id);
1901 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1902 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1903 f4a881ce 2018-11-17 stsp return NULL;
1906 268f7291 2018-12-24 stsp return open_tag(tag, repo, id, 0);
1909 f4a881ce 2018-11-17 stsp const struct got_error *
1910 f4a881ce 2018-11-17 stsp got_object_tag_open(struct got_tag_object **tag,
1911 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object *obj)
1913 268f7291 2018-12-24 stsp return open_tag(tag, repo, got_object_get_id(obj), 1);
1916 d24820bf 2019-08-11 stsp const char *
1917 d24820bf 2019-08-11 stsp got_object_tag_get_name(struct got_tag_object *tag)
1919 d24820bf 2019-08-11 stsp return tag->tag;
1923 0bd18d37 2019-02-01 stsp got_object_tag_get_object_type(struct got_tag_object *tag)
1925 0bd18d37 2019-02-01 stsp return tag->obj_type;
1928 0bd18d37 2019-02-01 stsp struct got_object_id *
1929 0bd18d37 2019-02-01 stsp got_object_tag_get_object_id(struct got_tag_object *tag)
1931 0bd18d37 2019-02-01 stsp return &tag->id;
1935 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_time(struct got_tag_object *tag)
1937 01073a5d 2019-08-22 stsp return tag->tagger_time;
1941 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1943 01073a5d 2019-08-22 stsp return tag->tagger_gmtoff;
1946 01073a5d 2019-08-22 stsp const char *
1947 01073a5d 2019-08-22 stsp got_object_tag_get_tagger(struct got_tag_object *tag)
1949 01073a5d 2019-08-22 stsp return tag->tagger;
1952 01073a5d 2019-08-22 stsp const char *
1953 01073a5d 2019-08-22 stsp got_object_tag_get_message(struct got_tag_object *tag)
1955 01073a5d 2019-08-22 stsp return tag->tagmsg;
1958 776d4d29 2018-06-17 stsp static struct got_tree_entry *
1959 65a9bbe9 2018-09-15 stsp find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1963 63da309a 2018-11-07 stsp /* Note that tree entries are sorted in strncmp() order. */
1964 56e0773d 2019-11-28 stsp for (i = 0; i < tree->nentries; i++) {
1965 56e0773d 2019-11-28 stsp struct got_tree_entry *te = &tree->entries[i];
1966 63da309a 2018-11-07 stsp int cmp = strncmp(te->name, name, len);
1967 63da309a 2018-11-07 stsp if (cmp < 0)
1969 63da309a 2018-11-07 stsp if (cmp > 0)
1971 63da309a 2018-11-07 stsp if (te->name[len] == '\0')
1972 776d4d29 2018-06-17 stsp return te;
1974 eb651edf 2018-02-11 stsp return NULL;
1977 56e0773d 2019-11-28 stsp struct got_tree_entry *
1978 a129376b 2019-03-28 stsp got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1980 a129376b 2019-03-28 stsp return find_entry_by_name(tree, name, strlen(name));
1983 776d4d29 2018-06-17 stsp const struct got_error *
1984 cc8021af 2021-10-12 thomas got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1985 cc8021af 2021-10-12 thomas struct got_repository *repo, struct got_tree_object *tree,
1986 cc8021af 2021-10-12 thomas const char *path)
1988 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
1989 cc8021af 2021-10-12 thomas struct got_tree_object *subtree = NULL;
1990 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
1991 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1992 b7cd37e5 2018-11-18 stsp size_t seglen;
1994 27d434c2 2018-09-15 stsp *id = NULL;
1997 5e54fb30 2019-05-31 stsp while (s[0] == '/')
2000 65a9bbe9 2018-09-15 stsp seglen = 0;
2001 cc8021af 2021-10-12 thomas subtree = tree;
2002 b7cd37e5 2018-11-18 stsp while (*s) {
2003 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
2005 776d4d29 2018-06-17 stsp if (*s != '/') {
2012 cc8021af 2021-10-12 thomas te = find_entry_by_name(subtree, seg, seglen);
2013 db37e2c0 2018-06-21 stsp if (te == NULL) {
2014 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2015 776d4d29 2018-06-17 stsp goto done;
2018 b7cd37e5 2018-11-18 stsp if (*s == '\0')
2021 776d4d29 2018-06-17 stsp seg = s + 1;
2022 65a9bbe9 2018-09-15 stsp seglen = 0;
2025 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
2027 db37e2c0 2018-06-21 stsp te = NULL;
2029 776d4d29 2018-06-17 stsp goto done;
2030 cc8021af 2021-10-12 thomas if (subtree != tree)
2031 cc8021af 2021-10-12 thomas got_object_tree_close(subtree);
2032 cc8021af 2021-10-12 thomas subtree = next_tree;
2037 56e0773d 2019-11-28 stsp *id = got_object_id_dup(&te->id);
2038 27d434c2 2018-09-15 stsp if (*id == NULL)
2039 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2040 cc8021af 2021-10-12 thomas if (mode)
2041 cc8021af 2021-10-12 thomas *mode = te->mode;
2043 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2045 cc8021af 2021-10-12 thomas if (subtree && subtree != tree)
2046 cc8021af 2021-10-12 thomas got_object_tree_close(subtree);
2047 cc8021af 2021-10-12 thomas return err;
2049 cc8021af 2021-10-12 thomas const struct got_error *
2050 cc8021af 2021-10-12 thomas got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
2051 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
2053 cc8021af 2021-10-12 thomas const struct got_error *err = NULL;
2054 cc8021af 2021-10-12 thomas struct got_tree_object *tree = NULL;
2056 cc8021af 2021-10-12 thomas *id = NULL;
2058 cc8021af 2021-10-12 thomas /* Handle opening of root of commit's tree. */
2059 cc8021af 2021-10-12 thomas if (got_path_is_root_dir(path)) {
2060 cc8021af 2021-10-12 thomas *id = got_object_id_dup(commit->tree_id);
2061 cc8021af 2021-10-12 thomas if (*id == NULL)
2062 cc8021af 2021-10-12 thomas err = got_error_from_errno("got_object_id_dup");
2063 cc8021af 2021-10-12 thomas } else {
2064 cc8021af 2021-10-12 thomas err = got_object_open_as_tree(&tree, repo, commit->tree_id);
2065 cc8021af 2021-10-12 thomas if (err)
2066 cc8021af 2021-10-12 thomas goto done;
2067 cc8021af 2021-10-12 thomas err = got_object_tree_find_path(id, NULL, repo, tree, path);
2071 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
2072 776d4d29 2018-06-17 stsp return err;
2076 ac5f2b26 2020-05-05 stsp * Normalize file mode bits to avoid false positive tree entry differences
2077 ac5f2b26 2020-05-05 stsp * in case tree entries have unexpected mode bits set.
2079 ac5f2b26 2020-05-05 stsp static mode_t
2080 ac5f2b26 2020-05-05 stsp normalize_mode_for_comparison(mode_t mode)
2083 ac5f2b26 2020-05-05 stsp * For directories, the only relevant bit is the IFDIR bit.
2084 ac5f2b26 2020-05-05 stsp * This allows us to detect paths changing from a directory
2085 ac5f2b26 2020-05-05 stsp * to a file and vice versa.
2087 ac5f2b26 2020-05-05 stsp if (S_ISDIR(mode))
2088 ac5f2b26 2020-05-05 stsp return mode & S_IFDIR;
2091 40dde666 2020-07-23 stsp * For symlinks, the only relevant bit is the IFLNK bit.
2092 40dde666 2020-07-23 stsp * This allows us to detect paths changing from a symlinks
2093 40dde666 2020-07-23 stsp * to a file or directory and vice versa.
2095 40dde666 2020-07-23 stsp if (S_ISLNK(mode))
2096 40dde666 2020-07-23 stsp return mode & S_IFLNK;
2098 ac5f2b26 2020-05-05 stsp /* For files, the only change we care about is the executable bit. */
2099 ac5f2b26 2020-05-05 stsp return mode & S_IXUSR;
2102 07862c20 2018-09-15 stsp const struct got_error *
2103 07862c20 2018-09-15 stsp got_object_tree_path_changed(int *changed,
2104 07862c20 2018-09-15 stsp struct got_tree_object *tree01, struct got_tree_object *tree02,
2105 07862c20 2018-09-15 stsp const char *path, struct got_repository *repo)
2107 07862c20 2018-09-15 stsp const struct got_error *err = NULL;
2108 07862c20 2018-09-15 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2109 07862c20 2018-09-15 stsp struct got_tree_entry *te1 = NULL, *te2 = NULL;
2110 65a9bbe9 2018-09-15 stsp const char *seg, *s;
2111 3b7f9878 2018-11-18 stsp size_t seglen;
2113 07862c20 2018-09-15 stsp *changed = 0;
2115 07862c20 2018-09-15 stsp /* We not do support comparing the root path. */
2116 61a7d79f 2020-02-29 stsp if (got_path_is_root_dir(path))
2117 63f810e6 2020-02-29 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
2119 07862c20 2018-09-15 stsp tree1 = tree01;
2120 07862c20 2018-09-15 stsp tree2 = tree02;
2122 61a7d79f 2020-02-29 stsp while (*s == '/')
2125 65a9bbe9 2018-09-15 stsp seglen = 0;
2126 3b7f9878 2018-11-18 stsp while (*s) {
2127 07862c20 2018-09-15 stsp struct got_tree_object *next_tree1, *next_tree2;
2128 ac5f2b26 2020-05-05 stsp mode_t mode1, mode2;
2130 07862c20 2018-09-15 stsp if (*s != '/') {
2137 65a9bbe9 2018-09-15 stsp te1 = find_entry_by_name(tree1, seg, seglen);
2138 07862c20 2018-09-15 stsp if (te1 == NULL) {
2139 07862c20 2018-09-15 stsp err = got_error(GOT_ERR_NO_OBJ);
2140 07862c20 2018-09-15 stsp goto done;
2143 e8bfb8f3 2020-12-18 stsp if (tree2)
2144 e8bfb8f3 2020-12-18 stsp te2 = find_entry_by_name(tree2, seg, seglen);
2146 e8bfb8f3 2020-12-18 stsp if (te2) {
2147 e8bfb8f3 2020-12-18 stsp mode1 = normalize_mode_for_comparison(te1->mode);
2148 e8bfb8f3 2020-12-18 stsp mode2 = normalize_mode_for_comparison(te2->mode);
2149 e8bfb8f3 2020-12-18 stsp if (mode1 != mode2) {
2150 e8bfb8f3 2020-12-18 stsp *changed = 1;
2151 e8bfb8f3 2020-12-18 stsp goto done;
2154 e8bfb8f3 2020-12-18 stsp if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2155 e8bfb8f3 2020-12-18 stsp *changed = 0;
2156 e8bfb8f3 2020-12-18 stsp goto done;
2160 3b7f9878 2018-11-18 stsp if (*s == '\0') { /* final path element */
2161 07862c20 2018-09-15 stsp *changed = 1;
2162 07862c20 2018-09-15 stsp goto done;
2165 07862c20 2018-09-15 stsp seg = s + 1;
2167 65a9bbe9 2018-09-15 stsp seglen = 0;
2169 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree1, repo,
2170 56e0773d 2019-11-28 stsp &te1->id);
2171 07862c20 2018-09-15 stsp te1 = NULL;
2173 07862c20 2018-09-15 stsp goto done;
2174 a31cea73 2018-09-15 stsp if (tree1 != tree01)
2175 a31cea73 2018-09-15 stsp got_object_tree_close(tree1);
2176 07862c20 2018-09-15 stsp tree1 = next_tree1;
2178 e8bfb8f3 2020-12-18 stsp if (te2) {
2179 e8bfb8f3 2020-12-18 stsp err = got_object_open_as_tree(&next_tree2, repo,
2180 e8bfb8f3 2020-12-18 stsp &te2->id);
2181 e8bfb8f3 2020-12-18 stsp te2 = NULL;
2183 e8bfb8f3 2020-12-18 stsp goto done;
2184 e8bfb8f3 2020-12-18 stsp if (tree2 != tree02)
2185 e8bfb8f3 2020-12-18 stsp got_object_tree_close(tree2);
2186 e8bfb8f3 2020-12-18 stsp tree2 = next_tree2;
2187 e8bfb8f3 2020-12-18 stsp } else if (tree2) {
2188 e8bfb8f3 2020-12-18 stsp if (tree2 != tree02)
2189 e8bfb8f3 2020-12-18 stsp got_object_tree_close(tree2);
2190 e8bfb8f3 2020-12-18 stsp tree2 = NULL;
2195 a31cea73 2018-09-15 stsp if (tree1 && tree1 != tree01)
2196 07862c20 2018-09-15 stsp got_object_tree_close(tree1);
2197 a31cea73 2018-09-15 stsp if (tree2 && tree2 != tree02)
2198 07862c20 2018-09-15 stsp got_object_tree_close(tree2);
2199 77880158 2018-11-04 stsp return err;
2202 ed175427 2019-05-09 stsp const struct got_error *
2203 ed175427 2019-05-09 stsp got_object_tree_entry_dup(struct got_tree_entry **new_te,
2204 ed175427 2019-05-09 stsp struct got_tree_entry *te)
2206 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
2208 ed175427 2019-05-09 stsp *new_te = calloc(1, sizeof(**new_te));
2209 ed175427 2019-05-09 stsp if (*new_te == NULL)
2210 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
2212 ed175427 2019-05-09 stsp (*new_te)->mode = te->mode;
2213 56e0773d 2019-11-28 stsp memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2214 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2215 8c4eabf2 2019-05-10 stsp return err;
2219 56e0773d 2019-11-28 stsp got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2221 63c5ca5d 2019-08-24 stsp return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2225 e40622f4 2020-07-23 stsp got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2227 e40622f4 2020-07-23 stsp /* S_IFDIR check avoids confusing symlinks with submodules. */
2228 e40622f4 2020-07-23 stsp return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2231 e40622f4 2020-07-23 stsp static const struct got_error *
2232 e40622f4 2020-07-23 stsp resolve_symlink(char **link_target, const char *path,
2233 945f9229 2022-04-16 thomas struct got_commit_object *commit, struct got_repository *repo)
2235 e40622f4 2020-07-23 stsp const struct got_error *err = NULL;
2236 dbdd6209 2020-10-19 stsp char buf[PATH_MAX];
2237 e40622f4 2020-07-23 stsp char *name, *parent_path = NULL;
2238 e40622f4 2020-07-23 stsp struct got_object_id *tree_obj_id = NULL;
2239 e40622f4 2020-07-23 stsp struct got_tree_object *tree = NULL;
2240 e40622f4 2020-07-23 stsp struct got_tree_entry *te = NULL;
2242 e40622f4 2020-07-23 stsp *link_target = NULL;
2244 dbdd6209 2020-10-19 stsp if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2245 dbdd6209 2020-10-19 stsp return got_error(GOT_ERR_NO_SPACE);
2247 dbdd6209 2020-10-19 stsp name = basename(buf);
2248 e40622f4 2020-07-23 stsp if (name == NULL)
2249 e40622f4 2020-07-23 stsp return got_error_from_errno2("basename", path);
2251 e40622f4 2020-07-23 stsp err = got_path_dirname(&parent_path, path);
2253 e40622f4 2020-07-23 stsp return err;
2255 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_obj_id, repo, commit,
2256 e40622f4 2020-07-23 stsp parent_path);
2257 e40622f4 2020-07-23 stsp if (err) {
2258 e40622f4 2020-07-23 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2259 e40622f4 2020-07-23 stsp /* Display the complete path in error message. */
2260 e40622f4 2020-07-23 stsp err = got_error_path(path, err->code);
2262 e40622f4 2020-07-23 stsp goto done;
2265 e40622f4 2020-07-23 stsp err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2267 e40622f4 2020-07-23 stsp goto done;
2269 e40622f4 2020-07-23 stsp te = got_object_tree_find_entry(tree, name);
2270 e40622f4 2020-07-23 stsp if (te == NULL) {
2271 e40622f4 2020-07-23 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2272 e40622f4 2020-07-23 stsp goto done;
2275 e40622f4 2020-07-23 stsp if (got_object_tree_entry_is_symlink(te)) {
2276 e40622f4 2020-07-23 stsp err = got_tree_entry_get_symlink_target(link_target, te, repo);
2278 e40622f4 2020-07-23 stsp goto done;
2279 e40622f4 2020-07-23 stsp if (!got_path_is_absolute(*link_target)) {
2280 e40622f4 2020-07-23 stsp char *abspath;
2281 e40622f4 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", parent_path,
2282 e40622f4 2020-07-23 stsp *link_target) == -1) {
2283 e40622f4 2020-07-23 stsp err = got_error_from_errno("asprintf");
2284 e40622f4 2020-07-23 stsp goto done;
2286 e40622f4 2020-07-23 stsp free(*link_target);
2287 e40622f4 2020-07-23 stsp *link_target = malloc(PATH_MAX);
2288 e40622f4 2020-07-23 stsp if (*link_target == NULL) {
2289 e40622f4 2020-07-23 stsp err = got_error_from_errno("malloc");
2290 e40622f4 2020-07-23 stsp goto done;
2292 e40622f4 2020-07-23 stsp err = got_canonpath(abspath, *link_target, PATH_MAX);
2293 e40622f4 2020-07-23 stsp free(abspath);
2295 e40622f4 2020-07-23 stsp goto done;
2299 e40622f4 2020-07-23 stsp free(tree_obj_id);
2301 e40622f4 2020-07-23 stsp got_object_tree_close(tree);
2302 e40622f4 2020-07-23 stsp if (err) {
2303 e40622f4 2020-07-23 stsp free(*link_target);
2304 e40622f4 2020-07-23 stsp *link_target = NULL;
2306 e40622f4 2020-07-23 stsp return err;
2309 ca6e02ac 2020-01-07 stsp const struct got_error *
2310 e40622f4 2020-07-23 stsp got_object_resolve_symlinks(char **link_target, const char *path,
2311 945f9229 2022-04-16 thomas struct got_commit_object *commit, struct got_repository *repo)
2313 e40622f4 2020-07-23 stsp const struct got_error *err = NULL;
2314 e40622f4 2020-07-23 stsp char *next_target = NULL;
2315 e40622f4 2020-07-23 stsp int max_recursion = 40; /* matches Git */
2317 e40622f4 2020-07-23 stsp *link_target = NULL;
2320 e40622f4 2020-07-23 stsp err = resolve_symlink(&next_target,
2321 945f9229 2022-04-16 thomas *link_target ? *link_target : path, commit, repo);
2324 e40622f4 2020-07-23 stsp if (next_target) {
2325 e40622f4 2020-07-23 stsp free(*link_target);
2326 e40622f4 2020-07-23 stsp if (--max_recursion == 0) {
2327 e40622f4 2020-07-23 stsp err = got_error_path(path, GOT_ERR_RECURSION);
2328 e40622f4 2020-07-23 stsp *link_target = NULL;
2331 e40622f4 2020-07-23 stsp *link_target = next_target;
2333 e40622f4 2020-07-23 stsp } while (next_target);
2335 e40622f4 2020-07-23 stsp return err;
2338 e40622f4 2020-07-23 stsp const struct got_error *
2339 ca6e02ac 2020-01-07 stsp got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2340 ca6e02ac 2020-01-07 stsp struct got_object_id *commit_id, const char *path,
2341 ca6e02ac 2020-01-07 stsp struct got_repository *repo)
2343 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
2344 ca6e02ac 2020-01-07 stsp struct got_pack *pack = NULL;
2345 ca6e02ac 2020-01-07 stsp struct got_packidx *packidx = NULL;
2346 ca6e02ac 2020-01-07 stsp char *path_packfile = NULL;
2347 ca6e02ac 2020-01-07 stsp struct got_commit_object *changed_commit = NULL;
2348 ca6e02ac 2020-01-07 stsp struct got_object_id *changed_commit_id = NULL;
2351 ca6e02ac 2020-01-07 stsp err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2352 ca6e02ac 2020-01-07 stsp if (err) {
2353 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
2354 ca6e02ac 2020-01-07 stsp return err;
2355 ca6e02ac 2020-01-07 stsp return NULL;
2358 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
2359 aea75d87 2021-07-06 stsp packidx->path_packidx);
2361 ca6e02ac 2020-01-07 stsp return err;
2363 ca6e02ac 2020-01-07 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
2364 ca6e02ac 2020-01-07 stsp if (pack == NULL) {
2365 ca6e02ac 2020-01-07 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2367 ca6e02ac 2020-01-07 stsp goto done;
2370 ca6e02ac 2020-01-07 stsp if (pack->privsep_child == NULL) {
2371 ca6e02ac 2020-01-07 stsp err = start_pack_privsep_child(pack, packidx);
2373 ca6e02ac 2020-01-07 stsp goto done;
2376 ca6e02ac 2020-01-07 stsp err = got_privsep_send_commit_traversal_request(
2377 ca6e02ac 2020-01-07 stsp pack->privsep_child->ibuf, commit_id, idx, path);
2379 ca6e02ac 2020-01-07 stsp goto done;
2381 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_traversed_commits(&changed_commit,
2382 ca6e02ac 2020-01-07 stsp &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2384 ca6e02ac 2020-01-07 stsp goto done;
2386 ca6e02ac 2020-01-07 stsp if (changed_commit) {
2388 ca6e02ac 2020-01-07 stsp * Cache the commit in which the path was changed.
2389 ca6e02ac 2020-01-07 stsp * This commit might be opened again soon.
2391 ca6e02ac 2020-01-07 stsp changed_commit->refcnt++;
2392 ca6e02ac 2020-01-07 stsp err = got_repo_cache_commit(repo, changed_commit_id,
2393 ca6e02ac 2020-01-07 stsp changed_commit);
2394 ca6e02ac 2020-01-07 stsp got_object_commit_close(changed_commit);
2397 ca6e02ac 2020-01-07 stsp free(path_packfile);
2398 ca6e02ac 2020-01-07 stsp free(changed_commit_id);
2399 ca6e02ac 2020-01-07 stsp return err;
2402 63915ee5 2022-06-23 thomas const struct got_error *
2403 e71f1e62 2022-06-23 thomas got_object_enumerate(int *found_all_objects,
2404 e71f1e62 2022-06-23 thomas got_object_enumerate_commit_cb cb_commit,
2405 63915ee5 2022-06-23 thomas got_object_enumerate_tree_cb cb_tree, void *cb_arg,
2406 63915ee5 2022-06-23 thomas struct got_object_id **ours, int nours,
2407 63915ee5 2022-06-23 thomas struct got_object_id **theirs, int ntheirs,
2408 63915ee5 2022-06-23 thomas struct got_packidx *packidx, struct got_repository *repo)
2410 63915ee5 2022-06-23 thomas const struct got_error *err = NULL;
2411 63915ee5 2022-06-23 thomas struct got_pack *pack;
2412 63915ee5 2022-06-23 thomas char *path_packfile = NULL;
2414 63915ee5 2022-06-23 thomas err = got_packidx_get_packfile_path(&path_packfile,
2415 63915ee5 2022-06-23 thomas packidx->path_packidx);
2416 63915ee5 2022-06-23 thomas if (err)
2417 63915ee5 2022-06-23 thomas return err;
2419 63915ee5 2022-06-23 thomas pack = got_repo_get_cached_pack(repo, path_packfile);
2420 63915ee5 2022-06-23 thomas if (pack == NULL) {
2421 63915ee5 2022-06-23 thomas err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2422 63915ee5 2022-06-23 thomas if (err)
2423 63915ee5 2022-06-23 thomas goto done;
2426 63915ee5 2022-06-23 thomas if (pack->privsep_child == NULL) {
2427 63915ee5 2022-06-23 thomas err = start_pack_privsep_child(pack, packidx);
2428 63915ee5 2022-06-23 thomas if (err)
2429 63915ee5 2022-06-23 thomas goto done;
2432 63915ee5 2022-06-23 thomas err = got_privsep_send_object_enumeration_request(
2433 63915ee5 2022-06-23 thomas pack->privsep_child->ibuf);
2434 63915ee5 2022-06-23 thomas if (err)
2435 63915ee5 2022-06-23 thomas goto done;
2437 63915ee5 2022-06-23 thomas err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
2438 63915ee5 2022-06-23 thomas ours, nours);
2439 63915ee5 2022-06-23 thomas if (err)
2440 63915ee5 2022-06-23 thomas goto done;
2441 63915ee5 2022-06-23 thomas err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
2442 63915ee5 2022-06-23 thomas if (err)
2443 63915ee5 2022-06-23 thomas goto done;
2445 63915ee5 2022-06-23 thomas err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
2446 63915ee5 2022-06-23 thomas theirs, ntheirs);
2447 63915ee5 2022-06-23 thomas if (err)
2448 63915ee5 2022-06-23 thomas goto done;
2449 63915ee5 2022-06-23 thomas err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
2450 63915ee5 2022-06-23 thomas if (err)
2451 63915ee5 2022-06-23 thomas goto done;
2453 e71f1e62 2022-06-23 thomas err = got_privsep_recv_enumerated_objects(found_all_objects,
2454 e71f1e62 2022-06-23 thomas pack->privsep_child->ibuf, cb_commit, cb_tree, cb_arg, repo);
2456 63915ee5 2022-06-23 thomas free(path_packfile);
2457 63915ee5 2022-06-23 thomas return err;