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, size_t len)
445 const struct got_error *err = NULL;
448 ssize_t remain = (ssize_t)len;
450 *commit = got_object_commit_alloc_partial();
452 return got_error_from_errno();
454 tlen = strlen(GOT_COMMIT_TAG_TREE);
455 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
457 if (remain < SHA1_DIGEST_STRING_LENGTH) {
458 err = got_error(GOT_ERR_BAD_OBJ_DATA);
462 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
463 err = got_error(GOT_ERR_BAD_OBJ_DATA);
466 remain -= SHA1_DIGEST_STRING_LENGTH;
467 s += SHA1_DIGEST_STRING_LENGTH;
469 err = got_error(GOT_ERR_BAD_OBJ_DATA);
473 tlen = strlen(GOT_COMMIT_TAG_PARENT);
474 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
476 if (remain < SHA1_DIGEST_STRING_LENGTH) {
477 err = got_error(GOT_ERR_BAD_OBJ_DATA);
481 err = got_object_commit_add_parent(*commit, s);
485 remain -= SHA1_DIGEST_STRING_LENGTH;
486 s += SHA1_DIGEST_STRING_LENGTH;
489 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
490 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
496 err = got_error(GOT_ERR_BAD_OBJ_DATA);
502 err = got_error(GOT_ERR_BAD_OBJ_DATA);
507 err = parse_commit_time(&(*commit)->author_time,
508 &(*commit)->author_gmtoff, s);
511 (*commit)->author = strdup(s);
512 if ((*commit)->author == NULL) {
513 err = got_error_from_errno();
520 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
521 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
527 err = got_error(GOT_ERR_BAD_OBJ_DATA);
533 err = got_error(GOT_ERR_BAD_OBJ_DATA);
538 err = parse_commit_time(&(*commit)->committer_time,
539 &(*commit)->committer_gmtoff, s);
542 (*commit)->committer = strdup(s);
543 if ((*commit)->committer == NULL) {
544 err = got_error_from_errno();
551 (*commit)->logmsg = strndup(s, remain);
552 if ((*commit)->logmsg == NULL) {
553 err = got_error_from_errno();
558 got_object_commit_close(*commit);
565 got_object_tree_entry_close(struct got_tree_entry *te)
573 got_object_tree_close(struct got_tree_object *tree)
575 struct got_tree_entry *te;
577 if (tree->refcnt > 0) {
579 if (tree->refcnt > 0)
583 while (!SIMPLEQ_EMPTY(&tree->entries.head)) {
584 te = SIMPLEQ_FIRST(&tree->entries.head);
585 SIMPLEQ_REMOVE_HEAD(&tree->entries.head, entry);
586 got_object_tree_entry_close(te);
592 struct got_tree_entry *
593 got_alloc_tree_entry_partial(void)
595 struct got_tree_entry *te;
597 te = malloc(sizeof(*te));
601 te->id = malloc(sizeof(*te->id));
602 if (te->id == NULL) {
609 static const struct got_error *
610 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
613 char *p = buf, *space;
614 const struct got_error *err = NULL;
616 *te = got_alloc_tree_entry_partial();
618 return got_error_from_errno();
620 *elen = strlen(buf) + 1;
621 if (*elen > maxlen) {
624 return got_error(GOT_ERR_BAD_OBJ_DATA);
627 space = strchr(buf, ' ');
629 err = got_error(GOT_ERR_BAD_OBJ_DATA);
636 if (*p < '0' && *p > '7') {
637 err = got_error(GOT_ERR_BAD_OBJ_DATA);
641 (*te)->mode |= *p - '0';
645 (*te)->name = strdup(space + 1);
646 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
647 err = got_error(GOT_ERR_BAD_OBJ_DATA);
651 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
652 *elen += SHA1_DIGEST_LENGTH;
655 got_object_tree_entry_close(*te);
661 const struct got_error *
662 got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
664 const struct got_error *err;
667 *tree = calloc(1, sizeof(**tree));
669 return got_error_from_errno();
671 SIMPLEQ_INIT(&(*tree)->entries.head);
674 struct got_tree_entry *te;
677 err = parse_tree_entry(&te, &elen, buf, remain);
680 (*tree)->entries.nentries++;
681 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
687 got_object_tree_close(*tree);
689 return got_error(GOT_ERR_BAD_OBJ_DATA);
696 got_object_tag_close(struct got_tag_object *tag)
704 const struct got_error *
705 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
707 const struct got_error *err = NULL;
712 *tag = calloc(1, sizeof(**tag));
714 return got_error_from_errno();
716 tlen = strlen(GOT_TAG_TAG_OBJECT);
717 if (strncmp(s, GOT_TAG_TAG_OBJECT, tlen) == 0) {
719 if (remain < SHA1_DIGEST_STRING_LENGTH) {
720 err = got_error(GOT_ERR_BAD_OBJ_DATA);
724 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
725 err = got_error(GOT_ERR_BAD_OBJ_DATA);
728 remain -= SHA1_DIGEST_STRING_LENGTH;
729 s += SHA1_DIGEST_STRING_LENGTH;
731 err = got_error(GOT_ERR_BAD_OBJ_DATA);
736 err = got_error(GOT_ERR_BAD_OBJ_DATA);
740 tlen = strlen(GOT_TAG_TAG_TYPE);
741 if (strncmp(s, GOT_TAG_TAG_TYPE, tlen) == 0) {
744 err = got_error(GOT_ERR_BAD_OBJ_DATA);
748 if (strncmp(s, GOT_OBJ_TAG_COMMIT,
749 strlen(GOT_OBJ_TAG_COMMIT)) == 0) {
750 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
751 tlen = strlen(GOT_OBJ_TAG_COMMIT);
754 } else if (strncmp(s, GOT_OBJ_TAG_TREE,
755 strlen(GOT_OBJ_TAG_TREE)) == 0) {
756 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
757 tlen = strlen(GOT_OBJ_TAG_TREE);
760 } else if (strncmp(s, GOT_OBJ_TAG_BLOB,
761 strlen(GOT_OBJ_TAG_BLOB)) == 0) {
762 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
763 tlen = strlen(GOT_OBJ_TAG_BLOB);
766 } else if (strncmp(s, GOT_OBJ_TAG_TAG,
767 strlen(GOT_OBJ_TAG_TAG)) == 0) {
768 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
769 tlen = strlen(GOT_OBJ_TAG_TAG);
773 err = got_error(GOT_ERR_BAD_OBJ_DATA);
777 if (remain <= 0 || *s != '\n') {
778 err = got_error(GOT_ERR_BAD_OBJ_DATA);
784 err = got_error(GOT_ERR_BAD_OBJ_DATA);
788 err = got_error(GOT_ERR_BAD_OBJ_DATA);
792 tlen = strlen(GOT_TAG_TAG_TAG);
793 if (strncmp(s, GOT_TAG_TAG_TAG, tlen) == 0) {
798 err = got_error(GOT_ERR_BAD_OBJ_DATA);
804 err = got_error(GOT_ERR_BAD_OBJ_DATA);
809 (*tag)->tag = strndup(s, slen);
810 if ((*tag)->tag == NULL) {
811 err = got_error_from_errno();
817 err = got_error(GOT_ERR_BAD_OBJ_DATA);
821 err = got_error(GOT_ERR_BAD_OBJ_DATA);
825 tlen = strlen(GOT_TAG_TAG_TAGGER);
826 if (strncmp(s, GOT_TAG_TAG_TAGGER, tlen) == 0) {
832 err = got_error(GOT_ERR_BAD_OBJ_DATA);
838 err = got_error(GOT_ERR_BAD_OBJ_DATA);
843 err = parse_commit_time(&(*tag)->tagger_time,
844 &(*tag)->tagger_gmtoff, s);
847 (*tag)->tagger = strdup(s);
848 if ((*tag)->tagger == NULL) {
849 err = got_error_from_errno();
855 err = got_error(GOT_ERR_BAD_OBJ_DATA);
859 err = got_error(GOT_ERR_BAD_OBJ_DATA);
863 (*tag)->tagmsg = strndup(s, remain);
864 if ((*tag)->tagmsg == NULL) {
865 err = got_error_from_errno();
870 got_object_tag_close(*tag);
876 const struct got_error *
877 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
879 const struct got_error *err = NULL;
880 static const size_t blocksize = 512;
881 size_t n, total, remain;
887 buf = malloc(blocksize);
889 return got_error_from_errno();
896 newbuf = reallocarray(buf, 1, total + blocksize);
897 if (newbuf == NULL) {
898 err = got_error_from_errno();
904 n = fread(buf + total, 1, remain, f);
907 err = got_ferror(f, GOT_ERR_IO);