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/types.h>
18 #include <sys/stat.h>
19 #include <sys/queue.h>
20 #include <sys/uio.h>
21 #include <sys/socket.h>
22 #include <sys/syslimits.h>
23 #include <sys/wait.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdint.h>
30 #include <sha1.h>
31 #include <zlib.h>
32 #include <ctype.h>
33 #include <limits.h>
34 #include <imsg.h>
35 #include <time.h>
36 #include <unistd.h>
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_repository.h"
41 #include "got_opentemp.h"
43 #include "got_lib_sha1.h"
44 #include "got_lib_delta.h"
45 #include "got_lib_inflate.h"
46 #include "got_lib_object.h"
47 #include "got_lib_object_cache.h"
48 #include "got_lib_pack.h"
49 #include "got_lib_privsep.h"
50 #include "got_lib_repository.h"
52 #ifndef nitems
53 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
54 #endif
56 #define GOT_OBJ_TAG_COMMIT "commit"
57 #define GOT_OBJ_TAG_TREE "tree"
58 #define GOT_OBJ_TAG_BLOB "blob"
59 #define GOT_OBJ_TAG_TAG "tag"
61 #define GOT_COMMIT_TAG_TREE "tree "
62 #define GOT_COMMIT_TAG_PARENT "parent "
63 #define GOT_COMMIT_TAG_AUTHOR "author "
64 #define GOT_COMMIT_TAG_COMMITTER "committer "
66 #define GOT_TAG_TAG_OBJECT "object "
67 #define GOT_TAG_TAG_TYPE "type "
68 #define GOT_TAG_TAG_TAG "tag "
69 #define GOT_TAG_TAG_TAGGER "tagger "
71 int
72 got_object_id_cmp(const struct got_object_id *id1,
73 const struct got_object_id *id2)
74 {
75 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
76 }
78 const struct got_error *
79 got_object_qid_alloc_partial(struct got_object_qid **qid)
80 {
81 const struct got_error *err = NULL;
83 *qid = malloc(sizeof(**qid));
84 if (*qid == NULL)
85 return got_error_from_errno();
87 (*qid)->id = malloc(sizeof(*((*qid)->id)));
88 if ((*qid)->id == NULL) {
89 err = got_error_from_errno();
90 got_object_qid_free(*qid);
91 *qid = NULL;
92 return err;
93 }
95 return NULL;
96 }
98 const struct got_error *
99 got_object_id_str(char **outbuf, struct got_object_id *id)
101 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
103 *outbuf = malloc(len);
104 if (*outbuf == NULL)
105 return got_error_from_errno();
107 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
108 free(*outbuf);
109 *outbuf = NULL;
110 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
113 return NULL;
116 void
117 got_object_close(struct got_object *obj)
119 if (obj->refcnt > 0) {
120 obj->refcnt--;
121 if (obj->refcnt > 0)
122 return;
125 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
126 struct got_delta *delta;
127 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
128 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
129 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
130 got_delta_close(delta);
133 if (obj->flags & GOT_OBJ_FLAG_PACKED)
134 free(obj->path_packfile);
135 free(obj);
138 void
139 got_object_qid_free(struct got_object_qid *qid)
141 free(qid->id);
142 free(qid);
145 struct got_commit_object *
146 got_object_commit_alloc_partial(void)
148 struct got_commit_object *commit;
150 commit = calloc(1, sizeof(*commit));
151 if (commit == NULL)
152 return NULL;
153 commit->tree_id = malloc(sizeof(*commit->tree_id));
154 if (commit->tree_id == NULL) {
155 free(commit);
156 return NULL;
159 SIMPLEQ_INIT(&commit->parent_ids);
161 return commit;
164 const struct got_error *
165 got_object_commit_add_parent(struct got_commit_object *commit,
166 const char *id_str)
168 const struct got_error *err = NULL;
169 struct got_object_qid *qid;
171 err = got_object_qid_alloc_partial(&qid);
172 if (err)
173 return err;
175 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
176 err = got_error(GOT_ERR_BAD_OBJ_DATA);
177 free(qid->id);
178 free(qid);
179 return err;
182 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
183 commit->nparents++;
185 return NULL;
188 static const struct got_error *
189 parse_gmtoff(time_t *gmtoff, const char *tzstr)
191 int sign = 1;
192 const char *p = tzstr;
193 time_t h, m;
195 *gmtoff = 0;
197 if (*p == '-')
198 sign = -1;
199 else if (*p != '+')
200 return got_error(GOT_ERR_BAD_OBJ_DATA);
201 p++;
202 if (!isdigit(*p) && !isdigit(*(p + 1)))
203 return got_error(GOT_ERR_BAD_OBJ_DATA);
204 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
206 p += 2;
207 if (!isdigit(*p) && !isdigit(*(p + 1)))
208 return got_error(GOT_ERR_BAD_OBJ_DATA);
209 m = ((*p - '0') * 10) + (*(p + 1) - '0');
211 *gmtoff = (h * 60 * 60 + m * 60) * sign;
212 return NULL;
215 static const struct got_error *
216 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
218 const struct got_error *err = NULL;
219 const char *errstr;
220 char *space, *tzstr;
222 /* Parse and strip off trailing timezone indicator string. */
223 space = strrchr(committer, ' ');
224 if (space == NULL)
225 return got_error(GOT_ERR_BAD_OBJ_DATA);
226 tzstr = strdup(space + 1);
227 if (tzstr == NULL)
228 return got_error_from_errno();
229 err = parse_gmtoff(gmtoff, tzstr);
230 free(tzstr);
231 if (err)
232 return err;
233 *space = '\0';
235 /* Timestamp is separated from committer name + email by space. */
236 space = strrchr(committer, ' ');
237 if (space == NULL)
238 return got_error(GOT_ERR_BAD_OBJ_DATA);
240 /* Timestamp parsed here is expressed in comitter's local time. */
241 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
242 if (errstr)
243 return got_error(GOT_ERR_BAD_OBJ_DATA);
245 /* Express the time stamp in UTC. */
246 *time -= *gmtoff;
248 /* Strip off parsed time information, leaving just author and email. */
249 *space = '\0';
251 return NULL;
254 void
255 got_object_commit_close(struct got_commit_object *commit)
257 struct got_object_qid *qid;
259 if (commit->refcnt > 0) {
260 commit->refcnt--;
261 if (commit->refcnt > 0)
262 return;
265 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
266 qid = SIMPLEQ_FIRST(&commit->parent_ids);
267 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
268 got_object_qid_free(qid);
271 free(commit->tree_id);
272 free(commit->author);
273 free(commit->committer);
274 free(commit->logmsg);
275 free(commit);
278 struct got_object_id *
279 got_object_commit_get_tree_id(struct got_commit_object *commit)
281 return commit->tree_id;
284 int
285 got_object_commit_get_nparents(struct got_commit_object *commit)
287 return commit->nparents;
290 const struct got_object_id_queue *
291 got_object_commit_get_parent_ids(struct got_commit_object *commit)
293 return &commit->parent_ids;
296 const char *
297 got_object_commit_get_author(struct got_commit_object *commit)
299 return commit->author;
302 time_t
303 got_object_commit_get_author_time(struct got_commit_object *commit)
305 return commit->author_time;
308 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
310 return commit->author_gmtoff;
313 const char *
314 got_object_commit_get_committer(struct got_commit_object *commit)
316 return commit->committer;
319 time_t
320 got_object_commit_get_committer_time(struct got_commit_object *commit)
322 return commit->committer_time;
325 time_t
326 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
328 return commit->committer_gmtoff;
331 const char *
332 got_object_commit_get_logmsg(struct got_commit_object *commit)
334 return commit->logmsg;
337 const struct got_error *
338 got_object_parse_commit(struct got_commit_object **commit, char *buf, size_t len)
340 const struct got_error *err = NULL;
341 char *s = buf;
342 size_t tlen;
343 ssize_t remain = (ssize_t)len;
345 *commit = got_object_commit_alloc_partial();
346 if (*commit == NULL)
347 return got_error_from_errno();
349 tlen = strlen(GOT_COMMIT_TAG_TREE);
350 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
351 remain -= tlen;
352 if (remain < SHA1_DIGEST_STRING_LENGTH) {
353 err = got_error(GOT_ERR_BAD_OBJ_DATA);
354 goto done;
356 s += tlen;
357 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
358 err = got_error(GOT_ERR_BAD_OBJ_DATA);
359 goto done;
361 remain -= SHA1_DIGEST_STRING_LENGTH;
362 s += SHA1_DIGEST_STRING_LENGTH;
363 } else {
364 err = got_error(GOT_ERR_BAD_OBJ_DATA);
365 goto done;
368 tlen = strlen(GOT_COMMIT_TAG_PARENT);
369 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
370 remain -= tlen;
371 if (remain < SHA1_DIGEST_STRING_LENGTH) {
372 err = got_error(GOT_ERR_BAD_OBJ_DATA);
373 goto done;
375 s += tlen;
376 err = got_object_commit_add_parent(*commit, s);
377 if (err)
378 goto done;
380 remain -= SHA1_DIGEST_STRING_LENGTH;
381 s += SHA1_DIGEST_STRING_LENGTH;
384 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
385 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
386 char *p;
387 size_t slen;
389 remain -= tlen;
390 if (remain <= 0) {
391 err = got_error(GOT_ERR_BAD_OBJ_DATA);
392 goto done;
394 s += tlen;
395 p = strchr(s, '\n');
396 if (p == NULL) {
397 err = got_error(GOT_ERR_BAD_OBJ_DATA);
398 goto done;
400 *p = '\0';
401 slen = strlen(s);
402 err = parse_commit_time(&(*commit)->author_time,
403 &(*commit)->author_gmtoff, s);
404 if (err)
405 goto done;
406 (*commit)->author = strdup(s);
407 if ((*commit)->author == NULL) {
408 err = got_error_from_errno();
409 goto done;
411 s += slen + 1;
412 remain -= slen + 1;
415 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
416 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
417 char *p;
418 size_t slen;
420 remain -= tlen;
421 if (remain <= 0) {
422 err = got_error(GOT_ERR_BAD_OBJ_DATA);
423 goto done;
425 s += tlen;
426 p = strchr(s, '\n');
427 if (p == NULL) {
428 err = got_error(GOT_ERR_BAD_OBJ_DATA);
429 goto done;
431 *p = '\0';
432 slen = strlen(s);
433 err = parse_commit_time(&(*commit)->committer_time,
434 &(*commit)->committer_gmtoff, s);
435 if (err)
436 goto done;
437 (*commit)->committer = strdup(s);
438 if ((*commit)->committer == NULL) {
439 err = got_error_from_errno();
440 goto done;
442 s += slen + 1;
443 remain -= slen + 1;
446 (*commit)->logmsg = strndup(s, remain);
447 if ((*commit)->logmsg == NULL) {
448 err = got_error_from_errno();
449 goto done;
451 done:
452 if (err) {
453 got_object_commit_close(*commit);
454 *commit = NULL;
456 return err;
459 void
460 got_object_tree_entry_close(struct got_tree_entry *te)
462 free(te->id);
463 free(te->name);
464 free(te);
467 void
468 got_object_tree_close(struct got_tree_object *tree)
470 struct got_tree_entry *te;
472 if (tree->refcnt > 0) {
473 tree->refcnt--;
474 if (tree->refcnt > 0)
475 return;
478 while (!SIMPLEQ_EMPTY(&tree->entries.head)) {
479 te = SIMPLEQ_FIRST(&tree->entries.head);
480 SIMPLEQ_REMOVE_HEAD(&tree->entries.head, entry);
481 got_object_tree_entry_close(te);
484 free(tree);
487 struct got_tree_entry *
488 got_alloc_tree_entry_partial(void)
490 struct got_tree_entry *te;
492 te = malloc(sizeof(*te));
493 if (te == NULL)
494 return NULL;
496 te->id = malloc(sizeof(*te->id));
497 if (te->id == NULL) {
498 free(te);
499 te = NULL;
501 return te;
504 static const struct got_error *
505 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
506 size_t maxlen)
508 char *p = buf, *space;
509 const struct got_error *err = NULL;
511 *te = got_alloc_tree_entry_partial();
512 if (*te == NULL)
513 return got_error_from_errno();
515 *elen = strlen(buf) + 1;
516 if (*elen > maxlen) {
517 free(*te);
518 *te = NULL;
519 return got_error(GOT_ERR_BAD_OBJ_DATA);
522 space = strchr(buf, ' ');
523 if (space == NULL) {
524 err = got_error(GOT_ERR_BAD_OBJ_DATA);
525 free(*te);
526 *te = NULL;
527 return err;
529 (*te)->mode = 0;
530 while (*p != ' ') {
531 if (*p < '0' && *p > '7') {
532 err = got_error(GOT_ERR_BAD_OBJ_DATA);
533 goto done;
535 (*te)->mode <<= 3;
536 (*te)->mode |= *p - '0';
537 p++;
540 (*te)->name = strdup(space + 1);
541 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
542 err = got_error(GOT_ERR_BAD_OBJ_DATA);
543 goto done;
545 buf += *elen;
546 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
547 *elen += SHA1_DIGEST_LENGTH;
548 done:
549 if (err) {
550 got_object_tree_entry_close(*te);
551 *te = NULL;
553 return err;
556 const struct got_error *
557 got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
559 const struct got_error *err;
560 size_t remain = len;
562 *tree = calloc(1, sizeof(**tree));
563 if (*tree == NULL)
564 return got_error_from_errno();
566 SIMPLEQ_INIT(&(*tree)->entries.head);
568 while (remain > 0) {
569 struct got_tree_entry *te;
570 size_t elen;
572 err = parse_tree_entry(&te, &elen, buf, remain);
573 if (err)
574 return err;
575 (*tree)->entries.nentries++;
576 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
577 buf += elen;
578 remain -= elen;
581 if (remain != 0) {
582 got_object_tree_close(*tree);
583 *tree = NULL;
584 return got_error(GOT_ERR_BAD_OBJ_DATA);
587 return NULL;
590 void
591 got_object_tag_close(struct got_tag_object *tag)
593 free(tag->tag);
594 free(tag->tagger);
595 free(tag->tagmsg);
596 free(tag);
599 const struct got_error *
600 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
602 const struct got_error *err = NULL;
603 size_t remain = len;
604 char *s = buf;
605 size_t tlen;
607 *tag = calloc(1, sizeof(**tag));
608 if (*tag == NULL)
609 return got_error_from_errno();
611 tlen = strlen(GOT_TAG_TAG_OBJECT);
612 if (strncmp(s, GOT_TAG_TAG_OBJECT, tlen) == 0) {
613 remain -= tlen;
614 if (remain < SHA1_DIGEST_STRING_LENGTH) {
615 err = got_error(GOT_ERR_BAD_OBJ_DATA);
616 goto done;
618 s += tlen;
619 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
620 err = got_error(GOT_ERR_BAD_OBJ_DATA);
621 goto done;
623 remain -= SHA1_DIGEST_STRING_LENGTH;
624 s += SHA1_DIGEST_STRING_LENGTH;
625 } else {
626 err = got_error(GOT_ERR_BAD_OBJ_DATA);
627 goto done;
630 if (remain <= 0) {
631 err = got_error(GOT_ERR_BAD_OBJ_DATA);
632 goto done;
635 tlen = strlen(GOT_TAG_TAG_TYPE);
636 if (strncmp(s, GOT_TAG_TAG_TYPE, tlen) == 0) {
637 remain -= tlen;
638 if (remain <= 0) {
639 err = got_error(GOT_ERR_BAD_OBJ_DATA);
640 goto done;
642 s += tlen;
643 if (strncmp(s, GOT_OBJ_TAG_COMMIT,
644 strlen(GOT_OBJ_TAG_COMMIT)) == 0) {
645 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
646 tlen = strlen(GOT_OBJ_TAG_COMMIT);
647 s += tlen;
648 remain -= tlen;
649 } else if (strncmp(s, GOT_OBJ_TAG_TREE,
650 strlen(GOT_OBJ_TAG_TREE)) == 0) {
651 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
652 tlen = strlen(GOT_OBJ_TAG_TREE);
653 s += tlen;
654 remain -= tlen;
655 } else if (strncmp(s, GOT_OBJ_TAG_BLOB,
656 strlen(GOT_OBJ_TAG_BLOB)) == 0) {
657 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
658 tlen = strlen(GOT_OBJ_TAG_BLOB);
659 s += tlen;
660 remain -= tlen;
661 } else if (strncmp(s, GOT_OBJ_TAG_TAG,
662 strlen(GOT_OBJ_TAG_TAG)) == 0) {
663 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
664 tlen = strlen(GOT_OBJ_TAG_TAG);
665 s += tlen;
666 remain -= tlen;
667 } else {
668 err = got_error(GOT_ERR_BAD_OBJ_DATA);
669 goto done;
672 if (remain <= 0 || *s != '\n') {
673 err = got_error(GOT_ERR_BAD_OBJ_DATA);
674 goto done;
676 s++;
677 remain--;
678 if (remain <= 0) {
679 err = got_error(GOT_ERR_BAD_OBJ_DATA);
680 goto done;
682 } else {
683 err = got_error(GOT_ERR_BAD_OBJ_DATA);
684 goto done;
687 tlen = strlen(GOT_TAG_TAG_TAG);
688 if (strncmp(s, GOT_TAG_TAG_TAG, tlen) == 0) {
689 char *p;
690 size_t slen;
691 remain -= tlen;
692 if (remain <= 0) {
693 err = got_error(GOT_ERR_BAD_OBJ_DATA);
694 goto done;
696 s += tlen;
697 p = strchr(s, '\n');
698 if (p == NULL) {
699 err = got_error(GOT_ERR_BAD_OBJ_DATA);
700 goto done;
702 *p = '\0';
703 slen = strlen(s);
704 (*tag)->tag = strndup(s, slen);
705 if ((*tag)->tag == NULL) {
706 err = got_error_from_errno();
707 goto done;
709 s += slen + 1;
710 remain -= slen + 1;
711 if (remain <= 0) {
712 err = got_error(GOT_ERR_BAD_OBJ_DATA);
713 goto done;
715 } else {
716 err = got_error(GOT_ERR_BAD_OBJ_DATA);
717 goto done;
720 tlen = strlen(GOT_TAG_TAG_TAGGER);
721 if (strncmp(s, GOT_TAG_TAG_TAGGER, tlen) == 0) {
722 char *p;
723 size_t slen;
725 remain -= tlen;
726 if (remain <= 0) {
727 err = got_error(GOT_ERR_BAD_OBJ_DATA);
728 goto done;
730 s += tlen;
731 p = strchr(s, '\n');
732 if (p == NULL) {
733 err = got_error(GOT_ERR_BAD_OBJ_DATA);
734 goto done;
736 *p = '\0';
737 slen = strlen(s);
738 err = parse_commit_time(&(*tag)->tagger_time,
739 &(*tag)->tagger_gmtoff, s);
740 if (err)
741 goto done;
742 (*tag)->tagger = strdup(s);
743 if ((*tag)->tagger == NULL) {
744 err = got_error_from_errno();
745 goto done;
747 s += slen + 1;
748 remain -= slen + 1;
749 if (remain <= 0) {
750 err = got_error(GOT_ERR_BAD_OBJ_DATA);
751 goto done;
753 } else {
754 err = got_error(GOT_ERR_BAD_OBJ_DATA);
755 goto done;
758 (*tag)->tagmsg = strndup(s, remain);
759 if ((*tag)->tagmsg == NULL) {
760 err = got_error_from_errno();
761 goto done;
763 done:
764 if (err) {
765 got_object_tag_close(*tag);
766 *tag = NULL;
768 return err;
771 const struct got_error *
772 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
774 const struct got_error *err = NULL;
775 static const size_t blocksize = 512;
776 size_t n, total, remain;
777 uint8_t *buf;
779 *outbuf = NULL;
780 *outlen = 0;
782 buf = malloc(blocksize);
783 if (buf == NULL)
784 return got_error_from_errno();
786 remain = blocksize;
787 total = 0;
788 while (1) {
789 if (remain == 0) {
790 uint8_t *newbuf;
791 newbuf = reallocarray(buf, 1, total + blocksize);
792 if (newbuf == NULL) {
793 err = got_error_from_errno();
794 goto done;
796 buf = newbuf;
797 remain += blocksize;
799 n = fread(buf + total, 1, remain, f);
800 if (n == 0) {
801 if (ferror(f)) {
802 err = got_ferror(f, GOT_ERR_IO);
803 goto done;
805 break; /* EOF */
807 remain -= n;
808 total += n;
809 };
811 done:
812 if (err == NULL) {
813 *outbuf = buf;
814 *outlen = total;
815 } else
816 free(buf);
817 return err;