2 * Copyright (c) 2017 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>
28 #include "got_error.h"
29 #include "got_object.h"
30 #include "got_repository.h"
34 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
38 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
41 #define GOT_OBJ_TAG_COMMIT "commit"
42 #define GOT_OBJ_TAG_TREE "tree"
43 #define GOT_OBJ_TAG_BLOB "blob"
45 #define GOT_COMMIT_TAG_TREE "tree "
46 #define GOT_COMMIT_TAG_PARENT "parent "
47 #define GOT_COMMIT_TAG_AUTHOR "author "
48 #define GOT_COMMIT_TAG_COMMITTER "committer "
51 got_object_id_str(struct got_object_id *id, char *buf, size_t size)
57 if (size < SHA1_DIGEST_STRING_LENGTH)
60 for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
61 snprintf(hex, sizeof(hex), "%.2x", id->sha1[i]);
72 inflate_end(struct got_zstream_buf *zb)
79 static const struct got_error *
80 inflate_init(struct got_zstream_buf *zb, size_t bufsize)
82 const struct got_error *err = NULL;
84 memset(zb, 0, sizeof(*zb));
86 zb->z.zalloc = Z_NULL;
88 if (inflateInit(&zb->z) != Z_OK) {
89 err = got_error(GOT_ERR_IO);
93 zb->inlen = zb->outlen = bufsize;
95 zb->inbuf = calloc(1, zb->inlen);
96 if (zb->inbuf == NULL) {
97 err = got_error(GOT_ERR_NO_MEM);
101 zb->outbuf = calloc(1, zb->outlen);
102 if (zb->outbuf == NULL) {
103 err = got_error(GOT_ERR_NO_MEM);
113 static const struct got_error *
114 inflate_read(struct got_zstream_buf *zb, FILE *f, size_t *outlenp)
116 size_t last_total_out = zb->z.total_out;
117 z_stream *z = &zb->z;
120 z->next_out = zb->outbuf;
121 z->avail_out = zb->outlen;
123 if (z->avail_in == 0 && (zb->flags & GOT_ZSTREAM_F_HAVE_MORE) == 0) {
125 n = fread(zb->inbuf, 1, zb->inlen, f);
128 return got_error(GOT_ERR_IO);
132 z->next_in = zb->inbuf;
136 ret = inflate(z, Z_SYNC_FLUSH);
138 if (z->avail_out == 0)
139 zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
141 zb->flags &= ~GOT_ZSTREAM_F_HAVE_MORE;
142 } else if (ret != Z_STREAM_END)
143 return got_error(GOT_ERR_DECOMPRESSION);
145 *outlenp = z->total_out - last_total_out;
149 static const struct got_error *
150 parse_object_header(struct got_object **obj, char *buf, size_t len)
152 const char *obj_tags[] = {
157 const int obj_types[] = {
163 size_t size = 0, hdrlen = 0;
165 char *p = strchr(buf, '\0');
168 return got_error(GOT_ERR_BAD_OBJ_HDR);
170 hdrlen = strlen(buf) + 1 /* '\0' */;
172 for (i = 0; i < nitems(obj_tags); i++) {
173 const char *tag = obj_tags[i];
174 size_t tlen = strlen(tag);
177 if (strncmp(buf, tag, tlen) != 0)
182 return got_error(GOT_ERR_BAD_OBJ_HDR);
183 size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
185 return got_error(GOT_ERR_BAD_OBJ_HDR);
190 return got_error(GOT_ERR_BAD_OBJ_HDR);
192 *obj = calloc(1, sizeof(**obj));
194 (*obj)->hdrlen = hdrlen;
199 static const struct got_error *
200 read_object_header(struct got_object **obj, struct got_repository *repo,
203 const struct got_error *err;
205 struct got_zstream_buf zb;
209 f = fopen(path, "rb");
211 return got_error(GOT_ERR_BAD_PATH);
213 err = inflate_init(&zb, 64);
219 err = inflate_read(&zb, f, &outlen);
223 err = parse_object_header(obj, zb.outbuf, outlen);
230 static const struct got_error *
231 object_path(char **path, struct got_object_id *id,
232 struct got_repository *repo)
234 const struct got_error *err = NULL;
235 char hex[SHA1_DIGEST_STRING_LENGTH];
236 char *path_objects = got_repo_get_path_objects(repo);
238 if (path_objects == NULL)
239 return got_error(GOT_ERR_NO_MEM);
241 got_object_id_str(id, hex, sizeof(hex));
243 if (asprintf(path, "%s/%.2x/%s", path_objects,
244 id->sha1[0], hex + 2) == -1)
245 err = got_error(GOT_ERR_NO_MEM);
251 const struct got_error *
252 got_object_open(struct got_object **obj, struct got_repository *repo,
253 struct got_object_id *id)
255 const struct got_error *err = NULL;
258 err = object_path(&path, id, repo);
262 err = read_object_header(obj, repo, path);
264 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
271 got_object_close(struct got_object *obj)
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)
418 mode_trailer(mode_t mode)
426 static const struct got_error *
427 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
430 char *p = buf, *space;
431 const struct got_error *err = NULL;
432 char hex[SHA1_DIGEST_STRING_LENGTH];
434 *te = calloc(1, sizeof(**te));
436 return got_error(GOT_ERR_NO_MEM);
438 *elen = strlen(buf) + 1;
439 if (*elen > maxlen) {
441 return got_error(GOT_ERR_BAD_OBJ_DATA);
444 space = strchr(buf, ' ');
447 return got_error(GOT_ERR_BAD_OBJ_DATA);
450 if (*p < '0' && *p > '7') {
451 err = got_error(GOT_ERR_BAD_OBJ_DATA);
455 (*te)->mode |= *p - '0';
459 (*te)->name = strdup(space + 1);
460 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
461 err = got_error(GOT_ERR_BAD_OBJ_DATA);
464 buf += strlen(buf) + 1;
465 memcpy((*te)->id.sha1, buf, SHA1_DIGEST_LENGTH);
466 *elen += SHA1_DIGEST_LENGTH;
469 tree_entry_close(*te);
473 static const struct got_error *
474 parse_tree_object(struct got_tree_object **tree, struct got_repository *repo,
475 char *buf, size_t len)
480 *tree = calloc(1, sizeof(**tree));
482 return got_error(GOT_ERR_NO_MEM);
484 SIMPLEQ_INIT(&(*tree)->entries);
487 struct got_tree_entry *te;
490 parse_tree_entry(&te, &elen, buf, remain);
492 SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
498 got_object_tree_close(*tree);
499 return got_error(GOT_ERR_BAD_OBJ_DATA);
505 static const struct got_error *
506 read_commit_object(struct got_commit_object **commit,
507 struct got_repository *repo, struct got_object *obj, const char *path)
509 const struct got_error *err = NULL;
511 struct got_zstream_buf zb;
516 f = fopen(path, "rb");
518 return got_error(GOT_ERR_BAD_PATH);
520 err = inflate_init(&zb, 8192);
527 err = inflate_read(&zb, f, &len);
530 } while (len < obj->hdrlen + obj->size);
532 if (len < obj->hdrlen + obj->size) {
533 err = got_error(GOT_ERR_BAD_OBJ_DATA);
537 /* Skip object header. */
539 err = parse_commit_object(commit, zb.outbuf + obj->hdrlen, len);
546 const struct got_error *
547 got_object_commit_open(struct got_commit_object **commit,
548 struct got_repository *repo, struct got_object *obj)
550 const struct got_error *err = NULL;
553 if (obj->type != GOT_OBJ_TYPE_COMMIT)
554 return got_error(GOT_ERR_OBJ_TYPE);
556 err = object_path(&path, &obj->id, repo);
560 err = read_commit_object(commit, repo, obj, path);
566 got_object_commit_close(struct got_commit_object *commit)
568 struct got_parent_id *pid;
570 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
571 pid = SIMPLEQ_FIRST(&commit->parent_ids);
572 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
576 free(commit->author);
577 free(commit->committer);
578 free(commit->logmsg);
582 static const struct got_error *
583 read_tree_object(struct got_tree_object **tree,
584 struct got_repository *repo, struct got_object *obj, const char *path)
586 const struct got_error *err = NULL;
588 struct got_zstream_buf zb;
593 f = fopen(path, "rb");
595 return got_error(GOT_ERR_BAD_PATH);
597 err = inflate_init(&zb, 8192);
604 err = inflate_read(&zb, f, &len);
607 } while (len < obj->hdrlen + obj->size);
609 if (len < obj->hdrlen + obj->size) {
610 err = got_error(GOT_ERR_BAD_OBJ_DATA);
614 /* Skip object header. */
616 err = parse_tree_object(tree, repo, zb.outbuf + obj->hdrlen, len);
623 const struct got_error *
624 got_object_tree_open(struct got_tree_object **tree,
625 struct got_repository *repo, struct got_object *obj)
627 const struct got_error *err = NULL;
630 if (obj->type != GOT_OBJ_TYPE_TREE)
631 return got_error(GOT_ERR_OBJ_TYPE);
633 err = object_path(&path, &obj->id, repo);
637 err = read_tree_object(tree, repo, obj, path);
643 got_object_tree_close(struct got_tree_object *tree)
645 struct got_tree_entry *te;
647 while (!SIMPLEQ_EMPTY(&tree->entries)) {
648 te = SIMPLEQ_FIRST(&tree->entries);
649 SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
650 tree_entry_close(te);
656 const struct got_error *
657 got_object_blob_open(struct got_blob_object **blob,
658 struct got_repository *repo, struct got_object *obj, size_t blocksize)
660 const struct got_error *err = NULL;
663 if (obj->type != GOT_OBJ_TYPE_BLOB)
664 return got_error(GOT_ERR_OBJ_TYPE);
666 if (blocksize < obj->hdrlen)
667 return got_error(GOT_ERR_NO_SPACE);
669 err = object_path(&path, &obj->id, repo);
673 *blob = calloc(1, sizeof(**blob));
676 return got_error(GOT_ERR_NO_MEM);
679 (*blob)->f = fopen(path, "rb");
680 if ((*blob)->f == NULL) {
683 return got_error(GOT_ERR_BAD_PATH);
686 err = inflate_init(&(*blob)->zb, blocksize);
694 (*blob)->hdrlen = obj->hdrlen;
695 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
702 got_object_blob_close(struct got_blob_object *blob)
704 inflate_end(&blob->zb);
709 const struct got_error *
710 got_object_blob_read_block(struct got_blob_object *blob, size_t *outlenp)
712 return inflate_read(&blob->zb, blob->f, outlenp);