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_lib_sha1.h"
34 #include "got_lib_delta.h"
35 #include "got_lib_pack.h"
36 #include "got_lib_zbuf.h"
37 #include "got_lib_object.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_from_errno();
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);
80 struct got_object_id *
81 got_object_id_dup(struct got_object_id *id1)
83 struct got_object_id *id2;
85 id2 = malloc(sizeof(*id2));
88 memcpy(id2, id1, sizeof(*id2));
93 got_object_get_type(struct got_object *obj)
96 case GOT_OBJ_TYPE_COMMIT:
97 case GOT_OBJ_TYPE_TREE:
98 case GOT_OBJ_TYPE_BLOB:
99 case GOT_OBJ_TYPE_TAG:
110 static const struct got_error *
111 parse_object_header(struct got_object **obj, char *buf, size_t len)
113 const char *obj_tags[] = {
118 const int obj_types[] = {
124 size_t size = 0, hdrlen = 0;
126 char *p = strchr(buf, '\0');
129 return got_error(GOT_ERR_BAD_OBJ_HDR);
131 hdrlen = strlen(buf) + 1 /* '\0' */;
133 for (i = 0; i < nitems(obj_tags); i++) {
134 const char *tag = obj_tags[i];
135 size_t tlen = strlen(tag);
138 if (strncmp(buf, tag, tlen) != 0)
143 return got_error(GOT_ERR_BAD_OBJ_HDR);
144 size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
146 return got_error(GOT_ERR_BAD_OBJ_HDR);
151 return got_error(GOT_ERR_BAD_OBJ_HDR);
153 *obj = calloc(1, sizeof(**obj));
155 return got_error_from_errno();
157 (*obj)->hdrlen = hdrlen;
162 static const struct got_error *
163 read_object_header(struct got_object **obj, struct got_repository *repo,
166 const struct got_error *err;
167 struct got_zstream_buf zb;
169 const size_t zbsize = 64;
170 size_t outlen, totlen;
173 buf = calloc(zbsize, sizeof(char));
175 return got_error_from_errno();
177 err = got_inflate_init(&zb, NULL, zbsize);
184 err = got_inflate_read(&zb, f, &outlen);
187 if (strchr(zb.outbuf, '\0') == NULL) {
188 buf = recallocarray(buf, 1 + i, 2 + i, zbsize);
190 err = got_error_from_errno();
194 memcpy(buf + totlen, zb.outbuf, outlen);
197 } while (strchr(zb.outbuf, '\0') == NULL);
199 err = parse_object_header(obj, buf, totlen);
201 got_inflate_end(&zb);
205 static const struct got_error *
206 object_path(char **path, struct got_object_id *id, struct got_repository *repo)
208 const struct got_error *err = NULL;
210 char *path_objects = got_repo_get_path_objects(repo);
212 if (path_objects == NULL)
213 return got_error_from_errno();
215 err = got_object_id_str(&hex, id);
219 if (asprintf(path, "%s/%.2x/%s", path_objects,
220 id->sha1[0], hex + 2) == -1)
221 err = got_error_from_errno();
228 static const struct got_error *
229 open_loose_object(FILE **f, struct got_object *obj, struct got_repository *repo)
231 const struct got_error *err = NULL;
234 err = object_path(&path, &obj->id, repo);
237 *f = fopen(path, "rb");
239 err = got_error_from_errno();
247 const struct got_error *
248 got_object_open(struct got_object **obj, struct got_repository *repo,
249 struct got_object_id *id)
251 const struct got_error *err = NULL;
255 err = object_path(&path, id, repo);
259 f = fopen(path, "rb");
261 if (errno != ENOENT) {
262 err = got_error_from_errno();
265 err = got_packfile_open_object(obj, id, repo);
269 err = got_error(GOT_ERR_NO_OBJ);
271 err = read_object_header(obj, repo, f);
274 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
284 const struct got_error *
285 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
288 struct got_object_id id;
290 if (!got_parse_sha1_digest(id.sha1, id_str))
291 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
293 return got_object_open(obj, repo, &id);
297 got_object_close(struct got_object *obj)
299 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
300 struct got_delta *delta;
301 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
302 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
303 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
304 got_delta_close(delta);
307 if (obj->flags & GOT_OBJ_FLAG_PACKED)
308 free(obj->path_packfile);
312 static const struct got_error *
313 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
315 const struct got_error *err = NULL;
318 ssize_t remain = (ssize_t)len;
320 *commit = calloc(1, sizeof(**commit));
322 return got_error_from_errno();
323 (*commit)->tree_id = calloc(1, sizeof(*(*commit)->tree_id));
324 if ((*commit)->tree_id == NULL) {
325 err = got_error_from_errno();
331 SIMPLEQ_INIT(&(*commit)->parent_ids);
333 tlen = strlen(GOT_COMMIT_TAG_TREE);
334 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
336 if (remain < SHA1_DIGEST_STRING_LENGTH) {
337 err = got_error(GOT_ERR_BAD_OBJ_DATA);
341 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
342 err = got_error(GOT_ERR_BAD_OBJ_DATA);
345 remain -= SHA1_DIGEST_STRING_LENGTH;
346 s += SHA1_DIGEST_STRING_LENGTH;
348 err = got_error(GOT_ERR_BAD_OBJ_DATA);
352 tlen = strlen(GOT_COMMIT_TAG_PARENT);
353 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
354 struct got_parent_id *pid;
357 if (remain < SHA1_DIGEST_STRING_LENGTH) {
358 err = got_error(GOT_ERR_BAD_OBJ_DATA);
362 pid = calloc(1, sizeof(*pid));
364 err = got_error_from_errno();
367 pid->id = calloc(1, sizeof(*pid->id));
368 if (pid->id == NULL) {
369 err = got_error_from_errno();
374 if (!got_parse_sha1_digest(pid->id->sha1, s)) {
375 err = got_error(GOT_ERR_BAD_OBJ_DATA);
380 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, pid, entry);
381 (*commit)->nparents++;
383 remain -= SHA1_DIGEST_STRING_LENGTH;
384 s += SHA1_DIGEST_STRING_LENGTH;
387 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
388 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
393 err = got_error(GOT_ERR_BAD_OBJ_DATA);
399 err = got_error(GOT_ERR_BAD_OBJ_DATA);
403 (*commit)->author = strdup(s);
404 if ((*commit)->author == NULL) {
405 err = got_error_from_errno();
408 s += strlen((*commit)->author) + 1;
409 remain -= strlen((*commit)->author) + 1;
412 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
413 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
418 err = got_error(GOT_ERR_BAD_OBJ_DATA);
424 err = got_error(GOT_ERR_BAD_OBJ_DATA);
428 (*commit)->committer = strdup(s);
429 if ((*commit)->committer == NULL) {
430 err = got_error_from_errno();
433 s += strlen((*commit)->committer) + 1;
434 remain -= strlen((*commit)->committer) + 1;
437 (*commit)->logmsg = strndup(s, remain);
438 if ((*commit)->logmsg == NULL) {
439 err = got_error_from_errno();
444 got_object_commit_close(*commit);
451 tree_entry_close(struct got_tree_entry *te)
458 static const struct got_error *
459 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
462 char *p = buf, *space;
463 const struct got_error *err = NULL;
465 *te = calloc(1, sizeof(**te));
467 return got_error_from_errno();
469 (*te)->id = calloc(1, sizeof(*(*te)->id));
470 if ((*te)->id == NULL) {
471 err = got_error_from_errno();
477 *elen = strlen(buf) + 1;
478 if (*elen > maxlen) {
481 return got_error(GOT_ERR_BAD_OBJ_DATA);
484 space = strchr(buf, ' ');
486 err = got_error(GOT_ERR_BAD_OBJ_DATA);
492 if (*p < '0' && *p > '7') {
493 err = got_error(GOT_ERR_BAD_OBJ_DATA);
497 (*te)->mode |= *p - '0';
501 (*te)->name = strdup(space + 1);
502 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
503 err = got_error(GOT_ERR_BAD_OBJ_DATA);
506 buf += strlen(buf) + 1;
507 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
508 *elen += SHA1_DIGEST_LENGTH;
511 tree_entry_close(*te);
517 static const struct got_error *
518 parse_tree_object(struct got_tree_object **tree, struct got_repository *repo,
519 uint8_t *buf, size_t len)
521 const struct got_error *err;
524 *tree = calloc(1, sizeof(**tree));
526 return got_error_from_errno();
528 SIMPLEQ_INIT(&(*tree)->entries);
531 struct got_tree_entry *te;
534 err = parse_tree_entry(&te, &elen, buf, remain);
538 SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
544 got_object_tree_close(*tree);
545 return got_error(GOT_ERR_BAD_OBJ_DATA);
551 static const struct got_error *
552 read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
554 const struct got_error *err = NULL;
555 static const size_t blocksize = 512;
556 size_t n, total, remain;
562 buf = calloc(1, blocksize);
564 return got_error_from_errno();
571 newbuf = reallocarray(buf, 1, total + blocksize);
572 if (newbuf == NULL) {
573 err = got_error_from_errno();
579 n = fread(buf + total, 1, remain, f);
582 err = got_ferror(f, GOT_ERR_IO);
600 static const struct got_error *
601 read_commit_object(struct got_commit_object **commit,
602 struct got_repository *repo, struct got_object *obj, FILE *f)
604 const struct got_error *err = NULL;
608 if (obj->flags & GOT_OBJ_FLAG_PACKED)
609 err = read_to_mem(&p, &len, f);
611 err = got_inflate_to_mem(&p, &len, f);
615 if (len < obj->hdrlen + obj->size) {
616 err = got_error(GOT_ERR_BAD_OBJ_DATA);
620 /* Skip object header. */
622 err = parse_commit_object(commit, p + obj->hdrlen, len);
628 const struct got_error *
629 got_object_commit_open(struct got_commit_object **commit,
630 struct got_repository *repo, struct got_object *obj)
632 const struct got_error *err = NULL;
634 if (obj->type != GOT_OBJ_TYPE_COMMIT)
635 return got_error(GOT_ERR_OBJ_TYPE);
637 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
640 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
644 err = parse_commit_object(commit, buf, len);
648 err = open_loose_object(&f, obj, repo);
651 err = read_commit_object(commit, repo, obj, f);
658 got_object_commit_close(struct got_commit_object *commit)
660 struct got_parent_id *pid;
662 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
663 pid = SIMPLEQ_FIRST(&commit->parent_ids);
664 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
669 free(commit->tree_id);
670 free(commit->author);
671 free(commit->committer);
672 free(commit->logmsg);
676 static const struct got_error *
677 read_tree_object(struct got_tree_object **tree,
678 struct got_repository *repo, struct got_object *obj, FILE *f)
680 const struct got_error *err = NULL;
684 if (obj->flags & GOT_OBJ_FLAG_PACKED)
685 err = read_to_mem(&p, &len, f);
687 err = got_inflate_to_mem(&p, &len, f);
691 if (len < obj->hdrlen + obj->size) {
692 err = got_error(GOT_ERR_BAD_OBJ_DATA);
696 /* Skip object header. */
698 err = parse_tree_object(tree, repo, p + obj->hdrlen, len);
704 const struct got_error *
705 got_object_tree_open(struct got_tree_object **tree,
706 struct got_repository *repo, struct got_object *obj)
708 const struct got_error *err = NULL;
710 if (obj->type != GOT_OBJ_TYPE_TREE)
711 return got_error(GOT_ERR_OBJ_TYPE);
713 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
716 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
720 err = parse_tree_object(tree, repo, buf, len);
724 err = open_loose_object(&f, obj, repo);
727 err = read_tree_object(tree, repo, obj, f);
734 got_object_tree_close(struct got_tree_object *tree)
736 struct got_tree_entry *te;
738 while (!SIMPLEQ_EMPTY(&tree->entries)) {
739 te = SIMPLEQ_FIRST(&tree->entries);
740 SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
741 tree_entry_close(te);
747 const struct got_error *
748 got_object_blob_open(struct got_blob_object **blob,
749 struct got_repository *repo, struct got_object *obj, size_t blocksize)
751 const struct got_error *err = NULL;
753 if (obj->type != GOT_OBJ_TYPE_BLOB)
754 return got_error(GOT_ERR_OBJ_TYPE);
756 if (blocksize < obj->hdrlen)
757 return got_error(GOT_ERR_NO_SPACE);
759 *blob = calloc(1, sizeof(**blob));
761 return got_error_from_errno();
763 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
764 (*blob)->read_buf = calloc(1, blocksize);
765 if ((*blob)->read_buf == NULL) {
766 err = got_error_from_errno();
771 err = got_packfile_extract_object(&((*blob)->f), obj, repo);
773 free((*blob)->read_buf);
779 err = open_loose_object(&((*blob)->f), obj, repo);
786 err = got_inflate_init(&(*blob)->zb, NULL, blocksize);
794 (*blob)->read_buf = (*blob)->zb.outbuf;
795 (*blob)->flags |= GOT_BLOB_F_COMPRESSED;
798 (*blob)->hdrlen = obj->hdrlen;
799 (*blob)->blocksize = blocksize;
800 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
806 got_object_blob_close(struct got_blob_object *blob)
808 if (blob->flags & GOT_BLOB_F_COMPRESSED)
809 got_inflate_end(&blob->zb);
811 free(blob->read_buf);
817 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
819 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
823 got_object_blob_get_hdrlen(struct got_blob_object *blob)
829 got_object_blob_get_read_buf(struct got_blob_object *blob)
831 return blob->read_buf;
834 const struct got_error *
835 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
839 if (blob->flags & GOT_BLOB_F_COMPRESSED)
840 return got_inflate_read(&blob->zb, blob->f, outlenp);
842 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
843 if (n == 0 && ferror(blob->f))
844 return got_ferror(blob->f, GOT_ERR_IO);