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 */
17 #include <sys/stat.h>
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/uio.h>
21 #include <sys/time.h>
22 #include <sys/mman.h>
24 #include <inttypes.h>
25 #include <limits.h>
26 #include <signal.h>
27 #include <stdint.h>
28 #include <imsg.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sha1.h>
33 #include <sha2.h>
34 #include <unistd.h>
35 #include <zlib.h>
37 #include "got_error.h"
38 #include "got_object.h"
39 #include "got_path.h"
41 #include "got_lib_delta.h"
42 #include "got_lib_delta_cache.h"
43 #include "got_lib_object.h"
44 #include "got_lib_object_cache.h"
45 #include "got_lib_object_parse.h"
46 #include "got_lib_object_idset.h"
47 #include "got_lib_privsep.h"
48 #include "got_lib_pack.h"
50 #ifndef nitems
51 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
52 #endif
54 static volatile sig_atomic_t sigint_received;
56 static void
57 catch_sigint(int signo)
58 {
59 sigint_received = 1;
60 }
62 static const struct got_error *
63 open_object(struct got_object **obj, struct got_pack *pack,
64 struct got_packidx *packidx, int idx, struct got_object_id *id,
65 struct got_object_cache *objcache)
66 {
67 const struct got_error *err;
69 err = got_packfile_open_object(obj, pack, packidx, idx, id);
70 if (err)
71 return err;
72 (*obj)->refcnt++;
74 err = got_object_cache_add(objcache, id, *obj);
75 if (err) {
76 if (err->code == GOT_ERR_OBJ_EXISTS ||
77 err->code == GOT_ERR_OBJ_TOO_LARGE)
78 err = NULL;
79 return err;
80 }
81 (*obj)->refcnt++;
82 return NULL;
83 }
85 static const struct got_error *
86 object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
87 struct got_packidx *packidx, struct got_object_cache *objcache)
88 {
89 const struct got_error *err = NULL;
90 struct got_imsg_packed_object iobj;
91 struct got_object *obj;
92 struct got_object_id id;
93 size_t datalen;
95 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
96 if (datalen != sizeof(iobj))
97 return got_error(GOT_ERR_PRIVSEP_LEN);
98 memcpy(&iobj, imsg->data, sizeof(iobj));
99 memcpy(&id, &iobj.id, sizeof(id));
101 obj = got_object_cache_get(objcache, &id);
102 if (obj) {
103 obj->refcnt++;
104 } else {
105 err = open_object(&obj, pack, packidx, iobj.idx, &id,
106 objcache);
107 if (err)
108 goto done;
111 err = got_privsep_send_obj(ibuf, obj);
112 done:
113 got_object_close(obj);
114 return err;
117 static const struct got_error *
118 open_commit(struct got_commit_object **commit, struct got_pack *pack,
119 struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
120 struct got_object_cache *objcache)
122 const struct got_error *err = NULL;
123 struct got_object *obj = NULL;
124 uint8_t *buf = NULL;
125 size_t len;
127 *commit = NULL;
129 obj = got_object_cache_get(objcache, id);
130 if (obj) {
131 obj->refcnt++;
132 } else {
133 err = open_object(&obj, pack, packidx, obj_idx, id,
134 objcache);
135 if (err)
136 return err;
139 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
140 if (err)
141 goto done;
143 obj->size = len;
145 err = got_object_parse_commit(commit, buf, len);
146 done:
147 got_object_close(obj);
148 free(buf);
149 return err;
152 static const struct got_error *
153 commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
154 struct got_packidx *packidx, struct got_object_cache *objcache)
156 const struct got_error *err = NULL;
157 struct got_imsg_packed_object iobj;
158 struct got_commit_object *commit = NULL;
159 struct got_object_id id;
160 size_t datalen;
162 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
163 if (datalen != sizeof(iobj))
164 return got_error(GOT_ERR_PRIVSEP_LEN);
165 memcpy(&iobj, imsg->data, sizeof(iobj));
166 memcpy(&id, &iobj.id, sizeof(id));
168 err = open_commit(&commit, pack, packidx, iobj.idx, &id, objcache);
169 if (err)
170 goto done;
172 err = got_privsep_send_commit(ibuf, commit);
173 done:
174 if (commit)
175 got_object_commit_close(commit);
176 if (err) {
177 if (err->code == GOT_ERR_PRIVSEP_PIPE)
178 err = NULL;
179 else
180 got_privsep_send_error(ibuf, err);
183 return err;
186 static const struct got_error *
187 open_tree(uint8_t **buf, struct got_parsed_tree_entry **entries, size_t *nentries,
188 size_t *nentries_alloc, struct got_pack *pack, struct got_packidx *packidx,
189 int obj_idx, struct got_object_id *id, struct got_object_cache *objcache)
191 const struct got_error *err = NULL;
192 struct got_object *obj = NULL;
193 size_t len;
195 *buf = NULL;
196 *nentries = 0;
198 obj = got_object_cache_get(objcache, id);
199 if (obj) {
200 obj->refcnt++;
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 obj->size = len;
214 err = got_object_parse_tree(entries, nentries, nentries_alloc,
215 *buf, len);
216 done:
217 got_object_close(obj);
218 if (err) {
219 free(*buf);
220 *buf = NULL;
222 return err;
225 static const struct got_error *
226 tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
227 struct got_packidx *packidx, struct got_object_cache *objcache,
228 struct got_parsed_tree_entry **entries, size_t *nentries,
229 size_t *nentries_alloc)
231 const struct got_error *err = NULL;
232 struct got_imsg_packed_object iobj;
233 uint8_t *buf = NULL;
234 struct got_object_id id;
235 size_t datalen;
237 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
238 if (datalen != sizeof(iobj))
239 return got_error(GOT_ERR_PRIVSEP_LEN);
240 memcpy(&iobj, imsg->data, sizeof(iobj));
241 memcpy(&id, &iobj.id, sizeof(id));
243 err = open_tree(&buf, entries, nentries, nentries_alloc,
244 pack, packidx, iobj.idx, &id, objcache);
245 if (err)
246 return err;
248 err = got_privsep_send_tree(ibuf, *entries, *nentries);
249 free(buf);
250 if (err) {
251 if (err->code == GOT_ERR_PRIVSEP_PIPE)
252 err = NULL;
253 else
254 got_privsep_send_error(ibuf, err);
257 return err;
260 static const struct got_error *
261 receive_file(FILE **f, struct imsgbuf *ibuf, uint32_t imsg_code)
263 const struct got_error *err;
264 struct imsg imsg;
265 size_t datalen;
267 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
268 if (err)
269 return err;
271 if (imsg.hdr.type != imsg_code) {
272 err = got_error(GOT_ERR_PRIVSEP_MSG);
273 goto done;
276 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
277 if (datalen != 0) {
278 err = got_error(GOT_ERR_PRIVSEP_LEN);
279 goto done;
281 if (imsg.fd == -1) {
282 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
283 goto done;
286 *f = fdopen(imsg.fd, "w+");
287 if (*f == NULL) {
288 err = got_error_from_errno("fdopen");
289 close(imsg.fd);
290 goto done;
292 done:
293 imsg_free(&imsg);
294 return err;
297 static const struct got_error *
298 receive_tempfile(FILE **f, const char *mode, struct imsg *imsg,
299 struct imsgbuf *ibuf)
301 size_t datalen;
303 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
304 if (datalen != 0)
305 return got_error(GOT_ERR_PRIVSEP_LEN);
307 if (imsg->fd == -1)
308 return got_error(GOT_ERR_PRIVSEP_NO_FD);
310 *f = fdopen(imsg->fd, mode);
311 if (*f == NULL)
312 return got_error_from_errno("fdopen");
313 imsg->fd = -1;
315 return NULL;
318 static const struct got_error *
319 blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
320 struct got_packidx *packidx, struct got_object_cache *objcache,
321 FILE *basefile, FILE *accumfile)
323 const struct got_error *err = NULL;
324 struct got_imsg_packed_object iobj;
325 struct got_object *obj = NULL;
326 FILE *outfile = NULL;
327 struct got_object_id id;
328 size_t datalen;
329 uint64_t blob_size;
330 uint8_t *buf = NULL;
332 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
333 if (datalen != sizeof(iobj))
334 return got_error(GOT_ERR_PRIVSEP_LEN);
335 memcpy(&iobj, imsg->data, sizeof(iobj));
336 memcpy(&id, &iobj.id, sizeof(id));
338 obj = got_object_cache_get(objcache, &id);
339 if (obj) {
340 obj->refcnt++;
341 } else {
342 err = open_object(&obj, pack, packidx, iobj.idx, &id,
343 objcache);
344 if (err)
345 return err;
348 err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
349 if (err)
350 goto done;
352 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
353 err = got_pack_get_max_delta_object_size(&blob_size, obj, pack);
354 if (err)
355 goto done;
356 } else
357 blob_size = obj->size;
359 if (blob_size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
360 err = got_packfile_extract_object_to_mem(&buf, &obj->size,
361 obj, pack);
362 else
363 err = got_packfile_extract_object(pack, obj, outfile, basefile,
364 accumfile);
365 if (err)
366 goto done;
368 err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, buf);
369 done:
370 free(buf);
371 if (outfile && fclose(outfile) == EOF && err == NULL)
372 err = got_error_from_errno("fclose");
373 got_object_close(obj);
374 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
375 got_privsep_send_error(ibuf, err);
377 return err;
380 static const struct got_error *
381 tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
382 struct got_packidx *packidx, struct got_object_cache *objcache)
384 const struct got_error *err = NULL;
385 struct got_imsg_packed_object iobj;
386 struct got_object *obj = NULL;
387 struct got_tag_object *tag = NULL;
388 uint8_t *buf = NULL;
389 size_t len;
390 struct got_object_id id;
391 size_t datalen;
393 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
394 if (datalen != sizeof(iobj))
395 return got_error(GOT_ERR_PRIVSEP_LEN);
396 memcpy(&iobj, imsg->data, sizeof(iobj));
397 memcpy(&id, &iobj.id, sizeof(id));
399 obj = got_object_cache_get(objcache, &id);
400 if (obj) {
401 obj->refcnt++;
402 } else {
403 err = open_object(&obj, pack, packidx, iobj.idx, &id,
404 objcache);
405 if (err)
406 return err;
409 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
410 if (err)
411 goto done;
413 obj->size = len;
414 err = got_object_parse_tag(&tag, buf, len);
415 if (err)
416 goto done;
418 err = got_privsep_send_tag(ibuf, tag);
419 done:
420 free(buf);
421 got_object_close(obj);
422 if (tag)
423 got_object_tag_close(tag);
424 if (err) {
425 if (err->code == GOT_ERR_PRIVSEP_PIPE)
426 err = NULL;
427 else
428 got_privsep_send_error(ibuf, err);
431 return err;
434 static struct got_parsed_tree_entry *
435 find_entry_by_name(struct got_parsed_tree_entry *entries, int nentries,
436 const char *name, size_t len)
438 struct got_parsed_tree_entry *pte;
439 int cmp, i;
441 /* Note that tree entries are sorted in strncmp() order. */
442 for (i = 0; i < nentries; i++) {
443 pte = &entries[i];
444 cmp = strncmp(pte->name, name, len);
445 if (cmp < 0)
446 continue;
447 if (cmp > 0)
448 break;
449 if (pte->name[len] == '\0')
450 return pte;
452 return NULL;
455 static const struct got_error *
456 tree_path_changed(int *changed, uint8_t **buf1, uint8_t **buf2,
457 struct got_parsed_tree_entry **entries1, size_t *nentries1,
458 size_t *nentries_alloc1,
459 struct got_parsed_tree_entry **entries2, size_t *nentries2,
460 size_t *nentries_alloc2,
461 const char *path, struct got_pack *pack, struct got_packidx *packidx,
462 struct imsgbuf *ibuf, struct got_object_cache *objcache)
464 const struct got_error *err = NULL;
465 struct got_parsed_tree_entry *pte1 = NULL, *pte2 = NULL;
466 const char *seg, *s;
467 size_t seglen;
469 *changed = 0;
471 /* We not do support comparing the root path. */
472 if (got_path_is_root_dir(path))
473 return got_error_path(path, GOT_ERR_BAD_PATH);
475 s = path;
476 while (*s == '/')
477 s++;
478 seg = s;
479 seglen = 0;
480 while (*s) {
481 if (*s != '/') {
482 s++;
483 seglen++;
484 if (*s)
485 continue;
488 pte1 = find_entry_by_name(*entries1, *nentries1, seg, seglen);
489 if (pte1 == NULL) {
490 err = got_error(GOT_ERR_NO_OBJ);
491 break;
494 pte2 = find_entry_by_name(*entries2, *nentries2, seg, seglen);
495 if (pte2 == NULL) {
496 *changed = 1;
497 break;
500 if (pte1->mode != pte2->mode) {
501 *changed = 1;
502 break;
505 if (memcmp(pte1->id, pte2->id, SHA1_DIGEST_LENGTH) == 0) {
506 *changed = 0;
507 break;
510 if (*s == '\0') { /* final path element */
511 *changed = 1;
512 break;
515 seg = s + 1;
516 s++;
517 seglen = 0;
518 if (*s) {
519 struct got_object_id id1, id2;
520 int idx;
522 memcpy(id1.sha1, pte1->id, SHA1_DIGEST_LENGTH);
523 idx = got_packidx_get_object_idx(packidx, &id1);
524 if (idx == -1) {
525 err = got_error_no_obj(&id1);
526 break;
528 *nentries1 = 0;
529 free(*buf1);
530 *buf1 = NULL;
531 err = open_tree(buf1, entries1, nentries1,
532 nentries_alloc1, pack, packidx, idx, &id1,
533 objcache);
534 pte1 = NULL;
535 if (err)
536 break;
538 memcpy(id2.sha1, pte2->id, SHA1_DIGEST_LENGTH);
539 idx = got_packidx_get_object_idx(packidx, &id2);
540 if (idx == -1) {
541 err = got_error_no_obj(&id2);
542 break;
544 *nentries2 = 0;
545 free(*buf2);
546 *buf2 = NULL;
547 err = open_tree(buf2, entries2, nentries2,
548 nentries_alloc2, pack, packidx, idx, &id2,
549 objcache);
550 pte2 = NULL;
551 if (err)
552 break;
556 return err;
559 static const struct got_error *
560 send_traversed_commits(struct got_object_id *commit_ids, size_t ncommits,
561 struct imsgbuf *ibuf)
563 struct ibuf *wbuf;
564 size_t i;
566 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
567 sizeof(struct got_imsg_traversed_commits) +
568 ncommits * sizeof(commit_ids[0]));
569 if (wbuf == NULL)
570 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
572 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1)
573 return got_error_from_errno("imsg_add TRAVERSED_COMMITS");
575 for (i = 0; i < ncommits; i++) {
576 struct got_object_id *id = &commit_ids[i];
577 if (imsg_add(wbuf, id, sizeof(*id)) == -1) {
578 return got_error_from_errno(
579 "imsg_add TRAVERSED_COMMITS");
583 wbuf->fd = -1;
584 imsg_close(ibuf, wbuf);
586 return got_privsep_flush_imsg(ibuf);
589 static const struct got_error *
590 send_commit_traversal_done(struct imsgbuf *ibuf)
592 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
593 NULL, 0) == -1)
594 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
596 return got_privsep_flush_imsg(ibuf);
599 static const struct got_error *
600 commit_traversal_request(struct imsg *imsg, struct imsgbuf *ibuf,
601 struct got_pack *pack, struct got_packidx *packidx,
602 struct got_object_cache *objcache)
604 const struct got_error *err = NULL;
605 struct got_imsg_commit_traversal_request ctreq;
606 struct got_object_qid *pid;
607 struct got_commit_object *commit = NULL, *pcommit = NULL;
608 struct got_parsed_tree_entry *entries = NULL, *pentries = NULL;
609 size_t nentries = 0, nentries_alloc = 0;
610 size_t pnentries = 0, pnentries_alloc = 0;
611 struct got_object_id id;
612 size_t datalen;
613 char *path = NULL;
614 const int min_alloc = 64;
615 int changed = 0, ncommits = 0, nallocated = 0;
616 struct got_object_id *commit_ids = NULL;
618 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
619 if (datalen < sizeof(ctreq))
620 return got_error(GOT_ERR_PRIVSEP_LEN);
621 memcpy(&ctreq, imsg->data, sizeof(ctreq));
622 memcpy(&id, &ctreq.iobj.id, sizeof(id));
624 if (datalen != sizeof(ctreq) + ctreq.path_len)
625 return got_error(GOT_ERR_PRIVSEP_LEN);
626 if (ctreq.path_len == 0)
627 return got_error(GOT_ERR_PRIVSEP_LEN);
629 path = strndup(imsg->data + sizeof(ctreq), ctreq.path_len);
630 if (path == NULL)
631 return got_error_from_errno("strndup");
633 nallocated = min_alloc;
634 commit_ids = reallocarray(NULL, nallocated, sizeof(*commit_ids));
635 if (commit_ids == NULL)
636 return got_error_from_errno("reallocarray");
638 do {
639 const size_t max_datalen = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
640 int idx;
642 if (sigint_received) {
643 err = got_error(GOT_ERR_CANCELLED);
644 goto done;
647 if (commit == NULL) {
648 idx = got_packidx_get_object_idx(packidx, &id);
649 if (idx == -1)
650 break;
651 err = open_commit(&commit, pack, packidx,
652 idx, &id, objcache);
653 if (err) {
654 if (err->code != GOT_ERR_NO_OBJ)
655 goto done;
656 err = NULL;
657 break;
661 if (sizeof(struct got_imsg_traversed_commits) +
662 ncommits * sizeof(commit_ids[0]) >= max_datalen) {
663 err = send_traversed_commits(commit_ids, ncommits,
664 ibuf);
665 if (err)
666 goto done;
667 ncommits = 0;
669 ncommits++;
670 if (ncommits > nallocated) {
671 struct got_object_id *new;
672 nallocated += min_alloc;
673 new = reallocarray(commit_ids, nallocated,
674 sizeof(*commit_ids));
675 if (new == NULL) {
676 err = got_error_from_errno("reallocarray");
677 goto done;
679 commit_ids = new;
681 memcpy(&commit_ids[ncommits - 1], &id, sizeof(id));
683 pid = STAILQ_FIRST(&commit->parent_ids);
684 if (pid == NULL)
685 break;
687 idx = got_packidx_get_object_idx(packidx, &pid->id);
688 if (idx == -1)
689 break;
691 err = open_commit(&pcommit, pack, packidx, idx, &pid->id,
692 objcache);
693 if (err) {
694 if (err->code != GOT_ERR_NO_OBJ)
695 goto done;
696 err = NULL;
697 break;
700 if (path[0] == '/' && path[1] == '\0') {
701 if (got_object_id_cmp(pcommit->tree_id,
702 commit->tree_id) != 0) {
703 changed = 1;
704 break;
706 } else {
707 int pidx;
708 uint8_t *buf = NULL, *pbuf = NULL;
710 idx = got_packidx_get_object_idx(packidx,
711 commit->tree_id);
712 if (idx == -1)
713 break;
714 pidx = got_packidx_get_object_idx(packidx,
715 pcommit->tree_id);
716 if (pidx == -1)
717 break;
719 err = open_tree(&buf, &entries, &nentries,
720 &nentries_alloc, pack, packidx, idx,
721 commit->tree_id, objcache);
722 if (err)
723 goto done;
724 err = open_tree(&pbuf, &pentries, &pnentries,
725 &pnentries_alloc, pack, packidx, pidx,
726 pcommit->tree_id, objcache);
727 if (err) {
728 free(buf);
729 goto done;
732 err = tree_path_changed(&changed, &buf, &pbuf,
733 &entries, &nentries, &nentries_alloc,
734 &pentries, &pnentries, &pnentries_alloc,
735 path, pack, packidx, ibuf, objcache);
737 nentries = 0;
738 free(buf);
739 pnentries = 0;
740 free(pbuf);
741 if (err) {
742 if (err->code != GOT_ERR_NO_OBJ)
743 goto done;
744 err = NULL;
745 break;
749 if (!changed) {
750 memcpy(&id, &pid->id, sizeof(id));
751 got_object_commit_close(commit);
752 commit = pcommit;
753 pcommit = NULL;
755 } while (!changed);
757 if (ncommits > 0) {
758 err = send_traversed_commits(commit_ids, ncommits, ibuf);
759 if (err)
760 goto done;
762 if (changed) {
763 err = got_privsep_send_commit(ibuf, commit);
764 if (err)
765 goto done;
768 err = send_commit_traversal_done(ibuf);
769 done:
770 free(path);
771 free(commit_ids);
772 if (commit)
773 got_object_commit_close(commit);
774 if (pcommit)
775 got_object_commit_close(pcommit);
776 free(entries);
777 free(pentries);
778 if (err) {
779 if (err->code == GOT_ERR_PRIVSEP_PIPE)
780 err = NULL;
781 else
782 got_privsep_send_error(ibuf, err);
785 return err;
788 static const struct got_error *
789 raw_object_request(struct imsg *imsg, struct imsgbuf *ibuf,
790 struct got_pack *pack, struct got_packidx *packidx,
791 struct got_object_cache *objcache, FILE *basefile, FILE *accumfile)
793 const struct got_error *err = NULL;
794 uint8_t *buf = NULL;
795 uint64_t size = 0;
796 FILE *outfile = NULL;
797 struct got_imsg_packed_object iobj;
798 struct got_object *obj;
799 struct got_object_id id;
800 size_t datalen;
802 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
803 if (datalen != sizeof(iobj))
804 return got_error(GOT_ERR_PRIVSEP_LEN);
805 memcpy(&iobj, imsg->data, sizeof(iobj));
806 memcpy(&id, &iobj.id, sizeof(id));
808 obj = got_object_cache_get(objcache, &id);
809 if (obj) {
810 obj->refcnt++;
811 } else {
812 err = open_object(&obj, pack, packidx, iobj.idx, &id,
813 objcache);
814 if (err)
815 return err;
818 err = receive_file(&outfile, ibuf, GOT_IMSG_RAW_OBJECT_OUTFD);
819 if (err)
820 return err;
822 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
823 err = got_pack_get_max_delta_object_size(&size, obj, pack);
824 if (err)
825 goto done;
826 } else
827 size = obj->size;
829 if (size <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
830 err = got_packfile_extract_object_to_mem(&buf, &obj->size,
831 obj, pack);
832 else
833 err = got_packfile_extract_object(pack, obj, outfile, basefile,
834 accumfile);
835 if (err)
836 goto done;
838 err = got_privsep_send_raw_obj(ibuf, obj->size, obj->hdrlen, buf);
839 done:
840 free(buf);
841 if (outfile && fclose(outfile) == EOF && err == NULL)
842 err = got_error_from_errno("fclose");
843 got_object_close(obj);
844 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
845 got_privsep_send_error(ibuf, err);
847 return err;
850 static const struct got_error *
851 get_base_object_id(struct got_object_id *base_id, struct got_packidx *packidx,
852 off_t base_offset)
854 const struct got_error *err;
855 int idx;
857 err = got_packidx_get_offset_idx(&idx, packidx, base_offset);
858 if (err)
859 return err;
860 if (idx == -1)
861 return got_error(GOT_ERR_BAD_PACKIDX);
863 return got_packidx_get_object_id(base_id, packidx, idx);
866 static const struct got_error *
867 raw_delta_request(struct imsg *imsg, struct imsgbuf *ibuf,
868 FILE *delta_outfile, struct got_pack *pack,
869 struct got_packidx *packidx)
871 const struct got_error *err = NULL;
872 struct got_imsg_raw_delta_request req;
873 size_t datalen, delta_size, delta_compressed_size;
874 off_t delta_offset, delta_data_offset;
875 uint8_t *delta_buf = NULL;
876 struct got_object_id id, base_id;
877 off_t base_offset, delta_out_offset = 0;
878 uint64_t base_size = 0, result_size = 0;
879 size_t w;
881 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
882 if (datalen != sizeof(req))
883 return got_error(GOT_ERR_PRIVSEP_LEN);
884 memcpy(&req, imsg->data, sizeof(req));
885 memcpy(&id, &req.id, sizeof(id));
887 imsg->fd = -1;
889 err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
890 &delta_compressed_size, &delta_offset, &delta_data_offset,
891 &base_offset, &base_id, &base_size, &result_size,
892 pack, packidx, req.idx);
893 if (err)
894 goto done;
896 /*
897 * If this is an offset delta we must determine the base
898 * object ID ourselves.
899 */
900 if (base_offset != 0) {
901 err = get_base_object_id(&base_id, packidx, base_offset);
902 if (err)
903 goto done;
906 delta_out_offset = ftello(delta_outfile);
907 w = fwrite(delta_buf, 1, delta_compressed_size, delta_outfile);
908 if (w != delta_compressed_size) {
909 err = got_ferror(delta_outfile, GOT_ERR_IO);
910 goto done;
912 if (fflush(delta_outfile) == -1) {
913 err = got_error_from_errno("fflush");
914 goto done;
917 err = got_privsep_send_raw_delta(ibuf, base_size, result_size,
918 delta_size, delta_compressed_size, delta_offset, delta_out_offset,
919 &base_id);
920 done:
921 free(delta_buf);
922 return err;
925 struct search_deltas_arg {
926 struct imsgbuf *ibuf;
927 struct got_packidx *packidx;
928 struct got_pack *pack;
929 struct got_object_idset *idset;
930 struct got_imsg_reused_delta deltas[GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS];
931 size_t ndeltas;
932 };
934 static const struct got_error *
935 search_delta_for_object(struct got_object_id *id, void *data, void *arg)
937 const struct got_error *err;
938 struct search_deltas_arg *a = arg;
939 int obj_idx;
940 uint8_t *delta_buf = NULL;
941 uint64_t base_size, result_size;
942 size_t delta_size, delta_compressed_size;
943 off_t delta_offset, delta_data_offset, base_offset;
944 struct got_object_id base_id;
946 if (sigint_received)
947 return got_error(GOT_ERR_CANCELLED);
949 obj_idx = got_packidx_get_object_idx(a->packidx, id);
950 if (obj_idx == -1)
951 return NULL; /* object not present in our pack file */
953 err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
954 &delta_compressed_size, &delta_offset, &delta_data_offset,
955 &base_offset, &base_id, &base_size, &result_size,
956 a->pack, a->packidx, obj_idx);
957 if (err) {
958 if (err->code == GOT_ERR_OBJ_TYPE)
959 return NULL; /* object not stored as a delta */
960 return err;
963 /*
964 * If this is an offset delta we must determine the base
965 * object ID ourselves.
966 */
967 if (base_offset != 0) {
968 err = get_base_object_id(&base_id, a->packidx, base_offset);
969 if (err)
970 goto done;
973 if (got_object_idset_contains(a->idset, &base_id)) {
974 struct got_imsg_reused_delta *delta;
976 delta = &a->deltas[a->ndeltas++];
977 memcpy(&delta->id, id, sizeof(delta->id));
978 memcpy(&delta->base_id, &base_id, sizeof(delta->base_id));
979 delta->base_size = base_size;
980 delta->result_size = result_size;
981 delta->delta_size = delta_size;
982 delta->delta_compressed_size = delta_compressed_size;
983 delta->delta_offset = delta_data_offset;
985 if (a->ndeltas >= GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS) {
986 err = got_privsep_send_reused_deltas(a->ibuf,
987 a->deltas, a->ndeltas);
988 if (err)
989 goto done;
990 a->ndeltas = 0;
993 done:
994 free(delta_buf);
995 return err;
998 static const struct got_error *
999 recv_object_ids(struct got_object_idset *idset, struct imsgbuf *ibuf)
1001 const struct got_error *err = NULL;
1002 int done = 0;
1003 struct got_object_id *ids;
1004 size_t nids, i;
1006 for (;;) {
1007 err = got_privsep_recv_object_idlist(&done, &ids, &nids, ibuf);
1008 if (err || done)
1009 break;
1010 for (i = 0; i < nids; i++) {
1011 err = got_object_idset_add(idset, &ids[i], NULL);
1012 if (err) {
1013 free(ids);
1014 return err;
1017 free(ids);
1020 return err;
1023 static const struct got_error *
1024 recv_object_id_queue(struct got_object_id_queue *queue,
1025 struct got_object_idset *queued_ids, struct imsgbuf *ibuf)
1027 const struct got_error *err = NULL;
1028 int done = 0;
1029 struct got_object_qid *qid;
1030 struct got_object_id *ids;
1031 size_t nids, i;
1033 for (;;) {
1034 err = got_privsep_recv_object_idlist(&done, &ids, &nids, ibuf);
1035 if (err || done)
1036 break;
1037 for (i = 0; i < nids; i++) {
1038 err = got_object_qid_alloc_partial(&qid);
1039 if (err)
1040 return err;
1041 memcpy(&qid->id, &ids[i], sizeof(qid->id));
1042 STAILQ_INSERT_TAIL(queue, qid, entry);
1043 err = got_object_idset_add(queued_ids, &qid->id, NULL);
1044 if (err)
1045 return err;
1049 return err;
1052 static const struct got_error *
1053 delta_reuse_request(struct imsg *imsg, struct imsgbuf *ibuf,
1054 struct got_pack *pack, struct got_packidx *packidx)
1056 const struct got_error *err = NULL;
1057 struct got_object_idset *idset;
1058 struct search_deltas_arg sda;
1060 idset = got_object_idset_alloc();
1061 if (idset == NULL)
1062 return got_error_from_errno("got_object_idset_alloc");
1064 err = recv_object_ids(idset, ibuf);
1065 if (err)
1066 return err;
1068 memset(&sda, 0, sizeof(sda));
1069 sda.ibuf = ibuf;
1070 sda.idset = idset;
1071 sda.pack = pack;
1072 sda.packidx = packidx;
1073 err = got_object_idset_for_each(idset, search_delta_for_object, &sda);
1074 if (err)
1075 goto done;
1077 if (sda.ndeltas > 0) {
1078 err = got_privsep_send_reused_deltas(ibuf, sda.deltas,
1079 sda.ndeltas);
1080 if (err)
1081 goto done;
1084 err = got_privsep_send_reused_deltas_done(ibuf);
1085 done:
1086 got_object_idset_free(idset);
1087 return err;
1090 static const struct got_error *
1091 receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
1093 const struct got_error *err = NULL;
1094 struct imsg imsg;
1095 struct got_imsg_packidx ipackidx;
1096 size_t datalen;
1097 struct got_packidx *p;
1099 *packidx = NULL;
1101 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1102 if (err)
1103 return err;
1105 p = calloc(1, sizeof(*p));
1106 if (p == NULL) {
1107 err = got_error_from_errno("calloc");
1108 goto done;
1111 if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
1112 err = got_error(GOT_ERR_PRIVSEP_MSG);
1113 goto done;
1116 if (imsg.fd == -1) {
1117 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1118 goto done;
1121 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1122 if (datalen != sizeof(ipackidx)) {
1123 err = got_error(GOT_ERR_PRIVSEP_LEN);
1124 goto done;
1126 memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
1128 p->len = ipackidx.len;
1129 p->fd = dup(imsg.fd);
1130 if (p->fd == -1) {
1131 err = got_error_from_errno("dup");
1132 goto done;
1134 if (lseek(p->fd, 0, SEEK_SET) == -1) {
1135 err = got_error_from_errno("lseek");
1136 goto done;
1139 #ifndef GOT_PACK_NO_MMAP
1140 if (p->len > 0 && p->len <= SIZE_MAX) {
1141 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
1142 if (p->map == MAP_FAILED)
1143 p->map = NULL; /* fall back to read(2) */
1145 #endif
1146 err = got_packidx_init_hdr(p, 1, ipackidx.packfile_size);
1147 done:
1148 if (err) {
1149 if (imsg.fd != -1)
1150 close(imsg.fd);
1151 got_packidx_close(p);
1152 } else
1153 *packidx = p;
1154 imsg_free(&imsg);
1155 return err;
1158 static const struct got_error *
1159 send_tree_enumeration_done(struct imsgbuf *ibuf)
1161 if (imsg_compose(ibuf, GOT_IMSG_TREE_ENUMERATION_DONE, 0, 0, -1,
1162 NULL, 0) == -1)
1163 return got_error_from_errno("imsg_compose TREE_ENUMERATION_DONE");
1165 return got_privsep_flush_imsg(ibuf);
1168 struct enumerated_tree {
1169 struct got_object_id id;
1170 char *path;
1171 uint8_t *buf;
1172 struct got_parsed_tree_entry *entries;
1173 int nentries;
1176 static const struct got_error *
1177 enumerate_tree(int *have_all_entries, struct imsgbuf *ibuf, size_t *totlen,
1178 struct got_object_id *tree_id,
1179 const char *path, struct got_pack *pack, struct got_packidx *packidx,
1180 struct got_object_cache *objcache, struct got_object_idset *idset,
1181 struct enumerated_tree **trees, size_t *nalloc, size_t *ntrees)
1183 const struct got_error *err = NULL;
1184 struct got_object_id_queue ids;
1185 struct got_object_qid *qid;
1186 uint8_t *buf = NULL;
1187 struct got_parsed_tree_entry *entries = NULL;
1188 size_t nentries = 0, nentries_alloc = 0, i;
1189 struct enumerated_tree *tree;
1191 *ntrees = 0;
1192 *have_all_entries = 1;
1193 STAILQ_INIT(&ids);
1195 err = got_object_qid_alloc_partial(&qid);
1196 if (err)
1197 return err;
1198 memcpy(&qid->id, tree_id, sizeof(*tree_id));
1199 qid->data = strdup(path);
1200 if (qid->data == NULL) {
1201 err = got_error_from_errno("strdup");
1202 goto done;
1204 STAILQ_INSERT_TAIL(&ids, qid, entry);
1205 qid = NULL;
1207 /* Traverse the tree hierarchy, gather tree object IDs and paths. */
1208 do {
1209 const char *path;
1210 int idx, i;
1212 if (sigint_received) {
1213 err = got_error(GOT_ERR_CANCELLED);
1214 goto done;
1217 qid = STAILQ_FIRST(&ids);
1218 STAILQ_REMOVE_HEAD(&ids, entry);
1219 path = qid->data;
1221 idx = got_packidx_get_object_idx(packidx, &qid->id);
1222 if (idx == -1) {
1223 *have_all_entries = 0;
1224 break;
1227 err = open_tree(&buf, &entries, &nentries, &nentries_alloc,
1228 pack, packidx, idx, &qid->id, objcache);
1229 if (err) {
1230 if (err->code != GOT_ERR_NO_OBJ)
1231 goto done;
1234 err = got_object_idset_add(idset, &qid->id, NULL);
1235 if (err)
1236 goto done;
1238 for (i = 0; i < nentries; i++) {
1239 struct got_object_qid *eqid = NULL;
1240 struct got_parsed_tree_entry *pte = &entries[i];
1241 char *p;
1243 if (!S_ISDIR(pte->mode))
1244 continue;
1246 err = got_object_qid_alloc_partial(&eqid);
1247 if (err)
1248 goto done;
1249 memcpy(eqid->id.sha1, pte->id, sizeof(eqid->id.sha1));
1251 if (got_object_idset_contains(idset, &eqid->id)) {
1252 got_object_qid_free(eqid);
1253 continue;
1256 if (asprintf(&p, "%s%s%s", path,
1257 got_path_is_root_dir(path) ? "" : "/",
1258 pte->name) == -1) {
1259 err = got_error_from_errno("asprintf");
1260 got_object_qid_free(eqid);
1261 goto done;
1263 eqid->data = p;
1264 STAILQ_INSERT_TAIL(&ids, eqid, entry);
1267 if (*ntrees >= *nalloc) {
1268 struct enumerated_tree *new;
1269 new = recallocarray(*trees, *nalloc, *nalloc + 16,
1270 sizeof(*new));
1271 if (new == NULL) {
1272 err = got_error_from_errno("malloc");
1273 goto done;
1275 *trees = new;
1276 *nalloc += 16;
1278 tree = &(*trees)[*ntrees];
1279 (*ntrees)++;
1280 memcpy(&tree->id, &qid->id, sizeof(tree->id));
1281 tree->path = qid->data;
1282 tree->buf = buf;
1283 buf = NULL;
1284 tree->entries = entries;
1285 entries = NULL;
1286 nentries_alloc = 0;
1287 tree->nentries = nentries;
1288 nentries = 0;
1290 got_object_qid_free(qid);
1291 qid = NULL;
1292 } while (!STAILQ_EMPTY(&ids));
1294 if (*have_all_entries) {
1295 int i;
1297 * We have managed to traverse all entries in the hierarchy.
1298 * Tell the main process what we have found.
1300 for (i = 0; i < *ntrees; i++) {
1301 tree = &(*trees)[i];
1302 err = got_privsep_send_enumerated_tree(totlen,
1303 ibuf, &tree->id, tree->path, tree->entries,
1304 tree->nentries);
1305 if (err)
1306 goto done;
1307 free(tree->buf);
1308 tree->buf = NULL;
1309 free(tree->path);
1310 tree->path = NULL;
1311 free(tree->entries);
1312 tree->entries = NULL;
1314 *ntrees = 0; /* don't loop again below to free memory */
1316 err = send_tree_enumeration_done(ibuf);
1317 } else {
1319 * We can only load fully packed tree hierarchies on
1320 * behalf of the main process, otherwise the main process
1321 * gets a wrong idea about which tree objects have
1322 * already been traversed.
1323 * Indicate a missing entry for the root of this tree.
1324 * The main process should continue by loading this
1325 * entire tree the slow way.
1327 err = got_privsep_send_enumerated_tree(totlen, ibuf,
1328 tree_id, "/", NULL, -1);
1329 if (err)
1330 goto done;
1332 done:
1333 free(buf);
1334 free(entries);
1335 for (i = 0; i < *ntrees; i++) {
1336 tree = &(*trees)[i];
1337 free(tree->buf);
1338 tree->buf = NULL;
1339 free(tree->path);
1340 tree->path = NULL;
1341 free(tree->entries);
1342 tree->entries = NULL;
1344 if (qid)
1345 free(qid->data);
1346 got_object_qid_free(qid);
1347 got_object_id_queue_free(&ids);
1348 if (err) {
1349 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1350 err = NULL;
1351 else
1352 got_privsep_send_error(ibuf, err);
1355 return err;
1358 static const struct got_error *
1359 enumeration_request(struct imsg *imsg, struct imsgbuf *ibuf,
1360 struct got_pack *pack, struct got_packidx *packidx,
1361 struct got_object_cache *objcache)
1363 const struct got_error *err = NULL;
1364 struct got_object_id_queue commit_ids;
1365 const struct got_object_id_queue *parents = NULL;
1366 struct got_object_qid *qid = NULL;
1367 struct got_object *obj = NULL;
1368 struct got_commit_object *commit = NULL;
1369 struct got_object_id *tree_id = NULL;
1370 size_t totlen = 0;
1371 struct got_object_idset *idset, *queued_ids = NULL;
1372 int i, idx, have_all_entries = 1;
1373 struct enumerated_tree *trees = NULL;
1374 size_t ntrees = 0, nalloc = 16;
1376 STAILQ_INIT(&commit_ids);
1378 trees = calloc(nalloc, sizeof(*trees));
1379 if (trees == NULL)
1380 return got_error_from_errno("calloc");
1382 idset = got_object_idset_alloc();
1383 if (idset == NULL) {
1384 err = got_error_from_errno("got_object_idset_alloc");
1385 goto done;
1388 queued_ids = got_object_idset_alloc();
1389 if (queued_ids == NULL) {
1390 err = got_error_from_errno("got_object_idset_alloc");
1391 goto done;
1394 err = recv_object_id_queue(&commit_ids, queued_ids, ibuf);
1395 if (err)
1396 goto done;
1398 if (STAILQ_EMPTY(&commit_ids)) {
1399 err = got_error(GOT_ERR_PRIVSEP_MSG);
1400 goto done;
1403 err = recv_object_ids(idset, ibuf);
1404 if (err)
1405 goto done;
1407 while (!STAILQ_EMPTY(&commit_ids)) {
1408 if (sigint_received) {
1409 err = got_error(GOT_ERR_CANCELLED);
1410 goto done;
1413 qid = STAILQ_FIRST(&commit_ids);
1414 STAILQ_REMOVE_HEAD(&commit_ids, entry);
1416 if (got_object_idset_contains(idset, &qid->id)) {
1417 got_object_qid_free(qid);
1418 qid = NULL;
1419 continue;
1422 idx = got_packidx_get_object_idx(packidx, &qid->id);
1423 if (idx == -1) {
1424 have_all_entries = 0;
1425 break;
1428 err = open_object(&obj, pack, packidx, idx, &qid->id,
1429 objcache);
1430 if (err)
1431 goto done;
1432 if (obj->type == GOT_OBJ_TYPE_TAG) {
1433 struct got_tag_object *tag;
1434 uint8_t *buf;
1435 size_t len;
1436 err = got_packfile_extract_object_to_mem(&buf,
1437 &len, obj, pack);
1438 if (err)
1439 goto done;
1440 obj->size = len;
1441 err = got_object_parse_tag(&tag, buf, len);
1442 if (err) {
1443 free(buf);
1444 goto done;
1446 idx = got_packidx_get_object_idx(packidx, &tag->id);
1447 if (idx == -1) {
1448 have_all_entries = 0;
1449 break;
1451 err = open_commit(&commit, pack, packidx, idx,
1452 &tag->id, objcache);
1453 got_object_tag_close(tag);
1454 free(buf);
1455 if (err)
1456 goto done;
1457 } else if (obj->type == GOT_OBJ_TYPE_COMMIT) {
1458 err = open_commit(&commit, pack, packidx, idx,
1459 &qid->id, objcache);
1460 if (err)
1461 goto done;
1462 } else {
1463 err = got_error(GOT_ERR_OBJ_TYPE);
1464 goto done;
1466 got_object_close(obj);
1467 obj = NULL;
1469 err = got_privsep_send_enumerated_commit(ibuf, &qid->id,
1470 got_object_commit_get_committer_time(commit));
1471 if (err)
1472 goto done;
1474 tree_id = got_object_commit_get_tree_id(commit);
1475 idx = got_packidx_get_object_idx(packidx, tree_id);
1476 if (idx == -1) {
1477 have_all_entries = 0;
1478 err = got_privsep_send_enumerated_tree(&totlen, ibuf,
1479 tree_id, "/", NULL, -1);
1480 if (err)
1481 goto done;
1482 break;
1485 if (got_object_idset_contains(idset, tree_id)) {
1486 got_object_qid_free(qid);
1487 qid = NULL;
1488 err = send_tree_enumeration_done(ibuf);
1489 if (err)
1490 goto done;
1491 continue;
1494 err = enumerate_tree(&have_all_entries, ibuf, &totlen,
1495 tree_id, "/", pack, packidx, objcache, idset,
1496 &trees, &nalloc, &ntrees);
1497 if (err)
1498 goto done;
1500 if (!have_all_entries)
1501 break;
1503 got_object_qid_free(qid);
1504 qid = NULL;
1506 parents = got_object_commit_get_parent_ids(commit);
1507 if (parents) {
1508 struct got_object_qid *pid;
1509 STAILQ_FOREACH(pid, parents, entry) {
1510 if (got_object_idset_contains(idset, &pid->id))
1511 continue;
1512 if (got_object_idset_contains(queued_ids, &pid->id))
1513 continue;
1514 err = got_object_qid_alloc_partial(&qid);
1515 if (err)
1516 goto done;
1517 memcpy(&qid->id, &pid->id, sizeof(qid->id));
1518 STAILQ_INSERT_TAIL(&commit_ids, qid, entry);
1519 qid = NULL;
1523 got_object_commit_close(commit);
1524 commit = NULL;
1527 if (have_all_entries) {
1528 err = got_privsep_send_object_enumeration_done(ibuf);
1529 if (err)
1530 goto done;
1531 } else {
1532 err = got_privsep_send_object_enumeration_incomplete(ibuf);
1533 if (err)
1534 goto done;
1536 done:
1537 if (obj)
1538 got_object_close(obj);
1539 if (commit)
1540 got_object_commit_close(commit);
1541 got_object_qid_free(qid);
1542 got_object_id_queue_free(&commit_ids);
1543 if (idset)
1544 got_object_idset_free(idset);
1545 if (queued_ids)
1546 got_object_idset_free(queued_ids);
1547 for (i = 0; i < ntrees; i++) {
1548 struct enumerated_tree *tree = &trees[i];
1549 free(tree->buf);
1550 free(tree->path);
1551 free(tree->entries);
1553 free(trees);
1554 return err;
1557 enum findtwixt_color {
1558 COLOR_KEEP = 0,
1559 COLOR_DROP,
1560 COLOR_SKIP,
1561 COLOR_MAX,
1564 static const struct got_error *
1565 paint_commit(struct got_object_qid *qid, intptr_t color)
1567 if (color < 0 || color >= COLOR_MAX)
1568 return got_error(GOT_ERR_RANGE);
1570 qid->data = (void *)color;
1571 return NULL;
1574 static const struct got_error *
1575 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1576 intptr_t color)
1578 const struct got_error *err;
1579 struct got_object_qid *qid;
1581 err = got_object_qid_alloc_partial(&qid);
1582 if (err)
1583 return err;
1585 memcpy(&qid->id, id, sizeof(qid->id));
1586 STAILQ_INSERT_TAIL(ids, qid, entry);
1587 return paint_commit(qid, color);
1590 static const struct got_error *
1591 paint_commits(struct got_object_id_queue *ids, int *nids,
1592 struct got_object_idset *keep, struct got_object_idset *drop,
1593 struct got_object_idset *skip, struct got_pack *pack,
1594 struct got_packidx *packidx, struct imsgbuf *ibuf,
1595 struct got_object_cache *objcache)
1597 const struct got_error *err = NULL;
1598 struct got_commit_object *commit = NULL;
1599 struct got_object_id_queue painted;
1600 const struct got_object_id_queue *parents;
1601 struct got_object_qid *qid = NULL;
1602 int nqueued = *nids, nskip = 0, npainted = 0;
1604 STAILQ_INIT(&painted);
1606 while (!STAILQ_EMPTY(ids) && nskip != nqueued) {
1607 int idx;
1608 intptr_t color;
1610 if (sigint_received) {
1611 err = got_error(GOT_ERR_CANCELLED);
1612 goto done;
1615 qid = STAILQ_FIRST(ids);
1616 idx = got_packidx_get_object_idx(packidx, &qid->id);
1617 if (idx == -1) {
1618 qid = NULL;
1619 break;
1622 STAILQ_REMOVE_HEAD(ids, entry);
1623 nqueued--;
1624 color = (intptr_t)qid->data;
1625 if (color == COLOR_SKIP)
1626 nskip--;
1628 if (got_object_idset_contains(skip, &qid->id)) {
1629 got_object_qid_free(qid);
1630 qid = NULL;
1631 continue;
1634 switch (color) {
1635 case COLOR_KEEP:
1636 if (got_object_idset_contains(keep, &qid->id)) {
1637 got_object_qid_free(qid);
1638 qid = NULL;
1639 continue;
1641 if (got_object_idset_contains(drop, &qid->id)) {
1642 err = paint_commit(qid, COLOR_SKIP);
1643 if (err)
1644 goto done;
1646 err = got_object_idset_add(keep, &qid->id, NULL);
1647 if (err)
1648 goto done;
1649 break;
1650 case COLOR_DROP:
1651 if (got_object_idset_contains(drop, &qid->id)) {
1652 got_object_qid_free(qid);
1653 qid = NULL;
1654 continue;
1656 if (got_object_idset_contains(keep, &qid->id)) {
1657 err = paint_commit(qid, COLOR_SKIP);
1658 if (err)
1659 goto done;
1661 err = got_object_idset_add(drop, &qid->id, NULL);
1662 if (err)
1663 goto done;
1664 break;
1665 case COLOR_SKIP:
1666 if (!got_object_idset_contains(skip, &qid->id)) {
1667 err = got_object_idset_add(skip, &qid->id,
1668 NULL);
1669 if (err)
1670 goto done;
1672 break;
1673 default:
1674 /* should not happen */
1675 err = got_error_fmt(GOT_ERR_NOT_IMPL,
1676 "%s invalid commit color %"PRIdPTR, __func__,
1677 color);
1678 goto done;
1681 err = open_commit(&commit, pack, packidx, idx, &qid->id,
1682 objcache);
1683 if (err)
1684 goto done;
1686 parents = got_object_commit_get_parent_ids(commit);
1687 if (parents) {
1688 struct got_object_qid *pid;
1689 color = (intptr_t)qid->data;
1690 STAILQ_FOREACH(pid, parents, entry) {
1691 err = queue_commit_id(ids, &pid->id, color);
1692 if (err)
1693 goto done;
1694 nqueued++;
1695 if (color == COLOR_SKIP)
1696 nskip++;
1700 got_object_commit_close(commit);
1701 commit = NULL;
1703 STAILQ_INSERT_TAIL(&painted, qid, entry);
1704 qid = NULL;
1705 npainted++;
1707 err = got_privsep_send_painted_commits(ibuf, &painted,
1708 &npainted, 1, 0);
1709 if (err)
1710 goto done;
1713 err = got_privsep_send_painted_commits(ibuf, &painted, &npainted, 1, 1);
1714 if (err)
1715 goto done;
1717 *nids = nqueued;
1718 done:
1719 if (commit)
1720 got_object_commit_close(commit);
1721 got_object_qid_free(qid);
1722 return err;
1725 static void
1726 commit_painting_free(struct got_object_idset **keep,
1727 struct got_object_idset **drop,
1728 struct got_object_idset **skip)
1730 if (*keep) {
1731 got_object_idset_free(*keep);
1732 *keep = NULL;
1734 if (*drop) {
1735 got_object_idset_free(*drop);
1736 *drop = NULL;
1738 if (*skip) {
1739 got_object_idset_free(*skip);
1740 *skip = NULL;
1744 static const struct got_error *
1745 commit_painting_init(struct imsgbuf *ibuf, struct got_object_idset **keep,
1746 struct got_object_idset **drop, struct got_object_idset **skip)
1748 const struct got_error *err = NULL;
1750 *keep = got_object_idset_alloc();
1751 if (*keep == NULL) {
1752 err = got_error_from_errno("got_object_idset_alloc");
1753 goto done;
1755 *drop = got_object_idset_alloc();
1756 if (*drop == NULL) {
1757 err = got_error_from_errno("got_object_idset_alloc");
1758 goto done;
1760 *skip = got_object_idset_alloc();
1761 if (*skip == NULL) {
1762 err = got_error_from_errno("got_object_idset_alloc");
1763 goto done;
1766 err = recv_object_ids(*keep, ibuf);
1767 if (err)
1768 goto done;
1769 err = recv_object_ids(*drop, ibuf);
1770 if (err)
1771 goto done;
1772 err = recv_object_ids(*skip, ibuf);
1773 if (err)
1774 goto done;
1776 done:
1777 if (err)
1778 commit_painting_free(keep, drop, skip);
1780 return err;
1783 static const struct got_error *
1784 commit_painting_request(struct imsg *imsg, struct imsgbuf *ibuf,
1785 struct got_pack *pack, struct got_packidx *packidx,
1786 struct got_object_cache *objcache, struct got_object_idset *keep,
1787 struct got_object_idset *drop, struct got_object_idset *skip)
1789 const struct got_error *err = NULL;
1790 struct got_imsg_commit_painting_request ireq;
1791 struct got_object_id id;
1792 size_t datalen;
1793 struct got_object_id_queue ids;
1794 int nids = 0;
1796 STAILQ_INIT(&ids);
1798 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1799 if (datalen != sizeof(ireq))
1800 return got_error(GOT_ERR_PRIVSEP_LEN);
1801 memcpy(&ireq, imsg->data, sizeof(ireq));
1802 memcpy(&id, &ireq.id, sizeof(id));
1804 err = queue_commit_id(&ids, &id, ireq.color);
1805 if (err)
1806 return err;
1807 nids = 1;
1809 err = paint_commits(&ids, &nids, keep, drop, skip,
1810 pack, packidx, ibuf, objcache);
1811 if (err)
1812 goto done;
1814 err = got_privsep_send_painted_commits(ibuf, &ids, &nids, 0, 1);
1815 if (err)
1816 goto done;
1818 err = got_privsep_send_painting_commits_done(ibuf);
1819 done:
1820 got_object_id_queue_free(&ids);
1821 return err;
1824 static const struct got_error *
1825 receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
1827 const struct got_error *err = NULL;
1828 struct imsg imsg;
1829 struct got_imsg_pack ipack;
1830 size_t datalen;
1831 struct got_pack *pack;
1833 *packp = NULL;
1835 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1836 if (err)
1837 return err;
1839 pack = calloc(1, sizeof(*pack));
1840 if (pack == NULL) {
1841 err = got_error_from_errno("calloc");
1842 goto done;
1845 if (imsg.hdr.type != GOT_IMSG_PACK) {
1846 err = got_error(GOT_ERR_PRIVSEP_MSG);
1847 goto done;
1850 if (imsg.fd == -1) {
1851 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1852 goto done;
1855 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1856 if (datalen != sizeof(ipack)) {
1857 err = got_error(GOT_ERR_PRIVSEP_LEN);
1858 goto done;
1860 memcpy(&ipack, imsg.data, sizeof(ipack));
1862 pack->filesize = ipack.filesize;
1863 pack->fd = dup(imsg.fd);
1864 if (pack->fd == -1) {
1865 err = got_error_from_errno("dup");
1866 goto done;
1868 if (lseek(pack->fd, 0, SEEK_SET) == -1) {
1869 err = got_error_from_errno("lseek");
1870 goto done;
1872 pack->path_packfile = strdup(ipack.path_packfile);
1873 if (pack->path_packfile == NULL) {
1874 err = got_error_from_errno("strdup");
1875 goto done;
1878 err = got_delta_cache_alloc(&pack->delta_cache);
1879 if (err)
1880 goto done;
1882 #ifndef GOT_PACK_NO_MMAP
1883 if (pack->filesize > 0 && pack->filesize <= SIZE_MAX) {
1884 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1885 pack->fd, 0);
1886 if (pack->map == MAP_FAILED)
1887 pack->map = NULL; /* fall back to read(2) */
1889 #endif
1890 done:
1891 if (err) {
1892 if (imsg.fd != -1)
1893 close(imsg.fd);
1894 free(pack);
1895 } else
1896 *packp = pack;
1897 imsg_free(&imsg);
1898 return err;
1901 int
1902 main(int argc, char *argv[])
1904 const struct got_error *err = NULL;
1905 struct imsgbuf ibuf;
1906 struct imsg imsg;
1907 struct got_packidx *packidx = NULL;
1908 struct got_pack *pack = NULL;
1909 struct got_object_cache objcache;
1910 FILE *basefile = NULL, *accumfile = NULL, *delta_outfile = NULL;
1911 struct got_object_idset *keep = NULL, *drop = NULL, *skip = NULL;
1912 struct got_parsed_tree_entry *entries = NULL;
1913 size_t nentries = 0, nentries_alloc = 0;
1915 //static int attached;
1916 //while (!attached) sleep(1);
1918 signal(SIGINT, catch_sigint);
1920 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
1922 err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
1923 if (err) {
1924 err = got_error_from_errno("got_object_cache_init");
1925 got_privsep_send_error(&ibuf, err);
1926 return 1;
1929 #ifndef PROFILE
1930 /* revoke access to most system calls */
1931 if (pledge("stdio recvfd", NULL) == -1) {
1932 err = got_error_from_errno("pledge");
1933 got_privsep_send_error(&ibuf, err);
1934 return 1;
1936 #endif
1938 err = receive_packidx(&packidx, &ibuf);
1939 if (err) {
1940 got_privsep_send_error(&ibuf, err);
1941 return 1;
1944 err = receive_pack(&pack, &ibuf);
1945 if (err) {
1946 got_privsep_send_error(&ibuf, err);
1947 return 1;
1950 for (;;) {
1951 imsg.fd = -1;
1953 if (sigint_received) {
1954 err = got_error(GOT_ERR_CANCELLED);
1955 break;
1958 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1959 if (err) {
1960 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1961 err = NULL;
1962 break;
1965 if (imsg.hdr.type == GOT_IMSG_STOP)
1966 break;
1968 switch (imsg.hdr.type) {
1969 case GOT_IMSG_TMPFD:
1970 if (basefile == NULL) {
1971 err = receive_tempfile(&basefile, "w+",
1972 &imsg, &ibuf);
1973 } else if (accumfile == NULL) {
1974 err = receive_tempfile(&accumfile, "w+",
1975 &imsg, &ibuf);
1976 } else
1977 err = got_error(GOT_ERR_PRIVSEP_MSG);
1978 break;
1979 case GOT_IMSG_PACKED_OBJECT_REQUEST:
1980 err = object_request(&imsg, &ibuf, pack, packidx,
1981 &objcache);
1982 break;
1983 case GOT_IMSG_PACKED_RAW_OBJECT_REQUEST:
1984 if (basefile == NULL || accumfile == NULL) {
1985 err = got_error(GOT_ERR_PRIVSEP_MSG);
1986 break;
1988 err = raw_object_request(&imsg, &ibuf, pack, packidx,
1989 &objcache, basefile, accumfile);
1990 break;
1991 case GOT_IMSG_RAW_DELTA_OUTFD:
1992 if (delta_outfile != NULL) {
1993 err = got_error(GOT_ERR_PRIVSEP_MSG);
1994 break;
1996 err = receive_tempfile(&delta_outfile, "w",
1997 &imsg, &ibuf);
1998 break;
1999 case GOT_IMSG_RAW_DELTA_REQUEST:
2000 if (delta_outfile == NULL) {
2001 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
2002 break;
2004 err = raw_delta_request(&imsg, &ibuf, delta_outfile,
2005 pack, packidx);
2006 break;
2007 case GOT_IMSG_DELTA_REUSE_REQUEST:
2008 err = delta_reuse_request(&imsg, &ibuf, pack, packidx);
2009 break;
2010 case GOT_IMSG_COMMIT_REQUEST:
2011 err = commit_request(&imsg, &ibuf, pack, packidx,
2012 &objcache);
2013 break;
2014 case GOT_IMSG_TREE_REQUEST:
2015 err = tree_request(&imsg, &ibuf, pack, packidx,
2016 &objcache, &entries, &nentries, &nentries_alloc);
2017 break;
2018 case GOT_IMSG_BLOB_REQUEST:
2019 if (basefile == NULL || accumfile == NULL) {
2020 err = got_error(GOT_ERR_PRIVSEP_MSG);
2021 break;
2023 err = blob_request(&imsg, &ibuf, pack, packidx,
2024 &objcache, basefile, accumfile);
2025 break;
2026 case GOT_IMSG_TAG_REQUEST:
2027 err = tag_request(&imsg, &ibuf, pack, packidx,
2028 &objcache);
2029 break;
2030 case GOT_IMSG_COMMIT_TRAVERSAL_REQUEST:
2031 err = commit_traversal_request(&imsg, &ibuf, pack,
2032 packidx, &objcache);
2033 break;
2034 case GOT_IMSG_OBJECT_ENUMERATION_REQUEST:
2035 err = enumeration_request(&imsg, &ibuf, pack,
2036 packidx, &objcache);
2037 break;
2038 case GOT_IMSG_COMMIT_PAINTING_INIT:
2039 commit_painting_free(&keep, &drop, &skip);
2040 err = commit_painting_init(&ibuf, &keep, &drop, &skip);
2041 break;
2042 case GOT_IMSG_COMMIT_PAINTING_REQUEST:
2043 if (keep == NULL || drop == NULL || skip == NULL) {
2044 err = got_error(GOT_ERR_PRIVSEP_MSG);
2045 break;
2047 err = commit_painting_request(&imsg, &ibuf, pack,
2048 packidx, &objcache, keep, drop, skip);
2049 break;
2050 case GOT_IMSG_COMMIT_PAINTING_DONE:
2051 commit_painting_free(&keep, &drop, &skip);
2052 break;
2053 default:
2054 err = got_error(GOT_ERR_PRIVSEP_MSG);
2055 break;
2058 if (imsg.fd != -1 && close(imsg.fd) == -1 && err == NULL)
2059 err = got_error_from_errno("close");
2060 imsg_free(&imsg);
2061 if (err)
2062 break;
2065 free(entries);
2066 commit_painting_free(&keep, &drop, &skip);
2067 if (packidx)
2068 got_packidx_close(packidx);
2069 if (pack)
2070 got_pack_close(pack);
2071 got_object_cache_close(&objcache);
2072 imsg_clear(&ibuf);
2073 if (basefile && fclose(basefile) == EOF && err == NULL)
2074 err = got_error_from_errno("fclose");
2075 if (accumfile && fclose(accumfile) == EOF && err == NULL)
2076 err = got_error_from_errno("fclose");
2077 if (delta_outfile && fclose(delta_outfile) == EOF && err == NULL)
2078 err = got_error_from_errno("fclose");
2079 if (err) {
2080 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
2081 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
2082 got_privsep_send_error(&ibuf, err);
2085 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
2086 err = got_error_from_errno("close");
2087 return err ? 1 : 0;