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