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 static const struct got_error *
403 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
405 const struct got_error *err = NULL;
408 ssize_t remain = (ssize_t)len;
410 *commit = calloc(1, sizeof(**commit));
412 return got_error_from_errno();
413 (*commit)->tree_id = calloc(1, sizeof(*(*commit)->tree_id));
414 if ((*commit)->tree_id == NULL) {
415 err = got_error_from_errno();
421 SIMPLEQ_INIT(&(*commit)->parent_ids);
423 tlen = strlen(GOT_COMMIT_TAG_TREE);
424 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
426 if (remain < SHA1_DIGEST_STRING_LENGTH) {
427 err = got_error(GOT_ERR_BAD_OBJ_DATA);
431 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
432 err = got_error(GOT_ERR_BAD_OBJ_DATA);
435 remain -= SHA1_DIGEST_STRING_LENGTH;
436 s += SHA1_DIGEST_STRING_LENGTH;
438 err = got_error(GOT_ERR_BAD_OBJ_DATA);
442 tlen = strlen(GOT_COMMIT_TAG_PARENT);
443 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
444 struct got_parent_id *pid;
447 if (remain < SHA1_DIGEST_STRING_LENGTH) {
448 err = got_error(GOT_ERR_BAD_OBJ_DATA);
452 pid = calloc(1, sizeof(*pid));
454 err = got_error_from_errno();
457 pid->id = calloc(1, sizeof(*pid->id));
458 if (pid->id == NULL) {
459 err = got_error_from_errno();
464 if (!got_parse_sha1_digest(pid->id->sha1, s)) {
465 err = got_error(GOT_ERR_BAD_OBJ_DATA);
470 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, pid, entry);
471 (*commit)->nparents++;
473 remain -= SHA1_DIGEST_STRING_LENGTH;
474 s += SHA1_DIGEST_STRING_LENGTH;
477 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
478 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
483 err = got_error(GOT_ERR_BAD_OBJ_DATA);
489 err = got_error(GOT_ERR_BAD_OBJ_DATA);
493 (*commit)->author = strdup(s);
494 if ((*commit)->author == NULL) {
495 err = got_error_from_errno();
498 s += strlen((*commit)->author) + 1;
499 remain -= strlen((*commit)->author) + 1;
502 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
503 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
508 err = got_error(GOT_ERR_BAD_OBJ_DATA);
514 err = got_error(GOT_ERR_BAD_OBJ_DATA);
518 (*commit)->committer = strdup(s);
519 if ((*commit)->committer == NULL) {
520 err = got_error_from_errno();
523 s += strlen((*commit)->committer) + 1;
524 remain -= strlen((*commit)->committer) + 1;
527 (*commit)->logmsg = strndup(s, remain);
528 if ((*commit)->logmsg == NULL) {
529 err = got_error_from_errno();
534 got_object_commit_close(*commit);
541 tree_entry_close(struct got_tree_entry *te)
548 static const struct got_error *
549 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
552 char *p = buf, *space;
553 const struct got_error *err = NULL;
555 *te = calloc(1, sizeof(**te));
557 return got_error_from_errno();
559 (*te)->id = calloc(1, sizeof(*(*te)->id));
560 if ((*te)->id == NULL) {
561 err = got_error_from_errno();
567 *elen = strlen(buf) + 1;
568 if (*elen > maxlen) {
571 return got_error(GOT_ERR_BAD_OBJ_DATA);
574 space = strchr(buf, ' ');
576 err = got_error(GOT_ERR_BAD_OBJ_DATA);
582 if (*p < '0' && *p > '7') {
583 err = got_error(GOT_ERR_BAD_OBJ_DATA);
587 (*te)->mode |= *p - '0';
591 (*te)->name = strdup(space + 1);
592 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
593 err = got_error(GOT_ERR_BAD_OBJ_DATA);
596 buf += strlen(buf) + 1;
597 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
598 *elen += SHA1_DIGEST_LENGTH;
601 tree_entry_close(*te);
607 static const struct got_error *
608 parse_tree_object(struct got_tree_object **tree, struct got_repository *repo,
609 uint8_t *buf, size_t len)
611 const struct got_error *err;
614 *tree = calloc(1, sizeof(**tree));
616 return got_error_from_errno();
618 SIMPLEQ_INIT(&(*tree)->entries);
621 struct got_tree_entry *te;
624 err = parse_tree_entry(&te, &elen, buf, remain);
628 SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
634 got_object_tree_close(*tree);
635 return got_error(GOT_ERR_BAD_OBJ_DATA);
641 static const struct got_error *
642 read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
644 const struct got_error *err = NULL;
645 static const size_t blocksize = 512;
646 size_t n, total, remain;
652 buf = calloc(1, blocksize);
654 return got_error_from_errno();
661 newbuf = reallocarray(buf, 1, total + blocksize);
662 if (newbuf == NULL) {
663 err = got_error_from_errno();
669 n = fread(buf + total, 1, remain, f);
672 err = got_ferror(f, GOT_ERR_IO);
690 static const struct got_error *
691 read_commit_object(struct got_commit_object **commit,
692 struct got_repository *repo, struct got_object *obj, FILE *f)
694 const struct got_error *err = NULL;
698 if (obj->flags & GOT_OBJ_FLAG_PACKED)
699 err = read_to_mem(&p, &len, f);
701 err = got_inflate_to_mem(&p, &len, f);
705 if (len < obj->hdrlen + obj->size) {
706 err = got_error(GOT_ERR_BAD_OBJ_DATA);
710 /* Skip object header. */
712 err = parse_commit_object(commit, p + obj->hdrlen, len);
718 const struct got_error *
719 got_object_commit_open(struct got_commit_object **commit,
720 struct got_repository *repo, struct got_object *obj)
722 const struct got_error *err = NULL;
724 if (obj->type != GOT_OBJ_TYPE_COMMIT)
725 return got_error(GOT_ERR_OBJ_TYPE);
727 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
730 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
734 err = parse_commit_object(commit, buf, len);
739 err = open_loose_object(&fd, obj, repo);
742 f = fdopen(fd, "rb");
744 err = got_error_from_errno();
748 err = read_commit_object(commit, repo, obj, f);
755 got_object_commit_close(struct got_commit_object *commit)
757 struct got_parent_id *pid;
759 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
760 pid = SIMPLEQ_FIRST(&commit->parent_ids);
761 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
766 free(commit->tree_id);
767 free(commit->author);
768 free(commit->committer);
769 free(commit->logmsg);
773 static const struct got_error *
774 read_tree_object(struct got_tree_object **tree,
775 struct got_repository *repo, struct got_object *obj, FILE *f)
777 const struct got_error *err = NULL;
781 if (obj->flags & GOT_OBJ_FLAG_PACKED)
782 err = read_to_mem(&p, &len, f);
784 err = got_inflate_to_mem(&p, &len, f);
788 if (len < obj->hdrlen + obj->size) {
789 err = got_error(GOT_ERR_BAD_OBJ_DATA);
793 /* Skip object header. */
795 err = parse_tree_object(tree, repo, p + obj->hdrlen, len);
801 const struct got_error *
802 got_object_tree_open(struct got_tree_object **tree,
803 struct got_repository *repo, struct got_object *obj)
805 const struct got_error *err = NULL;
807 if (obj->type != GOT_OBJ_TYPE_TREE)
808 return got_error(GOT_ERR_OBJ_TYPE);
810 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
813 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
817 err = parse_tree_object(tree, repo, buf, len);
822 err = open_loose_object(&fd, obj, repo);
825 f = fdopen(fd, "rb");
828 return got_error_from_errno();
830 err = read_tree_object(tree, repo, obj, f);
838 got_object_tree_close(struct got_tree_object *tree)
840 struct got_tree_entry *te;
842 while (!SIMPLEQ_EMPTY(&tree->entries)) {
843 te = SIMPLEQ_FIRST(&tree->entries);
844 SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
845 tree_entry_close(te);
851 const struct got_error *
852 got_object_blob_open(struct got_blob_object **blob,
853 struct got_repository *repo, struct got_object *obj, size_t blocksize)
855 const struct got_error *err = NULL;
857 if (obj->type != GOT_OBJ_TYPE_BLOB)
858 return got_error(GOT_ERR_OBJ_TYPE);
860 if (blocksize < obj->hdrlen)
861 return got_error(GOT_ERR_NO_SPACE);
863 *blob = calloc(1, sizeof(**blob));
865 return got_error_from_errno();
867 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
868 (*blob)->read_buf = calloc(1, blocksize);
869 if ((*blob)->read_buf == NULL) {
870 err = got_error_from_errno();
875 err = got_packfile_extract_object(&((*blob)->f), obj, repo);
877 free((*blob)->read_buf);
884 err = open_loose_object(&fd, obj, repo);
890 (*blob)->f = fdopen(fd, "rb");
891 if ((*blob)->f == NULL) {
898 err = got_inflate_init(&(*blob)->zb, NULL, blocksize);
906 (*blob)->read_buf = (*blob)->zb.outbuf;
907 (*blob)->flags |= GOT_BLOB_F_COMPRESSED;
910 (*blob)->hdrlen = obj->hdrlen;
911 (*blob)->blocksize = blocksize;
912 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
918 got_object_blob_close(struct got_blob_object *blob)
920 if (blob->flags & GOT_BLOB_F_COMPRESSED)
921 got_inflate_end(&blob->zb);
923 free(blob->read_buf);
929 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
931 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
935 got_object_blob_get_hdrlen(struct got_blob_object *blob)
941 got_object_blob_get_read_buf(struct got_blob_object *blob)
943 return blob->read_buf;
946 const struct got_error *
947 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
951 if (blob->flags & GOT_BLOB_F_COMPRESSED)
952 return got_inflate_read(&blob->zb, blob->f, outlenp);
954 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
955 if (n == 0 && ferror(blob->f))
956 return got_ferror(blob->f, GOT_ERR_IO);