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 *qid = calloc(1, sizeof(**qid));
932 dbc6a6b6 2018-07-12 stsp if (*qid == NULL)
933 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
934 dbc6a6b6 2018-07-12 stsp
935 ec242592 2022-04-22 thomas memcpy(&(*qid)->id, id, sizeof((*qid)->id));
936 9ca9aafb 2021-06-18 stsp return NULL;
937 9ca9aafb 2021-06-18 stsp }
938 9ca9aafb 2021-06-18 stsp
939 9ca9aafb 2021-06-18 stsp const struct got_error *
940 9ca9aafb 2021-06-18 stsp got_object_id_queue_copy(const struct got_object_id_queue *src,
941 9ca9aafb 2021-06-18 stsp struct got_object_id_queue *dest)
942 9ca9aafb 2021-06-18 stsp {
943 9ca9aafb 2021-06-18 stsp const struct got_error *err;
944 9ca9aafb 2021-06-18 stsp struct got_object_qid *qid;
945 9ca9aafb 2021-06-18 stsp
946 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, src, entry) {
947 9ca9aafb 2021-06-18 stsp struct got_object_qid *new;
948 9ca9aafb 2021-06-18 stsp /*
949 9ca9aafb 2021-06-18 stsp * Deep-copy the object ID only. Let the caller deal
950 9ca9aafb 2021-06-18 stsp * with setting up the new->data pointer if needed.
951 9ca9aafb 2021-06-18 stsp */
952 ec242592 2022-04-22 thomas err = got_object_qid_alloc(&new, &qid->id);
953 9ca9aafb 2021-06-18 stsp if (err) {
954 9ca9aafb 2021-06-18 stsp got_object_id_queue_free(dest);
955 9ca9aafb 2021-06-18 stsp return err;
956 9ca9aafb 2021-06-18 stsp }
957 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(dest, new, entry);
958 dbc6a6b6 2018-07-12 stsp }
959 dbc6a6b6 2018-07-12 stsp
960 dbc6a6b6 2018-07-12 stsp return NULL;
961 a158c901 2018-12-23 stsp }
962 a158c901 2018-12-23 stsp
963 a158c901 2018-12-23 stsp static const struct got_error *
964 13c729f7 2018-12-24 stsp request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
965 13c729f7 2018-12-24 stsp int pack_idx, struct got_object_id *id)
966 a158c901 2018-12-23 stsp {
967 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
968 a158c901 2018-12-23 stsp
969 13c729f7 2018-12-24 stsp err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
970 13c729f7 2018-12-24 stsp pack_idx);
971 a158c901 2018-12-23 stsp if (err)
972 a158c901 2018-12-23 stsp return err;
973 a158c901 2018-12-23 stsp
974 a158c901 2018-12-23 stsp return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
975 7e212e3d 2018-09-09 stsp }
976 7e212e3d 2018-09-09 stsp
977 71eb0e7f 2018-09-16 stsp static const struct got_error *
978 13c729f7 2018-12-24 stsp read_packed_tree_privsep(struct got_tree_object **tree,
979 13c729f7 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
980 13c729f7 2018-12-24 stsp struct got_object_id *id)
981 13c729f7 2018-12-24 stsp {
982 13c729f7 2018-12-24 stsp const struct got_error *err = NULL;
983 13c729f7 2018-12-24 stsp
984 13c729f7 2018-12-24 stsp if (pack->privsep_child)
985 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
986 13c729f7 2018-12-24 stsp
987 13c729f7 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
988 13c729f7 2018-12-24 stsp if (err)
989 13c729f7 2018-12-24 stsp return err;
990 13c729f7 2018-12-24 stsp
991 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
992 13c729f7 2018-12-24 stsp }
993 13c729f7 2018-12-24 stsp
994 13c729f7 2018-12-24 stsp static const struct got_error *
995 9f2369b0 2018-12-24 stsp request_tree(struct got_tree_object **tree, struct got_repository *repo,
996 d5c81d44 2021-07-08 stsp int fd, struct got_object_id *id)
997 9f2369b0 2018-12-24 stsp {
998 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
999 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1000 9f2369b0 2018-12-24 stsp
1001 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
1002 9f2369b0 2018-12-24 stsp
1003 d5c81d44 2021-07-08 stsp err = got_privsep_send_tree_req(ibuf, fd, id, -1);
1004 9f2369b0 2018-12-24 stsp if (err)
1005 9f2369b0 2018-12-24 stsp return err;
1006 9f2369b0 2018-12-24 stsp
1007 9f2369b0 2018-12-24 stsp return got_privsep_recv_tree(tree, ibuf);
1008 9f2369b0 2018-12-24 stsp }
1009 9f2369b0 2018-12-24 stsp
1010 9f2369b0 2018-12-24 stsp const struct got_error *
1011 9f2369b0 2018-12-24 stsp read_tree_privsep(struct got_tree_object **tree, int obj_fd,
1012 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo)
1013 9f2369b0 2018-12-24 stsp {
1014 ddc7b220 2019-09-08 stsp const struct got_error *err;
1015 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1016 9f2369b0 2018-12-24 stsp pid_t pid;
1017 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1018 9f2369b0 2018-12-24 stsp
1019 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
1020 d5c81d44 2021-07-08 stsp return request_tree(tree, repo, obj_fd, id);
1021 9f2369b0 2018-12-24 stsp
1022 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1023 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1024 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1025 9f2369b0 2018-12-24 stsp
1026 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1027 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1028 ddc7b220 2019-09-08 stsp free(ibuf);
1029 ddc7b220 2019-09-08 stsp return err;
1030 ddc7b220 2019-09-08 stsp }
1031 9f2369b0 2018-12-24 stsp
1032 9f2369b0 2018-12-24 stsp pid = fork();
1033 ddc7b220 2019-09-08 stsp if (pid == -1) {
1034 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1035 ddc7b220 2019-09-08 stsp free(ibuf);
1036 ddc7b220 2019-09-08 stsp return err;
1037 ddc7b220 2019-09-08 stsp }
1038 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1039 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1040 9f2369b0 2018-12-24 stsp repo->path);
1041 9f2369b0 2018-12-24 stsp /* not reached */
1042 9f2369b0 2018-12-24 stsp }
1043 9f2369b0 2018-12-24 stsp
1044 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
1045 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1046 ddc7b220 2019-09-08 stsp free(ibuf);
1047 ddc7b220 2019-09-08 stsp return err;
1048 ddc7b220 2019-09-08 stsp }
1049 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1050 9f2369b0 2018-12-24 stsp imsg_fds[0];
1051 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1052 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1053 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1054 9f2369b0 2018-12-24 stsp
1055 9f2369b0 2018-12-24 stsp
1056 d5c81d44 2021-07-08 stsp return request_tree(tree, repo, obj_fd, id);
1057 9f2369b0 2018-12-24 stsp }
1058 9f2369b0 2018-12-24 stsp
1059 9f2369b0 2018-12-24 stsp static const struct got_error *
1060 13c729f7 2018-12-24 stsp open_tree(struct got_tree_object **tree, struct got_repository *repo,
1061 13c729f7 2018-12-24 stsp struct got_object_id *id, int check_cache)
1062 0ffeb3c2 2017-11-26 stsp {
1063 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
1064 13c729f7 2018-12-24 stsp struct got_packidx *packidx = NULL;
1065 e82b1d81 2019-07-27 stsp int idx;
1066 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1067 f6be5c30 2018-06-22 stsp
1068 71eb0e7f 2018-09-16 stsp if (check_cache) {
1069 13c729f7 2018-12-24 stsp *tree = got_repo_get_cached_tree(repo, id);
1070 71eb0e7f 2018-09-16 stsp if (*tree != NULL) {
1071 71eb0e7f 2018-09-16 stsp (*tree)->refcnt++;
1072 71eb0e7f 2018-09-16 stsp return NULL;
1073 71eb0e7f 2018-09-16 stsp }
1074 71eb0e7f 2018-09-16 stsp } else
1075 71eb0e7f 2018-09-16 stsp *tree = NULL;
1076 0ffeb3c2 2017-11-26 stsp
1077 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1078 e82b1d81 2019-07-27 stsp if (err == NULL) {
1079 13c729f7 2018-12-24 stsp struct got_pack *pack = NULL;
1080 0ffeb3c2 2017-11-26 stsp
1081 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
1082 aea75d87 2021-07-06 stsp packidx->path_packidx);
1083 13c729f7 2018-12-24 stsp if (err)
1084 13c729f7 2018-12-24 stsp return err;
1085 13c729f7 2018-12-24 stsp
1086 13c729f7 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1087 e7885405 2018-09-10 stsp if (pack == NULL) {
1088 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1089 e82b1d81 2019-07-27 stsp packidx);
1090 e7885405 2018-09-10 stsp if (err)
1091 8d2c5ea3 2019-08-13 stsp goto done;
1092 e7885405 2018-09-10 stsp }
1093 13c729f7 2018-12-24 stsp err = read_packed_tree_privsep(tree, pack,
1094 13c729f7 2018-12-24 stsp packidx, idx, id);
1095 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1096 e82b1d81 2019-07-27 stsp int fd;
1097 e82b1d81 2019-07-27 stsp
1098 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1099 e82b1d81 2019-07-27 stsp if (err)
1100 e82b1d81 2019-07-27 stsp return err;
1101 d5c81d44 2021-07-08 stsp err = read_tree_privsep(tree, fd, id, repo);
1102 e82b1d81 2019-07-27 stsp }
1103 f6be5c30 2018-06-22 stsp
1104 f6be5c30 2018-06-22 stsp if (err == NULL) {
1105 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
1106 13c729f7 2018-12-24 stsp err = got_repo_cache_tree(repo, id, *tree);
1107 f6be5c30 2018-06-22 stsp }
1108 8d2c5ea3 2019-08-13 stsp done:
1109 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1110 776d4d29 2018-06-17 stsp return err;
1111 776d4d29 2018-06-17 stsp }
1112 776d4d29 2018-06-17 stsp
1113 776d4d29 2018-06-17 stsp const struct got_error *
1114 776d4d29 2018-06-17 stsp got_object_open_as_tree(struct got_tree_object **tree,
1115 776d4d29 2018-06-17 stsp struct got_repository *repo, struct got_object_id *id)
1116 776d4d29 2018-06-17 stsp {
1117 e8eb494a 2018-09-16 stsp *tree = got_repo_get_cached_tree(repo, id);
1118 e8eb494a 2018-09-16 stsp if (*tree != NULL) {
1119 e8eb494a 2018-09-16 stsp (*tree)->refcnt++;
1120 e8eb494a 2018-09-16 stsp return NULL;
1121 e0ab43e7 2018-03-16 stsp }
1122 776d4d29 2018-06-17 stsp
1123 13c729f7 2018-12-24 stsp return open_tree(tree, repo, id, 0);
1124 57efb1af 2018-04-24 stsp }
1125 ff6b18f8 2018-04-24 stsp
1126 71eb0e7f 2018-09-16 stsp const struct got_error *
1127 71eb0e7f 2018-09-16 stsp got_object_tree_open(struct got_tree_object **tree,
1128 71eb0e7f 2018-09-16 stsp struct got_repository *repo, struct got_object *obj)
1129 71eb0e7f 2018-09-16 stsp {
1130 13c729f7 2018-12-24 stsp return open_tree(tree, repo, got_object_get_id(obj), 1);
1131 71eb0e7f 2018-09-16 stsp }
1132 71eb0e7f 2018-09-16 stsp
1133 56e0773d 2019-11-28 stsp int
1134 56e0773d 2019-11-28 stsp got_object_tree_get_nentries(struct got_tree_object *tree)
1135 883f0469 2018-06-23 stsp {
1136 56e0773d 2019-11-28 stsp return tree->nentries;
1137 56e0773d 2019-11-28 stsp }
1138 56e0773d 2019-11-28 stsp
1139 56e0773d 2019-11-28 stsp struct got_tree_entry *
1140 56e0773d 2019-11-28 stsp got_object_tree_get_first_entry(struct got_tree_object *tree)
1141 56e0773d 2019-11-28 stsp {
1142 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, 0);
1143 56e0773d 2019-11-28 stsp }
1144 56e0773d 2019-11-28 stsp
1145 56e0773d 2019-11-28 stsp struct got_tree_entry *
1146 56e0773d 2019-11-28 stsp got_object_tree_get_last_entry(struct got_tree_object *tree)
1147 56e0773d 2019-11-28 stsp {
1148 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, tree->nentries - 1);
1149 56e0773d 2019-11-28 stsp }
1150 56e0773d 2019-11-28 stsp
1151 56e0773d 2019-11-28 stsp struct got_tree_entry *
1152 56e0773d 2019-11-28 stsp got_object_tree_get_entry(struct got_tree_object *tree, int i)
1153 56e0773d 2019-11-28 stsp {
1154 56e0773d 2019-11-28 stsp if (i < 0 || i >= tree->nentries)
1155 56e0773d 2019-11-28 stsp return NULL;
1156 56e0773d 2019-11-28 stsp return &tree->entries[i];
1157 883f0469 2018-06-23 stsp }
1158 3840f4c9 2018-09-12 stsp
1159 56e0773d 2019-11-28 stsp mode_t
1160 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(struct got_tree_entry *te)
1161 56e0773d 2019-11-28 stsp {
1162 56e0773d 2019-11-28 stsp return te->mode;
1163 56e0773d 2019-11-28 stsp }
1164 56e0773d 2019-11-28 stsp
1165 56e0773d 2019-11-28 stsp const char *
1166 56e0773d 2019-11-28 stsp got_tree_entry_get_name(struct got_tree_entry *te)
1167 56e0773d 2019-11-28 stsp {
1168 56e0773d 2019-11-28 stsp return &te->name[0];
1169 56e0773d 2019-11-28 stsp }
1170 56e0773d 2019-11-28 stsp
1171 56e0773d 2019-11-28 stsp struct got_object_id *
1172 56e0773d 2019-11-28 stsp got_tree_entry_get_id(struct got_tree_entry *te)
1173 56e0773d 2019-11-28 stsp {
1174 56e0773d 2019-11-28 stsp return &te->id;
1175 0d6c6ee3 2020-05-20 stsp }
1176 0d6c6ee3 2020-05-20 stsp
1177 0d6c6ee3 2020-05-20 stsp const struct got_error *
1178 af57b12a 2020-07-23 stsp got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1179 0d6c6ee3 2020-05-20 stsp {
1180 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
1181 32596e16 2020-07-23 stsp size_t len, totlen, hdrlen, offset;
1182 aa092692 2020-07-23 stsp
1183 aa092692 2020-07-23 stsp *s = NULL;
1184 0d6c6ee3 2020-05-20 stsp
1185 659dc16e 2020-07-23 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1186 659dc16e 2020-07-23 stsp totlen = 0;
1187 32596e16 2020-07-23 stsp offset = 0;
1188 659dc16e 2020-07-23 stsp do {
1189 659dc16e 2020-07-23 stsp char *p;
1190 0d6c6ee3 2020-05-20 stsp
1191 659dc16e 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1192 659dc16e 2020-07-23 stsp if (err)
1193 af57b12a 2020-07-23 stsp return err;
1194 659dc16e 2020-07-23 stsp
1195 659dc16e 2020-07-23 stsp if (len == 0)
1196 659dc16e 2020-07-23 stsp break;
1197 659dc16e 2020-07-23 stsp
1198 659dc16e 2020-07-23 stsp totlen += len - hdrlen;
1199 af57b12a 2020-07-23 stsp p = realloc(*s, totlen + 1);
1200 659dc16e 2020-07-23 stsp if (p == NULL) {
1201 659dc16e 2020-07-23 stsp err = got_error_from_errno("realloc");
1202 af57b12a 2020-07-23 stsp free(*s);
1203 af57b12a 2020-07-23 stsp *s = NULL;
1204 af57b12a 2020-07-23 stsp return err;
1205 659dc16e 2020-07-23 stsp }
1206 af57b12a 2020-07-23 stsp *s = p;
1207 659dc16e 2020-07-23 stsp /* Skip blob object header first time around. */
1208 af57b12a 2020-07-23 stsp memcpy(*s + offset,
1209 f8f7c882 2020-07-23 stsp got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1210 659dc16e 2020-07-23 stsp hdrlen = 0;
1211 32596e16 2020-07-23 stsp offset = totlen;
1212 659dc16e 2020-07-23 stsp } while (len > 0);
1213 af57b12a 2020-07-23 stsp
1214 af57b12a 2020-07-23 stsp (*s)[totlen] = '\0';
1215 af57b12a 2020-07-23 stsp return NULL;
1216 af57b12a 2020-07-23 stsp }
1217 af57b12a 2020-07-23 stsp
1218 af57b12a 2020-07-23 stsp const struct got_error *
1219 af57b12a 2020-07-23 stsp got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1220 af57b12a 2020-07-23 stsp struct got_repository *repo)
1221 af57b12a 2020-07-23 stsp {
1222 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
1223 af57b12a 2020-07-23 stsp struct got_blob_object *blob = NULL;
1224 af57b12a 2020-07-23 stsp
1225 af57b12a 2020-07-23 stsp *link_target = NULL;
1226 af57b12a 2020-07-23 stsp
1227 af57b12a 2020-07-23 stsp if (!got_object_tree_entry_is_symlink(te))
1228 af57b12a 2020-07-23 stsp return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1229 af57b12a 2020-07-23 stsp
1230 af57b12a 2020-07-23 stsp err = got_object_open_as_blob(&blob, repo,
1231 af57b12a 2020-07-23 stsp got_tree_entry_get_id(te), PATH_MAX);
1232 af57b12a 2020-07-23 stsp if (err)
1233 af57b12a 2020-07-23 stsp return err;
1234 af57b12a 2020-07-23 stsp
1235 af57b12a 2020-07-23 stsp err = got_object_blob_read_to_str(link_target, blob);
1236 af57b12a 2020-07-23 stsp got_object_blob_close(blob);
1237 659dc16e 2020-07-23 stsp if (err) {
1238 659dc16e 2020-07-23 stsp free(*link_target);
1239 659dc16e 2020-07-23 stsp *link_target = NULL;
1240 659dc16e 2020-07-23 stsp }
1241 0d6c6ee3 2020-05-20 stsp return err;
1242 56e0773d 2019-11-28 stsp }
1243 56e0773d 2019-11-28 stsp
1244 56e0773d 2019-11-28 stsp int
1245 56e0773d 2019-11-28 stsp got_tree_entry_get_index(struct got_tree_entry *te)
1246 56e0773d 2019-11-28 stsp {
1247 56e0773d 2019-11-28 stsp return te->idx;
1248 56e0773d 2019-11-28 stsp }
1249 56e0773d 2019-11-28 stsp
1250 56e0773d 2019-11-28 stsp struct got_tree_entry *
1251 56e0773d 2019-11-28 stsp got_tree_entry_get_next(struct got_tree_object *tree,
1252 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
1253 56e0773d 2019-11-28 stsp {
1254 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx + 1);
1255 56e0773d 2019-11-28 stsp }
1256 56e0773d 2019-11-28 stsp
1257 56e0773d 2019-11-28 stsp struct got_tree_entry *
1258 56e0773d 2019-11-28 stsp got_tree_entry_get_prev(struct got_tree_object *tree,
1259 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
1260 56e0773d 2019-11-28 stsp {
1261 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx - 1);
1262 56e0773d 2019-11-28 stsp }
1263 56e0773d 2019-11-28 stsp
1264 3840f4c9 2018-09-12 stsp static const struct got_error *
1265 ac544f8c 2019-01-13 stsp request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1266 ebc55e2d 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1267 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
1268 3840f4c9 2018-09-12 stsp {
1269 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
1270 bc1f382f 2022-01-05 thomas struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1271 bc1f382f 2022-01-05 thomas int outfd_child;
1272 24140570 2018-09-09 stsp
1273 bc1f382f 2022-01-05 thomas err = pack_child_send_tempfiles(ibuf, pack);
1274 bc1f382f 2022-01-05 thomas if (err)
1275 bc1f382f 2022-01-05 thomas return err;
1276 3840f4c9 2018-09-12 stsp
1277 3840f4c9 2018-09-12 stsp outfd_child = dup(outfd);
1278 3840f4c9 2018-09-12 stsp if (outfd_child == -1)
1279 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1280 3840f4c9 2018-09-12 stsp
1281 ebc55e2d 2018-12-24 stsp err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1282 3840f4c9 2018-09-12 stsp if (err)
1283 3840f4c9 2018-09-12 stsp return err;
1284 3840f4c9 2018-09-12 stsp
1285 3840f4c9 2018-09-12 stsp err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1286 3840f4c9 2018-09-12 stsp outfd_child);
1287 3840f4c9 2018-09-12 stsp if (err) {
1288 3840f4c9 2018-09-12 stsp return err;
1289 3840f4c9 2018-09-12 stsp }
1290 3840f4c9 2018-09-12 stsp
1291 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen,
1292 ebc55e2d 2018-12-24 stsp pack->privsep_child->ibuf);
1293 3840f4c9 2018-09-12 stsp if (err)
1294 3840f4c9 2018-09-12 stsp return err;
1295 3840f4c9 2018-09-12 stsp
1296 3840f4c9 2018-09-12 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1297 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1298 3840f4c9 2018-09-12 stsp
1299 3840f4c9 2018-09-12 stsp return err;
1300 3840f4c9 2018-09-12 stsp }
1301 3840f4c9 2018-09-12 stsp
1302 ebc55e2d 2018-12-24 stsp static const struct got_error *
1303 ac544f8c 2019-01-13 stsp read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1304 ac544f8c 2019-01-13 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1305 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
1306 68482ea3 2017-11-27 stsp {
1307 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
1308 ebc55e2d 2018-12-24 stsp
1309 ebc55e2d 2018-12-24 stsp if (pack->privsep_child == NULL) {
1310 ebc55e2d 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
1311 ebc55e2d 2018-12-24 stsp if (err)
1312 ebc55e2d 2018-12-24 stsp return err;
1313 ebc55e2d 2018-12-24 stsp }
1314 68482ea3 2017-11-27 stsp
1315 ac544f8c 2019-01-13 stsp return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1316 ac544f8c 2019-01-13 stsp idx, id);
1317 9f2369b0 2018-12-24 stsp }
1318 9f2369b0 2018-12-24 stsp
1319 9f2369b0 2018-12-24 stsp static const struct got_error *
1320 ac544f8c 2019-01-13 stsp request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1321 d5c81d44 2021-07-08 stsp int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1322 9f2369b0 2018-12-24 stsp {
1323 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1324 9f2369b0 2018-12-24 stsp int outfd_child;
1325 9f2369b0 2018-12-24 stsp
1326 9f2369b0 2018-12-24 stsp outfd_child = dup(outfd);
1327 9f2369b0 2018-12-24 stsp if (outfd_child == -1)
1328 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1329 9f2369b0 2018-12-24 stsp
1330 d5c81d44 2021-07-08 stsp err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1331 9f2369b0 2018-12-24 stsp if (err)
1332 9f2369b0 2018-12-24 stsp return err;
1333 9f2369b0 2018-12-24 stsp
1334 9f2369b0 2018-12-24 stsp err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1335 41496140 2019-02-21 stsp if (err)
1336 9f2369b0 2018-12-24 stsp return err;
1337 9f2369b0 2018-12-24 stsp
1338 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1339 9f2369b0 2018-12-24 stsp if (err)
1340 9f2369b0 2018-12-24 stsp return err;
1341 9f2369b0 2018-12-24 stsp
1342 9f2369b0 2018-12-24 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1343 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
1344 9f2369b0 2018-12-24 stsp
1345 9f2369b0 2018-12-24 stsp return err;
1346 9f2369b0 2018-12-24 stsp }
1347 9f2369b0 2018-12-24 stsp
1348 9f2369b0 2018-12-24 stsp static const struct got_error *
1349 ac544f8c 2019-01-13 stsp read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1350 d5c81d44 2021-07-08 stsp int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1351 9f2369b0 2018-12-24 stsp {
1352 ddc7b220 2019-09-08 stsp const struct got_error *err;
1353 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1354 9f2369b0 2018-12-24 stsp pid_t pid;
1355 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1356 9f2369b0 2018-12-24 stsp
1357 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1358 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1359 d5c81d44 2021-07-08 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1360 d5c81d44 2021-07-08 stsp ibuf);
1361 9f2369b0 2018-12-24 stsp }
1362 9f2369b0 2018-12-24 stsp
1363 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1364 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1365 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1366 9f2369b0 2018-12-24 stsp
1367 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1368 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1369 ddc7b220 2019-09-08 stsp free(ibuf);
1370 ddc7b220 2019-09-08 stsp return err;
1371 ddc7b220 2019-09-08 stsp }
1372 9f2369b0 2018-12-24 stsp
1373 9f2369b0 2018-12-24 stsp pid = fork();
1374 ddc7b220 2019-09-08 stsp if (pid == -1) {
1375 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1376 ddc7b220 2019-09-08 stsp free(ibuf);
1377 ddc7b220 2019-09-08 stsp return err;
1378 ddc7b220 2019-09-08 stsp }
1379 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1380 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1381 9f2369b0 2018-12-24 stsp repo->path);
1382 9f2369b0 2018-12-24 stsp /* not reached */
1383 9f2369b0 2018-12-24 stsp }
1384 9f2369b0 2018-12-24 stsp
1385 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
1386 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1387 ddc7b220 2019-09-08 stsp free(ibuf);
1388 ddc7b220 2019-09-08 stsp return err;
1389 ddc7b220 2019-09-08 stsp }
1390 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1391 9f2369b0 2018-12-24 stsp imsg_fds[0];
1392 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1393 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1394 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1395 9f2369b0 2018-12-24 stsp
1396 d5c81d44 2021-07-08 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1397 ebc55e2d 2018-12-24 stsp }
1398 68482ea3 2017-11-27 stsp
1399 ebc55e2d 2018-12-24 stsp static const struct got_error *
1400 ebc55e2d 2018-12-24 stsp open_blob(struct got_blob_object **blob, struct got_repository *repo,
1401 ebc55e2d 2018-12-24 stsp struct got_object_id *id, size_t blocksize)
1402 ebc55e2d 2018-12-24 stsp {
1403 ebc55e2d 2018-12-24 stsp const struct got_error *err = NULL;
1404 ebc55e2d 2018-12-24 stsp struct got_packidx *packidx = NULL;
1405 ebc55e2d 2018-12-24 stsp int idx;
1406 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1407 ac544f8c 2019-01-13 stsp uint8_t *outbuf;
1408 e82b1d81 2019-07-27 stsp int outfd;
1409 ebc55e2d 2018-12-24 stsp size_t size, hdrlen;
1410 ebc55e2d 2018-12-24 stsp struct stat sb;
1411 ebc55e2d 2018-12-24 stsp
1412 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
1413 4558fcd4 2018-01-14 stsp if (*blob == NULL)
1414 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1415 68482ea3 2017-11-27 stsp
1416 55da3778 2018-09-10 stsp outfd = got_opentempfd();
1417 55da3778 2018-09-10 stsp if (outfd == -1)
1418 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
1419 55da3778 2018-09-10 stsp
1420 062ebb78 2018-07-12 stsp (*blob)->read_buf = malloc(blocksize);
1421 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
1422 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1423 c7254d79 2018-04-24 stsp goto done;
1424 15c8b0e6 2018-04-24 stsp }
1425 ebc55e2d 2018-12-24 stsp
1426 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1427 e82b1d81 2019-07-27 stsp if (err == NULL) {
1428 ebc55e2d 2018-12-24 stsp struct got_pack *pack = NULL;
1429 34f480ff 2019-07-27 stsp
1430 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
1431 aea75d87 2021-07-06 stsp packidx->path_packidx);
1432 ebc55e2d 2018-12-24 stsp if (err)
1433 ebc55e2d 2018-12-24 stsp goto done;
1434 ebc55e2d 2018-12-24 stsp
1435 ebc55e2d 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1436 55da3778 2018-09-10 stsp if (pack == NULL) {
1437 ebc55e2d 2018-12-24 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1438 ebc55e2d 2018-12-24 stsp packidx);
1439 55da3778 2018-09-10 stsp if (err)
1440 55da3778 2018-09-10 stsp goto done;
1441 55da3778 2018-09-10 stsp }
1442 ac544f8c 2019-01-13 stsp err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1443 ac544f8c 2019-01-13 stsp pack, packidx, idx, id);
1444 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1445 e82b1d81 2019-07-27 stsp int infd;
1446 e82b1d81 2019-07-27 stsp
1447 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&infd, id, repo);
1448 e82b1d81 2019-07-27 stsp if (err)
1449 e82b1d81 2019-07-27 stsp goto done;
1450 ac544f8c 2019-01-13 stsp err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1451 d5c81d44 2021-07-08 stsp id, repo);
1452 e82b1d81 2019-07-27 stsp }
1453 ebc55e2d 2018-12-24 stsp if (err)
1454 55da3778 2018-09-10 stsp goto done;
1455 2967a784 2018-04-24 stsp
1456 de060dff 2018-12-24 stsp if (hdrlen > size) {
1457 ebc55e2d 2018-12-24 stsp err = got_error(GOT_ERR_BAD_OBJ_HDR);
1458 ebc55e2d 2018-12-24 stsp goto done;
1459 ebc55e2d 2018-12-24 stsp }
1460 ebc55e2d 2018-12-24 stsp
1461 ac544f8c 2019-01-13 stsp if (outbuf) {
1462 08578a35 2021-01-22 stsp if (close(outfd) == -1 && err == NULL)
1463 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1464 ac544f8c 2019-01-13 stsp outfd = -1;
1465 ac544f8c 2019-01-13 stsp (*blob)->f = fmemopen(outbuf, size, "rb");
1466 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1467 638f9024 2019-05-13 stsp err = got_error_from_errno("fmemopen");
1468 ac544f8c 2019-01-13 stsp free(outbuf);
1469 ac544f8c 2019-01-13 stsp goto done;
1470 ac544f8c 2019-01-13 stsp }
1471 ac544f8c 2019-01-13 stsp (*blob)->data = outbuf;
1472 ac544f8c 2019-01-13 stsp } else {
1473 ac544f8c 2019-01-13 stsp if (fstat(outfd, &sb) == -1) {
1474 638f9024 2019-05-13 stsp err = got_error_from_errno("fstat");
1475 ac544f8c 2019-01-13 stsp goto done;
1476 ac544f8c 2019-01-13 stsp }
1477 ac544f8c 2019-01-13 stsp
1478 ac544f8c 2019-01-13 stsp if (sb.st_size != size) {
1479 ac544f8c 2019-01-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1480 ac544f8c 2019-01-13 stsp goto done;
1481 ac544f8c 2019-01-13 stsp }
1482 ac544f8c 2019-01-13 stsp
1483 ac544f8c 2019-01-13 stsp (*blob)->f = fdopen(outfd, "rb");
1484 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1485 638f9024 2019-05-13 stsp err = got_error_from_errno("fdopen");
1486 ac544f8c 2019-01-13 stsp close(outfd);
1487 ac544f8c 2019-01-13 stsp outfd = -1;
1488 ac544f8c 2019-01-13 stsp goto done;
1489 ac544f8c 2019-01-13 stsp }
1490 68482ea3 2017-11-27 stsp }
1491 68482ea3 2017-11-27 stsp
1492 ebc55e2d 2018-12-24 stsp (*blob)->hdrlen = hdrlen;
1493 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
1494 ebc55e2d 2018-12-24 stsp memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1495 7d283eee 2017-11-29 stsp
1496 c7254d79 2018-04-24 stsp done:
1497 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1498 55da3778 2018-09-10 stsp if (err) {
1499 55da3778 2018-09-10 stsp if (*blob) {
1500 7baf5860 2019-03-19 stsp got_object_blob_close(*blob);
1501 55da3778 2018-09-10 stsp *blob = NULL;
1502 55da3778 2018-09-10 stsp } else if (outfd != -1)
1503 55da3778 2018-09-10 stsp close(outfd);
1504 a19581a2 2018-06-21 stsp }
1505 a19581a2 2018-06-21 stsp return err;
1506 a19581a2 2018-06-21 stsp }
1507 a19581a2 2018-06-21 stsp
1508 a19581a2 2018-06-21 stsp const struct got_error *
1509 a19581a2 2018-06-21 stsp got_object_open_as_blob(struct got_blob_object **blob,
1510 a19581a2 2018-06-21 stsp struct got_repository *repo, struct got_object_id *id,
1511 a19581a2 2018-06-21 stsp size_t blocksize)
1512 a19581a2 2018-06-21 stsp {
1513 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, id, blocksize);
1514 ebc55e2d 2018-12-24 stsp }
1515 835e0dbd 2018-06-21 stsp
1516 ebc55e2d 2018-12-24 stsp const struct got_error *
1517 ebc55e2d 2018-12-24 stsp got_object_blob_open(struct got_blob_object **blob,
1518 ebc55e2d 2018-12-24 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
1519 ebc55e2d 2018-12-24 stsp {
1520 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1521 0ffeb3c2 2017-11-26 stsp }
1522 68482ea3 2017-11-27 stsp
1523 fb43ecf1 2019-02-11 stsp const struct got_error *
1524 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
1525 68482ea3 2017-11-27 stsp {
1526 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL;
1527 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
1528 56b63ca4 2021-01-22 stsp if (blob->f && fclose(blob->f) == EOF)
1529 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1530 ac544f8c 2019-01-13 stsp free(blob->data);
1531 68482ea3 2017-11-27 stsp free(blob);
1532 fb43ecf1 2019-02-11 stsp return err;
1533 f934cf2c 2018-02-12 stsp }
1534 f934cf2c 2018-02-12 stsp
1535 8ba819a3 2020-07-23 stsp void
1536 8ba819a3 2020-07-23 stsp got_object_blob_rewind(struct got_blob_object *blob)
1537 8ba819a3 2020-07-23 stsp {
1538 8ba819a3 2020-07-23 stsp if (blob->f)
1539 8ba819a3 2020-07-23 stsp rewind(blob->f);
1540 8ba819a3 2020-07-23 stsp }
1541 8ba819a3 2020-07-23 stsp
1542 f934cf2c 2018-02-12 stsp char *
1543 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1544 f934cf2c 2018-02-12 stsp {
1545 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1546 f934cf2c 2018-02-12 stsp }
1547 f934cf2c 2018-02-12 stsp
1548 f934cf2c 2018-02-12 stsp size_t
1549 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
1550 f934cf2c 2018-02-12 stsp {
1551 f934cf2c 2018-02-12 stsp return blob->hdrlen;
1552 68482ea3 2017-11-27 stsp }
1553 68482ea3 2017-11-27 stsp
1554 f934cf2c 2018-02-12 stsp const uint8_t *
1555 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
1556 f934cf2c 2018-02-12 stsp {
1557 f934cf2c 2018-02-12 stsp return blob->read_buf;
1558 f934cf2c 2018-02-12 stsp }
1559 f934cf2c 2018-02-12 stsp
1560 68482ea3 2017-11-27 stsp const struct got_error *
1561 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1562 68482ea3 2017-11-27 stsp {
1563 eb651edf 2018-02-11 stsp size_t n;
1564 eb651edf 2018-02-11 stsp
1565 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1566 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
1567 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
1568 eb651edf 2018-02-11 stsp *outlenp = n;
1569 35e9ba5d 2018-06-21 stsp return NULL;
1570 35e9ba5d 2018-06-21 stsp }
1571 35e9ba5d 2018-06-21 stsp
1572 35e9ba5d 2018-06-21 stsp const struct got_error *
1573 be659d10 2020-11-18 stsp got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1574 6c4c42e0 2019-06-24 stsp off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1575 35e9ba5d 2018-06-21 stsp {
1576 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
1577 b6752625 2018-12-24 stsp size_t n, len, hdrlen;
1578 84451b3e 2018-07-10 stsp const uint8_t *buf;
1579 84451b3e 2018-07-10 stsp int i;
1580 c33ebc60 2020-11-18 stsp const int alloc_chunksz = 512;
1581 c33ebc60 2020-11-18 stsp size_t nalloc = 0;
1582 f595d9bd 2019-08-14 stsp off_t off = 0, total_len = 0;
1583 84451b3e 2018-07-10 stsp
1584 6c4c42e0 2019-06-24 stsp if (line_offsets)
1585 6c4c42e0 2019-06-24 stsp *line_offsets = NULL;
1586 f595d9bd 2019-08-14 stsp if (filesize)
1587 f595d9bd 2019-08-14 stsp *filesize = 0;
1588 84451b3e 2018-07-10 stsp if (nlines)
1589 84451b3e 2018-07-10 stsp *nlines = 0;
1590 35e9ba5d 2018-06-21 stsp
1591 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1592 35e9ba5d 2018-06-21 stsp do {
1593 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
1594 35e9ba5d 2018-06-21 stsp if (err)
1595 35e9ba5d 2018-06-21 stsp return err;
1596 35e9ba5d 2018-06-21 stsp if (len == 0)
1597 35e9ba5d 2018-06-21 stsp break;
1598 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
1599 b02560ec 2019-08-19 stsp i = hdrlen;
1600 f1cbc3bc 2020-11-18 stsp if (nlines) {
1601 f1cbc3bc 2020-11-18 stsp if (line_offsets && *line_offsets == NULL) {
1602 78695fb7 2019-08-12 stsp /* Have some data but perhaps no '\n'. */
1603 78695fb7 2019-08-12 stsp *nlines = 1;
1604 c33ebc60 2020-11-18 stsp nalloc = alloc_chunksz;
1605 c33ebc60 2020-11-18 stsp *line_offsets = calloc(nalloc,
1606 c33ebc60 2020-11-18 stsp sizeof(**line_offsets));
1607 78695fb7 2019-08-12 stsp if (*line_offsets == NULL)
1608 845785d4 2020-02-02 tracey return got_error_from_errno("calloc");
1609 b02560ec 2019-08-19 stsp
1610 b02560ec 2019-08-19 stsp /* Skip forward over end of first line. */
1611 b02560ec 2019-08-19 stsp while (i < len) {
1612 b02560ec 2019-08-19 stsp if (buf[i] == '\n')
1613 b02560ec 2019-08-19 stsp break;
1614 b02560ec 2019-08-19 stsp i++;
1615 b02560ec 2019-08-19 stsp }
1616 b02560ec 2019-08-19 stsp }
1617 b02560ec 2019-08-19 stsp /* Scan '\n' offsets in remaining chunk of data. */
1618 b02560ec 2019-08-19 stsp while (i < len) {
1619 b02560ec 2019-08-19 stsp if (buf[i] != '\n') {
1620 b02560ec 2019-08-19 stsp i++;
1621 f595d9bd 2019-08-14 stsp continue;
1622 b02560ec 2019-08-19 stsp }
1623 f595d9bd 2019-08-14 stsp (*nlines)++;
1624 c33ebc60 2020-11-18 stsp if (line_offsets && nalloc < *nlines) {
1625 c33ebc60 2020-11-18 stsp size_t n = *nlines + alloc_chunksz;
1626 78695fb7 2019-08-12 stsp off_t *o = recallocarray(*line_offsets,
1627 c33ebc60 2020-11-18 stsp nalloc, n, sizeof(**line_offsets));
1628 78695fb7 2019-08-12 stsp if (o == NULL) {
1629 78695fb7 2019-08-12 stsp free(*line_offsets);
1630 78695fb7 2019-08-12 stsp *line_offsets = NULL;
1631 78695fb7 2019-08-12 stsp return got_error_from_errno(
1632 78695fb7 2019-08-12 stsp "recallocarray");
1633 78695fb7 2019-08-12 stsp }
1634 78695fb7 2019-08-12 stsp *line_offsets = o;
1635 c33ebc60 2020-11-18 stsp nalloc = n;
1636 78695fb7 2019-08-12 stsp }
1637 f1cbc3bc 2020-11-18 stsp if (line_offsets) {
1638 f1cbc3bc 2020-11-18 stsp off = total_len + i - hdrlen + 1;
1639 f1cbc3bc 2020-11-18 stsp (*line_offsets)[*nlines - 1] = off;
1640 f1cbc3bc 2020-11-18 stsp }
1641 b02560ec 2019-08-19 stsp i++;
1642 6c4c42e0 2019-06-24 stsp }
1643 84451b3e 2018-07-10 stsp }
1644 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
1645 454a6b59 2018-12-24 stsp n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1646 b6752625 2018-12-24 stsp if (n != len - hdrlen)
1647 b6752625 2018-12-24 stsp return got_ferror(outfile, GOT_ERR_IO);
1648 f595d9bd 2019-08-14 stsp total_len += len - hdrlen;
1649 35e9ba5d 2018-06-21 stsp hdrlen = 0;
1650 35e9ba5d 2018-06-21 stsp } while (len != 0);
1651 35e9ba5d 2018-06-21 stsp
1652 cbe7f848 2019-02-11 stsp if (fflush(outfile) != 0)
1653 638f9024 2019-05-13 stsp return got_error_from_errno("fflush");
1654 35e9ba5d 2018-06-21 stsp rewind(outfile);
1655 35e9ba5d 2018-06-21 stsp
1656 f595d9bd 2019-08-14 stsp if (filesize)
1657 f595d9bd 2019-08-14 stsp *filesize = total_len;
1658 f595d9bd 2019-08-14 stsp
1659 776d4d29 2018-06-17 stsp return NULL;
1660 f4a881ce 2018-11-17 stsp }
1661 f4a881ce 2018-11-17 stsp
1662 f4a881ce 2018-11-17 stsp static const struct got_error *
1663 268f7291 2018-12-24 stsp request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1664 268f7291 2018-12-24 stsp int pack_idx, struct got_object_id *id)
1665 a158c901 2018-12-23 stsp {
1666 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
1667 a158c901 2018-12-23 stsp
1668 268f7291 2018-12-24 stsp err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1669 268f7291 2018-12-24 stsp pack_idx);
1670 a158c901 2018-12-23 stsp if (err)
1671 a158c901 2018-12-23 stsp return err;
1672 a158c901 2018-12-23 stsp
1673 a158c901 2018-12-23 stsp return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1674 268f7291 2018-12-24 stsp }
1675 268f7291 2018-12-24 stsp
1676 268f7291 2018-12-24 stsp static const struct got_error *
1677 268f7291 2018-12-24 stsp read_packed_tag_privsep(struct got_tag_object **tag,
1678 268f7291 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1679 268f7291 2018-12-24 stsp struct got_object_id *id)
1680 268f7291 2018-12-24 stsp {
1681 268f7291 2018-12-24 stsp const struct got_error *err = NULL;
1682 268f7291 2018-12-24 stsp
1683 268f7291 2018-12-24 stsp if (pack->privsep_child)
1684 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1685 268f7291 2018-12-24 stsp
1686 268f7291 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
1687 268f7291 2018-12-24 stsp if (err)
1688 268f7291 2018-12-24 stsp return err;
1689 268f7291 2018-12-24 stsp
1690 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1691 a158c901 2018-12-23 stsp }
1692 9f2369b0 2018-12-24 stsp
1693 9f2369b0 2018-12-24 stsp static const struct got_error *
1694 9f2369b0 2018-12-24 stsp request_tag(struct got_tag_object **tag, struct got_repository *repo,
1695 d5c81d44 2021-07-08 stsp int fd, struct got_object_id *id)
1696 9f2369b0 2018-12-24 stsp {
1697 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1698 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1699 9f2369b0 2018-12-24 stsp
1700 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1701 9f2369b0 2018-12-24 stsp
1702 d5c81d44 2021-07-08 stsp err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1703 9f2369b0 2018-12-24 stsp if (err)
1704 9f2369b0 2018-12-24 stsp return err;
1705 9f2369b0 2018-12-24 stsp
1706 9f2369b0 2018-12-24 stsp return got_privsep_recv_tag(tag, ibuf);
1707 9f2369b0 2018-12-24 stsp }
1708 9f2369b0 2018-12-24 stsp
1709 9f2369b0 2018-12-24 stsp static const struct got_error *
1710 9f2369b0 2018-12-24 stsp read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1711 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo)
1712 9f2369b0 2018-12-24 stsp {
1713 ddc7b220 2019-09-08 stsp const struct got_error *err;
1714 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1715 9f2369b0 2018-12-24 stsp pid_t pid;
1716 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1717 9f2369b0 2018-12-24 stsp
1718 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1719 d5c81d44 2021-07-08 stsp return request_tag(tag, repo, obj_fd, id);
1720 9f2369b0 2018-12-24 stsp
1721 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1722 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1723 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1724 9f2369b0 2018-12-24 stsp
1725 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1726 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1727 ddc7b220 2019-09-08 stsp free(ibuf);
1728 ddc7b220 2019-09-08 stsp return err;
1729 ddc7b220 2019-09-08 stsp }
1730 9f2369b0 2018-12-24 stsp
1731 9f2369b0 2018-12-24 stsp pid = fork();
1732 ddc7b220 2019-09-08 stsp if (pid == -1) {
1733 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1734 ddc7b220 2019-09-08 stsp free(ibuf);
1735 ddc7b220 2019-09-08 stsp return err;
1736 ddc7b220 2019-09-08 stsp }
1737 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1738 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1739 9f2369b0 2018-12-24 stsp repo->path);
1740 9f2369b0 2018-12-24 stsp /* not reached */
1741 9f2369b0 2018-12-24 stsp }
1742 9f2369b0 2018-12-24 stsp
1743 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
1744 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1745 ddc7b220 2019-09-08 stsp free(ibuf);
1746 ddc7b220 2019-09-08 stsp return err;
1747 ddc7b220 2019-09-08 stsp }
1748 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1749 9f2369b0 2018-12-24 stsp imsg_fds[0];
1750 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1751 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1752 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1753 a158c901 2018-12-23 stsp
1754 d5c81d44 2021-07-08 stsp return request_tag(tag, repo, obj_fd, id);
1755 9f2369b0 2018-12-24 stsp }
1756 a158c901 2018-12-23 stsp
1757 a158c901 2018-12-23 stsp static const struct got_error *
1758 268f7291 2018-12-24 stsp open_tag(struct got_tag_object **tag, struct got_repository *repo,
1759 268f7291 2018-12-24 stsp struct got_object_id *id, int check_cache)
1760 f4a881ce 2018-11-17 stsp {
1761 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1762 268f7291 2018-12-24 stsp struct got_packidx *packidx = NULL;
1763 e82b1d81 2019-07-27 stsp int idx;
1764 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1765 5d844a1e 2019-08-13 stsp struct got_object *obj = NULL;
1766 5d844a1e 2019-08-13 stsp int obj_type = GOT_OBJ_TYPE_ANY;
1767 f4a881ce 2018-11-17 stsp
1768 f4a881ce 2018-11-17 stsp if (check_cache) {
1769 268f7291 2018-12-24 stsp *tag = got_repo_get_cached_tag(repo, id);
1770 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1771 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1772 f4a881ce 2018-11-17 stsp return NULL;
1773 f4a881ce 2018-11-17 stsp }
1774 f4a881ce 2018-11-17 stsp } else
1775 f4a881ce 2018-11-17 stsp *tag = NULL;
1776 f4a881ce 2018-11-17 stsp
1777 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1778 e82b1d81 2019-07-27 stsp if (err == NULL) {
1779 268f7291 2018-12-24 stsp struct got_pack *pack = NULL;
1780 f4a881ce 2018-11-17 stsp
1781 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
1782 aea75d87 2021-07-06 stsp packidx->path_packidx);
1783 268f7291 2018-12-24 stsp if (err)
1784 268f7291 2018-12-24 stsp return err;
1785 268f7291 2018-12-24 stsp
1786 268f7291 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1787 f4a881ce 2018-11-17 stsp if (pack == NULL) {
1788 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1789 e82b1d81 2019-07-27 stsp packidx);
1790 f4a881ce 2018-11-17 stsp if (err)
1791 8d2c5ea3 2019-08-13 stsp goto done;
1792 f4a881ce 2018-11-17 stsp }
1793 5d844a1e 2019-08-13 stsp
1794 992eb9d8 2020-02-07 tracey /* Beware of "lightweight" tags: Check object type first. */
1795 5d844a1e 2019-08-13 stsp err = read_packed_object_privsep(&obj, repo, pack, packidx,
1796 5d844a1e 2019-08-13 stsp idx, id);
1797 5d844a1e 2019-08-13 stsp if (err)
1798 5d844a1e 2019-08-13 stsp goto done;
1799 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1800 5d844a1e 2019-08-13 stsp got_object_close(obj);
1801 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG) {
1802 5d844a1e 2019-08-13 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1803 5d844a1e 2019-08-13 stsp goto done;
1804 5d844a1e 2019-08-13 stsp }
1805 5d844a1e 2019-08-13 stsp err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1806 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1807 e82b1d81 2019-07-27 stsp int fd;
1808 e82b1d81 2019-07-27 stsp
1809 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1810 e82b1d81 2019-07-27 stsp if (err)
1811 e82b1d81 2019-07-27 stsp return err;
1812 d5c81d44 2021-07-08 stsp err = got_object_read_header_privsep(&obj, id, repo, fd);
1813 5d844a1e 2019-08-13 stsp if (err)
1814 5d844a1e 2019-08-13 stsp return err;
1815 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1816 5d844a1e 2019-08-13 stsp got_object_close(obj);
1817 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
1818 5d844a1e 2019-08-13 stsp return got_error(GOT_ERR_OBJ_TYPE);
1819 5d844a1e 2019-08-13 stsp
1820 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1821 5d844a1e 2019-08-13 stsp if (err)
1822 5d844a1e 2019-08-13 stsp return err;
1823 d5c81d44 2021-07-08 stsp err = read_tag_privsep(tag, fd, id, repo);
1824 e82b1d81 2019-07-27 stsp }
1825 f4a881ce 2018-11-17 stsp
1826 f4a881ce 2018-11-17 stsp if (err == NULL) {
1827 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1828 268f7291 2018-12-24 stsp err = got_repo_cache_tag(repo, id, *tag);
1829 f4a881ce 2018-11-17 stsp }
1830 8d2c5ea3 2019-08-13 stsp done:
1831 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1832 f4a881ce 2018-11-17 stsp return err;
1833 f4a881ce 2018-11-17 stsp }
1834 f4a881ce 2018-11-17 stsp
1835 f4a881ce 2018-11-17 stsp const struct got_error *
1836 f4a881ce 2018-11-17 stsp got_object_open_as_tag(struct got_tag_object **tag,
1837 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object_id *id)
1838 f4a881ce 2018-11-17 stsp {
1839 f4a881ce 2018-11-17 stsp *tag = got_repo_get_cached_tag(repo, id);
1840 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1841 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1842 f4a881ce 2018-11-17 stsp return NULL;
1843 f4a881ce 2018-11-17 stsp }
1844 f4a881ce 2018-11-17 stsp
1845 268f7291 2018-12-24 stsp return open_tag(tag, repo, id, 0);
1846 f4a881ce 2018-11-17 stsp }
1847 f4a881ce 2018-11-17 stsp
1848 f4a881ce 2018-11-17 stsp const struct got_error *
1849 f4a881ce 2018-11-17 stsp got_object_tag_open(struct got_tag_object **tag,
1850 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object *obj)
1851 f4a881ce 2018-11-17 stsp {
1852 268f7291 2018-12-24 stsp return open_tag(tag, repo, got_object_get_id(obj), 1);
1853 d24820bf 2019-08-11 stsp }
1854 d24820bf 2019-08-11 stsp
1855 d24820bf 2019-08-11 stsp const char *
1856 d24820bf 2019-08-11 stsp got_object_tag_get_name(struct got_tag_object *tag)
1857 d24820bf 2019-08-11 stsp {
1858 d24820bf 2019-08-11 stsp return tag->tag;
1859 0bd18d37 2019-02-01 stsp }
1860 0bd18d37 2019-02-01 stsp
1861 0bd18d37 2019-02-01 stsp int
1862 0bd18d37 2019-02-01 stsp got_object_tag_get_object_type(struct got_tag_object *tag)
1863 0bd18d37 2019-02-01 stsp {
1864 0bd18d37 2019-02-01 stsp return tag->obj_type;
1865 0bd18d37 2019-02-01 stsp }
1866 0bd18d37 2019-02-01 stsp
1867 0bd18d37 2019-02-01 stsp struct got_object_id *
1868 0bd18d37 2019-02-01 stsp got_object_tag_get_object_id(struct got_tag_object *tag)
1869 0bd18d37 2019-02-01 stsp {
1870 0bd18d37 2019-02-01 stsp return &tag->id;
1871 01073a5d 2019-08-22 stsp }
1872 01073a5d 2019-08-22 stsp
1873 01073a5d 2019-08-22 stsp time_t
1874 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_time(struct got_tag_object *tag)
1875 01073a5d 2019-08-22 stsp {
1876 01073a5d 2019-08-22 stsp return tag->tagger_time;
1877 01073a5d 2019-08-22 stsp }
1878 01073a5d 2019-08-22 stsp
1879 01073a5d 2019-08-22 stsp time_t
1880 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1881 01073a5d 2019-08-22 stsp {
1882 01073a5d 2019-08-22 stsp return tag->tagger_gmtoff;
1883 01073a5d 2019-08-22 stsp }
1884 01073a5d 2019-08-22 stsp
1885 01073a5d 2019-08-22 stsp const char *
1886 01073a5d 2019-08-22 stsp got_object_tag_get_tagger(struct got_tag_object *tag)
1887 01073a5d 2019-08-22 stsp {
1888 01073a5d 2019-08-22 stsp return tag->tagger;
1889 776d4d29 2018-06-17 stsp }
1890 776d4d29 2018-06-17 stsp
1891 01073a5d 2019-08-22 stsp const char *
1892 01073a5d 2019-08-22 stsp got_object_tag_get_message(struct got_tag_object *tag)
1893 01073a5d 2019-08-22 stsp {
1894 01073a5d 2019-08-22 stsp return tag->tagmsg;
1895 01073a5d 2019-08-22 stsp }
1896 01073a5d 2019-08-22 stsp
1897 776d4d29 2018-06-17 stsp static struct got_tree_entry *
1898 65a9bbe9 2018-09-15 stsp find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1899 776d4d29 2018-06-17 stsp {
1900 56e0773d 2019-11-28 stsp int i;
1901 776d4d29 2018-06-17 stsp
1902 63da309a 2018-11-07 stsp /* Note that tree entries are sorted in strncmp() order. */
1903 56e0773d 2019-11-28 stsp for (i = 0; i < tree->nentries; i++) {
1904 56e0773d 2019-11-28 stsp struct got_tree_entry *te = &tree->entries[i];
1905 63da309a 2018-11-07 stsp int cmp = strncmp(te->name, name, len);
1906 63da309a 2018-11-07 stsp if (cmp < 0)
1907 63da309a 2018-11-07 stsp continue;
1908 63da309a 2018-11-07 stsp if (cmp > 0)
1909 63da309a 2018-11-07 stsp break;
1910 63da309a 2018-11-07 stsp if (te->name[len] == '\0')
1911 776d4d29 2018-06-17 stsp return te;
1912 776d4d29 2018-06-17 stsp }
1913 eb651edf 2018-02-11 stsp return NULL;
1914 a129376b 2019-03-28 stsp }
1915 a129376b 2019-03-28 stsp
1916 56e0773d 2019-11-28 stsp struct got_tree_entry *
1917 a129376b 2019-03-28 stsp got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1918 a129376b 2019-03-28 stsp {
1919 a129376b 2019-03-28 stsp return find_entry_by_name(tree, name, strlen(name));
1920 776d4d29 2018-06-17 stsp }
1921 776d4d29 2018-06-17 stsp
1922 776d4d29 2018-06-17 stsp const struct got_error *
1923 cc8021af 2021-10-12 thomas got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1924 cc8021af 2021-10-12 thomas struct got_repository *repo, struct got_tree_object *tree,
1925 cc8021af 2021-10-12 thomas const char *path)
1926 776d4d29 2018-06-17 stsp {
1927 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
1928 cc8021af 2021-10-12 thomas struct got_tree_object *subtree = NULL;
1929 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
1930 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1931 b7cd37e5 2018-11-18 stsp size_t seglen;
1932 776d4d29 2018-06-17 stsp
1933 27d434c2 2018-09-15 stsp *id = NULL;
1934 776d4d29 2018-06-17 stsp
1935 65a9bbe9 2018-09-15 stsp s = path;
1936 5e54fb30 2019-05-31 stsp while (s[0] == '/')
1937 5e54fb30 2019-05-31 stsp s++;
1938 776d4d29 2018-06-17 stsp seg = s;
1939 65a9bbe9 2018-09-15 stsp seglen = 0;
1940 cc8021af 2021-10-12 thomas subtree = tree;
1941 b7cd37e5 2018-11-18 stsp while (*s) {
1942 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
1943 776d4d29 2018-06-17 stsp
1944 776d4d29 2018-06-17 stsp if (*s != '/') {
1945 776d4d29 2018-06-17 stsp s++;
1946 65a9bbe9 2018-09-15 stsp seglen++;
1947 00530cfb 2018-06-21 stsp if (*s)
1948 00530cfb 2018-06-21 stsp continue;
1949 776d4d29 2018-06-17 stsp }
1950 776d4d29 2018-06-17 stsp
1951 cc8021af 2021-10-12 thomas te = find_entry_by_name(subtree, seg, seglen);
1952 db37e2c0 2018-06-21 stsp if (te == NULL) {
1953 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1954 776d4d29 2018-06-17 stsp goto done;
1955 776d4d29 2018-06-17 stsp }
1956 776d4d29 2018-06-17 stsp
1957 b7cd37e5 2018-11-18 stsp if (*s == '\0')
1958 67606321 2018-06-21 stsp break;
1959 67606321 2018-06-21 stsp
1960 776d4d29 2018-06-17 stsp seg = s + 1;
1961 65a9bbe9 2018-09-15 stsp seglen = 0;
1962 776d4d29 2018-06-17 stsp s++;
1963 776d4d29 2018-06-17 stsp if (*s) {
1964 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
1965 56e0773d 2019-11-28 stsp &te->id);
1966 db37e2c0 2018-06-21 stsp te = NULL;
1967 776d4d29 2018-06-17 stsp if (err)
1968 776d4d29 2018-06-17 stsp goto done;
1969 cc8021af 2021-10-12 thomas if (subtree != tree)
1970 cc8021af 2021-10-12 thomas got_object_tree_close(subtree);
1971 cc8021af 2021-10-12 thomas subtree = next_tree;
1972 776d4d29 2018-06-17 stsp }
1973 776d4d29 2018-06-17 stsp }
1974 776d4d29 2018-06-17 stsp
1975 27d434c2 2018-09-15 stsp if (te) {
1976 56e0773d 2019-11-28 stsp *id = got_object_id_dup(&te->id);
1977 27d434c2 2018-09-15 stsp if (*id == NULL)
1978 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
1979 cc8021af 2021-10-12 thomas if (mode)
1980 cc8021af 2021-10-12 thomas *mode = te->mode;
1981 27d434c2 2018-09-15 stsp } else
1982 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1983 cc8021af 2021-10-12 thomas done:
1984 cc8021af 2021-10-12 thomas if (subtree && subtree != tree)
1985 cc8021af 2021-10-12 thomas got_object_tree_close(subtree);
1986 cc8021af 2021-10-12 thomas return err;
1987 cc8021af 2021-10-12 thomas }
1988 cc8021af 2021-10-12 thomas const struct got_error *
1989 cc8021af 2021-10-12 thomas got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1990 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
1991 cc8021af 2021-10-12 thomas {
1992 cc8021af 2021-10-12 thomas const struct got_error *err = NULL;
1993 cc8021af 2021-10-12 thomas struct got_tree_object *tree = NULL;
1994 cc8021af 2021-10-12 thomas
1995 cc8021af 2021-10-12 thomas *id = NULL;
1996 cc8021af 2021-10-12 thomas
1997 cc8021af 2021-10-12 thomas /* Handle opening of root of commit's tree. */
1998 cc8021af 2021-10-12 thomas if (got_path_is_root_dir(path)) {
1999 cc8021af 2021-10-12 thomas *id = got_object_id_dup(commit->tree_id);
2000 cc8021af 2021-10-12 thomas if (*id == NULL)
2001 cc8021af 2021-10-12 thomas err = got_error_from_errno("got_object_id_dup");
2002 cc8021af 2021-10-12 thomas } else {
2003 cc8021af 2021-10-12 thomas err = got_object_open_as_tree(&tree, repo, commit->tree_id);
2004 cc8021af 2021-10-12 thomas if (err)
2005 cc8021af 2021-10-12 thomas goto done;
2006 cc8021af 2021-10-12 thomas err = got_object_tree_find_path(id, NULL, repo, tree, path);
2007 cc8021af 2021-10-12 thomas }
2008 776d4d29 2018-06-17 stsp done:
2009 776d4d29 2018-06-17 stsp if (tree)
2010 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
2011 776d4d29 2018-06-17 stsp return err;
2012 ac5f2b26 2020-05-05 stsp }
2013 ac5f2b26 2020-05-05 stsp
2014 ac5f2b26 2020-05-05 stsp /*
2015 ac5f2b26 2020-05-05 stsp * Normalize file mode bits to avoid false positive tree entry differences
2016 ac5f2b26 2020-05-05 stsp * in case tree entries have unexpected mode bits set.
2017 ac5f2b26 2020-05-05 stsp */
2018 ac5f2b26 2020-05-05 stsp static mode_t
2019 ac5f2b26 2020-05-05 stsp normalize_mode_for_comparison(mode_t mode)
2020 ac5f2b26 2020-05-05 stsp {
2021 ac5f2b26 2020-05-05 stsp /*
2022 ac5f2b26 2020-05-05 stsp * For directories, the only relevant bit is the IFDIR bit.
2023 ac5f2b26 2020-05-05 stsp * This allows us to detect paths changing from a directory
2024 ac5f2b26 2020-05-05 stsp * to a file and vice versa.
2025 ac5f2b26 2020-05-05 stsp */
2026 ac5f2b26 2020-05-05 stsp if (S_ISDIR(mode))
2027 ac5f2b26 2020-05-05 stsp return mode & S_IFDIR;
2028 40dde666 2020-07-23 stsp
2029 40dde666 2020-07-23 stsp /*
2030 40dde666 2020-07-23 stsp * For symlinks, the only relevant bit is the IFLNK bit.
2031 40dde666 2020-07-23 stsp * This allows us to detect paths changing from a symlinks
2032 40dde666 2020-07-23 stsp * to a file or directory and vice versa.
2033 40dde666 2020-07-23 stsp */
2034 40dde666 2020-07-23 stsp if (S_ISLNK(mode))
2035 40dde666 2020-07-23 stsp return mode & S_IFLNK;
2036 ac5f2b26 2020-05-05 stsp
2037 ac5f2b26 2020-05-05 stsp /* For files, the only change we care about is the executable bit. */
2038 ac5f2b26 2020-05-05 stsp return mode & S_IXUSR;
2039 68482ea3 2017-11-27 stsp }
2040 07862c20 2018-09-15 stsp
2041 07862c20 2018-09-15 stsp const struct got_error *
2042 07862c20 2018-09-15 stsp got_object_tree_path_changed(int *changed,
2043 07862c20 2018-09-15 stsp struct got_tree_object *tree01, struct got_tree_object *tree02,
2044 07862c20 2018-09-15 stsp const char *path, struct got_repository *repo)
2045 07862c20 2018-09-15 stsp {
2046 07862c20 2018-09-15 stsp const struct got_error *err = NULL;
2047 07862c20 2018-09-15 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2048 07862c20 2018-09-15 stsp struct got_tree_entry *te1 = NULL, *te2 = NULL;
2049 65a9bbe9 2018-09-15 stsp const char *seg, *s;
2050 3b7f9878 2018-11-18 stsp size_t seglen;
2051 07862c20 2018-09-15 stsp
2052 07862c20 2018-09-15 stsp *changed = 0;
2053 07862c20 2018-09-15 stsp
2054 07862c20 2018-09-15 stsp /* We not do support comparing the root path. */
2055 61a7d79f 2020-02-29 stsp if (got_path_is_root_dir(path))
2056 63f810e6 2020-02-29 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
2057 07862c20 2018-09-15 stsp
2058 07862c20 2018-09-15 stsp tree1 = tree01;
2059 07862c20 2018-09-15 stsp tree2 = tree02;
2060 65a9bbe9 2018-09-15 stsp s = path;
2061 61a7d79f 2020-02-29 stsp while (*s == '/')
2062 61a7d79f 2020-02-29 stsp s++;
2063 07862c20 2018-09-15 stsp seg = s;
2064 65a9bbe9 2018-09-15 stsp seglen = 0;
2065 3b7f9878 2018-11-18 stsp while (*s) {
2066 07862c20 2018-09-15 stsp struct got_tree_object *next_tree1, *next_tree2;
2067 ac5f2b26 2020-05-05 stsp mode_t mode1, mode2;
2068 07862c20 2018-09-15 stsp
2069 07862c20 2018-09-15 stsp if (*s != '/') {
2070 07862c20 2018-09-15 stsp s++;
2071 65a9bbe9 2018-09-15 stsp seglen++;
2072 07862c20 2018-09-15 stsp if (*s)
2073 07862c20 2018-09-15 stsp continue;
2074 07862c20 2018-09-15 stsp }
2075 07862c20 2018-09-15 stsp
2076 65a9bbe9 2018-09-15 stsp te1 = find_entry_by_name(tree1, seg, seglen);
2077 07862c20 2018-09-15 stsp if (te1 == NULL) {
2078 07862c20 2018-09-15 stsp err = got_error(GOT_ERR_NO_OBJ);
2079 07862c20 2018-09-15 stsp goto done;
2080 07862c20 2018-09-15 stsp }
2081 07862c20 2018-09-15 stsp
2082 e8bfb8f3 2020-12-18 stsp if (tree2)
2083 e8bfb8f3 2020-12-18 stsp te2 = find_entry_by_name(tree2, seg, seglen);
2084 07862c20 2018-09-15 stsp
2085 e8bfb8f3 2020-12-18 stsp if (te2) {
2086 e8bfb8f3 2020-12-18 stsp mode1 = normalize_mode_for_comparison(te1->mode);
2087 e8bfb8f3 2020-12-18 stsp mode2 = normalize_mode_for_comparison(te2->mode);
2088 e8bfb8f3 2020-12-18 stsp if (mode1 != mode2) {
2089 e8bfb8f3 2020-12-18 stsp *changed = 1;
2090 e8bfb8f3 2020-12-18 stsp goto done;
2091 e8bfb8f3 2020-12-18 stsp }
2092 e8bfb8f3 2020-12-18 stsp
2093 e8bfb8f3 2020-12-18 stsp if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2094 e8bfb8f3 2020-12-18 stsp *changed = 0;
2095 e8bfb8f3 2020-12-18 stsp goto done;
2096 e8bfb8f3 2020-12-18 stsp }
2097 07862c20 2018-09-15 stsp }
2098 07862c20 2018-09-15 stsp
2099 3b7f9878 2018-11-18 stsp if (*s == '\0') { /* final path element */
2100 07862c20 2018-09-15 stsp *changed = 1;
2101 07862c20 2018-09-15 stsp goto done;
2102 07862c20 2018-09-15 stsp }
2103 07862c20 2018-09-15 stsp
2104 07862c20 2018-09-15 stsp seg = s + 1;
2105 07862c20 2018-09-15 stsp s++;
2106 65a9bbe9 2018-09-15 stsp seglen = 0;
2107 07862c20 2018-09-15 stsp if (*s) {
2108 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree1, repo,
2109 56e0773d 2019-11-28 stsp &te1->id);
2110 07862c20 2018-09-15 stsp te1 = NULL;
2111 07862c20 2018-09-15 stsp if (err)
2112 07862c20 2018-09-15 stsp goto done;
2113 a31cea73 2018-09-15 stsp if (tree1 != tree01)
2114 a31cea73 2018-09-15 stsp got_object_tree_close(tree1);
2115 07862c20 2018-09-15 stsp tree1 = next_tree1;
2116 07862c20 2018-09-15 stsp
2117 e8bfb8f3 2020-12-18 stsp if (te2) {
2118 e8bfb8f3 2020-12-18 stsp err = got_object_open_as_tree(&next_tree2, repo,
2119 e8bfb8f3 2020-12-18 stsp &te2->id);
2120 e8bfb8f3 2020-12-18 stsp te2 = NULL;
2121 e8bfb8f3 2020-12-18 stsp if (err)
2122 e8bfb8f3 2020-12-18 stsp goto done;
2123 e8bfb8f3 2020-12-18 stsp if (tree2 != tree02)
2124 e8bfb8f3 2020-12-18 stsp got_object_tree_close(tree2);
2125 e8bfb8f3 2020-12-18 stsp tree2 = next_tree2;
2126 e8bfb8f3 2020-12-18 stsp } else if (tree2) {
2127 e8bfb8f3 2020-12-18 stsp if (tree2 != tree02)
2128 e8bfb8f3 2020-12-18 stsp got_object_tree_close(tree2);
2129 e8bfb8f3 2020-12-18 stsp tree2 = NULL;
2130 e8bfb8f3 2020-12-18 stsp }
2131 07862c20 2018-09-15 stsp }
2132 07862c20 2018-09-15 stsp }
2133 07862c20 2018-09-15 stsp done:
2134 a31cea73 2018-09-15 stsp if (tree1 && tree1 != tree01)
2135 07862c20 2018-09-15 stsp got_object_tree_close(tree1);
2136 a31cea73 2018-09-15 stsp if (tree2 && tree2 != tree02)
2137 07862c20 2018-09-15 stsp got_object_tree_close(tree2);
2138 77880158 2018-11-04 stsp return err;
2139 77880158 2018-11-04 stsp }
2140 ed175427 2019-05-09 stsp
2141 ed175427 2019-05-09 stsp const struct got_error *
2142 ed175427 2019-05-09 stsp got_object_tree_entry_dup(struct got_tree_entry **new_te,
2143 ed175427 2019-05-09 stsp struct got_tree_entry *te)
2144 ed175427 2019-05-09 stsp {
2145 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
2146 ed175427 2019-05-09 stsp
2147 ed175427 2019-05-09 stsp *new_te = calloc(1, sizeof(**new_te));
2148 ed175427 2019-05-09 stsp if (*new_te == NULL)
2149 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
2150 ed175427 2019-05-09 stsp
2151 ed175427 2019-05-09 stsp (*new_te)->mode = te->mode;
2152 56e0773d 2019-11-28 stsp memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2153 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2154 8c4eabf2 2019-05-10 stsp return err;
2155 63c5ca5d 2019-08-24 stsp }
2156 63c5ca5d 2019-08-24 stsp
2157 63c5ca5d 2019-08-24 stsp int
2158 56e0773d 2019-11-28 stsp got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2159 63c5ca5d 2019-08-24 stsp {
2160 63c5ca5d 2019-08-24 stsp return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2161 e40622f4 2020-07-23 stsp }
2162 e40622f4 2020-07-23 stsp
2163 e40622f4 2020-07-23 stsp int
2164 e40622f4 2020-07-23 stsp got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2165 e40622f4 2020-07-23 stsp {
2166 e40622f4 2020-07-23 stsp /* S_IFDIR check avoids confusing symlinks with submodules. */
2167 e40622f4 2020-07-23 stsp return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2168 e40622f4 2020-07-23 stsp }
2169 e40622f4 2020-07-23 stsp
2170 e40622f4 2020-07-23 stsp static const struct got_error *
2171 e40622f4 2020-07-23 stsp resolve_symlink(char **link_target, const char *path,
2172 945f9229 2022-04-16 thomas struct got_commit_object *commit, struct got_repository *repo)
2173 e40622f4 2020-07-23 stsp {
2174 e40622f4 2020-07-23 stsp const struct got_error *err = NULL;
2175 dbdd6209 2020-10-19 stsp char buf[PATH_MAX];
2176 e40622f4 2020-07-23 stsp char *name, *parent_path = NULL;
2177 e40622f4 2020-07-23 stsp struct got_object_id *tree_obj_id = NULL;
2178 e40622f4 2020-07-23 stsp struct got_tree_object *tree = NULL;
2179 e40622f4 2020-07-23 stsp struct got_tree_entry *te = NULL;
2180 e40622f4 2020-07-23 stsp
2181 e40622f4 2020-07-23 stsp *link_target = NULL;
2182 559d127c 2020-07-23 stsp
2183 dbdd6209 2020-10-19 stsp if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2184 dbdd6209 2020-10-19 stsp return got_error(GOT_ERR_NO_SPACE);
2185 dbdd6209 2020-10-19 stsp
2186 dbdd6209 2020-10-19 stsp name = basename(buf);
2187 e40622f4 2020-07-23 stsp if (name == NULL)
2188 e40622f4 2020-07-23 stsp return got_error_from_errno2("basename", path);
2189 e40622f4 2020-07-23 stsp
2190 e40622f4 2020-07-23 stsp err = got_path_dirname(&parent_path, path);
2191 e40622f4 2020-07-23 stsp if (err)
2192 e40622f4 2020-07-23 stsp return err;
2193 e40622f4 2020-07-23 stsp
2194 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_obj_id, repo, commit,
2195 e40622f4 2020-07-23 stsp parent_path);
2196 e40622f4 2020-07-23 stsp if (err) {
2197 e40622f4 2020-07-23 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2198 e40622f4 2020-07-23 stsp /* Display the complete path in error message. */
2199 e40622f4 2020-07-23 stsp err = got_error_path(path, err->code);
2200 e40622f4 2020-07-23 stsp }
2201 e40622f4 2020-07-23 stsp goto done;
2202 e40622f4 2020-07-23 stsp }
2203 e40622f4 2020-07-23 stsp
2204 e40622f4 2020-07-23 stsp err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2205 e40622f4 2020-07-23 stsp if (err)
2206 e40622f4 2020-07-23 stsp goto done;
2207 e40622f4 2020-07-23 stsp
2208 e40622f4 2020-07-23 stsp te = got_object_tree_find_entry(tree, name);
2209 e40622f4 2020-07-23 stsp if (te == NULL) {
2210 e40622f4 2020-07-23 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2211 e40622f4 2020-07-23 stsp goto done;
2212 e40622f4 2020-07-23 stsp }
2213 e40622f4 2020-07-23 stsp
2214 e40622f4 2020-07-23 stsp if (got_object_tree_entry_is_symlink(te)) {
2215 e40622f4 2020-07-23 stsp err = got_tree_entry_get_symlink_target(link_target, te, repo);
2216 e40622f4 2020-07-23 stsp if (err)
2217 e40622f4 2020-07-23 stsp goto done;
2218 e40622f4 2020-07-23 stsp if (!got_path_is_absolute(*link_target)) {
2219 e40622f4 2020-07-23 stsp char *abspath;
2220 e40622f4 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", parent_path,
2221 e40622f4 2020-07-23 stsp *link_target) == -1) {
2222 e40622f4 2020-07-23 stsp err = got_error_from_errno("asprintf");
2223 e40622f4 2020-07-23 stsp goto done;
2224 e40622f4 2020-07-23 stsp }
2225 e40622f4 2020-07-23 stsp free(*link_target);
2226 e40622f4 2020-07-23 stsp *link_target = malloc(PATH_MAX);
2227 e40622f4 2020-07-23 stsp if (*link_target == NULL) {
2228 e40622f4 2020-07-23 stsp err = got_error_from_errno("malloc");
2229 e40622f4 2020-07-23 stsp goto done;
2230 e40622f4 2020-07-23 stsp }
2231 e40622f4 2020-07-23 stsp err = got_canonpath(abspath, *link_target, PATH_MAX);
2232 e40622f4 2020-07-23 stsp free(abspath);
2233 e40622f4 2020-07-23 stsp if (err)
2234 e40622f4 2020-07-23 stsp goto done;
2235 e40622f4 2020-07-23 stsp }
2236 e40622f4 2020-07-23 stsp }
2237 e40622f4 2020-07-23 stsp done:
2238 e40622f4 2020-07-23 stsp free(tree_obj_id);
2239 e40622f4 2020-07-23 stsp if (tree)
2240 e40622f4 2020-07-23 stsp got_object_tree_close(tree);
2241 e40622f4 2020-07-23 stsp if (err) {
2242 e40622f4 2020-07-23 stsp free(*link_target);
2243 e40622f4 2020-07-23 stsp *link_target = NULL;
2244 e40622f4 2020-07-23 stsp }
2245 e40622f4 2020-07-23 stsp return err;
2246 ca6e02ac 2020-01-07 stsp }
2247 ca6e02ac 2020-01-07 stsp
2248 ca6e02ac 2020-01-07 stsp const struct got_error *
2249 e40622f4 2020-07-23 stsp got_object_resolve_symlinks(char **link_target, const char *path,
2250 945f9229 2022-04-16 thomas struct got_commit_object *commit, struct got_repository *repo)
2251 e40622f4 2020-07-23 stsp {
2252 e40622f4 2020-07-23 stsp const struct got_error *err = NULL;
2253 e40622f4 2020-07-23 stsp char *next_target = NULL;
2254 e40622f4 2020-07-23 stsp int max_recursion = 40; /* matches Git */
2255 e40622f4 2020-07-23 stsp
2256 e40622f4 2020-07-23 stsp *link_target = NULL;
2257 e40622f4 2020-07-23 stsp
2258 e40622f4 2020-07-23 stsp do {
2259 e40622f4 2020-07-23 stsp err = resolve_symlink(&next_target,
2260 945f9229 2022-04-16 thomas *link_target ? *link_target : path, commit, repo);
2261 e40622f4 2020-07-23 stsp if (err)
2262 e40622f4 2020-07-23 stsp break;
2263 e40622f4 2020-07-23 stsp if (next_target) {
2264 e40622f4 2020-07-23 stsp free(*link_target);
2265 e40622f4 2020-07-23 stsp if (--max_recursion == 0) {
2266 e40622f4 2020-07-23 stsp err = got_error_path(path, GOT_ERR_RECURSION);
2267 e40622f4 2020-07-23 stsp *link_target = NULL;
2268 e40622f4 2020-07-23 stsp break;
2269 e40622f4 2020-07-23 stsp }
2270 e40622f4 2020-07-23 stsp *link_target = next_target;
2271 e40622f4 2020-07-23 stsp }
2272 e40622f4 2020-07-23 stsp } while (next_target);
2273 e40622f4 2020-07-23 stsp
2274 e40622f4 2020-07-23 stsp return err;
2275 e40622f4 2020-07-23 stsp }
2276 e40622f4 2020-07-23 stsp
2277 e40622f4 2020-07-23 stsp const struct got_error *
2278 ca6e02ac 2020-01-07 stsp got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2279 ca6e02ac 2020-01-07 stsp struct got_object_id *commit_id, const char *path,
2280 ca6e02ac 2020-01-07 stsp struct got_repository *repo)
2281 ca6e02ac 2020-01-07 stsp {
2282 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
2283 ca6e02ac 2020-01-07 stsp struct got_pack *pack = NULL;
2284 ca6e02ac 2020-01-07 stsp struct got_packidx *packidx = NULL;
2285 ca6e02ac 2020-01-07 stsp char *path_packfile = NULL;
2286 ca6e02ac 2020-01-07 stsp struct got_commit_object *changed_commit = NULL;
2287 ca6e02ac 2020-01-07 stsp struct got_object_id *changed_commit_id = NULL;
2288 ca6e02ac 2020-01-07 stsp int idx;
2289 ca6e02ac 2020-01-07 stsp
2290 ca6e02ac 2020-01-07 stsp err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2291 ca6e02ac 2020-01-07 stsp if (err) {
2292 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
2293 ca6e02ac 2020-01-07 stsp return err;
2294 ca6e02ac 2020-01-07 stsp return NULL;
2295 ca6e02ac 2020-01-07 stsp }
2296 ca6e02ac 2020-01-07 stsp
2297 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
2298 aea75d87 2021-07-06 stsp packidx->path_packidx);
2299 ca6e02ac 2020-01-07 stsp if (err)
2300 ca6e02ac 2020-01-07 stsp return err;
2301 ca6e02ac 2020-01-07 stsp
2302 ca6e02ac 2020-01-07 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
2303 ca6e02ac 2020-01-07 stsp if (pack == NULL) {
2304 ca6e02ac 2020-01-07 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2305 ca6e02ac 2020-01-07 stsp if (err)
2306 ca6e02ac 2020-01-07 stsp goto done;
2307 ca6e02ac 2020-01-07 stsp }
2308 ca6e02ac 2020-01-07 stsp
2309 ca6e02ac 2020-01-07 stsp if (pack->privsep_child == NULL) {
2310 ca6e02ac 2020-01-07 stsp err = start_pack_privsep_child(pack, packidx);
2311 ca6e02ac 2020-01-07 stsp if (err)
2312 ca6e02ac 2020-01-07 stsp goto done;
2313 ca6e02ac 2020-01-07 stsp }
2314 ca6e02ac 2020-01-07 stsp
2315 ca6e02ac 2020-01-07 stsp err = got_privsep_send_commit_traversal_request(
2316 ca6e02ac 2020-01-07 stsp pack->privsep_child->ibuf, commit_id, idx, path);
2317 ca6e02ac 2020-01-07 stsp if (err)
2318 ca6e02ac 2020-01-07 stsp goto done;
2319 ca6e02ac 2020-01-07 stsp
2320 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_traversed_commits(&changed_commit,
2321 ca6e02ac 2020-01-07 stsp &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2322 ca6e02ac 2020-01-07 stsp if (err)
2323 ca6e02ac 2020-01-07 stsp goto done;
2324 ca6e02ac 2020-01-07 stsp
2325 ca6e02ac 2020-01-07 stsp if (changed_commit) {
2326 ca6e02ac 2020-01-07 stsp /*
2327 ca6e02ac 2020-01-07 stsp * Cache the commit in which the path was changed.
2328 ca6e02ac 2020-01-07 stsp * This commit might be opened again soon.
2329 ca6e02ac 2020-01-07 stsp */
2330 ca6e02ac 2020-01-07 stsp changed_commit->refcnt++;
2331 ca6e02ac 2020-01-07 stsp err = got_repo_cache_commit(repo, changed_commit_id,
2332 ca6e02ac 2020-01-07 stsp changed_commit);
2333 ca6e02ac 2020-01-07 stsp got_object_commit_close(changed_commit);
2334 ca6e02ac 2020-01-07 stsp }
2335 ca6e02ac 2020-01-07 stsp done:
2336 ca6e02ac 2020-01-07 stsp free(path_packfile);
2337 ca6e02ac 2020-01-07 stsp free(changed_commit_id);
2338 ca6e02ac 2020-01-07 stsp return err;
2339 ed175427 2019-05-09 stsp }