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();
258 err = read_object_header(&child_obj, f);
262 err = got_privsep_send_obj(&child_ibuf, child_obj, 0);
265 got_object_close(child_obj);
267 got_privsep_send_error(&child_ibuf, err);
272 imsg_clear(&child_ibuf);
277 imsg_init(&parent_ibuf, imsg_fds[0]);
278 err = got_privsep_recv_obj(obj, &parent_ibuf);
279 imsg_clear(&parent_ibuf);
280 waitpid(pid, &child_status, 0);
285 static const struct got_error *
286 object_path(char **path, struct got_object_id *id, struct got_repository *repo)
288 const struct got_error *err = NULL;
290 char *path_objects = got_repo_get_path_objects(repo);
294 if (path_objects == NULL)
295 return got_error_from_errno();
297 err = got_object_id_str(&hex, id);
301 if (asprintf(path, "%s/%.2x/%s", path_objects,
302 id->sha1[0], hex + 2) == -1)
303 err = got_error_from_errno();
310 static const struct got_error *
311 open_loose_object(FILE **f, struct got_object *obj, struct got_repository *repo)
313 const struct got_error *err = NULL;
316 err = object_path(&path, &obj->id, repo);
319 *f = fopen(path, "rb");
321 err = got_error_from_errno();
329 const struct got_error *
330 got_object_open(struct got_object **obj, struct got_repository *repo,
331 struct got_object_id *id)
333 const struct got_error *err = NULL;
337 err = object_path(&path, id, repo);
341 fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
343 if (errno != ENOENT) {
344 err = got_error_from_errno();
347 err = got_packfile_open_object(obj, id, repo);
351 err = got_error(GOT_ERR_NO_OBJ);
353 err = read_object_header_privsep(obj, fd);
356 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
366 const struct got_error *
367 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
370 struct got_object_id id;
372 if (!got_parse_sha1_digest(id.sha1, id_str))
373 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
375 return got_object_open(obj, repo, &id);
379 got_object_close(struct got_object *obj)
381 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
382 struct got_delta *delta;
383 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
384 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
385 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
386 got_delta_close(delta);
389 if (obj->flags & GOT_OBJ_FLAG_PACKED)
390 free(obj->path_packfile);
394 static const struct got_error *
395 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
397 const struct got_error *err = NULL;
400 ssize_t remain = (ssize_t)len;
402 *commit = calloc(1, sizeof(**commit));
404 return got_error_from_errno();
405 (*commit)->tree_id = calloc(1, sizeof(*(*commit)->tree_id));
406 if ((*commit)->tree_id == NULL) {
407 err = got_error_from_errno();
413 SIMPLEQ_INIT(&(*commit)->parent_ids);
415 tlen = strlen(GOT_COMMIT_TAG_TREE);
416 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
418 if (remain < SHA1_DIGEST_STRING_LENGTH) {
419 err = got_error(GOT_ERR_BAD_OBJ_DATA);
423 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
424 err = got_error(GOT_ERR_BAD_OBJ_DATA);
427 remain -= SHA1_DIGEST_STRING_LENGTH;
428 s += SHA1_DIGEST_STRING_LENGTH;
430 err = got_error(GOT_ERR_BAD_OBJ_DATA);
434 tlen = strlen(GOT_COMMIT_TAG_PARENT);
435 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
436 struct got_parent_id *pid;
439 if (remain < SHA1_DIGEST_STRING_LENGTH) {
440 err = got_error(GOT_ERR_BAD_OBJ_DATA);
444 pid = calloc(1, sizeof(*pid));
446 err = got_error_from_errno();
449 pid->id = calloc(1, sizeof(*pid->id));
450 if (pid->id == NULL) {
451 err = got_error_from_errno();
456 if (!got_parse_sha1_digest(pid->id->sha1, s)) {
457 err = got_error(GOT_ERR_BAD_OBJ_DATA);
462 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, pid, entry);
463 (*commit)->nparents++;
465 remain -= SHA1_DIGEST_STRING_LENGTH;
466 s += SHA1_DIGEST_STRING_LENGTH;
469 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
470 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
475 err = got_error(GOT_ERR_BAD_OBJ_DATA);
481 err = got_error(GOT_ERR_BAD_OBJ_DATA);
485 (*commit)->author = strdup(s);
486 if ((*commit)->author == NULL) {
487 err = got_error_from_errno();
490 s += strlen((*commit)->author) + 1;
491 remain -= strlen((*commit)->author) + 1;
494 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
495 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
500 err = got_error(GOT_ERR_BAD_OBJ_DATA);
506 err = got_error(GOT_ERR_BAD_OBJ_DATA);
510 (*commit)->committer = strdup(s);
511 if ((*commit)->committer == NULL) {
512 err = got_error_from_errno();
515 s += strlen((*commit)->committer) + 1;
516 remain -= strlen((*commit)->committer) + 1;
519 (*commit)->logmsg = strndup(s, remain);
520 if ((*commit)->logmsg == NULL) {
521 err = got_error_from_errno();
526 got_object_commit_close(*commit);
533 tree_entry_close(struct got_tree_entry *te)
540 static const struct got_error *
541 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
544 char *p = buf, *space;
545 const struct got_error *err = NULL;
547 *te = calloc(1, sizeof(**te));
549 return got_error_from_errno();
551 (*te)->id = calloc(1, sizeof(*(*te)->id));
552 if ((*te)->id == NULL) {
553 err = got_error_from_errno();
559 *elen = strlen(buf) + 1;
560 if (*elen > maxlen) {
563 return got_error(GOT_ERR_BAD_OBJ_DATA);
566 space = strchr(buf, ' ');
568 err = got_error(GOT_ERR_BAD_OBJ_DATA);
574 if (*p < '0' && *p > '7') {
575 err = got_error(GOT_ERR_BAD_OBJ_DATA);
579 (*te)->mode |= *p - '0';
583 (*te)->name = strdup(space + 1);
584 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
585 err = got_error(GOT_ERR_BAD_OBJ_DATA);
588 buf += strlen(buf) + 1;
589 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
590 *elen += SHA1_DIGEST_LENGTH;
593 tree_entry_close(*te);
599 static const struct got_error *
600 parse_tree_object(struct got_tree_object **tree, struct got_repository *repo,
601 uint8_t *buf, size_t len)
603 const struct got_error *err;
606 *tree = calloc(1, sizeof(**tree));
608 return got_error_from_errno();
610 SIMPLEQ_INIT(&(*tree)->entries);
613 struct got_tree_entry *te;
616 err = parse_tree_entry(&te, &elen, buf, remain);
620 SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
626 got_object_tree_close(*tree);
627 return got_error(GOT_ERR_BAD_OBJ_DATA);
633 static const struct got_error *
634 read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
636 const struct got_error *err = NULL;
637 static const size_t blocksize = 512;
638 size_t n, total, remain;
644 buf = calloc(1, blocksize);
646 return got_error_from_errno();
653 newbuf = reallocarray(buf, 1, total + blocksize);
654 if (newbuf == NULL) {
655 err = got_error_from_errno();
661 n = fread(buf + total, 1, remain, f);
664 err = got_ferror(f, GOT_ERR_IO);
682 static const struct got_error *
683 read_commit_object(struct got_commit_object **commit,
684 struct got_repository *repo, struct got_object *obj, FILE *f)
686 const struct got_error *err = NULL;
690 if (obj->flags & GOT_OBJ_FLAG_PACKED)
691 err = read_to_mem(&p, &len, f);
693 err = got_inflate_to_mem(&p, &len, f);
697 if (len < obj->hdrlen + obj->size) {
698 err = got_error(GOT_ERR_BAD_OBJ_DATA);
702 /* Skip object header. */
704 err = parse_commit_object(commit, p + obj->hdrlen, len);
710 const struct got_error *
711 got_object_commit_open(struct got_commit_object **commit,
712 struct got_repository *repo, struct got_object *obj)
714 const struct got_error *err = NULL;
716 if (obj->type != GOT_OBJ_TYPE_COMMIT)
717 return got_error(GOT_ERR_OBJ_TYPE);
719 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
722 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
726 err = parse_commit_object(commit, buf, len);
730 err = open_loose_object(&f, obj, repo);
733 err = read_commit_object(commit, repo, obj, f);
740 got_object_commit_close(struct got_commit_object *commit)
742 struct got_parent_id *pid;
744 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
745 pid = SIMPLEQ_FIRST(&commit->parent_ids);
746 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
751 free(commit->tree_id);
752 free(commit->author);
753 free(commit->committer);
754 free(commit->logmsg);
758 static const struct got_error *
759 read_tree_object(struct got_tree_object **tree,
760 struct got_repository *repo, struct got_object *obj, FILE *f)
762 const struct got_error *err = NULL;
766 if (obj->flags & GOT_OBJ_FLAG_PACKED)
767 err = read_to_mem(&p, &len, f);
769 err = got_inflate_to_mem(&p, &len, f);
773 if (len < obj->hdrlen + obj->size) {
774 err = got_error(GOT_ERR_BAD_OBJ_DATA);
778 /* Skip object header. */
780 err = parse_tree_object(tree, repo, p + obj->hdrlen, len);
786 const struct got_error *
787 got_object_tree_open(struct got_tree_object **tree,
788 struct got_repository *repo, struct got_object *obj)
790 const struct got_error *err = NULL;
792 if (obj->type != GOT_OBJ_TYPE_TREE)
793 return got_error(GOT_ERR_OBJ_TYPE);
795 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
798 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
802 err = parse_tree_object(tree, repo, buf, len);
806 err = open_loose_object(&f, obj, repo);
809 err = read_tree_object(tree, repo, obj, f);
816 got_object_tree_close(struct got_tree_object *tree)
818 struct got_tree_entry *te;
820 while (!SIMPLEQ_EMPTY(&tree->entries)) {
821 te = SIMPLEQ_FIRST(&tree->entries);
822 SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
823 tree_entry_close(te);
829 const struct got_error *
830 got_object_blob_open(struct got_blob_object **blob,
831 struct got_repository *repo, struct got_object *obj, size_t blocksize)
833 const struct got_error *err = NULL;
835 if (obj->type != GOT_OBJ_TYPE_BLOB)
836 return got_error(GOT_ERR_OBJ_TYPE);
838 if (blocksize < obj->hdrlen)
839 return got_error(GOT_ERR_NO_SPACE);
841 *blob = calloc(1, sizeof(**blob));
843 return got_error_from_errno();
845 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
846 (*blob)->read_buf = calloc(1, blocksize);
847 if ((*blob)->read_buf == NULL) {
848 err = got_error_from_errno();
853 err = got_packfile_extract_object(&((*blob)->f), obj, repo);
855 free((*blob)->read_buf);
861 err = open_loose_object(&((*blob)->f), obj, repo);
868 err = got_inflate_init(&(*blob)->zb, NULL, blocksize);
876 (*blob)->read_buf = (*blob)->zb.outbuf;
877 (*blob)->flags |= GOT_BLOB_F_COMPRESSED;
880 (*blob)->hdrlen = obj->hdrlen;
881 (*blob)->blocksize = blocksize;
882 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
888 got_object_blob_close(struct got_blob_object *blob)
890 if (blob->flags & GOT_BLOB_F_COMPRESSED)
891 got_inflate_end(&blob->zb);
893 free(blob->read_buf);
899 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
901 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
905 got_object_blob_get_hdrlen(struct got_blob_object *blob)
911 got_object_blob_get_read_buf(struct got_blob_object *blob)
913 return blob->read_buf;
916 const struct got_error *
917 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
921 if (blob->flags & GOT_BLOB_F_COMPRESSED)
922 return got_inflate_read(&blob->zb, blob->f, outlenp);
924 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
925 if (n == 0 && ferror(blob->f))
926 return got_ferror(blob->f, GOT_ERR_IO);