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 af57b12a 2020-07-23 stsp
1162 af57b12a 2020-07-23 stsp *link_target = NULL;
1163 af57b12a 2020-07-23 stsp
1164 af57b12a 2020-07-23 stsp if (!got_object_tree_entry_is_symlink(te))
1165 af57b12a 2020-07-23 stsp return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1166 af57b12a 2020-07-23 stsp
1167 af57b12a 2020-07-23 stsp err = got_object_open_as_blob(&blob, repo,
1168 af57b12a 2020-07-23 stsp got_tree_entry_get_id(te), PATH_MAX);
1169 af57b12a 2020-07-23 stsp if (err)
1170 af57b12a 2020-07-23 stsp return err;
1171 af57b12a 2020-07-23 stsp
1172 af57b12a 2020-07-23 stsp err = got_object_blob_read_to_str(link_target, blob);
1173 af57b12a 2020-07-23 stsp got_object_blob_close(blob);
1174 659dc16e 2020-07-23 stsp if (err) {
1175 659dc16e 2020-07-23 stsp free(*link_target);
1176 659dc16e 2020-07-23 stsp *link_target = NULL;
1177 659dc16e 2020-07-23 stsp }
1178 0d6c6ee3 2020-05-20 stsp return err;
1179 56e0773d 2019-11-28 stsp }
1180 56e0773d 2019-11-28 stsp
1181 56e0773d 2019-11-28 stsp int
1182 56e0773d 2019-11-28 stsp got_tree_entry_get_index(struct got_tree_entry *te)
1183 56e0773d 2019-11-28 stsp {
1184 56e0773d 2019-11-28 stsp return te->idx;
1185 56e0773d 2019-11-28 stsp }
1186 56e0773d 2019-11-28 stsp
1187 56e0773d 2019-11-28 stsp struct got_tree_entry *
1188 56e0773d 2019-11-28 stsp got_tree_entry_get_next(struct got_tree_object *tree,
1189 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
1190 56e0773d 2019-11-28 stsp {
1191 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx + 1);
1192 56e0773d 2019-11-28 stsp }
1193 56e0773d 2019-11-28 stsp
1194 56e0773d 2019-11-28 stsp struct got_tree_entry *
1195 56e0773d 2019-11-28 stsp got_tree_entry_get_prev(struct got_tree_object *tree,
1196 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
1197 56e0773d 2019-11-28 stsp {
1198 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx - 1);
1199 56e0773d 2019-11-28 stsp }
1200 56e0773d 2019-11-28 stsp
1201 3840f4c9 2018-09-12 stsp static const struct got_error *
1202 ac544f8c 2019-01-13 stsp request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1203 ebc55e2d 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1204 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
1205 3840f4c9 2018-09-12 stsp {
1206 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
1207 bc1f382f 2022-01-05 thomas struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1208 bc1f382f 2022-01-05 thomas int outfd_child;
1209 24140570 2018-09-09 stsp
1210 bc1f382f 2022-01-05 thomas err = pack_child_send_tempfiles(ibuf, pack);
1211 bc1f382f 2022-01-05 thomas if (err)
1212 bc1f382f 2022-01-05 thomas return err;
1213 3840f4c9 2018-09-12 stsp
1214 3840f4c9 2018-09-12 stsp outfd_child = dup(outfd);
1215 3840f4c9 2018-09-12 stsp if (outfd_child == -1)
1216 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1217 3840f4c9 2018-09-12 stsp
1218 ebc55e2d 2018-12-24 stsp err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1219 3840f4c9 2018-09-12 stsp if (err)
1220 3840f4c9 2018-09-12 stsp return err;
1221 3840f4c9 2018-09-12 stsp
1222 3840f4c9 2018-09-12 stsp err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1223 3840f4c9 2018-09-12 stsp outfd_child);
1224 3840f4c9 2018-09-12 stsp if (err) {
1225 3840f4c9 2018-09-12 stsp return err;
1226 3840f4c9 2018-09-12 stsp }
1227 3840f4c9 2018-09-12 stsp
1228 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen,
1229 ebc55e2d 2018-12-24 stsp pack->privsep_child->ibuf);
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 if (lseek(outfd, SEEK_SET, 0) == -1)
1234 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1235 3840f4c9 2018-09-12 stsp
1236 3840f4c9 2018-09-12 stsp return err;
1237 3840f4c9 2018-09-12 stsp }
1238 3840f4c9 2018-09-12 stsp
1239 ebc55e2d 2018-12-24 stsp static const struct got_error *
1240 ac544f8c 2019-01-13 stsp read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1241 ac544f8c 2019-01-13 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1242 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
1243 68482ea3 2017-11-27 stsp {
1244 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
1245 ebc55e2d 2018-12-24 stsp
1246 ebc55e2d 2018-12-24 stsp if (pack->privsep_child == NULL) {
1247 68036464 2022-06-26 thomas err = got_pack_start_privsep_child(pack, packidx);
1248 ebc55e2d 2018-12-24 stsp if (err)
1249 ebc55e2d 2018-12-24 stsp return err;
1250 ebc55e2d 2018-12-24 stsp }
1251 68482ea3 2017-11-27 stsp
1252 ac544f8c 2019-01-13 stsp return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1253 ac544f8c 2019-01-13 stsp idx, id);
1254 9f2369b0 2018-12-24 stsp }
1255 9f2369b0 2018-12-24 stsp
1256 9f2369b0 2018-12-24 stsp static const struct got_error *
1257 ac544f8c 2019-01-13 stsp request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1258 d5c81d44 2021-07-08 stsp int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1259 9f2369b0 2018-12-24 stsp {
1260 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1261 9f2369b0 2018-12-24 stsp int outfd_child;
1262 9f2369b0 2018-12-24 stsp
1263 9f2369b0 2018-12-24 stsp outfd_child = dup(outfd);
1264 9f2369b0 2018-12-24 stsp if (outfd_child == -1)
1265 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1266 9f2369b0 2018-12-24 stsp
1267 d5c81d44 2021-07-08 stsp err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1268 9f2369b0 2018-12-24 stsp if (err)
1269 9f2369b0 2018-12-24 stsp return err;
1270 9f2369b0 2018-12-24 stsp
1271 9f2369b0 2018-12-24 stsp err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1272 41496140 2019-02-21 stsp if (err)
1273 9f2369b0 2018-12-24 stsp return err;
1274 9f2369b0 2018-12-24 stsp
1275 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1276 9f2369b0 2018-12-24 stsp if (err)
1277 9f2369b0 2018-12-24 stsp return err;
1278 9f2369b0 2018-12-24 stsp
1279 9f2369b0 2018-12-24 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1280 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
1281 9f2369b0 2018-12-24 stsp
1282 9f2369b0 2018-12-24 stsp return err;
1283 9f2369b0 2018-12-24 stsp }
1284 9f2369b0 2018-12-24 stsp
1285 9f2369b0 2018-12-24 stsp static const struct got_error *
1286 ac544f8c 2019-01-13 stsp read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1287 d5c81d44 2021-07-08 stsp int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1288 9f2369b0 2018-12-24 stsp {
1289 ddc7b220 2019-09-08 stsp const struct got_error *err;
1290 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1291 9f2369b0 2018-12-24 stsp pid_t pid;
1292 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1293 9f2369b0 2018-12-24 stsp
1294 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1295 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1296 d5c81d44 2021-07-08 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1297 d5c81d44 2021-07-08 stsp ibuf);
1298 9f2369b0 2018-12-24 stsp }
1299 9f2369b0 2018-12-24 stsp
1300 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1301 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1302 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1303 9f2369b0 2018-12-24 stsp
1304 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1305 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1306 ddc7b220 2019-09-08 stsp free(ibuf);
1307 ddc7b220 2019-09-08 stsp return err;
1308 ddc7b220 2019-09-08 stsp }
1309 9f2369b0 2018-12-24 stsp
1310 9f2369b0 2018-12-24 stsp pid = fork();
1311 ddc7b220 2019-09-08 stsp if (pid == -1) {
1312 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1313 ddc7b220 2019-09-08 stsp free(ibuf);
1314 ddc7b220 2019-09-08 stsp return err;
1315 ddc7b220 2019-09-08 stsp }
1316 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1317 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1318 9f2369b0 2018-12-24 stsp repo->path);
1319 9f2369b0 2018-12-24 stsp /* not reached */
1320 9f2369b0 2018-12-24 stsp }
1321 9f2369b0 2018-12-24 stsp
1322 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
1323 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
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 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1328 9f2369b0 2018-12-24 stsp imsg_fds[0];
1329 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1330 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1331 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1332 9f2369b0 2018-12-24 stsp
1333 d5c81d44 2021-07-08 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1334 ebc55e2d 2018-12-24 stsp }
1335 68482ea3 2017-11-27 stsp
1336 ebc55e2d 2018-12-24 stsp static const struct got_error *
1337 ebc55e2d 2018-12-24 stsp open_blob(struct got_blob_object **blob, struct got_repository *repo,
1338 ebc55e2d 2018-12-24 stsp struct got_object_id *id, size_t blocksize)
1339 ebc55e2d 2018-12-24 stsp {
1340 ebc55e2d 2018-12-24 stsp const struct got_error *err = NULL;
1341 ebc55e2d 2018-12-24 stsp struct got_packidx *packidx = NULL;
1342 ebc55e2d 2018-12-24 stsp int idx;
1343 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1344 ac544f8c 2019-01-13 stsp uint8_t *outbuf;
1345 e82b1d81 2019-07-27 stsp int outfd;
1346 ebc55e2d 2018-12-24 stsp size_t size, hdrlen;
1347 ebc55e2d 2018-12-24 stsp struct stat sb;
1348 ebc55e2d 2018-12-24 stsp
1349 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
1350 4558fcd4 2018-01-14 stsp if (*blob == NULL)
1351 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1352 68482ea3 2017-11-27 stsp
1353 55da3778 2018-09-10 stsp outfd = got_opentempfd();
1354 55da3778 2018-09-10 stsp if (outfd == -1)
1355 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
1356 55da3778 2018-09-10 stsp
1357 062ebb78 2018-07-12 stsp (*blob)->read_buf = malloc(blocksize);
1358 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
1359 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1360 c7254d79 2018-04-24 stsp goto done;
1361 15c8b0e6 2018-04-24 stsp }
1362 ebc55e2d 2018-12-24 stsp
1363 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1364 e82b1d81 2019-07-27 stsp if (err == NULL) {
1365 ebc55e2d 2018-12-24 stsp struct got_pack *pack = NULL;
1366 34f480ff 2019-07-27 stsp
1367 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
1368 aea75d87 2021-07-06 stsp packidx->path_packidx);
1369 ebc55e2d 2018-12-24 stsp if (err)
1370 ebc55e2d 2018-12-24 stsp goto done;
1371 ebc55e2d 2018-12-24 stsp
1372 ebc55e2d 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1373 55da3778 2018-09-10 stsp if (pack == NULL) {
1374 ebc55e2d 2018-12-24 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1375 ebc55e2d 2018-12-24 stsp packidx);
1376 55da3778 2018-09-10 stsp if (err)
1377 55da3778 2018-09-10 stsp goto done;
1378 55da3778 2018-09-10 stsp }
1379 ac544f8c 2019-01-13 stsp err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1380 ac544f8c 2019-01-13 stsp pack, packidx, idx, id);
1381 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1382 e82b1d81 2019-07-27 stsp int infd;
1383 e82b1d81 2019-07-27 stsp
1384 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&infd, id, repo);
1385 e82b1d81 2019-07-27 stsp if (err)
1386 e82b1d81 2019-07-27 stsp goto done;
1387 ac544f8c 2019-01-13 stsp err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1388 d5c81d44 2021-07-08 stsp id, repo);
1389 e82b1d81 2019-07-27 stsp }
1390 ebc55e2d 2018-12-24 stsp if (err)
1391 55da3778 2018-09-10 stsp goto done;
1392 2967a784 2018-04-24 stsp
1393 de060dff 2018-12-24 stsp if (hdrlen > size) {
1394 ebc55e2d 2018-12-24 stsp err = got_error(GOT_ERR_BAD_OBJ_HDR);
1395 ebc55e2d 2018-12-24 stsp goto done;
1396 ebc55e2d 2018-12-24 stsp }
1397 ebc55e2d 2018-12-24 stsp
1398 ac544f8c 2019-01-13 stsp if (outbuf) {
1399 08578a35 2021-01-22 stsp if (close(outfd) == -1 && err == NULL)
1400 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1401 ac544f8c 2019-01-13 stsp outfd = -1;
1402 ac544f8c 2019-01-13 stsp (*blob)->f = fmemopen(outbuf, size, "rb");
1403 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1404 638f9024 2019-05-13 stsp err = got_error_from_errno("fmemopen");
1405 ac544f8c 2019-01-13 stsp free(outbuf);
1406 ac544f8c 2019-01-13 stsp goto done;
1407 ac544f8c 2019-01-13 stsp }
1408 ac544f8c 2019-01-13 stsp (*blob)->data = outbuf;
1409 ac544f8c 2019-01-13 stsp } else {
1410 ac544f8c 2019-01-13 stsp if (fstat(outfd, &sb) == -1) {
1411 638f9024 2019-05-13 stsp err = got_error_from_errno("fstat");
1412 ac544f8c 2019-01-13 stsp goto done;
1413 ac544f8c 2019-01-13 stsp }
1414 ac544f8c 2019-01-13 stsp
1415 ac544f8c 2019-01-13 stsp if (sb.st_size != size) {
1416 ac544f8c 2019-01-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1417 ac544f8c 2019-01-13 stsp goto done;
1418 ac544f8c 2019-01-13 stsp }
1419 ac544f8c 2019-01-13 stsp
1420 ac544f8c 2019-01-13 stsp (*blob)->f = fdopen(outfd, "rb");
1421 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1422 638f9024 2019-05-13 stsp err = got_error_from_errno("fdopen");
1423 ac544f8c 2019-01-13 stsp close(outfd);
1424 ac544f8c 2019-01-13 stsp outfd = -1;
1425 ac544f8c 2019-01-13 stsp goto done;
1426 ac544f8c 2019-01-13 stsp }
1427 68482ea3 2017-11-27 stsp }
1428 68482ea3 2017-11-27 stsp
1429 ebc55e2d 2018-12-24 stsp (*blob)->hdrlen = hdrlen;
1430 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
1431 ebc55e2d 2018-12-24 stsp memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1432 7d283eee 2017-11-29 stsp
1433 c7254d79 2018-04-24 stsp done:
1434 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1435 55da3778 2018-09-10 stsp if (err) {
1436 55da3778 2018-09-10 stsp if (*blob) {
1437 7baf5860 2019-03-19 stsp got_object_blob_close(*blob);
1438 55da3778 2018-09-10 stsp *blob = NULL;
1439 55da3778 2018-09-10 stsp } else if (outfd != -1)
1440 55da3778 2018-09-10 stsp close(outfd);
1441 a19581a2 2018-06-21 stsp }
1442 a19581a2 2018-06-21 stsp return err;
1443 a19581a2 2018-06-21 stsp }
1444 a19581a2 2018-06-21 stsp
1445 a19581a2 2018-06-21 stsp const struct got_error *
1446 a19581a2 2018-06-21 stsp got_object_open_as_blob(struct got_blob_object **blob,
1447 a19581a2 2018-06-21 stsp struct got_repository *repo, struct got_object_id *id,
1448 a19581a2 2018-06-21 stsp size_t blocksize)
1449 a19581a2 2018-06-21 stsp {
1450 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, id, blocksize);
1451 ebc55e2d 2018-12-24 stsp }
1452 835e0dbd 2018-06-21 stsp
1453 ebc55e2d 2018-12-24 stsp const struct got_error *
1454 ebc55e2d 2018-12-24 stsp got_object_blob_open(struct got_blob_object **blob,
1455 ebc55e2d 2018-12-24 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
1456 ebc55e2d 2018-12-24 stsp {
1457 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1458 0ffeb3c2 2017-11-26 stsp }
1459 68482ea3 2017-11-27 stsp
1460 fb43ecf1 2019-02-11 stsp const struct got_error *
1461 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
1462 68482ea3 2017-11-27 stsp {
1463 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL;
1464 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
1465 56b63ca4 2021-01-22 stsp if (blob->f && fclose(blob->f) == EOF)
1466 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1467 ac544f8c 2019-01-13 stsp free(blob->data);
1468 68482ea3 2017-11-27 stsp free(blob);
1469 fb43ecf1 2019-02-11 stsp return err;
1470 f934cf2c 2018-02-12 stsp }
1471 f934cf2c 2018-02-12 stsp
1472 8ba819a3 2020-07-23 stsp void
1473 8ba819a3 2020-07-23 stsp got_object_blob_rewind(struct got_blob_object *blob)
1474 8ba819a3 2020-07-23 stsp {
1475 8ba819a3 2020-07-23 stsp if (blob->f)
1476 8ba819a3 2020-07-23 stsp rewind(blob->f);
1477 8ba819a3 2020-07-23 stsp }
1478 8ba819a3 2020-07-23 stsp
1479 f934cf2c 2018-02-12 stsp char *
1480 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1481 f934cf2c 2018-02-12 stsp {
1482 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1483 f934cf2c 2018-02-12 stsp }
1484 f934cf2c 2018-02-12 stsp
1485 f934cf2c 2018-02-12 stsp size_t
1486 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
1487 f934cf2c 2018-02-12 stsp {
1488 f934cf2c 2018-02-12 stsp return blob->hdrlen;
1489 68482ea3 2017-11-27 stsp }
1490 68482ea3 2017-11-27 stsp
1491 f934cf2c 2018-02-12 stsp const uint8_t *
1492 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
1493 f934cf2c 2018-02-12 stsp {
1494 f934cf2c 2018-02-12 stsp return blob->read_buf;
1495 f934cf2c 2018-02-12 stsp }
1496 f934cf2c 2018-02-12 stsp
1497 68482ea3 2017-11-27 stsp const struct got_error *
1498 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1499 68482ea3 2017-11-27 stsp {
1500 eb651edf 2018-02-11 stsp size_t n;
1501 eb651edf 2018-02-11 stsp
1502 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1503 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
1504 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
1505 eb651edf 2018-02-11 stsp *outlenp = n;
1506 35e9ba5d 2018-06-21 stsp return NULL;
1507 35e9ba5d 2018-06-21 stsp }
1508 35e9ba5d 2018-06-21 stsp
1509 35e9ba5d 2018-06-21 stsp const struct got_error *
1510 be659d10 2020-11-18 stsp got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1511 6c4c42e0 2019-06-24 stsp off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1512 35e9ba5d 2018-06-21 stsp {
1513 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
1514 b6752625 2018-12-24 stsp size_t n, len, hdrlen;
1515 84451b3e 2018-07-10 stsp const uint8_t *buf;
1516 84451b3e 2018-07-10 stsp int i;
1517 c33ebc60 2020-11-18 stsp const int alloc_chunksz = 512;
1518 c33ebc60 2020-11-18 stsp size_t nalloc = 0;
1519 f595d9bd 2019-08-14 stsp off_t off = 0, total_len = 0;
1520 84451b3e 2018-07-10 stsp
1521 6c4c42e0 2019-06-24 stsp if (line_offsets)
1522 6c4c42e0 2019-06-24 stsp *line_offsets = NULL;
1523 f595d9bd 2019-08-14 stsp if (filesize)
1524 f595d9bd 2019-08-14 stsp *filesize = 0;
1525 84451b3e 2018-07-10 stsp if (nlines)
1526 84451b3e 2018-07-10 stsp *nlines = 0;
1527 35e9ba5d 2018-06-21 stsp
1528 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1529 35e9ba5d 2018-06-21 stsp do {
1530 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
1531 35e9ba5d 2018-06-21 stsp if (err)
1532 35e9ba5d 2018-06-21 stsp return err;
1533 35e9ba5d 2018-06-21 stsp if (len == 0)
1534 35e9ba5d 2018-06-21 stsp break;
1535 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
1536 b02560ec 2019-08-19 stsp i = hdrlen;
1537 f1cbc3bc 2020-11-18 stsp if (nlines) {
1538 f1cbc3bc 2020-11-18 stsp if (line_offsets && *line_offsets == NULL) {
1539 78695fb7 2019-08-12 stsp /* Have some data but perhaps no '\n'. */
1540 78695fb7 2019-08-12 stsp *nlines = 1;
1541 c33ebc60 2020-11-18 stsp nalloc = alloc_chunksz;
1542 c33ebc60 2020-11-18 stsp *line_offsets = calloc(nalloc,
1543 c33ebc60 2020-11-18 stsp sizeof(**line_offsets));
1544 78695fb7 2019-08-12 stsp if (*line_offsets == NULL)
1545 845785d4 2020-02-02 tracey return got_error_from_errno("calloc");
1546 b02560ec 2019-08-19 stsp
1547 b02560ec 2019-08-19 stsp /* Skip forward over end of first line. */
1548 b02560ec 2019-08-19 stsp while (i < len) {
1549 b02560ec 2019-08-19 stsp if (buf[i] == '\n')
1550 b02560ec 2019-08-19 stsp break;
1551 b02560ec 2019-08-19 stsp i++;
1552 b02560ec 2019-08-19 stsp }
1553 b02560ec 2019-08-19 stsp }
1554 b02560ec 2019-08-19 stsp /* Scan '\n' offsets in remaining chunk of data. */
1555 b02560ec 2019-08-19 stsp while (i < len) {
1556 b02560ec 2019-08-19 stsp if (buf[i] != '\n') {
1557 b02560ec 2019-08-19 stsp i++;
1558 f595d9bd 2019-08-14 stsp continue;
1559 b02560ec 2019-08-19 stsp }
1560 f595d9bd 2019-08-14 stsp (*nlines)++;
1561 c33ebc60 2020-11-18 stsp if (line_offsets && nalloc < *nlines) {
1562 c33ebc60 2020-11-18 stsp size_t n = *nlines + alloc_chunksz;
1563 78695fb7 2019-08-12 stsp off_t *o = recallocarray(*line_offsets,
1564 c33ebc60 2020-11-18 stsp nalloc, n, sizeof(**line_offsets));
1565 78695fb7 2019-08-12 stsp if (o == NULL) {
1566 78695fb7 2019-08-12 stsp free(*line_offsets);
1567 78695fb7 2019-08-12 stsp *line_offsets = NULL;
1568 78695fb7 2019-08-12 stsp return got_error_from_errno(
1569 78695fb7 2019-08-12 stsp "recallocarray");
1570 78695fb7 2019-08-12 stsp }
1571 78695fb7 2019-08-12 stsp *line_offsets = o;
1572 c33ebc60 2020-11-18 stsp nalloc = n;
1573 78695fb7 2019-08-12 stsp }
1574 f1cbc3bc 2020-11-18 stsp if (line_offsets) {
1575 f1cbc3bc 2020-11-18 stsp off = total_len + i - hdrlen + 1;
1576 f1cbc3bc 2020-11-18 stsp (*line_offsets)[*nlines - 1] = off;
1577 f1cbc3bc 2020-11-18 stsp }
1578 b02560ec 2019-08-19 stsp i++;
1579 6c4c42e0 2019-06-24 stsp }
1580 84451b3e 2018-07-10 stsp }
1581 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
1582 454a6b59 2018-12-24 stsp n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1583 b6752625 2018-12-24 stsp if (n != len - hdrlen)
1584 b6752625 2018-12-24 stsp return got_ferror(outfile, GOT_ERR_IO);
1585 f595d9bd 2019-08-14 stsp total_len += len - hdrlen;
1586 35e9ba5d 2018-06-21 stsp hdrlen = 0;
1587 35e9ba5d 2018-06-21 stsp } while (len != 0);
1588 35e9ba5d 2018-06-21 stsp
1589 cbe7f848 2019-02-11 stsp if (fflush(outfile) != 0)
1590 638f9024 2019-05-13 stsp return got_error_from_errno("fflush");
1591 35e9ba5d 2018-06-21 stsp rewind(outfile);
1592 35e9ba5d 2018-06-21 stsp
1593 f595d9bd 2019-08-14 stsp if (filesize)
1594 f595d9bd 2019-08-14 stsp *filesize = total_len;
1595 f595d9bd 2019-08-14 stsp
1596 776d4d29 2018-06-17 stsp return NULL;
1597 f4a881ce 2018-11-17 stsp }
1598 f4a881ce 2018-11-17 stsp
1599 f4a881ce 2018-11-17 stsp static const struct got_error *
1600 268f7291 2018-12-24 stsp request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1601 268f7291 2018-12-24 stsp int pack_idx, struct got_object_id *id)
1602 a158c901 2018-12-23 stsp {
1603 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
1604 a158c901 2018-12-23 stsp
1605 268f7291 2018-12-24 stsp err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1606 268f7291 2018-12-24 stsp pack_idx);
1607 a158c901 2018-12-23 stsp if (err)
1608 a158c901 2018-12-23 stsp return err;
1609 a158c901 2018-12-23 stsp
1610 a158c901 2018-12-23 stsp return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1611 268f7291 2018-12-24 stsp }
1612 268f7291 2018-12-24 stsp
1613 268f7291 2018-12-24 stsp static const struct got_error *
1614 268f7291 2018-12-24 stsp read_packed_tag_privsep(struct got_tag_object **tag,
1615 268f7291 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1616 268f7291 2018-12-24 stsp struct got_object_id *id)
1617 268f7291 2018-12-24 stsp {
1618 268f7291 2018-12-24 stsp const struct got_error *err = NULL;
1619 268f7291 2018-12-24 stsp
1620 268f7291 2018-12-24 stsp if (pack->privsep_child)
1621 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1622 268f7291 2018-12-24 stsp
1623 68036464 2022-06-26 thomas err = got_pack_start_privsep_child(pack, packidx);
1624 268f7291 2018-12-24 stsp if (err)
1625 268f7291 2018-12-24 stsp return err;
1626 268f7291 2018-12-24 stsp
1627 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1628 a158c901 2018-12-23 stsp }
1629 9f2369b0 2018-12-24 stsp
1630 9f2369b0 2018-12-24 stsp static const struct got_error *
1631 9f2369b0 2018-12-24 stsp request_tag(struct got_tag_object **tag, struct got_repository *repo,
1632 d5c81d44 2021-07-08 stsp int fd, struct got_object_id *id)
1633 9f2369b0 2018-12-24 stsp {
1634 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1635 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1636 9f2369b0 2018-12-24 stsp
1637 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1638 9f2369b0 2018-12-24 stsp
1639 d5c81d44 2021-07-08 stsp err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1640 9f2369b0 2018-12-24 stsp if (err)
1641 9f2369b0 2018-12-24 stsp return err;
1642 9f2369b0 2018-12-24 stsp
1643 9f2369b0 2018-12-24 stsp return got_privsep_recv_tag(tag, ibuf);
1644 9f2369b0 2018-12-24 stsp }
1645 9f2369b0 2018-12-24 stsp
1646 9f2369b0 2018-12-24 stsp static const struct got_error *
1647 9f2369b0 2018-12-24 stsp read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1648 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo)
1649 9f2369b0 2018-12-24 stsp {
1650 ddc7b220 2019-09-08 stsp const struct got_error *err;
1651 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1652 9f2369b0 2018-12-24 stsp pid_t pid;
1653 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1654 9f2369b0 2018-12-24 stsp
1655 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1656 d5c81d44 2021-07-08 stsp return request_tag(tag, repo, obj_fd, id);
1657 9f2369b0 2018-12-24 stsp
1658 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1659 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1660 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1661 9f2369b0 2018-12-24 stsp
1662 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1663 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1664 ddc7b220 2019-09-08 stsp free(ibuf);
1665 ddc7b220 2019-09-08 stsp return err;
1666 ddc7b220 2019-09-08 stsp }
1667 9f2369b0 2018-12-24 stsp
1668 9f2369b0 2018-12-24 stsp pid = fork();
1669 ddc7b220 2019-09-08 stsp if (pid == -1) {
1670 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1671 ddc7b220 2019-09-08 stsp free(ibuf);
1672 ddc7b220 2019-09-08 stsp return err;
1673 ddc7b220 2019-09-08 stsp }
1674 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1675 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1676 9f2369b0 2018-12-24 stsp repo->path);
1677 9f2369b0 2018-12-24 stsp /* not reached */
1678 9f2369b0 2018-12-24 stsp }
1679 9f2369b0 2018-12-24 stsp
1680 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
1681 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
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 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1686 9f2369b0 2018-12-24 stsp imsg_fds[0];
1687 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1688 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1689 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1690 a158c901 2018-12-23 stsp
1691 d5c81d44 2021-07-08 stsp return request_tag(tag, repo, obj_fd, id);
1692 9f2369b0 2018-12-24 stsp }
1693 a158c901 2018-12-23 stsp
1694 a158c901 2018-12-23 stsp static const struct got_error *
1695 268f7291 2018-12-24 stsp open_tag(struct got_tag_object **tag, struct got_repository *repo,
1696 268f7291 2018-12-24 stsp struct got_object_id *id, int check_cache)
1697 f4a881ce 2018-11-17 stsp {
1698 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1699 268f7291 2018-12-24 stsp struct got_packidx *packidx = NULL;
1700 e82b1d81 2019-07-27 stsp int idx;
1701 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1702 5d844a1e 2019-08-13 stsp struct got_object *obj = NULL;
1703 5d844a1e 2019-08-13 stsp int obj_type = GOT_OBJ_TYPE_ANY;
1704 f4a881ce 2018-11-17 stsp
1705 f4a881ce 2018-11-17 stsp if (check_cache) {
1706 268f7291 2018-12-24 stsp *tag = got_repo_get_cached_tag(repo, id);
1707 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1708 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1709 f4a881ce 2018-11-17 stsp return NULL;
1710 f4a881ce 2018-11-17 stsp }
1711 f4a881ce 2018-11-17 stsp } else
1712 f4a881ce 2018-11-17 stsp *tag = NULL;
1713 f4a881ce 2018-11-17 stsp
1714 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1715 e82b1d81 2019-07-27 stsp if (err == NULL) {
1716 268f7291 2018-12-24 stsp struct got_pack *pack = NULL;
1717 f4a881ce 2018-11-17 stsp
1718 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
1719 aea75d87 2021-07-06 stsp packidx->path_packidx);
1720 268f7291 2018-12-24 stsp if (err)
1721 268f7291 2018-12-24 stsp return err;
1722 268f7291 2018-12-24 stsp
1723 268f7291 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1724 f4a881ce 2018-11-17 stsp if (pack == NULL) {
1725 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1726 e82b1d81 2019-07-27 stsp packidx);
1727 f4a881ce 2018-11-17 stsp if (err)
1728 8d2c5ea3 2019-08-13 stsp goto done;
1729 f4a881ce 2018-11-17 stsp }
1730 5d844a1e 2019-08-13 stsp
1731 992eb9d8 2020-02-07 tracey /* Beware of "lightweight" tags: Check object type first. */
1732 5d844a1e 2019-08-13 stsp err = read_packed_object_privsep(&obj, repo, pack, packidx,
1733 5d844a1e 2019-08-13 stsp idx, id);
1734 5d844a1e 2019-08-13 stsp if (err)
1735 5d844a1e 2019-08-13 stsp goto done;
1736 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1737 5d844a1e 2019-08-13 stsp got_object_close(obj);
1738 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG) {
1739 5d844a1e 2019-08-13 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1740 5d844a1e 2019-08-13 stsp goto done;
1741 5d844a1e 2019-08-13 stsp }
1742 5d844a1e 2019-08-13 stsp err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1743 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1744 e82b1d81 2019-07-27 stsp int fd;
1745 e82b1d81 2019-07-27 stsp
1746 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1747 e82b1d81 2019-07-27 stsp if (err)
1748 e82b1d81 2019-07-27 stsp return err;
1749 d5c81d44 2021-07-08 stsp err = got_object_read_header_privsep(&obj, id, repo, fd);
1750 5d844a1e 2019-08-13 stsp if (err)
1751 5d844a1e 2019-08-13 stsp return err;
1752 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1753 5d844a1e 2019-08-13 stsp got_object_close(obj);
1754 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
1755 5d844a1e 2019-08-13 stsp return got_error(GOT_ERR_OBJ_TYPE);
1756 5d844a1e 2019-08-13 stsp
1757 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1758 5d844a1e 2019-08-13 stsp if (err)
1759 5d844a1e 2019-08-13 stsp return err;
1760 d5c81d44 2021-07-08 stsp err = read_tag_privsep(tag, fd, id, repo);
1761 e82b1d81 2019-07-27 stsp }
1762 f4a881ce 2018-11-17 stsp
1763 f4a881ce 2018-11-17 stsp if (err == NULL) {
1764 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1765 268f7291 2018-12-24 stsp err = got_repo_cache_tag(repo, id, *tag);
1766 f4a881ce 2018-11-17 stsp }
1767 8d2c5ea3 2019-08-13 stsp done:
1768 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1769 f4a881ce 2018-11-17 stsp return err;
1770 f4a881ce 2018-11-17 stsp }
1771 f4a881ce 2018-11-17 stsp
1772 f4a881ce 2018-11-17 stsp const struct got_error *
1773 f4a881ce 2018-11-17 stsp got_object_open_as_tag(struct got_tag_object **tag,
1774 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object_id *id)
1775 f4a881ce 2018-11-17 stsp {
1776 f4a881ce 2018-11-17 stsp *tag = got_repo_get_cached_tag(repo, id);
1777 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1778 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1779 f4a881ce 2018-11-17 stsp return NULL;
1780 f4a881ce 2018-11-17 stsp }
1781 f4a881ce 2018-11-17 stsp
1782 268f7291 2018-12-24 stsp return open_tag(tag, repo, id, 0);
1783 f4a881ce 2018-11-17 stsp }
1784 f4a881ce 2018-11-17 stsp
1785 f4a881ce 2018-11-17 stsp const struct got_error *
1786 f4a881ce 2018-11-17 stsp got_object_tag_open(struct got_tag_object **tag,
1787 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object *obj)
1788 f4a881ce 2018-11-17 stsp {
1789 268f7291 2018-12-24 stsp return open_tag(tag, repo, got_object_get_id(obj), 1);
1790 d24820bf 2019-08-11 stsp }
1791 d24820bf 2019-08-11 stsp
1792 d24820bf 2019-08-11 stsp const char *
1793 d24820bf 2019-08-11 stsp got_object_tag_get_name(struct got_tag_object *tag)
1794 d24820bf 2019-08-11 stsp {
1795 d24820bf 2019-08-11 stsp return tag->tag;
1796 0bd18d37 2019-02-01 stsp }
1797 0bd18d37 2019-02-01 stsp
1798 0bd18d37 2019-02-01 stsp int
1799 0bd18d37 2019-02-01 stsp got_object_tag_get_object_type(struct got_tag_object *tag)
1800 0bd18d37 2019-02-01 stsp {
1801 0bd18d37 2019-02-01 stsp return tag->obj_type;
1802 0bd18d37 2019-02-01 stsp }
1803 0bd18d37 2019-02-01 stsp
1804 0bd18d37 2019-02-01 stsp struct got_object_id *
1805 0bd18d37 2019-02-01 stsp got_object_tag_get_object_id(struct got_tag_object *tag)
1806 0bd18d37 2019-02-01 stsp {
1807 0bd18d37 2019-02-01 stsp return &tag->id;
1808 01073a5d 2019-08-22 stsp }
1809 01073a5d 2019-08-22 stsp
1810 01073a5d 2019-08-22 stsp time_t
1811 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_time(struct got_tag_object *tag)
1812 01073a5d 2019-08-22 stsp {
1813 01073a5d 2019-08-22 stsp return tag->tagger_time;
1814 01073a5d 2019-08-22 stsp }
1815 01073a5d 2019-08-22 stsp
1816 01073a5d 2019-08-22 stsp time_t
1817 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1818 01073a5d 2019-08-22 stsp {
1819 01073a5d 2019-08-22 stsp return tag->tagger_gmtoff;
1820 01073a5d 2019-08-22 stsp }
1821 01073a5d 2019-08-22 stsp
1822 01073a5d 2019-08-22 stsp const char *
1823 01073a5d 2019-08-22 stsp got_object_tag_get_tagger(struct got_tag_object *tag)
1824 01073a5d 2019-08-22 stsp {
1825 01073a5d 2019-08-22 stsp return tag->tagger;
1826 776d4d29 2018-06-17 stsp }
1827 776d4d29 2018-06-17 stsp
1828 01073a5d 2019-08-22 stsp const char *
1829 01073a5d 2019-08-22 stsp got_object_tag_get_message(struct got_tag_object *tag)
1830 01073a5d 2019-08-22 stsp {
1831 01073a5d 2019-08-22 stsp return tag->tagmsg;
1832 01073a5d 2019-08-22 stsp }
1833 01073a5d 2019-08-22 stsp
1834 776d4d29 2018-06-17 stsp static struct got_tree_entry *
1835 65a9bbe9 2018-09-15 stsp find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1836 776d4d29 2018-06-17 stsp {
1837 56e0773d 2019-11-28 stsp int i;
1838 776d4d29 2018-06-17 stsp
1839 63da309a 2018-11-07 stsp /* Note that tree entries are sorted in strncmp() order. */
1840 56e0773d 2019-11-28 stsp for (i = 0; i < tree->nentries; i++) {
1841 56e0773d 2019-11-28 stsp struct got_tree_entry *te = &tree->entries[i];
1842 63da309a 2018-11-07 stsp int cmp = strncmp(te->name, name, len);
1843 63da309a 2018-11-07 stsp if (cmp < 0)
1844 63da309a 2018-11-07 stsp continue;
1845 63da309a 2018-11-07 stsp if (cmp > 0)
1846 63da309a 2018-11-07 stsp break;
1847 63da309a 2018-11-07 stsp if (te->name[len] == '\0')
1848 776d4d29 2018-06-17 stsp return te;
1849 776d4d29 2018-06-17 stsp }
1850 eb651edf 2018-02-11 stsp return NULL;
1851 a129376b 2019-03-28 stsp }
1852 a129376b 2019-03-28 stsp
1853 56e0773d 2019-11-28 stsp struct got_tree_entry *
1854 a129376b 2019-03-28 stsp got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1855 a129376b 2019-03-28 stsp {
1856 a129376b 2019-03-28 stsp return find_entry_by_name(tree, name, strlen(name));
1857 776d4d29 2018-06-17 stsp }
1858 776d4d29 2018-06-17 stsp
1859 776d4d29 2018-06-17 stsp const struct got_error *
1860 cc8021af 2021-10-12 thomas got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1861 cc8021af 2021-10-12 thomas struct got_repository *repo, struct got_tree_object *tree,
1862 cc8021af 2021-10-12 thomas const char *path)
1863 776d4d29 2018-06-17 stsp {
1864 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
1865 cc8021af 2021-10-12 thomas struct got_tree_object *subtree = NULL;
1866 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
1867 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1868 b7cd37e5 2018-11-18 stsp size_t seglen;
1869 776d4d29 2018-06-17 stsp
1870 27d434c2 2018-09-15 stsp *id = NULL;
1871 776d4d29 2018-06-17 stsp
1872 65a9bbe9 2018-09-15 stsp s = path;
1873 5e54fb30 2019-05-31 stsp while (s[0] == '/')
1874 5e54fb30 2019-05-31 stsp s++;
1875 776d4d29 2018-06-17 stsp seg = s;
1876 65a9bbe9 2018-09-15 stsp seglen = 0;
1877 cc8021af 2021-10-12 thomas subtree = tree;
1878 b7cd37e5 2018-11-18 stsp while (*s) {
1879 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
1880 776d4d29 2018-06-17 stsp
1881 776d4d29 2018-06-17 stsp if (*s != '/') {
1882 776d4d29 2018-06-17 stsp s++;
1883 65a9bbe9 2018-09-15 stsp seglen++;
1884 00530cfb 2018-06-21 stsp if (*s)
1885 00530cfb 2018-06-21 stsp continue;
1886 776d4d29 2018-06-17 stsp }
1887 776d4d29 2018-06-17 stsp
1888 cc8021af 2021-10-12 thomas te = find_entry_by_name(subtree, seg, seglen);
1889 db37e2c0 2018-06-21 stsp if (te == NULL) {
1890 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1891 776d4d29 2018-06-17 stsp goto done;
1892 776d4d29 2018-06-17 stsp }
1893 776d4d29 2018-06-17 stsp
1894 b7cd37e5 2018-11-18 stsp if (*s == '\0')
1895 67606321 2018-06-21 stsp break;
1896 67606321 2018-06-21 stsp
1897 776d4d29 2018-06-17 stsp seg = s + 1;
1898 65a9bbe9 2018-09-15 stsp seglen = 0;
1899 776d4d29 2018-06-17 stsp s++;
1900 776d4d29 2018-06-17 stsp if (*s) {
1901 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
1902 56e0773d 2019-11-28 stsp &te->id);
1903 db37e2c0 2018-06-21 stsp te = NULL;
1904 776d4d29 2018-06-17 stsp if (err)
1905 776d4d29 2018-06-17 stsp goto done;
1906 cc8021af 2021-10-12 thomas if (subtree != tree)
1907 cc8021af 2021-10-12 thomas got_object_tree_close(subtree);
1908 cc8021af 2021-10-12 thomas subtree = next_tree;
1909 776d4d29 2018-06-17 stsp }
1910 776d4d29 2018-06-17 stsp }
1911 776d4d29 2018-06-17 stsp
1912 27d434c2 2018-09-15 stsp if (te) {
1913 56e0773d 2019-11-28 stsp *id = got_object_id_dup(&te->id);
1914 27d434c2 2018-09-15 stsp if (*id == NULL)
1915 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
1916 cc8021af 2021-10-12 thomas if (mode)
1917 cc8021af 2021-10-12 thomas *mode = te->mode;
1918 27d434c2 2018-09-15 stsp } else
1919 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1920 cc8021af 2021-10-12 thomas done:
1921 cc8021af 2021-10-12 thomas if (subtree && subtree != tree)
1922 cc8021af 2021-10-12 thomas got_object_tree_close(subtree);
1923 cc8021af 2021-10-12 thomas return err;
1924 cc8021af 2021-10-12 thomas }
1925 cc8021af 2021-10-12 thomas const struct got_error *
1926 cc8021af 2021-10-12 thomas got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1927 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
1928 cc8021af 2021-10-12 thomas {
1929 cc8021af 2021-10-12 thomas const struct got_error *err = NULL;
1930 cc8021af 2021-10-12 thomas struct got_tree_object *tree = NULL;
1931 cc8021af 2021-10-12 thomas
1932 cc8021af 2021-10-12 thomas *id = NULL;
1933 cc8021af 2021-10-12 thomas
1934 cc8021af 2021-10-12 thomas /* Handle opening of root of commit's tree. */
1935 cc8021af 2021-10-12 thomas if (got_path_is_root_dir(path)) {
1936 cc8021af 2021-10-12 thomas *id = got_object_id_dup(commit->tree_id);
1937 cc8021af 2021-10-12 thomas if (*id == NULL)
1938 cc8021af 2021-10-12 thomas err = got_error_from_errno("got_object_id_dup");
1939 cc8021af 2021-10-12 thomas } else {
1940 cc8021af 2021-10-12 thomas err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1941 cc8021af 2021-10-12 thomas if (err)
1942 cc8021af 2021-10-12 thomas goto done;
1943 cc8021af 2021-10-12 thomas err = got_object_tree_find_path(id, NULL, repo, tree, path);
1944 cc8021af 2021-10-12 thomas }
1945 776d4d29 2018-06-17 stsp done:
1946 776d4d29 2018-06-17 stsp if (tree)
1947 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
1948 776d4d29 2018-06-17 stsp return err;
1949 ac5f2b26 2020-05-05 stsp }
1950 ac5f2b26 2020-05-05 stsp
1951 ac5f2b26 2020-05-05 stsp /*
1952 ac5f2b26 2020-05-05 stsp * Normalize file mode bits to avoid false positive tree entry differences
1953 ac5f2b26 2020-05-05 stsp * in case tree entries have unexpected mode bits set.
1954 ac5f2b26 2020-05-05 stsp */
1955 ac5f2b26 2020-05-05 stsp static mode_t
1956 ac5f2b26 2020-05-05 stsp normalize_mode_for_comparison(mode_t mode)
1957 ac5f2b26 2020-05-05 stsp {
1958 ac5f2b26 2020-05-05 stsp /*
1959 ac5f2b26 2020-05-05 stsp * For directories, the only relevant bit is the IFDIR bit.
1960 ac5f2b26 2020-05-05 stsp * This allows us to detect paths changing from a directory
1961 ac5f2b26 2020-05-05 stsp * to a file and vice versa.
1962 ac5f2b26 2020-05-05 stsp */
1963 ac5f2b26 2020-05-05 stsp if (S_ISDIR(mode))
1964 ac5f2b26 2020-05-05 stsp return mode & S_IFDIR;
1965 40dde666 2020-07-23 stsp
1966 40dde666 2020-07-23 stsp /*
1967 40dde666 2020-07-23 stsp * For symlinks, the only relevant bit is the IFLNK bit.
1968 40dde666 2020-07-23 stsp * This allows us to detect paths changing from a symlinks
1969 40dde666 2020-07-23 stsp * to a file or directory and vice versa.
1970 40dde666 2020-07-23 stsp */
1971 40dde666 2020-07-23 stsp if (S_ISLNK(mode))
1972 40dde666 2020-07-23 stsp return mode & S_IFLNK;
1973 ac5f2b26 2020-05-05 stsp
1974 ac5f2b26 2020-05-05 stsp /* For files, the only change we care about is the executable bit. */
1975 ac5f2b26 2020-05-05 stsp return mode & S_IXUSR;
1976 68482ea3 2017-11-27 stsp }
1977 07862c20 2018-09-15 stsp
1978 07862c20 2018-09-15 stsp const struct got_error *
1979 07862c20 2018-09-15 stsp got_object_tree_path_changed(int *changed,
1980 07862c20 2018-09-15 stsp struct got_tree_object *tree01, struct got_tree_object *tree02,
1981 07862c20 2018-09-15 stsp const char *path, struct got_repository *repo)
1982 07862c20 2018-09-15 stsp {
1983 07862c20 2018-09-15 stsp const struct got_error *err = NULL;
1984 07862c20 2018-09-15 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1985 07862c20 2018-09-15 stsp struct got_tree_entry *te1 = NULL, *te2 = NULL;
1986 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1987 3b7f9878 2018-11-18 stsp size_t seglen;
1988 07862c20 2018-09-15 stsp
1989 07862c20 2018-09-15 stsp *changed = 0;
1990 07862c20 2018-09-15 stsp
1991 07862c20 2018-09-15 stsp /* We not do support comparing the root path. */
1992 61a7d79f 2020-02-29 stsp if (got_path_is_root_dir(path))
1993 63f810e6 2020-02-29 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
1994 07862c20 2018-09-15 stsp
1995 07862c20 2018-09-15 stsp tree1 = tree01;
1996 07862c20 2018-09-15 stsp tree2 = tree02;
1997 65a9bbe9 2018-09-15 stsp s = path;
1998 61a7d79f 2020-02-29 stsp while (*s == '/')
1999 61a7d79f 2020-02-29 stsp s++;
2000 07862c20 2018-09-15 stsp seg = s;
2001 65a9bbe9 2018-09-15 stsp seglen = 0;
2002 3b7f9878 2018-11-18 stsp while (*s) {
2003 07862c20 2018-09-15 stsp struct got_tree_object *next_tree1, *next_tree2;
2004 ac5f2b26 2020-05-05 stsp mode_t mode1, mode2;
2005 07862c20 2018-09-15 stsp
2006 07862c20 2018-09-15 stsp if (*s != '/') {
2007 07862c20 2018-09-15 stsp s++;
2008 65a9bbe9 2018-09-15 stsp seglen++;
2009 07862c20 2018-09-15 stsp if (*s)
2010 07862c20 2018-09-15 stsp continue;
2011 07862c20 2018-09-15 stsp }
2012 07862c20 2018-09-15 stsp
2013 65a9bbe9 2018-09-15 stsp te1 = find_entry_by_name(tree1, seg, seglen);
2014 07862c20 2018-09-15 stsp if (te1 == NULL) {
2015 07862c20 2018-09-15 stsp err = got_error(GOT_ERR_NO_OBJ);
2016 07862c20 2018-09-15 stsp goto done;
2017 07862c20 2018-09-15 stsp }
2018 07862c20 2018-09-15 stsp
2019 e8bfb8f3 2020-12-18 stsp if (tree2)
2020 e8bfb8f3 2020-12-18 stsp te2 = find_entry_by_name(tree2, seg, seglen);
2021 07862c20 2018-09-15 stsp
2022 e8bfb8f3 2020-12-18 stsp if (te2) {
2023 e8bfb8f3 2020-12-18 stsp mode1 = normalize_mode_for_comparison(te1->mode);
2024 e8bfb8f3 2020-12-18 stsp mode2 = normalize_mode_for_comparison(te2->mode);
2025 e8bfb8f3 2020-12-18 stsp if (mode1 != mode2) {
2026 e8bfb8f3 2020-12-18 stsp *changed = 1;
2027 e8bfb8f3 2020-12-18 stsp goto done;
2028 e8bfb8f3 2020-12-18 stsp }
2029 e8bfb8f3 2020-12-18 stsp
2030 e8bfb8f3 2020-12-18 stsp if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2031 e8bfb8f3 2020-12-18 stsp *changed = 0;
2032 e8bfb8f3 2020-12-18 stsp goto done;
2033 e8bfb8f3 2020-12-18 stsp }
2034 07862c20 2018-09-15 stsp }
2035 07862c20 2018-09-15 stsp
2036 3b7f9878 2018-11-18 stsp if (*s == '\0') { /* final path element */
2037 07862c20 2018-09-15 stsp *changed = 1;
2038 07862c20 2018-09-15 stsp goto done;
2039 07862c20 2018-09-15 stsp }
2040 07862c20 2018-09-15 stsp
2041 07862c20 2018-09-15 stsp seg = s + 1;
2042 07862c20 2018-09-15 stsp s++;
2043 65a9bbe9 2018-09-15 stsp seglen = 0;
2044 07862c20 2018-09-15 stsp if (*s) {
2045 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree1, repo,
2046 56e0773d 2019-11-28 stsp &te1->id);
2047 07862c20 2018-09-15 stsp te1 = NULL;
2048 07862c20 2018-09-15 stsp if (err)
2049 07862c20 2018-09-15 stsp goto done;
2050 a31cea73 2018-09-15 stsp if (tree1 != tree01)
2051 a31cea73 2018-09-15 stsp got_object_tree_close(tree1);
2052 07862c20 2018-09-15 stsp tree1 = next_tree1;
2053 07862c20 2018-09-15 stsp
2054 e8bfb8f3 2020-12-18 stsp if (te2) {
2055 e8bfb8f3 2020-12-18 stsp err = got_object_open_as_tree(&next_tree2, repo,
2056 e8bfb8f3 2020-12-18 stsp &te2->id);
2057 e8bfb8f3 2020-12-18 stsp te2 = NULL;
2058 e8bfb8f3 2020-12-18 stsp if (err)
2059 e8bfb8f3 2020-12-18 stsp goto done;
2060 e8bfb8f3 2020-12-18 stsp if (tree2 != tree02)
2061 e8bfb8f3 2020-12-18 stsp got_object_tree_close(tree2);
2062 e8bfb8f3 2020-12-18 stsp tree2 = next_tree2;
2063 e8bfb8f3 2020-12-18 stsp } else if (tree2) {
2064 e8bfb8f3 2020-12-18 stsp if (tree2 != tree02)
2065 e8bfb8f3 2020-12-18 stsp got_object_tree_close(tree2);
2066 e8bfb8f3 2020-12-18 stsp tree2 = NULL;
2067 e8bfb8f3 2020-12-18 stsp }
2068 07862c20 2018-09-15 stsp }
2069 07862c20 2018-09-15 stsp }
2070 07862c20 2018-09-15 stsp done:
2071 a31cea73 2018-09-15 stsp if (tree1 && tree1 != tree01)
2072 07862c20 2018-09-15 stsp got_object_tree_close(tree1);
2073 a31cea73 2018-09-15 stsp if (tree2 && tree2 != tree02)
2074 07862c20 2018-09-15 stsp got_object_tree_close(tree2);
2075 77880158 2018-11-04 stsp return err;
2076 77880158 2018-11-04 stsp }
2077 ed175427 2019-05-09 stsp
2078 ed175427 2019-05-09 stsp const struct got_error *
2079 ed175427 2019-05-09 stsp got_object_tree_entry_dup(struct got_tree_entry **new_te,
2080 ed175427 2019-05-09 stsp struct got_tree_entry *te)
2081 ed175427 2019-05-09 stsp {
2082 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
2083 ed175427 2019-05-09 stsp
2084 ed175427 2019-05-09 stsp *new_te = calloc(1, sizeof(**new_te));
2085 ed175427 2019-05-09 stsp if (*new_te == NULL)
2086 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
2087 ed175427 2019-05-09 stsp
2088 ed175427 2019-05-09 stsp (*new_te)->mode = te->mode;
2089 56e0773d 2019-11-28 stsp memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2090 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2091 8c4eabf2 2019-05-10 stsp return err;
2092 63c5ca5d 2019-08-24 stsp }
2093 63c5ca5d 2019-08-24 stsp
2094 63c5ca5d 2019-08-24 stsp int
2095 56e0773d 2019-11-28 stsp got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2096 63c5ca5d 2019-08-24 stsp {
2097 63c5ca5d 2019-08-24 stsp return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2098 e40622f4 2020-07-23 stsp }
2099 e40622f4 2020-07-23 stsp
2100 e40622f4 2020-07-23 stsp int
2101 e40622f4 2020-07-23 stsp got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2102 e40622f4 2020-07-23 stsp {
2103 e40622f4 2020-07-23 stsp /* S_IFDIR check avoids confusing symlinks with submodules. */
2104 e40622f4 2020-07-23 stsp return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2105 e40622f4 2020-07-23 stsp }
2106 e40622f4 2020-07-23 stsp
2107 e40622f4 2020-07-23 stsp static const struct got_error *
2108 e40622f4 2020-07-23 stsp resolve_symlink(char **link_target, const char *path,
2109 945f9229 2022-04-16 thomas struct got_commit_object *commit, struct got_repository *repo)
2110 e40622f4 2020-07-23 stsp {
2111 e40622f4 2020-07-23 stsp const struct got_error *err = NULL;
2112 dbdd6209 2020-10-19 stsp char buf[PATH_MAX];
2113 e40622f4 2020-07-23 stsp char *name, *parent_path = NULL;
2114 e40622f4 2020-07-23 stsp struct got_object_id *tree_obj_id = NULL;
2115 e40622f4 2020-07-23 stsp struct got_tree_object *tree = NULL;
2116 e40622f4 2020-07-23 stsp struct got_tree_entry *te = NULL;
2117 e40622f4 2020-07-23 stsp
2118 e40622f4 2020-07-23 stsp *link_target = NULL;
2119 559d127c 2020-07-23 stsp
2120 dbdd6209 2020-10-19 stsp if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2121 dbdd6209 2020-10-19 stsp return got_error(GOT_ERR_NO_SPACE);
2122 dbdd6209 2020-10-19 stsp
2123 dbdd6209 2020-10-19 stsp name = basename(buf);
2124 e40622f4 2020-07-23 stsp if (name == NULL)
2125 e40622f4 2020-07-23 stsp return got_error_from_errno2("basename", path);
2126 e40622f4 2020-07-23 stsp
2127 e40622f4 2020-07-23 stsp err = got_path_dirname(&parent_path, path);
2128 e40622f4 2020-07-23 stsp if (err)
2129 e40622f4 2020-07-23 stsp return err;
2130 e40622f4 2020-07-23 stsp
2131 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_obj_id, repo, commit,
2132 e40622f4 2020-07-23 stsp parent_path);
2133 e40622f4 2020-07-23 stsp if (err) {
2134 e40622f4 2020-07-23 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2135 e40622f4 2020-07-23 stsp /* Display the complete path in error message. */
2136 e40622f4 2020-07-23 stsp err = got_error_path(path, err->code);
2137 e40622f4 2020-07-23 stsp }
2138 e40622f4 2020-07-23 stsp goto done;
2139 e40622f4 2020-07-23 stsp }
2140 e40622f4 2020-07-23 stsp
2141 e40622f4 2020-07-23 stsp err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2142 e40622f4 2020-07-23 stsp if (err)
2143 e40622f4 2020-07-23 stsp goto done;
2144 e40622f4 2020-07-23 stsp
2145 e40622f4 2020-07-23 stsp te = got_object_tree_find_entry(tree, name);
2146 e40622f4 2020-07-23 stsp if (te == NULL) {
2147 e40622f4 2020-07-23 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2148 e40622f4 2020-07-23 stsp goto done;
2149 e40622f4 2020-07-23 stsp }
2150 e40622f4 2020-07-23 stsp
2151 e40622f4 2020-07-23 stsp if (got_object_tree_entry_is_symlink(te)) {
2152 e40622f4 2020-07-23 stsp err = got_tree_entry_get_symlink_target(link_target, te, repo);
2153 e40622f4 2020-07-23 stsp if (err)
2154 e40622f4 2020-07-23 stsp goto done;
2155 e40622f4 2020-07-23 stsp if (!got_path_is_absolute(*link_target)) {
2156 e40622f4 2020-07-23 stsp char *abspath;
2157 e40622f4 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", parent_path,
2158 e40622f4 2020-07-23 stsp *link_target) == -1) {
2159 e40622f4 2020-07-23 stsp err = got_error_from_errno("asprintf");
2160 e40622f4 2020-07-23 stsp goto done;
2161 e40622f4 2020-07-23 stsp }
2162 e40622f4 2020-07-23 stsp free(*link_target);
2163 e40622f4 2020-07-23 stsp *link_target = malloc(PATH_MAX);
2164 e40622f4 2020-07-23 stsp if (*link_target == NULL) {
2165 e40622f4 2020-07-23 stsp err = got_error_from_errno("malloc");
2166 e40622f4 2020-07-23 stsp goto done;
2167 e40622f4 2020-07-23 stsp }
2168 e40622f4 2020-07-23 stsp err = got_canonpath(abspath, *link_target, PATH_MAX);
2169 e40622f4 2020-07-23 stsp free(abspath);
2170 e40622f4 2020-07-23 stsp if (err)
2171 e40622f4 2020-07-23 stsp goto done;
2172 e40622f4 2020-07-23 stsp }
2173 e40622f4 2020-07-23 stsp }
2174 e40622f4 2020-07-23 stsp done:
2175 e40622f4 2020-07-23 stsp free(tree_obj_id);
2176 e40622f4 2020-07-23 stsp if (tree)
2177 e40622f4 2020-07-23 stsp got_object_tree_close(tree);
2178 e40622f4 2020-07-23 stsp if (err) {
2179 e40622f4 2020-07-23 stsp free(*link_target);
2180 e40622f4 2020-07-23 stsp *link_target = NULL;
2181 e40622f4 2020-07-23 stsp }
2182 e40622f4 2020-07-23 stsp return err;
2183 ca6e02ac 2020-01-07 stsp }
2184 ca6e02ac 2020-01-07 stsp
2185 ca6e02ac 2020-01-07 stsp const struct got_error *
2186 e40622f4 2020-07-23 stsp got_object_resolve_symlinks(char **link_target, const char *path,
2187 945f9229 2022-04-16 thomas struct got_commit_object *commit, struct got_repository *repo)
2188 e40622f4 2020-07-23 stsp {
2189 e40622f4 2020-07-23 stsp const struct got_error *err = NULL;
2190 e40622f4 2020-07-23 stsp char *next_target = NULL;
2191 e40622f4 2020-07-23 stsp int max_recursion = 40; /* matches Git */
2192 e40622f4 2020-07-23 stsp
2193 e40622f4 2020-07-23 stsp *link_target = NULL;
2194 e40622f4 2020-07-23 stsp
2195 e40622f4 2020-07-23 stsp do {
2196 e40622f4 2020-07-23 stsp err = resolve_symlink(&next_target,
2197 945f9229 2022-04-16 thomas *link_target ? *link_target : path, commit, repo);
2198 e40622f4 2020-07-23 stsp if (err)
2199 e40622f4 2020-07-23 stsp break;
2200 e40622f4 2020-07-23 stsp if (next_target) {
2201 e40622f4 2020-07-23 stsp free(*link_target);
2202 e40622f4 2020-07-23 stsp if (--max_recursion == 0) {
2203 e40622f4 2020-07-23 stsp err = got_error_path(path, GOT_ERR_RECURSION);
2204 e40622f4 2020-07-23 stsp *link_target = NULL;
2205 e40622f4 2020-07-23 stsp break;
2206 e40622f4 2020-07-23 stsp }
2207 e40622f4 2020-07-23 stsp *link_target = next_target;
2208 e40622f4 2020-07-23 stsp }
2209 e40622f4 2020-07-23 stsp } while (next_target);
2210 e40622f4 2020-07-23 stsp
2211 e40622f4 2020-07-23 stsp return err;
2212 e40622f4 2020-07-23 stsp }
2213 e40622f4 2020-07-23 stsp
2214 e40622f4 2020-07-23 stsp const struct got_error *
2215 ca6e02ac 2020-01-07 stsp got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2216 ca6e02ac 2020-01-07 stsp struct got_object_id *commit_id, const char *path,
2217 ca6e02ac 2020-01-07 stsp struct got_repository *repo)
2218 ca6e02ac 2020-01-07 stsp {
2219 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
2220 ca6e02ac 2020-01-07 stsp struct got_pack *pack = NULL;
2221 ca6e02ac 2020-01-07 stsp struct got_packidx *packidx = NULL;
2222 ca6e02ac 2020-01-07 stsp char *path_packfile = NULL;
2223 ca6e02ac 2020-01-07 stsp struct got_commit_object *changed_commit = NULL;
2224 ca6e02ac 2020-01-07 stsp struct got_object_id *changed_commit_id = NULL;
2225 ca6e02ac 2020-01-07 stsp int idx;
2226 ca6e02ac 2020-01-07 stsp
2227 ca6e02ac 2020-01-07 stsp err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2228 ca6e02ac 2020-01-07 stsp if (err) {
2229 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
2230 ca6e02ac 2020-01-07 stsp return err;
2231 ca6e02ac 2020-01-07 stsp return NULL;
2232 ca6e02ac 2020-01-07 stsp }
2233 ca6e02ac 2020-01-07 stsp
2234 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
2235 aea75d87 2021-07-06 stsp packidx->path_packidx);
2236 ca6e02ac 2020-01-07 stsp if (err)
2237 ca6e02ac 2020-01-07 stsp return err;
2238 ca6e02ac 2020-01-07 stsp
2239 ca6e02ac 2020-01-07 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
2240 ca6e02ac 2020-01-07 stsp if (pack == NULL) {
2241 ca6e02ac 2020-01-07 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2242 ca6e02ac 2020-01-07 stsp if (err)
2243 ca6e02ac 2020-01-07 stsp goto done;
2244 ca6e02ac 2020-01-07 stsp }
2245 ca6e02ac 2020-01-07 stsp
2246 ca6e02ac 2020-01-07 stsp if (pack->privsep_child == NULL) {
2247 68036464 2022-06-26 thomas err = got_pack_start_privsep_child(pack, packidx);
2248 ca6e02ac 2020-01-07 stsp if (err)
2249 ca6e02ac 2020-01-07 stsp goto done;
2250 ca6e02ac 2020-01-07 stsp }
2251 ca6e02ac 2020-01-07 stsp
2252 ca6e02ac 2020-01-07 stsp err = got_privsep_send_commit_traversal_request(
2253 ca6e02ac 2020-01-07 stsp pack->privsep_child->ibuf, commit_id, idx, path);
2254 ca6e02ac 2020-01-07 stsp if (err)
2255 ca6e02ac 2020-01-07 stsp goto done;
2256 ca6e02ac 2020-01-07 stsp
2257 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_traversed_commits(&changed_commit,
2258 ca6e02ac 2020-01-07 stsp &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2259 ca6e02ac 2020-01-07 stsp if (err)
2260 ca6e02ac 2020-01-07 stsp goto done;
2261 ca6e02ac 2020-01-07 stsp
2262 ca6e02ac 2020-01-07 stsp if (changed_commit) {
2263 ca6e02ac 2020-01-07 stsp /*
2264 ca6e02ac 2020-01-07 stsp * Cache the commit in which the path was changed.
2265 ca6e02ac 2020-01-07 stsp * This commit might be opened again soon.
2266 ca6e02ac 2020-01-07 stsp */
2267 ca6e02ac 2020-01-07 stsp changed_commit->refcnt++;
2268 ca6e02ac 2020-01-07 stsp err = got_repo_cache_commit(repo, changed_commit_id,
2269 ca6e02ac 2020-01-07 stsp changed_commit);
2270 ca6e02ac 2020-01-07 stsp got_object_commit_close(changed_commit);
2271 ca6e02ac 2020-01-07 stsp }
2272 ca6e02ac 2020-01-07 stsp done:
2273 ca6e02ac 2020-01-07 stsp free(path_packfile);
2274 ca6e02ac 2020-01-07 stsp free(changed_commit_id);
2275 ca6e02ac 2020-01-07 stsp return err;
2276 ed175427 2019-05-09 stsp }
2277 63915ee5 2022-06-23 thomas
2278 63915ee5 2022-06-23 thomas const struct got_error *
2279 e71f1e62 2022-06-23 thomas got_object_enumerate(int *found_all_objects,
2280 e71f1e62 2022-06-23 thomas got_object_enumerate_commit_cb cb_commit,
2281 63915ee5 2022-06-23 thomas got_object_enumerate_tree_cb cb_tree, void *cb_arg,
2282 63915ee5 2022-06-23 thomas struct got_object_id **ours, int nours,
2283 63915ee5 2022-06-23 thomas struct got_object_id **theirs, int ntheirs,
2284 63915ee5 2022-06-23 thomas struct got_packidx *packidx, struct got_repository *repo)
2285 63915ee5 2022-06-23 thomas {
2286 63915ee5 2022-06-23 thomas const struct got_error *err = NULL;
2287 63915ee5 2022-06-23 thomas struct got_pack *pack;
2288 63915ee5 2022-06-23 thomas char *path_packfile = NULL;
2289 63915ee5 2022-06-23 thomas
2290 63915ee5 2022-06-23 thomas err = got_packidx_get_packfile_path(&path_packfile,
2291 63915ee5 2022-06-23 thomas packidx->path_packidx);
2292 63915ee5 2022-06-23 thomas if (err)
2293 63915ee5 2022-06-23 thomas return err;
2294 63915ee5 2022-06-23 thomas
2295 63915ee5 2022-06-23 thomas pack = got_repo_get_cached_pack(repo, path_packfile);
2296 63915ee5 2022-06-23 thomas if (pack == NULL) {
2297 63915ee5 2022-06-23 thomas err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2298 63915ee5 2022-06-23 thomas if (err)
2299 63915ee5 2022-06-23 thomas goto done;
2300 63915ee5 2022-06-23 thomas }
2301 63915ee5 2022-06-23 thomas
2302 63915ee5 2022-06-23 thomas if (pack->privsep_child == NULL) {
2303 68036464 2022-06-26 thomas err = got_pack_start_privsep_child(pack, packidx);
2304 63915ee5 2022-06-23 thomas if (err)
2305 63915ee5 2022-06-23 thomas goto done;
2306 63915ee5 2022-06-23 thomas }
2307 63915ee5 2022-06-23 thomas
2308 63915ee5 2022-06-23 thomas err = got_privsep_send_object_enumeration_request(
2309 63915ee5 2022-06-23 thomas pack->privsep_child->ibuf);
2310 63915ee5 2022-06-23 thomas if (err)
2311 63915ee5 2022-06-23 thomas goto done;
2312 63915ee5 2022-06-23 thomas
2313 63915ee5 2022-06-23 thomas err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
2314 63915ee5 2022-06-23 thomas ours, nours);
2315 63915ee5 2022-06-23 thomas if (err)
2316 63915ee5 2022-06-23 thomas goto done;
2317 63915ee5 2022-06-23 thomas err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
2318 63915ee5 2022-06-23 thomas if (err)
2319 63915ee5 2022-06-23 thomas goto done;
2320 63915ee5 2022-06-23 thomas
2321 63915ee5 2022-06-23 thomas err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
2322 63915ee5 2022-06-23 thomas theirs, ntheirs);
2323 63915ee5 2022-06-23 thomas if (err)
2324 63915ee5 2022-06-23 thomas goto done;
2325 63915ee5 2022-06-23 thomas err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
2326 63915ee5 2022-06-23 thomas if (err)
2327 63915ee5 2022-06-23 thomas goto done;
2328 63915ee5 2022-06-23 thomas
2329 e71f1e62 2022-06-23 thomas err = got_privsep_recv_enumerated_objects(found_all_objects,
2330 e71f1e62 2022-06-23 thomas pack->privsep_child->ibuf, cb_commit, cb_tree, cb_arg, repo);
2331 63915ee5 2022-06-23 thomas done:
2332 63915ee5 2022-06-23 thomas free(path_packfile);
2333 63915ee5 2022-06-23 thomas return err;
2334 63915ee5 2022-06-23 thomas }