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_error(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,
253 struct got_repository *repo)
255 const struct got_error *err = NULL;
256 char hex[SHA1_DIGEST_STRING_LENGTH];
257 char *path_objects = got_repo_get_path_objects(repo);
259 if (path_objects == NULL)
260 return got_error(GOT_ERR_NO_MEM);
262 got_object_id_str(id, hex, sizeof(hex));
264 if (asprintf(path, "%s/%.2x/%s", path_objects,
265 id->sha1[0], hex + 2) == -1)
266 err = got_error(GOT_ERR_NO_MEM);
272 const struct got_error *
273 got_object_open(struct got_object **obj, struct got_repository *repo,
274 struct got_object_id *id)
276 const struct got_error *err = NULL;
280 err = object_path(&path, id, repo);
284 f = fopen(path, "rb");
286 if (errno != ENOENT) {
287 err = got_error(GOT_ERR_BAD_PATH);
290 err = got_packfile_extract_object(&f, id, repo);
294 err = got_error(GOT_ERR_NO_OBJ);
299 err = read_object_header(obj, repo, f);
301 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
310 got_object_close(struct got_object *obj)
316 commit_object_valid(struct got_commit_object *commit)
325 for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
326 if (commit->tree_id.sha1[i] == 0)
329 if (n == SHA1_DIGEST_LENGTH)
335 static const struct got_error *
336 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
338 const struct got_error *err = NULL;
341 ssize_t remain = (ssize_t)len;
343 *commit = calloc(1, sizeof(**commit));
345 return got_error(GOT_ERR_NO_MEM);
347 SIMPLEQ_INIT(&(*commit)->parent_ids);
349 tlen = strlen(GOT_COMMIT_TAG_TREE);
350 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
352 if (remain < SHA1_DIGEST_STRING_LENGTH) {
353 err = got_error(GOT_ERR_BAD_OBJ_DATA);
357 if (!got_parse_sha1_digest((*commit)->tree_id.sha1, s)) {
358 err = got_error(GOT_ERR_BAD_OBJ_DATA);
361 remain -= SHA1_DIGEST_STRING_LENGTH;
362 s += SHA1_DIGEST_STRING_LENGTH;
364 err = got_error(GOT_ERR_BAD_OBJ_DATA);
368 tlen = strlen(GOT_COMMIT_TAG_PARENT);
369 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
370 struct got_parent_id *pid;
373 if (remain < SHA1_DIGEST_STRING_LENGTH) {
374 err = got_error(GOT_ERR_BAD_OBJ_DATA);
378 pid = calloc(1, sizeof(*pid));
380 err = got_error(GOT_ERR_NO_MEM);
384 if (!got_parse_sha1_digest(pid->id.sha1, s)) {
385 err = got_error(GOT_ERR_BAD_OBJ_DATA);
388 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, pid, entry);
389 (*commit)->nparents++;
391 s += SHA1_DIGEST_STRING_LENGTH;
394 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
395 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
400 err = got_error(GOT_ERR_BAD_OBJ_DATA);
406 err = got_error(GOT_ERR_BAD_OBJ_DATA);
410 (*commit)->author = strdup(s);
411 if ((*commit)->author == NULL) {
412 err = got_error(GOT_ERR_NO_MEM);
415 s += strlen((*commit)->author) + 1;
418 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
419 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
424 err = got_error(GOT_ERR_BAD_OBJ_DATA);
430 err = got_error(GOT_ERR_BAD_OBJ_DATA);
434 (*commit)->committer = strdup(s);
435 if ((*commit)->committer == NULL) {
436 err = got_error(GOT_ERR_NO_MEM);
439 s += strlen((*commit)->committer) + 1;
442 (*commit)->logmsg = strdup(s);
445 got_object_commit_close(*commit);
450 tree_entry_close(struct got_tree_entry *te)
456 static const struct got_error *
457 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
460 char *p = buf, *space;
461 const struct got_error *err = NULL;
462 char hex[SHA1_DIGEST_STRING_LENGTH];
464 *te = calloc(1, sizeof(**te));
466 return got_error(GOT_ERR_NO_MEM);
468 *elen = strlen(buf) + 1;
469 if (*elen > maxlen) {
471 return got_error(GOT_ERR_BAD_OBJ_DATA);
474 space = strchr(buf, ' ');
477 return got_error(GOT_ERR_BAD_OBJ_DATA);
480 if (*p < '0' && *p > '7') {
481 err = got_error(GOT_ERR_BAD_OBJ_DATA);
485 (*te)->mode |= *p - '0';
489 (*te)->name = strdup(space + 1);
490 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
491 err = got_error(GOT_ERR_BAD_OBJ_DATA);
494 buf += strlen(buf) + 1;
495 memcpy((*te)->id.sha1, buf, SHA1_DIGEST_LENGTH);
496 *elen += SHA1_DIGEST_LENGTH;
499 tree_entry_close(*te);
503 static const struct got_error *
504 parse_tree_object(struct got_tree_object **tree, struct got_repository *repo,
505 char *buf, size_t len)
510 *tree = calloc(1, sizeof(**tree));
512 return got_error(GOT_ERR_NO_MEM);
514 SIMPLEQ_INIT(&(*tree)->entries);
517 struct got_tree_entry *te;
520 parse_tree_entry(&te, &elen, buf, remain);
522 SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
528 got_object_tree_close(*tree);
529 return got_error(GOT_ERR_BAD_OBJ_DATA);
535 static const struct got_error *
536 read_commit_object(struct got_commit_object **commit,
537 struct got_repository *repo, struct got_object *obj, const char *path)
539 const struct got_error *err = NULL;
541 struct got_zstream_buf zb;
546 f = fopen(path, "rb");
548 return got_error(GOT_ERR_BAD_PATH);
550 err = inflate_init(&zb, 8192);
557 err = inflate_read(&zb, f, &len);
560 } while (len < obj->hdrlen + obj->size);
562 if (len < obj->hdrlen + obj->size) {
563 err = got_error(GOT_ERR_BAD_OBJ_DATA);
567 /* Skip object header. */
569 err = parse_commit_object(commit, zb.outbuf + obj->hdrlen, len);
576 const struct got_error *
577 got_object_commit_open(struct got_commit_object **commit,
578 struct got_repository *repo, struct got_object *obj)
580 const struct got_error *err = NULL;
583 if (obj->type != GOT_OBJ_TYPE_COMMIT)
584 return got_error(GOT_ERR_OBJ_TYPE);
586 err = object_path(&path, &obj->id, repo);
590 err = read_commit_object(commit, repo, obj, path);
596 got_object_commit_close(struct got_commit_object *commit)
598 struct got_parent_id *pid;
600 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
601 pid = SIMPLEQ_FIRST(&commit->parent_ids);
602 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
606 free(commit->author);
607 free(commit->committer);
608 free(commit->logmsg);
612 static const struct got_error *
613 read_tree_object(struct got_tree_object **tree,
614 struct got_repository *repo, struct got_object *obj, const char *path)
616 const struct got_error *err = NULL;
618 struct got_zstream_buf zb;
623 f = fopen(path, "rb");
625 return got_error(GOT_ERR_BAD_PATH);
627 err = inflate_init(&zb, 8192);
634 err = inflate_read(&zb, f, &len);
637 } while (len < obj->hdrlen + obj->size);
639 if (len < obj->hdrlen + obj->size) {
640 err = got_error(GOT_ERR_BAD_OBJ_DATA);
644 /* Skip object header. */
646 err = parse_tree_object(tree, repo, zb.outbuf + obj->hdrlen, len);
653 const struct got_error *
654 got_object_tree_open(struct got_tree_object **tree,
655 struct got_repository *repo, struct got_object *obj)
657 const struct got_error *err = NULL;
660 if (obj->type != GOT_OBJ_TYPE_TREE)
661 return got_error(GOT_ERR_OBJ_TYPE);
663 err = object_path(&path, &obj->id, repo);
667 err = read_tree_object(tree, repo, obj, path);
673 got_object_tree_close(struct got_tree_object *tree)
675 struct got_tree_entry *te;
677 while (!SIMPLEQ_EMPTY(&tree->entries)) {
678 te = SIMPLEQ_FIRST(&tree->entries);
679 SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
680 tree_entry_close(te);
686 const struct got_error *
687 got_object_blob_open(struct got_blob_object **blob,
688 struct got_repository *repo, struct got_object *obj, size_t blocksize)
690 const struct got_error *err = NULL;
693 if (obj->type != GOT_OBJ_TYPE_BLOB)
694 return got_error(GOT_ERR_OBJ_TYPE);
696 if (blocksize < obj->hdrlen)
697 return got_error(GOT_ERR_NO_SPACE);
699 err = object_path(&path, &obj->id, repo);
703 *blob = calloc(1, sizeof(**blob));
706 return got_error(GOT_ERR_NO_MEM);
709 (*blob)->f = fopen(path, "rb");
710 if ((*blob)->f == NULL) {
713 return got_error(GOT_ERR_BAD_PATH);
716 err = inflate_init(&(*blob)->zb, blocksize);
724 (*blob)->hdrlen = obj->hdrlen;
725 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
732 got_object_blob_close(struct got_blob_object *blob)
734 inflate_end(&blob->zb);
739 const struct got_error *
740 got_object_blob_read_block(struct got_blob_object *blob, size_t *outlenp)
742 return inflate_read(&blob->zb, blob->f, outlenp);