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.
17 #include <sys/types.h>
18 #include <sys/queue.h>
21 #include <sys/syslimits.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_privsep.h"
45 #include "got_lib_pack.h"
47 static volatile sig_atomic_t sigint_received;
50 catch_sigint(int signo)
55 static const struct got_error *
56 open_object(struct got_object **obj, struct got_pack *pack,
57 struct got_packidx *packidx, int idx, struct got_object_id *id,
58 struct got_object_cache *objcache)
60 const struct got_error *err;
62 err = got_packfile_open_object(obj, pack, packidx, idx, id);
67 err = got_object_cache_add(objcache, id, *obj);
69 if (err->code == GOT_ERR_OBJ_EXISTS ||
70 err->code == GOT_ERR_OBJ_TOO_LARGE)
78 static const struct got_error *
79 object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
80 struct got_packidx *packidx, struct got_object_cache *objcache)
82 const struct got_error *err = NULL;
83 struct got_imsg_packed_object iobj;
84 struct got_object *obj;
85 struct got_object_id id;
88 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
89 if (datalen != sizeof(iobj))
90 return got_error(GOT_ERR_PRIVSEP_LEN);
91 memcpy(&iobj, imsg->data, sizeof(iobj));
92 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
94 obj = got_object_cache_get(objcache, &id);
98 err = open_object(&obj, pack, packidx, iobj.idx, &id,
104 err = got_privsep_send_obj(ibuf, obj);
106 got_object_close(obj);
110 const struct got_error *
111 open_commit(struct got_commit_object **commit, struct got_pack *pack,
112 struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
113 struct got_object_cache *objcache)
115 const struct got_error *err = NULL;
116 struct got_object *obj = NULL;
122 obj = got_object_cache_get(objcache, id);
126 err = open_object(&obj, pack, packidx, obj_idx, id,
132 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
138 err = got_object_parse_commit(commit, buf, len);
140 got_object_close(obj);
145 static const struct got_error *
146 commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
147 struct got_packidx *packidx, struct got_object_cache *objcache)
149 const struct got_error *err = NULL;
150 struct got_imsg_packed_object iobj;
151 struct got_commit_object *commit = NULL;
152 struct got_object_id id;
155 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
156 if (datalen != sizeof(iobj))
157 return got_error(GOT_ERR_PRIVSEP_LEN);
158 memcpy(&iobj, imsg->data, sizeof(iobj));
159 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
161 err = open_commit(&commit, pack, packidx, iobj.idx, &id, objcache);
165 err = got_privsep_send_commit(ibuf, commit);
168 got_object_commit_close(commit);
170 if (err->code == GOT_ERR_PRIVSEP_PIPE)
173 got_privsep_send_error(ibuf, err);
179 const struct got_error *
180 open_tree(uint8_t **buf, struct got_pathlist_head *entries, int *nentries,
181 struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
182 struct got_object_id *id, struct got_object_cache *objcache)
184 const struct got_error *err = NULL;
185 struct got_object *obj = NULL;
191 obj = got_object_cache_get(objcache, id);
195 err = open_object(&obj, pack, packidx, obj_idx, id,
201 err = got_packfile_extract_object_to_mem(buf, &len, obj, pack);
207 err = got_object_parse_tree(entries, nentries, *buf, len);
209 got_object_close(obj);
217 static const struct got_error *
218 tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
219 struct got_packidx *packidx, struct got_object_cache *objcache)
221 const struct got_error *err = NULL;
222 struct got_imsg_packed_object iobj;
223 struct got_pathlist_head entries;
226 struct got_object_id id;
229 TAILQ_INIT(&entries);
231 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
232 if (datalen != sizeof(iobj))
233 return got_error(GOT_ERR_PRIVSEP_LEN);
234 memcpy(&iobj, imsg->data, sizeof(iobj));
235 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
237 err = open_tree(&buf, &entries, &nentries, pack, packidx, iobj.idx,
242 err = got_privsep_send_tree(ibuf, &entries, nentries);
243 got_object_parsed_tree_entries_free(&entries);
246 if (err->code == GOT_ERR_PRIVSEP_PIPE)
249 got_privsep_send_error(ibuf, err);
255 static const struct got_error *
256 receive_file(FILE **f, struct imsgbuf *ibuf, int imsg_code)
258 const struct got_error *err;
262 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
266 if (imsg.hdr.type != imsg_code) {
267 err = got_error(GOT_ERR_PRIVSEP_MSG);
271 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
273 err = got_error(GOT_ERR_PRIVSEP_LEN);
277 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
281 *f = fdopen(imsg.fd, "w+");
283 err = got_error_from_errno("fdopen");
292 static const struct got_error *
293 blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
294 struct got_packidx *packidx, struct got_object_cache *objcache)
296 const struct got_error *err = NULL;
297 struct got_imsg_packed_object iobj;
298 struct got_object *obj = NULL;
299 FILE *outfile = NULL, *basefile = NULL, *accumfile = NULL;
300 struct got_object_id id;
305 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
306 if (datalen != sizeof(iobj))
307 return got_error(GOT_ERR_PRIVSEP_LEN);
308 memcpy(&iobj, imsg->data, sizeof(iobj));
309 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
311 obj = got_object_cache_get(objcache, &id);
315 err = open_object(&obj, pack, packidx, iobj.idx, &id,
321 err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
324 err = receive_file(&basefile, ibuf, GOT_IMSG_TMPFD);
327 err = receive_file(&accumfile, ibuf, GOT_IMSG_TMPFD);
331 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
332 err = got_pack_get_max_delta_object_size(&blob_size, obj, pack);
336 blob_size = obj->size;
338 if (blob_size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
339 err = got_packfile_extract_object_to_mem(&buf, &obj->size,
342 err = got_packfile_extract_object(pack, obj, outfile, basefile,
347 err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, buf);
350 if (outfile && fclose(outfile) != 0 && err == NULL)
351 err = got_error_from_errno("fclose");
352 if (basefile && fclose(basefile) != 0 && err == NULL)
353 err = got_error_from_errno("fclose");
354 if (accumfile && fclose(accumfile) != 0 && err == NULL)
355 err = got_error_from_errno("fclose");
356 got_object_close(obj);
357 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
358 got_privsep_send_error(ibuf, err);
363 static const struct got_error *
364 tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
365 struct got_packidx *packidx, struct got_object_cache *objcache)
367 const struct got_error *err = NULL;
368 struct got_imsg_packed_object iobj;
369 struct got_object *obj = NULL;
370 struct got_tag_object *tag = NULL;
373 struct got_object_id id;
376 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
377 if (datalen != sizeof(iobj))
378 return got_error(GOT_ERR_PRIVSEP_LEN);
379 memcpy(&iobj, imsg->data, sizeof(iobj));
380 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
382 obj = got_object_cache_get(objcache, &id);
386 err = open_object(&obj, pack, packidx, iobj.idx, &id,
392 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
397 err = got_object_parse_tag(&tag, buf, len);
401 err = got_privsep_send_tag(ibuf, tag);
404 got_object_close(obj);
406 got_object_tag_close(tag);
408 if (err->code == GOT_ERR_PRIVSEP_PIPE)
411 got_privsep_send_error(ibuf, err);
417 static struct got_parsed_tree_entry *
418 find_entry_by_name(struct got_pathlist_head *entries, int nentries,
419 const char *name, size_t len)
421 struct got_pathlist_entry *pe;
423 /* Note that tree entries are sorted in strncmp() order. */
424 TAILQ_FOREACH(pe, entries, entry) {
425 int cmp = strncmp(pe->path, name, len);
430 if (pe->path[len] == '\0')
431 return (struct got_parsed_tree_entry *)pe->data;
436 const struct got_error *
437 tree_path_changed(int *changed, uint8_t **buf1, uint8_t **buf2,
438 struct got_pathlist_head *entries1, int *nentries1,
439 struct got_pathlist_head *entries2, int *nentries2,
440 const char *path, struct got_pack *pack, struct got_packidx *packidx,
441 struct imsgbuf *ibuf, struct got_object_cache *objcache)
443 const struct got_error *err = NULL;
444 struct got_parsed_tree_entry *pte1 = NULL, *pte2 = NULL;
450 /* We not do support comparing the root path. */
451 if (got_path_is_root_dir(path))
452 return got_error_path(path, GOT_ERR_BAD_PATH);
467 pte1 = find_entry_by_name(entries1, *nentries1, seg, seglen);
469 err = got_error(GOT_ERR_NO_OBJ);
473 pte2 = find_entry_by_name(entries2, *nentries2, seg, seglen);
479 if (pte1->mode != pte2->mode) {
484 if (memcmp(pte1->id, pte2->id, SHA1_DIGEST_LENGTH) == 0) {
489 if (*s == '\0') { /* final path element */
498 struct got_object_id id1, id2;
501 memcpy(id1.sha1, pte1->id, SHA1_DIGEST_LENGTH);
502 idx = got_packidx_get_object_idx(packidx, &id1);
504 err = got_error_no_obj(&id1);
507 got_object_parsed_tree_entries_free(entries1);
511 err = open_tree(buf1, entries1, nentries1, pack,
512 packidx, idx, &id1, objcache);
517 memcpy(id2.sha1, pte2->id, SHA1_DIGEST_LENGTH);
518 idx = got_packidx_get_object_idx(packidx, &id2);
520 err = got_error_no_obj(&id2);
523 got_object_parsed_tree_entries_free(entries2);
527 err = open_tree(buf2, entries2, nentries2, pack,
528 packidx, idx, &id2, objcache);
538 static const struct got_error *
539 send_traversed_commits(struct got_object_id *commit_ids, size_t ncommits,
540 struct imsgbuf *ibuf)
542 const struct got_error *err;
546 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
547 sizeof(struct got_imsg_traversed_commits) +
548 ncommits * SHA1_DIGEST_LENGTH);
550 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
552 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
553 err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
557 for (i = 0; i < ncommits; i++) {
558 struct got_object_id *id = &commit_ids[i];
559 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
560 err = got_error_from_errno(
561 "imsg_add TRAVERSED_COMMITS");
568 imsg_close(ibuf, wbuf);
570 return got_privsep_flush_imsg(ibuf);
573 static const struct got_error *
574 send_commit_traversal_done(struct imsgbuf *ibuf)
576 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
578 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
580 return got_privsep_flush_imsg(ibuf);
584 static const struct got_error *
585 commit_traversal_request(struct imsg *imsg, struct imsgbuf *ibuf,
586 struct got_pack *pack, struct got_packidx *packidx,
587 struct got_object_cache *objcache)
589 const struct got_error *err = NULL;
590 struct got_imsg_packed_object iobj;
591 struct got_object_qid *pid;
592 struct got_commit_object *commit = NULL, *pcommit = NULL;
593 struct got_pathlist_head entries, pentries;
594 int nentries = 0, pnentries = 0;
595 struct got_object_id id;
596 size_t datalen, path_len;
598 const int min_alloc = 64;
599 int changed = 0, ncommits = 0, nallocated = 0;
600 struct got_object_id *commit_ids = NULL;
602 TAILQ_INIT(&entries);
603 TAILQ_INIT(&pentries);
605 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
606 if (datalen < sizeof(iobj))
607 return got_error(GOT_ERR_PRIVSEP_LEN);
608 memcpy(&iobj, imsg->data, sizeof(iobj));
609 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
611 path_len = datalen - sizeof(iobj) - 1;
613 return got_error(GOT_ERR_PRIVSEP_LEN);
615 path = imsg->data + sizeof(iobj);
616 if (path[path_len] != '\0')
617 return got_error(GOT_ERR_PRIVSEP_LEN);
620 nallocated = min_alloc;
621 commit_ids = reallocarray(NULL, nallocated, sizeof(*commit_ids));
622 if (commit_ids == NULL)
623 return got_error_from_errno("reallocarray");
626 const size_t max_datalen = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
629 if (sigint_received) {
630 err = got_error(GOT_ERR_CANCELLED);
634 if (commit == NULL) {
635 idx = got_packidx_get_object_idx(packidx, &id);
638 err = open_commit(&commit, pack, packidx,
641 if (err->code != GOT_ERR_NO_OBJ)
648 if (sizeof(struct got_imsg_traversed_commits) +
649 ncommits * SHA1_DIGEST_LENGTH >= max_datalen) {
650 err = send_traversed_commits(commit_ids, ncommits,
657 if (ncommits > nallocated) {
658 struct got_object_id *new;
659 nallocated += min_alloc;
660 new = reallocarray(commit_ids, nallocated,
661 sizeof(*commit_ids));
663 err = got_error_from_errno("reallocarray");
668 memcpy(commit_ids[ncommits - 1].sha1, id.sha1,
671 pid = SIMPLEQ_FIRST(&commit->parent_ids);
675 idx = got_packidx_get_object_idx(packidx, pid->id);
679 err = open_commit(&pcommit, pack, packidx, idx, pid->id,
682 if (err->code != GOT_ERR_NO_OBJ)
688 if (path[0] == '/' && path[1] == '\0') {
689 if (got_object_id_cmp(pcommit->tree_id,
690 commit->tree_id) != 0) {
696 uint8_t *buf = NULL, *pbuf = NULL;
698 idx = got_packidx_get_object_idx(packidx,
702 pidx = got_packidx_get_object_idx(packidx,
707 err = open_tree(&buf, &entries, &nentries, pack,
708 packidx, idx, commit->tree_id, objcache);
711 err = open_tree(&pbuf, &pentries, &pnentries, pack,
712 packidx, pidx, pcommit->tree_id, objcache);
718 err = tree_path_changed(&changed, &buf, &pbuf,
719 &entries, &nentries, &pentries, &pnentries, path,
720 pack, packidx, ibuf, objcache);
722 got_object_parsed_tree_entries_free(&entries);
725 got_object_parsed_tree_entries_free(&pentries);
729 if (err->code != GOT_ERR_NO_OBJ)
737 memcpy(id.sha1, pid->id->sha1, SHA1_DIGEST_LENGTH);
738 got_object_commit_close(commit);
745 err = send_traversed_commits(commit_ids, ncommits, ibuf);
750 err = got_privsep_send_commit(ibuf, commit);
755 err = send_commit_traversal_done(ibuf);
759 got_object_commit_close(commit);
761 got_object_commit_close(pcommit);
763 got_object_parsed_tree_entries_free(&entries);
765 got_object_parsed_tree_entries_free(&pentries);
767 if (err->code == GOT_ERR_PRIVSEP_PIPE)
770 got_privsep_send_error(ibuf, err);
776 static const struct got_error *
777 receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
779 const struct got_error *err = NULL;
781 struct got_imsg_packidx ipackidx;
783 struct got_packidx *p;
787 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
791 p = calloc(1, sizeof(*p));
793 err = got_error_from_errno("calloc");
797 if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
798 err = got_error(GOT_ERR_PRIVSEP_MSG);
803 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
807 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
808 if (datalen != sizeof(ipackidx)) {
809 err = got_error(GOT_ERR_PRIVSEP_LEN);
812 memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
814 p->len = ipackidx.len;
815 p->fd = dup(imsg.fd);
817 err = got_error_from_errno("dup");
820 if (lseek(p->fd, 0, SEEK_SET) == -1) {
821 err = got_error_from_errno("lseek");
825 #ifndef GOT_PACK_NO_MMAP
826 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
827 if (p->map == MAP_FAILED)
828 p->map = NULL; /* fall back to read(2) */
830 err = got_packidx_init_hdr(p, 1);
835 got_packidx_close(p);
842 static const struct got_error *
843 receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
845 const struct got_error *err = NULL;
847 struct got_imsg_pack ipack;
849 struct got_pack *pack;
853 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
857 pack = calloc(1, sizeof(*pack));
859 err = got_error_from_errno("calloc");
863 if (imsg.hdr.type != GOT_IMSG_PACK) {
864 err = got_error(GOT_ERR_PRIVSEP_MSG);
869 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
873 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
874 if (datalen != sizeof(ipack)) {
875 err = got_error(GOT_ERR_PRIVSEP_LEN);
878 memcpy(&ipack, imsg.data, sizeof(ipack));
880 pack->filesize = ipack.filesize;
881 pack->fd = dup(imsg.fd);
882 if (pack->fd == -1) {
883 err = got_error_from_errno("dup");
886 if (lseek(pack->fd, 0, SEEK_SET) == -1) {
887 err = got_error_from_errno("lseek");
890 pack->path_packfile = strdup(ipack.path_packfile);
891 if (pack->path_packfile == NULL) {
892 err = got_error_from_errno("strdup");
896 pack->delta_cache = got_delta_cache_alloc(100,
897 GOT_DELTA_RESULT_SIZE_CACHED_MAX);
898 if (pack->delta_cache == NULL) {
899 err = got_error_from_errno("got_delta_cache_alloc");
903 #ifndef GOT_PACK_NO_MMAP
904 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
906 if (pack->map == MAP_FAILED)
907 pack->map = NULL; /* fall back to read(2) */
921 main(int argc, char *argv[])
923 const struct got_error *err = NULL;
926 struct got_packidx *packidx = NULL;
927 struct got_pack *pack = NULL;
928 struct got_object_cache objcache;
930 //static int attached;
931 //while (!attached) sleep(1);
933 signal(SIGINT, catch_sigint);
935 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
937 err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
939 err = got_error_from_errno("got_object_cache_init");
940 got_privsep_send_error(&ibuf, err);
945 /* revoke access to most system calls */
946 if (pledge("stdio recvfd", NULL) == -1) {
947 err = got_error_from_errno("pledge");
948 got_privsep_send_error(&ibuf, err);
953 err = receive_packidx(&packidx, &ibuf);
955 got_privsep_send_error(&ibuf, err);
959 err = receive_pack(&pack, &ibuf);
961 got_privsep_send_error(&ibuf, err);
968 if (sigint_received) {
969 err = got_error(GOT_ERR_CANCELLED);
973 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
975 if (err->code == GOT_ERR_PRIVSEP_PIPE)
980 if (imsg.hdr.type == GOT_IMSG_STOP)
983 switch (imsg.hdr.type) {
984 case GOT_IMSG_PACKED_OBJECT_REQUEST:
985 err = object_request(&imsg, &ibuf, pack, packidx,
988 case GOT_IMSG_COMMIT_REQUEST:
989 err = commit_request(&imsg, &ibuf, pack, packidx,
992 case GOT_IMSG_TREE_REQUEST:
993 err = tree_request(&imsg, &ibuf, pack, packidx,
996 case GOT_IMSG_BLOB_REQUEST:
997 err = blob_request(&imsg, &ibuf, pack, packidx,
1000 case GOT_IMSG_TAG_REQUEST:
1001 err = tag_request(&imsg, &ibuf, pack, packidx,
1004 case GOT_IMSG_COMMIT_TRAVERSAL_REQUEST:
1005 err = commit_traversal_request(&imsg, &ibuf, pack,
1006 packidx, &objcache);
1009 err = got_error(GOT_ERR_PRIVSEP_MSG);
1013 if (imsg.fd != -1 && close(imsg.fd) != 0 && err == NULL)
1014 err = got_error_from_errno("close");
1021 got_packidx_close(packidx);
1023 got_pack_close(pack);
1024 got_object_cache_close(&objcache);
1027 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
1028 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1029 got_privsep_send_error(&ibuf, err);
1032 if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
1033 err = got_error_from_errno("close");