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>
35 #include "got_compat.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 (!STAILQ_EMPTY(&obj->deltas.entries)) {
127 delta = STAILQ_FIRST(&obj->deltas.entries);
128 STAILQ_REMOVE_HEAD(&obj->deltas.entries, entry);
135 const struct got_error *
136 got_object_raw_close(struct got_raw_object *obj)
138 const struct got_error *err = NULL;
140 if (obj->refcnt > 0) {
146 if (obj->f == NULL) {
148 if (munmap(obj->data, obj->hdrlen + obj->size) == -1)
149 err = got_error_from_errno("munmap");
150 if (close(obj->fd) == -1 && err == NULL)
151 err = got_error_from_errno("close");
155 if (fclose(obj->f) == EOF && err == NULL)
156 err = got_error_from_errno("fclose");
163 got_object_qid_free(struct got_object_qid *qid)
170 got_object_id_queue_free(struct got_object_id_queue *ids)
172 struct got_object_qid *qid;
174 while (!STAILQ_EMPTY(ids)) {
175 qid = STAILQ_FIRST(ids);
176 STAILQ_REMOVE_HEAD(ids, entry);
177 got_object_qid_free(qid);
181 const struct got_error *
182 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
184 const char *obj_labels[] = {
185 GOT_OBJ_LABEL_COMMIT,
190 const int obj_types[] = {
197 size_t size = 0, hdrlen = 0;
202 hdrlen = strnlen(buf, len) + 1 /* '\0' */;
204 return got_error(GOT_ERR_BAD_OBJ_HDR);
206 for (i = 0; i < nitems(obj_labels); i++) {
207 const char *label = obj_labels[i];
208 size_t label_len = strlen(label);
211 if (strncmp(buf, label, label_len) != 0)
215 if (len <= label_len)
216 return got_error(GOT_ERR_BAD_OBJ_HDR);
217 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
219 return got_error(GOT_ERR_BAD_OBJ_HDR);
224 return got_error(GOT_ERR_BAD_OBJ_HDR);
226 *obj = calloc(1, sizeof(**obj));
228 return got_error_from_errno("calloc");
230 (*obj)->hdrlen = hdrlen;
235 const struct got_error *
236 got_object_read_header(struct got_object **obj, int fd)
238 const struct got_error *err;
239 struct got_inflate_buf zb;
241 const size_t zbsize = 64;
242 size_t outlen, totlen;
247 buf = malloc(zbsize);
249 return got_error_from_errno("malloc");
251 err = got_inflate_init(&zb, buf, zbsize, NULL);
257 err = got_inflate_read_fd(&zb, fd, &outlen, NULL);
263 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
266 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
267 if (newbuf == NULL) {
268 err = got_error_from_errno("recallocarray");
272 zb.outbuf = newbuf + totlen;
273 zb.outlen = (nbuf * zbsize) - totlen;
275 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
277 err = got_object_parse_header(obj, buf, totlen);
280 got_inflate_end(&zb);
284 struct got_commit_object *
285 got_object_commit_alloc_partial(void)
287 struct got_commit_object *commit;
289 commit = calloc(1, sizeof(*commit));
292 commit->tree_id = malloc(sizeof(*commit->tree_id));
293 if (commit->tree_id == NULL) {
298 STAILQ_INIT(&commit->parent_ids);
303 const struct got_error *
304 got_object_commit_add_parent(struct got_commit_object *commit,
307 const struct got_error *err = NULL;
308 struct got_object_qid *qid;
310 err = got_object_qid_alloc_partial(&qid);
314 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
315 err = got_error(GOT_ERR_BAD_OBJ_DATA);
316 got_object_qid_free(qid);
320 STAILQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
326 static const struct got_error *
327 parse_gmtoff(time_t *gmtoff, const char *tzstr)
330 const char *p = tzstr;
338 return got_error(GOT_ERR_BAD_OBJ_DATA);
340 if (!isdigit(*p) && !isdigit(*(p + 1)))
341 return got_error(GOT_ERR_BAD_OBJ_DATA);
342 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
345 if (!isdigit(*p) && !isdigit(*(p + 1)))
346 return got_error(GOT_ERR_BAD_OBJ_DATA);
347 m = ((*p - '0') * 10) + (*(p + 1) - '0');
349 *gmtoff = (h * 60 * 60 + m * 60) * sign;
353 static const struct got_error *
354 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
356 const struct got_error *err = NULL;
360 /* Parse and strip off trailing timezone indicator string. */
361 space = strrchr(committer, ' ');
363 return got_error(GOT_ERR_BAD_OBJ_DATA);
364 tzstr = strdup(space + 1);
366 return got_error_from_errno("strdup");
367 err = parse_gmtoff(gmtoff, tzstr);
370 if (err->code != GOT_ERR_BAD_OBJ_DATA)
372 /* Old versions of Git omitted the timestamp. */
379 /* Timestamp is separated from committer name + email by space. */
380 space = strrchr(committer, ' ');
382 return got_error(GOT_ERR_BAD_OBJ_DATA);
384 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
385 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
387 return got_error(GOT_ERR_BAD_OBJ_DATA);
389 /* Strip off parsed time information, leaving just author and email. */
396 got_object_commit_close(struct got_commit_object *commit)
398 if (commit->refcnt > 0) {
400 if (commit->refcnt > 0)
404 got_object_id_queue_free(&commit->parent_ids);
405 free(commit->tree_id);
406 free(commit->author);
407 free(commit->committer);
408 free(commit->logmsg);
412 struct got_object_id *
413 got_object_commit_get_tree_id(struct got_commit_object *commit)
415 return commit->tree_id;
419 got_object_commit_get_nparents(struct got_commit_object *commit)
421 return commit->nparents;
424 const struct got_object_id_queue *
425 got_object_commit_get_parent_ids(struct got_commit_object *commit)
427 return &commit->parent_ids;
431 got_object_commit_get_author(struct got_commit_object *commit)
433 return commit->author;
437 got_object_commit_get_author_time(struct got_commit_object *commit)
439 return commit->author_time;
442 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
444 return commit->author_gmtoff;
448 got_object_commit_get_committer(struct got_commit_object *commit)
450 return commit->committer;
454 got_object_commit_get_committer_time(struct got_commit_object *commit)
456 return commit->committer_time;
460 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
462 return commit->committer_gmtoff;
465 const struct got_error *
466 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
468 const struct got_error *err = NULL;
473 len = strlen(commit->logmsg);
474 *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
476 return got_error_from_errno("malloc");
479 * Strip out unusual headers. Headers are separated from the commit
480 * message body by a single empty line.
482 src = commit->logmsg;
484 while (*src != '\0' && *src != '\n') {
485 int copy_header = 1, eol = 0;
486 if (strncmp(src, GOT_COMMIT_LABEL_TREE,
487 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
488 strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
489 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
490 strncmp(src, GOT_COMMIT_LABEL_PARENT,
491 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
492 strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
493 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
496 while (*src != '\0' && !eol) {
508 if (strlcat(*logmsg, src, len + 1) >= len + 1) {
509 err = got_error(GOT_ERR_NO_SPACE);
513 /* Trim redundant trailing whitespace. */
514 len = strlen(*logmsg);
515 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
516 isspace((unsigned char)(*logmsg)[len - 1])) {
517 (*logmsg)[len - 1] = '\0';
521 /* Append a trailing newline if missing. */
522 if (len > 0 && (*logmsg)[len - 1] != '\n') {
523 (*logmsg)[len] = '\n';
524 (*logmsg)[len + 1] = '\0';
535 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
537 return commit->logmsg;
540 const struct got_error *
541 got_object_parse_commit(struct got_commit_object **commit, char *buf,
544 const struct got_error *err = NULL;
547 ssize_t remain = (ssize_t)len;
550 return got_error(GOT_ERR_BAD_OBJ_DATA);
552 *commit = got_object_commit_alloc_partial();
554 return got_error_from_errno("got_object_commit_alloc_partial");
556 label_len = strlen(GOT_COMMIT_LABEL_TREE);
557 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
559 if (remain < SHA1_DIGEST_STRING_LENGTH) {
560 err = got_error(GOT_ERR_BAD_OBJ_DATA);
564 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
565 err = got_error(GOT_ERR_BAD_OBJ_DATA);
568 remain -= SHA1_DIGEST_STRING_LENGTH;
569 s += SHA1_DIGEST_STRING_LENGTH;
571 err = got_error(GOT_ERR_BAD_OBJ_DATA);
575 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
576 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
578 if (remain < SHA1_DIGEST_STRING_LENGTH) {
579 err = got_error(GOT_ERR_BAD_OBJ_DATA);
583 err = got_object_commit_add_parent(*commit, s);
587 remain -= SHA1_DIGEST_STRING_LENGTH;
588 s += SHA1_DIGEST_STRING_LENGTH;
591 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
592 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
598 err = got_error(GOT_ERR_BAD_OBJ_DATA);
602 p = memchr(s, '\n', remain);
604 err = got_error(GOT_ERR_BAD_OBJ_DATA);
609 err = parse_commit_time(&(*commit)->author_time,
610 &(*commit)->author_gmtoff, s);
613 (*commit)->author = strdup(s);
614 if ((*commit)->author == NULL) {
615 err = got_error_from_errno("strdup");
622 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
623 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
629 err = got_error(GOT_ERR_BAD_OBJ_DATA);
633 p = memchr(s, '\n', remain);
635 err = got_error(GOT_ERR_BAD_OBJ_DATA);
640 err = parse_commit_time(&(*commit)->committer_time,
641 &(*commit)->committer_gmtoff, s);
644 (*commit)->committer = strdup(s);
645 if ((*commit)->committer == NULL) {
646 err = got_error_from_errno("strdup");
653 (*commit)->logmsg = strndup(s, remain);
654 if ((*commit)->logmsg == NULL) {
655 err = got_error_from_errno("strndup");
660 got_object_commit_close(*commit);
667 got_object_tree_close(struct got_tree_object *tree)
669 if (tree->refcnt > 0) {
671 if (tree->refcnt > 0)
679 static const struct got_error *
680 parse_tree_entry(struct got_parsed_tree_entry **pte, const char **name,
681 size_t *elen, char *buf,
685 const struct got_error *err = NULL;
690 *pte = malloc(sizeof(**pte));
692 return got_error_from_errno("malloc");
694 *elen = strnlen(buf, maxlen) + 1;
695 if (*elen > maxlen) {
698 return got_error(GOT_ERR_BAD_OBJ_DATA);
701 space = memchr(buf, ' ', *elen);
702 if (space == NULL || space <= buf) {
703 err = got_error(GOT_ERR_BAD_OBJ_DATA);
711 if (*p < '0' && *p > '7') {
712 err = got_error(GOT_ERR_BAD_OBJ_DATA);
716 (*pte)->mode |= *p - '0';
720 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
721 err = got_error(GOT_ERR_BAD_OBJ_DATA);
727 *elen += SHA1_DIGEST_LENGTH;
736 const struct got_error *
737 got_object_parse_tree(struct got_pathlist_head *entries, int *nentries,
738 uint8_t *buf, size_t len)
740 const struct got_error *err = NULL;
745 return NULL; /* tree is empty */
748 struct got_parsed_tree_entry *pte;
749 struct got_pathlist_entry *new = NULL;
753 err = parse_tree_entry(&pte, &name, &elen, buf, remain);
756 err = got_pathlist_insert(&new, entries, name, pte);
760 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
769 err = got_error(GOT_ERR_BAD_OBJ_DATA);
774 got_object_parsed_tree_entries_free(entries);
781 got_object_parsed_tree_entries_free(struct got_pathlist_head *entries)
783 struct got_pathlist_entry *pe;
785 TAILQ_FOREACH(pe, entries, entry) {
786 struct got_parsed_tree_entry *pte = pe->data;
789 got_pathlist_free(entries);
793 got_object_tag_close(struct got_tag_object *tag)
795 if (tag->refcnt > 0) {
807 const struct got_error *
808 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
810 const struct got_error *err = NULL;
816 return got_error(GOT_ERR_BAD_OBJ_DATA);
818 *tag = calloc(1, sizeof(**tag));
820 return got_error_from_errno("calloc");
822 label_len = strlen(GOT_TAG_LABEL_OBJECT);
823 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
825 if (remain < SHA1_DIGEST_STRING_LENGTH) {
826 err = got_error(GOT_ERR_BAD_OBJ_DATA);
830 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
831 err = got_error(GOT_ERR_BAD_OBJ_DATA);
834 remain -= SHA1_DIGEST_STRING_LENGTH;
835 s += SHA1_DIGEST_STRING_LENGTH;
837 err = got_error(GOT_ERR_BAD_OBJ_DATA);
842 err = got_error(GOT_ERR_BAD_OBJ_DATA);
846 label_len = strlen(GOT_TAG_LABEL_TYPE);
847 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
850 err = got_error(GOT_ERR_BAD_OBJ_DATA);
854 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
855 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
856 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
857 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
860 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
861 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
862 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
863 label_len = strlen(GOT_OBJ_LABEL_TREE);
866 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
867 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
868 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
869 label_len = strlen(GOT_OBJ_LABEL_BLOB);
872 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
873 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
874 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
875 label_len = strlen(GOT_OBJ_LABEL_TAG);
879 err = got_error(GOT_ERR_BAD_OBJ_DATA);
883 if (remain <= 0 || *s != '\n') {
884 err = got_error(GOT_ERR_BAD_OBJ_DATA);
890 err = got_error(GOT_ERR_BAD_OBJ_DATA);
894 err = got_error(GOT_ERR_BAD_OBJ_DATA);
898 label_len = strlen(GOT_TAG_LABEL_TAG);
899 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
904 err = got_error(GOT_ERR_BAD_OBJ_DATA);
908 p = memchr(s, '\n', remain);
910 err = got_error(GOT_ERR_BAD_OBJ_DATA);
915 (*tag)->tag = strndup(s, slen);
916 if ((*tag)->tag == NULL) {
917 err = got_error_from_errno("strndup");
923 err = got_error(GOT_ERR_BAD_OBJ_DATA);
927 err = got_error(GOT_ERR_BAD_OBJ_DATA);
931 label_len = strlen(GOT_TAG_LABEL_TAGGER);
932 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
938 err = got_error(GOT_ERR_BAD_OBJ_DATA);
942 p = memchr(s, '\n', remain);
944 err = got_error(GOT_ERR_BAD_OBJ_DATA);
949 err = parse_commit_time(&(*tag)->tagger_time,
950 &(*tag)->tagger_gmtoff, s);
953 (*tag)->tagger = strdup(s);
954 if ((*tag)->tagger == NULL) {
955 err = got_error_from_errno("strdup");
961 err = got_error(GOT_ERR_BAD_OBJ_DATA);
965 /* Some old tags in the Linux git repo have no tagger. */
966 (*tag)->tagger = strdup("");
967 if ((*tag)->tagger == NULL) {
968 err = got_error_from_errno("strdup");
973 (*tag)->tagmsg = strndup(s, remain);
974 if ((*tag)->tagmsg == NULL) {
975 err = got_error_from_errno("strndup");
980 got_object_tag_close(*tag);
986 const struct got_error *
987 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
989 const struct got_error *err = NULL;
990 static const size_t blocksize = 512;
991 size_t n, total, remain;
997 buf = malloc(blocksize);
999 return got_error_from_errno("malloc");
1006 newbuf = reallocarray(buf, 1, total + blocksize);
1007 if (newbuf == NULL) {
1008 err = got_error_from_errno("reallocarray");
1012 remain += blocksize;
1014 n = fread(buf + total, 1, remain, f);
1017 err = got_ferror(f, GOT_ERR_IO);