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>
34 #include "got_error.h"
35 #include "got_object.h"
38 #include "got_lib_delta.h"
39 #include "got_lib_delta_cache.h"
40 #include "got_lib_object.h"
41 #include "got_lib_object_cache.h"
42 #include "got_lib_object_parse.h"
43 #include "got_lib_privsep.h"
44 #include "got_lib_pack.h"
46 static volatile sig_atomic_t sigint_received;
49 catch_sigint(int signo)
54 static const struct got_error *
55 open_object(struct got_object **obj, struct got_pack *pack,
56 struct got_packidx *packidx, int idx, struct got_object_id *id,
57 struct got_object_cache *objcache)
59 const struct got_error *err;
61 err = got_packfile_open_object(obj, pack, packidx, idx, id);
66 err = got_object_cache_add(objcache, id, *obj);
68 if (err->code == GOT_ERR_OBJ_EXISTS ||
69 err->code == GOT_ERR_OBJ_TOO_LARGE)
77 static const struct got_error *
78 object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
79 struct got_packidx *packidx, struct got_object_cache *objcache)
81 const struct got_error *err = NULL;
82 struct got_imsg_packed_object iobj;
83 struct got_object *obj;
84 struct got_object_id id;
87 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
88 if (datalen != sizeof(iobj))
89 return got_error(GOT_ERR_PRIVSEP_LEN);
90 memcpy(&iobj, imsg->data, sizeof(iobj));
91 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
93 obj = got_object_cache_get(objcache, &id);
97 err = open_object(&obj, pack, packidx, iobj.idx, &id,
103 err = got_privsep_send_obj(ibuf, obj);
105 got_object_close(obj);
109 const struct got_error *
110 open_commit(struct got_commit_object **commit, struct got_pack *pack,
111 struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
112 struct got_object_cache *objcache)
114 const struct got_error *err = NULL;
115 struct got_object *obj = NULL;
121 obj = got_object_cache_get(objcache, id);
125 err = open_object(&obj, pack, packidx, obj_idx, id,
131 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
137 err = got_object_parse_commit(commit, buf, len);
139 got_object_close(obj);
144 static const struct got_error *
145 commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
146 struct got_packidx *packidx, struct got_object_cache *objcache)
148 const struct got_error *err = NULL;
149 struct got_imsg_packed_object iobj;
150 struct got_commit_object *commit = NULL;
151 struct got_object_id id;
154 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
155 if (datalen != sizeof(iobj))
156 return got_error(GOT_ERR_PRIVSEP_LEN);
157 memcpy(&iobj, imsg->data, sizeof(iobj));
158 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
160 err = open_commit(&commit, pack, packidx, iobj.idx, &id, objcache);
164 err = got_privsep_send_commit(ibuf, commit);
167 got_object_commit_close(commit);
169 if (err->code == GOT_ERR_PRIVSEP_PIPE)
172 got_privsep_send_error(ibuf, err);
178 const struct got_error *
179 open_tree(uint8_t **buf, struct got_pathlist_head *entries, int *nentries,
180 struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
181 struct got_object_id *id, struct got_object_cache *objcache)
183 const struct got_error *err = NULL;
184 struct got_object *obj = NULL;
190 obj = got_object_cache_get(objcache, id);
194 err = open_object(&obj, pack, packidx, obj_idx, id,
200 err = got_packfile_extract_object_to_mem(buf, &len, obj, pack);
206 err = got_object_parse_tree(entries, nentries, *buf, len);
208 got_object_close(obj);
216 static const struct got_error *
217 tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
218 struct got_packidx *packidx, struct got_object_cache *objcache)
220 const struct got_error *err = NULL;
221 struct got_imsg_packed_object iobj;
222 struct got_pathlist_head entries;
225 struct got_object_id id;
228 TAILQ_INIT(&entries);
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);
242 got_object_parsed_tree_entries_free(&entries);
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, int 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 blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
293 struct got_packidx *packidx, struct got_object_cache *objcache)
295 const struct got_error *err = NULL;
296 struct got_imsg_packed_object iobj;
297 struct got_object *obj = NULL;
298 FILE *outfile = NULL, *basefile = NULL, *accumfile = NULL;
299 struct got_object_id id;
304 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
305 if (datalen != sizeof(iobj))
306 return got_error(GOT_ERR_PRIVSEP_LEN);
307 memcpy(&iobj, imsg->data, sizeof(iobj));
308 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
310 obj = got_object_cache_get(objcache, &id);
314 err = open_object(&obj, pack, packidx, iobj.idx, &id,
320 err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
323 err = receive_file(&basefile, ibuf, GOT_IMSG_TMPFD);
326 err = receive_file(&accumfile, ibuf, GOT_IMSG_TMPFD);
330 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
331 err = got_pack_get_max_delta_object_size(&blob_size, obj, pack);
335 blob_size = obj->size;
337 if (blob_size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
338 err = got_packfile_extract_object_to_mem(&buf, &obj->size,
341 err = got_packfile_extract_object(pack, obj, outfile, basefile,
346 err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, buf);
349 if (outfile && fclose(outfile) != 0 && err == NULL)
350 err = got_error_from_errno("fclose");
351 if (basefile && fclose(basefile) != 0 && err == NULL)
352 err = got_error_from_errno("fclose");
353 if (accumfile && fclose(accumfile) != 0 && err == NULL)
354 err = got_error_from_errno("fclose");
355 got_object_close(obj);
356 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
357 got_privsep_send_error(ibuf, err);
362 static const struct got_error *
363 tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
364 struct got_packidx *packidx, struct got_object_cache *objcache)
366 const struct got_error *err = NULL;
367 struct got_imsg_packed_object iobj;
368 struct got_object *obj = NULL;
369 struct got_tag_object *tag = NULL;
372 struct got_object_id id;
375 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
376 if (datalen != sizeof(iobj))
377 return got_error(GOT_ERR_PRIVSEP_LEN);
378 memcpy(&iobj, imsg->data, sizeof(iobj));
379 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
381 obj = got_object_cache_get(objcache, &id);
385 err = open_object(&obj, pack, packidx, iobj.idx, &id,
391 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
396 err = got_object_parse_tag(&tag, buf, len);
400 err = got_privsep_send_tag(ibuf, tag);
403 got_object_close(obj);
405 got_object_tag_close(tag);
407 if (err->code == GOT_ERR_PRIVSEP_PIPE)
410 got_privsep_send_error(ibuf, err);
416 static struct got_parsed_tree_entry *
417 find_entry_by_name(struct got_pathlist_head *entries, int nentries,
418 const char *name, size_t len)
420 struct got_pathlist_entry *pe;
422 /* Note that tree entries are sorted in strncmp() order. */
423 TAILQ_FOREACH(pe, entries, entry) {
424 int cmp = strncmp(pe->path, name, len);
429 if (pe->path[len] == '\0')
430 return (struct got_parsed_tree_entry *)pe->data;
435 const struct got_error *
436 tree_path_changed(int *changed, uint8_t **buf1, uint8_t **buf2,
437 struct got_pathlist_head *entries1, int *nentries1,
438 struct got_pathlist_head *entries2, int *nentries2,
439 const char *path, struct got_pack *pack, struct got_packidx *packidx,
440 struct imsgbuf *ibuf, struct got_object_cache *objcache)
442 const struct got_error *err = NULL;
443 struct got_parsed_tree_entry *pte1 = NULL, *pte2 = NULL;
449 /* We are expecting an absolute in-repository path. */
451 return got_error(GOT_ERR_NOT_ABSPATH);
453 /* We not do support comparing the root path. */
455 return got_error(GOT_ERR_BAD_PATH);
458 s++; /* skip leading '/' */
469 pte1 = find_entry_by_name(entries1, *nentries1, seg, seglen);
471 err = got_error(GOT_ERR_NO_OBJ);
475 pte2 = find_entry_by_name(entries2, *nentries2, seg, seglen);
481 if (pte1->mode != pte2->mode) {
486 if (memcmp(pte1->id, pte2->id, SHA1_DIGEST_LENGTH) == 0) {
491 if (*s == '\0') { /* final path element */
500 struct got_object_id id1, id2;
503 idx = got_packidx_get_object_idx_sha1(packidx,
506 err = got_error(GOT_ERR_NO_OBJ);
509 memcpy(id1.sha1, pte1->id, SHA1_DIGEST_LENGTH);
510 got_object_parsed_tree_entries_free(entries1);
514 err = open_tree(buf1, entries1, nentries1, pack,
515 packidx, idx, &id1, objcache);
520 idx = got_packidx_get_object_idx_sha1(packidx,
523 err = got_error(GOT_ERR_NO_OBJ);
526 memcpy(id2.sha1, pte2->id, SHA1_DIGEST_LENGTH);
527 got_object_parsed_tree_entries_free(entries2);
531 err = open_tree(buf2, entries2, nentries2, pack,
532 packidx, idx, &id2, objcache);
542 static const struct got_error *
543 commit_traversal_request(struct imsg *imsg, struct imsgbuf *ibuf,
544 struct got_pack *pack, struct got_packidx *packidx,
545 struct got_object_cache *objcache)
547 const struct got_error *err = NULL;
548 struct got_imsg_packed_object iobj;
549 struct got_object_qid *pid;
550 struct got_commit_object *commit = NULL, *pcommit = NULL;
551 struct got_pathlist_head entries, pentries;
552 int nentries = 0, pnentries = 0;
553 struct got_object_id id;
554 size_t datalen, path_len;
556 const int min_alloc = 64;
557 int changed = 0, ncommits = 0, nallocated = 0;
558 struct got_object_id *commit_ids = NULL;
560 TAILQ_INIT(&entries);
561 TAILQ_INIT(&pentries);
563 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
564 if (datalen < sizeof(iobj))
565 return got_error(GOT_ERR_PRIVSEP_LEN);
566 memcpy(&iobj, imsg->data, sizeof(iobj));
567 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
569 path_len = datalen - sizeof(iobj) - 1;
571 return got_error(GOT_ERR_PRIVSEP_LEN);
573 path = imsg->data + sizeof(iobj);
574 if (path[path_len] != '\0')
575 return got_error(GOT_ERR_PRIVSEP_LEN);
578 nallocated = min_alloc;
579 commit_ids = reallocarray(NULL, nallocated, sizeof(*commit_ids));
580 if (commit_ids == NULL)
581 return got_error_from_errno("reallocarray");
584 const size_t max_datalen = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
587 if (sigint_received) {
588 err = got_error(GOT_ERR_CANCELLED);
592 if (commit == NULL) {
593 idx = got_packidx_get_object_idx(packidx, &id);
596 err = open_commit(&commit, pack, packidx,
599 if (err->code != GOT_ERR_NO_OBJ)
606 if (sizeof(struct got_imsg_traversed_commits) +
607 ncommits * SHA1_DIGEST_LENGTH >= max_datalen) {
608 err = got_privsep_send_traversed_commits(commit_ids,
615 if (ncommits > nallocated) {
616 struct got_object_id *new;
617 nallocated += min_alloc;
618 new = reallocarray(commit_ids, nallocated,
619 sizeof(*commit_ids));
621 err = got_error_from_errno("reallocarray");
626 memcpy(commit_ids[ncommits - 1].sha1, id.sha1,
629 pid = SIMPLEQ_FIRST(&commit->parent_ids);
633 idx = got_packidx_get_object_idx(packidx, pid->id);
637 err = open_commit(&pcommit, pack, packidx, idx, pid->id,
640 if (err->code != GOT_ERR_NO_OBJ)
646 if (path[0] == '/' && path[1] == '\0') {
647 if (got_object_id_cmp(pcommit->tree_id,
648 commit->tree_id) != 0) {
654 uint8_t *buf = NULL, *pbuf = NULL;
656 idx = got_packidx_get_object_idx(packidx,
660 pidx = got_packidx_get_object_idx(packidx,
665 err = open_tree(&buf, &entries, &nentries, pack,
666 packidx, idx, commit->tree_id, objcache);
669 err = open_tree(&pbuf, &pentries, &pnentries, pack,
670 packidx, pidx, pcommit->tree_id, objcache);
676 err = tree_path_changed(&changed, &buf, &pbuf,
677 &entries, &nentries, &pentries, &pnentries, path,
678 pack, packidx, ibuf, objcache);
680 got_object_parsed_tree_entries_free(&entries);
683 got_object_parsed_tree_entries_free(&pentries);
687 if (err->code != GOT_ERR_NO_OBJ)
695 memcpy(id.sha1, pid->id->sha1, SHA1_DIGEST_LENGTH);
696 got_object_commit_close(commit);
703 err = got_privsep_send_traversed_commits(commit_ids,
709 err = got_privsep_send_commit(ibuf, commit);
714 err = got_privsep_send_commit_traversal_done(ibuf);
718 got_object_commit_close(commit);
720 got_object_commit_close(pcommit);
722 got_object_parsed_tree_entries_free(&entries);
724 got_object_parsed_tree_entries_free(&pentries);
726 if (err->code == GOT_ERR_PRIVSEP_PIPE)
729 got_privsep_send_error(ibuf, err);
735 static const struct got_error *
736 receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
738 const struct got_error *err = NULL;
740 struct got_imsg_packidx ipackidx;
742 struct got_packidx *p;
746 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
750 p = calloc(1, sizeof(*p));
752 err = got_error_from_errno("calloc");
756 if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
757 err = got_error(GOT_ERR_PRIVSEP_MSG);
762 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
766 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
767 if (datalen != sizeof(ipackidx)) {
768 err = got_error(GOT_ERR_PRIVSEP_LEN);
771 memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
773 p->len = ipackidx.len;
774 p->fd = dup(imsg.fd);
776 err = got_error_from_errno("dup");
779 if (lseek(p->fd, 0, SEEK_SET) == -1) {
780 err = got_error_from_errno("lseek");
784 #ifndef GOT_PACK_NO_MMAP
785 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
786 if (p->map == MAP_FAILED)
787 p->map = NULL; /* fall back to read(2) */
789 err = got_packidx_init_hdr(p, 1);
794 got_packidx_close(p);
801 static const struct got_error *
802 receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
804 const struct got_error *err = NULL;
806 struct got_imsg_pack ipack;
808 struct got_pack *pack;
812 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
816 pack = calloc(1, sizeof(*pack));
818 err = got_error_from_errno("calloc");
822 if (imsg.hdr.type != GOT_IMSG_PACK) {
823 err = got_error(GOT_ERR_PRIVSEP_MSG);
828 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
832 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
833 if (datalen != sizeof(ipack)) {
834 err = got_error(GOT_ERR_PRIVSEP_LEN);
837 memcpy(&ipack, imsg.data, sizeof(ipack));
839 pack->filesize = ipack.filesize;
840 pack->fd = dup(imsg.fd);
841 if (pack->fd == -1) {
842 err = got_error_from_errno("dup");
845 if (lseek(pack->fd, 0, SEEK_SET) == -1) {
846 err = got_error_from_errno("lseek");
849 pack->path_packfile = strdup(ipack.path_packfile);
850 if (pack->path_packfile == NULL) {
851 err = got_error_from_errno("strdup");
855 pack->delta_cache = got_delta_cache_alloc(100,
856 GOT_DELTA_RESULT_SIZE_CACHED_MAX);
857 if (pack->delta_cache == NULL) {
858 err = got_error_from_errno("got_delta_cache_alloc");
862 #ifndef GOT_PACK_NO_MMAP
863 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
865 if (pack->map == MAP_FAILED)
866 pack->map = NULL; /* fall back to read(2) */
880 main(int argc, char *argv[])
882 const struct got_error *err = NULL;
885 struct got_packidx *packidx = NULL;
886 struct got_pack *pack = NULL;
887 struct got_object_cache objcache;
889 //static int attached;
890 //while (!attached) sleep(1);
892 signal(SIGINT, catch_sigint);
894 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
896 err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
898 err = got_error_from_errno("got_object_cache_init");
899 got_privsep_send_error(&ibuf, err);
904 /* revoke access to most system calls */
905 if (pledge("stdio recvfd", NULL) == -1) {
906 err = got_error_from_errno("pledge");
907 got_privsep_send_error(&ibuf, err);
912 err = receive_packidx(&packidx, &ibuf);
914 got_privsep_send_error(&ibuf, err);
918 err = receive_pack(&pack, &ibuf);
920 got_privsep_send_error(&ibuf, err);
927 if (sigint_received) {
928 err = got_error(GOT_ERR_CANCELLED);
932 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
934 if (err->code == GOT_ERR_PRIVSEP_PIPE)
939 if (imsg.hdr.type == GOT_IMSG_STOP)
942 switch (imsg.hdr.type) {
943 case GOT_IMSG_PACKED_OBJECT_REQUEST:
944 err = object_request(&imsg, &ibuf, pack, packidx,
947 case GOT_IMSG_COMMIT_REQUEST:
948 err = commit_request(&imsg, &ibuf, pack, packidx,
951 case GOT_IMSG_TREE_REQUEST:
952 err = tree_request(&imsg, &ibuf, pack, packidx,
955 case GOT_IMSG_BLOB_REQUEST:
956 err = blob_request(&imsg, &ibuf, pack, packidx,
959 case GOT_IMSG_TAG_REQUEST:
960 err = tag_request(&imsg, &ibuf, pack, packidx,
963 case GOT_IMSG_COMMIT_TRAVERSAL_REQUEST:
964 err = commit_traversal_request(&imsg, &ibuf, pack,
968 err = got_error(GOT_ERR_PRIVSEP_MSG);
972 if (imsg.fd != -1 && close(imsg.fd) != 0 && err == NULL)
973 err = got_error_from_errno("close");
980 got_packidx_close(packidx);
982 got_pack_close(pack);
983 got_object_cache_close(&objcache);
986 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
987 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
988 got_privsep_send_error(&ibuf, err);
991 if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
992 err = got_error_from_errno("close");