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_lib.h"
34 #include "got_delta_lib.h"
35 #include "got_pack_lib.h"
36 #include "got_zbuf_lib.h"
37 #include "got_object_lib.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;
157 const size_t zbsize = 64;
158 size_t outlen, totlen;
161 buf = calloc(zbsize, sizeof(char));
163 return got_error(GOT_ERR_NO_MEM);
165 err = got_inflate_init(&zb, zbsize);
172 err = got_inflate_read(&zb, f, &outlen);
175 if (strchr(zb.outbuf, '\0') == NULL) {
176 buf = recallocarray(buf, 1 + i, 2 + i, zbsize);
178 err = got_error(GOT_ERR_NO_MEM);
182 memcpy(buf + totlen, zb.outbuf, outlen);
185 } while (strchr(zb.outbuf, '\0') == NULL);
187 err = parse_object_header(obj, buf, totlen);
189 got_inflate_end(&zb);
193 static const struct got_error *
194 object_path(char **path, struct got_object_id *id, struct got_repository *repo)
196 const struct got_error *err = NULL;
198 char *path_objects = got_repo_get_path_objects(repo);
200 if (path_objects == NULL)
201 return got_error(GOT_ERR_NO_MEM);
203 err = got_object_id_str(&hex, id);
207 if (asprintf(path, "%s/%.2x/%s", path_objects,
208 id->sha1[0], hex + 2) == -1)
209 err = got_error(GOT_ERR_NO_MEM);
216 static const struct got_error *
217 open_loose_object(FILE **f, struct got_object *obj, struct got_repository *repo)
219 const struct got_error *err = NULL;
222 err = object_path(&path, &obj->id, repo);
225 *f = fopen(path, "rb");
227 err = got_error_from_errno();
235 const struct got_error *
236 got_object_open(struct got_object **obj, struct got_repository *repo,
237 struct got_object_id *id)
239 const struct got_error *err = NULL;
243 err = object_path(&path, id, repo);
247 f = fopen(path, "rb");
249 if (errno != ENOENT) {
250 err = got_error_from_errno();
253 err = got_packfile_open_object(obj, id, repo);
257 err = got_error(GOT_ERR_NO_OBJ);
259 err = read_object_header(obj, repo, f);
262 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
272 const struct got_error *
273 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
276 struct got_object_id id;
278 if (!got_parse_sha1_digest(id.sha1, id_str))
279 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
281 return got_object_open(obj, repo, &id);
285 got_object_close(struct got_object *obj)
287 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
288 struct got_delta *delta;
289 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
290 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
291 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
292 got_delta_close(delta);
295 if (obj->flags & GOT_OBJ_FLAG_PACKED)
296 free(obj->path_packfile);
300 static const struct got_error *
301 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
303 const struct got_error *err = NULL;
306 ssize_t remain = (ssize_t)len;
308 *commit = calloc(1, sizeof(**commit));
310 return got_error(GOT_ERR_NO_MEM);
311 (*commit)->tree_id = calloc(1, sizeof(*(*commit)->tree_id));
312 if ((*commit)->tree_id == NULL) {
315 return got_error(GOT_ERR_NO_MEM);
318 SIMPLEQ_INIT(&(*commit)->parent_ids);
320 tlen = strlen(GOT_COMMIT_TAG_TREE);
321 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
323 if (remain < SHA1_DIGEST_STRING_LENGTH) {
324 err = got_error(GOT_ERR_BAD_OBJ_DATA);
328 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
329 err = got_error(GOT_ERR_BAD_OBJ_DATA);
332 remain -= SHA1_DIGEST_STRING_LENGTH;
333 s += SHA1_DIGEST_STRING_LENGTH;
335 err = got_error(GOT_ERR_BAD_OBJ_DATA);
339 tlen = strlen(GOT_COMMIT_TAG_PARENT);
340 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
341 struct got_parent_id *pid;
344 if (remain < SHA1_DIGEST_STRING_LENGTH) {
345 err = got_error(GOT_ERR_BAD_OBJ_DATA);
349 pid = calloc(1, sizeof(*pid));
351 err = got_error(GOT_ERR_NO_MEM);
354 pid->id = calloc(1, sizeof(*pid->id));
355 if (pid->id == NULL) {
357 err = got_error(GOT_ERR_NO_MEM);
361 if (!got_parse_sha1_digest(pid->id->sha1, s)) {
362 err = got_error(GOT_ERR_BAD_OBJ_DATA);
367 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, pid, entry);
368 (*commit)->nparents++;
370 remain -= SHA1_DIGEST_STRING_LENGTH;
371 s += SHA1_DIGEST_STRING_LENGTH;
374 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
375 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
380 err = got_error(GOT_ERR_BAD_OBJ_DATA);
386 err = got_error(GOT_ERR_BAD_OBJ_DATA);
390 (*commit)->author = strdup(s);
391 if ((*commit)->author == NULL) {
392 err = got_error(GOT_ERR_NO_MEM);
395 s += strlen((*commit)->author) + 1;
396 remain -= strlen((*commit)->author) + 1;
399 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
400 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
405 err = got_error(GOT_ERR_BAD_OBJ_DATA);
411 err = got_error(GOT_ERR_BAD_OBJ_DATA);
415 (*commit)->committer = strdup(s);
416 if ((*commit)->committer == NULL) {
417 err = got_error(GOT_ERR_NO_MEM);
420 s += strlen((*commit)->committer) + 1;
421 remain -= strlen((*commit)->committer) + 1;
424 (*commit)->logmsg = strndup(s, remain);
425 if ((*commit)->logmsg == NULL) {
426 err = got_error(GOT_ERR_NO_MEM);
431 got_object_commit_close(*commit);
438 tree_entry_close(struct got_tree_entry *te)
445 static const struct got_error *
446 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
449 char *p = buf, *space;
450 const struct got_error *err = NULL;
452 *te = calloc(1, sizeof(**te));
454 return got_error(GOT_ERR_NO_MEM);
456 (*te)->id = calloc(1, sizeof(*(*te)->id));
457 if ((*te)->id == NULL) {
460 return got_error(GOT_ERR_NO_MEM);
463 *elen = strlen(buf) + 1;
464 if (*elen > maxlen) {
467 return got_error(GOT_ERR_BAD_OBJ_DATA);
470 space = strchr(buf, ' ');
474 return got_error(GOT_ERR_BAD_OBJ_DATA);
477 if (*p < '0' && *p > '7') {
478 err = got_error(GOT_ERR_BAD_OBJ_DATA);
482 (*te)->mode |= *p - '0';
486 (*te)->name = strdup(space + 1);
487 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
488 err = got_error(GOT_ERR_BAD_OBJ_DATA);
491 buf += strlen(buf) + 1;
492 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
493 *elen += SHA1_DIGEST_LENGTH;
496 tree_entry_close(*te);
502 static const struct got_error *
503 parse_tree_object(struct got_tree_object **tree, struct got_repository *repo,
504 char *buf, size_t len)
506 const struct got_error *err;
509 *tree = calloc(1, sizeof(**tree));
511 return got_error(GOT_ERR_NO_MEM);
513 SIMPLEQ_INIT(&(*tree)->entries);
516 struct got_tree_entry *te;
519 err = parse_tree_entry(&te, &elen, buf, remain);
523 SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
529 got_object_tree_close(*tree);
530 return got_error(GOT_ERR_BAD_OBJ_DATA);
536 static const struct got_error *
537 read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
539 const struct got_error *err = NULL;
540 static const size_t blocksize = 512;
541 size_t n, total, remain;
547 buf = calloc(1, blocksize);
549 return got_error(GOT_ERR_NO_MEM);
556 newbuf = reallocarray(buf, 1, total + blocksize);
557 if (newbuf == NULL) {
558 err = got_error(GOT_ERR_NO_MEM);
564 n = fread(buf + total, 1, remain, f);
567 err = got_ferror(f, GOT_ERR_IO);
585 static const struct got_error *
586 read_commit_object(struct got_commit_object **commit,
587 struct got_repository *repo, struct got_object *obj, FILE *f)
589 const struct got_error *err = NULL;
593 if (obj->flags & GOT_OBJ_FLAG_PACKED)
594 err = read_to_mem(&p, &len, f);
596 err = got_inflate_to_mem(&p, &len, f);
600 if (len < obj->hdrlen + obj->size) {
601 err = got_error(GOT_ERR_BAD_OBJ_DATA);
605 /* Skip object header. */
607 err = parse_commit_object(commit, p + obj->hdrlen, len);
613 const struct got_error *
614 got_object_commit_open(struct got_commit_object **commit,
615 struct got_repository *repo, struct got_object *obj)
617 const struct got_error *err = NULL;
620 if (obj->type != GOT_OBJ_TYPE_COMMIT)
621 return got_error(GOT_ERR_OBJ_TYPE);
623 if (obj->flags & GOT_OBJ_FLAG_PACKED)
624 err = got_packfile_extract_object(&f, obj, repo);
626 err = open_loose_object(&f, obj, repo);
630 err = read_commit_object(commit, repo, obj, f);
636 got_object_commit_close(struct got_commit_object *commit)
638 struct got_parent_id *pid;
640 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
641 pid = SIMPLEQ_FIRST(&commit->parent_ids);
642 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
647 free(commit->tree_id);
648 free(commit->author);
649 free(commit->committer);
650 free(commit->logmsg);
654 static const struct got_error *
655 read_tree_object(struct got_tree_object **tree,
656 struct got_repository *repo, struct got_object *obj, FILE *f)
658 const struct got_error *err = NULL;
662 if (obj->flags & GOT_OBJ_FLAG_PACKED)
663 err = read_to_mem(&p, &len, f);
665 err = got_inflate_to_mem(&p, &len, f);
669 if (len < obj->hdrlen + obj->size) {
670 err = got_error(GOT_ERR_BAD_OBJ_DATA);
674 /* Skip object header. */
676 err = parse_tree_object(tree, repo, p + obj->hdrlen, len);
682 const struct got_error *
683 got_object_tree_open(struct got_tree_object **tree,
684 struct got_repository *repo, struct got_object *obj)
686 const struct got_error *err = NULL;
689 if (obj->type != GOT_OBJ_TYPE_TREE)
690 return got_error(GOT_ERR_OBJ_TYPE);
692 if (obj->flags & GOT_OBJ_FLAG_PACKED)
693 err = got_packfile_extract_object(&f, obj, repo);
695 err = open_loose_object(&f, obj, repo);
699 err = read_tree_object(tree, repo, obj, f);
705 got_object_tree_close(struct got_tree_object *tree)
707 struct got_tree_entry *te;
709 while (!SIMPLEQ_EMPTY(&tree->entries)) {
710 te = SIMPLEQ_FIRST(&tree->entries);
711 SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
712 tree_entry_close(te);
718 const struct got_error *
719 got_object_blob_open(struct got_blob_object **blob,
720 struct got_repository *repo, struct got_object *obj, size_t blocksize)
722 const struct got_error *err = NULL;
724 if (obj->type != GOT_OBJ_TYPE_BLOB)
725 return got_error(GOT_ERR_OBJ_TYPE);
727 if (blocksize < obj->hdrlen)
728 return got_error(GOT_ERR_NO_SPACE);
730 *blob = calloc(1, sizeof(**blob));
732 return got_error(GOT_ERR_NO_MEM);
734 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
735 (*blob)->read_buf = calloc(1, blocksize);
736 if ((*blob)->read_buf == NULL)
737 return got_error(GOT_ERR_NO_MEM);
738 err = got_packfile_extract_object(&((*blob)->f), obj, repo);
742 err = open_loose_object(&((*blob)->f), obj, repo);
748 err = got_inflate_init(&(*blob)->zb, blocksize);
755 (*blob)->read_buf = (*blob)->zb.outbuf;
756 (*blob)->flags |= GOT_BLOB_F_COMPRESSED;
759 (*blob)->hdrlen = obj->hdrlen;
760 (*blob)->blocksize = blocksize;
761 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
767 got_object_blob_close(struct got_blob_object *blob)
769 if (blob->flags & GOT_BLOB_F_COMPRESSED)
770 got_inflate_end(&blob->zb);
772 free(blob->read_buf);
778 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
780 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
784 got_object_blob_get_hdrlen(struct got_blob_object *blob)
790 got_object_blob_get_read_buf(struct got_blob_object *blob)
792 return blob->read_buf;
795 const struct got_error *
796 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
800 if (blob->flags & GOT_BLOB_F_COMPRESSED)
801 return got_inflate_read(&blob->zb, blob->f, outlenp);
803 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
804 if (n == 0 && ferror(blob->f))
805 return got_ferror(blob->f, GOT_ERR_IO);