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"
51 #include "got_lib_path.h"
54 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
57 #define GOT_OBJ_TAG_COMMIT "commit"
58 #define GOT_OBJ_TAG_TREE "tree"
59 #define GOT_OBJ_TAG_BLOB "blob"
60 #define GOT_OBJ_TAG_TAG "tag"
62 #define GOT_COMMIT_TAG_TREE "tree "
63 #define GOT_COMMIT_TAG_PARENT "parent "
64 #define GOT_COMMIT_TAG_AUTHOR "author "
65 #define GOT_COMMIT_TAG_COMMITTER "committer "
67 #define GOT_TAG_TAG_OBJECT "object "
68 #define GOT_TAG_TAG_TYPE "type "
69 #define GOT_TAG_TAG_TAG "tag "
70 #define GOT_TAG_TAG_TAGGER "tagger "
73 got_object_id_cmp(const struct got_object_id *id1,
74 const struct got_object_id *id2)
76 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
79 const struct got_error *
80 got_object_qid_alloc_partial(struct got_object_qid **qid)
82 const struct got_error *err = NULL;
84 *qid = malloc(sizeof(**qid));
86 return got_error_from_errno();
88 (*qid)->id = malloc(sizeof(*((*qid)->id)));
89 if ((*qid)->id == NULL) {
90 err = got_error_from_errno();
91 got_object_qid_free(*qid);
99 const struct got_error *
100 got_object_id_str(char **outbuf, struct got_object_id *id)
102 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
104 *outbuf = malloc(len);
106 return got_error_from_errno();
108 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
111 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
118 got_object_close(struct got_object *obj)
120 if (obj->refcnt > 0) {
126 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
127 struct got_delta *delta;
128 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
129 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
130 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
131 got_delta_close(delta);
134 if (obj->flags & GOT_OBJ_FLAG_PACKED)
135 free(obj->path_packfile);
140 got_object_qid_free(struct got_object_qid *qid)
146 const struct got_error *
147 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
149 const char *obj_tags[] = {
155 const int obj_types[] = {
162 size_t size = 0, hdrlen = 0;
164 char *p = strchr(buf, '\0');
169 return got_error(GOT_ERR_BAD_OBJ_HDR);
171 hdrlen = strlen(buf) + 1 /* '\0' */;
173 for (i = 0; i < nitems(obj_tags); i++) {
174 const char *tag = obj_tags[i];
175 size_t tlen = strlen(tag);
178 if (strncmp(buf, tag, tlen) != 0)
183 return got_error(GOT_ERR_BAD_OBJ_HDR);
184 size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
186 return got_error(GOT_ERR_BAD_OBJ_HDR);
191 return got_error(GOT_ERR_BAD_OBJ_HDR);
193 *obj = calloc(1, sizeof(**obj));
195 return got_error_from_errno();
197 (*obj)->hdrlen = hdrlen;
202 const struct got_error *
203 got_object_read_header(struct got_object **obj, int fd)
205 const struct got_error *err;
206 struct got_zstream_buf zb;
208 const size_t zbsize = 64;
209 size_t outlen, totlen;
214 buf = malloc(zbsize);
216 return got_error_from_errno();
218 err = got_inflate_init(&zb, buf, zbsize);
224 err = got_inflate_read_fd(&zb, fd, &outlen);
230 if (strchr(zb.outbuf, '\0') == NULL) {
233 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
234 if (newbuf == NULL) {
235 err = got_error_from_errno();
239 zb.outbuf = newbuf + totlen;
240 zb.outlen = (nbuf * zbsize) - totlen;
242 } while (strchr(zb.outbuf, '\0') == NULL);
244 err = got_object_parse_header(obj, buf, totlen);
247 got_inflate_end(&zb);
251 struct got_commit_object *
252 got_object_commit_alloc_partial(void)
254 struct got_commit_object *commit;
256 commit = calloc(1, sizeof(*commit));
259 commit->tree_id = malloc(sizeof(*commit->tree_id));
260 if (commit->tree_id == NULL) {
265 SIMPLEQ_INIT(&commit->parent_ids);
270 const struct got_error *
271 got_object_commit_add_parent(struct got_commit_object *commit,
274 const struct got_error *err = NULL;
275 struct got_object_qid *qid;
277 err = got_object_qid_alloc_partial(&qid);
281 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
282 err = got_error(GOT_ERR_BAD_OBJ_DATA);
288 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
294 static const struct got_error *
295 parse_gmtoff(time_t *gmtoff, const char *tzstr)
298 const char *p = tzstr;
306 return got_error(GOT_ERR_BAD_OBJ_DATA);
308 if (!isdigit(*p) && !isdigit(*(p + 1)))
309 return got_error(GOT_ERR_BAD_OBJ_DATA);
310 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
313 if (!isdigit(*p) && !isdigit(*(p + 1)))
314 return got_error(GOT_ERR_BAD_OBJ_DATA);
315 m = ((*p - '0') * 10) + (*(p + 1) - '0');
317 *gmtoff = (h * 60 * 60 + m * 60) * sign;
321 static const struct got_error *
322 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
324 const struct got_error *err = NULL;
328 /* Parse and strip off trailing timezone indicator string. */
329 space = strrchr(committer, ' ');
331 return got_error(GOT_ERR_BAD_OBJ_DATA);
332 tzstr = strdup(space + 1);
334 return got_error_from_errno();
335 err = parse_gmtoff(gmtoff, tzstr);
341 /* Timestamp is separated from committer name + email by space. */
342 space = strrchr(committer, ' ');
344 return got_error(GOT_ERR_BAD_OBJ_DATA);
346 /* Timestamp parsed here is expressed in comitter's local time. */
347 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
349 return got_error(GOT_ERR_BAD_OBJ_DATA);
351 /* Express the time stamp in UTC. */
354 /* Strip off parsed time information, leaving just author and email. */
361 got_object_commit_close(struct got_commit_object *commit)
363 struct got_object_qid *qid;
365 if (commit->refcnt > 0) {
367 if (commit->refcnt > 0)
371 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
372 qid = SIMPLEQ_FIRST(&commit->parent_ids);
373 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
374 got_object_qid_free(qid);
377 free(commit->tree_id);
378 free(commit->author);
379 free(commit->committer);
380 free(commit->logmsg);
384 struct got_object_id *
385 got_object_commit_get_tree_id(struct got_commit_object *commit)
387 return commit->tree_id;
391 got_object_commit_get_nparents(struct got_commit_object *commit)
393 return commit->nparents;
396 const struct got_object_id_queue *
397 got_object_commit_get_parent_ids(struct got_commit_object *commit)
399 return &commit->parent_ids;
403 got_object_commit_get_author(struct got_commit_object *commit)
405 return commit->author;
409 got_object_commit_get_author_time(struct got_commit_object *commit)
411 return commit->author_time;
414 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
416 return commit->author_gmtoff;
420 got_object_commit_get_committer(struct got_commit_object *commit)
422 return commit->committer;
426 got_object_commit_get_committer_time(struct got_commit_object *commit)
428 return commit->committer_time;
432 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
434 return commit->committer_gmtoff;
438 got_object_commit_get_logmsg(struct got_commit_object *commit)
440 return commit->logmsg;
443 const struct got_error *
444 got_object_parse_commit(struct got_commit_object **commit, char *buf,
447 const struct got_error *err = NULL;
450 ssize_t remain = (ssize_t)len;
452 *commit = got_object_commit_alloc_partial();
454 return got_error_from_errno();
456 tlen = strlen(GOT_COMMIT_TAG_TREE);
457 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
459 if (remain < SHA1_DIGEST_STRING_LENGTH) {
460 err = got_error(GOT_ERR_BAD_OBJ_DATA);
464 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
465 err = got_error(GOT_ERR_BAD_OBJ_DATA);
468 remain -= SHA1_DIGEST_STRING_LENGTH;
469 s += SHA1_DIGEST_STRING_LENGTH;
471 err = got_error(GOT_ERR_BAD_OBJ_DATA);
475 tlen = strlen(GOT_COMMIT_TAG_PARENT);
476 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
478 if (remain < SHA1_DIGEST_STRING_LENGTH) {
479 err = got_error(GOT_ERR_BAD_OBJ_DATA);
483 err = got_object_commit_add_parent(*commit, s);
487 remain -= SHA1_DIGEST_STRING_LENGTH;
488 s += SHA1_DIGEST_STRING_LENGTH;
491 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
492 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
498 err = got_error(GOT_ERR_BAD_OBJ_DATA);
504 err = got_error(GOT_ERR_BAD_OBJ_DATA);
509 err = parse_commit_time(&(*commit)->author_time,
510 &(*commit)->author_gmtoff, s);
513 (*commit)->author = strdup(s);
514 if ((*commit)->author == NULL) {
515 err = got_error_from_errno();
522 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
523 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
529 err = got_error(GOT_ERR_BAD_OBJ_DATA);
535 err = got_error(GOT_ERR_BAD_OBJ_DATA);
540 err = parse_commit_time(&(*commit)->committer_time,
541 &(*commit)->committer_gmtoff, s);
544 (*commit)->committer = strdup(s);
545 if ((*commit)->committer == NULL) {
546 err = got_error_from_errno();
553 (*commit)->logmsg = strndup(s, remain);
554 if ((*commit)->logmsg == NULL) {
555 err = got_error_from_errno();
560 got_object_commit_close(*commit);
567 got_object_tree_entry_close(struct got_tree_entry *te)
575 got_object_tree_close(struct got_tree_object *tree)
577 struct got_tree_entry *te;
579 if (tree->refcnt > 0) {
581 if (tree->refcnt > 0)
585 while (!SIMPLEQ_EMPTY(&tree->entries.head)) {
586 te = SIMPLEQ_FIRST(&tree->entries.head);
587 SIMPLEQ_REMOVE_HEAD(&tree->entries.head, entry);
588 got_object_tree_entry_close(te);
594 struct got_tree_entry *
595 got_alloc_tree_entry_partial(void)
597 struct got_tree_entry *te;
599 te = malloc(sizeof(*te));
603 te->id = malloc(sizeof(*te->id));
604 if (te->id == NULL) {
611 static const struct got_error *
612 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
615 char *p = buf, *space;
616 const struct got_error *err = NULL;
618 *te = got_alloc_tree_entry_partial();
620 return got_error_from_errno();
622 *elen = strlen(buf) + 1;
623 if (*elen > maxlen) {
626 return got_error(GOT_ERR_BAD_OBJ_DATA);
629 space = strchr(buf, ' ');
631 err = got_error(GOT_ERR_BAD_OBJ_DATA);
638 if (*p < '0' && *p > '7') {
639 err = got_error(GOT_ERR_BAD_OBJ_DATA);
643 (*te)->mode |= *p - '0';
647 (*te)->name = strdup(space + 1);
648 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
649 err = got_error(GOT_ERR_BAD_OBJ_DATA);
653 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
654 *elen += SHA1_DIGEST_LENGTH;
657 got_object_tree_entry_close(*te);
663 const struct got_error *
664 got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
666 const struct got_error *err;
668 struct got_pathlist_head pathlist;
669 struct got_pathlist_entry *pe;
671 TAILQ_INIT(&pathlist);
673 *tree = calloc(1, sizeof(**tree));
675 return got_error_from_errno();
677 SIMPLEQ_INIT(&(*tree)->entries.head);
680 struct got_tree_entry *te;
681 struct got_pathlist_entry *new = NULL;
684 err = parse_tree_entry(&te, &elen, buf, remain);
687 err = got_pathlist_insert(&new, &pathlist, te->name, te);
691 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
699 got_object_tree_close(*tree);
701 err = got_error(GOT_ERR_BAD_OBJ_DATA);
705 TAILQ_FOREACH(pe, &pathlist, entry) {
706 struct got_tree_entry *te = pe->data;
707 (*tree)->entries.nentries++;
708 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
711 got_pathlist_free(&pathlist);
716 got_object_tag_close(struct got_tag_object *tag)
724 const struct got_error *
725 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
727 const struct got_error *err = NULL;
732 *tag = calloc(1, sizeof(**tag));
734 return got_error_from_errno();
736 tlen = strlen(GOT_TAG_TAG_OBJECT);
737 if (strncmp(s, GOT_TAG_TAG_OBJECT, tlen) == 0) {
739 if (remain < SHA1_DIGEST_STRING_LENGTH) {
740 err = got_error(GOT_ERR_BAD_OBJ_DATA);
744 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
745 err = got_error(GOT_ERR_BAD_OBJ_DATA);
748 remain -= SHA1_DIGEST_STRING_LENGTH;
749 s += SHA1_DIGEST_STRING_LENGTH;
751 err = got_error(GOT_ERR_BAD_OBJ_DATA);
756 err = got_error(GOT_ERR_BAD_OBJ_DATA);
760 tlen = strlen(GOT_TAG_TAG_TYPE);
761 if (strncmp(s, GOT_TAG_TAG_TYPE, tlen) == 0) {
764 err = got_error(GOT_ERR_BAD_OBJ_DATA);
768 if (strncmp(s, GOT_OBJ_TAG_COMMIT,
769 strlen(GOT_OBJ_TAG_COMMIT)) == 0) {
770 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
771 tlen = strlen(GOT_OBJ_TAG_COMMIT);
774 } else if (strncmp(s, GOT_OBJ_TAG_TREE,
775 strlen(GOT_OBJ_TAG_TREE)) == 0) {
776 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
777 tlen = strlen(GOT_OBJ_TAG_TREE);
780 } else if (strncmp(s, GOT_OBJ_TAG_BLOB,
781 strlen(GOT_OBJ_TAG_BLOB)) == 0) {
782 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
783 tlen = strlen(GOT_OBJ_TAG_BLOB);
786 } else if (strncmp(s, GOT_OBJ_TAG_TAG,
787 strlen(GOT_OBJ_TAG_TAG)) == 0) {
788 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
789 tlen = strlen(GOT_OBJ_TAG_TAG);
793 err = got_error(GOT_ERR_BAD_OBJ_DATA);
797 if (remain <= 0 || *s != '\n') {
798 err = got_error(GOT_ERR_BAD_OBJ_DATA);
804 err = got_error(GOT_ERR_BAD_OBJ_DATA);
808 err = got_error(GOT_ERR_BAD_OBJ_DATA);
812 tlen = strlen(GOT_TAG_TAG_TAG);
813 if (strncmp(s, GOT_TAG_TAG_TAG, tlen) == 0) {
818 err = got_error(GOT_ERR_BAD_OBJ_DATA);
824 err = got_error(GOT_ERR_BAD_OBJ_DATA);
829 (*tag)->tag = strndup(s, slen);
830 if ((*tag)->tag == NULL) {
831 err = got_error_from_errno();
837 err = got_error(GOT_ERR_BAD_OBJ_DATA);
841 err = got_error(GOT_ERR_BAD_OBJ_DATA);
845 tlen = strlen(GOT_TAG_TAG_TAGGER);
846 if (strncmp(s, GOT_TAG_TAG_TAGGER, tlen) == 0) {
852 err = got_error(GOT_ERR_BAD_OBJ_DATA);
858 err = got_error(GOT_ERR_BAD_OBJ_DATA);
863 err = parse_commit_time(&(*tag)->tagger_time,
864 &(*tag)->tagger_gmtoff, s);
867 (*tag)->tagger = strdup(s);
868 if ((*tag)->tagger == NULL) {
869 err = got_error_from_errno();
875 err = got_error(GOT_ERR_BAD_OBJ_DATA);
879 /* Some old tags in the Linux git repo have no tagger. */
880 (*tag)->tagger = strdup("");
881 if ((*tag)->tagger == NULL) {
882 err = got_error_from_errno();
887 (*tag)->tagmsg = strndup(s, remain);
888 if ((*tag)->tagmsg == NULL) {
889 err = got_error_from_errno();
894 got_object_tag_close(*tag);
900 const struct got_error *
901 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
903 const struct got_error *err = NULL;
904 static const size_t blocksize = 512;
905 size_t n, total, remain;
911 buf = malloc(blocksize);
913 return got_error_from_errno();
920 newbuf = reallocarray(buf, 1, total + blocksize);
921 if (newbuf == NULL) {
922 err = got_error_from_errno();
928 n = fread(buf + total, 1, remain, f);
931 err = got_ferror(f, GOT_ERR_IO);