2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 876c234b 2018-09-10 stsp * Permission to use, copy, modify, and distribute this software for any
5 876c234b 2018-09-10 stsp * purpose with or without fee is hereby granted, provided that the above
6 876c234b 2018-09-10 stsp * copyright notice and this permission notice appear in all copies.
8 876c234b 2018-09-10 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 876c234b 2018-09-10 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 876c234b 2018-09-10 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 876c234b 2018-09-10 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 876c234b 2018-09-10 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 876c234b 2018-09-10 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 876c234b 2018-09-10 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 876c234b 2018-09-10 stsp #include <sys/types.h>
18 876c234b 2018-09-10 stsp #include <sys/queue.h>
19 876c234b 2018-09-10 stsp #include <sys/uio.h>
20 876c234b 2018-09-10 stsp #include <sys/time.h>
21 876c234b 2018-09-10 stsp #include <sys/mman.h>
23 876c234b 2018-09-10 stsp #include <limits.h>
24 99437157 2018-11-11 stsp #include <signal.h>
25 876c234b 2018-09-10 stsp #include <stdint.h>
26 876c234b 2018-09-10 stsp #include <imsg.h>
27 876c234b 2018-09-10 stsp #include <stdio.h>
28 876c234b 2018-09-10 stsp #include <stdlib.h>
29 876c234b 2018-09-10 stsp #include <string.h>
30 876c234b 2018-09-10 stsp #include <sha1.h>
31 81a12da5 2020-09-09 naddy #include <unistd.h>
32 876c234b 2018-09-10 stsp #include <zlib.h>
34 876c234b 2018-09-10 stsp #include "got_error.h"
35 876c234b 2018-09-10 stsp #include "got_object.h"
36 3022d272 2019-11-14 stsp #include "got_path.h"
38 876c234b 2018-09-10 stsp #include "got_lib_delta.h"
39 ab2f42e7 2019-11-10 stsp #include "got_lib_delta_cache.h"
40 876c234b 2018-09-10 stsp #include "got_lib_object.h"
41 c59b3346 2018-09-11 stsp #include "got_lib_object_cache.h"
42 876c234b 2018-09-10 stsp #include "got_lib_object_parse.h"
43 fae7e038 2022-05-07 stsp #include "got_lib_object_idset.h"
44 876c234b 2018-09-10 stsp #include "got_lib_privsep.h"
45 876c234b 2018-09-10 stsp #include "got_lib_pack.h"
47 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
50 99437157 2018-11-11 stsp catch_sigint(int signo)
52 99437157 2018-11-11 stsp sigint_received = 1;
55 876c234b 2018-09-10 stsp static const struct got_error *
56 704b89c4 2019-05-23 stsp open_object(struct got_object **obj, struct got_pack *pack,
57 704b89c4 2019-05-23 stsp struct got_packidx *packidx, int idx, struct got_object_id *id,
58 704b89c4 2019-05-23 stsp struct got_object_cache *objcache)
60 704b89c4 2019-05-23 stsp const struct got_error *err;
62 704b89c4 2019-05-23 stsp err = got_packfile_open_object(obj, pack, packidx, idx, id);
65 704b89c4 2019-05-23 stsp (*obj)->refcnt++;
67 704b89c4 2019-05-23 stsp err = got_object_cache_add(objcache, id, *obj);
69 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
70 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
74 704b89c4 2019-05-23 stsp (*obj)->refcnt++;
75 704b89c4 2019-05-23 stsp return NULL;
78 704b89c4 2019-05-23 stsp static const struct got_error *
79 876c234b 2018-09-10 stsp object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
80 c59b3346 2018-09-11 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
82 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
83 876c234b 2018-09-10 stsp struct got_imsg_packed_object iobj;
84 876c234b 2018-09-10 stsp struct got_object *obj;
85 106807b4 2018-09-15 stsp struct got_object_id id;
86 876c234b 2018-09-10 stsp size_t datalen;
88 876c234b 2018-09-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
89 876c234b 2018-09-10 stsp if (datalen != sizeof(iobj))
90 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
91 876c234b 2018-09-10 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
92 106807b4 2018-09-15 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
94 704b89c4 2019-05-23 stsp obj = got_object_cache_get(objcache, &id);
96 704b89c4 2019-05-23 stsp obj->refcnt++;
98 704b89c4 2019-05-23 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
104 876c234b 2018-09-10 stsp err = got_privsep_send_obj(ibuf, obj);
106 876c234b 2018-09-10 stsp got_object_close(obj);
107 876c234b 2018-09-10 stsp return err;
110 50127d69 2021-09-25 stsp static const struct got_error *
111 ca6e02ac 2020-01-07 stsp open_commit(struct got_commit_object **commit, struct got_pack *pack,
112 ca6e02ac 2020-01-07 stsp struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
113 ca6e02ac 2020-01-07 stsp struct got_object_cache *objcache)
115 cfd633c2 2018-09-10 stsp const struct got_error *err = NULL;
116 cb5e38fd 2019-05-23 stsp struct got_object *obj = NULL;
117 cb5e38fd 2019-05-23 stsp uint8_t *buf = NULL;
118 cfd633c2 2018-09-10 stsp size_t len;
120 ca6e02ac 2020-01-07 stsp *commit = NULL;
122 ca6e02ac 2020-01-07 stsp obj = got_object_cache_get(objcache, id);
124 704b89c4 2019-05-23 stsp obj->refcnt++;
126 ca6e02ac 2020-01-07 stsp err = open_object(&obj, pack, packidx, obj_idx, id,
129 704b89c4 2019-05-23 stsp return err;
132 cfd633c2 2018-09-10 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
136 cfd633c2 2018-09-10 stsp obj->size = len;
138 ca6e02ac 2020-01-07 stsp err = got_object_parse_commit(commit, buf, len);
140 ca6e02ac 2020-01-07 stsp got_object_close(obj);
142 ca6e02ac 2020-01-07 stsp return err;
145 ca6e02ac 2020-01-07 stsp static const struct got_error *
146 ca6e02ac 2020-01-07 stsp commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
147 ca6e02ac 2020-01-07 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
149 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
150 ca6e02ac 2020-01-07 stsp struct got_imsg_packed_object iobj;
151 ca6e02ac 2020-01-07 stsp struct got_commit_object *commit = NULL;
152 ca6e02ac 2020-01-07 stsp struct got_object_id id;
153 ca6e02ac 2020-01-07 stsp size_t datalen;
155 ca6e02ac 2020-01-07 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
156 ca6e02ac 2020-01-07 stsp if (datalen != sizeof(iobj))
157 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
158 ca6e02ac 2020-01-07 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
159 ca6e02ac 2020-01-07 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
161 ca6e02ac 2020-01-07 stsp err = open_commit(&commit, pack, packidx, iobj.idx, &id, objcache);
165 cfd633c2 2018-09-10 stsp err = got_privsep_send_commit(ibuf, commit);
167 cb5e38fd 2019-05-23 stsp if (commit)
168 cb5e38fd 2019-05-23 stsp got_object_commit_close(commit);
170 7762fe12 2018-11-05 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
171 7762fe12 2018-11-05 stsp err = NULL;
173 7762fe12 2018-11-05 stsp got_privsep_send_error(ibuf, err);
176 7762fe12 2018-11-05 stsp return err;
179 50127d69 2021-09-25 stsp static const struct got_error *
180 9985f404 2022-05-19 stsp open_tree(uint8_t **buf, struct got_parsed_tree_entry **entries, int *nentries,
181 ca6e02ac 2020-01-07 stsp struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
182 ca6e02ac 2020-01-07 stsp struct got_object_id *id, struct got_object_cache *objcache)
184 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
185 ca6e02ac 2020-01-07 stsp struct got_object *obj = NULL;
186 ca6e02ac 2020-01-07 stsp size_t len;
188 ca6e02ac 2020-01-07 stsp *buf = NULL;
189 ca6e02ac 2020-01-07 stsp *nentries = 0;
191 ca6e02ac 2020-01-07 stsp obj = got_object_cache_get(objcache, id);
193 ca6e02ac 2020-01-07 stsp obj->refcnt++;
195 ca6e02ac 2020-01-07 stsp err = open_object(&obj, pack, packidx, obj_idx, id,
198 ca6e02ac 2020-01-07 stsp return err;
201 ca6e02ac 2020-01-07 stsp err = got_packfile_extract_object_to_mem(buf, &len, obj, pack);
205 ca6e02ac 2020-01-07 stsp obj->size = len;
207 ca6e02ac 2020-01-07 stsp err = got_object_parse_tree(entries, nentries, *buf, len);
209 ca6e02ac 2020-01-07 stsp got_object_close(obj);
211 ca6e02ac 2020-01-07 stsp free(*buf);
212 ca6e02ac 2020-01-07 stsp *buf = NULL;
214 ca6e02ac 2020-01-07 stsp return err;
217 7762fe12 2018-11-05 stsp static const struct got_error *
218 876c234b 2018-09-10 stsp tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
219 c59b3346 2018-09-11 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
221 e7885405 2018-09-10 stsp const struct got_error *err = NULL;
222 13c729f7 2018-12-24 stsp struct got_imsg_packed_object iobj;
223 9985f404 2022-05-19 stsp struct got_parsed_tree_entry *entries = NULL;
224 3022d272 2019-11-14 stsp int nentries = 0;
225 cb5e38fd 2019-05-23 stsp uint8_t *buf = NULL;
226 13c729f7 2018-12-24 stsp struct got_object_id id;
227 13c729f7 2018-12-24 stsp size_t datalen;
229 13c729f7 2018-12-24 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
230 13c729f7 2018-12-24 stsp if (datalen != sizeof(iobj))
231 13c729f7 2018-12-24 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
232 13c729f7 2018-12-24 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
233 13c729f7 2018-12-24 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
235 ca6e02ac 2020-01-07 stsp err = open_tree(&buf, &entries, &nentries, pack, packidx, iobj.idx,
236 62d463ca 2020-10-20 naddy &id, objcache);
238 ca6e02ac 2020-01-07 stsp return err;
240 9985f404 2022-05-19 stsp err = got_privsep_send_tree(ibuf, entries, nentries);
241 9985f404 2022-05-19 stsp free(entries);
244 e7885405 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
245 e7885405 2018-09-10 stsp err = NULL;
247 e7885405 2018-09-10 stsp got_privsep_send_error(ibuf, err);
250 e7885405 2018-09-10 stsp return err;
253 876c234b 2018-09-10 stsp static const struct got_error *
254 030daac8 2021-09-25 stsp receive_file(FILE **f, struct imsgbuf *ibuf, uint32_t imsg_code)
256 3840f4c9 2018-09-12 stsp const struct got_error *err;
257 3840f4c9 2018-09-12 stsp struct imsg imsg;
258 55da3778 2018-09-10 stsp size_t datalen;
260 3840f4c9 2018-09-12 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
262 55da3778 2018-09-10 stsp return err;
264 3840f4c9 2018-09-12 stsp if (imsg.hdr.type != imsg_code) {
265 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
269 3840f4c9 2018-09-12 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
270 55da3778 2018-09-10 stsp if (datalen != 0) {
271 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
274 3840f4c9 2018-09-12 stsp if (imsg.fd == -1) {
275 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
279 3840f4c9 2018-09-12 stsp *f = fdopen(imsg.fd, "w+");
280 3840f4c9 2018-09-12 stsp if (*f == NULL) {
281 638f9024 2019-05-13 stsp err = got_error_from_errno("fdopen");
282 3a6ce05a 2019-02-11 stsp close(imsg.fd);
286 3840f4c9 2018-09-12 stsp imsg_free(&imsg);
287 3840f4c9 2018-09-12 stsp return err;
290 db696021 2022-01-04 stsp static const struct got_error *
291 67fd6849 2022-02-13 stsp receive_tempfile(FILE **f, const char *mode, struct imsg *imsg,
292 db696021 2022-01-04 stsp struct imsgbuf *ibuf)
294 db696021 2022-01-04 stsp size_t datalen;
296 db696021 2022-01-04 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
297 db696021 2022-01-04 stsp if (datalen != 0)
298 db696021 2022-01-04 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
300 db696021 2022-01-04 stsp if (imsg->fd == -1)
301 db696021 2022-01-04 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
303 67fd6849 2022-02-13 stsp *f = fdopen(imsg->fd, mode);
304 db696021 2022-01-04 stsp if (*f == NULL)
305 db696021 2022-01-04 stsp return got_error_from_errno("fdopen");
306 db696021 2022-01-04 stsp imsg->fd = -1;
308 db696021 2022-01-04 stsp return NULL;
311 3840f4c9 2018-09-12 stsp static const struct got_error *
312 3840f4c9 2018-09-12 stsp blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
313 db696021 2022-01-04 stsp struct got_packidx *packidx, struct got_object_cache *objcache,
314 db696021 2022-01-04 stsp FILE *basefile, FILE *accumfile)
316 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
317 ebc55e2d 2018-12-24 stsp struct got_imsg_packed_object iobj;
318 3840f4c9 2018-09-12 stsp struct got_object *obj = NULL;
319 db696021 2022-01-04 stsp FILE *outfile = NULL;
320 ebc55e2d 2018-12-24 stsp struct got_object_id id;
321 ebc55e2d 2018-12-24 stsp size_t datalen;
322 ac544f8c 2019-01-13 stsp uint64_t blob_size;
323 ac544f8c 2019-01-13 stsp uint8_t *buf = NULL;
325 ebc55e2d 2018-12-24 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
326 ebc55e2d 2018-12-24 stsp if (datalen != sizeof(iobj))
327 ebc55e2d 2018-12-24 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
328 ebc55e2d 2018-12-24 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
329 ebc55e2d 2018-12-24 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
331 704b89c4 2019-05-23 stsp obj = got_object_cache_get(objcache, &id);
333 704b89c4 2019-05-23 stsp obj->refcnt++;
335 704b89c4 2019-05-23 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
338 704b89c4 2019-05-23 stsp return err;
341 3840f4c9 2018-09-12 stsp err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
345 ac544f8c 2019-01-13 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
346 42c69117 2019-11-10 stsp err = got_pack_get_max_delta_object_size(&blob_size, obj, pack);
350 ac544f8c 2019-01-13 stsp blob_size = obj->size;
352 ac544f8c 2019-01-13 stsp if (blob_size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
353 ac544f8c 2019-01-13 stsp err = got_packfile_extract_object_to_mem(&buf, &obj->size,
354 ac544f8c 2019-01-13 stsp obj, pack);
356 ac544f8c 2019-01-13 stsp err = got_packfile_extract_object(pack, obj, outfile, basefile,
357 ac544f8c 2019-01-13 stsp accumfile);
361 ac544f8c 2019-01-13 stsp err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, buf);
364 56b63ca4 2021-01-22 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
365 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
366 cb5e38fd 2019-05-23 stsp got_object_close(obj);
367 3840f4c9 2018-09-12 stsp if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
368 3840f4c9 2018-09-12 stsp got_privsep_send_error(ibuf, err);
370 55da3778 2018-09-10 stsp return err;
373 876c234b 2018-09-10 stsp static const struct got_error *
374 f4a881ce 2018-11-17 stsp tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
375 f4a881ce 2018-11-17 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
377 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
378 268f7291 2018-12-24 stsp struct got_imsg_packed_object iobj;
379 f4a881ce 2018-11-17 stsp struct got_object *obj = NULL;
380 f4a881ce 2018-11-17 stsp struct got_tag_object *tag = NULL;
381 cb5e38fd 2019-05-23 stsp uint8_t *buf = NULL;
382 f4a881ce 2018-11-17 stsp size_t len;
383 268f7291 2018-12-24 stsp struct got_object_id id;
384 268f7291 2018-12-24 stsp size_t datalen;
386 268f7291 2018-12-24 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
387 268f7291 2018-12-24 stsp if (datalen != sizeof(iobj))
388 268f7291 2018-12-24 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
389 268f7291 2018-12-24 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
390 268f7291 2018-12-24 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
392 704b89c4 2019-05-23 stsp obj = got_object_cache_get(objcache, &id);
394 704b89c4 2019-05-23 stsp obj->refcnt++;
396 704b89c4 2019-05-23 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
399 704b89c4 2019-05-23 stsp return err;
402 f4a881ce 2018-11-17 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
406 f4a881ce 2018-11-17 stsp obj->size = len;
407 f4a881ce 2018-11-17 stsp err = got_object_parse_tag(&tag, buf, len);
411 f4a881ce 2018-11-17 stsp err = got_privsep_send_tag(ibuf, tag);
414 cb5e38fd 2019-05-23 stsp got_object_close(obj);
416 cb5e38fd 2019-05-23 stsp got_object_tag_close(tag);
418 ca6e02ac 2020-01-07 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
419 ca6e02ac 2020-01-07 stsp err = NULL;
421 ca6e02ac 2020-01-07 stsp got_privsep_send_error(ibuf, err);
424 ca6e02ac 2020-01-07 stsp return err;
427 ca6e02ac 2020-01-07 stsp static struct got_parsed_tree_entry *
428 9985f404 2022-05-19 stsp find_entry_by_name(struct got_parsed_tree_entry *entries, int nentries,
429 ca6e02ac 2020-01-07 stsp const char *name, size_t len)
431 9985f404 2022-05-19 stsp struct got_parsed_tree_entry *pte;
432 9985f404 2022-05-19 stsp int cmp, i;
434 ca6e02ac 2020-01-07 stsp /* Note that tree entries are sorted in strncmp() order. */
435 9985f404 2022-05-19 stsp for (i = 0; i < nentries; i++) {
436 9985f404 2022-05-19 stsp pte = &entries[i];
437 9985f404 2022-05-19 stsp cmp = strncmp(pte->name, name, len);
438 ca6e02ac 2020-01-07 stsp if (cmp < 0)
440 ca6e02ac 2020-01-07 stsp if (cmp > 0)
442 9985f404 2022-05-19 stsp if (pte->name[len] == '\0')
443 9985f404 2022-05-19 stsp return pte;
445 ca6e02ac 2020-01-07 stsp return NULL;
448 50127d69 2021-09-25 stsp static const struct got_error *
449 ca6e02ac 2020-01-07 stsp tree_path_changed(int *changed, uint8_t **buf1, uint8_t **buf2,
450 9985f404 2022-05-19 stsp struct got_parsed_tree_entry **entries1, int *nentries1,
451 9985f404 2022-05-19 stsp struct got_parsed_tree_entry **entries2, int *nentries2,
452 ca6e02ac 2020-01-07 stsp const char *path, struct got_pack *pack, struct got_packidx *packidx,
453 ca6e02ac 2020-01-07 stsp struct imsgbuf *ibuf, struct got_object_cache *objcache)
455 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
456 ca6e02ac 2020-01-07 stsp struct got_parsed_tree_entry *pte1 = NULL, *pte2 = NULL;
457 ca6e02ac 2020-01-07 stsp const char *seg, *s;
458 ca6e02ac 2020-01-07 stsp size_t seglen;
460 ca6e02ac 2020-01-07 stsp *changed = 0;
462 ca6e02ac 2020-01-07 stsp /* We not do support comparing the root path. */
463 61a7d79f 2020-02-29 stsp if (got_path_is_root_dir(path))
464 63f810e6 2020-02-29 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
467 61a7d79f 2020-02-29 stsp while (*s == '/')
470 ca6e02ac 2020-01-07 stsp seglen = 0;
471 ca6e02ac 2020-01-07 stsp while (*s) {
472 ca6e02ac 2020-01-07 stsp if (*s != '/') {
479 9985f404 2022-05-19 stsp pte1 = find_entry_by_name(*entries1, *nentries1, seg, seglen);
480 ca6e02ac 2020-01-07 stsp if (pte1 == NULL) {
481 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_NO_OBJ);
485 9985f404 2022-05-19 stsp pte2 = find_entry_by_name(*entries2, *nentries2, seg, seglen);
486 ca6e02ac 2020-01-07 stsp if (pte2 == NULL) {
487 ca6e02ac 2020-01-07 stsp *changed = 1;
491 ca6e02ac 2020-01-07 stsp if (pte1->mode != pte2->mode) {
492 ca6e02ac 2020-01-07 stsp *changed = 1;
496 ca6e02ac 2020-01-07 stsp if (memcmp(pte1->id, pte2->id, SHA1_DIGEST_LENGTH) == 0) {
497 ca6e02ac 2020-01-07 stsp *changed = 0;
501 ca6e02ac 2020-01-07 stsp if (*s == '\0') { /* final path element */
502 ca6e02ac 2020-01-07 stsp *changed = 1;
506 ca6e02ac 2020-01-07 stsp seg = s + 1;
508 ca6e02ac 2020-01-07 stsp seglen = 0;
510 ca6e02ac 2020-01-07 stsp struct got_object_id id1, id2;
513 ded8fbb8 2020-04-19 stsp memcpy(id1.sha1, pte1->id, SHA1_DIGEST_LENGTH);
514 00927983 2020-04-19 stsp idx = got_packidx_get_object_idx(packidx, &id1);
515 ca6e02ac 2020-01-07 stsp if (idx == -1) {
516 ded8fbb8 2020-04-19 stsp err = got_error_no_obj(&id1);
519 9985f404 2022-05-19 stsp free(*entries1);
520 ca6e02ac 2020-01-07 stsp *nentries1 = 0;
521 ca6e02ac 2020-01-07 stsp free(*buf1);
522 ca6e02ac 2020-01-07 stsp *buf1 = NULL;
523 ca6e02ac 2020-01-07 stsp err = open_tree(buf1, entries1, nentries1, pack,
524 ca6e02ac 2020-01-07 stsp packidx, idx, &id1, objcache);
525 ca6e02ac 2020-01-07 stsp pte1 = NULL;
529 ded8fbb8 2020-04-19 stsp memcpy(id2.sha1, pte2->id, SHA1_DIGEST_LENGTH);
530 00927983 2020-04-19 stsp idx = got_packidx_get_object_idx(packidx, &id2);
531 ca6e02ac 2020-01-07 stsp if (idx == -1) {
532 ded8fbb8 2020-04-19 stsp err = got_error_no_obj(&id2);
535 9985f404 2022-05-19 stsp free(*entries2);
536 ca6e02ac 2020-01-07 stsp *nentries2 = 0;
537 ca6e02ac 2020-01-07 stsp free(*buf2);
538 ca6e02ac 2020-01-07 stsp *buf2 = NULL;
539 ca6e02ac 2020-01-07 stsp err = open_tree(buf2, entries2, nentries2, pack,
540 ca6e02ac 2020-01-07 stsp packidx, idx, &id2, objcache);
541 ca6e02ac 2020-01-07 stsp pte2 = NULL;
547 ca6e02ac 2020-01-07 stsp return err;
550 ca6e02ac 2020-01-07 stsp static const struct got_error *
551 e70bf110 2020-03-22 stsp send_traversed_commits(struct got_object_id *commit_ids, size_t ncommits,
552 e70bf110 2020-03-22 stsp struct imsgbuf *ibuf)
554 e70bf110 2020-03-22 stsp struct ibuf *wbuf;
557 e70bf110 2020-03-22 stsp wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
558 e70bf110 2020-03-22 stsp sizeof(struct got_imsg_traversed_commits) +
559 e70bf110 2020-03-22 stsp ncommits * SHA1_DIGEST_LENGTH);
560 e70bf110 2020-03-22 stsp if (wbuf == NULL)
561 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
563 1453347d 2022-05-19 stsp if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1)
564 1453347d 2022-05-19 stsp return got_error_from_errno("imsg_add TRAVERSED_COMMITS");
566 e70bf110 2020-03-22 stsp for (i = 0; i < ncommits; i++) {
567 e70bf110 2020-03-22 stsp struct got_object_id *id = &commit_ids[i];
568 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
569 1453347d 2022-05-19 stsp return got_error_from_errno(
570 e70bf110 2020-03-22 stsp "imsg_add TRAVERSED_COMMITS");
574 e70bf110 2020-03-22 stsp wbuf->fd = -1;
575 e70bf110 2020-03-22 stsp imsg_close(ibuf, wbuf);
577 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
580 e70bf110 2020-03-22 stsp static const struct got_error *
581 e70bf110 2020-03-22 stsp send_commit_traversal_done(struct imsgbuf *ibuf)
583 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
584 e70bf110 2020-03-22 stsp NULL, 0) == -1)
585 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
587 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
591 e70bf110 2020-03-22 stsp static const struct got_error *
592 ca6e02ac 2020-01-07 stsp commit_traversal_request(struct imsg *imsg, struct imsgbuf *ibuf,
593 ca6e02ac 2020-01-07 stsp struct got_pack *pack, struct got_packidx *packidx,
594 ca6e02ac 2020-01-07 stsp struct got_object_cache *objcache)
596 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
597 ca6e02ac 2020-01-07 stsp struct got_imsg_packed_object iobj;
598 ca6e02ac 2020-01-07 stsp struct got_object_qid *pid;
599 ca6e02ac 2020-01-07 stsp struct got_commit_object *commit = NULL, *pcommit = NULL;
600 9985f404 2022-05-19 stsp struct got_parsed_tree_entry *entries = NULL, *pentries = NULL;
601 ca6e02ac 2020-01-07 stsp int nentries = 0, pnentries = 0;
602 ca6e02ac 2020-01-07 stsp struct got_object_id id;
603 ca6e02ac 2020-01-07 stsp size_t datalen, path_len;
604 ca6e02ac 2020-01-07 stsp char *path = NULL;
605 ca6e02ac 2020-01-07 stsp const int min_alloc = 64;
606 ca6e02ac 2020-01-07 stsp int changed = 0, ncommits = 0, nallocated = 0;
607 ca6e02ac 2020-01-07 stsp struct got_object_id *commit_ids = NULL;
609 ca6e02ac 2020-01-07 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
610 ca6e02ac 2020-01-07 stsp if (datalen < sizeof(iobj))
611 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
612 ca6e02ac 2020-01-07 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
613 ca6e02ac 2020-01-07 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
615 ca6e02ac 2020-01-07 stsp path_len = datalen - sizeof(iobj) - 1;
616 ca6e02ac 2020-01-07 stsp if (path_len < 0)
617 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
618 ca6e02ac 2020-01-07 stsp if (path_len > 0) {
619 ca6e02ac 2020-01-07 stsp path = imsg->data + sizeof(iobj);
620 ca6e02ac 2020-01-07 stsp if (path[path_len] != '\0')
621 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
624 ca6e02ac 2020-01-07 stsp nallocated = min_alloc;
625 ca6e02ac 2020-01-07 stsp commit_ids = reallocarray(NULL, nallocated, sizeof(*commit_ids));
626 ca6e02ac 2020-01-07 stsp if (commit_ids == NULL)
627 ca6e02ac 2020-01-07 stsp return got_error_from_errno("reallocarray");
630 ca6e02ac 2020-01-07 stsp const size_t max_datalen = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
633 ca6e02ac 2020-01-07 stsp if (sigint_received) {
634 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_CANCELLED);
638 ca6e02ac 2020-01-07 stsp if (commit == NULL) {
639 ca6e02ac 2020-01-07 stsp idx = got_packidx_get_object_idx(packidx, &id);
640 ca6e02ac 2020-01-07 stsp if (idx == -1)
642 ca6e02ac 2020-01-07 stsp err = open_commit(&commit, pack, packidx,
643 ca6e02ac 2020-01-07 stsp idx, &id, objcache);
645 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
647 ca6e02ac 2020-01-07 stsp err = NULL;
652 ca6e02ac 2020-01-07 stsp if (sizeof(struct got_imsg_traversed_commits) +
653 ca6e02ac 2020-01-07 stsp ncommits * SHA1_DIGEST_LENGTH >= max_datalen) {
654 e70bf110 2020-03-22 stsp err = send_traversed_commits(commit_ids, ncommits,
658 ca6e02ac 2020-01-07 stsp ncommits = 0;
660 ca6e02ac 2020-01-07 stsp ncommits++;
661 ca6e02ac 2020-01-07 stsp if (ncommits > nallocated) {
662 ca6e02ac 2020-01-07 stsp struct got_object_id *new;
663 ca6e02ac 2020-01-07 stsp nallocated += min_alloc;
664 ca6e02ac 2020-01-07 stsp new = reallocarray(commit_ids, nallocated,
665 ca6e02ac 2020-01-07 stsp sizeof(*commit_ids));
666 ca6e02ac 2020-01-07 stsp if (new == NULL) {
667 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("reallocarray");
670 ca6e02ac 2020-01-07 stsp commit_ids = new;
672 ca6e02ac 2020-01-07 stsp memcpy(commit_ids[ncommits - 1].sha1, id.sha1,
673 ca6e02ac 2020-01-07 stsp SHA1_DIGEST_LENGTH);
675 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(&commit->parent_ids);
676 ca6e02ac 2020-01-07 stsp if (pid == NULL)
679 d7b5a0e8 2022-04-20 stsp idx = got_packidx_get_object_idx(packidx, &pid->id);
680 ca6e02ac 2020-01-07 stsp if (idx == -1)
683 d7b5a0e8 2022-04-20 stsp err = open_commit(&pcommit, pack, packidx, idx, &pid->id,
686 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
688 ca6e02ac 2020-01-07 stsp err = NULL;
692 ca6e02ac 2020-01-07 stsp if (path[0] == '/' && path[1] == '\0') {
693 ca6e02ac 2020-01-07 stsp if (got_object_id_cmp(pcommit->tree_id,
694 ca6e02ac 2020-01-07 stsp commit->tree_id) != 0) {
695 ca6e02ac 2020-01-07 stsp changed = 1;
700 ca6e02ac 2020-01-07 stsp uint8_t *buf = NULL, *pbuf = NULL;
702 ca6e02ac 2020-01-07 stsp idx = got_packidx_get_object_idx(packidx,
703 ca6e02ac 2020-01-07 stsp commit->tree_id);
704 ca6e02ac 2020-01-07 stsp if (idx == -1)
706 ca6e02ac 2020-01-07 stsp pidx = got_packidx_get_object_idx(packidx,
707 ca6e02ac 2020-01-07 stsp pcommit->tree_id);
708 ca6e02ac 2020-01-07 stsp if (pidx == -1)
711 ca6e02ac 2020-01-07 stsp err = open_tree(&buf, &entries, &nentries, pack,
712 ca6e02ac 2020-01-07 stsp packidx, idx, commit->tree_id, objcache);
715 ca6e02ac 2020-01-07 stsp err = open_tree(&pbuf, &pentries, &pnentries, pack,
716 ca6e02ac 2020-01-07 stsp packidx, pidx, pcommit->tree_id, objcache);
722 ca6e02ac 2020-01-07 stsp err = tree_path_changed(&changed, &buf, &pbuf,
723 ca6e02ac 2020-01-07 stsp &entries, &nentries, &pentries, &pnentries, path,
724 ca6e02ac 2020-01-07 stsp pack, packidx, ibuf, objcache);
726 9985f404 2022-05-19 stsp free(entries);
727 9985f404 2022-05-19 stsp entries = NULL;
728 ca6e02ac 2020-01-07 stsp nentries = 0;
730 9985f404 2022-05-19 stsp free(pentries);
731 9985f404 2022-05-19 stsp pentries = NULL;
732 ca6e02ac 2020-01-07 stsp pnentries = 0;
733 ca6e02ac 2020-01-07 stsp free(pbuf);
735 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
737 ca6e02ac 2020-01-07 stsp err = NULL;
742 ca6e02ac 2020-01-07 stsp if (!changed) {
743 d7b5a0e8 2022-04-20 stsp memcpy(id.sha1, pid->id.sha1, SHA1_DIGEST_LENGTH);
744 ca6e02ac 2020-01-07 stsp got_object_commit_close(commit);
745 ca6e02ac 2020-01-07 stsp commit = pcommit;
746 ca6e02ac 2020-01-07 stsp pcommit = NULL;
748 ca6e02ac 2020-01-07 stsp } while (!changed);
750 ca6e02ac 2020-01-07 stsp if (ncommits > 0) {
751 e70bf110 2020-03-22 stsp err = send_traversed_commits(commit_ids, ncommits, ibuf);
755 ca6e02ac 2020-01-07 stsp if (changed) {
756 ca6e02ac 2020-01-07 stsp err = got_privsep_send_commit(ibuf, commit);
761 e70bf110 2020-03-22 stsp err = send_commit_traversal_done(ibuf);
763 ca6e02ac 2020-01-07 stsp free(commit_ids);
764 ca6e02ac 2020-01-07 stsp if (commit)
765 ca6e02ac 2020-01-07 stsp got_object_commit_close(commit);
766 ca6e02ac 2020-01-07 stsp if (pcommit)
767 ca6e02ac 2020-01-07 stsp got_object_commit_close(pcommit);
768 9985f404 2022-05-19 stsp free(entries);
769 9985f404 2022-05-19 stsp free(pentries);
771 f4a881ce 2018-11-17 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
772 f4a881ce 2018-11-17 stsp err = NULL;
774 f4a881ce 2018-11-17 stsp got_privsep_send_error(ibuf, err);
777 f4a881ce 2018-11-17 stsp return err;
780 f4a881ce 2018-11-17 stsp static const struct got_error *
781 c0df5966 2021-12-31 stsp raw_object_request(struct imsg *imsg, struct imsgbuf *ibuf,
782 c0df5966 2021-12-31 stsp struct got_pack *pack, struct got_packidx *packidx,
783 db696021 2022-01-04 stsp struct got_object_cache *objcache, FILE *basefile, FILE *accumfile)
785 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
786 59d1e4a0 2021-03-10 stsp uint8_t *buf = NULL;
787 59d1e4a0 2021-03-10 stsp uint64_t size = 0;
788 db696021 2022-01-04 stsp FILE *outfile = NULL;
789 59d1e4a0 2021-03-10 stsp struct got_imsg_packed_object iobj;
790 59d1e4a0 2021-03-10 stsp struct got_object *obj;
791 59d1e4a0 2021-03-10 stsp struct got_object_id id;
792 59d1e4a0 2021-03-10 stsp size_t datalen;
794 59d1e4a0 2021-03-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
795 59d1e4a0 2021-03-10 stsp if (datalen != sizeof(iobj))
796 59d1e4a0 2021-03-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
797 59d1e4a0 2021-03-10 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
798 59d1e4a0 2021-03-10 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
800 59d1e4a0 2021-03-10 stsp obj = got_object_cache_get(objcache, &id);
802 59d1e4a0 2021-03-10 stsp obj->refcnt++;
804 59d1e4a0 2021-03-10 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
807 59d1e4a0 2021-03-10 stsp return err;
810 59d1e4a0 2021-03-10 stsp err = receive_file(&outfile, ibuf, GOT_IMSG_RAW_OBJECT_OUTFD);
812 59d1e4a0 2021-03-10 stsp return err;
814 59d1e4a0 2021-03-10 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
815 59d1e4a0 2021-03-10 stsp err = got_pack_get_max_delta_object_size(&size, obj, pack);
819 59d1e4a0 2021-03-10 stsp size = obj->size;
821 59d1e4a0 2021-03-10 stsp if (size <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
822 59d1e4a0 2021-03-10 stsp err = got_packfile_extract_object_to_mem(&buf, &obj->size,
823 59d1e4a0 2021-03-10 stsp obj, pack);
825 59d1e4a0 2021-03-10 stsp err = got_packfile_extract_object(pack, obj, outfile, basefile,
826 59d1e4a0 2021-03-10 stsp accumfile);
830 40e3cb72 2021-06-22 stsp err = got_privsep_send_raw_obj(ibuf, obj->size, obj->hdrlen, buf);
833 59d1e4a0 2021-03-10 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
834 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fclose");
835 59d1e4a0 2021-03-10 stsp got_object_close(obj);
836 59d1e4a0 2021-03-10 stsp if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
837 59d1e4a0 2021-03-10 stsp got_privsep_send_error(ibuf, err);
839 59d1e4a0 2021-03-10 stsp return err;
842 67fd6849 2022-02-13 stsp static const struct got_error *
843 67fd6849 2022-02-13 stsp get_base_object_id(struct got_object_id *base_id, struct got_packidx *packidx,
844 67fd6849 2022-02-13 stsp off_t base_offset)
846 67fd6849 2022-02-13 stsp const struct got_error *err;
849 67fd6849 2022-02-13 stsp err = got_packidx_get_offset_idx(&idx, packidx, base_offset);
851 67fd6849 2022-02-13 stsp return err;
852 67fd6849 2022-02-13 stsp if (idx == -1)
853 67fd6849 2022-02-13 stsp return got_error(GOT_ERR_BAD_PACKIDX);
855 67fd6849 2022-02-13 stsp return got_packidx_get_object_id(base_id, packidx, idx);
858 59d1e4a0 2021-03-10 stsp static const struct got_error *
859 67fd6849 2022-02-13 stsp raw_delta_request(struct imsg *imsg, struct imsgbuf *ibuf,
860 67fd6849 2022-02-13 stsp FILE *delta_outfile, struct got_pack *pack,
861 67fd6849 2022-02-13 stsp struct got_packidx *packidx)
863 67fd6849 2022-02-13 stsp const struct got_error *err = NULL;
864 67fd6849 2022-02-13 stsp struct got_imsg_raw_delta_request req;
865 2d9e6abf 2022-05-04 stsp size_t datalen, delta_size, delta_compressed_size;
866 67fd6849 2022-02-13 stsp off_t delta_offset;
867 67fd6849 2022-02-13 stsp uint8_t *delta_buf = NULL;
868 67fd6849 2022-02-13 stsp struct got_object_id id, base_id;
869 67fd6849 2022-02-13 stsp off_t base_offset, delta_out_offset = 0;
870 67fd6849 2022-02-13 stsp uint64_t base_size = 0, result_size = 0;
873 67fd6849 2022-02-13 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
874 67fd6849 2022-02-13 stsp if (datalen != sizeof(req))
875 67fd6849 2022-02-13 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
876 67fd6849 2022-02-13 stsp memcpy(&req, imsg->data, sizeof(req));
877 67fd6849 2022-02-13 stsp memcpy(id.sha1, req.id, SHA1_DIGEST_LENGTH);
879 67fd6849 2022-02-13 stsp imsg->fd = -1;
881 67fd6849 2022-02-13 stsp err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
882 2d9e6abf 2022-05-04 stsp &delta_compressed_size, &delta_offset, &base_offset, &base_id,
883 2d9e6abf 2022-05-04 stsp &base_size, &result_size, pack, packidx, req.idx);
888 67fd6849 2022-02-13 stsp * If this is an offset delta we must determine the base
889 67fd6849 2022-02-13 stsp * object ID ourselves.
891 67fd6849 2022-02-13 stsp if (base_offset != 0) {
892 67fd6849 2022-02-13 stsp err = get_base_object_id(&base_id, packidx, base_offset);
897 67fd6849 2022-02-13 stsp delta_out_offset = ftello(delta_outfile);
898 2d9e6abf 2022-05-04 stsp w = fwrite(delta_buf, 1, delta_compressed_size, delta_outfile);
899 2d9e6abf 2022-05-04 stsp if (w != delta_compressed_size) {
900 67fd6849 2022-02-13 stsp err = got_ferror(delta_outfile, GOT_ERR_IO);
903 67fd6849 2022-02-13 stsp if (fflush(delta_outfile) == -1) {
904 67fd6849 2022-02-13 stsp err = got_error_from_errno("fflush");
908 67fd6849 2022-02-13 stsp err = got_privsep_send_raw_delta(ibuf, base_size, result_size,
909 2d9e6abf 2022-05-04 stsp delta_size, delta_compressed_size, delta_offset, delta_out_offset,
912 67fd6849 2022-02-13 stsp free(delta_buf);
913 67fd6849 2022-02-13 stsp return err;
916 fae7e038 2022-05-07 stsp struct search_deltas_arg {
917 fae7e038 2022-05-07 stsp struct imsgbuf *ibuf;
918 fae7e038 2022-05-07 stsp struct got_packidx *packidx;
919 fae7e038 2022-05-07 stsp struct got_pack *pack;
920 fae7e038 2022-05-07 stsp struct got_object_idset *idset;
921 fae7e038 2022-05-07 stsp FILE *delta_outfile;
922 fae7e038 2022-05-07 stsp struct got_imsg_reused_delta deltas[GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS];
923 fae7e038 2022-05-07 stsp size_t ndeltas;
926 67fd6849 2022-02-13 stsp static const struct got_error *
927 fae7e038 2022-05-07 stsp search_delta_for_object(struct got_object_id *id, void *data, void *arg)
929 fae7e038 2022-05-07 stsp const struct got_error *err;
930 fae7e038 2022-05-07 stsp struct search_deltas_arg *a = arg;
931 fae7e038 2022-05-07 stsp int obj_idx;
932 fae7e038 2022-05-07 stsp uint8_t *delta_buf = NULL;
933 fae7e038 2022-05-07 stsp uint64_t base_size, result_size;
934 fae7e038 2022-05-07 stsp size_t delta_size, delta_compressed_size;
935 fae7e038 2022-05-07 stsp off_t delta_offset, base_offset;
936 fae7e038 2022-05-07 stsp struct got_object_id base_id;
938 fae7e038 2022-05-07 stsp if (sigint_received)
939 fae7e038 2022-05-07 stsp return got_error(GOT_ERR_CANCELLED);
941 fae7e038 2022-05-07 stsp obj_idx = got_packidx_get_object_idx(a->packidx, id);
942 fae7e038 2022-05-07 stsp if (obj_idx == -1)
943 fae7e038 2022-05-07 stsp return NULL; /* object not present in our pack file */
945 fae7e038 2022-05-07 stsp err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
946 fae7e038 2022-05-07 stsp &delta_compressed_size, &delta_offset, &base_offset, &base_id,
947 fae7e038 2022-05-07 stsp &base_size, &result_size, a->pack, a->packidx, obj_idx);
949 fae7e038 2022-05-07 stsp if (err->code == GOT_ERR_OBJ_TYPE)
950 fae7e038 2022-05-07 stsp return NULL; /* object not stored as a delta */
951 fae7e038 2022-05-07 stsp return err;
955 fae7e038 2022-05-07 stsp * If this is an offset delta we must determine the base
956 fae7e038 2022-05-07 stsp * object ID ourselves.
958 fae7e038 2022-05-07 stsp if (base_offset != 0) {
959 fae7e038 2022-05-07 stsp err = get_base_object_id(&base_id, a->packidx, base_offset);
964 fae7e038 2022-05-07 stsp if (got_object_idset_contains(a->idset, &base_id)) {
965 fae7e038 2022-05-07 stsp struct got_imsg_reused_delta *delta;
966 fae7e038 2022-05-07 stsp off_t delta_out_offset = ftello(a->delta_outfile);
969 fae7e038 2022-05-07 stsp w = fwrite(delta_buf, 1, delta_compressed_size,
970 fae7e038 2022-05-07 stsp a->delta_outfile);
971 fae7e038 2022-05-07 stsp if (w != delta_compressed_size) {
972 fae7e038 2022-05-07 stsp err = got_ferror(a->delta_outfile, GOT_ERR_IO);
976 fae7e038 2022-05-07 stsp delta = &a->deltas[a->ndeltas++];
977 fae7e038 2022-05-07 stsp memcpy(&delta->id, id, sizeof(delta->id));
978 fae7e038 2022-05-07 stsp memcpy(&delta->base_id, &base_id, sizeof(delta->base_id));
979 fae7e038 2022-05-07 stsp delta->base_size = base_size;
980 fae7e038 2022-05-07 stsp delta->result_size = result_size;
981 fae7e038 2022-05-07 stsp delta->delta_size = delta_size;
982 fae7e038 2022-05-07 stsp delta->delta_compressed_size = delta_compressed_size;
983 fae7e038 2022-05-07 stsp delta->delta_offset = delta_offset;
984 fae7e038 2022-05-07 stsp delta->delta_out_offset = delta_out_offset;
986 fae7e038 2022-05-07 stsp if (a->ndeltas >= GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS) {
987 fae7e038 2022-05-07 stsp err = got_privsep_send_reused_deltas(a->ibuf,
988 fae7e038 2022-05-07 stsp a->deltas, a->ndeltas);
991 fae7e038 2022-05-07 stsp a->ndeltas = 0;
995 fae7e038 2022-05-07 stsp free(delta_buf);
996 fae7e038 2022-05-07 stsp return err;
999 fae7e038 2022-05-07 stsp static const struct got_error *
1000 fae7e038 2022-05-07 stsp recv_object_ids(struct got_object_idset *idset, struct imsgbuf *ibuf)
1002 fae7e038 2022-05-07 stsp const struct got_error *err = NULL;
1003 fae7e038 2022-05-07 stsp int done = 0;
1004 fae7e038 2022-05-07 stsp struct got_object_id *ids;
1005 fae7e038 2022-05-07 stsp size_t nids, i;
1007 fae7e038 2022-05-07 stsp for (;;) {
1008 fae7e038 2022-05-07 stsp err = got_privsep_recv_object_idlist(&done, &ids, &nids, ibuf);
1009 fae7e038 2022-05-07 stsp if (err || done)
1011 fae7e038 2022-05-07 stsp for (i = 0; i < nids; i++) {
1012 fae7e038 2022-05-07 stsp err = got_object_idset_add(idset, &ids[i], NULL);
1013 fae7e038 2022-05-07 stsp if (err) {
1014 fae7e038 2022-05-07 stsp free(ids);
1015 fae7e038 2022-05-07 stsp return err;
1018 fae7e038 2022-05-07 stsp free(ids);
1021 fae7e038 2022-05-07 stsp return err;
1024 fae7e038 2022-05-07 stsp static const struct got_error *
1025 fae7e038 2022-05-07 stsp delta_reuse_request(struct imsg *imsg, struct imsgbuf *ibuf,
1026 fae7e038 2022-05-07 stsp FILE *delta_outfile, struct got_pack *pack, struct got_packidx *packidx)
1028 fae7e038 2022-05-07 stsp const struct got_error *err = NULL;
1029 fae7e038 2022-05-07 stsp struct got_object_idset *idset;
1030 fae7e038 2022-05-07 stsp struct search_deltas_arg sda;
1032 fae7e038 2022-05-07 stsp idset = got_object_idset_alloc();
1033 fae7e038 2022-05-07 stsp if (idset == NULL)
1034 fae7e038 2022-05-07 stsp return got_error_from_errno("got_object_idset_alloc");
1036 fae7e038 2022-05-07 stsp err = recv_object_ids(idset, ibuf);
1038 fae7e038 2022-05-07 stsp return err;
1040 fae7e038 2022-05-07 stsp memset(&sda, 0, sizeof(sda));
1041 fae7e038 2022-05-07 stsp sda.ibuf = ibuf;
1042 fae7e038 2022-05-07 stsp sda.idset = idset;
1043 fae7e038 2022-05-07 stsp sda.pack = pack;
1044 fae7e038 2022-05-07 stsp sda.packidx = packidx;
1045 fae7e038 2022-05-07 stsp sda.delta_outfile = delta_outfile;
1046 fae7e038 2022-05-07 stsp err = got_object_idset_for_each(idset, search_delta_for_object, &sda);
1048 fae7e038 2022-05-07 stsp goto done;
1050 fae7e038 2022-05-07 stsp if (sda.ndeltas > 0) {
1051 fae7e038 2022-05-07 stsp err = got_privsep_send_reused_deltas(ibuf, sda.deltas,
1052 fae7e038 2022-05-07 stsp sda.ndeltas);
1054 fae7e038 2022-05-07 stsp goto done;
1057 fae7e038 2022-05-07 stsp if (fflush(delta_outfile) == -1) {
1058 fae7e038 2022-05-07 stsp err = got_error_from_errno("fflush");
1059 fae7e038 2022-05-07 stsp goto done;
1062 fae7e038 2022-05-07 stsp err = got_privsep_send_reused_deltas_done(ibuf);
1064 fae7e038 2022-05-07 stsp got_object_idset_free(idset);
1065 fae7e038 2022-05-07 stsp return err;
1068 fae7e038 2022-05-07 stsp static const struct got_error *
1069 876c234b 2018-09-10 stsp receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
1071 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
1072 876c234b 2018-09-10 stsp struct imsg imsg;
1073 876c234b 2018-09-10 stsp struct got_imsg_packidx ipackidx;
1074 876c234b 2018-09-10 stsp size_t datalen;
1075 876c234b 2018-09-10 stsp struct got_packidx *p;
1077 876c234b 2018-09-10 stsp *packidx = NULL;
1079 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1081 876c234b 2018-09-10 stsp return err;
1083 876c234b 2018-09-10 stsp p = calloc(1, sizeof(*p));
1084 876c234b 2018-09-10 stsp if (p == NULL) {
1085 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1086 876c234b 2018-09-10 stsp goto done;
1089 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
1090 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1091 876c234b 2018-09-10 stsp goto done;
1094 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
1095 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1096 876c234b 2018-09-10 stsp goto done;
1099 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1100 876c234b 2018-09-10 stsp if (datalen != sizeof(ipackidx)) {
1101 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1102 876c234b 2018-09-10 stsp goto done;
1104 876c234b 2018-09-10 stsp memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
1106 876c234b 2018-09-10 stsp p->len = ipackidx.len;
1107 876c234b 2018-09-10 stsp p->fd = dup(imsg.fd);
1108 876c234b 2018-09-10 stsp if (p->fd == -1) {
1109 638f9024 2019-05-13 stsp err = got_error_from_errno("dup");
1110 56bef47a 2018-09-15 stsp goto done;
1112 56bef47a 2018-09-15 stsp if (lseek(p->fd, 0, SEEK_SET) == -1) {
1113 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1114 876c234b 2018-09-10 stsp goto done;
1117 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
1118 876c234b 2018-09-10 stsp p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
1119 876c234b 2018-09-10 stsp if (p->map == MAP_FAILED)
1120 876c234b 2018-09-10 stsp p->map = NULL; /* fall back to read(2) */
1122 c3564dfa 2021-07-15 stsp err = got_packidx_init_hdr(p, 1, ipackidx.packfile_size);
1124 876c234b 2018-09-10 stsp if (err) {
1125 876c234b 2018-09-10 stsp if (imsg.fd != -1)
1126 876c234b 2018-09-10 stsp close(imsg.fd);
1127 876c234b 2018-09-10 stsp got_packidx_close(p);
1129 876c234b 2018-09-10 stsp *packidx = p;
1130 876c234b 2018-09-10 stsp imsg_free(&imsg);
1131 876c234b 2018-09-10 stsp return err;
1134 876c234b 2018-09-10 stsp static const struct got_error *
1135 876c234b 2018-09-10 stsp receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
1137 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
1138 876c234b 2018-09-10 stsp struct imsg imsg;
1139 876c234b 2018-09-10 stsp struct got_imsg_pack ipack;
1140 876c234b 2018-09-10 stsp size_t datalen;
1141 876c234b 2018-09-10 stsp struct got_pack *pack;
1143 876c234b 2018-09-10 stsp *packp = NULL;
1145 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1147 876c234b 2018-09-10 stsp return err;
1149 876c234b 2018-09-10 stsp pack = calloc(1, sizeof(*pack));
1150 876c234b 2018-09-10 stsp if (pack == NULL) {
1151 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1152 876c234b 2018-09-10 stsp goto done;
1155 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACK) {
1156 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1157 876c234b 2018-09-10 stsp goto done;
1160 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
1161 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1162 876c234b 2018-09-10 stsp goto done;
1165 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1166 876c234b 2018-09-10 stsp if (datalen != sizeof(ipack)) {
1167 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1168 876c234b 2018-09-10 stsp goto done;
1170 876c234b 2018-09-10 stsp memcpy(&ipack, imsg.data, sizeof(ipack));
1172 876c234b 2018-09-10 stsp pack->filesize = ipack.filesize;
1173 876c234b 2018-09-10 stsp pack->fd = dup(imsg.fd);
1174 876c234b 2018-09-10 stsp if (pack->fd == -1) {
1175 638f9024 2019-05-13 stsp err = got_error_from_errno("dup");
1176 876c234b 2018-09-10 stsp goto done;
1178 56bef47a 2018-09-15 stsp if (lseek(pack->fd, 0, SEEK_SET) == -1) {
1179 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1180 56bef47a 2018-09-15 stsp goto done;
1182 876c234b 2018-09-10 stsp pack->path_packfile = strdup(ipack.path_packfile);
1183 876c234b 2018-09-10 stsp if (pack->path_packfile == NULL) {
1184 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1185 ab2f42e7 2019-11-10 stsp goto done;
1188 dac5c75e 2022-06-04 stsp err = got_delta_cache_alloc(&pack->delta_cache);
1190 876c234b 2018-09-10 stsp goto done;
1192 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
1193 876c234b 2018-09-10 stsp pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1194 876c234b 2018-09-10 stsp pack->fd, 0);
1195 876c234b 2018-09-10 stsp if (pack->map == MAP_FAILED)
1196 876c234b 2018-09-10 stsp pack->map = NULL; /* fall back to read(2) */
1199 876c234b 2018-09-10 stsp if (err) {
1200 876c234b 2018-09-10 stsp if (imsg.fd != -1)
1201 876c234b 2018-09-10 stsp close(imsg.fd);
1202 876c234b 2018-09-10 stsp free(pack);
1204 876c234b 2018-09-10 stsp *packp = pack;
1205 876c234b 2018-09-10 stsp imsg_free(&imsg);
1206 876c234b 2018-09-10 stsp return err;
1210 876c234b 2018-09-10 stsp main(int argc, char *argv[])
1212 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
1213 876c234b 2018-09-10 stsp struct imsgbuf ibuf;
1214 876c234b 2018-09-10 stsp struct imsg imsg;
1215 c59b3346 2018-09-11 stsp struct got_packidx *packidx = NULL;
1216 c59b3346 2018-09-11 stsp struct got_pack *pack = NULL;
1217 c59b3346 2018-09-11 stsp struct got_object_cache objcache;
1218 67fd6849 2022-02-13 stsp FILE *basefile = NULL, *accumfile = NULL, *delta_outfile = NULL;
1220 876c234b 2018-09-10 stsp //static int attached;
1221 876c234b 2018-09-10 stsp //while (!attached) sleep(1);
1223 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
1225 876c234b 2018-09-10 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
1227 c59b3346 2018-09-11 stsp err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
1228 c59b3346 2018-09-11 stsp if (err) {
1229 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_cache_init");
1230 c59b3346 2018-09-11 stsp got_privsep_send_error(&ibuf, err);
1234 2ff12563 2018-09-15 stsp #ifndef PROFILE
1235 876c234b 2018-09-10 stsp /* revoke access to most system calls */
1236 876c234b 2018-09-10 stsp if (pledge("stdio recvfd", NULL) == -1) {
1237 638f9024 2019-05-13 stsp err = got_error_from_errno("pledge");
1238 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
1243 876c234b 2018-09-10 stsp err = receive_packidx(&packidx, &ibuf);
1244 876c234b 2018-09-10 stsp if (err) {
1245 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
1249 876c234b 2018-09-10 stsp err = receive_pack(&pack, &ibuf);
1250 876c234b 2018-09-10 stsp if (err) {
1251 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
1256 876c234b 2018-09-10 stsp imsg.fd = -1;
1258 99437157 2018-11-11 stsp if (sigint_received) {
1259 99437157 2018-11-11 stsp err = got_error(GOT_ERR_CANCELLED);
1263 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1264 876c234b 2018-09-10 stsp if (err) {
1265 876c234b 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
1266 876c234b 2018-09-10 stsp err = NULL;
1270 876c234b 2018-09-10 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1273 876c234b 2018-09-10 stsp switch (imsg.hdr.type) {
1274 db696021 2022-01-04 stsp case GOT_IMSG_TMPFD:
1275 67fd6849 2022-02-13 stsp if (basefile == NULL) {
1276 67fd6849 2022-02-13 stsp err = receive_tempfile(&basefile, "w+",
1277 67fd6849 2022-02-13 stsp &imsg, &ibuf);
1278 67fd6849 2022-02-13 stsp } else if (accumfile == NULL) {
1279 67fd6849 2022-02-13 stsp err = receive_tempfile(&accumfile, "w+",
1280 67fd6849 2022-02-13 stsp &imsg, &ibuf);
1282 67fd6849 2022-02-13 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1284 876c234b 2018-09-10 stsp case GOT_IMSG_PACKED_OBJECT_REQUEST:
1285 c59b3346 2018-09-11 stsp err = object_request(&imsg, &ibuf, pack, packidx,
1286 c59b3346 2018-09-11 stsp &objcache);
1288 59d1e4a0 2021-03-10 stsp case GOT_IMSG_PACKED_RAW_OBJECT_REQUEST:
1289 db696021 2022-01-04 stsp if (basefile == NULL || accumfile == NULL) {
1290 db696021 2022-01-04 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1293 59d1e4a0 2021-03-10 stsp err = raw_object_request(&imsg, &ibuf, pack, packidx,
1294 db696021 2022-01-04 stsp &objcache, basefile, accumfile);
1296 67fd6849 2022-02-13 stsp case GOT_IMSG_RAW_DELTA_OUTFD:
1297 67fd6849 2022-02-13 stsp if (delta_outfile != NULL) {
1298 67fd6849 2022-02-13 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1301 67fd6849 2022-02-13 stsp err = receive_tempfile(&delta_outfile, "w",
1302 67fd6849 2022-02-13 stsp &imsg, &ibuf);
1304 67fd6849 2022-02-13 stsp case GOT_IMSG_RAW_DELTA_REQUEST:
1305 67fd6849 2022-02-13 stsp if (delta_outfile == NULL) {
1306 67fd6849 2022-02-13 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1309 67fd6849 2022-02-13 stsp err = raw_delta_request(&imsg, &ibuf, delta_outfile,
1310 67fd6849 2022-02-13 stsp pack, packidx);
1312 fae7e038 2022-05-07 stsp case GOT_IMSG_DELTA_REUSE_REQUEST:
1313 fae7e038 2022-05-07 stsp if (delta_outfile == NULL) {
1314 fae7e038 2022-05-07 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1317 fae7e038 2022-05-07 stsp err = delta_reuse_request(&imsg, &ibuf,
1318 fae7e038 2022-05-07 stsp delta_outfile, pack, packidx);
1320 876c234b 2018-09-10 stsp case GOT_IMSG_COMMIT_REQUEST:
1321 c59b3346 2018-09-11 stsp err = commit_request(&imsg, &ibuf, pack, packidx,
1322 7762fe12 2018-11-05 stsp &objcache);
1324 876c234b 2018-09-10 stsp case GOT_IMSG_TREE_REQUEST:
1325 c59b3346 2018-09-11 stsp err = tree_request(&imsg, &ibuf, pack, packidx,
1326 62d463ca 2020-10-20 naddy &objcache);
1328 876c234b 2018-09-10 stsp case GOT_IMSG_BLOB_REQUEST:
1329 db696021 2022-01-04 stsp if (basefile == NULL || accumfile == NULL) {
1330 db696021 2022-01-04 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1333 c59b3346 2018-09-11 stsp err = blob_request(&imsg, &ibuf, pack, packidx,
1334 db696021 2022-01-04 stsp &objcache, basefile, accumfile);
1336 f4a881ce 2018-11-17 stsp case GOT_IMSG_TAG_REQUEST:
1337 f4a881ce 2018-11-17 stsp err = tag_request(&imsg, &ibuf, pack, packidx,
1338 62d463ca 2020-10-20 naddy &objcache);
1340 ca6e02ac 2020-01-07 stsp case GOT_IMSG_COMMIT_TRAVERSAL_REQUEST:
1341 ca6e02ac 2020-01-07 stsp err = commit_traversal_request(&imsg, &ibuf, pack,
1342 ca6e02ac 2020-01-07 stsp packidx, &objcache);
1345 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1349 08578a35 2021-01-22 stsp if (imsg.fd != -1 && close(imsg.fd) == -1 && err == NULL)
1350 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1351 876c234b 2018-09-10 stsp imsg_free(&imsg);
1356 c59b3346 2018-09-11 stsp if (packidx)
1357 c59b3346 2018-09-11 stsp got_packidx_close(packidx);
1359 c59b3346 2018-09-11 stsp got_pack_close(pack);
1360 48d5fe42 2018-09-15 stsp got_object_cache_close(&objcache);
1361 876c234b 2018-09-10 stsp imsg_clear(&ibuf);
1362 db696021 2022-01-04 stsp if (basefile && fclose(basefile) == EOF && err == NULL)
1363 db696021 2022-01-04 stsp err = got_error_from_errno("fclose");
1364 db696021 2022-01-04 stsp if (accumfile && fclose(accumfile) == EOF && err == NULL)
1365 db696021 2022-01-04 stsp err = got_error_from_errno("fclose");
1366 67fd6849 2022-02-13 stsp if (delta_outfile && fclose(delta_outfile) == EOF && err == NULL)
1367 67fd6849 2022-02-13 stsp err = got_error_from_errno("fclose");
1368 99437157 2018-11-11 stsp if (err) {
1369 80d5f134 2018-11-11 stsp if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
1370 80d5f134 2018-11-11 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1371 99437157 2018-11-11 stsp got_privsep_send_error(&ibuf, err);
1374 08578a35 2021-01-22 stsp if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
1375 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1376 876c234b 2018-09-10 stsp return err ? 1 : 0;