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;
272 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
273 if (err)
274 return err;
276 if (imsg.hdr.type != imsg_code) {
277 err = got_error(GOT_ERR_PRIVSEP_MSG);
278 goto done;
281 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
282 if (datalen != 0) {
283 err = got_error(GOT_ERR_PRIVSEP_LEN);
284 goto done;
286 if (imsg.fd == -1) {
287 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
288 goto done;
291 *f = fdopen(imsg.fd, "w+");
292 if (*f == NULL) {
293 err = got_error_from_errno("fdopen");
294 close(imsg.fd);
295 goto done;
297 done:
298 imsg_free(&imsg);
299 return err;
302 static const struct got_error *
303 receive_tempfile(FILE **f, const char *mode, struct imsg *imsg,
304 struct imsgbuf *ibuf)
306 size_t datalen;
308 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
309 if (datalen != 0)
310 return got_error(GOT_ERR_PRIVSEP_LEN);
312 if (imsg->fd == -1)
313 return got_error(GOT_ERR_PRIVSEP_NO_FD);
315 *f = fdopen(imsg->fd, mode);
316 if (*f == NULL)
317 return got_error_from_errno("fdopen");
318 imsg->fd = -1;
320 return NULL;
323 static const struct got_error *
324 blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
325 struct got_packidx *packidx, struct got_object_cache *objcache,
326 FILE *basefile, FILE *accumfile)
328 const struct got_error *err = NULL;
329 struct got_imsg_packed_object iobj;
330 struct got_object *obj = NULL;
331 FILE *outfile = NULL;
332 struct got_object_id id;
333 size_t datalen;
334 uint64_t blob_size;
335 uint8_t *buf = NULL;
337 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
338 if (datalen != sizeof(iobj))
339 return got_error(GOT_ERR_PRIVSEP_LEN);
340 memcpy(&iobj, imsg->data, sizeof(iobj));
341 memcpy(&id, &iobj.id, sizeof(id));
343 obj = got_object_cache_get(objcache, &id);
344 if (obj) {
345 obj->refcnt++;
346 } else {
347 err = open_object(&obj, pack, packidx, iobj.idx, &id,
348 objcache);
349 if (err)
350 return err;
353 err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
354 if (err)
355 goto done;
357 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
358 err = got_pack_get_max_delta_object_size(&blob_size, obj, pack);
359 if (err)
360 goto done;
361 } else
362 blob_size = obj->size;
364 if (blob_size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
365 err = got_packfile_extract_object_to_mem(&buf, &obj->size,
366 obj, pack);
367 else
368 err = got_packfile_extract_object(pack, obj, outfile, basefile,
369 accumfile);
370 if (err)
371 goto done;
373 err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, buf);
374 done:
375 free(buf);
376 if (outfile && fclose(outfile) == EOF && err == NULL)
377 err = got_error_from_errno("fclose");
378 got_object_close(obj);
379 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
380 got_privsep_send_error(ibuf, err);
382 return err;
385 static const struct got_error *
386 tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
387 struct got_packidx *packidx, struct got_object_cache *objcache)
389 const struct got_error *err = NULL;
390 struct got_imsg_packed_object iobj;
391 struct got_object *obj = NULL;
392 struct got_tag_object *tag = NULL;
393 uint8_t *buf = NULL;
394 size_t len;
395 struct got_object_id id;
396 size_t datalen;
398 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
399 if (datalen != sizeof(iobj))
400 return got_error(GOT_ERR_PRIVSEP_LEN);
401 memcpy(&iobj, imsg->data, sizeof(iobj));
402 memcpy(&id, &iobj.id, sizeof(id));
404 obj = got_object_cache_get(objcache, &id);
405 if (obj) {
406 obj->refcnt++;
407 } else {
408 err = open_object(&obj, pack, packidx, iobj.idx, &id,
409 objcache);
410 if (err)
411 return err;
414 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
415 if (err)
416 goto done;
418 obj->size = len;
419 err = got_object_parse_tag(&tag, buf, len);
420 if (err)
421 goto done;
423 err = got_privsep_send_tag(ibuf, tag);
424 done:
425 free(buf);
426 got_object_close(obj);
427 if (tag)
428 got_object_tag_close(tag);
429 if (err) {
430 if (err->code == GOT_ERR_PRIVSEP_PIPE)
431 err = NULL;
432 else
433 got_privsep_send_error(ibuf, err);
436 return err;
439 static const struct got_error *
440 tree_path_changed(int *changed, uint8_t **buf1, size_t *len1,
441 uint8_t **buf2, size_t *len2, const char *path,
442 struct got_pack *pack, struct got_packidx *packidx,
443 struct imsgbuf *ibuf, struct got_object_cache *objcache)
445 const struct got_error *err = NULL;
446 struct got_parsed_tree_entry pte1, pte2;
447 const char *seg, *s;
448 size_t seglen;
449 size_t remain1 = *len1, remain2 = *len2, elen;
450 uint8_t *next_entry1 = *buf1;
451 uint8_t *next_entry2 = *buf2;
453 memset(&pte1, 0, sizeof(pte1));
454 memset(&pte2, 0, sizeof(pte2));
456 *changed = 0;
458 /* We not do support comparing the root path. */
459 if (got_path_is_root_dir(path))
460 return got_error_path(path, GOT_ERR_BAD_PATH);
462 s = path;
463 while (*s == '/')
464 s++;
465 seg = s;
466 seglen = 0;
467 while (*s) {
468 if (*s != '/') {
469 s++;
470 seglen++;
471 if (*s)
472 continue;
475 /*
476 * As an optimization we compare entries in on-disk order
477 * rather than in got_path_cmp() order. We only need to
478 * find out if any entries differ. Parsing all entries and
479 * sorting them slows us down significantly when tree objects
480 * have thousands of entries. We can assume that on-disk entry
481 * ordering is stable, as per got_object_tree_create() and
482 * sort_tree_entries_the_way_git_likes_it(). Other orderings
483 * are incompatible with Git and would yield false positives
484 * here, too.
485 */
486 while (remain1 > 0) {
487 err = got_object_parse_tree_entry(&pte1, &elen,
488 next_entry1, remain1);
489 if (err)
490 return err;
491 next_entry1 += elen;
492 remain1 -= elen;
493 if (strncmp(pte1.name, seg, seglen) != 0 ||
494 pte1.name[seglen] != '\0') {
495 memset(&pte1, 0, sizeof(pte1));
496 continue;
497 } else
498 break;
500 if (pte1.name == NULL) {
501 err = got_error(GOT_ERR_NO_OBJ);
502 break;
505 if (remain2 == 0) {
506 *changed = 1;
507 break;
510 while (remain2 > 0) {
511 err = got_object_parse_tree_entry(&pte2, &elen,
512 next_entry2, remain2);
513 if (err)
514 return err;
515 next_entry2 += elen;
516 remain2 -= elen;
517 if (strncmp(pte2.name, seg, seglen) != 0 ||
518 pte2.name[seglen] != '\0') {
519 memset(&pte2, 0, sizeof(pte2));
520 continue;
521 } else
522 break;
525 if (pte2.name == NULL) {
526 *changed = 1;
527 break;
530 if (pte1.mode != pte2.mode) {
531 *changed = 1;
532 break;
535 if (memcmp(pte1.id, pte2.id, SHA1_DIGEST_LENGTH) == 0) {
536 *changed = 0;
537 break;
540 if (*s == '\0') { /* final path element */
541 *changed = 1;
542 break;
545 seg = s + 1;
546 s++;
547 seglen = 0;
548 if (*s) {
549 struct got_object_id id1, id2;
550 int idx;
552 memcpy(id1.sha1, pte1.id, SHA1_DIGEST_LENGTH);
553 idx = got_packidx_get_object_idx(packidx, &id1);
554 if (idx == -1) {
555 err = got_error_no_obj(&id1);
556 break;
558 free(*buf1);
559 *buf1 = NULL;
560 err = open_tree(buf1, len1, pack, packidx, idx, &id1,
561 objcache);
562 memset(&pte1, 0, sizeof(pte1));
563 if (err)
564 break;
565 next_entry1 = *buf1;
566 remain1 = *len1;
568 memcpy(id2.sha1, pte2.id, SHA1_DIGEST_LENGTH);
569 idx = got_packidx_get_object_idx(packidx, &id2);
570 if (idx == -1) {
571 err = got_error_no_obj(&id2);
572 break;
574 free(*buf2);
575 *buf2 = NULL;
576 err = open_tree(buf2, len2, pack, packidx, idx, &id2,
577 objcache);
578 memset(&pte2, 0, sizeof(pte2));
579 if (err)
580 break;
581 next_entry2 = *buf2;
582 remain2 = *len2;
586 return err;
589 static const struct got_error *
590 send_traversed_commits(struct got_object_id *commit_ids, size_t ncommits,
591 struct imsgbuf *ibuf)
593 struct ibuf *wbuf;
594 size_t i;
596 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
597 sizeof(struct got_imsg_traversed_commits) +
598 ncommits * sizeof(commit_ids[0]));
599 if (wbuf == NULL)
600 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
602 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1)
603 return got_error_from_errno("imsg_add TRAVERSED_COMMITS");
605 for (i = 0; i < ncommits; i++) {
606 struct got_object_id *id = &commit_ids[i];
607 if (imsg_add(wbuf, id, sizeof(*id)) == -1) {
608 return got_error_from_errno(
609 "imsg_add TRAVERSED_COMMITS");
613 wbuf->fd = -1;
614 imsg_close(ibuf, wbuf);
616 return got_privsep_flush_imsg(ibuf);
619 static const struct got_error *
620 send_commit_traversal_done(struct imsgbuf *ibuf)
622 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
623 NULL, 0) == -1)
624 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
626 return got_privsep_flush_imsg(ibuf);
629 static const struct got_error *
630 commit_traversal_request(struct imsg *imsg, struct imsgbuf *ibuf,
631 struct got_pack *pack, struct got_packidx *packidx,
632 struct got_object_cache *objcache)
634 const struct got_error *err = NULL;
635 struct got_imsg_commit_traversal_request ctreq;
636 struct got_object_qid *pid;
637 struct got_commit_object *commit = NULL, *pcommit = NULL;
638 struct got_object_id id;
639 size_t datalen;
640 char *path = NULL;
641 const int min_alloc = 64;
642 int changed = 0, ncommits = 0, nallocated = 0;
643 struct got_object_id *commit_ids = NULL;
645 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
646 if (datalen < sizeof(ctreq))
647 return got_error(GOT_ERR_PRIVSEP_LEN);
648 memcpy(&ctreq, imsg->data, sizeof(ctreq));
649 memcpy(&id, &ctreq.iobj.id, sizeof(id));
651 if (datalen != sizeof(ctreq) + ctreq.path_len)
652 return got_error(GOT_ERR_PRIVSEP_LEN);
653 if (ctreq.path_len == 0)
654 return got_error(GOT_ERR_PRIVSEP_LEN);
656 path = strndup(imsg->data + sizeof(ctreq), ctreq.path_len);
657 if (path == NULL)
658 return got_error_from_errno("strndup");
660 nallocated = min_alloc;
661 commit_ids = reallocarray(NULL, nallocated, sizeof(*commit_ids));
662 if (commit_ids == NULL)
663 return got_error_from_errno("reallocarray");
665 do {
666 const size_t max_datalen = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
667 int idx;
669 if (sigint_received) {
670 err = got_error(GOT_ERR_CANCELLED);
671 goto done;
674 if (commit == NULL) {
675 idx = got_packidx_get_object_idx(packidx, &id);
676 if (idx == -1)
677 break;
678 err = open_commit(&commit, pack, packidx,
679 idx, &id, objcache);
680 if (err) {
681 if (err->code != GOT_ERR_NO_OBJ)
682 goto done;
683 err = NULL;
684 break;
688 if (sizeof(struct got_imsg_traversed_commits) +
689 ncommits * sizeof(commit_ids[0]) >= max_datalen) {
690 err = send_traversed_commits(commit_ids, ncommits,
691 ibuf);
692 if (err)
693 goto done;
694 ncommits = 0;
696 ncommits++;
697 if (ncommits > nallocated) {
698 struct got_object_id *new;
699 nallocated += min_alloc;
700 new = reallocarray(commit_ids, nallocated,
701 sizeof(*commit_ids));
702 if (new == NULL) {
703 err = got_error_from_errno("reallocarray");
704 goto done;
706 commit_ids = new;
708 memcpy(&commit_ids[ncommits - 1], &id, sizeof(id));
710 pid = STAILQ_FIRST(&commit->parent_ids);
711 if (pid == NULL)
712 break;
714 idx = got_packidx_get_object_idx(packidx, &pid->id);
715 if (idx == -1)
716 break;
718 err = open_commit(&pcommit, pack, packidx, idx, &pid->id,
719 objcache);
720 if (err) {
721 if (err->code != GOT_ERR_NO_OBJ)
722 goto done;
723 err = NULL;
724 break;
727 if (path[0] == '/' && path[1] == '\0') {
728 if (got_object_id_cmp(pcommit->tree_id,
729 commit->tree_id) != 0) {
730 changed = 1;
731 break;
733 } else {
734 int pidx;
735 uint8_t *buf = NULL, *pbuf = NULL;
736 size_t len = 0, plen = 0;
738 idx = got_packidx_get_object_idx(packidx,
739 commit->tree_id);
740 if (idx == -1)
741 break;
742 pidx = got_packidx_get_object_idx(packidx,
743 pcommit->tree_id);
744 if (pidx == -1)
745 break;
747 err = open_tree(&buf, &len, pack, packidx, idx,
748 commit->tree_id, objcache);
749 if (err)
750 goto done;
752 err = open_tree(&pbuf, &plen, pack, packidx, pidx,
753 pcommit->tree_id, objcache);
754 if (err) {
755 free(buf);
756 goto done;
759 err = tree_path_changed(&changed, &buf, &len,
760 &pbuf, &plen, path, pack, packidx, ibuf,
761 objcache);
763 free(buf);
764 free(pbuf);
765 if (err) {
766 if (err->code != GOT_ERR_NO_OBJ)
767 goto done;
768 err = NULL;
769 break;
773 if (!changed) {
774 memcpy(&id, &pid->id, sizeof(id));
775 got_object_commit_close(commit);
776 commit = pcommit;
777 pcommit = NULL;
779 } while (!changed);
781 if (ncommits > 0) {
782 err = send_traversed_commits(commit_ids, ncommits, ibuf);
783 if (err)
784 goto done;
786 if (changed) {
787 err = got_privsep_send_commit(ibuf, commit);
788 if (err)
789 goto done;
792 err = send_commit_traversal_done(ibuf);
793 done:
794 free(path);
795 free(commit_ids);
796 if (commit)
797 got_object_commit_close(commit);
798 if (pcommit)
799 got_object_commit_close(pcommit);
800 if (err) {
801 if (err->code == GOT_ERR_PRIVSEP_PIPE)
802 err = NULL;
803 else
804 got_privsep_send_error(ibuf, err);
807 return err;
810 static const struct got_error *
811 raw_object_request(struct imsg *imsg, struct imsgbuf *ibuf,
812 struct got_pack *pack, struct got_packidx *packidx,
813 struct got_object_cache *objcache, FILE *basefile, FILE *accumfile)
815 const struct got_error *err = NULL;
816 uint8_t *buf = NULL;
817 uint64_t size = 0;
818 FILE *outfile = NULL;
819 struct got_imsg_packed_object iobj;
820 struct got_object *obj;
821 struct got_object_id id;
822 size_t datalen;
824 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
825 if (datalen != sizeof(iobj))
826 return got_error(GOT_ERR_PRIVSEP_LEN);
827 memcpy(&iobj, imsg->data, sizeof(iobj));
828 memcpy(&id, &iobj.id, sizeof(id));
830 obj = got_object_cache_get(objcache, &id);
831 if (obj) {
832 obj->refcnt++;
833 } else {
834 err = open_object(&obj, pack, packidx, iobj.idx, &id,
835 objcache);
836 if (err)
837 return err;
840 err = receive_file(&outfile, ibuf, GOT_IMSG_RAW_OBJECT_OUTFD);
841 if (err)
842 return err;
844 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
845 err = got_pack_get_max_delta_object_size(&size, obj, pack);
846 if (err)
847 goto done;
848 } else
849 size = obj->size;
851 if (size <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
852 err = got_packfile_extract_object_to_mem(&buf, &obj->size,
853 obj, pack);
854 else
855 err = got_packfile_extract_object(pack, obj, outfile, basefile,
856 accumfile);
857 if (err)
858 goto done;
860 err = got_privsep_send_raw_obj(ibuf, obj->size, obj->hdrlen, buf);
861 done:
862 free(buf);
863 if (outfile && fclose(outfile) == EOF && err == NULL)
864 err = got_error_from_errno("fclose");
865 got_object_close(obj);
866 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
867 got_privsep_send_error(ibuf, err);
869 return err;
872 static const struct got_error *
873 get_base_object_id(struct got_object_id *base_id, struct got_packidx *packidx,
874 off_t base_offset)
876 const struct got_error *err;
877 int idx;
879 err = got_packidx_get_offset_idx(&idx, packidx, base_offset);
880 if (err)
881 return err;
882 if (idx == -1)
883 return got_error(GOT_ERR_BAD_PACKIDX);
885 return got_packidx_get_object_id(base_id, packidx, idx);
888 static const struct got_error *
889 raw_delta_request(struct imsg *imsg, struct imsgbuf *ibuf,
890 FILE *delta_outfile, struct got_pack *pack,
891 struct got_packidx *packidx)
893 const struct got_error *err = NULL;
894 struct got_imsg_raw_delta_request req;
895 size_t datalen, delta_size, delta_compressed_size;
896 off_t delta_offset, delta_data_offset;
897 uint8_t *delta_buf = NULL;
898 struct got_object_id id, base_id;
899 off_t base_offset, delta_out_offset = 0;
900 uint64_t base_size = 0, result_size = 0;
901 size_t w;
903 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
904 if (datalen != sizeof(req))
905 return got_error(GOT_ERR_PRIVSEP_LEN);
906 memcpy(&req, imsg->data, sizeof(req));
907 memcpy(&id, &req.id, sizeof(id));
909 imsg->fd = -1;
911 err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
912 &delta_compressed_size, &delta_offset, &delta_data_offset,
913 &base_offset, &base_id, &base_size, &result_size,
914 pack, packidx, req.idx);
915 if (err)
916 goto done;
918 /*
919 * If this is an offset delta we must determine the base
920 * object ID ourselves.
921 */
922 if (base_offset != 0) {
923 err = get_base_object_id(&base_id, packidx, base_offset);
924 if (err)
925 goto done;
928 delta_out_offset = ftello(delta_outfile);
929 w = fwrite(delta_buf, 1, delta_compressed_size, delta_outfile);
930 if (w != delta_compressed_size) {
931 err = got_ferror(delta_outfile, GOT_ERR_IO);
932 goto done;
934 if (fflush(delta_outfile) == -1) {
935 err = got_error_from_errno("fflush");
936 goto done;
939 err = got_privsep_send_raw_delta(ibuf, base_size, result_size,
940 delta_size, delta_compressed_size, delta_offset, delta_out_offset,
941 &base_id);
942 done:
943 free(delta_buf);
944 return err;
947 struct search_deltas_arg {
948 struct imsgbuf *ibuf;
949 struct got_packidx *packidx;
950 struct got_pack *pack;
951 struct got_object_idset *idset;
952 struct got_imsg_reused_delta deltas[GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS];
953 size_t ndeltas;
954 };
956 static const struct got_error *
957 search_delta_for_object(struct got_object_id *id, void *data, void *arg)
959 const struct got_error *err;
960 struct search_deltas_arg *a = arg;
961 int obj_idx;
962 uint8_t *delta_buf = NULL;
963 uint64_t base_size, result_size;
964 size_t delta_size, delta_compressed_size;
965 off_t delta_offset, delta_data_offset, base_offset;
966 struct got_object_id base_id;
968 if (sigint_received)
969 return got_error(GOT_ERR_CANCELLED);
971 obj_idx = got_packidx_get_object_idx(a->packidx, id);
972 if (obj_idx == -1)
973 return NULL; /* object not present in our pack file */
975 err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
976 &delta_compressed_size, &delta_offset, &delta_data_offset,
977 &base_offset, &base_id, &base_size, &result_size,
978 a->pack, a->packidx, obj_idx);
979 if (err) {
980 if (err->code == GOT_ERR_OBJ_TYPE)
981 return NULL; /* object not stored as a delta */
982 return err;
985 /*
986 * If this is an offset delta we must determine the base
987 * object ID ourselves.
988 */
989 if (base_offset != 0) {
990 err = get_base_object_id(&base_id, a->packidx, base_offset);
991 if (err)
992 goto done;
995 if (got_object_idset_contains(a->idset, &base_id)) {
996 struct got_imsg_reused_delta *delta;
998 delta = &a->deltas[a->ndeltas++];
999 memcpy(&delta->id, id, sizeof(delta->id));
1000 memcpy(&delta->base_id, &base_id, sizeof(delta->base_id));
1001 delta->base_size = base_size;
1002 delta->result_size = result_size;
1003 delta->delta_size = delta_size;
1004 delta->delta_compressed_size = delta_compressed_size;
1005 delta->delta_offset = delta_data_offset;
1007 if (a->ndeltas >= GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS) {
1008 err = got_privsep_send_reused_deltas(a->ibuf,
1009 a->deltas, a->ndeltas);
1010 if (err)
1011 goto done;
1012 a->ndeltas = 0;
1015 done:
1016 free(delta_buf);
1017 return err;
1020 static const struct got_error *
1021 recv_object_ids(struct got_object_idset *idset, struct imsgbuf *ibuf)
1023 const struct got_error *err = NULL;
1024 int done = 0;
1025 struct got_object_id *ids;
1026 size_t nids, i;
1028 for (;;) {
1029 err = got_privsep_recv_object_idlist(&done, &ids, &nids, ibuf);
1030 if (err || done)
1031 break;
1032 for (i = 0; i < nids; i++) {
1033 err = got_object_idset_add(idset, &ids[i], NULL);
1034 if (err) {
1035 free(ids);
1036 return err;
1039 free(ids);
1042 return err;
1045 static const struct got_error *
1046 recv_object_id_queue(struct got_object_id_queue *queue,
1047 struct got_object_idset *queued_ids, struct imsgbuf *ibuf)
1049 const struct got_error *err = NULL;
1050 int done = 0;
1051 struct got_object_qid *qid;
1052 struct got_object_id *ids;
1053 size_t nids, i;
1055 for (;;) {
1056 err = got_privsep_recv_object_idlist(&done, &ids, &nids, ibuf);
1057 if (err || done)
1058 break;
1059 for (i = 0; i < nids; i++) {
1060 err = got_object_qid_alloc_partial(&qid);
1061 if (err)
1062 return err;
1063 memcpy(&qid->id, &ids[i], sizeof(qid->id));
1064 STAILQ_INSERT_TAIL(queue, qid, entry);
1065 err = got_object_idset_add(queued_ids, &qid->id, NULL);
1066 if (err)
1067 return err;
1071 return err;
1074 static const struct got_error *
1075 delta_reuse_request(struct imsg *imsg, struct imsgbuf *ibuf,
1076 struct got_pack *pack, struct got_packidx *packidx)
1078 const struct got_error *err = NULL;
1079 struct got_object_idset *idset;
1080 struct search_deltas_arg sda;
1082 idset = got_object_idset_alloc();
1083 if (idset == NULL)
1084 return got_error_from_errno("got_object_idset_alloc");
1086 err = recv_object_ids(idset, ibuf);
1087 if (err)
1088 return err;
1090 memset(&sda, 0, sizeof(sda));
1091 sda.ibuf = ibuf;
1092 sda.idset = idset;
1093 sda.pack = pack;
1094 sda.packidx = packidx;
1095 err = got_object_idset_for_each(idset, search_delta_for_object, &sda);
1096 if (err)
1097 goto done;
1099 if (sda.ndeltas > 0) {
1100 err = got_privsep_send_reused_deltas(ibuf, sda.deltas,
1101 sda.ndeltas);
1102 if (err)
1103 goto done;
1106 err = got_privsep_send_reused_deltas_done(ibuf);
1107 done:
1108 got_object_idset_free(idset);
1109 return err;
1112 static const struct got_error *
1113 receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
1115 const struct got_error *err = NULL;
1116 struct imsg imsg;
1117 struct got_imsg_packidx ipackidx;
1118 size_t datalen;
1119 struct got_packidx *p;
1121 *packidx = NULL;
1123 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1124 if (err)
1125 return err;
1127 p = calloc(1, sizeof(*p));
1128 if (p == NULL) {
1129 err = got_error_from_errno("calloc");
1130 goto done;
1133 if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
1134 err = got_error(GOT_ERR_PRIVSEP_MSG);
1135 goto done;
1138 if (imsg.fd == -1) {
1139 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1140 goto done;
1143 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1144 if (datalen != sizeof(ipackidx)) {
1145 err = got_error(GOT_ERR_PRIVSEP_LEN);
1146 goto done;
1148 memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
1150 p->len = ipackidx.len;
1151 p->fd = dup(imsg.fd);
1152 if (p->fd == -1) {
1153 err = got_error_from_errno("dup");
1154 goto done;
1156 if (lseek(p->fd, 0, SEEK_SET) == -1) {
1157 err = got_error_from_errno("lseek");
1158 goto done;
1161 #ifndef GOT_PACK_NO_MMAP
1162 if (p->len > 0 && p->len <= SIZE_MAX) {
1163 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
1164 if (p->map == MAP_FAILED)
1165 p->map = NULL; /* fall back to read(2) */
1167 #endif
1168 err = got_packidx_init_hdr(p, 1, ipackidx.packfile_size);
1169 done:
1170 if (err) {
1171 if (imsg.fd != -1)
1172 close(imsg.fd);
1173 got_packidx_close(p);
1174 } else
1175 *packidx = p;
1176 imsg_free(&imsg);
1177 return err;
1180 static const struct got_error *
1181 send_tree_enumeration_done(struct imsgbuf *ibuf)
1183 if (imsg_compose(ibuf, GOT_IMSG_TREE_ENUMERATION_DONE, 0, 0, -1,
1184 NULL, 0) == -1)
1185 return got_error_from_errno("imsg_compose TREE_ENUMERATION_DONE");
1187 return got_privsep_flush_imsg(ibuf);
1190 struct enumerated_tree {
1191 struct got_object_id id;
1192 char *path;
1193 uint8_t *buf;
1194 struct got_parsed_tree_entry *entries;
1195 int nentries;
1198 static const struct got_error *
1199 enumerate_tree(int *have_all_entries, struct imsgbuf *ibuf, size_t *totlen,
1200 struct got_object_id *tree_id,
1201 const char *path, struct got_pack *pack, struct got_packidx *packidx,
1202 struct got_object_cache *objcache, struct got_object_idset *idset,
1203 struct enumerated_tree **trees, size_t *nalloc, size_t *ntrees)
1205 const struct got_error *err = NULL;
1206 struct got_object_id_queue ids;
1207 struct got_object_qid *qid;
1208 uint8_t *buf = NULL;
1209 size_t len = 0;
1210 struct got_parsed_tree_entry *entries = NULL;
1211 size_t nentries = 0, nentries_alloc = 0, i;
1212 struct enumerated_tree *tree;
1214 *ntrees = 0;
1215 *have_all_entries = 1;
1216 STAILQ_INIT(&ids);
1218 err = got_object_qid_alloc_partial(&qid);
1219 if (err)
1220 return err;
1221 memcpy(&qid->id, tree_id, sizeof(*tree_id));
1222 qid->data = strdup(path);
1223 if (qid->data == NULL) {
1224 err = got_error_from_errno("strdup");
1225 goto done;
1227 STAILQ_INSERT_TAIL(&ids, qid, entry);
1228 qid = NULL;
1230 /* Traverse the tree hierarchy, gather tree object IDs and paths. */
1231 do {
1232 const char *path;
1233 int idx, i;
1235 if (sigint_received) {
1236 err = got_error(GOT_ERR_CANCELLED);
1237 goto done;
1240 qid = STAILQ_FIRST(&ids);
1241 STAILQ_REMOVE_HEAD(&ids, entry);
1242 path = qid->data;
1244 idx = got_packidx_get_object_idx(packidx, &qid->id);
1245 if (idx == -1) {
1246 *have_all_entries = 0;
1247 break;
1250 err = open_tree(&buf, &len, pack, packidx, idx, &qid->id,
1251 objcache);
1252 if (err) {
1253 if (err->code != GOT_ERR_NO_OBJ)
1254 goto done;
1257 err = got_object_parse_tree(&entries, &nentries,
1258 &nentries_alloc, buf, len);
1259 if (err)
1260 goto done;
1262 err = got_object_idset_add(idset, &qid->id, NULL);
1263 if (err)
1264 goto done;
1266 for (i = 0; i < nentries; i++) {
1267 struct got_object_qid *eqid = NULL;
1268 struct got_parsed_tree_entry *pte = &entries[i];
1269 char *p;
1271 if (!S_ISDIR(pte->mode))
1272 continue;
1274 err = got_object_qid_alloc_partial(&eqid);
1275 if (err)
1276 goto done;
1277 memcpy(eqid->id.sha1, pte->id, sizeof(eqid->id.sha1));
1279 if (got_object_idset_contains(idset, &eqid->id)) {
1280 got_object_qid_free(eqid);
1281 continue;
1284 if (asprintf(&p, "%s%s%s", path,
1285 got_path_is_root_dir(path) ? "" : "/",
1286 pte->name) == -1) {
1287 err = got_error_from_errno("asprintf");
1288 got_object_qid_free(eqid);
1289 goto done;
1291 eqid->data = p;
1292 STAILQ_INSERT_TAIL(&ids, eqid, entry);
1295 if (*ntrees >= *nalloc) {
1296 struct enumerated_tree *new;
1297 new = recallocarray(*trees, *nalloc, *nalloc + 16,
1298 sizeof(*new));
1299 if (new == NULL) {
1300 err = got_error_from_errno("malloc");
1301 goto done;
1303 *trees = new;
1304 *nalloc += 16;
1306 tree = &(*trees)[*ntrees];
1307 (*ntrees)++;
1308 memcpy(&tree->id, &qid->id, sizeof(tree->id));
1309 tree->path = qid->data;
1310 tree->buf = buf;
1311 buf = NULL;
1312 tree->entries = entries;
1313 entries = NULL;
1314 nentries_alloc = 0;
1315 tree->nentries = nentries;
1316 nentries = 0;
1318 got_object_qid_free(qid);
1319 qid = NULL;
1320 } while (!STAILQ_EMPTY(&ids));
1322 if (*have_all_entries) {
1323 int i;
1325 * We have managed to traverse all entries in the hierarchy.
1326 * Tell the main process what we have found.
1328 for (i = 0; i < *ntrees; i++) {
1329 tree = &(*trees)[i];
1330 err = got_privsep_send_enumerated_tree(totlen,
1331 ibuf, &tree->id, tree->path, tree->entries,
1332 tree->nentries);
1333 if (err)
1334 goto done;
1335 free(tree->buf);
1336 tree->buf = NULL;
1337 free(tree->path);
1338 tree->path = NULL;
1339 free(tree->entries);
1340 tree->entries = NULL;
1342 *ntrees = 0; /* don't loop again below to free memory */
1344 err = send_tree_enumeration_done(ibuf);
1345 } else {
1347 * We can only load fully packed tree hierarchies on
1348 * behalf of the main process, otherwise the main process
1349 * gets a wrong idea about which tree objects have
1350 * already been traversed.
1351 * Indicate a missing entry for the root of this tree.
1352 * The main process should continue by loading this
1353 * entire tree the slow way.
1355 err = got_privsep_send_enumerated_tree(totlen, ibuf,
1356 tree_id, "/", NULL, -1);
1357 if (err)
1358 goto done;
1360 done:
1361 free(buf);
1362 free(entries);
1363 for (i = 0; i < *ntrees; i++) {
1364 tree = &(*trees)[i];
1365 free(tree->buf);
1366 tree->buf = NULL;
1367 free(tree->path);
1368 tree->path = NULL;
1369 free(tree->entries);
1370 tree->entries = NULL;
1372 if (qid)
1373 free(qid->data);
1374 got_object_qid_free(qid);
1375 got_object_id_queue_free(&ids);
1376 if (err) {
1377 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1378 err = NULL;
1379 else
1380 got_privsep_send_error(ibuf, err);
1383 return err;
1386 static const struct got_error *
1387 enumeration_request(struct imsg *imsg, struct imsgbuf *ibuf,
1388 struct got_pack *pack, struct got_packidx *packidx,
1389 struct got_object_cache *objcache)
1391 const struct got_error *err = NULL;
1392 struct got_object_id_queue commit_ids;
1393 const struct got_object_id_queue *parents = NULL;
1394 struct got_object_qid *qid = NULL;
1395 struct got_object *obj = NULL;
1396 struct got_commit_object *commit = NULL;
1397 struct got_object_id *tree_id = NULL;
1398 size_t totlen = 0;
1399 struct got_object_idset *idset, *queued_ids = NULL;
1400 int i, idx, have_all_entries = 1;
1401 struct enumerated_tree *trees = NULL;
1402 size_t ntrees = 0, nalloc = 16;
1404 STAILQ_INIT(&commit_ids);
1406 trees = calloc(nalloc, sizeof(*trees));
1407 if (trees == NULL)
1408 return got_error_from_errno("calloc");
1410 idset = got_object_idset_alloc();
1411 if (idset == NULL) {
1412 err = got_error_from_errno("got_object_idset_alloc");
1413 goto done;
1416 queued_ids = got_object_idset_alloc();
1417 if (queued_ids == NULL) {
1418 err = got_error_from_errno("got_object_idset_alloc");
1419 goto done;
1422 err = recv_object_id_queue(&commit_ids, queued_ids, ibuf);
1423 if (err)
1424 goto done;
1426 if (STAILQ_EMPTY(&commit_ids)) {
1427 err = got_error(GOT_ERR_PRIVSEP_MSG);
1428 goto done;
1431 err = recv_object_ids(idset, ibuf);
1432 if (err)
1433 goto done;
1435 while (!STAILQ_EMPTY(&commit_ids)) {
1436 if (sigint_received) {
1437 err = got_error(GOT_ERR_CANCELLED);
1438 goto done;
1441 qid = STAILQ_FIRST(&commit_ids);
1442 STAILQ_REMOVE_HEAD(&commit_ids, entry);
1444 if (got_object_idset_contains(idset, &qid->id)) {
1445 got_object_qid_free(qid);
1446 qid = NULL;
1447 continue;
1450 idx = got_packidx_get_object_idx(packidx, &qid->id);
1451 if (idx == -1) {
1452 have_all_entries = 0;
1453 break;
1456 err = open_object(&obj, pack, packidx, idx, &qid->id,
1457 objcache);
1458 if (err)
1459 goto done;
1460 if (obj->type == GOT_OBJ_TYPE_TAG) {
1461 struct got_tag_object *tag;
1462 uint8_t *buf;
1463 size_t len;
1464 err = got_packfile_extract_object_to_mem(&buf,
1465 &len, obj, pack);
1466 if (err)
1467 goto done;
1468 obj->size = len;
1469 err = got_object_parse_tag(&tag, buf, len);
1470 if (err) {
1471 free(buf);
1472 goto done;
1474 idx = got_packidx_get_object_idx(packidx, &tag->id);
1475 if (idx == -1) {
1476 have_all_entries = 0;
1477 break;
1479 err = open_commit(&commit, pack, packidx, idx,
1480 &tag->id, objcache);
1481 got_object_tag_close(tag);
1482 free(buf);
1483 if (err)
1484 goto done;
1485 } else if (obj->type == GOT_OBJ_TYPE_COMMIT) {
1486 err = open_commit(&commit, pack, packidx, idx,
1487 &qid->id, objcache);
1488 if (err)
1489 goto done;
1490 } else {
1491 err = got_error(GOT_ERR_OBJ_TYPE);
1492 goto done;
1494 got_object_close(obj);
1495 obj = NULL;
1497 err = got_privsep_send_enumerated_commit(ibuf, &qid->id,
1498 got_object_commit_get_committer_time(commit));
1499 if (err)
1500 goto done;
1502 tree_id = got_object_commit_get_tree_id(commit);
1503 idx = got_packidx_get_object_idx(packidx, tree_id);
1504 if (idx == -1) {
1505 have_all_entries = 0;
1506 err = got_privsep_send_enumerated_tree(&totlen, ibuf,
1507 tree_id, "/", NULL, -1);
1508 if (err)
1509 goto done;
1510 break;
1513 if (got_object_idset_contains(idset, tree_id)) {
1514 got_object_qid_free(qid);
1515 qid = NULL;
1516 err = send_tree_enumeration_done(ibuf);
1517 if (err)
1518 goto done;
1519 continue;
1522 err = enumerate_tree(&have_all_entries, ibuf, &totlen,
1523 tree_id, "/", pack, packidx, objcache, idset,
1524 &trees, &nalloc, &ntrees);
1525 if (err)
1526 goto done;
1528 if (!have_all_entries)
1529 break;
1531 got_object_qid_free(qid);
1532 qid = NULL;
1534 parents = got_object_commit_get_parent_ids(commit);
1535 if (parents) {
1536 struct got_object_qid *pid;
1537 STAILQ_FOREACH(pid, parents, entry) {
1538 if (got_object_idset_contains(idset, &pid->id))
1539 continue;
1540 if (got_object_idset_contains(queued_ids, &pid->id))
1541 continue;
1542 err = got_object_qid_alloc_partial(&qid);
1543 if (err)
1544 goto done;
1545 memcpy(&qid->id, &pid->id, sizeof(qid->id));
1546 STAILQ_INSERT_TAIL(&commit_ids, qid, entry);
1547 qid = NULL;
1551 got_object_commit_close(commit);
1552 commit = NULL;
1555 if (have_all_entries) {
1556 err = got_privsep_send_object_enumeration_done(ibuf);
1557 if (err)
1558 goto done;
1559 } else {
1560 err = got_privsep_send_object_enumeration_incomplete(ibuf);
1561 if (err)
1562 goto done;
1564 done:
1565 if (obj)
1566 got_object_close(obj);
1567 if (commit)
1568 got_object_commit_close(commit);
1569 got_object_qid_free(qid);
1570 got_object_id_queue_free(&commit_ids);
1571 if (idset)
1572 got_object_idset_free(idset);
1573 if (queued_ids)
1574 got_object_idset_free(queued_ids);
1575 for (i = 0; i < ntrees; i++) {
1576 struct enumerated_tree *tree = &trees[i];
1577 free(tree->buf);
1578 free(tree->path);
1579 free(tree->entries);
1581 free(trees);
1582 return err;
1585 enum findtwixt_color {
1586 COLOR_KEEP = 0,
1587 COLOR_DROP,
1588 COLOR_SKIP,
1589 COLOR_MAX,
1592 static const struct got_error *
1593 paint_commit(struct got_object_qid *qid, intptr_t color)
1595 if (color < 0 || color >= COLOR_MAX)
1596 return got_error(GOT_ERR_RANGE);
1598 qid->data = (void *)color;
1599 return NULL;
1602 static const struct got_error *
1603 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1604 intptr_t color)
1606 const struct got_error *err;
1607 struct got_object_qid *qid;
1609 err = got_object_qid_alloc_partial(&qid);
1610 if (err)
1611 return err;
1613 memcpy(&qid->id, id, sizeof(qid->id));
1614 STAILQ_INSERT_TAIL(ids, qid, entry);
1615 return paint_commit(qid, color);
1618 static const struct got_error *
1619 paint_commits(struct got_object_id_queue *ids, int *nids,
1620 struct got_object_idset *keep, struct got_object_idset *drop,
1621 struct got_object_idset *skip, struct got_pack *pack,
1622 struct got_packidx *packidx, struct imsgbuf *ibuf,
1623 struct got_object_cache *objcache)
1625 const struct got_error *err = NULL;
1626 struct got_commit_object *commit = NULL;
1627 struct got_object_id_queue painted;
1628 const struct got_object_id_queue *parents;
1629 struct got_object_qid *qid = NULL;
1630 int nqueued = *nids, nskip = 0, npainted = 0;
1632 STAILQ_INIT(&painted);
1634 while (!STAILQ_EMPTY(ids) && nskip != nqueued) {
1635 int idx;
1636 intptr_t color;
1638 if (sigint_received) {
1639 err = got_error(GOT_ERR_CANCELLED);
1640 goto done;
1643 qid = STAILQ_FIRST(ids);
1644 idx = got_packidx_get_object_idx(packidx, &qid->id);
1645 if (idx == -1) {
1646 qid = NULL;
1647 break;
1650 STAILQ_REMOVE_HEAD(ids, entry);
1651 nqueued--;
1652 color = (intptr_t)qid->data;
1653 if (color == COLOR_SKIP)
1654 nskip--;
1656 if (got_object_idset_contains(skip, &qid->id)) {
1657 got_object_qid_free(qid);
1658 qid = NULL;
1659 continue;
1662 switch (color) {
1663 case COLOR_KEEP:
1664 if (got_object_idset_contains(keep, &qid->id)) {
1665 got_object_qid_free(qid);
1666 qid = NULL;
1667 continue;
1669 if (got_object_idset_contains(drop, &qid->id)) {
1670 err = paint_commit(qid, COLOR_SKIP);
1671 if (err)
1672 goto done;
1674 err = got_object_idset_add(keep, &qid->id, NULL);
1675 if (err)
1676 goto done;
1677 break;
1678 case COLOR_DROP:
1679 if (got_object_idset_contains(drop, &qid->id)) {
1680 got_object_qid_free(qid);
1681 qid = NULL;
1682 continue;
1684 if (got_object_idset_contains(keep, &qid->id)) {
1685 err = paint_commit(qid, COLOR_SKIP);
1686 if (err)
1687 goto done;
1689 err = got_object_idset_add(drop, &qid->id, NULL);
1690 if (err)
1691 goto done;
1692 break;
1693 case COLOR_SKIP:
1694 if (!got_object_idset_contains(skip, &qid->id)) {
1695 err = got_object_idset_add(skip, &qid->id,
1696 NULL);
1697 if (err)
1698 goto done;
1700 break;
1701 default:
1702 /* should not happen */
1703 err = got_error_fmt(GOT_ERR_NOT_IMPL,
1704 "%s invalid commit color %"PRIdPTR, __func__,
1705 color);
1706 goto done;
1709 err = open_commit(&commit, pack, packidx, idx, &qid->id,
1710 objcache);
1711 if (err)
1712 goto done;
1714 parents = got_object_commit_get_parent_ids(commit);
1715 if (parents) {
1716 struct got_object_qid *pid;
1717 color = (intptr_t)qid->data;
1718 STAILQ_FOREACH(pid, parents, entry) {
1719 err = queue_commit_id(ids, &pid->id, color);
1720 if (err)
1721 goto done;
1722 nqueued++;
1723 if (color == COLOR_SKIP)
1724 nskip++;
1728 got_object_commit_close(commit);
1729 commit = NULL;
1731 STAILQ_INSERT_TAIL(&painted, qid, entry);
1732 qid = NULL;
1733 npainted++;
1735 err = got_privsep_send_painted_commits(ibuf, &painted,
1736 &npainted, 1, 0);
1737 if (err)
1738 goto done;
1741 err = got_privsep_send_painted_commits(ibuf, &painted, &npainted, 1, 1);
1742 if (err)
1743 goto done;
1745 *nids = nqueued;
1746 done:
1747 if (commit)
1748 got_object_commit_close(commit);
1749 got_object_qid_free(qid);
1750 return err;
1753 static void
1754 commit_painting_free(struct got_object_idset **keep,
1755 struct got_object_idset **drop,
1756 struct got_object_idset **skip)
1758 if (*keep) {
1759 got_object_idset_free(*keep);
1760 *keep = NULL;
1762 if (*drop) {
1763 got_object_idset_free(*drop);
1764 *drop = NULL;
1766 if (*skip) {
1767 got_object_idset_free(*skip);
1768 *skip = NULL;
1772 static const struct got_error *
1773 commit_painting_init(struct imsgbuf *ibuf, struct got_object_idset **keep,
1774 struct got_object_idset **drop, struct got_object_idset **skip)
1776 const struct got_error *err = NULL;
1778 *keep = got_object_idset_alloc();
1779 if (*keep == NULL) {
1780 err = got_error_from_errno("got_object_idset_alloc");
1781 goto done;
1783 *drop = got_object_idset_alloc();
1784 if (*drop == NULL) {
1785 err = got_error_from_errno("got_object_idset_alloc");
1786 goto done;
1788 *skip = got_object_idset_alloc();
1789 if (*skip == NULL) {
1790 err = got_error_from_errno("got_object_idset_alloc");
1791 goto done;
1794 err = recv_object_ids(*keep, ibuf);
1795 if (err)
1796 goto done;
1797 err = recv_object_ids(*drop, ibuf);
1798 if (err)
1799 goto done;
1800 err = recv_object_ids(*skip, ibuf);
1801 if (err)
1802 goto done;
1804 done:
1805 if (err)
1806 commit_painting_free(keep, drop, skip);
1808 return err;
1811 static const struct got_error *
1812 commit_painting_request(struct imsg *imsg, struct imsgbuf *ibuf,
1813 struct got_pack *pack, struct got_packidx *packidx,
1814 struct got_object_cache *objcache, struct got_object_idset *keep,
1815 struct got_object_idset *drop, struct got_object_idset *skip)
1817 const struct got_error *err = NULL;
1818 struct got_imsg_commit_painting_request ireq;
1819 struct got_object_id id;
1820 size_t datalen;
1821 struct got_object_id_queue ids;
1822 int nids = 0;
1824 STAILQ_INIT(&ids);
1826 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1827 if (datalen != sizeof(ireq))
1828 return got_error(GOT_ERR_PRIVSEP_LEN);
1829 memcpy(&ireq, imsg->data, sizeof(ireq));
1830 memcpy(&id, &ireq.id, sizeof(id));
1832 err = queue_commit_id(&ids, &id, ireq.color);
1833 if (err)
1834 return err;
1835 nids = 1;
1837 err = paint_commits(&ids, &nids, keep, drop, skip,
1838 pack, packidx, ibuf, objcache);
1839 if (err)
1840 goto done;
1842 err = got_privsep_send_painted_commits(ibuf, &ids, &nids, 0, 1);
1843 if (err)
1844 goto done;
1846 err = got_privsep_send_painting_commits_done(ibuf);
1847 done:
1848 got_object_id_queue_free(&ids);
1849 return err;
1852 static const struct got_error *
1853 receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
1855 const struct got_error *err = NULL;
1856 struct imsg imsg;
1857 struct got_imsg_pack ipack;
1858 size_t datalen;
1859 struct got_pack *pack;
1861 *packp = NULL;
1863 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1864 if (err)
1865 return err;
1867 pack = calloc(1, sizeof(*pack));
1868 if (pack == NULL) {
1869 err = got_error_from_errno("calloc");
1870 goto done;
1873 if (imsg.hdr.type != GOT_IMSG_PACK) {
1874 err = got_error(GOT_ERR_PRIVSEP_MSG);
1875 goto done;
1878 if (imsg.fd == -1) {
1879 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1880 goto done;
1883 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1884 if (datalen != sizeof(ipack)) {
1885 err = got_error(GOT_ERR_PRIVSEP_LEN);
1886 goto done;
1888 memcpy(&ipack, imsg.data, sizeof(ipack));
1890 pack->filesize = ipack.filesize;
1891 pack->fd = dup(imsg.fd);
1892 if (pack->fd == -1) {
1893 err = got_error_from_errno("dup");
1894 goto done;
1896 if (lseek(pack->fd, 0, SEEK_SET) == -1) {
1897 err = got_error_from_errno("lseek");
1898 goto done;
1900 pack->path_packfile = strdup(ipack.path_packfile);
1901 if (pack->path_packfile == NULL) {
1902 err = got_error_from_errno("strdup");
1903 goto done;
1906 err = got_delta_cache_alloc(&pack->delta_cache);
1907 if (err)
1908 goto done;
1910 #ifndef GOT_PACK_NO_MMAP
1911 if (pack->filesize > 0 && pack->filesize <= SIZE_MAX) {
1912 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1913 pack->fd, 0);
1914 if (pack->map == MAP_FAILED)
1915 pack->map = NULL; /* fall back to read(2) */
1917 #endif
1918 done:
1919 if (err) {
1920 if (imsg.fd != -1)
1921 close(imsg.fd);
1922 free(pack);
1923 } else
1924 *packp = pack;
1925 imsg_free(&imsg);
1926 return err;
1929 int
1930 main(int argc, char *argv[])
1932 const struct got_error *err = NULL;
1933 struct imsgbuf ibuf;
1934 struct imsg imsg;
1935 struct got_packidx *packidx = NULL;
1936 struct got_pack *pack = NULL;
1937 struct got_object_cache objcache;
1938 FILE *basefile = NULL, *accumfile = NULL, *delta_outfile = NULL;
1939 struct got_object_idset *keep = NULL, *drop = NULL, *skip = NULL;
1940 struct got_parsed_tree_entry *entries = NULL;
1941 size_t nentries = 0, nentries_alloc = 0;
1943 //static int attached;
1944 //while (!attached) sleep(1);
1946 signal(SIGINT, catch_sigint);
1948 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
1950 err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
1951 if (err) {
1952 err = got_error_from_errno("got_object_cache_init");
1953 got_privsep_send_error(&ibuf, err);
1954 return 1;
1957 #ifndef PROFILE
1958 /* revoke access to most system calls */
1959 if (pledge("stdio recvfd", NULL) == -1) {
1960 err = got_error_from_errno("pledge");
1961 got_privsep_send_error(&ibuf, err);
1962 return 1;
1964 #endif
1966 err = receive_packidx(&packidx, &ibuf);
1967 if (err) {
1968 got_privsep_send_error(&ibuf, err);
1969 return 1;
1972 err = receive_pack(&pack, &ibuf);
1973 if (err) {
1974 got_privsep_send_error(&ibuf, err);
1975 return 1;
1978 for (;;) {
1979 imsg.fd = -1;
1981 if (sigint_received) {
1982 err = got_error(GOT_ERR_CANCELLED);
1983 break;
1986 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1987 if (err) {
1988 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1989 err = NULL;
1990 break;
1993 if (imsg.hdr.type == GOT_IMSG_STOP)
1994 break;
1996 switch (imsg.hdr.type) {
1997 case GOT_IMSG_TMPFD:
1998 if (basefile == NULL) {
1999 err = receive_tempfile(&basefile, "w+",
2000 &imsg, &ibuf);
2001 } else if (accumfile == NULL) {
2002 err = receive_tempfile(&accumfile, "w+",
2003 &imsg, &ibuf);
2004 } else
2005 err = got_error(GOT_ERR_PRIVSEP_MSG);
2006 break;
2007 case GOT_IMSG_PACKED_OBJECT_REQUEST:
2008 err = object_request(&imsg, &ibuf, pack, packidx,
2009 &objcache);
2010 break;
2011 case GOT_IMSG_PACKED_RAW_OBJECT_REQUEST:
2012 if (basefile == NULL || accumfile == NULL) {
2013 err = got_error(GOT_ERR_PRIVSEP_MSG);
2014 break;
2016 err = raw_object_request(&imsg, &ibuf, pack, packidx,
2017 &objcache, basefile, accumfile);
2018 break;
2019 case GOT_IMSG_RAW_DELTA_OUTFD:
2020 if (delta_outfile != NULL) {
2021 err = got_error(GOT_ERR_PRIVSEP_MSG);
2022 break;
2024 err = receive_tempfile(&delta_outfile, "w",
2025 &imsg, &ibuf);
2026 break;
2027 case GOT_IMSG_RAW_DELTA_REQUEST:
2028 if (delta_outfile == NULL) {
2029 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
2030 break;
2032 err = raw_delta_request(&imsg, &ibuf, delta_outfile,
2033 pack, packidx);
2034 break;
2035 case GOT_IMSG_DELTA_REUSE_REQUEST:
2036 err = delta_reuse_request(&imsg, &ibuf, pack, packidx);
2037 break;
2038 case GOT_IMSG_COMMIT_REQUEST:
2039 err = commit_request(&imsg, &ibuf, pack, packidx,
2040 &objcache);
2041 break;
2042 case GOT_IMSG_TREE_REQUEST:
2043 err = tree_request(&imsg, &ibuf, pack, packidx,
2044 &objcache, &entries, &nentries, &nentries_alloc);
2045 break;
2046 case GOT_IMSG_BLOB_REQUEST:
2047 if (basefile == NULL || accumfile == NULL) {
2048 err = got_error(GOT_ERR_PRIVSEP_MSG);
2049 break;
2051 err = blob_request(&imsg, &ibuf, pack, packidx,
2052 &objcache, basefile, accumfile);
2053 break;
2054 case GOT_IMSG_TAG_REQUEST:
2055 err = tag_request(&imsg, &ibuf, pack, packidx,
2056 &objcache);
2057 break;
2058 case GOT_IMSG_COMMIT_TRAVERSAL_REQUEST:
2059 err = commit_traversal_request(&imsg, &ibuf, pack,
2060 packidx, &objcache);
2061 break;
2062 case GOT_IMSG_OBJECT_ENUMERATION_REQUEST:
2063 err = enumeration_request(&imsg, &ibuf, pack,
2064 packidx, &objcache);
2065 break;
2066 case GOT_IMSG_COMMIT_PAINTING_INIT:
2067 commit_painting_free(&keep, &drop, &skip);
2068 err = commit_painting_init(&ibuf, &keep, &drop, &skip);
2069 break;
2070 case GOT_IMSG_COMMIT_PAINTING_REQUEST:
2071 if (keep == NULL || drop == NULL || skip == NULL) {
2072 err = got_error(GOT_ERR_PRIVSEP_MSG);
2073 break;
2075 err = commit_painting_request(&imsg, &ibuf, pack,
2076 packidx, &objcache, keep, drop, skip);
2077 break;
2078 case GOT_IMSG_COMMIT_PAINTING_DONE:
2079 commit_painting_free(&keep, &drop, &skip);
2080 break;
2081 default:
2082 err = got_error(GOT_ERR_PRIVSEP_MSG);
2083 break;
2086 if (imsg.fd != -1 && close(imsg.fd) == -1 && err == NULL)
2087 err = got_error_from_errno("close");
2088 imsg_free(&imsg);
2089 if (err)
2090 break;
2093 free(entries);
2094 commit_painting_free(&keep, &drop, &skip);
2095 if (packidx)
2096 got_packidx_close(packidx);
2097 if (pack)
2098 got_pack_close(pack);
2099 got_object_cache_close(&objcache);
2100 imsg_clear(&ibuf);
2101 if (basefile && fclose(basefile) == EOF && err == NULL)
2102 err = got_error_from_errno("fclose");
2103 if (accumfile && fclose(accumfile) == EOF && err == NULL)
2104 err = got_error_from_errno("fclose");
2105 if (delta_outfile && fclose(delta_outfile) == EOF && err == NULL)
2106 err = got_error_from_errno("fclose");
2107 if (err) {
2108 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
2109 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
2110 got_privsep_send_error(&ibuf, err);
2113 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
2114 err = got_error_from_errno("close");
2115 return err ? 1 : 0;