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);
240 memcpy(buf, zb.outbuf, outlen);
243 } while (strchr(zb.outbuf, '\0') == NULL);
245 err = parse_object_header(obj, buf, totlen);
252 static const struct got_error *
253 object_path(char **path, struct got_object_id *id,
254 struct got_repository *repo)
256 const struct got_error *err = NULL;
257 char hex[SHA1_DIGEST_STRING_LENGTH];
258 char *path_objects = got_repo_get_path_objects(repo);
260 if (path_objects == NULL)
261 return got_error(GOT_ERR_NO_MEM);
263 got_object_id_str(id, hex, sizeof(hex));
265 if (asprintf(path, "%s/%.2x/%s", path_objects,
266 id->sha1[0], hex + 2) == -1)
267 err = got_error(GOT_ERR_NO_MEM);
273 const struct got_error *
274 got_object_open(struct got_object **obj, struct got_repository *repo,
275 struct got_object_id *id)
277 const struct got_error *err = NULL;
280 err = object_path(&path, id, repo);
284 err = read_object_header(obj, repo, path);
286 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
293 got_object_close(struct got_object *obj)
299 commit_object_valid(struct got_commit_object *commit)
308 for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
309 if (commit->tree_id.sha1[i] == 0)
312 if (n == SHA1_DIGEST_LENGTH)
318 static const struct got_error *
319 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
321 const struct got_error *err = NULL;
324 ssize_t remain = (ssize_t)len;
326 *commit = calloc(1, sizeof(**commit));
328 return got_error(GOT_ERR_NO_MEM);
330 SIMPLEQ_INIT(&(*commit)->parent_ids);
332 tlen = strlen(GOT_COMMIT_TAG_TREE);
333 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
335 if (remain < SHA1_DIGEST_STRING_LENGTH) {
336 err = got_error(GOT_ERR_BAD_OBJ_DATA);
340 if (!got_parse_sha1_digest((*commit)->tree_id.sha1, s)) {
341 err = got_error(GOT_ERR_BAD_OBJ_DATA);
344 remain -= SHA1_DIGEST_STRING_LENGTH;
345 s += SHA1_DIGEST_STRING_LENGTH;
347 err = got_error(GOT_ERR_BAD_OBJ_DATA);
351 tlen = strlen(GOT_COMMIT_TAG_PARENT);
352 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
353 struct got_parent_id *pid;
356 if (remain < SHA1_DIGEST_STRING_LENGTH) {
357 err = got_error(GOT_ERR_BAD_OBJ_DATA);
361 pid = calloc(1, sizeof(*pid));
363 err = got_error(GOT_ERR_NO_MEM);
367 if (!got_parse_sha1_digest(pid->id.sha1, s)) {
368 err = got_error(GOT_ERR_BAD_OBJ_DATA);
371 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, pid, entry);
372 (*commit)->nparents++;
374 s += SHA1_DIGEST_STRING_LENGTH;
377 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
378 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
383 err = got_error(GOT_ERR_BAD_OBJ_DATA);
389 err = got_error(GOT_ERR_BAD_OBJ_DATA);
393 (*commit)->author = strdup(s);
394 if ((*commit)->author == NULL) {
395 err = got_error(GOT_ERR_NO_MEM);
398 s += strlen((*commit)->author) + 1;
401 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
402 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
407 err = got_error(GOT_ERR_BAD_OBJ_DATA);
413 err = got_error(GOT_ERR_BAD_OBJ_DATA);
417 (*commit)->committer = strdup(s);
418 if ((*commit)->committer == NULL) {
419 err = got_error(GOT_ERR_NO_MEM);
422 s += strlen((*commit)->committer) + 1;
425 (*commit)->logmsg = strdup(s);
428 got_object_commit_close(*commit);
433 tree_entry_close(struct got_tree_entry *te)
439 static const struct got_error *
440 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
443 char *p = buf, *space;
444 const struct got_error *err = NULL;
445 char hex[SHA1_DIGEST_STRING_LENGTH];
447 *te = calloc(1, sizeof(**te));
449 return got_error(GOT_ERR_NO_MEM);
451 *elen = strlen(buf) + 1;
452 if (*elen > maxlen) {
454 return got_error(GOT_ERR_BAD_OBJ_DATA);
457 space = strchr(buf, ' ');
460 return got_error(GOT_ERR_BAD_OBJ_DATA);
463 if (*p < '0' && *p > '7') {
464 err = got_error(GOT_ERR_BAD_OBJ_DATA);
468 (*te)->mode |= *p - '0';
472 (*te)->name = strdup(space + 1);
473 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
474 err = got_error(GOT_ERR_BAD_OBJ_DATA);
477 buf += strlen(buf) + 1;
478 memcpy((*te)->id.sha1, buf, SHA1_DIGEST_LENGTH);
479 *elen += SHA1_DIGEST_LENGTH;
482 tree_entry_close(*te);
486 static const struct got_error *
487 parse_tree_object(struct got_tree_object **tree, struct got_repository *repo,
488 char *buf, size_t len)
493 *tree = calloc(1, sizeof(**tree));
495 return got_error(GOT_ERR_NO_MEM);
497 SIMPLEQ_INIT(&(*tree)->entries);
500 struct got_tree_entry *te;
503 parse_tree_entry(&te, &elen, buf, remain);
505 SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
511 got_object_tree_close(*tree);
512 return got_error(GOT_ERR_BAD_OBJ_DATA);
518 static const struct got_error *
519 read_commit_object(struct got_commit_object **commit,
520 struct got_repository *repo, struct got_object *obj, const char *path)
522 const struct got_error *err = NULL;
524 struct got_zstream_buf zb;
529 f = fopen(path, "rb");
531 return got_error(GOT_ERR_BAD_PATH);
533 err = inflate_init(&zb, 8192);
540 err = inflate_read(&zb, f, &len);
543 } while (len < obj->hdrlen + obj->size);
545 if (len < obj->hdrlen + obj->size) {
546 err = got_error(GOT_ERR_BAD_OBJ_DATA);
550 /* Skip object header. */
552 err = parse_commit_object(commit, zb.outbuf + obj->hdrlen, len);
559 const struct got_error *
560 got_object_commit_open(struct got_commit_object **commit,
561 struct got_repository *repo, struct got_object *obj)
563 const struct got_error *err = NULL;
566 if (obj->type != GOT_OBJ_TYPE_COMMIT)
567 return got_error(GOT_ERR_OBJ_TYPE);
569 err = object_path(&path, &obj->id, repo);
573 err = read_commit_object(commit, repo, obj, path);
579 got_object_commit_close(struct got_commit_object *commit)
581 struct got_parent_id *pid;
583 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
584 pid = SIMPLEQ_FIRST(&commit->parent_ids);
585 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
589 free(commit->author);
590 free(commit->committer);
591 free(commit->logmsg);
595 static const struct got_error *
596 read_tree_object(struct got_tree_object **tree,
597 struct got_repository *repo, struct got_object *obj, const char *path)
599 const struct got_error *err = NULL;
601 struct got_zstream_buf zb;
606 f = fopen(path, "rb");
608 return got_error(GOT_ERR_BAD_PATH);
610 err = inflate_init(&zb, 8192);
617 err = inflate_read(&zb, f, &len);
620 } while (len < obj->hdrlen + obj->size);
622 if (len < obj->hdrlen + obj->size) {
623 err = got_error(GOT_ERR_BAD_OBJ_DATA);
627 /* Skip object header. */
629 err = parse_tree_object(tree, repo, zb.outbuf + obj->hdrlen, len);
636 const struct got_error *
637 got_object_tree_open(struct got_tree_object **tree,
638 struct got_repository *repo, struct got_object *obj)
640 const struct got_error *err = NULL;
643 if (obj->type != GOT_OBJ_TYPE_TREE)
644 return got_error(GOT_ERR_OBJ_TYPE);
646 err = object_path(&path, &obj->id, repo);
650 err = read_tree_object(tree, repo, obj, path);
656 got_object_tree_close(struct got_tree_object *tree)
658 struct got_tree_entry *te;
660 while (!SIMPLEQ_EMPTY(&tree->entries)) {
661 te = SIMPLEQ_FIRST(&tree->entries);
662 SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
663 tree_entry_close(te);
669 const struct got_error *
670 got_object_blob_open(struct got_blob_object **blob,
671 struct got_repository *repo, struct got_object *obj, size_t blocksize)
673 const struct got_error *err = NULL;
676 if (obj->type != GOT_OBJ_TYPE_BLOB)
677 return got_error(GOT_ERR_OBJ_TYPE);
679 if (blocksize < obj->hdrlen)
680 return got_error(GOT_ERR_NO_SPACE);
682 err = object_path(&path, &obj->id, repo);
686 *blob = calloc(1, sizeof(**blob));
689 return got_error(GOT_ERR_NO_MEM);
692 (*blob)->f = fopen(path, "rb");
693 if ((*blob)->f == NULL) {
696 return got_error(GOT_ERR_BAD_PATH);
699 err = inflate_init(&(*blob)->zb, blocksize);
707 (*blob)->hdrlen = obj->hdrlen;
708 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
715 got_object_blob_close(struct got_blob_object *blob)
717 inflate_end(&blob->zb);
722 const struct got_error *
723 got_object_blob_read_block(struct got_blob_object *blob, size_t *outlenp)
725 return inflate_read(&blob->zb, blob->f, outlenp);