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