Blame


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