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>
22 #include <sys/socket.h>
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_repository.h"
42 #include "got_opentemp.h"
45 #include "got_lib_sha1.h"
46 #include "got_lib_delta.h"
47 #include "got_lib_inflate.h"
48 #include "got_lib_object.h"
49 #include "got_lib_object_parse.h"
50 #include "got_lib_object_cache.h"
51 #include "got_lib_pack.h"
52 #include "got_lib_privsep.h"
53 #include "got_lib_repository.h"
56 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
59 struct got_object_id *
60 got_object_id_dup(struct got_object_id *id1)
62 struct got_object_id *id2;
64 id2 = malloc(sizeof(*id2));
67 memcpy(id2, id1, sizeof(*id2));
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("malloc");
87 (*qid)->id = malloc(sizeof(*((*qid)->id)));
88 if ((*qid)->id == NULL) {
89 err = got_error_from_errno("malloc");
90 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("malloc");
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 (!STAILQ_EMPTY(&obj->deltas.entries)) {
129 delta = STAILQ_FIRST(&obj->deltas.entries);
130 STAILQ_REMOVE_HEAD(&obj->deltas.entries, entry);
137 const struct got_error *
138 got_object_raw_close(struct got_raw_object *obj)
140 const struct got_error *err = NULL;
142 if (obj->refcnt > 0) {
148 if (obj->f == NULL) {
150 if (munmap(obj->data, obj->hdrlen + obj->size) == -1)
151 err = got_error_from_errno("munmap");
152 if (close(obj->fd) == -1 && err == NULL)
153 err = got_error_from_errno("close");
157 if (fclose(obj->f) == EOF && err == NULL)
158 err = got_error_from_errno("fclose");
165 got_object_qid_free(struct got_object_qid *qid)
172 got_object_id_queue_free(struct got_object_id_queue *ids)
174 struct got_object_qid *qid;
176 while (!STAILQ_EMPTY(ids)) {
177 qid = STAILQ_FIRST(ids);
178 STAILQ_REMOVE_HEAD(ids, entry);
179 got_object_qid_free(qid);
183 const struct got_error *
184 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
186 const char *obj_labels[] = {
187 GOT_OBJ_LABEL_COMMIT,
192 const int obj_types[] = {
199 size_t size = 0, hdrlen = 0;
204 hdrlen = strnlen(buf, len) + 1 /* '\0' */;
206 return got_error(GOT_ERR_BAD_OBJ_HDR);
208 for (i = 0; i < nitems(obj_labels); i++) {
209 const char *label = obj_labels[i];
210 size_t label_len = strlen(label);
213 if (strncmp(buf, label, label_len) != 0)
217 if (len <= label_len)
218 return got_error(GOT_ERR_BAD_OBJ_HDR);
219 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
221 return got_error(GOT_ERR_BAD_OBJ_HDR);
226 return got_error(GOT_ERR_BAD_OBJ_HDR);
228 *obj = calloc(1, sizeof(**obj));
230 return got_error_from_errno("calloc");
232 (*obj)->hdrlen = hdrlen;
237 const struct got_error *
238 got_object_read_header(struct got_object **obj, int fd)
240 const struct got_error *err;
241 struct got_inflate_buf zb;
243 const size_t zbsize = 64;
244 size_t outlen, totlen;
249 buf = malloc(zbsize);
251 return got_error_from_errno("malloc");
253 err = got_inflate_init(&zb, buf, zbsize, NULL);
259 err = got_inflate_read_fd(&zb, fd, &outlen, NULL);
265 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
268 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
269 if (newbuf == NULL) {
270 err = got_error_from_errno("recallocarray");
274 zb.outbuf = newbuf + totlen;
275 zb.outlen = (nbuf * zbsize) - totlen;
277 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
279 err = got_object_parse_header(obj, buf, totlen);
282 got_inflate_end(&zb);
286 struct got_commit_object *
287 got_object_commit_alloc_partial(void)
289 struct got_commit_object *commit;
291 commit = calloc(1, sizeof(*commit));
294 commit->tree_id = malloc(sizeof(*commit->tree_id));
295 if (commit->tree_id == NULL) {
300 STAILQ_INIT(&commit->parent_ids);
305 const struct got_error *
306 got_object_commit_add_parent(struct got_commit_object *commit,
309 const struct got_error *err = NULL;
310 struct got_object_qid *qid;
312 err = got_object_qid_alloc_partial(&qid);
316 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
317 err = got_error(GOT_ERR_BAD_OBJ_DATA);
318 got_object_qid_free(qid);
322 STAILQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
328 static const struct got_error *
329 parse_gmtoff(time_t *gmtoff, const char *tzstr)
332 const char *p = tzstr;
340 return got_error(GOT_ERR_BAD_OBJ_DATA);
342 if (!isdigit(*p) && !isdigit(*(p + 1)))
343 return got_error(GOT_ERR_BAD_OBJ_DATA);
344 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
347 if (!isdigit(*p) && !isdigit(*(p + 1)))
348 return got_error(GOT_ERR_BAD_OBJ_DATA);
349 m = ((*p - '0') * 10) + (*(p + 1) - '0');
351 *gmtoff = (h * 60 * 60 + m * 60) * sign;
355 static const struct got_error *
356 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
358 const struct got_error *err = NULL;
362 /* Parse and strip off trailing timezone indicator string. */
363 space = strrchr(committer, ' ');
365 return got_error(GOT_ERR_BAD_OBJ_DATA);
366 tzstr = strdup(space + 1);
368 return got_error_from_errno("strdup");
369 err = parse_gmtoff(gmtoff, tzstr);
372 if (err->code != GOT_ERR_BAD_OBJ_DATA)
374 /* Old versions of Git omitted the timestamp. */
381 /* Timestamp is separated from committer name + email by space. */
382 space = strrchr(committer, ' ');
384 return got_error(GOT_ERR_BAD_OBJ_DATA);
386 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
387 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
389 return got_error(GOT_ERR_BAD_OBJ_DATA);
391 /* Strip off parsed time information, leaving just author and email. */
398 got_object_commit_close(struct got_commit_object *commit)
400 if (commit->refcnt > 0) {
402 if (commit->refcnt > 0)
406 got_object_id_queue_free(&commit->parent_ids);
407 free(commit->tree_id);
408 free(commit->author);
409 free(commit->committer);
410 free(commit->logmsg);
414 struct got_object_id *
415 got_object_commit_get_tree_id(struct got_commit_object *commit)
417 return commit->tree_id;
421 got_object_commit_get_nparents(struct got_commit_object *commit)
423 return commit->nparents;
426 const struct got_object_id_queue *
427 got_object_commit_get_parent_ids(struct got_commit_object *commit)
429 return &commit->parent_ids;
433 got_object_commit_get_author(struct got_commit_object *commit)
435 return commit->author;
439 got_object_commit_get_author_time(struct got_commit_object *commit)
441 return commit->author_time;
444 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
446 return commit->author_gmtoff;
450 got_object_commit_get_committer(struct got_commit_object *commit)
452 return commit->committer;
456 got_object_commit_get_committer_time(struct got_commit_object *commit)
458 return commit->committer_time;
462 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
464 return commit->committer_gmtoff;
467 const struct got_error *
468 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
470 const struct got_error *err = NULL;
475 len = strlen(commit->logmsg);
476 *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
478 return got_error_from_errno("malloc");
481 * Strip out unusual headers. Headers are separated from the commit
482 * message body by a single empty line.
484 src = commit->logmsg;
486 while (*src != '\0' && *src != '\n') {
487 int copy_header = 1, eol = 0;
488 if (strncmp(src, GOT_COMMIT_LABEL_TREE,
489 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
490 strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
491 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
492 strncmp(src, GOT_COMMIT_LABEL_PARENT,
493 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
494 strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
495 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
498 while (*src != '\0' && !eol) {
510 if (strlcat(*logmsg, src, len + 1) >= len + 1) {
511 err = got_error(GOT_ERR_NO_SPACE);
515 /* Trim redundant trailing whitespace. */
516 len = strlen(*logmsg);
517 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
518 isspace((unsigned char)(*logmsg)[len - 1])) {
519 (*logmsg)[len - 1] = '\0';
523 /* Append a trailing newline if missing. */
524 if (len > 0 && (*logmsg)[len - 1] != '\n') {
525 (*logmsg)[len] = '\n';
526 (*logmsg)[len + 1] = '\0';
537 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
539 return commit->logmsg;
542 const struct got_error *
543 got_object_parse_commit(struct got_commit_object **commit, char *buf,
546 const struct got_error *err = NULL;
549 ssize_t remain = (ssize_t)len;
552 return got_error(GOT_ERR_BAD_OBJ_DATA);
554 *commit = got_object_commit_alloc_partial();
556 return got_error_from_errno("got_object_commit_alloc_partial");
558 label_len = strlen(GOT_COMMIT_LABEL_TREE);
559 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
561 if (remain < SHA1_DIGEST_STRING_LENGTH) {
562 err = got_error(GOT_ERR_BAD_OBJ_DATA);
566 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
567 err = got_error(GOT_ERR_BAD_OBJ_DATA);
570 remain -= SHA1_DIGEST_STRING_LENGTH;
571 s += SHA1_DIGEST_STRING_LENGTH;
573 err = got_error(GOT_ERR_BAD_OBJ_DATA);
577 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
578 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
580 if (remain < SHA1_DIGEST_STRING_LENGTH) {
581 err = got_error(GOT_ERR_BAD_OBJ_DATA);
585 err = got_object_commit_add_parent(*commit, s);
589 remain -= SHA1_DIGEST_STRING_LENGTH;
590 s += SHA1_DIGEST_STRING_LENGTH;
593 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
594 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
600 err = got_error(GOT_ERR_BAD_OBJ_DATA);
604 p = memchr(s, '\n', remain);
606 err = got_error(GOT_ERR_BAD_OBJ_DATA);
611 err = parse_commit_time(&(*commit)->author_time,
612 &(*commit)->author_gmtoff, s);
615 (*commit)->author = strdup(s);
616 if ((*commit)->author == NULL) {
617 err = got_error_from_errno("strdup");
624 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
625 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
631 err = got_error(GOT_ERR_BAD_OBJ_DATA);
635 p = memchr(s, '\n', remain);
637 err = got_error(GOT_ERR_BAD_OBJ_DATA);
642 err = parse_commit_time(&(*commit)->committer_time,
643 &(*commit)->committer_gmtoff, s);
646 (*commit)->committer = strdup(s);
647 if ((*commit)->committer == NULL) {
648 err = got_error_from_errno("strdup");
655 (*commit)->logmsg = strndup(s, remain);
656 if ((*commit)->logmsg == NULL) {
657 err = got_error_from_errno("strndup");
662 got_object_commit_close(*commit);
669 got_object_tree_close(struct got_tree_object *tree)
671 if (tree->refcnt > 0) {
673 if (tree->refcnt > 0)
681 static const struct got_error *
682 parse_tree_entry(struct got_parsed_tree_entry **pte, const char **name,
683 size_t *elen, char *buf,
687 const struct got_error *err = NULL;
692 *pte = malloc(sizeof(**pte));
694 return got_error_from_errno("malloc");
696 *elen = strnlen(buf, maxlen) + 1;
697 if (*elen > maxlen) {
700 return got_error(GOT_ERR_BAD_OBJ_DATA);
703 space = memchr(buf, ' ', *elen);
704 if (space == NULL || space <= buf) {
705 err = got_error(GOT_ERR_BAD_OBJ_DATA);
713 if (*p < '0' && *p > '7') {
714 err = got_error(GOT_ERR_BAD_OBJ_DATA);
718 (*pte)->mode |= *p - '0';
722 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
723 err = got_error(GOT_ERR_BAD_OBJ_DATA);
729 *elen += SHA1_DIGEST_LENGTH;
738 const struct got_error *
739 got_object_parse_tree(struct got_pathlist_head *entries, int *nentries,
740 uint8_t *buf, size_t len)
742 const struct got_error *err = NULL;
747 return NULL; /* tree is empty */
750 struct got_parsed_tree_entry *pte;
751 struct got_pathlist_entry *new = NULL;
755 err = parse_tree_entry(&pte, &name, &elen, buf, remain);
758 err = got_pathlist_insert(&new, entries, name, pte);
762 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
771 err = got_error(GOT_ERR_BAD_OBJ_DATA);
776 got_object_parsed_tree_entries_free(entries);
783 got_object_parsed_tree_entries_free(struct got_pathlist_head *entries)
785 struct got_pathlist_entry *pe;
787 TAILQ_FOREACH(pe, entries, entry) {
788 struct got_parsed_tree_entry *pte = pe->data;
791 got_pathlist_free(entries);
795 got_object_tag_close(struct got_tag_object *tag)
797 if (tag->refcnt > 0) {
809 const struct got_error *
810 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
812 const struct got_error *err = NULL;
818 return got_error(GOT_ERR_BAD_OBJ_DATA);
820 *tag = calloc(1, sizeof(**tag));
822 return got_error_from_errno("calloc");
824 label_len = strlen(GOT_TAG_LABEL_OBJECT);
825 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
827 if (remain < SHA1_DIGEST_STRING_LENGTH) {
828 err = got_error(GOT_ERR_BAD_OBJ_DATA);
832 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
833 err = got_error(GOT_ERR_BAD_OBJ_DATA);
836 remain -= SHA1_DIGEST_STRING_LENGTH;
837 s += SHA1_DIGEST_STRING_LENGTH;
839 err = got_error(GOT_ERR_BAD_OBJ_DATA);
844 err = got_error(GOT_ERR_BAD_OBJ_DATA);
848 label_len = strlen(GOT_TAG_LABEL_TYPE);
849 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
852 err = got_error(GOT_ERR_BAD_OBJ_DATA);
856 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
857 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
858 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
859 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
862 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
863 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
864 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
865 label_len = strlen(GOT_OBJ_LABEL_TREE);
868 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
869 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
870 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
871 label_len = strlen(GOT_OBJ_LABEL_BLOB);
874 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
875 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
876 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
877 label_len = strlen(GOT_OBJ_LABEL_TAG);
881 err = got_error(GOT_ERR_BAD_OBJ_DATA);
885 if (remain <= 0 || *s != '\n') {
886 err = got_error(GOT_ERR_BAD_OBJ_DATA);
892 err = got_error(GOT_ERR_BAD_OBJ_DATA);
896 err = got_error(GOT_ERR_BAD_OBJ_DATA);
900 label_len = strlen(GOT_TAG_LABEL_TAG);
901 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
906 err = got_error(GOT_ERR_BAD_OBJ_DATA);
910 p = memchr(s, '\n', remain);
912 err = got_error(GOT_ERR_BAD_OBJ_DATA);
917 (*tag)->tag = strndup(s, slen);
918 if ((*tag)->tag == NULL) {
919 err = got_error_from_errno("strndup");
925 err = got_error(GOT_ERR_BAD_OBJ_DATA);
929 err = got_error(GOT_ERR_BAD_OBJ_DATA);
933 label_len = strlen(GOT_TAG_LABEL_TAGGER);
934 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
940 err = got_error(GOT_ERR_BAD_OBJ_DATA);
944 p = memchr(s, '\n', remain);
946 err = got_error(GOT_ERR_BAD_OBJ_DATA);
951 err = parse_commit_time(&(*tag)->tagger_time,
952 &(*tag)->tagger_gmtoff, s);
955 (*tag)->tagger = strdup(s);
956 if ((*tag)->tagger == NULL) {
957 err = got_error_from_errno("strdup");
963 err = got_error(GOT_ERR_BAD_OBJ_DATA);
967 /* Some old tags in the Linux git repo have no tagger. */
968 (*tag)->tagger = strdup("");
969 if ((*tag)->tagger == NULL) {
970 err = got_error_from_errno("strdup");
975 (*tag)->tagmsg = strndup(s, remain);
976 if ((*tag)->tagmsg == NULL) {
977 err = got_error_from_errno("strndup");
982 got_object_tag_close(*tag);
988 const struct got_error *
989 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
991 const struct got_error *err = NULL;
992 static const size_t blocksize = 512;
993 size_t n, total, remain;
999 buf = malloc(blocksize);
1001 return got_error_from_errno("malloc");
1008 newbuf = reallocarray(buf, 1, total + blocksize);
1009 if (newbuf == NULL) {
1010 err = got_error_from_errno("reallocarray");
1014 remain += blocksize;
1016 n = fread(buf + total, 1, remain, f);
1019 err = got_ferror(f, GOT_ERR_IO);