2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
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.
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.
18 #include <sys/types.h>
19 #include <sys/queue.h>
35 #include "got_error.h"
36 #include "got_object.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 static volatile sig_atomic_t sigint_received;
51 catch_sigint(int signo)
56 static const struct got_error *
57 open_object(struct got_object **obj, struct got_pack *pack,
58 struct got_packidx *packidx, int idx, struct got_object_id *id,
59 struct got_object_cache *objcache)
61 const struct got_error *err;
63 err = got_packfile_open_object(obj, pack, packidx, idx, id);
68 err = got_object_cache_add(objcache, id, *obj);
70 if (err->code == GOT_ERR_OBJ_EXISTS ||
71 err->code == GOT_ERR_OBJ_TOO_LARGE)
79 static const struct got_error *
80 object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
81 struct got_packidx *packidx, struct got_object_cache *objcache)
83 const struct got_error *err = NULL;
84 struct got_imsg_packed_object iobj;
85 struct got_object *obj;
86 struct got_object_id id;
89 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
90 if (datalen != sizeof(iobj))
91 return got_error(GOT_ERR_PRIVSEP_LEN);
92 memcpy(&iobj, imsg->data, sizeof(iobj));
93 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
95 obj = got_object_cache_get(objcache, &id);
99 err = open_object(&obj, pack, packidx, iobj.idx, &id,
105 err = got_privsep_send_obj(ibuf, obj);
107 got_object_close(obj);
111 static const struct got_error *
112 open_commit(struct got_commit_object **commit, struct got_pack *pack,
113 struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
114 struct got_object_cache *objcache)
116 const struct got_error *err = NULL;
117 struct got_object *obj = NULL;
123 obj = got_object_cache_get(objcache, id);
127 err = open_object(&obj, pack, packidx, obj_idx, id,
133 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
139 err = got_object_parse_commit(commit, buf, len);
141 got_object_close(obj);
146 static const struct got_error *
147 commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
148 struct got_packidx *packidx, struct got_object_cache *objcache)
150 const struct got_error *err = NULL;
151 struct got_imsg_packed_object iobj;
152 struct got_commit_object *commit = NULL;
153 struct got_object_id id;
156 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
157 if (datalen != sizeof(iobj))
158 return got_error(GOT_ERR_PRIVSEP_LEN);
159 memcpy(&iobj, imsg->data, sizeof(iobj));
160 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
162 err = open_commit(&commit, pack, packidx, iobj.idx, &id, objcache);
166 err = got_privsep_send_commit(ibuf, commit);
169 got_object_commit_close(commit);
171 if (err->code == GOT_ERR_PRIVSEP_PIPE)
174 got_privsep_send_error(ibuf, err);
180 static const struct got_error *
181 open_tree(uint8_t **buf, struct got_parsed_tree_entry **entries, int *nentries,
182 struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
183 struct got_object_id *id, struct got_object_cache *objcache)
185 const struct got_error *err = NULL;
186 struct got_object *obj = NULL;
192 obj = got_object_cache_get(objcache, id);
196 err = open_object(&obj, pack, packidx, obj_idx, id,
202 err = got_packfile_extract_object_to_mem(buf, &len, obj, pack);
208 err = got_object_parse_tree(entries, nentries, *buf, len);
210 got_object_close(obj);
218 static const struct got_error *
219 tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
220 struct got_packidx *packidx, struct got_object_cache *objcache)
222 const struct got_error *err = NULL;
223 struct got_imsg_packed_object iobj;
224 struct got_parsed_tree_entry *entries = NULL;
227 struct got_object_id id;
230 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
231 if (datalen != sizeof(iobj))
232 return got_error(GOT_ERR_PRIVSEP_LEN);
233 memcpy(&iobj, imsg->data, sizeof(iobj));
234 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
236 err = open_tree(&buf, &entries, &nentries, pack, packidx, iobj.idx,
241 err = got_privsep_send_tree(ibuf, entries, nentries);
245 if (err->code == GOT_ERR_PRIVSEP_PIPE)
248 got_privsep_send_error(ibuf, err);
254 static const struct got_error *
255 receive_file(FILE **f, struct imsgbuf *ibuf, uint32_t imsg_code)
257 const struct got_error *err;
261 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
265 if (imsg.hdr.type != imsg_code) {
266 err = got_error(GOT_ERR_PRIVSEP_MSG);
270 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
272 err = got_error(GOT_ERR_PRIVSEP_LEN);
276 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
280 *f = fdopen(imsg.fd, "w+");
282 err = got_error_from_errno("fdopen");
291 static const struct got_error *
292 receive_tempfile(FILE **f, const char *mode, struct imsg *imsg,
293 struct imsgbuf *ibuf)
297 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
299 return got_error(GOT_ERR_PRIVSEP_LEN);
302 return got_error(GOT_ERR_PRIVSEP_NO_FD);
304 *f = fdopen(imsg->fd, mode);
306 return got_error_from_errno("fdopen");
312 static const struct got_error *
313 blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
314 struct got_packidx *packidx, struct got_object_cache *objcache,
315 FILE *basefile, FILE *accumfile)
317 const struct got_error *err = NULL;
318 struct got_imsg_packed_object iobj;
319 struct got_object *obj = NULL;
320 FILE *outfile = NULL;
321 struct got_object_id id;
326 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
327 if (datalen != sizeof(iobj))
328 return got_error(GOT_ERR_PRIVSEP_LEN);
329 memcpy(&iobj, imsg->data, sizeof(iobj));
330 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
332 obj = got_object_cache_get(objcache, &id);
336 err = open_object(&obj, pack, packidx, iobj.idx, &id,
342 err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
346 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
347 err = got_pack_get_max_delta_object_size(&blob_size, obj, pack);
351 blob_size = obj->size;
353 if (blob_size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
354 err = got_packfile_extract_object_to_mem(&buf, &obj->size,
357 err = got_packfile_extract_object(pack, obj, outfile, basefile,
362 err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, buf);
365 if (outfile && fclose(outfile) == EOF && err == NULL)
366 err = got_error_from_errno("fclose");
367 got_object_close(obj);
368 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
369 got_privsep_send_error(ibuf, err);
374 static const struct got_error *
375 tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
376 struct got_packidx *packidx, struct got_object_cache *objcache)
378 const struct got_error *err = NULL;
379 struct got_imsg_packed_object iobj;
380 struct got_object *obj = NULL;
381 struct got_tag_object *tag = NULL;
384 struct got_object_id id;
387 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
388 if (datalen != sizeof(iobj))
389 return got_error(GOT_ERR_PRIVSEP_LEN);
390 memcpy(&iobj, imsg->data, sizeof(iobj));
391 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
393 obj = got_object_cache_get(objcache, &id);
397 err = open_object(&obj, pack, packidx, iobj.idx, &id,
403 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
408 err = got_object_parse_tag(&tag, buf, len);
412 err = got_privsep_send_tag(ibuf, tag);
415 got_object_close(obj);
417 got_object_tag_close(tag);
419 if (err->code == GOT_ERR_PRIVSEP_PIPE)
422 got_privsep_send_error(ibuf, err);
428 static struct got_parsed_tree_entry *
429 find_entry_by_name(struct got_parsed_tree_entry *entries, int nentries,
430 const char *name, size_t len)
432 struct got_parsed_tree_entry *pte;
435 /* Note that tree entries are sorted in strncmp() order. */
436 for (i = 0; i < nentries; i++) {
438 cmp = strncmp(pte->name, name, len);
443 if (pte->name[len] == '\0')
449 static const struct got_error *
450 tree_path_changed(int *changed, uint8_t **buf1, uint8_t **buf2,
451 struct got_parsed_tree_entry **entries1, int *nentries1,
452 struct got_parsed_tree_entry **entries2, int *nentries2,
453 const char *path, struct got_pack *pack, struct got_packidx *packidx,
454 struct imsgbuf *ibuf, struct got_object_cache *objcache)
456 const struct got_error *err = NULL;
457 struct got_parsed_tree_entry *pte1 = NULL, *pte2 = NULL;
463 /* We not do support comparing the root path. */
464 if (got_path_is_root_dir(path))
465 return got_error_path(path, GOT_ERR_BAD_PATH);
480 pte1 = find_entry_by_name(*entries1, *nentries1, seg, seglen);
482 err = got_error(GOT_ERR_NO_OBJ);
486 pte2 = find_entry_by_name(*entries2, *nentries2, seg, seglen);
492 if (pte1->mode != pte2->mode) {
497 if (memcmp(pte1->id, pte2->id, SHA1_DIGEST_LENGTH) == 0) {
502 if (*s == '\0') { /* final path element */
511 struct got_object_id id1, id2;
514 memcpy(id1.sha1, pte1->id, SHA1_DIGEST_LENGTH);
515 idx = got_packidx_get_object_idx(packidx, &id1);
517 err = got_error_no_obj(&id1);
524 err = open_tree(buf1, entries1, nentries1, pack,
525 packidx, idx, &id1, objcache);
530 memcpy(id2.sha1, pte2->id, SHA1_DIGEST_LENGTH);
531 idx = got_packidx_get_object_idx(packidx, &id2);
533 err = got_error_no_obj(&id2);
540 err = open_tree(buf2, entries2, nentries2, pack,
541 packidx, idx, &id2, objcache);
551 static const struct got_error *
552 send_traversed_commits(struct got_object_id *commit_ids, size_t ncommits,
553 struct imsgbuf *ibuf)
558 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
559 sizeof(struct got_imsg_traversed_commits) +
560 ncommits * SHA1_DIGEST_LENGTH);
562 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
564 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1)
565 return got_error_from_errno("imsg_add TRAVERSED_COMMITS");
567 for (i = 0; i < ncommits; i++) {
568 struct got_object_id *id = &commit_ids[i];
569 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
570 return got_error_from_errno(
571 "imsg_add TRAVERSED_COMMITS");
576 imsg_close(ibuf, wbuf);
578 return got_privsep_flush_imsg(ibuf);
581 static const struct got_error *
582 send_commit_traversal_done(struct imsgbuf *ibuf)
584 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
586 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
588 return got_privsep_flush_imsg(ibuf);
591 static const struct got_error *
592 commit_traversal_request(struct imsg *imsg, struct imsgbuf *ibuf,
593 struct got_pack *pack, struct got_packidx *packidx,
594 struct got_object_cache *objcache)
596 const struct got_error *err = NULL;
597 struct got_imsg_packed_object iobj;
598 struct got_object_qid *pid;
599 struct got_commit_object *commit = NULL, *pcommit = NULL;
600 struct got_parsed_tree_entry *entries = NULL, *pentries = NULL;
601 int nentries = 0, pnentries = 0;
602 struct got_object_id id;
603 size_t datalen, path_len;
605 const int min_alloc = 64;
606 int changed = 0, ncommits = 0, nallocated = 0;
607 struct got_object_id *commit_ids = NULL;
609 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
610 if (datalen < sizeof(iobj))
611 return got_error(GOT_ERR_PRIVSEP_LEN);
612 memcpy(&iobj, imsg->data, sizeof(iobj));
613 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
615 path_len = datalen - sizeof(iobj) - 1;
617 return got_error(GOT_ERR_PRIVSEP_LEN);
619 path = imsg->data + sizeof(iobj);
620 if (path[path_len] != '\0')
621 return got_error(GOT_ERR_PRIVSEP_LEN);
624 nallocated = min_alloc;
625 commit_ids = reallocarray(NULL, nallocated, sizeof(*commit_ids));
626 if (commit_ids == NULL)
627 return got_error_from_errno("reallocarray");
630 const size_t max_datalen = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
633 if (sigint_received) {
634 err = got_error(GOT_ERR_CANCELLED);
638 if (commit == NULL) {
639 idx = got_packidx_get_object_idx(packidx, &id);
642 err = open_commit(&commit, pack, packidx,
645 if (err->code != GOT_ERR_NO_OBJ)
652 if (sizeof(struct got_imsg_traversed_commits) +
653 ncommits * SHA1_DIGEST_LENGTH >= max_datalen) {
654 err = send_traversed_commits(commit_ids, ncommits,
661 if (ncommits > nallocated) {
662 struct got_object_id *new;
663 nallocated += min_alloc;
664 new = reallocarray(commit_ids, nallocated,
665 sizeof(*commit_ids));
667 err = got_error_from_errno("reallocarray");
672 memcpy(commit_ids[ncommits - 1].sha1, id.sha1,
675 pid = STAILQ_FIRST(&commit->parent_ids);
679 idx = got_packidx_get_object_idx(packidx, &pid->id);
683 err = open_commit(&pcommit, pack, packidx, idx, &pid->id,
686 if (err->code != GOT_ERR_NO_OBJ)
692 if (path[0] == '/' && path[1] == '\0') {
693 if (got_object_id_cmp(pcommit->tree_id,
694 commit->tree_id) != 0) {
700 uint8_t *buf = NULL, *pbuf = NULL;
702 idx = got_packidx_get_object_idx(packidx,
706 pidx = got_packidx_get_object_idx(packidx,
711 err = open_tree(&buf, &entries, &nentries, pack,
712 packidx, idx, commit->tree_id, objcache);
715 err = open_tree(&pbuf, &pentries, &pnentries, pack,
716 packidx, pidx, pcommit->tree_id, objcache);
722 err = tree_path_changed(&changed, &buf, &pbuf,
723 &entries, &nentries, &pentries, &pnentries, path,
724 pack, packidx, ibuf, objcache);
735 if (err->code != GOT_ERR_NO_OBJ)
743 memcpy(id.sha1, pid->id.sha1, SHA1_DIGEST_LENGTH);
744 got_object_commit_close(commit);
751 err = send_traversed_commits(commit_ids, ncommits, ibuf);
756 err = got_privsep_send_commit(ibuf, commit);
761 err = send_commit_traversal_done(ibuf);
765 got_object_commit_close(commit);
767 got_object_commit_close(pcommit);
771 if (err->code == GOT_ERR_PRIVSEP_PIPE)
774 got_privsep_send_error(ibuf, err);
780 static const struct got_error *
781 raw_object_request(struct imsg *imsg, struct imsgbuf *ibuf,
782 struct got_pack *pack, struct got_packidx *packidx,
783 struct got_object_cache *objcache, FILE *basefile, FILE *accumfile)
785 const struct got_error *err = NULL;
788 FILE *outfile = NULL;
789 struct got_imsg_packed_object iobj;
790 struct got_object *obj;
791 struct got_object_id id;
794 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
795 if (datalen != sizeof(iobj))
796 return got_error(GOT_ERR_PRIVSEP_LEN);
797 memcpy(&iobj, imsg->data, sizeof(iobj));
798 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
800 obj = got_object_cache_get(objcache, &id);
804 err = open_object(&obj, pack, packidx, iobj.idx, &id,
810 err = receive_file(&outfile, ibuf, GOT_IMSG_RAW_OBJECT_OUTFD);
814 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
815 err = got_pack_get_max_delta_object_size(&size, obj, pack);
821 if (size <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
822 err = got_packfile_extract_object_to_mem(&buf, &obj->size,
825 err = got_packfile_extract_object(pack, obj, outfile, basefile,
830 err = got_privsep_send_raw_obj(ibuf, obj->size, obj->hdrlen, buf);
833 if (outfile && fclose(outfile) == EOF && err == NULL)
834 err = got_error_from_errno("fclose");
835 got_object_close(obj);
836 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
837 got_privsep_send_error(ibuf, err);
842 static const struct got_error *
843 get_base_object_id(struct got_object_id *base_id, struct got_packidx *packidx,
846 const struct got_error *err;
849 err = got_packidx_get_offset_idx(&idx, packidx, base_offset);
853 return got_error(GOT_ERR_BAD_PACKIDX);
855 return got_packidx_get_object_id(base_id, packidx, idx);
858 static const struct got_error *
859 raw_delta_request(struct imsg *imsg, struct imsgbuf *ibuf,
860 FILE *delta_outfile, struct got_pack *pack,
861 struct got_packidx *packidx)
863 const struct got_error *err = NULL;
864 struct got_imsg_raw_delta_request req;
865 size_t datalen, delta_size, delta_compressed_size;
867 uint8_t *delta_buf = NULL;
868 struct got_object_id id, base_id;
869 off_t base_offset, delta_out_offset = 0;
870 uint64_t base_size = 0, result_size = 0;
873 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
874 if (datalen != sizeof(req))
875 return got_error(GOT_ERR_PRIVSEP_LEN);
876 memcpy(&req, imsg->data, sizeof(req));
877 memcpy(id.sha1, req.id, SHA1_DIGEST_LENGTH);
881 err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
882 &delta_compressed_size, &delta_offset, &base_offset, &base_id,
883 &base_size, &result_size, pack, packidx, req.idx);
888 * If this is an offset delta we must determine the base
889 * object ID ourselves.
891 if (base_offset != 0) {
892 err = get_base_object_id(&base_id, packidx, base_offset);
897 delta_out_offset = ftello(delta_outfile);
898 w = fwrite(delta_buf, 1, delta_compressed_size, delta_outfile);
899 if (w != delta_compressed_size) {
900 err = got_ferror(delta_outfile, GOT_ERR_IO);
903 if (fflush(delta_outfile) == -1) {
904 err = got_error_from_errno("fflush");
908 err = got_privsep_send_raw_delta(ibuf, base_size, result_size,
909 delta_size, delta_compressed_size, delta_offset, delta_out_offset,
916 struct search_deltas_arg {
917 struct imsgbuf *ibuf;
918 struct got_packidx *packidx;
919 struct got_pack *pack;
920 struct got_object_idset *idset;
922 struct got_imsg_reused_delta deltas[GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS];
926 static const struct got_error *
927 search_delta_for_object(struct got_object_id *id, void *data, void *arg)
929 const struct got_error *err;
930 struct search_deltas_arg *a = arg;
932 uint8_t *delta_buf = NULL;
933 uint64_t base_size, result_size;
934 size_t delta_size, delta_compressed_size;
935 off_t delta_offset, base_offset;
936 struct got_object_id base_id;
939 return got_error(GOT_ERR_CANCELLED);
941 obj_idx = got_packidx_get_object_idx(a->packidx, id);
943 return NULL; /* object not present in our pack file */
945 err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
946 &delta_compressed_size, &delta_offset, &base_offset, &base_id,
947 &base_size, &result_size, a->pack, a->packidx, obj_idx);
949 if (err->code == GOT_ERR_OBJ_TYPE)
950 return NULL; /* object not stored as a delta */
955 * If this is an offset delta we must determine the base
956 * object ID ourselves.
958 if (base_offset != 0) {
959 err = get_base_object_id(&base_id, a->packidx, base_offset);
964 if (got_object_idset_contains(a->idset, &base_id)) {
965 struct got_imsg_reused_delta *delta;
966 off_t delta_out_offset = ftello(a->delta_outfile);
969 w = fwrite(delta_buf, 1, delta_compressed_size,
971 if (w != delta_compressed_size) {
972 err = got_ferror(a->delta_outfile, GOT_ERR_IO);
976 delta = &a->deltas[a->ndeltas++];
977 memcpy(&delta->id, id, sizeof(delta->id));
978 memcpy(&delta->base_id, &base_id, sizeof(delta->base_id));
979 delta->base_size = base_size;
980 delta->result_size = result_size;
981 delta->delta_size = delta_size;
982 delta->delta_compressed_size = delta_compressed_size;
983 delta->delta_offset = delta_offset;
984 delta->delta_out_offset = delta_out_offset;
986 if (a->ndeltas >= GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS) {
987 err = got_privsep_send_reused_deltas(a->ibuf,
988 a->deltas, a->ndeltas);
999 static const struct got_error *
1000 recv_object_ids(struct got_object_idset *idset, struct imsgbuf *ibuf)
1002 const struct got_error *err = NULL;
1004 struct got_object_id *ids;
1008 err = got_privsep_recv_object_idlist(&done, &ids, &nids, ibuf);
1011 for (i = 0; i < nids; i++) {
1012 err = got_object_idset_add(idset, &ids[i], NULL);
1024 static const struct got_error *
1025 recv_object_id_queue(struct got_object_id_queue *queue, struct imsgbuf *ibuf)
1027 const struct got_error *err = NULL;
1029 struct got_object_qid *qid;
1030 struct got_object_id *ids;
1034 err = got_privsep_recv_object_idlist(&done, &ids, &nids, ibuf);
1037 for (i = 0; i < nids; i++) {
1038 err = got_object_qid_alloc_partial(&qid);
1041 memcpy(&qid->id, &ids[i], sizeof(qid->id));
1042 STAILQ_INSERT_TAIL(queue, qid, entry);
1049 static const struct got_error *
1050 delta_reuse_request(struct imsg *imsg, struct imsgbuf *ibuf,
1051 FILE *delta_outfile, struct got_pack *pack, struct got_packidx *packidx)
1053 const struct got_error *err = NULL;
1054 struct got_object_idset *idset;
1055 struct search_deltas_arg sda;
1057 idset = got_object_idset_alloc();
1059 return got_error_from_errno("got_object_idset_alloc");
1061 err = recv_object_ids(idset, ibuf);
1065 memset(&sda, 0, sizeof(sda));
1069 sda.packidx = packidx;
1070 sda.delta_outfile = delta_outfile;
1071 err = got_object_idset_for_each(idset, search_delta_for_object, &sda);
1075 if (sda.ndeltas > 0) {
1076 err = got_privsep_send_reused_deltas(ibuf, sda.deltas,
1082 if (fflush(delta_outfile) == -1) {
1083 err = got_error_from_errno("fflush");
1087 err = got_privsep_send_reused_deltas_done(ibuf);
1089 got_object_idset_free(idset);
1093 static const struct got_error *
1094 receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
1096 const struct got_error *err = NULL;
1098 struct got_imsg_packidx ipackidx;
1100 struct got_packidx *p;
1104 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1108 p = calloc(1, sizeof(*p));
1110 err = got_error_from_errno("calloc");
1114 if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
1115 err = got_error(GOT_ERR_PRIVSEP_MSG);
1119 if (imsg.fd == -1) {
1120 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1124 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1125 if (datalen != sizeof(ipackidx)) {
1126 err = got_error(GOT_ERR_PRIVSEP_LEN);
1129 memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
1131 p->len = ipackidx.len;
1132 p->fd = dup(imsg.fd);
1134 err = got_error_from_errno("dup");
1137 if (lseek(p->fd, 0, SEEK_SET) == -1) {
1138 err = got_error_from_errno("lseek");
1142 #ifndef GOT_PACK_NO_MMAP
1143 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
1144 if (p->map == MAP_FAILED)
1145 p->map = NULL; /* fall back to read(2) */
1147 err = got_packidx_init_hdr(p, 1, ipackidx.packfile_size);
1152 got_packidx_close(p);
1159 static const struct got_error *
1160 send_tree_enumeration_done(struct imsgbuf *ibuf)
1162 if (imsg_compose(ibuf, GOT_IMSG_TREE_ENUMERATION_DONE, 0, 0, -1,
1164 return got_error_from_errno("imsg_compose TREE_ENUMERATION_DONE");
1166 return got_privsep_flush_imsg(ibuf);
1169 static const struct got_error *
1170 enumerate_tree(struct imsgbuf *ibuf, size_t *totlen,
1171 struct got_object_id *tree_id,
1172 const char *path, struct got_pack *pack, struct got_packidx *packidx,
1173 struct got_object_cache *objcache, struct got_object_idset *idset)
1175 const struct got_error *err = NULL;
1176 struct got_object_id_queue ids;
1177 struct got_object_qid *qid;
1178 uint8_t *buf = NULL;
1179 struct got_parsed_tree_entry *entries = NULL;
1183 err = got_object_qid_alloc_partial(&qid);
1186 memcpy(&qid->id.sha1, tree_id, SHA1_DIGEST_LENGTH);
1187 qid->data = strdup(path);
1188 if (qid->data == NULL) {
1189 err = got_error_from_errno("strdup");
1192 STAILQ_INSERT_TAIL(&ids, qid, entry);
1197 int idx, nentries, i;
1199 if (sigint_received) {
1200 err = got_error(GOT_ERR_CANCELLED);
1204 qid = STAILQ_FIRST(&ids);
1205 STAILQ_REMOVE_HEAD(&ids, entry);
1208 idx = got_packidx_get_object_idx(packidx, &qid->id);
1210 err = got_privsep_send_enumerated_tree(totlen, ibuf,
1211 &qid->id, path, NULL, -1);
1215 err = open_tree(&buf, &entries, &nentries,
1216 pack, packidx, idx, &qid->id, objcache);
1218 if (err->code != GOT_ERR_NO_OBJ)
1222 err = got_privsep_send_enumerated_tree(totlen,
1223 ibuf, &qid->id, path, entries, nentries);
1227 err = got_object_idset_add(idset, &qid->id, NULL);
1231 for (i = 0; i < nentries; i++) {
1232 struct got_object_qid *eqid = NULL;
1233 struct got_parsed_tree_entry *pte = &entries[i];
1236 if (!S_ISDIR(pte->mode))
1239 err = got_object_qid_alloc_partial(&eqid);
1242 memcpy(eqid->id.sha1, pte->id, sizeof(eqid->id.sha1));
1244 if (got_object_idset_contains(idset, &eqid->id)) {
1245 got_object_qid_free(eqid);
1249 if (asprintf(&p, "%s%s%s", path,
1250 got_path_is_root_dir(path) ? "" : "/",
1252 err = got_error_from_errno("asprintf");
1253 got_object_qid_free(eqid);
1257 STAILQ_INSERT_TAIL(&ids, eqid, entry);
1258 idx = got_packidx_get_object_idx(packidx, &eqid->id);
1264 got_object_qid_free(qid);
1271 } while (!STAILQ_EMPTY(&ids));
1273 err = send_tree_enumeration_done(ibuf);
1278 got_object_qid_free(qid);
1279 got_object_id_queue_free(&ids);
1282 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1285 got_privsep_send_error(ibuf, err);
1291 static const struct got_error *
1292 enumeration_request(struct imsg *imsg, struct imsgbuf *ibuf,
1293 struct got_pack *pack, struct got_packidx *packidx,
1294 struct got_object_cache *objcache)
1296 const struct got_error *err = NULL;
1297 struct got_object_id_queue commit_ids;
1298 const struct got_object_id_queue *parents = NULL;
1299 struct got_object_qid *qid = NULL;
1300 struct got_object *obj = NULL;
1301 struct got_commit_object *commit = NULL;
1302 struct got_object_id *tree_id = NULL;
1304 struct got_object_idset *idset;
1307 STAILQ_INIT(&commit_ids);
1309 idset = got_object_idset_alloc();
1311 return got_error_from_errno("got_object_idset_alloc");
1313 err = recv_object_id_queue(&commit_ids, ibuf);
1317 err = recv_object_ids(idset, ibuf);
1321 while (!STAILQ_EMPTY(&commit_ids)) {
1322 if (sigint_received) {
1323 err = got_error(GOT_ERR_CANCELLED);
1327 qid = STAILQ_FIRST(&commit_ids);
1328 STAILQ_REMOVE_HEAD(&commit_ids, entry);
1330 if (got_object_idset_contains(idset, &qid->id)) {
1331 got_object_qid_free(qid);
1336 idx = got_packidx_get_object_idx(packidx, &qid->id);
1340 err = open_object(&obj, pack, packidx, idx, &qid->id,
1344 if (obj->type == GOT_OBJ_TYPE_TAG) {
1345 struct got_tag_object *tag;
1348 err = got_packfile_extract_object_to_mem(&buf,
1353 err = got_object_parse_tag(&tag, buf, len);
1358 err = open_commit(&commit, pack, packidx, idx,
1359 &tag->id, objcache);
1360 got_object_tag_close(tag);
1364 } else if (obj->type == GOT_OBJ_TYPE_COMMIT) {
1365 err = open_commit(&commit, pack, packidx, idx,
1366 &qid->id, objcache);
1370 err = got_error(GOT_ERR_OBJ_TYPE);
1373 got_object_close(obj);
1376 err = got_privsep_send_enumerated_commit(ibuf, &qid->id,
1377 got_object_commit_get_committer_time(commit));
1381 tree_id = got_object_commit_get_tree_id(commit);
1382 idx = got_packidx_get_object_idx(packidx, tree_id);
1384 err = got_privsep_send_enumerated_tree(&totlen, ibuf,
1385 tree_id, "", NULL, -1);
1391 if (got_object_idset_contains(idset, tree_id)) {
1392 got_object_qid_free(qid);
1397 err = enumerate_tree(ibuf, &totlen, tree_id, "/",
1398 pack, packidx, objcache, idset);
1402 got_object_qid_free(qid);
1405 parents = got_object_commit_get_parent_ids(commit);
1407 struct got_object_qid *pid;
1408 STAILQ_FOREACH(pid, parents, entry) {
1409 if (got_object_idset_contains(idset, &pid->id))
1411 err = got_object_qid_alloc_partial(&qid);
1414 memcpy(&qid->id, &pid->id, sizeof(qid->id));
1415 STAILQ_INSERT_TAIL(&commit_ids, qid, entry);
1420 got_object_commit_close(commit);
1424 err = got_privsep_send_object_enumeration_done(ibuf);
1429 got_object_close(obj);
1431 got_object_commit_close(commit);
1432 got_object_qid_free(qid);
1433 got_object_id_queue_free(&commit_ids);
1434 got_object_idset_free(idset);
1438 static const struct got_error *
1439 receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
1441 const struct got_error *err = NULL;
1443 struct got_imsg_pack ipack;
1445 struct got_pack *pack;
1449 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1453 pack = calloc(1, sizeof(*pack));
1455 err = got_error_from_errno("calloc");
1459 if (imsg.hdr.type != GOT_IMSG_PACK) {
1460 err = got_error(GOT_ERR_PRIVSEP_MSG);
1464 if (imsg.fd == -1) {
1465 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1469 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1470 if (datalen != sizeof(ipack)) {
1471 err = got_error(GOT_ERR_PRIVSEP_LEN);
1474 memcpy(&ipack, imsg.data, sizeof(ipack));
1476 pack->filesize = ipack.filesize;
1477 pack->fd = dup(imsg.fd);
1478 if (pack->fd == -1) {
1479 err = got_error_from_errno("dup");
1482 if (lseek(pack->fd, 0, SEEK_SET) == -1) {
1483 err = got_error_from_errno("lseek");
1486 pack->path_packfile = strdup(ipack.path_packfile);
1487 if (pack->path_packfile == NULL) {
1488 err = got_error_from_errno("strdup");
1492 err = got_delta_cache_alloc(&pack->delta_cache);
1496 #ifndef GOT_PACK_NO_MMAP
1497 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1499 if (pack->map == MAP_FAILED)
1500 pack->map = NULL; /* fall back to read(2) */
1514 main(int argc, char *argv[])
1516 const struct got_error *err = NULL;
1517 struct imsgbuf ibuf;
1519 struct got_packidx *packidx = NULL;
1520 struct got_pack *pack = NULL;
1521 struct got_object_cache objcache;
1522 FILE *basefile = NULL, *accumfile = NULL, *delta_outfile = NULL;
1524 //static int attached;
1525 //while (!attached) sleep(1);
1527 signal(SIGINT, catch_sigint);
1529 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
1531 err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
1533 err = got_error_from_errno("got_object_cache_init");
1534 got_privsep_send_error(&ibuf, err);
1539 /* revoke access to most system calls */
1540 if (pledge("stdio recvfd", NULL) == -1) {
1541 err = got_error_from_errno("pledge");
1542 got_privsep_send_error(&ibuf, err);
1547 err = receive_packidx(&packidx, &ibuf);
1549 got_privsep_send_error(&ibuf, err);
1553 err = receive_pack(&pack, &ibuf);
1555 got_privsep_send_error(&ibuf, err);
1562 if (sigint_received) {
1563 err = got_error(GOT_ERR_CANCELLED);
1567 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1569 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1574 if (imsg.hdr.type == GOT_IMSG_STOP)
1577 switch (imsg.hdr.type) {
1578 case GOT_IMSG_TMPFD:
1579 if (basefile == NULL) {
1580 err = receive_tempfile(&basefile, "w+",
1582 } else if (accumfile == NULL) {
1583 err = receive_tempfile(&accumfile, "w+",
1586 err = got_error(GOT_ERR_PRIVSEP_MSG);
1588 case GOT_IMSG_PACKED_OBJECT_REQUEST:
1589 err = object_request(&imsg, &ibuf, pack, packidx,
1592 case GOT_IMSG_PACKED_RAW_OBJECT_REQUEST:
1593 if (basefile == NULL || accumfile == NULL) {
1594 err = got_error(GOT_ERR_PRIVSEP_MSG);
1597 err = raw_object_request(&imsg, &ibuf, pack, packidx,
1598 &objcache, basefile, accumfile);
1600 case GOT_IMSG_RAW_DELTA_OUTFD:
1601 if (delta_outfile != NULL) {
1602 err = got_error(GOT_ERR_PRIVSEP_MSG);
1605 err = receive_tempfile(&delta_outfile, "w",
1608 case GOT_IMSG_RAW_DELTA_REQUEST:
1609 if (delta_outfile == NULL) {
1610 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1613 err = raw_delta_request(&imsg, &ibuf, delta_outfile,
1616 case GOT_IMSG_DELTA_REUSE_REQUEST:
1617 if (delta_outfile == NULL) {
1618 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1621 err = delta_reuse_request(&imsg, &ibuf,
1622 delta_outfile, pack, packidx);
1624 case GOT_IMSG_COMMIT_REQUEST:
1625 err = commit_request(&imsg, &ibuf, pack, packidx,
1628 case GOT_IMSG_TREE_REQUEST:
1629 err = tree_request(&imsg, &ibuf, pack, packidx,
1632 case GOT_IMSG_BLOB_REQUEST:
1633 if (basefile == NULL || accumfile == NULL) {
1634 err = got_error(GOT_ERR_PRIVSEP_MSG);
1637 err = blob_request(&imsg, &ibuf, pack, packidx,
1638 &objcache, basefile, accumfile);
1640 case GOT_IMSG_TAG_REQUEST:
1641 err = tag_request(&imsg, &ibuf, pack, packidx,
1644 case GOT_IMSG_COMMIT_TRAVERSAL_REQUEST:
1645 err = commit_traversal_request(&imsg, &ibuf, pack,
1646 packidx, &objcache);
1648 case GOT_IMSG_OBJECT_ENUMERATION_REQUEST:
1649 err = enumeration_request(&imsg, &ibuf, pack,
1650 packidx, &objcache);
1653 err = got_error(GOT_ERR_PRIVSEP_MSG);
1657 if (imsg.fd != -1 && close(imsg.fd) == -1 && err == NULL)
1658 err = got_error_from_errno("close");
1665 got_packidx_close(packidx);
1667 got_pack_close(pack);
1668 got_object_cache_close(&objcache);
1670 if (basefile && fclose(basefile) == EOF && err == NULL)
1671 err = got_error_from_errno("fclose");
1672 if (accumfile && fclose(accumfile) == EOF && err == NULL)
1673 err = got_error_from_errno("fclose");
1674 if (delta_outfile && fclose(delta_outfile) == EOF && err == NULL)
1675 err = got_error_from_errno("fclose");
1677 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
1678 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1679 got_privsep_send_error(&ibuf, err);
1682 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
1683 err = got_error_from_errno("close");