2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
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.
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.
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>
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>
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"
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"
59 ab9a70b2 2017-11-06 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 0ab4c957 2022-06-13 stsp #ifndef nitems
63 0ab4c957 2022-06-13 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
66 3235492e 2018-04-01 stsp struct got_object_id *
67 3235492e 2018-04-01 stsp got_object_get_id(struct got_object *obj)
69 6402fb3c 2018-09-15 stsp return &obj->id;
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)
75 bacc9935 2018-05-20 stsp return got_object_id_str(outbuf, &obj->id);
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)
82 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
83 15a94983 2018-12-23 stsp struct got_object *obj;
85 15a94983 2018-12-23 stsp err = got_object_open(&obj, repo, id);
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;
97 15a94983 2018-12-23 stsp err = got_error(GOT_ERR_OBJ_TYPE);
101 15a94983 2018-12-23 stsp got_object_close(obj);
102 15a94983 2018-12-23 stsp return err;
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)
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;
113 e6b1056e 2018-04-22 stsp *path = NULL;
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");
119 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
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");
129 d1cda826 2017-11-06 stsp free(path_objects);
130 d1cda826 2017-11-06 stsp return err;
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)
137 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
138 a1fd68d8 2018-01-12 stsp char *path;
140 90bdb554 2019-04-11 stsp err = got_object_get_path(&path, id, repo);
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);
149 4558fcd4 2018-01-14 stsp free(path);
150 2090a03d 2018-09-09 stsp return err;
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)
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;
160 a158c901 2018-12-23 stsp err = got_privsep_send_packed_obj_req(ibuf, idx, id);
162 a158c901 2018-12-23 stsp return err;
164 a158c901 2018-12-23 stsp err = got_privsep_recv_obj(obj, ibuf);
166 a158c901 2018-12-23 stsp return err;
168 a158c901 2018-12-23 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
170 a158c901 2018-12-23 stsp return NULL;
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)
177 db696021 2022-01-04 stsp const struct got_error *err;
178 57160834 2022-05-31 stsp int basefd = -1, accumfd = -1;
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'.
186 db696021 2022-01-04 stsp if (pack->child_has_tempfiles)
187 db696021 2022-01-04 stsp return NULL;
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");
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");
199 db696021 2022-01-04 stsp err = got_privsep_send_tmpfd(ibuf, basefd);
203 db696021 2022-01-04 stsp err = got_privsep_send_tmpfd(ibuf, accumfd);
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);
211 57160834 2022-05-31 stsp pack->child_has_tempfiles = 1;
212 db696021 2022-01-04 stsp return NULL;
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)
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;
223 db696021 2022-01-04 stsp err = pack_child_send_tempfiles(ibuf, pack);
225 db696021 2022-01-04 stsp return err;
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");
231 59d1e4a0 2021-03-10 stsp err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
233 59d1e4a0 2021-03-10 stsp close(outfd_child);
234 59d1e4a0 2021-03-10 stsp return err;
237 59d1e4a0 2021-03-10 stsp err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
239 59d1e4a0 2021-03-10 stsp return err;
241 59d1e4a0 2021-03-10 stsp err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
243 59d1e4a0 2021-03-10 stsp return err;
245 59d1e4a0 2021-03-10 stsp return NULL;
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)
253 711fb6e8 2018-12-23 stsp const struct got_error *err = NULL;
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);
258 59d1e4a0 2021-03-10 stsp return err;
261 711fb6e8 2018-12-23 stsp return request_packed_object(obj, pack, idx, id);
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)
269 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
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);
274 59d1e4a0 2021-03-10 stsp return err;
277 59d1e4a0 2021-03-10 stsp return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
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)
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;
289 2090a03d 2018-09-09 stsp char *path_packfile;
291 2090a03d 2018-09-09 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
293 2090a03d 2018-09-09 stsp return err;
295 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
296 aea75d87 2021-07-06 stsp packidx->path_packidx);
298 2090a03d 2018-09-09 stsp return err;
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);
307 a158c901 2018-12-23 stsp err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
311 2090a03d 2018-09-09 stsp free(path_packfile);
312 4558fcd4 2018-01-14 stsp return err;
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)
320 67fd6849 2022-02-13 stsp return read_packed_object_privsep(obj, repo, pack, packidx,
321 67fd6849 2022-02-13 stsp obj_idx, id);
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)
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;
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;
342 67fd6849 2022-02-13 stsp err = got_packidx_get_packfile_path(&path_packfile,
343 67fd6849 2022-02-13 stsp packidx->path_packidx);
345 67fd6849 2022-02-13 stsp return err;
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);
351 67fd6849 2022-02-13 stsp return err;
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);
357 67fd6849 2022-02-13 stsp return err;
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);
368 67fd6849 2022-02-13 stsp return err;
369 67fd6849 2022-02-13 stsp pack->child_has_delta_outfd = 1;
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);
375 67fd6849 2022-02-13 stsp return err;
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);
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)
386 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
387 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
389 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
391 d5c81d44 2021-07-08 stsp err = got_privsep_send_obj_req(ibuf, fd, id);
393 9f2369b0 2018-12-24 stsp return err;
395 9f2369b0 2018-12-24 stsp return got_privsep_recv_obj(obj, ibuf);
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)
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;
406 59d1e4a0 2021-03-10 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
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");
412 d5c81d44 2021-07-08 stsp err = got_privsep_send_raw_obj_req(ibuf, infd, id);
414 59d1e4a0 2021-03-10 stsp return err;
416 59d1e4a0 2021-03-10 stsp err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
418 59d1e4a0 2021-03-10 stsp return err;
420 59d1e4a0 2021-03-10 stsp return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
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)
426 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
427 9f2369b0 2018-12-24 stsp int imsg_fds[2];
429 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
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");
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;
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;
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 */
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;
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;
465 59d1e4a0 2021-03-10 stsp return NULL;
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)
472 59d1e4a0 2021-03-10 stsp const struct got_error *err;
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);
477 59d1e4a0 2021-03-10 stsp err = start_read_object_child(repo);
479 7495ec13 2021-04-04 stsp close(obj_fd);
480 59d1e4a0 2021-03-10 stsp return err;
483 d5c81d44 2021-07-08 stsp return request_object(obj, id, repo, obj_fd);
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)
491 59d1e4a0 2021-03-10 stsp const struct got_error *err;
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);
497 59d1e4a0 2021-03-10 stsp err = start_read_object_child(repo);
499 59d1e4a0 2021-03-10 stsp return err;
501 d5c81d44 2021-07-08 stsp return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
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)
509 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
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;
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);
526 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
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;
533 d5c81d44 2021-07-08 stsp err = got_object_read_header_privsep(obj, id, repo, fd);
535 4558fcd4 2018-01-14 stsp return err;
537 762d73f4 2021-04-10 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
539 59790a32 2018-09-15 stsp (*obj)->refcnt++;
540 762d73f4 2021-04-10 stsp return got_repo_cache_object(repo, id, *obj);
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)
548 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
549 59d1e4a0 2021-03-10 stsp struct got_packidx *packidx = NULL;
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;
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;
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");
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;
572 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
573 aea75d87 2021-07-06 stsp packidx->path_packidx);
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,
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);
588 59d1e4a0 2021-03-10 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
591 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
594 d3c116bf 2021-10-15 stsp err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
595 d5c81d44 2021-07-08 stsp id, repo, fd);
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");
605 64a8571e 2022-01-07 stsp (*obj)->fd = -1;
607 59d1e4a0 2021-03-10 stsp if (outbuf) {
608 59d1e4a0 2021-03-10 stsp (*obj)->data = outbuf;
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");
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);
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");
629 64a8571e 2022-01-07 stsp (*obj)->data = NULL;
631 64a8571e 2022-01-07 stsp (*obj)->fd = *outfd;
632 64a8571e 2022-01-07 stsp *outfd = -1;
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");
642 64a8571e 2022-01-07 stsp *outfd = -1;
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);
649 59d1e4a0 2021-03-10 stsp free(path_packfile);
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;
655 59d1e4a0 2021-03-10 stsp free(outbuf);
657 d3c116bf 2021-10-15 stsp (*obj)->refcnt++;
658 59d1e4a0 2021-03-10 stsp return err;
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)
665 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
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);
670 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
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)
677 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
678 15a94983 2018-12-23 stsp struct got_object *obj;
680 15a94983 2018-12-23 stsp err = got_object_open_by_id_str(&obj, repo, id_str);
682 15a94983 2018-12-23 stsp return err;
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");
689 15a94983 2018-12-23 stsp return NULL;
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)
696 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
698 a158c901 2018-12-23 stsp err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
701 a158c901 2018-12-23 stsp return err;
703 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
705 ca6e02ac 2020-01-07 stsp return err;
707 ca6e02ac 2020-01-07 stsp (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
708 ca6e02ac 2020-01-07 stsp return NULL;
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)
716 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
718 a158c901 2018-12-23 stsp if (pack->privsep_child)
719 a158c901 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
721 3d589bee 2022-06-25 stsp err = got_pack_start_privsep_child(pack, packidx);
723 a158c901 2018-12-23 stsp return err;
725 711fb6e8 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
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)
732 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
733 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
735 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
737 d5c81d44 2021-07-08 stsp err = got_privsep_send_commit_req(ibuf, fd, id, -1);
739 9f2369b0 2018-12-24 stsp return err;
741 9f2369b0 2018-12-24 stsp return got_privsep_recv_commit(commit, ibuf);
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)
748 ddc7b220 2019-09-08 stsp const struct got_error *err;
749 9f2369b0 2018-12-24 stsp int imsg_fds[2];
751 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
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);
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");
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;
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;
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 */
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;
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;
789 d5c81d44 2021-07-08 stsp return request_commit(commit, repo, obj_fd, id);
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)
797 434025f3 2018-09-16 stsp const struct got_error *err = NULL;
798 1785f84a 2018-12-23 stsp struct got_packidx *packidx = NULL;
800 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
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;
809 434025f3 2018-09-16 stsp *commit = NULL;
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;
815 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
816 aea75d87 2021-07-06 stsp packidx->path_packidx);
818 1785f84a 2018-12-23 stsp return err;
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,
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) {
832 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
834 e82b1d81 2019-07-27 stsp return err;
835 d5c81d44 2021-07-08 stsp err = read_commit_privsep(commit, fd, id, repo);
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);
843 8d2c5ea3 2019-08-13 stsp free(path_packfile);
844 e32baab7 2018-11-05 stsp return err;
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)
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;
857 1785f84a 2018-12-23 stsp return open_commit(commit, repo, id, 0);
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)
864 1785f84a 2018-12-23 stsp return open_commit(commit, repo, got_object_get_id(obj), 1);
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)
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");
874 d7b5a0e8 2022-04-20 stsp memcpy(&(*qid)->id, id, sizeof((*qid)->id));
875 9ca9aafb 2021-06-18 stsp return NULL;
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)
882 9ca9aafb 2021-06-18 stsp const struct got_error *err;
883 9ca9aafb 2021-06-18 stsp struct got_object_qid *qid;
885 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, src, entry) {
886 9ca9aafb 2021-06-18 stsp struct got_object_qid *new;
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.
891 d7b5a0e8 2022-04-20 stsp err = got_object_qid_alloc(&new, &qid->id);
893 9ca9aafb 2021-06-18 stsp got_object_id_queue_free(dest);
894 9ca9aafb 2021-06-18 stsp return err;
896 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(dest, new, entry);
899 dbc6a6b6 2018-07-12 stsp return NULL;
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)
906 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
908 13c729f7 2018-12-24 stsp err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
911 a158c901 2018-12-23 stsp return err;
913 a158c901 2018-12-23 stsp return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
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)
921 13c729f7 2018-12-24 stsp const struct got_error *err = NULL;
923 13c729f7 2018-12-24 stsp if (pack->privsep_child)
924 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
926 3d589bee 2022-06-25 stsp err = got_pack_start_privsep_child(pack, packidx);
928 13c729f7 2018-12-24 stsp return err;
930 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
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)
937 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
938 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
940 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
942 d5c81d44 2021-07-08 stsp err = got_privsep_send_tree_req(ibuf, fd, id, -1);
944 9f2369b0 2018-12-24 stsp return err;
946 9f2369b0 2018-12-24 stsp return got_privsep_recv_tree(tree, ibuf);
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)
953 ddc7b220 2019-09-08 stsp const struct got_error *err;
954 9f2369b0 2018-12-24 stsp int imsg_fds[2];
956 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
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);
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");
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;
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;
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 */
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;
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;
995 d5c81d44 2021-07-08 stsp return request_tree(tree, repo, obj_fd, id);
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)
1002 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
1003 13c729f7 2018-12-24 stsp struct got_packidx *packidx = NULL;
1005 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
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;
1014 71eb0e7f 2018-09-16 stsp *tree = NULL;
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;
1020 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
1021 aea75d87 2021-07-06 stsp packidx->path_packidx);
1023 13c729f7 2018-12-24 stsp return err;
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,
1030 8d2c5ea3 2019-08-13 stsp goto done;
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) {
1037 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1039 e82b1d81 2019-07-27 stsp return err;
1040 d5c81d44 2021-07-08 stsp err = read_tree_privsep(tree, fd, id, repo);
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);
1048 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1049 776d4d29 2018-06-17 stsp return err;
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)
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;
1062 13c729f7 2018-12-24 stsp return open_tree(tree, repo, id, 0);
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)
1069 13c729f7 2018-12-24 stsp return open_tree(tree, repo, got_object_get_id(obj), 1);
1073 56e0773d 2019-11-28 stsp got_object_tree_get_nentries(struct got_tree_object *tree)
1075 56e0773d 2019-11-28 stsp return tree->nentries;
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)
1081 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, 0);
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)
1087 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, tree->nentries - 1);
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)
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];
1099 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(struct got_tree_entry *te)
1101 56e0773d 2019-11-28 stsp return te->mode;
1104 56e0773d 2019-11-28 stsp const char *
1105 56e0773d 2019-11-28 stsp got_tree_entry_get_name(struct got_tree_entry *te)
1107 56e0773d 2019-11-28 stsp return &te->name[0];
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)
1113 56e0773d 2019-11-28 stsp return &te->id;
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)
1119 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
1120 32596e16 2020-07-23 stsp size_t len, totlen, hdrlen, offset;
1122 aa092692 2020-07-23 stsp *s = NULL;
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;
1130 659dc16e 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1132 af57b12a 2020-07-23 stsp return err;
1134 659dc16e 2020-07-23 stsp if (len == 0)
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");
1142 af57b12a 2020-07-23 stsp *s = NULL;
1143 af57b12a 2020-07-23 stsp return err;
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);
1153 af57b12a 2020-07-23 stsp (*s)[totlen] = '\0';
1154 af57b12a 2020-07-23 stsp return NULL;
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)
1161 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
1162 af57b12a 2020-07-23 stsp struct got_blob_object *blob = NULL;
1164 af57b12a 2020-07-23 stsp *link_target = NULL;
1166 af57b12a 2020-07-23 stsp if (!got_object_tree_entry_is_symlink(te))
1167 af57b12a 2020-07-23 stsp return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1169 af57b12a 2020-07-23 stsp err = got_object_open_as_blob(&blob, repo,
1170 af57b12a 2020-07-23 stsp got_tree_entry_get_id(te), PATH_MAX);
1172 af57b12a 2020-07-23 stsp return err;
1174 af57b12a 2020-07-23 stsp err = got_object_blob_read_to_str(link_target, blob);
1175 af57b12a 2020-07-23 stsp got_object_blob_close(blob);
1176 659dc16e 2020-07-23 stsp if (err) {
1177 659dc16e 2020-07-23 stsp free(*link_target);
1178 659dc16e 2020-07-23 stsp *link_target = NULL;
1180 0d6c6ee3 2020-05-20 stsp return err;
1184 56e0773d 2019-11-28 stsp got_tree_entry_get_index(struct got_tree_entry *te)
1186 56e0773d 2019-11-28 stsp return te->idx;
1189 56e0773d 2019-11-28 stsp struct got_tree_entry *
1190 56e0773d 2019-11-28 stsp got_tree_entry_get_next(struct got_tree_object *tree,
1191 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
1193 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx + 1);
1196 56e0773d 2019-11-28 stsp struct got_tree_entry *
1197 56e0773d 2019-11-28 stsp got_tree_entry_get_prev(struct got_tree_object *tree,
1198 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
1200 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx - 1);
1203 3840f4c9 2018-09-12 stsp static const struct got_error *
1204 ac544f8c 2019-01-13 stsp request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1205 ebc55e2d 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1206 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
1208 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
1209 db696021 2022-01-04 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1210 db696021 2022-01-04 stsp int outfd_child;
1212 db696021 2022-01-04 stsp err = pack_child_send_tempfiles(ibuf, pack);
1214 db696021 2022-01-04 stsp return err;
1216 3840f4c9 2018-09-12 stsp outfd_child = dup(outfd);
1217 3840f4c9 2018-09-12 stsp if (outfd_child == -1)
1218 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1220 ebc55e2d 2018-12-24 stsp err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1222 3840f4c9 2018-09-12 stsp return err;
1224 3840f4c9 2018-09-12 stsp err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1225 3840f4c9 2018-09-12 stsp outfd_child);
1226 3840f4c9 2018-09-12 stsp if (err) {
1227 3840f4c9 2018-09-12 stsp return err;
1230 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen,
1231 ebc55e2d 2018-12-24 stsp pack->privsep_child->ibuf);
1233 3840f4c9 2018-09-12 stsp return err;
1235 3840f4c9 2018-09-12 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1236 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1238 3840f4c9 2018-09-12 stsp return err;
1241 ebc55e2d 2018-12-24 stsp static const struct got_error *
1242 ac544f8c 2019-01-13 stsp read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1243 ac544f8c 2019-01-13 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1244 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
1246 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
1248 ebc55e2d 2018-12-24 stsp if (pack->privsep_child == NULL) {
1249 3d589bee 2022-06-25 stsp err = got_pack_start_privsep_child(pack, packidx);
1251 ebc55e2d 2018-12-24 stsp return err;
1254 ac544f8c 2019-01-13 stsp return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1258 9f2369b0 2018-12-24 stsp static const struct got_error *
1259 ac544f8c 2019-01-13 stsp request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1260 d5c81d44 2021-07-08 stsp int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1262 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1263 9f2369b0 2018-12-24 stsp int outfd_child;
1265 9f2369b0 2018-12-24 stsp outfd_child = dup(outfd);
1266 9f2369b0 2018-12-24 stsp if (outfd_child == -1)
1267 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1269 d5c81d44 2021-07-08 stsp err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1271 9f2369b0 2018-12-24 stsp return err;
1273 9f2369b0 2018-12-24 stsp err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1275 9f2369b0 2018-12-24 stsp return err;
1277 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1279 9f2369b0 2018-12-24 stsp return err;
1281 9f2369b0 2018-12-24 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1282 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
1284 9f2369b0 2018-12-24 stsp return err;
1287 9f2369b0 2018-12-24 stsp static const struct got_error *
1288 ac544f8c 2019-01-13 stsp read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1289 d5c81d44 2021-07-08 stsp int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1291 ddc7b220 2019-09-08 stsp const struct got_error *err;
1292 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1293 9f2369b0 2018-12-24 stsp pid_t pid;
1294 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1296 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1297 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1298 d5c81d44 2021-07-08 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1302 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1303 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1304 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1306 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1307 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1308 ddc7b220 2019-09-08 stsp free(ibuf);
1309 ddc7b220 2019-09-08 stsp return err;
1312 9f2369b0 2018-12-24 stsp pid = fork();
1313 ddc7b220 2019-09-08 stsp if (pid == -1) {
1314 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1315 ddc7b220 2019-09-08 stsp free(ibuf);
1316 ddc7b220 2019-09-08 stsp return err;
1318 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1319 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1320 9f2369b0 2018-12-24 stsp repo->path);
1321 9f2369b0 2018-12-24 stsp /* not reached */
1324 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
1325 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1326 ddc7b220 2019-09-08 stsp free(ibuf);
1327 ddc7b220 2019-09-08 stsp return err;
1329 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1330 9f2369b0 2018-12-24 stsp imsg_fds[0];
1331 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1332 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1333 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1335 d5c81d44 2021-07-08 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1338 ebc55e2d 2018-12-24 stsp static const struct got_error *
1339 ebc55e2d 2018-12-24 stsp open_blob(struct got_blob_object **blob, struct got_repository *repo,
1340 ebc55e2d 2018-12-24 stsp struct got_object_id *id, size_t blocksize)
1342 ebc55e2d 2018-12-24 stsp const struct got_error *err = NULL;
1343 ebc55e2d 2018-12-24 stsp struct got_packidx *packidx = NULL;
1345 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1346 ac544f8c 2019-01-13 stsp uint8_t *outbuf;
1347 e82b1d81 2019-07-27 stsp int outfd;
1348 ebc55e2d 2018-12-24 stsp size_t size, hdrlen;
1349 ebc55e2d 2018-12-24 stsp struct stat sb;
1351 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
1352 4558fcd4 2018-01-14 stsp if (*blob == NULL)
1353 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1355 55da3778 2018-09-10 stsp outfd = got_opentempfd();
1356 55da3778 2018-09-10 stsp if (outfd == -1)
1357 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
1359 062ebb78 2018-07-12 stsp (*blob)->read_buf = malloc(blocksize);
1360 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
1361 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1362 c7254d79 2018-04-24 stsp goto done;
1365 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1366 e82b1d81 2019-07-27 stsp if (err == NULL) {
1367 ebc55e2d 2018-12-24 stsp struct got_pack *pack = NULL;
1369 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
1370 aea75d87 2021-07-06 stsp packidx->path_packidx);
1372 ebc55e2d 2018-12-24 stsp goto done;
1374 ebc55e2d 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1375 55da3778 2018-09-10 stsp if (pack == NULL) {
1376 ebc55e2d 2018-12-24 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1379 55da3778 2018-09-10 stsp goto done;
1381 ac544f8c 2019-01-13 stsp err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1382 ac544f8c 2019-01-13 stsp pack, packidx, idx, id);
1383 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1386 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&infd, id, repo);
1388 e82b1d81 2019-07-27 stsp goto done;
1389 ac544f8c 2019-01-13 stsp err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1390 d5c81d44 2021-07-08 stsp id, repo);
1393 55da3778 2018-09-10 stsp goto done;
1395 de060dff 2018-12-24 stsp if (hdrlen > size) {
1396 ebc55e2d 2018-12-24 stsp err = got_error(GOT_ERR_BAD_OBJ_HDR);
1397 ebc55e2d 2018-12-24 stsp goto done;
1400 ac544f8c 2019-01-13 stsp if (outbuf) {
1401 08578a35 2021-01-22 stsp if (close(outfd) == -1 && err == NULL)
1402 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1403 ac544f8c 2019-01-13 stsp outfd = -1;
1404 ac544f8c 2019-01-13 stsp (*blob)->f = fmemopen(outbuf, size, "rb");
1405 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1406 638f9024 2019-05-13 stsp err = got_error_from_errno("fmemopen");
1407 ac544f8c 2019-01-13 stsp free(outbuf);
1408 ac544f8c 2019-01-13 stsp goto done;
1410 ac544f8c 2019-01-13 stsp (*blob)->data = outbuf;
1412 ac544f8c 2019-01-13 stsp if (fstat(outfd, &sb) == -1) {
1413 638f9024 2019-05-13 stsp err = got_error_from_errno("fstat");
1414 ac544f8c 2019-01-13 stsp goto done;
1417 ac544f8c 2019-01-13 stsp if (sb.st_size != size) {
1418 ac544f8c 2019-01-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1419 ac544f8c 2019-01-13 stsp goto done;
1422 ac544f8c 2019-01-13 stsp (*blob)->f = fdopen(outfd, "rb");
1423 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1424 638f9024 2019-05-13 stsp err = got_error_from_errno("fdopen");
1425 ac544f8c 2019-01-13 stsp close(outfd);
1426 ac544f8c 2019-01-13 stsp outfd = -1;
1427 ac544f8c 2019-01-13 stsp goto done;
1431 ebc55e2d 2018-12-24 stsp (*blob)->hdrlen = hdrlen;
1432 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
1433 ebc55e2d 2018-12-24 stsp memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1436 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1437 55da3778 2018-09-10 stsp if (err) {
1438 55da3778 2018-09-10 stsp if (*blob) {
1439 7baf5860 2019-03-19 stsp got_object_blob_close(*blob);
1440 55da3778 2018-09-10 stsp *blob = NULL;
1441 55da3778 2018-09-10 stsp } else if (outfd != -1)
1442 55da3778 2018-09-10 stsp close(outfd);
1444 a19581a2 2018-06-21 stsp return err;
1447 a19581a2 2018-06-21 stsp const struct got_error *
1448 a19581a2 2018-06-21 stsp got_object_open_as_blob(struct got_blob_object **blob,
1449 a19581a2 2018-06-21 stsp struct got_repository *repo, struct got_object_id *id,
1450 a19581a2 2018-06-21 stsp size_t blocksize)
1452 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, id, blocksize);
1455 ebc55e2d 2018-12-24 stsp const struct got_error *
1456 ebc55e2d 2018-12-24 stsp got_object_blob_open(struct got_blob_object **blob,
1457 ebc55e2d 2018-12-24 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
1459 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1462 fb43ecf1 2019-02-11 stsp const struct got_error *
1463 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
1465 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL;
1466 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
1467 56b63ca4 2021-01-22 stsp if (blob->f && fclose(blob->f) == EOF)
1468 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1469 ac544f8c 2019-01-13 stsp free(blob->data);
1470 68482ea3 2017-11-27 stsp free(blob);
1471 fb43ecf1 2019-02-11 stsp return err;
1475 8ba819a3 2020-07-23 stsp got_object_blob_rewind(struct got_blob_object *blob)
1477 8ba819a3 2020-07-23 stsp if (blob->f)
1478 8ba819a3 2020-07-23 stsp rewind(blob->f);
1482 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1484 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1488 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
1490 f934cf2c 2018-02-12 stsp return blob->hdrlen;
1493 f934cf2c 2018-02-12 stsp const uint8_t *
1494 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
1496 f934cf2c 2018-02-12 stsp return blob->read_buf;
1499 68482ea3 2017-11-27 stsp const struct got_error *
1500 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1504 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1505 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
1506 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
1507 eb651edf 2018-02-11 stsp *outlenp = n;
1508 35e9ba5d 2018-06-21 stsp return NULL;
1511 35e9ba5d 2018-06-21 stsp const struct got_error *
1512 be659d10 2020-11-18 stsp got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1513 6c4c42e0 2019-06-24 stsp off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1515 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
1516 b6752625 2018-12-24 stsp size_t n, len, hdrlen;
1517 84451b3e 2018-07-10 stsp const uint8_t *buf;
1519 c33ebc60 2020-11-18 stsp const int alloc_chunksz = 512;
1520 c33ebc60 2020-11-18 stsp size_t nalloc = 0;
1521 f595d9bd 2019-08-14 stsp off_t off = 0, total_len = 0;
1523 6c4c42e0 2019-06-24 stsp if (line_offsets)
1524 6c4c42e0 2019-06-24 stsp *line_offsets = NULL;
1525 f595d9bd 2019-08-14 stsp if (filesize)
1526 f595d9bd 2019-08-14 stsp *filesize = 0;
1527 84451b3e 2018-07-10 stsp if (nlines)
1528 84451b3e 2018-07-10 stsp *nlines = 0;
1530 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1532 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
1534 35e9ba5d 2018-06-21 stsp return err;
1535 35e9ba5d 2018-06-21 stsp if (len == 0)
1537 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
1538 b02560ec 2019-08-19 stsp i = hdrlen;
1539 f1cbc3bc 2020-11-18 stsp if (nlines) {
1540 f1cbc3bc 2020-11-18 stsp if (line_offsets && *line_offsets == NULL) {
1541 78695fb7 2019-08-12 stsp /* Have some data but perhaps no '\n'. */
1542 78695fb7 2019-08-12 stsp *nlines = 1;
1543 c33ebc60 2020-11-18 stsp nalloc = alloc_chunksz;
1544 c33ebc60 2020-11-18 stsp *line_offsets = calloc(nalloc,
1545 c33ebc60 2020-11-18 stsp sizeof(**line_offsets));
1546 78695fb7 2019-08-12 stsp if (*line_offsets == NULL)
1547 845785d4 2020-02-02 tracey return got_error_from_errno("calloc");
1549 b02560ec 2019-08-19 stsp /* Skip forward over end of first line. */
1550 b02560ec 2019-08-19 stsp while (i < len) {
1551 b02560ec 2019-08-19 stsp if (buf[i] == '\n')
1556 b02560ec 2019-08-19 stsp /* Scan '\n' offsets in remaining chunk of data. */
1557 b02560ec 2019-08-19 stsp while (i < len) {
1558 b02560ec 2019-08-19 stsp if (buf[i] != '\n') {
1562 f595d9bd 2019-08-14 stsp (*nlines)++;
1563 c33ebc60 2020-11-18 stsp if (line_offsets && nalloc < *nlines) {
1564 c33ebc60 2020-11-18 stsp size_t n = *nlines + alloc_chunksz;
1565 78695fb7 2019-08-12 stsp off_t *o = recallocarray(*line_offsets,
1566 c33ebc60 2020-11-18 stsp nalloc, n, sizeof(**line_offsets));
1567 78695fb7 2019-08-12 stsp if (o == NULL) {
1568 78695fb7 2019-08-12 stsp free(*line_offsets);
1569 78695fb7 2019-08-12 stsp *line_offsets = NULL;
1570 78695fb7 2019-08-12 stsp return got_error_from_errno(
1571 78695fb7 2019-08-12 stsp "recallocarray");
1573 78695fb7 2019-08-12 stsp *line_offsets = o;
1574 c33ebc60 2020-11-18 stsp nalloc = n;
1576 f1cbc3bc 2020-11-18 stsp if (line_offsets) {
1577 f1cbc3bc 2020-11-18 stsp off = total_len + i - hdrlen + 1;
1578 f1cbc3bc 2020-11-18 stsp (*line_offsets)[*nlines - 1] = off;
1583 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
1584 454a6b59 2018-12-24 stsp n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1585 b6752625 2018-12-24 stsp if (n != len - hdrlen)
1586 b6752625 2018-12-24 stsp return got_ferror(outfile, GOT_ERR_IO);
1587 f595d9bd 2019-08-14 stsp total_len += len - hdrlen;
1588 35e9ba5d 2018-06-21 stsp hdrlen = 0;
1589 35e9ba5d 2018-06-21 stsp } while (len != 0);
1591 cbe7f848 2019-02-11 stsp if (fflush(outfile) != 0)
1592 638f9024 2019-05-13 stsp return got_error_from_errno("fflush");
1593 35e9ba5d 2018-06-21 stsp rewind(outfile);
1595 f595d9bd 2019-08-14 stsp if (filesize)
1596 f595d9bd 2019-08-14 stsp *filesize = total_len;
1598 776d4d29 2018-06-17 stsp return NULL;
1601 f4a881ce 2018-11-17 stsp static const struct got_error *
1602 268f7291 2018-12-24 stsp request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1603 268f7291 2018-12-24 stsp int pack_idx, struct got_object_id *id)
1605 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
1607 268f7291 2018-12-24 stsp err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1608 268f7291 2018-12-24 stsp pack_idx);
1610 a158c901 2018-12-23 stsp return err;
1612 a158c901 2018-12-23 stsp return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1615 268f7291 2018-12-24 stsp static const struct got_error *
1616 268f7291 2018-12-24 stsp read_packed_tag_privsep(struct got_tag_object **tag,
1617 268f7291 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1618 268f7291 2018-12-24 stsp struct got_object_id *id)
1620 268f7291 2018-12-24 stsp const struct got_error *err = NULL;
1622 268f7291 2018-12-24 stsp if (pack->privsep_child)
1623 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1625 3d589bee 2022-06-25 stsp err = got_pack_start_privsep_child(pack, packidx);
1627 268f7291 2018-12-24 stsp return err;
1629 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1632 9f2369b0 2018-12-24 stsp static const struct got_error *
1633 9f2369b0 2018-12-24 stsp request_tag(struct got_tag_object **tag, struct got_repository *repo,
1634 d5c81d44 2021-07-08 stsp int fd, struct got_object_id *id)
1636 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1637 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1639 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1641 d5c81d44 2021-07-08 stsp err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1643 9f2369b0 2018-12-24 stsp return err;
1645 9f2369b0 2018-12-24 stsp return got_privsep_recv_tag(tag, ibuf);
1648 9f2369b0 2018-12-24 stsp static const struct got_error *
1649 9f2369b0 2018-12-24 stsp read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1650 d5c81d44 2021-07-08 stsp struct got_object_id *id, struct got_repository *repo)
1652 ddc7b220 2019-09-08 stsp const struct got_error *err;
1653 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1654 9f2369b0 2018-12-24 stsp pid_t pid;
1655 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1657 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1658 d5c81d44 2021-07-08 stsp return request_tag(tag, repo, obj_fd, id);
1660 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1661 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1662 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1664 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1665 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1666 ddc7b220 2019-09-08 stsp free(ibuf);
1667 ddc7b220 2019-09-08 stsp return err;
1670 9f2369b0 2018-12-24 stsp pid = fork();
1671 ddc7b220 2019-09-08 stsp if (pid == -1) {
1672 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1673 ddc7b220 2019-09-08 stsp free(ibuf);
1674 ddc7b220 2019-09-08 stsp return err;
1676 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1677 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1678 9f2369b0 2018-12-24 stsp repo->path);
1679 9f2369b0 2018-12-24 stsp /* not reached */
1682 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
1683 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1684 ddc7b220 2019-09-08 stsp free(ibuf);
1685 ddc7b220 2019-09-08 stsp return err;
1687 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1688 9f2369b0 2018-12-24 stsp imsg_fds[0];
1689 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1690 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1691 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1693 d5c81d44 2021-07-08 stsp return request_tag(tag, repo, obj_fd, id);
1696 a158c901 2018-12-23 stsp static const struct got_error *
1697 268f7291 2018-12-24 stsp open_tag(struct got_tag_object **tag, struct got_repository *repo,
1698 268f7291 2018-12-24 stsp struct got_object_id *id, int check_cache)
1700 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1701 268f7291 2018-12-24 stsp struct got_packidx *packidx = NULL;
1703 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1704 5d844a1e 2019-08-13 stsp struct got_object *obj = NULL;
1705 5d844a1e 2019-08-13 stsp int obj_type = GOT_OBJ_TYPE_ANY;
1707 f4a881ce 2018-11-17 stsp if (check_cache) {
1708 268f7291 2018-12-24 stsp *tag = got_repo_get_cached_tag(repo, id);
1709 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1710 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1711 f4a881ce 2018-11-17 stsp return NULL;
1714 f4a881ce 2018-11-17 stsp *tag = NULL;
1716 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1717 e82b1d81 2019-07-27 stsp if (err == NULL) {
1718 268f7291 2018-12-24 stsp struct got_pack *pack = NULL;
1720 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
1721 aea75d87 2021-07-06 stsp packidx->path_packidx);
1723 268f7291 2018-12-24 stsp return err;
1725 268f7291 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1726 f4a881ce 2018-11-17 stsp if (pack == NULL) {
1727 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1730 8d2c5ea3 2019-08-13 stsp goto done;
1733 992eb9d8 2020-02-07 tracey /* Beware of "lightweight" tags: Check object type first. */
1734 5d844a1e 2019-08-13 stsp err = read_packed_object_privsep(&obj, repo, pack, packidx,
1737 5d844a1e 2019-08-13 stsp goto done;
1738 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1739 5d844a1e 2019-08-13 stsp got_object_close(obj);
1740 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG) {
1741 5d844a1e 2019-08-13 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1742 5d844a1e 2019-08-13 stsp goto done;
1744 5d844a1e 2019-08-13 stsp err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1745 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1748 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1750 e82b1d81 2019-07-27 stsp return err;
1751 d5c81d44 2021-07-08 stsp err = got_object_read_header_privsep(&obj, id, repo, fd);
1753 5d844a1e 2019-08-13 stsp return err;
1754 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1755 5d844a1e 2019-08-13 stsp got_object_close(obj);
1756 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
1757 5d844a1e 2019-08-13 stsp return got_error(GOT_ERR_OBJ_TYPE);
1759 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1761 5d844a1e 2019-08-13 stsp return err;
1762 d5c81d44 2021-07-08 stsp err = read_tag_privsep(tag, fd, id, repo);
1765 f4a881ce 2018-11-17 stsp if (err == NULL) {
1766 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1767 268f7291 2018-12-24 stsp err = got_repo_cache_tag(repo, id, *tag);
1770 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1771 f4a881ce 2018-11-17 stsp return err;
1774 f4a881ce 2018-11-17 stsp const struct got_error *
1775 f4a881ce 2018-11-17 stsp got_object_open_as_tag(struct got_tag_object **tag,
1776 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object_id *id)
1778 f4a881ce 2018-11-17 stsp *tag = got_repo_get_cached_tag(repo, id);
1779 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1780 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1781 f4a881ce 2018-11-17 stsp return NULL;
1784 268f7291 2018-12-24 stsp return open_tag(tag, repo, id, 0);
1787 f4a881ce 2018-11-17 stsp const struct got_error *
1788 f4a881ce 2018-11-17 stsp got_object_tag_open(struct got_tag_object **tag,
1789 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object *obj)
1791 268f7291 2018-12-24 stsp return open_tag(tag, repo, got_object_get_id(obj), 1);
1794 d24820bf 2019-08-11 stsp const char *
1795 d24820bf 2019-08-11 stsp got_object_tag_get_name(struct got_tag_object *tag)
1797 d24820bf 2019-08-11 stsp return tag->tag;
1801 0bd18d37 2019-02-01 stsp got_object_tag_get_object_type(struct got_tag_object *tag)
1803 0bd18d37 2019-02-01 stsp return tag->obj_type;
1806 0bd18d37 2019-02-01 stsp struct got_object_id *
1807 0bd18d37 2019-02-01 stsp got_object_tag_get_object_id(struct got_tag_object *tag)
1809 0bd18d37 2019-02-01 stsp return &tag->id;
1813 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_time(struct got_tag_object *tag)
1815 01073a5d 2019-08-22 stsp return tag->tagger_time;
1819 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1821 01073a5d 2019-08-22 stsp return tag->tagger_gmtoff;
1824 01073a5d 2019-08-22 stsp const char *
1825 01073a5d 2019-08-22 stsp got_object_tag_get_tagger(struct got_tag_object *tag)
1827 01073a5d 2019-08-22 stsp return tag->tagger;
1830 01073a5d 2019-08-22 stsp const char *
1831 01073a5d 2019-08-22 stsp got_object_tag_get_message(struct got_tag_object *tag)
1833 01073a5d 2019-08-22 stsp return tag->tagmsg;
1836 776d4d29 2018-06-17 stsp static struct got_tree_entry *
1837 65a9bbe9 2018-09-15 stsp find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1841 63da309a 2018-11-07 stsp /* Note that tree entries are sorted in strncmp() order. */
1842 56e0773d 2019-11-28 stsp for (i = 0; i < tree->nentries; i++) {
1843 56e0773d 2019-11-28 stsp struct got_tree_entry *te = &tree->entries[i];
1844 63da309a 2018-11-07 stsp int cmp = strncmp(te->name, name, len);
1845 63da309a 2018-11-07 stsp if (cmp < 0)
1847 63da309a 2018-11-07 stsp if (cmp > 0)
1849 63da309a 2018-11-07 stsp if (te->name[len] == '\0')
1850 776d4d29 2018-06-17 stsp return te;
1852 eb651edf 2018-02-11 stsp return NULL;
1855 56e0773d 2019-11-28 stsp struct got_tree_entry *
1856 a129376b 2019-03-28 stsp got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1858 a129376b 2019-03-28 stsp return find_entry_by_name(tree, name, strlen(name));
1861 776d4d29 2018-06-17 stsp const struct got_error *
1862 67b631c9 2021-10-10 stsp got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1863 67b631c9 2021-10-10 stsp struct got_repository *repo, struct got_tree_object *tree,
1864 67b631c9 2021-10-10 stsp const char *path)
1866 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
1867 67b631c9 2021-10-10 stsp struct got_tree_object *subtree = NULL;
1868 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
1869 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1870 b7cd37e5 2018-11-18 stsp size_t seglen;
1872 27d434c2 2018-09-15 stsp *id = NULL;
1875 5e54fb30 2019-05-31 stsp while (s[0] == '/')
1878 65a9bbe9 2018-09-15 stsp seglen = 0;
1879 67b631c9 2021-10-10 stsp subtree = tree;
1880 b7cd37e5 2018-11-18 stsp while (*s) {
1881 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
1883 776d4d29 2018-06-17 stsp if (*s != '/') {
1890 67b631c9 2021-10-10 stsp te = find_entry_by_name(subtree, seg, seglen);
1891 db37e2c0 2018-06-21 stsp if (te == NULL) {
1892 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1893 776d4d29 2018-06-17 stsp goto done;
1896 b7cd37e5 2018-11-18 stsp if (*s == '\0')
1899 776d4d29 2018-06-17 stsp seg = s + 1;
1900 65a9bbe9 2018-09-15 stsp seglen = 0;
1903 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
1905 db37e2c0 2018-06-21 stsp te = NULL;
1907 776d4d29 2018-06-17 stsp goto done;
1908 67b631c9 2021-10-10 stsp if (subtree != tree)
1909 67b631c9 2021-10-10 stsp got_object_tree_close(subtree);
1910 67b631c9 2021-10-10 stsp subtree = next_tree;
1915 56e0773d 2019-11-28 stsp *id = got_object_id_dup(&te->id);
1916 27d434c2 2018-09-15 stsp if (*id == NULL)
1917 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
1919 67b631c9 2021-10-10 stsp *mode = te->mode;
1921 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1923 67b631c9 2021-10-10 stsp if (subtree && subtree != tree)
1924 67b631c9 2021-10-10 stsp got_object_tree_close(subtree);
1925 67b631c9 2021-10-10 stsp return err;
1927 67b631c9 2021-10-10 stsp const struct got_error *
1928 67b631c9 2021-10-10 stsp got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1929 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
1931 67b631c9 2021-10-10 stsp const struct got_error *err = NULL;
1932 67b631c9 2021-10-10 stsp struct got_tree_object *tree = NULL;
1934 67b631c9 2021-10-10 stsp *id = NULL;
1936 67b631c9 2021-10-10 stsp /* Handle opening of root of commit's tree. */
1937 67b631c9 2021-10-10 stsp if (got_path_is_root_dir(path)) {
1938 67b631c9 2021-10-10 stsp *id = got_object_id_dup(commit->tree_id);
1939 67b631c9 2021-10-10 stsp if (*id == NULL)
1940 67b631c9 2021-10-10 stsp err = got_error_from_errno("got_object_id_dup");
1942 67b631c9 2021-10-10 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1944 67b631c9 2021-10-10 stsp goto done;
1945 67b631c9 2021-10-10 stsp err = got_object_tree_find_path(id, NULL, repo, tree, path);
1949 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
1950 776d4d29 2018-06-17 stsp return err;
1954 ac5f2b26 2020-05-05 stsp * Normalize file mode bits to avoid false positive tree entry differences
1955 ac5f2b26 2020-05-05 stsp * in case tree entries have unexpected mode bits set.
1957 ac5f2b26 2020-05-05 stsp static mode_t
1958 ac5f2b26 2020-05-05 stsp normalize_mode_for_comparison(mode_t mode)
1961 ac5f2b26 2020-05-05 stsp * For directories, the only relevant bit is the IFDIR bit.
1962 ac5f2b26 2020-05-05 stsp * This allows us to detect paths changing from a directory
1963 ac5f2b26 2020-05-05 stsp * to a file and vice versa.
1965 ac5f2b26 2020-05-05 stsp if (S_ISDIR(mode))
1966 ac5f2b26 2020-05-05 stsp return mode & S_IFDIR;
1969 40dde666 2020-07-23 stsp * For symlinks, the only relevant bit is the IFLNK bit.
1970 40dde666 2020-07-23 stsp * This allows us to detect paths changing from a symlinks
1971 40dde666 2020-07-23 stsp * to a file or directory and vice versa.
1973 40dde666 2020-07-23 stsp if (S_ISLNK(mode))
1974 40dde666 2020-07-23 stsp return mode & S_IFLNK;
1976 ac5f2b26 2020-05-05 stsp /* For files, the only change we care about is the executable bit. */
1977 ac5f2b26 2020-05-05 stsp return mode & S_IXUSR;
1980 07862c20 2018-09-15 stsp const struct got_error *
1981 07862c20 2018-09-15 stsp got_object_tree_path_changed(int *changed,
1982 07862c20 2018-09-15 stsp struct got_tree_object *tree01, struct got_tree_object *tree02,
1983 07862c20 2018-09-15 stsp const char *path, struct got_repository *repo)
1985 07862c20 2018-09-15 stsp const struct got_error *err = NULL;
1986 07862c20 2018-09-15 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1987 07862c20 2018-09-15 stsp struct got_tree_entry *te1 = NULL, *te2 = NULL;
1988 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1989 3b7f9878 2018-11-18 stsp size_t seglen;
1991 07862c20 2018-09-15 stsp *changed = 0;
1993 07862c20 2018-09-15 stsp /* We not do support comparing the root path. */
1994 61a7d79f 2020-02-29 stsp if (got_path_is_root_dir(path))
1995 63f810e6 2020-02-29 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
1997 07862c20 2018-09-15 stsp tree1 = tree01;
1998 07862c20 2018-09-15 stsp tree2 = tree02;
2000 61a7d79f 2020-02-29 stsp while (*s == '/')
2003 65a9bbe9 2018-09-15 stsp seglen = 0;
2004 3b7f9878 2018-11-18 stsp while (*s) {
2005 07862c20 2018-09-15 stsp struct got_tree_object *next_tree1, *next_tree2;
2006 ac5f2b26 2020-05-05 stsp mode_t mode1, mode2;
2008 07862c20 2018-09-15 stsp if (*s != '/') {
2015 65a9bbe9 2018-09-15 stsp te1 = find_entry_by_name(tree1, seg, seglen);
2016 07862c20 2018-09-15 stsp if (te1 == NULL) {
2017 07862c20 2018-09-15 stsp err = got_error(GOT_ERR_NO_OBJ);
2018 07862c20 2018-09-15 stsp goto done;
2021 e8bfb8f3 2020-12-18 stsp if (tree2)
2022 e8bfb8f3 2020-12-18 stsp te2 = find_entry_by_name(tree2, seg, seglen);
2024 e8bfb8f3 2020-12-18 stsp if (te2) {
2025 e8bfb8f3 2020-12-18 stsp mode1 = normalize_mode_for_comparison(te1->mode);
2026 e8bfb8f3 2020-12-18 stsp mode2 = normalize_mode_for_comparison(te2->mode);
2027 e8bfb8f3 2020-12-18 stsp if (mode1 != mode2) {
2028 e8bfb8f3 2020-12-18 stsp *changed = 1;
2029 e8bfb8f3 2020-12-18 stsp goto done;
2032 e8bfb8f3 2020-12-18 stsp if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2033 e8bfb8f3 2020-12-18 stsp *changed = 0;
2034 e8bfb8f3 2020-12-18 stsp goto done;
2038 3b7f9878 2018-11-18 stsp if (*s == '\0') { /* final path element */
2039 07862c20 2018-09-15 stsp *changed = 1;
2040 07862c20 2018-09-15 stsp goto done;
2043 07862c20 2018-09-15 stsp seg = s + 1;
2045 65a9bbe9 2018-09-15 stsp seglen = 0;
2047 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree1, repo,
2048 56e0773d 2019-11-28 stsp &te1->id);
2049 07862c20 2018-09-15 stsp te1 = NULL;
2051 07862c20 2018-09-15 stsp goto done;
2052 a31cea73 2018-09-15 stsp if (tree1 != tree01)
2053 a31cea73 2018-09-15 stsp got_object_tree_close(tree1);
2054 07862c20 2018-09-15 stsp tree1 = next_tree1;
2056 e8bfb8f3 2020-12-18 stsp if (te2) {
2057 e8bfb8f3 2020-12-18 stsp err = got_object_open_as_tree(&next_tree2, repo,
2058 e8bfb8f3 2020-12-18 stsp &te2->id);
2059 e8bfb8f3 2020-12-18 stsp te2 = NULL;
2061 e8bfb8f3 2020-12-18 stsp goto done;
2062 e8bfb8f3 2020-12-18 stsp if (tree2 != tree02)
2063 e8bfb8f3 2020-12-18 stsp got_object_tree_close(tree2);
2064 e8bfb8f3 2020-12-18 stsp tree2 = next_tree2;
2065 e8bfb8f3 2020-12-18 stsp } else if (tree2) {
2066 e8bfb8f3 2020-12-18 stsp if (tree2 != tree02)
2067 e8bfb8f3 2020-12-18 stsp got_object_tree_close(tree2);
2068 e8bfb8f3 2020-12-18 stsp tree2 = NULL;
2073 a31cea73 2018-09-15 stsp if (tree1 && tree1 != tree01)
2074 07862c20 2018-09-15 stsp got_object_tree_close(tree1);
2075 a31cea73 2018-09-15 stsp if (tree2 && tree2 != tree02)
2076 07862c20 2018-09-15 stsp got_object_tree_close(tree2);
2077 77880158 2018-11-04 stsp return err;
2080 ed175427 2019-05-09 stsp const struct got_error *
2081 ed175427 2019-05-09 stsp got_object_tree_entry_dup(struct got_tree_entry **new_te,
2082 ed175427 2019-05-09 stsp struct got_tree_entry *te)
2084 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
2086 ed175427 2019-05-09 stsp *new_te = calloc(1, sizeof(**new_te));
2087 ed175427 2019-05-09 stsp if (*new_te == NULL)
2088 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
2090 ed175427 2019-05-09 stsp (*new_te)->mode = te->mode;
2091 56e0773d 2019-11-28 stsp memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2092 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2093 8c4eabf2 2019-05-10 stsp return err;
2097 56e0773d 2019-11-28 stsp got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2099 63c5ca5d 2019-08-24 stsp return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2103 e40622f4 2020-07-23 stsp got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2105 e40622f4 2020-07-23 stsp /* S_IFDIR check avoids confusing symlinks with submodules. */
2106 e40622f4 2020-07-23 stsp return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2109 e40622f4 2020-07-23 stsp static const struct got_error *
2110 e40622f4 2020-07-23 stsp resolve_symlink(char **link_target, const char *path,
2111 a44927cc 2022-04-07 stsp struct got_commit_object *commit, struct got_repository *repo)
2113 e40622f4 2020-07-23 stsp const struct got_error *err = NULL;
2114 dbdd6209 2020-10-19 stsp char buf[PATH_MAX];
2115 e40622f4 2020-07-23 stsp char *name, *parent_path = NULL;
2116 e40622f4 2020-07-23 stsp struct got_object_id *tree_obj_id = NULL;
2117 e40622f4 2020-07-23 stsp struct got_tree_object *tree = NULL;
2118 e40622f4 2020-07-23 stsp struct got_tree_entry *te = NULL;
2120 e40622f4 2020-07-23 stsp *link_target = NULL;
2122 dbdd6209 2020-10-19 stsp if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2123 dbdd6209 2020-10-19 stsp return got_error(GOT_ERR_NO_SPACE);
2125 dbdd6209 2020-10-19 stsp name = basename(buf);
2126 e40622f4 2020-07-23 stsp if (name == NULL)
2127 e40622f4 2020-07-23 stsp return got_error_from_errno2("basename", path);
2129 e40622f4 2020-07-23 stsp err = got_path_dirname(&parent_path, path);
2131 e40622f4 2020-07-23 stsp return err;
2133 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_obj_id, repo, commit,
2134 e40622f4 2020-07-23 stsp parent_path);
2135 e40622f4 2020-07-23 stsp if (err) {
2136 e40622f4 2020-07-23 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2137 e40622f4 2020-07-23 stsp /* Display the complete path in error message. */
2138 e40622f4 2020-07-23 stsp err = got_error_path(path, err->code);
2140 e40622f4 2020-07-23 stsp goto done;
2143 e40622f4 2020-07-23 stsp err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2145 e40622f4 2020-07-23 stsp goto done;
2147 e40622f4 2020-07-23 stsp te = got_object_tree_find_entry(tree, name);
2148 e40622f4 2020-07-23 stsp if (te == NULL) {
2149 e40622f4 2020-07-23 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2150 e40622f4 2020-07-23 stsp goto done;
2153 e40622f4 2020-07-23 stsp if (got_object_tree_entry_is_symlink(te)) {
2154 e40622f4 2020-07-23 stsp err = got_tree_entry_get_symlink_target(link_target, te, repo);
2156 e40622f4 2020-07-23 stsp goto done;
2157 e40622f4 2020-07-23 stsp if (!got_path_is_absolute(*link_target)) {
2158 e40622f4 2020-07-23 stsp char *abspath;
2159 e40622f4 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", parent_path,
2160 e40622f4 2020-07-23 stsp *link_target) == -1) {
2161 e40622f4 2020-07-23 stsp err = got_error_from_errno("asprintf");
2162 e40622f4 2020-07-23 stsp goto done;
2164 e40622f4 2020-07-23 stsp free(*link_target);
2165 e40622f4 2020-07-23 stsp *link_target = malloc(PATH_MAX);
2166 e40622f4 2020-07-23 stsp if (*link_target == NULL) {
2167 e40622f4 2020-07-23 stsp err = got_error_from_errno("malloc");
2168 e40622f4 2020-07-23 stsp goto done;
2170 e40622f4 2020-07-23 stsp err = got_canonpath(abspath, *link_target, PATH_MAX);
2171 e40622f4 2020-07-23 stsp free(abspath);
2173 e40622f4 2020-07-23 stsp goto done;
2177 e40622f4 2020-07-23 stsp free(tree_obj_id);
2179 e40622f4 2020-07-23 stsp got_object_tree_close(tree);
2180 e40622f4 2020-07-23 stsp if (err) {
2181 e40622f4 2020-07-23 stsp free(*link_target);
2182 e40622f4 2020-07-23 stsp *link_target = NULL;
2184 e40622f4 2020-07-23 stsp return err;
2187 ca6e02ac 2020-01-07 stsp const struct got_error *
2188 e40622f4 2020-07-23 stsp got_object_resolve_symlinks(char **link_target, const char *path,
2189 a44927cc 2022-04-07 stsp struct got_commit_object *commit, struct got_repository *repo)
2191 e40622f4 2020-07-23 stsp const struct got_error *err = NULL;
2192 e40622f4 2020-07-23 stsp char *next_target = NULL;
2193 e40622f4 2020-07-23 stsp int max_recursion = 40; /* matches Git */
2195 e40622f4 2020-07-23 stsp *link_target = NULL;
2198 e40622f4 2020-07-23 stsp err = resolve_symlink(&next_target,
2199 a44927cc 2022-04-07 stsp *link_target ? *link_target : path, commit, repo);
2202 e40622f4 2020-07-23 stsp if (next_target) {
2203 e40622f4 2020-07-23 stsp free(*link_target);
2204 e40622f4 2020-07-23 stsp if (--max_recursion == 0) {
2205 e40622f4 2020-07-23 stsp err = got_error_path(path, GOT_ERR_RECURSION);
2206 e40622f4 2020-07-23 stsp *link_target = NULL;
2209 e40622f4 2020-07-23 stsp *link_target = next_target;
2211 e40622f4 2020-07-23 stsp } while (next_target);
2213 e40622f4 2020-07-23 stsp return err;
2216 e40622f4 2020-07-23 stsp const struct got_error *
2217 ca6e02ac 2020-01-07 stsp got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2218 ca6e02ac 2020-01-07 stsp struct got_object_id *commit_id, const char *path,
2219 ca6e02ac 2020-01-07 stsp struct got_repository *repo)
2221 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
2222 ca6e02ac 2020-01-07 stsp struct got_pack *pack = NULL;
2223 ca6e02ac 2020-01-07 stsp struct got_packidx *packidx = NULL;
2224 ca6e02ac 2020-01-07 stsp char *path_packfile = NULL;
2225 ca6e02ac 2020-01-07 stsp struct got_commit_object *changed_commit = NULL;
2226 ca6e02ac 2020-01-07 stsp struct got_object_id *changed_commit_id = NULL;
2229 ca6e02ac 2020-01-07 stsp err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2230 ca6e02ac 2020-01-07 stsp if (err) {
2231 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
2232 ca6e02ac 2020-01-07 stsp return err;
2233 ca6e02ac 2020-01-07 stsp return NULL;
2236 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
2237 aea75d87 2021-07-06 stsp packidx->path_packidx);
2239 ca6e02ac 2020-01-07 stsp return err;
2241 ca6e02ac 2020-01-07 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
2242 ca6e02ac 2020-01-07 stsp if (pack == NULL) {
2243 ca6e02ac 2020-01-07 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2245 ca6e02ac 2020-01-07 stsp goto done;
2248 ca6e02ac 2020-01-07 stsp if (pack->privsep_child == NULL) {
2249 3d589bee 2022-06-25 stsp err = got_pack_start_privsep_child(pack, packidx);
2251 ca6e02ac 2020-01-07 stsp goto done;
2254 ca6e02ac 2020-01-07 stsp err = got_privsep_send_commit_traversal_request(
2255 ca6e02ac 2020-01-07 stsp pack->privsep_child->ibuf, commit_id, idx, path);
2257 ca6e02ac 2020-01-07 stsp goto done;
2259 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_traversed_commits(&changed_commit,
2260 ca6e02ac 2020-01-07 stsp &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2262 ca6e02ac 2020-01-07 stsp goto done;
2264 ca6e02ac 2020-01-07 stsp if (changed_commit) {
2266 ca6e02ac 2020-01-07 stsp * Cache the commit in which the path was changed.
2267 ca6e02ac 2020-01-07 stsp * This commit might be opened again soon.
2269 ca6e02ac 2020-01-07 stsp changed_commit->refcnt++;
2270 ca6e02ac 2020-01-07 stsp err = got_repo_cache_commit(repo, changed_commit_id,
2271 ca6e02ac 2020-01-07 stsp changed_commit);
2272 ca6e02ac 2020-01-07 stsp got_object_commit_close(changed_commit);
2275 ca6e02ac 2020-01-07 stsp free(path_packfile);
2276 ca6e02ac 2020-01-07 stsp free(changed_commit_id);
2277 ca6e02ac 2020-01-07 stsp return err;
2280 0ab4c957 2022-06-13 stsp const struct got_error *
2281 db9b9b1c 2022-06-14 stsp got_object_enumerate(int *found_all_objects,
2282 db9b9b1c 2022-06-14 stsp got_object_enumerate_commit_cb cb_commit,
2283 0ab4c957 2022-06-13 stsp got_object_enumerate_tree_cb cb_tree, void *cb_arg,
2284 0ab4c957 2022-06-13 stsp struct got_object_id **ours, int nours,
2285 0ab4c957 2022-06-13 stsp struct got_object_id **theirs, int ntheirs,
2286 0ab4c957 2022-06-13 stsp struct got_packidx *packidx, struct got_repository *repo)
2288 0ab4c957 2022-06-13 stsp const struct got_error *err = NULL;
2289 0ab4c957 2022-06-13 stsp struct got_pack *pack;
2290 0ab4c957 2022-06-13 stsp char *path_packfile = NULL;
2292 0ab4c957 2022-06-13 stsp err = got_packidx_get_packfile_path(&path_packfile,
2293 0ab4c957 2022-06-13 stsp packidx->path_packidx);
2295 0ab4c957 2022-06-13 stsp return err;
2297 0ab4c957 2022-06-13 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
2298 0ab4c957 2022-06-13 stsp if (pack == NULL) {
2299 0ab4c957 2022-06-13 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2301 0ab4c957 2022-06-13 stsp goto done;
2304 0ab4c957 2022-06-13 stsp if (pack->privsep_child == NULL) {
2305 3d589bee 2022-06-25 stsp err = got_pack_start_privsep_child(pack, packidx);
2307 0ab4c957 2022-06-13 stsp goto done;
2310 0ab4c957 2022-06-13 stsp err = got_privsep_send_object_enumeration_request(
2311 0ab4c957 2022-06-13 stsp pack->privsep_child->ibuf);
2313 0ab4c957 2022-06-13 stsp goto done;
2315 0ab4c957 2022-06-13 stsp err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
2316 0ab4c957 2022-06-13 stsp ours, nours);
2318 0ab4c957 2022-06-13 stsp goto done;
2319 0ab4c957 2022-06-13 stsp err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
2321 0ab4c957 2022-06-13 stsp goto done;
2323 0ab4c957 2022-06-13 stsp err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
2324 0ab4c957 2022-06-13 stsp theirs, ntheirs);
2326 0ab4c957 2022-06-13 stsp goto done;
2327 0ab4c957 2022-06-13 stsp err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
2329 0ab4c957 2022-06-13 stsp goto done;
2331 db9b9b1c 2022-06-14 stsp err = got_privsep_recv_enumerated_objects(found_all_objects,
2332 db9b9b1c 2022-06-14 stsp pack->privsep_child->ibuf, cb_commit, cb_tree, cb_arg, repo);
2334 0ab4c957 2022-06-13 stsp free(path_packfile);
2335 0ab4c957 2022-06-13 stsp return err;