Blame


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