2 * Copyright (c) 2017 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>
28 #include "got_error.h"
29 #include "got_object.h"
30 #include "got_repository.h"
34 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
38 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
41 #define GOT_OBJ_TAG_COMMIT "commit"
42 #define GOT_OBJ_TAG_TREE "tree"
43 #define GOT_OBJ_TAG_BLOB "blob"
45 #define GOT_COMMIT_TAG_TREE "tree "
46 #define GOT_COMMIT_TAG_PARENT "parent "
47 #define GOT_COMMIT_TAG_AUTHOR "author "
48 #define GOT_COMMIT_TAG_COMMITTER "committer "
51 got_object_id_str(struct got_object_id *id, char *buf, size_t size)
57 if (size < SHA1_DIGEST_STRING_LENGTH)
60 for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
61 snprintf(hex, sizeof(hex), "%.2x", id->sha1[i]);
72 inflate_end(struct got_zstream_buf *zb)
79 static const struct got_error *
80 inflate_init(struct got_zstream_buf *zb, size_t bufsize)
82 const struct got_error *err = NULL;
84 memset(zb, 0, sizeof(*zb));
86 zb->z.zalloc = Z_NULL;
88 if (inflateInit(&zb->z) != Z_OK) {
89 err = got_error(GOT_ERR_IO);
93 zb->inlen = zb->outlen = bufsize;
95 zb->inbuf = calloc(1, zb->inlen);
96 if (zb->inbuf == NULL) {
97 err = got_error(GOT_ERR_NO_MEM);
101 zb->outbuf = calloc(1, zb->outlen);
102 if (zb->outbuf == NULL) {
103 err = got_error(GOT_ERR_NO_MEM);
113 static const struct got_error *
114 inflate_read(struct got_zstream_buf *zb, FILE *f, size_t *outlenp)
116 size_t last_total_out = zb->z.total_out;
117 z_stream *z = &zb->z;
120 z->next_out = zb->outbuf;
121 z->avail_out = zb->outlen;
124 if (z->avail_in == 0) {
126 n = fread(zb->inbuf, 1, zb->inlen, f);
129 return got_error(GOT_ERR_IO);
133 z->next_in = zb->inbuf;
136 ret = inflate(z, Z_SYNC_FLUSH);
137 } while (ret == Z_OK && z->avail_out > 0);
140 if (ret != Z_STREAM_END)
141 return got_error(GOT_ERR_DECOMPRESSION);
142 zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
145 *outlenp = z->total_out - last_total_out;
149 static const struct got_error *
150 parse_object_header(struct got_object **obj, char *buf, size_t len)
152 const char *obj_tags[] = {
157 const int obj_types[] = {
163 size_t size = 0, hdrlen = 0;
165 char *p = strchr(buf, '\0');
168 return got_error(GOT_ERR_BAD_OBJ_HDR);
170 hdrlen = strlen(buf) + 1 /* '\0' */;
172 for (i = 0; i < nitems(obj_tags); i++) {
173 const char *tag = obj_tags[i];
174 size_t tlen = strlen(tag);
177 if (strncmp(buf, tag, tlen) != 0)
182 return got_error(GOT_ERR_BAD_OBJ_HDR);
183 size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
185 return got_error(GOT_ERR_BAD_OBJ_HDR);
190 return got_error(GOT_ERR_BAD_OBJ_HDR);
192 *obj = calloc(1, sizeof(**obj));
194 (*obj)->hdrlen = hdrlen;
199 static const struct got_error *
200 read_object_header(struct got_object **obj, struct got_repository *repo,
203 const struct got_error *err;
205 struct got_zstream_buf zb;
208 const size_t zbsize = 64;
212 f = fopen(path, "rb");
214 return got_error(GOT_ERR_BAD_PATH);
217 buf = calloc(totalsz, sizeof(char));
219 return got_error(GOT_ERR_NO_MEM);
221 err = inflate_init(&zb, zbsize);
229 err = inflate_read(&zb, f, &outlen);
232 if (strchr(zb.outbuf, '\0') == NULL) {
233 buf = recallocarray(buf, 1 + i, 2 + i, zbsize);
236 memcpy(buf, zb.outbuf, zbsize);
238 } while (strchr(zb.outbuf, '\0') == NULL);
240 err = parse_object_header(obj, buf, totalsz);
247 static const struct got_error *
248 object_path(char **path, struct got_object_id *id,
249 struct got_repository *repo)
251 const struct got_error *err = NULL;
252 char hex[SHA1_DIGEST_STRING_LENGTH];
253 char *path_objects = got_repo_get_path_objects(repo);
255 if (path_objects == NULL)
256 return got_error(GOT_ERR_NO_MEM);
258 got_object_id_str(id, hex, sizeof(hex));
260 if (asprintf(path, "%s/%.2x/%s", path_objects,
261 id->sha1[0], hex + 2) == -1)
262 err = got_error(GOT_ERR_NO_MEM);
268 const struct got_error *
269 got_object_open(struct got_object **obj, struct got_repository *repo,
270 struct got_object_id *id)
272 const struct got_error *err = NULL;
275 err = object_path(&path, id, repo);
279 err = read_object_header(obj, repo, path);
281 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
288 got_object_close(struct got_object *obj)
294 commit_object_valid(struct got_commit_object *commit)
303 for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
304 if (commit->tree_id.sha1[i] == 0)
307 if (n == SHA1_DIGEST_LENGTH)
313 static const struct got_error *
314 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
316 const struct got_error *err = NULL;
319 ssize_t remain = (ssize_t)len;
321 *commit = calloc(1, sizeof(**commit));
323 return got_error(GOT_ERR_NO_MEM);
325 SIMPLEQ_INIT(&(*commit)->parent_ids);
327 tlen = strlen(GOT_COMMIT_TAG_TREE);
328 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
330 if (remain < SHA1_DIGEST_STRING_LENGTH) {
331 err = got_error(GOT_ERR_BAD_OBJ_DATA);
335 if (!got_parse_sha1_digest((*commit)->tree_id.sha1, s)) {
336 err = got_error(GOT_ERR_BAD_OBJ_DATA);
339 remain -= SHA1_DIGEST_STRING_LENGTH;
340 s += SHA1_DIGEST_STRING_LENGTH;
342 err = got_error(GOT_ERR_BAD_OBJ_DATA);
346 tlen = strlen(GOT_COMMIT_TAG_PARENT);
347 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
348 struct got_parent_id *pid;
351 if (remain < SHA1_DIGEST_STRING_LENGTH) {
352 err = got_error(GOT_ERR_BAD_OBJ_DATA);
356 pid = calloc(1, sizeof(*pid));
358 err = got_error(GOT_ERR_NO_MEM);
362 if (!got_parse_sha1_digest(pid->id.sha1, s)) {
363 err = got_error(GOT_ERR_BAD_OBJ_DATA);
366 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, pid, entry);
367 (*commit)->nparents++;
369 s += SHA1_DIGEST_STRING_LENGTH;
372 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
373 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
378 err = got_error(GOT_ERR_BAD_OBJ_DATA);
384 err = got_error(GOT_ERR_BAD_OBJ_DATA);
388 (*commit)->author = strdup(s);
389 if ((*commit)->author == NULL) {
390 err = got_error(GOT_ERR_NO_MEM);
393 s += strlen((*commit)->author) + 1;
396 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
397 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
402 err = got_error(GOT_ERR_BAD_OBJ_DATA);
408 err = got_error(GOT_ERR_BAD_OBJ_DATA);
412 (*commit)->committer = strdup(s);
413 if ((*commit)->committer == NULL) {
414 err = got_error(GOT_ERR_NO_MEM);
417 s += strlen((*commit)->committer) + 1;
420 (*commit)->logmsg = strdup(s);
423 got_object_commit_close(*commit);
428 tree_entry_close(struct got_tree_entry *te)
434 static const struct got_error *
435 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
438 char *p = buf, *space;
439 const struct got_error *err = NULL;
440 char hex[SHA1_DIGEST_STRING_LENGTH];
442 *te = calloc(1, sizeof(**te));
444 return got_error(GOT_ERR_NO_MEM);
446 *elen = strlen(buf) + 1;
447 if (*elen > maxlen) {
449 return got_error(GOT_ERR_BAD_OBJ_DATA);
452 space = strchr(buf, ' ');
455 return got_error(GOT_ERR_BAD_OBJ_DATA);
458 if (*p < '0' && *p > '7') {
459 err = got_error(GOT_ERR_BAD_OBJ_DATA);
463 (*te)->mode |= *p - '0';
467 (*te)->name = strdup(space + 1);
468 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
469 err = got_error(GOT_ERR_BAD_OBJ_DATA);
472 buf += strlen(buf) + 1;
473 memcpy((*te)->id.sha1, buf, SHA1_DIGEST_LENGTH);
474 *elen += SHA1_DIGEST_LENGTH;
477 tree_entry_close(*te);
481 static const struct got_error *
482 parse_tree_object(struct got_tree_object **tree, struct got_repository *repo,
483 char *buf, size_t len)
488 *tree = calloc(1, sizeof(**tree));
490 return got_error(GOT_ERR_NO_MEM);
492 SIMPLEQ_INIT(&(*tree)->entries);
495 struct got_tree_entry *te;
498 parse_tree_entry(&te, &elen, buf, remain);
500 SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
506 got_object_tree_close(*tree);
507 return got_error(GOT_ERR_BAD_OBJ_DATA);
513 static const struct got_error *
514 read_commit_object(struct got_commit_object **commit,
515 struct got_repository *repo, struct got_object *obj, const char *path)
517 const struct got_error *err = NULL;
519 struct got_zstream_buf zb;
524 f = fopen(path, "rb");
526 return got_error(GOT_ERR_BAD_PATH);
528 err = inflate_init(&zb, 8192);
535 err = inflate_read(&zb, f, &len);
538 } while (len < obj->hdrlen + obj->size);
540 if (len < obj->hdrlen + obj->size) {
541 err = got_error(GOT_ERR_BAD_OBJ_DATA);
545 /* Skip object header. */
547 err = parse_commit_object(commit, zb.outbuf + obj->hdrlen, len);
554 const struct got_error *
555 got_object_commit_open(struct got_commit_object **commit,
556 struct got_repository *repo, struct got_object *obj)
558 const struct got_error *err = NULL;
561 if (obj->type != GOT_OBJ_TYPE_COMMIT)
562 return got_error(GOT_ERR_OBJ_TYPE);
564 err = object_path(&path, &obj->id, repo);
568 err = read_commit_object(commit, repo, obj, path);
574 got_object_commit_close(struct got_commit_object *commit)
576 struct got_parent_id *pid;
578 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
579 pid = SIMPLEQ_FIRST(&commit->parent_ids);
580 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
584 free(commit->author);
585 free(commit->committer);
586 free(commit->logmsg);
590 static const struct got_error *
591 read_tree_object(struct got_tree_object **tree,
592 struct got_repository *repo, struct got_object *obj, const char *path)
594 const struct got_error *err = NULL;
596 struct got_zstream_buf zb;
601 f = fopen(path, "rb");
603 return got_error(GOT_ERR_BAD_PATH);
605 err = inflate_init(&zb, 8192);
612 err = inflate_read(&zb, f, &len);
615 } while (len < obj->hdrlen + obj->size);
617 if (len < obj->hdrlen + obj->size) {
618 err = got_error(GOT_ERR_BAD_OBJ_DATA);
622 /* Skip object header. */
624 err = parse_tree_object(tree, repo, zb.outbuf + obj->hdrlen, len);
631 const struct got_error *
632 got_object_tree_open(struct got_tree_object **tree,
633 struct got_repository *repo, struct got_object *obj)
635 const struct got_error *err = NULL;
638 if (obj->type != GOT_OBJ_TYPE_TREE)
639 return got_error(GOT_ERR_OBJ_TYPE);
641 err = object_path(&path, &obj->id, repo);
645 err = read_tree_object(tree, repo, obj, path);
651 got_object_tree_close(struct got_tree_object *tree)
653 struct got_tree_entry *te;
655 while (!SIMPLEQ_EMPTY(&tree->entries)) {
656 te = SIMPLEQ_FIRST(&tree->entries);
657 SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
658 tree_entry_close(te);
664 const struct got_error *
665 got_object_blob_open(struct got_blob_object **blob,
666 struct got_repository *repo, struct got_object *obj, size_t blocksize)
668 const struct got_error *err = NULL;
671 if (obj->type != GOT_OBJ_TYPE_BLOB)
672 return got_error(GOT_ERR_OBJ_TYPE);
674 if (blocksize < obj->hdrlen)
675 return got_error(GOT_ERR_NO_SPACE);
677 err = object_path(&path, &obj->id, repo);
681 *blob = calloc(1, sizeof(**blob));
684 return got_error(GOT_ERR_NO_MEM);
687 (*blob)->f = fopen(path, "rb");
688 if ((*blob)->f == NULL) {
691 return got_error(GOT_ERR_BAD_PATH);
694 err = inflate_init(&(*blob)->zb, blocksize);
702 (*blob)->hdrlen = obj->hdrlen;
703 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
710 got_object_blob_close(struct got_blob_object *blob)
712 inflate_end(&blob->zb);
717 const struct got_error *
718 got_object_blob_read_block(struct got_blob_object *blob, size_t *outlenp)
720 return inflate_read(&blob->zb, blob->f, outlenp);