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 <unistd.h>
34 #include <zlib.h>
36 #include "got_error.h"
37 #include "got_object.h"
38 #include "got_path.h"
40 #include "got_lib_delta.h"
41 #include "got_lib_delta_cache.h"
42 #include "got_lib_object.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.sha1, iobj.id, SHA1_DIGEST_LENGTH);
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.sha1, iobj.id, SHA1_DIGEST_LENGTH);
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, struct got_parsed_tree_entry **entries, int *nentries,
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 size_t len;
194 *buf = NULL;
195 *nentries = 0;
197 obj = got_object_cache_get(objcache, id);
198 if (obj) {
199 obj->refcnt++;
200 } else {
201 err = open_object(&obj, pack, packidx, obj_idx, id,
202 objcache);
203 if (err)
204 return err;
207 err = got_packfile_extract_object_to_mem(buf, &len, obj, pack);
208 if (err)
209 goto done;
211 obj->size = len;
213 err = got_object_parse_tree(entries, nentries, *buf, 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)
227 const struct got_error *err = NULL;
228 struct got_imsg_packed_object iobj;
229 struct got_parsed_tree_entry *entries = NULL;
230 int nentries = 0;
231 uint8_t *buf = NULL;
232 struct got_object_id id;
233 size_t datalen;
235 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
236 if (datalen != sizeof(iobj))
237 return got_error(GOT_ERR_PRIVSEP_LEN);
238 memcpy(&iobj, imsg->data, sizeof(iobj));
239 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
241 err = open_tree(&buf, &entries, &nentries, pack, packidx, iobj.idx,
242 &id, objcache);
243 if (err)
244 return err;
246 err = got_privsep_send_tree(ibuf, entries, nentries);
247 free(entries);
248 free(buf);
249 if (err) {
250 if (err->code == GOT_ERR_PRIVSEP_PIPE)
251 err = NULL;
252 else
253 got_privsep_send_error(ibuf, err);
256 return err;
259 static const struct got_error *
260 receive_file(FILE **f, struct imsgbuf *ibuf, uint32_t imsg_code)
262 const struct got_error *err;
263 struct imsg imsg;
264 size_t datalen;
266 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
267 if (err)
268 return err;
270 if (imsg.hdr.type != imsg_code) {
271 err = got_error(GOT_ERR_PRIVSEP_MSG);
272 goto done;
275 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
276 if (datalen != 0) {
277 err = got_error(GOT_ERR_PRIVSEP_LEN);
278 goto done;
280 if (imsg.fd == -1) {
281 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
282 goto done;
285 *f = fdopen(imsg.fd, "w+");
286 if (*f == NULL) {
287 err = got_error_from_errno("fdopen");
288 close(imsg.fd);
289 goto done;
291 done:
292 imsg_free(&imsg);
293 return err;
296 static const struct got_error *
297 receive_tempfile(FILE **f, const char *mode, struct imsg *imsg,
298 struct imsgbuf *ibuf)
300 size_t datalen;
302 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
303 if (datalen != 0)
304 return got_error(GOT_ERR_PRIVSEP_LEN);
306 if (imsg->fd == -1)
307 return got_error(GOT_ERR_PRIVSEP_NO_FD);
309 *f = fdopen(imsg->fd, mode);
310 if (*f == NULL)
311 return got_error_from_errno("fdopen");
312 imsg->fd = -1;
314 return NULL;
317 static const struct got_error *
318 blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
319 struct got_packidx *packidx, struct got_object_cache *objcache,
320 FILE *basefile, FILE *accumfile)
322 const struct got_error *err = NULL;
323 struct got_imsg_packed_object iobj;
324 struct got_object *obj = NULL;
325 FILE *outfile = NULL;
326 struct got_object_id id;
327 size_t datalen;
328 uint64_t blob_size;
329 uint8_t *buf = NULL;
331 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
332 if (datalen != sizeof(iobj))
333 return got_error(GOT_ERR_PRIVSEP_LEN);
334 memcpy(&iobj, imsg->data, sizeof(iobj));
335 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
337 obj = got_object_cache_get(objcache, &id);
338 if (obj) {
339 obj->refcnt++;
340 } else {
341 err = open_object(&obj, pack, packidx, iobj.idx, &id,
342 objcache);
343 if (err)
344 return err;
347 err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
348 if (err)
349 goto done;
351 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
352 err = got_pack_get_max_delta_object_size(&blob_size, obj, pack);
353 if (err)
354 goto done;
355 } else
356 blob_size = obj->size;
358 if (blob_size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
359 err = got_packfile_extract_object_to_mem(&buf, &obj->size,
360 obj, pack);
361 else
362 err = got_packfile_extract_object(pack, obj, outfile, basefile,
363 accumfile);
364 if (err)
365 goto done;
367 err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, buf);
368 done:
369 free(buf);
370 if (outfile && fclose(outfile) == EOF && err == NULL)
371 err = got_error_from_errno("fclose");
372 got_object_close(obj);
373 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
374 got_privsep_send_error(ibuf, err);
376 return err;
379 static const struct got_error *
380 tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
381 struct got_packidx *packidx, struct got_object_cache *objcache)
383 const struct got_error *err = NULL;
384 struct got_imsg_packed_object iobj;
385 struct got_object *obj = NULL;
386 struct got_tag_object *tag = NULL;
387 uint8_t *buf = NULL;
388 size_t len;
389 struct got_object_id id;
390 size_t datalen;
392 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
393 if (datalen != sizeof(iobj))
394 return got_error(GOT_ERR_PRIVSEP_LEN);
395 memcpy(&iobj, imsg->data, sizeof(iobj));
396 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
398 obj = got_object_cache_get(objcache, &id);
399 if (obj) {
400 obj->refcnt++;
401 } else {
402 err = open_object(&obj, pack, packidx, iobj.idx, &id,
403 objcache);
404 if (err)
405 return err;
408 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
409 if (err)
410 goto done;
412 obj->size = len;
413 err = got_object_parse_tag(&tag, buf, len);
414 if (err)
415 goto done;
417 err = got_privsep_send_tag(ibuf, tag);
418 done:
419 free(buf);
420 got_object_close(obj);
421 if (tag)
422 got_object_tag_close(tag);
423 if (err) {
424 if (err->code == GOT_ERR_PRIVSEP_PIPE)
425 err = NULL;
426 else
427 got_privsep_send_error(ibuf, err);
430 return err;
433 static struct got_parsed_tree_entry *
434 find_entry_by_name(struct got_parsed_tree_entry *entries, int nentries,
435 const char *name, size_t len)
437 struct got_parsed_tree_entry *pte;
438 int cmp, i;
440 /* Note that tree entries are sorted in strncmp() order. */
441 for (i = 0; i < nentries; i++) {
442 pte = &entries[i];
443 cmp = strncmp(pte->name, name, len);
444 if (cmp < 0)
445 continue;
446 if (cmp > 0)
447 break;
448 if (pte->name[len] == '\0')
449 return pte;
451 return NULL;
454 static const struct got_error *
455 tree_path_changed(int *changed, uint8_t **buf1, uint8_t **buf2,
456 struct got_parsed_tree_entry **entries1, int *nentries1,
457 struct got_parsed_tree_entry **entries2, int *nentries2,
458 const char *path, struct got_pack *pack, struct got_packidx *packidx,
459 struct imsgbuf *ibuf, struct got_object_cache *objcache)
461 const struct got_error *err = NULL;
462 struct got_parsed_tree_entry *pte1 = NULL, *pte2 = NULL;
463 const char *seg, *s;
464 size_t seglen;
466 *changed = 0;
468 /* We not do support comparing the root path. */
469 if (got_path_is_root_dir(path))
470 return got_error_path(path, GOT_ERR_BAD_PATH);
472 s = path;
473 while (*s == '/')
474 s++;
475 seg = s;
476 seglen = 0;
477 while (*s) {
478 if (*s != '/') {
479 s++;
480 seglen++;
481 if (*s)
482 continue;
485 pte1 = find_entry_by_name(*entries1, *nentries1, seg, seglen);
486 if (pte1 == NULL) {
487 err = got_error(GOT_ERR_NO_OBJ);
488 break;
491 pte2 = find_entry_by_name(*entries2, *nentries2, seg, seglen);
492 if (pte2 == NULL) {
493 *changed = 1;
494 break;
497 if (pte1->mode != pte2->mode) {
498 *changed = 1;
499 break;
502 if (memcmp(pte1->id, pte2->id, SHA1_DIGEST_LENGTH) == 0) {
503 *changed = 0;
504 break;
507 if (*s == '\0') { /* final path element */
508 *changed = 1;
509 break;
512 seg = s + 1;
513 s++;
514 seglen = 0;
515 if (*s) {
516 struct got_object_id id1, id2;
517 int idx;
519 memcpy(id1.sha1, pte1->id, SHA1_DIGEST_LENGTH);
520 idx = got_packidx_get_object_idx(packidx, &id1);
521 if (idx == -1) {
522 err = got_error_no_obj(&id1);
523 break;
525 free(*entries1);
526 *nentries1 = 0;
527 free(*buf1);
528 *buf1 = NULL;
529 err = open_tree(buf1, entries1, nentries1, pack,
530 packidx, idx, &id1, objcache);
531 pte1 = NULL;
532 if (err)
533 break;
535 memcpy(id2.sha1, pte2->id, SHA1_DIGEST_LENGTH);
536 idx = got_packidx_get_object_idx(packidx, &id2);
537 if (idx == -1) {
538 err = got_error_no_obj(&id2);
539 break;
541 free(*entries2);
542 *nentries2 = 0;
543 free(*buf2);
544 *buf2 = NULL;
545 err = open_tree(buf2, entries2, nentries2, pack,
546 packidx, idx, &id2, objcache);
547 pte2 = NULL;
548 if (err)
549 break;
553 return err;
556 static const struct got_error *
557 send_traversed_commits(struct got_object_id *commit_ids, size_t ncommits,
558 struct imsgbuf *ibuf)
560 struct ibuf *wbuf;
561 size_t i;
563 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
564 sizeof(struct got_imsg_traversed_commits) +
565 ncommits * SHA1_DIGEST_LENGTH);
566 if (wbuf == NULL)
567 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
569 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1)
570 return got_error_from_errno("imsg_add TRAVERSED_COMMITS");
572 for (i = 0; i < ncommits; i++) {
573 struct got_object_id *id = &commit_ids[i];
574 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
575 return got_error_from_errno(
576 "imsg_add TRAVERSED_COMMITS");
580 wbuf->fd = -1;
581 imsg_close(ibuf, wbuf);
583 return got_privsep_flush_imsg(ibuf);
586 static const struct got_error *
587 send_commit_traversal_done(struct imsgbuf *ibuf)
589 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
590 NULL, 0) == -1)
591 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
593 return got_privsep_flush_imsg(ibuf);
596 static const struct got_error *
597 commit_traversal_request(struct imsg *imsg, struct imsgbuf *ibuf,
598 struct got_pack *pack, struct got_packidx *packidx,
599 struct got_object_cache *objcache)
601 const struct got_error *err = NULL;
602 struct got_imsg_packed_object iobj;
603 struct got_object_qid *pid;
604 struct got_commit_object *commit = NULL, *pcommit = NULL;
605 struct got_parsed_tree_entry *entries = NULL, *pentries = NULL;
606 int nentries = 0, pnentries = 0;
607 struct got_object_id id;
608 size_t datalen, path_len;
609 char *path = NULL;
610 const int min_alloc = 64;
611 int changed = 0, ncommits = 0, nallocated = 0;
612 struct got_object_id *commit_ids = NULL;
614 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
615 if (datalen < sizeof(iobj))
616 return got_error(GOT_ERR_PRIVSEP_LEN);
617 memcpy(&iobj, imsg->data, sizeof(iobj));
618 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
620 path_len = datalen - sizeof(iobj) - 1;
621 if (path_len < 0)
622 return got_error(GOT_ERR_PRIVSEP_LEN);
623 if (path_len > 0) {
624 path = imsg->data + sizeof(iobj);
625 if (path[path_len] != '\0')
626 return got_error(GOT_ERR_PRIVSEP_LEN);
629 nallocated = min_alloc;
630 commit_ids = reallocarray(NULL, nallocated, sizeof(*commit_ids));
631 if (commit_ids == NULL)
632 return got_error_from_errno("reallocarray");
634 do {
635 const size_t max_datalen = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
636 int idx;
638 if (sigint_received) {
639 err = got_error(GOT_ERR_CANCELLED);
640 goto done;
643 if (commit == NULL) {
644 idx = got_packidx_get_object_idx(packidx, &id);
645 if (idx == -1)
646 break;
647 err = open_commit(&commit, pack, packidx,
648 idx, &id, objcache);
649 if (err) {
650 if (err->code != GOT_ERR_NO_OBJ)
651 goto done;
652 err = NULL;
653 break;
657 if (sizeof(struct got_imsg_traversed_commits) +
658 ncommits * SHA1_DIGEST_LENGTH >= max_datalen) {
659 err = send_traversed_commits(commit_ids, ncommits,
660 ibuf);
661 if (err)
662 goto done;
663 ncommits = 0;
665 ncommits++;
666 if (ncommits > nallocated) {
667 struct got_object_id *new;
668 nallocated += min_alloc;
669 new = reallocarray(commit_ids, nallocated,
670 sizeof(*commit_ids));
671 if (new == NULL) {
672 err = got_error_from_errno("reallocarray");
673 goto done;
675 commit_ids = new;
677 memcpy(commit_ids[ncommits - 1].sha1, id.sha1,
678 SHA1_DIGEST_LENGTH);
680 pid = STAILQ_FIRST(&commit->parent_ids);
681 if (pid == NULL)
682 break;
684 idx = got_packidx_get_object_idx(packidx, &pid->id);
685 if (idx == -1)
686 break;
688 err = open_commit(&pcommit, pack, packidx, idx, &pid->id,
689 objcache);
690 if (err) {
691 if (err->code != GOT_ERR_NO_OBJ)
692 goto done;
693 err = NULL;
694 break;
697 if (path[0] == '/' && path[1] == '\0') {
698 if (got_object_id_cmp(pcommit->tree_id,
699 commit->tree_id) != 0) {
700 changed = 1;
701 break;
703 } else {
704 int pidx;
705 uint8_t *buf = NULL, *pbuf = NULL;
707 idx = got_packidx_get_object_idx(packidx,
708 commit->tree_id);
709 if (idx == -1)
710 break;
711 pidx = got_packidx_get_object_idx(packidx,
712 pcommit->tree_id);
713 if (pidx == -1)
714 break;
716 err = open_tree(&buf, &entries, &nentries, pack,
717 packidx, idx, commit->tree_id, objcache);
718 if (err)
719 goto done;
720 err = open_tree(&pbuf, &pentries, &pnentries, pack,
721 packidx, pidx, pcommit->tree_id, objcache);
722 if (err) {
723 free(buf);
724 goto done;
727 err = tree_path_changed(&changed, &buf, &pbuf,
728 &entries, &nentries, &pentries, &pnentries, path,
729 pack, packidx, ibuf, objcache);
731 free(entries);
732 entries = NULL;
733 nentries = 0;
734 free(buf);
735 free(pentries);
736 pentries = NULL;
737 pnentries = 0;
738 free(pbuf);
739 if (err) {
740 if (err->code != GOT_ERR_NO_OBJ)
741 goto done;
742 err = NULL;
743 break;
747 if (!changed) {
748 memcpy(id.sha1, pid->id.sha1, SHA1_DIGEST_LENGTH);
749 got_object_commit_close(commit);
750 commit = pcommit;
751 pcommit = NULL;
753 } while (!changed);
755 if (ncommits > 0) {
756 err = send_traversed_commits(commit_ids, ncommits, ibuf);
757 if (err)
758 goto done;
760 if (changed) {
761 err = got_privsep_send_commit(ibuf, commit);
762 if (err)
763 goto done;
766 err = send_commit_traversal_done(ibuf);
767 done:
768 free(commit_ids);
769 if (commit)
770 got_object_commit_close(commit);
771 if (pcommit)
772 got_object_commit_close(pcommit);
773 free(entries);
774 free(pentries);
775 if (err) {
776 if (err->code == GOT_ERR_PRIVSEP_PIPE)
777 err = NULL;
778 else
779 got_privsep_send_error(ibuf, err);
782 return err;
785 static const struct got_error *
786 raw_object_request(struct imsg *imsg, struct imsgbuf *ibuf,
787 struct got_pack *pack, struct got_packidx *packidx,
788 struct got_object_cache *objcache, FILE *basefile, FILE *accumfile)
790 const struct got_error *err = NULL;
791 uint8_t *buf = NULL;
792 uint64_t size = 0;
793 FILE *outfile = NULL;
794 struct got_imsg_packed_object iobj;
795 struct got_object *obj;
796 struct got_object_id id;
797 size_t datalen;
799 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
800 if (datalen != sizeof(iobj))
801 return got_error(GOT_ERR_PRIVSEP_LEN);
802 memcpy(&iobj, imsg->data, sizeof(iobj));
803 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
805 obj = got_object_cache_get(objcache, &id);
806 if (obj) {
807 obj->refcnt++;
808 } else {
809 err = open_object(&obj, pack, packidx, iobj.idx, &id,
810 objcache);
811 if (err)
812 return err;
815 err = receive_file(&outfile, ibuf, GOT_IMSG_RAW_OBJECT_OUTFD);
816 if (err)
817 return err;
819 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
820 err = got_pack_get_max_delta_object_size(&size, obj, pack);
821 if (err)
822 goto done;
823 } else
824 size = obj->size;
826 if (size <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
827 err = got_packfile_extract_object_to_mem(&buf, &obj->size,
828 obj, pack);
829 else
830 err = got_packfile_extract_object(pack, obj, outfile, basefile,
831 accumfile);
832 if (err)
833 goto done;
835 err = got_privsep_send_raw_obj(ibuf, obj->size, obj->hdrlen, buf);
836 done:
837 free(buf);
838 if (outfile && fclose(outfile) == EOF && err == NULL)
839 err = got_error_from_errno("fclose");
840 got_object_close(obj);
841 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
842 got_privsep_send_error(ibuf, err);
844 return err;
847 static const struct got_error *
848 get_base_object_id(struct got_object_id *base_id, struct got_packidx *packidx,
849 off_t base_offset)
851 const struct got_error *err;
852 int idx;
854 err = got_packidx_get_offset_idx(&idx, packidx, base_offset);
855 if (err)
856 return err;
857 if (idx == -1)
858 return got_error(GOT_ERR_BAD_PACKIDX);
860 return got_packidx_get_object_id(base_id, packidx, idx);
863 static const struct got_error *
864 raw_delta_request(struct imsg *imsg, struct imsgbuf *ibuf,
865 FILE *delta_outfile, struct got_pack *pack,
866 struct got_packidx *packidx)
868 const struct got_error *err = NULL;
869 struct got_imsg_raw_delta_request req;
870 size_t datalen, delta_size, delta_compressed_size;
871 off_t delta_offset;
872 uint8_t *delta_buf = NULL;
873 struct got_object_id id, base_id;
874 off_t base_offset, delta_out_offset = 0;
875 uint64_t base_size = 0, result_size = 0;
876 size_t w;
878 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
879 if (datalen != sizeof(req))
880 return got_error(GOT_ERR_PRIVSEP_LEN);
881 memcpy(&req, imsg->data, sizeof(req));
882 memcpy(id.sha1, req.id, SHA1_DIGEST_LENGTH);
884 imsg->fd = -1;
886 err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
887 &delta_compressed_size, &delta_offset, &base_offset, &base_id,
888 &base_size, &result_size, pack, packidx, req.idx);
889 if (err)
890 goto done;
892 /*
893 * If this is an offset delta we must determine the base
894 * object ID ourselves.
895 */
896 if (base_offset != 0) {
897 err = get_base_object_id(&base_id, packidx, base_offset);
898 if (err)
899 goto done;
902 delta_out_offset = ftello(delta_outfile);
903 w = fwrite(delta_buf, 1, delta_compressed_size, delta_outfile);
904 if (w != delta_compressed_size) {
905 err = got_ferror(delta_outfile, GOT_ERR_IO);
906 goto done;
908 if (fflush(delta_outfile) == -1) {
909 err = got_error_from_errno("fflush");
910 goto done;
913 err = got_privsep_send_raw_delta(ibuf, base_size, result_size,
914 delta_size, delta_compressed_size, delta_offset, delta_out_offset,
915 &base_id);
916 done:
917 free(delta_buf);
918 return err;
921 struct search_deltas_arg {
922 struct imsgbuf *ibuf;
923 struct got_packidx *packidx;
924 struct got_pack *pack;
925 struct got_object_idset *idset;
926 FILE *delta_outfile;
927 struct got_imsg_reused_delta deltas[GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS];
928 size_t ndeltas;
929 };
931 static const struct got_error *
932 search_delta_for_object(struct got_object_id *id, void *data, void *arg)
934 const struct got_error *err;
935 struct search_deltas_arg *a = arg;
936 int obj_idx;
937 uint8_t *delta_buf = NULL;
938 uint64_t base_size, result_size;
939 size_t delta_size, delta_compressed_size;
940 off_t delta_offset, base_offset;
941 struct got_object_id base_id;
943 if (sigint_received)
944 return got_error(GOT_ERR_CANCELLED);
946 obj_idx = got_packidx_get_object_idx(a->packidx, id);
947 if (obj_idx == -1)
948 return NULL; /* object not present in our pack file */
950 err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
951 &delta_compressed_size, &delta_offset, &base_offset, &base_id,
952 &base_size, &result_size, a->pack, a->packidx, obj_idx);
953 if (err) {
954 if (err->code == GOT_ERR_OBJ_TYPE)
955 return NULL; /* object not stored as a delta */
956 return err;
959 /*
960 * If this is an offset delta we must determine the base
961 * object ID ourselves.
962 */
963 if (base_offset != 0) {
964 err = get_base_object_id(&base_id, a->packidx, base_offset);
965 if (err)
966 goto done;
969 if (got_object_idset_contains(a->idset, &base_id)) {
970 struct got_imsg_reused_delta *delta;
971 off_t delta_out_offset = ftello(a->delta_outfile);
972 size_t w;
974 w = fwrite(delta_buf, 1, delta_compressed_size,
975 a->delta_outfile);
976 if (w != delta_compressed_size) {
977 err = got_ferror(a->delta_outfile, GOT_ERR_IO);
978 goto done;
981 delta = &a->deltas[a->ndeltas++];
982 memcpy(&delta->id, id, sizeof(delta->id));
983 memcpy(&delta->base_id, &base_id, sizeof(delta->base_id));
984 delta->base_size = base_size;
985 delta->result_size = result_size;
986 delta->delta_size = delta_size;
987 delta->delta_compressed_size = delta_compressed_size;
988 delta->delta_offset = delta_offset;
989 delta->delta_out_offset = delta_out_offset;
991 if (a->ndeltas >= GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS) {
992 err = got_privsep_send_reused_deltas(a->ibuf,
993 a->deltas, a->ndeltas);
994 if (err)
995 goto done;
996 a->ndeltas = 0;
999 done:
1000 free(delta_buf);
1001 return err;
1004 static const struct got_error *
1005 recv_object_ids(struct got_object_idset *idset, struct imsgbuf *ibuf)
1007 const struct got_error *err = NULL;
1008 int done = 0;
1009 struct got_object_id *ids;
1010 size_t nids, i;
1012 for (;;) {
1013 err = got_privsep_recv_object_idlist(&done, &ids, &nids, ibuf);
1014 if (err || done)
1015 break;
1016 for (i = 0; i < nids; i++) {
1017 err = got_object_idset_add(idset, &ids[i], NULL);
1018 if (err) {
1019 free(ids);
1020 return err;
1023 free(ids);
1026 return err;
1029 static const struct got_error *
1030 recv_object_id_queue(struct got_object_id_queue *queue, struct imsgbuf *ibuf)
1032 const struct got_error *err = NULL;
1033 int done = 0;
1034 struct got_object_qid *qid;
1035 struct got_object_id *ids;
1036 size_t nids, i;
1038 for (;;) {
1039 err = got_privsep_recv_object_idlist(&done, &ids, &nids, ibuf);
1040 if (err || done)
1041 break;
1042 for (i = 0; i < nids; i++) {
1043 err = got_object_qid_alloc_partial(&qid);
1044 if (err)
1045 return err;
1046 memcpy(&qid->id, &ids[i], sizeof(qid->id));
1047 STAILQ_INSERT_TAIL(queue, qid, entry);
1051 return err;
1054 static const struct got_error *
1055 delta_reuse_request(struct imsg *imsg, struct imsgbuf *ibuf,
1056 FILE *delta_outfile, struct got_pack *pack, struct got_packidx *packidx)
1058 const struct got_error *err = NULL;
1059 struct got_object_idset *idset;
1060 struct search_deltas_arg sda;
1062 idset = got_object_idset_alloc();
1063 if (idset == NULL)
1064 return got_error_from_errno("got_object_idset_alloc");
1066 err = recv_object_ids(idset, ibuf);
1067 if (err)
1068 return err;
1070 memset(&sda, 0, sizeof(sda));
1071 sda.ibuf = ibuf;
1072 sda.idset = idset;
1073 sda.pack = pack;
1074 sda.packidx = packidx;
1075 sda.delta_outfile = delta_outfile;
1076 err = got_object_idset_for_each(idset, search_delta_for_object, &sda);
1077 if (err)
1078 goto done;
1080 if (sda.ndeltas > 0) {
1081 err = got_privsep_send_reused_deltas(ibuf, sda.deltas,
1082 sda.ndeltas);
1083 if (err)
1084 goto done;
1087 if (fflush(delta_outfile) == -1) {
1088 err = got_error_from_errno("fflush");
1089 goto done;
1092 err = got_privsep_send_reused_deltas_done(ibuf);
1093 done:
1094 got_object_idset_free(idset);
1095 return err;
1098 static const struct got_error *
1099 receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
1101 const struct got_error *err = NULL;
1102 struct imsg imsg;
1103 struct got_imsg_packidx ipackidx;
1104 size_t datalen;
1105 struct got_packidx *p;
1107 *packidx = NULL;
1109 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1110 if (err)
1111 return err;
1113 p = calloc(1, sizeof(*p));
1114 if (p == NULL) {
1115 err = got_error_from_errno("calloc");
1116 goto done;
1119 if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
1120 err = got_error(GOT_ERR_PRIVSEP_MSG);
1121 goto done;
1124 if (imsg.fd == -1) {
1125 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1126 goto done;
1129 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1130 if (datalen != sizeof(ipackidx)) {
1131 err = got_error(GOT_ERR_PRIVSEP_LEN);
1132 goto done;
1134 memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
1136 p->len = ipackidx.len;
1137 p->fd = dup(imsg.fd);
1138 if (p->fd == -1) {
1139 err = got_error_from_errno("dup");
1140 goto done;
1142 if (lseek(p->fd, 0, SEEK_SET) == -1) {
1143 err = got_error_from_errno("lseek");
1144 goto done;
1147 #ifndef GOT_PACK_NO_MMAP
1148 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
1149 if (p->map == MAP_FAILED)
1150 p->map = NULL; /* fall back to read(2) */
1151 #endif
1152 err = got_packidx_init_hdr(p, 1, ipackidx.packfile_size);
1153 done:
1154 if (err) {
1155 if (imsg.fd != -1)
1156 close(imsg.fd);
1157 got_packidx_close(p);
1158 } else
1159 *packidx = p;
1160 imsg_free(&imsg);
1161 return err;
1164 static const struct got_error *
1165 send_tree_enumeration_done(struct imsgbuf *ibuf)
1167 if (imsg_compose(ibuf, GOT_IMSG_TREE_ENUMERATION_DONE, 0, 0, -1,
1168 NULL, 0) == -1)
1169 return got_error_from_errno("imsg_compose TREE_ENUMERATION_DONE");
1171 return got_privsep_flush_imsg(ibuf);
1174 struct enumerated_tree {
1175 struct got_object_id id;
1176 char *path;
1177 uint8_t *buf;
1178 struct got_parsed_tree_entry *entries;
1179 int nentries;
1182 static const struct got_error *
1183 enumerate_tree(int *have_all_entries, struct imsgbuf *ibuf, size_t *totlen,
1184 struct got_object_id *tree_id,
1185 const char *path, struct got_pack *pack, struct got_packidx *packidx,
1186 struct got_object_cache *objcache, struct got_object_idset *idset,
1187 struct enumerated_tree **trees, size_t *nalloc, size_t *ntrees)
1189 const struct got_error *err = NULL;
1190 struct got_object_id_queue ids;
1191 struct got_object_qid *qid;
1192 uint8_t *buf = NULL;
1193 struct got_parsed_tree_entry *entries = NULL;
1194 int nentries = 0, i;
1195 struct enumerated_tree *tree;
1197 *ntrees = 0;
1198 *have_all_entries = 1;
1199 STAILQ_INIT(&ids);
1201 err = got_object_qid_alloc_partial(&qid);
1202 if (err)
1203 return err;
1204 memcpy(&qid->id.sha1, tree_id, SHA1_DIGEST_LENGTH);
1205 qid->data = strdup(path);
1206 if (qid->data == NULL) {
1207 err = got_error_from_errno("strdup");
1208 goto done;
1210 STAILQ_INSERT_TAIL(&ids, qid, entry);
1211 qid = NULL;
1213 /* Traverse the tree hierarchy, gather tree object IDs and paths. */
1214 do {
1215 const char *path;
1216 int idx, i;
1218 if (sigint_received) {
1219 err = got_error(GOT_ERR_CANCELLED);
1220 goto done;
1223 qid = STAILQ_FIRST(&ids);
1224 STAILQ_REMOVE_HEAD(&ids, entry);
1225 path = qid->data;
1227 idx = got_packidx_get_object_idx(packidx, &qid->id);
1228 if (idx == -1) {
1229 *have_all_entries = 0;
1230 break;
1233 err = open_tree(&buf, &entries, &nentries,
1234 pack, packidx, idx, &qid->id, objcache);
1235 if (err) {
1236 if (err->code != GOT_ERR_NO_OBJ)
1237 goto done;
1240 err = got_object_idset_add(idset, &qid->id, NULL);
1241 if (err)
1242 goto done;
1244 for (i = 0; i < nentries; i++) {
1245 struct got_object_qid *eqid = NULL;
1246 struct got_parsed_tree_entry *pte = &entries[i];
1247 char *p;
1249 if (!S_ISDIR(pte->mode))
1250 continue;
1252 err = got_object_qid_alloc_partial(&eqid);
1253 if (err)
1254 goto done;
1255 memcpy(eqid->id.sha1, pte->id, sizeof(eqid->id.sha1));
1257 if (got_object_idset_contains(idset, &eqid->id)) {
1258 got_object_qid_free(eqid);
1259 continue;
1262 if (asprintf(&p, "%s%s%s", path,
1263 got_path_is_root_dir(path) ? "" : "/",
1264 pte->name) == -1) {
1265 err = got_error_from_errno("asprintf");
1266 got_object_qid_free(eqid);
1267 goto done;
1269 eqid->data = p;
1270 STAILQ_INSERT_TAIL(&ids, eqid, entry);
1273 if (*ntrees >= *nalloc) {
1274 struct enumerated_tree *new;
1275 new = recallocarray(*trees, *nalloc, *nalloc + 16,
1276 sizeof(*new));
1277 if (new == NULL) {
1278 err = got_error_from_errno("malloc");
1279 goto done;
1281 *trees = new;
1282 *nalloc += 16;
1284 tree = &(*trees)[*ntrees];
1285 (*ntrees)++;
1286 memcpy(&tree->id, &qid->id, sizeof(tree->id));
1287 tree->path = qid->data;
1288 tree->buf = buf;
1289 buf = NULL;
1290 tree->entries = entries;
1291 entries = NULL;
1292 tree->nentries = nentries;
1294 got_object_qid_free(qid);
1295 qid = NULL;
1296 } while (!STAILQ_EMPTY(&ids));
1298 if (*have_all_entries) {
1299 int i;
1301 * We have managed to traverse all entries in the hierarchy.
1302 * Tell the main process what we have found.
1304 for (i = 0; i < *ntrees; i++) {
1305 tree = &(*trees)[i];
1306 err = got_privsep_send_enumerated_tree(totlen,
1307 ibuf, &tree->id, tree->path, tree->entries,
1308 tree->nentries);
1309 if (err)
1310 goto done;
1311 free(tree->buf);
1312 tree->buf = NULL;
1313 free(tree->path);
1314 tree->path = NULL;
1315 free(tree->entries);
1316 tree->entries = NULL;
1318 *ntrees = 0; /* don't loop again below to free memory */
1320 err = send_tree_enumeration_done(ibuf);
1321 } else {
1323 * We can only load fully packed tree hierarchies on
1324 * behalf of the main process, otherwise the main process
1325 * gets a wrong idea about which tree objects have
1326 * already been traversed.
1327 * Indicate a missing entry for the root of this tree.
1328 * The main process should continue by loading this
1329 * entire tree the slow way.
1331 err = got_privsep_send_enumerated_tree(totlen, ibuf,
1332 tree_id, "/", NULL, -1);
1333 if (err)
1334 goto done;
1336 done:
1337 free(buf);
1338 free(entries);
1339 for (i = 0; i < *ntrees; i++) {
1340 tree = &(*trees)[i];
1341 free(tree->buf);
1342 tree->buf = NULL;
1343 free(tree->path);
1344 tree->path = NULL;
1345 free(tree->entries);
1346 tree->entries = NULL;
1348 if (qid)
1349 free(qid->data);
1350 got_object_qid_free(qid);
1351 got_object_id_queue_free(&ids);
1352 if (err) {
1353 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1354 err = NULL;
1355 else
1356 got_privsep_send_error(ibuf, err);
1359 return err;
1362 static const struct got_error *
1363 enumeration_request(struct imsg *imsg, struct imsgbuf *ibuf,
1364 struct got_pack *pack, struct got_packidx *packidx,
1365 struct got_object_cache *objcache)
1367 const struct got_error *err = NULL;
1368 struct got_object_id_queue commit_ids;
1369 const struct got_object_id_queue *parents = NULL;
1370 struct got_object_qid *qid = NULL;
1371 struct got_object *obj = NULL;
1372 struct got_commit_object *commit = NULL;
1373 struct got_object_id *tree_id = NULL;
1374 size_t totlen = 0;
1375 struct got_object_idset *idset;
1376 int i, idx, have_all_entries = 1;
1377 struct enumerated_tree *trees = NULL;
1378 size_t ntrees = 0, nalloc = 16;
1380 STAILQ_INIT(&commit_ids);
1382 trees = calloc(nalloc, sizeof(*trees));
1383 if (trees == NULL)
1384 return got_error_from_errno("calloc");
1386 idset = got_object_idset_alloc();
1387 if (idset == NULL) {
1388 err = got_error_from_errno("got_object_idset_alloc");
1389 goto done;
1392 err = recv_object_id_queue(&commit_ids, ibuf);
1393 if (err)
1394 goto done;
1396 if (STAILQ_EMPTY(&commit_ids)) {
1397 err = got_error(GOT_ERR_PRIVSEP_MSG);
1398 goto done;
1401 err = recv_object_ids(idset, ibuf);
1402 if (err)
1403 goto done;
1405 while (!STAILQ_EMPTY(&commit_ids)) {
1406 if (sigint_received) {
1407 err = got_error(GOT_ERR_CANCELLED);
1408 goto done;
1411 qid = STAILQ_FIRST(&commit_ids);
1412 STAILQ_REMOVE_HEAD(&commit_ids, entry);
1414 if (got_object_idset_contains(idset, &qid->id)) {
1415 got_object_qid_free(qid);
1416 qid = NULL;
1417 continue;
1420 idx = got_packidx_get_object_idx(packidx, &qid->id);
1421 if (idx == -1) {
1422 have_all_entries = 0;
1423 break;
1426 err = open_object(&obj, pack, packidx, idx, &qid->id,
1427 objcache);
1428 if (err)
1429 goto done;
1430 if (obj->type == GOT_OBJ_TYPE_TAG) {
1431 struct got_tag_object *tag;
1432 uint8_t *buf;
1433 size_t len;
1434 err = got_packfile_extract_object_to_mem(&buf,
1435 &len, obj, pack);
1436 if (err)
1437 goto done;
1438 obj->size = len;
1439 err = got_object_parse_tag(&tag, buf, len);
1440 if (err) {
1441 free(buf);
1442 goto done;
1444 idx = got_packidx_get_object_idx(packidx, &tag->id);
1445 if (idx == -1) {
1446 have_all_entries = 0;
1447 break;
1449 err = open_commit(&commit, pack, packidx, idx,
1450 &tag->id, objcache);
1451 got_object_tag_close(tag);
1452 free(buf);
1453 if (err)
1454 goto done;
1455 } else if (obj->type == GOT_OBJ_TYPE_COMMIT) {
1456 err = open_commit(&commit, pack, packidx, idx,
1457 &qid->id, objcache);
1458 if (err)
1459 goto done;
1460 } else {
1461 err = got_error(GOT_ERR_OBJ_TYPE);
1462 goto done;
1464 got_object_close(obj);
1465 obj = NULL;
1467 err = got_privsep_send_enumerated_commit(ibuf, &qid->id,
1468 got_object_commit_get_committer_time(commit));
1469 if (err)
1470 goto done;
1472 tree_id = got_object_commit_get_tree_id(commit);
1473 idx = got_packidx_get_object_idx(packidx, tree_id);
1474 if (idx == -1) {
1475 have_all_entries = 0;
1476 err = got_privsep_send_enumerated_tree(&totlen, ibuf,
1477 tree_id, "/", NULL, -1);
1478 if (err)
1479 goto done;
1480 break;
1483 if (got_object_idset_contains(idset, tree_id)) {
1484 got_object_qid_free(qid);
1485 qid = NULL;
1486 continue;
1489 err = enumerate_tree(&have_all_entries, ibuf, &totlen,
1490 tree_id, "/", pack, packidx, objcache, idset,
1491 &trees, &nalloc, &ntrees);
1492 if (err)
1493 goto done;
1495 if (!have_all_entries)
1496 break;
1498 got_object_qid_free(qid);
1499 qid = NULL;
1501 parents = got_object_commit_get_parent_ids(commit);
1502 if (parents) {
1503 struct got_object_qid *pid;
1504 STAILQ_FOREACH(pid, parents, entry) {
1505 if (got_object_idset_contains(idset, &pid->id))
1506 continue;
1507 err = got_object_qid_alloc_partial(&qid);
1508 if (err)
1509 goto done;
1510 memcpy(&qid->id, &pid->id, sizeof(qid->id));
1511 STAILQ_INSERT_TAIL(&commit_ids, qid, entry);
1512 qid = NULL;
1516 got_object_commit_close(commit);
1517 commit = NULL;
1520 if (have_all_entries) {
1521 err = got_privsep_send_object_enumeration_done(ibuf);
1522 if (err)
1523 goto done;
1524 } else {
1525 err = got_privsep_send_object_enumeration_incomplete(ibuf);
1526 if (err)
1527 goto done;
1529 done:
1530 if (obj)
1531 got_object_close(obj);
1532 if (commit)
1533 got_object_commit_close(commit);
1534 got_object_qid_free(qid);
1535 got_object_id_queue_free(&commit_ids);
1536 if (idset)
1537 got_object_idset_free(idset);
1538 for (i = 0; i < ntrees; i++) {
1539 struct enumerated_tree *tree = &trees[i];
1540 free(tree->buf);
1541 free(tree->path);
1542 free(tree->entries);
1544 free(trees);
1545 return err;
1548 enum findtwixt_color {
1549 COLOR_KEEP = 0,
1550 COLOR_DROP,
1551 COLOR_SKIP,
1552 COLOR_MAX,
1555 static const struct got_error *
1556 paint_commit(struct got_object_qid *qid, intptr_t color)
1558 if (color < 0 || color >= COLOR_MAX)
1559 return got_error(GOT_ERR_RANGE);
1561 qid->data = (void *)color;
1562 return NULL;
1565 static const struct got_error *
1566 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1567 intptr_t color)
1569 const struct got_error *err;
1570 struct got_object_qid *qid;
1572 err = got_object_qid_alloc_partial(&qid);
1573 if (err)
1574 return err;
1576 memcpy(&qid->id, id, sizeof(qid->id));
1577 STAILQ_INSERT_TAIL(ids, qid, entry);
1578 return paint_commit(qid, color);
1581 static const struct got_error *
1582 paint_commits(struct got_object_id_queue *ids, int *nids,
1583 struct got_object_idset *keep, struct got_object_idset *drop,
1584 struct got_object_idset *skip, struct got_pack *pack,
1585 struct got_packidx *packidx, struct imsgbuf *ibuf,
1586 struct got_object_cache *objcache)
1588 const struct got_error *err = NULL;
1589 struct got_commit_object *commit = NULL;
1590 struct got_object_id_queue painted;
1591 const struct got_object_id_queue *parents;
1592 struct got_object_qid *qid = NULL;
1593 int nqueued = *nids, nskip = 0, npainted = 0;
1595 STAILQ_INIT(&painted);
1597 while (!STAILQ_EMPTY(ids) && nskip != nqueued) {
1598 int idx;
1599 intptr_t color;
1601 if (sigint_received) {
1602 err = got_error(GOT_ERR_CANCELLED);
1603 goto done;
1606 qid = STAILQ_FIRST(ids);
1607 idx = got_packidx_get_object_idx(packidx, &qid->id);
1608 if (idx == -1) {
1609 qid = NULL;
1610 break;
1613 STAILQ_REMOVE_HEAD(ids, entry);
1614 nqueued--;
1615 color = (intptr_t)qid->data;
1616 if (color == COLOR_SKIP)
1617 nskip--;
1619 if (got_object_idset_contains(skip, &qid->id)) {
1620 got_object_qid_free(qid);
1621 qid = NULL;
1622 continue;
1625 switch (color) {
1626 case COLOR_KEEP:
1627 if (got_object_idset_contains(keep, &qid->id)) {
1628 got_object_qid_free(qid);
1629 qid = NULL;
1630 continue;
1632 if (got_object_idset_contains(drop, &qid->id)) {
1633 err = paint_commit(qid, COLOR_SKIP);
1634 if (err)
1635 goto done;
1637 err = got_object_idset_add(keep, &qid->id, NULL);
1638 if (err)
1639 goto done;
1640 break;
1641 case COLOR_DROP:
1642 if (got_object_idset_contains(drop, &qid->id)) {
1643 got_object_qid_free(qid);
1644 qid = NULL;
1645 continue;
1647 if (got_object_idset_contains(keep, &qid->id)) {
1648 err = paint_commit(qid, COLOR_SKIP);
1649 if (err)
1650 goto done;
1652 err = got_object_idset_add(drop, &qid->id, NULL);
1653 if (err)
1654 goto done;
1655 break;
1656 case COLOR_SKIP:
1657 if (!got_object_idset_contains(skip, &qid->id)) {
1658 err = got_object_idset_add(skip, &qid->id,
1659 NULL);
1660 if (err)
1661 goto done;
1663 break;
1664 default:
1665 /* should not happen */
1666 err = got_error_fmt(GOT_ERR_NOT_IMPL,
1667 "%s invalid commit color %"PRIdPTR, __func__,
1668 color);
1669 goto done;
1672 err = open_commit(&commit, pack, packidx, idx, &qid->id,
1673 objcache);
1674 if (err)
1675 goto done;
1677 parents = got_object_commit_get_parent_ids(commit);
1678 if (parents) {
1679 struct got_object_qid *pid;
1680 color = (intptr_t)qid->data;
1681 STAILQ_FOREACH(pid, parents, entry) {
1682 err = queue_commit_id(ids, &pid->id, color);
1683 if (err)
1684 goto done;
1685 nqueued++;
1686 if (color == COLOR_SKIP)
1687 nskip++;
1691 got_object_commit_close(commit);
1692 commit = NULL;
1694 STAILQ_INSERT_TAIL(&painted, qid, entry);
1695 qid = NULL;
1696 npainted++;
1698 err = got_privsep_send_painted_commits(ibuf, &painted,
1699 &npainted, 1, 0);
1700 if (err)
1701 goto done;
1704 err = got_privsep_send_painted_commits(ibuf, &painted, &npainted, 1, 1);
1705 if (err)
1706 goto done;
1708 *nids = nqueued;
1709 done:
1710 if (commit)
1711 got_object_commit_close(commit);
1712 got_object_qid_free(qid);
1713 return err;
1716 static void
1717 commit_painting_free(struct got_object_idset **keep,
1718 struct got_object_idset **drop,
1719 struct got_object_idset **skip)
1721 if (*keep) {
1722 got_object_idset_free(*keep);
1723 *keep = NULL;
1725 if (*drop) {
1726 got_object_idset_free(*drop);
1727 *drop = NULL;
1729 if (*skip) {
1730 got_object_idset_free(*skip);
1731 *skip = NULL;
1735 static const struct got_error *
1736 commit_painting_init(struct imsgbuf *ibuf, struct got_object_idset **keep,
1737 struct got_object_idset **drop, struct got_object_idset **skip)
1739 const struct got_error *err = NULL;
1741 *keep = got_object_idset_alloc();
1742 if (*keep == NULL) {
1743 err = got_error_from_errno("got_object_idset_alloc");
1744 goto done;
1746 *drop = got_object_idset_alloc();
1747 if (*drop == NULL) {
1748 err = got_error_from_errno("got_object_idset_alloc");
1749 goto done;
1751 *skip = got_object_idset_alloc();
1752 if (*skip == NULL) {
1753 err = got_error_from_errno("got_object_idset_alloc");
1754 goto done;
1757 err = recv_object_ids(*keep, ibuf);
1758 if (err)
1759 goto done;
1760 err = recv_object_ids(*drop, ibuf);
1761 if (err)
1762 goto done;
1763 err = recv_object_ids(*skip, ibuf);
1764 if (err)
1765 goto done;
1767 done:
1768 if (err)
1769 commit_painting_free(keep, drop, skip);
1771 return err;
1774 static const struct got_error *
1775 commit_painting_request(struct imsg *imsg, struct imsgbuf *ibuf,
1776 struct got_pack *pack, struct got_packidx *packidx,
1777 struct got_object_cache *objcache, struct got_object_idset *keep,
1778 struct got_object_idset *drop, struct got_object_idset *skip)
1780 const struct got_error *err = NULL;
1781 struct got_imsg_commit_painting_request ireq;
1782 struct got_object_id id;
1783 size_t datalen;
1784 struct got_object_id_queue ids;
1785 int nids = 0;
1787 STAILQ_INIT(&ids);
1789 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1790 if (datalen != sizeof(ireq))
1791 return got_error(GOT_ERR_PRIVSEP_LEN);
1792 memcpy(&ireq, imsg->data, sizeof(ireq));
1793 memcpy(id.sha1, ireq.id, SHA1_DIGEST_LENGTH);
1795 err = queue_commit_id(&ids, &id, ireq.color);
1796 if (err)
1797 return err;
1798 nids = 1;
1800 err = paint_commits(&ids, &nids, keep, drop, skip,
1801 pack, packidx, ibuf, objcache);
1802 if (err)
1803 goto done;
1805 err = got_privsep_send_painted_commits(ibuf, &ids, &nids, 0, 1);
1806 if (err)
1807 goto done;
1809 err = got_privsep_send_painting_commits_done(ibuf);
1810 done:
1811 got_object_id_queue_free(&ids);
1812 return err;
1815 static const struct got_error *
1816 receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
1818 const struct got_error *err = NULL;
1819 struct imsg imsg;
1820 struct got_imsg_pack ipack;
1821 size_t datalen;
1822 struct got_pack *pack;
1824 *packp = NULL;
1826 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1827 if (err)
1828 return err;
1830 pack = calloc(1, sizeof(*pack));
1831 if (pack == NULL) {
1832 err = got_error_from_errno("calloc");
1833 goto done;
1836 if (imsg.hdr.type != GOT_IMSG_PACK) {
1837 err = got_error(GOT_ERR_PRIVSEP_MSG);
1838 goto done;
1841 if (imsg.fd == -1) {
1842 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1843 goto done;
1846 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1847 if (datalen != sizeof(ipack)) {
1848 err = got_error(GOT_ERR_PRIVSEP_LEN);
1849 goto done;
1851 memcpy(&ipack, imsg.data, sizeof(ipack));
1853 pack->filesize = ipack.filesize;
1854 pack->fd = dup(imsg.fd);
1855 if (pack->fd == -1) {
1856 err = got_error_from_errno("dup");
1857 goto done;
1859 if (lseek(pack->fd, 0, SEEK_SET) == -1) {
1860 err = got_error_from_errno("lseek");
1861 goto done;
1863 pack->path_packfile = strdup(ipack.path_packfile);
1864 if (pack->path_packfile == NULL) {
1865 err = got_error_from_errno("strdup");
1866 goto done;
1869 err = got_delta_cache_alloc(&pack->delta_cache);
1870 if (err)
1871 goto done;
1873 #ifndef GOT_PACK_NO_MMAP
1874 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1875 pack->fd, 0);
1876 if (pack->map == MAP_FAILED)
1877 pack->map = NULL; /* fall back to read(2) */
1878 #endif
1879 done:
1880 if (err) {
1881 if (imsg.fd != -1)
1882 close(imsg.fd);
1883 free(pack);
1884 } else
1885 *packp = pack;
1886 imsg_free(&imsg);
1887 return err;
1890 int
1891 main(int argc, char *argv[])
1893 const struct got_error *err = NULL;
1894 struct imsgbuf ibuf;
1895 struct imsg imsg;
1896 struct got_packidx *packidx = NULL;
1897 struct got_pack *pack = NULL;
1898 struct got_object_cache objcache;
1899 FILE *basefile = NULL, *accumfile = NULL, *delta_outfile = NULL;
1900 struct got_object_idset *keep = NULL, *drop = NULL, *skip = NULL;
1902 //static int attached;
1903 //while (!attached) sleep(1);
1905 signal(SIGINT, catch_sigint);
1907 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
1909 err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
1910 if (err) {
1911 err = got_error_from_errno("got_object_cache_init");
1912 got_privsep_send_error(&ibuf, err);
1913 return 1;
1916 #ifndef PROFILE
1917 /* revoke access to most system calls */
1918 if (pledge("stdio recvfd", NULL) == -1) {
1919 err = got_error_from_errno("pledge");
1920 got_privsep_send_error(&ibuf, err);
1921 return 1;
1923 #endif
1925 err = receive_packidx(&packidx, &ibuf);
1926 if (err) {
1927 got_privsep_send_error(&ibuf, err);
1928 return 1;
1931 err = receive_pack(&pack, &ibuf);
1932 if (err) {
1933 got_privsep_send_error(&ibuf, err);
1934 return 1;
1937 for (;;) {
1938 imsg.fd = -1;
1940 if (sigint_received) {
1941 err = got_error(GOT_ERR_CANCELLED);
1942 break;
1945 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1946 if (err) {
1947 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1948 err = NULL;
1949 break;
1952 if (imsg.hdr.type == GOT_IMSG_STOP)
1953 break;
1955 switch (imsg.hdr.type) {
1956 case GOT_IMSG_TMPFD:
1957 if (basefile == NULL) {
1958 err = receive_tempfile(&basefile, "w+",
1959 &imsg, &ibuf);
1960 } else if (accumfile == NULL) {
1961 err = receive_tempfile(&accumfile, "w+",
1962 &imsg, &ibuf);
1963 } else
1964 err = got_error(GOT_ERR_PRIVSEP_MSG);
1965 break;
1966 case GOT_IMSG_PACKED_OBJECT_REQUEST:
1967 err = object_request(&imsg, &ibuf, pack, packidx,
1968 &objcache);
1969 break;
1970 case GOT_IMSG_PACKED_RAW_OBJECT_REQUEST:
1971 if (basefile == NULL || accumfile == NULL) {
1972 err = got_error(GOT_ERR_PRIVSEP_MSG);
1973 break;
1975 err = raw_object_request(&imsg, &ibuf, pack, packidx,
1976 &objcache, basefile, accumfile);
1977 break;
1978 case GOT_IMSG_RAW_DELTA_OUTFD:
1979 if (delta_outfile != NULL) {
1980 err = got_error(GOT_ERR_PRIVSEP_MSG);
1981 break;
1983 err = receive_tempfile(&delta_outfile, "w",
1984 &imsg, &ibuf);
1985 break;
1986 case GOT_IMSG_RAW_DELTA_REQUEST:
1987 if (delta_outfile == NULL) {
1988 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1989 break;
1991 err = raw_delta_request(&imsg, &ibuf, delta_outfile,
1992 pack, packidx);
1993 break;
1994 case GOT_IMSG_DELTA_REUSE_REQUEST:
1995 if (delta_outfile == NULL) {
1996 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1997 break;
1999 err = delta_reuse_request(&imsg, &ibuf,
2000 delta_outfile, pack, packidx);
2001 break;
2002 case GOT_IMSG_COMMIT_REQUEST:
2003 err = commit_request(&imsg, &ibuf, pack, packidx,
2004 &objcache);
2005 break;
2006 case GOT_IMSG_TREE_REQUEST:
2007 err = tree_request(&imsg, &ibuf, pack, packidx,
2008 &objcache);
2009 break;
2010 case GOT_IMSG_BLOB_REQUEST:
2011 if (basefile == NULL || accumfile == NULL) {
2012 err = got_error(GOT_ERR_PRIVSEP_MSG);
2013 break;
2015 err = blob_request(&imsg, &ibuf, pack, packidx,
2016 &objcache, basefile, accumfile);
2017 break;
2018 case GOT_IMSG_TAG_REQUEST:
2019 err = tag_request(&imsg, &ibuf, pack, packidx,
2020 &objcache);
2021 break;
2022 case GOT_IMSG_COMMIT_TRAVERSAL_REQUEST:
2023 err = commit_traversal_request(&imsg, &ibuf, pack,
2024 packidx, &objcache);
2025 break;
2026 case GOT_IMSG_OBJECT_ENUMERATION_REQUEST:
2027 err = enumeration_request(&imsg, &ibuf, pack,
2028 packidx, &objcache);
2029 break;
2030 case GOT_IMSG_COMMIT_PAINTING_INIT:
2031 commit_painting_free(&keep, &drop, &skip);
2032 err = commit_painting_init(&ibuf, &keep, &drop, &skip);
2033 break;
2034 case GOT_IMSG_COMMIT_PAINTING_REQUEST:
2035 if (keep == NULL || drop == NULL || skip == NULL) {
2036 err = got_error(GOT_ERR_PRIVSEP_MSG);
2037 break;
2039 err = commit_painting_request(&imsg, &ibuf, pack,
2040 packidx, &objcache, keep, drop, skip);
2041 break;
2042 case GOT_IMSG_COMMIT_PAINTING_DONE:
2043 commit_painting_free(&keep, &drop, &skip);
2044 break;
2045 default:
2046 err = got_error(GOT_ERR_PRIVSEP_MSG);
2047 break;
2050 if (imsg.fd != -1 && close(imsg.fd) == -1 && err == NULL)
2051 err = got_error_from_errno("close");
2052 imsg_free(&imsg);
2053 if (err)
2054 break;
2057 commit_painting_free(&keep, &drop, &skip);
2058 if (packidx)
2059 got_packidx_close(packidx);
2060 if (pack)
2061 got_pack_close(pack);
2062 got_object_cache_close(&objcache);
2063 imsg_clear(&ibuf);
2064 if (basefile && fclose(basefile) == EOF && err == NULL)
2065 err = got_error_from_errno("fclose");
2066 if (accumfile && fclose(accumfile) == EOF && err == NULL)
2067 err = got_error_from_errno("fclose");
2068 if (delta_outfile && fclose(delta_outfile) == EOF && err == NULL)
2069 err = got_error_from_errno("fclose");
2070 if (err) {
2071 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
2072 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
2073 got_privsep_send_error(&ibuf, err);
2076 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
2077 err = got_error_from_errno("close");
2078 return err ? 1 : 0;