2 * Copyright (c) 2018, 2019 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]))
58 got_object_id_cmp(const struct got_object_id *id1,
59 const struct got_object_id *id2)
61 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
64 const struct got_error *
65 got_object_qid_alloc_partial(struct got_object_qid **qid)
67 const struct got_error *err = NULL;
69 *qid = malloc(sizeof(**qid));
71 return got_error_from_errno();
73 (*qid)->id = malloc(sizeof(*((*qid)->id)));
74 if ((*qid)->id == NULL) {
75 err = got_error_from_errno();
76 got_object_qid_free(*qid);
84 const struct got_error *
85 got_object_id_str(char **outbuf, struct got_object_id *id)
87 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
89 *outbuf = malloc(len);
91 return got_error_from_errno();
93 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
96 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
103 got_object_close(struct got_object *obj)
105 if (obj->refcnt > 0) {
111 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
112 struct got_delta *delta;
113 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
114 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
115 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
116 got_delta_close(delta);
119 if (obj->flags & GOT_OBJ_FLAG_PACKED)
120 free(obj->path_packfile);
125 got_object_qid_free(struct got_object_qid *qid)
131 const struct got_error *
132 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
134 const char *obj_labels[] = {
135 GOT_OBJ_LABEL_COMMIT,
140 const int obj_types[] = {
147 size_t size = 0, hdrlen = 0;
152 hdrlen = strnlen(buf, len) + 1 /* '\0' */;
154 return got_error(GOT_ERR_BAD_OBJ_HDR);
156 for (i = 0; i < nitems(obj_labels); i++) {
157 const char *label = obj_labels[i];
158 size_t label_len = strlen(label);
161 if (strncmp(buf, label, label_len) != 0)
165 if (len <= label_len)
166 return got_error(GOT_ERR_BAD_OBJ_HDR);
167 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
169 return got_error(GOT_ERR_BAD_OBJ_HDR);
174 return got_error(GOT_ERR_BAD_OBJ_HDR);
176 *obj = calloc(1, sizeof(**obj));
178 return got_error_from_errno();
180 (*obj)->hdrlen = hdrlen;
185 const struct got_error *
186 got_object_read_header(struct got_object **obj, int fd)
188 const struct got_error *err;
189 struct got_inflate_buf zb;
191 const size_t zbsize = 64;
192 size_t outlen, totlen;
197 buf = malloc(zbsize);
199 return got_error_from_errno();
201 err = got_inflate_init(&zb, buf, zbsize);
207 err = got_inflate_read_fd(&zb, fd, &outlen);
213 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
216 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
217 if (newbuf == NULL) {
218 err = got_error_from_errno();
222 zb.outbuf = newbuf + totlen;
223 zb.outlen = (nbuf * zbsize) - totlen;
225 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
227 err = got_object_parse_header(obj, buf, totlen);
230 got_inflate_end(&zb);
234 struct got_commit_object *
235 got_object_commit_alloc_partial(void)
237 struct got_commit_object *commit;
239 commit = calloc(1, sizeof(*commit));
242 commit->tree_id = malloc(sizeof(*commit->tree_id));
243 if (commit->tree_id == NULL) {
248 SIMPLEQ_INIT(&commit->parent_ids);
253 const struct got_error *
254 got_object_commit_add_parent(struct got_commit_object *commit,
257 const struct got_error *err = NULL;
258 struct got_object_qid *qid;
260 err = got_object_qid_alloc_partial(&qid);
264 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
265 err = got_error(GOT_ERR_BAD_OBJ_DATA);
271 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
277 static const struct got_error *
278 parse_gmtoff(time_t *gmtoff, const char *tzstr)
281 const char *p = tzstr;
289 return got_error(GOT_ERR_BAD_OBJ_DATA);
291 if (!isdigit(*p) && !isdigit(*(p + 1)))
292 return got_error(GOT_ERR_BAD_OBJ_DATA);
293 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
296 if (!isdigit(*p) && !isdigit(*(p + 1)))
297 return got_error(GOT_ERR_BAD_OBJ_DATA);
298 m = ((*p - '0') * 10) + (*(p + 1) - '0');
300 *gmtoff = (h * 60 * 60 + m * 60) * sign;
304 static const struct got_error *
305 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
307 const struct got_error *err = NULL;
311 /* Parse and strip off trailing timezone indicator string. */
312 space = strrchr(committer, ' ');
314 return got_error(GOT_ERR_BAD_OBJ_DATA);
315 tzstr = strdup(space + 1);
317 return got_error_from_errno();
318 err = parse_gmtoff(gmtoff, tzstr);
324 /* Timestamp is separated from committer name + email by space. */
325 space = strrchr(committer, ' ');
327 return got_error(GOT_ERR_BAD_OBJ_DATA);
329 /* Timestamp parsed here is expressed in comitter's local time. */
330 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
332 return got_error(GOT_ERR_BAD_OBJ_DATA);
334 /* Express the time stamp in UTC. */
337 /* Strip off parsed time information, leaving just author and email. */
344 got_object_commit_close(struct got_commit_object *commit)
346 struct got_object_qid *qid;
348 if (commit->refcnt > 0) {
350 if (commit->refcnt > 0)
354 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
355 qid = SIMPLEQ_FIRST(&commit->parent_ids);
356 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
357 got_object_qid_free(qid);
360 free(commit->tree_id);
361 free(commit->author);
362 free(commit->committer);
363 free(commit->logmsg);
367 struct got_object_id *
368 got_object_commit_get_tree_id(struct got_commit_object *commit)
370 return commit->tree_id;
374 got_object_commit_get_nparents(struct got_commit_object *commit)
376 return commit->nparents;
379 const struct got_object_id_queue *
380 got_object_commit_get_parent_ids(struct got_commit_object *commit)
382 return &commit->parent_ids;
386 got_object_commit_get_author(struct got_commit_object *commit)
388 return commit->author;
392 got_object_commit_get_author_time(struct got_commit_object *commit)
394 return commit->author_time;
397 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
399 return commit->author_gmtoff;
403 got_object_commit_get_committer(struct got_commit_object *commit)
405 return commit->committer;
409 got_object_commit_get_committer_time(struct got_commit_object *commit)
411 return commit->committer_time;
415 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
417 return commit->committer_gmtoff;
421 got_object_commit_get_logmsg(struct got_commit_object *commit)
423 return commit->logmsg;
426 const struct got_error *
427 got_object_parse_commit(struct got_commit_object **commit, char *buf,
430 const struct got_error *err = NULL;
433 ssize_t remain = (ssize_t)len;
435 *commit = got_object_commit_alloc_partial();
437 return got_error_from_errno();
439 label_len = strlen(GOT_COMMIT_LABEL_TREE);
440 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
442 if (remain < SHA1_DIGEST_STRING_LENGTH) {
443 err = got_error(GOT_ERR_BAD_OBJ_DATA);
447 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
448 err = got_error(GOT_ERR_BAD_OBJ_DATA);
451 remain -= SHA1_DIGEST_STRING_LENGTH;
452 s += SHA1_DIGEST_STRING_LENGTH;
454 err = got_error(GOT_ERR_BAD_OBJ_DATA);
458 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
459 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
461 if (remain < SHA1_DIGEST_STRING_LENGTH) {
462 err = got_error(GOT_ERR_BAD_OBJ_DATA);
466 err = got_object_commit_add_parent(*commit, s);
470 remain -= SHA1_DIGEST_STRING_LENGTH;
471 s += SHA1_DIGEST_STRING_LENGTH;
474 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
475 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
481 err = got_error(GOT_ERR_BAD_OBJ_DATA);
485 p = memchr(s, '\n', remain);
487 err = got_error(GOT_ERR_BAD_OBJ_DATA);
492 err = parse_commit_time(&(*commit)->author_time,
493 &(*commit)->author_gmtoff, s);
496 (*commit)->author = strdup(s);
497 if ((*commit)->author == NULL) {
498 err = got_error_from_errno();
505 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
506 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
512 err = got_error(GOT_ERR_BAD_OBJ_DATA);
516 p = memchr(s, '\n', remain);
518 err = got_error(GOT_ERR_BAD_OBJ_DATA);
523 err = parse_commit_time(&(*commit)->committer_time,
524 &(*commit)->committer_gmtoff, s);
527 (*commit)->committer = strdup(s);
528 if ((*commit)->committer == NULL) {
529 err = got_error_from_errno();
536 (*commit)->logmsg = strndup(s, remain);
537 if ((*commit)->logmsg == NULL) {
538 err = got_error_from_errno();
543 got_object_commit_close(*commit);
550 got_object_tree_entry_close(struct got_tree_entry *te)
558 got_object_tree_entries_close(struct got_tree_entries *entries)
560 struct got_tree_entry *te;
562 while (!SIMPLEQ_EMPTY(&entries->head)) {
563 te = SIMPLEQ_FIRST(&entries->head);
564 SIMPLEQ_REMOVE_HEAD(&entries->head, entry);
565 got_object_tree_entry_close(te);
570 got_object_tree_close(struct got_tree_object *tree)
572 if (tree->refcnt > 0) {
574 if (tree->refcnt > 0)
578 got_object_tree_entries_close(&tree->entries);
582 struct got_tree_entry *
583 got_alloc_tree_entry_partial(void)
585 struct got_tree_entry *te;
587 te = malloc(sizeof(*te));
591 te->id = malloc(sizeof(*te->id));
592 if (te->id == NULL) {
599 static const struct got_error *
600 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
604 const struct got_error *err = NULL;
606 *te = got_alloc_tree_entry_partial();
608 return got_error_from_errno();
610 *elen = strnlen(buf, maxlen) + 1;
611 if (*elen > maxlen) {
614 return got_error(GOT_ERR_BAD_OBJ_DATA);
617 space = memchr(buf, ' ', *elen);
618 if (space == NULL || space <= buf) {
619 err = got_error(GOT_ERR_BAD_OBJ_DATA);
627 if (*p < '0' && *p > '7') {
628 err = got_error(GOT_ERR_BAD_OBJ_DATA);
632 (*te)->mode |= *p - '0';
636 (*te)->name = strdup(space + 1);
637 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
638 err = got_error(GOT_ERR_BAD_OBJ_DATA);
642 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
643 *elen += SHA1_DIGEST_LENGTH;
646 got_object_tree_entry_close(*te);
652 const struct got_error *
653 got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
655 const struct got_error *err;
657 struct got_pathlist_head pathlist;
658 struct got_pathlist_entry *pe;
660 TAILQ_INIT(&pathlist);
662 *tree = calloc(1, sizeof(**tree));
664 return got_error_from_errno();
666 SIMPLEQ_INIT(&(*tree)->entries.head);
669 struct got_tree_entry *te;
670 struct got_pathlist_entry *new = NULL;
673 err = parse_tree_entry(&te, &elen, buf, remain);
676 err = got_pathlist_insert(&new, &pathlist, te->name, te);
680 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
688 got_object_tree_close(*tree);
690 err = got_error(GOT_ERR_BAD_OBJ_DATA);
694 TAILQ_FOREACH(pe, &pathlist, entry) {
695 struct got_tree_entry *te = pe->data;
696 (*tree)->entries.nentries++;
697 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
700 got_pathlist_free(&pathlist);
705 got_object_tag_close(struct got_tag_object *tag)
713 const struct got_error *
714 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
716 const struct got_error *err = NULL;
721 *tag = calloc(1, sizeof(**tag));
723 return got_error_from_errno();
725 label_len = strlen(GOT_TAG_LABEL_OBJECT);
726 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
728 if (remain < SHA1_DIGEST_STRING_LENGTH) {
729 err = got_error(GOT_ERR_BAD_OBJ_DATA);
733 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
734 err = got_error(GOT_ERR_BAD_OBJ_DATA);
737 remain -= SHA1_DIGEST_STRING_LENGTH;
738 s += SHA1_DIGEST_STRING_LENGTH;
740 err = got_error(GOT_ERR_BAD_OBJ_DATA);
745 err = got_error(GOT_ERR_BAD_OBJ_DATA);
749 label_len = strlen(GOT_TAG_LABEL_TYPE);
750 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
753 err = got_error(GOT_ERR_BAD_OBJ_DATA);
757 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
758 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
759 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
760 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
763 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
764 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
765 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
766 label_len = strlen(GOT_OBJ_LABEL_TREE);
769 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
770 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
771 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
772 label_len = strlen(GOT_OBJ_LABEL_BLOB);
775 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
776 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
777 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
778 label_len = strlen(GOT_OBJ_LABEL_TAG);
782 err = got_error(GOT_ERR_BAD_OBJ_DATA);
786 if (remain <= 0 || *s != '\n') {
787 err = got_error(GOT_ERR_BAD_OBJ_DATA);
793 err = got_error(GOT_ERR_BAD_OBJ_DATA);
797 err = got_error(GOT_ERR_BAD_OBJ_DATA);
801 label_len = strlen(GOT_TAG_LABEL_TAG);
802 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
807 err = got_error(GOT_ERR_BAD_OBJ_DATA);
811 p = memchr(s, '\n', remain);
813 err = got_error(GOT_ERR_BAD_OBJ_DATA);
818 (*tag)->tag = strndup(s, slen);
819 if ((*tag)->tag == NULL) {
820 err = got_error_from_errno();
826 err = got_error(GOT_ERR_BAD_OBJ_DATA);
830 err = got_error(GOT_ERR_BAD_OBJ_DATA);
834 label_len = strlen(GOT_TAG_LABEL_TAGGER);
835 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
841 err = got_error(GOT_ERR_BAD_OBJ_DATA);
845 p = memchr(s, '\n', remain);
847 err = got_error(GOT_ERR_BAD_OBJ_DATA);
852 err = parse_commit_time(&(*tag)->tagger_time,
853 &(*tag)->tagger_gmtoff, s);
856 (*tag)->tagger = strdup(s);
857 if ((*tag)->tagger == NULL) {
858 err = got_error_from_errno();
864 err = got_error(GOT_ERR_BAD_OBJ_DATA);
868 /* Some old tags in the Linux git repo have no tagger. */
869 (*tag)->tagger = strdup("");
870 if ((*tag)->tagger == NULL) {
871 err = got_error_from_errno();
876 (*tag)->tagmsg = strndup(s, remain);
877 if ((*tag)->tagmsg == NULL) {
878 err = got_error_from_errno();
883 got_object_tag_close(*tag);
889 const struct got_error *
890 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
892 const struct got_error *err = NULL;
893 static const size_t blocksize = 512;
894 size_t n, total, remain;
900 buf = malloc(blocksize);
902 return got_error_from_errno();
909 newbuf = reallocarray(buf, 1, total + blocksize);
910 if (newbuf == NULL) {
911 err = got_error_from_errno();
917 n = fread(buf + total, 1, remain, f);
920 err = got_ferror(f, GOT_ERR_IO);