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));
92 struct got_object_id *
93 got_object_get_id(struct got_object *obj)
95 return got_object_id_dup(&obj->id);
99 got_object_get_type(struct got_object *obj)
102 case GOT_OBJ_TYPE_COMMIT:
103 case GOT_OBJ_TYPE_TREE:
104 case GOT_OBJ_TYPE_BLOB:
105 case GOT_OBJ_TYPE_TAG:
116 static const struct got_error *
117 parse_object_header(struct got_object **obj, char *buf, size_t len)
119 const char *obj_tags[] = {
124 const int obj_types[] = {
130 size_t size = 0, hdrlen = 0;
132 char *p = strchr(buf, '\0');
135 return got_error(GOT_ERR_BAD_OBJ_HDR);
137 hdrlen = strlen(buf) + 1 /* '\0' */;
139 for (i = 0; i < nitems(obj_tags); i++) {
140 const char *tag = obj_tags[i];
141 size_t tlen = strlen(tag);
144 if (strncmp(buf, tag, tlen) != 0)
149 return got_error(GOT_ERR_BAD_OBJ_HDR);
150 size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
152 return got_error(GOT_ERR_BAD_OBJ_HDR);
157 return got_error(GOT_ERR_BAD_OBJ_HDR);
159 *obj = calloc(1, sizeof(**obj));
161 return got_error_from_errno();
163 (*obj)->hdrlen = hdrlen;
168 static const struct got_error *
169 read_object_header(struct got_object **obj, struct got_repository *repo,
172 const struct got_error *err;
173 struct got_zstream_buf zb;
175 const size_t zbsize = 64;
176 size_t outlen, totlen;
179 buf = calloc(zbsize, sizeof(char));
181 return got_error_from_errno();
183 err = got_inflate_init(&zb, NULL, zbsize);
190 err = got_inflate_read(&zb, f, &outlen);
193 if (strchr(zb.outbuf, '\0') == NULL) {
194 buf = recallocarray(buf, 1 + i, 2 + i, zbsize);
196 err = got_error_from_errno();
200 memcpy(buf + totlen, zb.outbuf, outlen);
203 } while (strchr(zb.outbuf, '\0') == NULL);
205 err = parse_object_header(obj, buf, totlen);
207 got_inflate_end(&zb);
211 static const struct got_error *
212 object_path(char **path, struct got_object_id *id, struct got_repository *repo)
214 const struct got_error *err = NULL;
216 char *path_objects = got_repo_get_path_objects(repo);
218 if (path_objects == NULL)
219 return got_error_from_errno();
221 err = got_object_id_str(&hex, id);
225 if (asprintf(path, "%s/%.2x/%s", path_objects,
226 id->sha1[0], hex + 2) == -1)
227 err = got_error_from_errno();
234 static const struct got_error *
235 open_loose_object(FILE **f, struct got_object *obj, struct got_repository *repo)
237 const struct got_error *err = NULL;
240 err = object_path(&path, &obj->id, repo);
243 *f = fopen(path, "rb");
245 err = got_error_from_errno();
253 const struct got_error *
254 got_object_open(struct got_object **obj, struct got_repository *repo,
255 struct got_object_id *id)
257 const struct got_error *err = NULL;
261 err = object_path(&path, id, repo);
265 f = fopen(path, "rb");
267 if (errno != ENOENT) {
268 err = got_error_from_errno();
271 err = got_packfile_open_object(obj, id, repo);
275 err = got_error(GOT_ERR_NO_OBJ);
277 err = read_object_header(obj, repo, f);
280 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
290 const struct got_error *
291 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
294 struct got_object_id id;
296 if (!got_parse_sha1_digest(id.sha1, id_str))
297 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
299 return got_object_open(obj, repo, &id);
303 got_object_close(struct got_object *obj)
305 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
306 struct got_delta *delta;
307 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
308 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
309 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
310 got_delta_close(delta);
313 if (obj->flags & GOT_OBJ_FLAG_PACKED)
314 free(obj->path_packfile);
318 static const struct got_error *
319 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
321 const struct got_error *err = NULL;
324 ssize_t remain = (ssize_t)len;
326 *commit = calloc(1, sizeof(**commit));
328 return got_error_from_errno();
329 (*commit)->tree_id = calloc(1, sizeof(*(*commit)->tree_id));
330 if ((*commit)->tree_id == NULL) {
331 err = got_error_from_errno();
337 SIMPLEQ_INIT(&(*commit)->parent_ids);
339 tlen = strlen(GOT_COMMIT_TAG_TREE);
340 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
342 if (remain < SHA1_DIGEST_STRING_LENGTH) {
343 err = got_error(GOT_ERR_BAD_OBJ_DATA);
347 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
348 err = got_error(GOT_ERR_BAD_OBJ_DATA);
351 remain -= SHA1_DIGEST_STRING_LENGTH;
352 s += SHA1_DIGEST_STRING_LENGTH;
354 err = got_error(GOT_ERR_BAD_OBJ_DATA);
358 tlen = strlen(GOT_COMMIT_TAG_PARENT);
359 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
360 struct got_parent_id *pid;
363 if (remain < SHA1_DIGEST_STRING_LENGTH) {
364 err = got_error(GOT_ERR_BAD_OBJ_DATA);
368 pid = calloc(1, sizeof(*pid));
370 err = got_error_from_errno();
373 pid->id = calloc(1, sizeof(*pid->id));
374 if (pid->id == NULL) {
375 err = got_error_from_errno();
380 if (!got_parse_sha1_digest(pid->id->sha1, s)) {
381 err = got_error(GOT_ERR_BAD_OBJ_DATA);
386 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, pid, entry);
387 (*commit)->nparents++;
389 remain -= SHA1_DIGEST_STRING_LENGTH;
390 s += SHA1_DIGEST_STRING_LENGTH;
393 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
394 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
399 err = got_error(GOT_ERR_BAD_OBJ_DATA);
405 err = got_error(GOT_ERR_BAD_OBJ_DATA);
409 (*commit)->author = strdup(s);
410 if ((*commit)->author == NULL) {
411 err = got_error_from_errno();
414 s += strlen((*commit)->author) + 1;
415 remain -= strlen((*commit)->author) + 1;
418 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
419 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
424 err = got_error(GOT_ERR_BAD_OBJ_DATA);
430 err = got_error(GOT_ERR_BAD_OBJ_DATA);
434 (*commit)->committer = strdup(s);
435 if ((*commit)->committer == NULL) {
436 err = got_error_from_errno();
439 s += strlen((*commit)->committer) + 1;
440 remain -= strlen((*commit)->committer) + 1;
443 (*commit)->logmsg = strndup(s, remain);
444 if ((*commit)->logmsg == NULL) {
445 err = got_error_from_errno();
450 got_object_commit_close(*commit);
457 tree_entry_close(struct got_tree_entry *te)
464 static const struct got_error *
465 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
468 char *p = buf, *space;
469 const struct got_error *err = NULL;
471 *te = calloc(1, sizeof(**te));
473 return got_error_from_errno();
475 (*te)->id = calloc(1, sizeof(*(*te)->id));
476 if ((*te)->id == NULL) {
477 err = got_error_from_errno();
483 *elen = strlen(buf) + 1;
484 if (*elen > maxlen) {
487 return got_error(GOT_ERR_BAD_OBJ_DATA);
490 space = strchr(buf, ' ');
492 err = got_error(GOT_ERR_BAD_OBJ_DATA);
498 if (*p < '0' && *p > '7') {
499 err = got_error(GOT_ERR_BAD_OBJ_DATA);
503 (*te)->mode |= *p - '0';
507 (*te)->name = strdup(space + 1);
508 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
509 err = got_error(GOT_ERR_BAD_OBJ_DATA);
512 buf += strlen(buf) + 1;
513 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
514 *elen += SHA1_DIGEST_LENGTH;
517 tree_entry_close(*te);
523 static const struct got_error *
524 parse_tree_object(struct got_tree_object **tree, struct got_repository *repo,
525 uint8_t *buf, size_t len)
527 const struct got_error *err;
530 *tree = calloc(1, sizeof(**tree));
532 return got_error_from_errno();
534 SIMPLEQ_INIT(&(*tree)->entries);
537 struct got_tree_entry *te;
540 err = parse_tree_entry(&te, &elen, buf, remain);
544 SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
550 got_object_tree_close(*tree);
551 return got_error(GOT_ERR_BAD_OBJ_DATA);
557 static const struct got_error *
558 read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
560 const struct got_error *err = NULL;
561 static const size_t blocksize = 512;
562 size_t n, total, remain;
568 buf = calloc(1, blocksize);
570 return got_error_from_errno();
577 newbuf = reallocarray(buf, 1, total + blocksize);
578 if (newbuf == NULL) {
579 err = got_error_from_errno();
585 n = fread(buf + total, 1, remain, f);
588 err = got_ferror(f, GOT_ERR_IO);
606 static const struct got_error *
607 read_commit_object(struct got_commit_object **commit,
608 struct got_repository *repo, struct got_object *obj, FILE *f)
610 const struct got_error *err = NULL;
614 if (obj->flags & GOT_OBJ_FLAG_PACKED)
615 err = read_to_mem(&p, &len, f);
617 err = got_inflate_to_mem(&p, &len, f);
621 if (len < obj->hdrlen + obj->size) {
622 err = got_error(GOT_ERR_BAD_OBJ_DATA);
626 /* Skip object header. */
628 err = parse_commit_object(commit, p + obj->hdrlen, len);
634 const struct got_error *
635 got_object_commit_open(struct got_commit_object **commit,
636 struct got_repository *repo, struct got_object *obj)
638 const struct got_error *err = NULL;
640 if (obj->type != GOT_OBJ_TYPE_COMMIT)
641 return got_error(GOT_ERR_OBJ_TYPE);
643 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
646 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
650 err = parse_commit_object(commit, buf, len);
654 err = open_loose_object(&f, obj, repo);
657 err = read_commit_object(commit, repo, obj, f);
664 got_object_commit_close(struct got_commit_object *commit)
666 struct got_parent_id *pid;
668 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
669 pid = SIMPLEQ_FIRST(&commit->parent_ids);
670 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
675 free(commit->tree_id);
676 free(commit->author);
677 free(commit->committer);
678 free(commit->logmsg);
682 static const struct got_error *
683 read_tree_object(struct got_tree_object **tree,
684 struct got_repository *repo, struct got_object *obj, FILE *f)
686 const struct got_error *err = NULL;
690 if (obj->flags & GOT_OBJ_FLAG_PACKED)
691 err = read_to_mem(&p, &len, f);
693 err = got_inflate_to_mem(&p, &len, f);
697 if (len < obj->hdrlen + obj->size) {
698 err = got_error(GOT_ERR_BAD_OBJ_DATA);
702 /* Skip object header. */
704 err = parse_tree_object(tree, repo, p + obj->hdrlen, len);
710 const struct got_error *
711 got_object_tree_open(struct got_tree_object **tree,
712 struct got_repository *repo, struct got_object *obj)
714 const struct got_error *err = NULL;
716 if (obj->type != GOT_OBJ_TYPE_TREE)
717 return got_error(GOT_ERR_OBJ_TYPE);
719 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
722 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
726 err = parse_tree_object(tree, repo, buf, len);
730 err = open_loose_object(&f, obj, repo);
733 err = read_tree_object(tree, repo, obj, f);
740 got_object_tree_close(struct got_tree_object *tree)
742 struct got_tree_entry *te;
744 while (!SIMPLEQ_EMPTY(&tree->entries)) {
745 te = SIMPLEQ_FIRST(&tree->entries);
746 SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
747 tree_entry_close(te);
753 const struct got_error *
754 got_object_blob_open(struct got_blob_object **blob,
755 struct got_repository *repo, struct got_object *obj, size_t blocksize)
757 const struct got_error *err = NULL;
759 if (obj->type != GOT_OBJ_TYPE_BLOB)
760 return got_error(GOT_ERR_OBJ_TYPE);
762 if (blocksize < obj->hdrlen)
763 return got_error(GOT_ERR_NO_SPACE);
765 *blob = calloc(1, sizeof(**blob));
767 return got_error_from_errno();
769 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
770 (*blob)->read_buf = calloc(1, blocksize);
771 if ((*blob)->read_buf == NULL) {
772 err = got_error_from_errno();
777 err = got_packfile_extract_object(&((*blob)->f), obj, repo);
779 free((*blob)->read_buf);
785 err = open_loose_object(&((*blob)->f), obj, repo);
792 err = got_inflate_init(&(*blob)->zb, NULL, blocksize);
800 (*blob)->read_buf = (*blob)->zb.outbuf;
801 (*blob)->flags |= GOT_BLOB_F_COMPRESSED;
804 (*blob)->hdrlen = obj->hdrlen;
805 (*blob)->blocksize = blocksize;
806 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
812 got_object_blob_close(struct got_blob_object *blob)
814 if (blob->flags & GOT_BLOB_F_COMPRESSED)
815 got_inflate_end(&blob->zb);
817 free(blob->read_buf);
823 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
825 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
829 got_object_blob_get_hdrlen(struct got_blob_object *blob)
835 got_object_blob_get_read_buf(struct got_blob_object *blob)
837 return blob->read_buf;
840 const struct got_error *
841 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
845 if (blob->flags & GOT_BLOB_F_COMPRESSED)
846 return got_inflate_read(&blob->zb, blob->f, outlenp);
848 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
849 if (n == 0 && ferror(blob->f))
850 return got_ferror(blob->f, GOT_ERR_IO);