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