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