2 * Copyright (c) 2018 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.
17 #include <sys/types.h>
19 #include <sys/queue.h>
21 #include <sys/socket.h>
22 #include <sys/syslimits.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"
53 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
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 "
72 got_object_id_cmp(const struct got_object_id *id1,
73 const struct got_object_id *id2)
75 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
78 const struct got_error *
79 got_object_qid_alloc_partial(struct got_object_qid **qid)
81 const struct got_error *err = NULL;
83 *qid = malloc(sizeof(**qid));
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);
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);
105 return got_error_from_errno();
107 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
110 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
117 got_object_close(struct got_object *obj)
119 if (obj->refcnt > 0) {
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);
139 got_object_qid_free(struct got_object_qid *qid)
145 const struct got_error *
146 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
148 const char *obj_tags[] = {
154 const int obj_types[] = {
161 size_t size = 0, hdrlen = 0;
163 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 return got_error_from_errno();
196 (*obj)->hdrlen = hdrlen;
201 const struct got_error *
202 got_object_read_header(struct got_object **obj, int fd)
204 const struct got_error *err;
205 struct got_zstream_buf zb;
207 const size_t zbsize = 64;
208 size_t outlen, totlen;
213 buf = malloc(zbsize);
215 return got_error_from_errno();
217 err = got_inflate_init(&zb, buf, zbsize);
223 err = got_inflate_read_fd(&zb, fd, &outlen);
229 if (strchr(zb.outbuf, '\0') == NULL) {
232 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
233 if (newbuf == NULL) {
234 err = got_error_from_errno();
238 zb.outbuf = newbuf + totlen;
239 zb.outlen = (nbuf * zbsize) - totlen;
241 } while (strchr(zb.outbuf, '\0') == NULL);
243 err = got_object_parse_header(obj, buf, totlen);
246 got_inflate_end(&zb);
250 struct got_commit_object *
251 got_object_commit_alloc_partial(void)
253 struct got_commit_object *commit;
255 commit = calloc(1, sizeof(*commit));
258 commit->tree_id = malloc(sizeof(*commit->tree_id));
259 if (commit->tree_id == NULL) {
264 SIMPLEQ_INIT(&commit->parent_ids);
269 const struct got_error *
270 got_object_commit_add_parent(struct got_commit_object *commit,
273 const struct got_error *err = NULL;
274 struct got_object_qid *qid;
276 err = got_object_qid_alloc_partial(&qid);
280 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
281 err = got_error(GOT_ERR_BAD_OBJ_DATA);
287 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
293 static const struct got_error *
294 parse_gmtoff(time_t *gmtoff, const char *tzstr)
297 const char *p = tzstr;
305 return got_error(GOT_ERR_BAD_OBJ_DATA);
307 if (!isdigit(*p) && !isdigit(*(p + 1)))
308 return got_error(GOT_ERR_BAD_OBJ_DATA);
309 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
312 if (!isdigit(*p) && !isdigit(*(p + 1)))
313 return got_error(GOT_ERR_BAD_OBJ_DATA);
314 m = ((*p - '0') * 10) + (*(p + 1) - '0');
316 *gmtoff = (h * 60 * 60 + m * 60) * sign;
320 static const struct got_error *
321 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
323 const struct got_error *err = NULL;
327 /* Parse and strip off trailing timezone indicator string. */
328 space = strrchr(committer, ' ');
330 return got_error(GOT_ERR_BAD_OBJ_DATA);
331 tzstr = strdup(space + 1);
333 return got_error_from_errno();
334 err = parse_gmtoff(gmtoff, tzstr);
340 /* Timestamp is separated from committer name + email by space. */
341 space = strrchr(committer, ' ');
343 return got_error(GOT_ERR_BAD_OBJ_DATA);
345 /* Timestamp parsed here is expressed in comitter's local time. */
346 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
348 return got_error(GOT_ERR_BAD_OBJ_DATA);
350 /* Express the time stamp in UTC. */
353 /* Strip off parsed time information, leaving just author and email. */
360 got_object_commit_close(struct got_commit_object *commit)
362 struct got_object_qid *qid;
364 if (commit->refcnt > 0) {
366 if (commit->refcnt > 0)
370 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
371 qid = SIMPLEQ_FIRST(&commit->parent_ids);
372 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
373 got_object_qid_free(qid);
376 free(commit->tree_id);
377 free(commit->author);
378 free(commit->committer);
379 free(commit->logmsg);
383 struct got_object_id *
384 got_object_commit_get_tree_id(struct got_commit_object *commit)
386 return commit->tree_id;
390 got_object_commit_get_nparents(struct got_commit_object *commit)
392 return commit->nparents;
395 const struct got_object_id_queue *
396 got_object_commit_get_parent_ids(struct got_commit_object *commit)
398 return &commit->parent_ids;
402 got_object_commit_get_author(struct got_commit_object *commit)
404 return commit->author;
408 got_object_commit_get_author_time(struct got_commit_object *commit)
410 return commit->author_time;
413 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
415 return commit->author_gmtoff;
419 got_object_commit_get_committer(struct got_commit_object *commit)
421 return commit->committer;
425 got_object_commit_get_committer_time(struct got_commit_object *commit)
427 return commit->committer_time;
431 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
433 return commit->committer_gmtoff;
437 got_object_commit_get_logmsg(struct got_commit_object *commit)
439 return commit->logmsg;
442 const struct got_error *
443 got_object_parse_commit(struct got_commit_object **commit, char *buf,
446 const struct got_error *err = NULL;
449 ssize_t remain = (ssize_t)len;
451 *commit = got_object_commit_alloc_partial();
453 return got_error_from_errno();
455 tlen = strlen(GOT_COMMIT_TAG_TREE);
456 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
458 if (remain < SHA1_DIGEST_STRING_LENGTH) {
459 err = got_error(GOT_ERR_BAD_OBJ_DATA);
463 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
464 err = got_error(GOT_ERR_BAD_OBJ_DATA);
467 remain -= SHA1_DIGEST_STRING_LENGTH;
468 s += SHA1_DIGEST_STRING_LENGTH;
470 err = got_error(GOT_ERR_BAD_OBJ_DATA);
474 tlen = strlen(GOT_COMMIT_TAG_PARENT);
475 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
477 if (remain < SHA1_DIGEST_STRING_LENGTH) {
478 err = got_error(GOT_ERR_BAD_OBJ_DATA);
482 err = got_object_commit_add_parent(*commit, s);
486 remain -= SHA1_DIGEST_STRING_LENGTH;
487 s += SHA1_DIGEST_STRING_LENGTH;
490 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
491 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
497 err = got_error(GOT_ERR_BAD_OBJ_DATA);
503 err = got_error(GOT_ERR_BAD_OBJ_DATA);
508 err = parse_commit_time(&(*commit)->author_time,
509 &(*commit)->author_gmtoff, s);
512 (*commit)->author = strdup(s);
513 if ((*commit)->author == NULL) {
514 err = got_error_from_errno();
521 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
522 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
528 err = got_error(GOT_ERR_BAD_OBJ_DATA);
534 err = got_error(GOT_ERR_BAD_OBJ_DATA);
539 err = parse_commit_time(&(*commit)->committer_time,
540 &(*commit)->committer_gmtoff, s);
543 (*commit)->committer = strdup(s);
544 if ((*commit)->committer == NULL) {
545 err = got_error_from_errno();
552 (*commit)->logmsg = strndup(s, remain);
553 if ((*commit)->logmsg == NULL) {
554 err = got_error_from_errno();
559 got_object_commit_close(*commit);
566 got_object_tree_entry_close(struct got_tree_entry *te)
574 got_object_tree_close(struct got_tree_object *tree)
576 struct got_tree_entry *te;
578 if (tree->refcnt > 0) {
580 if (tree->refcnt > 0)
584 while (!SIMPLEQ_EMPTY(&tree->entries.head)) {
585 te = SIMPLEQ_FIRST(&tree->entries.head);
586 SIMPLEQ_REMOVE_HEAD(&tree->entries.head, entry);
587 got_object_tree_entry_close(te);
593 struct got_tree_entry *
594 got_alloc_tree_entry_partial(void)
596 struct got_tree_entry *te;
598 te = malloc(sizeof(*te));
602 te->id = malloc(sizeof(*te->id));
603 if (te->id == NULL) {
610 static const struct got_error *
611 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
614 char *p = buf, *space;
615 const struct got_error *err = NULL;
617 *te = got_alloc_tree_entry_partial();
619 return got_error_from_errno();
621 *elen = strlen(buf) + 1;
622 if (*elen > maxlen) {
625 return got_error(GOT_ERR_BAD_OBJ_DATA);
628 space = strchr(buf, ' ');
630 err = got_error(GOT_ERR_BAD_OBJ_DATA);
637 if (*p < '0' && *p > '7') {
638 err = got_error(GOT_ERR_BAD_OBJ_DATA);
642 (*te)->mode |= *p - '0';
646 (*te)->name = strdup(space + 1);
647 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
648 err = got_error(GOT_ERR_BAD_OBJ_DATA);
652 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
653 *elen += SHA1_DIGEST_LENGTH;
656 got_object_tree_entry_close(*te);
662 const struct got_error *
663 got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
665 const struct got_error *err;
668 *tree = calloc(1, sizeof(**tree));
670 return got_error_from_errno();
672 SIMPLEQ_INIT(&(*tree)->entries.head);
675 struct got_tree_entry *te;
678 err = parse_tree_entry(&te, &elen, buf, remain);
681 (*tree)->entries.nentries++;
682 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
688 got_object_tree_close(*tree);
690 return got_error(GOT_ERR_BAD_OBJ_DATA);
697 got_object_tag_close(struct got_tag_object *tag)
705 const struct got_error *
706 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
708 const struct got_error *err = NULL;
713 *tag = calloc(1, sizeof(**tag));
715 return got_error_from_errno();
717 tlen = strlen(GOT_TAG_TAG_OBJECT);
718 if (strncmp(s, GOT_TAG_TAG_OBJECT, tlen) == 0) {
720 if (remain < SHA1_DIGEST_STRING_LENGTH) {
721 err = got_error(GOT_ERR_BAD_OBJ_DATA);
725 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
726 err = got_error(GOT_ERR_BAD_OBJ_DATA);
729 remain -= SHA1_DIGEST_STRING_LENGTH;
730 s += SHA1_DIGEST_STRING_LENGTH;
732 err = got_error(GOT_ERR_BAD_OBJ_DATA);
737 err = got_error(GOT_ERR_BAD_OBJ_DATA);
741 tlen = strlen(GOT_TAG_TAG_TYPE);
742 if (strncmp(s, GOT_TAG_TAG_TYPE, tlen) == 0) {
745 err = got_error(GOT_ERR_BAD_OBJ_DATA);
749 if (strncmp(s, GOT_OBJ_TAG_COMMIT,
750 strlen(GOT_OBJ_TAG_COMMIT)) == 0) {
751 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
752 tlen = strlen(GOT_OBJ_TAG_COMMIT);
755 } else if (strncmp(s, GOT_OBJ_TAG_TREE,
756 strlen(GOT_OBJ_TAG_TREE)) == 0) {
757 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
758 tlen = strlen(GOT_OBJ_TAG_TREE);
761 } else if (strncmp(s, GOT_OBJ_TAG_BLOB,
762 strlen(GOT_OBJ_TAG_BLOB)) == 0) {
763 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
764 tlen = strlen(GOT_OBJ_TAG_BLOB);
767 } else if (strncmp(s, GOT_OBJ_TAG_TAG,
768 strlen(GOT_OBJ_TAG_TAG)) == 0) {
769 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
770 tlen = strlen(GOT_OBJ_TAG_TAG);
774 err = got_error(GOT_ERR_BAD_OBJ_DATA);
778 if (remain <= 0 || *s != '\n') {
779 err = got_error(GOT_ERR_BAD_OBJ_DATA);
785 err = got_error(GOT_ERR_BAD_OBJ_DATA);
789 err = got_error(GOT_ERR_BAD_OBJ_DATA);
793 tlen = strlen(GOT_TAG_TAG_TAG);
794 if (strncmp(s, GOT_TAG_TAG_TAG, tlen) == 0) {
799 err = got_error(GOT_ERR_BAD_OBJ_DATA);
805 err = got_error(GOT_ERR_BAD_OBJ_DATA);
810 (*tag)->tag = strndup(s, slen);
811 if ((*tag)->tag == NULL) {
812 err = got_error_from_errno();
818 err = got_error(GOT_ERR_BAD_OBJ_DATA);
822 err = got_error(GOT_ERR_BAD_OBJ_DATA);
826 tlen = strlen(GOT_TAG_TAG_TAGGER);
827 if (strncmp(s, GOT_TAG_TAG_TAGGER, tlen) == 0) {
833 err = got_error(GOT_ERR_BAD_OBJ_DATA);
839 err = got_error(GOT_ERR_BAD_OBJ_DATA);
844 err = parse_commit_time(&(*tag)->tagger_time,
845 &(*tag)->tagger_gmtoff, s);
848 (*tag)->tagger = strdup(s);
849 if ((*tag)->tagger == NULL) {
850 err = got_error_from_errno();
856 err = got_error(GOT_ERR_BAD_OBJ_DATA);
860 err = got_error(GOT_ERR_BAD_OBJ_DATA);
864 (*tag)->tagmsg = strndup(s, remain);
865 if ((*tag)->tagmsg == NULL) {
866 err = got_error_from_errno();
871 got_object_tag_close(*tag);
877 const struct got_error *
878 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
880 const struct got_error *err = NULL;
881 static const size_t blocksize = 512;
882 size_t n, total, remain;
888 buf = malloc(blocksize);
890 return got_error_from_errno();
897 newbuf = reallocarray(buf, 1, total + blocksize);
898 if (newbuf == NULL) {
899 err = got_error_from_errno();
905 n = fread(buf + total, 1, remain, f);
908 err = got_ferror(f, GOT_ERR_IO);