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