Blame


1 d71d75ad 2017-11-05 stsp /*
2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 d71d75ad 2017-11-05 stsp *
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.
7 d71d75ad 2017-11-05 stsp *
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.
15 d71d75ad 2017-11-05 stsp */
16 d71d75ad 2017-11-05 stsp
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>
24 d1cda826 2017-11-06 stsp
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>
37 dd038bc6 2021-09-21 thomas.ad
38 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
39 d71d75ad 2017-11-05 stsp
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"
45 d71d75ad 2017-11-05 stsp
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"
56 1411938b 2018-02-12 stsp
57 ab9a70b2 2017-11-06 stsp #ifndef MIN
58 ab9a70b2 2017-11-06 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 ab9a70b2 2017-11-06 stsp #endif
60 3235492e 2018-04-01 stsp
61 3235492e 2018-04-01 stsp struct got_object_id *
62 3235492e 2018-04-01 stsp got_object_get_id(struct got_object *obj)
63 3235492e 2018-04-01 stsp {
64 6402fb3c 2018-09-15 stsp return &obj->id;
65 bacc9935 2018-05-20 stsp }
66 bacc9935 2018-05-20 stsp
67 bacc9935 2018-05-20 stsp const struct got_error *
68 bacc9935 2018-05-20 stsp got_object_get_id_str(char **outbuf, struct got_object *obj)
69 bacc9935 2018-05-20 stsp {
70 bacc9935 2018-05-20 stsp return got_object_id_str(outbuf, &obj->id);
71 a1fd68d8 2018-01-12 stsp }
72 d71d75ad 2017-11-05 stsp
73 15a94983 2018-12-23 stsp const struct got_error *
74 15a94983 2018-12-23 stsp got_object_get_type(int *type, struct got_repository *repo,
75 15a94983 2018-12-23 stsp struct got_object_id *id)
76 a1fd68d8 2018-01-12 stsp {
77 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
78 15a94983 2018-12-23 stsp struct got_object *obj;
79 15a94983 2018-12-23 stsp
80 15a94983 2018-12-23 stsp err = got_object_open(&obj, repo, id);
81 15a94983 2018-12-23 stsp if (err)
82 15a94983 2018-12-23 stsp return err;
83 15a94983 2018-12-23 stsp
84 b107e67f 2018-01-19 stsp switch (obj->type) {
85 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_COMMIT:
86 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_TREE:
87 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_BLOB:
88 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
89 15a94983 2018-12-23 stsp *type = obj->type;
90 15a94983 2018-12-23 stsp break;
91 96f5e8b3 2018-01-23 stsp default:
92 15a94983 2018-12-23 stsp err = got_error(GOT_ERR_OBJ_TYPE);
93 96f5e8b3 2018-01-23 stsp break;
94 d71d75ad 2017-11-05 stsp }
95 d71d75ad 2017-11-05 stsp
96 15a94983 2018-12-23 stsp got_object_close(obj);
97 15a94983 2018-12-23 stsp return err;
98 d71d75ad 2017-11-05 stsp }
99 ab9a70b2 2017-11-06 stsp
100 90bdb554 2019-04-11 stsp const struct got_error *
101 90bdb554 2019-04-11 stsp got_object_get_path(char **path, struct got_object_id *id,
102 90bdb554 2019-04-11 stsp struct got_repository *repo)
103 ab9a70b2 2017-11-06 stsp {
104 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
105 7a132809 2018-07-23 stsp char *hex = NULL;
106 41d2888b 2019-08-11 stsp char *path_objects;
107 e6b1056e 2018-04-22 stsp
108 e6b1056e 2018-04-22 stsp *path = NULL;
109 ab9a70b2 2017-11-06 stsp
110 41d2888b 2019-08-11 stsp path_objects = got_repo_get_path_objects(repo);
111 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
112 638f9024 2019-05-13 stsp return got_error_from_errno("got_repo_get_path_objects");
113 ab9a70b2 2017-11-06 stsp
114 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
115 ef0981d5 2018-02-12 stsp if (err)
116 7a132809 2018-07-23 stsp goto done;
117 ab9a70b2 2017-11-06 stsp
118 d1cda826 2017-11-06 stsp if (asprintf(path, "%s/%.2x/%s", path_objects,
119 d1cda826 2017-11-06 stsp id->sha1[0], hex + 2) == -1)
120 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
121 ab9a70b2 2017-11-06 stsp
122 7a132809 2018-07-23 stsp done:
123 ef0981d5 2018-02-12 stsp free(hex);
124 d1cda826 2017-11-06 stsp free(path_objects);
125 d1cda826 2017-11-06 stsp return err;
126 d1cda826 2017-11-06 stsp }
127 d1cda826 2017-11-06 stsp
128 762d73f4 2021-04-10 stsp const struct got_error *
129 762d73f4 2021-04-10 stsp got_object_open_loose_fd(int *fd, struct got_object_id *id,
130 4796fb13 2018-12-23 stsp struct got_repository *repo)
131 d1cda826 2017-11-06 stsp {
132 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
133 a1fd68d8 2018-01-12 stsp char *path;
134 6c00b545 2018-01-17 stsp
135 90bdb554 2019-04-11 stsp err = got_object_get_path(&path, id, repo);
136 d1cda826 2017-11-06 stsp if (err)
137 d1cda826 2017-11-06 stsp return err;
138 06340621 2021-12-31 thomas *fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
139 d5003b79 2018-04-22 stsp if (*fd == -1) {
140 e82b1d81 2019-07-27 stsp err = got_error_from_errno2("open", path);
141 6c00b545 2018-01-17 stsp goto done;
142 a1fd68d8 2018-01-12 stsp }
143 4558fcd4 2018-01-14 stsp done:
144 4558fcd4 2018-01-14 stsp free(path);
145 2090a03d 2018-09-09 stsp return err;
146 2090a03d 2018-09-09 stsp }
147 2090a03d 2018-09-09 stsp
148 2090a03d 2018-09-09 stsp static const struct got_error *
149 a158c901 2018-12-23 stsp request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
150 a158c901 2018-12-23 stsp struct got_object_id *id)
151 a158c901 2018-12-23 stsp {
152 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
153 a158c901 2018-12-23 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
154 a158c901 2018-12-23 stsp
155 a158c901 2018-12-23 stsp err = got_privsep_send_packed_obj_req(ibuf, idx, id);
156 a158c901 2018-12-23 stsp if (err)
157 a158c901 2018-12-23 stsp return err;
158 a158c901 2018-12-23 stsp
159 a158c901 2018-12-23 stsp err = got_privsep_recv_obj(obj, ibuf);
160 a158c901 2018-12-23 stsp if (err)
161 a158c901 2018-12-23 stsp return err;
162 a158c901 2018-12-23 stsp
163 a158c901 2018-12-23 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
164 a158c901 2018-12-23 stsp
165 a158c901 2018-12-23 stsp return NULL;
166 b48e2ddb 2019-05-22 stsp }
167 b48e2ddb 2019-05-22 stsp
168 bc1f382f 2022-01-05 thomas /* Create temporary files used during delta application. */
169 59d1e4a0 2021-03-10 stsp static const struct got_error *
170 bc1f382f 2022-01-05 thomas pack_child_send_tempfiles(struct imsgbuf *ibuf, struct got_pack *pack)
171 59d1e4a0 2021-03-10 stsp {
172 bc1f382f 2022-01-05 thomas const struct got_error *err;
173 bc1f382f 2022-01-05 thomas int basefd, accumfd;
174 59d1e4a0 2021-03-10 stsp
175 bc1f382f 2022-01-05 thomas /*
176 bc1f382f 2022-01-05 thomas * For performance reasons, the child will keep reusing the
177 bc1f382f 2022-01-05 thomas * same temporary files during every object request.
178 bc1f382f 2022-01-05 thomas * Opening and closing new files for every object request is
179 bc1f382f 2022-01-05 thomas * too expensive during operations such as 'gotadmin pack'.
180 bc1f382f 2022-01-05 thomas */
181 bc1f382f 2022-01-05 thomas if (pack->child_has_tempfiles)
182 bc1f382f 2022-01-05 thomas return NULL;
183 bc1f382f 2022-01-05 thomas
184 59d1e4a0 2021-03-10 stsp basefd = got_opentempfd();
185 59d1e4a0 2021-03-10 stsp if (basefd == -1)
186 59d1e4a0 2021-03-10 stsp return got_error_from_errno("got_opentempfd");
187 59d1e4a0 2021-03-10 stsp
188 bc1f382f 2022-01-05 thomas err = got_privsep_send_tmpfd(ibuf, basefd);
189 bc1f382f 2022-01-05 thomas if (err)
190 bc1f382f 2022-01-05 thomas return err;
191 bc1f382f 2022-01-05 thomas
192 59d1e4a0 2021-03-10 stsp accumfd = got_opentempfd();
193 bc1f382f 2022-01-05 thomas if (accumfd == -1)
194 59d1e4a0 2021-03-10 stsp return got_error_from_errno("got_opentempfd");
195 59d1e4a0 2021-03-10 stsp
196 bc1f382f 2022-01-05 thomas err = got_privsep_send_tmpfd(ibuf, accumfd);
197 bc1f382f 2022-01-05 thomas if (err)
198 59d1e4a0 2021-03-10 stsp return err;
199 59d1e4a0 2021-03-10 stsp
200 bc1f382f 2022-01-05 thomas pack->child_has_tempfiles = 1;
201 bc1f382f 2022-01-05 thomas return NULL;
202 bc1f382f 2022-01-05 thomas }
203 bc1f382f 2022-01-05 thomas
204 bc1f382f 2022-01-05 thomas static const struct got_error *
205 bc1f382f 2022-01-05 thomas request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
206 bc1f382f 2022-01-05 thomas int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
207 bc1f382f 2022-01-05 thomas {
208 bc1f382f 2022-01-05 thomas const struct got_error *err = NULL;
209 bc1f382f 2022-01-05 thomas struct imsgbuf *ibuf = pack->privsep_child->ibuf;
210 bc1f382f 2022-01-05 thomas int outfd_child;
211 bc1f382f 2022-01-05 thomas
212 bc1f382f 2022-01-05 thomas err = pack_child_send_tempfiles(ibuf, pack);
213 bc1f382f 2022-01-05 thomas if (err)
214 bc1f382f 2022-01-05 thomas return err;
215 bc1f382f 2022-01-05 thomas
216 bc1f382f 2022-01-05 thomas outfd_child = dup(outfd);
217 bc1f382f 2022-01-05 thomas if (outfd_child == -1)
218 bc1f382f 2022-01-05 thomas return got_error_from_errno("dup");
219 bc1f382f 2022-01-05 thomas
220 59d1e4a0 2021-03-10 stsp err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
221 59d1e4a0 2021-03-10 stsp if (err) {
222 59d1e4a0 2021-03-10 stsp close(outfd_child);
223 59d1e4a0 2021-03-10 stsp return err;
224 59d1e4a0 2021-03-10 stsp }
225 59d1e4a0 2021-03-10 stsp
226 59d1e4a0 2021-03-10 stsp err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
227 59d1e4a0 2021-03-10 stsp if (err)
228 59d1e4a0 2021-03-10 stsp return err;
229 59d1e4a0 2021-03-10 stsp
230 59d1e4a0 2021-03-10 stsp err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
231 59d1e4a0 2021-03-10 stsp if (err)
232 59d1e4a0 2021-03-10 stsp return err;
233 59d1e4a0 2021-03-10 stsp
234 59d1e4a0 2021-03-10 stsp return NULL;
235 59d1e4a0 2021-03-10 stsp }
236 59d1e4a0 2021-03-10 stsp
237 da506691 2019-05-22 stsp static void
238 b48e2ddb 2019-05-22 stsp set_max_datasize(void)
239 b48e2ddb 2019-05-22 stsp {
240 b48e2ddb 2019-05-22 stsp struct rlimit rl;
241 b48e2ddb 2019-05-22 stsp
242 b48e2ddb 2019-05-22 stsp if (getrlimit(RLIMIT_DATA, &rl) != 0)
243 b48e2ddb 2019-05-22 stsp return;
244 b48e2ddb 2019-05-22 stsp
245 b48e2ddb 2019-05-22 stsp rl.rlim_cur = rl.rlim_max;
246 b48e2ddb 2019-05-22 stsp setrlimit(RLIMIT_DATA, &rl);
247 a158c901 2018-12-23 stsp }
248 a158c901 2018-12-23 stsp
249 a158c901 2018-12-23 stsp static const struct got_error *
250 711fb6e8 2018-12-23 stsp start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
251 a158c901 2018-12-23 stsp {
252 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
253 a158c901 2018-12-23 stsp int imsg_fds[2];
254 a158c901 2018-12-23 stsp pid_t pid;
255 a158c901 2018-12-23 stsp struct imsgbuf *ibuf;
256 a158c901 2018-12-23 stsp
257 a158c901 2018-12-23 stsp ibuf = calloc(1, sizeof(*ibuf));
258 a158c901 2018-12-23 stsp if (ibuf == NULL)
259 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
260 a158c901 2018-12-23 stsp
261 a158c901 2018-12-23 stsp pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
262 a158c901 2018-12-23 stsp if (pack->privsep_child == NULL) {
263 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
264 a158c901 2018-12-23 stsp free(ibuf);
265 a158c901 2018-12-23 stsp return err;
266 a158c901 2018-12-23 stsp }
267 bc1f382f 2022-01-05 thomas pack->child_has_tempfiles = 0;
268 f9c2e8e5 2022-02-13 thomas pack->child_has_delta_outfd = 0;
269 a158c901 2018-12-23 stsp
270 a158c901 2018-12-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
271 638f9024 2019-05-13 stsp err = got_error_from_errno("socketpair");
272 a158c901 2018-12-23 stsp goto done;
273 a158c901 2018-12-23 stsp }
274 a158c901 2018-12-23 stsp
275 a158c901 2018-12-23 stsp pid = fork();
276 a158c901 2018-12-23 stsp if (pid == -1) {
277 638f9024 2019-05-13 stsp err = got_error_from_errno("fork");
278 a158c901 2018-12-23 stsp goto done;
279 a158c901 2018-12-23 stsp } else if (pid == 0) {
280 b48e2ddb 2019-05-22 stsp set_max_datasize();
281 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
282 a158c901 2018-12-23 stsp pack->path_packfile);
283 a158c901 2018-12-23 stsp /* not reached */
284 a158c901 2018-12-23 stsp }
285 a158c901 2018-12-23 stsp
286 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1)
287 638f9024 2019-05-13 stsp return got_error_from_errno("close");
288 a158c901 2018-12-23 stsp pack->privsep_child->imsg_fd = imsg_fds[0];
289 a158c901 2018-12-23 stsp pack->privsep_child->pid = pid;
290 a158c901 2018-12-23 stsp imsg_init(ibuf, imsg_fds[0]);
291 a158c901 2018-12-23 stsp pack->privsep_child->ibuf = ibuf;
292 a158c901 2018-12-23 stsp
293 a158c901 2018-12-23 stsp err = got_privsep_init_pack_child(ibuf, pack, packidx);
294 a158c901 2018-12-23 stsp if (err) {
295 a158c901 2018-12-23 stsp const struct got_error *child_err;
296 a158c901 2018-12-23 stsp err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
297 a158c901 2018-12-23 stsp child_err = got_privsep_wait_for_child(
298 a158c901 2018-12-23 stsp pack->privsep_child->pid);
299 a158c901 2018-12-23 stsp if (child_err && err == NULL)
300 a158c901 2018-12-23 stsp err = child_err;
301 a158c901 2018-12-23 stsp }
302 a158c901 2018-12-23 stsp done:
303 a158c901 2018-12-23 stsp if (err) {
304 a158c901 2018-12-23 stsp free(ibuf);
305 a158c901 2018-12-23 stsp free(pack->privsep_child);
306 a158c901 2018-12-23 stsp pack->privsep_child = NULL;
307 711fb6e8 2018-12-23 stsp }
308 a158c901 2018-12-23 stsp return err;
309 a158c901 2018-12-23 stsp }
310 a158c901 2018-12-23 stsp
311 711fb6e8 2018-12-23 stsp static const struct got_error *
312 711fb6e8 2018-12-23 stsp read_packed_object_privsep(struct got_object **obj,
313 711fb6e8 2018-12-23 stsp struct got_repository *repo, struct got_pack *pack,
314 711fb6e8 2018-12-23 stsp struct got_packidx *packidx, int idx, struct got_object_id *id)
315 711fb6e8 2018-12-23 stsp {
316 711fb6e8 2018-12-23 stsp const struct got_error *err = NULL;
317 a158c901 2018-12-23 stsp
318 59d1e4a0 2021-03-10 stsp if (pack->privsep_child == NULL) {
319 59d1e4a0 2021-03-10 stsp err = start_pack_privsep_child(pack, packidx);
320 59d1e4a0 2021-03-10 stsp if (err)
321 59d1e4a0 2021-03-10 stsp return err;
322 59d1e4a0 2021-03-10 stsp }
323 711fb6e8 2018-12-23 stsp
324 711fb6e8 2018-12-23 stsp return request_packed_object(obj, pack, idx, id);
325 711fb6e8 2018-12-23 stsp }
326 711fb6e8 2018-12-23 stsp
327 59d1e4a0 2021-03-10 stsp static const struct got_error *
328 59d1e4a0 2021-03-10 stsp read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
329 59d1e4a0 2021-03-10 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
330 59d1e4a0 2021-03-10 stsp struct got_object_id *id)
331 59d1e4a0 2021-03-10 stsp {
332 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
333 711fb6e8 2018-12-23 stsp
334 59d1e4a0 2021-03-10 stsp if (pack->privsep_child == NULL) {
335 59d1e4a0 2021-03-10 stsp err = start_pack_privsep_child(pack, packidx);
336 59d1e4a0 2021-03-10 stsp if (err)
337 59d1e4a0 2021-03-10 stsp return err;
338 59d1e4a0 2021-03-10 stsp }
339 59d1e4a0 2021-03-10 stsp
340 59d1e4a0 2021-03-10 stsp return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
341 59d1e4a0 2021-03-10 stsp idx, id);
342 59d1e4a0 2021-03-10 stsp }
343 59d1e4a0 2021-03-10 stsp
344 b3d68e7f 2021-07-03 stsp const struct got_error *
345 b3d68e7f 2021-07-03 stsp got_object_open_packed(struct got_object **obj, struct got_object_id *id,
346 2090a03d 2018-09-09 stsp struct got_repository *repo)
347 2090a03d 2018-09-09 stsp {
348 2090a03d 2018-09-09 stsp const struct got_error *err = NULL;
349 2090a03d 2018-09-09 stsp struct got_pack *pack = NULL;
350 2090a03d 2018-09-09 stsp struct got_packidx *packidx = NULL;
351 2090a03d 2018-09-09 stsp int idx;
352 2090a03d 2018-09-09 stsp char *path_packfile;
353 2090a03d 2018-09-09 stsp
354 2090a03d 2018-09-09 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
355 2090a03d 2018-09-09 stsp if (err)
356 2090a03d 2018-09-09 stsp return err;
357 2090a03d 2018-09-09 stsp
358 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
359 aea75d87 2021-07-06 stsp packidx->path_packidx);
360 2090a03d 2018-09-09 stsp if (err)
361 2090a03d 2018-09-09 stsp return err;
362 2090a03d 2018-09-09 stsp
363 2090a03d 2018-09-09 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
364 2090a03d 2018-09-09 stsp if (pack == NULL) {
365 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
366 2090a03d 2018-09-09 stsp if (err)
367 2090a03d 2018-09-09 stsp goto done;
368 2090a03d 2018-09-09 stsp }
369 2090a03d 2018-09-09 stsp
370 a158c901 2018-12-23 stsp err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
371 2090a03d 2018-09-09 stsp if (err)
372 2090a03d 2018-09-09 stsp goto done;
373 2090a03d 2018-09-09 stsp done:
374 2090a03d 2018-09-09 stsp free(path_packfile);
375 4558fcd4 2018-01-14 stsp return err;
376 f9c2e8e5 2022-02-13 thomas }
377 f9c2e8e5 2022-02-13 thomas
378 f9c2e8e5 2022-02-13 thomas const struct got_error *
379 f9c2e8e5 2022-02-13 thomas got_object_open_from_packfile(struct got_object **obj, struct got_object_id *id,
380 f9c2e8e5 2022-02-13 thomas struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
381 f9c2e8e5 2022-02-13 thomas struct got_repository *repo)
382 f9c2e8e5 2022-02-13 thomas {
383 f9c2e8e5 2022-02-13 thomas return read_packed_object_privsep(obj, repo, pack, packidx,
384 f9c2e8e5 2022-02-13 thomas obj_idx, id);
385 f9c2e8e5 2022-02-13 thomas }
386 f9c2e8e5 2022-02-13 thomas
387 f9c2e8e5 2022-02-13 thomas const struct got_error *
388 f9c2e8e5 2022-02-13 thomas got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
389 9249e7e3 2022-05-12 thomas off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
390 9249e7e3 2022-05-12 thomas off_t *delta_out_offset, struct got_object_id **base_id, int delta_cache_fd,
391 f9c2e8e5 2022-02-13 thomas struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
392 f9c2e8e5 2022-02-13 thomas struct got_repository *repo)
393 f9c2e8e5 2022-02-13 thomas {
394 f9c2e8e5 2022-02-13 thomas const struct got_error *err = NULL;
395 f9c2e8e5 2022-02-13 thomas struct got_pack *pack = NULL;
396 f9c2e8e5 2022-02-13 thomas char *path_packfile;
397 f9c2e8e5 2022-02-13 thomas
398 f9c2e8e5 2022-02-13 thomas *base_size = 0;
399 f9c2e8e5 2022-02-13 thomas *result_size = 0;
400 f9c2e8e5 2022-02-13 thomas *delta_size = 0;
401 9249e7e3 2022-05-12 thomas *delta_compressed_size = 0;
402 f9c2e8e5 2022-02-13 thomas *delta_offset = 0;
403 f9c2e8e5 2022-02-13 thomas *delta_out_offset = 0;
404 f9c2e8e5 2022-02-13 thomas
405 f9c2e8e5 2022-02-13 thomas err = got_packidx_get_packfile_path(&path_packfile,
406 f9c2e8e5 2022-02-13 thomas packidx->path_packidx);
407 f9c2e8e5 2022-02-13 thomas if (err)
408 f9c2e8e5 2022-02-13 thomas return err;
409 f9c2e8e5 2022-02-13 thomas
410 f9c2e8e5 2022-02-13 thomas pack = got_repo_get_cached_pack(repo, path_packfile);
411 f9c2e8e5 2022-02-13 thomas if (pack == NULL) {
412 f9c2e8e5 2022-02-13 thomas err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
413 f9c2e8e5 2022-02-13 thomas if (err)
414 f9c2e8e5 2022-02-13 thomas return err;
415 f9c2e8e5 2022-02-13 thomas }
416 f9c2e8e5 2022-02-13 thomas
417 f9c2e8e5 2022-02-13 thomas if (pack->privsep_child == NULL) {
418 f9c2e8e5 2022-02-13 thomas err = start_pack_privsep_child(pack, packidx);
419 f9c2e8e5 2022-02-13 thomas if (err)
420 f9c2e8e5 2022-02-13 thomas return err;
421 f9c2e8e5 2022-02-13 thomas }
422 f9c2e8e5 2022-02-13 thomas
423 f9c2e8e5 2022-02-13 thomas if (!pack->child_has_delta_outfd) {
424 f9c2e8e5 2022-02-13 thomas int outfd_child;
425 f9c2e8e5 2022-02-13 thomas outfd_child = dup(delta_cache_fd);
426 f9c2e8e5 2022-02-13 thomas if (outfd_child == -1)
427 f9c2e8e5 2022-02-13 thomas return got_error_from_errno("dup");
428 f9c2e8e5 2022-02-13 thomas err = got_privsep_send_raw_delta_outfd(
429 f9c2e8e5 2022-02-13 thomas pack->privsep_child->ibuf, outfd_child);
430 f9c2e8e5 2022-02-13 thomas if (err)
431 f9c2e8e5 2022-02-13 thomas return err;
432 f9c2e8e5 2022-02-13 thomas pack->child_has_delta_outfd = 1;
433 f9c2e8e5 2022-02-13 thomas }
434 f9c2e8e5 2022-02-13 thomas
435 f9c2e8e5 2022-02-13 thomas err = got_privsep_send_raw_delta_req(pack->privsep_child->ibuf,
436 f9c2e8e5 2022-02-13 thomas obj_idx, id);
437 f9c2e8e5 2022-02-13 thomas if (err)
438 f9c2e8e5 2022-02-13 thomas return err;
439 f9c2e8e5 2022-02-13 thomas
440 f9c2e8e5 2022-02-13 thomas return got_privsep_recv_raw_delta(base_size, result_size, delta_size,
441 9249e7e3 2022-05-12 thomas delta_compressed_size, delta_offset, delta_out_offset, base_id,
442 9249e7e3 2022-05-12 thomas pack->privsep_child->ibuf);
443 9f2369b0 2018-12-24 stsp }
444 7d0d4920 2022-05-12 thomas
445 7d0d4920 2022-05-12 thomas /*
446 7d0d4920 2022-05-12 thomas * XXX This function does not really belong in object.c. It is only here
447 7d0d4920 2022-05-12 thomas * because it needs start_pack_privsep_child(); relevant code should
448 7d0d4920 2022-05-12 thomas * probably be moved to pack.c/pack_create.c.
449 7d0d4920 2022-05-12 thomas */
450 7d0d4920 2022-05-12 thomas const struct got_error *
451 7d0d4920 2022-05-12 thomas got_object_prepare_delta_reuse(struct got_pack **pack,
452 7d0d4920 2022-05-12 thomas struct got_packidx *packidx, int delta_outfd, struct got_repository *repo)
453 7d0d4920 2022-05-12 thomas {
454 7d0d4920 2022-05-12 thomas const struct got_error *err = NULL;
455 7d0d4920 2022-05-12 thomas char *path_packfile = NULL;
456 7d0d4920 2022-05-12 thomas
457 7d0d4920 2022-05-12 thomas err = got_packidx_get_packfile_path(&path_packfile,
458 7d0d4920 2022-05-12 thomas packidx->path_packidx);
459 7d0d4920 2022-05-12 thomas if (err)
460 7d0d4920 2022-05-12 thomas return err;
461 9f2369b0 2018-12-24 stsp
462 7d0d4920 2022-05-12 thomas *pack = got_repo_get_cached_pack(repo, path_packfile);
463 7d0d4920 2022-05-12 thomas if (*pack == NULL) {
464 7d0d4920 2022-05-12 thomas err = got_repo_cache_pack(pack, repo, path_packfile, packidx);
465 7d0d4920 2022-05-12 thomas if (err)
466 7d0d4920 2022-05-12 thomas goto done;
467 7d0d4920 2022-05-12 thomas }
468 7d0d4920 2022-05-12 thomas if ((*pack)->privsep_child == NULL) {
469 7d0d4920 2022-05-12 thomas err = start_pack_privsep_child(*pack, packidx);
470 7d0d4920 2022-05-12 thomas if (err)
471 7d0d4920 2022-05-12 thomas goto done;
472 7d0d4920 2022-05-12 thomas }
473 7d0d4920 2022-05-12 thomas
474 7d0d4920 2022-05-12 thomas if (!(*pack)->child_has_delta_outfd) {
475 7d0d4920 2022-05-12 thomas int outfd_child;
476 7d0d4920 2022-05-12 thomas outfd_child = dup(delta_outfd);
477 7d0d4920 2022-05-12 thomas if (outfd_child == -1) {
478 7d0d4920 2022-05-12 thomas err = got_error_from_errno("dup");
479 7d0d4920 2022-05-12 thomas goto done;
480 7d0d4920 2022-05-12 thomas }
481 7d0d4920 2022-05-12 thomas err = got_privsep_send_raw_delta_outfd(
482 7d0d4920 2022-05-12 thomas (*pack)->privsep_child->ibuf, outfd_child);
483 7d0d4920 2022-05-12 thomas if (err)
484 7d0d4920 2022-05-12 thomas goto done;
485 7d0d4920 2022-05-12 thomas (*pack)->child_has_delta_outfd = 1;
486 7d0d4920 2022-05-12 thomas }
487 7d0d4920 2022-05-12 thomas
488 7d0d4920 2022-05-12 thomas err = got_privsep_send_delta_reuse_req((*pack)->privsep_child->ibuf);
489 7d0d4920 2022-05-12 thomas done:
490 7d0d4920 2022-05-12 thomas free(path_packfile);
491 7d0d4920 2022-05-12 thomas return err;
492 7d0d4920 2022-05-12 thomas }
493 7d0d4920 2022-05-12 thomas
494 9f2369b0 2018-12-24 stsp static const struct got_error *
495 d5c81d44 2021-07-08 stsp request_object(struct got_object **obj, struct got_object_id *id,
496 d5c81d44 2021-07-08 stsp struct got_repository *repo, int fd)
497 9f2369b0 2018-12-24 stsp {
498 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
499 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
500 9f2369b0 2018-12-24 stsp
501 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
502 9f2369b0 2018-12-24 stsp
503 d5c81d44 2021-07-08 stsp err = got_privsep_send_obj_req(ibuf, fd, id);
504 9f2369b0 2018-12-24 stsp if (err)
505 9f2369b0 2018-12-24 stsp return err;
506 9f2369b0 2018-12-24 stsp
507 9f2369b0 2018-12-24 stsp return got_privsep_recv_obj(obj, ibuf);
508 9f2369b0 2018-12-24 stsp }
509 9f2369b0 2018-12-24 stsp
510 9f2369b0 2018-12-24 stsp static const struct got_error *
511 59d1e4a0 2021-03-10 stsp request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
512 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo, int infd)
513 9f2369b0 2018-12-24 stsp {
514 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
515 59d1e4a0 2021-03-10 stsp struct imsgbuf *ibuf;
516 59d1e4a0 2021-03-10 stsp int outfd_child;
517 59d1e4a0 2021-03-10 stsp
518 59d1e4a0 2021-03-10 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
519 59d1e4a0 2021-03-10 stsp
520 59d1e4a0 2021-03-10 stsp outfd_child = dup(outfd);
521 59d1e4a0 2021-03-10 stsp if (outfd_child == -1)
522 59d1e4a0 2021-03-10 stsp return got_error_from_errno("dup");
523 59d1e4a0 2021-03-10 stsp
524 d5c81d44 2021-07-08 stsp err = got_privsep_send_raw_obj_req(ibuf, infd, id);
525 59d1e4a0 2021-03-10 stsp if (err)
526 59d1e4a0 2021-03-10 stsp return err;
527 59d1e4a0 2021-03-10 stsp
528 59d1e4a0 2021-03-10 stsp err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
529 59d1e4a0 2021-03-10 stsp if (err)
530 59d1e4a0 2021-03-10 stsp return err;
531 59d1e4a0 2021-03-10 stsp
532 59d1e4a0 2021-03-10 stsp return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
533 59d1e4a0 2021-03-10 stsp }
534 59d1e4a0 2021-03-10 stsp
535 59d1e4a0 2021-03-10 stsp static const struct got_error *
536 59d1e4a0 2021-03-10 stsp start_read_object_child(struct got_repository *repo)
537 59d1e4a0 2021-03-10 stsp {
538 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
539 9f2369b0 2018-12-24 stsp int imsg_fds[2];
540 9f2369b0 2018-12-24 stsp pid_t pid;
541 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
542 9f2369b0 2018-12-24 stsp
543 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
544 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
545 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
546 9f2369b0 2018-12-24 stsp
547 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
548 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
549 ddc7b220 2019-09-08 stsp free(ibuf);
550 ddc7b220 2019-09-08 stsp return err;
551 ddc7b220 2019-09-08 stsp }
552 9f2369b0 2018-12-24 stsp
553 9f2369b0 2018-12-24 stsp pid = fork();
554 ddc7b220 2019-09-08 stsp if (pid == -1) {
555 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
556 ddc7b220 2019-09-08 stsp free(ibuf);
557 ddc7b220 2019-09-08 stsp return err;
558 ddc7b220 2019-09-08 stsp }
559 9f2369b0 2018-12-24 stsp else if (pid == 0) {
560 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
561 9f2369b0 2018-12-24 stsp repo->path);
562 9f2369b0 2018-12-24 stsp /* not reached */
563 9f2369b0 2018-12-24 stsp }
564 9f2369b0 2018-12-24 stsp
565 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
566 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
567 ddc7b220 2019-09-08 stsp free(ibuf);
568 ddc7b220 2019-09-08 stsp return err;
569 ddc7b220 2019-09-08 stsp }
570 59d1e4a0 2021-03-10 stsp
571 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
572 9f2369b0 2018-12-24 stsp imsg_fds[0];
573 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
574 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
575 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
576 9f2369b0 2018-12-24 stsp
577 59d1e4a0 2021-03-10 stsp return NULL;
578 59d1e4a0 2021-03-10 stsp }
579 59d1e4a0 2021-03-10 stsp
580 b3d68e7f 2021-07-03 stsp const struct got_error *
581 b3d68e7f 2021-07-03 stsp got_object_read_header_privsep(struct got_object **obj,
582 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo, int obj_fd)
583 59d1e4a0 2021-03-10 stsp {
584 59d1e4a0 2021-03-10 stsp const struct got_error *err;
585 59d1e4a0 2021-03-10 stsp
586 59d1e4a0 2021-03-10 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
587 d5c81d44 2021-07-08 stsp return request_object(obj, id, repo, obj_fd);
588 59d1e4a0 2021-03-10 stsp
589 59d1e4a0 2021-03-10 stsp err = start_read_object_child(repo);
590 7495ec13 2021-04-04 stsp if (err) {
591 7495ec13 2021-04-04 stsp close(obj_fd);
592 59d1e4a0 2021-03-10 stsp return err;
593 7495ec13 2021-04-04 stsp }
594 59d1e4a0 2021-03-10 stsp
595 d5c81d44 2021-07-08 stsp return request_object(obj, id, repo, obj_fd);
596 4558fcd4 2018-01-14 stsp }
597 a1fd68d8 2018-01-12 stsp
598 59d1e4a0 2021-03-10 stsp static const struct got_error *
599 59d1e4a0 2021-03-10 stsp read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
600 d5c81d44 2021-07-08 stsp int outfd, struct got_object_id *id, struct got_repository *repo,
601 d5c81d44 2021-07-08 stsp int obj_fd)
602 59d1e4a0 2021-03-10 stsp {
603 59d1e4a0 2021-03-10 stsp const struct got_error *err;
604 59d1e4a0 2021-03-10 stsp
605 59d1e4a0 2021-03-10 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
606 d5c81d44 2021-07-08 stsp return request_raw_object(outbuf, size, hdrlen, outfd, id,
607 d5c81d44 2021-07-08 stsp repo, obj_fd);
608 59d1e4a0 2021-03-10 stsp
609 59d1e4a0 2021-03-10 stsp err = start_read_object_child(repo);
610 59d1e4a0 2021-03-10 stsp if (err)
611 59d1e4a0 2021-03-10 stsp return err;
612 59d1e4a0 2021-03-10 stsp
613 d5c81d44 2021-07-08 stsp return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
614 d5c81d44 2021-07-08 stsp obj_fd);
615 59d1e4a0 2021-03-10 stsp }
616 9f2369b0 2018-12-24 stsp
617 4558fcd4 2018-01-14 stsp const struct got_error *
618 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
619 4558fcd4 2018-01-14 stsp struct got_object_id *id)
620 4558fcd4 2018-01-14 stsp {
621 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
622 2178c42e 2018-04-22 stsp int fd;
623 7bb0daa1 2018-06-21 stsp
624 7bb0daa1 2018-06-21 stsp *obj = got_repo_get_cached_object(repo, id);
625 7bb0daa1 2018-06-21 stsp if (*obj != NULL) {
626 7bb0daa1 2018-06-21 stsp (*obj)->refcnt++;
627 7bb0daa1 2018-06-21 stsp return NULL;
628 7bb0daa1 2018-06-21 stsp }
629 4558fcd4 2018-01-14 stsp
630 b3d68e7f 2021-07-03 stsp err = got_object_open_packed(obj, id, repo);
631 e82b1d81 2019-07-27 stsp if (err && err->code != GOT_ERR_NO_OBJ)
632 e82b1d81 2019-07-27 stsp return err;
633 e82b1d81 2019-07-27 stsp if (*obj) {
634 e82b1d81 2019-07-27 stsp (*obj)->refcnt++;
635 e82b1d81 2019-07-27 stsp return got_repo_cache_object(repo, id, *obj);
636 e82b1d81 2019-07-27 stsp }
637 e82b1d81 2019-07-27 stsp
638 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
639 762d73f4 2021-04-10 stsp if (err) {
640 762d73f4 2021-04-10 stsp if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
641 762d73f4 2021-04-10 stsp err = got_error_no_obj(id);
642 762d73f4 2021-04-10 stsp return err;
643 762d73f4 2021-04-10 stsp }
644 762d73f4 2021-04-10 stsp
645 d5c81d44 2021-07-08 stsp err = got_object_read_header_privsep(obj, id, repo, fd);
646 4558fcd4 2018-01-14 stsp if (err)
647 4558fcd4 2018-01-14 stsp return err;
648 4558fcd4 2018-01-14 stsp
649 762d73f4 2021-04-10 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
650 7bb0daa1 2018-06-21 stsp
651 59790a32 2018-09-15 stsp (*obj)->refcnt++;
652 762d73f4 2021-04-10 stsp return got_repo_cache_object(repo, id, *obj);
653 59d1e4a0 2021-03-10 stsp }
654 6c00b545 2018-01-17 stsp
655 8ab9215c 2021-10-15 thomas /* *outfd must be initialized to -1 by caller */
656 59d1e4a0 2021-03-10 stsp const struct got_error *
657 8ab9215c 2021-10-15 thomas got_object_raw_open(struct got_raw_object **obj, int *outfd,
658 f8bb1d3e 2021-10-17 thomas struct got_repository *repo, struct got_object_id *id)
659 59d1e4a0 2021-03-10 stsp {
660 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
661 59d1e4a0 2021-03-10 stsp struct got_packidx *packidx = NULL;
662 59d1e4a0 2021-03-10 stsp int idx;
663 59d1e4a0 2021-03-10 stsp uint8_t *outbuf = NULL;
664 59d1e4a0 2021-03-10 stsp off_t size = 0;
665 59d1e4a0 2021-03-10 stsp size_t hdrlen = 0;
666 59d1e4a0 2021-03-10 stsp char *path_packfile = NULL;
667 59d1e4a0 2021-03-10 stsp
668 8ab9215c 2021-10-15 thomas *obj = got_repo_get_cached_raw_object(repo, id);
669 8ab9215c 2021-10-15 thomas if (*obj != NULL) {
670 8ab9215c 2021-10-15 thomas (*obj)->refcnt++;
671 8ab9215c 2021-10-15 thomas return NULL;
672 8ab9215c 2021-10-15 thomas }
673 59d1e4a0 2021-03-10 stsp
674 8ab9215c 2021-10-15 thomas if (*outfd == -1) {
675 8ab9215c 2021-10-15 thomas *outfd = got_opentempfd();
676 8ab9215c 2021-10-15 thomas if (*outfd == -1)
677 8ab9215c 2021-10-15 thomas return got_error_from_errno("got_opentempfd");
678 8ab9215c 2021-10-15 thomas }
679 8ab9215c 2021-10-15 thomas
680 59d1e4a0 2021-03-10 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
681 59d1e4a0 2021-03-10 stsp if (err == NULL) {
682 59d1e4a0 2021-03-10 stsp struct got_pack *pack = NULL;
683 59d1e4a0 2021-03-10 stsp
684 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
685 aea75d87 2021-07-06 stsp packidx->path_packidx);
686 59d1e4a0 2021-03-10 stsp if (err)
687 59d1e4a0 2021-03-10 stsp goto done;
688 59d1e4a0 2021-03-10 stsp
689 59d1e4a0 2021-03-10 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
690 59d1e4a0 2021-03-10 stsp if (pack == NULL) {
691 59d1e4a0 2021-03-10 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
692 59d1e4a0 2021-03-10 stsp packidx);
693 59d1e4a0 2021-03-10 stsp if (err)
694 59d1e4a0 2021-03-10 stsp goto done;
695 59d1e4a0 2021-03-10 stsp }
696 59d1e4a0 2021-03-10 stsp err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
697 8ab9215c 2021-10-15 thomas *outfd, pack, packidx, idx, id);
698 dd3af45a 2021-10-15 thomas if (err)
699 dd3af45a 2021-10-15 thomas goto done;
700 59d1e4a0 2021-03-10 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
701 59d1e4a0 2021-03-10 stsp int fd;
702 59d1e4a0 2021-03-10 stsp
703 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
704 59d1e4a0 2021-03-10 stsp if (err)
705 59d1e4a0 2021-03-10 stsp goto done;
706 8ab9215c 2021-10-15 thomas err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
707 d5c81d44 2021-07-08 stsp id, repo, fd);
708 dd3af45a 2021-10-15 thomas if (err)
709 dd3af45a 2021-10-15 thomas goto done;
710 59d1e4a0 2021-03-10 stsp }
711 59d1e4a0 2021-03-10 stsp
712 59d1e4a0 2021-03-10 stsp *obj = calloc(1, sizeof(**obj));
713 59d1e4a0 2021-03-10 stsp if (*obj == NULL) {
714 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("calloc");
715 59d1e4a0 2021-03-10 stsp goto done;
716 59d1e4a0 2021-03-10 stsp }
717 2b0ae357 2022-01-10 thomas (*obj)->fd = -1;
718 59d1e4a0 2021-03-10 stsp
719 59d1e4a0 2021-03-10 stsp if (outbuf) {
720 59d1e4a0 2021-03-10 stsp (*obj)->data = outbuf;
721 59d1e4a0 2021-03-10 stsp } else {
722 59d1e4a0 2021-03-10 stsp struct stat sb;
723 8ab9215c 2021-10-15 thomas if (fstat(*outfd, &sb) == -1) {
724 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fstat");
725 59d1e4a0 2021-03-10 stsp goto done;
726 59d1e4a0 2021-03-10 stsp }
727 59d1e4a0 2021-03-10 stsp
728 a8591711 2021-06-18 stsp if (sb.st_size != hdrlen + size) {
729 59d1e4a0 2021-03-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
730 59d1e4a0 2021-03-10 stsp goto done;
731 59d1e4a0 2021-03-10 stsp }
732 2b0ae357 2022-01-10 thomas #ifndef GOT_PACK_NO_MMAP
733 2b0ae357 2022-01-10 thomas if (hdrlen + size > 0) {
734 2b0ae357 2022-01-10 thomas (*obj)->data = mmap(NULL, hdrlen + size, PROT_READ,
735 2b0ae357 2022-01-10 thomas MAP_PRIVATE, *outfd, 0);
736 2b0ae357 2022-01-10 thomas if ((*obj)->data == MAP_FAILED) {
737 2b0ae357 2022-01-10 thomas if (errno != ENOMEM) {
738 2b0ae357 2022-01-10 thomas err = got_error_from_errno("mmap");
739 2b0ae357 2022-01-10 thomas goto done;
740 2b0ae357 2022-01-10 thomas }
741 2b0ae357 2022-01-10 thomas (*obj)->data = NULL;
742 2b0ae357 2022-01-10 thomas } else {
743 2b0ae357 2022-01-10 thomas (*obj)->fd = *outfd;
744 2b0ae357 2022-01-10 thomas *outfd = -1;
745 2b0ae357 2022-01-10 thomas }
746 59d1e4a0 2021-03-10 stsp }
747 2b0ae357 2022-01-10 thomas #endif
748 2b0ae357 2022-01-10 thomas if (*outfd != -1) {
749 2b0ae357 2022-01-10 thomas (*obj)->f = fdopen(*outfd, "r");
750 2b0ae357 2022-01-10 thomas if ((*obj)->f == NULL) {
751 2b0ae357 2022-01-10 thomas err = got_error_from_errno("fdopen");
752 2b0ae357 2022-01-10 thomas goto done;
753 2b0ae357 2022-01-10 thomas }
754 2b0ae357 2022-01-10 thomas *outfd = -1;
755 2b0ae357 2022-01-10 thomas }
756 59d1e4a0 2021-03-10 stsp }
757 59d1e4a0 2021-03-10 stsp (*obj)->hdrlen = hdrlen;
758 59d1e4a0 2021-03-10 stsp (*obj)->size = size;
759 8ab9215c 2021-10-15 thomas err = got_repo_cache_raw_object(repo, id, *obj);
760 59d1e4a0 2021-03-10 stsp done:
761 59d1e4a0 2021-03-10 stsp free(path_packfile);
762 59d1e4a0 2021-03-10 stsp if (err) {
763 59d1e4a0 2021-03-10 stsp if (*obj) {
764 59d1e4a0 2021-03-10 stsp got_object_raw_close(*obj);
765 59d1e4a0 2021-03-10 stsp *obj = NULL;
766 59d1e4a0 2021-03-10 stsp }
767 59d1e4a0 2021-03-10 stsp free(outbuf);
768 8ab9215c 2021-10-15 thomas } else
769 8ab9215c 2021-10-15 thomas (*obj)->refcnt++;
770 59d1e4a0 2021-03-10 stsp return err;
771 ab9a70b2 2017-11-06 stsp }
772 ab9a70b2 2017-11-06 stsp
773 59d1e4a0 2021-03-10 stsp const struct got_error *
774 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
775 6dfa2fd3 2018-02-12 stsp const char *id_str)
776 6dfa2fd3 2018-02-12 stsp {
777 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
778 6dfa2fd3 2018-02-12 stsp
779 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
780 6dd1ece6 2019-11-10 stsp return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
781 6dfa2fd3 2018-02-12 stsp
782 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
783 15a94983 2018-12-23 stsp }
784 15a94983 2018-12-23 stsp
785 15a94983 2018-12-23 stsp const struct got_error *
786 15a94983 2018-12-23 stsp got_object_resolve_id_str(struct got_object_id **id,
787 15a94983 2018-12-23 stsp struct got_repository *repo, const char *id_str)
788 15a94983 2018-12-23 stsp {
789 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
790 15a94983 2018-12-23 stsp struct got_object *obj;
791 15a94983 2018-12-23 stsp
792 15a94983 2018-12-23 stsp err = got_object_open_by_id_str(&obj, repo, id_str);
793 15a94983 2018-12-23 stsp if (err)
794 15a94983 2018-12-23 stsp return err;
795 15a94983 2018-12-23 stsp
796 15a94983 2018-12-23 stsp *id = got_object_id_dup(got_object_get_id(obj));
797 15a94983 2018-12-23 stsp got_object_close(obj);
798 15a94983 2018-12-23 stsp if (*id == NULL)
799 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
800 15a94983 2018-12-23 stsp
801 15a94983 2018-12-23 stsp return NULL;
802 434025f3 2018-09-16 stsp }
803 434025f3 2018-09-16 stsp
804 434025f3 2018-09-16 stsp static const struct got_error *
805 a158c901 2018-12-23 stsp request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
806 a158c901 2018-12-23 stsp int pack_idx, struct got_object_id *id)
807 a158c901 2018-12-23 stsp {
808 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
809 a158c901 2018-12-23 stsp
810 a158c901 2018-12-23 stsp err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
811 a158c901 2018-12-23 stsp pack_idx);
812 a158c901 2018-12-23 stsp if (err)
813 a158c901 2018-12-23 stsp return err;
814 a158c901 2018-12-23 stsp
815 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
816 ca6e02ac 2020-01-07 stsp if (err)
817 ca6e02ac 2020-01-07 stsp return err;
818 ca6e02ac 2020-01-07 stsp
819 ca6e02ac 2020-01-07 stsp (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
820 ca6e02ac 2020-01-07 stsp return NULL;
821 a158c901 2018-12-23 stsp }
822 a158c901 2018-12-23 stsp
823 a158c901 2018-12-23 stsp static const struct got_error *
824 a158c901 2018-12-23 stsp read_packed_commit_privsep(struct got_commit_object **commit,
825 a158c901 2018-12-23 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
826 a158c901 2018-12-23 stsp struct got_object_id *id)
827 a158c901 2018-12-23 stsp {
828 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
829 a158c901 2018-12-23 stsp
830 a158c901 2018-12-23 stsp if (pack->privsep_child)
831 a158c901 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
832 a158c901 2018-12-23 stsp
833 711fb6e8 2018-12-23 stsp err = start_pack_privsep_child(pack, packidx);
834 711fb6e8 2018-12-23 stsp if (err)
835 a158c901 2018-12-23 stsp return err;
836 a158c901 2018-12-23 stsp
837 711fb6e8 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
838 9f2369b0 2018-12-24 stsp }
839 9f2369b0 2018-12-24 stsp
840 9f2369b0 2018-12-24 stsp static const struct got_error *
841 9f2369b0 2018-12-24 stsp request_commit(struct got_commit_object **commit, struct got_repository *repo,
842 d5c81d44 2021-07-08 stsp int fd, struct got_object_id *id)
843 9f2369b0 2018-12-24 stsp {
844 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
845 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
846 9f2369b0 2018-12-24 stsp
847 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
848 9f2369b0 2018-12-24 stsp
849 d5c81d44 2021-07-08 stsp err = got_privsep_send_commit_req(ibuf, fd, id, -1);
850 9f2369b0 2018-12-24 stsp if (err)
851 9f2369b0 2018-12-24 stsp return err;
852 9f2369b0 2018-12-24 stsp
853 9f2369b0 2018-12-24 stsp return got_privsep_recv_commit(commit, ibuf);
854 9f2369b0 2018-12-24 stsp }
855 9f2369b0 2018-12-24 stsp
856 9f2369b0 2018-12-24 stsp static const struct got_error *
857 9f2369b0 2018-12-24 stsp read_commit_privsep(struct got_commit_object **commit, int obj_fd,
858 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo)
859 9f2369b0 2018-12-24 stsp {
860 ddc7b220 2019-09-08 stsp const struct got_error *err;
861 9f2369b0 2018-12-24 stsp int imsg_fds[2];
862 9f2369b0 2018-12-24 stsp pid_t pid;
863 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
864 9f2369b0 2018-12-24 stsp
865 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
866 d5c81d44 2021-07-08 stsp return request_commit(commit, repo, obj_fd, id);
867 9f2369b0 2018-12-24 stsp
868 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
869 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
870 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
871 9f2369b0 2018-12-24 stsp
872 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
873 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
874 ddc7b220 2019-09-08 stsp free(ibuf);
875 ddc7b220 2019-09-08 stsp return err;
876 ddc7b220 2019-09-08 stsp }
877 9f2369b0 2018-12-24 stsp
878 9f2369b0 2018-12-24 stsp pid = fork();
879 ddc7b220 2019-09-08 stsp if (pid == -1) {
880 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
881 ddc7b220 2019-09-08 stsp free(ibuf);
882 ddc7b220 2019-09-08 stsp return err;
883 ddc7b220 2019-09-08 stsp }
884 9f2369b0 2018-12-24 stsp else if (pid == 0) {
885 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
886 9f2369b0 2018-12-24 stsp repo->path);
887 9f2369b0 2018-12-24 stsp /* not reached */
888 9f2369b0 2018-12-24 stsp }
889 9f2369b0 2018-12-24 stsp
890 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
891 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
892 ddc7b220 2019-09-08 stsp free(ibuf);
893 ddc7b220 2019-09-08 stsp return err;
894 ddc7b220 2019-09-08 stsp }
895 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
896 9f2369b0 2018-12-24 stsp imsg_fds[0];
897 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
898 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
899 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
900 9f2369b0 2018-12-24 stsp
901 d5c81d44 2021-07-08 stsp return request_commit(commit, repo, obj_fd, id);
902 a158c901 2018-12-23 stsp }
903 a158c901 2018-12-23 stsp
904 9f2369b0 2018-12-24 stsp
905 a158c901 2018-12-23 stsp static const struct got_error *
906 434025f3 2018-09-16 stsp open_commit(struct got_commit_object **commit,
907 1785f84a 2018-12-23 stsp struct got_repository *repo, struct got_object_id *id, int check_cache)
908 434025f3 2018-09-16 stsp {
909 434025f3 2018-09-16 stsp const struct got_error *err = NULL;
910 1785f84a 2018-12-23 stsp struct got_packidx *packidx = NULL;
911 e82b1d81 2019-07-27 stsp int idx;
912 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
913 434025f3 2018-09-16 stsp
914 434025f3 2018-09-16 stsp if (check_cache) {
915 1785f84a 2018-12-23 stsp *commit = got_repo_get_cached_commit(repo, id);
916 434025f3 2018-09-16 stsp if (*commit != NULL) {
917 434025f3 2018-09-16 stsp (*commit)->refcnt++;
918 434025f3 2018-09-16 stsp return NULL;
919 434025f3 2018-09-16 stsp }
920 434025f3 2018-09-16 stsp } else
921 434025f3 2018-09-16 stsp *commit = NULL;
922 434025f3 2018-09-16 stsp
923 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
924 e82b1d81 2019-07-27 stsp if (err == NULL) {
925 1785f84a 2018-12-23 stsp struct got_pack *pack = NULL;
926 34f480ff 2019-07-27 stsp
927 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
928 aea75d87 2021-07-06 stsp packidx->path_packidx);
929 1785f84a 2018-12-23 stsp if (err)
930 1785f84a 2018-12-23 stsp return err;
931 1785f84a 2018-12-23 stsp
932 1785f84a 2018-12-23 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
933 434025f3 2018-09-16 stsp if (pack == NULL) {
934 1785f84a 2018-12-23 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
935 1785f84a 2018-12-23 stsp packidx);
936 434025f3 2018-09-16 stsp if (err)
937 8d2c5ea3 2019-08-13 stsp goto done;
938 434025f3 2018-09-16 stsp }
939 a158c901 2018-12-23 stsp err = read_packed_commit_privsep(commit, pack,
940 1785f84a 2018-12-23 stsp packidx, idx, id);
941 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
942 e82b1d81 2019-07-27 stsp int fd;
943 e82b1d81 2019-07-27 stsp
944 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
945 e82b1d81 2019-07-27 stsp if (err)
946 e82b1d81 2019-07-27 stsp return err;
947 d5c81d44 2021-07-08 stsp err = read_commit_privsep(commit, fd, id, repo);
948 e82b1d81 2019-07-27 stsp }
949 434025f3 2018-09-16 stsp
950 434025f3 2018-09-16 stsp if (err == NULL) {
951 434025f3 2018-09-16 stsp (*commit)->refcnt++;
952 1785f84a 2018-12-23 stsp err = got_repo_cache_commit(repo, id, *commit);
953 e32baab7 2018-11-05 stsp }
954 8d2c5ea3 2019-08-13 stsp done:
955 8d2c5ea3 2019-08-13 stsp free(path_packfile);
956 e32baab7 2018-11-05 stsp return err;
957 e32baab7 2018-11-05 stsp }
958 e32baab7 2018-11-05 stsp
959 e32baab7 2018-11-05 stsp const struct got_error *
960 e32baab7 2018-11-05 stsp got_object_open_as_commit(struct got_commit_object **commit,
961 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object_id *id)
962 e32baab7 2018-11-05 stsp {
963 e32baab7 2018-11-05 stsp *commit = got_repo_get_cached_commit(repo, id);
964 e32baab7 2018-11-05 stsp if (*commit != NULL) {
965 e32baab7 2018-11-05 stsp (*commit)->refcnt++;
966 e32baab7 2018-11-05 stsp return NULL;
967 e32baab7 2018-11-05 stsp }
968 e32baab7 2018-11-05 stsp
969 1785f84a 2018-12-23 stsp return open_commit(commit, repo, id, 0);
970 7762fe12 2018-11-05 stsp }
971 7762fe12 2018-11-05 stsp
972 e32baab7 2018-11-05 stsp const struct got_error *
973 e32baab7 2018-11-05 stsp got_object_commit_open(struct got_commit_object **commit,
974 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object *obj)
975 e32baab7 2018-11-05 stsp {
976 1785f84a 2018-12-23 stsp return open_commit(commit, repo, got_object_get_id(obj), 1);
977 434025f3 2018-09-16 stsp }
978 434025f3 2018-09-16 stsp
979 434025f3 2018-09-16 stsp const struct got_error *
980 dbc6a6b6 2018-07-12 stsp got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
981 dbc6a6b6 2018-07-12 stsp {
982 dbc6a6b6 2018-07-12 stsp *qid = calloc(1, sizeof(**qid));
983 dbc6a6b6 2018-07-12 stsp if (*qid == NULL)
984 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
985 dbc6a6b6 2018-07-12 stsp
986 ec242592 2022-04-22 thomas memcpy(&(*qid)->id, id, sizeof((*qid)->id));
987 9ca9aafb 2021-06-18 stsp return NULL;
988 9ca9aafb 2021-06-18 stsp }
989 9ca9aafb 2021-06-18 stsp
990 9ca9aafb 2021-06-18 stsp const struct got_error *
991 9ca9aafb 2021-06-18 stsp got_object_id_queue_copy(const struct got_object_id_queue *src,
992 9ca9aafb 2021-06-18 stsp struct got_object_id_queue *dest)
993 9ca9aafb 2021-06-18 stsp {
994 9ca9aafb 2021-06-18 stsp const struct got_error *err;
995 9ca9aafb 2021-06-18 stsp struct got_object_qid *qid;
996 9ca9aafb 2021-06-18 stsp
997 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, src, entry) {
998 9ca9aafb 2021-06-18 stsp struct got_object_qid *new;
999 9ca9aafb 2021-06-18 stsp /*
1000 9ca9aafb 2021-06-18 stsp * Deep-copy the object ID only. Let the caller deal
1001 9ca9aafb 2021-06-18 stsp * with setting up the new->data pointer if needed.
1002 9ca9aafb 2021-06-18 stsp */
1003 ec242592 2022-04-22 thomas err = got_object_qid_alloc(&new, &qid->id);
1004 9ca9aafb 2021-06-18 stsp if (err) {
1005 9ca9aafb 2021-06-18 stsp got_object_id_queue_free(dest);
1006 9ca9aafb 2021-06-18 stsp return err;
1007 9ca9aafb 2021-06-18 stsp }
1008 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(dest, new, entry);
1009 dbc6a6b6 2018-07-12 stsp }
1010 dbc6a6b6 2018-07-12 stsp
1011 dbc6a6b6 2018-07-12 stsp return NULL;
1012 a158c901 2018-12-23 stsp }
1013 a158c901 2018-12-23 stsp
1014 a158c901 2018-12-23 stsp static const struct got_error *
1015 13c729f7 2018-12-24 stsp request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
1016 13c729f7 2018-12-24 stsp int pack_idx, struct got_object_id *id)
1017 a158c901 2018-12-23 stsp {
1018 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
1019 a158c901 2018-12-23 stsp
1020 13c729f7 2018-12-24 stsp err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
1021 13c729f7 2018-12-24 stsp pack_idx);
1022 a158c901 2018-12-23 stsp if (err)
1023 a158c901 2018-12-23 stsp return err;
1024 a158c901 2018-12-23 stsp
1025 a158c901 2018-12-23 stsp return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
1026 7e212e3d 2018-09-09 stsp }
1027 7e212e3d 2018-09-09 stsp
1028 71eb0e7f 2018-09-16 stsp static const struct got_error *
1029 13c729f7 2018-12-24 stsp read_packed_tree_privsep(struct got_tree_object **tree,
1030 13c729f7 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1031 13c729f7 2018-12-24 stsp struct got_object_id *id)
1032 13c729f7 2018-12-24 stsp {
1033 13c729f7 2018-12-24 stsp const struct got_error *err = NULL;
1034 13c729f7 2018-12-24 stsp
1035 13c729f7 2018-12-24 stsp if (pack->privsep_child)
1036 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
1037 13c729f7 2018-12-24 stsp
1038 13c729f7 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
1039 13c729f7 2018-12-24 stsp if (err)
1040 13c729f7 2018-12-24 stsp return err;
1041 13c729f7 2018-12-24 stsp
1042 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
1043 13c729f7 2018-12-24 stsp }
1044 13c729f7 2018-12-24 stsp
1045 13c729f7 2018-12-24 stsp static const struct got_error *
1046 9f2369b0 2018-12-24 stsp request_tree(struct got_tree_object **tree, struct got_repository *repo,
1047 d5c81d44 2021-07-08 stsp int fd, struct got_object_id *id)
1048 9f2369b0 2018-12-24 stsp {
1049 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1050 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1051 9f2369b0 2018-12-24 stsp
1052 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
1053 9f2369b0 2018-12-24 stsp
1054 d5c81d44 2021-07-08 stsp err = got_privsep_send_tree_req(ibuf, fd, id, -1);
1055 9f2369b0 2018-12-24 stsp if (err)
1056 9f2369b0 2018-12-24 stsp return err;
1057 9f2369b0 2018-12-24 stsp
1058 9f2369b0 2018-12-24 stsp return got_privsep_recv_tree(tree, ibuf);
1059 9f2369b0 2018-12-24 stsp }
1060 9f2369b0 2018-12-24 stsp
1061 9f2369b0 2018-12-24 stsp const struct got_error *
1062 9f2369b0 2018-12-24 stsp read_tree_privsep(struct got_tree_object **tree, int obj_fd,
1063 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo)
1064 9f2369b0 2018-12-24 stsp {
1065 ddc7b220 2019-09-08 stsp const struct got_error *err;
1066 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1067 9f2369b0 2018-12-24 stsp pid_t pid;
1068 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1069 9f2369b0 2018-12-24 stsp
1070 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
1071 d5c81d44 2021-07-08 stsp return request_tree(tree, repo, obj_fd, id);
1072 9f2369b0 2018-12-24 stsp
1073 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1074 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1075 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1076 9f2369b0 2018-12-24 stsp
1077 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1078 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1079 ddc7b220 2019-09-08 stsp free(ibuf);
1080 ddc7b220 2019-09-08 stsp return err;
1081 ddc7b220 2019-09-08 stsp }
1082 9f2369b0 2018-12-24 stsp
1083 9f2369b0 2018-12-24 stsp pid = fork();
1084 ddc7b220 2019-09-08 stsp if (pid == -1) {
1085 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1086 ddc7b220 2019-09-08 stsp free(ibuf);
1087 ddc7b220 2019-09-08 stsp return err;
1088 ddc7b220 2019-09-08 stsp }
1089 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1090 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1091 9f2369b0 2018-12-24 stsp repo->path);
1092 9f2369b0 2018-12-24 stsp /* not reached */
1093 9f2369b0 2018-12-24 stsp }
1094 9f2369b0 2018-12-24 stsp
1095 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
1096 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1097 ddc7b220 2019-09-08 stsp free(ibuf);
1098 ddc7b220 2019-09-08 stsp return err;
1099 ddc7b220 2019-09-08 stsp }
1100 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1101 9f2369b0 2018-12-24 stsp imsg_fds[0];
1102 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1103 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1104 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1105 9f2369b0 2018-12-24 stsp
1106 9f2369b0 2018-12-24 stsp
1107 d5c81d44 2021-07-08 stsp return request_tree(tree, repo, obj_fd, id);
1108 9f2369b0 2018-12-24 stsp }
1109 9f2369b0 2018-12-24 stsp
1110 9f2369b0 2018-12-24 stsp static const struct got_error *
1111 13c729f7 2018-12-24 stsp open_tree(struct got_tree_object **tree, struct got_repository *repo,
1112 13c729f7 2018-12-24 stsp struct got_object_id *id, int check_cache)
1113 0ffeb3c2 2017-11-26 stsp {
1114 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
1115 13c729f7 2018-12-24 stsp struct got_packidx *packidx = NULL;
1116 e82b1d81 2019-07-27 stsp int idx;
1117 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1118 f6be5c30 2018-06-22 stsp
1119 71eb0e7f 2018-09-16 stsp if (check_cache) {
1120 13c729f7 2018-12-24 stsp *tree = got_repo_get_cached_tree(repo, id);
1121 71eb0e7f 2018-09-16 stsp if (*tree != NULL) {
1122 71eb0e7f 2018-09-16 stsp (*tree)->refcnt++;
1123 71eb0e7f 2018-09-16 stsp return NULL;
1124 71eb0e7f 2018-09-16 stsp }
1125 71eb0e7f 2018-09-16 stsp } else
1126 71eb0e7f 2018-09-16 stsp *tree = NULL;
1127 0ffeb3c2 2017-11-26 stsp
1128 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1129 e82b1d81 2019-07-27 stsp if (err == NULL) {
1130 13c729f7 2018-12-24 stsp struct got_pack *pack = NULL;
1131 0ffeb3c2 2017-11-26 stsp
1132 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
1133 aea75d87 2021-07-06 stsp packidx->path_packidx);
1134 13c729f7 2018-12-24 stsp if (err)
1135 13c729f7 2018-12-24 stsp return err;
1136 13c729f7 2018-12-24 stsp
1137 13c729f7 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1138 e7885405 2018-09-10 stsp if (pack == NULL) {
1139 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1140 e82b1d81 2019-07-27 stsp packidx);
1141 e7885405 2018-09-10 stsp if (err)
1142 8d2c5ea3 2019-08-13 stsp goto done;
1143 e7885405 2018-09-10 stsp }
1144 13c729f7 2018-12-24 stsp err = read_packed_tree_privsep(tree, pack,
1145 13c729f7 2018-12-24 stsp packidx, idx, id);
1146 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1147 e82b1d81 2019-07-27 stsp int fd;
1148 e82b1d81 2019-07-27 stsp
1149 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1150 e82b1d81 2019-07-27 stsp if (err)
1151 e82b1d81 2019-07-27 stsp return err;
1152 d5c81d44 2021-07-08 stsp err = read_tree_privsep(tree, fd, id, repo);
1153 e82b1d81 2019-07-27 stsp }
1154 f6be5c30 2018-06-22 stsp
1155 f6be5c30 2018-06-22 stsp if (err == NULL) {
1156 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
1157 13c729f7 2018-12-24 stsp err = got_repo_cache_tree(repo, id, *tree);
1158 f6be5c30 2018-06-22 stsp }
1159 8d2c5ea3 2019-08-13 stsp done:
1160 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1161 776d4d29 2018-06-17 stsp return err;
1162 776d4d29 2018-06-17 stsp }
1163 776d4d29 2018-06-17 stsp
1164 776d4d29 2018-06-17 stsp const struct got_error *
1165 776d4d29 2018-06-17 stsp got_object_open_as_tree(struct got_tree_object **tree,
1166 776d4d29 2018-06-17 stsp struct got_repository *repo, struct got_object_id *id)
1167 776d4d29 2018-06-17 stsp {
1168 e8eb494a 2018-09-16 stsp *tree = got_repo_get_cached_tree(repo, id);
1169 e8eb494a 2018-09-16 stsp if (*tree != NULL) {
1170 e8eb494a 2018-09-16 stsp (*tree)->refcnt++;
1171 e8eb494a 2018-09-16 stsp return NULL;
1172 e0ab43e7 2018-03-16 stsp }
1173 776d4d29 2018-06-17 stsp
1174 13c729f7 2018-12-24 stsp return open_tree(tree, repo, id, 0);
1175 57efb1af 2018-04-24 stsp }
1176 ff6b18f8 2018-04-24 stsp
1177 71eb0e7f 2018-09-16 stsp const struct got_error *
1178 71eb0e7f 2018-09-16 stsp got_object_tree_open(struct got_tree_object **tree,
1179 71eb0e7f 2018-09-16 stsp struct got_repository *repo, struct got_object *obj)
1180 71eb0e7f 2018-09-16 stsp {
1181 13c729f7 2018-12-24 stsp return open_tree(tree, repo, got_object_get_id(obj), 1);
1182 71eb0e7f 2018-09-16 stsp }
1183 71eb0e7f 2018-09-16 stsp
1184 56e0773d 2019-11-28 stsp int
1185 56e0773d 2019-11-28 stsp got_object_tree_get_nentries(struct got_tree_object *tree)
1186 883f0469 2018-06-23 stsp {
1187 56e0773d 2019-11-28 stsp return tree->nentries;
1188 56e0773d 2019-11-28 stsp }
1189 56e0773d 2019-11-28 stsp
1190 56e0773d 2019-11-28 stsp struct got_tree_entry *
1191 56e0773d 2019-11-28 stsp got_object_tree_get_first_entry(struct got_tree_object *tree)
1192 56e0773d 2019-11-28 stsp {
1193 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, 0);
1194 56e0773d 2019-11-28 stsp }
1195 56e0773d 2019-11-28 stsp
1196 56e0773d 2019-11-28 stsp struct got_tree_entry *
1197 56e0773d 2019-11-28 stsp got_object_tree_get_last_entry(struct got_tree_object *tree)
1198 56e0773d 2019-11-28 stsp {
1199 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, tree->nentries - 1);
1200 56e0773d 2019-11-28 stsp }
1201 56e0773d 2019-11-28 stsp
1202 56e0773d 2019-11-28 stsp struct got_tree_entry *
1203 56e0773d 2019-11-28 stsp got_object_tree_get_entry(struct got_tree_object *tree, int i)
1204 56e0773d 2019-11-28 stsp {
1205 56e0773d 2019-11-28 stsp if (i < 0 || i >= tree->nentries)
1206 56e0773d 2019-11-28 stsp return NULL;
1207 56e0773d 2019-11-28 stsp return &tree->entries[i];
1208 883f0469 2018-06-23 stsp }
1209 3840f4c9 2018-09-12 stsp
1210 56e0773d 2019-11-28 stsp mode_t
1211 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(struct got_tree_entry *te)
1212 56e0773d 2019-11-28 stsp {
1213 56e0773d 2019-11-28 stsp return te->mode;
1214 56e0773d 2019-11-28 stsp }
1215 56e0773d 2019-11-28 stsp
1216 56e0773d 2019-11-28 stsp const char *
1217 56e0773d 2019-11-28 stsp got_tree_entry_get_name(struct got_tree_entry *te)
1218 56e0773d 2019-11-28 stsp {
1219 56e0773d 2019-11-28 stsp return &te->name[0];
1220 56e0773d 2019-11-28 stsp }
1221 56e0773d 2019-11-28 stsp
1222 56e0773d 2019-11-28 stsp struct got_object_id *
1223 56e0773d 2019-11-28 stsp got_tree_entry_get_id(struct got_tree_entry *te)
1224 56e0773d 2019-11-28 stsp {
1225 56e0773d 2019-11-28 stsp return &te->id;
1226 0d6c6ee3 2020-05-20 stsp }
1227 0d6c6ee3 2020-05-20 stsp
1228 0d6c6ee3 2020-05-20 stsp const struct got_error *
1229 af57b12a 2020-07-23 stsp got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1230 0d6c6ee3 2020-05-20 stsp {
1231 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
1232 32596e16 2020-07-23 stsp size_t len, totlen, hdrlen, offset;
1233 aa092692 2020-07-23 stsp
1234 aa092692 2020-07-23 stsp *s = NULL;
1235 0d6c6ee3 2020-05-20 stsp
1236 659dc16e 2020-07-23 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1237 659dc16e 2020-07-23 stsp totlen = 0;
1238 32596e16 2020-07-23 stsp offset = 0;
1239 659dc16e 2020-07-23 stsp do {
1240 659dc16e 2020-07-23 stsp char *p;
1241 0d6c6ee3 2020-05-20 stsp
1242 659dc16e 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1243 659dc16e 2020-07-23 stsp if (err)
1244 af57b12a 2020-07-23 stsp return err;
1245 659dc16e 2020-07-23 stsp
1246 659dc16e 2020-07-23 stsp if (len == 0)
1247 659dc16e 2020-07-23 stsp break;
1248 659dc16e 2020-07-23 stsp
1249 659dc16e 2020-07-23 stsp totlen += len - hdrlen;
1250 af57b12a 2020-07-23 stsp p = realloc(*s, totlen + 1);
1251 659dc16e 2020-07-23 stsp if (p == NULL) {
1252 659dc16e 2020-07-23 stsp err = got_error_from_errno("realloc");
1253 af57b12a 2020-07-23 stsp free(*s);
1254 af57b12a 2020-07-23 stsp *s = NULL;
1255 af57b12a 2020-07-23 stsp return err;
1256 659dc16e 2020-07-23 stsp }
1257 af57b12a 2020-07-23 stsp *s = p;
1258 659dc16e 2020-07-23 stsp /* Skip blob object header first time around. */
1259 af57b12a 2020-07-23 stsp memcpy(*s + offset,
1260 f8f7c882 2020-07-23 stsp got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1261 659dc16e 2020-07-23 stsp hdrlen = 0;
1262 32596e16 2020-07-23 stsp offset = totlen;
1263 659dc16e 2020-07-23 stsp } while (len > 0);
1264 af57b12a 2020-07-23 stsp
1265 af57b12a 2020-07-23 stsp (*s)[totlen] = '\0';
1266 af57b12a 2020-07-23 stsp return NULL;
1267 af57b12a 2020-07-23 stsp }
1268 af57b12a 2020-07-23 stsp
1269 af57b12a 2020-07-23 stsp const struct got_error *
1270 af57b12a 2020-07-23 stsp got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1271 af57b12a 2020-07-23 stsp struct got_repository *repo)
1272 af57b12a 2020-07-23 stsp {
1273 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
1274 af57b12a 2020-07-23 stsp struct got_blob_object *blob = NULL;
1275 af57b12a 2020-07-23 stsp
1276 af57b12a 2020-07-23 stsp *link_target = NULL;
1277 af57b12a 2020-07-23 stsp
1278 af57b12a 2020-07-23 stsp if (!got_object_tree_entry_is_symlink(te))
1279 af57b12a 2020-07-23 stsp return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1280 af57b12a 2020-07-23 stsp
1281 af57b12a 2020-07-23 stsp err = got_object_open_as_blob(&blob, repo,
1282 af57b12a 2020-07-23 stsp got_tree_entry_get_id(te), PATH_MAX);
1283 af57b12a 2020-07-23 stsp if (err)
1284 af57b12a 2020-07-23 stsp return err;
1285 af57b12a 2020-07-23 stsp
1286 af57b12a 2020-07-23 stsp err = got_object_blob_read_to_str(link_target, blob);
1287 af57b12a 2020-07-23 stsp got_object_blob_close(blob);
1288 659dc16e 2020-07-23 stsp if (err) {
1289 659dc16e 2020-07-23 stsp free(*link_target);
1290 659dc16e 2020-07-23 stsp *link_target = NULL;
1291 659dc16e 2020-07-23 stsp }
1292 0d6c6ee3 2020-05-20 stsp return err;
1293 56e0773d 2019-11-28 stsp }
1294 56e0773d 2019-11-28 stsp
1295 56e0773d 2019-11-28 stsp int
1296 56e0773d 2019-11-28 stsp got_tree_entry_get_index(struct got_tree_entry *te)
1297 56e0773d 2019-11-28 stsp {
1298 56e0773d 2019-11-28 stsp return te->idx;
1299 56e0773d 2019-11-28 stsp }
1300 56e0773d 2019-11-28 stsp
1301 56e0773d 2019-11-28 stsp struct got_tree_entry *
1302 56e0773d 2019-11-28 stsp got_tree_entry_get_next(struct got_tree_object *tree,
1303 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
1304 56e0773d 2019-11-28 stsp {
1305 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx + 1);
1306 56e0773d 2019-11-28 stsp }
1307 56e0773d 2019-11-28 stsp
1308 56e0773d 2019-11-28 stsp struct got_tree_entry *
1309 56e0773d 2019-11-28 stsp got_tree_entry_get_prev(struct got_tree_object *tree,
1310 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
1311 56e0773d 2019-11-28 stsp {
1312 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx - 1);
1313 56e0773d 2019-11-28 stsp }
1314 56e0773d 2019-11-28 stsp
1315 3840f4c9 2018-09-12 stsp static const struct got_error *
1316 ac544f8c 2019-01-13 stsp request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1317 ebc55e2d 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1318 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
1319 3840f4c9 2018-09-12 stsp {
1320 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
1321 bc1f382f 2022-01-05 thomas struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1322 bc1f382f 2022-01-05 thomas int outfd_child;
1323 24140570 2018-09-09 stsp
1324 bc1f382f 2022-01-05 thomas err = pack_child_send_tempfiles(ibuf, pack);
1325 bc1f382f 2022-01-05 thomas if (err)
1326 bc1f382f 2022-01-05 thomas return err;
1327 3840f4c9 2018-09-12 stsp
1328 3840f4c9 2018-09-12 stsp outfd_child = dup(outfd);
1329 3840f4c9 2018-09-12 stsp if (outfd_child == -1)
1330 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1331 3840f4c9 2018-09-12 stsp
1332 ebc55e2d 2018-12-24 stsp err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1333 3840f4c9 2018-09-12 stsp if (err)
1334 3840f4c9 2018-09-12 stsp return err;
1335 3840f4c9 2018-09-12 stsp
1336 3840f4c9 2018-09-12 stsp err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1337 3840f4c9 2018-09-12 stsp outfd_child);
1338 3840f4c9 2018-09-12 stsp if (err) {
1339 3840f4c9 2018-09-12 stsp return err;
1340 3840f4c9 2018-09-12 stsp }
1341 3840f4c9 2018-09-12 stsp
1342 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen,
1343 ebc55e2d 2018-12-24 stsp pack->privsep_child->ibuf);
1344 3840f4c9 2018-09-12 stsp if (err)
1345 3840f4c9 2018-09-12 stsp return err;
1346 3840f4c9 2018-09-12 stsp
1347 3840f4c9 2018-09-12 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1348 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1349 3840f4c9 2018-09-12 stsp
1350 3840f4c9 2018-09-12 stsp return err;
1351 3840f4c9 2018-09-12 stsp }
1352 3840f4c9 2018-09-12 stsp
1353 ebc55e2d 2018-12-24 stsp static const struct got_error *
1354 ac544f8c 2019-01-13 stsp read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1355 ac544f8c 2019-01-13 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1356 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
1357 68482ea3 2017-11-27 stsp {
1358 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
1359 ebc55e2d 2018-12-24 stsp
1360 ebc55e2d 2018-12-24 stsp if (pack->privsep_child == NULL) {
1361 ebc55e2d 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
1362 ebc55e2d 2018-12-24 stsp if (err)
1363 ebc55e2d 2018-12-24 stsp return err;
1364 ebc55e2d 2018-12-24 stsp }
1365 68482ea3 2017-11-27 stsp
1366 ac544f8c 2019-01-13 stsp return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1367 ac544f8c 2019-01-13 stsp idx, id);
1368 9f2369b0 2018-12-24 stsp }
1369 9f2369b0 2018-12-24 stsp
1370 9f2369b0 2018-12-24 stsp static const struct got_error *
1371 ac544f8c 2019-01-13 stsp request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1372 d5c81d44 2021-07-08 stsp int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1373 9f2369b0 2018-12-24 stsp {
1374 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1375 9f2369b0 2018-12-24 stsp int outfd_child;
1376 9f2369b0 2018-12-24 stsp
1377 9f2369b0 2018-12-24 stsp outfd_child = dup(outfd);
1378 9f2369b0 2018-12-24 stsp if (outfd_child == -1)
1379 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1380 9f2369b0 2018-12-24 stsp
1381 d5c81d44 2021-07-08 stsp err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1382 9f2369b0 2018-12-24 stsp if (err)
1383 9f2369b0 2018-12-24 stsp return err;
1384 9f2369b0 2018-12-24 stsp
1385 9f2369b0 2018-12-24 stsp err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1386 41496140 2019-02-21 stsp if (err)
1387 9f2369b0 2018-12-24 stsp return err;
1388 9f2369b0 2018-12-24 stsp
1389 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1390 9f2369b0 2018-12-24 stsp if (err)
1391 9f2369b0 2018-12-24 stsp return err;
1392 9f2369b0 2018-12-24 stsp
1393 9f2369b0 2018-12-24 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1394 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
1395 9f2369b0 2018-12-24 stsp
1396 9f2369b0 2018-12-24 stsp return err;
1397 9f2369b0 2018-12-24 stsp }
1398 9f2369b0 2018-12-24 stsp
1399 9f2369b0 2018-12-24 stsp static const struct got_error *
1400 ac544f8c 2019-01-13 stsp read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1401 d5c81d44 2021-07-08 stsp int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1402 9f2369b0 2018-12-24 stsp {
1403 ddc7b220 2019-09-08 stsp const struct got_error *err;
1404 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1405 9f2369b0 2018-12-24 stsp pid_t pid;
1406 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1407 9f2369b0 2018-12-24 stsp
1408 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1409 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1410 d5c81d44 2021-07-08 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1411 d5c81d44 2021-07-08 stsp ibuf);
1412 9f2369b0 2018-12-24 stsp }
1413 9f2369b0 2018-12-24 stsp
1414 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1415 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1416 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1417 9f2369b0 2018-12-24 stsp
1418 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1419 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1420 ddc7b220 2019-09-08 stsp free(ibuf);
1421 ddc7b220 2019-09-08 stsp return err;
1422 ddc7b220 2019-09-08 stsp }
1423 9f2369b0 2018-12-24 stsp
1424 9f2369b0 2018-12-24 stsp pid = fork();
1425 ddc7b220 2019-09-08 stsp if (pid == -1) {
1426 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1427 ddc7b220 2019-09-08 stsp free(ibuf);
1428 ddc7b220 2019-09-08 stsp return err;
1429 ddc7b220 2019-09-08 stsp }
1430 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1431 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1432 9f2369b0 2018-12-24 stsp repo->path);
1433 9f2369b0 2018-12-24 stsp /* not reached */
1434 9f2369b0 2018-12-24 stsp }
1435 9f2369b0 2018-12-24 stsp
1436 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
1437 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1438 ddc7b220 2019-09-08 stsp free(ibuf);
1439 ddc7b220 2019-09-08 stsp return err;
1440 ddc7b220 2019-09-08 stsp }
1441 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1442 9f2369b0 2018-12-24 stsp imsg_fds[0];
1443 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1444 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1445 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1446 9f2369b0 2018-12-24 stsp
1447 d5c81d44 2021-07-08 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1448 ebc55e2d 2018-12-24 stsp }
1449 68482ea3 2017-11-27 stsp
1450 ebc55e2d 2018-12-24 stsp static const struct got_error *
1451 ebc55e2d 2018-12-24 stsp open_blob(struct got_blob_object **blob, struct got_repository *repo,
1452 ebc55e2d 2018-12-24 stsp struct got_object_id *id, size_t blocksize)
1453 ebc55e2d 2018-12-24 stsp {
1454 ebc55e2d 2018-12-24 stsp const struct got_error *err = NULL;
1455 ebc55e2d 2018-12-24 stsp struct got_packidx *packidx = NULL;
1456 ebc55e2d 2018-12-24 stsp int idx;
1457 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1458 ac544f8c 2019-01-13 stsp uint8_t *outbuf;
1459 e82b1d81 2019-07-27 stsp int outfd;
1460 ebc55e2d 2018-12-24 stsp size_t size, hdrlen;
1461 ebc55e2d 2018-12-24 stsp struct stat sb;
1462 ebc55e2d 2018-12-24 stsp
1463 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
1464 4558fcd4 2018-01-14 stsp if (*blob == NULL)
1465 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1466 68482ea3 2017-11-27 stsp
1467 55da3778 2018-09-10 stsp outfd = got_opentempfd();
1468 55da3778 2018-09-10 stsp if (outfd == -1)
1469 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
1470 55da3778 2018-09-10 stsp
1471 062ebb78 2018-07-12 stsp (*blob)->read_buf = malloc(blocksize);
1472 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
1473 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1474 c7254d79 2018-04-24 stsp goto done;
1475 15c8b0e6 2018-04-24 stsp }
1476 ebc55e2d 2018-12-24 stsp
1477 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1478 e82b1d81 2019-07-27 stsp if (err == NULL) {
1479 ebc55e2d 2018-12-24 stsp struct got_pack *pack = NULL;
1480 34f480ff 2019-07-27 stsp
1481 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
1482 aea75d87 2021-07-06 stsp packidx->path_packidx);
1483 ebc55e2d 2018-12-24 stsp if (err)
1484 ebc55e2d 2018-12-24 stsp goto done;
1485 ebc55e2d 2018-12-24 stsp
1486 ebc55e2d 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1487 55da3778 2018-09-10 stsp if (pack == NULL) {
1488 ebc55e2d 2018-12-24 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1489 ebc55e2d 2018-12-24 stsp packidx);
1490 55da3778 2018-09-10 stsp if (err)
1491 55da3778 2018-09-10 stsp goto done;
1492 55da3778 2018-09-10 stsp }
1493 ac544f8c 2019-01-13 stsp err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1494 ac544f8c 2019-01-13 stsp pack, packidx, idx, id);
1495 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1496 e82b1d81 2019-07-27 stsp int infd;
1497 e82b1d81 2019-07-27 stsp
1498 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&infd, id, repo);
1499 e82b1d81 2019-07-27 stsp if (err)
1500 e82b1d81 2019-07-27 stsp goto done;
1501 ac544f8c 2019-01-13 stsp err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1502 d5c81d44 2021-07-08 stsp id, repo);
1503 e82b1d81 2019-07-27 stsp }
1504 ebc55e2d 2018-12-24 stsp if (err)
1505 55da3778 2018-09-10 stsp goto done;
1506 2967a784 2018-04-24 stsp
1507 de060dff 2018-12-24 stsp if (hdrlen > size) {
1508 ebc55e2d 2018-12-24 stsp err = got_error(GOT_ERR_BAD_OBJ_HDR);
1509 ebc55e2d 2018-12-24 stsp goto done;
1510 ebc55e2d 2018-12-24 stsp }
1511 ebc55e2d 2018-12-24 stsp
1512 ac544f8c 2019-01-13 stsp if (outbuf) {
1513 08578a35 2021-01-22 stsp if (close(outfd) == -1 && err == NULL)
1514 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1515 ac544f8c 2019-01-13 stsp outfd = -1;
1516 ac544f8c 2019-01-13 stsp (*blob)->f = fmemopen(outbuf, size, "rb");
1517 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1518 638f9024 2019-05-13 stsp err = got_error_from_errno("fmemopen");
1519 ac544f8c 2019-01-13 stsp free(outbuf);
1520 ac544f8c 2019-01-13 stsp goto done;
1521 ac544f8c 2019-01-13 stsp }
1522 ac544f8c 2019-01-13 stsp (*blob)->data = outbuf;
1523 ac544f8c 2019-01-13 stsp } else {
1524 ac544f8c 2019-01-13 stsp if (fstat(outfd, &sb) == -1) {
1525 638f9024 2019-05-13 stsp err = got_error_from_errno("fstat");
1526 ac544f8c 2019-01-13 stsp goto done;
1527 ac544f8c 2019-01-13 stsp }
1528 ac544f8c 2019-01-13 stsp
1529 ac544f8c 2019-01-13 stsp if (sb.st_size != size) {
1530 ac544f8c 2019-01-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1531 ac544f8c 2019-01-13 stsp goto done;
1532 ac544f8c 2019-01-13 stsp }
1533 ac544f8c 2019-01-13 stsp
1534 ac544f8c 2019-01-13 stsp (*blob)->f = fdopen(outfd, "rb");
1535 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1536 638f9024 2019-05-13 stsp err = got_error_from_errno("fdopen");
1537 ac544f8c 2019-01-13 stsp close(outfd);
1538 ac544f8c 2019-01-13 stsp outfd = -1;
1539 ac544f8c 2019-01-13 stsp goto done;
1540 ac544f8c 2019-01-13 stsp }
1541 68482ea3 2017-11-27 stsp }
1542 68482ea3 2017-11-27 stsp
1543 ebc55e2d 2018-12-24 stsp (*blob)->hdrlen = hdrlen;
1544 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
1545 ebc55e2d 2018-12-24 stsp memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1546 7d283eee 2017-11-29 stsp
1547 c7254d79 2018-04-24 stsp done:
1548 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1549 55da3778 2018-09-10 stsp if (err) {
1550 55da3778 2018-09-10 stsp if (*blob) {
1551 7baf5860 2019-03-19 stsp got_object_blob_close(*blob);
1552 55da3778 2018-09-10 stsp *blob = NULL;
1553 55da3778 2018-09-10 stsp } else if (outfd != -1)
1554 55da3778 2018-09-10 stsp close(outfd);
1555 a19581a2 2018-06-21 stsp }
1556 a19581a2 2018-06-21 stsp return err;
1557 a19581a2 2018-06-21 stsp }
1558 a19581a2 2018-06-21 stsp
1559 a19581a2 2018-06-21 stsp const struct got_error *
1560 a19581a2 2018-06-21 stsp got_object_open_as_blob(struct got_blob_object **blob,
1561 a19581a2 2018-06-21 stsp struct got_repository *repo, struct got_object_id *id,
1562 a19581a2 2018-06-21 stsp size_t blocksize)
1563 a19581a2 2018-06-21 stsp {
1564 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, id, blocksize);
1565 ebc55e2d 2018-12-24 stsp }
1566 835e0dbd 2018-06-21 stsp
1567 ebc55e2d 2018-12-24 stsp const struct got_error *
1568 ebc55e2d 2018-12-24 stsp got_object_blob_open(struct got_blob_object **blob,
1569 ebc55e2d 2018-12-24 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
1570 ebc55e2d 2018-12-24 stsp {
1571 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1572 0ffeb3c2 2017-11-26 stsp }
1573 68482ea3 2017-11-27 stsp
1574 fb43ecf1 2019-02-11 stsp const struct got_error *
1575 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
1576 68482ea3 2017-11-27 stsp {
1577 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL;
1578 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
1579 56b63ca4 2021-01-22 stsp if (blob->f && fclose(blob->f) == EOF)
1580 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1581 ac544f8c 2019-01-13 stsp free(blob->data);
1582 68482ea3 2017-11-27 stsp free(blob);
1583 fb43ecf1 2019-02-11 stsp return err;
1584 f934cf2c 2018-02-12 stsp }
1585 f934cf2c 2018-02-12 stsp
1586 8ba819a3 2020-07-23 stsp void
1587 8ba819a3 2020-07-23 stsp got_object_blob_rewind(struct got_blob_object *blob)
1588 8ba819a3 2020-07-23 stsp {
1589 8ba819a3 2020-07-23 stsp if (blob->f)
1590 8ba819a3 2020-07-23 stsp rewind(blob->f);
1591 8ba819a3 2020-07-23 stsp }
1592 8ba819a3 2020-07-23 stsp
1593 f934cf2c 2018-02-12 stsp char *
1594 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1595 f934cf2c 2018-02-12 stsp {
1596 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1597 f934cf2c 2018-02-12 stsp }
1598 f934cf2c 2018-02-12 stsp
1599 f934cf2c 2018-02-12 stsp size_t
1600 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
1601 f934cf2c 2018-02-12 stsp {
1602 f934cf2c 2018-02-12 stsp return blob->hdrlen;
1603 68482ea3 2017-11-27 stsp }
1604 68482ea3 2017-11-27 stsp
1605 f934cf2c 2018-02-12 stsp const uint8_t *
1606 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
1607 f934cf2c 2018-02-12 stsp {
1608 f934cf2c 2018-02-12 stsp return blob->read_buf;
1609 f934cf2c 2018-02-12 stsp }
1610 f934cf2c 2018-02-12 stsp
1611 68482ea3 2017-11-27 stsp const struct got_error *
1612 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1613 68482ea3 2017-11-27 stsp {
1614 eb651edf 2018-02-11 stsp size_t n;
1615 eb651edf 2018-02-11 stsp
1616 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1617 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
1618 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
1619 eb651edf 2018-02-11 stsp *outlenp = n;
1620 35e9ba5d 2018-06-21 stsp return NULL;
1621 35e9ba5d 2018-06-21 stsp }
1622 35e9ba5d 2018-06-21 stsp
1623 35e9ba5d 2018-06-21 stsp const struct got_error *
1624 be659d10 2020-11-18 stsp got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1625 6c4c42e0 2019-06-24 stsp off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1626 35e9ba5d 2018-06-21 stsp {
1627 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
1628 b6752625 2018-12-24 stsp size_t n, len, hdrlen;
1629 84451b3e 2018-07-10 stsp const uint8_t *buf;
1630 84451b3e 2018-07-10 stsp int i;
1631 c33ebc60 2020-11-18 stsp const int alloc_chunksz = 512;
1632 c33ebc60 2020-11-18 stsp size_t nalloc = 0;
1633 f595d9bd 2019-08-14 stsp off_t off = 0, total_len = 0;
1634 84451b3e 2018-07-10 stsp
1635 6c4c42e0 2019-06-24 stsp if (line_offsets)
1636 6c4c42e0 2019-06-24 stsp *line_offsets = NULL;
1637 f595d9bd 2019-08-14 stsp if (filesize)
1638 f595d9bd 2019-08-14 stsp *filesize = 0;
1639 84451b3e 2018-07-10 stsp if (nlines)
1640 84451b3e 2018-07-10 stsp *nlines = 0;
1641 35e9ba5d 2018-06-21 stsp
1642 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1643 35e9ba5d 2018-06-21 stsp do {
1644 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
1645 35e9ba5d 2018-06-21 stsp if (err)
1646 35e9ba5d 2018-06-21 stsp return err;
1647 35e9ba5d 2018-06-21 stsp if (len == 0)
1648 35e9ba5d 2018-06-21 stsp break;
1649 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
1650 b02560ec 2019-08-19 stsp i = hdrlen;
1651 f1cbc3bc 2020-11-18 stsp if (nlines) {
1652 f1cbc3bc 2020-11-18 stsp if (line_offsets && *line_offsets == NULL) {
1653 78695fb7 2019-08-12 stsp /* Have some data but perhaps no '\n'. */
1654 78695fb7 2019-08-12 stsp *nlines = 1;
1655 c33ebc60 2020-11-18 stsp nalloc = alloc_chunksz;
1656 c33ebc60 2020-11-18 stsp *line_offsets = calloc(nalloc,
1657 c33ebc60 2020-11-18 stsp sizeof(**line_offsets));
1658 78695fb7 2019-08-12 stsp if (*line_offsets == NULL)
1659 845785d4 2020-02-02 tracey return got_error_from_errno("calloc");
1660 b02560ec 2019-08-19 stsp
1661 b02560ec 2019-08-19 stsp /* Skip forward over end of first line. */
1662 b02560ec 2019-08-19 stsp while (i < len) {
1663 b02560ec 2019-08-19 stsp if (buf[i] == '\n')
1664 b02560ec 2019-08-19 stsp break;
1665 b02560ec 2019-08-19 stsp i++;
1666 b02560ec 2019-08-19 stsp }
1667 b02560ec 2019-08-19 stsp }
1668 b02560ec 2019-08-19 stsp /* Scan '\n' offsets in remaining chunk of data. */
1669 b02560ec 2019-08-19 stsp while (i < len) {
1670 b02560ec 2019-08-19 stsp if (buf[i] != '\n') {
1671 b02560ec 2019-08-19 stsp i++;
1672 f595d9bd 2019-08-14 stsp continue;
1673 b02560ec 2019-08-19 stsp }
1674 f595d9bd 2019-08-14 stsp (*nlines)++;
1675 c33ebc60 2020-11-18 stsp if (line_offsets && nalloc < *nlines) {
1676 c33ebc60 2020-11-18 stsp size_t n = *nlines + alloc_chunksz;
1677 78695fb7 2019-08-12 stsp off_t *o = recallocarray(*line_offsets,
1678 c33ebc60 2020-11-18 stsp nalloc, n, sizeof(**line_offsets));
1679 78695fb7 2019-08-12 stsp if (o == NULL) {
1680 78695fb7 2019-08-12 stsp free(*line_offsets);
1681 78695fb7 2019-08-12 stsp *line_offsets = NULL;
1682 78695fb7 2019-08-12 stsp return got_error_from_errno(
1683 78695fb7 2019-08-12 stsp "recallocarray");
1684 78695fb7 2019-08-12 stsp }
1685 78695fb7 2019-08-12 stsp *line_offsets = o;
1686 c33ebc60 2020-11-18 stsp nalloc = n;
1687 78695fb7 2019-08-12 stsp }
1688 f1cbc3bc 2020-11-18 stsp if (line_offsets) {
1689 f1cbc3bc 2020-11-18 stsp off = total_len + i - hdrlen + 1;
1690 f1cbc3bc 2020-11-18 stsp (*line_offsets)[*nlines - 1] = off;
1691 f1cbc3bc 2020-11-18 stsp }
1692 b02560ec 2019-08-19 stsp i++;
1693 6c4c42e0 2019-06-24 stsp }
1694 84451b3e 2018-07-10 stsp }
1695 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
1696 454a6b59 2018-12-24 stsp n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1697 b6752625 2018-12-24 stsp if (n != len - hdrlen)
1698 b6752625 2018-12-24 stsp return got_ferror(outfile, GOT_ERR_IO);
1699 f595d9bd 2019-08-14 stsp total_len += len - hdrlen;
1700 35e9ba5d 2018-06-21 stsp hdrlen = 0;
1701 35e9ba5d 2018-06-21 stsp } while (len != 0);
1702 35e9ba5d 2018-06-21 stsp
1703 cbe7f848 2019-02-11 stsp if (fflush(outfile) != 0)
1704 638f9024 2019-05-13 stsp return got_error_from_errno("fflush");
1705 35e9ba5d 2018-06-21 stsp rewind(outfile);
1706 35e9ba5d 2018-06-21 stsp
1707 f595d9bd 2019-08-14 stsp if (filesize)
1708 f595d9bd 2019-08-14 stsp *filesize = total_len;
1709 f595d9bd 2019-08-14 stsp
1710 776d4d29 2018-06-17 stsp return NULL;
1711 f4a881ce 2018-11-17 stsp }
1712 f4a881ce 2018-11-17 stsp
1713 f4a881ce 2018-11-17 stsp static const struct got_error *
1714 268f7291 2018-12-24 stsp request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1715 268f7291 2018-12-24 stsp int pack_idx, struct got_object_id *id)
1716 a158c901 2018-12-23 stsp {
1717 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
1718 a158c901 2018-12-23 stsp
1719 268f7291 2018-12-24 stsp err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1720 268f7291 2018-12-24 stsp pack_idx);
1721 a158c901 2018-12-23 stsp if (err)
1722 a158c901 2018-12-23 stsp return err;
1723 a158c901 2018-12-23 stsp
1724 a158c901 2018-12-23 stsp return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1725 268f7291 2018-12-24 stsp }
1726 268f7291 2018-12-24 stsp
1727 268f7291 2018-12-24 stsp static const struct got_error *
1728 268f7291 2018-12-24 stsp read_packed_tag_privsep(struct got_tag_object **tag,
1729 268f7291 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1730 268f7291 2018-12-24 stsp struct got_object_id *id)
1731 268f7291 2018-12-24 stsp {
1732 268f7291 2018-12-24 stsp const struct got_error *err = NULL;
1733 268f7291 2018-12-24 stsp
1734 268f7291 2018-12-24 stsp if (pack->privsep_child)
1735 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1736 268f7291 2018-12-24 stsp
1737 268f7291 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
1738 268f7291 2018-12-24 stsp if (err)
1739 268f7291 2018-12-24 stsp return err;
1740 268f7291 2018-12-24 stsp
1741 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1742 a158c901 2018-12-23 stsp }
1743 9f2369b0 2018-12-24 stsp
1744 9f2369b0 2018-12-24 stsp static const struct got_error *
1745 9f2369b0 2018-12-24 stsp request_tag(struct got_tag_object **tag, struct got_repository *repo,
1746 d5c81d44 2021-07-08 stsp int fd, struct got_object_id *id)
1747 9f2369b0 2018-12-24 stsp {
1748 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1749 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1750 9f2369b0 2018-12-24 stsp
1751 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1752 9f2369b0 2018-12-24 stsp
1753 d5c81d44 2021-07-08 stsp err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1754 9f2369b0 2018-12-24 stsp if (err)
1755 9f2369b0 2018-12-24 stsp return err;
1756 9f2369b0 2018-12-24 stsp
1757 9f2369b0 2018-12-24 stsp return got_privsep_recv_tag(tag, ibuf);
1758 9f2369b0 2018-12-24 stsp }
1759 9f2369b0 2018-12-24 stsp
1760 9f2369b0 2018-12-24 stsp static const struct got_error *
1761 9f2369b0 2018-12-24 stsp read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1762 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo)
1763 9f2369b0 2018-12-24 stsp {
1764 ddc7b220 2019-09-08 stsp const struct got_error *err;
1765 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1766 9f2369b0 2018-12-24 stsp pid_t pid;
1767 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1768 9f2369b0 2018-12-24 stsp
1769 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1770 d5c81d44 2021-07-08 stsp return request_tag(tag, repo, obj_fd, id);
1771 9f2369b0 2018-12-24 stsp
1772 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1773 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1774 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1775 9f2369b0 2018-12-24 stsp
1776 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1777 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1778 ddc7b220 2019-09-08 stsp free(ibuf);
1779 ddc7b220 2019-09-08 stsp return err;
1780 ddc7b220 2019-09-08 stsp }
1781 9f2369b0 2018-12-24 stsp
1782 9f2369b0 2018-12-24 stsp pid = fork();
1783 ddc7b220 2019-09-08 stsp if (pid == -1) {
1784 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1785 ddc7b220 2019-09-08 stsp free(ibuf);
1786 ddc7b220 2019-09-08 stsp return err;
1787 ddc7b220 2019-09-08 stsp }
1788 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1789 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1790 9f2369b0 2018-12-24 stsp repo->path);
1791 9f2369b0 2018-12-24 stsp /* not reached */
1792 9f2369b0 2018-12-24 stsp }
1793 9f2369b0 2018-12-24 stsp
1794 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
1795 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1796 ddc7b220 2019-09-08 stsp free(ibuf);
1797 ddc7b220 2019-09-08 stsp return err;
1798 ddc7b220 2019-09-08 stsp }
1799 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1800 9f2369b0 2018-12-24 stsp imsg_fds[0];
1801 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1802 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1803 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1804 a158c901 2018-12-23 stsp
1805 d5c81d44 2021-07-08 stsp return request_tag(tag, repo, obj_fd, id);
1806 9f2369b0 2018-12-24 stsp }
1807 a158c901 2018-12-23 stsp
1808 a158c901 2018-12-23 stsp static const struct got_error *
1809 268f7291 2018-12-24 stsp open_tag(struct got_tag_object **tag, struct got_repository *repo,
1810 268f7291 2018-12-24 stsp struct got_object_id *id, int check_cache)
1811 f4a881ce 2018-11-17 stsp {
1812 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1813 268f7291 2018-12-24 stsp struct got_packidx *packidx = NULL;
1814 e82b1d81 2019-07-27 stsp int idx;
1815 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1816 5d844a1e 2019-08-13 stsp struct got_object *obj = NULL;
1817 5d844a1e 2019-08-13 stsp int obj_type = GOT_OBJ_TYPE_ANY;
1818 f4a881ce 2018-11-17 stsp
1819 f4a881ce 2018-11-17 stsp if (check_cache) {
1820 268f7291 2018-12-24 stsp *tag = got_repo_get_cached_tag(repo, id);
1821 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1822 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1823 f4a881ce 2018-11-17 stsp return NULL;
1824 f4a881ce 2018-11-17 stsp }
1825 f4a881ce 2018-11-17 stsp } else
1826 f4a881ce 2018-11-17 stsp *tag = NULL;
1827 f4a881ce 2018-11-17 stsp
1828 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1829 e82b1d81 2019-07-27 stsp if (err == NULL) {
1830 268f7291 2018-12-24 stsp struct got_pack *pack = NULL;
1831 f4a881ce 2018-11-17 stsp
1832 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
1833 aea75d87 2021-07-06 stsp packidx->path_packidx);
1834 268f7291 2018-12-24 stsp if (err)
1835 268f7291 2018-12-24 stsp return err;
1836 268f7291 2018-12-24 stsp
1837 268f7291 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1838 f4a881ce 2018-11-17 stsp if (pack == NULL) {
1839 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1840 e82b1d81 2019-07-27 stsp packidx);
1841 f4a881ce 2018-11-17 stsp if (err)
1842 8d2c5ea3 2019-08-13 stsp goto done;
1843 f4a881ce 2018-11-17 stsp }
1844 5d844a1e 2019-08-13 stsp
1845 992eb9d8 2020-02-07 tracey /* Beware of "lightweight" tags: Check object type first. */
1846 5d844a1e 2019-08-13 stsp err = read_packed_object_privsep(&obj, repo, pack, packidx,
1847 5d844a1e 2019-08-13 stsp idx, id);
1848 5d844a1e 2019-08-13 stsp if (err)
1849 5d844a1e 2019-08-13 stsp goto done;
1850 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1851 5d844a1e 2019-08-13 stsp got_object_close(obj);
1852 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG) {
1853 5d844a1e 2019-08-13 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1854 5d844a1e 2019-08-13 stsp goto done;
1855 5d844a1e 2019-08-13 stsp }
1856 5d844a1e 2019-08-13 stsp err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1857 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1858 e82b1d81 2019-07-27 stsp int fd;
1859 e82b1d81 2019-07-27 stsp
1860 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1861 e82b1d81 2019-07-27 stsp if (err)
1862 e82b1d81 2019-07-27 stsp return err;
1863 d5c81d44 2021-07-08 stsp err = got_object_read_header_privsep(&obj, id, repo, fd);
1864 5d844a1e 2019-08-13 stsp if (err)
1865 5d844a1e 2019-08-13 stsp return err;
1866 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1867 5d844a1e 2019-08-13 stsp got_object_close(obj);
1868 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
1869 5d844a1e 2019-08-13 stsp return got_error(GOT_ERR_OBJ_TYPE);
1870 5d844a1e 2019-08-13 stsp
1871 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1872 5d844a1e 2019-08-13 stsp if (err)
1873 5d844a1e 2019-08-13 stsp return err;
1874 d5c81d44 2021-07-08 stsp err = read_tag_privsep(tag, fd, id, repo);
1875 e82b1d81 2019-07-27 stsp }
1876 f4a881ce 2018-11-17 stsp
1877 f4a881ce 2018-11-17 stsp if (err == NULL) {
1878 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1879 268f7291 2018-12-24 stsp err = got_repo_cache_tag(repo, id, *tag);
1880 f4a881ce 2018-11-17 stsp }
1881 8d2c5ea3 2019-08-13 stsp done:
1882 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1883 f4a881ce 2018-11-17 stsp return err;
1884 f4a881ce 2018-11-17 stsp }
1885 f4a881ce 2018-11-17 stsp
1886 f4a881ce 2018-11-17 stsp const struct got_error *
1887 f4a881ce 2018-11-17 stsp got_object_open_as_tag(struct got_tag_object **tag,
1888 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object_id *id)
1889 f4a881ce 2018-11-17 stsp {
1890 f4a881ce 2018-11-17 stsp *tag = got_repo_get_cached_tag(repo, id);
1891 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1892 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1893 f4a881ce 2018-11-17 stsp return NULL;
1894 f4a881ce 2018-11-17 stsp }
1895 f4a881ce 2018-11-17 stsp
1896 268f7291 2018-12-24 stsp return open_tag(tag, repo, id, 0);
1897 f4a881ce 2018-11-17 stsp }
1898 f4a881ce 2018-11-17 stsp
1899 f4a881ce 2018-11-17 stsp const struct got_error *
1900 f4a881ce 2018-11-17 stsp got_object_tag_open(struct got_tag_object **tag,
1901 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object *obj)
1902 f4a881ce 2018-11-17 stsp {
1903 268f7291 2018-12-24 stsp return open_tag(tag, repo, got_object_get_id(obj), 1);
1904 d24820bf 2019-08-11 stsp }
1905 d24820bf 2019-08-11 stsp
1906 d24820bf 2019-08-11 stsp const char *
1907 d24820bf 2019-08-11 stsp got_object_tag_get_name(struct got_tag_object *tag)
1908 d24820bf 2019-08-11 stsp {
1909 d24820bf 2019-08-11 stsp return tag->tag;
1910 0bd18d37 2019-02-01 stsp }
1911 0bd18d37 2019-02-01 stsp
1912 0bd18d37 2019-02-01 stsp int
1913 0bd18d37 2019-02-01 stsp got_object_tag_get_object_type(struct got_tag_object *tag)
1914 0bd18d37 2019-02-01 stsp {
1915 0bd18d37 2019-02-01 stsp return tag->obj_type;
1916 0bd18d37 2019-02-01 stsp }
1917 0bd18d37 2019-02-01 stsp
1918 0bd18d37 2019-02-01 stsp struct got_object_id *
1919 0bd18d37 2019-02-01 stsp got_object_tag_get_object_id(struct got_tag_object *tag)
1920 0bd18d37 2019-02-01 stsp {
1921 0bd18d37 2019-02-01 stsp return &tag->id;
1922 01073a5d 2019-08-22 stsp }
1923 01073a5d 2019-08-22 stsp
1924 01073a5d 2019-08-22 stsp time_t
1925 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_time(struct got_tag_object *tag)
1926 01073a5d 2019-08-22 stsp {
1927 01073a5d 2019-08-22 stsp return tag->tagger_time;
1928 01073a5d 2019-08-22 stsp }
1929 01073a5d 2019-08-22 stsp
1930 01073a5d 2019-08-22 stsp time_t
1931 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1932 01073a5d 2019-08-22 stsp {
1933 01073a5d 2019-08-22 stsp return tag->tagger_gmtoff;
1934 01073a5d 2019-08-22 stsp }
1935 01073a5d 2019-08-22 stsp
1936 01073a5d 2019-08-22 stsp const char *
1937 01073a5d 2019-08-22 stsp got_object_tag_get_tagger(struct got_tag_object *tag)
1938 01073a5d 2019-08-22 stsp {
1939 01073a5d 2019-08-22 stsp return tag->tagger;
1940 776d4d29 2018-06-17 stsp }
1941 776d4d29 2018-06-17 stsp
1942 01073a5d 2019-08-22 stsp const char *
1943 01073a5d 2019-08-22 stsp got_object_tag_get_message(struct got_tag_object *tag)
1944 01073a5d 2019-08-22 stsp {
1945 01073a5d 2019-08-22 stsp return tag->tagmsg;
1946 01073a5d 2019-08-22 stsp }
1947 01073a5d 2019-08-22 stsp
1948 776d4d29 2018-06-17 stsp static struct got_tree_entry *
1949 65a9bbe9 2018-09-15 stsp find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1950 776d4d29 2018-06-17 stsp {
1951 56e0773d 2019-11-28 stsp int i;
1952 776d4d29 2018-06-17 stsp
1953 63da309a 2018-11-07 stsp /* Note that tree entries are sorted in strncmp() order. */
1954 56e0773d 2019-11-28 stsp for (i = 0; i < tree->nentries; i++) {
1955 56e0773d 2019-11-28 stsp struct got_tree_entry *te = &tree->entries[i];
1956 63da309a 2018-11-07 stsp int cmp = strncmp(te->name, name, len);
1957 63da309a 2018-11-07 stsp if (cmp < 0)
1958 63da309a 2018-11-07 stsp continue;
1959 63da309a 2018-11-07 stsp if (cmp > 0)
1960 63da309a 2018-11-07 stsp break;
1961 63da309a 2018-11-07 stsp if (te->name[len] == '\0')
1962 776d4d29 2018-06-17 stsp return te;
1963 776d4d29 2018-06-17 stsp }
1964 eb651edf 2018-02-11 stsp return NULL;
1965 a129376b 2019-03-28 stsp }
1966 a129376b 2019-03-28 stsp
1967 56e0773d 2019-11-28 stsp struct got_tree_entry *
1968 a129376b 2019-03-28 stsp got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1969 a129376b 2019-03-28 stsp {
1970 a129376b 2019-03-28 stsp return find_entry_by_name(tree, name, strlen(name));
1971 776d4d29 2018-06-17 stsp }
1972 776d4d29 2018-06-17 stsp
1973 776d4d29 2018-06-17 stsp const struct got_error *
1974 cc8021af 2021-10-12 thomas got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1975 cc8021af 2021-10-12 thomas struct got_repository *repo, struct got_tree_object *tree,
1976 cc8021af 2021-10-12 thomas const char *path)
1977 776d4d29 2018-06-17 stsp {
1978 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
1979 cc8021af 2021-10-12 thomas struct got_tree_object *subtree = NULL;
1980 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
1981 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1982 b7cd37e5 2018-11-18 stsp size_t seglen;
1983 776d4d29 2018-06-17 stsp
1984 27d434c2 2018-09-15 stsp *id = NULL;
1985 776d4d29 2018-06-17 stsp
1986 65a9bbe9 2018-09-15 stsp s = path;
1987 5e54fb30 2019-05-31 stsp while (s[0] == '/')
1988 5e54fb30 2019-05-31 stsp s++;
1989 776d4d29 2018-06-17 stsp seg = s;
1990 65a9bbe9 2018-09-15 stsp seglen = 0;
1991 cc8021af 2021-10-12 thomas subtree = tree;
1992 b7cd37e5 2018-11-18 stsp while (*s) {
1993 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
1994 776d4d29 2018-06-17 stsp
1995 776d4d29 2018-06-17 stsp if (*s != '/') {
1996 776d4d29 2018-06-17 stsp s++;
1997 65a9bbe9 2018-09-15 stsp seglen++;
1998 00530cfb 2018-06-21 stsp if (*s)
1999 00530cfb 2018-06-21 stsp continue;
2000 776d4d29 2018-06-17 stsp }
2001 776d4d29 2018-06-17 stsp
2002 cc8021af 2021-10-12 thomas te = find_entry_by_name(subtree, seg, seglen);
2003 db37e2c0 2018-06-21 stsp if (te == NULL) {
2004 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2005 776d4d29 2018-06-17 stsp goto done;
2006 776d4d29 2018-06-17 stsp }
2007 776d4d29 2018-06-17 stsp
2008 b7cd37e5 2018-11-18 stsp if (*s == '\0')
2009 67606321 2018-06-21 stsp break;
2010 67606321 2018-06-21 stsp
2011 776d4d29 2018-06-17 stsp seg = s + 1;
2012 65a9bbe9 2018-09-15 stsp seglen = 0;
2013 776d4d29 2018-06-17 stsp s++;
2014 776d4d29 2018-06-17 stsp if (*s) {
2015 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
2016 56e0773d 2019-11-28 stsp &te->id);
2017 db37e2c0 2018-06-21 stsp te = NULL;
2018 776d4d29 2018-06-17 stsp if (err)
2019 776d4d29 2018-06-17 stsp goto done;
2020 cc8021af 2021-10-12 thomas if (subtree != tree)
2021 cc8021af 2021-10-12 thomas got_object_tree_close(subtree);
2022 cc8021af 2021-10-12 thomas subtree = next_tree;
2023 776d4d29 2018-06-17 stsp }
2024 776d4d29 2018-06-17 stsp }
2025 776d4d29 2018-06-17 stsp
2026 27d434c2 2018-09-15 stsp if (te) {
2027 56e0773d 2019-11-28 stsp *id = got_object_id_dup(&te->id);
2028 27d434c2 2018-09-15 stsp if (*id == NULL)
2029 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2030 cc8021af 2021-10-12 thomas if (mode)
2031 cc8021af 2021-10-12 thomas *mode = te->mode;
2032 27d434c2 2018-09-15 stsp } else
2033 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2034 cc8021af 2021-10-12 thomas done:
2035 cc8021af 2021-10-12 thomas if (subtree && subtree != tree)
2036 cc8021af 2021-10-12 thomas got_object_tree_close(subtree);
2037 cc8021af 2021-10-12 thomas return err;
2038 cc8021af 2021-10-12 thomas }
2039 cc8021af 2021-10-12 thomas const struct got_error *
2040 cc8021af 2021-10-12 thomas got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
2041 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
2042 cc8021af 2021-10-12 thomas {
2043 cc8021af 2021-10-12 thomas const struct got_error *err = NULL;
2044 cc8021af 2021-10-12 thomas struct got_tree_object *tree = NULL;
2045 cc8021af 2021-10-12 thomas
2046 cc8021af 2021-10-12 thomas *id = NULL;
2047 cc8021af 2021-10-12 thomas
2048 cc8021af 2021-10-12 thomas /* Handle opening of root of commit's tree. */
2049 cc8021af 2021-10-12 thomas if (got_path_is_root_dir(path)) {
2050 cc8021af 2021-10-12 thomas *id = got_object_id_dup(commit->tree_id);
2051 cc8021af 2021-10-12 thomas if (*id == NULL)
2052 cc8021af 2021-10-12 thomas err = got_error_from_errno("got_object_id_dup");
2053 cc8021af 2021-10-12 thomas } else {
2054 cc8021af 2021-10-12 thomas err = got_object_open_as_tree(&tree, repo, commit->tree_id);
2055 cc8021af 2021-10-12 thomas if (err)
2056 cc8021af 2021-10-12 thomas goto done;
2057 cc8021af 2021-10-12 thomas err = got_object_tree_find_path(id, NULL, repo, tree, path);
2058 cc8021af 2021-10-12 thomas }
2059 776d4d29 2018-06-17 stsp done:
2060 776d4d29 2018-06-17 stsp if (tree)
2061 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
2062 776d4d29 2018-06-17 stsp return err;
2063 ac5f2b26 2020-05-05 stsp }
2064 ac5f2b26 2020-05-05 stsp
2065 ac5f2b26 2020-05-05 stsp /*
2066 ac5f2b26 2020-05-05 stsp * Normalize file mode bits to avoid false positive tree entry differences
2067 ac5f2b26 2020-05-05 stsp * in case tree entries have unexpected mode bits set.
2068 ac5f2b26 2020-05-05 stsp */
2069 ac5f2b26 2020-05-05 stsp static mode_t
2070 ac5f2b26 2020-05-05 stsp normalize_mode_for_comparison(mode_t mode)
2071 ac5f2b26 2020-05-05 stsp {
2072 ac5f2b26 2020-05-05 stsp /*
2073 ac5f2b26 2020-05-05 stsp * For directories, the only relevant bit is the IFDIR bit.
2074 ac5f2b26 2020-05-05 stsp * This allows us to detect paths changing from a directory
2075 ac5f2b26 2020-05-05 stsp * to a file and vice versa.
2076 ac5f2b26 2020-05-05 stsp */
2077 ac5f2b26 2020-05-05 stsp if (S_ISDIR(mode))
2078 ac5f2b26 2020-05-05 stsp return mode & S_IFDIR;
2079 40dde666 2020-07-23 stsp
2080 40dde666 2020-07-23 stsp /*
2081 40dde666 2020-07-23 stsp * For symlinks, the only relevant bit is the IFLNK bit.
2082 40dde666 2020-07-23 stsp * This allows us to detect paths changing from a symlinks
2083 40dde666 2020-07-23 stsp * to a file or directory and vice versa.
2084 40dde666 2020-07-23 stsp */
2085 40dde666 2020-07-23 stsp if (S_ISLNK(mode))
2086 40dde666 2020-07-23 stsp return mode & S_IFLNK;
2087 ac5f2b26 2020-05-05 stsp
2088 ac5f2b26 2020-05-05 stsp /* For files, the only change we care about is the executable bit. */
2089 ac5f2b26 2020-05-05 stsp return mode & S_IXUSR;
2090 68482ea3 2017-11-27 stsp }
2091 07862c20 2018-09-15 stsp
2092 07862c20 2018-09-15 stsp const struct got_error *
2093 07862c20 2018-09-15 stsp got_object_tree_path_changed(int *changed,
2094 07862c20 2018-09-15 stsp struct got_tree_object *tree01, struct got_tree_object *tree02,
2095 07862c20 2018-09-15 stsp const char *path, struct got_repository *repo)
2096 07862c20 2018-09-15 stsp {
2097 07862c20 2018-09-15 stsp const struct got_error *err = NULL;
2098 07862c20 2018-09-15 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2099 07862c20 2018-09-15 stsp struct got_tree_entry *te1 = NULL, *te2 = NULL;
2100 65a9bbe9 2018-09-15 stsp const char *seg, *s;
2101 3b7f9878 2018-11-18 stsp size_t seglen;
2102 07862c20 2018-09-15 stsp
2103 07862c20 2018-09-15 stsp *changed = 0;
2104 07862c20 2018-09-15 stsp
2105 07862c20 2018-09-15 stsp /* We not do support comparing the root path. */
2106 61a7d79f 2020-02-29 stsp if (got_path_is_root_dir(path))
2107 63f810e6 2020-02-29 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
2108 07862c20 2018-09-15 stsp
2109 07862c20 2018-09-15 stsp tree1 = tree01;
2110 07862c20 2018-09-15 stsp tree2 = tree02;
2111 65a9bbe9 2018-09-15 stsp s = path;
2112 61a7d79f 2020-02-29 stsp while (*s == '/')
2113 61a7d79f 2020-02-29 stsp s++;
2114 07862c20 2018-09-15 stsp seg = s;
2115 65a9bbe9 2018-09-15 stsp seglen = 0;
2116 3b7f9878 2018-11-18 stsp while (*s) {
2117 07862c20 2018-09-15 stsp struct got_tree_object *next_tree1, *next_tree2;
2118 ac5f2b26 2020-05-05 stsp mode_t mode1, mode2;
2119 07862c20 2018-09-15 stsp
2120 07862c20 2018-09-15 stsp if (*s != '/') {
2121 07862c20 2018-09-15 stsp s++;
2122 65a9bbe9 2018-09-15 stsp seglen++;
2123 07862c20 2018-09-15 stsp if (*s)
2124 07862c20 2018-09-15 stsp continue;
2125 07862c20 2018-09-15 stsp }
2126 07862c20 2018-09-15 stsp
2127 65a9bbe9 2018-09-15 stsp te1 = find_entry_by_name(tree1, seg, seglen);
2128 07862c20 2018-09-15 stsp if (te1 == NULL) {
2129 07862c20 2018-09-15 stsp err = got_error(GOT_ERR_NO_OBJ);
2130 07862c20 2018-09-15 stsp goto done;
2131 07862c20 2018-09-15 stsp }
2132 07862c20 2018-09-15 stsp
2133 e8bfb8f3 2020-12-18 stsp if (tree2)
2134 e8bfb8f3 2020-12-18 stsp te2 = find_entry_by_name(tree2, seg, seglen);
2135 07862c20 2018-09-15 stsp
2136 e8bfb8f3 2020-12-18 stsp if (te2) {
2137 e8bfb8f3 2020-12-18 stsp mode1 = normalize_mode_for_comparison(te1->mode);
2138 e8bfb8f3 2020-12-18 stsp mode2 = normalize_mode_for_comparison(te2->mode);
2139 e8bfb8f3 2020-12-18 stsp if (mode1 != mode2) {
2140 e8bfb8f3 2020-12-18 stsp *changed = 1;
2141 e8bfb8f3 2020-12-18 stsp goto done;
2142 e8bfb8f3 2020-12-18 stsp }
2143 e8bfb8f3 2020-12-18 stsp
2144 e8bfb8f3 2020-12-18 stsp if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2145 e8bfb8f3 2020-12-18 stsp *changed = 0;
2146 e8bfb8f3 2020-12-18 stsp goto done;
2147 e8bfb8f3 2020-12-18 stsp }
2148 07862c20 2018-09-15 stsp }
2149 07862c20 2018-09-15 stsp
2150 3b7f9878 2018-11-18 stsp if (*s == '\0') { /* final path element */
2151 07862c20 2018-09-15 stsp *changed = 1;
2152 07862c20 2018-09-15 stsp goto done;
2153 07862c20 2018-09-15 stsp }
2154 07862c20 2018-09-15 stsp
2155 07862c20 2018-09-15 stsp seg = s + 1;
2156 07862c20 2018-09-15 stsp s++;
2157 65a9bbe9 2018-09-15 stsp seglen = 0;
2158 07862c20 2018-09-15 stsp if (*s) {
2159 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree1, repo,
2160 56e0773d 2019-11-28 stsp &te1->id);
2161 07862c20 2018-09-15 stsp te1 = NULL;
2162 07862c20 2018-09-15 stsp if (err)
2163 07862c20 2018-09-15 stsp goto done;
2164 a31cea73 2018-09-15 stsp if (tree1 != tree01)
2165 a31cea73 2018-09-15 stsp got_object_tree_close(tree1);
2166 07862c20 2018-09-15 stsp tree1 = next_tree1;
2167 07862c20 2018-09-15 stsp
2168 e8bfb8f3 2020-12-18 stsp if (te2) {
2169 e8bfb8f3 2020-12-18 stsp err = got_object_open_as_tree(&next_tree2, repo,
2170 e8bfb8f3 2020-12-18 stsp &te2->id);
2171 e8bfb8f3 2020-12-18 stsp te2 = NULL;
2172 e8bfb8f3 2020-12-18 stsp if (err)
2173 e8bfb8f3 2020-12-18 stsp goto done;
2174 e8bfb8f3 2020-12-18 stsp if (tree2 != tree02)
2175 e8bfb8f3 2020-12-18 stsp got_object_tree_close(tree2);
2176 e8bfb8f3 2020-12-18 stsp tree2 = next_tree2;
2177 e8bfb8f3 2020-12-18 stsp } else if (tree2) {
2178 e8bfb8f3 2020-12-18 stsp if (tree2 != tree02)
2179 e8bfb8f3 2020-12-18 stsp got_object_tree_close(tree2);
2180 e8bfb8f3 2020-12-18 stsp tree2 = NULL;
2181 e8bfb8f3 2020-12-18 stsp }
2182 07862c20 2018-09-15 stsp }
2183 07862c20 2018-09-15 stsp }
2184 07862c20 2018-09-15 stsp done:
2185 a31cea73 2018-09-15 stsp if (tree1 && tree1 != tree01)
2186 07862c20 2018-09-15 stsp got_object_tree_close(tree1);
2187 a31cea73 2018-09-15 stsp if (tree2 && tree2 != tree02)
2188 07862c20 2018-09-15 stsp got_object_tree_close(tree2);
2189 77880158 2018-11-04 stsp return err;
2190 77880158 2018-11-04 stsp }
2191 ed175427 2019-05-09 stsp
2192 ed175427 2019-05-09 stsp const struct got_error *
2193 ed175427 2019-05-09 stsp got_object_tree_entry_dup(struct got_tree_entry **new_te,
2194 ed175427 2019-05-09 stsp struct got_tree_entry *te)
2195 ed175427 2019-05-09 stsp {
2196 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
2197 ed175427 2019-05-09 stsp
2198 ed175427 2019-05-09 stsp *new_te = calloc(1, sizeof(**new_te));
2199 ed175427 2019-05-09 stsp if (*new_te == NULL)
2200 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
2201 ed175427 2019-05-09 stsp
2202 ed175427 2019-05-09 stsp (*new_te)->mode = te->mode;
2203 56e0773d 2019-11-28 stsp memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2204 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2205 8c4eabf2 2019-05-10 stsp return err;
2206 63c5ca5d 2019-08-24 stsp }
2207 63c5ca5d 2019-08-24 stsp
2208 63c5ca5d 2019-08-24 stsp int
2209 56e0773d 2019-11-28 stsp got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2210 63c5ca5d 2019-08-24 stsp {
2211 63c5ca5d 2019-08-24 stsp return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2212 e40622f4 2020-07-23 stsp }
2213 e40622f4 2020-07-23 stsp
2214 e40622f4 2020-07-23 stsp int
2215 e40622f4 2020-07-23 stsp got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2216 e40622f4 2020-07-23 stsp {
2217 e40622f4 2020-07-23 stsp /* S_IFDIR check avoids confusing symlinks with submodules. */
2218 e40622f4 2020-07-23 stsp return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2219 e40622f4 2020-07-23 stsp }
2220 e40622f4 2020-07-23 stsp
2221 e40622f4 2020-07-23 stsp static const struct got_error *
2222 e40622f4 2020-07-23 stsp resolve_symlink(char **link_target, const char *path,
2223 945f9229 2022-04-16 thomas struct got_commit_object *commit, struct got_repository *repo)
2224 e40622f4 2020-07-23 stsp {
2225 e40622f4 2020-07-23 stsp const struct got_error *err = NULL;
2226 dbdd6209 2020-10-19 stsp char buf[PATH_MAX];
2227 e40622f4 2020-07-23 stsp char *name, *parent_path = NULL;
2228 e40622f4 2020-07-23 stsp struct got_object_id *tree_obj_id = NULL;
2229 e40622f4 2020-07-23 stsp struct got_tree_object *tree = NULL;
2230 e40622f4 2020-07-23 stsp struct got_tree_entry *te = NULL;
2231 e40622f4 2020-07-23 stsp
2232 e40622f4 2020-07-23 stsp *link_target = NULL;
2233 559d127c 2020-07-23 stsp
2234 dbdd6209 2020-10-19 stsp if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2235 dbdd6209 2020-10-19 stsp return got_error(GOT_ERR_NO_SPACE);
2236 dbdd6209 2020-10-19 stsp
2237 dbdd6209 2020-10-19 stsp name = basename(buf);
2238 e40622f4 2020-07-23 stsp if (name == NULL)
2239 e40622f4 2020-07-23 stsp return got_error_from_errno2("basename", path);
2240 e40622f4 2020-07-23 stsp
2241 e40622f4 2020-07-23 stsp err = got_path_dirname(&parent_path, path);
2242 e40622f4 2020-07-23 stsp if (err)
2243 e40622f4 2020-07-23 stsp return err;
2244 e40622f4 2020-07-23 stsp
2245 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_obj_id, repo, commit,
2246 e40622f4 2020-07-23 stsp parent_path);
2247 e40622f4 2020-07-23 stsp if (err) {
2248 e40622f4 2020-07-23 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2249 e40622f4 2020-07-23 stsp /* Display the complete path in error message. */
2250 e40622f4 2020-07-23 stsp err = got_error_path(path, err->code);
2251 e40622f4 2020-07-23 stsp }
2252 e40622f4 2020-07-23 stsp goto done;
2253 e40622f4 2020-07-23 stsp }
2254 e40622f4 2020-07-23 stsp
2255 e40622f4 2020-07-23 stsp err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2256 e40622f4 2020-07-23 stsp if (err)
2257 e40622f4 2020-07-23 stsp goto done;
2258 e40622f4 2020-07-23 stsp
2259 e40622f4 2020-07-23 stsp te = got_object_tree_find_entry(tree, name);
2260 e40622f4 2020-07-23 stsp if (te == NULL) {
2261 e40622f4 2020-07-23 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2262 e40622f4 2020-07-23 stsp goto done;
2263 e40622f4 2020-07-23 stsp }
2264 e40622f4 2020-07-23 stsp
2265 e40622f4 2020-07-23 stsp if (got_object_tree_entry_is_symlink(te)) {
2266 e40622f4 2020-07-23 stsp err = got_tree_entry_get_symlink_target(link_target, te, repo);
2267 e40622f4 2020-07-23 stsp if (err)
2268 e40622f4 2020-07-23 stsp goto done;
2269 e40622f4 2020-07-23 stsp if (!got_path_is_absolute(*link_target)) {
2270 e40622f4 2020-07-23 stsp char *abspath;
2271 e40622f4 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", parent_path,
2272 e40622f4 2020-07-23 stsp *link_target) == -1) {
2273 e40622f4 2020-07-23 stsp err = got_error_from_errno("asprintf");
2274 e40622f4 2020-07-23 stsp goto done;
2275 e40622f4 2020-07-23 stsp }
2276 e40622f4 2020-07-23 stsp free(*link_target);
2277 e40622f4 2020-07-23 stsp *link_target = malloc(PATH_MAX);
2278 e40622f4 2020-07-23 stsp if (*link_target == NULL) {
2279 e40622f4 2020-07-23 stsp err = got_error_from_errno("malloc");
2280 e40622f4 2020-07-23 stsp goto done;
2281 e40622f4 2020-07-23 stsp }
2282 e40622f4 2020-07-23 stsp err = got_canonpath(abspath, *link_target, PATH_MAX);
2283 e40622f4 2020-07-23 stsp free(abspath);
2284 e40622f4 2020-07-23 stsp if (err)
2285 e40622f4 2020-07-23 stsp goto done;
2286 e40622f4 2020-07-23 stsp }
2287 e40622f4 2020-07-23 stsp }
2288 e40622f4 2020-07-23 stsp done:
2289 e40622f4 2020-07-23 stsp free(tree_obj_id);
2290 e40622f4 2020-07-23 stsp if (tree)
2291 e40622f4 2020-07-23 stsp got_object_tree_close(tree);
2292 e40622f4 2020-07-23 stsp if (err) {
2293 e40622f4 2020-07-23 stsp free(*link_target);
2294 e40622f4 2020-07-23 stsp *link_target = NULL;
2295 e40622f4 2020-07-23 stsp }
2296 e40622f4 2020-07-23 stsp return err;
2297 ca6e02ac 2020-01-07 stsp }
2298 ca6e02ac 2020-01-07 stsp
2299 ca6e02ac 2020-01-07 stsp const struct got_error *
2300 e40622f4 2020-07-23 stsp got_object_resolve_symlinks(char **link_target, const char *path,
2301 945f9229 2022-04-16 thomas struct got_commit_object *commit, struct got_repository *repo)
2302 e40622f4 2020-07-23 stsp {
2303 e40622f4 2020-07-23 stsp const struct got_error *err = NULL;
2304 e40622f4 2020-07-23 stsp char *next_target = NULL;
2305 e40622f4 2020-07-23 stsp int max_recursion = 40; /* matches Git */
2306 e40622f4 2020-07-23 stsp
2307 e40622f4 2020-07-23 stsp *link_target = NULL;
2308 e40622f4 2020-07-23 stsp
2309 e40622f4 2020-07-23 stsp do {
2310 e40622f4 2020-07-23 stsp err = resolve_symlink(&next_target,
2311 945f9229 2022-04-16 thomas *link_target ? *link_target : path, commit, repo);
2312 e40622f4 2020-07-23 stsp if (err)
2313 e40622f4 2020-07-23 stsp break;
2314 e40622f4 2020-07-23 stsp if (next_target) {
2315 e40622f4 2020-07-23 stsp free(*link_target);
2316 e40622f4 2020-07-23 stsp if (--max_recursion == 0) {
2317 e40622f4 2020-07-23 stsp err = got_error_path(path, GOT_ERR_RECURSION);
2318 e40622f4 2020-07-23 stsp *link_target = NULL;
2319 e40622f4 2020-07-23 stsp break;
2320 e40622f4 2020-07-23 stsp }
2321 e40622f4 2020-07-23 stsp *link_target = next_target;
2322 e40622f4 2020-07-23 stsp }
2323 e40622f4 2020-07-23 stsp } while (next_target);
2324 e40622f4 2020-07-23 stsp
2325 e40622f4 2020-07-23 stsp return err;
2326 e40622f4 2020-07-23 stsp }
2327 e40622f4 2020-07-23 stsp
2328 e40622f4 2020-07-23 stsp const struct got_error *
2329 ca6e02ac 2020-01-07 stsp got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2330 ca6e02ac 2020-01-07 stsp struct got_object_id *commit_id, const char *path,
2331 ca6e02ac 2020-01-07 stsp struct got_repository *repo)
2332 ca6e02ac 2020-01-07 stsp {
2333 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
2334 ca6e02ac 2020-01-07 stsp struct got_pack *pack = NULL;
2335 ca6e02ac 2020-01-07 stsp struct got_packidx *packidx = NULL;
2336 ca6e02ac 2020-01-07 stsp char *path_packfile = NULL;
2337 ca6e02ac 2020-01-07 stsp struct got_commit_object *changed_commit = NULL;
2338 ca6e02ac 2020-01-07 stsp struct got_object_id *changed_commit_id = NULL;
2339 ca6e02ac 2020-01-07 stsp int idx;
2340 ca6e02ac 2020-01-07 stsp
2341 ca6e02ac 2020-01-07 stsp err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2342 ca6e02ac 2020-01-07 stsp if (err) {
2343 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
2344 ca6e02ac 2020-01-07 stsp return err;
2345 ca6e02ac 2020-01-07 stsp return NULL;
2346 ca6e02ac 2020-01-07 stsp }
2347 ca6e02ac 2020-01-07 stsp
2348 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
2349 aea75d87 2021-07-06 stsp packidx->path_packidx);
2350 ca6e02ac 2020-01-07 stsp if (err)
2351 ca6e02ac 2020-01-07 stsp return err;
2352 ca6e02ac 2020-01-07 stsp
2353 ca6e02ac 2020-01-07 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
2354 ca6e02ac 2020-01-07 stsp if (pack == NULL) {
2355 ca6e02ac 2020-01-07 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2356 ca6e02ac 2020-01-07 stsp if (err)
2357 ca6e02ac 2020-01-07 stsp goto done;
2358 ca6e02ac 2020-01-07 stsp }
2359 ca6e02ac 2020-01-07 stsp
2360 ca6e02ac 2020-01-07 stsp if (pack->privsep_child == NULL) {
2361 ca6e02ac 2020-01-07 stsp err = start_pack_privsep_child(pack, packidx);
2362 ca6e02ac 2020-01-07 stsp if (err)
2363 ca6e02ac 2020-01-07 stsp goto done;
2364 ca6e02ac 2020-01-07 stsp }
2365 ca6e02ac 2020-01-07 stsp
2366 ca6e02ac 2020-01-07 stsp err = got_privsep_send_commit_traversal_request(
2367 ca6e02ac 2020-01-07 stsp pack->privsep_child->ibuf, commit_id, idx, path);
2368 ca6e02ac 2020-01-07 stsp if (err)
2369 ca6e02ac 2020-01-07 stsp goto done;
2370 ca6e02ac 2020-01-07 stsp
2371 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_traversed_commits(&changed_commit,
2372 ca6e02ac 2020-01-07 stsp &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2373 ca6e02ac 2020-01-07 stsp if (err)
2374 ca6e02ac 2020-01-07 stsp goto done;
2375 ca6e02ac 2020-01-07 stsp
2376 ca6e02ac 2020-01-07 stsp if (changed_commit) {
2377 ca6e02ac 2020-01-07 stsp /*
2378 ca6e02ac 2020-01-07 stsp * Cache the commit in which the path was changed.
2379 ca6e02ac 2020-01-07 stsp * This commit might be opened again soon.
2380 ca6e02ac 2020-01-07 stsp */
2381 ca6e02ac 2020-01-07 stsp changed_commit->refcnt++;
2382 ca6e02ac 2020-01-07 stsp err = got_repo_cache_commit(repo, changed_commit_id,
2383 ca6e02ac 2020-01-07 stsp changed_commit);
2384 ca6e02ac 2020-01-07 stsp got_object_commit_close(changed_commit);
2385 ca6e02ac 2020-01-07 stsp }
2386 ca6e02ac 2020-01-07 stsp done:
2387 ca6e02ac 2020-01-07 stsp free(path_packfile);
2388 ca6e02ac 2020-01-07 stsp free(changed_commit_id);
2389 ca6e02ac 2020-01-07 stsp return err;
2390 ed175427 2019-05-09 stsp }