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