Blame


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