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_qid.h"
45 #include "got_lib_object_cache.h"
46 #include "got_lib_object_parse.h"
47 #include "got_lib_object_idset.h"
48 #include "got_lib_privsep.h"
49 #include "got_lib_pack.h"
51 #ifndef nitems
52 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
53 #endif
55 static volatile sig_atomic_t sigint_received;
57 static void
58 catch_sigint(int signo)
59 {
60 sigint_received = 1;
61 }
63 static const struct got_error *
64 open_object(struct got_object **obj, struct got_pack *pack,
65 struct got_packidx *packidx, int idx, struct got_object_id *id,
66 struct got_object_cache *objcache)
67 {
68 const struct got_error *err;
70 err = got_packfile_open_object(obj, pack, packidx, idx, id);
71 if (err)
72 return err;
73 (*obj)->refcnt++;
75 err = got_object_cache_add(objcache, id, *obj);
76 if (err) {
77 if (err->code == GOT_ERR_OBJ_EXISTS ||
78 err->code == GOT_ERR_OBJ_TOO_LARGE)
79 err = NULL;
80 return err;
81 }
82 (*obj)->refcnt++;
83 return NULL;
84 }
86 static const struct got_error *
87 object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
88 struct got_packidx *packidx, struct got_object_cache *objcache)
89 {
90 const struct got_error *err = NULL;
91 struct got_imsg_packed_object iobj;
92 struct got_object *obj;
93 struct got_object_id id;
94 size_t datalen;
96 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
97 if (datalen != sizeof(iobj))
98 return got_error(GOT_ERR_PRIVSEP_LEN);
99 memcpy(&iobj, imsg->data, sizeof(iobj));
100 memcpy(&id, &iobj.id, sizeof(id));
102 obj = got_object_cache_get(objcache, &id);
103 if (obj) {
104 obj->refcnt++;
105 } else {
106 err = open_object(&obj, pack, packidx, iobj.idx, &id,
107 objcache);
108 if (err)
109 goto done;
112 err = got_privsep_send_obj(ibuf, obj);
113 done:
114 got_object_close(obj);
115 return err;
118 static const struct got_error *
119 open_commit(struct got_commit_object **commit, struct got_pack *pack,
120 struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
121 struct got_object_cache *objcache)
123 const struct got_error *err = NULL;
124 struct got_object *obj = NULL;
125 uint8_t *buf = NULL;
126 size_t len;
128 *commit = NULL;
130 obj = got_object_cache_get(objcache, id);
131 if (obj) {
132 obj->refcnt++;
133 } else {
134 err = open_object(&obj, pack, packidx, obj_idx, id,
135 objcache);
136 if (err)
137 return err;
140 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
141 if (err)
142 goto done;
144 obj->size = len;
146 err = got_object_parse_commit(commit, buf, len);
147 done:
148 got_object_close(obj);
149 free(buf);
150 return err;
153 static const struct got_error *
154 commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
155 struct got_packidx *packidx, struct got_object_cache *objcache)
157 const struct got_error *err = NULL;
158 struct got_imsg_packed_object iobj;
159 struct got_commit_object *commit = NULL;
160 struct got_object_id id;
161 size_t datalen;
163 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
164 if (datalen != sizeof(iobj))
165 return got_error(GOT_ERR_PRIVSEP_LEN);
166 memcpy(&iobj, imsg->data, sizeof(iobj));
167 memcpy(&id, &iobj.id, sizeof(id));
169 err = open_commit(&commit, pack, packidx, iobj.idx, &id, objcache);
170 if (err)
171 goto done;
173 err = got_privsep_send_commit(ibuf, commit);
174 done:
175 if (commit)
176 got_object_commit_close(commit);
177 if (err) {
178 if (err->code == GOT_ERR_PRIVSEP_PIPE)
179 err = NULL;
180 else
181 got_privsep_send_error(ibuf, err);
184 return err;
187 static const struct got_error *
188 open_tree(uint8_t **buf, size_t *len,
189 struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
190 struct got_object_id *id, struct got_object_cache *objcache)
192 const struct got_error *err = NULL;
193 struct got_object *obj = NULL;
194 int cached = 0;
196 *buf = NULL;
197 *len = 0;
199 obj = got_object_cache_get(objcache, id);
200 if (obj) {
201 obj->refcnt++;
202 cached = 1;
203 } else {
204 err = open_object(&obj, pack, packidx, obj_idx, id,
205 objcache);
206 if (err)
207 return err;
210 err = got_packfile_extract_object_to_mem(buf, len, obj, pack);
211 if (err)
212 goto done;
214 if (!cached)
215 obj->size = *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 size_t len = 0;
235 struct got_object_id id;
236 size_t datalen;
238 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
239 if (datalen != sizeof(iobj))
240 return got_error(GOT_ERR_PRIVSEP_LEN);
241 memcpy(&iobj, imsg->data, sizeof(iobj));
242 memcpy(&id, &iobj.id, sizeof(id));
244 err = open_tree(&buf, &len, pack, packidx, iobj.idx, &id, objcache);
245 if (err)
246 return err;
248 err = got_object_parse_tree(entries, nentries, nentries_alloc,
249 buf, len);
250 if (err)
251 goto done;
253 err = got_privsep_send_tree(ibuf, *entries, *nentries);
254 if (err) {
255 if (err->code == GOT_ERR_PRIVSEP_PIPE)
256 err = NULL;
257 else
258 got_privsep_send_error(ibuf, err);
260 done:
261 free(buf);
262 return err;
265 static const struct got_error *
266 receive_file(FILE **f, struct imsgbuf *ibuf, uint32_t imsg_code)
268 const struct got_error *err;
269 struct imsg imsg;
270 size_t datalen;
271 int fd;
273 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
274 if (err)
275 return err;
277 if (imsg.hdr.type != imsg_code) {
278 err = got_error(GOT_ERR_PRIVSEP_MSG);
279 goto done;
282 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
283 if (datalen != 0) {
284 err = got_error(GOT_ERR_PRIVSEP_LEN);
285 goto done;
287 fd = imsg_get_fd(&imsg);
288 if (fd == -1) {
289 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
290 goto done;
293 *f = fdopen(fd, "w+");
294 if (*f == NULL) {
295 err = got_error_from_errno("fdopen");
296 close(fd);
297 goto done;
299 done:
300 imsg_free(&imsg);
301 return err;
304 static const struct got_error *
305 receive_tempfile(FILE **f, const char *mode, struct imsg *imsg,
306 struct imsgbuf *ibuf)
308 const struct got_error *err;
309 size_t datalen;
310 int fd;
312 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
313 if (datalen != 0)
314 return got_error(GOT_ERR_PRIVSEP_LEN);
316 fd = imsg_get_fd(imsg);
317 if (fd == -1)
318 return got_error(GOT_ERR_PRIVSEP_NO_FD);
320 *f = fdopen(fd, mode);
321 if (*f == NULL) {
322 err = got_error_from_errno("fdopen");
323 close(fd);
324 return err;
327 return NULL;
330 static const struct got_error *
331 blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
332 struct got_packidx *packidx, struct got_object_cache *objcache,
333 FILE *basefile, FILE *accumfile)
335 const struct got_error *err = NULL;
336 struct got_imsg_packed_object iobj;
337 struct got_object *obj = NULL;
338 FILE *outfile = NULL;
339 struct got_object_id id;
340 size_t datalen;
341 uint64_t blob_size;
342 uint8_t *buf = NULL;
344 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
345 if (datalen != sizeof(iobj))
346 return got_error(GOT_ERR_PRIVSEP_LEN);
347 memcpy(&iobj, imsg->data, sizeof(iobj));
348 memcpy(&id, &iobj.id, sizeof(id));
350 obj = got_object_cache_get(objcache, &id);
351 if (obj) {
352 obj->refcnt++;
353 } else {
354 err = open_object(&obj, pack, packidx, iobj.idx, &id,
355 objcache);
356 if (err)
357 return err;
360 err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
361 if (err)
362 goto done;
364 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
365 err = got_pack_get_max_delta_object_size(&blob_size, obj, pack);
366 if (err)
367 goto done;
368 } else
369 blob_size = obj->size;
371 if (blob_size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
372 err = got_packfile_extract_object_to_mem(&buf, &obj->size,
373 obj, pack);
374 else
375 err = got_packfile_extract_object(pack, obj, outfile, basefile,
376 accumfile);
377 if (err)
378 goto done;
380 err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, buf);
381 done:
382 free(buf);
383 if (outfile && fclose(outfile) == EOF && err == NULL)
384 err = got_error_from_errno("fclose");
385 got_object_close(obj);
386 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
387 got_privsep_send_error(ibuf, err);
389 return err;
392 static const struct got_error *
393 tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
394 struct got_packidx *packidx, struct got_object_cache *objcache)
396 const struct got_error *err = NULL;
397 struct got_imsg_packed_object iobj;
398 struct got_object *obj = NULL;
399 struct got_tag_object *tag = NULL;
400 uint8_t *buf = NULL;
401 size_t len;
402 struct got_object_id id;
403 size_t datalen;
405 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
406 if (datalen != sizeof(iobj))
407 return got_error(GOT_ERR_PRIVSEP_LEN);
408 memcpy(&iobj, imsg->data, sizeof(iobj));
409 memcpy(&id, &iobj.id, sizeof(id));
411 obj = got_object_cache_get(objcache, &id);
412 if (obj) {
413 obj->refcnt++;
414 } else {
415 err = open_object(&obj, pack, packidx, iobj.idx, &id,
416 objcache);
417 if (err)
418 return err;
421 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
422 if (err)
423 goto done;
425 obj->size = len;
426 err = got_object_parse_tag(&tag, buf, len);
427 if (err)
428 goto done;
430 err = got_privsep_send_tag(ibuf, tag);
431 done:
432 free(buf);
433 got_object_close(obj);
434 if (tag)
435 got_object_tag_close(tag);
436 if (err) {
437 if (err->code == GOT_ERR_PRIVSEP_PIPE)
438 err = NULL;
439 else
440 got_privsep_send_error(ibuf, err);
443 return err;
446 static const struct got_error *
447 tree_path_changed(int *changed, uint8_t **buf1, size_t *len1,
448 uint8_t **buf2, size_t *len2, const char *path,
449 struct got_pack *pack, struct got_packidx *packidx,
450 struct imsgbuf *ibuf, struct got_object_cache *objcache)
452 const struct got_error *err = NULL;
453 struct got_parsed_tree_entry pte1, pte2;
454 const char *seg, *s;
455 size_t seglen;
456 size_t remain1 = *len1, remain2 = *len2, elen;
457 uint8_t *next_entry1 = *buf1;
458 uint8_t *next_entry2 = *buf2;
460 memset(&pte1, 0, sizeof(pte1));
461 memset(&pte2, 0, sizeof(pte2));
463 *changed = 0;
465 /* We not do support comparing the root path. */
466 if (got_path_is_root_dir(path))
467 return got_error_path(path, GOT_ERR_BAD_PATH);
469 s = path;
470 while (*s == '/')
471 s++;
472 seg = s;
473 seglen = 0;
474 while (*s) {
475 if (*s != '/') {
476 s++;
477 seglen++;
478 if (*s)
479 continue;
482 /*
483 * As an optimization we compare entries in on-disk order
484 * rather than in got_path_cmp() order. We only need to
485 * find out if any entries differ. Parsing all entries and
486 * sorting them slows us down significantly when tree objects
487 * have thousands of entries. We can assume that on-disk entry
488 * ordering is stable, as per got_object_tree_create() and
489 * sort_tree_entries_the_way_git_likes_it(). Other orderings
490 * are incompatible with Git and would yield false positives
491 * here, too.
492 */
493 while (remain1 > 0) {
494 err = got_object_parse_tree_entry(&pte1, &elen,
495 next_entry1, remain1);
496 if (err)
497 return err;
498 next_entry1 += elen;
499 remain1 -= elen;
500 if (strncmp(pte1.name, seg, seglen) != 0 ||
501 pte1.name[seglen] != '\0') {
502 memset(&pte1, 0, sizeof(pte1));
503 continue;
504 } else
505 break;
507 if (pte1.name == NULL) {
508 err = got_error(GOT_ERR_NO_OBJ);
509 break;
512 if (remain2 == 0) {
513 *changed = 1;
514 break;
517 while (remain2 > 0) {
518 err = got_object_parse_tree_entry(&pte2, &elen,
519 next_entry2, remain2);
520 if (err)
521 return err;
522 next_entry2 += elen;
523 remain2 -= elen;
524 if (strncmp(pte2.name, seg, seglen) != 0 ||
525 pte2.name[seglen] != '\0') {
526 memset(&pte2, 0, sizeof(pte2));
527 continue;
528 } else
529 break;
532 if (pte2.name == NULL) {
533 *changed = 1;
534 break;
537 if (pte1.mode != pte2.mode) {
538 *changed = 1;
539 break;
542 if (memcmp(pte1.id, pte2.id, SHA1_DIGEST_LENGTH) == 0) {
543 *changed = 0;
544 break;
547 if (*s == '\0') { /* final path element */
548 *changed = 1;
549 break;
552 seg = s + 1;
553 s++;
554 seglen = 0;
555 if (*s) {
556 struct got_object_id id1, id2;
557 int idx;
559 memcpy(id1.sha1, pte1.id, SHA1_DIGEST_LENGTH);
560 idx = got_packidx_get_object_idx(packidx, &id1);
561 if (idx == -1) {
562 err = got_error_no_obj(&id1);
563 break;
565 free(*buf1);
566 *buf1 = NULL;
567 err = open_tree(buf1, len1, pack, packidx, idx, &id1,
568 objcache);
569 memset(&pte1, 0, sizeof(pte1));
570 if (err)
571 break;
572 next_entry1 = *buf1;
573 remain1 = *len1;
575 memcpy(id2.sha1, pte2.id, SHA1_DIGEST_LENGTH);
576 idx = got_packidx_get_object_idx(packidx, &id2);
577 if (idx == -1) {
578 err = got_error_no_obj(&id2);
579 break;
581 free(*buf2);
582 *buf2 = NULL;
583 err = open_tree(buf2, len2, pack, packidx, idx, &id2,
584 objcache);
585 memset(&pte2, 0, sizeof(pte2));
586 if (err)
587 break;
588 next_entry2 = *buf2;
589 remain2 = *len2;
593 return err;
596 static const struct got_error *
597 send_traversed_commits(struct got_object_id *commit_ids, size_t ncommits,
598 struct imsgbuf *ibuf)
600 struct ibuf *wbuf;
601 size_t i;
603 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
604 sizeof(struct got_imsg_traversed_commits) +
605 ncommits * sizeof(commit_ids[0]));
606 if (wbuf == NULL)
607 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
609 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1)
610 return got_error_from_errno("imsg_add TRAVERSED_COMMITS");
612 for (i = 0; i < ncommits; i++) {
613 struct got_object_id *id = &commit_ids[i];
614 if (imsg_add(wbuf, id, sizeof(*id)) == -1) {
615 return got_error_from_errno(
616 "imsg_add TRAVERSED_COMMITS");
620 imsg_close(ibuf, wbuf);
622 return got_privsep_flush_imsg(ibuf);
625 static const struct got_error *
626 send_commit_traversal_done(struct imsgbuf *ibuf)
628 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
629 NULL, 0) == -1)
630 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
632 return got_privsep_flush_imsg(ibuf);
635 static const struct got_error *
636 commit_traversal_request(struct imsg *imsg, struct imsgbuf *ibuf,
637 struct got_pack *pack, struct got_packidx *packidx,
638 struct got_object_cache *objcache)
640 const struct got_error *err = NULL;
641 struct got_imsg_commit_traversal_request ctreq;
642 struct got_object_qid *pid;
643 struct got_commit_object *commit = NULL, *pcommit = NULL;
644 struct got_object_id id;
645 size_t datalen;
646 char *path = NULL;
647 const int min_alloc = 64;
648 int changed = 0, ncommits = 0, nallocated = 0;
649 struct got_object_id *commit_ids = NULL;
651 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
652 if (datalen < sizeof(ctreq))
653 return got_error(GOT_ERR_PRIVSEP_LEN);
654 memcpy(&ctreq, imsg->data, sizeof(ctreq));
655 memcpy(&id, &ctreq.iobj.id, sizeof(id));
657 if (datalen != sizeof(ctreq) + ctreq.path_len)
658 return got_error(GOT_ERR_PRIVSEP_LEN);
659 if (ctreq.path_len == 0)
660 return got_error(GOT_ERR_PRIVSEP_LEN);
662 path = strndup(imsg->data + sizeof(ctreq), ctreq.path_len);
663 if (path == NULL)
664 return got_error_from_errno("strndup");
666 nallocated = min_alloc;
667 commit_ids = reallocarray(NULL, nallocated, sizeof(*commit_ids));
668 if (commit_ids == NULL)
669 return got_error_from_errno("reallocarray");
671 do {
672 const size_t max_datalen = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
673 int idx;
675 if (sigint_received) {
676 err = got_error(GOT_ERR_CANCELLED);
677 goto done;
680 if (commit == NULL) {
681 idx = got_packidx_get_object_idx(packidx, &id);
682 if (idx == -1)
683 break;
684 err = open_commit(&commit, pack, packidx,
685 idx, &id, objcache);
686 if (err) {
687 if (err->code != GOT_ERR_NO_OBJ)
688 goto done;
689 err = NULL;
690 break;
694 if (sizeof(struct got_imsg_traversed_commits) +
695 ncommits * sizeof(commit_ids[0]) >= max_datalen) {
696 err = send_traversed_commits(commit_ids, ncommits,
697 ibuf);
698 if (err)
699 goto done;
700 ncommits = 0;
702 ncommits++;
703 if (ncommits > nallocated) {
704 struct got_object_id *new;
705 nallocated += min_alloc;
706 new = reallocarray(commit_ids, nallocated,
707 sizeof(*commit_ids));
708 if (new == NULL) {
709 err = got_error_from_errno("reallocarray");
710 goto done;
712 commit_ids = new;
714 memcpy(&commit_ids[ncommits - 1], &id, sizeof(id));
716 pid = STAILQ_FIRST(&commit->parent_ids);
717 if (pid == NULL)
718 break;
720 idx = got_packidx_get_object_idx(packidx, &pid->id);
721 if (idx == -1)
722 break;
724 err = open_commit(&pcommit, pack, packidx, idx, &pid->id,
725 objcache);
726 if (err) {
727 if (err->code != GOT_ERR_NO_OBJ)
728 goto done;
729 err = NULL;
730 break;
733 if (path[0] == '/' && path[1] == '\0') {
734 if (got_object_id_cmp(pcommit->tree_id,
735 commit->tree_id) != 0) {
736 changed = 1;
737 break;
739 } else {
740 int pidx;
741 uint8_t *buf = NULL, *pbuf = NULL;
742 size_t len = 0, plen = 0;
744 idx = got_packidx_get_object_idx(packidx,
745 commit->tree_id);
746 if (idx == -1)
747 break;
748 pidx = got_packidx_get_object_idx(packidx,
749 pcommit->tree_id);
750 if (pidx == -1)
751 break;
753 err = open_tree(&buf, &len, pack, packidx, idx,
754 commit->tree_id, objcache);
755 if (err)
756 goto done;
758 err = open_tree(&pbuf, &plen, pack, packidx, pidx,
759 pcommit->tree_id, objcache);
760 if (err) {
761 free(buf);
762 goto done;
765 err = tree_path_changed(&changed, &buf, &len,
766 &pbuf, &plen, path, pack, packidx, ibuf,
767 objcache);
769 free(buf);
770 free(pbuf);
771 if (err) {
772 if (err->code != GOT_ERR_NO_OBJ)
773 goto done;
774 err = NULL;
775 break;
779 if (!changed) {
780 memcpy(&id, &pid->id, sizeof(id));
781 got_object_commit_close(commit);
782 commit = pcommit;
783 pcommit = NULL;
785 } while (!changed);
787 if (ncommits > 0) {
788 err = send_traversed_commits(commit_ids, ncommits, ibuf);
789 if (err)
790 goto done;
792 if (changed) {
793 err = got_privsep_send_commit(ibuf, commit);
794 if (err)
795 goto done;
798 err = send_commit_traversal_done(ibuf);
799 done:
800 free(path);
801 free(commit_ids);
802 if (commit)
803 got_object_commit_close(commit);
804 if (pcommit)
805 got_object_commit_close(pcommit);
806 if (err) {
807 if (err->code == GOT_ERR_PRIVSEP_PIPE)
808 err = NULL;
809 else
810 got_privsep_send_error(ibuf, err);
813 return err;
816 static const struct got_error *
817 raw_object_request(struct imsg *imsg, struct imsgbuf *ibuf,
818 struct got_pack *pack, struct got_packidx *packidx,
819 struct got_object_cache *objcache, FILE *basefile, FILE *accumfile)
821 const struct got_error *err = NULL;
822 uint8_t *buf = NULL;
823 uint64_t size = 0;
824 FILE *outfile = NULL;
825 struct got_imsg_packed_object iobj;
826 struct got_object *obj;
827 struct got_object_id id;
828 size_t datalen;
830 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
831 if (datalen != sizeof(iobj))
832 return got_error(GOT_ERR_PRIVSEP_LEN);
833 memcpy(&iobj, imsg->data, sizeof(iobj));
834 memcpy(&id, &iobj.id, sizeof(id));
836 obj = got_object_cache_get(objcache, &id);
837 if (obj) {
838 obj->refcnt++;
839 } else {
840 err = open_object(&obj, pack, packidx, iobj.idx, &id,
841 objcache);
842 if (err)
843 return err;
846 err = receive_file(&outfile, ibuf, GOT_IMSG_RAW_OBJECT_OUTFD);
847 if (err)
848 return err;
850 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
851 err = got_pack_get_max_delta_object_size(&size, obj, pack);
852 if (err)
853 goto done;
854 } else
855 size = obj->size;
857 if (size <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
858 err = got_packfile_extract_object_to_mem(&buf, &obj->size,
859 obj, pack);
860 else
861 err = got_packfile_extract_object(pack, obj, outfile, basefile,
862 accumfile);
863 if (err)
864 goto done;
866 err = got_privsep_send_raw_obj(ibuf, obj->size, obj->hdrlen, buf);
867 done:
868 free(buf);
869 if (outfile && fclose(outfile) == EOF && err == NULL)
870 err = got_error_from_errno("fclose");
871 got_object_close(obj);
872 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
873 got_privsep_send_error(ibuf, err);
875 return err;
878 static const struct got_error *
879 get_base_object_id(struct got_object_id *base_id, struct got_packidx *packidx,
880 off_t base_offset)
882 const struct got_error *err;
883 int idx;
885 err = got_packidx_get_offset_idx(&idx, packidx, base_offset);
886 if (err)
887 return err;
888 if (idx == -1)
889 return got_error(GOT_ERR_BAD_PACKIDX);
891 return got_packidx_get_object_id(base_id, packidx, idx);
894 static const struct got_error *
895 raw_delta_request(struct imsg *imsg, struct imsgbuf *ibuf,
896 FILE *delta_outfile, struct got_pack *pack,
897 struct got_packidx *packidx)
899 const struct got_error *err = NULL;
900 struct got_imsg_raw_delta_request req;
901 size_t datalen, delta_size, delta_compressed_size;
902 off_t delta_offset, delta_data_offset;
903 uint8_t *delta_buf = NULL;
904 struct got_object_id id, base_id;
905 off_t base_offset, delta_out_offset = 0;
906 uint64_t base_size = 0, result_size = 0;
907 size_t w;
909 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
910 if (datalen != sizeof(req))
911 return got_error(GOT_ERR_PRIVSEP_LEN);
912 memcpy(&req, imsg->data, sizeof(req));
913 memcpy(&id, &req.id, sizeof(id));
915 err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
916 &delta_compressed_size, &delta_offset, &delta_data_offset,
917 &base_offset, &base_id, &base_size, &result_size,
918 pack, packidx, req.idx);
919 if (err)
920 goto done;
922 /*
923 * If this is an offset delta we must determine the base
924 * object ID ourselves.
925 */
926 if (base_offset != 0) {
927 err = get_base_object_id(&base_id, packidx, base_offset);
928 if (err)
929 goto done;
932 delta_out_offset = ftello(delta_outfile);
933 w = fwrite(delta_buf, 1, delta_compressed_size, delta_outfile);
934 if (w != delta_compressed_size) {
935 err = got_ferror(delta_outfile, GOT_ERR_IO);
936 goto done;
938 if (fflush(delta_outfile) == -1) {
939 err = got_error_from_errno("fflush");
940 goto done;
943 err = got_privsep_send_raw_delta(ibuf, base_size, result_size,
944 delta_size, delta_compressed_size, delta_offset, delta_out_offset,
945 &base_id);
946 done:
947 free(delta_buf);
948 return err;
951 struct search_deltas_arg {
952 struct imsgbuf *ibuf;
953 struct got_packidx *packidx;
954 struct got_pack *pack;
955 struct got_object_idset *idset;
956 struct got_imsg_reused_delta deltas[GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS];
957 size_t ndeltas;
958 };
960 static const struct got_error *
961 search_delta_for_object(struct got_object_id *id, void *data, void *arg)
963 const struct got_error *err;
964 struct search_deltas_arg *a = arg;
965 int obj_idx;
966 uint8_t *delta_buf = NULL;
967 uint64_t base_size, result_size;
968 size_t delta_size, delta_compressed_size;
969 off_t delta_offset, delta_data_offset, base_offset;
970 struct got_object_id base_id;
972 if (sigint_received)
973 return got_error(GOT_ERR_CANCELLED);
975 obj_idx = got_packidx_get_object_idx(a->packidx, id);
976 if (obj_idx == -1)
977 return NULL; /* object not present in our pack file */
979 err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
980 &delta_compressed_size, &delta_offset, &delta_data_offset,
981 &base_offset, &base_id, &base_size, &result_size,
982 a->pack, a->packidx, obj_idx);
983 if (err) {
984 if (err->code == GOT_ERR_OBJ_TYPE)
985 return NULL; /* object not stored as a delta */
986 return err;
989 /*
990 * If this is an offset delta we must determine the base
991 * object ID ourselves.
992 */
993 if (base_offset != 0) {
994 err = get_base_object_id(&base_id, a->packidx, base_offset);
995 if (err)
996 goto done;
999 if (got_object_idset_contains(a->idset, &base_id)) {
1000 struct got_imsg_reused_delta *delta;
1002 delta = &a->deltas[a->ndeltas++];
1003 memcpy(&delta->id, id, sizeof(delta->id));
1004 memcpy(&delta->base_id, &base_id, sizeof(delta->base_id));
1005 delta->base_size = base_size;
1006 delta->result_size = result_size;
1007 delta->delta_size = delta_size;
1008 delta->delta_compressed_size = delta_compressed_size;
1009 delta->delta_offset = delta_data_offset;
1011 if (a->ndeltas >= GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS) {
1012 err = got_privsep_send_reused_deltas(a->ibuf,
1013 a->deltas, a->ndeltas);
1014 if (err)
1015 goto done;
1016 a->ndeltas = 0;
1019 done:
1020 free(delta_buf);
1021 return err;
1024 static const struct got_error *
1025 recv_object_ids(struct got_object_idset *idset, struct imsgbuf *ibuf)
1027 const struct got_error *err = NULL;
1028 int done = 0;
1029 struct got_object_id *ids;
1030 size_t nids, i;
1032 for (;;) {
1033 err = got_privsep_recv_object_idlist(&done, &ids, &nids, ibuf);
1034 if (err || done)
1035 break;
1036 for (i = 0; i < nids; i++) {
1037 err = got_object_idset_add(idset, &ids[i], NULL);
1038 if (err) {
1039 free(ids);
1040 return err;
1043 free(ids);
1046 return err;
1049 static const struct got_error *
1050 recv_object_id_queue(struct got_object_id_queue *queue,
1051 struct got_object_idset *queued_ids, struct imsgbuf *ibuf)
1053 const struct got_error *err = NULL;
1054 int done = 0;
1055 struct got_object_qid *qid;
1056 struct got_object_id *ids;
1057 size_t nids, i;
1059 for (;;) {
1060 err = got_privsep_recv_object_idlist(&done, &ids, &nids, ibuf);
1061 if (err || done)
1062 break;
1063 for (i = 0; i < nids; i++) {
1064 err = got_object_qid_alloc_partial(&qid);
1065 if (err)
1066 return err;
1067 memcpy(&qid->id, &ids[i], sizeof(qid->id));
1068 STAILQ_INSERT_TAIL(queue, qid, entry);
1069 err = got_object_idset_add(queued_ids, &qid->id, NULL);
1070 if (err)
1071 return err;
1075 return err;
1078 static const struct got_error *
1079 delta_reuse_request(struct imsg *imsg, struct imsgbuf *ibuf,
1080 struct got_pack *pack, struct got_packidx *packidx)
1082 const struct got_error *err = NULL;
1083 struct got_object_idset *idset;
1084 struct search_deltas_arg sda;
1086 idset = got_object_idset_alloc();
1087 if (idset == NULL)
1088 return got_error_from_errno("got_object_idset_alloc");
1090 err = recv_object_ids(idset, ibuf);
1091 if (err)
1092 return err;
1094 memset(&sda, 0, sizeof(sda));
1095 sda.ibuf = ibuf;
1096 sda.idset = idset;
1097 sda.pack = pack;
1098 sda.packidx = packidx;
1099 err = got_object_idset_for_each(idset, search_delta_for_object, &sda);
1100 if (err)
1101 goto done;
1103 if (sda.ndeltas > 0) {
1104 err = got_privsep_send_reused_deltas(ibuf, sda.deltas,
1105 sda.ndeltas);
1106 if (err)
1107 goto done;
1110 err = got_privsep_send_reused_deltas_done(ibuf);
1111 done:
1112 got_object_idset_free(idset);
1113 return err;
1116 static const struct got_error *
1117 receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
1119 const struct got_error *err = NULL;
1120 struct imsg imsg;
1121 struct got_imsg_packidx ipackidx;
1122 size_t datalen;
1123 struct got_packidx *p;
1125 *packidx = NULL;
1127 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1128 if (err)
1129 return err;
1131 p = calloc(1, sizeof(*p));
1132 if (p == NULL) {
1133 err = got_error_from_errno("calloc");
1134 goto done;
1137 if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
1138 err = got_error(GOT_ERR_PRIVSEP_MSG);
1139 goto done;
1142 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1143 if (datalen != sizeof(ipackidx)) {
1144 err = got_error(GOT_ERR_PRIVSEP_LEN);
1145 goto done;
1147 memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
1149 p->fd = imsg_get_fd(&imsg);
1150 p->len = ipackidx.len;
1151 if (p->fd == -1) {
1152 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1153 goto done;
1155 if (lseek(p->fd, 0, SEEK_SET) == -1) {
1156 err = got_error_from_errno("lseek");
1157 goto done;
1160 #ifndef GOT_PACK_NO_MMAP
1161 if (p->len > 0 && p->len <= SIZE_MAX) {
1162 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
1163 if (p->map == MAP_FAILED)
1164 p->map = NULL; /* fall back to read(2) */
1166 #endif
1167 err = got_packidx_init_hdr(p, 1, ipackidx.packfile_size);
1168 done:
1169 if (err) {
1170 if (p != NULL)
1171 got_packidx_close(p);
1172 } else
1173 *packidx = p;
1174 imsg_free(&imsg);
1175 return err;
1178 static const struct got_error *
1179 send_tree_enumeration_done(struct imsgbuf *ibuf)
1181 if (imsg_compose(ibuf, GOT_IMSG_TREE_ENUMERATION_DONE, 0, 0, -1,
1182 NULL, 0) == -1)
1183 return got_error_from_errno("imsg_compose TREE_ENUMERATION_DONE");
1185 return got_privsep_flush_imsg(ibuf);
1188 struct enumerated_tree {
1189 struct got_object_id id;
1190 char *path;
1191 uint8_t *buf;
1192 struct got_parsed_tree_entry *entries;
1193 int nentries;
1196 static const struct got_error *
1197 enumerate_tree(int *have_all_entries, struct imsgbuf *ibuf, size_t *totlen,
1198 struct got_object_id *tree_id,
1199 const char *path, struct got_pack *pack, struct got_packidx *packidx,
1200 struct got_object_cache *objcache, struct got_object_idset *idset,
1201 struct enumerated_tree **trees, size_t *nalloc, size_t *ntrees)
1203 const struct got_error *err = NULL;
1204 struct got_object_id_queue ids;
1205 struct got_object_qid *qid;
1206 uint8_t *buf = NULL;
1207 size_t len = 0;
1208 struct got_parsed_tree_entry *entries = NULL;
1209 size_t nentries = 0, nentries_alloc = 0, i;
1210 struct enumerated_tree *tree;
1212 *ntrees = 0;
1213 *have_all_entries = 1;
1214 STAILQ_INIT(&ids);
1216 err = got_object_qid_alloc_partial(&qid);
1217 if (err)
1218 return err;
1219 memcpy(&qid->id, tree_id, sizeof(*tree_id));
1220 qid->data = strdup(path);
1221 if (qid->data == NULL) {
1222 err = got_error_from_errno("strdup");
1223 goto done;
1225 STAILQ_INSERT_TAIL(&ids, qid, entry);
1226 qid = NULL;
1228 /* Traverse the tree hierarchy, gather tree object IDs and paths. */
1229 do {
1230 const char *path;
1231 int idx, i;
1233 if (sigint_received) {
1234 err = got_error(GOT_ERR_CANCELLED);
1235 goto done;
1238 qid = STAILQ_FIRST(&ids);
1239 STAILQ_REMOVE_HEAD(&ids, entry);
1240 path = qid->data;
1242 idx = got_packidx_get_object_idx(packidx, &qid->id);
1243 if (idx == -1) {
1244 *have_all_entries = 0;
1245 break;
1248 err = open_tree(&buf, &len, pack, packidx, idx, &qid->id,
1249 objcache);
1250 if (err) {
1251 if (err->code != GOT_ERR_NO_OBJ)
1252 goto done;
1255 err = got_object_parse_tree(&entries, &nentries,
1256 &nentries_alloc, buf, len);
1257 if (err)
1258 goto done;
1260 err = got_object_idset_add(idset, &qid->id, NULL);
1261 if (err)
1262 goto done;
1264 for (i = 0; i < nentries; i++) {
1265 struct got_object_qid *eqid = NULL;
1266 struct got_parsed_tree_entry *pte = &entries[i];
1267 char *p;
1269 if (!S_ISDIR(pte->mode))
1270 continue;
1272 err = got_object_qid_alloc_partial(&eqid);
1273 if (err)
1274 goto done;
1275 memcpy(eqid->id.sha1, pte->id, sizeof(eqid->id.sha1));
1277 if (got_object_idset_contains(idset, &eqid->id)) {
1278 got_object_qid_free(eqid);
1279 continue;
1282 if (asprintf(&p, "%s%s%s", path,
1283 got_path_is_root_dir(path) ? "" : "/",
1284 pte->name) == -1) {
1285 err = got_error_from_errno("asprintf");
1286 got_object_qid_free(eqid);
1287 goto done;
1289 eqid->data = p;
1290 STAILQ_INSERT_TAIL(&ids, eqid, entry);
1293 if (*ntrees >= *nalloc) {
1294 struct enumerated_tree *new;
1295 new = recallocarray(*trees, *nalloc, *nalloc + 16,
1296 sizeof(*new));
1297 if (new == NULL) {
1298 err = got_error_from_errno("malloc");
1299 goto done;
1301 *trees = new;
1302 *nalloc += 16;
1304 tree = &(*trees)[*ntrees];
1305 (*ntrees)++;
1306 memcpy(&tree->id, &qid->id, sizeof(tree->id));
1307 tree->path = qid->data;
1308 tree->buf = buf;
1309 buf = NULL;
1310 tree->entries = entries;
1311 entries = NULL;
1312 nentries_alloc = 0;
1313 tree->nentries = nentries;
1314 nentries = 0;
1316 got_object_qid_free(qid);
1317 qid = NULL;
1318 } while (!STAILQ_EMPTY(&ids));
1320 if (*have_all_entries) {
1321 int i;
1323 * We have managed to traverse all entries in the hierarchy.
1324 * Tell the main process what we have found.
1326 for (i = 0; i < *ntrees; i++) {
1327 tree = &(*trees)[i];
1328 err = got_privsep_send_enumerated_tree(totlen,
1329 ibuf, &tree->id, tree->path, tree->entries,
1330 tree->nentries);
1331 if (err)
1332 goto done;
1333 free(tree->buf);
1334 tree->buf = NULL;
1335 free(tree->path);
1336 tree->path = NULL;
1337 free(tree->entries);
1338 tree->entries = NULL;
1340 *ntrees = 0; /* don't loop again below to free memory */
1342 err = send_tree_enumeration_done(ibuf);
1343 } else {
1345 * We can only load fully packed tree hierarchies on
1346 * behalf of the main process, otherwise the main process
1347 * gets a wrong idea about which tree objects have
1348 * already been traversed.
1349 * Indicate a missing entry for the root of this tree.
1350 * The main process should continue by loading this
1351 * entire tree the slow way.
1353 err = got_privsep_send_enumerated_tree(totlen, ibuf,
1354 tree_id, "/", NULL, -1);
1355 if (err)
1356 goto done;
1358 done:
1359 free(buf);
1360 free(entries);
1361 for (i = 0; i < *ntrees; i++) {
1362 tree = &(*trees)[i];
1363 free(tree->buf);
1364 tree->buf = NULL;
1365 free(tree->path);
1366 tree->path = NULL;
1367 free(tree->entries);
1368 tree->entries = NULL;
1370 if (qid)
1371 free(qid->data);
1372 got_object_qid_free(qid);
1373 got_object_id_queue_free(&ids);
1374 if (err) {
1375 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1376 err = NULL;
1377 else
1378 got_privsep_send_error(ibuf, err);
1381 return err;
1384 static const struct got_error *
1385 enumeration_request(struct imsg *imsg, struct imsgbuf *ibuf,
1386 struct got_pack *pack, struct got_packidx *packidx,
1387 struct got_object_cache *objcache)
1389 const struct got_error *err = NULL;
1390 struct got_object_id_queue commit_ids;
1391 const struct got_object_id_queue *parents = NULL;
1392 struct got_object_qid *qid = NULL;
1393 struct got_object *obj = NULL;
1394 struct got_commit_object *commit = NULL;
1395 struct got_object_id *tree_id = NULL;
1396 size_t totlen = 0;
1397 struct got_object_idset *idset, *queued_ids = NULL;
1398 int i, idx, have_all_entries = 1;
1399 struct enumerated_tree *trees = NULL;
1400 size_t ntrees = 0, nalloc = 16;
1402 STAILQ_INIT(&commit_ids);
1404 trees = calloc(nalloc, sizeof(*trees));
1405 if (trees == NULL)
1406 return got_error_from_errno("calloc");
1408 idset = got_object_idset_alloc();
1409 if (idset == NULL) {
1410 err = got_error_from_errno("got_object_idset_alloc");
1411 goto done;
1414 queued_ids = got_object_idset_alloc();
1415 if (queued_ids == NULL) {
1416 err = got_error_from_errno("got_object_idset_alloc");
1417 goto done;
1420 err = recv_object_id_queue(&commit_ids, queued_ids, ibuf);
1421 if (err)
1422 goto done;
1424 if (STAILQ_EMPTY(&commit_ids)) {
1425 err = got_error(GOT_ERR_PRIVSEP_MSG);
1426 goto done;
1429 err = recv_object_ids(idset, ibuf);
1430 if (err)
1431 goto done;
1433 while (!STAILQ_EMPTY(&commit_ids)) {
1434 if (sigint_received) {
1435 err = got_error(GOT_ERR_CANCELLED);
1436 goto done;
1439 qid = STAILQ_FIRST(&commit_ids);
1440 STAILQ_REMOVE_HEAD(&commit_ids, entry);
1442 if (got_object_idset_contains(idset, &qid->id)) {
1443 got_object_qid_free(qid);
1444 qid = NULL;
1445 continue;
1448 idx = got_packidx_get_object_idx(packidx, &qid->id);
1449 if (idx == -1) {
1450 have_all_entries = 0;
1451 break;
1454 err = open_object(&obj, pack, packidx, idx, &qid->id,
1455 objcache);
1456 if (err)
1457 goto done;
1458 if (obj->type == GOT_OBJ_TYPE_TAG) {
1459 struct got_tag_object *tag;
1460 uint8_t *buf;
1461 size_t len;
1462 err = got_packfile_extract_object_to_mem(&buf,
1463 &len, obj, pack);
1464 if (err)
1465 goto done;
1466 obj->size = len;
1467 err = got_object_parse_tag(&tag, buf, len);
1468 if (err) {
1469 free(buf);
1470 goto done;
1472 idx = got_packidx_get_object_idx(packidx, &tag->id);
1473 if (idx == -1) {
1474 have_all_entries = 0;
1475 break;
1477 err = open_commit(&commit, pack, packidx, idx,
1478 &tag->id, objcache);
1479 got_object_tag_close(tag);
1480 free(buf);
1481 if (err)
1482 goto done;
1483 } else if (obj->type == GOT_OBJ_TYPE_COMMIT) {
1484 err = open_commit(&commit, pack, packidx, idx,
1485 &qid->id, objcache);
1486 if (err)
1487 goto done;
1488 } else {
1489 err = got_error(GOT_ERR_OBJ_TYPE);
1490 goto done;
1492 got_object_close(obj);
1493 obj = NULL;
1495 err = got_privsep_send_enumerated_commit(ibuf, &qid->id,
1496 got_object_commit_get_committer_time(commit));
1497 if (err)
1498 goto done;
1500 tree_id = got_object_commit_get_tree_id(commit);
1501 idx = got_packidx_get_object_idx(packidx, tree_id);
1502 if (idx == -1) {
1503 have_all_entries = 0;
1504 err = got_privsep_send_enumerated_tree(&totlen, ibuf,
1505 tree_id, "/", NULL, -1);
1506 if (err)
1507 goto done;
1508 break;
1511 if (got_object_idset_contains(idset, tree_id)) {
1512 got_object_qid_free(qid);
1513 qid = NULL;
1514 err = send_tree_enumeration_done(ibuf);
1515 if (err)
1516 goto done;
1517 continue;
1520 err = enumerate_tree(&have_all_entries, ibuf, &totlen,
1521 tree_id, "/", pack, packidx, objcache, idset,
1522 &trees, &nalloc, &ntrees);
1523 if (err)
1524 goto done;
1526 if (!have_all_entries)
1527 break;
1529 got_object_qid_free(qid);
1530 qid = NULL;
1532 parents = got_object_commit_get_parent_ids(commit);
1533 if (parents) {
1534 struct got_object_qid *pid;
1535 STAILQ_FOREACH(pid, parents, entry) {
1536 if (got_object_idset_contains(idset, &pid->id))
1537 continue;
1538 if (got_object_idset_contains(queued_ids, &pid->id))
1539 continue;
1540 err = got_object_qid_alloc_partial(&qid);
1541 if (err)
1542 goto done;
1543 memcpy(&qid->id, &pid->id, sizeof(qid->id));
1544 STAILQ_INSERT_TAIL(&commit_ids, qid, entry);
1545 qid = NULL;
1549 got_object_commit_close(commit);
1550 commit = NULL;
1553 if (have_all_entries) {
1554 err = got_privsep_send_object_enumeration_done(ibuf);
1555 if (err)
1556 goto done;
1557 } else {
1558 err = got_privsep_send_object_enumeration_incomplete(ibuf);
1559 if (err)
1560 goto done;
1562 done:
1563 if (obj)
1564 got_object_close(obj);
1565 if (commit)
1566 got_object_commit_close(commit);
1567 got_object_qid_free(qid);
1568 got_object_id_queue_free(&commit_ids);
1569 if (idset)
1570 got_object_idset_free(idset);
1571 if (queued_ids)
1572 got_object_idset_free(queued_ids);
1573 for (i = 0; i < ntrees; i++) {
1574 struct enumerated_tree *tree = &trees[i];
1575 free(tree->buf);
1576 free(tree->path);
1577 free(tree->entries);
1579 free(trees);
1580 return err;
1583 enum findtwixt_color {
1584 COLOR_KEEP = 0,
1585 COLOR_DROP,
1586 COLOR_SKIP,
1587 COLOR_MAX,
1590 static const struct got_error *
1591 paint_commit(struct got_object_qid *qid, intptr_t color)
1593 if (color < 0 || color >= COLOR_MAX)
1594 return got_error(GOT_ERR_RANGE);
1596 qid->data = (void *)color;
1597 return NULL;
1600 static const struct got_error *
1601 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1602 intptr_t color)
1604 const struct got_error *err;
1605 struct got_object_qid *qid;
1607 err = got_object_qid_alloc_partial(&qid);
1608 if (err)
1609 return err;
1611 memcpy(&qid->id, id, sizeof(qid->id));
1612 STAILQ_INSERT_TAIL(ids, qid, entry);
1613 return paint_commit(qid, color);
1616 static const struct got_error *
1617 paint_commits(struct got_object_id_queue *ids, int *nids,
1618 struct got_object_idset *keep, struct got_object_idset *drop,
1619 struct got_object_idset *skip, struct got_pack *pack,
1620 struct got_packidx *packidx, struct imsgbuf *ibuf,
1621 struct got_object_cache *objcache)
1623 const struct got_error *err = NULL;
1624 struct got_commit_object *commit = NULL;
1625 struct got_object_id_queue painted;
1626 const struct got_object_id_queue *parents;
1627 struct got_object_qid *qid = NULL;
1628 int nqueued = *nids, nskip = 0, npainted = 0;
1630 STAILQ_INIT(&painted);
1632 while (!STAILQ_EMPTY(ids) && nskip != nqueued) {
1633 int idx;
1634 intptr_t color;
1636 if (sigint_received) {
1637 err = got_error(GOT_ERR_CANCELLED);
1638 goto done;
1641 qid = STAILQ_FIRST(ids);
1642 idx = got_packidx_get_object_idx(packidx, &qid->id);
1643 if (idx == -1) {
1644 qid = NULL;
1645 break;
1648 STAILQ_REMOVE_HEAD(ids, entry);
1649 nqueued--;
1650 color = (intptr_t)qid->data;
1651 if (color == COLOR_SKIP)
1652 nskip--;
1654 if (got_object_idset_contains(skip, &qid->id)) {
1655 got_object_qid_free(qid);
1656 qid = NULL;
1657 continue;
1660 switch (color) {
1661 case COLOR_KEEP:
1662 if (got_object_idset_contains(keep, &qid->id)) {
1663 got_object_qid_free(qid);
1664 qid = NULL;
1665 continue;
1667 if (got_object_idset_contains(drop, &qid->id)) {
1668 err = paint_commit(qid, COLOR_SKIP);
1669 if (err)
1670 goto done;
1672 err = got_object_idset_add(keep, &qid->id, NULL);
1673 if (err)
1674 goto done;
1675 break;
1676 case COLOR_DROP:
1677 if (got_object_idset_contains(drop, &qid->id)) {
1678 got_object_qid_free(qid);
1679 qid = NULL;
1680 continue;
1682 if (got_object_idset_contains(keep, &qid->id)) {
1683 err = paint_commit(qid, COLOR_SKIP);
1684 if (err)
1685 goto done;
1687 err = got_object_idset_add(drop, &qid->id, NULL);
1688 if (err)
1689 goto done;
1690 break;
1691 case COLOR_SKIP:
1692 if (!got_object_idset_contains(skip, &qid->id)) {
1693 err = got_object_idset_add(skip, &qid->id,
1694 NULL);
1695 if (err)
1696 goto done;
1698 break;
1699 default:
1700 /* should not happen */
1701 err = got_error_fmt(GOT_ERR_NOT_IMPL,
1702 "%s invalid commit color %"PRIdPTR, __func__,
1703 color);
1704 goto done;
1707 err = open_commit(&commit, pack, packidx, idx, &qid->id,
1708 objcache);
1709 if (err)
1710 goto done;
1712 parents = got_object_commit_get_parent_ids(commit);
1713 if (parents) {
1714 struct got_object_qid *pid;
1715 color = (intptr_t)qid->data;
1716 STAILQ_FOREACH(pid, parents, entry) {
1717 err = queue_commit_id(ids, &pid->id, color);
1718 if (err)
1719 goto done;
1720 nqueued++;
1721 if (color == COLOR_SKIP)
1722 nskip++;
1726 got_object_commit_close(commit);
1727 commit = NULL;
1729 STAILQ_INSERT_TAIL(&painted, qid, entry);
1730 qid = NULL;
1731 npainted++;
1733 err = got_privsep_send_painted_commits(ibuf, &painted,
1734 &npainted, 1, 0);
1735 if (err)
1736 goto done;
1739 err = got_privsep_send_painted_commits(ibuf, &painted, &npainted, 1, 1);
1740 if (err)
1741 goto done;
1743 *nids = nqueued;
1744 done:
1745 if (commit)
1746 got_object_commit_close(commit);
1747 got_object_qid_free(qid);
1748 return err;
1751 static void
1752 commit_painting_free(struct got_object_idset **keep,
1753 struct got_object_idset **drop,
1754 struct got_object_idset **skip)
1756 if (*keep) {
1757 got_object_idset_free(*keep);
1758 *keep = NULL;
1760 if (*drop) {
1761 got_object_idset_free(*drop);
1762 *drop = NULL;
1764 if (*skip) {
1765 got_object_idset_free(*skip);
1766 *skip = NULL;
1770 static const struct got_error *
1771 commit_painting_init(struct imsgbuf *ibuf, struct got_object_idset **keep,
1772 struct got_object_idset **drop, struct got_object_idset **skip)
1774 const struct got_error *err = NULL;
1776 *keep = got_object_idset_alloc();
1777 if (*keep == NULL) {
1778 err = got_error_from_errno("got_object_idset_alloc");
1779 goto done;
1781 *drop = got_object_idset_alloc();
1782 if (*drop == NULL) {
1783 err = got_error_from_errno("got_object_idset_alloc");
1784 goto done;
1786 *skip = got_object_idset_alloc();
1787 if (*skip == NULL) {
1788 err = got_error_from_errno("got_object_idset_alloc");
1789 goto done;
1792 err = recv_object_ids(*keep, ibuf);
1793 if (err)
1794 goto done;
1795 err = recv_object_ids(*drop, ibuf);
1796 if (err)
1797 goto done;
1798 err = recv_object_ids(*skip, ibuf);
1799 if (err)
1800 goto done;
1802 done:
1803 if (err)
1804 commit_painting_free(keep, drop, skip);
1806 return err;
1809 static const struct got_error *
1810 commit_painting_request(struct imsg *imsg, struct imsgbuf *ibuf,
1811 struct got_pack *pack, struct got_packidx *packidx,
1812 struct got_object_cache *objcache, struct got_object_idset *keep,
1813 struct got_object_idset *drop, struct got_object_idset *skip)
1815 const struct got_error *err = NULL;
1816 struct got_imsg_commit_painting_request ireq;
1817 struct got_object_id id;
1818 size_t datalen;
1819 struct got_object_id_queue ids;
1820 int nids = 0;
1822 STAILQ_INIT(&ids);
1824 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1825 if (datalen != sizeof(ireq))
1826 return got_error(GOT_ERR_PRIVSEP_LEN);
1827 memcpy(&ireq, imsg->data, sizeof(ireq));
1828 memcpy(&id, &ireq.id, sizeof(id));
1830 err = queue_commit_id(&ids, &id, ireq.color);
1831 if (err)
1832 return err;
1833 nids = 1;
1835 err = paint_commits(&ids, &nids, keep, drop, skip,
1836 pack, packidx, ibuf, objcache);
1837 if (err)
1838 goto done;
1840 err = got_privsep_send_painted_commits(ibuf, &ids, &nids, 0, 1);
1841 if (err)
1842 goto done;
1844 err = got_privsep_send_painting_commits_done(ibuf);
1845 done:
1846 got_object_id_queue_free(&ids);
1847 return err;
1850 static const struct got_error *
1851 receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
1853 const struct got_error *err = NULL;
1854 struct imsg imsg;
1855 struct got_imsg_pack ipack;
1856 size_t datalen;
1857 struct got_pack *pack;
1859 *packp = NULL;
1861 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1862 if (err)
1863 return err;
1865 pack = calloc(1, sizeof(*pack));
1866 if (pack == NULL) {
1867 err = got_error_from_errno("calloc");
1868 goto done;
1871 if (imsg.hdr.type != GOT_IMSG_PACK) {
1872 err = got_error(GOT_ERR_PRIVSEP_MSG);
1873 goto done;
1876 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1877 if (datalen != sizeof(ipack)) {
1878 err = got_error(GOT_ERR_PRIVSEP_LEN);
1879 goto done;
1881 memcpy(&ipack, imsg.data, sizeof(ipack));
1883 pack->filesize = ipack.filesize;
1884 pack->fd = imsg_get_fd(&imsg);
1885 if (pack->fd == -1) {
1886 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1887 goto done;
1889 if (lseek(pack->fd, 0, SEEK_SET) == -1) {
1890 err = got_error_from_errno("lseek");
1891 goto done;
1893 pack->path_packfile = strdup(ipack.path_packfile);
1894 if (pack->path_packfile == NULL) {
1895 err = got_error_from_errno("strdup");
1896 goto done;
1899 err = got_delta_cache_alloc(&pack->delta_cache);
1900 if (err)
1901 goto done;
1903 #ifndef GOT_PACK_NO_MMAP
1904 if (pack->filesize > 0 && pack->filesize <= SIZE_MAX) {
1905 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1906 pack->fd, 0);
1907 if (pack->map == MAP_FAILED)
1908 pack->map = NULL; /* fall back to read(2) */
1910 #endif
1911 done:
1912 if (err) {
1913 if (pack != NULL)
1914 got_pack_close(pack);
1915 } else
1916 *packp = pack;
1917 imsg_free(&imsg);
1918 return err;
1921 int
1922 main(int argc, char *argv[])
1924 const struct got_error *err = NULL;
1925 struct imsgbuf ibuf;
1926 struct imsg imsg;
1927 struct got_packidx *packidx = NULL;
1928 struct got_pack *pack = NULL;
1929 struct got_object_cache objcache;
1930 FILE *basefile = NULL, *accumfile = NULL, *delta_outfile = NULL;
1931 struct got_object_idset *keep = NULL, *drop = NULL, *skip = NULL;
1932 struct got_parsed_tree_entry *entries = NULL;
1933 size_t nentries = 0, nentries_alloc = 0;
1935 //static int attached;
1936 //while (!attached) sleep(1);
1938 signal(SIGINT, catch_sigint);
1940 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
1942 err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
1943 if (err) {
1944 err = got_error_from_errno("got_object_cache_init");
1945 got_privsep_send_error(&ibuf, err);
1946 return 1;
1949 #ifndef PROFILE
1950 /* revoke access to most system calls */
1951 if (pledge("stdio recvfd", NULL) == -1) {
1952 err = got_error_from_errno("pledge");
1953 got_privsep_send_error(&ibuf, err);
1954 return 1;
1956 #endif
1958 err = receive_packidx(&packidx, &ibuf);
1959 if (err) {
1960 got_privsep_send_error(&ibuf, err);
1961 return 1;
1964 err = receive_pack(&pack, &ibuf);
1965 if (err) {
1966 got_privsep_send_error(&ibuf, err);
1967 return 1;
1970 for (;;) {
1971 if (sigint_received) {
1972 err = got_error(GOT_ERR_CANCELLED);
1973 break;
1976 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1977 if (err) {
1978 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1979 err = NULL;
1980 break;
1983 if (imsg.hdr.type == GOT_IMSG_STOP) {
1984 imsg_free(&imsg);
1985 break;
1988 switch (imsg.hdr.type) {
1989 case GOT_IMSG_TMPFD:
1990 if (basefile == NULL) {
1991 err = receive_tempfile(&basefile, "w+",
1992 &imsg, &ibuf);
1993 } else if (accumfile == NULL) {
1994 err = receive_tempfile(&accumfile, "w+",
1995 &imsg, &ibuf);
1996 } else
1997 err = got_error(GOT_ERR_PRIVSEP_MSG);
1998 break;
1999 case GOT_IMSG_PACKED_OBJECT_REQUEST:
2000 err = object_request(&imsg, &ibuf, pack, packidx,
2001 &objcache);
2002 break;
2003 case GOT_IMSG_PACKED_RAW_OBJECT_REQUEST:
2004 if (basefile == NULL || accumfile == NULL) {
2005 err = got_error(GOT_ERR_PRIVSEP_MSG);
2006 break;
2008 err = raw_object_request(&imsg, &ibuf, pack, packidx,
2009 &objcache, basefile, accumfile);
2010 break;
2011 case GOT_IMSG_RAW_DELTA_OUTFD:
2012 if (delta_outfile != NULL) {
2013 err = got_error(GOT_ERR_PRIVSEP_MSG);
2014 break;
2016 err = receive_tempfile(&delta_outfile, "w",
2017 &imsg, &ibuf);
2018 break;
2019 case GOT_IMSG_RAW_DELTA_REQUEST:
2020 if (delta_outfile == NULL) {
2021 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
2022 break;
2024 err = raw_delta_request(&imsg, &ibuf, delta_outfile,
2025 pack, packidx);
2026 break;
2027 case GOT_IMSG_DELTA_REUSE_REQUEST:
2028 err = delta_reuse_request(&imsg, &ibuf, pack, packidx);
2029 break;
2030 case GOT_IMSG_COMMIT_REQUEST:
2031 err = commit_request(&imsg, &ibuf, pack, packidx,
2032 &objcache);
2033 break;
2034 case GOT_IMSG_TREE_REQUEST:
2035 err = tree_request(&imsg, &ibuf, pack, packidx,
2036 &objcache, &entries, &nentries, &nentries_alloc);
2037 break;
2038 case GOT_IMSG_BLOB_REQUEST:
2039 if (basefile == NULL || accumfile == NULL) {
2040 err = got_error(GOT_ERR_PRIVSEP_MSG);
2041 break;
2043 err = blob_request(&imsg, &ibuf, pack, packidx,
2044 &objcache, basefile, accumfile);
2045 break;
2046 case GOT_IMSG_TAG_REQUEST:
2047 err = tag_request(&imsg, &ibuf, pack, packidx,
2048 &objcache);
2049 break;
2050 case GOT_IMSG_COMMIT_TRAVERSAL_REQUEST:
2051 err = commit_traversal_request(&imsg, &ibuf, pack,
2052 packidx, &objcache);
2053 break;
2054 case GOT_IMSG_OBJECT_ENUMERATION_REQUEST:
2055 err = enumeration_request(&imsg, &ibuf, pack,
2056 packidx, &objcache);
2057 break;
2058 case GOT_IMSG_COMMIT_PAINTING_INIT:
2059 commit_painting_free(&keep, &drop, &skip);
2060 err = commit_painting_init(&ibuf, &keep, &drop, &skip);
2061 break;
2062 case GOT_IMSG_COMMIT_PAINTING_REQUEST:
2063 if (keep == NULL || drop == NULL || skip == NULL) {
2064 err = got_error(GOT_ERR_PRIVSEP_MSG);
2065 break;
2067 err = commit_painting_request(&imsg, &ibuf, pack,
2068 packidx, &objcache, keep, drop, skip);
2069 break;
2070 case GOT_IMSG_COMMIT_PAINTING_DONE:
2071 commit_painting_free(&keep, &drop, &skip);
2072 break;
2073 default:
2074 err = got_error(GOT_ERR_PRIVSEP_MSG);
2075 break;
2078 imsg_free(&imsg);
2079 if (err)
2080 break;
2083 free(entries);
2084 commit_painting_free(&keep, &drop, &skip);
2085 if (packidx)
2086 got_packidx_close(packidx);
2087 if (pack)
2088 got_pack_close(pack);
2089 got_object_cache_close(&objcache);
2090 imsg_clear(&ibuf);
2091 if (basefile && fclose(basefile) == EOF && err == NULL)
2092 err = got_error_from_errno("fclose");
2093 if (accumfile && fclose(accumfile) == EOF && err == NULL)
2094 err = got_error_from_errno("fclose");
2095 if (delta_outfile && fclose(delta_outfile) == EOF && err == NULL)
2096 err = got_error_from_errno("fclose");
2097 if (err) {
2098 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
2099 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
2100 got_privsep_send_error(&ibuf, err);
2103 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
2104 err = got_error_from_errno("close");
2105 return err ? 1 : 0;