Blame


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