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