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"
36 #include "zb.h"
38 #ifndef MIN
39 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
40 #endif
42 #ifndef nitems
43 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
44 #endif
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 "
55 char *
56 got_object_id_str(struct got_object_id *id, char *buf, size_t size)
57 {
58 return got_sha1_digest_to_str(id->sha1, buf, size);
59 }
61 int
62 got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
63 {
64 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
65 }
67 int
68 got_object_get_type(struct got_object *obj)
69 {
70 switch (obj->type) {
71 case GOT_OBJ_TYPE_COMMIT:
72 case GOT_OBJ_TYPE_TREE:
73 case GOT_OBJ_TYPE_BLOB:
74 case GOT_OBJ_TYPE_TAG:
75 return obj->type;
76 default:
77 abort();
78 break;
79 }
81 /* not reached */
82 return 0;
83 }
85 static const struct got_error *
86 parse_object_header(struct got_object **obj, char *buf, size_t len)
87 {
88 const char *obj_tags[] = {
89 GOT_OBJ_TAG_COMMIT,
90 GOT_OBJ_TAG_TREE,
91 GOT_OBJ_TAG_BLOB
92 };
93 const int obj_types[] = {
94 GOT_OBJ_TYPE_COMMIT,
95 GOT_OBJ_TYPE_TREE,
96 GOT_OBJ_TYPE_BLOB,
97 };
98 int type = 0;
99 size_t size = 0, hdrlen = 0;
100 int i;
101 char *p = strchr(buf, '\0');
103 if (p == NULL)
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);
111 const char *errstr;
113 if (strncmp(buf, tag, tlen) != 0)
114 continue;
116 type = obj_types[i];
117 if (len <= tlen)
118 return got_error(GOT_ERR_BAD_OBJ_HDR);
119 size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
120 if (errstr != NULL)
121 return got_error(GOT_ERR_BAD_OBJ_HDR);
122 break;
125 if (type == 0)
126 return got_error(GOT_ERR_BAD_OBJ_HDR);
128 *obj = calloc(1, sizeof(**obj));
129 if (*obj == NULL)
130 return got_error(GOT_ERR_NO_MEM);
131 (*obj)->type = type;
132 (*obj)->hdrlen = hdrlen;
133 (*obj)->size = size;
134 return NULL;
137 static const struct got_error *
138 read_object_header(struct got_object **obj, struct got_repository *repo,
139 FILE *f)
141 const struct got_error *err;
142 struct got_zstream_buf zb;
143 char *buf;
144 size_t len;
145 const size_t zbsize = 64;
146 size_t outlen, totlen;
147 int i, ret;
149 buf = calloc(zbsize, sizeof(char));
150 if (buf == NULL)
151 return got_error(GOT_ERR_NO_MEM);
153 err = got_inflate_init(&zb, zbsize);
154 if (err)
155 return err;
157 i = 0;
158 totlen = 0;
159 do {
160 err = got_inflate_read(&zb, f, NULL, &outlen);
161 if (err)
162 goto done;
163 if (strchr(zb.outbuf, '\0') == NULL) {
164 buf = recallocarray(buf, 1 + i, 2 + i, zbsize);
165 if (buf == NULL) {
166 err = got_error(GOT_ERR_NO_MEM);
167 goto done;
170 memcpy(buf + totlen, zb.outbuf, outlen);
171 totlen += outlen;
172 i++;
173 } while (strchr(zb.outbuf, '\0') == NULL);
175 err = parse_object_header(obj, buf, totlen);
176 done:
177 got_inflate_end(&zb);
178 return err;
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);
197 free(path_objects);
198 return err;
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;
205 char *path;
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);
211 if (err)
212 return err;
213 *f = fopen(path, "rb");
214 if (*f == NULL) {
215 err = got_error_from_errno();
216 goto done;
218 done:
219 free(path);
220 return err;
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;
228 char *path;
229 FILE *f;
231 err = object_path(&path, id, repo);
232 if (err)
233 return err;
235 f = fopen(path, "rb");
236 if (f == NULL) {
237 if (errno != ENOENT) {
238 err = got_error_from_errno();
239 goto done;
241 err = got_packfile_open_object(obj, id, repo);
242 if (err)
243 goto done;
244 if (*obj == NULL)
245 err = got_error(GOT_ERR_NO_OBJ);
246 } else {
247 err = read_object_header(obj, repo, f);
248 if (err)
249 goto done;
250 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
252 done:
253 free(path);
254 if (err && f)
255 fclose(f);
256 return err;
260 void
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);
273 free(obj);
276 static int
277 commit_object_valid(struct got_commit_object *commit)
279 int i;
280 int n;
282 if (commit == NULL)
283 return 0;
285 n = 0;
286 for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
287 if (commit->tree_id.sha1[i] == 0)
288 n++;
290 if (n == SHA1_DIGEST_LENGTH)
291 return 0;
293 return 1;
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;
300 char *s = buf;
301 size_t tlen;
302 ssize_t remain = (ssize_t)len;
304 *commit = calloc(1, sizeof(**commit));
305 if (*commit == NULL)
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) {
312 remain -= tlen;
313 if (remain < SHA1_DIGEST_STRING_LENGTH) {
314 err = got_error(GOT_ERR_BAD_OBJ_DATA);
315 goto done;
317 s += tlen;
318 if (!got_parse_sha1_digest((*commit)->tree_id.sha1, s)) {
319 err = got_error(GOT_ERR_BAD_OBJ_DATA);
320 goto done;
322 remain -= SHA1_DIGEST_STRING_LENGTH;
323 s += SHA1_DIGEST_STRING_LENGTH;
324 } else {
325 err = got_error(GOT_ERR_BAD_OBJ_DATA);
326 goto done;
329 tlen = strlen(GOT_COMMIT_TAG_PARENT);
330 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
331 struct got_parent_id *pid;
333 remain -= tlen;
334 if (remain < SHA1_DIGEST_STRING_LENGTH) {
335 err = got_error(GOT_ERR_BAD_OBJ_DATA);
336 goto done;
339 pid = calloc(1, sizeof(*pid));
340 if (pid == NULL) {
341 err = got_error(GOT_ERR_NO_MEM);
342 goto done;
344 s += tlen;
345 if (!got_parse_sha1_digest(pid->id.sha1, s)) {
346 err = got_error(GOT_ERR_BAD_OBJ_DATA);
347 goto done;
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) {
357 char *p;
359 remain -= tlen;
360 if (remain <= 0) {
361 err = got_error(GOT_ERR_BAD_OBJ_DATA);
362 goto done;
364 s += tlen;
365 p = strchr(s, '\n');
366 if (p == NULL) {
367 err = got_error(GOT_ERR_BAD_OBJ_DATA);
368 goto done;
370 *p = '\0';
371 (*commit)->author = strdup(s);
372 if ((*commit)->author == NULL) {
373 err = got_error(GOT_ERR_NO_MEM);
374 goto done;
376 s += strlen((*commit)->author) + 1;
379 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
380 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
381 char *p;
383 remain -= tlen;
384 if (remain <= 0) {
385 err = got_error(GOT_ERR_BAD_OBJ_DATA);
386 goto done;
388 s += tlen;
389 p = strchr(s, '\n');
390 if (p == NULL) {
391 err = got_error(GOT_ERR_BAD_OBJ_DATA);
392 goto done;
394 *p = '\0';
395 (*commit)->committer = strdup(s);
396 if ((*commit)->committer == NULL) {
397 err = got_error(GOT_ERR_NO_MEM);
398 goto done;
400 s += strlen((*commit)->committer) + 1;
403 (*commit)->logmsg = strdup(s);
404 done:
405 if (err)
406 got_object_commit_close(*commit);
407 return err;
410 static void
411 tree_entry_close(struct got_tree_entry *te)
413 free(te->name);
414 free(te);
417 static const struct got_error *
418 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
419 size_t maxlen)
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));
426 if (*te == NULL)
427 return got_error(GOT_ERR_NO_MEM);
429 *elen = strlen(buf) + 1;
430 if (*elen > maxlen) {
431 free(*te);
432 return got_error(GOT_ERR_BAD_OBJ_DATA);
435 space = strchr(buf, ' ');
436 if (space == NULL) {
437 free(*te);
438 return got_error(GOT_ERR_BAD_OBJ_DATA);
440 while (*p != ' ') {
441 if (*p < '0' && *p > '7') {
442 err = got_error(GOT_ERR_BAD_OBJ_DATA);
443 goto done;
445 (*te)->mode <<= 3;
446 (*te)->mode |= *p - '0';
447 p++;
450 (*te)->name = strdup(space + 1);
451 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
452 err = got_error(GOT_ERR_BAD_OBJ_DATA);
453 goto done;
455 buf += strlen(buf) + 1;
456 memcpy((*te)->id.sha1, buf, SHA1_DIGEST_LENGTH);
457 *elen += SHA1_DIGEST_LENGTH;
458 done:
459 if (err)
460 tree_entry_close(*te);
461 return err;
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;
469 size_t remain = len;
470 int nentries;
472 *tree = calloc(1, sizeof(**tree));
473 if (*tree == NULL)
474 return got_error(GOT_ERR_NO_MEM);
476 SIMPLEQ_INIT(&(*tree)->entries);
478 while (remain > 0) {
479 struct got_tree_entry *te;
480 size_t elen;
482 err = parse_tree_entry(&te, &elen, buf, remain);
483 if (err)
484 return err;
485 (*tree)->nentries++;
486 SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
487 buf += elen;
488 remain -= elen;
491 if (remain != 0) {
492 got_object_tree_close(*tree);
493 return got_error(GOT_ERR_BAD_OBJ_DATA);
496 return NULL;
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;
505 size_t len;
506 char *p;
507 int i, ret;
509 err = got_inflate_init(&zb, 8192);
510 if (err)
511 return err;
513 do {
514 err = got_inflate_read(&zb, f, NULL, &len);
515 if (err || len == 0)
516 break;
517 } while (len < obj->hdrlen + obj->size);
519 if (len < obj->hdrlen + obj->size) {
520 err = got_error(GOT_ERR_BAD_OBJ_DATA);
521 goto done;
524 /* Skip object header. */
525 len -= obj->hdrlen;
526 err = parse_commit_object(commit, zb.outbuf + obj->hdrlen, len);
527 done:
528 got_inflate_end(&zb);
529 return err;
532 const struct got_error *
533 got_object_commit_open(struct got_commit_object **commit,
534 struct got_repository *repo, struct got_object *obj)
536 const struct got_error *err = NULL;
537 FILE *f;
539 if (obj->type != GOT_OBJ_TYPE_COMMIT)
540 return got_error(GOT_ERR_OBJ_TYPE);
542 err = fopen_object(&f, obj, repo);
543 if (err)
544 return err;
546 err = read_commit_object(commit, repo, obj, f);
547 fclose(f);
548 return err;
551 void
552 got_object_commit_close(struct got_commit_object *commit)
554 struct got_parent_id *pid;
556 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
557 pid = SIMPLEQ_FIRST(&commit->parent_ids);
558 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
559 free(pid);
562 free(commit->author);
563 free(commit->committer);
564 free(commit->logmsg);
565 free(commit);
568 static const struct got_error *
569 read_tree_object(struct got_tree_object **tree,
570 struct got_repository *repo, struct got_object *obj, FILE *f)
572 const struct got_error *err = NULL;
573 struct got_zstream_buf zb;
574 size_t len;
575 char *p;
576 int i, ret;
578 err = got_inflate_init(&zb, 8192);
579 if (err)
580 return err;
582 do {
583 err = got_inflate_read(&zb, f, NULL, &len);
584 if (err || len == 0)
585 break;
586 } while (len < obj->hdrlen + obj->size);
588 if (len < obj->hdrlen + obj->size) {
589 err = got_error(GOT_ERR_BAD_OBJ_DATA);
590 goto done;
593 /* Skip object header. */
594 len -= obj->hdrlen;
595 err = parse_tree_object(tree, repo, zb.outbuf + obj->hdrlen, len);
596 done:
597 got_inflate_end(&zb);
598 return err;
601 const struct got_error *
602 got_object_tree_open(struct got_tree_object **tree,
603 struct got_repository *repo, struct got_object *obj)
605 const struct got_error *err = NULL;
606 FILE *f;
608 if (obj->type != GOT_OBJ_TYPE_TREE)
609 return got_error(GOT_ERR_OBJ_TYPE);
611 err = fopen_object(&f, obj, repo);
612 if (err)
613 return err;
615 err = read_tree_object(tree, repo, obj, f);
616 fclose(f);
617 return err;
620 void
621 got_object_tree_close(struct got_tree_object *tree)
623 struct got_tree_entry *te;
625 while (!SIMPLEQ_EMPTY(&tree->entries)) {
626 te = SIMPLEQ_FIRST(&tree->entries);
627 SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
628 tree_entry_close(te);
631 free(tree);
634 const struct got_error *
635 got_object_blob_open(struct got_blob_object **blob,
636 struct got_repository *repo, struct got_object *obj, size_t blocksize)
638 const struct got_error *err = NULL;
640 if (obj->type != GOT_OBJ_TYPE_BLOB)
641 return got_error(GOT_ERR_OBJ_TYPE);
643 if (blocksize < obj->hdrlen)
644 return got_error(GOT_ERR_NO_SPACE);
646 *blob = calloc(1, sizeof(**blob));
647 if (*blob == NULL)
648 return got_error(GOT_ERR_NO_MEM);
650 err = fopen_object(&((*blob)->f), obj, repo);
651 if (err) {
652 free(*blob);
653 return err;
656 err = got_inflate_init(&(*blob)->zb, blocksize);
657 if (err != NULL) {
658 fclose((*blob)->f);
659 free(*blob);
660 return err;
663 (*blob)->hdrlen = obj->hdrlen;
664 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
666 return err;
669 void
670 got_object_blob_close(struct got_blob_object *blob)
672 got_inflate_end(&blob->zb);
673 fclose(blob->f);
674 free(blob);
677 const struct got_error *
678 got_object_blob_read_block(struct got_blob_object *blob, size_t *outlenp)
680 return got_inflate_read(&blob->zb, blob->f, NULL, outlenp);