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