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 return got_error(GOT_ERR_NO_MEM);
209 (*obj)->hdrlen = hdrlen;
214 static const struct got_error *
215 read_object_header(struct got_object **obj, struct got_repository *repo,
218 const struct got_error *err;
219 struct got_zstream_buf zb;
222 const size_t zbsize = 64;
223 size_t outlen, totlen;
226 buf = calloc(zbsize, sizeof(char));
228 return got_error(GOT_ERR_NO_MEM);
230 err = inflate_init(&zb, zbsize);
237 err = inflate_read(&zb, f, &outlen);
240 if (strchr(zb.outbuf, '\0') == NULL) {
241 buf = recallocarray(buf, 1 + i, 2 + i, zbsize);
243 err = got_error(GOT_ERR_NO_MEM);
247 memcpy(buf + totlen, zb.outbuf, outlen);
250 } while (strchr(zb.outbuf, '\0') == NULL);
252 err = parse_object_header(obj, buf, totlen);
258 static const struct got_error *
259 object_path(char **path, struct got_object_id *id, struct got_repository *repo)
261 const struct got_error *err = NULL;
262 char hex[SHA1_DIGEST_STRING_LENGTH];
263 char *path_objects = got_repo_get_path_objects(repo);
265 if (path_objects == NULL)
266 return got_error(GOT_ERR_NO_MEM);
268 got_object_id_str(id, hex, sizeof(hex));
270 if (asprintf(path, "%s/%.2x/%s", path_objects,
271 id->sha1[0], hex + 2) == -1)
272 err = got_error(GOT_ERR_NO_MEM);
278 static const struct got_error *
279 fopen_object(FILE **f, struct got_object *obj, struct got_repository *repo)
281 const struct got_error *err = NULL;
284 if (obj->flags & GOT_OBJ_FLAG_PACKED)
285 return got_packfile_extract_object(f, obj, repo);
287 err = object_path(&path, &obj->id, repo);
290 *f = fopen(path, "rb");
292 err = got_error_from_errno();
300 const struct got_error *
301 got_object_open(struct got_object **obj, struct got_repository *repo,
302 struct got_object_id *id)
304 const struct got_error *err = NULL;
308 err = object_path(&path, id, repo);
312 f = fopen(path, "rb");
314 if (errno != ENOENT) {
315 err = got_error_from_errno();
318 err = got_packfile_open_object(obj, id, repo);
322 err = got_error(GOT_ERR_NO_OBJ);
324 err = read_object_header(obj, repo, f);
327 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
338 got_object_close(struct got_object *obj)
340 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
341 struct got_delta *delta;
342 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
343 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
344 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
345 got_delta_close(delta);
348 if (obj->flags & GOT_OBJ_FLAG_PACKED)
349 free(obj->path_packfile);
354 commit_object_valid(struct got_commit_object *commit)
363 for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
364 if (commit->tree_id.sha1[i] == 0)
367 if (n == SHA1_DIGEST_LENGTH)
373 static const struct got_error *
374 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
376 const struct got_error *err = NULL;
379 ssize_t remain = (ssize_t)len;
381 *commit = calloc(1, sizeof(**commit));
383 return got_error(GOT_ERR_NO_MEM);
385 SIMPLEQ_INIT(&(*commit)->parent_ids);
387 tlen = strlen(GOT_COMMIT_TAG_TREE);
388 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
390 if (remain < SHA1_DIGEST_STRING_LENGTH) {
391 err = got_error(GOT_ERR_BAD_OBJ_DATA);
395 if (!got_parse_sha1_digest((*commit)->tree_id.sha1, s)) {
396 err = got_error(GOT_ERR_BAD_OBJ_DATA);
399 remain -= SHA1_DIGEST_STRING_LENGTH;
400 s += SHA1_DIGEST_STRING_LENGTH;
402 err = got_error(GOT_ERR_BAD_OBJ_DATA);
406 tlen = strlen(GOT_COMMIT_TAG_PARENT);
407 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
408 struct got_parent_id *pid;
411 if (remain < SHA1_DIGEST_STRING_LENGTH) {
412 err = got_error(GOT_ERR_BAD_OBJ_DATA);
416 pid = calloc(1, sizeof(*pid));
418 err = got_error(GOT_ERR_NO_MEM);
422 if (!got_parse_sha1_digest(pid->id.sha1, s)) {
423 err = got_error(GOT_ERR_BAD_OBJ_DATA);
426 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, pid, entry);
427 (*commit)->nparents++;
429 s += SHA1_DIGEST_STRING_LENGTH;
432 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
433 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
438 err = got_error(GOT_ERR_BAD_OBJ_DATA);
444 err = got_error(GOT_ERR_BAD_OBJ_DATA);
448 (*commit)->author = strdup(s);
449 if ((*commit)->author == NULL) {
450 err = got_error(GOT_ERR_NO_MEM);
453 s += strlen((*commit)->author) + 1;
456 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
457 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
462 err = got_error(GOT_ERR_BAD_OBJ_DATA);
468 err = got_error(GOT_ERR_BAD_OBJ_DATA);
472 (*commit)->committer = strdup(s);
473 if ((*commit)->committer == NULL) {
474 err = got_error(GOT_ERR_NO_MEM);
477 s += strlen((*commit)->committer) + 1;
480 (*commit)->logmsg = strdup(s);
483 got_object_commit_close(*commit);
488 tree_entry_close(struct got_tree_entry *te)
494 static const struct got_error *
495 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
498 char *p = buf, *space;
499 const struct got_error *err = NULL;
500 char hex[SHA1_DIGEST_STRING_LENGTH];
502 *te = calloc(1, sizeof(**te));
504 return got_error(GOT_ERR_NO_MEM);
506 *elen = strlen(buf) + 1;
507 if (*elen > maxlen) {
509 return got_error(GOT_ERR_BAD_OBJ_DATA);
512 space = strchr(buf, ' ');
515 return got_error(GOT_ERR_BAD_OBJ_DATA);
518 if (*p < '0' && *p > '7') {
519 err = got_error(GOT_ERR_BAD_OBJ_DATA);
523 (*te)->mode |= *p - '0';
527 (*te)->name = strdup(space + 1);
528 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
529 err = got_error(GOT_ERR_BAD_OBJ_DATA);
532 buf += strlen(buf) + 1;
533 memcpy((*te)->id.sha1, buf, SHA1_DIGEST_LENGTH);
534 *elen += SHA1_DIGEST_LENGTH;
537 tree_entry_close(*te);
541 static const struct got_error *
542 parse_tree_object(struct got_tree_object **tree, struct got_repository *repo,
543 char *buf, size_t len)
548 *tree = calloc(1, sizeof(**tree));
550 return got_error(GOT_ERR_NO_MEM);
552 SIMPLEQ_INIT(&(*tree)->entries);
555 struct got_tree_entry *te;
558 parse_tree_entry(&te, &elen, buf, remain);
560 SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
566 got_object_tree_close(*tree);
567 return got_error(GOT_ERR_BAD_OBJ_DATA);
573 static const struct got_error *
574 read_commit_object(struct got_commit_object **commit,
575 struct got_repository *repo, struct got_object *obj, FILE *f)
577 const struct got_error *err = NULL;
578 struct got_zstream_buf zb;
583 err = inflate_init(&zb, 8192);
588 err = inflate_read(&zb, f, &len);
591 } while (len < obj->hdrlen + obj->size);
593 if (len < obj->hdrlen + obj->size) {
594 err = got_error(GOT_ERR_BAD_OBJ_DATA);
598 /* Skip object header. */
600 err = parse_commit_object(commit, zb.outbuf + obj->hdrlen, len);
606 const struct got_error *
607 got_object_commit_open(struct got_commit_object **commit,
608 struct got_repository *repo, struct got_object *obj)
610 const struct got_error *err = NULL;
613 if (obj->type != GOT_OBJ_TYPE_COMMIT)
614 return got_error(GOT_ERR_OBJ_TYPE);
616 err = fopen_object(&f, obj, repo);
620 err = read_commit_object(commit, repo, obj, f);
626 got_object_commit_close(struct got_commit_object *commit)
628 struct got_parent_id *pid;
630 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
631 pid = SIMPLEQ_FIRST(&commit->parent_ids);
632 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
636 free(commit->author);
637 free(commit->committer);
638 free(commit->logmsg);
642 static const struct got_error *
643 read_tree_object(struct got_tree_object **tree,
644 struct got_repository *repo, struct got_object *obj, FILE *f)
646 const struct got_error *err = NULL;
647 struct got_zstream_buf zb;
652 err = inflate_init(&zb, 8192);
657 err = inflate_read(&zb, f, &len);
660 } while (len < obj->hdrlen + obj->size);
662 if (len < obj->hdrlen + obj->size) {
663 err = got_error(GOT_ERR_BAD_OBJ_DATA);
667 /* Skip object header. */
669 err = parse_tree_object(tree, repo, zb.outbuf + obj->hdrlen, len);
675 const struct got_error *
676 got_object_tree_open(struct got_tree_object **tree,
677 struct got_repository *repo, struct got_object *obj)
679 const struct got_error *err = NULL;
682 if (obj->type != GOT_OBJ_TYPE_TREE)
683 return got_error(GOT_ERR_OBJ_TYPE);
685 err = fopen_object(&f, obj, repo);
689 err = read_tree_object(tree, repo, obj, f);
695 got_object_tree_close(struct got_tree_object *tree)
697 struct got_tree_entry *te;
699 while (!SIMPLEQ_EMPTY(&tree->entries)) {
700 te = SIMPLEQ_FIRST(&tree->entries);
701 SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
702 tree_entry_close(te);
708 const struct got_error *
709 got_object_blob_open(struct got_blob_object **blob,
710 struct got_repository *repo, struct got_object *obj, size_t blocksize)
712 const struct got_error *err = NULL;
714 if (obj->type != GOT_OBJ_TYPE_BLOB)
715 return got_error(GOT_ERR_OBJ_TYPE);
717 if (blocksize < obj->hdrlen)
718 return got_error(GOT_ERR_NO_SPACE);
720 *blob = calloc(1, sizeof(**blob));
722 return got_error(GOT_ERR_NO_MEM);
724 err = fopen_object(&((*blob)->f), obj, repo);
730 err = inflate_init(&(*blob)->zb, blocksize);
737 (*blob)->hdrlen = obj->hdrlen;
738 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
744 got_object_blob_close(struct got_blob_object *blob)
746 inflate_end(&blob->zb);
751 const struct got_error *
752 got_object_blob_read_block(struct got_blob_object *blob, size_t *outlenp)
754 return inflate_read(&blob->zb, blob->f, outlenp);