Blob


1 /*
2 * Copyright (c) 2017 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 <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sha1.h>
24 #include <zlib.h>
25 #include <ctype.h>
26 #include <limits.h>
28 #include "got_error.h"
29 #include "got_object.h"
30 #include "got_repository.h"
31 #include "got_sha1.h"
33 #ifndef MIN
34 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
35 #endif
37 #ifndef nitems
38 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
39 #endif
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 "
50 char *
51 got_object_id_str(struct got_object_id *id, char *buf, size_t size)
52 {
53 char *p = buf;
54 char hex[3];
55 int i;
57 if (size < SHA1_DIGEST_STRING_LENGTH)
58 return NULL;
60 for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
61 snprintf(hex, sizeof(hex), "%.2x", id->sha1[i]);
62 p[0] = hex[0];
63 p[1] = hex[1];
64 p += 2;
65 }
66 p[0] = '\0';
68 return buf;
69 }
71 static void
72 inflate_end(struct got_zstream_buf *zb)
73 {
74 free(zb->inbuf);
75 free(zb->outbuf);
76 inflateEnd(&zb->z);
77 }
79 static const struct got_error *
80 inflate_init(struct got_zstream_buf *zb, size_t bufsize)
81 {
82 const struct got_error *err = NULL;
84 memset(zb, 0, sizeof(*zb));
86 zb->z.zalloc = Z_NULL;
87 zb->z.zfree = Z_NULL;
88 if (inflateInit(&zb->z) != Z_OK) {
89 err = got_error(GOT_ERR_IO);
90 goto done;
91 }
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);
98 goto done;
99 }
101 zb->outbuf = calloc(1, zb->outlen);
102 if (zb->outbuf == NULL) {
103 err = got_error(GOT_ERR_NO_MEM);
104 goto done;
107 done:
108 if (err)
109 inflate_end(zb);
110 return err;
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;
118 int n, ret;
120 z->next_out = zb->outbuf;
121 z->avail_out = zb->outlen;
123 do {
124 if (z->avail_in == 0) {
125 int i;
126 n = fread(zb->inbuf, 1, zb->inlen, f);
127 if (n == 0) {
128 if (ferror(f))
129 return got_error(GOT_ERR_IO);
130 *outlenp = 0;
131 return NULL;
133 z->next_in = zb->inbuf;
134 z->avail_in = n;
136 ret = inflate(z, Z_SYNC_FLUSH);
137 } while (ret == Z_OK && z->avail_out > 0);
139 if (ret != Z_OK) {
140 if (ret != Z_STREAM_END)
141 return got_error(GOT_ERR_DECOMPRESSION);
142 zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
145 *outlenp = z->total_out - last_total_out;
146 return NULL;
149 static const struct got_error *
150 parse_object_header(struct got_object **obj, char *buf, size_t len)
152 const char *obj_tags[] = {
153 GOT_OBJ_TAG_COMMIT,
154 GOT_OBJ_TAG_TREE,
155 GOT_OBJ_TAG_BLOB
156 };
157 const int obj_types[] = {
158 GOT_OBJ_TYPE_COMMIT,
159 GOT_OBJ_TYPE_TREE,
160 GOT_OBJ_TYPE_BLOB,
161 };
162 int type = 0;
163 size_t size = 0, hdrlen = 0;
164 int i;
165 char *p = strchr(buf, '\0');
167 if (p == NULL)
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);
175 const char *errstr;
177 if (strncmp(buf, tag, tlen) != 0)
178 continue;
180 type = obj_types[i];
181 if (len <= tlen)
182 return got_error(GOT_ERR_BAD_OBJ_HDR);
183 size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
184 if (errstr != NULL)
185 return got_error(GOT_ERR_BAD_OBJ_HDR);
186 break;
189 if (type == 0)
190 return got_error(GOT_ERR_BAD_OBJ_HDR);
192 *obj = calloc(1, sizeof(**obj));
193 (*obj)->type = type;
194 (*obj)->hdrlen = hdrlen;
195 (*obj)->size = size;
196 return NULL;
199 static const struct got_error *
200 read_object_header(struct got_object **obj, struct got_repository *repo,
201 const char *path)
203 const struct got_error *err;
204 FILE *f;
205 struct got_zstream_buf zb;
206 char *buf;
207 size_t totalsz;
208 const size_t zbsize = 64;
209 size_t outlen;
210 int i, ret;
212 f = fopen(path, "rb");
213 if (f == NULL)
214 return got_error(GOT_ERR_BAD_PATH);
216 totalsz = zbsize;
217 buf = calloc(totalsz, sizeof(char));
218 if (buf == NULL)
219 return got_error(GOT_ERR_NO_MEM);
221 err = inflate_init(&zb, zbsize);
222 if (err) {
223 fclose(f);
224 return err;
227 i = 0;
228 do {
229 err = inflate_read(&zb, f, &outlen);
230 if (err)
231 goto done;
232 if (strchr(zb.outbuf, '\0') == NULL) {
233 buf = recallocarray(buf, 1 + i, 2 + i, zbsize);
234 totalsz += zbsize;
236 memcpy(buf, zb.outbuf, zbsize);
237 i++;
238 } while (strchr(zb.outbuf, '\0') == NULL);
240 err = parse_object_header(obj, buf, totalsz);
241 done:
242 inflate_end(&zb);
243 fclose(f);
244 return err;
247 static const struct got_error *
248 object_path(char **path, struct got_object_id *id,
249 struct got_repository *repo)
251 const struct got_error *err = NULL;
252 char hex[SHA1_DIGEST_STRING_LENGTH];
253 char *path_objects = got_repo_get_path_objects(repo);
255 if (path_objects == NULL)
256 return got_error(GOT_ERR_NO_MEM);
258 got_object_id_str(id, hex, sizeof(hex));
260 if (asprintf(path, "%s/%.2x/%s", path_objects,
261 id->sha1[0], hex + 2) == -1)
262 err = got_error(GOT_ERR_NO_MEM);
264 free(path_objects);
265 return err;
268 const struct got_error *
269 got_object_open(struct got_object **obj, struct got_repository *repo,
270 struct got_object_id *id)
272 const struct got_error *err = NULL;
273 char *path = NULL;
275 err = object_path(&path, id, repo);
276 if (err)
277 return err;
279 err = read_object_header(obj, repo, path);
280 if (err == NULL)
281 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
282 done:
283 free(path);
284 return err;
287 void
288 got_object_close(struct got_object *obj)
290 free(obj);
293 static int
294 commit_object_valid(struct got_commit_object *commit)
296 int i;
297 int n;
299 if (commit == NULL)
300 return 0;
302 n = 0;
303 for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
304 if (commit->tree_id.sha1[i] == 0)
305 n++;
307 if (n == SHA1_DIGEST_LENGTH)
308 return 0;
310 return 1;
313 static const struct got_error *
314 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
316 const struct got_error *err = NULL;
317 char *s = buf;
318 size_t tlen;
319 ssize_t remain = (ssize_t)len;
321 *commit = calloc(1, sizeof(**commit));
322 if (*commit == NULL)
323 return got_error(GOT_ERR_NO_MEM);
325 SIMPLEQ_INIT(&(*commit)->parent_ids);
327 tlen = strlen(GOT_COMMIT_TAG_TREE);
328 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
329 remain -= tlen;
330 if (remain < SHA1_DIGEST_STRING_LENGTH) {
331 err = got_error(GOT_ERR_BAD_OBJ_DATA);
332 goto done;
334 s += tlen;
335 if (!got_parse_sha1_digest((*commit)->tree_id.sha1, s)) {
336 err = got_error(GOT_ERR_BAD_OBJ_DATA);
337 goto done;
339 remain -= SHA1_DIGEST_STRING_LENGTH;
340 s += SHA1_DIGEST_STRING_LENGTH;
341 } else {
342 err = got_error(GOT_ERR_BAD_OBJ_DATA);
343 goto done;
346 tlen = strlen(GOT_COMMIT_TAG_PARENT);
347 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
348 struct got_parent_id *pid;
350 remain -= tlen;
351 if (remain < SHA1_DIGEST_STRING_LENGTH) {
352 err = got_error(GOT_ERR_BAD_OBJ_DATA);
353 goto done;
356 pid = calloc(1, sizeof(*pid));
357 if (pid == NULL) {
358 err = got_error(GOT_ERR_NO_MEM);
359 goto done;
361 s += tlen;
362 if (!got_parse_sha1_digest(pid->id.sha1, s)) {
363 err = got_error(GOT_ERR_BAD_OBJ_DATA);
364 goto done;
366 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, pid, entry);
367 (*commit)->nparents++;
369 s += SHA1_DIGEST_STRING_LENGTH;
372 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
373 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
374 char *p;
376 remain -= tlen;
377 if (remain <= 0) {
378 err = got_error(GOT_ERR_BAD_OBJ_DATA);
379 goto done;
381 s += tlen;
382 p = strchr(s, '\n');
383 if (p == NULL) {
384 err = got_error(GOT_ERR_BAD_OBJ_DATA);
385 goto done;
387 *p = '\0';
388 (*commit)->author = strdup(s);
389 if ((*commit)->author == NULL) {
390 err = got_error(GOT_ERR_NO_MEM);
391 goto done;
393 s += strlen((*commit)->author) + 1;
396 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
397 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
398 char *p;
400 remain -= tlen;
401 if (remain <= 0) {
402 err = got_error(GOT_ERR_BAD_OBJ_DATA);
403 goto done;
405 s += tlen;
406 p = strchr(s, '\n');
407 if (p == NULL) {
408 err = got_error(GOT_ERR_BAD_OBJ_DATA);
409 goto done;
411 *p = '\0';
412 (*commit)->committer = strdup(s);
413 if ((*commit)->committer == NULL) {
414 err = got_error(GOT_ERR_NO_MEM);
415 goto done;
417 s += strlen((*commit)->committer) + 1;
420 (*commit)->logmsg = strdup(s);
421 done:
422 if (err)
423 got_object_commit_close(*commit);
424 return err;
427 static void
428 tree_entry_close(struct got_tree_entry *te)
430 free(te->name);
431 free(te);
434 static const struct got_error *
435 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
436 size_t maxlen)
438 char *p = buf, *space;
439 const struct got_error *err = NULL;
440 char hex[SHA1_DIGEST_STRING_LENGTH];
442 *te = calloc(1, sizeof(**te));
443 if (*te == NULL)
444 return got_error(GOT_ERR_NO_MEM);
446 *elen = strlen(buf) + 1;
447 if (*elen > maxlen) {
448 free(*te);
449 return got_error(GOT_ERR_BAD_OBJ_DATA);
452 space = strchr(buf, ' ');
453 if (space == NULL) {
454 free(*te);
455 return got_error(GOT_ERR_BAD_OBJ_DATA);
457 while (*p != ' ') {
458 if (*p < '0' && *p > '7') {
459 err = got_error(GOT_ERR_BAD_OBJ_DATA);
460 goto done;
462 (*te)->mode <<= 3;
463 (*te)->mode |= *p - '0';
464 p++;
467 (*te)->name = strdup(space + 1);
468 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
469 err = got_error(GOT_ERR_BAD_OBJ_DATA);
470 goto done;
472 buf += strlen(buf) + 1;
473 memcpy((*te)->id.sha1, buf, SHA1_DIGEST_LENGTH);
474 *elen += SHA1_DIGEST_LENGTH;
475 done:
476 if (err)
477 tree_entry_close(*te);
478 return err;
481 static const struct got_error *
482 parse_tree_object(struct got_tree_object **tree, struct got_repository *repo,
483 char *buf, size_t len)
485 size_t remain = len;
486 int nentries;
488 *tree = calloc(1, sizeof(**tree));
489 if (*tree == NULL)
490 return got_error(GOT_ERR_NO_MEM);
492 SIMPLEQ_INIT(&(*tree)->entries);
494 while (remain > 0) {
495 struct got_tree_entry *te;
496 size_t elen;
498 parse_tree_entry(&te, &elen, buf, remain);
499 (*tree)->nentries++;
500 SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
501 buf += elen;
502 remain -= elen;
505 if (remain != 0) {
506 got_object_tree_close(*tree);
507 return got_error(GOT_ERR_BAD_OBJ_DATA);
510 return NULL;
513 static const struct got_error *
514 read_commit_object(struct got_commit_object **commit,
515 struct got_repository *repo, struct got_object *obj, const char *path)
517 const struct got_error *err = NULL;
518 FILE *f;
519 struct got_zstream_buf zb;
520 size_t len;
521 char *p;
522 int i, ret;
524 f = fopen(path, "rb");
525 if (f == NULL)
526 return got_error(GOT_ERR_BAD_PATH);
528 err = inflate_init(&zb, 8192);
529 if (err) {
530 fclose(f);
531 return err;
534 do {
535 err = inflate_read(&zb, f, &len);
536 if (err || len == 0)
537 break;
538 } while (len < obj->hdrlen + obj->size);
540 if (len < obj->hdrlen + obj->size) {
541 err = got_error(GOT_ERR_BAD_OBJ_DATA);
542 goto done;
545 /* Skip object header. */
546 len -= obj->hdrlen;
547 err = parse_commit_object(commit, zb.outbuf + obj->hdrlen, len);
548 done:
549 inflate_end(&zb);
550 fclose(f);
551 return err;
554 const struct got_error *
555 got_object_commit_open(struct got_commit_object **commit,
556 struct got_repository *repo, struct got_object *obj)
558 const struct got_error *err = NULL;
559 char *path = NULL;
561 if (obj->type != GOT_OBJ_TYPE_COMMIT)
562 return got_error(GOT_ERR_OBJ_TYPE);
564 err = object_path(&path, &obj->id, repo);
565 if (err)
566 return err;
568 err = read_commit_object(commit, repo, obj, path);
569 free(path);
570 return err;
573 void
574 got_object_commit_close(struct got_commit_object *commit)
576 struct got_parent_id *pid;
578 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
579 pid = SIMPLEQ_FIRST(&commit->parent_ids);
580 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
581 free(pid);
584 free(commit->author);
585 free(commit->committer);
586 free(commit->logmsg);
587 free(commit);
590 static const struct got_error *
591 read_tree_object(struct got_tree_object **tree,
592 struct got_repository *repo, struct got_object *obj, const char *path)
594 const struct got_error *err = NULL;
595 FILE *f;
596 struct got_zstream_buf zb;
597 size_t len;
598 char *p;
599 int i, ret;
601 f = fopen(path, "rb");
602 if (f == NULL)
603 return got_error(GOT_ERR_BAD_PATH);
605 err = inflate_init(&zb, 8192);
606 if (err) {
607 fclose(f);
608 return err;
611 do {
612 err = inflate_read(&zb, f, &len);
613 if (err || len == 0)
614 break;
615 } while (len < obj->hdrlen + obj->size);
617 if (len < obj->hdrlen + obj->size) {
618 err = got_error(GOT_ERR_BAD_OBJ_DATA);
619 goto done;
622 /* Skip object header. */
623 len -= obj->hdrlen;
624 err = parse_tree_object(tree, repo, zb.outbuf + obj->hdrlen, len);
625 done:
626 inflate_end(&zb);
627 fclose(f);
628 return err;
631 const struct got_error *
632 got_object_tree_open(struct got_tree_object **tree,
633 struct got_repository *repo, struct got_object *obj)
635 const struct got_error *err = NULL;
636 char *path = NULL;
638 if (obj->type != GOT_OBJ_TYPE_TREE)
639 return got_error(GOT_ERR_OBJ_TYPE);
641 err = object_path(&path, &obj->id, repo);
642 if (err)
643 return err;
645 err = read_tree_object(tree, repo, obj, path);
646 free(path);
647 return err;
650 void
651 got_object_tree_close(struct got_tree_object *tree)
653 struct got_tree_entry *te;
655 while (!SIMPLEQ_EMPTY(&tree->entries)) {
656 te = SIMPLEQ_FIRST(&tree->entries);
657 SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
658 tree_entry_close(te);
661 free(tree);
664 const struct got_error *
665 got_object_blob_open(struct got_blob_object **blob,
666 struct got_repository *repo, struct got_object *obj, size_t blocksize)
668 const struct got_error *err = NULL;
669 char *path;
671 if (obj->type != GOT_OBJ_TYPE_BLOB)
672 return got_error(GOT_ERR_OBJ_TYPE);
674 if (blocksize < obj->hdrlen)
675 return got_error(GOT_ERR_NO_SPACE);
677 err = object_path(&path, &obj->id, repo);
678 if (err)
679 return err;
681 *blob = calloc(1, sizeof(**blob));
682 if (*blob == NULL) {
683 free(path);
684 return got_error(GOT_ERR_NO_MEM);
687 (*blob)->f = fopen(path, "rb");
688 if ((*blob)->f == NULL) {
689 free(*blob);
690 free(path);
691 return got_error(GOT_ERR_BAD_PATH);
694 err = inflate_init(&(*blob)->zb, blocksize);
695 if (err != NULL) {
696 fclose((*blob)->f);
697 free(*blob);
698 free(path);
699 return err;
702 (*blob)->hdrlen = obj->hdrlen;
703 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
705 free(path);
706 return err;
709 void
710 got_object_blob_close(struct got_blob_object *blob)
712 inflate_end(&blob->zb);
713 fclose(blob->f);
714 free(blob);
717 const struct got_error *
718 got_object_blob_read_block(struct got_blob_object *blob, size_t *outlenp)
720 return inflate_read(&blob->zb, blob->f, outlenp);