2 * Copyright (c) 2018 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>
19 #include <sys/queue.h>
21 #include <sys/socket.h>
36 #include "got_error.h"
37 #include "got_object.h"
38 #include "got_repository.h"
40 #include "got_lib_sha1.h"
41 #include "got_lib_delta.h"
42 #include "got_lib_pack.h"
43 #include "got_lib_path.h"
44 #include "got_lib_zbuf.h"
45 #include "got_lib_object.h"
46 #include "got_lib_privsep.h"
49 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
53 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
56 #define GOT_OBJ_TAG_COMMIT "commit"
57 #define GOT_OBJ_TAG_TREE "tree"
58 #define GOT_OBJ_TAG_BLOB "blob"
60 #define GOT_COMMIT_TAG_TREE "tree "
61 #define GOT_COMMIT_TAG_PARENT "parent "
62 #define GOT_COMMIT_TAG_AUTHOR "author "
63 #define GOT_COMMIT_TAG_COMMITTER "committer "
65 const struct got_error *
66 got_object_id_str(char **outbuf, struct got_object_id *id)
68 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
70 *outbuf = calloc(1, len);
72 return got_error_from_errno();
74 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
77 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
84 got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
86 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
89 struct got_object_id *
90 got_object_id_dup(struct got_object_id *id1)
92 struct got_object_id *id2;
94 id2 = malloc(sizeof(*id2));
97 memcpy(id2, id1, sizeof(*id2));
101 struct got_object_id *
102 got_object_get_id(struct got_object *obj)
104 return got_object_id_dup(&obj->id);
108 got_object_get_type(struct got_object *obj)
111 case GOT_OBJ_TYPE_COMMIT:
112 case GOT_OBJ_TYPE_TREE:
113 case GOT_OBJ_TYPE_BLOB:
114 case GOT_OBJ_TYPE_TAG:
125 static const struct got_error *
126 parse_object_header(struct got_object **obj, char *buf, size_t len)
128 const char *obj_tags[] = {
133 const int obj_types[] = {
139 size_t size = 0, hdrlen = 0;
141 char *p = strchr(buf, '\0');
144 return got_error(GOT_ERR_BAD_OBJ_HDR);
146 hdrlen = strlen(buf) + 1 /* '\0' */;
148 for (i = 0; i < nitems(obj_tags); i++) {
149 const char *tag = obj_tags[i];
150 size_t tlen = strlen(tag);
153 if (strncmp(buf, tag, tlen) != 0)
158 return got_error(GOT_ERR_BAD_OBJ_HDR);
159 size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
161 return got_error(GOT_ERR_BAD_OBJ_HDR);
166 return got_error(GOT_ERR_BAD_OBJ_HDR);
168 *obj = calloc(1, sizeof(**obj));
170 return got_error_from_errno();
172 (*obj)->hdrlen = hdrlen;
177 static const struct got_error *
178 read_object_header(struct got_object **obj, FILE *f)
180 const struct got_error *err;
181 struct got_zstream_buf zb;
183 const size_t zbsize = 64;
184 size_t outlen, totlen;
187 buf = calloc(zbsize, sizeof(char));
189 return got_error_from_errno();
191 err = got_inflate_init(&zb, NULL, zbsize);
198 err = got_inflate_read(&zb, f, &outlen);
201 if (strchr(zb.outbuf, '\0') == NULL) {
202 buf = recallocarray(buf, 1 + i, 2 + i, zbsize);
204 err = got_error_from_errno();
208 memcpy(buf + totlen, zb.outbuf, outlen);
211 } while (strchr(zb.outbuf, '\0') == NULL);
213 err = parse_object_header(obj, buf, totlen);
215 got_inflate_end(&zb);
220 read_object_header_privsep_child(int obj_fd, int imsg_fds[2])
222 const struct got_error *err = NULL;
223 struct got_object *obj = NULL;
228 setproctitle("got: read object header");
230 imsg_init(&ibuf, imsg_fds[1]);
232 /* revoke access to most system calls */
233 if (pledge("stdio", NULL) == -1) {
234 err = got_error_from_errno();
238 f = fdopen(obj_fd, "rb");
240 err = got_error_from_errno();
245 err = read_object_header(&obj, f);
249 err = got_privsep_send_obj(&ibuf, obj, 0);
252 got_object_close(obj);
254 got_privsep_send_error(&ibuf, err);
264 static const struct got_error *
265 read_object_header_privsep(struct got_object **obj, int fd)
267 struct imsgbuf parent_ibuf;
269 const struct got_error *err = NULL;
273 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
274 return got_error_from_errno();
278 return got_error_from_errno();
280 read_object_header_privsep_child(fd, imsg_fds);
285 imsg_init(&parent_ibuf, imsg_fds[0]);
286 err = got_privsep_recv_obj(obj, &parent_ibuf);
287 imsg_clear(&parent_ibuf);
288 waitpid(pid, &child_status, 0);
293 static const struct got_error *
294 object_path(char **path, struct got_object_id *id, struct got_repository *repo)
296 const struct got_error *err = NULL;
298 char *path_objects = got_repo_get_path_objects(repo);
302 if (path_objects == NULL)
303 return got_error_from_errno();
305 err = got_object_id_str(&hex, id);
309 if (asprintf(path, "%s/%.2x/%s", path_objects,
310 id->sha1[0], hex + 2) == -1)
311 err = got_error_from_errno();
318 static const struct got_error *
319 open_loose_object(int *fd, struct got_object *obj, struct got_repository *repo)
321 const struct got_error *err = NULL;
324 err = object_path(&path, &obj->id, repo);
327 *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
329 err = got_error_from_errno();
337 const struct got_error *
338 got_object_open(struct got_object **obj, struct got_repository *repo,
339 struct got_object_id *id)
341 const struct got_error *err = NULL;
345 err = object_path(&path, id, repo);
349 fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
351 if (errno != ENOENT) {
352 err = got_error_from_errno();
355 err = got_packfile_open_object(obj, id, repo);
359 err = got_error(GOT_ERR_NO_OBJ);
361 err = read_object_header_privsep(obj, fd);
364 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
374 const struct got_error *
375 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
378 struct got_object_id id;
380 if (!got_parse_sha1_digest(id.sha1, id_str))
381 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
383 return got_object_open(obj, repo, &id);
387 got_object_close(struct got_object *obj)
389 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
390 struct got_delta *delta;
391 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
392 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
393 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
394 got_delta_close(delta);
397 if (obj->flags & GOT_OBJ_FLAG_PACKED)
398 free(obj->path_packfile);
402 struct got_commit_object *
403 got_object_commit_alloc_partial(void)
405 struct got_commit_object *commit;
407 commit = calloc(1, sizeof(*commit));
410 commit->tree_id = calloc(1, sizeof(*commit->tree_id));
411 if (commit->tree_id == NULL) {
416 SIMPLEQ_INIT(&commit->parent_ids);
421 const struct got_error *
422 got_object_commit_add_parent(struct got_commit_object *commit,
425 const struct got_error *err = NULL;
426 struct got_parent_id *pid;
428 pid = calloc(1, sizeof(*pid));
430 return got_error_from_errno();
432 pid->id = calloc(1, sizeof(*pid->id));
433 if (pid->id == NULL) {
434 err = got_error_from_errno();
439 if (!got_parse_sha1_digest(pid->id->sha1, id_str)) {
440 err = got_error(GOT_ERR_BAD_OBJ_DATA);
446 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, pid, entry);
452 static const struct got_error *
453 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
455 const struct got_error *err = NULL;
458 ssize_t remain = (ssize_t)len;
460 *commit = got_object_commit_alloc_partial();
462 return got_error_from_errno();
464 tlen = strlen(GOT_COMMIT_TAG_TREE);
465 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
467 if (remain < SHA1_DIGEST_STRING_LENGTH) {
468 err = got_error(GOT_ERR_BAD_OBJ_DATA);
472 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
473 err = got_error(GOT_ERR_BAD_OBJ_DATA);
476 remain -= SHA1_DIGEST_STRING_LENGTH;
477 s += SHA1_DIGEST_STRING_LENGTH;
479 err = got_error(GOT_ERR_BAD_OBJ_DATA);
483 tlen = strlen(GOT_COMMIT_TAG_PARENT);
484 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
486 if (remain < SHA1_DIGEST_STRING_LENGTH) {
487 err = got_error(GOT_ERR_BAD_OBJ_DATA);
491 err = got_object_commit_add_parent(*commit, s);
495 remain -= SHA1_DIGEST_STRING_LENGTH;
496 s += SHA1_DIGEST_STRING_LENGTH;
499 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
500 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
505 err = got_error(GOT_ERR_BAD_OBJ_DATA);
511 err = got_error(GOT_ERR_BAD_OBJ_DATA);
515 (*commit)->author = strdup(s);
516 if ((*commit)->author == NULL) {
517 err = got_error_from_errno();
520 s += strlen((*commit)->author) + 1;
521 remain -= strlen((*commit)->author) + 1;
524 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
525 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
530 err = got_error(GOT_ERR_BAD_OBJ_DATA);
536 err = got_error(GOT_ERR_BAD_OBJ_DATA);
540 (*commit)->committer = strdup(s);
541 if ((*commit)->committer == NULL) {
542 err = got_error_from_errno();
545 s += strlen((*commit)->committer) + 1;
546 remain -= strlen((*commit)->committer) + 1;
549 (*commit)->logmsg = strndup(s, remain);
550 if ((*commit)->logmsg == NULL) {
551 err = got_error_from_errno();
556 got_object_commit_close(*commit);
563 tree_entry_close(struct got_tree_entry *te)
570 struct got_tree_entry *
571 got_alloc_tree_entry_partial(void)
573 struct got_tree_entry *te;
575 te = calloc(1, sizeof(*te));
579 te->id = calloc(1, sizeof(*te->id));
580 if (te->id == NULL) {
587 static const struct got_error *
588 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
591 char *p = buf, *space;
592 const struct got_error *err = NULL;
594 *te = got_alloc_tree_entry_partial();
596 return got_error_from_errno();
598 *elen = strlen(buf) + 1;
599 if (*elen > maxlen) {
602 return got_error(GOT_ERR_BAD_OBJ_DATA);
605 space = strchr(buf, ' ');
607 err = got_error(GOT_ERR_BAD_OBJ_DATA);
613 if (*p < '0' && *p > '7') {
614 err = got_error(GOT_ERR_BAD_OBJ_DATA);
618 (*te)->mode |= *p - '0';
622 (*te)->name = strdup(space + 1);
623 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
624 err = got_error(GOT_ERR_BAD_OBJ_DATA);
627 buf += strlen(buf) + 1;
628 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
629 *elen += SHA1_DIGEST_LENGTH;
632 tree_entry_close(*te);
638 static const struct got_error *
639 parse_tree_object(struct got_tree_object **tree, uint8_t *buf, size_t len)
641 const struct got_error *err;
644 *tree = calloc(1, sizeof(**tree));
646 return got_error_from_errno();
648 SIMPLEQ_INIT(&(*tree)->entries);
651 struct got_tree_entry *te;
654 err = parse_tree_entry(&te, &elen, buf, remain);
658 SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
664 got_object_tree_close(*tree);
665 return got_error(GOT_ERR_BAD_OBJ_DATA);
671 static const struct got_error *
672 read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
674 const struct got_error *err = NULL;
675 static const size_t blocksize = 512;
676 size_t n, total, remain;
682 buf = calloc(1, blocksize);
684 return got_error_from_errno();
691 newbuf = reallocarray(buf, 1, total + blocksize);
692 if (newbuf == NULL) {
693 err = got_error_from_errno();
699 n = fread(buf + total, 1, remain, f);
702 err = got_ferror(f, GOT_ERR_IO);
720 static const struct got_error *
721 read_commit_object(struct got_commit_object **commit, struct got_object *obj,
724 const struct got_error *err = NULL;
728 if (obj->flags & GOT_OBJ_FLAG_PACKED)
729 err = read_to_mem(&p, &len, f);
731 err = got_inflate_to_mem(&p, &len, f);
735 if (len < obj->hdrlen + obj->size) {
736 err = got_error(GOT_ERR_BAD_OBJ_DATA);
740 /* Skip object header. */
742 err = parse_commit_object(commit, p + obj->hdrlen, len);
749 read_commit_object_privsep_child(struct got_object *obj, int obj_fd,
752 const struct got_error *err = NULL;
753 struct got_commit_object *commit = NULL;
758 setproctitle("got: read commit object");
760 imsg_init(&ibuf, imsg_fds[1]);
762 /* revoke access to most system calls */
763 if (pledge("stdio", NULL) == -1) {
764 err = got_error_from_errno();
768 f = fdopen(obj_fd, "rb");
770 err = got_error_from_errno();
775 err = read_commit_object(&commit, obj, f);
779 err = got_privsep_send_commit_obj(&ibuf, commit);
782 got_object_commit_close(commit);
784 got_privsep_send_error(&ibuf, err);
794 static const struct got_error *
795 read_commit_object_privsep(struct got_commit_object **commit,
796 struct got_repository *repo, struct got_object *obj, int fd)
798 const struct got_error *err = NULL;
799 struct imsgbuf parent_ibuf;
804 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
805 return got_error_from_errno();
809 return got_error_from_errno();
811 read_commit_object_privsep_child(obj, fd, imsg_fds);
816 imsg_init(&parent_ibuf, imsg_fds[0]);
817 err = got_privsep_recv_commit_obj(commit, &parent_ibuf);
818 imsg_clear(&parent_ibuf);
819 waitpid(pid, &child_status, 0);
824 const struct got_error *
825 got_object_commit_open(struct got_commit_object **commit,
826 struct got_repository *repo, struct got_object *obj)
828 const struct got_error *err = NULL;
830 if (obj->type != GOT_OBJ_TYPE_COMMIT)
831 return got_error(GOT_ERR_OBJ_TYPE);
833 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
836 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
840 err = parse_commit_object(commit, buf, len);
844 err = open_loose_object(&fd, obj, repo);
847 err = read_commit_object_privsep(commit, repo, obj, fd);
854 got_object_commit_close(struct got_commit_object *commit)
856 struct got_parent_id *pid;
858 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
859 pid = SIMPLEQ_FIRST(&commit->parent_ids);
860 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
865 free(commit->tree_id);
866 free(commit->author);
867 free(commit->committer);
868 free(commit->logmsg);
872 static const struct got_error *
873 read_tree_object(struct got_tree_object **tree, struct got_object *obj, FILE *f)
875 const struct got_error *err = NULL;
879 if (obj->flags & GOT_OBJ_FLAG_PACKED)
880 err = read_to_mem(&p, &len, f);
882 err = got_inflate_to_mem(&p, &len, f);
886 if (len < obj->hdrlen + obj->size) {
887 err = got_error(GOT_ERR_BAD_OBJ_DATA);
891 /* Skip object header. */
893 err = parse_tree_object(tree, p + obj->hdrlen, len);
900 read_tree_object_privsep_child(struct got_object *obj, int obj_fd,
903 const struct got_error *err = NULL;
904 struct got_tree_object *tree = NULL;
909 setproctitle("got: read tree object");
911 imsg_init(&ibuf, imsg_fds[1]);
913 /* revoke access to most system calls */
914 if (pledge("stdio", NULL) == -1) {
915 err = got_error_from_errno();
919 f = fdopen(obj_fd, "rb");
921 err = got_error_from_errno();
926 err = read_tree_object(&tree, obj, f);
930 err = got_privsep_send_tree_obj(&ibuf, tree);
933 got_object_tree_close(tree);
935 got_privsep_send_error(&ibuf, err);
945 static const struct got_error *
946 read_tree_object_privsep(struct got_tree_object **tree, struct got_object *obj,
949 const struct got_error *err = NULL;
950 struct imsgbuf parent_ibuf;
955 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
956 return got_error_from_errno();
960 return got_error_from_errno();
962 read_tree_object_privsep_child(obj, fd, imsg_fds);
967 imsg_init(&parent_ibuf, imsg_fds[0]);
968 err = got_privsep_recv_tree_obj(tree, &parent_ibuf);
969 imsg_clear(&parent_ibuf);
970 waitpid(pid, &child_status, 0);
975 const struct got_error *
976 got_object_tree_open(struct got_tree_object **tree,
977 struct got_repository *repo, struct got_object *obj)
979 const struct got_error *err = NULL;
981 if (obj->type != GOT_OBJ_TYPE_TREE)
982 return got_error(GOT_ERR_OBJ_TYPE);
984 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
987 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
991 err = parse_tree_object(tree, buf, len);
995 err = open_loose_object(&fd, obj, repo);
998 err = read_tree_object_privsep(tree, obj, fd);
1005 got_object_tree_close(struct got_tree_object *tree)
1007 struct got_tree_entry *te;
1009 while (!SIMPLEQ_EMPTY(&tree->entries)) {
1010 te = SIMPLEQ_FIRST(&tree->entries);
1011 SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
1012 tree_entry_close(te);
1018 const struct got_error *
1019 got_object_blob_open(struct got_blob_object **blob,
1020 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1022 const struct got_error *err = NULL;
1024 if (obj->type != GOT_OBJ_TYPE_BLOB)
1025 return got_error(GOT_ERR_OBJ_TYPE);
1027 if (blocksize < obj->hdrlen)
1028 return got_error(GOT_ERR_NO_SPACE);
1030 *blob = calloc(1, sizeof(**blob));
1032 return got_error_from_errno();
1034 (*blob)->read_buf = calloc(1, blocksize);
1035 if ((*blob)->read_buf == NULL) {
1036 err = got_error_from_errno();
1041 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1042 err = got_packfile_extract_object(&((*blob)->f), obj, repo);
1044 free((*blob)->read_buf);
1053 err = open_loose_object(&fd, obj, repo);
1055 free((*blob)->read_buf);
1060 f = fdopen(fd, "rb");
1062 free((*blob)->read_buf);
1068 (*blob)->f = got_opentemp();
1069 if ((*blob)->f == NULL) {
1070 err = got_error_from_errno();
1071 free((*blob)->read_buf);
1079 err = got_inflate_to_file(&size, f, (*blob)->f);
1082 free((*blob)->read_buf);
1090 (*blob)->hdrlen = obj->hdrlen;
1091 (*blob)->blocksize = blocksize;
1092 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
1098 got_object_blob_close(struct got_blob_object *blob)
1100 free(blob->read_buf);
1106 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1108 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1112 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1114 return blob->hdrlen;
1118 got_object_blob_get_read_buf(struct got_blob_object *blob)
1120 return blob->read_buf;
1123 const struct got_error *
1124 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1128 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1129 if (n == 0 && ferror(blob->f))
1130 return got_ferror(blob->f, GOT_ERR_IO);