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