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