2 * Copyright (c) 2018, 2019, 2020 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>
37 #include "got_error.h"
38 #include "got_object.h"
39 #include "got_repository.h"
40 #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_parse.h"
48 #include "got_lib_object_cache.h"
49 #include "got_lib_pack.h"
50 #include "got_lib_privsep.h"
51 #include "got_lib_repository.h"
54 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
57 struct got_object_id *
58 got_object_id_dup(struct got_object_id *id1)
60 struct got_object_id *id2;
62 id2 = malloc(sizeof(*id2));
65 memcpy(id2, id1, sizeof(*id2));
70 got_object_id_cmp(const struct got_object_id *id1,
71 const struct got_object_id *id2)
73 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
76 const struct got_error *
77 got_object_qid_alloc_partial(struct got_object_qid **qid)
79 const struct got_error *err = NULL;
81 *qid = malloc(sizeof(**qid));
83 return got_error_from_errno("malloc");
85 (*qid)->id = malloc(sizeof(*((*qid)->id)));
86 if ((*qid)->id == NULL) {
87 err = got_error_from_errno("malloc");
88 got_object_qid_free(*qid);
96 const struct got_error *
97 got_object_id_str(char **outbuf, struct got_object_id *id)
99 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
101 *outbuf = malloc(len);
103 return got_error_from_errno("malloc");
105 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
108 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
115 got_object_close(struct got_object *obj)
117 if (obj->refcnt > 0) {
123 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
124 struct got_delta *delta;
125 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
126 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
127 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
135 got_object_qid_free(struct got_object_qid *qid)
142 got_object_id_queue_free(struct got_object_id_queue *ids)
144 struct got_object_qid *qid;
146 while (!SIMPLEQ_EMPTY(ids)) {
147 qid = SIMPLEQ_FIRST(ids);
148 SIMPLEQ_REMOVE_HEAD(ids, entry);
149 got_object_qid_free(qid);
153 const struct got_error *
154 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
156 const char *obj_labels[] = {
157 GOT_OBJ_LABEL_COMMIT,
162 const int obj_types[] = {
169 size_t size = 0, hdrlen = 0;
174 hdrlen = strnlen(buf, len) + 1 /* '\0' */;
176 return got_error(GOT_ERR_BAD_OBJ_HDR);
178 for (i = 0; i < nitems(obj_labels); i++) {
179 const char *label = obj_labels[i];
180 size_t label_len = strlen(label);
183 if (strncmp(buf, label, label_len) != 0)
187 if (len <= label_len)
188 return got_error(GOT_ERR_BAD_OBJ_HDR);
189 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
191 return got_error(GOT_ERR_BAD_OBJ_HDR);
196 return got_error(GOT_ERR_BAD_OBJ_HDR);
198 *obj = calloc(1, sizeof(**obj));
200 return got_error_from_errno("calloc");
202 (*obj)->hdrlen = hdrlen;
207 const struct got_error *
208 got_object_read_header(struct got_object **obj, int fd)
210 const struct got_error *err;
211 struct got_inflate_buf zb;
213 const size_t zbsize = 64;
214 size_t outlen, totlen;
219 buf = malloc(zbsize);
221 return got_error_from_errno("malloc");
223 err = got_inflate_init(&zb, buf, zbsize, NULL);
229 err = got_inflate_read_fd(&zb, fd, &outlen, NULL);
235 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
238 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
239 if (newbuf == NULL) {
240 err = got_error_from_errno("recallocarray");
244 zb.outbuf = newbuf + totlen;
245 zb.outlen = (nbuf * zbsize) - totlen;
247 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
249 err = got_object_parse_header(obj, buf, totlen);
252 got_inflate_end(&zb);
256 struct got_commit_object *
257 got_object_commit_alloc_partial(void)
259 struct got_commit_object *commit;
261 commit = calloc(1, sizeof(*commit));
264 commit->tree_id = malloc(sizeof(*commit->tree_id));
265 if (commit->tree_id == NULL) {
270 SIMPLEQ_INIT(&commit->parent_ids);
275 const struct got_error *
276 got_object_commit_add_parent(struct got_commit_object *commit,
279 const struct got_error *err = NULL;
280 struct got_object_qid *qid;
282 err = got_object_qid_alloc_partial(&qid);
286 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
287 err = got_error(GOT_ERR_BAD_OBJ_DATA);
288 got_object_qid_free(qid);
292 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
298 static const struct got_error *
299 parse_gmtoff(time_t *gmtoff, const char *tzstr)
302 const char *p = tzstr;
310 return got_error(GOT_ERR_BAD_OBJ_DATA);
312 if (!isdigit(*p) && !isdigit(*(p + 1)))
313 return got_error(GOT_ERR_BAD_OBJ_DATA);
314 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
317 if (!isdigit(*p) && !isdigit(*(p + 1)))
318 return got_error(GOT_ERR_BAD_OBJ_DATA);
319 m = ((*p - '0') * 10) + (*(p + 1) - '0');
321 *gmtoff = (h * 60 * 60 + m * 60) * sign;
325 static const struct got_error *
326 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
328 const struct got_error *err = NULL;
332 /* Parse and strip off trailing timezone indicator string. */
333 space = strrchr(committer, ' ');
335 return got_error(GOT_ERR_BAD_OBJ_DATA);
336 tzstr = strdup(space + 1);
338 return got_error_from_errno("strdup");
339 err = parse_gmtoff(gmtoff, tzstr);
342 if (err->code != GOT_ERR_BAD_OBJ_DATA)
344 /* Old versions of Git omitted the timestamp. */
351 /* Timestamp is separated from committer name + email by space. */
352 space = strrchr(committer, ' ');
354 return got_error(GOT_ERR_BAD_OBJ_DATA);
356 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
357 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
359 return got_error(GOT_ERR_BAD_OBJ_DATA);
361 /* Strip off parsed time information, leaving just author and email. */
368 got_object_commit_close(struct got_commit_object *commit)
370 if (commit->refcnt > 0) {
372 if (commit->refcnt > 0)
376 got_object_id_queue_free(&commit->parent_ids);
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;
437 const struct got_error *
438 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
440 const struct got_error *err = NULL;
441 char *msg0, *msg, *line, *s;
447 msg0 = strdup(commit->logmsg);
449 return got_error_from_errno("strdup");
451 /* Copy log message line by line to strip out unusual headers... */
454 if ((line = strsep(&msg, "\n")) == NULL)
458 if (line[0] != '\0' &&
459 strncmp(line, GOT_COMMIT_LABEL_TREE,
460 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
461 strncmp(line, GOT_COMMIT_LABEL_AUTHOR,
462 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
463 strncmp(line, GOT_COMMIT_LABEL_PARENT,
464 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
465 strncmp(line, GOT_COMMIT_LABEL_COMMITTER,
466 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
473 if (asprintf(&s, "%s%s\n",
474 *logmsg ? *logmsg : "", line) == -1) {
475 err = got_error_from_errno("asprintf");
483 if (*logmsg == NULL) {
484 /* log message does not contain \n */
485 *logmsg = strdup(commit->logmsg);
486 if (*logmsg == NULL) {
487 err = got_error_from_errno("strdup");
492 /* Trim redundant trailing whitespace. */
493 len = strlen(*logmsg);
494 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
495 isspace((unsigned char)(*logmsg)[len - 1])) {
496 (*logmsg)[len - 1] = '\0';
509 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
511 return commit->logmsg;
514 const struct got_error *
515 got_object_parse_commit(struct got_commit_object **commit, char *buf,
518 const struct got_error *err = NULL;
521 ssize_t remain = (ssize_t)len;
524 return got_error(GOT_ERR_BAD_OBJ_DATA);
526 *commit = got_object_commit_alloc_partial();
528 return got_error_from_errno("got_object_commit_alloc_partial");
530 label_len = strlen(GOT_COMMIT_LABEL_TREE);
531 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
533 if (remain < SHA1_DIGEST_STRING_LENGTH) {
534 err = got_error(GOT_ERR_BAD_OBJ_DATA);
538 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
539 err = got_error(GOT_ERR_BAD_OBJ_DATA);
542 remain -= SHA1_DIGEST_STRING_LENGTH;
543 s += SHA1_DIGEST_STRING_LENGTH;
545 err = got_error(GOT_ERR_BAD_OBJ_DATA);
549 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
550 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
552 if (remain < SHA1_DIGEST_STRING_LENGTH) {
553 err = got_error(GOT_ERR_BAD_OBJ_DATA);
557 err = got_object_commit_add_parent(*commit, s);
561 remain -= SHA1_DIGEST_STRING_LENGTH;
562 s += SHA1_DIGEST_STRING_LENGTH;
565 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
566 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
572 err = got_error(GOT_ERR_BAD_OBJ_DATA);
576 p = memchr(s, '\n', remain);
578 err = got_error(GOT_ERR_BAD_OBJ_DATA);
583 err = parse_commit_time(&(*commit)->author_time,
584 &(*commit)->author_gmtoff, s);
587 (*commit)->author = strdup(s);
588 if ((*commit)->author == NULL) {
589 err = got_error_from_errno("strdup");
596 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
597 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
603 err = got_error(GOT_ERR_BAD_OBJ_DATA);
607 p = memchr(s, '\n', remain);
609 err = got_error(GOT_ERR_BAD_OBJ_DATA);
614 err = parse_commit_time(&(*commit)->committer_time,
615 &(*commit)->committer_gmtoff, s);
618 (*commit)->committer = strdup(s);
619 if ((*commit)->committer == NULL) {
620 err = got_error_from_errno("strdup");
627 (*commit)->logmsg = strndup(s, remain);
628 if ((*commit)->logmsg == NULL) {
629 err = got_error_from_errno("strndup");
634 got_object_commit_close(*commit);
641 got_object_tree_close(struct got_tree_object *tree)
643 if (tree->refcnt > 0) {
645 if (tree->refcnt > 0)
653 static const struct got_error *
654 parse_tree_entry(struct got_parsed_tree_entry **pte, const char **name,
655 size_t *elen, char *buf,
659 const struct got_error *err = NULL;
664 *pte = malloc(sizeof(**pte));
666 return got_error_from_errno("malloc");
668 *elen = strnlen(buf, maxlen) + 1;
669 if (*elen > maxlen) {
672 return got_error(GOT_ERR_BAD_OBJ_DATA);
675 space = memchr(buf, ' ', *elen);
676 if (space == NULL || space <= buf) {
677 err = got_error(GOT_ERR_BAD_OBJ_DATA);
685 if (*p < '0' && *p > '7') {
686 err = got_error(GOT_ERR_BAD_OBJ_DATA);
690 (*pte)->mode |= *p - '0';
694 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
695 err = got_error(GOT_ERR_BAD_OBJ_DATA);
701 *elen += SHA1_DIGEST_LENGTH;
710 const struct got_error *
711 got_object_parse_tree(struct got_pathlist_head *entries, int *nentries,
712 uint8_t *buf, size_t len)
714 const struct got_error *err = NULL;
719 return NULL; /* tree is empty */
722 struct got_parsed_tree_entry *pte;
723 struct got_pathlist_entry *new = NULL;
727 err = parse_tree_entry(&pte, &name, &elen, buf, remain);
730 err = got_pathlist_insert(&new, entries, name, pte);
734 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
743 err = got_error(GOT_ERR_BAD_OBJ_DATA);
748 got_object_parsed_tree_entries_free(entries);
755 got_object_parsed_tree_entries_free(struct got_pathlist_head *entries)
757 struct got_pathlist_entry *pe;
759 TAILQ_FOREACH(pe, entries, entry) {
760 struct got_parsed_tree_entry *pte = pe->data;
763 got_pathlist_free(entries);
767 got_object_tag_close(struct got_tag_object *tag)
769 if (tag->refcnt > 0) {
781 const struct got_error *
782 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
784 const struct got_error *err = NULL;
790 return got_error(GOT_ERR_BAD_OBJ_DATA);
792 *tag = calloc(1, sizeof(**tag));
794 return got_error_from_errno("calloc");
796 label_len = strlen(GOT_TAG_LABEL_OBJECT);
797 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
799 if (remain < SHA1_DIGEST_STRING_LENGTH) {
800 err = got_error(GOT_ERR_BAD_OBJ_DATA);
804 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
805 err = got_error(GOT_ERR_BAD_OBJ_DATA);
808 remain -= SHA1_DIGEST_STRING_LENGTH;
809 s += SHA1_DIGEST_STRING_LENGTH;
811 err = got_error(GOT_ERR_BAD_OBJ_DATA);
816 err = got_error(GOT_ERR_BAD_OBJ_DATA);
820 label_len = strlen(GOT_TAG_LABEL_TYPE);
821 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
824 err = got_error(GOT_ERR_BAD_OBJ_DATA);
828 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
829 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
830 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
831 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
834 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
835 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
836 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
837 label_len = strlen(GOT_OBJ_LABEL_TREE);
840 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
841 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
842 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
843 label_len = strlen(GOT_OBJ_LABEL_BLOB);
846 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
847 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
848 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
849 label_len = strlen(GOT_OBJ_LABEL_TAG);
853 err = got_error(GOT_ERR_BAD_OBJ_DATA);
857 if (remain <= 0 || *s != '\n') {
858 err = got_error(GOT_ERR_BAD_OBJ_DATA);
864 err = got_error(GOT_ERR_BAD_OBJ_DATA);
868 err = got_error(GOT_ERR_BAD_OBJ_DATA);
872 label_len = strlen(GOT_TAG_LABEL_TAG);
873 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
878 err = got_error(GOT_ERR_BAD_OBJ_DATA);
882 p = memchr(s, '\n', remain);
884 err = got_error(GOT_ERR_BAD_OBJ_DATA);
889 (*tag)->tag = strndup(s, slen);
890 if ((*tag)->tag == NULL) {
891 err = got_error_from_errno("strndup");
897 err = got_error(GOT_ERR_BAD_OBJ_DATA);
901 err = got_error(GOT_ERR_BAD_OBJ_DATA);
905 label_len = strlen(GOT_TAG_LABEL_TAGGER);
906 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
912 err = got_error(GOT_ERR_BAD_OBJ_DATA);
916 p = memchr(s, '\n', remain);
918 err = got_error(GOT_ERR_BAD_OBJ_DATA);
923 err = parse_commit_time(&(*tag)->tagger_time,
924 &(*tag)->tagger_gmtoff, s);
927 (*tag)->tagger = strdup(s);
928 if ((*tag)->tagger == NULL) {
929 err = got_error_from_errno("strdup");
935 err = got_error(GOT_ERR_BAD_OBJ_DATA);
939 /* Some old tags in the Linux git repo have no tagger. */
940 (*tag)->tagger = strdup("");
941 if ((*tag)->tagger == NULL) {
942 err = got_error_from_errno("strdup");
947 (*tag)->tagmsg = strndup(s, remain);
948 if ((*tag)->tagmsg == NULL) {
949 err = got_error_from_errno("strndup");
954 got_object_tag_close(*tag);
960 const struct got_error *
961 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
963 const struct got_error *err = NULL;
964 static const size_t blocksize = 512;
965 size_t n, total, remain;
971 buf = malloc(blocksize);
973 return got_error_from_errno("malloc");
980 newbuf = reallocarray(buf, 1, total + blocksize);
981 if (newbuf == NULL) {
982 err = got_error_from_errno("reallocarray");
988 n = fread(buf + total, 1, remain, f);
991 err = got_ferror(f, GOT_ERR_IO);