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);
97 const struct got_error *
98 got_object_id_str(char **outbuf, struct got_object_id *id)
100 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
102 *outbuf = malloc(len);
104 return got_error_from_errno("malloc");
106 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
109 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
116 got_object_close(struct got_object *obj)
118 if (obj->refcnt > 0) {
124 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
125 struct got_delta *delta;
126 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
127 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
128 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
136 got_object_qid_free(struct got_object_qid *qid)
143 got_object_id_queue_free(struct got_object_id_queue *ids)
145 struct got_object_qid *qid;
147 while (!SIMPLEQ_EMPTY(ids)) {
148 qid = SIMPLEQ_FIRST(ids);
149 SIMPLEQ_REMOVE_HEAD(ids, entry);
150 got_object_qid_free(qid);
154 const struct got_error *
155 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
157 const char *obj_labels[] = {
158 GOT_OBJ_LABEL_COMMIT,
163 const int obj_types[] = {
170 size_t size = 0, hdrlen = 0;
175 hdrlen = strnlen(buf, len) + 1 /* '\0' */;
177 return got_error(GOT_ERR_BAD_OBJ_HDR);
179 for (i = 0; i < nitems(obj_labels); i++) {
180 const char *label = obj_labels[i];
181 size_t label_len = strlen(label);
184 if (strncmp(buf, label, label_len) != 0)
188 if (len <= label_len)
189 return got_error(GOT_ERR_BAD_OBJ_HDR);
190 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
192 return got_error(GOT_ERR_BAD_OBJ_HDR);
197 return got_error(GOT_ERR_BAD_OBJ_HDR);
199 *obj = calloc(1, sizeof(**obj));
201 return got_error_from_errno("calloc");
203 (*obj)->hdrlen = hdrlen;
208 const struct got_error *
209 got_object_read_header(struct got_object **obj, int fd)
211 const struct got_error *err;
212 struct got_inflate_buf zb;
214 const size_t zbsize = 64;
215 size_t outlen, totlen;
220 buf = malloc(zbsize);
222 return got_error_from_errno("malloc");
224 err = got_inflate_init(&zb, buf, zbsize, NULL);
230 err = got_inflate_read_fd(&zb, fd, &outlen, NULL);
236 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
239 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
240 if (newbuf == NULL) {
241 err = got_error_from_errno("recallocarray");
245 zb.outbuf = newbuf + totlen;
246 zb.outlen = (nbuf * zbsize) - totlen;
248 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
250 err = got_object_parse_header(obj, buf, totlen);
253 got_inflate_end(&zb);
257 struct got_commit_object *
258 got_object_commit_alloc_partial(void)
260 struct got_commit_object *commit;
262 commit = calloc(1, sizeof(*commit));
265 commit->tree_id = malloc(sizeof(*commit->tree_id));
266 if (commit->tree_id == NULL) {
271 SIMPLEQ_INIT(&commit->parent_ids);
276 const struct got_error *
277 got_object_commit_add_parent(struct got_commit_object *commit,
280 const struct got_error *err = NULL;
281 struct got_object_qid *qid;
283 err = got_object_qid_alloc_partial(&qid);
287 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
288 err = got_error(GOT_ERR_BAD_OBJ_DATA);
289 got_object_qid_free(qid);
293 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
299 static const struct got_error *
300 parse_gmtoff(time_t *gmtoff, const char *tzstr)
303 const char *p = tzstr;
311 return got_error(GOT_ERR_BAD_OBJ_DATA);
313 if (!isdigit(*p) && !isdigit(*(p + 1)))
314 return got_error(GOT_ERR_BAD_OBJ_DATA);
315 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
318 if (!isdigit(*p) && !isdigit(*(p + 1)))
319 return got_error(GOT_ERR_BAD_OBJ_DATA);
320 m = ((*p - '0') * 10) + (*(p + 1) - '0');
322 *gmtoff = (h * 60 * 60 + m * 60) * sign;
326 static const struct got_error *
327 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
329 const struct got_error *err = NULL;
333 /* Parse and strip off trailing timezone indicator string. */
334 space = strrchr(committer, ' ');
336 return got_error(GOT_ERR_BAD_OBJ_DATA);
337 tzstr = strdup(space + 1);
339 return got_error_from_errno("strdup");
340 err = parse_gmtoff(gmtoff, tzstr);
343 if (err->code != GOT_ERR_BAD_OBJ_DATA)
345 /* Old versions of Git omitted the timestamp. */
352 /* Timestamp is separated from committer name + email by space. */
353 space = strrchr(committer, ' ');
355 return got_error(GOT_ERR_BAD_OBJ_DATA);
357 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
358 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
360 return got_error(GOT_ERR_BAD_OBJ_DATA);
362 /* Strip off parsed time information, leaving just author and email. */
369 got_object_commit_close(struct got_commit_object *commit)
371 if (commit->refcnt > 0) {
373 if (commit->refcnt > 0)
377 got_object_id_queue_free(&commit->parent_ids);
378 free(commit->tree_id);
379 free(commit->author);
380 free(commit->committer);
381 free(commit->logmsg);
385 struct got_object_id *
386 got_object_commit_get_tree_id(struct got_commit_object *commit)
388 return commit->tree_id;
392 got_object_commit_get_nparents(struct got_commit_object *commit)
394 return commit->nparents;
397 const struct got_object_id_queue *
398 got_object_commit_get_parent_ids(struct got_commit_object *commit)
400 return &commit->parent_ids;
404 got_object_commit_get_author(struct got_commit_object *commit)
406 return commit->author;
410 got_object_commit_get_author_time(struct got_commit_object *commit)
412 return commit->author_time;
415 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
417 return commit->author_gmtoff;
421 got_object_commit_get_committer(struct got_commit_object *commit)
423 return commit->committer;
427 got_object_commit_get_committer_time(struct got_commit_object *commit)
429 return commit->committer_time;
433 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
435 return commit->committer_gmtoff;
438 const struct got_error *
439 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
441 const struct got_error *err = NULL;
442 char *msg0, *msg, *line, *s;
448 msg0 = strdup(commit->logmsg);
450 return got_error_from_errno("strdup");
452 /* Copy log message line by line to strip out unusual headers... */
455 if ((line = strsep(&msg, "\n")) == NULL)
459 if (line[0] != '\0' &&
460 strncmp(line, GOT_COMMIT_LABEL_TREE,
461 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
462 strncmp(line, GOT_COMMIT_LABEL_AUTHOR,
463 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
464 strncmp(line, GOT_COMMIT_LABEL_PARENT,
465 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
466 strncmp(line, GOT_COMMIT_LABEL_COMMITTER,
467 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
474 if (asprintf(&s, "%s%s\n",
475 *logmsg ? *logmsg : "", line) == -1) {
476 err = got_error_from_errno("asprintf");
484 if (*logmsg == NULL) {
485 /* log message does not contain \n */
486 *logmsg = strdup(commit->logmsg);
487 if (*logmsg == NULL) {
488 err = got_error_from_errno("strdup");
493 /* Trim redundant trailing whitespace. */
494 len = strlen(*logmsg);
495 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
496 isspace((unsigned char)(*logmsg)[len - 1])) {
497 (*logmsg)[len - 1] = '\0';
510 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
512 return commit->logmsg;
515 const struct got_error *
516 got_object_parse_commit(struct got_commit_object **commit, char *buf,
519 const struct got_error *err = NULL;
522 ssize_t remain = (ssize_t)len;
525 return got_error(GOT_ERR_BAD_OBJ_DATA);
527 *commit = got_object_commit_alloc_partial();
529 return got_error_from_errno("got_object_commit_alloc_partial");
531 label_len = strlen(GOT_COMMIT_LABEL_TREE);
532 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
534 if (remain < SHA1_DIGEST_STRING_LENGTH) {
535 err = got_error(GOT_ERR_BAD_OBJ_DATA);
539 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
540 err = got_error(GOT_ERR_BAD_OBJ_DATA);
543 remain -= SHA1_DIGEST_STRING_LENGTH;
544 s += SHA1_DIGEST_STRING_LENGTH;
546 err = got_error(GOT_ERR_BAD_OBJ_DATA);
550 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
551 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
553 if (remain < SHA1_DIGEST_STRING_LENGTH) {
554 err = got_error(GOT_ERR_BAD_OBJ_DATA);
558 err = got_object_commit_add_parent(*commit, s);
562 remain -= SHA1_DIGEST_STRING_LENGTH;
563 s += SHA1_DIGEST_STRING_LENGTH;
566 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
567 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
573 err = got_error(GOT_ERR_BAD_OBJ_DATA);
577 p = memchr(s, '\n', remain);
579 err = got_error(GOT_ERR_BAD_OBJ_DATA);
584 err = parse_commit_time(&(*commit)->author_time,
585 &(*commit)->author_gmtoff, s);
588 (*commit)->author = strdup(s);
589 if ((*commit)->author == NULL) {
590 err = got_error_from_errno("strdup");
597 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
598 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
604 err = got_error(GOT_ERR_BAD_OBJ_DATA);
608 p = memchr(s, '\n', remain);
610 err = got_error(GOT_ERR_BAD_OBJ_DATA);
615 err = parse_commit_time(&(*commit)->committer_time,
616 &(*commit)->committer_gmtoff, s);
619 (*commit)->committer = strdup(s);
620 if ((*commit)->committer == NULL) {
621 err = got_error_from_errno("strdup");
628 (*commit)->logmsg = strndup(s, remain);
629 if ((*commit)->logmsg == NULL) {
630 err = got_error_from_errno("strndup");
635 got_object_commit_close(*commit);
642 got_object_tree_close(struct got_tree_object *tree)
644 if (tree->refcnt > 0) {
646 if (tree->refcnt > 0)
654 static const struct got_error *
655 parse_tree_entry(struct got_parsed_tree_entry **pte, const char **name,
656 size_t *elen, char *buf,
660 const struct got_error *err = NULL;
665 *pte = malloc(sizeof(**pte));
667 return got_error_from_errno("malloc");
669 *elen = strnlen(buf, maxlen) + 1;
670 if (*elen > maxlen) {
673 return got_error(GOT_ERR_BAD_OBJ_DATA);
676 space = memchr(buf, ' ', *elen);
677 if (space == NULL || space <= buf) {
678 err = got_error(GOT_ERR_BAD_OBJ_DATA);
686 if (*p < '0' && *p > '7') {
687 err = got_error(GOT_ERR_BAD_OBJ_DATA);
691 (*pte)->mode |= *p - '0';
695 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
696 err = got_error(GOT_ERR_BAD_OBJ_DATA);
702 *elen += SHA1_DIGEST_LENGTH;
711 const struct got_error *
712 got_object_parse_tree(struct got_pathlist_head *entries, int *nentries,
713 uint8_t *buf, size_t len)
715 const struct got_error *err = NULL;
720 return NULL; /* tree is empty */
723 struct got_parsed_tree_entry *pte;
724 struct got_pathlist_entry *new = NULL;
728 err = parse_tree_entry(&pte, &name, &elen, buf, remain);
731 err = got_pathlist_insert(&new, entries, name, pte);
735 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
744 err = got_error(GOT_ERR_BAD_OBJ_DATA);
749 got_object_parsed_tree_entries_free(entries);
756 got_object_parsed_tree_entries_free(struct got_pathlist_head *entries)
758 struct got_pathlist_entry *pe;
760 TAILQ_FOREACH(pe, entries, entry) {
761 struct got_parsed_tree_entry *pte = pe->data;
764 got_pathlist_free(entries);
768 got_object_tag_close(struct got_tag_object *tag)
770 if (tag->refcnt > 0) {
782 const struct got_error *
783 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
785 const struct got_error *err = NULL;
791 return got_error(GOT_ERR_BAD_OBJ_DATA);
793 *tag = calloc(1, sizeof(**tag));
795 return got_error_from_errno("calloc");
797 label_len = strlen(GOT_TAG_LABEL_OBJECT);
798 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
800 if (remain < SHA1_DIGEST_STRING_LENGTH) {
801 err = got_error(GOT_ERR_BAD_OBJ_DATA);
805 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
806 err = got_error(GOT_ERR_BAD_OBJ_DATA);
809 remain -= SHA1_DIGEST_STRING_LENGTH;
810 s += SHA1_DIGEST_STRING_LENGTH;
812 err = got_error(GOT_ERR_BAD_OBJ_DATA);
817 err = got_error(GOT_ERR_BAD_OBJ_DATA);
821 label_len = strlen(GOT_TAG_LABEL_TYPE);
822 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
825 err = got_error(GOT_ERR_BAD_OBJ_DATA);
829 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
830 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
831 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
832 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
835 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
836 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
837 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
838 label_len = strlen(GOT_OBJ_LABEL_TREE);
841 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
842 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
843 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
844 label_len = strlen(GOT_OBJ_LABEL_BLOB);
847 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
848 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
849 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
850 label_len = strlen(GOT_OBJ_LABEL_TAG);
854 err = got_error(GOT_ERR_BAD_OBJ_DATA);
858 if (remain <= 0 || *s != '\n') {
859 err = got_error(GOT_ERR_BAD_OBJ_DATA);
865 err = got_error(GOT_ERR_BAD_OBJ_DATA);
869 err = got_error(GOT_ERR_BAD_OBJ_DATA);
873 label_len = strlen(GOT_TAG_LABEL_TAG);
874 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
879 err = got_error(GOT_ERR_BAD_OBJ_DATA);
883 p = memchr(s, '\n', remain);
885 err = got_error(GOT_ERR_BAD_OBJ_DATA);
890 (*tag)->tag = strndup(s, slen);
891 if ((*tag)->tag == NULL) {
892 err = got_error_from_errno("strndup");
898 err = got_error(GOT_ERR_BAD_OBJ_DATA);
902 err = got_error(GOT_ERR_BAD_OBJ_DATA);
906 label_len = strlen(GOT_TAG_LABEL_TAGGER);
907 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
913 err = got_error(GOT_ERR_BAD_OBJ_DATA);
917 p = memchr(s, '\n', remain);
919 err = got_error(GOT_ERR_BAD_OBJ_DATA);
924 err = parse_commit_time(&(*tag)->tagger_time,
925 &(*tag)->tagger_gmtoff, s);
928 (*tag)->tagger = strdup(s);
929 if ((*tag)->tagger == NULL) {
930 err = got_error_from_errno("strdup");
936 err = got_error(GOT_ERR_BAD_OBJ_DATA);
940 /* Some old tags in the Linux git repo have no tagger. */
941 (*tag)->tagger = strdup("");
942 if ((*tag)->tagger == NULL) {
943 err = got_error_from_errno("strdup");
948 (*tag)->tagmsg = strndup(s, remain);
949 if ((*tag)->tagmsg == NULL) {
950 err = got_error_from_errno("strndup");
955 got_object_tag_close(*tag);
961 const struct got_error *
962 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
964 const struct got_error *err = NULL;
965 static const size_t blocksize = 512;
966 size_t n, total, remain;
972 buf = malloc(blocksize);
974 return got_error_from_errno("malloc");
981 newbuf = reallocarray(buf, 1, total + blocksize);
982 if (newbuf == NULL) {
983 err = got_error_from_errno("reallocarray");
989 n = fread(buf + total, 1, remain, f);
992 err = got_ferror(f, GOT_ERR_IO);