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 <limits.h>
25 #include <signal.h>
26 #include <stdint.h>
27 #include <imsg.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sha1.h>
32 #include <unistd.h>
33 #include <zlib.h>
35 #include "got_error.h"
36 #include "got_object.h"
37 #include "got_path.h"
39 #include "got_lib_delta.h"
40 #include "got_lib_delta_cache.h"
41 #include "got_lib_object.h"
42 #include "got_lib_object_cache.h"
43 #include "got_lib_object_parse.h"
44 #include "got_lib_object_idset.h"
45 #include "got_lib_privsep.h"
46 #include "got_lib_pack.h"
48 #ifndef nitems
49 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
50 #endif
52 static volatile sig_atomic_t sigint_received;
54 static void
55 catch_sigint(int signo)
56 {
57 sigint_received = 1;
58 }
60 static const struct got_error *
61 open_object(struct got_object **obj, struct got_pack *pack,
62 struct got_packidx *packidx, int idx, struct got_object_id *id,
63 struct got_object_cache *objcache)
64 {
65 const struct got_error *err;
67 err = got_packfile_open_object(obj, pack, packidx, idx, id);
68 if (err)
69 return err;
70 (*obj)->refcnt++;
72 err = got_object_cache_add(objcache, id, *obj);
73 if (err) {
74 if (err->code == GOT_ERR_OBJ_EXISTS ||
75 err->code == GOT_ERR_OBJ_TOO_LARGE)
76 err = NULL;
77 return err;
78 }
79 (*obj)->refcnt++;
80 return NULL;
81 }
83 static const struct got_error *
84 object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
85 struct got_packidx *packidx, struct got_object_cache *objcache)
86 {
87 const struct got_error *err = NULL;
88 struct got_imsg_packed_object iobj;
89 struct got_object *obj;
90 struct got_object_id id;
91 size_t datalen;
93 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
94 if (datalen != sizeof(iobj))
95 return got_error(GOT_ERR_PRIVSEP_LEN);
96 memcpy(&iobj, imsg->data, sizeof(iobj));
97 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
99 obj = got_object_cache_get(objcache, &id);
100 if (obj) {
101 obj->refcnt++;
102 } else {
103 err = open_object(&obj, pack, packidx, iobj.idx, &id,
104 objcache);
105 if (err)
106 goto done;
109 err = got_privsep_send_obj(ibuf, obj);
110 done:
111 got_object_close(obj);
112 return err;
115 static const struct got_error *
116 open_commit(struct got_commit_object **commit, struct got_pack *pack,
117 struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
118 struct got_object_cache *objcache)
120 const struct got_error *err = NULL;
121 struct got_object *obj = NULL;
122 uint8_t *buf = NULL;
123 size_t len;
125 *commit = NULL;
127 obj = got_object_cache_get(objcache, id);
128 if (obj) {
129 obj->refcnt++;
130 } else {
131 err = open_object(&obj, pack, packidx, obj_idx, id,
132 objcache);
133 if (err)
134 return err;
137 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
138 if (err)
139 goto done;
141 obj->size = len;
143 err = got_object_parse_commit(commit, buf, len);
144 done:
145 got_object_close(obj);
146 free(buf);
147 return err;
150 static const struct got_error *
151 commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
152 struct got_packidx *packidx, struct got_object_cache *objcache)
154 const struct got_error *err = NULL;
155 struct got_imsg_packed_object iobj;
156 struct got_commit_object *commit = NULL;
157 struct got_object_id id;
158 size_t datalen;
160 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
161 if (datalen != sizeof(iobj))
162 return got_error(GOT_ERR_PRIVSEP_LEN);
163 memcpy(&iobj, imsg->data, sizeof(iobj));
164 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
166 err = open_commit(&commit, pack, packidx, iobj.idx, &id, objcache);
167 if (err)
168 goto done;
170 err = got_privsep_send_commit(ibuf, commit);
171 done:
172 if (commit)
173 got_object_commit_close(commit);
174 if (err) {
175 if (err->code == GOT_ERR_PRIVSEP_PIPE)
176 err = NULL;
177 else
178 got_privsep_send_error(ibuf, err);
181 return err;
184 static const struct got_error *
185 open_tree(uint8_t **buf, struct got_parsed_tree_entry **entries, int *nentries,
186 struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
187 struct got_object_id *id, struct got_object_cache *objcache)
189 const struct got_error *err = NULL;
190 struct got_object *obj = NULL;
191 size_t len;
193 *buf = NULL;
194 *nentries = 0;
196 obj = got_object_cache_get(objcache, id);
197 if (obj) {
198 obj->refcnt++;
199 } else {
200 err = open_object(&obj, pack, packidx, obj_idx, id,
201 objcache);
202 if (err)
203 return err;
206 err = got_packfile_extract_object_to_mem(buf, &len, obj, pack);
207 if (err)
208 goto done;
210 obj->size = len;
212 err = got_object_parse_tree(entries, nentries, *buf, len);
213 done:
214 got_object_close(obj);
215 if (err) {
216 free(*buf);
217 *buf = NULL;
219 return err;
222 static const struct got_error *
223 tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
224 struct got_packidx *packidx, struct got_object_cache *objcache)
226 const struct got_error *err = NULL;
227 struct got_imsg_packed_object iobj;
228 struct got_parsed_tree_entry *entries = NULL;
229 int nentries = 0;
230 uint8_t *buf = NULL;
231 struct got_object_id id;
232 size_t datalen;
234 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
235 if (datalen != sizeof(iobj))
236 return got_error(GOT_ERR_PRIVSEP_LEN);
237 memcpy(&iobj, imsg->data, sizeof(iobj));
238 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
240 err = open_tree(&buf, &entries, &nentries, pack, packidx, iobj.idx,
241 &id, objcache);
242 if (err)
243 return err;
245 err = got_privsep_send_tree(ibuf, entries, nentries);
246 free(entries);
247 free(buf);
248 if (err) {
249 if (err->code == GOT_ERR_PRIVSEP_PIPE)
250 err = NULL;
251 else
252 got_privsep_send_error(ibuf, err);
255 return err;
258 static const struct got_error *
259 receive_file(FILE **f, struct imsgbuf *ibuf, uint32_t imsg_code)
261 const struct got_error *err;
262 struct imsg imsg;
263 size_t datalen;
265 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
266 if (err)
267 return err;
269 if (imsg.hdr.type != imsg_code) {
270 err = got_error(GOT_ERR_PRIVSEP_MSG);
271 goto done;
274 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
275 if (datalen != 0) {
276 err = got_error(GOT_ERR_PRIVSEP_LEN);
277 goto done;
279 if (imsg.fd == -1) {
280 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
281 goto done;
284 *f = fdopen(imsg.fd, "w+");
285 if (*f == NULL) {
286 err = got_error_from_errno("fdopen");
287 close(imsg.fd);
288 goto done;
290 done:
291 imsg_free(&imsg);
292 return err;
295 static const struct got_error *
296 receive_tempfile(FILE **f, const char *mode, struct imsg *imsg,
297 struct imsgbuf *ibuf)
299 size_t datalen;
301 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
302 if (datalen != 0)
303 return got_error(GOT_ERR_PRIVSEP_LEN);
305 if (imsg->fd == -1)
306 return got_error(GOT_ERR_PRIVSEP_NO_FD);
308 *f = fdopen(imsg->fd, mode);
309 if (*f == NULL)
310 return got_error_from_errno("fdopen");
311 imsg->fd = -1;
313 return NULL;
316 static const struct got_error *
317 blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
318 struct got_packidx *packidx, struct got_object_cache *objcache,
319 FILE *basefile, FILE *accumfile)
321 const struct got_error *err = NULL;
322 struct got_imsg_packed_object iobj;
323 struct got_object *obj = NULL;
324 FILE *outfile = NULL;
325 struct got_object_id id;
326 size_t datalen;
327 uint64_t blob_size;
328 uint8_t *buf = NULL;
330 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
331 if (datalen != sizeof(iobj))
332 return got_error(GOT_ERR_PRIVSEP_LEN);
333 memcpy(&iobj, imsg->data, sizeof(iobj));
334 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
336 obj = got_object_cache_get(objcache, &id);
337 if (obj) {
338 obj->refcnt++;
339 } else {
340 err = open_object(&obj, pack, packidx, iobj.idx, &id,
341 objcache);
342 if (err)
343 return err;
346 err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
347 if (err)
348 goto done;
350 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
351 err = got_pack_get_max_delta_object_size(&blob_size, obj, pack);
352 if (err)
353 goto done;
354 } else
355 blob_size = obj->size;
357 if (blob_size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
358 err = got_packfile_extract_object_to_mem(&buf, &obj->size,
359 obj, pack);
360 else
361 err = got_packfile_extract_object(pack, obj, outfile, basefile,
362 accumfile);
363 if (err)
364 goto done;
366 err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, buf);
367 done:
368 free(buf);
369 if (outfile && fclose(outfile) == EOF && err == NULL)
370 err = got_error_from_errno("fclose");
371 got_object_close(obj);
372 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
373 got_privsep_send_error(ibuf, err);
375 return err;
378 static const struct got_error *
379 tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
380 struct got_packidx *packidx, struct got_object_cache *objcache)
382 const struct got_error *err = NULL;
383 struct got_imsg_packed_object iobj;
384 struct got_object *obj = NULL;
385 struct got_tag_object *tag = NULL;
386 uint8_t *buf = NULL;
387 size_t len;
388 struct got_object_id id;
389 size_t datalen;
391 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
392 if (datalen != sizeof(iobj))
393 return got_error(GOT_ERR_PRIVSEP_LEN);
394 memcpy(&iobj, imsg->data, sizeof(iobj));
395 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
397 obj = got_object_cache_get(objcache, &id);
398 if (obj) {
399 obj->refcnt++;
400 } else {
401 err = open_object(&obj, pack, packidx, iobj.idx, &id,
402 objcache);
403 if (err)
404 return err;
407 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
408 if (err)
409 goto done;
411 obj->size = len;
412 err = got_object_parse_tag(&tag, buf, len);
413 if (err)
414 goto done;
416 err = got_privsep_send_tag(ibuf, tag);
417 done:
418 free(buf);
419 got_object_close(obj);
420 if (tag)
421 got_object_tag_close(tag);
422 if (err) {
423 if (err->code == GOT_ERR_PRIVSEP_PIPE)
424 err = NULL;
425 else
426 got_privsep_send_error(ibuf, err);
429 return err;
432 static struct got_parsed_tree_entry *
433 find_entry_by_name(struct got_parsed_tree_entry *entries, int nentries,
434 const char *name, size_t len)
436 struct got_parsed_tree_entry *pte;
437 int cmp, i;
439 /* Note that tree entries are sorted in strncmp() order. */
440 for (i = 0; i < nentries; i++) {
441 pte = &entries[i];
442 cmp = strncmp(pte->name, name, len);
443 if (cmp < 0)
444 continue;
445 if (cmp > 0)
446 break;
447 if (pte->name[len] == '\0')
448 return pte;
450 return NULL;
453 static const struct got_error *
454 tree_path_changed(int *changed, uint8_t **buf1, uint8_t **buf2,
455 struct got_parsed_tree_entry **entries1, int *nentries1,
456 struct got_parsed_tree_entry **entries2, int *nentries2,
457 const char *path, struct got_pack *pack, struct got_packidx *packidx,
458 struct imsgbuf *ibuf, struct got_object_cache *objcache)
460 const struct got_error *err = NULL;
461 struct got_parsed_tree_entry *pte1 = NULL, *pte2 = NULL;
462 const char *seg, *s;
463 size_t seglen;
465 *changed = 0;
467 /* We not do support comparing the root path. */
468 if (got_path_is_root_dir(path))
469 return got_error_path(path, GOT_ERR_BAD_PATH);
471 s = path;
472 while (*s == '/')
473 s++;
474 seg = s;
475 seglen = 0;
476 while (*s) {
477 if (*s != '/') {
478 s++;
479 seglen++;
480 if (*s)
481 continue;
484 pte1 = find_entry_by_name(*entries1, *nentries1, seg, seglen);
485 if (pte1 == NULL) {
486 err = got_error(GOT_ERR_NO_OBJ);
487 break;
490 pte2 = find_entry_by_name(*entries2, *nentries2, seg, seglen);
491 if (pte2 == NULL) {
492 *changed = 1;
493 break;
496 if (pte1->mode != pte2->mode) {
497 *changed = 1;
498 break;
501 if (memcmp(pte1->id, pte2->id, SHA1_DIGEST_LENGTH) == 0) {
502 *changed = 0;
503 break;
506 if (*s == '\0') { /* final path element */
507 *changed = 1;
508 break;
511 seg = s + 1;
512 s++;
513 seglen = 0;
514 if (*s) {
515 struct got_object_id id1, id2;
516 int idx;
518 memcpy(id1.sha1, pte1->id, SHA1_DIGEST_LENGTH);
519 idx = got_packidx_get_object_idx(packidx, &id1);
520 if (idx == -1) {
521 err = got_error_no_obj(&id1);
522 break;
524 free(*entries1);
525 *nentries1 = 0;
526 free(*buf1);
527 *buf1 = NULL;
528 err = open_tree(buf1, entries1, nentries1, pack,
529 packidx, idx, &id1, objcache);
530 pte1 = NULL;
531 if (err)
532 break;
534 memcpy(id2.sha1, pte2->id, SHA1_DIGEST_LENGTH);
535 idx = got_packidx_get_object_idx(packidx, &id2);
536 if (idx == -1) {
537 err = got_error_no_obj(&id2);
538 break;
540 free(*entries2);
541 *nentries2 = 0;
542 free(*buf2);
543 *buf2 = NULL;
544 err = open_tree(buf2, entries2, nentries2, pack,
545 packidx, idx, &id2, objcache);
546 pte2 = NULL;
547 if (err)
548 break;
552 return err;
555 static const struct got_error *
556 send_traversed_commits(struct got_object_id *commit_ids, size_t ncommits,
557 struct imsgbuf *ibuf)
559 struct ibuf *wbuf;
560 size_t i;
562 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
563 sizeof(struct got_imsg_traversed_commits) +
564 ncommits * SHA1_DIGEST_LENGTH);
565 if (wbuf == NULL)
566 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
568 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1)
569 return got_error_from_errno("imsg_add TRAVERSED_COMMITS");
571 for (i = 0; i < ncommits; i++) {
572 struct got_object_id *id = &commit_ids[i];
573 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
574 return got_error_from_errno(
575 "imsg_add TRAVERSED_COMMITS");
579 wbuf->fd = -1;
580 imsg_close(ibuf, wbuf);
582 return got_privsep_flush_imsg(ibuf);
585 static const struct got_error *
586 send_commit_traversal_done(struct imsgbuf *ibuf)
588 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
589 NULL, 0) == -1)
590 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
592 return got_privsep_flush_imsg(ibuf);
595 static const struct got_error *
596 commit_traversal_request(struct imsg *imsg, struct imsgbuf *ibuf,
597 struct got_pack *pack, struct got_packidx *packidx,
598 struct got_object_cache *objcache)
600 const struct got_error *err = NULL;
601 struct got_imsg_packed_object iobj;
602 struct got_object_qid *pid;
603 struct got_commit_object *commit = NULL, *pcommit = NULL;
604 struct got_parsed_tree_entry *entries = NULL, *pentries = NULL;
605 int nentries = 0, pnentries = 0;
606 struct got_object_id id;
607 size_t datalen, path_len;
608 char *path = NULL;
609 const int min_alloc = 64;
610 int changed = 0, ncommits = 0, nallocated = 0;
611 struct got_object_id *commit_ids = NULL;
613 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
614 if (datalen < sizeof(iobj))
615 return got_error(GOT_ERR_PRIVSEP_LEN);
616 memcpy(&iobj, imsg->data, sizeof(iobj));
617 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
619 path_len = datalen - sizeof(iobj) - 1;
620 if (path_len < 0)
621 return got_error(GOT_ERR_PRIVSEP_LEN);
622 if (path_len > 0) {
623 path = imsg->data + sizeof(iobj);
624 if (path[path_len] != '\0')
625 return got_error(GOT_ERR_PRIVSEP_LEN);
628 nallocated = min_alloc;
629 commit_ids = reallocarray(NULL, nallocated, sizeof(*commit_ids));
630 if (commit_ids == NULL)
631 return got_error_from_errno("reallocarray");
633 do {
634 const size_t max_datalen = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
635 int idx;
637 if (sigint_received) {
638 err = got_error(GOT_ERR_CANCELLED);
639 goto done;
642 if (commit == NULL) {
643 idx = got_packidx_get_object_idx(packidx, &id);
644 if (idx == -1)
645 break;
646 err = open_commit(&commit, pack, packidx,
647 idx, &id, objcache);
648 if (err) {
649 if (err->code != GOT_ERR_NO_OBJ)
650 goto done;
651 err = NULL;
652 break;
656 if (sizeof(struct got_imsg_traversed_commits) +
657 ncommits * SHA1_DIGEST_LENGTH >= max_datalen) {
658 err = send_traversed_commits(commit_ids, ncommits,
659 ibuf);
660 if (err)
661 goto done;
662 ncommits = 0;
664 ncommits++;
665 if (ncommits > nallocated) {
666 struct got_object_id *new;
667 nallocated += min_alloc;
668 new = reallocarray(commit_ids, nallocated,
669 sizeof(*commit_ids));
670 if (new == NULL) {
671 err = got_error_from_errno("reallocarray");
672 goto done;
674 commit_ids = new;
676 memcpy(commit_ids[ncommits - 1].sha1, id.sha1,
677 SHA1_DIGEST_LENGTH);
679 pid = STAILQ_FIRST(&commit->parent_ids);
680 if (pid == NULL)
681 break;
683 idx = got_packidx_get_object_idx(packidx, &pid->id);
684 if (idx == -1)
685 break;
687 err = open_commit(&pcommit, pack, packidx, idx, &pid->id,
688 objcache);
689 if (err) {
690 if (err->code != GOT_ERR_NO_OBJ)
691 goto done;
692 err = NULL;
693 break;
696 if (path[0] == '/' && path[1] == '\0') {
697 if (got_object_id_cmp(pcommit->tree_id,
698 commit->tree_id) != 0) {
699 changed = 1;
700 break;
702 } else {
703 int pidx;
704 uint8_t *buf = NULL, *pbuf = NULL;
706 idx = got_packidx_get_object_idx(packidx,
707 commit->tree_id);
708 if (idx == -1)
709 break;
710 pidx = got_packidx_get_object_idx(packidx,
711 pcommit->tree_id);
712 if (pidx == -1)
713 break;
715 err = open_tree(&buf, &entries, &nentries, pack,
716 packidx, idx, commit->tree_id, objcache);
717 if (err)
718 goto done;
719 err = open_tree(&pbuf, &pentries, &pnentries, pack,
720 packidx, pidx, pcommit->tree_id, objcache);
721 if (err) {
722 free(buf);
723 goto done;
726 err = tree_path_changed(&changed, &buf, &pbuf,
727 &entries, &nentries, &pentries, &pnentries, path,
728 pack, packidx, ibuf, objcache);
730 free(entries);
731 entries = NULL;
732 nentries = 0;
733 free(buf);
734 free(pentries);
735 pentries = NULL;
736 pnentries = 0;
737 free(pbuf);
738 if (err) {
739 if (err->code != GOT_ERR_NO_OBJ)
740 goto done;
741 err = NULL;
742 break;
746 if (!changed) {
747 memcpy(id.sha1, pid->id.sha1, SHA1_DIGEST_LENGTH);
748 got_object_commit_close(commit);
749 commit = pcommit;
750 pcommit = NULL;
752 } while (!changed);
754 if (ncommits > 0) {
755 err = send_traversed_commits(commit_ids, ncommits, ibuf);
756 if (err)
757 goto done;
759 if (changed) {
760 err = got_privsep_send_commit(ibuf, commit);
761 if (err)
762 goto done;
765 err = send_commit_traversal_done(ibuf);
766 done:
767 free(commit_ids);
768 if (commit)
769 got_object_commit_close(commit);
770 if (pcommit)
771 got_object_commit_close(pcommit);
772 free(entries);
773 free(pentries);
774 if (err) {
775 if (err->code == GOT_ERR_PRIVSEP_PIPE)
776 err = NULL;
777 else
778 got_privsep_send_error(ibuf, err);
781 return err;
784 static const struct got_error *
785 raw_object_request(struct imsg *imsg, struct imsgbuf *ibuf,
786 struct got_pack *pack, struct got_packidx *packidx,
787 struct got_object_cache *objcache, FILE *basefile, FILE *accumfile)
789 const struct got_error *err = NULL;
790 uint8_t *buf = NULL;
791 uint64_t size = 0;
792 FILE *outfile = NULL;
793 struct got_imsg_packed_object iobj;
794 struct got_object *obj;
795 struct got_object_id id;
796 size_t datalen;
798 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
799 if (datalen != sizeof(iobj))
800 return got_error(GOT_ERR_PRIVSEP_LEN);
801 memcpy(&iobj, imsg->data, sizeof(iobj));
802 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
804 obj = got_object_cache_get(objcache, &id);
805 if (obj) {
806 obj->refcnt++;
807 } else {
808 err = open_object(&obj, pack, packidx, iobj.idx, &id,
809 objcache);
810 if (err)
811 return err;
814 err = receive_file(&outfile, ibuf, GOT_IMSG_RAW_OBJECT_OUTFD);
815 if (err)
816 return err;
818 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
819 err = got_pack_get_max_delta_object_size(&size, obj, pack);
820 if (err)
821 goto done;
822 } else
823 size = obj->size;
825 if (size <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
826 err = got_packfile_extract_object_to_mem(&buf, &obj->size,
827 obj, pack);
828 else
829 err = got_packfile_extract_object(pack, obj, outfile, basefile,
830 accumfile);
831 if (err)
832 goto done;
834 err = got_privsep_send_raw_obj(ibuf, obj->size, obj->hdrlen, buf);
835 done:
836 free(buf);
837 if (outfile && fclose(outfile) == EOF && err == NULL)
838 err = got_error_from_errno("fclose");
839 got_object_close(obj);
840 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
841 got_privsep_send_error(ibuf, err);
843 return err;
846 static const struct got_error *
847 get_base_object_id(struct got_object_id *base_id, struct got_packidx *packidx,
848 off_t base_offset)
850 const struct got_error *err;
851 int idx;
853 err = got_packidx_get_offset_idx(&idx, packidx, base_offset);
854 if (err)
855 return err;
856 if (idx == -1)
857 return got_error(GOT_ERR_BAD_PACKIDX);
859 return got_packidx_get_object_id(base_id, packidx, idx);
862 static const struct got_error *
863 raw_delta_request(struct imsg *imsg, struct imsgbuf *ibuf,
864 FILE *delta_outfile, struct got_pack *pack,
865 struct got_packidx *packidx)
867 const struct got_error *err = NULL;
868 struct got_imsg_raw_delta_request req;
869 size_t datalen, delta_size, delta_compressed_size;
870 off_t delta_offset;
871 uint8_t *delta_buf = NULL;
872 struct got_object_id id, base_id;
873 off_t base_offset, delta_out_offset = 0;
874 uint64_t base_size = 0, result_size = 0;
875 size_t w;
877 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
878 if (datalen != sizeof(req))
879 return got_error(GOT_ERR_PRIVSEP_LEN);
880 memcpy(&req, imsg->data, sizeof(req));
881 memcpy(id.sha1, req.id, SHA1_DIGEST_LENGTH);
883 imsg->fd = -1;
885 err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
886 &delta_compressed_size, &delta_offset, &base_offset, &base_id,
887 &base_size, &result_size, pack, packidx, req.idx);
888 if (err)
889 goto done;
891 /*
892 * If this is an offset delta we must determine the base
893 * object ID ourselves.
894 */
895 if (base_offset != 0) {
896 err = get_base_object_id(&base_id, packidx, base_offset);
897 if (err)
898 goto done;
901 delta_out_offset = ftello(delta_outfile);
902 w = fwrite(delta_buf, 1, delta_compressed_size, delta_outfile);
903 if (w != delta_compressed_size) {
904 err = got_ferror(delta_outfile, GOT_ERR_IO);
905 goto done;
907 if (fflush(delta_outfile) == -1) {
908 err = got_error_from_errno("fflush");
909 goto done;
912 err = got_privsep_send_raw_delta(ibuf, base_size, result_size,
913 delta_size, delta_compressed_size, delta_offset, delta_out_offset,
914 &base_id);
915 done:
916 free(delta_buf);
917 return err;
920 struct search_deltas_arg {
921 struct imsgbuf *ibuf;
922 struct got_packidx *packidx;
923 struct got_pack *pack;
924 struct got_object_idset *idset;
925 FILE *delta_outfile;
926 struct got_imsg_reused_delta deltas[GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS];
927 size_t ndeltas;
928 };
930 static const struct got_error *
931 search_delta_for_object(struct got_object_id *id, void *data, void *arg)
933 const struct got_error *err;
934 struct search_deltas_arg *a = arg;
935 int obj_idx;
936 uint8_t *delta_buf = NULL;
937 uint64_t base_size, result_size;
938 size_t delta_size, delta_compressed_size;
939 off_t delta_offset, base_offset;
940 struct got_object_id base_id;
942 if (sigint_received)
943 return got_error(GOT_ERR_CANCELLED);
945 obj_idx = got_packidx_get_object_idx(a->packidx, id);
946 if (obj_idx == -1)
947 return NULL; /* object not present in our pack file */
949 err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
950 &delta_compressed_size, &delta_offset, &base_offset, &base_id,
951 &base_size, &result_size, a->pack, a->packidx, obj_idx);
952 if (err) {
953 if (err->code == GOT_ERR_OBJ_TYPE)
954 return NULL; /* object not stored as a delta */
955 return err;
958 /*
959 * If this is an offset delta we must determine the base
960 * object ID ourselves.
961 */
962 if (base_offset != 0) {
963 err = get_base_object_id(&base_id, a->packidx, base_offset);
964 if (err)
965 goto done;
968 if (got_object_idset_contains(a->idset, &base_id)) {
969 struct got_imsg_reused_delta *delta;
970 off_t delta_out_offset = ftello(a->delta_outfile);
971 size_t w;
973 w = fwrite(delta_buf, 1, delta_compressed_size,
974 a->delta_outfile);
975 if (w != delta_compressed_size) {
976 err = got_ferror(a->delta_outfile, GOT_ERR_IO);
977 goto done;
980 delta = &a->deltas[a->ndeltas++];
981 memcpy(&delta->id, id, sizeof(delta->id));
982 memcpy(&delta->base_id, &base_id, sizeof(delta->base_id));
983 delta->base_size = base_size;
984 delta->result_size = result_size;
985 delta->delta_size = delta_size;
986 delta->delta_compressed_size = delta_compressed_size;
987 delta->delta_offset = delta_offset;
988 delta->delta_out_offset = delta_out_offset;
990 if (a->ndeltas >= GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS) {
991 err = got_privsep_send_reused_deltas(a->ibuf,
992 a->deltas, a->ndeltas);
993 if (err)
994 goto done;
995 a->ndeltas = 0;
998 done:
999 free(delta_buf);
1000 return err;
1003 static const struct got_error *
1004 recv_object_ids(struct got_object_idset *idset, struct imsgbuf *ibuf)
1006 const struct got_error *err = NULL;
1007 int done = 0;
1008 struct got_object_id *ids;
1009 size_t nids, i;
1011 for (;;) {
1012 err = got_privsep_recv_object_idlist(&done, &ids, &nids, ibuf);
1013 if (err || done)
1014 break;
1015 for (i = 0; i < nids; i++) {
1016 err = got_object_idset_add(idset, &ids[i], NULL);
1017 if (err) {
1018 free(ids);
1019 return err;
1022 free(ids);
1025 return err;
1028 static const struct got_error *
1029 recv_object_id_queue(struct got_object_id_queue *queue, struct imsgbuf *ibuf)
1031 const struct got_error *err = NULL;
1032 int done = 0;
1033 struct got_object_qid *qid;
1034 struct got_object_id *ids;
1035 size_t nids, i;
1037 for (;;) {
1038 err = got_privsep_recv_object_idlist(&done, &ids, &nids, ibuf);
1039 if (err || done)
1040 break;
1041 for (i = 0; i < nids; i++) {
1042 err = got_object_qid_alloc_partial(&qid);
1043 if (err)
1044 return err;
1045 memcpy(&qid->id, &ids[i], sizeof(qid->id));
1046 STAILQ_INSERT_TAIL(queue, qid, entry);
1050 return err;
1053 static const struct got_error *
1054 delta_reuse_request(struct imsg *imsg, struct imsgbuf *ibuf,
1055 FILE *delta_outfile, struct got_pack *pack, struct got_packidx *packidx)
1057 const struct got_error *err = NULL;
1058 struct got_object_idset *idset;
1059 struct search_deltas_arg sda;
1061 idset = got_object_idset_alloc();
1062 if (idset == NULL)
1063 return got_error_from_errno("got_object_idset_alloc");
1065 err = recv_object_ids(idset, ibuf);
1066 if (err)
1067 return err;
1069 memset(&sda, 0, sizeof(sda));
1070 sda.ibuf = ibuf;
1071 sda.idset = idset;
1072 sda.pack = pack;
1073 sda.packidx = packidx;
1074 sda.delta_outfile = delta_outfile;
1075 err = got_object_idset_for_each(idset, search_delta_for_object, &sda);
1076 if (err)
1077 goto done;
1079 if (sda.ndeltas > 0) {
1080 err = got_privsep_send_reused_deltas(ibuf, sda.deltas,
1081 sda.ndeltas);
1082 if (err)
1083 goto done;
1086 if (fflush(delta_outfile) == -1) {
1087 err = got_error_from_errno("fflush");
1088 goto done;
1091 err = got_privsep_send_reused_deltas_done(ibuf);
1092 done:
1093 got_object_idset_free(idset);
1094 return err;
1097 static const struct got_error *
1098 receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
1100 const struct got_error *err = NULL;
1101 struct imsg imsg;
1102 struct got_imsg_packidx ipackidx;
1103 size_t datalen;
1104 struct got_packidx *p;
1106 *packidx = NULL;
1108 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1109 if (err)
1110 return err;
1112 p = calloc(1, sizeof(*p));
1113 if (p == NULL) {
1114 err = got_error_from_errno("calloc");
1115 goto done;
1118 if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
1119 err = got_error(GOT_ERR_PRIVSEP_MSG);
1120 goto done;
1123 if (imsg.fd == -1) {
1124 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1125 goto done;
1128 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1129 if (datalen != sizeof(ipackidx)) {
1130 err = got_error(GOT_ERR_PRIVSEP_LEN);
1131 goto done;
1133 memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
1135 p->len = ipackidx.len;
1136 p->fd = dup(imsg.fd);
1137 if (p->fd == -1) {
1138 err = got_error_from_errno("dup");
1139 goto done;
1141 if (lseek(p->fd, 0, SEEK_SET) == -1) {
1142 err = got_error_from_errno("lseek");
1143 goto done;
1146 #ifndef GOT_PACK_NO_MMAP
1147 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
1148 if (p->map == MAP_FAILED)
1149 p->map = NULL; /* fall back to read(2) */
1150 #endif
1151 err = got_packidx_init_hdr(p, 1, ipackidx.packfile_size);
1152 done:
1153 if (err) {
1154 if (imsg.fd != -1)
1155 close(imsg.fd);
1156 got_packidx_close(p);
1157 } else
1158 *packidx = p;
1159 imsg_free(&imsg);
1160 return err;
1163 static const struct got_error *
1164 send_tree_enumeration_done(struct imsgbuf *ibuf)
1166 if (imsg_compose(ibuf, GOT_IMSG_TREE_ENUMERATION_DONE, 0, 0, -1,
1167 NULL, 0) == -1)
1168 return got_error_from_errno("imsg_compose TREE_ENUMERATION_DONE");
1170 return got_privsep_flush_imsg(ibuf);
1173 struct enumerated_tree {
1174 struct got_object_id id;
1175 char *path;
1176 uint8_t *buf;
1177 struct got_parsed_tree_entry *entries;
1178 int nentries;
1181 static const struct got_error *
1182 enumerate_tree(int *have_all_entries, struct imsgbuf *ibuf, size_t *totlen,
1183 struct got_object_id *tree_id,
1184 const char *path, struct got_pack *pack, struct got_packidx *packidx,
1185 struct got_object_cache *objcache, struct got_object_idset *idset,
1186 struct enumerated_tree **trees, size_t *nalloc, size_t *ntrees)
1188 const struct got_error *err = NULL;
1189 struct got_object_id_queue ids;
1190 struct got_object_qid *qid;
1191 uint8_t *buf = NULL;
1192 struct got_parsed_tree_entry *entries = NULL;
1193 int nentries = 0, i;
1194 struct enumerated_tree *tree;
1196 *ntrees = 0;
1197 *have_all_entries = 1;
1198 STAILQ_INIT(&ids);
1200 err = got_object_qid_alloc_partial(&qid);
1201 if (err)
1202 return err;
1203 memcpy(&qid->id.sha1, tree_id, SHA1_DIGEST_LENGTH);
1204 qid->data = strdup(path);
1205 if (qid->data == NULL) {
1206 err = got_error_from_errno("strdup");
1207 goto done;
1209 STAILQ_INSERT_TAIL(&ids, qid, entry);
1210 qid = NULL;
1212 /* Traverse the tree hierarchy, gather tree object IDs and paths. */
1213 do {
1214 const char *path;
1215 int idx, i;
1217 if (sigint_received) {
1218 err = got_error(GOT_ERR_CANCELLED);
1219 goto done;
1222 qid = STAILQ_FIRST(&ids);
1223 STAILQ_REMOVE_HEAD(&ids, entry);
1224 path = qid->data;
1226 idx = got_packidx_get_object_idx(packidx, &qid->id);
1227 if (idx == -1) {
1228 *have_all_entries = 0;
1229 break;
1232 err = open_tree(&buf, &entries, &nentries,
1233 pack, packidx, idx, &qid->id, objcache);
1234 if (err) {
1235 if (err->code != GOT_ERR_NO_OBJ)
1236 goto done;
1239 err = got_object_idset_add(idset, &qid->id, NULL);
1240 if (err)
1241 goto done;
1243 for (i = 0; i < nentries; i++) {
1244 struct got_object_qid *eqid = NULL;
1245 struct got_parsed_tree_entry *pte = &entries[i];
1246 char *p;
1248 if (!S_ISDIR(pte->mode))
1249 continue;
1251 err = got_object_qid_alloc_partial(&eqid);
1252 if (err)
1253 goto done;
1254 memcpy(eqid->id.sha1, pte->id, sizeof(eqid->id.sha1));
1256 if (got_object_idset_contains(idset, &eqid->id)) {
1257 got_object_qid_free(eqid);
1258 continue;
1261 if (asprintf(&p, "%s%s%s", path,
1262 got_path_is_root_dir(path) ? "" : "/",
1263 pte->name) == -1) {
1264 err = got_error_from_errno("asprintf");
1265 got_object_qid_free(eqid);
1266 goto done;
1268 eqid->data = p;
1269 STAILQ_INSERT_TAIL(&ids, eqid, entry);
1272 if (*ntrees >= *nalloc) {
1273 struct enumerated_tree *new;
1274 new = recallocarray(*trees, *nalloc, *nalloc + 16,
1275 sizeof(*new));
1276 if (new == NULL) {
1277 err = got_error_from_errno("malloc");
1278 goto done;
1280 *trees = new;
1281 *nalloc += 16;
1283 tree = &(*trees)[*ntrees];
1284 (*ntrees)++;
1285 memcpy(&tree->id, &qid->id, sizeof(tree->id));
1286 tree->path = qid->data;
1287 tree->buf = buf;
1288 buf = NULL;
1289 tree->entries = entries;
1290 entries = NULL;
1291 tree->nentries = nentries;
1293 got_object_qid_free(qid);
1294 qid = NULL;
1295 } while (!STAILQ_EMPTY(&ids));
1297 if (*have_all_entries) {
1298 int i;
1300 * We have managed to traverse all entries in the hierarchy.
1301 * Tell the main process what we have found.
1303 for (i = 0; i < *ntrees; i++) {
1304 tree = &(*trees)[i];
1305 err = got_privsep_send_enumerated_tree(totlen,
1306 ibuf, &tree->id, tree->path, tree->entries,
1307 tree->nentries);
1308 if (err)
1309 goto done;
1310 free(tree->buf);
1311 tree->buf = NULL;
1312 free(tree->path);
1313 tree->path = NULL;
1314 free(tree->entries);
1315 tree->entries = NULL;
1317 *ntrees = 0; /* don't loop again below to free memory */
1319 err = send_tree_enumeration_done(ibuf);
1320 } else {
1322 * We can only load fully packed tree hierarchies on
1323 * behalf of the main process, otherwise the main process
1324 * gets a wrong idea about which tree objects have
1325 * already been traversed.
1326 * Indicate a missing entry for the root of this tree.
1327 * The main process should continue by loading this
1328 * entire tree the slow way.
1330 err = got_privsep_send_enumerated_tree(totlen, ibuf,
1331 tree_id, "/", NULL, -1);
1332 if (err)
1333 goto done;
1335 done:
1336 free(buf);
1337 free(entries);
1338 for (i = 0; i < *ntrees; i++) {
1339 tree = &(*trees)[i];
1340 free(tree->buf);
1341 tree->buf = NULL;
1342 free(tree->path);
1343 tree->path = NULL;
1344 free(tree->entries);
1345 tree->entries = NULL;
1347 if (qid)
1348 free(qid->data);
1349 got_object_qid_free(qid);
1350 got_object_id_queue_free(&ids);
1351 if (err) {
1352 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1353 err = NULL;
1354 else
1355 got_privsep_send_error(ibuf, err);
1358 return err;
1361 static const struct got_error *
1362 enumeration_request(struct imsg *imsg, struct imsgbuf *ibuf,
1363 struct got_pack *pack, struct got_packidx *packidx,
1364 struct got_object_cache *objcache)
1366 const struct got_error *err = NULL;
1367 struct got_object_id_queue commit_ids;
1368 const struct got_object_id_queue *parents = NULL;
1369 struct got_object_qid *qid = NULL;
1370 struct got_object *obj = NULL;
1371 struct got_commit_object *commit = NULL;
1372 struct got_object_id *tree_id = NULL;
1373 size_t totlen = 0;
1374 struct got_object_idset *idset;
1375 int i, idx, have_all_entries = 1;
1376 struct enumerated_tree *trees = NULL;
1377 size_t ntrees = 0, nalloc = 16;
1379 STAILQ_INIT(&commit_ids);
1381 trees = calloc(nalloc, sizeof(*trees));
1382 if (trees == NULL)
1383 return got_error_from_errno("calloc");
1385 idset = got_object_idset_alloc();
1386 if (idset == NULL) {
1387 err = got_error_from_errno("got_object_idset_alloc");
1388 goto done;
1391 err = recv_object_id_queue(&commit_ids, ibuf);
1392 if (err)
1393 goto done;
1395 if (STAILQ_EMPTY(&commit_ids)) {
1396 err = got_error(GOT_ERR_PRIVSEP_MSG);
1397 goto done;
1400 err = recv_object_ids(idset, ibuf);
1401 if (err)
1402 goto done;
1404 while (!STAILQ_EMPTY(&commit_ids)) {
1405 if (sigint_received) {
1406 err = got_error(GOT_ERR_CANCELLED);
1407 goto done;
1410 qid = STAILQ_FIRST(&commit_ids);
1411 STAILQ_REMOVE_HEAD(&commit_ids, entry);
1413 if (got_object_idset_contains(idset, &qid->id)) {
1414 got_object_qid_free(qid);
1415 qid = NULL;
1416 continue;
1419 idx = got_packidx_get_object_idx(packidx, &qid->id);
1420 if (idx == -1) {
1421 have_all_entries = 0;
1422 break;
1425 err = open_object(&obj, pack, packidx, idx, &qid->id,
1426 objcache);
1427 if (err)
1428 goto done;
1429 if (obj->type == GOT_OBJ_TYPE_TAG) {
1430 struct got_tag_object *tag;
1431 uint8_t *buf;
1432 size_t len;
1433 err = got_packfile_extract_object_to_mem(&buf,
1434 &len, obj, pack);
1435 if (err)
1436 goto done;
1437 obj->size = len;
1438 err = got_object_parse_tag(&tag, buf, len);
1439 if (err) {
1440 free(buf);
1441 goto done;
1443 idx = got_packidx_get_object_idx(packidx, &tag->id);
1444 if (idx == -1) {
1445 have_all_entries = 0;
1446 break;
1448 err = open_commit(&commit, pack, packidx, idx,
1449 &tag->id, objcache);
1450 got_object_tag_close(tag);
1451 free(buf);
1452 if (err)
1453 goto done;
1454 } else if (obj->type == GOT_OBJ_TYPE_COMMIT) {
1455 err = open_commit(&commit, pack, packidx, idx,
1456 &qid->id, objcache);
1457 if (err)
1458 goto done;
1459 } else {
1460 err = got_error(GOT_ERR_OBJ_TYPE);
1461 goto done;
1463 got_object_close(obj);
1464 obj = NULL;
1466 err = got_privsep_send_enumerated_commit(ibuf, &qid->id,
1467 got_object_commit_get_committer_time(commit));
1468 if (err)
1469 goto done;
1471 tree_id = got_object_commit_get_tree_id(commit);
1472 idx = got_packidx_get_object_idx(packidx, tree_id);
1473 if (idx == -1) {
1474 have_all_entries = 0;
1475 err = got_privsep_send_enumerated_tree(&totlen, ibuf,
1476 tree_id, "/", NULL, -1);
1477 if (err)
1478 goto done;
1479 break;
1482 if (got_object_idset_contains(idset, tree_id)) {
1483 got_object_qid_free(qid);
1484 qid = NULL;
1485 continue;
1488 err = enumerate_tree(&have_all_entries, ibuf, &totlen,
1489 tree_id, "/", pack, packidx, objcache, idset,
1490 &trees, &nalloc, &ntrees);
1491 if (err)
1492 goto done;
1494 if (!have_all_entries)
1495 break;
1497 got_object_qid_free(qid);
1498 qid = NULL;
1500 parents = got_object_commit_get_parent_ids(commit);
1501 if (parents) {
1502 struct got_object_qid *pid;
1503 STAILQ_FOREACH(pid, parents, entry) {
1504 if (got_object_idset_contains(idset, &pid->id))
1505 continue;
1506 err = got_object_qid_alloc_partial(&qid);
1507 if (err)
1508 goto done;
1509 memcpy(&qid->id, &pid->id, sizeof(qid->id));
1510 STAILQ_INSERT_TAIL(&commit_ids, qid, entry);
1511 qid = NULL;
1515 got_object_commit_close(commit);
1516 commit = NULL;
1519 if (have_all_entries) {
1520 err = got_privsep_send_object_enumeration_done(ibuf);
1521 if (err)
1522 goto done;
1523 } else {
1524 err = got_privsep_send_object_enumeration_incomplete(ibuf);
1525 if (err)
1526 goto done;
1528 done:
1529 if (obj)
1530 got_object_close(obj);
1531 if (commit)
1532 got_object_commit_close(commit);
1533 got_object_qid_free(qid);
1534 got_object_id_queue_free(&commit_ids);
1535 if (idset)
1536 got_object_idset_free(idset);
1537 for (i = 0; i < ntrees; i++) {
1538 struct enumerated_tree *tree = &trees[i];
1539 free(tree->buf);
1540 free(tree->path);
1541 free(tree->entries);
1543 free(trees);
1544 return err;
1547 enum findtwixt_color {
1548 COLOR_KEEP = 0,
1549 COLOR_DROP,
1550 COLOR_SKIP,
1551 COLOR_MAX,
1554 static const struct got_error *
1555 paint_commit(struct got_object_qid *qid, intptr_t color)
1557 if (color < 0 || color >= COLOR_MAX)
1558 return got_error(GOT_ERR_RANGE);
1560 qid->data = (void *)color;
1561 return NULL;
1564 static const struct got_error *
1565 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1566 intptr_t color)
1568 const struct got_error *err;
1569 struct got_object_qid *qid;
1571 err = got_object_qid_alloc_partial(&qid);
1572 if (err)
1573 return err;
1575 memcpy(&qid->id, id, sizeof(qid->id));
1576 STAILQ_INSERT_TAIL(ids, qid, entry);
1577 return paint_commit(qid, color);
1580 static const struct got_error *
1581 paint_commits(struct got_object_id_queue *ids, int *nids,
1582 struct got_object_idset *keep, struct got_object_idset *drop,
1583 struct got_object_idset *skip, struct got_pack *pack,
1584 struct got_packidx *packidx, struct imsgbuf *ibuf,
1585 struct got_object_cache *objcache)
1587 const struct got_error *err = NULL;
1588 struct got_commit_object *commit = NULL;
1589 struct got_object_id_queue painted;
1590 const struct got_object_id_queue *parents;
1591 struct got_object_qid *qid = NULL;
1592 int nqueued = *nids, nskip = 0, npainted = 0;
1594 STAILQ_INIT(&painted);
1596 while (!STAILQ_EMPTY(ids) && nskip != nqueued) {
1597 int idx;
1598 intptr_t color;
1600 if (sigint_received) {
1601 err = got_error(GOT_ERR_CANCELLED);
1602 goto done;
1605 qid = STAILQ_FIRST(ids);
1606 idx = got_packidx_get_object_idx(packidx, &qid->id);
1607 if (idx == -1) {
1608 qid = NULL;
1609 break;
1612 STAILQ_REMOVE_HEAD(ids, entry);
1613 nqueued--;
1614 color = (intptr_t)qid->data;
1615 if (color == COLOR_SKIP)
1616 nskip--;
1618 if (got_object_idset_contains(skip, &qid->id)) {
1619 got_object_qid_free(qid);
1620 qid = NULL;
1621 continue;
1624 switch (color) {
1625 case COLOR_KEEP:
1626 if (got_object_idset_contains(keep, &qid->id)) {
1627 got_object_qid_free(qid);
1628 qid = NULL;
1629 continue;
1631 if (got_object_idset_contains(drop, &qid->id)) {
1632 err = paint_commit(qid, COLOR_SKIP);
1633 if (err)
1634 goto done;
1636 err = got_object_idset_add(keep, &qid->id, NULL);
1637 if (err)
1638 goto done;
1639 break;
1640 case COLOR_DROP:
1641 if (got_object_idset_contains(drop, &qid->id)) {
1642 got_object_qid_free(qid);
1643 qid = NULL;
1644 continue;
1646 if (got_object_idset_contains(keep, &qid->id)) {
1647 err = paint_commit(qid, COLOR_SKIP);
1648 if (err)
1649 goto done;
1651 err = got_object_idset_add(drop, &qid->id, NULL);
1652 if (err)
1653 goto done;
1654 break;
1655 case COLOR_SKIP:
1656 if (!got_object_idset_contains(skip, &qid->id)) {
1657 err = got_object_idset_add(skip, &qid->id,
1658 NULL);
1659 if (err)
1660 goto done;
1662 break;
1663 default:
1664 /* should not happen */
1665 err = got_error_fmt(GOT_ERR_NOT_IMPL,
1666 "%s invalid commit color %d", __func__, color);
1667 goto done;
1670 err = open_commit(&commit, pack, packidx, idx, &qid->id,
1671 objcache);
1672 if (err)
1673 goto done;
1675 parents = got_object_commit_get_parent_ids(commit);
1676 if (parents) {
1677 struct got_object_qid *pid;
1678 color = (intptr_t)qid->data;
1679 STAILQ_FOREACH(pid, parents, entry) {
1680 err = queue_commit_id(ids, &pid->id, color);
1681 if (err)
1682 goto done;
1683 nqueued++;
1684 if (color == COLOR_SKIP)
1685 nskip++;
1689 got_object_commit_close(commit);
1690 commit = NULL;
1692 STAILQ_INSERT_TAIL(&painted, qid, entry);
1693 qid = NULL;
1694 npainted++;
1696 err = got_privsep_send_painted_commits(ibuf, &painted,
1697 &npainted, 1, 0);
1698 if (err)
1699 goto done;
1702 err = got_privsep_send_painted_commits(ibuf, &painted, &npainted, 1, 1);
1703 if (err)
1704 goto done;
1706 *nids = nqueued;
1707 done:
1708 if (commit)
1709 got_object_commit_close(commit);
1710 got_object_qid_free(qid);
1711 return err;
1714 static void
1715 commit_painting_free(struct got_object_idset **keep,
1716 struct got_object_idset **drop,
1717 struct got_object_idset **skip)
1719 if (*keep) {
1720 got_object_idset_free(*keep);
1721 *keep = NULL;
1723 if (*drop) {
1724 got_object_idset_free(*drop);
1725 *drop = NULL;
1727 if (*skip) {
1728 got_object_idset_free(*skip);
1729 *skip = NULL;
1733 static const struct got_error *
1734 commit_painting_init(struct imsgbuf *ibuf, struct got_object_idset **keep,
1735 struct got_object_idset **drop, struct got_object_idset **skip)
1737 const struct got_error *err = NULL;
1739 *keep = got_object_idset_alloc();
1740 if (*keep == NULL) {
1741 err = got_error_from_errno("got_object_idset_alloc");
1742 goto done;
1744 *drop = got_object_idset_alloc();
1745 if (*drop == NULL) {
1746 err = got_error_from_errno("got_object_idset_alloc");
1747 goto done;
1749 *skip = got_object_idset_alloc();
1750 if (*skip == NULL) {
1751 err = got_error_from_errno("got_object_idset_alloc");
1752 goto done;
1755 err = recv_object_ids(*keep, ibuf);
1756 if (err)
1757 goto done;
1758 err = recv_object_ids(*drop, ibuf);
1759 if (err)
1760 goto done;
1761 err = recv_object_ids(*skip, ibuf);
1762 if (err)
1763 goto done;
1765 done:
1766 if (err)
1767 commit_painting_free(keep, drop, skip);
1769 return err;
1772 static const struct got_error *
1773 commit_painting_request(struct imsg *imsg, struct imsgbuf *ibuf,
1774 struct got_pack *pack, struct got_packidx *packidx,
1775 struct got_object_cache *objcache, struct got_object_idset *keep,
1776 struct got_object_idset *drop, struct got_object_idset *skip)
1778 const struct got_error *err = NULL;
1779 struct got_imsg_commit_painting_request ireq;
1780 struct got_object_id id;
1781 size_t datalen;
1782 struct got_object_id_queue ids;
1783 int nids = 0;
1785 STAILQ_INIT(&ids);
1787 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1788 if (datalen != sizeof(ireq))
1789 return got_error(GOT_ERR_PRIVSEP_LEN);
1790 memcpy(&ireq, imsg->data, sizeof(ireq));
1791 memcpy(id.sha1, ireq.id, SHA1_DIGEST_LENGTH);
1793 err = queue_commit_id(&ids, &id, ireq.color);
1794 if (err)
1795 return err;
1796 nids = 1;
1798 err = paint_commits(&ids, &nids, keep, drop, skip,
1799 pack, packidx, ibuf, objcache);
1800 if (err)
1801 goto done;
1803 err = got_privsep_send_painted_commits(ibuf, &ids, &nids, 0, 1);
1804 if (err)
1805 goto done;
1807 err = got_privsep_send_painting_commits_done(ibuf);
1808 done:
1809 got_object_id_queue_free(&ids);
1810 return err;
1813 static const struct got_error *
1814 receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
1816 const struct got_error *err = NULL;
1817 struct imsg imsg;
1818 struct got_imsg_pack ipack;
1819 size_t datalen;
1820 struct got_pack *pack;
1822 *packp = NULL;
1824 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1825 if (err)
1826 return err;
1828 pack = calloc(1, sizeof(*pack));
1829 if (pack == NULL) {
1830 err = got_error_from_errno("calloc");
1831 goto done;
1834 if (imsg.hdr.type != GOT_IMSG_PACK) {
1835 err = got_error(GOT_ERR_PRIVSEP_MSG);
1836 goto done;
1839 if (imsg.fd == -1) {
1840 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1841 goto done;
1844 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1845 if (datalen != sizeof(ipack)) {
1846 err = got_error(GOT_ERR_PRIVSEP_LEN);
1847 goto done;
1849 memcpy(&ipack, imsg.data, sizeof(ipack));
1851 pack->filesize = ipack.filesize;
1852 pack->fd = dup(imsg.fd);
1853 if (pack->fd == -1) {
1854 err = got_error_from_errno("dup");
1855 goto done;
1857 if (lseek(pack->fd, 0, SEEK_SET) == -1) {
1858 err = got_error_from_errno("lseek");
1859 goto done;
1861 pack->path_packfile = strdup(ipack.path_packfile);
1862 if (pack->path_packfile == NULL) {
1863 err = got_error_from_errno("strdup");
1864 goto done;
1867 err = got_delta_cache_alloc(&pack->delta_cache);
1868 if (err)
1869 goto done;
1871 #ifndef GOT_PACK_NO_MMAP
1872 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1873 pack->fd, 0);
1874 if (pack->map == MAP_FAILED)
1875 pack->map = NULL; /* fall back to read(2) */
1876 #endif
1877 done:
1878 if (err) {
1879 if (imsg.fd != -1)
1880 close(imsg.fd);
1881 free(pack);
1882 } else
1883 *packp = pack;
1884 imsg_free(&imsg);
1885 return err;
1888 int
1889 main(int argc, char *argv[])
1891 const struct got_error *err = NULL;
1892 struct imsgbuf ibuf;
1893 struct imsg imsg;
1894 struct got_packidx *packidx = NULL;
1895 struct got_pack *pack = NULL;
1896 struct got_object_cache objcache;
1897 FILE *basefile = NULL, *accumfile = NULL, *delta_outfile = NULL;
1898 struct got_object_idset *keep = NULL, *drop = NULL, *skip = NULL;
1900 //static int attached;
1901 //while (!attached) sleep(1);
1903 signal(SIGINT, catch_sigint);
1905 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
1907 err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
1908 if (err) {
1909 err = got_error_from_errno("got_object_cache_init");
1910 got_privsep_send_error(&ibuf, err);
1911 return 1;
1914 #ifndef PROFILE
1915 /* revoke access to most system calls */
1916 if (pledge("stdio recvfd", NULL) == -1) {
1917 err = got_error_from_errno("pledge");
1918 got_privsep_send_error(&ibuf, err);
1919 return 1;
1921 #endif
1923 err = receive_packidx(&packidx, &ibuf);
1924 if (err) {
1925 got_privsep_send_error(&ibuf, err);
1926 return 1;
1929 err = receive_pack(&pack, &ibuf);
1930 if (err) {
1931 got_privsep_send_error(&ibuf, err);
1932 return 1;
1935 for (;;) {
1936 imsg.fd = -1;
1938 if (sigint_received) {
1939 err = got_error(GOT_ERR_CANCELLED);
1940 break;
1943 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1944 if (err) {
1945 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1946 err = NULL;
1947 break;
1950 if (imsg.hdr.type == GOT_IMSG_STOP)
1951 break;
1953 switch (imsg.hdr.type) {
1954 case GOT_IMSG_TMPFD:
1955 if (basefile == NULL) {
1956 err = receive_tempfile(&basefile, "w+",
1957 &imsg, &ibuf);
1958 } else if (accumfile == NULL) {
1959 err = receive_tempfile(&accumfile, "w+",
1960 &imsg, &ibuf);
1961 } else
1962 err = got_error(GOT_ERR_PRIVSEP_MSG);
1963 break;
1964 case GOT_IMSG_PACKED_OBJECT_REQUEST:
1965 err = object_request(&imsg, &ibuf, pack, packidx,
1966 &objcache);
1967 break;
1968 case GOT_IMSG_PACKED_RAW_OBJECT_REQUEST:
1969 if (basefile == NULL || accumfile == NULL) {
1970 err = got_error(GOT_ERR_PRIVSEP_MSG);
1971 break;
1973 err = raw_object_request(&imsg, &ibuf, pack, packidx,
1974 &objcache, basefile, accumfile);
1975 break;
1976 case GOT_IMSG_RAW_DELTA_OUTFD:
1977 if (delta_outfile != NULL) {
1978 err = got_error(GOT_ERR_PRIVSEP_MSG);
1979 break;
1981 err = receive_tempfile(&delta_outfile, "w",
1982 &imsg, &ibuf);
1983 break;
1984 case GOT_IMSG_RAW_DELTA_REQUEST:
1985 if (delta_outfile == NULL) {
1986 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1987 break;
1989 err = raw_delta_request(&imsg, &ibuf, delta_outfile,
1990 pack, packidx);
1991 break;
1992 case GOT_IMSG_DELTA_REUSE_REQUEST:
1993 if (delta_outfile == NULL) {
1994 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1995 break;
1997 err = delta_reuse_request(&imsg, &ibuf,
1998 delta_outfile, pack, packidx);
1999 break;
2000 case GOT_IMSG_COMMIT_REQUEST:
2001 err = commit_request(&imsg, &ibuf, pack, packidx,
2002 &objcache);
2003 break;
2004 case GOT_IMSG_TREE_REQUEST:
2005 err = tree_request(&imsg, &ibuf, pack, packidx,
2006 &objcache);
2007 break;
2008 case GOT_IMSG_BLOB_REQUEST:
2009 if (basefile == NULL || accumfile == NULL) {
2010 err = got_error(GOT_ERR_PRIVSEP_MSG);
2011 break;
2013 err = blob_request(&imsg, &ibuf, pack, packidx,
2014 &objcache, basefile, accumfile);
2015 break;
2016 case GOT_IMSG_TAG_REQUEST:
2017 err = tag_request(&imsg, &ibuf, pack, packidx,
2018 &objcache);
2019 break;
2020 case GOT_IMSG_COMMIT_TRAVERSAL_REQUEST:
2021 err = commit_traversal_request(&imsg, &ibuf, pack,
2022 packidx, &objcache);
2023 break;
2024 case GOT_IMSG_OBJECT_ENUMERATION_REQUEST:
2025 err = enumeration_request(&imsg, &ibuf, pack,
2026 packidx, &objcache);
2027 break;
2028 case GOT_IMSG_COMMIT_PAINTING_INIT:
2029 commit_painting_free(&keep, &drop, &skip);
2030 err = commit_painting_init(&ibuf, &keep, &drop, &skip);
2031 break;
2032 case GOT_IMSG_COMMIT_PAINTING_REQUEST:
2033 if (keep == NULL || drop == NULL || skip == NULL) {
2034 err = got_error(GOT_ERR_PRIVSEP_MSG);
2035 break;
2037 err = commit_painting_request(&imsg, &ibuf, pack,
2038 packidx, &objcache, keep, drop, skip);
2039 break;
2040 case GOT_IMSG_COMMIT_PAINTING_DONE:
2041 commit_painting_free(&keep, &drop, &skip);
2042 break;
2043 default:
2044 err = got_error(GOT_ERR_PRIVSEP_MSG);
2045 break;
2048 if (imsg.fd != -1 && close(imsg.fd) == -1 && err == NULL)
2049 err = got_error_from_errno("close");
2050 imsg_free(&imsg);
2051 if (err)
2052 break;
2055 commit_painting_free(&keep, &drop, &skip);
2056 if (packidx)
2057 got_packidx_close(packidx);
2058 if (pack)
2059 got_pack_close(pack);
2060 got_object_cache_close(&objcache);
2061 imsg_clear(&ibuf);
2062 if (basefile && fclose(basefile) == EOF && err == NULL)
2063 err = got_error_from_errno("fclose");
2064 if (accumfile && fclose(accumfile) == EOF && err == NULL)
2065 err = got_error_from_errno("fclose");
2066 if (delta_outfile && fclose(delta_outfile) == EOF && err == NULL)
2067 err = got_error_from_errno("fclose");
2068 if (err) {
2069 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
2070 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
2071 got_privsep_send_error(&ibuf, err);
2074 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
2075 err = got_error_from_errno("close");
2076 return err ? 1 : 0;