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;
209 size_t outlen, totlen;
212 f = fopen(path, "rb");
214 return got_error(GOT_ERR_BAD_PATH);
216 buf = calloc(zbsize, sizeof(char));
218 return got_error(GOT_ERR_NO_MEM);
220 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);
235 err = got_error(GOT_ERR_NO_MEM);
239 memcpy(buf + totlen, zb.outbuf, outlen);
242 } while (strchr(zb.outbuf, '\0') == NULL);
244 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;
279 err = object_path(&path, id, repo);
283 err = read_object_header(obj, repo, path);
285 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
292 got_object_close(struct got_object *obj)
298 commit_object_valid(struct got_commit_object *commit)
307 for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
308 if (commit->tree_id.sha1[i] == 0)
311 if (n == SHA1_DIGEST_LENGTH)
317 static const struct got_error *
318 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
320 const struct got_error *err = NULL;
323 ssize_t remain = (ssize_t)len;
325 *commit = calloc(1, sizeof(**commit));
327 return got_error(GOT_ERR_NO_MEM);
329 SIMPLEQ_INIT(&(*commit)->parent_ids);
331 tlen = strlen(GOT_COMMIT_TAG_TREE);
332 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
334 if (remain < SHA1_DIGEST_STRING_LENGTH) {
335 err = got_error(GOT_ERR_BAD_OBJ_DATA);
339 if (!got_parse_sha1_digest((*commit)->tree_id.sha1, s)) {
340 err = got_error(GOT_ERR_BAD_OBJ_DATA);
343 remain -= SHA1_DIGEST_STRING_LENGTH;
344 s += SHA1_DIGEST_STRING_LENGTH;
346 err = got_error(GOT_ERR_BAD_OBJ_DATA);
350 tlen = strlen(GOT_COMMIT_TAG_PARENT);
351 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
352 struct got_parent_id *pid;
355 if (remain < SHA1_DIGEST_STRING_LENGTH) {
356 err = got_error(GOT_ERR_BAD_OBJ_DATA);
360 pid = calloc(1, sizeof(*pid));
362 err = got_error(GOT_ERR_NO_MEM);
366 if (!got_parse_sha1_digest(pid->id.sha1, s)) {
367 err = got_error(GOT_ERR_BAD_OBJ_DATA);
370 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, pid, entry);
371 (*commit)->nparents++;
373 s += SHA1_DIGEST_STRING_LENGTH;
376 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
377 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
382 err = got_error(GOT_ERR_BAD_OBJ_DATA);
388 err = got_error(GOT_ERR_BAD_OBJ_DATA);
392 (*commit)->author = strdup(s);
393 if ((*commit)->author == NULL) {
394 err = got_error(GOT_ERR_NO_MEM);
397 s += strlen((*commit)->author) + 1;
400 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
401 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
406 err = got_error(GOT_ERR_BAD_OBJ_DATA);
412 err = got_error(GOT_ERR_BAD_OBJ_DATA);
416 (*commit)->committer = strdup(s);
417 if ((*commit)->committer == NULL) {
418 err = got_error(GOT_ERR_NO_MEM);
421 s += strlen((*commit)->committer) + 1;
424 (*commit)->logmsg = strdup(s);
427 got_object_commit_close(*commit);
432 tree_entry_close(struct got_tree_entry *te)
438 static const struct got_error *
439 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
442 char *p = buf, *space;
443 const struct got_error *err = NULL;
444 char hex[SHA1_DIGEST_STRING_LENGTH];
446 *te = calloc(1, sizeof(**te));
448 return got_error(GOT_ERR_NO_MEM);
450 *elen = strlen(buf) + 1;
451 if (*elen > maxlen) {
453 return got_error(GOT_ERR_BAD_OBJ_DATA);
456 space = strchr(buf, ' ');
459 return got_error(GOT_ERR_BAD_OBJ_DATA);
462 if (*p < '0' && *p > '7') {
463 err = got_error(GOT_ERR_BAD_OBJ_DATA);
467 (*te)->mode |= *p - '0';
471 (*te)->name = strdup(space + 1);
472 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
473 err = got_error(GOT_ERR_BAD_OBJ_DATA);
476 buf += strlen(buf) + 1;
477 memcpy((*te)->id.sha1, buf, SHA1_DIGEST_LENGTH);
478 *elen += SHA1_DIGEST_LENGTH;
481 tree_entry_close(*te);
485 static const struct got_error *
486 parse_tree_object(struct got_tree_object **tree, struct got_repository *repo,
487 char *buf, size_t len)
492 *tree = calloc(1, sizeof(**tree));
494 return got_error(GOT_ERR_NO_MEM);
496 SIMPLEQ_INIT(&(*tree)->entries);
499 struct got_tree_entry *te;
502 parse_tree_entry(&te, &elen, buf, remain);
504 SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
510 got_object_tree_close(*tree);
511 return got_error(GOT_ERR_BAD_OBJ_DATA);
517 static const struct got_error *
518 read_commit_object(struct got_commit_object **commit,
519 struct got_repository *repo, struct got_object *obj, const char *path)
521 const struct got_error *err = NULL;
523 struct got_zstream_buf zb;
528 f = fopen(path, "rb");
530 return got_error(GOT_ERR_BAD_PATH);
532 err = inflate_init(&zb, 8192);
539 err = inflate_read(&zb, f, &len);
542 } while (len < obj->hdrlen + obj->size);
544 if (len < obj->hdrlen + obj->size) {
545 err = got_error(GOT_ERR_BAD_OBJ_DATA);
549 /* Skip object header. */
551 err = parse_commit_object(commit, zb.outbuf + obj->hdrlen, len);
558 const struct got_error *
559 got_object_commit_open(struct got_commit_object **commit,
560 struct got_repository *repo, struct got_object *obj)
562 const struct got_error *err = NULL;
565 if (obj->type != GOT_OBJ_TYPE_COMMIT)
566 return got_error(GOT_ERR_OBJ_TYPE);
568 err = object_path(&path, &obj->id, repo);
572 err = read_commit_object(commit, repo, obj, path);
578 got_object_commit_close(struct got_commit_object *commit)
580 struct got_parent_id *pid;
582 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
583 pid = SIMPLEQ_FIRST(&commit->parent_ids);
584 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
588 free(commit->author);
589 free(commit->committer);
590 free(commit->logmsg);
594 static const struct got_error *
595 read_tree_object(struct got_tree_object **tree,
596 struct got_repository *repo, struct got_object *obj, const char *path)
598 const struct got_error *err = NULL;
600 struct got_zstream_buf zb;
605 f = fopen(path, "rb");
607 return got_error(GOT_ERR_BAD_PATH);
609 err = inflate_init(&zb, 8192);
616 err = inflate_read(&zb, f, &len);
619 } while (len < obj->hdrlen + obj->size);
621 if (len < obj->hdrlen + obj->size) {
622 err = got_error(GOT_ERR_BAD_OBJ_DATA);
626 /* Skip object header. */
628 err = parse_tree_object(tree, repo, zb.outbuf + obj->hdrlen, len);
635 const struct got_error *
636 got_object_tree_open(struct got_tree_object **tree,
637 struct got_repository *repo, struct got_object *obj)
639 const struct got_error *err = NULL;
642 if (obj->type != GOT_OBJ_TYPE_TREE)
643 return got_error(GOT_ERR_OBJ_TYPE);
645 err = object_path(&path, &obj->id, repo);
649 err = read_tree_object(tree, repo, obj, path);
655 got_object_tree_close(struct got_tree_object *tree)
657 struct got_tree_entry *te;
659 while (!SIMPLEQ_EMPTY(&tree->entries)) {
660 te = SIMPLEQ_FIRST(&tree->entries);
661 SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
662 tree_entry_close(te);
668 const struct got_error *
669 got_object_blob_open(struct got_blob_object **blob,
670 struct got_repository *repo, struct got_object *obj, size_t blocksize)
672 const struct got_error *err = NULL;
675 if (obj->type != GOT_OBJ_TYPE_BLOB)
676 return got_error(GOT_ERR_OBJ_TYPE);
678 if (blocksize < obj->hdrlen)
679 return got_error(GOT_ERR_NO_SPACE);
681 err = object_path(&path, &obj->id, repo);
685 *blob = calloc(1, sizeof(**blob));
688 return got_error(GOT_ERR_NO_MEM);
691 (*blob)->f = fopen(path, "rb");
692 if ((*blob)->f == NULL) {
695 return got_error(GOT_ERR_BAD_PATH);
698 err = inflate_init(&(*blob)->zb, blocksize);
706 (*blob)->hdrlen = obj->hdrlen;
707 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
714 got_object_blob_close(struct got_blob_object *blob)
716 inflate_end(&blob->zb);
721 const struct got_error *
722 got_object_blob_read_block(struct got_blob_object *blob, size_t *outlenp)
724 return inflate_read(&blob->zb, blob->f, outlenp);