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"
38 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
42 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
45 #define GOT_OBJ_TAG_COMMIT "commit"
46 #define GOT_OBJ_TAG_TREE "tree"
47 #define GOT_OBJ_TAG_BLOB "blob"
49 #define GOT_COMMIT_TAG_TREE "tree "
50 #define GOT_COMMIT_TAG_PARENT "parent "
51 #define GOT_COMMIT_TAG_AUTHOR "author "
52 #define GOT_COMMIT_TAG_COMMITTER "committer "
55 got_object_id_str(struct got_object_id *id, char *buf, size_t size)
57 return got_sha1_digest_to_str(id->sha1, buf, size);
61 got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
63 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
67 got_object_get_type(struct got_object *obj)
70 case GOT_OBJ_TYPE_COMMIT:
71 case GOT_OBJ_TYPE_TREE:
72 case GOT_OBJ_TYPE_BLOB:
73 case GOT_OBJ_TYPE_TAG:
85 inflate_end(struct got_zstream_buf *zb)
92 static const struct got_error *
93 inflate_init(struct got_zstream_buf *zb, size_t bufsize)
95 const struct got_error *err = NULL;
97 memset(zb, 0, sizeof(*zb));
99 zb->z.zalloc = Z_NULL;
100 zb->z.zfree = Z_NULL;
101 if (inflateInit(&zb->z) != Z_OK) {
102 err = got_error(GOT_ERR_IO);
106 zb->inlen = zb->outlen = bufsize;
108 zb->inbuf = calloc(1, zb->inlen);
109 if (zb->inbuf == NULL) {
110 err = got_error(GOT_ERR_NO_MEM);
114 zb->outbuf = calloc(1, zb->outlen);
115 if (zb->outbuf == NULL) {
116 err = got_error(GOT_ERR_NO_MEM);
126 static const struct got_error *
127 inflate_read(struct got_zstream_buf *zb, FILE *f, size_t *outlenp)
129 size_t last_total_out = zb->z.total_out;
130 z_stream *z = &zb->z;
133 z->next_out = zb->outbuf;
134 z->avail_out = zb->outlen;
137 if (z->avail_in == 0) {
139 n = fread(zb->inbuf, 1, zb->inlen, f);
142 return got_ferror(f, GOT_ERR_IO);
146 z->next_in = zb->inbuf;
149 ret = inflate(z, Z_SYNC_FLUSH);
150 } while (ret == Z_OK && z->avail_out > 0);
153 if (ret != Z_STREAM_END)
154 return got_error(GOT_ERR_DECOMPRESSION);
155 zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
158 *outlenp = z->total_out - last_total_out;
162 static const struct got_error *
163 parse_object_header(struct got_object **obj, char *buf, size_t len)
165 const char *obj_tags[] = {
170 const int obj_types[] = {
176 size_t size = 0, hdrlen = 0;
178 char *p = strchr(buf, '\0');
181 return got_error(GOT_ERR_BAD_OBJ_HDR);
183 hdrlen = strlen(buf) + 1 /* '\0' */;
185 for (i = 0; i < nitems(obj_tags); i++) {
186 const char *tag = obj_tags[i];
187 size_t tlen = strlen(tag);
190 if (strncmp(buf, tag, tlen) != 0)
195 return got_error(GOT_ERR_BAD_OBJ_HDR);
196 size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
198 return got_error(GOT_ERR_BAD_OBJ_HDR);
203 return got_error(GOT_ERR_BAD_OBJ_HDR);
205 *obj = calloc(1, sizeof(**obj));
207 (*obj)->hdrlen = hdrlen;
212 static const struct got_error *
213 read_object_header(struct got_object **obj, struct got_repository *repo,
216 const struct got_error *err;
217 struct got_zstream_buf zb;
220 const size_t zbsize = 64;
221 size_t outlen, totlen;
224 buf = calloc(zbsize, sizeof(char));
226 return got_error(GOT_ERR_NO_MEM);
228 err = inflate_init(&zb, zbsize);
235 err = inflate_read(&zb, f, &outlen);
238 if (strchr(zb.outbuf, '\0') == NULL) {
239 buf = recallocarray(buf, 1 + i, 2 + i, zbsize);
241 err = got_error(GOT_ERR_NO_MEM);
245 memcpy(buf + totlen, zb.outbuf, outlen);
248 } while (strchr(zb.outbuf, '\0') == NULL);
250 err = parse_object_header(obj, buf, totlen);
256 static const struct got_error *
257 object_path(char **path, struct got_object_id *id, struct got_repository *repo)
259 const struct got_error *err = NULL;
260 char hex[SHA1_DIGEST_STRING_LENGTH];
261 char *path_objects = got_repo_get_path_objects(repo);
263 if (path_objects == NULL)
264 return got_error(GOT_ERR_NO_MEM);
266 got_object_id_str(id, hex, sizeof(hex));
268 if (asprintf(path, "%s/%.2x/%s", path_objects,
269 id->sha1[0], hex + 2) == -1)
270 err = got_error(GOT_ERR_NO_MEM);
276 const struct got_error *
277 open_object(FILE **f, struct got_object *obj, struct got_repository *repo)
279 const struct got_error *err = NULL;
282 if (obj->flags & GOT_OBJ_FLAG_PACKED)
283 return got_packfile_extract_object(f, obj, repo);
285 err = object_path(&path, &obj->id, repo);
288 *f = fopen(path, "rb");
290 err = got_error_from_errno();
298 const struct got_error *
299 got_object_open(struct got_object **obj, struct got_repository *repo,
300 struct got_object_id *id)
302 const struct got_error *err = NULL;
306 err = object_path(&path, id, repo);
310 f = fopen(path, "rb");
312 if (errno != ENOENT) {
313 err = got_error_from_errno();
316 err = got_packfile_open_object(obj, id, repo);
320 err = got_error(GOT_ERR_NO_OBJ);
322 err = read_object_header(obj, repo, f);
325 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
336 got_object_close(struct got_object *obj)
338 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
339 struct got_delta *delta;
340 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
341 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
342 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
343 got_delta_close(delta);
346 if (obj->flags & GOT_OBJ_FLAG_PACKED)
347 free(obj->path_packfile);
352 commit_object_valid(struct got_commit_object *commit)
361 for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
362 if (commit->tree_id.sha1[i] == 0)
365 if (n == SHA1_DIGEST_LENGTH)
371 static const struct got_error *
372 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
374 const struct got_error *err = NULL;
377 ssize_t remain = (ssize_t)len;
379 *commit = calloc(1, sizeof(**commit));
381 return got_error(GOT_ERR_NO_MEM);
383 SIMPLEQ_INIT(&(*commit)->parent_ids);
385 tlen = strlen(GOT_COMMIT_TAG_TREE);
386 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
388 if (remain < SHA1_DIGEST_STRING_LENGTH) {
389 err = got_error(GOT_ERR_BAD_OBJ_DATA);
393 if (!got_parse_sha1_digest((*commit)->tree_id.sha1, s)) {
394 err = got_error(GOT_ERR_BAD_OBJ_DATA);
397 remain -= SHA1_DIGEST_STRING_LENGTH;
398 s += SHA1_DIGEST_STRING_LENGTH;
400 err = got_error(GOT_ERR_BAD_OBJ_DATA);
404 tlen = strlen(GOT_COMMIT_TAG_PARENT);
405 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
406 struct got_parent_id *pid;
409 if (remain < SHA1_DIGEST_STRING_LENGTH) {
410 err = got_error(GOT_ERR_BAD_OBJ_DATA);
414 pid = calloc(1, sizeof(*pid));
416 err = got_error(GOT_ERR_NO_MEM);
420 if (!got_parse_sha1_digest(pid->id.sha1, s)) {
421 err = got_error(GOT_ERR_BAD_OBJ_DATA);
424 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, pid, entry);
425 (*commit)->nparents++;
427 s += SHA1_DIGEST_STRING_LENGTH;
430 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
431 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
436 err = got_error(GOT_ERR_BAD_OBJ_DATA);
442 err = got_error(GOT_ERR_BAD_OBJ_DATA);
446 (*commit)->author = strdup(s);
447 if ((*commit)->author == NULL) {
448 err = got_error(GOT_ERR_NO_MEM);
451 s += strlen((*commit)->author) + 1;
454 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
455 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
460 err = got_error(GOT_ERR_BAD_OBJ_DATA);
466 err = got_error(GOT_ERR_BAD_OBJ_DATA);
470 (*commit)->committer = strdup(s);
471 if ((*commit)->committer == NULL) {
472 err = got_error(GOT_ERR_NO_MEM);
475 s += strlen((*commit)->committer) + 1;
478 (*commit)->logmsg = strdup(s);
481 got_object_commit_close(*commit);
486 tree_entry_close(struct got_tree_entry *te)
492 static const struct got_error *
493 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
496 char *p = buf, *space;
497 const struct got_error *err = NULL;
498 char hex[SHA1_DIGEST_STRING_LENGTH];
500 *te = calloc(1, sizeof(**te));
502 return got_error(GOT_ERR_NO_MEM);
504 *elen = strlen(buf) + 1;
505 if (*elen > maxlen) {
507 return got_error(GOT_ERR_BAD_OBJ_DATA);
510 space = strchr(buf, ' ');
513 return got_error(GOT_ERR_BAD_OBJ_DATA);
516 if (*p < '0' && *p > '7') {
517 err = got_error(GOT_ERR_BAD_OBJ_DATA);
521 (*te)->mode |= *p - '0';
525 (*te)->name = strdup(space + 1);
526 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
527 err = got_error(GOT_ERR_BAD_OBJ_DATA);
530 buf += strlen(buf) + 1;
531 memcpy((*te)->id.sha1, buf, SHA1_DIGEST_LENGTH);
532 *elen += SHA1_DIGEST_LENGTH;
535 tree_entry_close(*te);
539 static const struct got_error *
540 parse_tree_object(struct got_tree_object **tree, struct got_repository *repo,
541 char *buf, size_t len)
546 *tree = calloc(1, sizeof(**tree));
548 return got_error(GOT_ERR_NO_MEM);
550 SIMPLEQ_INIT(&(*tree)->entries);
553 struct got_tree_entry *te;
556 parse_tree_entry(&te, &elen, buf, remain);
558 SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
564 got_object_tree_close(*tree);
565 return got_error(GOT_ERR_BAD_OBJ_DATA);
571 static const struct got_error *
572 read_commit_object(struct got_commit_object **commit,
573 struct got_repository *repo, struct got_object *obj, FILE *f)
575 const struct got_error *err = NULL;
576 struct got_zstream_buf zb;
581 err = inflate_init(&zb, 8192);
586 err = inflate_read(&zb, f, &len);
589 } while (len < obj->hdrlen + obj->size);
591 if (len < obj->hdrlen + obj->size) {
592 err = got_error(GOT_ERR_BAD_OBJ_DATA);
596 /* Skip object header. */
598 err = parse_commit_object(commit, zb.outbuf + obj->hdrlen, len);
604 const struct got_error *
605 got_object_commit_open(struct got_commit_object **commit,
606 struct got_repository *repo, struct got_object *obj)
608 const struct got_error *err = NULL;
611 if (obj->type != GOT_OBJ_TYPE_COMMIT)
612 return got_error(GOT_ERR_OBJ_TYPE);
614 err = open_object(&f, obj, repo);
618 err = read_commit_object(commit, repo, obj, f);
624 got_object_commit_close(struct got_commit_object *commit)
626 struct got_parent_id *pid;
628 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
629 pid = SIMPLEQ_FIRST(&commit->parent_ids);
630 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
634 free(commit->author);
635 free(commit->committer);
636 free(commit->logmsg);
640 static const struct got_error *
641 read_tree_object(struct got_tree_object **tree,
642 struct got_repository *repo, struct got_object *obj, FILE *f)
644 const struct got_error *err = NULL;
645 struct got_zstream_buf zb;
650 err = inflate_init(&zb, 8192);
655 err = inflate_read(&zb, f, &len);
658 } while (len < obj->hdrlen + obj->size);
660 if (len < obj->hdrlen + obj->size) {
661 err = got_error(GOT_ERR_BAD_OBJ_DATA);
665 /* Skip object header. */
667 err = parse_tree_object(tree, repo, zb.outbuf + obj->hdrlen, len);
673 const struct got_error *
674 got_object_tree_open(struct got_tree_object **tree,
675 struct got_repository *repo, struct got_object *obj)
677 const struct got_error *err = NULL;
680 if (obj->type != GOT_OBJ_TYPE_TREE)
681 return got_error(GOT_ERR_OBJ_TYPE);
683 err = open_object(&f, obj, repo);
687 err = read_tree_object(tree, repo, obj, f);
693 got_object_tree_close(struct got_tree_object *tree)
695 struct got_tree_entry *te;
697 while (!SIMPLEQ_EMPTY(&tree->entries)) {
698 te = SIMPLEQ_FIRST(&tree->entries);
699 SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
700 tree_entry_close(te);
706 const struct got_error *
707 got_object_blob_open(struct got_blob_object **blob,
708 struct got_repository *repo, struct got_object *obj, size_t blocksize)
710 const struct got_error *err = NULL;
712 if (obj->type != GOT_OBJ_TYPE_BLOB)
713 return got_error(GOT_ERR_OBJ_TYPE);
715 if (blocksize < obj->hdrlen)
716 return got_error(GOT_ERR_NO_SPACE);
718 *blob = calloc(1, sizeof(**blob));
720 return got_error(GOT_ERR_NO_MEM);
722 err = open_object(&((*blob)->f), obj, repo);
728 err = inflate_init(&(*blob)->zb, blocksize);
735 (*blob)->hdrlen = obj->hdrlen;
736 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
742 got_object_blob_close(struct got_blob_object *blob)
744 inflate_end(&blob->zb);
749 const struct got_error *
750 got_object_blob_read_block(struct got_blob_object *blob, size_t *outlenp)
752 return inflate_read(&blob->zb, blob->f, outlenp);