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"
33 #include "got_sha1_priv.h"
34 #include "got_delta_priv.h"
35 #include "got_pack_priv.h"
36 #include "got_zb_priv.h"
37 #include "got_object_priv.h"
40 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
44 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
47 #define GOT_OBJ_TAG_COMMIT "commit"
48 #define GOT_OBJ_TAG_TREE "tree"
49 #define GOT_OBJ_TAG_BLOB "blob"
51 #define GOT_COMMIT_TAG_TREE "tree "
52 #define GOT_COMMIT_TAG_PARENT "parent "
53 #define GOT_COMMIT_TAG_AUTHOR "author "
54 #define GOT_COMMIT_TAG_COMMITTER "committer "
56 const struct got_error *
57 got_object_id_str(char **outbuf, struct got_object_id *id)
59 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
61 *outbuf = calloc(1, len);
63 return got_error(GOT_ERR_NO_MEM);
65 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
68 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
75 got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
77 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
81 got_object_get_type(struct got_object *obj)
84 case GOT_OBJ_TYPE_COMMIT:
85 case GOT_OBJ_TYPE_TREE:
86 case GOT_OBJ_TYPE_BLOB:
87 case GOT_OBJ_TYPE_TAG:
98 static const struct got_error *
99 parse_object_header(struct got_object **obj, char *buf, size_t len)
101 const char *obj_tags[] = {
106 const int obj_types[] = {
112 size_t size = 0, hdrlen = 0;
114 char *p = strchr(buf, '\0');
117 return got_error(GOT_ERR_BAD_OBJ_HDR);
119 hdrlen = strlen(buf) + 1 /* '\0' */;
121 for (i = 0; i < nitems(obj_tags); i++) {
122 const char *tag = obj_tags[i];
123 size_t tlen = strlen(tag);
126 if (strncmp(buf, tag, tlen) != 0)
131 return got_error(GOT_ERR_BAD_OBJ_HDR);
132 size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
134 return got_error(GOT_ERR_BAD_OBJ_HDR);
139 return got_error(GOT_ERR_BAD_OBJ_HDR);
141 *obj = calloc(1, sizeof(**obj));
143 return got_error(GOT_ERR_NO_MEM);
145 (*obj)->hdrlen = hdrlen;
150 static const struct got_error *
151 read_object_header(struct got_object **obj, struct got_repository *repo,
154 const struct got_error *err;
155 struct got_zstream_buf zb;
158 const size_t zbsize = 64;
159 size_t outlen, totlen;
162 buf = calloc(zbsize, sizeof(char));
164 return got_error(GOT_ERR_NO_MEM);
166 err = got_inflate_init(&zb, zbsize);
173 err = got_inflate_read(&zb, f, &outlen);
176 if (strchr(zb.outbuf, '\0') == NULL) {
177 buf = recallocarray(buf, 1 + i, 2 + i, zbsize);
179 err = got_error(GOT_ERR_NO_MEM);
183 memcpy(buf + totlen, zb.outbuf, outlen);
186 } while (strchr(zb.outbuf, '\0') == NULL);
188 err = parse_object_header(obj, buf, totlen);
190 got_inflate_end(&zb);
194 static const struct got_error *
195 object_path(char **path, struct got_object_id *id, struct got_repository *repo)
197 const struct got_error *err = NULL;
199 char *path_objects = got_repo_get_path_objects(repo);
201 if (path_objects == NULL)
202 return got_error(GOT_ERR_NO_MEM);
204 err = got_object_id_str(&hex, id);
208 if (asprintf(path, "%s/%.2x/%s", path_objects,
209 id->sha1[0], hex + 2) == -1)
210 err = got_error(GOT_ERR_NO_MEM);
217 static const struct got_error *
218 open_loose_object(FILE **f, struct got_object *obj, struct got_repository *repo)
220 const struct got_error *err = NULL;
223 err = object_path(&path, &obj->id, repo);
226 *f = fopen(path, "rb");
228 err = got_error_from_errno();
236 const struct got_error *
237 got_object_open(struct got_object **obj, struct got_repository *repo,
238 struct got_object_id *id)
240 const struct got_error *err = NULL;
244 err = object_path(&path, id, repo);
248 f = fopen(path, "rb");
250 if (errno != ENOENT) {
251 err = got_error_from_errno();
254 err = got_packfile_open_object(obj, id, repo);
258 err = got_error(GOT_ERR_NO_OBJ);
260 err = read_object_header(obj, repo, f);
263 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
273 const struct got_error *
274 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
277 struct got_object_id id;
279 if (!got_parse_sha1_digest(id.sha1, id_str))
280 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
282 return got_object_open(obj, repo, &id);
286 got_object_close(struct got_object *obj)
288 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
289 struct got_delta *delta;
290 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
291 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
292 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
293 got_delta_close(delta);
296 if (obj->flags & GOT_OBJ_FLAG_PACKED)
297 free(obj->path_packfile);
302 commit_object_valid(struct got_commit_object *commit)
311 for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
312 if (commit->tree_id->sha1[i] == 0)
315 if (n == SHA1_DIGEST_LENGTH)
321 static const struct got_error *
322 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
324 const struct got_error *err = NULL;
327 ssize_t remain = (ssize_t)len;
329 *commit = calloc(1, sizeof(**commit));
331 return got_error(GOT_ERR_NO_MEM);
332 (*commit)->tree_id = calloc(1, sizeof(*(*commit)->tree_id));
333 if ((*commit)->tree_id == NULL) {
336 return got_error(GOT_ERR_NO_MEM);
339 SIMPLEQ_INIT(&(*commit)->parent_ids);
341 tlen = strlen(GOT_COMMIT_TAG_TREE);
342 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
344 if (remain < SHA1_DIGEST_STRING_LENGTH) {
345 err = got_error(GOT_ERR_BAD_OBJ_DATA);
349 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
350 err = got_error(GOT_ERR_BAD_OBJ_DATA);
353 remain -= SHA1_DIGEST_STRING_LENGTH;
354 s += SHA1_DIGEST_STRING_LENGTH;
356 err = got_error(GOT_ERR_BAD_OBJ_DATA);
360 tlen = strlen(GOT_COMMIT_TAG_PARENT);
361 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
362 struct got_parent_id *pid;
365 if (remain < SHA1_DIGEST_STRING_LENGTH) {
366 err = got_error(GOT_ERR_BAD_OBJ_DATA);
370 pid = calloc(1, sizeof(*pid));
372 err = got_error(GOT_ERR_NO_MEM);
375 pid->id = calloc(1, sizeof(*pid->id));
376 if (pid->id == NULL) {
378 err = got_error(GOT_ERR_NO_MEM);
382 if (!got_parse_sha1_digest(pid->id->sha1, s)) {
383 err = got_error(GOT_ERR_BAD_OBJ_DATA);
388 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, pid, entry);
389 (*commit)->nparents++;
391 remain -= SHA1_DIGEST_STRING_LENGTH;
392 s += SHA1_DIGEST_STRING_LENGTH;
395 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
396 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
401 err = got_error(GOT_ERR_BAD_OBJ_DATA);
407 err = got_error(GOT_ERR_BAD_OBJ_DATA);
411 (*commit)->author = strdup(s);
412 if ((*commit)->author == NULL) {
413 err = got_error(GOT_ERR_NO_MEM);
416 s += strlen((*commit)->author) + 1;
417 remain -= strlen((*commit)->author) + 1;
420 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
421 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
426 err = got_error(GOT_ERR_BAD_OBJ_DATA);
432 err = got_error(GOT_ERR_BAD_OBJ_DATA);
436 (*commit)->committer = strdup(s);
437 if ((*commit)->committer == NULL) {
438 err = got_error(GOT_ERR_NO_MEM);
441 s += strlen((*commit)->committer) + 1;
442 remain -= strlen((*commit)->committer) + 1;
445 (*commit)->logmsg = strndup(s, remain);
446 if ((*commit)->logmsg == NULL) {
447 err = got_error(GOT_ERR_NO_MEM);
452 got_object_commit_close(*commit);
459 tree_entry_close(struct got_tree_entry *te)
466 static const struct got_error *
467 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
470 char *p = buf, *space;
471 const struct got_error *err = NULL;
472 char hex[SHA1_DIGEST_STRING_LENGTH];
474 *te = calloc(1, sizeof(**te));
476 return got_error(GOT_ERR_NO_MEM);
478 (*te)->id = calloc(1, sizeof(*(*te)->id));
479 if ((*te)->id == NULL) {
482 return got_error(GOT_ERR_NO_MEM);
485 *elen = strlen(buf) + 1;
486 if (*elen > maxlen) {
489 return got_error(GOT_ERR_BAD_OBJ_DATA);
492 space = strchr(buf, ' ');
496 return got_error(GOT_ERR_BAD_OBJ_DATA);
499 if (*p < '0' && *p > '7') {
500 err = got_error(GOT_ERR_BAD_OBJ_DATA);
504 (*te)->mode |= *p - '0';
508 (*te)->name = strdup(space + 1);
509 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
510 err = got_error(GOT_ERR_BAD_OBJ_DATA);
513 buf += strlen(buf) + 1;
514 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
515 *elen += SHA1_DIGEST_LENGTH;
518 tree_entry_close(*te);
524 static const struct got_error *
525 parse_tree_object(struct got_tree_object **tree, struct got_repository *repo,
526 char *buf, size_t len)
528 const struct got_error *err;
532 *tree = calloc(1, sizeof(**tree));
534 return got_error(GOT_ERR_NO_MEM);
536 SIMPLEQ_INIT(&(*tree)->entries);
539 struct got_tree_entry *te;
542 err = parse_tree_entry(&te, &elen, buf, remain);
546 SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
552 got_object_tree_close(*tree);
553 return got_error(GOT_ERR_BAD_OBJ_DATA);
559 static const struct got_error *
560 read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
562 const struct got_error *err = NULL;
563 static const size_t blocksize = 512;
564 size_t n, total, remain;
570 buf = calloc(1, blocksize);
572 return got_error(GOT_ERR_NO_MEM);
579 newbuf = reallocarray(buf, 1, total + blocksize);
580 if (newbuf == NULL) {
581 err = got_error(GOT_ERR_NO_MEM);
587 n = fread(buf + total, 1, remain, f);
590 err = got_ferror(f, GOT_ERR_IO);
608 static const struct got_error *
609 read_commit_object(struct got_commit_object **commit,
610 struct got_repository *repo, struct got_object *obj, FILE *f)
612 const struct got_error *err = NULL;
617 if (obj->flags & GOT_OBJ_FLAG_PACKED)
618 err = read_to_mem(&p, &len, f);
620 err = got_inflate_to_mem(&p, &len, f);
624 if (len < obj->hdrlen + obj->size) {
625 err = got_error(GOT_ERR_BAD_OBJ_DATA);
629 /* Skip object header. */
631 err = parse_commit_object(commit, p + obj->hdrlen, len);
637 const struct got_error *
638 got_object_commit_open(struct got_commit_object **commit,
639 struct got_repository *repo, struct got_object *obj)
641 const struct got_error *err = NULL;
644 if (obj->type != GOT_OBJ_TYPE_COMMIT)
645 return got_error(GOT_ERR_OBJ_TYPE);
647 if (obj->flags & GOT_OBJ_FLAG_PACKED)
648 err = got_packfile_extract_object(&f, obj, repo);
650 err = open_loose_object(&f, obj, repo);
654 err = read_commit_object(commit, repo, obj, f);
660 got_object_commit_close(struct got_commit_object *commit)
662 struct got_parent_id *pid;
664 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
665 pid = SIMPLEQ_FIRST(&commit->parent_ids);
666 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
671 free(commit->tree_id);
672 free(commit->author);
673 free(commit->committer);
674 free(commit->logmsg);
678 static const struct got_error *
679 read_tree_object(struct got_tree_object **tree,
680 struct got_repository *repo, struct got_object *obj, FILE *f)
682 const struct got_error *err = NULL;
687 if (obj->flags & GOT_OBJ_FLAG_PACKED)
688 err = read_to_mem(&p, &len, f);
690 err = got_inflate_to_mem(&p, &len, f);
694 if (len < obj->hdrlen + obj->size) {
695 err = got_error(GOT_ERR_BAD_OBJ_DATA);
699 /* Skip object header. */
701 err = parse_tree_object(tree, repo, p + obj->hdrlen, len);
707 const struct got_error *
708 got_object_tree_open(struct got_tree_object **tree,
709 struct got_repository *repo, struct got_object *obj)
711 const struct got_error *err = NULL;
714 if (obj->type != GOT_OBJ_TYPE_TREE)
715 return got_error(GOT_ERR_OBJ_TYPE);
717 if (obj->flags & GOT_OBJ_FLAG_PACKED)
718 err = got_packfile_extract_object(&f, obj, repo);
720 err = open_loose_object(&f, obj, repo);
724 err = read_tree_object(tree, repo, obj, f);
730 got_object_tree_close(struct got_tree_object *tree)
732 struct got_tree_entry *te;
734 while (!SIMPLEQ_EMPTY(&tree->entries)) {
735 te = SIMPLEQ_FIRST(&tree->entries);
736 SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
737 tree_entry_close(te);
743 const struct got_error *
744 got_object_blob_open(struct got_blob_object **blob,
745 struct got_repository *repo, struct got_object *obj, size_t blocksize)
747 const struct got_error *err = NULL;
749 if (obj->type != GOT_OBJ_TYPE_BLOB)
750 return got_error(GOT_ERR_OBJ_TYPE);
752 if (blocksize < obj->hdrlen)
753 return got_error(GOT_ERR_NO_SPACE);
755 *blob = calloc(1, sizeof(**blob));
757 return got_error(GOT_ERR_NO_MEM);
759 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
760 (*blob)->read_buf = calloc(1, blocksize);
761 if ((*blob)->read_buf == NULL)
762 return got_error(GOT_ERR_NO_MEM);
763 err = got_packfile_extract_object(&((*blob)->f), obj, repo);
767 err = open_loose_object(&((*blob)->f), obj, repo);
773 err = got_inflate_init(&(*blob)->zb, blocksize);
780 (*blob)->read_buf = (*blob)->zb.outbuf;
781 (*blob)->flags |= GOT_BLOB_F_COMPRESSED;
784 (*blob)->hdrlen = obj->hdrlen;
785 (*blob)->blocksize = blocksize;
786 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
792 got_object_blob_close(struct got_blob_object *blob)
794 if (blob->flags & GOT_BLOB_F_COMPRESSED)
795 got_inflate_end(&blob->zb);
797 free(blob->read_buf);
803 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
805 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
809 got_object_blob_get_hdrlen(struct got_blob_object *blob)
815 got_object_blob_get_read_buf(struct got_blob_object *blob)
817 return blob->read_buf;
820 const struct got_error *
821 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
825 if (blob->flags & GOT_BLOB_F_COMPRESSED)
826 return got_inflate_read(&blob->zb, blob->f, outlenp);
828 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
829 if (n == 0 && ferror(blob->f))
830 return got_ferror(blob->f, GOT_ERR_IO);