Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
20 #include <errno.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sha1.h>
25 #include <zlib.h>
26 #include <ctype.h>
27 #include <limits.h>
29 #include "got_error.h"
30 #include "got_object.h"
31 #include "got_repository.h"
32 #include "got_sha1.h"
33 #include "pack.h"
34 #include "delta.h"
35 #include "object.h"
37 #ifndef MIN
38 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
39 #endif
41 #ifndef nitems
42 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
43 #endif
45 #define GOT_OBJ_TAG_COMMIT "commit"
46 #define GOT_OBJ_TAG_TREE "tree"
47 #define GOT_OBJ_TAG_BLOB "blob"
49 #define GOT_COMMIT_TAG_TREE "tree "
50 #define GOT_COMMIT_TAG_PARENT "parent "
51 #define GOT_COMMIT_TAG_AUTHOR "author "
52 #define GOT_COMMIT_TAG_COMMITTER "committer "
54 char *
55 got_object_id_str(struct got_object_id *id, char *buf, size_t size)
56 {
57 return got_sha1_digest_to_str(id->sha1, buf, size);
58 }
60 int
61 got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
62 {
63 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
64 }
66 int
67 got_object_get_type(struct got_object *obj)
68 {
69 switch (obj->type) {
70 case GOT_OBJ_TYPE_COMMIT:
71 case GOT_OBJ_TYPE_TREE:
72 case GOT_OBJ_TYPE_BLOB:
73 case GOT_OBJ_TYPE_TAG:
74 return obj->type;
75 default:
76 abort();
77 break;
78 }
80 /* not reached */
81 return 0;
82 }
84 static void
85 inflate_end(struct got_zstream_buf *zb)
86 {
87 free(zb->inbuf);
88 free(zb->outbuf);
89 inflateEnd(&zb->z);
90 }
92 static const struct got_error *
93 inflate_init(struct got_zstream_buf *zb, size_t bufsize)
94 {
95 const struct got_error *err = NULL;
97 memset(zb, 0, sizeof(*zb));
99 zb->z.zalloc = Z_NULL;
100 zb->z.zfree = Z_NULL;
101 if (inflateInit(&zb->z) != Z_OK) {
102 err = got_error(GOT_ERR_IO);
103 goto done;
106 zb->inlen = zb->outlen = bufsize;
108 zb->inbuf = calloc(1, zb->inlen);
109 if (zb->inbuf == NULL) {
110 err = got_error(GOT_ERR_NO_MEM);
111 goto done;
114 zb->outbuf = calloc(1, zb->outlen);
115 if (zb->outbuf == NULL) {
116 err = got_error(GOT_ERR_NO_MEM);
117 goto done;
120 done:
121 if (err)
122 inflate_end(zb);
123 return err;
126 static const struct got_error *
127 inflate_read(struct got_zstream_buf *zb, FILE *f, size_t *outlenp)
129 size_t last_total_out = zb->z.total_out;
130 z_stream *z = &zb->z;
131 int n, ret;
133 z->next_out = zb->outbuf;
134 z->avail_out = zb->outlen;
136 do {
137 if (z->avail_in == 0) {
138 int i;
139 n = fread(zb->inbuf, 1, zb->inlen, f);
140 if (n == 0) {
141 if (ferror(f))
142 return got_ferror(f, GOT_ERR_IO);
143 *outlenp = 0;
144 return NULL;
146 z->next_in = zb->inbuf;
147 z->avail_in = n;
149 ret = inflate(z, Z_SYNC_FLUSH);
150 } while (ret == Z_OK && z->avail_out > 0);
152 if (ret != Z_OK) {
153 if (ret != Z_STREAM_END)
154 return got_error(GOT_ERR_DECOMPRESSION);
155 zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
158 *outlenp = z->total_out - last_total_out;
159 return NULL;
162 static const struct got_error *
163 parse_object_header(struct got_object **obj, char *buf, size_t len)
165 const char *obj_tags[] = {
166 GOT_OBJ_TAG_COMMIT,
167 GOT_OBJ_TAG_TREE,
168 GOT_OBJ_TAG_BLOB
169 };
170 const int obj_types[] = {
171 GOT_OBJ_TYPE_COMMIT,
172 GOT_OBJ_TYPE_TREE,
173 GOT_OBJ_TYPE_BLOB,
174 };
175 int type = 0;
176 size_t size = 0, hdrlen = 0;
177 int i;
178 char *p = strchr(buf, '\0');
180 if (p == NULL)
181 return got_error(GOT_ERR_BAD_OBJ_HDR);
183 hdrlen = strlen(buf) + 1 /* '\0' */;
185 for (i = 0; i < nitems(obj_tags); i++) {
186 const char *tag = obj_tags[i];
187 size_t tlen = strlen(tag);
188 const char *errstr;
190 if (strncmp(buf, tag, tlen) != 0)
191 continue;
193 type = obj_types[i];
194 if (len <= tlen)
195 return got_error(GOT_ERR_BAD_OBJ_HDR);
196 size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
197 if (errstr != NULL)
198 return got_error(GOT_ERR_BAD_OBJ_HDR);
199 break;
202 if (type == 0)
203 return got_error(GOT_ERR_BAD_OBJ_HDR);
205 *obj = calloc(1, sizeof(**obj));
206 (*obj)->type = type;
207 (*obj)->hdrlen = hdrlen;
208 (*obj)->size = size;
209 return NULL;
212 static const struct got_error *
213 read_object_header(struct got_object **obj, struct got_repository *repo,
214 FILE *f)
216 const struct got_error *err;
217 struct got_zstream_buf zb;
218 char *buf;
219 size_t len;
220 const size_t zbsize = 64;
221 size_t outlen, totlen;
222 int i, ret;
224 buf = calloc(zbsize, sizeof(char));
225 if (buf == NULL)
226 return got_error(GOT_ERR_NO_MEM);
228 err = inflate_init(&zb, zbsize);
229 if (err)
230 return err;
232 i = 0;
233 totlen = 0;
234 do {
235 err = inflate_read(&zb, f, &outlen);
236 if (err)
237 goto done;
238 if (strchr(zb.outbuf, '\0') == NULL) {
239 buf = recallocarray(buf, 1 + i, 2 + i, zbsize);
240 if (buf == NULL) {
241 err = got_error(GOT_ERR_NO_MEM);
242 goto done;
245 memcpy(buf + totlen, zb.outbuf, outlen);
246 totlen += outlen;
247 i++;
248 } while (strchr(zb.outbuf, '\0') == NULL);
250 err = parse_object_header(obj, buf, totlen);
251 done:
252 inflate_end(&zb);
253 return err;
256 static const struct got_error *
257 object_path(char **path, struct got_object_id *id, struct got_repository *repo)
259 const struct got_error *err = NULL;
260 char hex[SHA1_DIGEST_STRING_LENGTH];
261 char *path_objects = got_repo_get_path_objects(repo);
263 if (path_objects == NULL)
264 return got_error(GOT_ERR_NO_MEM);
266 got_object_id_str(id, hex, sizeof(hex));
268 if (asprintf(path, "%s/%.2x/%s", path_objects,
269 id->sha1[0], hex + 2) == -1)
270 err = got_error(GOT_ERR_NO_MEM);
272 free(path_objects);
273 return err;
276 const struct got_error *
277 open_object(FILE **f, struct got_object *obj, struct got_repository *repo)
279 const struct got_error *err = NULL;
280 char *path;
282 if (obj->flags & GOT_OBJ_FLAG_PACKED)
283 return got_packfile_extract_object(f, obj, repo);
285 err = object_path(&path, &obj->id, repo);
286 if (err)
287 return err;
288 *f = fopen(path, "rb");
289 if (*f == NULL) {
290 err = got_error_from_errno();
291 goto done;
293 done:
294 free(path);
295 return err;
298 const struct got_error *
299 got_object_open(struct got_object **obj, struct got_repository *repo,
300 struct got_object_id *id)
302 const struct got_error *err = NULL;
303 char *path;
304 FILE *f;
306 err = object_path(&path, id, repo);
307 if (err)
308 return err;
310 f = fopen(path, "rb");
311 if (f == NULL) {
312 if (errno != ENOENT) {
313 err = got_error_from_errno();
314 goto done;
316 err = got_packfile_open_object(obj, id, repo);
317 if (err)
318 goto done;
319 if (*obj == NULL)
320 err = got_error(GOT_ERR_NO_OBJ);
321 } else {
322 err = read_object_header(obj, repo, f);
323 if (err)
324 goto done;
325 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
327 done:
328 free(path);
329 if (err && f)
330 fclose(f);
331 return err;
335 void
336 got_object_close(struct got_object *obj)
338 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
339 struct got_delta *delta;
340 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
341 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
342 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
343 got_delta_close(delta);
346 if (obj->flags & GOT_OBJ_FLAG_PACKED)
347 free(obj->path_packfile);
348 free(obj);
351 static int
352 commit_object_valid(struct got_commit_object *commit)
354 int i;
355 int n;
357 if (commit == NULL)
358 return 0;
360 n = 0;
361 for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
362 if (commit->tree_id.sha1[i] == 0)
363 n++;
365 if (n == SHA1_DIGEST_LENGTH)
366 return 0;
368 return 1;
371 static const struct got_error *
372 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
374 const struct got_error *err = NULL;
375 char *s = buf;
376 size_t tlen;
377 ssize_t remain = (ssize_t)len;
379 *commit = calloc(1, sizeof(**commit));
380 if (*commit == NULL)
381 return got_error(GOT_ERR_NO_MEM);
383 SIMPLEQ_INIT(&(*commit)->parent_ids);
385 tlen = strlen(GOT_COMMIT_TAG_TREE);
386 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
387 remain -= tlen;
388 if (remain < SHA1_DIGEST_STRING_LENGTH) {
389 err = got_error(GOT_ERR_BAD_OBJ_DATA);
390 goto done;
392 s += tlen;
393 if (!got_parse_sha1_digest((*commit)->tree_id.sha1, s)) {
394 err = got_error(GOT_ERR_BAD_OBJ_DATA);
395 goto done;
397 remain -= SHA1_DIGEST_STRING_LENGTH;
398 s += SHA1_DIGEST_STRING_LENGTH;
399 } else {
400 err = got_error(GOT_ERR_BAD_OBJ_DATA);
401 goto done;
404 tlen = strlen(GOT_COMMIT_TAG_PARENT);
405 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
406 struct got_parent_id *pid;
408 remain -= tlen;
409 if (remain < SHA1_DIGEST_STRING_LENGTH) {
410 err = got_error(GOT_ERR_BAD_OBJ_DATA);
411 goto done;
414 pid = calloc(1, sizeof(*pid));
415 if (pid == NULL) {
416 err = got_error(GOT_ERR_NO_MEM);
417 goto done;
419 s += tlen;
420 if (!got_parse_sha1_digest(pid->id.sha1, s)) {
421 err = got_error(GOT_ERR_BAD_OBJ_DATA);
422 goto done;
424 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, pid, entry);
425 (*commit)->nparents++;
427 s += SHA1_DIGEST_STRING_LENGTH;
430 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
431 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
432 char *p;
434 remain -= tlen;
435 if (remain <= 0) {
436 err = got_error(GOT_ERR_BAD_OBJ_DATA);
437 goto done;
439 s += tlen;
440 p = strchr(s, '\n');
441 if (p == NULL) {
442 err = got_error(GOT_ERR_BAD_OBJ_DATA);
443 goto done;
445 *p = '\0';
446 (*commit)->author = strdup(s);
447 if ((*commit)->author == NULL) {
448 err = got_error(GOT_ERR_NO_MEM);
449 goto done;
451 s += strlen((*commit)->author) + 1;
454 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
455 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
456 char *p;
458 remain -= tlen;
459 if (remain <= 0) {
460 err = got_error(GOT_ERR_BAD_OBJ_DATA);
461 goto done;
463 s += tlen;
464 p = strchr(s, '\n');
465 if (p == NULL) {
466 err = got_error(GOT_ERR_BAD_OBJ_DATA);
467 goto done;
469 *p = '\0';
470 (*commit)->committer = strdup(s);
471 if ((*commit)->committer == NULL) {
472 err = got_error(GOT_ERR_NO_MEM);
473 goto done;
475 s += strlen((*commit)->committer) + 1;
478 (*commit)->logmsg = strdup(s);
479 done:
480 if (err)
481 got_object_commit_close(*commit);
482 return err;
485 static void
486 tree_entry_close(struct got_tree_entry *te)
488 free(te->name);
489 free(te);
492 static const struct got_error *
493 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
494 size_t maxlen)
496 char *p = buf, *space;
497 const struct got_error *err = NULL;
498 char hex[SHA1_DIGEST_STRING_LENGTH];
500 *te = calloc(1, sizeof(**te));
501 if (*te == NULL)
502 return got_error(GOT_ERR_NO_MEM);
504 *elen = strlen(buf) + 1;
505 if (*elen > maxlen) {
506 free(*te);
507 return got_error(GOT_ERR_BAD_OBJ_DATA);
510 space = strchr(buf, ' ');
511 if (space == NULL) {
512 free(*te);
513 return got_error(GOT_ERR_BAD_OBJ_DATA);
515 while (*p != ' ') {
516 if (*p < '0' && *p > '7') {
517 err = got_error(GOT_ERR_BAD_OBJ_DATA);
518 goto done;
520 (*te)->mode <<= 3;
521 (*te)->mode |= *p - '0';
522 p++;
525 (*te)->name = strdup(space + 1);
526 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
527 err = got_error(GOT_ERR_BAD_OBJ_DATA);
528 goto done;
530 buf += strlen(buf) + 1;
531 memcpy((*te)->id.sha1, buf, SHA1_DIGEST_LENGTH);
532 *elen += SHA1_DIGEST_LENGTH;
533 done:
534 if (err)
535 tree_entry_close(*te);
536 return err;
539 static const struct got_error *
540 parse_tree_object(struct got_tree_object **tree, struct got_repository *repo,
541 char *buf, size_t len)
543 size_t remain = len;
544 int nentries;
546 *tree = calloc(1, sizeof(**tree));
547 if (*tree == NULL)
548 return got_error(GOT_ERR_NO_MEM);
550 SIMPLEQ_INIT(&(*tree)->entries);
552 while (remain > 0) {
553 struct got_tree_entry *te;
554 size_t elen;
556 parse_tree_entry(&te, &elen, buf, remain);
557 (*tree)->nentries++;
558 SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
559 buf += elen;
560 remain -= elen;
563 if (remain != 0) {
564 got_object_tree_close(*tree);
565 return got_error(GOT_ERR_BAD_OBJ_DATA);
568 return NULL;
571 static const struct got_error *
572 read_commit_object(struct got_commit_object **commit,
573 struct got_repository *repo, struct got_object *obj, FILE *f)
575 const struct got_error *err = NULL;
576 struct got_zstream_buf zb;
577 size_t len;
578 char *p;
579 int i, ret;
581 err = inflate_init(&zb, 8192);
582 if (err)
583 return err;
585 do {
586 err = inflate_read(&zb, f, &len);
587 if (err || len == 0)
588 break;
589 } while (len < obj->hdrlen + obj->size);
591 if (len < obj->hdrlen + obj->size) {
592 err = got_error(GOT_ERR_BAD_OBJ_DATA);
593 goto done;
596 /* Skip object header. */
597 len -= obj->hdrlen;
598 err = parse_commit_object(commit, zb.outbuf + obj->hdrlen, len);
599 done:
600 inflate_end(&zb);
601 return err;
604 const struct got_error *
605 got_object_commit_open(struct got_commit_object **commit,
606 struct got_repository *repo, struct got_object *obj)
608 const struct got_error *err = NULL;
609 FILE *f;
611 if (obj->type != GOT_OBJ_TYPE_COMMIT)
612 return got_error(GOT_ERR_OBJ_TYPE);
614 err = open_object(&f, obj, repo);
615 if (err)
616 return err;
618 err = read_commit_object(commit, repo, obj, f);
619 fclose(f);
620 return err;
623 void
624 got_object_commit_close(struct got_commit_object *commit)
626 struct got_parent_id *pid;
628 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
629 pid = SIMPLEQ_FIRST(&commit->parent_ids);
630 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
631 free(pid);
634 free(commit->author);
635 free(commit->committer);
636 free(commit->logmsg);
637 free(commit);
640 static const struct got_error *
641 read_tree_object(struct got_tree_object **tree,
642 struct got_repository *repo, struct got_object *obj, FILE *f)
644 const struct got_error *err = NULL;
645 struct got_zstream_buf zb;
646 size_t len;
647 char *p;
648 int i, ret;
650 err = inflate_init(&zb, 8192);
651 if (err)
652 return err;
654 do {
655 err = inflate_read(&zb, f, &len);
656 if (err || len == 0)
657 break;
658 } while (len < obj->hdrlen + obj->size);
660 if (len < obj->hdrlen + obj->size) {
661 err = got_error(GOT_ERR_BAD_OBJ_DATA);
662 goto done;
665 /* Skip object header. */
666 len -= obj->hdrlen;
667 err = parse_tree_object(tree, repo, zb.outbuf + obj->hdrlen, len);
668 done:
669 inflate_end(&zb);
670 return err;
673 const struct got_error *
674 got_object_tree_open(struct got_tree_object **tree,
675 struct got_repository *repo, struct got_object *obj)
677 const struct got_error *err = NULL;
678 FILE *f;
680 if (obj->type != GOT_OBJ_TYPE_TREE)
681 return got_error(GOT_ERR_OBJ_TYPE);
683 err = open_object(&f, obj, repo);
684 if (err)
685 return err;
687 err = read_tree_object(tree, repo, obj, f);
688 fclose(f);
689 return err;
692 void
693 got_object_tree_close(struct got_tree_object *tree)
695 struct got_tree_entry *te;
697 while (!SIMPLEQ_EMPTY(&tree->entries)) {
698 te = SIMPLEQ_FIRST(&tree->entries);
699 SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
700 tree_entry_close(te);
703 free(tree);
706 const struct got_error *
707 got_object_blob_open(struct got_blob_object **blob,
708 struct got_repository *repo, struct got_object *obj, size_t blocksize)
710 const struct got_error *err = NULL;
712 if (obj->type != GOT_OBJ_TYPE_BLOB)
713 return got_error(GOT_ERR_OBJ_TYPE);
715 if (blocksize < obj->hdrlen)
716 return got_error(GOT_ERR_NO_SPACE);
718 *blob = calloc(1, sizeof(**blob));
719 if (*blob == NULL)
720 return got_error(GOT_ERR_NO_MEM);
722 err = open_object(&((*blob)->f), obj, repo);
723 if (err) {
724 free(*blob);
725 return err;
728 err = inflate_init(&(*blob)->zb, blocksize);
729 if (err != NULL) {
730 fclose((*blob)->f);
731 free(*blob);
732 return err;
735 (*blob)->hdrlen = obj->hdrlen;
736 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
738 return err;
741 void
742 got_object_blob_close(struct got_blob_object *blob)
744 inflate_end(&blob->zb);
745 fclose(blob->f);
746 free(blob);
749 const struct got_error *
750 got_object_blob_read_block(struct got_blob_object *blob, size_t *outlenp)
752 return inflate_read(&blob->zb, blob->f, outlenp);