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);
219 static const struct got_error *
220 read_object_header_privsep(struct got_object **obj, int fd)
222 struct imsgbuf parent_ibuf;
224 const struct got_error *err = NULL;
228 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
229 return got_error_from_errno();
233 return got_error_from_errno();
235 struct got_object *child_obj = NULL;
236 struct imsgbuf child_ibuf;
240 setproctitle("got: read object header");
242 imsg_init(&child_ibuf, imsg_fds[1]);
246 /* revoke access to most system calls */
247 if (pledge("stdio", NULL) == -1) {
248 err = got_error_from_errno();
252 f = fdopen(fd, "rb");
254 err = got_error_from_errno();
259 err = read_object_header(&child_obj, f);
263 err = got_privsep_send_obj(&child_ibuf, child_obj, 0);
266 got_object_close(child_obj);
268 got_privsep_send_error(&child_ibuf, err);
273 imsg_clear(&child_ibuf);
279 imsg_init(&parent_ibuf, imsg_fds[0]);
280 err = got_privsep_recv_obj(obj, &parent_ibuf);
281 imsg_clear(&parent_ibuf);
282 waitpid(pid, &child_status, 0);
287 static const struct got_error *
288 object_path(char **path, struct got_object_id *id, struct got_repository *repo)
290 const struct got_error *err = NULL;
292 char *path_objects = got_repo_get_path_objects(repo);
296 if (path_objects == NULL)
297 return got_error_from_errno();
299 err = got_object_id_str(&hex, id);
303 if (asprintf(path, "%s/%.2x/%s", path_objects,
304 id->sha1[0], hex + 2) == -1)
305 err = got_error_from_errno();
312 static const struct got_error *
313 open_loose_object(int *fd, struct got_object *obj, struct got_repository *repo)
315 const struct got_error *err = NULL;
318 err = object_path(&path, &obj->id, repo);
321 *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
323 err = got_error_from_errno();
331 const struct got_error *
332 got_object_open(struct got_object **obj, struct got_repository *repo,
333 struct got_object_id *id)
335 const struct got_error *err = NULL;
339 err = object_path(&path, id, repo);
343 fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
345 if (errno != ENOENT) {
346 err = got_error_from_errno();
349 err = got_packfile_open_object(obj, id, repo);
353 err = got_error(GOT_ERR_NO_OBJ);
355 err = read_object_header_privsep(obj, fd);
358 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
368 const struct got_error *
369 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
372 struct got_object_id id;
374 if (!got_parse_sha1_digest(id.sha1, id_str))
375 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
377 return got_object_open(obj, repo, &id);
381 got_object_close(struct got_object *obj)
383 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
384 struct got_delta *delta;
385 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
386 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
387 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
388 got_delta_close(delta);
391 if (obj->flags & GOT_OBJ_FLAG_PACKED)
392 free(obj->path_packfile);
396 static const struct got_error *
397 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
399 const struct got_error *err = NULL;
402 ssize_t remain = (ssize_t)len;
404 *commit = calloc(1, sizeof(**commit));
406 return got_error_from_errno();
407 (*commit)->tree_id = calloc(1, sizeof(*(*commit)->tree_id));
408 if ((*commit)->tree_id == NULL) {
409 err = got_error_from_errno();
415 SIMPLEQ_INIT(&(*commit)->parent_ids);
417 tlen = strlen(GOT_COMMIT_TAG_TREE);
418 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
420 if (remain < SHA1_DIGEST_STRING_LENGTH) {
421 err = got_error(GOT_ERR_BAD_OBJ_DATA);
425 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
426 err = got_error(GOT_ERR_BAD_OBJ_DATA);
429 remain -= SHA1_DIGEST_STRING_LENGTH;
430 s += SHA1_DIGEST_STRING_LENGTH;
432 err = got_error(GOT_ERR_BAD_OBJ_DATA);
436 tlen = strlen(GOT_COMMIT_TAG_PARENT);
437 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
438 struct got_parent_id *pid;
441 if (remain < SHA1_DIGEST_STRING_LENGTH) {
442 err = got_error(GOT_ERR_BAD_OBJ_DATA);
446 pid = calloc(1, sizeof(*pid));
448 err = got_error_from_errno();
451 pid->id = calloc(1, sizeof(*pid->id));
452 if (pid->id == NULL) {
453 err = got_error_from_errno();
458 if (!got_parse_sha1_digest(pid->id->sha1, s)) {
459 err = got_error(GOT_ERR_BAD_OBJ_DATA);
464 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, pid, entry);
465 (*commit)->nparents++;
467 remain -= SHA1_DIGEST_STRING_LENGTH;
468 s += SHA1_DIGEST_STRING_LENGTH;
471 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
472 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
477 err = got_error(GOT_ERR_BAD_OBJ_DATA);
483 err = got_error(GOT_ERR_BAD_OBJ_DATA);
487 (*commit)->author = strdup(s);
488 if ((*commit)->author == NULL) {
489 err = got_error_from_errno();
492 s += strlen((*commit)->author) + 1;
493 remain -= strlen((*commit)->author) + 1;
496 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
497 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
502 err = got_error(GOT_ERR_BAD_OBJ_DATA);
508 err = got_error(GOT_ERR_BAD_OBJ_DATA);
512 (*commit)->committer = strdup(s);
513 if ((*commit)->committer == NULL) {
514 err = got_error_from_errno();
517 s += strlen((*commit)->committer) + 1;
518 remain -= strlen((*commit)->committer) + 1;
521 (*commit)->logmsg = strndup(s, remain);
522 if ((*commit)->logmsg == NULL) {
523 err = got_error_from_errno();
528 got_object_commit_close(*commit);
535 tree_entry_close(struct got_tree_entry *te)
542 static const struct got_error *
543 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
546 char *p = buf, *space;
547 const struct got_error *err = NULL;
549 *te = calloc(1, sizeof(**te));
551 return got_error_from_errno();
553 (*te)->id = calloc(1, sizeof(*(*te)->id));
554 if ((*te)->id == NULL) {
555 err = got_error_from_errno();
561 *elen = strlen(buf) + 1;
562 if (*elen > maxlen) {
565 return got_error(GOT_ERR_BAD_OBJ_DATA);
568 space = strchr(buf, ' ');
570 err = got_error(GOT_ERR_BAD_OBJ_DATA);
576 if (*p < '0' && *p > '7') {
577 err = got_error(GOT_ERR_BAD_OBJ_DATA);
581 (*te)->mode |= *p - '0';
585 (*te)->name = strdup(space + 1);
586 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
587 err = got_error(GOT_ERR_BAD_OBJ_DATA);
590 buf += strlen(buf) + 1;
591 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
592 *elen += SHA1_DIGEST_LENGTH;
595 tree_entry_close(*te);
601 static const struct got_error *
602 parse_tree_object(struct got_tree_object **tree, struct got_repository *repo,
603 uint8_t *buf, size_t len)
605 const struct got_error *err;
608 *tree = calloc(1, sizeof(**tree));
610 return got_error_from_errno();
612 SIMPLEQ_INIT(&(*tree)->entries);
615 struct got_tree_entry *te;
618 err = parse_tree_entry(&te, &elen, buf, remain);
622 SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
628 got_object_tree_close(*tree);
629 return got_error(GOT_ERR_BAD_OBJ_DATA);
635 static const struct got_error *
636 read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
638 const struct got_error *err = NULL;
639 static const size_t blocksize = 512;
640 size_t n, total, remain;
646 buf = calloc(1, blocksize);
648 return got_error_from_errno();
655 newbuf = reallocarray(buf, 1, total + blocksize);
656 if (newbuf == NULL) {
657 err = got_error_from_errno();
663 n = fread(buf + total, 1, remain, f);
666 err = got_ferror(f, GOT_ERR_IO);
684 static const struct got_error *
685 read_commit_object(struct got_commit_object **commit,
686 struct got_repository *repo, struct got_object *obj, FILE *f)
688 const struct got_error *err = NULL;
692 if (obj->flags & GOT_OBJ_FLAG_PACKED)
693 err = read_to_mem(&p, &len, f);
695 err = got_inflate_to_mem(&p, &len, f);
699 if (len < obj->hdrlen + obj->size) {
700 err = got_error(GOT_ERR_BAD_OBJ_DATA);
704 /* Skip object header. */
706 err = parse_commit_object(commit, p + obj->hdrlen, len);
712 const struct got_error *
713 got_object_commit_open(struct got_commit_object **commit,
714 struct got_repository *repo, struct got_object *obj)
716 const struct got_error *err = NULL;
718 if (obj->type != GOT_OBJ_TYPE_COMMIT)
719 return got_error(GOT_ERR_OBJ_TYPE);
721 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
724 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
728 err = parse_commit_object(commit, buf, len);
733 err = open_loose_object(&fd, obj, repo);
736 f = fdopen(fd, "rb");
738 err = got_error_from_errno();
742 err = read_commit_object(commit, repo, obj, f);
749 got_object_commit_close(struct got_commit_object *commit)
751 struct got_parent_id *pid;
753 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
754 pid = SIMPLEQ_FIRST(&commit->parent_ids);
755 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
760 free(commit->tree_id);
761 free(commit->author);
762 free(commit->committer);
763 free(commit->logmsg);
767 static const struct got_error *
768 read_tree_object(struct got_tree_object **tree,
769 struct got_repository *repo, struct got_object *obj, FILE *f)
771 const struct got_error *err = NULL;
775 if (obj->flags & GOT_OBJ_FLAG_PACKED)
776 err = read_to_mem(&p, &len, f);
778 err = got_inflate_to_mem(&p, &len, f);
782 if (len < obj->hdrlen + obj->size) {
783 err = got_error(GOT_ERR_BAD_OBJ_DATA);
787 /* Skip object header. */
789 err = parse_tree_object(tree, repo, p + obj->hdrlen, len);
795 const struct got_error *
796 got_object_tree_open(struct got_tree_object **tree,
797 struct got_repository *repo, struct got_object *obj)
799 const struct got_error *err = NULL;
801 if (obj->type != GOT_OBJ_TYPE_TREE)
802 return got_error(GOT_ERR_OBJ_TYPE);
804 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
807 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
811 err = parse_tree_object(tree, repo, buf, len);
816 err = open_loose_object(&fd, obj, repo);
819 f = fdopen(fd, "rb");
822 return got_error_from_errno();
824 err = read_tree_object(tree, repo, obj, f);
832 got_object_tree_close(struct got_tree_object *tree)
834 struct got_tree_entry *te;
836 while (!SIMPLEQ_EMPTY(&tree->entries)) {
837 te = SIMPLEQ_FIRST(&tree->entries);
838 SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
839 tree_entry_close(te);
845 const struct got_error *
846 got_object_blob_open(struct got_blob_object **blob,
847 struct got_repository *repo, struct got_object *obj, size_t blocksize)
849 const struct got_error *err = NULL;
851 if (obj->type != GOT_OBJ_TYPE_BLOB)
852 return got_error(GOT_ERR_OBJ_TYPE);
854 if (blocksize < obj->hdrlen)
855 return got_error(GOT_ERR_NO_SPACE);
857 *blob = calloc(1, sizeof(**blob));
859 return got_error_from_errno();
861 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
862 (*blob)->read_buf = calloc(1, blocksize);
863 if ((*blob)->read_buf == NULL) {
864 err = got_error_from_errno();
869 err = got_packfile_extract_object(&((*blob)->f), obj, repo);
871 free((*blob)->read_buf);
878 err = open_loose_object(&fd, obj, repo);
884 (*blob)->f = fdopen(fd, "rb");
885 if ((*blob)->f == NULL) {
892 err = got_inflate_init(&(*blob)->zb, NULL, blocksize);
900 (*blob)->read_buf = (*blob)->zb.outbuf;
901 (*blob)->flags |= GOT_BLOB_F_COMPRESSED;
904 (*blob)->hdrlen = obj->hdrlen;
905 (*blob)->blocksize = blocksize;
906 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
912 got_object_blob_close(struct got_blob_object *blob)
914 if (blob->flags & GOT_BLOB_F_COMPRESSED)
915 got_inflate_end(&blob->zb);
917 free(blob->read_buf);
923 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
925 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
929 got_object_blob_get_hdrlen(struct got_blob_object *blob)
935 got_object_blob_get_read_buf(struct got_blob_object *blob)
937 return blob->read_buf;
940 const struct got_error *
941 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
945 if (blob->flags & GOT_BLOB_F_COMPRESSED)
946 return got_inflate_read(&blob->zb, blob->f, outlenp);
948 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
949 if (n == 0 && ferror(blob->f))
950 return got_ferror(blob->f, GOT_ERR_IO);