Blame


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