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