Blame


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