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"
39 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
43 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
46 #define GOT_OBJ_TAG_COMMIT "commit"
47 #define GOT_OBJ_TAG_TREE "tree"
48 #define GOT_OBJ_TAG_BLOB "blob"
50 #define GOT_COMMIT_TAG_TREE "tree "
51 #define GOT_COMMIT_TAG_PARENT "parent "
52 #define GOT_COMMIT_TAG_AUTHOR "author "
53 #define GOT_COMMIT_TAG_COMMITTER "committer "
56 got_object_id_str(struct got_object_id *id, char *buf, size_t size)
58 return got_sha1_digest_to_str(id->sha1, buf, size);
62 got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
64 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
68 got_object_get_type(struct got_object *obj)
71 case GOT_OBJ_TYPE_COMMIT:
72 case GOT_OBJ_TYPE_TREE:
73 case GOT_OBJ_TYPE_BLOB:
74 case GOT_OBJ_TYPE_TAG:
85 static const struct got_error *
86 parse_object_header(struct got_object **obj, char *buf, size_t len)
88 const char *obj_tags[] = {
93 const int obj_types[] = {
99 size_t size = 0, hdrlen = 0;
101 char *p = strchr(buf, '\0');
104 return got_error(GOT_ERR_BAD_OBJ_HDR);
106 hdrlen = strlen(buf) + 1 /* '\0' */;
108 for (i = 0; i < nitems(obj_tags); i++) {
109 const char *tag = obj_tags[i];
110 size_t tlen = strlen(tag);
113 if (strncmp(buf, tag, tlen) != 0)
118 return got_error(GOT_ERR_BAD_OBJ_HDR);
119 size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
121 return got_error(GOT_ERR_BAD_OBJ_HDR);
126 return got_error(GOT_ERR_BAD_OBJ_HDR);
128 *obj = calloc(1, sizeof(**obj));
130 return got_error(GOT_ERR_NO_MEM);
132 (*obj)->hdrlen = hdrlen;
137 static const struct got_error *
138 read_object_header(struct got_object **obj, struct got_repository *repo,
141 const struct got_error *err;
142 struct got_zstream_buf zb;
145 const size_t zbsize = 64;
146 size_t outlen, totlen;
149 buf = calloc(zbsize, sizeof(char));
151 return got_error(GOT_ERR_NO_MEM);
153 err = got_inflate_init(&zb, zbsize);
160 err = got_inflate_read(&zb, f, &outlen);
163 if (strchr(zb.outbuf, '\0') == NULL) {
164 buf = recallocarray(buf, 1 + i, 2 + i, zbsize);
166 err = got_error(GOT_ERR_NO_MEM);
170 memcpy(buf + totlen, zb.outbuf, outlen);
173 } while (strchr(zb.outbuf, '\0') == NULL);
175 err = parse_object_header(obj, buf, totlen);
177 got_inflate_end(&zb);
181 static const struct got_error *
182 object_path(char **path, struct got_object_id *id, struct got_repository *repo)
184 const struct got_error *err = NULL;
185 char hex[SHA1_DIGEST_STRING_LENGTH];
186 char *path_objects = got_repo_get_path_objects(repo);
188 if (path_objects == NULL)
189 return got_error(GOT_ERR_NO_MEM);
191 got_object_id_str(id, hex, sizeof(hex));
193 if (asprintf(path, "%s/%.2x/%s", path_objects,
194 id->sha1[0], hex + 2) == -1)
195 err = got_error(GOT_ERR_NO_MEM);
201 static const struct got_error *
202 open_loose_object(FILE **f, struct got_object *obj, struct got_repository *repo)
204 const struct got_error *err = NULL;
207 err = object_path(&path, &obj->id, repo);
210 *f = fopen(path, "rb");
212 err = got_error_from_errno();
220 const struct got_error *
221 got_object_open(struct got_object **obj, struct got_repository *repo,
222 struct got_object_id *id)
224 const struct got_error *err = NULL;
228 err = object_path(&path, id, repo);
232 f = fopen(path, "rb");
234 if (errno != ENOENT) {
235 err = got_error_from_errno();
238 err = got_packfile_open_object(obj, id, repo);
242 err = got_error(GOT_ERR_NO_OBJ);
244 err = read_object_header(obj, repo, f);
247 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
258 got_object_close(struct got_object *obj)
260 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
261 struct got_delta *delta;
262 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
263 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
264 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
265 got_delta_close(delta);
268 if (obj->flags & GOT_OBJ_FLAG_PACKED)
269 free(obj->path_packfile);
274 commit_object_valid(struct got_commit_object *commit)
283 for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
284 if (commit->tree_id.sha1[i] == 0)
287 if (n == SHA1_DIGEST_LENGTH)
293 static const struct got_error *
294 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
296 const struct got_error *err = NULL;
299 ssize_t remain = (ssize_t)len;
301 *commit = calloc(1, sizeof(**commit));
303 return got_error(GOT_ERR_NO_MEM);
305 SIMPLEQ_INIT(&(*commit)->parent_ids);
307 tlen = strlen(GOT_COMMIT_TAG_TREE);
308 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
310 if (remain < SHA1_DIGEST_STRING_LENGTH) {
311 err = got_error(GOT_ERR_BAD_OBJ_DATA);
315 if (!got_parse_sha1_digest((*commit)->tree_id.sha1, s)) {
316 err = got_error(GOT_ERR_BAD_OBJ_DATA);
319 remain -= SHA1_DIGEST_STRING_LENGTH;
320 s += SHA1_DIGEST_STRING_LENGTH;
322 err = got_error(GOT_ERR_BAD_OBJ_DATA);
326 tlen = strlen(GOT_COMMIT_TAG_PARENT);
327 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
328 struct got_parent_id *pid;
331 if (remain < SHA1_DIGEST_STRING_LENGTH) {
332 err = got_error(GOT_ERR_BAD_OBJ_DATA);
336 pid = calloc(1, sizeof(*pid));
338 err = got_error(GOT_ERR_NO_MEM);
342 if (!got_parse_sha1_digest(pid->id.sha1, s)) {
343 err = got_error(GOT_ERR_BAD_OBJ_DATA);
346 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, pid, entry);
347 (*commit)->nparents++;
349 remain -= SHA1_DIGEST_STRING_LENGTH;
350 s += SHA1_DIGEST_STRING_LENGTH;
353 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
354 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
359 err = got_error(GOT_ERR_BAD_OBJ_DATA);
365 err = got_error(GOT_ERR_BAD_OBJ_DATA);
369 (*commit)->author = strdup(s);
370 if ((*commit)->author == NULL) {
371 err = got_error(GOT_ERR_NO_MEM);
374 s += strlen((*commit)->author) + 1;
375 remain -= strlen((*commit)->author) + 1;
378 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
379 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
384 err = got_error(GOT_ERR_BAD_OBJ_DATA);
390 err = got_error(GOT_ERR_BAD_OBJ_DATA);
394 (*commit)->committer = strdup(s);
395 if ((*commit)->committer == NULL) {
396 err = got_error(GOT_ERR_NO_MEM);
399 s += strlen((*commit)->committer) + 1;
400 remain -= strlen((*commit)->committer) + 1;
403 (*commit)->logmsg = strndup(s, remain);
404 if ((*commit)->logmsg == NULL) {
405 err = got_error(GOT_ERR_NO_MEM);
410 got_object_commit_close(*commit);
415 tree_entry_close(struct got_tree_entry *te)
421 static const struct got_error *
422 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
425 char *p = buf, *space;
426 const struct got_error *err = NULL;
427 char hex[SHA1_DIGEST_STRING_LENGTH];
429 *te = calloc(1, sizeof(**te));
431 return got_error(GOT_ERR_NO_MEM);
433 *elen = strlen(buf) + 1;
434 if (*elen > maxlen) {
436 return got_error(GOT_ERR_BAD_OBJ_DATA);
439 space = strchr(buf, ' ');
442 return got_error(GOT_ERR_BAD_OBJ_DATA);
445 if (*p < '0' && *p > '7') {
446 err = got_error(GOT_ERR_BAD_OBJ_DATA);
450 (*te)->mode |= *p - '0';
454 (*te)->name = strdup(space + 1);
455 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
456 err = got_error(GOT_ERR_BAD_OBJ_DATA);
459 buf += strlen(buf) + 1;
460 memcpy((*te)->id.sha1, buf, SHA1_DIGEST_LENGTH);
461 *elen += SHA1_DIGEST_LENGTH;
464 tree_entry_close(*te);
468 static const struct got_error *
469 parse_tree_object(struct got_tree_object **tree, struct got_repository *repo,
470 char *buf, size_t len)
472 const struct got_error *err;
476 *tree = calloc(1, sizeof(**tree));
478 return got_error(GOT_ERR_NO_MEM);
480 SIMPLEQ_INIT(&(*tree)->entries);
483 struct got_tree_entry *te;
486 err = parse_tree_entry(&te, &elen, buf, remain);
490 SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
496 got_object_tree_close(*tree);
497 return got_error(GOT_ERR_BAD_OBJ_DATA);
503 static const struct got_error *
504 read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
506 const struct got_error *err = NULL;
507 static const size_t blocksize = 512;
508 size_t n, total, remain;
514 buf = calloc(1, blocksize);
516 return got_error(GOT_ERR_NO_MEM);
523 newbuf = reallocarray(buf, 1, total + blocksize);
524 if (newbuf == NULL) {
525 err = got_error(GOT_ERR_NO_MEM);
531 n = fread(buf, 1, remain, f);
534 err = got_ferror(f, GOT_ERR_IO);
552 static const struct got_error *
553 read_commit_object(struct got_commit_object **commit,
554 struct got_repository *repo, struct got_object *obj, FILE *f)
556 const struct got_error *err = NULL;
561 if (obj->flags & GOT_OBJ_FLAG_PACKED)
562 err = read_to_mem(&p, &len, f);
564 err = got_inflate_to_mem(&p, &len, f);
568 if (len < obj->hdrlen + obj->size) {
569 err = got_error(GOT_ERR_BAD_OBJ_DATA);
573 /* Skip object header. */
575 err = parse_commit_object(commit, p + obj->hdrlen, len);
581 const struct got_error *
582 got_object_commit_open(struct got_commit_object **commit,
583 struct got_repository *repo, struct got_object *obj)
585 const struct got_error *err = NULL;
588 if (obj->type != GOT_OBJ_TYPE_COMMIT)
589 return got_error(GOT_ERR_OBJ_TYPE);
591 if (obj->flags & GOT_OBJ_FLAG_PACKED)
592 err = got_packfile_extract_object(&f, obj, repo);
594 err = open_loose_object(&f, obj, repo);
598 err = read_commit_object(commit, repo, obj, f);
604 got_object_commit_close(struct got_commit_object *commit)
606 struct got_parent_id *pid;
608 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
609 pid = SIMPLEQ_FIRST(&commit->parent_ids);
610 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
614 free(commit->author);
615 free(commit->committer);
616 free(commit->logmsg);
620 static const struct got_error *
621 read_tree_object(struct got_tree_object **tree,
622 struct got_repository *repo, struct got_object *obj, FILE *f)
624 const struct got_error *err = NULL;
629 if (obj->flags & GOT_OBJ_FLAG_PACKED)
630 err = read_to_mem(&p, &len, f);
632 err = got_inflate_to_mem(&p, &len, f);
636 if (len < obj->hdrlen + obj->size) {
637 err = got_error(GOT_ERR_BAD_OBJ_DATA);
641 /* Skip object header. */
643 err = parse_tree_object(tree, repo, p + obj->hdrlen, len);
649 const struct got_error *
650 got_object_tree_open(struct got_tree_object **tree,
651 struct got_repository *repo, struct got_object *obj)
653 const struct got_error *err = NULL;
656 if (obj->type != GOT_OBJ_TYPE_TREE)
657 return got_error(GOT_ERR_OBJ_TYPE);
659 if (obj->flags & GOT_OBJ_FLAG_PACKED)
660 err = got_packfile_extract_object(&f, obj, repo);
662 err = open_loose_object(&f, obj, repo);
666 err = read_tree_object(tree, repo, obj, f);
672 got_object_tree_close(struct got_tree_object *tree)
674 struct got_tree_entry *te;
676 while (!SIMPLEQ_EMPTY(&tree->entries)) {
677 te = SIMPLEQ_FIRST(&tree->entries);
678 SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
679 tree_entry_close(te);
685 const struct got_error *
686 got_object_blob_open(struct got_blob_object **blob,
687 struct got_repository *repo, struct got_object *obj, size_t blocksize)
689 const struct got_error *err = NULL;
691 if (obj->type != GOT_OBJ_TYPE_BLOB)
692 return got_error(GOT_ERR_OBJ_TYPE);
694 if (blocksize < obj->hdrlen)
695 return got_error(GOT_ERR_NO_SPACE);
697 *blob = calloc(1, sizeof(**blob));
699 return got_error(GOT_ERR_NO_MEM);
701 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
702 (*blob)->read_buf = calloc(1, blocksize);
703 if ((*blob)->read_buf == NULL)
704 return got_error(GOT_ERR_NO_MEM);
705 err = got_packfile_extract_object(&((*blob)->f), obj, repo);
709 err = open_loose_object(&((*blob)->f), obj, repo);
715 err = got_inflate_init(&(*blob)->zb, blocksize);
722 (*blob)->read_buf = (*blob)->zb.outbuf;
723 (*blob)->flags |= GOT_BLOB_F_COMPRESSED;
726 (*blob)->hdrlen = obj->hdrlen;
727 (*blob)->blocksize = blocksize;
728 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
734 got_object_blob_close(struct got_blob_object *blob)
736 if (blob->flags & GOT_BLOB_F_COMPRESSED)
737 got_inflate_end(&blob->zb);
739 free(blob->read_buf);
744 const struct got_error *
745 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
749 if (blob->flags & GOT_BLOB_F_COMPRESSED)
750 return got_inflate_read(&blob->zb, blob->f, outlenp);
752 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
753 if (n == 0 && ferror(blob->f))
754 return got_ferror(blob->f, GOT_ERR_IO);