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 fopen_object(FILE **f, struct got_object *obj, struct got_repository *repo)
204 const struct got_error *err = NULL;
207 if (obj->flags & GOT_OBJ_FLAG_PACKED)
208 return got_packfile_extract_object(f, obj, repo);
210 err = object_path(&path, &obj->id, repo);
213 *f = fopen(path, "rb");
215 err = got_error_from_errno();
223 const struct got_error *
224 got_object_open(struct got_object **obj, struct got_repository *repo,
225 struct got_object_id *id)
227 const struct got_error *err = NULL;
231 err = object_path(&path, id, repo);
235 f = fopen(path, "rb");
237 if (errno != ENOENT) {
238 err = got_error_from_errno();
241 err = got_packfile_open_object(obj, id, repo);
245 err = got_error(GOT_ERR_NO_OBJ);
247 err = read_object_header(obj, repo, f);
250 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
261 got_object_close(struct got_object *obj)
263 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
264 struct got_delta *delta;
265 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
266 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
267 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
268 got_delta_close(delta);
271 if (obj->flags & GOT_OBJ_FLAG_PACKED)
272 free(obj->path_packfile);
277 commit_object_valid(struct got_commit_object *commit)
286 for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
287 if (commit->tree_id.sha1[i] == 0)
290 if (n == SHA1_DIGEST_LENGTH)
296 static const struct got_error *
297 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
299 const struct got_error *err = NULL;
302 ssize_t remain = (ssize_t)len;
304 *commit = calloc(1, sizeof(**commit));
306 return got_error(GOT_ERR_NO_MEM);
308 SIMPLEQ_INIT(&(*commit)->parent_ids);
310 tlen = strlen(GOT_COMMIT_TAG_TREE);
311 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
313 if (remain < SHA1_DIGEST_STRING_LENGTH) {
314 err = got_error(GOT_ERR_BAD_OBJ_DATA);
318 if (!got_parse_sha1_digest((*commit)->tree_id.sha1, s)) {
319 err = got_error(GOT_ERR_BAD_OBJ_DATA);
322 remain -= SHA1_DIGEST_STRING_LENGTH;
323 s += SHA1_DIGEST_STRING_LENGTH;
325 err = got_error(GOT_ERR_BAD_OBJ_DATA);
329 tlen = strlen(GOT_COMMIT_TAG_PARENT);
330 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
331 struct got_parent_id *pid;
334 if (remain < SHA1_DIGEST_STRING_LENGTH) {
335 err = got_error(GOT_ERR_BAD_OBJ_DATA);
339 pid = calloc(1, sizeof(*pid));
341 err = got_error(GOT_ERR_NO_MEM);
345 if (!got_parse_sha1_digest(pid->id.sha1, s)) {
346 err = got_error(GOT_ERR_BAD_OBJ_DATA);
349 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, pid, entry);
350 (*commit)->nparents++;
352 s += SHA1_DIGEST_STRING_LENGTH;
355 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
356 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
361 err = got_error(GOT_ERR_BAD_OBJ_DATA);
367 err = got_error(GOT_ERR_BAD_OBJ_DATA);
371 (*commit)->author = strdup(s);
372 if ((*commit)->author == NULL) {
373 err = got_error(GOT_ERR_NO_MEM);
376 s += strlen((*commit)->author) + 1;
379 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
380 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
385 err = got_error(GOT_ERR_BAD_OBJ_DATA);
391 err = got_error(GOT_ERR_BAD_OBJ_DATA);
395 (*commit)->committer = strdup(s);
396 if ((*commit)->committer == NULL) {
397 err = got_error(GOT_ERR_NO_MEM);
400 s += strlen((*commit)->committer) + 1;
403 (*commit)->logmsg = strdup(s);
406 got_object_commit_close(*commit);
411 tree_entry_close(struct got_tree_entry *te)
417 static const struct got_error *
418 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
421 char *p = buf, *space;
422 const struct got_error *err = NULL;
423 char hex[SHA1_DIGEST_STRING_LENGTH];
425 *te = calloc(1, sizeof(**te));
427 return got_error(GOT_ERR_NO_MEM);
429 *elen = strlen(buf) + 1;
430 if (*elen > maxlen) {
432 return got_error(GOT_ERR_BAD_OBJ_DATA);
435 space = strchr(buf, ' ');
438 return got_error(GOT_ERR_BAD_OBJ_DATA);
441 if (*p < '0' && *p > '7') {
442 err = got_error(GOT_ERR_BAD_OBJ_DATA);
446 (*te)->mode |= *p - '0';
450 (*te)->name = strdup(space + 1);
451 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
452 err = got_error(GOT_ERR_BAD_OBJ_DATA);
455 buf += strlen(buf) + 1;
456 memcpy((*te)->id.sha1, buf, SHA1_DIGEST_LENGTH);
457 *elen += SHA1_DIGEST_LENGTH;
460 tree_entry_close(*te);
464 static const struct got_error *
465 parse_tree_object(struct got_tree_object **tree, struct got_repository *repo,
466 char *buf, size_t len)
468 const struct got_error *err;
472 *tree = calloc(1, sizeof(**tree));
474 return got_error(GOT_ERR_NO_MEM);
476 SIMPLEQ_INIT(&(*tree)->entries);
479 struct got_tree_entry *te;
482 err = parse_tree_entry(&te, &elen, buf, remain);
486 SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
492 got_object_tree_close(*tree);
493 return got_error(GOT_ERR_BAD_OBJ_DATA);
499 static const struct got_error *
500 read_commit_object(struct got_commit_object **commit,
501 struct got_repository *repo, struct got_object *obj, FILE *f)
503 const struct got_error *err = NULL;
504 struct got_zstream_buf zb;
509 err = got_inflate_init(&zb, 8192);
513 err = got_inflate_read(&zb, f, &len);
517 if (len < obj->hdrlen + obj->size) {
518 err = got_error(GOT_ERR_BAD_OBJ_DATA);
522 /* Skip object header. */
524 err = parse_commit_object(commit, zb.outbuf + obj->hdrlen, len);
526 got_inflate_end(&zb);
530 const struct got_error *
531 got_object_commit_open(struct got_commit_object **commit,
532 struct got_repository *repo, struct got_object *obj)
534 const struct got_error *err = NULL;
537 if (obj->type != GOT_OBJ_TYPE_COMMIT)
538 return got_error(GOT_ERR_OBJ_TYPE);
540 err = fopen_object(&f, obj, repo);
544 err = read_commit_object(commit, repo, obj, f);
550 got_object_commit_close(struct got_commit_object *commit)
552 struct got_parent_id *pid;
554 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
555 pid = SIMPLEQ_FIRST(&commit->parent_ids);
556 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
560 free(commit->author);
561 free(commit->committer);
562 free(commit->logmsg);
566 static const struct got_error *
567 read_tree_object(struct got_tree_object **tree,
568 struct got_repository *repo, struct got_object *obj, FILE *f)
570 const struct got_error *err = NULL;
571 struct got_zstream_buf zb;
576 err = got_inflate_init(&zb, 8192);
580 err = got_inflate_read(&zb, f, &len);
584 if (len < obj->hdrlen + obj->size) {
585 err = got_error(GOT_ERR_BAD_OBJ_DATA);
589 /* Skip object header. */
591 err = parse_tree_object(tree, repo, zb.outbuf + obj->hdrlen, len);
593 got_inflate_end(&zb);
597 const struct got_error *
598 got_object_tree_open(struct got_tree_object **tree,
599 struct got_repository *repo, struct got_object *obj)
601 const struct got_error *err = NULL;
604 if (obj->type != GOT_OBJ_TYPE_TREE)
605 return got_error(GOT_ERR_OBJ_TYPE);
607 err = fopen_object(&f, obj, repo);
611 err = read_tree_object(tree, repo, obj, f);
617 got_object_tree_close(struct got_tree_object *tree)
619 struct got_tree_entry *te;
621 while (!SIMPLEQ_EMPTY(&tree->entries)) {
622 te = SIMPLEQ_FIRST(&tree->entries);
623 SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
624 tree_entry_close(te);
630 const struct got_error *
631 got_object_blob_open(struct got_blob_object **blob,
632 struct got_repository *repo, struct got_object *obj, size_t blocksize)
634 const struct got_error *err = NULL;
636 if (obj->type != GOT_OBJ_TYPE_BLOB)
637 return got_error(GOT_ERR_OBJ_TYPE);
639 if (blocksize < obj->hdrlen)
640 return got_error(GOT_ERR_NO_SPACE);
642 *blob = calloc(1, sizeof(**blob));
644 return got_error(GOT_ERR_NO_MEM);
646 err = fopen_object(&((*blob)->f), obj, repo);
652 err = got_inflate_init(&(*blob)->zb, blocksize);
659 (*blob)->hdrlen = obj->hdrlen;
660 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
666 got_object_blob_close(struct got_blob_object *blob)
668 got_inflate_end(&blob->zb);
673 const struct got_error *
674 got_object_blob_read_block(struct got_blob_object *blob, size_t *outlenp)
676 return got_inflate_read(&blob->zb, blob->f, outlenp);