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>
20 #include <sys/socket.h>
34 #include "got_compat.h"
36 #include "got_error.h"
37 #include "got_object.h"
38 #include "got_repository.h"
39 #include "got_opentemp.h"
42 #include "got_lib_sha1.h"
43 #include "got_lib_delta.h"
44 #include "got_lib_inflate.h"
45 #include "got_lib_object.h"
46 #include "got_lib_object_parse.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"
53 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
56 struct got_object_id *
57 got_object_id_dup(struct got_object_id *id1)
59 struct got_object_id *id2;
61 id2 = malloc(sizeof(*id2));
64 memcpy(id2, id1, sizeof(*id2));
69 got_object_id_cmp(const struct got_object_id *id1,
70 const struct got_object_id *id2)
72 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
75 const struct got_error *
76 got_object_qid_alloc_partial(struct got_object_qid **qid)
78 const struct got_error *err = NULL;
80 *qid = malloc(sizeof(**qid));
82 return got_error_from_errno("malloc");
84 (*qid)->id = malloc(sizeof(*((*qid)->id)));
85 if ((*qid)->id == NULL) {
86 err = got_error_from_errno("malloc");
87 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 (!STAILQ_EMPTY(&obj->deltas.entries)) {
126 delta = STAILQ_FIRST(&obj->deltas.entries);
127 STAILQ_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 (!STAILQ_EMPTY(ids)) {
147 qid = STAILQ_FIRST(ids);
148 STAILQ_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 STAILQ_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 STAILQ_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;
445 len = strlen(commit->logmsg);
446 *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
448 return got_error_from_errno("malloc");
451 * Strip out unusual headers. Headers are separated from the commit
452 * message body by a single empty line.
454 src = commit->logmsg;
456 while (*src != '\0' && *src != '\n') {
457 int copy_header = 1, eol = 0;
458 if (strncmp(src, GOT_COMMIT_LABEL_TREE,
459 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
460 strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
461 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
462 strncmp(src, GOT_COMMIT_LABEL_PARENT,
463 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
464 strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
465 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
468 while (*src != '\0' && !eol) {
480 if (strlcat(*logmsg, src, len + 1) >= len + 1) {
481 err = got_error(GOT_ERR_NO_SPACE);
485 /* Trim redundant trailing whitespace. */
486 len = strlen(*logmsg);
487 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
488 isspace((unsigned char)(*logmsg)[len - 1])) {
489 (*logmsg)[len - 1] = '\0';
493 /* Append a trailing newline if missing. */
494 if (len > 0 && (*logmsg)[len - 1] != '\n') {
495 (*logmsg)[len] = '\n';
496 (*logmsg)[len + 1] = '\0';
507 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
509 return commit->logmsg;
512 const struct got_error *
513 got_object_parse_commit(struct got_commit_object **commit, char *buf,
516 const struct got_error *err = NULL;
519 ssize_t remain = (ssize_t)len;
522 return got_error(GOT_ERR_BAD_OBJ_DATA);
524 *commit = got_object_commit_alloc_partial();
526 return got_error_from_errno("got_object_commit_alloc_partial");
528 label_len = strlen(GOT_COMMIT_LABEL_TREE);
529 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
531 if (remain < SHA1_DIGEST_STRING_LENGTH) {
532 err = got_error(GOT_ERR_BAD_OBJ_DATA);
536 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
537 err = got_error(GOT_ERR_BAD_OBJ_DATA);
540 remain -= SHA1_DIGEST_STRING_LENGTH;
541 s += SHA1_DIGEST_STRING_LENGTH;
543 err = got_error(GOT_ERR_BAD_OBJ_DATA);
547 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
548 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
550 if (remain < SHA1_DIGEST_STRING_LENGTH) {
551 err = got_error(GOT_ERR_BAD_OBJ_DATA);
555 err = got_object_commit_add_parent(*commit, s);
559 remain -= SHA1_DIGEST_STRING_LENGTH;
560 s += SHA1_DIGEST_STRING_LENGTH;
563 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
564 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
570 err = got_error(GOT_ERR_BAD_OBJ_DATA);
574 p = memchr(s, '\n', remain);
576 err = got_error(GOT_ERR_BAD_OBJ_DATA);
581 err = parse_commit_time(&(*commit)->author_time,
582 &(*commit)->author_gmtoff, s);
585 (*commit)->author = strdup(s);
586 if ((*commit)->author == NULL) {
587 err = got_error_from_errno("strdup");
594 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
595 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
601 err = got_error(GOT_ERR_BAD_OBJ_DATA);
605 p = memchr(s, '\n', remain);
607 err = got_error(GOT_ERR_BAD_OBJ_DATA);
612 err = parse_commit_time(&(*commit)->committer_time,
613 &(*commit)->committer_gmtoff, s);
616 (*commit)->committer = strdup(s);
617 if ((*commit)->committer == NULL) {
618 err = got_error_from_errno("strdup");
625 (*commit)->logmsg = strndup(s, remain);
626 if ((*commit)->logmsg == NULL) {
627 err = got_error_from_errno("strndup");
632 got_object_commit_close(*commit);
639 got_object_tree_close(struct got_tree_object *tree)
641 if (tree->refcnt > 0) {
643 if (tree->refcnt > 0)
651 static const struct got_error *
652 parse_tree_entry(struct got_parsed_tree_entry **pte, const char **name,
653 size_t *elen, char *buf,
657 const struct got_error *err = NULL;
662 *pte = malloc(sizeof(**pte));
664 return got_error_from_errno("malloc");
666 *elen = strnlen(buf, maxlen) + 1;
667 if (*elen > maxlen) {
670 return got_error(GOT_ERR_BAD_OBJ_DATA);
673 space = memchr(buf, ' ', *elen);
674 if (space == NULL || space <= buf) {
675 err = got_error(GOT_ERR_BAD_OBJ_DATA);
683 if (*p < '0' && *p > '7') {
684 err = got_error(GOT_ERR_BAD_OBJ_DATA);
688 (*pte)->mode |= *p - '0';
692 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
693 err = got_error(GOT_ERR_BAD_OBJ_DATA);
699 *elen += SHA1_DIGEST_LENGTH;
708 const struct got_error *
709 got_object_parse_tree(struct got_pathlist_head *entries, int *nentries,
710 uint8_t *buf, size_t len)
712 const struct got_error *err = NULL;
717 return NULL; /* tree is empty */
720 struct got_parsed_tree_entry *pte;
721 struct got_pathlist_entry *new = NULL;
725 err = parse_tree_entry(&pte, &name, &elen, buf, remain);
728 err = got_pathlist_insert(&new, entries, name, pte);
732 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
741 err = got_error(GOT_ERR_BAD_OBJ_DATA);
746 got_object_parsed_tree_entries_free(entries);
753 got_object_parsed_tree_entries_free(struct got_pathlist_head *entries)
755 struct got_pathlist_entry *pe;
757 TAILQ_FOREACH(pe, entries, entry) {
758 struct got_parsed_tree_entry *pte = pe->data;
761 got_pathlist_free(entries);
765 got_object_tag_close(struct got_tag_object *tag)
767 if (tag->refcnt > 0) {
779 const struct got_error *
780 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
782 const struct got_error *err = NULL;
788 return got_error(GOT_ERR_BAD_OBJ_DATA);
790 *tag = calloc(1, sizeof(**tag));
792 return got_error_from_errno("calloc");
794 label_len = strlen(GOT_TAG_LABEL_OBJECT);
795 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
797 if (remain < SHA1_DIGEST_STRING_LENGTH) {
798 err = got_error(GOT_ERR_BAD_OBJ_DATA);
802 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
803 err = got_error(GOT_ERR_BAD_OBJ_DATA);
806 remain -= SHA1_DIGEST_STRING_LENGTH;
807 s += SHA1_DIGEST_STRING_LENGTH;
809 err = got_error(GOT_ERR_BAD_OBJ_DATA);
814 err = got_error(GOT_ERR_BAD_OBJ_DATA);
818 label_len = strlen(GOT_TAG_LABEL_TYPE);
819 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
822 err = got_error(GOT_ERR_BAD_OBJ_DATA);
826 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
827 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
828 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
829 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
832 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
833 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
834 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
835 label_len = strlen(GOT_OBJ_LABEL_TREE);
838 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
839 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
840 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
841 label_len = strlen(GOT_OBJ_LABEL_BLOB);
844 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
845 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
846 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
847 label_len = strlen(GOT_OBJ_LABEL_TAG);
851 err = got_error(GOT_ERR_BAD_OBJ_DATA);
855 if (remain <= 0 || *s != '\n') {
856 err = got_error(GOT_ERR_BAD_OBJ_DATA);
862 err = got_error(GOT_ERR_BAD_OBJ_DATA);
866 err = got_error(GOT_ERR_BAD_OBJ_DATA);
870 label_len = strlen(GOT_TAG_LABEL_TAG);
871 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
876 err = got_error(GOT_ERR_BAD_OBJ_DATA);
880 p = memchr(s, '\n', remain);
882 err = got_error(GOT_ERR_BAD_OBJ_DATA);
887 (*tag)->tag = strndup(s, slen);
888 if ((*tag)->tag == NULL) {
889 err = got_error_from_errno("strndup");
895 err = got_error(GOT_ERR_BAD_OBJ_DATA);
899 err = got_error(GOT_ERR_BAD_OBJ_DATA);
903 label_len = strlen(GOT_TAG_LABEL_TAGGER);
904 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
910 err = got_error(GOT_ERR_BAD_OBJ_DATA);
914 p = memchr(s, '\n', remain);
916 err = got_error(GOT_ERR_BAD_OBJ_DATA);
921 err = parse_commit_time(&(*tag)->tagger_time,
922 &(*tag)->tagger_gmtoff, s);
925 (*tag)->tagger = strdup(s);
926 if ((*tag)->tagger == NULL) {
927 err = got_error_from_errno("strdup");
933 err = got_error(GOT_ERR_BAD_OBJ_DATA);
937 /* Some old tags in the Linux git repo have no tagger. */
938 (*tag)->tagger = strdup("");
939 if ((*tag)->tagger == NULL) {
940 err = got_error_from_errno("strdup");
945 (*tag)->tagmsg = strndup(s, remain);
946 if ((*tag)->tagmsg == NULL) {
947 err = got_error_from_errno("strndup");
952 got_object_tag_close(*tag);
958 const struct got_error *
959 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
961 const struct got_error *err = NULL;
962 static const size_t blocksize = 512;
963 size_t n, total, remain;
969 buf = malloc(blocksize);
971 return got_error_from_errno("malloc");
978 newbuf = reallocarray(buf, 1, total + blocksize);
979 if (newbuf == NULL) {
980 err = got_error_from_errno("reallocarray");
986 n = fread(buf + total, 1, remain, f);
989 err = got_ferror(f, GOT_ERR_IO);