Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16 #include "got_compat.h"
18 #include <sys/stat.h>
19 #include <sys/types.h>
20 #include <sys/queue.h>
21 #include <sys/uio.h>
22 #include <sys/time.h>
23 #include <sys/mman.h>
25 #include <inttypes.h>
26 #include <limits.h>
27 #include <signal.h>
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <zlib.h>
35 #include "got_error.h"
36 #include "got_object.h"
37 #include "got_path.h"
39 #include "got_lib_delta.h"
40 #include "got_lib_delta_cache.h"
41 #include "got_lib_object.h"
42 #include "got_lib_object_qid.h"
43 #include "got_lib_object_cache.h"
44 #include "got_lib_object_parse.h"
45 #include "got_lib_object_idset.h"
46 #include "got_lib_privsep.h"
47 #include "got_lib_pack.h"
49 #ifndef nitems
50 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
51 #endif
53 static volatile sig_atomic_t sigint_received;
55 static void
56 catch_sigint(int signo)
57 {
58 sigint_received = 1;
59 }
61 static const struct got_error *
62 open_object(struct got_object **obj, struct got_pack *pack,
63 struct got_packidx *packidx, int idx, struct got_object_id *id,
64 struct got_object_cache *objcache)
65 {
66 const struct got_error *err;
68 err = got_packfile_open_object(obj, pack, packidx, idx, id);
69 if (err)
70 return err;
71 (*obj)->refcnt++;
73 err = got_object_cache_add(objcache, id, *obj);
74 if (err) {
75 if (err->code == GOT_ERR_OBJ_EXISTS ||
76 err->code == GOT_ERR_OBJ_TOO_LARGE)
77 err = NULL;
78 return err;
79 }
80 (*obj)->refcnt++;
81 return NULL;
82 }
84 static const struct got_error *
85 object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
86 struct got_packidx *packidx, struct got_object_cache *objcache)
87 {
88 const struct got_error *err = NULL;
89 struct got_imsg_packed_object iobj;
90 struct got_object *obj;
91 struct got_object_id id;
92 size_t datalen;
94 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
95 if (datalen != sizeof(iobj))
96 return got_error(GOT_ERR_PRIVSEP_LEN);
97 memcpy(&iobj, imsg->data, sizeof(iobj));
98 memcpy(&id, &iobj.id, sizeof(id));
100 obj = got_object_cache_get(objcache, &id);
101 if (obj) {
102 obj->refcnt++;
103 } else {
104 err = open_object(&obj, pack, packidx, iobj.idx, &id,
105 objcache);
106 if (err)
107 goto done;
110 err = got_privsep_send_obj(ibuf, obj);
111 done:
112 got_object_close(obj);
113 return err;
116 static const struct got_error *
117 open_commit(struct got_commit_object **commit, struct got_pack *pack,
118 struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
119 struct got_object_cache *objcache)
121 const struct got_error *err = NULL;
122 struct got_object *obj = NULL;
123 uint8_t *buf = NULL;
124 size_t len;
126 *commit = NULL;
128 obj = got_object_cache_get(objcache, id);
129 if (obj) {
130 obj->refcnt++;
131 } else {
132 err = open_object(&obj, pack, packidx, obj_idx, id,
133 objcache);
134 if (err)
135 return err;
138 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
139 if (err)
140 goto done;
142 obj->size = len;
144 err = got_object_parse_commit(commit, buf, len);
145 done:
146 got_object_close(obj);
147 free(buf);
148 return err;
151 static const struct got_error *
152 commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
153 struct got_packidx *packidx, struct got_object_cache *objcache)
155 const struct got_error *err = NULL;
156 struct got_imsg_packed_object iobj;
157 struct got_commit_object *commit = NULL;
158 struct got_object_id id;
159 size_t datalen;
161 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
162 if (datalen != sizeof(iobj))
163 return got_error(GOT_ERR_PRIVSEP_LEN);
164 memcpy(&iobj, imsg->data, sizeof(iobj));
165 memcpy(&id, &iobj.id, sizeof(id));
167 err = open_commit(&commit, pack, packidx, iobj.idx, &id, objcache);
168 if (err)
169 goto done;
171 err = got_privsep_send_commit(ibuf, commit);
172 done:
173 if (commit)
174 got_object_commit_close(commit);
175 if (err) {
176 if (err->code == GOT_ERR_PRIVSEP_PIPE)
177 err = NULL;
178 else
179 got_privsep_send_error(ibuf, err);
182 return err;
185 static const struct got_error *
186 open_tree(uint8_t **buf, size_t *len,
187 struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
188 struct got_object_id *id, struct got_object_cache *objcache)
190 const struct got_error *err = NULL;
191 struct got_object *obj = NULL;
192 int cached = 0;
194 *buf = NULL;
195 *len = 0;
197 obj = got_object_cache_get(objcache, id);
198 if (obj) {
199 obj->refcnt++;
200 cached = 1;
201 } else {
202 err = open_object(&obj, pack, packidx, obj_idx, id,
203 objcache);
204 if (err)
205 return err;
208 err = got_packfile_extract_object_to_mem(buf, len, obj, pack);
209 if (err)
210 goto done;
212 if (!cached)
213 obj->size = *len;
214 done:
215 got_object_close(obj);
216 if (err) {
217 free(*buf);
218 *buf = NULL;
220 return err;
223 static const struct got_error *
224 tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
225 struct got_packidx *packidx, struct got_object_cache *objcache,
226 struct got_parsed_tree_entry **entries, size_t *nentries,
227 size_t *nentries_alloc)
229 const struct got_error *err = NULL;
230 struct got_imsg_packed_object iobj;
231 uint8_t *buf = NULL;
232 size_t len = 0;
233 struct got_object_id id;
234 size_t datalen;
236 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
237 if (datalen != sizeof(iobj))
238 return got_error(GOT_ERR_PRIVSEP_LEN);
239 memcpy(&iobj, imsg->data, sizeof(iobj));
240 memcpy(&id, &iobj.id, sizeof(id));
242 err = open_tree(&buf, &len, pack, packidx, iobj.idx, &id, objcache);
243 if (err)
244 return err;
246 err = got_object_parse_tree(entries, nentries, nentries_alloc,
247 buf, len);
248 if (err)
249 goto done;
251 err = got_privsep_send_tree(ibuf, *entries, *nentries);
252 if (err) {
253 if (err->code == GOT_ERR_PRIVSEP_PIPE)
254 err = NULL;
255 else
256 got_privsep_send_error(ibuf, err);
258 done:
259 free(buf);
260 return err;
263 static const struct got_error *
264 receive_file(FILE **f, struct imsgbuf *ibuf, uint32_t imsg_code)
266 const struct got_error *err;
267 struct imsg imsg;
268 size_t datalen;
270 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
271 if (err)
272 return err;
274 if (imsg.hdr.type != imsg_code) {
275 err = got_error(GOT_ERR_PRIVSEP_MSG);
276 goto done;
279 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
280 if (datalen != 0) {
281 err = got_error(GOT_ERR_PRIVSEP_LEN);
282 goto done;
284 if (imsg.fd == -1) {
285 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
286 goto done;
289 *f = fdopen(imsg.fd, "w+");
290 if (*f == NULL) {
291 err = got_error_from_errno("fdopen");
292 close(imsg.fd);
293 goto done;
295 done:
296 imsg_free(&imsg);
297 return err;
300 static const struct got_error *
301 receive_tempfile(FILE **f, const char *mode, struct imsg *imsg,
302 struct imsgbuf *ibuf)
304 size_t datalen;
306 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
307 if (datalen != 0)
308 return got_error(GOT_ERR_PRIVSEP_LEN);
310 if (imsg->fd == -1)
311 return got_error(GOT_ERR_PRIVSEP_NO_FD);
313 *f = fdopen(imsg->fd, mode);
314 if (*f == NULL)
315 return got_error_from_errno("fdopen");
316 imsg->fd = -1;
318 return NULL;
321 static const struct got_error *
322 blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
323 struct got_packidx *packidx, struct got_object_cache *objcache,
324 FILE *basefile, FILE *accumfile)
326 const struct got_error *err = NULL;
327 struct got_imsg_packed_object iobj;
328 struct got_object *obj = NULL;
329 FILE *outfile = NULL;
330 struct got_object_id id;
331 size_t datalen;
332 uint64_t blob_size;
333 uint8_t *buf = NULL;
335 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
336 if (datalen != sizeof(iobj))
337 return got_error(GOT_ERR_PRIVSEP_LEN);
338 memcpy(&iobj, imsg->data, sizeof(iobj));
339 memcpy(&id, &iobj.id, sizeof(id));
341 obj = got_object_cache_get(objcache, &id);
342 if (obj) {
343 obj->refcnt++;
344 } else {
345 err = open_object(&obj, pack, packidx, iobj.idx, &id,
346 objcache);
347 if (err)
348 return err;
351 err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
352 if (err)
353 goto done;
355 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
356 err = got_pack_get_max_delta_object_size(&blob_size, obj, pack);
357 if (err)
358 goto done;
359 } else
360 blob_size = obj->size;
362 if (blob_size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
363 err = got_packfile_extract_object_to_mem(&buf, &obj->size,
364 obj, pack);
365 else
366 err = got_packfile_extract_object(pack, obj, outfile, basefile,
367 accumfile);
368 if (err)
369 goto done;
371 err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, buf);
372 done:
373 free(buf);
374 if (outfile && fclose(outfile) == EOF && err == NULL)
375 err = got_error_from_errno("fclose");
376 got_object_close(obj);
377 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
378 got_privsep_send_error(ibuf, err);
380 return err;
383 static const struct got_error *
384 tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
385 struct got_packidx *packidx, struct got_object_cache *objcache)
387 const struct got_error *err = NULL;
388 struct got_imsg_packed_object iobj;
389 struct got_object *obj = NULL;
390 struct got_tag_object *tag = NULL;
391 uint8_t *buf = NULL;
392 size_t len;
393 struct got_object_id id;
394 size_t datalen;
396 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
397 if (datalen != sizeof(iobj))
398 return got_error(GOT_ERR_PRIVSEP_LEN);
399 memcpy(&iobj, imsg->data, sizeof(iobj));
400 memcpy(&id, &iobj.id, sizeof(id));
402 obj = got_object_cache_get(objcache, &id);
403 if (obj) {
404 obj->refcnt++;
405 } else {
406 err = open_object(&obj, pack, packidx, iobj.idx, &id,
407 objcache);
408 if (err)
409 return err;
412 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
413 if (err)
414 goto done;
416 obj->size = len;
417 err = got_object_parse_tag(&tag, buf, len);
418 if (err)
419 goto done;
421 err = got_privsep_send_tag(ibuf, tag);
422 done:
423 free(buf);
424 got_object_close(obj);
425 if (tag)
426 got_object_tag_close(tag);
427 if (err) {
428 if (err->code == GOT_ERR_PRIVSEP_PIPE)
429 err = NULL;
430 else
431 got_privsep_send_error(ibuf, err);
434 return err;
437 static const struct got_error *
438 tree_path_changed(int *changed, uint8_t **buf1, size_t *len1,
439 uint8_t **buf2, size_t *len2, const char *path,
440 struct got_pack *pack, struct got_packidx *packidx,
441 struct imsgbuf *ibuf, struct got_object_cache *objcache)
443 const struct got_error *err = NULL;
444 struct got_parsed_tree_entry pte1, pte2;
445 const char *seg, *s;
446 size_t seglen;
447 size_t remain1 = *len1, remain2 = *len2, elen;
448 uint8_t *next_entry1 = *buf1;
449 uint8_t *next_entry2 = *buf2;
451 memset(&pte1, 0, sizeof(pte1));
452 memset(&pte2, 0, sizeof(pte2));
454 *changed = 0;
456 /* We not do support comparing the root path. */
457 if (got_path_is_root_dir(path))
458 return got_error_path(path, GOT_ERR_BAD_PATH);
460 s = path;
461 while (*s == '/')
462 s++;
463 seg = s;
464 seglen = 0;
465 while (*s) {
466 if (*s != '/') {
467 s++;
468 seglen++;
469 if (*s)
470 continue;
473 /*
474 * As an optimization we compare entries in on-disk order
475 * rather than in got_path_cmp() order. We only need to
476 * find out if any entries differ. Parsing all entries and
477 * sorting them slows us down significantly when tree objects
478 * have thousands of entries. We can assume that on-disk entry
479 * ordering is stable, as per got_object_tree_create() and
480 * sort_tree_entries_the_way_git_likes_it(). Other orderings
481 * are incompatible with Git and would yield false positives
482 * here, too.
483 */
484 while (remain1 > 0) {
485 err = got_object_parse_tree_entry(&pte1, &elen,
486 next_entry1, remain1);
487 if (err)
488 return err;
489 next_entry1 += elen;
490 remain1 -= elen;
491 if (strncmp(pte1.name, seg, seglen) != 0 ||
492 pte1.name[seglen] != '\0') {
493 memset(&pte1, 0, sizeof(pte1));
494 continue;
495 } else
496 break;
498 if (pte1.name == NULL) {
499 err = got_error(GOT_ERR_NO_OBJ);
500 break;
503 if (remain2 == 0) {
504 *changed = 1;
505 break;
508 while (remain2 > 0) {
509 err = got_object_parse_tree_entry(&pte2, &elen,
510 next_entry2, remain2);
511 if (err)
512 return err;
513 next_entry2 += elen;
514 remain2 -= elen;
515 if (strncmp(pte2.name, seg, seglen) != 0 ||
516 pte2.name[seglen] != '\0') {
517 memset(&pte2, 0, sizeof(pte2));
518 continue;
519 } else
520 break;
523 if (pte2.name == NULL) {
524 *changed = 1;
525 break;
528 if (pte1.mode != pte2.mode) {
529 *changed = 1;
530 break;
533 if (memcmp(pte1.id, pte2.id, SHA1_DIGEST_LENGTH) == 0) {
534 *changed = 0;
535 break;
538 if (*s == '\0') { /* final path element */
539 *changed = 1;
540 break;
543 seg = s + 1;
544 s++;
545 seglen = 0;
546 if (*s) {
547 struct got_object_id id1, id2;
548 int idx;
550 memcpy(id1.sha1, pte1.id, SHA1_DIGEST_LENGTH);
551 idx = got_packidx_get_object_idx(packidx, &id1);
552 if (idx == -1) {
553 err = got_error_no_obj(&id1);
554 break;
556 free(*buf1);
557 *buf1 = NULL;
558 err = open_tree(buf1, len1, pack, packidx, idx, &id1,
559 objcache);
560 memset(&pte1, 0, sizeof(pte1));
561 if (err)
562 break;
563 next_entry1 = *buf1;
564 remain1 = *len1;
566 memcpy(id2.sha1, pte2.id, SHA1_DIGEST_LENGTH);
567 idx = got_packidx_get_object_idx(packidx, &id2);
568 if (idx == -1) {
569 err = got_error_no_obj(&id2);
570 break;
572 free(*buf2);
573 *buf2 = NULL;
574 err = open_tree(buf2, len2, pack, packidx, idx, &id2,
575 objcache);
576 memset(&pte2, 0, sizeof(pte2));
577 if (err)
578 break;
579 next_entry2 = *buf2;
580 remain2 = *len2;
584 return err;
587 static const struct got_error *
588 send_traversed_commits(struct got_object_id *commit_ids, size_t ncommits,
589 struct imsgbuf *ibuf)
591 struct ibuf *wbuf;
592 size_t i;
594 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
595 sizeof(struct got_imsg_traversed_commits) +
596 ncommits * sizeof(commit_ids[0]));
597 if (wbuf == NULL)
598 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
600 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1)
601 return got_error_from_errno("imsg_add TRAVERSED_COMMITS");
603 for (i = 0; i < ncommits; i++) {
604 struct got_object_id *id = &commit_ids[i];
605 if (imsg_add(wbuf, id, sizeof(*id)) == -1) {
606 return got_error_from_errno(
607 "imsg_add TRAVERSED_COMMITS");
611 wbuf->fd = -1;
612 imsg_close(ibuf, wbuf);
614 return got_privsep_flush_imsg(ibuf);
617 static const struct got_error *
618 send_commit_traversal_done(struct imsgbuf *ibuf)
620 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
621 NULL, 0) == -1)
622 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
624 return got_privsep_flush_imsg(ibuf);
627 static const struct got_error *
628 commit_traversal_request(struct imsg *imsg, struct imsgbuf *ibuf,
629 struct got_pack *pack, struct got_packidx *packidx,
630 struct got_object_cache *objcache)
632 const struct got_error *err = NULL;
633 struct got_imsg_commit_traversal_request ctreq;
634 struct got_object_qid *pid;
635 struct got_commit_object *commit = NULL, *pcommit = NULL;
636 struct got_object_id id;
637 size_t datalen;
638 char *path = NULL;
639 const int min_alloc = 64;
640 int changed = 0, ncommits = 0, nallocated = 0;
641 struct got_object_id *commit_ids = NULL;
643 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
644 if (datalen < sizeof(ctreq))
645 return got_error(GOT_ERR_PRIVSEP_LEN);
646 memcpy(&ctreq, imsg->data, sizeof(ctreq));
647 memcpy(&id, &ctreq.iobj.id, sizeof(id));
649 if (datalen != sizeof(ctreq) + ctreq.path_len)
650 return got_error(GOT_ERR_PRIVSEP_LEN);
651 if (ctreq.path_len == 0)
652 return got_error(GOT_ERR_PRIVSEP_LEN);
654 path = strndup(imsg->data + sizeof(ctreq), ctreq.path_len);
655 if (path == NULL)
656 return got_error_from_errno("strndup");
658 nallocated = min_alloc;
659 commit_ids = reallocarray(NULL, nallocated, sizeof(*commit_ids));
660 if (commit_ids == NULL)
661 return got_error_from_errno("reallocarray");
663 do {
664 const size_t max_datalen = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
665 int idx;
667 if (sigint_received) {
668 err = got_error(GOT_ERR_CANCELLED);
669 goto done;
672 if (commit == NULL) {
673 idx = got_packidx_get_object_idx(packidx, &id);
674 if (idx == -1)
675 break;
676 err = open_commit(&commit, pack, packidx,
677 idx, &id, objcache);
678 if (err) {
679 if (err->code != GOT_ERR_NO_OBJ)
680 goto done;
681 err = NULL;
682 break;
686 if (sizeof(struct got_imsg_traversed_commits) +
687 ncommits * sizeof(commit_ids[0]) >= max_datalen) {
688 err = send_traversed_commits(commit_ids, ncommits,
689 ibuf);
690 if (err)
691 goto done;
692 ncommits = 0;
694 ncommits++;
695 if (ncommits > nallocated) {
696 struct got_object_id *new;
697 nallocated += min_alloc;
698 new = reallocarray(commit_ids, nallocated,
699 sizeof(*commit_ids));
700 if (new == NULL) {
701 err = got_error_from_errno("reallocarray");
702 goto done;
704 commit_ids = new;
706 memcpy(&commit_ids[ncommits - 1], &id, sizeof(id));
708 pid = STAILQ_FIRST(&commit->parent_ids);
709 if (pid == NULL)
710 break;
712 idx = got_packidx_get_object_idx(packidx, &pid->id);
713 if (idx == -1)
714 break;
716 err = open_commit(&pcommit, pack, packidx, idx, &pid->id,
717 objcache);
718 if (err) {
719 if (err->code != GOT_ERR_NO_OBJ)
720 goto done;
721 err = NULL;
722 break;
725 if (path[0] == '/' && path[1] == '\0') {
726 if (got_object_id_cmp(pcommit->tree_id,
727 commit->tree_id) != 0) {
728 changed = 1;
729 break;
731 } else {
732 int pidx;
733 uint8_t *buf = NULL, *pbuf = NULL;
734 size_t len = 0, plen = 0;
736 idx = got_packidx_get_object_idx(packidx,
737 commit->tree_id);
738 if (idx == -1)
739 break;
740 pidx = got_packidx_get_object_idx(packidx,
741 pcommit->tree_id);
742 if (pidx == -1)
743 break;
745 err = open_tree(&buf, &len, pack, packidx, idx,
746 commit->tree_id, objcache);
747 if (err)
748 goto done;
750 err = open_tree(&pbuf, &plen, pack, packidx, pidx,
751 pcommit->tree_id, objcache);
752 if (err) {
753 free(buf);
754 goto done;
757 err = tree_path_changed(&changed, &buf, &len,
758 &pbuf, &plen, path, pack, packidx, ibuf,
759 objcache);
761 free(buf);
762 free(pbuf);
763 if (err) {
764 if (err->code != GOT_ERR_NO_OBJ)
765 goto done;
766 err = NULL;
767 break;
771 if (!changed) {
772 memcpy(&id, &pid->id, sizeof(id));
773 got_object_commit_close(commit);
774 commit = pcommit;
775 pcommit = NULL;
777 } while (!changed);
779 if (ncommits > 0) {
780 err = send_traversed_commits(commit_ids, ncommits, ibuf);
781 if (err)
782 goto done;
784 if (changed) {
785 err = got_privsep_send_commit(ibuf, commit);
786 if (err)
787 goto done;
790 err = send_commit_traversal_done(ibuf);
791 done:
792 free(path);
793 free(commit_ids);
794 if (commit)
795 got_object_commit_close(commit);
796 if (pcommit)
797 got_object_commit_close(pcommit);
798 if (err) {
799 if (err->code == GOT_ERR_PRIVSEP_PIPE)
800 err = NULL;
801 else
802 got_privsep_send_error(ibuf, err);
805 return err;
808 static const struct got_error *
809 raw_object_request(struct imsg *imsg, struct imsgbuf *ibuf,
810 struct got_pack *pack, struct got_packidx *packidx,
811 struct got_object_cache *objcache, FILE *basefile, FILE *accumfile)
813 const struct got_error *err = NULL;
814 uint8_t *buf = NULL;
815 uint64_t size = 0;
816 FILE *outfile = NULL;
817 struct got_imsg_packed_object iobj;
818 struct got_object *obj;
819 struct got_object_id id;
820 size_t datalen;
822 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
823 if (datalen != sizeof(iobj))
824 return got_error(GOT_ERR_PRIVSEP_LEN);
825 memcpy(&iobj, imsg->data, sizeof(iobj));
826 memcpy(&id, &iobj.id, sizeof(id));
828 obj = got_object_cache_get(objcache, &id);
829 if (obj) {
830 obj->refcnt++;
831 } else {
832 err = open_object(&obj, pack, packidx, iobj.idx, &id,
833 objcache);
834 if (err)
835 return err;
838 err = receive_file(&outfile, ibuf, GOT_IMSG_RAW_OBJECT_OUTFD);
839 if (err)
840 return err;
842 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
843 err = got_pack_get_max_delta_object_size(&size, obj, pack);
844 if (err)
845 goto done;
846 } else
847 size = obj->size;
849 if (size <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
850 err = got_packfile_extract_object_to_mem(&buf, &obj->size,
851 obj, pack);
852 else
853 err = got_packfile_extract_object(pack, obj, outfile, basefile,
854 accumfile);
855 if (err)
856 goto done;
858 err = got_privsep_send_raw_obj(ibuf, obj->size, obj->hdrlen, buf);
859 done:
860 free(buf);
861 if (outfile && fclose(outfile) == EOF && err == NULL)
862 err = got_error_from_errno("fclose");
863 got_object_close(obj);
864 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
865 got_privsep_send_error(ibuf, err);
867 return err;
870 static const struct got_error *
871 get_base_object_id(struct got_object_id *base_id, struct got_packidx *packidx,
872 off_t base_offset)
874 const struct got_error *err;
875 int idx;
877 err = got_packidx_get_offset_idx(&idx, packidx, base_offset);
878 if (err)
879 return err;
880 if (idx == -1)
881 return got_error(GOT_ERR_BAD_PACKIDX);
883 return got_packidx_get_object_id(base_id, packidx, idx);
886 static const struct got_error *
887 raw_delta_request(struct imsg *imsg, struct imsgbuf *ibuf,
888 FILE *delta_outfile, struct got_pack *pack,
889 struct got_packidx *packidx)
891 const struct got_error *err = NULL;
892 struct got_imsg_raw_delta_request req;
893 size_t datalen, delta_size, delta_compressed_size;
894 off_t delta_offset, delta_data_offset;
895 uint8_t *delta_buf = NULL;
896 struct got_object_id id, base_id;
897 off_t base_offset, delta_out_offset = 0;
898 uint64_t base_size = 0, result_size = 0;
899 size_t w;
901 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
902 if (datalen != sizeof(req))
903 return got_error(GOT_ERR_PRIVSEP_LEN);
904 memcpy(&req, imsg->data, sizeof(req));
905 memcpy(&id, &req.id, sizeof(id));
907 imsg->fd = -1;
909 err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
910 &delta_compressed_size, &delta_offset, &delta_data_offset,
911 &base_offset, &base_id, &base_size, &result_size,
912 pack, packidx, req.idx);
913 if (err)
914 goto done;
916 /*
917 * If this is an offset delta we must determine the base
918 * object ID ourselves.
919 */
920 if (base_offset != 0) {
921 err = get_base_object_id(&base_id, packidx, base_offset);
922 if (err)
923 goto done;
926 delta_out_offset = ftello(delta_outfile);
927 w = fwrite(delta_buf, 1, delta_compressed_size, delta_outfile);
928 if (w != delta_compressed_size) {
929 err = got_ferror(delta_outfile, GOT_ERR_IO);
930 goto done;
932 if (fflush(delta_outfile) == -1) {
933 err = got_error_from_errno("fflush");
934 goto done;
937 err = got_privsep_send_raw_delta(ibuf, base_size, result_size,
938 delta_size, delta_compressed_size, delta_offset, delta_out_offset,
939 &base_id);
940 done:
941 free(delta_buf);
942 return err;
945 struct search_deltas_arg {
946 struct imsgbuf *ibuf;
947 struct got_packidx *packidx;
948 struct got_pack *pack;
949 struct got_object_idset *idset;
950 struct got_imsg_reused_delta deltas[GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS];
951 size_t ndeltas;
952 };
954 static const struct got_error *
955 search_delta_for_object(struct got_object_id *id, void *data, void *arg)
957 const struct got_error *err;
958 struct search_deltas_arg *a = arg;
959 int obj_idx;
960 uint8_t *delta_buf = NULL;
961 uint64_t base_size, result_size;
962 size_t delta_size, delta_compressed_size;
963 off_t delta_offset, delta_data_offset, base_offset;
964 struct got_object_id base_id;
966 if (sigint_received)
967 return got_error(GOT_ERR_CANCELLED);
969 obj_idx = got_packidx_get_object_idx(a->packidx, id);
970 if (obj_idx == -1)
971 return NULL; /* object not present in our pack file */
973 err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
974 &delta_compressed_size, &delta_offset, &delta_data_offset,
975 &base_offset, &base_id, &base_size, &result_size,
976 a->pack, a->packidx, obj_idx);
977 if (err) {
978 if (err->code == GOT_ERR_OBJ_TYPE)
979 return NULL; /* object not stored as a delta */
980 return err;
983 /*
984 * If this is an offset delta we must determine the base
985 * object ID ourselves.
986 */
987 if (base_offset != 0) {
988 err = get_base_object_id(&base_id, a->packidx, base_offset);
989 if (err)
990 goto done;
993 if (got_object_idset_contains(a->idset, &base_id)) {
994 struct got_imsg_reused_delta *delta;
996 delta = &a->deltas[a->ndeltas++];
997 memcpy(&delta->id, id, sizeof(delta->id));
998 memcpy(&delta->base_id, &base_id, sizeof(delta->base_id));
999 delta->base_size = base_size;
1000 delta->result_size = result_size;
1001 delta->delta_size = delta_size;
1002 delta->delta_compressed_size = delta_compressed_size;
1003 delta->delta_offset = delta_data_offset;
1005 if (a->ndeltas >= GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS) {
1006 err = got_privsep_send_reused_deltas(a->ibuf,
1007 a->deltas, a->ndeltas);
1008 if (err)
1009 goto done;
1010 a->ndeltas = 0;
1013 done:
1014 free(delta_buf);
1015 return err;
1018 static const struct got_error *
1019 recv_object_ids(struct got_object_idset *idset, struct imsgbuf *ibuf)
1021 const struct got_error *err = NULL;
1022 int done = 0;
1023 struct got_object_id *ids;
1024 size_t nids, i;
1026 for (;;) {
1027 err = got_privsep_recv_object_idlist(&done, &ids, &nids, ibuf);
1028 if (err || done)
1029 break;
1030 for (i = 0; i < nids; i++) {
1031 err = got_object_idset_add(idset, &ids[i], NULL);
1032 if (err) {
1033 free(ids);
1034 return err;
1037 free(ids);
1040 return err;
1043 static const struct got_error *
1044 recv_object_id_queue(struct got_object_id_queue *queue,
1045 struct got_object_idset *queued_ids, struct imsgbuf *ibuf)
1047 const struct got_error *err = NULL;
1048 int done = 0;
1049 struct got_object_qid *qid;
1050 struct got_object_id *ids;
1051 size_t nids, i;
1053 for (;;) {
1054 err = got_privsep_recv_object_idlist(&done, &ids, &nids, ibuf);
1055 if (err || done)
1056 break;
1057 for (i = 0; i < nids; i++) {
1058 err = got_object_qid_alloc_partial(&qid);
1059 if (err)
1060 return err;
1061 memcpy(&qid->id, &ids[i], sizeof(qid->id));
1062 STAILQ_INSERT_TAIL(queue, qid, entry);
1063 err = got_object_idset_add(queued_ids, &qid->id, NULL);
1064 if (err)
1065 return err;
1069 return err;
1072 static const struct got_error *
1073 delta_reuse_request(struct imsg *imsg, struct imsgbuf *ibuf,
1074 struct got_pack *pack, struct got_packidx *packidx)
1076 const struct got_error *err = NULL;
1077 struct got_object_idset *idset;
1078 struct search_deltas_arg sda;
1080 idset = got_object_idset_alloc();
1081 if (idset == NULL)
1082 return got_error_from_errno("got_object_idset_alloc");
1084 err = recv_object_ids(idset, ibuf);
1085 if (err)
1086 return err;
1088 memset(&sda, 0, sizeof(sda));
1089 sda.ibuf = ibuf;
1090 sda.idset = idset;
1091 sda.pack = pack;
1092 sda.packidx = packidx;
1093 err = got_object_idset_for_each(idset, search_delta_for_object, &sda);
1094 if (err)
1095 goto done;
1097 if (sda.ndeltas > 0) {
1098 err = got_privsep_send_reused_deltas(ibuf, sda.deltas,
1099 sda.ndeltas);
1100 if (err)
1101 goto done;
1104 err = got_privsep_send_reused_deltas_done(ibuf);
1105 done:
1106 got_object_idset_free(idset);
1107 return err;
1110 static const struct got_error *
1111 receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
1113 const struct got_error *err = NULL;
1114 struct imsg imsg;
1115 struct got_imsg_packidx ipackidx;
1116 size_t datalen;
1117 struct got_packidx *p;
1119 *packidx = NULL;
1121 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1122 if (err)
1123 return err;
1125 p = calloc(1, sizeof(*p));
1126 if (p == NULL) {
1127 err = got_error_from_errno("calloc");
1128 goto done;
1131 if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
1132 err = got_error(GOT_ERR_PRIVSEP_MSG);
1133 goto done;
1136 if (imsg.fd == -1) {
1137 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1138 goto done;
1141 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1142 if (datalen != sizeof(ipackidx)) {
1143 err = got_error(GOT_ERR_PRIVSEP_LEN);
1144 goto done;
1146 memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
1148 p->len = ipackidx.len;
1149 p->fd = dup(imsg.fd);
1150 if (p->fd == -1) {
1151 err = got_error_from_errno("dup");
1152 goto done;
1154 if (lseek(p->fd, 0, SEEK_SET) == -1) {
1155 err = got_error_from_errno("lseek");
1156 goto done;
1159 #ifndef GOT_PACK_NO_MMAP
1160 if (p->len > 0 && p->len <= SIZE_MAX) {
1161 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
1162 if (p->map == MAP_FAILED)
1163 p->map = NULL; /* fall back to read(2) */
1165 #endif
1166 err = got_packidx_init_hdr(p, 1, ipackidx.packfile_size);
1167 done:
1168 if (err) {
1169 if (imsg.fd != -1)
1170 close(imsg.fd);
1171 got_packidx_close(p);
1172 } else
1173 *packidx = p;
1174 imsg_free(&imsg);
1175 return err;
1178 static const struct got_error *
1179 send_tree_enumeration_done(struct imsgbuf *ibuf)
1181 if (imsg_compose(ibuf, GOT_IMSG_TREE_ENUMERATION_DONE, 0, 0, -1,
1182 NULL, 0) == -1)
1183 return got_error_from_errno("imsg_compose TREE_ENUMERATION_DONE");
1185 return got_privsep_flush_imsg(ibuf);
1188 struct enumerated_tree {
1189 struct got_object_id id;
1190 char *path;
1191 uint8_t *buf;
1192 struct got_parsed_tree_entry *entries;
1193 int nentries;
1196 static const struct got_error *
1197 enumerate_tree(int *have_all_entries, struct imsgbuf *ibuf, size_t *totlen,
1198 struct got_object_id *tree_id,
1199 const char *path, struct got_pack *pack, struct got_packidx *packidx,
1200 struct got_object_cache *objcache, struct got_object_idset *idset,
1201 struct enumerated_tree **trees, size_t *nalloc, size_t *ntrees)
1203 const struct got_error *err = NULL;
1204 struct got_object_id_queue ids;
1205 struct got_object_qid *qid;
1206 uint8_t *buf = NULL;
1207 size_t len = 0;
1208 struct got_parsed_tree_entry *entries = NULL;
1209 size_t nentries = 0, nentries_alloc = 0, i;
1210 struct enumerated_tree *tree;
1212 *ntrees = 0;
1213 *have_all_entries = 1;
1214 STAILQ_INIT(&ids);
1216 err = got_object_qid_alloc_partial(&qid);
1217 if (err)
1218 return err;
1219 memcpy(&qid->id, tree_id, sizeof(*tree_id));
1220 qid->data = strdup(path);
1221 if (qid->data == NULL) {
1222 err = got_error_from_errno("strdup");
1223 goto done;
1225 STAILQ_INSERT_TAIL(&ids, qid, entry);
1226 qid = NULL;
1228 /* Traverse the tree hierarchy, gather tree object IDs and paths. */
1229 do {
1230 const char *path;
1231 int idx, i;
1233 if (sigint_received) {
1234 err = got_error(GOT_ERR_CANCELLED);
1235 goto done;
1238 qid = STAILQ_FIRST(&ids);
1239 STAILQ_REMOVE_HEAD(&ids, entry);
1240 path = qid->data;
1242 idx = got_packidx_get_object_idx(packidx, &qid->id);
1243 if (idx == -1) {
1244 *have_all_entries = 0;
1245 break;
1248 err = open_tree(&buf, &len, pack, packidx, idx, &qid->id,
1249 objcache);
1250 if (err) {
1251 if (err->code != GOT_ERR_NO_OBJ)
1252 goto done;
1255 err = got_object_parse_tree(&entries, &nentries,
1256 &nentries_alloc, buf, len);
1257 if (err)
1258 goto done;
1260 err = got_object_idset_add(idset, &qid->id, NULL);
1261 if (err)
1262 goto done;
1264 for (i = 0; i < nentries; i++) {
1265 struct got_object_qid *eqid = NULL;
1266 struct got_parsed_tree_entry *pte = &entries[i];
1267 char *p;
1269 if (!S_ISDIR(pte->mode))
1270 continue;
1272 err = got_object_qid_alloc_partial(&eqid);
1273 if (err)
1274 goto done;
1275 memcpy(eqid->id.sha1, pte->id, sizeof(eqid->id.sha1));
1277 if (got_object_idset_contains(idset, &eqid->id)) {
1278 got_object_qid_free(eqid);
1279 continue;
1282 if (asprintf(&p, "%s%s%s", path,
1283 got_path_is_root_dir(path) ? "" : "/",
1284 pte->name) == -1) {
1285 err = got_error_from_errno("asprintf");
1286 got_object_qid_free(eqid);
1287 goto done;
1289 eqid->data = p;
1290 STAILQ_INSERT_TAIL(&ids, eqid, entry);
1293 if (*ntrees >= *nalloc) {
1294 struct enumerated_tree *new;
1295 new = recallocarray(*trees, *nalloc, *nalloc + 16,
1296 sizeof(*new));
1297 if (new == NULL) {
1298 err = got_error_from_errno("malloc");
1299 goto done;
1301 *trees = new;
1302 *nalloc += 16;
1304 tree = &(*trees)[*ntrees];
1305 (*ntrees)++;
1306 memcpy(&tree->id, &qid->id, sizeof(tree->id));
1307 tree->path = qid->data;
1308 tree->buf = buf;
1309 buf = NULL;
1310 tree->entries = entries;
1311 entries = NULL;
1312 nentries_alloc = 0;
1313 tree->nentries = nentries;
1314 nentries = 0;
1316 got_object_qid_free(qid);
1317 qid = NULL;
1318 } while (!STAILQ_EMPTY(&ids));
1320 if (*have_all_entries) {
1321 int i;
1323 * We have managed to traverse all entries in the hierarchy.
1324 * Tell the main process what we have found.
1326 for (i = 0; i < *ntrees; i++) {
1327 tree = &(*trees)[i];
1328 err = got_privsep_send_enumerated_tree(totlen,
1329 ibuf, &tree->id, tree->path, tree->entries,
1330 tree->nentries);
1331 if (err)
1332 goto done;
1333 free(tree->buf);
1334 tree->buf = NULL;
1335 free(tree->path);
1336 tree->path = NULL;
1337 free(tree->entries);
1338 tree->entries = NULL;
1340 *ntrees = 0; /* don't loop again below to free memory */
1342 err = send_tree_enumeration_done(ibuf);
1343 } else {
1345 * We can only load fully packed tree hierarchies on
1346 * behalf of the main process, otherwise the main process
1347 * gets a wrong idea about which tree objects have
1348 * already been traversed.
1349 * Indicate a missing entry for the root of this tree.
1350 * The main process should continue by loading this
1351 * entire tree the slow way.
1353 err = got_privsep_send_enumerated_tree(totlen, ibuf,
1354 tree_id, "/", NULL, -1);
1355 if (err)
1356 goto done;
1358 done:
1359 free(buf);
1360 free(entries);
1361 for (i = 0; i < *ntrees; i++) {
1362 tree = &(*trees)[i];
1363 free(tree->buf);
1364 tree->buf = NULL;
1365 free(tree->path);
1366 tree->path = NULL;
1367 free(tree->entries);
1368 tree->entries = NULL;
1370 if (qid)
1371 free(qid->data);
1372 got_object_qid_free(qid);
1373 got_object_id_queue_free(&ids);
1374 if (err) {
1375 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1376 err = NULL;
1377 else
1378 got_privsep_send_error(ibuf, err);
1381 return err;
1384 static const struct got_error *
1385 enumeration_request(struct imsg *imsg, struct imsgbuf *ibuf,
1386 struct got_pack *pack, struct got_packidx *packidx,
1387 struct got_object_cache *objcache)
1389 const struct got_error *err = NULL;
1390 struct got_object_id_queue commit_ids;
1391 const struct got_object_id_queue *parents = NULL;
1392 struct got_object_qid *qid = NULL;
1393 struct got_object *obj = NULL;
1394 struct got_commit_object *commit = NULL;
1395 struct got_object_id *tree_id = NULL;
1396 size_t totlen = 0;
1397 struct got_object_idset *idset, *queued_ids = NULL;
1398 int i, idx, have_all_entries = 1;
1399 struct enumerated_tree *trees = NULL;
1400 size_t ntrees = 0, nalloc = 16;
1402 STAILQ_INIT(&commit_ids);
1404 trees = calloc(nalloc, sizeof(*trees));
1405 if (trees == NULL)
1406 return got_error_from_errno("calloc");
1408 idset = got_object_idset_alloc();
1409 if (idset == NULL) {
1410 err = got_error_from_errno("got_object_idset_alloc");
1411 goto done;
1414 queued_ids = got_object_idset_alloc();
1415 if (queued_ids == NULL) {
1416 err = got_error_from_errno("got_object_idset_alloc");
1417 goto done;
1420 err = recv_object_id_queue(&commit_ids, queued_ids, ibuf);
1421 if (err)
1422 goto done;
1424 if (STAILQ_EMPTY(&commit_ids)) {
1425 err = got_error(GOT_ERR_PRIVSEP_MSG);
1426 goto done;
1429 err = recv_object_ids(idset, ibuf);
1430 if (err)
1431 goto done;
1433 while (!STAILQ_EMPTY(&commit_ids)) {
1434 if (sigint_received) {
1435 err = got_error(GOT_ERR_CANCELLED);
1436 goto done;
1439 qid = STAILQ_FIRST(&commit_ids);
1440 STAILQ_REMOVE_HEAD(&commit_ids, entry);
1442 if (got_object_idset_contains(idset, &qid->id)) {
1443 got_object_qid_free(qid);
1444 qid = NULL;
1445 continue;
1448 idx = got_packidx_get_object_idx(packidx, &qid->id);
1449 if (idx == -1) {
1450 have_all_entries = 0;
1451 break;
1454 err = open_object(&obj, pack, packidx, idx, &qid->id,
1455 objcache);
1456 if (err)
1457 goto done;
1458 if (obj->type == GOT_OBJ_TYPE_TAG) {
1459 struct got_tag_object *tag;
1460 uint8_t *buf;
1461 size_t len;
1462 err = got_packfile_extract_object_to_mem(&buf,
1463 &len, obj, pack);
1464 if (err)
1465 goto done;
1466 obj->size = len;
1467 err = got_object_parse_tag(&tag, buf, len);
1468 if (err) {
1469 free(buf);
1470 goto done;
1472 idx = got_packidx_get_object_idx(packidx, &tag->id);
1473 if (idx == -1) {
1474 have_all_entries = 0;
1475 break;
1477 err = open_commit(&commit, pack, packidx, idx,
1478 &tag->id, objcache);
1479 got_object_tag_close(tag);
1480 free(buf);
1481 if (err)
1482 goto done;
1483 } else if (obj->type == GOT_OBJ_TYPE_COMMIT) {
1484 err = open_commit(&commit, pack, packidx, idx,
1485 &qid->id, objcache);
1486 if (err)
1487 goto done;
1488 } else {
1489 err = got_error(GOT_ERR_OBJ_TYPE);
1490 goto done;
1492 got_object_close(obj);
1493 obj = NULL;
1495 err = got_privsep_send_enumerated_commit(ibuf, &qid->id,
1496 got_object_commit_get_committer_time(commit));
1497 if (err)
1498 goto done;
1500 tree_id = got_object_commit_get_tree_id(commit);
1501 idx = got_packidx_get_object_idx(packidx, tree_id);
1502 if (idx == -1) {
1503 have_all_entries = 0;
1504 err = got_privsep_send_enumerated_tree(&totlen, ibuf,
1505 tree_id, "/", NULL, -1);
1506 if (err)
1507 goto done;
1508 break;
1511 if (got_object_idset_contains(idset, tree_id)) {
1512 got_object_qid_free(qid);
1513 qid = NULL;
1514 err = send_tree_enumeration_done(ibuf);
1515 if (err)
1516 goto done;
1517 continue;
1520 err = enumerate_tree(&have_all_entries, ibuf, &totlen,
1521 tree_id, "/", pack, packidx, objcache, idset,
1522 &trees, &nalloc, &ntrees);
1523 if (err)
1524 goto done;
1526 if (!have_all_entries)
1527 break;
1529 got_object_qid_free(qid);
1530 qid = NULL;
1532 parents = got_object_commit_get_parent_ids(commit);
1533 if (parents) {
1534 struct got_object_qid *pid;
1535 STAILQ_FOREACH(pid, parents, entry) {
1536 if (got_object_idset_contains(idset, &pid->id))
1537 continue;
1538 if (got_object_idset_contains(queued_ids, &pid->id))
1539 continue;
1540 err = got_object_qid_alloc_partial(&qid);
1541 if (err)
1542 goto done;
1543 memcpy(&qid->id, &pid->id, sizeof(qid->id));
1544 STAILQ_INSERT_TAIL(&commit_ids, qid, entry);
1545 qid = NULL;
1549 got_object_commit_close(commit);
1550 commit = NULL;
1553 if (have_all_entries) {
1554 err = got_privsep_send_object_enumeration_done(ibuf);
1555 if (err)
1556 goto done;
1557 } else {
1558 err = got_privsep_send_object_enumeration_incomplete(ibuf);
1559 if (err)
1560 goto done;
1562 done:
1563 if (obj)
1564 got_object_close(obj);
1565 if (commit)
1566 got_object_commit_close(commit);
1567 got_object_qid_free(qid);
1568 got_object_id_queue_free(&commit_ids);
1569 if (idset)
1570 got_object_idset_free(idset);
1571 if (queued_ids)
1572 got_object_idset_free(queued_ids);
1573 for (i = 0; i < ntrees; i++) {
1574 struct enumerated_tree *tree = &trees[i];
1575 free(tree->buf);
1576 free(tree->path);
1577 free(tree->entries);
1579 free(trees);
1580 return err;
1583 enum findtwixt_color {
1584 COLOR_KEEP = 0,
1585 COLOR_DROP,
1586 COLOR_SKIP,
1587 COLOR_MAX,
1590 static const struct got_error *
1591 paint_commit(struct got_object_qid *qid, intptr_t color)
1593 if (color < 0 || color >= COLOR_MAX)
1594 return got_error(GOT_ERR_RANGE);
1596 qid->data = (void *)color;
1597 return NULL;
1600 static const struct got_error *
1601 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1602 intptr_t color)
1604 const struct got_error *err;
1605 struct got_object_qid *qid;
1607 err = got_object_qid_alloc_partial(&qid);
1608 if (err)
1609 return err;
1611 memcpy(&qid->id, id, sizeof(qid->id));
1612 STAILQ_INSERT_TAIL(ids, qid, entry);
1613 return paint_commit(qid, color);
1616 static const struct got_error *
1617 paint_commits(struct got_object_id_queue *ids, int *nids,
1618 struct got_object_idset *keep, struct got_object_idset *drop,
1619 struct got_object_idset *skip, struct got_pack *pack,
1620 struct got_packidx *packidx, struct imsgbuf *ibuf,
1621 struct got_object_cache *objcache)
1623 const struct got_error *err = NULL;
1624 struct got_commit_object *commit = NULL;
1625 struct got_object_id_queue painted;
1626 const struct got_object_id_queue *parents;
1627 struct got_object_qid *qid = NULL;
1628 int nqueued = *nids, nskip = 0, npainted = 0;
1630 STAILQ_INIT(&painted);
1632 while (!STAILQ_EMPTY(ids) && nskip != nqueued) {
1633 int idx;
1634 intptr_t color;
1636 if (sigint_received) {
1637 err = got_error(GOT_ERR_CANCELLED);
1638 goto done;
1641 qid = STAILQ_FIRST(ids);
1642 idx = got_packidx_get_object_idx(packidx, &qid->id);
1643 if (idx == -1) {
1644 qid = NULL;
1645 break;
1648 STAILQ_REMOVE_HEAD(ids, entry);
1649 nqueued--;
1650 color = (intptr_t)qid->data;
1651 if (color == COLOR_SKIP)
1652 nskip--;
1654 if (got_object_idset_contains(skip, &qid->id)) {
1655 got_object_qid_free(qid);
1656 qid = NULL;
1657 continue;
1660 switch (color) {
1661 case COLOR_KEEP:
1662 if (got_object_idset_contains(keep, &qid->id)) {
1663 got_object_qid_free(qid);
1664 qid = NULL;
1665 continue;
1667 if (got_object_idset_contains(drop, &qid->id)) {
1668 err = paint_commit(qid, COLOR_SKIP);
1669 if (err)
1670 goto done;
1672 err = got_object_idset_add(keep, &qid->id, NULL);
1673 if (err)
1674 goto done;
1675 break;
1676 case COLOR_DROP:
1677 if (got_object_idset_contains(drop, &qid->id)) {
1678 got_object_qid_free(qid);
1679 qid = NULL;
1680 continue;
1682 if (got_object_idset_contains(keep, &qid->id)) {
1683 err = paint_commit(qid, COLOR_SKIP);
1684 if (err)
1685 goto done;
1687 err = got_object_idset_add(drop, &qid->id, NULL);
1688 if (err)
1689 goto done;
1690 break;
1691 case COLOR_SKIP:
1692 if (!got_object_idset_contains(skip, &qid->id)) {
1693 err = got_object_idset_add(skip, &qid->id,
1694 NULL);
1695 if (err)
1696 goto done;
1698 break;
1699 default:
1700 /* should not happen */
1701 err = got_error_fmt(GOT_ERR_NOT_IMPL,
1702 "%s invalid commit color %"PRIdPTR, __func__,
1703 color);
1704 goto done;
1707 err = open_commit(&commit, pack, packidx, idx, &qid->id,
1708 objcache);
1709 if (err)
1710 goto done;
1712 parents = got_object_commit_get_parent_ids(commit);
1713 if (parents) {
1714 struct got_object_qid *pid;
1715 color = (intptr_t)qid->data;
1716 STAILQ_FOREACH(pid, parents, entry) {
1717 err = queue_commit_id(ids, &pid->id, color);
1718 if (err)
1719 goto done;
1720 nqueued++;
1721 if (color == COLOR_SKIP)
1722 nskip++;
1726 got_object_commit_close(commit);
1727 commit = NULL;
1729 STAILQ_INSERT_TAIL(&painted, qid, entry);
1730 qid = NULL;
1731 npainted++;
1733 err = got_privsep_send_painted_commits(ibuf, &painted,
1734 &npainted, 1, 0);
1735 if (err)
1736 goto done;
1739 err = got_privsep_send_painted_commits(ibuf, &painted, &npainted, 1, 1);
1740 if (err)
1741 goto done;
1743 *nids = nqueued;
1744 done:
1745 if (commit)
1746 got_object_commit_close(commit);
1747 got_object_qid_free(qid);
1748 return err;
1751 static void
1752 commit_painting_free(struct got_object_idset **keep,
1753 struct got_object_idset **drop,
1754 struct got_object_idset **skip)
1756 if (*keep) {
1757 got_object_idset_free(*keep);
1758 *keep = NULL;
1760 if (*drop) {
1761 got_object_idset_free(*drop);
1762 *drop = NULL;
1764 if (*skip) {
1765 got_object_idset_free(*skip);
1766 *skip = NULL;
1770 static const struct got_error *
1771 commit_painting_init(struct imsgbuf *ibuf, struct got_object_idset **keep,
1772 struct got_object_idset **drop, struct got_object_idset **skip)
1774 const struct got_error *err = NULL;
1776 *keep = got_object_idset_alloc();
1777 if (*keep == NULL) {
1778 err = got_error_from_errno("got_object_idset_alloc");
1779 goto done;
1781 *drop = got_object_idset_alloc();
1782 if (*drop == NULL) {
1783 err = got_error_from_errno("got_object_idset_alloc");
1784 goto done;
1786 *skip = got_object_idset_alloc();
1787 if (*skip == NULL) {
1788 err = got_error_from_errno("got_object_idset_alloc");
1789 goto done;
1792 err = recv_object_ids(*keep, ibuf);
1793 if (err)
1794 goto done;
1795 err = recv_object_ids(*drop, ibuf);
1796 if (err)
1797 goto done;
1798 err = recv_object_ids(*skip, ibuf);
1799 if (err)
1800 goto done;
1802 done:
1803 if (err)
1804 commit_painting_free(keep, drop, skip);
1806 return err;
1809 static const struct got_error *
1810 commit_painting_request(struct imsg *imsg, struct imsgbuf *ibuf,
1811 struct got_pack *pack, struct got_packidx *packidx,
1812 struct got_object_cache *objcache, struct got_object_idset *keep,
1813 struct got_object_idset *drop, struct got_object_idset *skip)
1815 const struct got_error *err = NULL;
1816 struct got_imsg_commit_painting_request ireq;
1817 struct got_object_id id;
1818 size_t datalen;
1819 struct got_object_id_queue ids;
1820 int nids = 0;
1822 STAILQ_INIT(&ids);
1824 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1825 if (datalen != sizeof(ireq))
1826 return got_error(GOT_ERR_PRIVSEP_LEN);
1827 memcpy(&ireq, imsg->data, sizeof(ireq));
1828 memcpy(&id, &ireq.id, sizeof(id));
1830 err = queue_commit_id(&ids, &id, ireq.color);
1831 if (err)
1832 return err;
1833 nids = 1;
1835 err = paint_commits(&ids, &nids, keep, drop, skip,
1836 pack, packidx, ibuf, objcache);
1837 if (err)
1838 goto done;
1840 err = got_privsep_send_painted_commits(ibuf, &ids, &nids, 0, 1);
1841 if (err)
1842 goto done;
1844 err = got_privsep_send_painting_commits_done(ibuf);
1845 done:
1846 got_object_id_queue_free(&ids);
1847 return err;
1850 static const struct got_error *
1851 receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
1853 const struct got_error *err = NULL;
1854 struct imsg imsg;
1855 struct got_imsg_pack ipack;
1856 size_t datalen;
1857 struct got_pack *pack;
1859 *packp = NULL;
1861 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1862 if (err)
1863 return err;
1865 pack = calloc(1, sizeof(*pack));
1866 if (pack == NULL) {
1867 err = got_error_from_errno("calloc");
1868 goto done;
1871 if (imsg.hdr.type != GOT_IMSG_PACK) {
1872 err = got_error(GOT_ERR_PRIVSEP_MSG);
1873 goto done;
1876 if (imsg.fd == -1) {
1877 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1878 goto done;
1881 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1882 if (datalen != sizeof(ipack)) {
1883 err = got_error(GOT_ERR_PRIVSEP_LEN);
1884 goto done;
1886 memcpy(&ipack, imsg.data, sizeof(ipack));
1888 pack->filesize = ipack.filesize;
1889 pack->fd = dup(imsg.fd);
1890 if (pack->fd == -1) {
1891 err = got_error_from_errno("dup");
1892 goto done;
1894 if (lseek(pack->fd, 0, SEEK_SET) == -1) {
1895 err = got_error_from_errno("lseek");
1896 goto done;
1898 pack->path_packfile = strdup(ipack.path_packfile);
1899 if (pack->path_packfile == NULL) {
1900 err = got_error_from_errno("strdup");
1901 goto done;
1904 err = got_delta_cache_alloc(&pack->delta_cache);
1905 if (err)
1906 goto done;
1908 #ifndef GOT_PACK_NO_MMAP
1909 if (pack->filesize > 0 && pack->filesize <= SIZE_MAX) {
1910 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1911 pack->fd, 0);
1912 if (pack->map == MAP_FAILED)
1913 pack->map = NULL; /* fall back to read(2) */
1915 #endif
1916 done:
1917 if (err) {
1918 if (imsg.fd != -1)
1919 close(imsg.fd);
1920 free(pack);
1921 } else
1922 *packp = pack;
1923 imsg_free(&imsg);
1924 return err;
1927 int
1928 main(int argc, char *argv[])
1930 const struct got_error *err = NULL;
1931 struct imsgbuf ibuf;
1932 struct imsg imsg;
1933 struct got_packidx *packidx = NULL;
1934 struct got_pack *pack = NULL;
1935 struct got_object_cache objcache;
1936 FILE *basefile = NULL, *accumfile = NULL, *delta_outfile = NULL;
1937 struct got_object_idset *keep = NULL, *drop = NULL, *skip = NULL;
1938 struct got_parsed_tree_entry *entries = NULL;
1939 size_t nentries = 0, nentries_alloc = 0;
1941 //static int attached;
1942 //while (!attached) sleep(1);
1944 signal(SIGINT, catch_sigint);
1946 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
1948 err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
1949 if (err) {
1950 err = got_error_from_errno("got_object_cache_init");
1951 got_privsep_send_error(&ibuf, err);
1952 return 1;
1955 #ifndef PROFILE
1956 /* revoke access to most system calls */
1957 if (pledge("stdio recvfd", NULL) == -1) {
1958 err = got_error_from_errno("pledge");
1959 got_privsep_send_error(&ibuf, err);
1960 return 1;
1963 /* revoke fs access */
1964 if (landlock_no_fs() == -1) {
1965 err = got_error_from_errno("landlock_no_fs");
1966 got_privsep_send_error(&ibuf, err);
1967 return 1;
1969 if (cap_enter() == -1) {
1970 err = got_error_from_errno("cap_enter");
1971 got_privsep_send_error(&ibuf, err);
1972 return 1;
1974 #endif
1976 err = receive_packidx(&packidx, &ibuf);
1977 if (err) {
1978 got_privsep_send_error(&ibuf, err);
1979 return 1;
1982 err = receive_pack(&pack, &ibuf);
1983 if (err) {
1984 got_privsep_send_error(&ibuf, err);
1985 return 1;
1988 for (;;) {
1989 imsg.fd = -1;
1991 if (sigint_received) {
1992 err = got_error(GOT_ERR_CANCELLED);
1993 break;
1996 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1997 if (err) {
1998 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1999 err = NULL;
2000 break;
2003 if (imsg.hdr.type == GOT_IMSG_STOP)
2004 break;
2006 switch (imsg.hdr.type) {
2007 case GOT_IMSG_TMPFD:
2008 if (basefile == NULL) {
2009 err = receive_tempfile(&basefile, "w+",
2010 &imsg, &ibuf);
2011 } else if (accumfile == NULL) {
2012 err = receive_tempfile(&accumfile, "w+",
2013 &imsg, &ibuf);
2014 } else
2015 err = got_error(GOT_ERR_PRIVSEP_MSG);
2016 break;
2017 case GOT_IMSG_PACKED_OBJECT_REQUEST:
2018 err = object_request(&imsg, &ibuf, pack, packidx,
2019 &objcache);
2020 break;
2021 case GOT_IMSG_PACKED_RAW_OBJECT_REQUEST:
2022 if (basefile == NULL || accumfile == NULL) {
2023 err = got_error(GOT_ERR_PRIVSEP_MSG);
2024 break;
2026 err = raw_object_request(&imsg, &ibuf, pack, packidx,
2027 &objcache, basefile, accumfile);
2028 break;
2029 case GOT_IMSG_RAW_DELTA_OUTFD:
2030 if (delta_outfile != NULL) {
2031 err = got_error(GOT_ERR_PRIVSEP_MSG);
2032 break;
2034 err = receive_tempfile(&delta_outfile, "w",
2035 &imsg, &ibuf);
2036 break;
2037 case GOT_IMSG_RAW_DELTA_REQUEST:
2038 if (delta_outfile == NULL) {
2039 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
2040 break;
2042 err = raw_delta_request(&imsg, &ibuf, delta_outfile,
2043 pack, packidx);
2044 break;
2045 case GOT_IMSG_DELTA_REUSE_REQUEST:
2046 err = delta_reuse_request(&imsg, &ibuf, pack, packidx);
2047 break;
2048 case GOT_IMSG_COMMIT_REQUEST:
2049 err = commit_request(&imsg, &ibuf, pack, packidx,
2050 &objcache);
2051 break;
2052 case GOT_IMSG_TREE_REQUEST:
2053 err = tree_request(&imsg, &ibuf, pack, packidx,
2054 &objcache, &entries, &nentries, &nentries_alloc);
2055 break;
2056 case GOT_IMSG_BLOB_REQUEST:
2057 if (basefile == NULL || accumfile == NULL) {
2058 err = got_error(GOT_ERR_PRIVSEP_MSG);
2059 break;
2061 err = blob_request(&imsg, &ibuf, pack, packidx,
2062 &objcache, basefile, accumfile);
2063 break;
2064 case GOT_IMSG_TAG_REQUEST:
2065 err = tag_request(&imsg, &ibuf, pack, packidx,
2066 &objcache);
2067 break;
2068 case GOT_IMSG_COMMIT_TRAVERSAL_REQUEST:
2069 err = commit_traversal_request(&imsg, &ibuf, pack,
2070 packidx, &objcache);
2071 break;
2072 case GOT_IMSG_OBJECT_ENUMERATION_REQUEST:
2073 err = enumeration_request(&imsg, &ibuf, pack,
2074 packidx, &objcache);
2075 break;
2076 case GOT_IMSG_COMMIT_PAINTING_INIT:
2077 commit_painting_free(&keep, &drop, &skip);
2078 err = commit_painting_init(&ibuf, &keep, &drop, &skip);
2079 break;
2080 case GOT_IMSG_COMMIT_PAINTING_REQUEST:
2081 if (keep == NULL || drop == NULL || skip == NULL) {
2082 err = got_error(GOT_ERR_PRIVSEP_MSG);
2083 break;
2085 err = commit_painting_request(&imsg, &ibuf, pack,
2086 packidx, &objcache, keep, drop, skip);
2087 break;
2088 case GOT_IMSG_COMMIT_PAINTING_DONE:
2089 commit_painting_free(&keep, &drop, &skip);
2090 break;
2091 default:
2092 err = got_error(GOT_ERR_PRIVSEP_MSG);
2093 break;
2096 if (imsg.fd != -1 && close(imsg.fd) == -1 && err == NULL)
2097 err = got_error_from_errno("close");
2098 imsg_free(&imsg);
2099 if (err)
2100 break;
2103 free(entries);
2104 commit_painting_free(&keep, &drop, &skip);
2105 if (packidx)
2106 got_packidx_close(packidx);
2107 if (pack)
2108 got_pack_close(pack);
2109 got_object_cache_close(&objcache);
2110 imsg_clear(&ibuf);
2111 if (basefile && fclose(basefile) == EOF && err == NULL)
2112 err = got_error_from_errno("fclose");
2113 if (accumfile && fclose(accumfile) == EOF && err == NULL)
2114 err = got_error_from_errno("fclose");
2115 if (delta_outfile && fclose(delta_outfile) == EOF && err == NULL)
2116 err = got_error_from_errno("fclose");
2117 if (err) {
2118 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
2119 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
2120 got_privsep_send_error(&ibuf, err);
2123 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
2124 err = got_error_from_errno("close");
2125 return err ? 1 : 0;