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.
18 #include <sys/queue.h>
29 #include "got_error.h"
30 #include "got_object.h"
31 #include "got_repository.h"
36 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
40 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
43 #define GOT_OBJ_TAG_COMMIT "commit"
44 #define GOT_OBJ_TAG_TREE "tree"
45 #define GOT_OBJ_TAG_BLOB "blob"
47 #define GOT_COMMIT_TAG_TREE "tree "
48 #define GOT_COMMIT_TAG_PARENT "parent "
49 #define GOT_COMMIT_TAG_AUTHOR "author "
50 #define GOT_COMMIT_TAG_COMMITTER "committer "
53 got_object_id_str(struct got_object_id *id, char *buf, size_t size)
55 return got_sha1_digest_to_str(id->sha1, buf, size);
59 got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
61 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
65 got_object_get_type_tag(int type)
68 case GOT_OBJ_TYPE_COMMIT:
69 return GOT_OBJ_TAG_COMMIT;
70 case GOT_OBJ_TYPE_TREE:
71 return GOT_OBJ_TAG_TREE;
72 case GOT_OBJ_TYPE_BLOB:
73 return GOT_OBJ_TAG_BLOB;
80 inflate_end(struct got_zstream_buf *zb)
87 static const struct got_error *
88 inflate_init(struct got_zstream_buf *zb, size_t bufsize)
90 const struct got_error *err = NULL;
92 memset(zb, 0, sizeof(*zb));
94 zb->z.zalloc = Z_NULL;
96 if (inflateInit(&zb->z) != Z_OK) {
97 err = got_error(GOT_ERR_IO);
101 zb->inlen = zb->outlen = bufsize;
103 zb->inbuf = calloc(1, zb->inlen);
104 if (zb->inbuf == NULL) {
105 err = got_error(GOT_ERR_NO_MEM);
109 zb->outbuf = calloc(1, zb->outlen);
110 if (zb->outbuf == NULL) {
111 err = got_error(GOT_ERR_NO_MEM);
121 static const struct got_error *
122 inflate_read(struct got_zstream_buf *zb, FILE *f, size_t *outlenp)
124 size_t last_total_out = zb->z.total_out;
125 z_stream *z = &zb->z;
128 z->next_out = zb->outbuf;
129 z->avail_out = zb->outlen;
132 if (z->avail_in == 0) {
134 n = fread(zb->inbuf, 1, zb->inlen, f);
137 return got_ferror(f, GOT_ERR_IO);
141 z->next_in = zb->inbuf;
144 ret = inflate(z, Z_SYNC_FLUSH);
145 } while (ret == Z_OK && z->avail_out > 0);
148 if (ret != Z_STREAM_END)
149 return got_error(GOT_ERR_DECOMPRESSION);
150 zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
153 *outlenp = z->total_out - last_total_out;
157 static const struct got_error *
158 parse_object_header(struct got_object **obj, char *buf, size_t len)
160 const char *obj_tags[] = {
165 const int obj_types[] = {
171 size_t size = 0, hdrlen = 0;
173 char *p = strchr(buf, '\0');
176 return got_error(GOT_ERR_BAD_OBJ_HDR);
178 hdrlen = strlen(buf) + 1 /* '\0' */;
180 for (i = 0; i < nitems(obj_tags); i++) {
181 const char *tag = obj_tags[i];
182 size_t tlen = strlen(tag);
185 if (strncmp(buf, tag, tlen) != 0)
190 return got_error(GOT_ERR_BAD_OBJ_HDR);
191 size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
193 return got_error(GOT_ERR_BAD_OBJ_HDR);
198 return got_error(GOT_ERR_BAD_OBJ_HDR);
200 *obj = calloc(1, sizeof(**obj));
202 (*obj)->hdrlen = hdrlen;
207 static const struct got_error *
208 read_object_header(struct got_object **obj, struct got_repository *repo,
211 const struct got_error *err;
212 struct got_zstream_buf zb;
215 const size_t zbsize = 64;
216 size_t outlen, totlen;
219 buf = calloc(zbsize, sizeof(char));
221 return got_error(GOT_ERR_NO_MEM);
223 err = inflate_init(&zb, zbsize);
230 err = inflate_read(&zb, f, &outlen);
233 if (strchr(zb.outbuf, '\0') == NULL) {
234 buf = recallocarray(buf, 1 + i, 2 + i, zbsize);
236 err = got_error(GOT_ERR_NO_MEM);
240 memcpy(buf + totlen, zb.outbuf, outlen);
243 } while (strchr(zb.outbuf, '\0') == NULL);
245 err = parse_object_header(obj, buf, totlen);
251 static const struct got_error *
252 object_path(char **path, struct got_object_id *id, struct got_repository *repo)
254 const struct got_error *err = NULL;
255 char hex[SHA1_DIGEST_STRING_LENGTH];
256 char *path_objects = got_repo_get_path_objects(repo);
258 if (path_objects == NULL)
259 return got_error(GOT_ERR_NO_MEM);
261 got_object_id_str(id, hex, sizeof(hex));
263 if (asprintf(path, "%s/%.2x/%s", path_objects,
264 id->sha1[0], hex + 2) == -1)
265 err = got_error(GOT_ERR_NO_MEM);
271 const struct got_error *
272 open_object(FILE **f, struct got_object *obj, struct got_repository *repo)
274 const struct got_error *err = NULL;
277 if (obj->flags & GOT_OBJ_FLAG_PACKED)
278 return got_packfile_extract_object(f, obj, repo);
280 err = object_path(&path, &obj->id, repo);
283 *f = fopen(path, "rb");
285 err = got_error_from_errno();
293 const struct got_error *
294 got_object_open(struct got_object **obj, struct got_repository *repo,
295 struct got_object_id *id)
297 const struct got_error *err = NULL;
301 err = object_path(&path, id, repo);
305 f = fopen(path, "rb");
307 if (errno != ENOENT) {
308 err = got_error_from_errno();
311 err = got_packfile_open_object(obj, id, repo);
315 err = got_error(GOT_ERR_NO_OBJ);
317 err = read_object_header(obj, repo, f);
320 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
331 got_object_close(struct got_object *obj)
333 free(obj->path_packfile);
338 commit_object_valid(struct got_commit_object *commit)
347 for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
348 if (commit->tree_id.sha1[i] == 0)
351 if (n == SHA1_DIGEST_LENGTH)
357 static const struct got_error *
358 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
360 const struct got_error *err = NULL;
363 ssize_t remain = (ssize_t)len;
365 *commit = calloc(1, sizeof(**commit));
367 return got_error(GOT_ERR_NO_MEM);
369 SIMPLEQ_INIT(&(*commit)->parent_ids);
371 tlen = strlen(GOT_COMMIT_TAG_TREE);
372 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
374 if (remain < SHA1_DIGEST_STRING_LENGTH) {
375 err = got_error(GOT_ERR_BAD_OBJ_DATA);
379 if (!got_parse_sha1_digest((*commit)->tree_id.sha1, s)) {
380 err = got_error(GOT_ERR_BAD_OBJ_DATA);
383 remain -= SHA1_DIGEST_STRING_LENGTH;
384 s += SHA1_DIGEST_STRING_LENGTH;
386 err = got_error(GOT_ERR_BAD_OBJ_DATA);
390 tlen = strlen(GOT_COMMIT_TAG_PARENT);
391 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
392 struct got_parent_id *pid;
395 if (remain < SHA1_DIGEST_STRING_LENGTH) {
396 err = got_error(GOT_ERR_BAD_OBJ_DATA);
400 pid = calloc(1, sizeof(*pid));
402 err = got_error(GOT_ERR_NO_MEM);
406 if (!got_parse_sha1_digest(pid->id.sha1, s)) {
407 err = got_error(GOT_ERR_BAD_OBJ_DATA);
410 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, pid, entry);
411 (*commit)->nparents++;
413 s += SHA1_DIGEST_STRING_LENGTH;
416 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
417 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
422 err = got_error(GOT_ERR_BAD_OBJ_DATA);
428 err = got_error(GOT_ERR_BAD_OBJ_DATA);
432 (*commit)->author = strdup(s);
433 if ((*commit)->author == NULL) {
434 err = got_error(GOT_ERR_NO_MEM);
437 s += strlen((*commit)->author) + 1;
440 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
441 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
446 err = got_error(GOT_ERR_BAD_OBJ_DATA);
452 err = got_error(GOT_ERR_BAD_OBJ_DATA);
456 (*commit)->committer = strdup(s);
457 if ((*commit)->committer == NULL) {
458 err = got_error(GOT_ERR_NO_MEM);
461 s += strlen((*commit)->committer) + 1;
464 (*commit)->logmsg = strdup(s);
467 got_object_commit_close(*commit);
472 tree_entry_close(struct got_tree_entry *te)
478 static const struct got_error *
479 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
482 char *p = buf, *space;
483 const struct got_error *err = NULL;
484 char hex[SHA1_DIGEST_STRING_LENGTH];
486 *te = calloc(1, sizeof(**te));
488 return got_error(GOT_ERR_NO_MEM);
490 *elen = strlen(buf) + 1;
491 if (*elen > maxlen) {
493 return got_error(GOT_ERR_BAD_OBJ_DATA);
496 space = strchr(buf, ' ');
499 return got_error(GOT_ERR_BAD_OBJ_DATA);
502 if (*p < '0' && *p > '7') {
503 err = got_error(GOT_ERR_BAD_OBJ_DATA);
507 (*te)->mode |= *p - '0';
511 (*te)->name = strdup(space + 1);
512 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
513 err = got_error(GOT_ERR_BAD_OBJ_DATA);
516 buf += strlen(buf) + 1;
517 memcpy((*te)->id.sha1, buf, SHA1_DIGEST_LENGTH);
518 *elen += SHA1_DIGEST_LENGTH;
521 tree_entry_close(*te);
525 static const struct got_error *
526 parse_tree_object(struct got_tree_object **tree, struct got_repository *repo,
527 char *buf, size_t len)
532 *tree = calloc(1, sizeof(**tree));
534 return got_error(GOT_ERR_NO_MEM);
536 SIMPLEQ_INIT(&(*tree)->entries);
539 struct got_tree_entry *te;
542 parse_tree_entry(&te, &elen, buf, remain);
544 SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
550 got_object_tree_close(*tree);
551 return got_error(GOT_ERR_BAD_OBJ_DATA);
557 static const struct got_error *
558 read_commit_object(struct got_commit_object **commit,
559 struct got_repository *repo, struct got_object *obj, FILE *f)
561 const struct got_error *err = NULL;
562 struct got_zstream_buf zb;
567 err = inflate_init(&zb, 8192);
572 err = inflate_read(&zb, f, &len);
575 } while (len < obj->hdrlen + obj->size);
577 if (len < obj->hdrlen + obj->size) {
578 err = got_error(GOT_ERR_BAD_OBJ_DATA);
582 /* Skip object header. */
584 err = parse_commit_object(commit, zb.outbuf + obj->hdrlen, len);
590 const struct got_error *
591 got_object_commit_open(struct got_commit_object **commit,
592 struct got_repository *repo, struct got_object *obj)
594 const struct got_error *err = NULL;
597 if (obj->type != GOT_OBJ_TYPE_COMMIT)
598 return got_error(GOT_ERR_OBJ_TYPE);
600 err = open_object(&f, obj, repo);
604 err = read_commit_object(commit, repo, obj, f);
610 got_object_commit_close(struct got_commit_object *commit)
612 struct got_parent_id *pid;
614 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
615 pid = SIMPLEQ_FIRST(&commit->parent_ids);
616 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
620 free(commit->author);
621 free(commit->committer);
622 free(commit->logmsg);
626 static const struct got_error *
627 read_tree_object(struct got_tree_object **tree,
628 struct got_repository *repo, struct got_object *obj, FILE *f)
630 const struct got_error *err = NULL;
631 struct got_zstream_buf zb;
636 err = inflate_init(&zb, 8192);
641 err = inflate_read(&zb, f, &len);
644 } while (len < obj->hdrlen + obj->size);
646 if (len < obj->hdrlen + obj->size) {
647 err = got_error(GOT_ERR_BAD_OBJ_DATA);
651 /* Skip object header. */
653 err = parse_tree_object(tree, repo, zb.outbuf + obj->hdrlen, len);
659 const struct got_error *
660 got_object_tree_open(struct got_tree_object **tree,
661 struct got_repository *repo, struct got_object *obj)
663 const struct got_error *err = NULL;
666 if (obj->type != GOT_OBJ_TYPE_TREE)
667 return got_error(GOT_ERR_OBJ_TYPE);
669 err = open_object(&f, obj, repo);
673 err = read_tree_object(tree, repo, obj, f);
679 got_object_tree_close(struct got_tree_object *tree)
681 struct got_tree_entry *te;
683 while (!SIMPLEQ_EMPTY(&tree->entries)) {
684 te = SIMPLEQ_FIRST(&tree->entries);
685 SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
686 tree_entry_close(te);
692 const struct got_error *
693 got_object_blob_open(struct got_blob_object **blob,
694 struct got_repository *repo, struct got_object *obj, size_t blocksize)
696 const struct got_error *err = NULL;
698 if (obj->type != GOT_OBJ_TYPE_BLOB)
699 return got_error(GOT_ERR_OBJ_TYPE);
701 if (blocksize < obj->hdrlen)
702 return got_error(GOT_ERR_NO_SPACE);
704 *blob = calloc(1, sizeof(**blob));
706 return got_error(GOT_ERR_NO_MEM);
708 err = open_object(&((*blob)->f), obj, repo);
714 err = inflate_init(&(*blob)->zb, blocksize);
721 (*blob)->hdrlen = obj->hdrlen;
722 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
728 got_object_blob_close(struct got_blob_object *blob)
730 inflate_end(&blob->zb);
735 const struct got_error *
736 got_object_blob_read_block(struct got_blob_object *blob, size_t *outlenp)
738 return inflate_read(&blob->zb, blob->f, outlenp);