Blame


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